Skip to content

Package: AxiomSaver

AxiomSaver

nameinstructionbranchcomplexitylinemethod
AxiomSaver(StorageConnector)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
dataPropertyValuesToStatements(Collection, Resource, Assertion, Property)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$dataPropertyValuesToStatements$5(Value)
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$dataPropertyValuesToStatements$6(Assertion, Resource, Property, Value)
M: 0 C: 40
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
lambda$saveAxioms$0(String, List)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$transformToStatements$1(Value)
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$transformToStatements$2(Resource, Property, Value)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$transformToStatements$3(Value)
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$transformToStatements$4(Resource, Property, Assertion, Value)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
saveAxioms(AxiomValueDescriptor)
M: 0 C: 57
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
saveAxioms(NamedResource, Map, URI)
M: 0 C: 45
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
transformToStatements(Assertion, Collection, Resource)
M: 0 C: 42
100%
M: 0 C: 3
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.jena;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
18: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
19: import cz.cvut.kbss.ontodriver.jena.util.JenaUtils;
20: import cz.cvut.kbss.ontodriver.model.Assertion;
21: import cz.cvut.kbss.ontodriver.model.NamedResource;
22: import cz.cvut.kbss.ontodriver.model.Value;
23: import org.apache.jena.rdf.model.*;
24:
25: import java.net.URI;
26: import java.util.*;
27: import java.util.stream.Collectors;
28:
29: class AxiomSaver {
30:
31: private final StorageConnector connector;
32:
33: AxiomSaver(StorageConnector connector) {
34: this.connector = connector;
35: }
36:
37: /**
38: * Persists statements corresponding to axioms specified in the descriptor.
39: *
40: * @param descriptor Data container
41: */
42: void saveAxioms(AxiomValueDescriptor descriptor) {
43: final Resource subject = ResourceFactory.createResource(descriptor.getSubject().getIdentifier().toString());
44: final Map<String, List<Statement>> statements = new HashMap<>();
45:• for (Assertion a : descriptor.getAssertions()) {
46: final URI context = descriptor.getAssertionContext(a);
47:• final String strContext = context != null ? context.toString() : null;
48: statements.putIfAbsent(strContext, new ArrayList<>());
49: statements.get(strContext).addAll(transformToStatements(a, descriptor.getAssertionValues(a), subject));
50: }
51: statements.forEach((ctx, toAdd) -> connector.add(toAdd, ctx));
52: }
53:
54: private static List<Statement> transformToStatements(Assertion assertion, Collection<Value<?>> values, Resource subject) {
55: final Property property = ResourceFactory.createProperty(assertion.getIdentifier().toString());
56:• switch (assertion.getType()) {
57: // Intentional fall-through
58: case CLASS:
59: case OBJECT_PROPERTY:
60:• return values.stream().filter(v -> v != Value.nullValue()).map(v -> ResourceFactory
61: .createStatement(subject, property, ResourceFactory.createResource(v.stringValue())))
62: .collect(Collectors.toList());
63: case DATA_PROPERTY:
64: return dataPropertyValuesToStatements(values, subject, assertion, property);
65: default:
66:• return values.stream().filter(v -> v != Value.nullValue())
67: .map(v -> ResourceFactory
68: .createStatement(subject, property, JenaUtils.valueToRdfNode(assertion, v)))
69: .collect(Collectors.toList());
70:
71: }
72: }
73:
74: private static List<Statement> dataPropertyValuesToStatements(Collection<Value<?>> values, Resource subject, Assertion a,
75: Property property) {
76:• return values.stream().filter(v -> v != Value.nullValue()).map(v -> {
77: final Literal value;
78:• if (a.hasLanguage() && v.getValue() instanceof String) {
79: value = ResourceFactory.createLangLiteral(v.stringValue(), a.getLanguage());
80:• } else if (v.getValue() instanceof Date) {
81: // Jena does not like java.util.Date, it works with Calendar values
82: final GregorianCalendar cal = new GregorianCalendar();
83: cal.setTime((Date) v.getValue());
84: value = ResourceFactory.createTypedLiteral(cal);
85: } else {
86: value = ResourceFactory.createTypedLiteral(v.getValue());
87: }
88: return ResourceFactory.createStatement(subject, property, value);
89: }).collect(Collectors.toList());
90: }
91:
92: /**
93: * Persists statements corresponding to the specified data.
94: *
95: * @param subject Statement subject
96: * @param properties Assertion to value map, which will be transformed to property to statement object
97: * @param context Context into which statements should be inserted
98: */
99: void saveAxioms(NamedResource subject, Map<Assertion, Set<Value<?>>> properties, URI context) {
100: final Resource resource = ResourceFactory.createResource(subject.getIdentifier().toString());
101: final List<Statement> statements = new ArrayList<>(properties.size());
102:• for (Map.Entry<Assertion, Set<Value<?>>> e : properties.entrySet()) {
103: statements.addAll(transformToStatements(e.getKey(), e.getValue(), resource));
104: }
105:• connector.add(statements, context != null ? context.toString() : null);
106: }
107: }