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