Skip to content

Package: JenaUtils

JenaUtils

nameinstructionbranchcomplexitylinemethod
JenaUtils()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
createLiteral(Literal)
M: 0 C: 23
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
lambda$valueToRdfNode$0(LangString, String)
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%
lambda$valueToRdfNode$1(LangString)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
literalToValue(Literal)
M: 0 C: 25
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
valueToRdfNode(Assertion, Value)
M: 0 C: 88
100%
M: 0 C: 16
100%
M: 0 C: 9
100%
M: 0 C: 25
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.jena.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.Value;
22: import cz.cvut.kbss.ontodriver.util.IdentifierUtils;
23: import org.apache.jena.datatypes.BaseDatatype;
24: import org.apache.jena.datatypes.RDFDatatype;
25: import org.apache.jena.datatypes.TypeMapper;
26: import org.apache.jena.datatypes.xsd.impl.RDFLangString;
27: import org.apache.jena.rdf.model.Literal;
28: import org.apache.jena.rdf.model.RDFNode;
29: import org.apache.jena.rdf.model.ResourceFactory;
30:
31: import java.time.temporal.TemporalAccessor;
32: import java.time.temporal.TemporalAmount;
33: import java.util.Date;
34: import java.util.Objects;
35:
36: /**
37: * Utility methods for working with Jena API.
38: */
39: public class JenaUtils {
40:
41: private JenaUtils() {
42: throw new AssertionError();
43: }
44:
45: /**
46: * Transforms the specified value to an {@link RDFNode}, be it a resource or a literal.
47: *
48: * @param assertion Assertion representing the asserted property
49: * @param value Value to transform
50: * @return Jena RDFNode
51: */
52: public static <T> RDFNode valueToRdfNode(Assertion assertion, Value<T> value) {
53: final T val = value.getValue();
54: Objects.requireNonNull(val);
55:• if (IdentifierUtils.isResourceIdentifierType(val.getClass())) {
56: return ResourceFactory.createResource(value.stringValue());
57:• } else if (val instanceof LangString) {
58: final LangString langString = (LangString) val;
59: return langString.getLanguage().map(lang -> ResourceFactory.createLangLiteral(langString.getValue(), lang))
60: .orElseGet(() -> ResourceFactory.createTypedLiteral(langString.getValue()));
61:• } else if (val instanceof String) {
62:• return assertion.hasLanguage() ? ResourceFactory.createLangLiteral((String) val,
63: assertion.getLanguage()) :
64: ResourceFactory.createTypedLiteral(val);
65:• } else if (val instanceof cz.cvut.kbss.ontodriver.model.Literal) {
66: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = (cz.cvut.kbss.ontodriver.model.Literal) val;
67: return createLiteral(ontoLiteral);
68:• } else if (val instanceof Date) {
69: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((Date) val).toInstant());
70: return createLiteral(ontoLiteral);
71:• } else if (val instanceof TemporalAccessor) {
72: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((TemporalAccessor) val));
73: return createLiteral(ontoLiteral);
74:• } else if (val instanceof TemporalAmount) {
75: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((TemporalAmount) val));
76: return createLiteral(ontoLiteral);
77: } else {
78: return ResourceFactory.createTypedLiteral(val);
79: }
80: }
81:
82: private static Literal createLiteral(cz.cvut.kbss.ontodriver.model.Literal ontoLiteral) {
83: final TypeMapper typeMapper = TypeMapper.getInstance();
84: RDFDatatype datatype = typeMapper.getTypeByName(ontoLiteral.getDatatype());
85:• if (datatype == null) {
86: // If the datatype does not exist, register it as a base datatype without any special behavior
87: datatype = new BaseDatatype(ontoLiteral.getDatatype());
88: typeMapper.registerDatatype(datatype);
89: }
90: return ResourceFactory.createTypedLiteral(ontoLiteral.getLexicalForm(), datatype);
91: }
92:
93: public static Object literalToValue(Literal literal) {
94:• if (RDFLangString.rdfLangString.equals(literal.getDatatype())) {
95: return new LangString(literal.getString(), literal.getLanguage());
96: }
97: final cz.cvut.kbss.ontodriver.model.Literal lit = cz.cvut.kbss.ontodriver.model.Literal.from(
98: literal.getLexicalForm(), literal.getDatatypeURI());
99: return XsdDatatypeMapper.getInstance().map(lit).orElse(lit);
100: }
101: }