Skip to content

Package: OwlapiUtils

OwlapiUtils

nameinstructionbranchcomplexitylinemethod
OwlapiUtils()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
createOWLLiteralFromValue(Object, OWLDataFactory, String)
M: 17 C: 76
82%
M: 1 C: 13
93%
M: 1 C: 7
88%
M: 1 C: 17
94%
M: 0 C: 1
100%
doesLanguageMatch(OWLLiteral, String)
M: 4 C: 32
89%
M: 2 C: 12
86%
M: 2 C: 6
75%
M: 0 C: 5
100%
M: 0 C: 1
100%
getIndividual(NamedResource, OWLDataFactory)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isIndividualIri(Object)
M: 3 C: 26
90%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 2 C: 6
75%
M: 0 C: 1
100%
owlLiteralToValue(OWLLiteral)
M: 43 C: 48
53%
M: 5 C: 9
64%
M: 5 C: 7
58%
M: 7 C: 10
59%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

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.*;
19: import org.semanticweb.owlapi.vocab.OWL2Datatype;
20:
21: import java.net.URI;
22: import java.net.URL;
23: import java.text.ParseException;
24: import java.text.SimpleDateFormat;
25: import java.util.Date;
26: import java.util.Objects;
27:
28: /**
29: * Utility methods for the OWLAPI driver.
30: */
31: public class OwlapiUtils {
32:
33: private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
34:
35: private OwlapiUtils() {
36: throw new AssertionError("Can't create instance.");
37: }
38:
39: /**
40: * Creates OWLLiteral from the specified Java instance.
41: *
42: * @param value The value to transform
43: * @param dataFactory Data factory
44: * @param lang Ontology language
45: * @return OWLLiteral representing the value
46: * @throws IllegalArgumentException If {@code value} is of unsupported type
47: */
48: public static OWLLiteral createOWLLiteralFromValue(Object value, OWLDataFactory dataFactory, String lang) {
49: Objects.requireNonNull(value);
50:• if (value instanceof Integer) {
51: // Java implementations map int/Integer to xsd:int, because xsd:integer is unbounded, whereas xsd:int is 32-bit signed, same as Java
52: return dataFactory.getOWLLiteral(value.toString(), OWL2Datatype.XSD_INT);
53:• } else if (value instanceof Long) {
54: return dataFactory.getOWLLiteral(value.toString(), OWL2Datatype.XSD_LONG);
55:• } else if (value instanceof Boolean) {
56: return dataFactory.getOWLLiteral((Boolean) value);
57:• } else if (value instanceof Double) {
58: return dataFactory.getOWLLiteral((Double) value);
59:• } else if (value instanceof String) {
60: return dataFactory.getOWLLiteral((String) value, lang);
61:• } else if (value instanceof Date) {
62: SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
63: return dataFactory.getOWLLiteral(sdf.format(((Date) value)),
64: dataFactory.getOWLDatatype(OWL2Datatype.XSD_DATE_TIME.getIRI()));
65:• } else if (value.getClass().isEnum()) {
66: return dataFactory.getOWLLiteral(value.toString());
67: } else {
68: throw new IllegalArgumentException("Unsupported value " + value + " of type " + value.getClass());
69: }
70: }
71:
72: /**
73: * Transforms OWLLiteral to a plain Java object (boxed primitive or date/time).
74: *
75: * @param literal The literal to transform
76: * @return Transformed value
77: * @throws IllegalArgumentException If the literal is of unsupported type
78: */
79: public static Object owlLiteralToValue(final OWLLiteral literal) {
80:• if (literal.isRDFPlainLiteral()) {
81: return literal.getLiteral();
82:• } else if (literal.getDatatype().isBuiltIn())
83:• switch (literal.getDatatype().getBuiltInDatatype()) {
84: case XSD_SHORT:
85: return Short.parseShort(literal.getLiteral());
86: case XSD_LONG:
87: return Long.parseLong(literal.getLiteral());
88: case XSD_INT:
89: case XSD_INTEGER:
90: return Integer.parseInt(literal.getLiteral());
91: case XSD_DOUBLE:
92: case XSD_DECIMAL:
93: return Double.parseDouble(literal.getLiteral());
94: case XSD_FLOAT:
95: return Float.parseFloat(literal.getLiteral());
96: case XSD_STRING:
97: case RDF_XML_LITERAL:
98: return literal.getLiteral();
99: case XSD_BOOLEAN:
100: return Boolean.parseBoolean(literal.getLiteral());
101: case XSD_ANY_URI:
102: return URI.create(literal.getLiteral());
103: case XSD_DATE_TIME_STAMP:
104: case XSD_DATE_TIME:
105: try {
106: return new SimpleDateFormat(DATE_TIME_FORMAT).parse(literal.getLiteral());
107: } catch (ParseException e) {
108: throw new IllegalArgumentException(
109: "The date time '" + literal.getLiteral() + "' cannot be parsed.");
110: }
111: }
112:
113: throw new IllegalArgumentException("Unsupported datatype: " + literal.getDatatype());
114: }
115:
116: /**
117: * Checks whether the specified literal matches to the specified language.
118: * <p>
119: * If the literal is not a string, it automatically matches. If it is a string, it matches if {@code language} is
120: * not specified, it is without language tag or if the language tag matches the specified language.
121: *
122: * @param literal Literal to check
123: * @param language Expected language, possibly {@code null}
124: * @return {@code true} if the literal matches the language, {@code false} otherwise
125: */
126: public static boolean doesLanguageMatch(OWLLiteral literal, String language) {
127:• assert literal != null;
128:
129: final OWLDatatype datatype = literal.getDatatype();
130:• if (datatype.isBuiltIn() && datatype.isString() || datatype.isRDFPlainLiteral()) {
131:• return language == null || literal.getLang().isEmpty() || literal.getLang().equals(language);
132: }
133: return true;
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: }