-
Notifications
You must be signed in to change notification settings - Fork 14
/
AxiomSaver.java
97 lines (87 loc) · 4.54 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
/**
* Copyright (C) 2020 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.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) -> connector.add(toAdd, ctx));
}
private static 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 static List<Statement> dataPropertyValuesToStatements(Collection<Value<?>> values, Resource subject, Assertion a,
Property property) {
return values.stream().filter(v -> v != Value.nullValue()).map(v -> {
final RDFNode value = JenaUtils.valueToRdfNode(a, v);
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));
}
connector.add(statements, context != null ? context.toString() : null);
}
}