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