-
Notifications
You must be signed in to change notification settings - Fork 14
/
OwlapiUtils.java
123 lines (115 loc) · 5.17 KB
/
OwlapiUtils.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
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* This program 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 3 of the License, or (at your option) any
* later version.
*
* This program 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/>.
*/
package cz.cvut.kbss.ontodriver.owlapi.util;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.vocab.OWL2Datatype;
import java.net.URI;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
/**
* Utility methods for the OWLAPI driver.
*/
public class OwlapiUtils {
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
/**
* Creates OWLLiteral from the specified Java instance.
*
* @param value The value to transform
* @param dataFactory Data factory
* @param lang Ontology language
* @return OWLLiteral representing the value
* @throws IllegalArgumentException If {@code value} is of unsupported type
*/
public static OWLLiteral createOWLLiteralFromValue(Object value, OWLDataFactory dataFactory, String lang) {
Objects.requireNonNull(value);
if (value instanceof Integer) {
return dataFactory.getOWLLiteral((Integer) value);
} else if (value instanceof Long) {
return dataFactory.getOWLLiteral(value.toString(), OWL2Datatype.XSD_LONG);
} else if (value instanceof Boolean) {
return dataFactory.getOWLLiteral((Boolean) value);
} else if (value instanceof Double) {
return dataFactory.getOWLLiteral((Double) value);
} else if (value instanceof String) {
return dataFactory.getOWLLiteral((String) value, lang);
} else if (value instanceof Date) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
return dataFactory.getOWLLiteral(sdf.format(((Date) value)),
dataFactory.getOWLDatatype(OWL2Datatype.XSD_DATE_TIME.getIRI()));
} else if (value.getClass().isEnum()) {
return dataFactory.getOWLLiteral(value.toString());
} else {
throw new IllegalArgumentException();
}
}
/**
* Transforms OWLLiteral to a plain Java object (boxed primitive or date/time).
*
* @param literal The literal to transform
* @return Transformed value
* @throws IllegalArgumentException If the literal is of unsupported type
*/
public static Object owlLiteralToValue(final OWLLiteral literal) {
if (literal.isRDFPlainLiteral()) {
return literal.getLiteral();
} else if (literal.getDatatype().isBuiltIn())
switch (literal.getDatatype().getBuiltInDatatype()) {
case XSD_SHORT:
return Short.parseShort(literal.getLiteral());
case XSD_LONG:
return Long.parseLong(literal.getLiteral());
case XSD_INT:
case XSD_INTEGER:
return Integer.parseInt(literal.getLiteral());
case XSD_DOUBLE:
case XSD_DECIMAL:
return Double.parseDouble(literal.getLiteral());
case XSD_FLOAT:
return Float.parseFloat(literal.getLiteral());
case XSD_STRING:
case RDF_XML_LITERAL:
return literal.getLiteral();
case XSD_BOOLEAN:
return Boolean.parseBoolean(literal.getLiteral());
case XSD_ANY_URI:
return URI.create(literal.getLiteral());
case XSD_DATE_TIME_STAMP:
case XSD_DATE_TIME:
try {
return new SimpleDateFormat(DATE_TIME_FORMAT).parse(literal.getLiteral());
} catch (ParseException e) {
throw new IllegalArgumentException(
"The date time '" + literal.getLiteral() + "' cannot be parsed.");
}
}
throw new IllegalArgumentException("Unsupported datatype: " + literal.getDatatype());
}
/**
* Gets OWLNamedIndividual for the specified named resource.
*
* @param subject Named resource to transform to individual
* @param dataFactory OWL data factor
* @return OWLNamedIndividual
*/
public static OWLNamedIndividual getIndividual(NamedResource subject, OWLDataFactory dataFactory) {
return dataFactory.getOWLNamedIndividual(IRI.create(subject.getIdentifier()));
}
}