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