Skip to content

Package: Rdf4jUtils

Rdf4jUtils

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