-
Notifications
You must be signed in to change notification settings - Fork 14
/
AxiomBuilder.java
100 lines (87 loc) · 3.58 KB
/
AxiomBuilder.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
package cz.cvut.kbss.ontodriver.sesame.util;
import cz.cvut.kbss.ontodriver.model.*;
import org.openrdf.model.Literal;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import java.util.Map;
public class AxiomBuilder {
private final NamedResource subject;
private final Map<URI, Assertion> propertyToAssertion;
private final Assertion unspecifiedProperty;
public AxiomBuilder(NamedResource subject, Map<URI, Assertion> propertyToAssertion, Assertion unspecifiedProperty) {
this.subject = subject;
this.propertyToAssertion = propertyToAssertion;
this.unspecifiedProperty = unspecifiedProperty;
}
public Axiom<?> statementToAxiom(Statement statement) {
Assertion assertion = resolveAssertion(statement.getPredicate());
final Value<?> val = resolveValue(statement, assertion);
if (val == null) {
return null;
}
return new AxiomImpl<>(subject, assertion, val);
}
private Value<?> resolveValue(Statement stmt, Assertion assertion) {
if (assertion == null || SesameUtils.isBlankNode(stmt.getObject())) {
return null;
}
Value<?> val = createValue(assertion.getType(), stmt.getObject());
if (val == null) {
return null;
}
return val;
}
public Axiom<?> statementToAxiom(Statement statement, Assertion assertion) {
final Value<?> val = resolveValue(statement, assertion);
if (val == null) {
return null;
}
return new AxiomImpl<>(subject, assertion, val);
}
private Assertion resolveAssertion(URI predicate) {
Assertion assertion = propertyToAssertion.get(predicate);
if (assertion == null) {
if (unspecifiedProperty != null) {
assertion = Assertion
.createPropertyAssertion(SesameUtils.toJavaUri(predicate), unspecifiedProperty.isInferred());
} else {
assertion = null;
}
} else if (assertion.getType() == Assertion.AssertionType.PROPERTY) {
// If the property was unspecified, create assertion based on the actual property URI
assertion = Assertion.createPropertyAssertion(SesameUtils.toJavaUri(predicate), assertion.isInferred());
}
return assertion;
}
private Value<?> createValue(Assertion.AssertionType assertionType, org.openrdf.model.Value value) {
switch (assertionType) {
case DATA_PROPERTY:
if (!(value instanceof Literal)) {
return null;
}
return new Value<>(SesameUtils.getDataPropertyValue((Literal) value));
case CLASS:
if (!(value instanceof Resource)) {
return null;
}
return new Value<>(SesameUtils.toJavaUri((Resource) value));
case OBJECT_PROPERTY:
if (!(value instanceof Resource)) {
return null;
}
return new Value<>(NamedResource.create(value.stringValue()));
case ANNOTATION_PROPERTY: // Intentional fall-through
case PROPERTY:
return resolveValue(value);
}
return null;
}
private Value<?> resolveValue(org.openrdf.model.Value object) {
if (object instanceof Literal) {
return new Value<>(SesameUtils.getDataPropertyValue((Literal) object));
} else {
return new Value<>(SesameUtils.toJavaUri((Resource) object));
}
}
}