Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 85 additions & 52 deletions src/sparql/QueryObjectBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class QueryObjectBuilder {
public constructor() {
this.queryObject = {
queryType: 'SELECT',
distinct: true,
variables: [],
where: [],
type: 'query',
Expand All @@ -25,48 +26,6 @@ export default class QueryObjectBuilder {
},
];

if ( !queryRepresentation.omitLabels ) {
this.queryObject.variables.push(
{
termType: 'Variable',
value: 'itemLabel',
} );

if ( !this.queryObject.where ) {
this.queryObject.where = [];
}

this.queryObject.where.push(
{
type: 'service',
patterns: [
{
type: 'bgp',
triples: [ {
subject: {
termType: 'NamedNode',
value: rdfNamespaces.bd + 'serviceParam',
},
predicate: {
termType: 'NamedNode',
value: rdfNamespaces.wikibase + 'language',
},
object: {
termType: 'Literal',
value: '[AUTO_LANGUAGE]',
},
} ],
},
],
name: {
termType: 'NamedNode',
value: rdfNamespaces.wikibase + 'label',
},
silent: false,
},
);
}

for ( let i = 0; i < queryRepresentation.conditions.length; i++ ) {
this.buildFromQueryCondition( queryRepresentation.conditions[ i ] );
}
Expand All @@ -76,7 +35,7 @@ export default class QueryObjectBuilder {
let isOnlyNegateQuery = true;
for ( let i = 0; i < this.queryObject.where?.length; i++ ) {
const type = this.queryObject.where[ i ].type;
if ( type !== 'minus' && type !== 'service' ) {
if ( type !== 'minus' ) {
isOnlyNegateQuery = false;
break;
}
Expand Down Expand Up @@ -111,6 +70,10 @@ export default class QueryObjectBuilder {
this.queryObject.limit = queryRepresentation.limit;
}

if ( !queryRepresentation.omitLabels ) {
return this.wrapQueryWithLabel();
}

return this.queryObject;
}

Expand Down Expand Up @@ -214,21 +177,91 @@ export default class QueryObjectBuilder {

if ( condition.propertyValueRelation === PropertyValueRelation.NotMatching ) {
const filterCondition = {
type: 'filter',
expression: {
type: 'operation',
operator: '!=',
args: [
type: 'minus',
patterns: [ {
type: 'bgp',
triples: [
{
termType: 'Variable',
value: 'instance',
subject: {
termType: 'Variable',
value: 'item',
},
predicate: { type: 'path',
pathType: '/',
items: [ {
termType: 'NamedNode',
value: rdfNamespaces.p + condition.propertyId,
},
{
termType: 'NamedNode',
value: rdfNamespaces.ps + condition.propertyId,
} ] },
object: this.buildTripleObjectForExplicitValue( condition.datatype, condition.value ),
},
this.buildTripleObjectForExplicitValue( condition.datatype, condition.value ),
],
},
} ],
};

this.queryObject.where.push( filterCondition as Pattern );
}
}

private wrapQueryWithLabel(): SelectQuery {
const wrapperQuery: SelectQuery = {
queryType: 'SELECT',
distinct: true,
variables: [
{
termType: 'Variable',
value: 'item',
},
{
termType: 'Variable',
value: 'itemLabel',
},
],
where: [
{
type: 'service',
patterns: [
{
type: 'bgp',
triples: [ {
subject: {
termType: 'NamedNode',
value: rdfNamespaces.bd + 'serviceParam',
},
predicate: {
termType: 'NamedNode',
value: rdfNamespaces.wikibase + 'language',
},
object: {
termType: 'Literal',
value: '[AUTO_LANGUAGE]',
},
} ],
},
],
name: {
termType: 'NamedNode',
value: rdfNamespaces.wikibase + 'label',
},
silent: false,
},
],
type: 'query',
prefixes: rdfNamespaces,
};

if ( !wrapperQuery.where ) {
wrapperQuery.where = [];
}
this.queryObject.prefixes = {};
wrapperQuery.where.push( {
type: 'group',
patterns: [ this.queryObject ],
} );

return wrapperQuery;
}
}
154 changes: 99 additions & 55 deletions tests/unit/sparql/QueryObjectBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe( 'QueryObjectBuilder', () => {
const builder = new QueryObjectBuilder();
const expected = {
queryType: 'SELECT',
distinct: true,
variables: [
{
termType: 'Variable',
Expand Down Expand Up @@ -68,6 +69,7 @@ describe( 'QueryObjectBuilder', () => {
const builder = new QueryObjectBuilder();
const expected = {
queryType: 'SELECT',
distinct: true,
variables: [
{
termType: 'Variable',
Expand Down Expand Up @@ -128,8 +130,49 @@ describe( 'QueryObjectBuilder', () => {
it( 'with labels (omitLabels = false)', () => {
const prefixes = allNamespaces;
const builder = new QueryObjectBuilder();
const internalExpectedQuery = {
queryType: 'SELECT',
distinct: true,
variables: [
{
termType: 'Variable',
value: 'item',
},
],
where: [
{
type: 'bgp',
triples: [
{
subject: {
termType: 'Variable',
value: 'item',
},
predicate: { type: 'path',
pathType: '/',
items: [ {
termType: 'NamedNode',
value: 'http://www.wikidata.org/prop/P281',
},
{
termType: 'NamedNode',
value: 'http://www.wikidata.org/prop/statement/P281',
},
] },
object: {
termType: 'Literal',
value: 'XXXX',
},
},
],
},
],
type: 'query',
};

const expected = {
queryType: 'SELECT',
distinct: true,
variables: [
{
termType: 'Variable',
Expand Down Expand Up @@ -169,29 +212,9 @@ describe( 'QueryObjectBuilder', () => {
silent: false,
},
{
type: 'bgp',
triples: [
{
subject: {
termType: 'Variable',
value: 'item',
},
predicate: { type: 'path',
pathType: '/',
items: [ {
termType: 'NamedNode',
value: 'http://www.wikidata.org/prop/P281',
},
{
termType: 'NamedNode',
value: 'http://www.wikidata.org/prop/statement/P281',
},
] },
object: {
termType: 'Literal',
value: 'XXXX',
},
},
type: 'group',
patterns: [
internalExpectedQuery,
],
},
],
Expand Down Expand Up @@ -221,6 +244,7 @@ describe( 'QueryObjectBuilder', () => {
const builder = new QueryObjectBuilder();
const expected = {
queryType: 'SELECT',
distinct: true,
variables: [
{
termType: 'Variable',
Expand Down Expand Up @@ -290,6 +314,7 @@ describe( 'QueryObjectBuilder', () => {
const builder = new QueryObjectBuilder();
const expected = {
queryType: 'SELECT',
distinct: true,
variables: [
{
termType: 'Variable',
Expand Down Expand Up @@ -372,46 +397,16 @@ describe( 'QueryObjectBuilder', () => {
it( 'with negate but labels enabled', () => {
const prefixes = allNamespaces;
const builder = new QueryObjectBuilder();
const expected = {
const internalExpectedQuery = {
queryType: 'SELECT',
distinct: true,
variables: [
{
termType: 'Variable',
value: 'item',
},
{
termType: 'Variable',
value: 'itemLabel',
},
],
where: [
{
type: 'service',
patterns: [
{
type: 'bgp',
triples: [ {
subject: {
termType: 'NamedNode',
value: 'http://www.bigdata.com/rdf#serviceParam',
},
predicate: {
termType: 'NamedNode',
value: 'http://wikiba.se/ontology#language',
},
object: {
termType: 'Literal',
value: '[AUTO_LANGUAGE]',
},
} ],
},
],
name: {
termType: 'NamedNode',
value: 'http://wikiba.se/ontology#label',
},
silent: false,
},
{
type: 'minus',
patterns: [
Expand Down Expand Up @@ -464,6 +459,55 @@ describe( 'QueryObjectBuilder', () => {
},
],
type: 'query',
prefixes: {},
};
const expected = {
queryType: 'SELECT',
distinct: true,
variables: [
{
termType: 'Variable',
value: 'item',
},
{
termType: 'Variable',
value: 'itemLabel',
},
],
where: [
{
type: 'service',
patterns: [
{
type: 'bgp',
triples: [ {
subject: {
termType: 'NamedNode',
value: 'http://www.bigdata.com/rdf#serviceParam',
},
predicate: {
termType: 'NamedNode',
value: 'http://wikiba.se/ontology#language',
},
object: {
termType: 'Literal',
value: '[AUTO_LANGUAGE]',
},
} ],
},
],
name: {
termType: 'NamedNode',
value: 'http://wikiba.se/ontology#label',
},
silent: false,
},
{
type: 'group',
patterns: [ internalExpectedQuery ],
},
],
type: 'query',
prefixes: prefixes,
};

Expand Down
Loading