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