Skip to content

Package: TypesHandler

TypesHandler

nameinstructionbranchcomplexitylinemethod
TypesHandler(StorageConnector, InferredStorageConnector)
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: 15
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
generateStatementsForTypes(NamedResource, Set)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getStatements(NamedResource, Collection, boolean)
M: 0 C: 31
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getTypes(NamedResource, Collection, boolean)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
lambda$generateStatementsForTypes$2(Resource, Property, URI)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$getTypes$0(Statement)
M: 1 C: 11
92%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$getTypes$1(NamedResource, Assertion, Statement)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
removeTypes(NamedResource, URI, Set)
M: 0 C: 15
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.jena;
14:
15: import cz.cvut.kbss.ontodriver.Types;
16: import cz.cvut.kbss.ontodriver.jena.connector.InferredStorageConnector;
17: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
18: import cz.cvut.kbss.ontodriver.model.*;
19: import cz.cvut.kbss.ontodriver.util.Vocabulary;
20: import org.apache.jena.rdf.model.Property;
21: import org.apache.jena.rdf.model.Resource;
22: import org.apache.jena.rdf.model.ResourceFactory;
23: import org.apache.jena.rdf.model.Statement;
24: import org.apache.jena.vocabulary.RDF;
25:
26: import java.net.URI;
27: import java.util.Collection;
28: import java.util.List;
29: import java.util.Set;
30: import java.util.stream.Collectors;
31:
32: public class TypesHandler implements Types {
33:
34: private final StorageConnector connector;
35:
36: private final InferredStorageConnector inferenceConnector;
37:
38: TypesHandler(StorageConnector connector, InferredStorageConnector inferenceConnector) {
39: this.connector = connector;
40: this.inferenceConnector = inferenceConnector;
41: }
42:
43: @Override
44: public Set<Axiom<URI>> getTypes(NamedResource individual, Collection<URI> contexts, boolean includeInferred) {
45: final Collection<Statement> statements = getStatements(individual, contexts, includeInferred);
46: final Assertion assertion = Assertion.createClassAssertion(includeInferred);
47: // Skip possible non-resources and anonymous resources (not likely to appear, but safety first)
48:• return statements.stream().filter(s -> s.getObject().isResource() && !s.getObject().isAnon())
49: .map(s -> new AxiomImpl<>(individual, assertion,
50: new Value<>(URI.create(s.getObject().asResource().getURI())))).collect(
51: Collectors.toSet());
52: }
53:
54: private Collection<Statement> getStatements(NamedResource individual, Collection<URI> contexts,
55: boolean includedInferred) {
56: final Resource subject = ResourceFactory.createResource(individual.getIdentifier().toString());
57: final Collection<String> ctx = contexts.stream().map(URI::toString).collect(Collectors.toSet());
58:• if (includedInferred) {
59: return inferenceConnector.findWithInference(subject, RDF.type, null, ctx);
60: } else {
61: return connector.find(subject, RDF.type, null, ctx);
62: }
63: }
64:
65: @Override
66: public void addTypes(NamedResource individual, URI context, Set<URI> types) {
67: final List<Statement> statements = generateStatementsForTypes(individual, types);
68:• connector.add(statements, context != null ? context.toString() : null);
69: }
70:
71: private static List<Statement> generateStatementsForTypes(NamedResource individual, Set<URI> types) {
72: final Resource subject = ResourceFactory.createResource(individual.getIdentifier().toString());
73: final Property property = ResourceFactory.createProperty(Vocabulary.RDF_TYPE);
74: return types.stream().map(t -> ResourceFactory
75: .createStatement(subject, property, ResourceFactory.createResource(t.toString()))).collect(
76: Collectors.toList());
77: }
78:
79: @Override
80: public void removeTypes(NamedResource individual, URI context, Set<URI> types) {
81: final List<Statement> statements = generateStatementsForTypes(individual, types);
82:• connector.remove(statements, context != null ? context.toString() : null);
83: }
84: }