Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
modify the queryRepresentation and generated query to show subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
guergana committed Jan 11, 2021
1 parent 103aeff commit 9175db1
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/sparql/QueryObjectBuilder.ts
@@ -1,6 +1,6 @@
import rdfNamespaces from '@/sparql/rdfNamespaces';
import QueryRepresentation, { Condition } from '@/sparql/QueryRepresentation';
import { Pattern, SelectQuery, Term } from 'sparqljs';
import { IriTerm, Pattern, PropertyPath, SelectQuery, Term } from 'sparqljs';
import PropertyValueRelation from '@/data-model/PropertyValueRelation';

export default class QueryObjectBuilder {
Expand Down Expand Up @@ -117,6 +117,33 @@ export default class QueryObjectBuilder {
}
}

private buildTriplePredicateItems( condition: Condition ): ( PropertyPath|IriTerm )[] {
const items: ( PropertyPath|IriTerm )[] = [ {
termType: 'NamedNode',
value: rdfNamespaces.p + condition.propertyId,
},
{
termType: 'NamedNode',
value: rdfNamespaces.ps + condition.propertyId,
} ];

if ( condition.subclasses ) {
items.push(
{
type: 'path',
pathType: '*',
items: [ {
termType: 'NamedNode',
value: rdfNamespaces.wdt + process.env.VUE_APP_SUBCLASS_PROPERTY,
},
],
},
);
}

return items;
}

private buildFromQueryCondition( condition: Condition ): void {
const tripleObject: Term = this.buildTripleObject( condition );

Expand All @@ -135,15 +162,7 @@ export default class QueryObjectBuilder {
},
predicate: { type: 'path',
pathType: '/',
items: [ {
termType: 'NamedNode',
value: rdfNamespaces.p + condition.propertyId,
},
{
termType: 'NamedNode',
value: rdfNamespaces.ps + condition.propertyId,
},
] },
items: this.buildTriplePredicateItems( condition ) },
object: tripleObject,
},
],
Expand Down
1 change: 1 addition & 0 deletions src/sparql/QueryRepresentation.ts
Expand Up @@ -5,6 +5,7 @@ export type Condition = {
value: string;
datatype: string; // FIXME: make enum?
propertyValueRelation: PropertyValueRelation;
subclasses: boolean;
};

export default interface QueryRepresentation {
Expand Down
1 change: 1 addition & 0 deletions src/store/getters.ts
Expand Up @@ -14,6 +14,7 @@ export default {
value: condition.valueData.value,
propertyValueRelation: condition.propertyValueRelationData.value,
datatype: condition.propertyData.datatype!,
subclasses: condition.subclasses,
};
} ),
...rootState.useLimit && { limit: rootState.limit },
Expand Down
71 changes: 71 additions & 0 deletions tests/unit/sparql/QueryObjectBuilder.spec.ts
Expand Up @@ -53,6 +53,7 @@ describe( 'QueryObjectBuilder', () => {
value: 'XXXX',
datatype: 'string',
propertyValueRelation: PropertyValueRelation.Matching,
subclasses: false,
},
],
omitLabels: true,
Expand Down Expand Up @@ -112,6 +113,7 @@ describe( 'QueryObjectBuilder', () => {
value: 'XXXX',
propertyValueRelation: PropertyValueRelation.Matching,
datatype: 'string',
subclasses: false,
},
],
omitLabels: true,
Expand Down Expand Up @@ -202,6 +204,7 @@ describe( 'QueryObjectBuilder', () => {
value: 'XXXX',
datatype: 'string',
propertyValueRelation: PropertyValueRelation.Matching,
subclasses: false,
},
],
omitLabels: false,
Expand All @@ -210,4 +213,72 @@ describe( 'QueryObjectBuilder', () => {
expect( actual ).toMatchObject( expected );
} );

it( 'with subclasses', () => {
const prefixes = allNamespaces;
const builder = new QueryObjectBuilder();
const expected = {
queryType: 'SELECT',
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',
},
{
type: 'path',
pathType: '*',
items: [ {
termType: 'NamedNode',
value: 'http://www.wikidata.org/prop/direct/P279',
},
],
},
] },
object: {
termType: 'Literal',
value: 'XXXX',
},
},
],
},
],
type: 'query',
prefixes: prefixes,
};

const actual = builder.buildFromQueryRepresentation( {
conditions: [
{
propertyId: 'P281',
value: 'XXXX',
datatype: 'string',
propertyValueRelation: PropertyValueRelation.Matching,
subclasses: true,
},
],
omitLabels: true,
} );

expect( actual ).toStrictEqual( expected );
} );

} );
35 changes: 35 additions & 0 deletions tests/unit/sparql/buildQuery.spec.ts
Expand Up @@ -13,6 +13,7 @@ describe( 'buildQuery', () => {
value,
datatype: 'string',
propertyValueRelation,
subclasses: false,
},
],
omitLabels: true,
Expand All @@ -36,6 +37,7 @@ describe( 'buildQuery', () => {
value,
datatype: 'string',
propertyValueRelation,
subclasses: false,
},
], omitLabels: true } );

Expand All @@ -52,6 +54,7 @@ describe( 'buildQuery', () => {
value: '',
datatype: 'string',
propertyValueRelation,
subclasses: false,
},
],
omitLabels: true,
Expand All @@ -70,12 +73,14 @@ describe( 'buildQuery', () => {
value: 'blah',
datatype: 'string',
propertyValueRelation: PropertyValueRelation.Matching,
subclasses: false,
},
{
propertyId: 'P66',
value: '',
datatype: 'string',
propertyValueRelation: PropertyValueRelation.Regardless,
subclasses: false,
},
], omitLabels: true } );

Expand All @@ -95,12 +100,14 @@ describe( 'buildQuery', () => {
value: 'blah',
datatype: 'string',
propertyValueRelation: PropertyValueRelation.Matching,
subclasses: false,
},
{
propertyId: 'P66',
value: 'foo',
datatype: 'string',
propertyValueRelation: PropertyValueRelation.NotMatching,
subclasses: false,
},
], omitLabels: true } );

Expand All @@ -120,12 +127,14 @@ describe( 'buildQuery', () => {
value: 'blah',
datatype: 'string',
propertyValueRelation: PropertyValueRelation.NotMatching,
subclasses: false,
},
{
propertyId: 'P66',
value: 'foo',
datatype: 'string',
propertyValueRelation: PropertyValueRelation.Matching,
subclasses: false,
},
], omitLabels: true } );

Expand All @@ -143,6 +152,7 @@ describe( 'buildQuery', () => {
value,
propertyValueRelation,
datatype,
subclasses: false,
},
], omitLabels: true,
} ) ).toEqual( `SELECT ?item WHERE { ?item (p:${propertyId}/ps:${propertyId}) wd:${value}. }` );
Expand All @@ -162,10 +172,35 @@ describe( 'buildQuery', () => {
value,
datatype: 'string',
propertyValueRelation,
subclasses: false,
},
],
omitLabels: false,
} );
expect( actualQuery.replace( /\s+/g, ' ' ) ).toEqual( expectedQuery.replace( /\s+/g, ' ' ) );
} );

it( 'builds a query from a property and a string value with subclasses', () => {
const propertyId = 'P666';
const value = 'blah';
const subclassesId = 'P279';
const propertyValueRelation = PropertyValueRelation.Matching;
const expectedQuery =
`SELECT ?item WHERE { ?item (p:${propertyId}/ps:${propertyId}/(wdt:${subclassesId}*)) "${value}". }`;

const actualQuery = buildQuery( { conditions: [
{
propertyId,
value,
datatype: 'string',
propertyValueRelation,
subclasses: true,
},
],
omitLabels: true,
} );

expect( actualQuery.replace( /\s+/g, ' ' ) ).toEqual( expectedQuery.replace( /\s+/g, ' ' ) );
} );

} );
34 changes: 34 additions & 0 deletions tests/unit/store/getters.spec.ts
Expand Up @@ -89,6 +89,7 @@ describe( 'getters', () => {
value: 'foo',
propertyValueRelation: PropertyValueRelation.Matching,
datatype: 'string',
subclasses: false,
},
],
omitLabels: true,
Expand Down Expand Up @@ -119,6 +120,7 @@ describe( 'getters', () => {
value: 'foo',
propertyValueRelation: PropertyValueRelation.Matching,
datatype: 'string',
subclasses: false,
},
],
omitLabels: true,
Expand All @@ -127,5 +129,37 @@ describe( 'getters', () => {

expect( getters.query( state ) ).toStrictEqual( expectedValue );
} );

it( 'returns the QueryRepresentation of the RootState with subclasses', () => {
const state: RootState = {
conditionRows: [ {
valueData: { value: 'foo', valueError: null },
propertyData: { id: 'P123', label: 'abc', datatype: 'string', propertyError: null },
propertyValueRelationData: { value: PropertyValueRelation.Matching },
conditionId: '0.123',
subclasses: true,
} ],
omitLabels: true,
limit: 0,
useLimit: false,
errors: [],
};

const expectedValue: QueryRepresentation = {
conditions: [
{
propertyId: 'P123',
value: 'foo',
propertyValueRelation: PropertyValueRelation.Matching,
datatype: 'string',
subclasses: true,
},
],
omitLabels: true,
};

expect( getters.query( state ) ).toStrictEqual( expectedValue );
} );

} );
} );

0 comments on commit 9175db1

Please sign in to comment.