This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
/
QueryObjectBuilder.ts
191 lines (171 loc) · 4.49 KB
/
QueryObjectBuilder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import rdfNamespaces from '@/sparql/rdfNamespaces';
import QueryRepresentation, { Condition } from '@/sparql/QueryRepresentation';
import { IriTerm, Pattern, PropertyPath, SelectQuery, Term } from 'sparqljs';
import PropertyValueRelation from '@/data-model/PropertyValueRelation';
export default class QueryObjectBuilder {
private queryObject: SelectQuery;
public constructor() {
this.queryObject = {
queryType: 'SELECT',
variables: [],
where: [],
type: 'query',
prefixes: rdfNamespaces,
};
}
public buildFromQueryRepresentation( queryRepresentation: QueryRepresentation ): SelectQuery {
this.queryObject.variables = [
{
termType: 'Variable',
value: 'item',
},
];
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 ] );
}
if ( queryRepresentation.limit ) {
this.queryObject.limit = queryRepresentation.limit;
}
return this.queryObject;
}
private buildTripleObject( condition: Condition ): Term {
// TODO: Consider extracting this into a new class when adding less than / more than
switch ( condition.propertyValueRelation ) {
case ( PropertyValueRelation.NotMatching ):
return {
termType: 'Variable',
value: 'instance',
};
case ( PropertyValueRelation.Regardless ):
return {
termType: 'BlankNode',
value: 'anyValue' + condition.propertyId,
};
case ( PropertyValueRelation.Matching ):
return this.buildTripleObjectForExplicitValue( condition.datatype, condition.value );
default:
throw new Error( `unsupported relation: ${condition.propertyValueRelation}` );
}
}
private buildTripleObjectForExplicitValue( datatype: string, value: string ): Term {
// TODO: Consider extracting this into a new class when adding unit type
switch ( datatype ) {
case 'string':
case 'external-id':
return {
termType: 'Literal',
value: value,
};
case 'wikibase-item':
return {
termType: 'NamedNode', // this.mapDatatypeToTermType( condition.datatype ),
value: `${rdfNamespaces.wd}${value}`,
};
default:
throw new Error( `unsupported datatype: ${datatype}` );
}
}
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 );
if ( !this.queryObject.where ) {
this.queryObject.where = [];
}
this.queryObject.where.push(
{
type: 'bgp',
triples: [
{
subject: {
termType: 'Variable',
value: 'item',
},
predicate: { type: 'path',
pathType: '/',
items: this.buildTriplePredicateItems( condition ) },
object: tripleObject,
},
],
},
);
if ( condition.propertyValueRelation === PropertyValueRelation.NotMatching ) {
const filterCondition = {
type: 'filter',
expression: {
type: 'operation',
operator: '!=',
args: [
{
termType: 'Variable',
value: 'instance',
},
this.buildTripleObjectForExplicitValue( condition.datatype, condition.value ),
],
},
};
this.queryObject.where.push( filterCondition as Pattern );
}
}
}