Skip to content

Package: Rdf4jUtils

Rdf4jUtils

nameinstructionbranchcomplexitylinemethod
createLiteral(Object, String, ValueFactory)
M: 23 C: 163
88%
M: 4 C: 34
89%
M: 4 C: 16
80%
M: 3 C: 37
93%
M: 0 C: 1
100%
createLiteral(ValueFactory, Literal)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
doesLanguageMatch(Literal, Assertion)
M: 4 C: 45
92%
M: 3 C: 13
81%
M: 3 C: 6
67%
M: 0 C: 10
100%
M: 0 C: 1
100%
getLiteralValue(Literal)
M: 8 C: 36
82%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 8
100%
M: 0 C: 1
100%
isBlankNode(Value)
M: 4 C: 6
60%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 2
100%
M: 0 C: 1
100%
isResourceIdentifier(Object)
M: 0 C: 10
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 1
100%
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%
toJavaUri(Resource)
M: 8 C: 9
53%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 3 C: 3
50%
M: 0 C: 1
100%
toRdf4jIri(NamedResource, ValueFactory)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toRdf4jIri(URI, ValueFactory)
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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.rdf4j.util;
16:
17: import cz.cvut.kbss.jopa.datatype.xsd.XsdDatatypeMapper;
18: import cz.cvut.kbss.jopa.datatype.xsd.XsdTemporalMapper;
19: import cz.cvut.kbss.ontodriver.model.Assertion;
20: import cz.cvut.kbss.ontodriver.model.LangString;
21: import cz.cvut.kbss.ontodriver.model.NamedResource;
22: import cz.cvut.kbss.ontodriver.util.IdentifierUtils;
23: import org.eclipse.rdf4j.model.*;
24: import org.eclipse.rdf4j.model.vocabulary.RDF;
25: import org.eclipse.rdf4j.model.vocabulary.XSD;
26: import org.slf4j.LoggerFactory;
27:
28: import java.math.BigDecimal;
29: import java.math.BigInteger;
30: import java.net.URI;
31: import java.time.temporal.TemporalAccessor;
32: import java.time.temporal.TemporalAmount;
33: import java.util.Date;
34:
35: /**
36: * Utility methods for the RDF4J driver.
37: */
38: public final class Rdf4jUtils {
39:
40: private Rdf4jUtils() {
41: // Private constructor
42: }
43:
44: /**
45: * Gets value of the specified literal as the corresponding Java object.
46: * <p>
47: * Primitives are returned boxed. If the type cannot be mapped to a corresponding Java type,
48: * it is returned as {@link cz.cvut.kbss.ontodriver.model.Literal}.
49: *
50: * @param literal RDF literal value
51: * @return Java value corresponding to datatype
52: */
53: public static Object getLiteralValue(Literal literal) {
54:• assert literal != null;
55:
56: final IRI datatype = literal.getDatatype();
57:• assert datatype != null;
58:
59:• if (datatype.equals(RDF.LANGSTRING)) {
60: return new LangString(literal.stringValue(), literal.getLanguage().orElse(null));
61: } else {
62: final cz.cvut.kbss.ontodriver.model.Literal lit = cz.cvut.kbss.ontodriver.model.Literal.from(
63: literal.getLabel(), datatype.stringValue());
64: return XsdDatatypeMapper.getInstance().map(lit).orElse(lit);
65: }
66: }
67:
68: /**
69: * Checks whether the language of the specified literal matches the specified assertion language.
70: * <p>
71: * If the assertion does not specify a language, any literal will match. If the literal is not a string, it
72: * matches as well.
73: *
74: * @param literal Literal to check
75: * @param assertion Assertion
76: * @return {@code false} if the literal is a string literal and its language does not match the one specified by the
77: * assertion, {@code true} otherwise
78: */
79: public static boolean doesLanguageMatch(Literal literal, Assertion assertion) {
80:• assert assertion != null;
81:• if (!assertion.hasLanguage()) {
82: return true;
83: }
84: final String language = assertion.getLanguage();
85: final IRI datatype = literal.getDatatype();
86:• if (datatype.equals(XSD.STRING) || datatype.equals(XSD.NORMALIZEDSTRING) ||
87:• datatype.equals(RDF.LANGSTRING)) {
88:• return language == null || literal.getLanguage().isEmpty() ||
89:• literal.getLanguage().get().equals(language);
90: }
91: return true;
92: }
93:
94: /**
95: * Creates RDF4J literal from the specified value.
96: *
97: * @param value The value to transform
98: * @param language Language to add to string literals, optional
99: * @param vf RDF4J value factory
100: * @return RFD4J Literal
101: * @throws IllegalArgumentException If the type of the value is not supported
102: */
103: public static Literal createLiteral(Object value, String language, ValueFactory vf) {
104:• assert value != null;
105:
106:• if (value instanceof Integer) {
107: return vf.createLiteral((Integer) value);
108:• } else if (value instanceof String) {
109:• return language != null ? vf.createLiteral((String) value, language) : vf.createLiteral((String) value);
110:• } else if (value instanceof LangString) {
111: final LangString ls = (LangString) value;
112:• return ls.getLanguage().isPresent() ? vf.createLiteral(ls.getValue(), ls.getLanguage().get()) :
113: vf.createLiteral(ls.getValue());
114:• } else if (value instanceof Byte) {
115: return vf.createLiteral((Byte) value);
116:• } else if (value instanceof Short) {
117: return vf.createLiteral((Short) value);
118:• } else if (value instanceof Boolean) {
119: return vf.createLiteral((Boolean) value);
120:• } else if (value instanceof Float) {
121: return vf.createLiteral((Float) value);
122:• } else if (value instanceof Double) {
123: return vf.createLiteral((Double) value);
124:• } else if (value instanceof Long) {
125: return vf.createLiteral((Long) value);
126:• } else if (value instanceof BigInteger) {
127: return vf.createLiteral((BigInteger) value);
128:• } else if (value instanceof BigDecimal) {
129: return vf.createLiteral((BigDecimal) value);
130:• } else if (value instanceof Date) {
131: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((Date) value).toInstant());
132: return createLiteral(vf, ontoLiteral);
133:• } else if (value instanceof TemporalAccessor) {
134: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map((TemporalAccessor) value);
135: return createLiteral(vf, ontoLiteral);
136:• } else if (value instanceof TemporalAmount) {
137: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map((TemporalAmount) value);
138: return createLiteral(vf, ontoLiteral);
139:• } else if (value.getClass().isEnum()) {
140: return vf.createLiteral(value.toString());
141:• } else if (value instanceof cz.cvut.kbss.ontodriver.model.Literal) {
142: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = (cz.cvut.kbss.ontodriver.model.Literal) value;
143: return createLiteral(vf, ontoLiteral);
144: } else {
145: throw new IllegalArgumentException("Unsupported literal type " + value.getClass());
146: }
147: }
148:
149: private static Literal createLiteral(ValueFactory vf, cz.cvut.kbss.ontodriver.model.Literal ontoLiteral) {
150: return vf.createLiteral(ontoLiteral.getLexicalForm(), vf.createIRI(ontoLiteral.getDatatype()));
151: }
152:
153: /**
154: * Checks whether the specified value is a blank node.
155: *
156: * @param value The value to check
157: * @return {@code true} if the value is a blank node, {@code false} otherwise
158: */
159: public static boolean isBlankNode(Value value) {
160:• assert value != null;
161: return value instanceof BNode;
162: }
163:
164: /**
165: * Resolves whether the specified value is a resource identifier.
166: *
167: * Only values of supported identifier types are considered identifiers.
168: *
169: * @param value The value to check
170: * @return {@code true} if the value is either a URI or a URL
171: */
172: public static boolean isResourceIdentifier(Object value) {
173:• return value != null && IdentifierUtils.isResourceIdentifierType(value.getClass());
174: }
175:
176: /**
177: * Constructs a RDF4J IRI from the specified java.net.URI.
178: *
179: * @param javaUri The uri to convert
180: * @param factory RDF4J value factory used for the conversion
181: * @return RDF4J IRI
182: */
183: public static IRI toRdf4jIri(java.net.URI javaUri, ValueFactory factory) {
184:• return (javaUri != null ? factory.createIRI(javaUri.toString()) : null);
185: }
186:
187: /**
188: * Constructs a RDF4J IRI from identifier of the specified resource.
189: * @param resource Resource whose identifier to transform
190: * @param factory RDF4J value factory used for the conversion
191: * @return RDF4J IRI
192: * @see #toRdf4jIri(URI, ValueFactory)
193: */
194: public static IRI toRdf4jIri(NamedResource resource, ValueFactory factory) {
195: return toRdf4jIri(resource.getIdentifier(), factory);
196: }
197:
198: public static java.net.URI toJavaUri(Resource resource) {
199:• if (resource instanceof BNode) {
200: // We have to check for BNode explicitly, because java's URI treats
201: // BNode's identifier as a valid URI
202: return null;
203: }
204: try {
205: return java.net.URI.create(resource.stringValue());
206: } catch (IllegalArgumentException e) {
207: // This shouldn't happen
208: LoggerFactory.getLogger(Rdf4jUtils.class).error("RDF4J resource is not a valid URI: " + e);
209: return null;
210: }
211: }
212: }