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