Skip to content

Package: OwlapiUtils

OwlapiUtils

nameinstructionbranchcomplexitylinemethod
OwlapiUtils()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createOWLLiteralFromValue(Object, OWLDataFactory, String)
M: 4 C: 76
95%
M: 1 C: 13
93%
M: 1 C: 7
88%
M: 1 C: 17
94%
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%
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%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
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: *
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.text.ParseException;
26: import java.text.SimpleDateFormat;
27: import java.util.Date;
28: import java.util.Objects;
29:
30: /**
31: * Utility methods for the OWLAPI driver.
32: */
33: public class OwlapiUtils {
34:
35: private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
36:
37: /**
38: * Creates OWLLiteral from the specified Java instance.
39: *
40: * @param value The value to transform
41: * @param dataFactory Data factory
42: * @param lang Ontology language
43: * @return OWLLiteral representing the value
44: * @throws IllegalArgumentException If {@code value} is of unsupported type
45: */
46: public static OWLLiteral createOWLLiteralFromValue(Object value, OWLDataFactory dataFactory, String lang) {
47: Objects.requireNonNull(value);
48:• if (value instanceof Integer) {
49: return dataFactory.getOWLLiteral((Integer) value);
50:• } else if (value instanceof Long) {
51: return dataFactory.getOWLLiteral(value.toString(), OWL2Datatype.XSD_LONG);
52:• } else if (value instanceof Boolean) {
53: return dataFactory.getOWLLiteral((Boolean) value);
54:• } else if (value instanceof Double) {
55: return dataFactory.getOWLLiteral((Double) value);
56:• } else if (value instanceof String) {
57: return dataFactory.getOWLLiteral((String) value, lang);
58:• } else if (value instanceof Date) {
59: SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
60: return dataFactory.getOWLLiteral(sdf.format(((Date) value)),
61: dataFactory.getOWLDatatype(OWL2Datatype.XSD_DATE_TIME.getIRI()));
62:• } else if (value.getClass().isEnum()) {
63: return dataFactory.getOWLLiteral(value.toString());
64: } else {
65: throw new IllegalArgumentException();
66: }
67: }
68:
69: /**
70: * Transforms OWLLiteral to a plain Java object (boxed primitive or date/time).
71: *
72: * @param literal The literal to transform
73: * @return Transformed value
74: * @throws IllegalArgumentException If the literal is of unsupported type
75: */
76: public static Object owlLiteralToValue(final OWLLiteral literal) {
77:• if (literal.isRDFPlainLiteral()) {
78: return literal.getLiteral();
79:• } else if (literal.getDatatype().isBuiltIn())
80:• switch (literal.getDatatype().getBuiltInDatatype()) {
81: case XSD_SHORT:
82: return Short.parseShort(literal.getLiteral());
83: case XSD_LONG:
84: return Long.parseLong(literal.getLiteral());
85: case XSD_INT:
86: case XSD_INTEGER:
87: return Integer.parseInt(literal.getLiteral());
88: case XSD_DOUBLE:
89: case XSD_DECIMAL:
90: return Double.parseDouble(literal.getLiteral());
91: case XSD_FLOAT:
92: return Float.parseFloat(literal.getLiteral());
93: case XSD_STRING:
94: case RDF_XML_LITERAL:
95: return literal.getLiteral();
96: case XSD_BOOLEAN:
97: return Boolean.parseBoolean(literal.getLiteral());
98: case XSD_ANY_URI:
99: return URI.create(literal.getLiteral());
100: case XSD_DATE_TIME_STAMP:
101: case XSD_DATE_TIME:
102: try {
103: return new SimpleDateFormat(DATE_TIME_FORMAT).parse(literal.getLiteral());
104: } catch (ParseException e) {
105: throw new IllegalArgumentException(
106: "The date time '" + literal.getLiteral() + "' cannot be parsed.");
107: }
108: }
109:
110: throw new IllegalArgumentException("Unsupported datatype: " + literal.getDatatype());
111: }
112:
113: /**
114: * Gets OWLNamedIndividual for the specified named resource.
115: *
116: * @param subject Named resource to transform to individual
117: * @param dataFactory OWL data factor
118: * @return OWLNamedIndividual
119: */
120: public static OWLNamedIndividual getIndividual(NamedResource subject, OWLDataFactory dataFactory) {
121: return dataFactory.getOWLNamedIndividual(IRI.create(subject.getIdentifier()));
122: }
123: }