-
Notifications
You must be signed in to change notification settings - Fork 14
/
AxiomLoader.java
98 lines (86 loc) · 4.25 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
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 org.apache.jena.rdf.model.*;
import java.net.URI;
import java.util.*;
// TODO The loader does not support inferred statements, yet
class AxiomLoader {
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) {
final Resource subject = ResourceFactory.createResource(descriptor.getSubject().getIdentifier().toString());
final Collection<Statement> statements = connector.find(subject, null, null);
final List<Axiom<?>> result = new ArrayList<>(statements.size());
processProperties(descriptor);
for (Statement statement : statements) {
final Property property = statement.getPredicate();
if (!assertedProperties.containsKey(property.getURI()) && unspecifiedProperty == null) {
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 -> result.add(new AxiomImpl<>(descriptor.getSubject(), a, v)));
}
return result;
}
private void processProperties(AxiomDescriptor descriptor) {
this.assertedProperties = new HashMap<>(descriptor.getAssertions().size());
final Assertion unspecified = Assertion.createUnspecifiedPropertyAssertion(false);
for (Assertion a : descriptor.getAssertions()) {
if (a.isInferred()) {
continue; // For now
}
if (a.equals(unspecified)) {
this.unspecifiedProperty = a;
continue;
}
assertedProperties.put(a.getIdentifier().toString(), a);
}
}
private Optional<Value<?>> resolveValue(Assertion assertion, RDFNode object) {
// TODO We shouldn't load types for unspecified property, they should be loaded only for class assertion
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) {
if (value.isResource()) {
return Assertion
.createObjectPropertyAssertion(URI.create(property.getURI()), unspecifiedProperty.isInferred());
} else {
return Assertion
.createDataPropertyAssertion(URI.create(property.getURI()), unspecifiedProperty.isInferred());
}
}
}