Package: TypesHandler
TypesHandler
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TypesHandler(RepoConnection, ValueFactory) |
|
|
|
|
|
||||||||||||||||||||
addTypes(NamedResource, URI, Set) |
|
|
|
|
|
||||||||||||||||||||
getTypes(NamedResource, Collection, boolean) |
|
|
|
|
|
||||||||||||||||||||
getTypesStatements(NamedResource, Collection, boolean) |
|
|
|
|
|
||||||||||||||||||||
lambda$getTypesStatements$0(URI) |
|
|
|
|
|
||||||||||||||||||||
prepareStatements(NamedResource, URI, Set) |
|
|
|
|
|
||||||||||||||||||||
removeTypes(NamedResource, URI, Set) |
|
|
|
|
|
||||||||||||||||||||
resolveTypes(NamedResource, boolean, Collection) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.rdf4j;
19:
20: import cz.cvut.kbss.ontodriver.model.*;
21: import cz.cvut.kbss.ontodriver.rdf4j.connector.RepoConnection;
22: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
23: import cz.cvut.kbss.ontodriver.rdf4j.util.Rdf4jUtils;
24: import org.eclipse.rdf4j.model.Resource;
25: import org.eclipse.rdf4j.model.Statement;
26: import org.eclipse.rdf4j.model.ValueFactory;
27: import org.eclipse.rdf4j.model.vocabulary.RDF;
28:
29: import java.net.URI;
30: import java.util.*;
31: import java.util.stream.Collectors;
32:
33: class TypesHandler {
34:
35: private final RepoConnection connector;
36: private final ValueFactory valueFactory;
37:
38: TypesHandler(RepoConnection connector, ValueFactory valueFactory) {
39: this.connector = connector;
40: this.valueFactory = valueFactory;
41: }
42:
43: Set<Axiom<URI>> getTypes(NamedResource individual, Collection<URI> contexts, boolean includeInferred)
44: throws Rdf4jDriverException {
45: final Collection<Statement> statements = getTypesStatements(individual, contexts, includeInferred);
46:• if (statements.isEmpty()) {
47: return Collections.emptySet();
48: }
49: return resolveTypes(individual, includeInferred, statements);
50: }
51:
52: private Collection<Statement> getTypesStatements(NamedResource individual, Collection<URI> contexts,
53: boolean includeInferred)
54: throws Rdf4jDriverException {
55: final Resource subject = Rdf4jUtils.toRdf4jIri(individual.getIdentifier(), valueFactory);
56: return connector.findStatements(subject, RDF.TYPE, null, includeInferred,
57: contexts.stream().map(c -> Rdf4jUtils.toRdf4jIri(c, valueFactory)).collect(Collectors.toSet()));
58: }
59:
60: private static Set<Axiom<URI>> resolveTypes(NamedResource individual, boolean includeInferred,
61: Collection<Statement> statements) {
62: final Set<Axiom<URI>> types = new HashSet<>(statements.size());
63: final Assertion clsAssertion = Assertion.createClassAssertion(includeInferred);
64:• for (Statement stmt : statements) {
65:• assert stmt.getObject() instanceof Resource;
66: final URI type = Rdf4jUtils.toJavaUri((Resource) stmt.getObject());
67:• if (type == null) {
68: // It was a blank node
69: continue;
70: }
71: types.add(new AxiomImpl<>(individual, clsAssertion, new Value<>(type)));
72: }
73: return types;
74: }
75:
76: void addTypes(NamedResource individual, URI context, Set<URI> types) throws Rdf4jDriverException {
77: final Collection<Statement> statements = prepareStatements(individual, context, types);
78: connector.addStatements(statements);
79: }
80:
81: private Collection<Statement> prepareStatements(NamedResource individual, URI context, Set<URI> types) {
82: final org.eclipse.rdf4j.model.IRI subject = Rdf4jUtils.toRdf4jIri(individual.getIdentifier(), valueFactory);
83: final org.eclipse.rdf4j.model.IRI contextUri = Rdf4jUtils.toRdf4jIri(context, valueFactory);
84: final Collection<Statement> statements = new ArrayList<>(types.size());
85:• for (URI type : types) {
86: statements.add(valueFactory
87: .createStatement(subject, RDF.TYPE, valueFactory.createIRI(type.toString()), contextUri));
88: }
89: return statements;
90: }
91:
92: void removeTypes(NamedResource individual, URI context, Set<URI> types) throws Rdf4jDriverException {
93: final Collection<Statement> statements = prepareStatements(individual, context, types);
94: connector.removeStatements(statements);
95: }
96: }