Skip to content

Method: inferClasses(NamedResource)

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