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, URI, boolean)
M: 0 C: 30
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getTypes(NamedResource, URI, 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: *
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.jena;
16:
17: import cz.cvut.kbss.ontodriver.Types;
18: import cz.cvut.kbss.ontodriver.jena.connector.InferredStorageConnector;
19: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
20: import cz.cvut.kbss.ontodriver.model.*;
21: import cz.cvut.kbss.ontodriver.util.Vocabulary;
22: import org.apache.jena.rdf.model.Property;
23: import org.apache.jena.rdf.model.Resource;
24: import org.apache.jena.rdf.model.ResourceFactory;
25: import org.apache.jena.rdf.model.Statement;
26: import org.apache.jena.vocabulary.RDF;
27:
28: import java.net.URI;
29: import java.util.Collection;
30: import java.util.List;
31: import java.util.Set;
32: import java.util.stream.Collectors;
33:
34: public class TypesHandler implements Types {
35:
36: private final StorageConnector connector;
37:
38: private final InferredStorageConnector inferenceConnector;
39:
40: TypesHandler(StorageConnector connector, InferredStorageConnector inferenceConnector) {
41: this.connector = connector;
42: this.inferenceConnector = inferenceConnector;
43: }
44:
45: @Override
46: public Set<Axiom<URI>> getTypes(NamedResource individual, URI context, boolean includeInferred) {
47: final Collection<Statement> statements = getStatements(individual, context, includeInferred);
48: final Assertion assertion = Assertion.createClassAssertion(includeInferred);
49: // Skip possible non-resources and anonymous resources (not likely to appear, but safety first)
50:• return statements.stream().filter(s -> s.getObject().isResource() && !s.getObject().isAnon())
51: .map(s -> new AxiomImpl<>(individual, assertion,
52: new Value<>(URI.create(s.getObject().asResource().getURI())))).collect(
53: Collectors.toSet());
54: }
55:
56: private Collection<Statement> getStatements(NamedResource individual, URI context, boolean includedInferred) {
57: final Resource subject = ResourceFactory.createResource(individual.getIdentifier().toString());
58:• final String ctx = context != null ? context.toString() : null;
59:• if (includedInferred) {
60: return inferenceConnector.findWithInference(subject, RDF.type, null, ctx);
61: } else {
62: return connector.find(subject, RDF.type, null, ctx);
63: }
64: }
65:
66: @Override
67: public void addTypes(NamedResource individual, URI context, Set<URI> types) {
68: final List<Statement> statements = generateStatementsForTypes(individual, types);
69:• connector.add(statements, context != null ? context.toString() : null);
70: }
71:
72: private static List<Statement> generateStatementsForTypes(NamedResource individual, Set<URI> types) {
73: final Resource subject = ResourceFactory.createResource(individual.getIdentifier().toString());
74: final Property property = ResourceFactory.createProperty(Vocabulary.RDF_TYPE);
75: return types.stream().map(t -> ResourceFactory
76: .createStatement(subject, property, ResourceFactory.createResource(t.toString()))).collect(
77: Collectors.toList());
78: }
79:
80: @Override
81: public void removeTypes(NamedResource individual, URI context, Set<URI> types) {
82: final List<Statement> statements = generateStatementsForTypes(individual, types);
83:• connector.remove(statements, context != null ? context.toString() : null);
84: }
85: }