Package: TypesHandler
TypesHandler
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TypesHandler(OwlapiAdapter, OntologySnapshot) |
|
|
|
|
|
||||||||||||||||||||
addTypes(NamedResource, URI, Set) |
|
|
|
|
|
||||||||||||||||||||
getIndividual(NamedResource) |
|
|
|
|
|
||||||||||||||||||||
getOwlAxiomsForTypes(NamedResource, Set) |
|
|
|
|
|
||||||||||||||||||||
getTypes(NamedResource, URI, boolean) |
|
|
|
|
|
||||||||||||||||||||
inferClasses(NamedResource) |
|
|
|
|
|
||||||||||||||||||||
lambda$addTypes$1(OWLAxiom) |
|
|
|
|
|
||||||||||||||||||||
lambda$getOwlAxiomsForTypes$2(OWLNamedIndividual, URI) |
|
|
|
|
|
||||||||||||||||||||
lambda$owlClassesToAxioms$0(NamedResource, boolean, OWLClassExpression) |
|
|
|
|
|
||||||||||||||||||||
lambda$removeTypes$3(OWLAxiom) |
|
|
|
|
|
||||||||||||||||||||
loadExplicitClasses(NamedResource) |
|
|
|
|
|
||||||||||||||||||||
owlClassesToAxioms(NamedResource, boolean, Collection) |
|
|
|
|
|
||||||||||||||||||||
removeTypes(NamedResource, URI, Set) |
|
|
|
|
|
||||||||||||||||||||
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.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
18: import cz.cvut.kbss.ontodriver.owlapi.util.MutableAddAxiom;
19: import cz.cvut.kbss.ontodriver.owlapi.util.MutableRemoveAxiom;
20: import cz.cvut.kbss.ontodriver.model.*;
21: import org.semanticweb.owlapi.model.*;
22: import org.semanticweb.owlapi.reasoner.OWLReasoner;
23: import org.semanticweb.owlapi.search.EntitySearcher;
24:
25: import java.net.URI;
26: import java.util.*;
27: import java.util.stream.Collectors;
28:
29: class TypesHandler {
30:
31: private final OWLOntology ontology;
32: private final OWLDataFactory dataFactory;
33: private final OWLReasoner reasoner;
34: private final OntologySnapshot snapshot;
35:
36: private final OwlapiAdapter adapter;
37:
38: TypesHandler(OwlapiAdapter adapter, OntologySnapshot snapshot) {
39: this.adapter = adapter;
40: this.snapshot = snapshot;
41: this.ontology = snapshot.getOntology();
42: this.dataFactory = snapshot.getDataFactory();
43: this.reasoner = snapshot.getReasoner();
44: }
45:
46: Set<Axiom<URI>> getTypes(NamedResource subject, URI context, boolean includeInferred) {
47: final Collection<? extends OWLClassExpression> owlClasses;
48:• if (!includeInferred) {
49: owlClasses = loadExplicitClasses(subject);
50: } else {
51: owlClasses = inferClasses(subject);
52: }
53: return owlClassesToAxioms(subject, includeInferred, owlClasses);
54: }
55:
56: private Collection<OWLClassExpression> loadExplicitClasses(NamedResource subject) {
57: return EntitySearcher.getTypes(getIndividual(subject), ontology).collect(Collectors.toSet());
58: }
59:
60: private OWLNamedIndividual getIndividual(NamedResource subject) {
61: return dataFactory.getOWLNamedIndividual(IRI.create(subject.getIdentifier()));
62: }
63:
64: private Set<Axiom<URI>> owlClassesToAxioms(NamedResource subject, boolean inferred,
65: Collection<? extends OWLClassExpression> owlClasses) {
66: return owlClasses.stream().map(expr -> new AxiomImpl<>(subject,
67: Assertion.createClassAssertion(inferred), new Value<>(expr.asOWLClass().getIRI().toURI())))
68: .collect(Collectors.toSet());
69: }
70:
71: private Collection<? extends OWLClassExpression> inferClasses(NamedResource subject) {
72: final OWLNamedIndividual individual = getIndividual(subject);
73: return reasoner.getTypes(individual, false).entities().collect(Collectors.toSet());
74: }
75:
76: void addTypes(NamedResource subject, URI context, Set<URI> types) {
77:• assert !types.isEmpty();
78:
79: final List<OWLAxiom> axioms = getOwlAxiomsForTypes(subject, types);
80: final List<OWLOntologyChange> changes = axioms.stream().map(axiom -> new MutableAddAxiom(ontology, axiom))
81: .collect(Collectors.toList());
82:
83: adapter.addTransactionalChanges(snapshot.applyChanges(changes));
84: }
85:
86: private List<OWLAxiom> getOwlAxiomsForTypes(NamedResource subject, Set<URI> types) {
87: final List<OWLAxiom> axioms = new ArrayList<>(types.size());
88: final OWLNamedIndividual individual = getIndividual(subject);
89: axioms.addAll(types.stream().map(type -> dataFactory
90: .getOWLClassAssertionAxiom(dataFactory.getOWLClass(IRI.create(type)), individual))
91: .collect(Collectors.toList()));
92: return axioms;
93: }
94:
95: void removeTypes(NamedResource subject, URI context, Set<URI> types) {
96:• assert !types.isEmpty();
97:
98: final List<OWLAxiom> axioms = getOwlAxiomsForTypes(subject, types);
99: final List<OWLOntologyChange> changes = axioms.stream().map(axiom -> new MutableRemoveAxiom(ontology, axiom))
100: .collect(Collectors.toList());
101:
102: adapter.addTransactionalChanges(snapshot.applyChanges(changes));
103: }
104: }