-
Notifications
You must be signed in to change notification settings - Fork 14
/
AxiomSaver.java
98 lines (88 loc) · 4.27 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
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.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.NamedResource;
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;
}
/**
* Persists statements corresponding to axioms specified in the descriptor.
*
* @param descriptor Data container
*/
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, JenaUtils.valueToRdfNode(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() && v.getValue() instanceof String) {
value = ResourceFactory.createLangLiteral(v.stringValue(), a.getLanguage());
} else {
value = ResourceFactory.createTypedLiteral(v.getValue());
}
return ResourceFactory.createStatement(subject, property, value);
}).collect(Collectors.toList());
}
/**
* Persists statements corresponding to the specified data.
*
* @param subject Statement subject
* @param properties Assertion to value map, which will be transformed to property to statement object
* @param context Context into which statements should be inserted
*/
void saveAxioms(NamedResource subject, Map<Assertion, Set<Value<?>>> properties, URI context) {
final Resource resource = ResourceFactory.createResource(subject.getIdentifier().toString());
final List<Statement> statements = new ArrayList<>(properties.size());
for (Map.Entry<Assertion, Set<Value<?>>> e : properties.entrySet()) {
statements.addAll(transformToStatements(e.getKey(), e.getValue(), resource));
}
if (context != null) {
connector.add(statements, context.toString());
} else {
connector.add(statements);
}
}
}