Skip to content

Package: TypesHandler

TypesHandler

nameinstructionbranchcomplexitylinemethod
TypesHandler(Connector, ValueFactory)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
addTypes(NamedResource, URI, Set)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getTypes(NamedResource, URI, boolean)
M: 0 C: 17
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getTypesStatements(NamedResource, URI, boolean)
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
prepareSesameStatements(NamedResource, URI, Set)
M: 0 C: 44
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
removeTypes(NamedResource, URI, Set)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
resolveTypes(NamedResource, boolean, Collection)
M: 5 C: 46
90%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 1 C: 9
90%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
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 cz.cvut.kbss.ontodriver.model.*;
18: import cz.cvut.kbss.ontodriver.sesame.connector.Connector;
19: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
20: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
21: import org.eclipse.rdf4j.model.Resource;
22: import org.eclipse.rdf4j.model.Statement;
23: import org.eclipse.rdf4j.model.ValueFactory;
24: import org.eclipse.rdf4j.model.vocabulary.RDF;
25:
26: import java.net.URI;
27: import java.util.*;
28:
29: class TypesHandler {
30:
31: private final Connector connector;
32: private final ValueFactory valueFactory;
33:
34: TypesHandler(Connector connector, ValueFactory valueFactory) {
35: this.connector = connector;
36: this.valueFactory = valueFactory;
37: }
38:
39: Set<Axiom<URI>> getTypes(NamedResource individual, URI context, boolean includeInferred)
40: throws SesameDriverException {
41: final Collection<Statement> statements = getTypesStatements(individual, context, includeInferred);
42:• if (statements.isEmpty()) {
43: return Collections.emptySet();
44: }
45: return resolveTypes(individual, includeInferred, statements);
46: }
47:
48: private Collection<Statement> getTypesStatements(NamedResource individual, URI context, boolean includeInferred)
49: throws SesameDriverException {
50: final Resource subject = SesameUtils.toSesameIri(individual.getIdentifier(), valueFactory);
51: final org.eclipse.rdf4j.model.IRI contextUri = SesameUtils.toSesameIri(context, valueFactory);
52: return connector.findStatements(subject, RDF.TYPE, null, includeInferred, contextUri);
53: }
54:
55: private Set<Axiom<URI>> resolveTypes(NamedResource individual, boolean includeInferred,
56: Collection<Statement> statements) {
57: final Set<Axiom<URI>> types = new HashSet<>(statements.size());
58: final Assertion clsAssertion = Assertion.createClassAssertion(includeInferred);
59:• for (Statement stmt : statements) {
60:• assert stmt.getObject() instanceof Resource;
61: final URI type = SesameUtils.toJavaUri((Resource) stmt.getObject());
62:• if (type == null) {
63: // It was a blank node
64: continue;
65: }
66: types.add(new AxiomImpl<>(individual, clsAssertion, new Value<>(type)));
67: }
68: return types;
69: }
70:
71: void addTypes(NamedResource individual, URI context, Set<URI> types) throws SesameDriverException {
72: final Collection<Statement> statements = prepareSesameStatements(individual, context, types);
73: connector.addStatements(statements);
74: }
75:
76: private Collection<Statement> prepareSesameStatements(NamedResource individual, URI context, Set<URI> types) {
77: final org.eclipse.rdf4j.model.IRI subject = SesameUtils.toSesameIri(individual.getIdentifier(), valueFactory);
78: final org.eclipse.rdf4j.model.IRI contextUri = SesameUtils.toSesameIri(context, valueFactory);
79: final Collection<Statement> statements = new ArrayList<>(types.size());
80:• for (URI type : types) {
81: statements.add(valueFactory
82: .createStatement(subject, RDF.TYPE, valueFactory.createIRI(type.toString()), contextUri));
83: }
84: return statements;
85: }
86:
87: void removeTypes(NamedResource individual, URI context, Set<URI> types) throws SesameDriverException {
88: final Collection<Statement> statements = prepareSesameStatements(individual, context, types);
89: connector.removeStatements(statements);
90: }
91: }