-
Notifications
You must be signed in to change notification settings - Fork 3
/
NimbleSpecificSPARQLFactory.java
408 lines (308 loc) · 16.4 KB
/
NimbleSpecificSPARQLFactory.java
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package eu.nimble.service.catalog.search.mediator;
import java.util.ArrayList;
import java.util.List;
import de.biba.triple.store.access.IReader;
import de.biba.triple.store.access.marmotta.MarmottaReader;
import eu.nimble.service.catalog.search.impl.dao.enums.PropertySource;
import eu.nimble.service.catalog.search.impl.dao.input.InputParamaterForExecuteSelect;
import eu.nimble.service.catalog.search.services.NimbleAdaptionServiceOfSearchResults;
import eu.nimble.service.catalog.search.services.SQPDerivationService;
public class NimbleSpecificSPARQLFactory {
private MediatorSPARQLDerivationAndExecution sparqlDerivation;
private NimbleSpecificSPARQLDeriviationAndExecution nimbleSpecificSPARQLDeriviation;
private IReader reader;
private SQPDerivationService sqpDerivationService;
public NimbleSpecificSPARQLFactory(MediatorSPARQLDerivationAndExecution sparqlDerivation,
SQPDerivationService sqpDerivationService) {
super();
this.sparqlDerivation = sparqlDerivation;
this.sqpDerivationService = sqpDerivationService;
}
public List<String> createSparql(InputParamaterForExecuteSelect inputParamaterForExecuteSelect, IReader reader) {
this.reader = reader;
if (!(reader instanceof MarmottaReader)) {
return createJenaSPARQLQuery(inputParamaterForExecuteSelect);
} else {
return createMarmottaSPARQLQuery(inputParamaterForExecuteSelect);
}
}
public String createSPARQLForFinalValue(String uuidOfProperty, String propertyname, IReader reader) {
String sparql = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> select distinct ?hasValue ?property where {";
PropertySource source = detectSourceOnBasisOfName(propertyname);
switch (source) {
case DIMENSION:
sparql += extendSPARQLForDimensionToGivenInstance(uuidOfProperty);
break;
case CERTIFICATE:
sparql += extendSPARQLForCertificateToGivenInstance(uuidOfProperty);
break;
case ADDITIONAL_ITEM_PROPERTY:
sparql += extendSPARQLForDomainAndCustomPropertiesToGivenInstance(uuidOfProperty, reader);
break;
case MANUFACTURER_PARTY:
sparql += extendSPARQLForManufacturerPartyToGivenInstance(uuidOfProperty);
break;
case MANUFACTURER_ITEMS_IDENTIFICATION:
sparql += extendSPARQLForManufacturersItemIdentificationToGiven(uuidOfProperty);
break;
case CATALOGUE_DOCUMENT_REFERENCE:
sparql +=extendSPARQLForCatalogueDocumentReferenceToGiven(uuidOfProperty);
break;
default:
return null;
}
sparql += "}";
return sparql;
}
private String extendSPARQLForCatalogueDocumentReferenceToGiven(String uuidOfProperty) {
String sparql = "<" + uuidOfProperty + ">"
+ "<urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ID> ?hasValue.";
return sparql;
}
private String extendSPARQLForManufacturersItemIdentificationToGiven(String uuidOfProperty) {
String sparql = "<" + uuidOfProperty + ">"
+
"<urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ID> ?hasValue.";
return sparql;
}
private String extendSPARQLForManufacturerPartyToGivenInstance(String uuidOfProperty) {
String sparql = "<" + uuidOfProperty + ">"
+ " <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Name> ?hasValue.";
return sparql;
}
private String extendSPARQLForDomainAndCustomPropertiesToGivenInstance(String uuidOfProperty, IReader reader) {
String sparql = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> select distinct ?hasValue ?property ?quality where {";
sparql += "<" + uuidOfProperty + ">"
+ "<urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Name> ?property. <"
+ uuidOfProperty
+ "> <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ValueQualifier> ?quality.";
sparql += "}";
Object result = reader.query(sparql);
List<String> quantifier = reader.createResultList(result, "quality");
if (!quantifier.isEmpty()) {
String type = quantifier.get(0);
sparql = "<" + uuidOfProperty + ">"
+ "<urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Name> ?property.";
if (type.contains("REAL_MEASURE")) {
sparql += "<" + uuidOfProperty + ">"
+ "<urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ValueDecimal> ?hasValue";
}
if (type.contains("QUANTITY")) {
sparql += "<" + uuidOfProperty + ">"
+ "<urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ValueQuantity> ?qType. ?qType <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ValueDecimal> ?hasValue.";
}
if (type.contains("STRING")) {
sparql += "<" + uuidOfProperty + ">"
+ "<urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Value> ?hasValue.";
}
}
return sparql;
}
private String extendSPARQLForCertificateToGivenInstance(String uuidOfProperty) {
return "<" + uuidOfProperty + ">"
+ " <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#CertificateType> ?hasValue";
}
private String extendSPARQLForDimensionToGivenInstance(String uuidOfProperty) {
return "<" + uuidOfProperty + ">"
+ "<urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#AttributeID> ?property. " + "<"
+ uuidOfProperty + ">"
+ "<urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Measure> ?measure. ?measure <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ValueDecimal> ?hasValue";
}
private PropertySource detectSourceOnBasisOfName(String propertyname) {
if (propertyname.contains("Additional")) {
return PropertySource.ADDITIONAL_ITEM_PROPERTY;
}
PropertySource propertySource = detectSubsetofPotentialPropertySource(propertyname);
if (propertySource != PropertySource.UNKNOWN) {
return propertySource;
}
return PropertySource.UNKNOWN;
}
private List<String> createMarmottaSPARQLQuery(InputParamaterForExecuteSelect inputParamaterForExecuteSelect) {
nimbleSpecificSPARQLDeriviation = new NimbleSpecificSPARQLDeriviationAndExecution((MarmottaReader) reader,
sqpDerivationService, sparqlDerivation);
List<String> result = new ArrayList<String>();
int counter = 0;
for (String param : inputParamaterForExecuteSelect.getParameters()) {
String sparql = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> select distinct ?instance ";
sparql += " ?property";
sparql += " ?hasValue";
// Define it is a ItemType
sparql += " where{";
sparql += "?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2#ItemType>. ?instance <urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2#CommodityClassification> ?type. ?type <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ItemClassificationCode> ?code. ?code <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#URI> ?codeValue.";
String urlOfconcept = inputParamaterForExecuteSelect.getConcept();
if (sparqlDerivation != null) {
urlOfconcept = sparqlDerivation.getURIOfConcept(inputParamaterForExecuteSelect.getConcept());
}
sparql += addPropertyForMarmotta(inputParamaterForExecuteSelect.getParametersURL().get(counter),
inputParamaterForExecuteSelect.getPropertySources().get(counter));
sparql += "Filter regex( ?codeValue , \"" + urlOfconcept + "\").";
sparql += "}";
result.add(sparql);
counter++;
}
return result;
}
private String addPropertyForMarmotta(String propertyURL, PropertySource propertySource2) {
String result = "";
PropertySource propertySource = detectPropertySource(propertyURL, propertySource2);
switch (propertySource) {
case ADDITIONAL_ITEM_PROPERTY:
break;
case DOMAIN_SPECIFIC_PROPERTY:
result += extendForDomainSpecificProperty(propertyURL);
return result;
case DESCRIPTION:
result += extendForDescriptionProperty();
break;
case NAME:
result += extendForNameProperty();
break;
case CERTIFICATE:
result += extendForCertificate();
break;
case DIMENSION:
result += extendForDimension(propertyURL);
return result;
case MANUFACTURER_ITEMS_IDENTIFICATION:
result += extendForManufacturerIdentification();
return result;
case MANUFACTURER_PARTY:
result += extendForManufacturerParty(propertyURL);
return result;
case CUSTOM_STRING:
result += extendForCustomString(propertyURL);
return result;
case CUSTOM_DECIMAL:
result += extendForCustomDecimal(propertyURL);
return result;
}
if (propertySource != PropertySource.DOMAIN_SPECIFIC_PROPERTY) {
result += "?instance ?property ?hasValue .";
} else {
result += "?code2 <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Value> ?property.";
}
return result;
}
private String extendForManufacturerIdentification() {
String sparql = "?instance <urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2#ManufacturersItemIdentification> ?ManufacturerParty. ?ManufacturerParty <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ID> ?hasValue.";
sparql += "?instance ?property ?ManufacturerParty .";
return sparql;
}
private String extendForManufacturerParty(String propertyURL) {
String sparql = "?instance <urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2#ManufacturerParty> ?ManufacturerParty. ?ManufacturerParty <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Name> ?hasValue.";
sparql += "?instance ?property ?ManufacturerParty .";
return sparql;
}
private String extendForCustomString(String propertyName) {
String sparql = "?instance"
+ " <urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2#AdditionalItemProperty> ?propertyValue. ?propertyValue <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Name> ?property. ?propertyValue <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Value> ?hasValue. ?propertyValue <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ItemClassificationCode> ?codeOfProperty. ?codeOfProperty <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#listID> ?value.";
sparql += "Filter (regex( ?property , \"" + propertyName + "\") && regex (?value, \"Custom\", \" i\")).";
return sparql;
}
private String extendForCustomDecimal(String propertyName) {
String sparql = "?instance"
+ " <urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2#AdditionalItemProperty> ?propertyValue. ?propertyValue <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Name> ?property. ?propertyValue <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ValueQuantity> ?valueQ. ?valueQ <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ValueDecimal> ?hasValue. ?propertyValue <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ItemClassificationCode> ?codeOfProperty. ?codeOfProperty <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#listID> ?value.";
sparql += "Filter (regex( ?property , \"" + propertyName + "\") && regex (?value, \"Custom\", \" i\")).";
return sparql;
}
private String extendForDomainSpecificProperty(String propertyURL) {
// this is the old query which uses the classificationCode to refer to
// an domain specific property
// String sparql = " ?instance
// <urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2#AdditionalItemProperty>
// ?aproperty. ?aproperty
// <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Value>
// ?hasValue. ?aproperty
// <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ItemClassificationCode>
// ?code2. ?code2
// <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Value>
// ?value";
String sparql = " ?instance <urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2#AdditionalItemProperty> ?aproperty. ?aproperty <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#URI> ?property. ?aproperty <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Value> ?hasValue.";
sparql += " Filter regex( ?property , \"" + propertyURL + "\").";
return sparql;
}
private String extendForDimension(String propertyName) {
String sparql = "?instance"
+ "<urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2#Dimension> ?dimension. ?dimension <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#AttributeID> ?property. ?dimension <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Measure> ?measure. ?measure <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#ValueDecimal> ?hasValue";
sparql += " Filter regex( ?property , \"" + propertyName + "\").";
return sparql;
}
private String extendForCertificate() {
return "?instance"
+ "<urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2#Certificate> ?cert. ?cert <urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#CertificateType> ?hasValue .";
}
private String extendForDescriptionProperty() {
return "?instance"
+ "<urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2#Description> ?hasValue .";
}
private String extendForNameProperty() {
// TODO Auto-generated method stub
return "?instance" + "<" + NimbleAdaptionServiceOfSearchResults.propertyURLForName + "> ?hasValue .";
}
private PropertySource detectPropertySource(String propertyURL, PropertySource propertySource2) {
switch (propertySource2) {
case DIMENSION:
return PropertySource.DIMENSION;
case CUSTOM_DECIMAL:
return PropertySource.CUSTOM_DECIMAL;
case CUSTOM_STRING:
return PropertySource.CUSTOM_STRING;
case DOMAIN_SPECIFIC_PROPERTY:
return PropertySource.DOMAIN_SPECIFIC_PROPERTY;
default:
String propertyName = propertyURL.toLowerCase();
int index = propertyName.indexOf("#");
propertyName = propertyName.substring(index + 1);
if (propertyName.contains("name")) {
return PropertySource.NAME;
}
if (propertyName.contains("description")) {
return PropertySource.DESCRIPTION;
}
PropertySource propertySource = detectSubsetofPotentialPropertySource(propertyName);
if (propertySource != PropertySource.UNKNOWN) {
return propertySource;
}
if (nimbleSpecificSPARQLDeriviation.isItADomainSpecificPropertyWhichHasValues(propertyURL)) {
return PropertySource.DOMAIN_SPECIFIC_PROPERTY;
}
}
return PropertySource.ADDITIONAL_ITEM_PROPERTY;
}
public PropertySource detectSubsetofPotentialPropertySource(String propertyName) {
propertyName = propertyName.toLowerCase();
if (propertyName.contains("certi")) {
return PropertySource.CERTIFICATE;
}
if (propertyName.contains("dimension")) {
return PropertySource.DIMENSION;
}
if (propertyName.contains("manufacturerparty")) {
return PropertySource.MANUFACTURER_PARTY;
}
if (propertyName.contains("manufacturerparty")) {
return PropertySource.MANUFACTURER_PARTY;
}
if (propertyName.contains("manufacturersitemidentification" )){
return PropertySource.MANUFACTURER_ITEMS_IDENTIFICATION;
}
if (propertyName.contains("manufacturersitemidentification")) {
return PropertySource.MANUFACTURER_ITEMS_IDENTIFICATION;
}
if (propertyName.contains("cataloguedocumentreference")){
return PropertySource.CATALOGUE_DOCUMENT_REFERENCE;
}
return PropertySource.UNKNOWN;
}
private List<String> createJenaSPARQLQuery(InputParamaterForExecuteSelect inputParamaterForExecuteSelect) {
// TODO Auto-generated method stub
return null;
}
public String extractNameOfURL(String param) {
if (param.contains("#")) {
param = param.substring(param.indexOf("#") + 1);
}
return param;
}
}