-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAbstractTermMap.java
More file actions
463 lines (413 loc) · 19.1 KB
/
AbstractTermMap.java
File metadata and controls
463 lines (413 loc) · 19.1 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* Copyright 2011 Antidot opensource@antidot.net
* https://github.com/antidot/db2triples
*
* DB2Triples is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* DB2Triples is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* *************************************************************************
*
* R2RML Model : Abstract Term Map
*
* Partial implementation of a term map.
*
* modified by mielvandersande
*
***************************************************************************
*/
package be.ugent.mmlab.rml.model;
import be.ugent.mmlab.rml.model.reference.ReferenceIdentifier;
import be.ugent.mmlab.rml.model.reference.ReferenceIdentifierImpl;
import be.ugent.mmlab.rml.tools.CustomRDFDataValidator;
import java.util.HashSet;
import java.util.Set;
import net.antidot.semantic.rdf.model.tools.RDFDataValidator;
import net.antidot.semantic.rdf.rdb2rdf.r2rml.exception.InvalidR2RMLStructureException;
import net.antidot.semantic.rdf.rdb2rdf.r2rml.exception.InvalidR2RMLSyntaxException;
import net.antidot.semantic.rdf.rdb2rdf.r2rml.exception.R2RMLDataError;
import net.antidot.semantic.rdf.rdb2rdf.r2rml.tools.R2RMLToolkit;
import net.antidot.semantic.xmls.xsd.XSDLexicalTransformation;
import net.antidot.semantic.xmls.xsd.XSDType;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openrdf.model.Literal;
import org.openrdf.model.Resource;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
public abstract class AbstractTermMap implements TermMap {
// Log
private static Log log = LogFactory.getLog(AbstractTermMap.class);
private Value constantValue;
private URI dataType; //MVS: Changed to resource, since XSD is to limited
private TermType termType;
private URI implicitDataType; //MVS: Changed to resource, since XSD is to limited
private String languageTag;
private String stringTemplate;
private ReferenceIdentifier referenceValue;
private String inverseExpression;
private String stringGuard;
protected AbstractTermMap(Value constantValue, URI dataType,
String languageTag, String stringTemplate, URI termType,
String stringGuard, String inverseExpression, ReferenceIdentifier referenceValue)
throws R2RMLDataError, InvalidR2RMLStructureException,
InvalidR2RMLSyntaxException {
setConstantValue(constantValue);
setReferenceValue(referenceValue);
setLanguageTag(languageTag);
setStringTemplate(stringTemplate);
setStringGuard(stringGuard);
setTermType(termType, dataType);
setDataType(dataType);
setInversionExpression(inverseExpression);
checkGlobalConsistency();
}
/**
* Check if the global structure of this TermMap is consistent and valid
* according to R2RML standard.
*
* @throws InvalidR2RMLStructureException
*/
private void checkGlobalConsistency() throws InvalidR2RMLStructureException {
// A term map must be exactly one term map type
if (getTermMapType() == null) // In db2triples and contrary to the R2RML norm, we accepts
// auto-assignments of blank nodes.
{
if (getTermType() != TermType.BLANK_NODE) {
throw new InvalidR2RMLStructureException(
"[AbstractTermMap:checkGlobalConsistency] A constant RDF Term,"
+ " a column name or a string template must be specified.");
}
}
}
private void setInversionExpression(String inverseExpression)
throws InvalidR2RMLSyntaxException, InvalidR2RMLStructureException {
// An inverse expression is associated with
// a column-valued term map or template-value term map
if (inverseExpression != null && getTermMapType() != null
&& getTermMapType() == TermMapType.CONSTANT_VALUED) {
throw new InvalidR2RMLStructureException(
"[AbstractTermMap:setInversionExpression] An inverseExpression "
+ "can not be associated with a constant-value term map.");
}
// This property is optional
if (inverseExpression != null) {
checkInverseExpression(inverseExpression);
}
this.inverseExpression = inverseExpression;
}
private void checkInverseExpression(String inverseExpression)
throws InvalidR2RMLSyntaxException {
// An inverse expression must satisfy a lot of conditions
if (!R2RMLToolkit.checkInverseExpression(inverseExpression)) {
throw new InvalidR2RMLSyntaxException(
"[AbstractTermMap:checkInverseExpression] Not a valid inverse "
+ "expression : " + stringTemplate);
}
}
private void setReferenceValue(ReferenceIdentifier referenceValue)
throws InvalidR2RMLSyntaxException, InvalidR2RMLStructureException {
// The value of the rml:reference property MUST be a valid reference for this queryLanguage.
// if (columnValue != null)
// checkColumnValue(columnValue);
this.referenceValue = referenceValue;
}
// private void checkColumnValue(String columnValue)
// throws InvalidR2RMLSyntaxException {
// if (!SQLDataValidator.isValidSQLIdentifier(columnValue))
// throw new InvalidR2RMLSyntaxException(
// "[AbstractTermMap:checkColumnValue] Not a valid column "
// + "value : " + termType);
// }
protected void setTermType(URI termType, URI dataType)
throws InvalidR2RMLSyntaxException, R2RMLDataError,
InvalidR2RMLStructureException {
if (termType == null) {
// If the term map does not have a rr:termType property :
// rr:Literal by default, if it is an object map and at
// least one of the following conditions is true
if ((this instanceof StdObjectMap)
&& (getReferenceValue() != null || dataType != null
|| getLanguageTag() != null || constantValue instanceof Literal)) {
this.termType = TermType.LITERAL;
log.debug("[AbstractTermMap:setTermType] No term type specified : use Literal by default.");
} else {
// otherwise its term type is IRI
this.termType = TermType.IRI;
log.debug("[AbstractTermMap:setTermType] No term type specified : use IRI by default." + getReferenceValue());
}
} else {
TermType tt = null;
if (termType != null) {
tt = checkTermType(termType);
}
this.termType = tt;
}
}
private TermType checkTermType(URI termType)
throws InvalidR2RMLSyntaxException, InvalidR2RMLStructureException,
R2RMLDataError {
// Its value MUST be an IRI
if (!RDFDataValidator.isValidURI(termType.stringValue())) {
throw new R2RMLDataError(
"[AbstractTermMap:checkTermType] Not a valid URI : "
+ termType);
}
// (IRIs, blank nodes or literals)
TermType tt = TermType.toTermType(termType.stringValue());
if (tt == null) {
throw new InvalidR2RMLSyntaxException(
"[AbstractTermMap:checkTermType] Not a valid term type : "
+ termType);
}
// Check rules in function of term map nature (subject, predicate ...)
checkSpecificTermType(tt);
return tt;
}
protected abstract void checkSpecificTermType(TermType tt)
throws InvalidR2RMLStructureException;
private void setStringTemplate(String stringTemplate)
throws InvalidR2RMLSyntaxException, InvalidR2RMLStructureException {
// he value of the rr:template property MUST be a
// valid string template.
if (stringTemplate != null) {
checkStringTemplate(stringTemplate);
}
this.stringTemplate = stringTemplate;
}
private void setStringGuard(String stringGuard)
throws InvalidR2RMLSyntaxException, InvalidR2RMLStructureException {
// he value of the rr:template property MUST be a
// valid string template.
this.stringGuard = stringGuard;
}
/**
* A string template is a format string that can be used to build
* strings from multiple components. It can reference column names by
* enclosing them in curly braces.
*
* @throws R2RMLDataError
*/
private void checkStringTemplate(String stringTemplate)
throws InvalidR2RMLSyntaxException {
// Its value MUST be an IRI
if (!R2RMLToolkit.checkStringTemplate(stringTemplate)) {
throw new InvalidR2RMLSyntaxException(
"[AbstractTermMap:checkStringTemplate] Not a valid string "
+ "template : " + stringTemplate);
}
}
private void setLanguageTag(String languageTag) throws R2RMLDataError {
// its value MUST be a valid language tag
if (languageTag != null) {
checkLanguageTag(languageTag);
}
this.languageTag = languageTag;
}
/**
* Check if language tag is valid, as defined by [RFC-3066]
*
* @throws R2RMLDataError
*/
private void checkLanguageTag(String languageTag) throws R2RMLDataError {
// Its value MUST be an IRI
if (!RDFDataValidator.isValidLanguageTag(languageTag)) {
throw new R2RMLDataError(
"[AbstractTermMap:checkLanguageTag] Not a valid language tag : "
+ languageTag);
}
}
/**
* Check if constant value is correctly defined. Constant value is an
* IRI or literal in function of this term map type.
*/
protected abstract void checkConstantValue(Value constantValue)
throws R2RMLDataError;
public void setConstantValue(Value constantValue) throws R2RMLDataError,
InvalidR2RMLStructureException {
// Check if constant value is valid
if (constantValue != null) {
checkConstantValue(constantValue);
}
this.constantValue = constantValue;
}
/**
* Check if datatype is correctly defined.
*
* @throws R2RMLDataError
*/
public void checkDataType(URI dataType) throws R2RMLDataError {
// Its value MUST be an IRI
//MVS: class below prevents datatypes other than XSD
//if (!RDFDataValidator.isValidDatatype(dataType.stringValue())) {
if (!CustomRDFDataValidator.isValidDatatype(dataType.stringValue())) {
throw new R2RMLDataError(
"[AbstractTermMap:checkDataType] Not a valid URI : "
+ dataType);
}
}
public void setDataType(URI dataType) throws R2RMLDataError,
InvalidR2RMLStructureException {
if (!isTypeable() && dataType != null) {
throw new InvalidR2RMLStructureException(
"[AbstractTermMap:setDataType] A term map that is not "
+ "a typeable term map MUST NOT have an rr:datatype"
+ " property.");
}
if (dataType != null) {
// Check if datatype is valid
checkDataType(dataType);
//this.dataType = new dataType.stringValue();
this.dataType = dataType;
}
}
public Value getConstantValue() {
return constantValue;
}
public URI getDataType() {
return dataType;
}
public URI getImplicitDataType() {
return implicitDataType;
}
public XSDLexicalTransformation.Transformation getImplicitTransformation() {
if (implicitDataType == null) {
return null;
} else {
return XSDLexicalTransformation
.getLexicalTransformation(XSDType.toXSDType(implicitDataType.stringValue()));
}
}
public String getInverseExpression() {
return inverseExpression;
}
public String getLanguageTag() {
return languageTag;
}
public Set<ReferenceIdentifier> getReferencedSelectors() {
Set<ReferenceIdentifier> referencedColumns = new HashSet<ReferenceIdentifier>();
switch (getTermMapType()) {
case CONSTANT_VALUED:
// The referenced columns of a constant-valued term map is the
// empty set.
break;
case REFERENCE_VALUED:
// The referenced columns of a column-valued term map is
// the singleton set containing the value of rr:column.
// referencedColumns.add(R2RMLToolkit.deleteBackSlash(columnValue));
referencedColumns.add(referenceValue);
break;
case TEMPLATE_VALUED:
// The referenced columns of a template-valued term map is
// the set of column names enclosed in unescaped curly braces
// in the template string.
for (String colName : R2RMLToolkit
.extractColumnNamesFromStringTemplate(stringTemplate)) {
referencedColumns.add(ReferenceIdentifierImpl.buildFromR2RMLConfigFile(colName));
}
break;
default:
break;
}
log.debug("[AbstractTermMap:getReferencedColumns] ReferencedColumns are now : "
+ referencedColumns);
return referencedColumns;
}
public String getStringTemplate() {
return stringTemplate;
}
public String getStringGuard() {
return stringGuard;
}
public TermMapType getTermMapType() {
if (constantValue != null) {
return TermMapType.CONSTANT_VALUED;
} else if (referenceValue != null) {
return TermMapType.REFERENCE_VALUED;
} else if (stringTemplate != null) {
return TermMapType.TEMPLATE_VALUED;
} else if (termType == TermType.BLANK_NODE) {
return TermMapType.NO_VALUE_FOR_BNODE;
}
return null;
}
public TermType getTermType() {
return termType;
}
public ReferenceIdentifier getReferenceValue() {
return referenceValue;
}
public boolean isOveridden() {
if (implicitDataType == null) {
// No implicit datatype extracted for yet
// Return false by default
return false;
}
return dataType != implicitDataType;
}
public boolean isTypeable() {
return (termType == TermType.LITERAL) && (languageTag == null);
}
public void setImplicitDataType(URI implicitDataType) {
this.implicitDataType = implicitDataType;
}
// public String getValue(Map<ColumnIdentifier, byte[]> dbValues,
// ResultSetMetaData dbTypes) throws R2RMLDataError, SQLException,
// UnsupportedEncodingException {
//
// log.debug("[AbstractTermMap:getValue] Extract value of termType with termMapType : "
// + getTermMapType());
// switch (getTermMapType()) {
// case CONSTANT_VALUED:
// return constantValue.stringValue();
//
// case REFERENCE_VALUED:
// if (dbValues.keySet().isEmpty())
// throw new IllegalStateException(
// "[AbstractTermMap:getValue] impossible to extract from an empty database value set.");
// byte[] bytesResult = dbValues.get(referenceValue);
// /* Extract the SQLType in dbValues from the key which is
// * equals to "columnValue" */
// SQLType sqlType = null;
// for(ColumnIdentifier colId : dbValues.keySet()) {
// if(colId.equals(referenceValue)) {
// sqlType = colId.getSqlType();
// break;
// }
// }
// // Apply cast to string to the SQL data value
// String result;
// if (sqlType != null) {
// XSDType xsdType = SQLToXMLS.getEquivalentType(sqlType);
// result = XSDLexicalTransformation.extractNaturalRDFFormFrom(
// xsdType, bytesResult);
// }
// else
// {
// result = new String(bytesResult, "UTF-8");
// }
// return result;
//
// case TEMPLATE_VALUED:
// if (dbValues.keySet().isEmpty())
// throw new IllegalStateException(
// "[AbstractTermMap:getValue] impossible to extract from an empty database value set.");
// result = R2RMLToolkit.extractColumnValueFromStringTemplate(
// stringTemplate, dbValues, dbTypes);
// return result;
//
// default:
// return null;
// }
//}
}