Skip to content

Method: createAssertionForStatement(Statement)

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