-
Notifications
You must be signed in to change notification settings - Fork 14
/
ExplicitAxiomLoader.java
127 lines (112 loc) · 6.12 KB
/
ExplicitAxiomLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Copyright (C) 2019 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.jena;
import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.AxiomImpl;
import cz.cvut.kbss.ontodriver.model.Value;
import cz.cvut.kbss.ontodriver.util.Vocabulary;
import org.apache.jena.rdf.model.*;
import java.net.URI;
import java.util.*;
class ExplicitAxiomLoader extends AbstractAxiomLoader {
private static final Assertion UNSPECIFIED_ASSERTION = Assertion.createUnspecifiedPropertyAssertion(false);
private final StorageConnector connector;
private Map<String, Assertion> assertedProperties;
private Assertion unspecifiedProperty;
ExplicitAxiomLoader(StorageConnector connector) {
this.connector = connector;
}
@Override
boolean contains(Resource subject, Property property, RDFNode object, URI context) {
return connector.contains(subject, property, object, context != null ? context.toString() : null);
}
@Override
Collection<Axiom<?>> find(AxiomDescriptor descriptor, Map<String, Assertion> assertions) {
this.assertedProperties = assertions;
this.unspecifiedProperty = resolveUnspecifiedProperty();
final Resource subject = ResourceFactory.createResource(descriptor.getSubject().getIdentifier().toString());
final Collection<Statement> statements = findStatements(subject, null, descriptor.getSubjectContext());
final List<Axiom<?>> result = transformStatementsToAxioms(descriptor, statements);
result.addAll(loadAxiomsForPropertiesInContext(descriptor, subject));
return result;
}
private Assertion resolveUnspecifiedProperty() {
final Optional<Assertion> unspecified =
assertedProperties.values().stream().filter(a -> a.equals(UNSPECIFIED_ASSERTION)).findAny();
return unspecified.orElse(null);
}
@Override
Collection<Statement> findStatements(Resource subject, Property property, URI context) {
return connector.find(subject, property, null, context != null ? context.toString() : null);
}
private List<Axiom<?>> transformStatementsToAxioms(AxiomDescriptor descriptor, Collection<Statement> statements) {
final List<Axiom<?>> axioms = new ArrayList<>(statements.size());
for (Statement statement : statements) {
final Property property = statement.getPredicate();
if (shouldSkipProperty(property, descriptor)) {
continue;
}
final Assertion a =
assertedProperties.containsKey(property.getURI()) ? assertedProperties.get(property.getURI()) :
createAssertionForStatement(statement);
final Optional<Value<?>> value = resolveValue(a, statement.getObject());
value.ifPresent(v -> axioms.add(new AxiomImpl<>(descriptor.getSubject(), a, v)));
}
return axioms;
}
private boolean shouldSkipProperty(Property property, AxiomDescriptor descriptor) {
final String propertyUri = property.getURI();
// If the property is not mapped and either there is not the unspecified property or the property is rdf:type,
// which is handled by Types
if (!assertedProperties.containsKey(propertyUri) && (unspecifiedProperty == null || propertyUri
.equals(Vocabulary.RDF_TYPE))) {
return true;
}
final Assertion a = assertedProperties.getOrDefault(propertyUri, unspecifiedProperty);
return !assertionContextSameAsSubject(descriptor.getSubjectContext(), descriptor.getAssertionContext(a));
}
private List<Axiom<?>> loadAxiomsForPropertiesInContext(AxiomDescriptor descriptor, Resource subject) {
final List<Axiom<?>> axioms = new ArrayList<>();
for (Assertion a : assertedProperties.values()) {
final URI assertionCtx = descriptor.getAssertionContext(a);
if (assertionContextSameAsSubject(descriptor.getSubjectContext(), assertionCtx)) {
continue;
}
final Property property = ResourceFactory.createProperty(a.getIdentifier().toString());
final Collection<Statement> statements = findStatements(subject, property, assertionCtx);
statements.forEach(statement -> {
final Optional<Value<?>> value = resolveValue(a, statement.getObject());
value.ifPresent(v -> axioms.add(new AxiomImpl<>(descriptor.getSubject(), a, v)));
});
}
if (unspecifiedProperty != null && !assertionContextSameAsSubject(descriptor.getSubjectContext(),
descriptor.getAssertionContext(unspecifiedProperty))) {
final Collection<Statement> statements =
findStatements(subject, null, descriptor.getAssertionContext(unspecifiedProperty));
for (Statement s : statements) {
final Assertion a = createAssertionForStatement(s);
final Optional<Value<?>> value = resolveValue(a, s.getObject());
value.ifPresent(v -> axioms.add(new AxiomImpl<>(descriptor.getSubject(), a, v)));
}
}
return axioms;
}
private static boolean assertionContextSameAsSubject(URI subjectCtx, URI assertionCtx) {
return assertionCtx == null && subjectCtx == null || (subjectCtx != null && subjectCtx.equals(assertionCtx));
}
}