Skip to contentMethod: owlLiteralToValue(OWLLiteral)
1: /**
2: * Copyright (C) 2023 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.jopa.datatype.xsd.XsdDatatypeMapper;
18: import cz.cvut.kbss.jopa.owlapi.DatatypeTransformer;
19: import cz.cvut.kbss.ontodriver.model.Assertion;
20: import cz.cvut.kbss.ontodriver.model.LangString;
21: import cz.cvut.kbss.ontodriver.model.Literal;
22: import cz.cvut.kbss.ontodriver.model.NamedResource;
23: import cz.cvut.kbss.ontodriver.owlapi.config.Constants;
24: import cz.cvut.kbss.ontodriver.util.IdentifierUtils;
25: import org.semanticweb.owlapi.model.*;
26: import org.semanticweb.owlapi.vocab.OWL2Datatype;
27:
28: import java.net.URI;
29: import java.net.URL;
30:
31: /**
32: * Utility methods for the OWLAPI driver.
33: */
34: public class OwlapiUtils {
35:
36: private OwlapiUtils() {
37: throw new AssertionError("Can't create instance.");
38: }
39:
40: /**
41: * Creates OWLLiteral from the specified Java instance.
42: *
43: * @param value The value to transform
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, String lang) {
49: return DatatypeTransformer.transform(value, lang);
50: }
51:
52: /**
53: * Transforms OWLLiteral to a plain Java object (boxed primitive or date/time).
54: *
55: * @param owlLiteral The literal to transform
56: * @return Transformed value
57: * @throws IllegalArgumentException If the literal is of unsupported type
58: */
59: public static Object owlLiteralToValue(final OWLLiteral owlLiteral) {
60: final OWLDatatype datatype = owlLiteral.getDatatype();
61:• if (datatype.isBuiltIn() && datatype.getBuiltInDatatype() == OWL2Datatype.RDF_LANG_STRING) {
62: return new LangString(owlLiteral.getLiteral(), owlLiteral.getLang());
63: }
64: final Literal literal = Literal.from(owlLiteral.getLiteral(), owlLiteral.getDatatype().toStringID());
65: return XsdDatatypeMapper.getInstance().map(literal).orElse(literal);
66: }
67:
68: /**
69: * Checks whether the specified literal matches to the language of the specified assertion.
70: * <p>
71: * If the literal is not a string, it automatically matches. If it is a string, it matches if assertion language is
72: * not specified, it is without language tag or if the language tag matches the specified assertion language.
73: *
74: * @param literal Literal to check
75: * @param assertion Assertion with language specification (possibly empty)
76: * @return {@code true} if the literal matches the assertion language, {@code false} otherwise
77: */
78: public static boolean doesLanguageMatch(OWLLiteral literal, Assertion assertion) {
79: assert literal != null;
80: assert assertion != null;
81:
82: return !assertion.hasLanguage() || literal.getLang().isEmpty() ||
83: literal.getLang().equals(assertion.getLanguage());
84: }
85:
86: /**
87: * Retrieves language from the specified assertion.
88: * <p>
89: * If the assertion does not specify language, {@link Constants#DEFAULT_LANGUAGE} is returned.
90: *
91: * @param assertion Assertion to get language for
92: * @return Assertion language or default language
93: */
94: public static String getAssertionLanguage(Assertion assertion) {
95: return assertion.hasLanguage() ? assertion.getLanguage() : Constants.DEFAULT_LANGUAGE;
96: }
97:
98: /**
99: * Gets OWLNamedIndividual for the specified named resource.
100: *
101: * @param subject Named resource to transform to individual
102: * @param dataFactory OWL data factory
103: * @return OWLNamedIndividual
104: */
105: public static OWLNamedIndividual getIndividual(NamedResource subject, OWLDataFactory dataFactory) {
106: return dataFactory.getOWLNamedIndividual(IRI.create(subject.getIdentifier()));
107: }
108:
109: /**
110: * Checks whether the specified value is a valid IRI.
111: * <p>
112: * Only absolute IRIs are accepted.
113: *
114: * @param value The value to check
115: * @return {@code true} for instances of {@link NamedResource}, {@link URI}, {@link URL} or {@link IRI} and for
116: * Strings parseable by {@link URI#create(String)}.
117: */
118: public static boolean isIndividualIri(Object value) {
119: return IdentifierUtils.isResourceIdentifierType(value.getClass()) || value instanceof IRI;
120: }
121: }