Package: AxiomSaver
AxiomSaver
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
AxiomSaver(Connector, ValueFactory, String) |
|
|
|
|
|
||||||||||||||||||||
createSesameStatements(NamedResource, Assertion, Collection, URI) |
|
|
|
|
|
||||||||||||||||||||
createStatement(Resource, URI, Value, URI) |
|
|
|
|
|
||||||||||||||||||||
persistAxioms(AxiomValueDescriptor) |
|
|
|
|
|
||||||||||||||||||||
persistAxioms(NamedResource, Map, URI) |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2016 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.sesame;
16:
17: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
18: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
19: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
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 cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
24: import org.openrdf.model.Resource;
25: import org.openrdf.model.Statement;
26: import org.openrdf.model.ValueFactory;
27:
28: import java.net.URI;
29: import java.util.*;
30:
31: class AxiomSaver {
32:
33: private final Connector connector;
34: private final ValueFactory valueFactory;
35: private final SesameValueConverter valueConverter;
36:
37: AxiomSaver(Connector connector, ValueFactory valueFactory, String language) {
38: this.connector = connector;
39: this.valueFactory = valueFactory;
40: this.valueConverter = new SesameValueConverter(valueFactory, language);
41: }
42:
43: void persistAxioms(AxiomValueDescriptor axiomDescriptor) throws SesameDriverException {
44: final List<Statement> statements = new ArrayList<>();
45:• for (Assertion assertion : axiomDescriptor.getAssertions()) {
46: statements.addAll(createSesameStatements(axiomDescriptor.getSubject(), assertion,
47: axiomDescriptor.getAssertionValues(assertion),
48: axiomDescriptor.getAssertionContext(assertion)));
49: }
50:• if (!statements.isEmpty()) {
51: connector.addStatements(statements);
52: }
53: }
54:
55: void persistAxioms(NamedResource subject, Map<Assertion, Set<Value<?>>> values, URI context) throws SesameDriverException {
56: final List<Statement> statements = new ArrayList<>();
57:• for (Map.Entry<Assertion, Set<Value<?>>> entry : values.entrySet()) {
58: statements.addAll(createSesameStatements(subject, entry.getKey(), entry.getValue(), context));
59: }
60:• if (!statements.isEmpty()) {
61: connector.addStatements(statements);
62: }
63: }
64:
65: private Collection<? extends Statement> createSesameStatements(NamedResource subject,
66: Assertion assertion, Collection<Value<?>> assertionValues, URI assertionContext)
67: throws SesameDriverException {
68: final List<Statement> statements = new ArrayList<>(assertionValues.size());
69:
70: final org.openrdf.model.Resource subjectUri = SesameUtils.toSesameUri(
71: subject.getIdentifier(), valueFactory);
72: final org.openrdf.model.URI property = SesameUtils.toSesameUri(assertion.getIdentifier(),
73: valueFactory);
74:• final org.openrdf.model.URI context = assertionContext != null ? SesameUtils.toSesameUri(
75: assertionContext, valueFactory) : null;
76:• for (Value<?> val : assertionValues) {
77:• if (val == Value.nullValue()) {
78: continue;
79: }
80: org.openrdf.model.Value value = valueConverter.toSesameValue(assertion, val);
81: statements.add(createStatement(subjectUri, property, value, context));
82: }
83: return statements;
84: }
85:
86: private Statement createStatement(Resource subject, org.openrdf.model.URI property,
87: org.openrdf.model.Value value, org.openrdf.model.URI context) {
88:• if (context != null) {
89: return valueFactory.createStatement(subject, property, value, context);
90: } else {
91: return valueFactory.createStatement(subject, property, value);
92: }
93: }
94: }