-
Notifications
You must be signed in to change notification settings - Fork 14
/
AxiomLoader.java
159 lines (140 loc) · 7.44 KB
/
AxiomLoader.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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.jena.util.JenaUtils;
import cz.cvut.kbss.ontodriver.model.*;
import cz.cvut.kbss.ontodriver.util.Vocabulary;
import org.apache.jena.rdf.model.*;
import java.net.URI;
import java.util.*;
import static cz.cvut.kbss.ontodriver.model.Assertion.createDataPropertyAssertion;
import static cz.cvut.kbss.ontodriver.model.Assertion.createObjectPropertyAssertion;
// TODO The loader does not support inferred statements, yet
class AxiomLoader {
private static final Assertion UNSPECIFIED_ASSERTION = Assertion.createUnspecifiedPropertyAssertion(false);
private final StorageConnector connector;
private Map<String, Assertion> assertedProperties;
private Assertion unspecifiedProperty;
AxiomLoader(StorageConnector connector) {
this.connector = connector;
}
boolean contains(Axiom<?> axiom, URI context) {
final Resource subject = ResourceFactory.createResource(axiom.getSubject().getIdentifier().toString());
final Property property = ResourceFactory.createProperty(axiom.getAssertion().getIdentifier().toString());
final RDFNode object = JenaUtils.valueToRdfNode(axiom.getAssertion(), axiom.getValue());
if (context != null) {
return connector.contains(subject, property, object, context.toString());
} else {
return connector.contains(subject, property, object);
}
}
Collection<Axiom<?>> find(AxiomDescriptor descriptor) {
mapProperties(descriptor);
final Resource subject = ResourceFactory.createResource(descriptor.getSubject().getIdentifier().toString());
final Collection<Statement> statements = findInternal(subject, null, descriptor.getSubjectContext());
final List<Axiom<?>> result = transformStatementsToAxioms(descriptor, statements);
result.addAll(loadAxiomsForPropertiesInContext(descriptor, subject));
return result;
}
private void mapProperties(AxiomDescriptor descriptor) {
this.assertedProperties = new HashMap<>(descriptor.getAssertions().size());
for (Assertion a : descriptor.getAssertions()) {
if (a.isInferred()) {
continue; // For now
}
if (a.equals(UNSPECIFIED_ASSERTION)) {
this.unspecifiedProperty = a;
continue;
}
assertedProperties.put(a.getIdentifier().toString(), a);
}
}
private Collection<Statement> findInternal(Resource subject, Property property, URI context) {
if (context != null) {
return connector.find(subject, property, null, context.toString());
} else {
return connector.find(subject, property, 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(property, statement.getObject());
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 Optional<Value<?>> resolveValue(Assertion assertion, RDFNode object) {
if (object.isResource()) {
if (object.isAnon() || assertion.getType() == Assertion.AssertionType.DATA_PROPERTY) {
return Optional.empty();
}
return Optional.of(new Value<>(NamedResource.create(object.asResource().getURI())));
} else {
if (shouldSkipLiteral(assertion, object)) {
return Optional.empty();
}
return Optional.of(new Value<>(object.asLiteral().getValue()));
}
}
private boolean shouldSkipLiteral(Assertion assertion, RDFNode object) {
return assertion.getType() == Assertion.AssertionType.OBJECT_PROPERTY || assertion.hasLanguage() && !assertion
.getLanguage().equals(object.asLiteral().getLanguage());
}
private Assertion createAssertionForStatement(Property property, RDFNode value) {
assert unspecifiedProperty != null;
if (value.isResource()) {
return createObjectPropertyAssertion(URI.create(property.getURI()), unspecifiedProperty.isInferred());
} else {
return createDataPropertyAssertion(URI.create(property.getURI()), unspecifiedProperty.isInferred());
}
}
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 = findInternal(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 =
findInternal(subject, null, descriptor.getAssertionContext(unspecifiedProperty));
for (Statement s : statements) {
final Assertion a = createAssertionForStatement(s.getPredicate(), s.getObject());
final Optional<Value<?>> value = resolveValue(a, s.getObject());
value.ifPresent(v -> axioms.add(new AxiomImpl<>(descriptor.getSubject(), a, v)));
}
}
return axioms;
}
private boolean assertionContextSameAsSubject(URI subjectCtx, URI assertionCtx) {
return assertionCtx == null && subjectCtx == null || (subjectCtx != null && assertionCtx != null && subjectCtx
.equals(assertionCtx));
}
}