-
Notifications
You must be signed in to change notification settings - Fork 14
/
AxiomBuilder.java
111 lines (97 loc) · 4.56 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
101
102
103
104
105
106
107
108
109
110
111
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* 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.
* <p>
* 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.sesame.util;
import cz.cvut.kbss.ontodriver.model.*;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.Statement;
import java.util.Map;
import java.util.Optional;
public class AxiomBuilder {
private final NamedResource subject;
private final Map<IRI, Assertion> propertyToAssertion;
private final Assertion unspecifiedProperty;
private final String language;
public AxiomBuilder(NamedResource subject, Map<IRI, Assertion> propertyToAssertion, Assertion unspecifiedProperty,
String language) {
this.subject = subject;
this.propertyToAssertion = propertyToAssertion;
this.unspecifiedProperty = unspecifiedProperty;
this.language = language;
}
public Axiom<?> statementToAxiom(Statement statement) {
final Assertion assertion = resolveAssertion(statement.getPredicate());
return statementToAxiom(statement, assertion);
}
private Optional<Value<?>> resolveValue(Statement stmt, Assertion assertion) {
if (assertion == null || SesameUtils.isBlankNode(stmt.getObject())) {
return Optional.empty();
}
return createValue(assertion.getType(), stmt.getObject());
}
public Axiom<?> statementToAxiom(Statement statement, Assertion assertion) {
final Optional<Value<?>> val = resolveValue(statement, assertion);
return val.map(v -> new AxiomImpl<>(subject, assertion, v)).orElse(null);
}
private Assertion resolveAssertion(IRI 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 Optional<Value<?>> createValue(Assertion.AssertionType assertionType, org.eclipse.rdf4j.model.Value value) {
switch (assertionType) {
case DATA_PROPERTY:
if (!(value instanceof Literal) || !SesameUtils.doesLanguageMatch((Literal) value, language)) {
return Optional.empty();
}
return Optional.of(new Value<>(SesameUtils.getDataPropertyValue((Literal) value)));
case CLASS:
if (!(value instanceof Resource)) {
return Optional.empty();
}
return Optional.of(new Value<>(SesameUtils.toJavaUri((Resource) value)));
case OBJECT_PROPERTY:
if (!(value instanceof Resource)) {
return Optional.empty();
}
return Optional.of(new Value<>(NamedResource.create(value.stringValue())));
case ANNOTATION_PROPERTY: // Intentional fall-through
case PROPERTY:
return resolveValue(value);
}
return Optional.empty();
}
private Optional<Value<?>> resolveValue(org.eclipse.rdf4j.model.Value object) {
if (object instanceof Literal) {
if (!SesameUtils.doesLanguageMatch((Literal) object, language)) {
return Optional.empty();
}
return Optional.of(new Value<>(SesameUtils.getDataPropertyValue((Literal) object)));
} else {
return Optional.of(new Value<>(NamedResource.create(object.stringValue())));
}
}
}