-
Notifications
You must be signed in to change notification settings - Fork 14
/
AxiomSaver.java
80 lines (70 loc) · 3.5 KB
/
AxiomSaver.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
package cz.cvut.kbss.ontodriver.jena;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
import cz.cvut.kbss.ontodriver.jena.util.JenaUtils;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Value;
import org.apache.jena.rdf.model.*;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
class AxiomSaver {
private final StorageConnector connector;
AxiomSaver(StorageConnector connector) {
this.connector = connector;
}
void saveAxioms(AxiomValueDescriptor descriptor) {
final Resource subject = ResourceFactory.createResource(descriptor.getSubject().getIdentifier().toString());
final Map<String, List<Statement>> statements = new HashMap<>();
for (Assertion a : descriptor.getAssertions()) {
final URI context = descriptor.getAssertionContext(a);
final String strContext = context != null ? context.toString() : null;
statements.putIfAbsent(strContext, new ArrayList<>());
statements.get(strContext).addAll(transformToStatements(a, descriptor.getAssertionValues(a), subject));
}
statements.forEach((ctx, toAdd) -> {
if (ctx != null) {
connector.add(toAdd, ctx);
} else {
connector.add(toAdd);
}
});
}
private List<Statement> transformToStatements(Assertion assertion, Collection<Value<?>> values, Resource subject) {
final Property property = ResourceFactory.createProperty(assertion.getIdentifier().toString());
switch (assertion.getType()) {
// Intentional fall-through
case CLASS:
case OBJECT_PROPERTY:
return values.stream().filter(v -> v != Value.nullValue()).map(v -> ResourceFactory
.createStatement(subject, property, ResourceFactory.createResource(v.stringValue())))
.collect(Collectors.toList());
case DATA_PROPERTY:
return dataPropertyValuesToStatements(values, subject, assertion, property);
default:
return values.stream().filter(v -> v != Value.nullValue())
.map(v -> ResourceFactory.createStatement(subject, property, resolveValue(assertion, v)))
.collect(Collectors.toList());
}
}
private List<Statement> dataPropertyValuesToStatements(Collection<Value<?>> values, Resource subject, Assertion a,
Property property) {
return values.stream().filter(v -> v != Value.nullValue()).map(v -> {
final Literal value;
if (a.hasLanguage()) {
value = ResourceFactory.createLangLiteral(v.stringValue(), a.getLanguage());
} else {
value = ResourceFactory.createTypedLiteral(v.getValue());
}
return ResourceFactory.createStatement(subject, property, value);
}).collect(Collectors.toList());
}
private RDFNode resolveValue(Assertion a, Value<?> value) {
if (JenaUtils.isResourceIdentifier(value.getValue())) {
return ResourceFactory.createResource(value.stringValue());
} else {
return a.hasLanguage() ? ResourceFactory.createLangLiteral(value.stringValue(), a.getLanguage()) :
ResourceFactory.createTypedLiteral(value.getValue());
}
}
}