Skip to content

Package: AxiomBuilder

AxiomBuilder

nameinstructionbranchcomplexitylinemethod
AxiomBuilder(NamedResource, Map, Assertion)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createValue(Assertion, Value)
M: 4 C: 55
93%
M: 2 C: 11
85%
M: 2 C: 7
78%
M: 2 C: 11
85%
M: 0 C: 1
100%
lambda$statementToAxiom$0(Assertion, Value)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
resolveAssertion(IRI)
M: 6 C: 25
81%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 7
88%
M: 0 C: 1
100%
resolveValue(Assertion, Value)
M: 0 C: 26
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
resolveValue(Statement, Assertion)
M: 0 C: 14
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 3
100%
M: 0 C: 1
100%
statementToAxiom(Statement)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
statementToAxiom(Statement, Assertion)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 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.sesame.util;
16:
17: import cz.cvut.kbss.ontodriver.model.*;
18: import org.eclipse.rdf4j.model.IRI;
19: import org.eclipse.rdf4j.model.Literal;
20: import org.eclipse.rdf4j.model.Resource;
21: import org.eclipse.rdf4j.model.Statement;
22:
23: import java.util.Map;
24: import java.util.Optional;
25:
26: public class AxiomBuilder {
27:
28: private final NamedResource subject;
29: private final Map<IRI, Assertion> propertyToAssertion;
30:
31: private final Assertion unspecifiedProperty;
32:
33: public AxiomBuilder(NamedResource subject, Map<IRI, Assertion> propertyToAssertion, Assertion unspecifiedProperty) {
34: this.subject = subject;
35: this.propertyToAssertion = propertyToAssertion;
36: this.unspecifiedProperty = unspecifiedProperty;
37: }
38:
39: public Axiom<?> statementToAxiom(Statement statement) {
40: final Assertion assertion = resolveAssertion(statement.getPredicate());
41:
42: return statementToAxiom(statement, assertion);
43: }
44:
45: private Optional<Value<?>> resolveValue(Statement stmt, Assertion assertion) {
46:• if (assertion == null || SesameUtils.isBlankNode(stmt.getObject())) {
47: return Optional.empty();
48: }
49: return createValue(assertion, stmt.getObject());
50: }
51:
52: public Axiom<?> statementToAxiom(Statement statement, Assertion assertion) {
53: final Optional<Value<?>> val = resolveValue(statement, assertion);
54: return val.map(v -> new AxiomImpl<>(subject, assertion, v)).orElse(null);
55: }
56:
57: private Assertion resolveAssertion(IRI predicate) {
58: Assertion assertion = propertyToAssertion.get(predicate);
59:• if (assertion == null) {
60:• if (unspecifiedProperty != null) {
61: assertion = Assertion
62: .createPropertyAssertion(SesameUtils.toJavaUri(predicate), unspecifiedProperty.isInferred());
63: }
64:• } else if (assertion.getType() == Assertion.AssertionType.PROPERTY) {
65: // If the property was unspecified, create assertion based on the actual property URI
66: assertion = Assertion.createPropertyAssertion(SesameUtils.toJavaUri(predicate), assertion.isInferred());
67: }
68: return assertion;
69: }
70:
71: private Optional<Value<?>> createValue(Assertion assertion, org.eclipse.rdf4j.model.Value value) {
72: final Assertion.AssertionType assertionType = assertion.getType();
73:• switch (assertionType) {
74: case DATA_PROPERTY:
75:• if (!(value instanceof Literal) || !SesameUtils.doesLanguageMatch((Literal) value, assertion)) {
76: return Optional.empty();
77: }
78: return Optional.of(new Value<>(SesameUtils.getDataPropertyValue((Literal) value)));
79: case CLASS:
80:• if (!(value instanceof Resource)) {
81: return Optional.empty();
82: }
83: return Optional.of(new Value<>(SesameUtils.toJavaUri((Resource) value)));
84: case OBJECT_PROPERTY:
85:• if (!(value instanceof Resource)) {
86: return Optional.empty();
87: }
88: return Optional.of(new Value<>(NamedResource.create(value.stringValue())));
89: case ANNOTATION_PROPERTY: // Intentional fall-through
90: case PROPERTY:
91: return resolveValue(assertion, value);
92: }
93: return Optional.empty();
94: }
95:
96: private Optional<Value<?>> resolveValue(Assertion assertion, org.eclipse.rdf4j.model.Value value) {
97:• if (value instanceof Literal) {
98:• if (!SesameUtils.doesLanguageMatch((Literal) value, assertion)) {
99: return Optional.empty();
100: }
101: return Optional.of(new Value<>(SesameUtils.getDataPropertyValue((Literal) value)));
102: } else {
103: return Optional.of(new Value<>(NamedResource.create(value.stringValue())));
104: }
105: }
106: }