Package: OwlapiUtils
OwlapiUtils
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
OwlapiUtils() |
|
|
|
|
|
||||||||||||||||||||
createOWLLiteralFromValue(Object, OWLDataFactory, String) |
|
|
|
|
|
||||||||||||||||||||
doesLanguageMatch(OWLLiteral, String) |
|
|
|
|
|
||||||||||||||||||||
getIndividual(NamedResource, OWLDataFactory) |
|
|
|
|
|
||||||||||||||||||||
isIndividualIri(Object) |
|
|
|
|
|
||||||||||||||||||||
owlLiteralToValue(OWLLiteral) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: * <p>
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi.util;
16:
17: import cz.cvut.kbss.ontodriver.model.NamedResource;
18: import org.semanticweb.owlapi.model.IRI;
19: import org.semanticweb.owlapi.model.OWLDataFactory;
20: import org.semanticweb.owlapi.model.OWLLiteral;
21: import org.semanticweb.owlapi.model.OWLNamedIndividual;
22: import org.semanticweb.owlapi.vocab.OWL2Datatype;
23:
24: import java.net.URI;
25: import java.net.URL;
26: import java.text.ParseException;
27: import java.text.SimpleDateFormat;
28: import java.util.Date;
29: import java.util.Objects;
30:
31: /**
32: * Utility methods for the OWLAPI driver.
33: */
34: public class OwlapiUtils {
35:
36: private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
37:
38: private OwlapiUtils() {
39: throw new AssertionError("Can't create instance.");
40: }
41:
42: /**
43: * Creates OWLLiteral from the specified Java instance.
44: *
45: * @param value The value to transform
46: * @param dataFactory Data factory
47: * @param lang Ontology language
48: * @return OWLLiteral representing the value
49: * @throws IllegalArgumentException If {@code value} is of unsupported type
50: */
51: public static OWLLiteral createOWLLiteralFromValue(Object value, OWLDataFactory dataFactory, String lang) {
52: Objects.requireNonNull(value);
53:• if (value instanceof Integer) {
54: // Java implementations map int/Integer to xsd:int, because xsd:integer is unbounded, whereas xsd:int is 32-bit signed, same as Java
55: return dataFactory.getOWLLiteral(value.toString(), OWL2Datatype.XSD_INT);
56:• } else if (value instanceof Long) {
57: return dataFactory.getOWLLiteral(value.toString(), OWL2Datatype.XSD_LONG);
58:• } else if (value instanceof Boolean) {
59: return dataFactory.getOWLLiteral((Boolean) value);
60:• } else if (value instanceof Double) {
61: return dataFactory.getOWLLiteral((Double) value);
62:• } else if (value instanceof String) {
63: return dataFactory.getOWLLiteral((String) value, lang);
64:• } else if (value instanceof Date) {
65: SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
66: return dataFactory.getOWLLiteral(sdf.format((Date) value),
67: dataFactory.getOWLDatatype(OWL2Datatype.XSD_DATE_TIME.getIRI()));
68:• } else if (value.getClass().isEnum()) {
69: return dataFactory.getOWLLiteral(value.toString());
70: } else {
71: throw new IllegalArgumentException("Unsupported value " + value + " of type " + value.getClass());
72: }
73: }
74:
75: /**
76: * Transforms OWLLiteral to a plain Java object (boxed primitive or date/time).
77: *
78: * @param literal The literal to transform
79: * @return Transformed value
80: * @throws IllegalArgumentException If the literal is of unsupported type
81: */
82: public static Object owlLiteralToValue(final OWLLiteral literal) {
83:• if (literal.isRDFPlainLiteral()) {
84: return literal.getLiteral();
85:• } else if (literal.getDatatype().isBuiltIn()) {
86:• switch (literal.getDatatype().getBuiltInDatatype()) {
87: case XSD_SHORT:
88: return Short.parseShort(literal.getLiteral());
89: case XSD_LONG:
90: return Long.parseLong(literal.getLiteral());
91: case XSD_INT:
92: case XSD_INTEGER:
93: return Integer.parseInt(literal.getLiteral());
94: case XSD_DOUBLE:
95: case XSD_DECIMAL:
96: return Double.parseDouble(literal.getLiteral());
97: case XSD_FLOAT:
98: return Float.parseFloat(literal.getLiteral());
99: case XSD_STRING:
100: case RDF_XML_LITERAL:
101: case RDF_LANG_STRING:
102: return literal.getLiteral();
103: case XSD_BOOLEAN:
104: return Boolean.parseBoolean(literal.getLiteral());
105: case XSD_ANY_URI:
106: return URI.create(literal.getLiteral());
107: case XSD_DATE_TIME_STAMP:
108: case XSD_DATE_TIME:
109: try {
110: return new SimpleDateFormat(DATE_TIME_FORMAT).parse(literal.getLiteral());
111: } catch (ParseException e) {
112: throw new IllegalArgumentException(
113: "The date time '" + literal.getLiteral() + "' cannot be parsed.");
114: }
115: }
116: }
117: throw new IllegalArgumentException("Unsupported datatype: " + literal.getDatatype());
118: }
119:
120: /**
121: * Checks whether the specified literal matches to the specified language.
122: * <p>
123: * If the literal is not a string, it automatically matches. If it is a string, it matches if {@code language} is
124: * not specified, it is without language tag or if the language tag matches the specified language.
125: *
126: * @param literal Literal to check
127: * @param language Expected language, possibly {@code null}
128: * @return {@code true} if the literal matches the language, {@code false} otherwise
129: */
130: public static boolean doesLanguageMatch(OWLLiteral literal, String language) {
131:• assert literal != null;
132:
133:• return language == null || literal.getLang().isEmpty() || literal.getLang().equals(language);
134: }
135:
136: /**
137: * Gets OWLNamedIndividual for the specified named resource.
138: *
139: * @param subject Named resource to transform to individual
140: * @param dataFactory OWL data factory
141: * @return OWLNamedIndividual
142: */
143: public static OWLNamedIndividual getIndividual(NamedResource subject, OWLDataFactory dataFactory) {
144: return dataFactory.getOWLNamedIndividual(IRI.create(subject.getIdentifier()));
145: }
146:
147: /**
148: * Checks whether the specified value is a valid IRI.
149: * <p>
150: * Only absolute IRIs are accepted.
151: *
152: * @param value The value to check
153: * @return {@code true} for instances of {@link NamedResource}, {@link URI}, {@link URL} or {@link IRI} and for
154: * Strings parseable by {@link URI#create(String)}.
155: */
156: public static boolean isIndividualIri(Object value) {
157:• if (value instanceof NamedResource || value instanceof URI || value instanceof URL || value instanceof IRI) {
158: return true;
159: }
160:• if (!(value instanceof String)) {
161: return false;
162: }
163: try {
164: final IRI iri = IRI.create(value.toString());
165: return iri.isAbsolute();
166: } catch (IllegalArgumentException e) {
167: return false;
168: }
169: }
170: }