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