Skip to content

Package: ValueConverter

ValueConverter

nameinstructionbranchcomplexitylinemethod
ValueConverter(ValueFactory)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
fromRdf4jValue(Assertion, Value)
M: 4 C: 45
92%
M: 2 C: 11
85%
M: 2 C: 7
78%
M: 2 C: 11
85%
M: 0 C: 1
100%
getValueAsIri(Object)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
language(Assertion)
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
resolvePropertyValue(Assertion, Object)
M: 0 C: 22
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
resolveUnknownPropertyTypeValue(Assertion, Value)
M: 0 C: 20
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
toRdf4jValue(Assertion, Object)
M: 7 C: 22
76%
M: 1 C: 3
75%
M: 1 C: 3
75%
M: 1 C: 4
80%
M: 0 C: 1
100%
toRdf4jValue(Assertion, Value)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
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.ontodriver.model.Assertion;
21: import cz.cvut.kbss.ontodriver.model.NamedResource;
22: import cz.cvut.kbss.ontodriver.rdf4j.config.Constants;
23: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
24: import org.eclipse.rdf4j.model.IRI;
25: import org.eclipse.rdf4j.model.Literal;
26: import org.eclipse.rdf4j.model.Resource;
27: import org.eclipse.rdf4j.model.Value;
28: import org.eclipse.rdf4j.model.ValueFactory;
29:
30: import java.util.Optional;
31:
32: /**
33: * Converts values between the OntoDriver model and RDF4J model.
34: */
35: public class ValueConverter {
36:
37: private final ValueFactory vf;
38:
39: public ValueConverter(ValueFactory vf) {
40: this.vf = vf;
41: }
42:
43: public static Optional<Object> fromRdf4jValue(Assertion assertion, Value value) {
44: final Assertion.AssertionType assertionType = assertion.getType();
45:• switch (assertionType) {
46: case DATA_PROPERTY:
47:• if (!(value instanceof Literal) || !Rdf4jUtils.doesLanguageMatch((Literal) value, assertion)) {
48: return Optional.empty();
49: }
50: return Optional.of(Rdf4jUtils.getLiteralValue((Literal) value));
51: case CLASS:
52:• if (!(value instanceof Resource)) {
53: return Optional.empty();
54: }
55: return Optional.ofNullable(Rdf4jUtils.toJavaUri((Resource) value));
56: case OBJECT_PROPERTY:
57:• if (!(value instanceof Resource)) {
58: return Optional.empty();
59: }
60: return Optional.of(NamedResource.create(value.stringValue()));
61: case ANNOTATION_PROPERTY: // Intentional fall-through
62: case PROPERTY:
63: return resolveUnknownPropertyTypeValue(assertion, value);
64: }
65: return Optional.empty();
66: }
67:
68: private static Optional<Object> resolveUnknownPropertyTypeValue(Assertion assertion, Value value) {
69:• if (value instanceof Literal) {
70:• if (!Rdf4jUtils.doesLanguageMatch((Literal) value, assertion)) {
71: return Optional.empty();
72: }
73: return Optional.of(Rdf4jUtils.getLiteralValue((Literal) value));
74: } else {
75: return Optional.of(NamedResource.create(value.stringValue()));
76: }
77: }
78:
79: public Value toRdf4jValue(Assertion assertion, cz.cvut.kbss.ontodriver.model.Value<?> val) throws Rdf4jDriverException {
80: return toRdf4jValue(assertion, val.getValue());
81: }
82:
83: public Value toRdf4jValue(Assertion assertion, Object val) throws Rdf4jDriverException {
84:• switch (assertion.getType()) {
85: case DATA_PROPERTY:
86: return Rdf4jUtils.createLiteral(val, language(assertion), vf);
87: case CLASS:
88: case OBJECT_PROPERTY:
89: return getValueAsIri(val);
90: case ANNOTATION_PROPERTY: // Intentional fall-through
91: case PROPERTY:
92: return resolvePropertyValue(assertion, val);
93: default:
94: // Failsafe
95: throw new IllegalArgumentException("Unsupported assertion type " + assertion.getType());
96: }
97: }
98:
99: private static String language(Assertion assertion) {
100:• return assertion.hasLanguage() ? assertion.getLanguage() : Constants.DEFAULT_LANG;
101: }
102:
103: private IRI getValueAsIri(Object val) throws Rdf4jDriverException {
104: try {
105: return vf.createIRI(val.toString());
106: } catch (IllegalArgumentException e) {
107: throw new Rdf4jDriverException(e);
108: }
109: }
110:
111: private Value resolvePropertyValue(Assertion assertion, Object val) {
112:• if (!(val instanceof String && !assertion.hasLanguage()) && Rdf4jUtils.isResourceIdentifier(val)) {
113: return vf.createIRI(val.toString());
114: } else {
115: return Rdf4jUtils.createLiteral(val, language(assertion), vf);
116: }
117: }
118: }