Skip to content

Package: AbstractAxiomLoader

AbstractAxiomLoader

nameinstructionbranchcomplexitylinemethod
AbstractAxiomLoader()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
contains(Axiom, URI)
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createAssertionForStatement(Statement)
M: 0 C: 20
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
doesLanguageMatch(Assertion, Literal)
M: 0 C: 23
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
find(NamedResource, URI)
M: 0 C: 50
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
lambda$find$0(List, NamedResource, Assertion, Value)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
resolveValue(Assertion, RDFNode)
M: 2 C: 44
96%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 1 C: 9
90%
M: 0 C: 1
100%
shouldSkipLiteral(Assertion, RDFNode)
M: 4 C: 18
82%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 3
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%

Coverage

1: /**
2: * Copyright (C) 2019 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.jena;
14:
15: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
16: import cz.cvut.kbss.ontodriver.jena.util.JenaUtils;
17: import cz.cvut.kbss.ontodriver.model.*;
18: import org.apache.jena.datatypes.xsd.XSDDateTime;
19: import org.apache.jena.rdf.model.*;
20: import org.apache.jena.vocabulary.RDF;
21:
22: import java.net.URI;
23: import java.util.*;
24:
25: import static cz.cvut.kbss.ontodriver.model.Assertion.createDataPropertyAssertion;
26: import static cz.cvut.kbss.ontodriver.model.Assertion.createObjectPropertyAssertion;
27:
28: abstract class AbstractAxiomLoader {
29:
30: boolean inferred = false;
31:
32: /**
33: * Checks whether the storage contains the specified axiom.
34: *
35: * @param axiom Axiom whose existence should be verified
36: * @param context Context to search, optional
37: * @return {@code true} if the axiom exists, {@code false} otherwise
38: */
39: boolean contains(Axiom<?> axiom, URI context) {
40: final Resource subject = ResourceFactory.createResource(axiom.getSubject().getIdentifier().toString());
41: final Property property = ResourceFactory.createProperty(axiom.getAssertion().getIdentifier().toString());
42: final RDFNode object = JenaUtils.valueToRdfNode(axiom.getAssertion(), axiom.getValue());
43: return contains(subject, property, object, context);
44: }
45:
46: abstract boolean contains(Resource subject, Property property, RDFNode object, URI context);
47:
48: Assertion createAssertionForStatement(Statement statement) {
49:• if (statement.getObject().isResource()) {
50: return createObjectPropertyAssertion(URI.create(statement.getPredicate().getURI()), inferred);
51: } else {
52: return createDataPropertyAssertion(URI.create(statement.getPredicate().getURI()), inferred);
53: }
54: }
55:
56: /**
57: * Loads statements corresponding to subject and assertions specified by the arguments.
58: * <p>
59: * The specified descriptor is used in context and subject resolution.
60: *
61: * @param descriptor Loading descriptor, contains subject and context info
62: * @param assertions Assertions to load
63: * @return Matching axioms
64: */
65: abstract Collection<Axiom<?>> find(AxiomDescriptor descriptor, Map<String, Assertion> assertions);
66:
67: /**
68: * Loads all property statements with the specified subject.
69: * <p>
70: * Note that type assertion statements (those with property {@code rdf:type}) are skipped.
71: *
72: * @param subject Statement subject
73: * @param context Context identifier, optional
74: * @return Matching statements
75: */
76: Collection<Axiom<?>> find(NamedResource subject, URI context) {
77: final Resource resource = ResourceFactory.createResource(subject.getIdentifier().toString());
78: final Collection<Statement> statements = findStatements(resource, null, context);
79: final List<Axiom<?>> axioms = new ArrayList<>(statements.size());
80:• for (Statement statement : statements) {
81:• if (statement.getPredicate().equals(RDF.type)) {
82: continue;
83: }
84: final Assertion a = createAssertionForStatement(statement);
85: resolveValue(a, statement.getObject()).ifPresent(v -> axioms.add(new AxiomImpl<>(subject, a, v)));
86: }
87: return axioms;
88: }
89:
90: abstract Collection<Statement> findStatements(Resource subject, Property property, URI context);
91:
92: Optional<Value<?>> resolveValue(Assertion assertion, RDFNode object) {
93:• if (object.isResource()) {
94:• if (object.isAnon() || assertion.getType() == Assertion.AssertionType.DATA_PROPERTY) {
95: return Optional.empty();
96: }
97: return Optional.of(new Value<>(NamedResource.create(object.asResource().getURI())));
98: } else {
99:• if (shouldSkipLiteral(assertion, object)) {
100: return Optional.empty();
101: }
102: Object val = JenaUtils.literalToValue(object.asLiteral());
103: // Jena does not like java.util.Date
104:• if (val instanceof XSDDateTime) {
105: val = ((XSDDateTime) val).asCalendar().getTime();
106: }
107: return Optional.of(new Value<>(val));
108: }
109: }
110:
111: private boolean shouldSkipLiteral(Assertion assertion, RDFNode object) {
112:• assert object.isLiteral();
113:• return assertion.getType() == Assertion.AssertionType.OBJECT_PROPERTY ||
114:• !doesLanguageMatch(assertion, object.asLiteral());
115: }
116:
117: private boolean doesLanguageMatch(Assertion assertion, Literal literal) {
118:• if (!(literal.getValue() instanceof String) || !assertion.hasLanguage()) {
119: return true;
120: }
121:• return Objects.equals(assertion.getLanguage(), literal.getLanguage()) || literal.getLanguage().isEmpty();
122: }
123: }