Package: OwlapiUtils
OwlapiUtils
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
OwlapiUtils() |
|
|
|
|
|
||||||||||||||||||||
createOWLLiteralFromValue(Object, OWLDataFactory, String) |
|
|
|
|
|
||||||||||||||||||||
getIndividual(NamedResource, OWLDataFactory) |
|
|
|
|
|
||||||||||||||||||||
isIndividualIri(Object) |
|
|
|
|
|
||||||||||||||||||||
owlLiteralToValue(OWLLiteral) |
|
|
|
|
|
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.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: /**
39: * Creates OWLLiteral from the specified Java instance.
40: *
41: * @param value The value to transform
42: * @param dataFactory Data factory
43: * @param lang Ontology language
44: * @return OWLLiteral representing the value
45: * @throws IllegalArgumentException If {@code value} is of unsupported type
46: */
47: public static OWLLiteral createOWLLiteralFromValue(Object value, OWLDataFactory dataFactory, String lang) {
48: Objects.requireNonNull(value);
49:• if (value instanceof Integer) {
50: // Java implementations map int/Integer to xsd:int, because xsd:integer is unbounded, whereas xsd:int is 32-bit signed, same as Java
51: return dataFactory.getOWLLiteral(value.toString(), OWL2Datatype.XSD_INT);
52:• } else if (value instanceof Long) {
53: return dataFactory.getOWLLiteral(value.toString(), OWL2Datatype.XSD_LONG);
54:• } else if (value instanceof Boolean) {
55: return dataFactory.getOWLLiteral((Boolean) value);
56:• } else if (value instanceof Double) {
57: return dataFactory.getOWLLiteral((Double) value);
58:• } else if (value instanceof String) {
59: return dataFactory.getOWLLiteral((String) value, lang);
60:• } else if (value instanceof Date) {
61: SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
62: return dataFactory.getOWLLiteral(sdf.format(((Date) value)),
63: dataFactory.getOWLDatatype(OWL2Datatype.XSD_DATE_TIME.getIRI()));
64:• } else if (value.getClass().isEnum()) {
65: return dataFactory.getOWLLiteral(value.toString());
66: } else {
67: throw new IllegalArgumentException("Unsupported value " + value + " of type " + value.getClass());
68: }
69: }
70:
71: /**
72: * Transforms OWLLiteral to a plain Java object (boxed primitive or date/time).
73: *
74: * @param literal The literal to transform
75: * @return Transformed value
76: * @throws IllegalArgumentException If the literal is of unsupported type
77: */
78: public static Object owlLiteralToValue(final OWLLiteral literal) {
79:• if (literal.isRDFPlainLiteral()) {
80: return literal.getLiteral();
81:• } else if (literal.getDatatype().isBuiltIn())
82:• switch (literal.getDatatype().getBuiltInDatatype()) {
83: case XSD_SHORT:
84: return Short.parseShort(literal.getLiteral());
85: case XSD_LONG:
86: return Long.parseLong(literal.getLiteral());
87: case XSD_INT:
88: case XSD_INTEGER:
89: return Integer.parseInt(literal.getLiteral());
90: case XSD_DOUBLE:
91: case XSD_DECIMAL:
92: return Double.parseDouble(literal.getLiteral());
93: case XSD_FLOAT:
94: return Float.parseFloat(literal.getLiteral());
95: case XSD_STRING:
96: case RDF_XML_LITERAL:
97: return literal.getLiteral();
98: case XSD_BOOLEAN:
99: return Boolean.parseBoolean(literal.getLiteral());
100: case XSD_ANY_URI:
101: return URI.create(literal.getLiteral());
102: case XSD_DATE_TIME_STAMP:
103: case XSD_DATE_TIME:
104: try {
105: return new SimpleDateFormat(DATE_TIME_FORMAT).parse(literal.getLiteral());
106: } catch (ParseException e) {
107: throw new IllegalArgumentException(
108: "The date time '" + literal.getLiteral() + "' cannot be parsed.");
109: }
110: }
111:
112: throw new IllegalArgumentException("Unsupported datatype: " + literal.getDatatype());
113: }
114:
115: /**
116: * Gets OWLNamedIndividual for the specified named resource.
117: *
118: * @param subject Named resource to transform to individual
119: * @param dataFactory OWL data factory
120: * @return OWLNamedIndividual
121: */
122: public static OWLNamedIndividual getIndividual(NamedResource subject, OWLDataFactory dataFactory) {
123: return dataFactory.getOWLNamedIndividual(IRI.create(subject.getIdentifier()));
124: }
125:
126: /**
127: * Checks whether the specified value is a valid IRI.
128: * <p/>
129: * Only absolute IRIs are accepted.
130: *
131: * @param value The value to check
132: * @return {@code true} for instances of {@link NamedResource}, {@link URI}, {@link URL} or {@link IRI} and for
133: * Strings parsable by {@link URI#create(String)}.
134: */
135: public static boolean isIndividualIri(Object value) {
136:• if (value instanceof NamedResource || value instanceof URI || value instanceof URL || value instanceof IRI) {
137: return true;
138: }
139:• if (!(value instanceof String)) {
140: return false;
141: }
142: try {
143: final IRI iri = IRI.create(value.toString());
144: return iri.isAbsolute();
145: } catch (IllegalArgumentException e) {
146: return false;
147: }
148: }
149: }