Skip to content

Package: AxiomLoader

AxiomLoader

nameinstructionbranchcomplexitylinemethod
AxiomLoader(Connector, ValueFactory)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createAxiom(Statement, Map)
M: 0 C: 49
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
createValue(Assertion.AssertionType, Value)
M: 6 C: 41
87%
M: 3 C: 8
73%
M: 3 C: 5
63%
M: 3 C: 9
75%
M: 0 C: 1
100%
findStatements(AxiomDescriptor)
M: 0 C: 77
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
getPropertyUri(Assertion)
M: 0 C: 25
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
loadAxioms(AxiomDescriptor)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
loadAxioms(NamedResource, boolean, URI)
M: 15 C: 31
67%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 6
86%
M: 0 C: 1
100%
resolveAssertion(URI)
M: 2 C: 26
93%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 1 C: 8
89%
M: 0 C: 1
100%
resolveValue(Value)
M: 0 C: 17
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
sameContext(Assertion, Assertion, AxiomDescriptor)
M: 7 C: 14
67%
M: 5 C: 1
17%
M: 3 C: 1
25%
M: 0 C: 3
100%
M: 0 C: 1
100%
shouldLoad(Assertion, AxiomDescriptor)
M: 0 C: 25
100%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 0 C: 3
100%
M: 0 C: 1
100%
transformStatementsToAxioms(Collection)
M: 0 C: 35
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 9
100%
M: 0 C: 1
100%

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 java.util.*;
18:
19: import org.openrdf.model.Literal;
20: import org.openrdf.model.Resource;
21: import org.openrdf.model.Statement;
22: import org.openrdf.model.URI;
23: import org.openrdf.model.ValueFactory;
24:
25: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
26: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
27: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
28: import cz.cvut.kbss.ontodriver.model.Assertion;
29: import cz.cvut.kbss.ontodriver.model.Assertion.AssertionType;
30: import cz.cvut.kbss.ontodriver.model.Axiom;
31: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
32: import cz.cvut.kbss.ontodriver.model.NamedResource;
33: import cz.cvut.kbss.ontodriver.model.Value;
34:
35: class AxiomLoader {
36:
37: private final Connector connector;
38: private final ValueFactory valueFactory;
39:
40: private Map<URI, Assertion> propertyToAssertion;
41: private Assertion unspecifiedProperty;
42:
43: AxiomLoader(Connector connector, ValueFactory valueFactory) {
44: this.connector = connector;
45: this.valueFactory = valueFactory;
46: this.propertyToAssertion = new HashMap<>();
47: }
48:
49: Collection<Axiom<?>> loadAxioms(AxiomDescriptor axiomDescriptor) throws SesameDriverException {
50: Collection<Statement> statements = findStatements(axiomDescriptor);
51: return transformStatementsToAxioms(statements);
52: }
53:
54: private Collection<Statement> findStatements(AxiomDescriptor descriptor)
55: throws SesameDriverException {
56: final Collection<Statement> result = new HashSet<>();
57: final Resource subject = SesameUtils.toSesameUri(descriptor.getSubject().getIdentifier(),
58: valueFactory);
59:• for (Assertion assertion : descriptor.getAssertions()) {
60: final URI context = SesameUtils.toSesameUri(descriptor.getAssertionContext(assertion), valueFactory);
61: final URI property = getPropertyUri(assertion);
62:
63:• if (property != null && !shouldLoad(assertion, descriptor)) {
64: continue;
65: }
66:• if (context != null) {
67: result.addAll(connector.findStatements(subject, property, null, assertion.isInferred(), context));
68: } else {
69: result.addAll(connector.findStatements(subject, property, null, assertion.isInferred()));
70: }
71: }
72: return result;
73: }
74:
75: private boolean shouldLoad(Assertion assertion, AxiomDescriptor descriptor) {
76:• return unspecifiedProperty == null || assertion.getType() == AssertionType.CLASS
77:• || !sameContext(unspecifiedProperty, assertion, descriptor)
78:• || (!unspecifiedProperty.isInferred() && assertion.isInferred());
79: }
80:
81: private boolean sameContext(Assertion assertionOne, Assertion assertionTwo, AxiomDescriptor descriptor) {
82: final java.net.URI cOne = descriptor.getAssertionContext(assertionOne);
83: final java.net.URI cTwo = descriptor.getAssertionContext(assertionTwo);
84:• return cOne == cTwo || (cOne != null && cOne.equals(cTwo));
85: }
86:
87: private URI getPropertyUri(Assertion assertion) {
88:• if (assertion.equals(Assertion.createUnspecifiedPropertyAssertion(assertion.isInferred()))) {
89: this.unspecifiedProperty = assertion;
90: return null;
91: }
92: final URI property = SesameUtils.toSesameUri(assertion.getIdentifier(), valueFactory);
93: propertyToAssertion.put(property, assertion);
94: return property;
95: }
96:
97: private List<Axiom<?>> transformStatementsToAxioms(Collection<Statement> statements) {
98: final List<Axiom<?>> axioms = new ArrayList<>(statements.size());
99: final Map<Resource, NamedResource> subjects = new HashMap<>();
100:• for (Statement stmt : statements) {
101: final Axiom<?> axiom = createAxiom(stmt, subjects);
102:• if (axiom == null) {
103: continue;
104: }
105: axioms.add(axiom);
106: }
107: return axioms;
108: }
109:
110: private Axiom<?> createAxiom(Statement stmt, Map<Resource, NamedResource> knownSubjects) {
111:• if (!knownSubjects.containsKey(stmt.getSubject())) {
112: knownSubjects.put(stmt.getSubject(),
113: NamedResource.create(SesameUtils.toJavaUri(stmt.getSubject())));
114: }
115: final NamedResource subject = knownSubjects.get(stmt.getSubject());
116: Assertion assertion = resolveAssertion(stmt.getPredicate());
117:• if (SesameUtils.isBlankNode(stmt.getObject())) {
118: return null;
119: }
120: Value<?> val = createValue(assertion.getType(), stmt.getObject());
121:• if (val == null) {
122: return null;
123: }
124: return new AxiomImpl<>(subject, assertion, val);
125: }
126:
127: private Assertion resolveAssertion(URI predicate) {
128: Assertion assertion = propertyToAssertion.get(predicate);
129:• if (assertion == null) {
130:• if (unspecifiedProperty == null) {
131: return null;
132: } else {
133: assertion = unspecifiedProperty;
134: }
135: }
136: // If the property was unspecified, create assertion based on the actual property URI
137:• if (assertion.getType() == AssertionType.PROPERTY) {
138: assertion = Assertion.createPropertyAssertion(
139: SesameUtils.toJavaUri(predicate), assertion.isInferred());
140: }
141: return assertion;
142: }
143:
144: private Value<?> createValue(AssertionType assertionType, org.openrdf.model.Value value) {
145:• switch (assertionType) {
146: case DATA_PROPERTY:
147:• if (!(value instanceof Literal)) {
148: return null;
149: }
150: return new Value<>(SesameUtils.getDataPropertyValue((Literal) value));
151: case CLASS:
152:• if (!(value instanceof Resource)) {
153: return null;
154: }
155: return new Value<>(SesameUtils.toJavaUri((Resource) value));
156: case OBJECT_PROPERTY:
157:• if (!(value instanceof Resource)) {
158: return null;
159: }
160: return new Value<>(NamedResource.create(value.stringValue()));
161: case ANNOTATION_PROPERTY: // Intentional fall-through
162: case PROPERTY:
163: return resolveValue(value);
164: }
165: return null;
166: }
167:
168: private Value<?> resolveValue(org.openrdf.model.Value object) {
169:• if (object instanceof Literal) {
170: return new Value<>(SesameUtils.getDataPropertyValue((Literal) object));
171: } else {
172: return new Value<>(SesameUtils.toJavaUri((Resource) object));
173: }
174: }
175:
176: public Collection<Axiom<?>> loadAxioms(NamedResource individual, boolean includeInferred, java.net.URI context)
177: throws SesameDriverException {
178: final URI sesameContext = SesameUtils.toSesameUri(context, valueFactory);
179: final URI subject = SesameUtils.toSesameUri(individual.getIdentifier(), valueFactory);
180: this.unspecifiedProperty = Assertion.createUnspecifiedPropertyAssertion(includeInferred);
181: final Collection<Statement> statements;
182:• if (sesameContext != null) {
183: statements = connector.findStatements(subject, null, null, includeInferred, sesameContext);
184: } else {
185: statements = connector.findStatements(subject, null, null, includeInferred);
186: }
187: return transformStatementsToAxioms(statements);
188: }
189: }