Skip to contentMethod: lambda$loadPropertyAxioms$4(Collection, NamedResource, Assertion, OWLNamedIndividual)
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.connector.OntologySnapshot;
22: import cz.cvut.kbss.ontodriver.owlapi.exception.ReasonerNotAvailableException;
23: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
24: import org.semanticweb.owlapi.model.*;
25: import org.semanticweb.owlapi.reasoner.OWLReasoner;
26:
27: import java.util.*;
28: import java.util.stream.Collectors;
29: import java.util.stream.Stream;
30:
31: public class InferredAxiomLoader implements AxiomLoader {
32:
33: private final OWLReasoner reasoner;
34: private final OWLOntology ontology;
35: private final OWLDataFactory dataFactory;
36:
37: private final OwlapiAdapter adapter;
38: private final AxiomAdapter axiomAdapter;
39:
40: private NamedResource subject;
41:
42: InferredAxiomLoader(OwlapiAdapter adapter, OntologySnapshot snapshot) {
43: this.adapter = adapter;
44: this.reasoner = snapshot.getReasoner();
45: this.ontology = snapshot.getOntology();
46: this.dataFactory = snapshot.getDataFactory();
47: this.axiomAdapter = new AxiomAdapter(snapshot.getDataFactory());
48: }
49:
50: @Override
51: public Collection<Axiom<?>> loadAxioms(NamedResource subject, Set<Assertion> assertions) {
52: this.subject = subject;
53: if (assertions.isEmpty()) {
54: return Collections.emptySet();
55: }
56: if (reasoner == null) {
57: throw new ReasonerNotAvailableException();
58: }
59: reasoner.flush();
60: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
61: final Collection<Axiom<?>> axioms = new HashSet<>();
62: for (Assertion a : assertions) {
63: switch (a.getType()) {
64: case CLASS:
65: axioms.addAll(adapter.getTypesHandler().getTypes(subject, null, true));
66: break;
67: case DATA_PROPERTY:
68: axioms.addAll(inferDataPropertyValues(individual, a));
69: break;
70: case OBJECT_PROPERTY:
71: axioms.addAll(inferObjectPropertyValues(individual, a));
72: break;
73: case PROPERTY:
74: // When we don't know, try all
75: axioms.addAll(adapter.getTypesHandler().getTypes(subject, null, true));
76: axioms.addAll(inferDataPropertyValues(individual, a));
77: axioms.addAll(inferObjectPropertyValues(individual, a));
78: break;
79: default:
80: break;
81: }
82: }
83: return axioms;
84: }
85:
86: private Collection<Axiom<?>> inferDataPropertyValues(OWLNamedIndividual individual, Assertion dpAssertion) {
87: final Set<OWLLiteral> literals = reasoner.getDataPropertyValues(individual, dataProperty(dpAssertion));
88: return literals.stream().filter(lit -> OwlapiUtils.doesLanguageMatch(lit, dpAssertion))
89: .map(owlLiteral -> new AxiomImpl<>(subject, dpAssertion,
90: new Value<>(OwlapiUtils.owlLiteralToValue(owlLiteral)))).collect(Collectors.toSet());
91: }
92:
93: private OWLDataProperty dataProperty(Assertion dataPropertyAssertion) {
94: return dataFactory.getOWLDataProperty(IRI.create(dataPropertyAssertion.getIdentifier()));
95: }
96:
97: private Collection<Axiom<?>> inferObjectPropertyValues(OWLNamedIndividual individual, Assertion opAssertion) {
98: final Stream<OWLNamedIndividual> individuals =
99: reasoner.getObjectPropertyValues(individual, objectProperty(opAssertion)).entities();
100: return individuals.map(
101: target -> axiomAdapter.createAxiom(subject, opAssertion, NamedResource.create(target.getIRI().toURI())))
102: .collect(
103: Collectors.toList());
104: }
105:
106: private OWLObjectProperty objectProperty(Assertion objectPropertyAssertion) {
107: return dataFactory.getOWLObjectProperty(IRI.create(objectPropertyAssertion.getIdentifier()));
108: }
109:
110: @Override
111: public Collection<Axiom<?>> loadPropertyAxioms(NamedResource subject) {
112: final Collection<Axiom<?>> axioms = new ArrayList<>();
113: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
114: ontology.dataPropertiesInSignature().forEach(dp -> {
115: final Set<OWLLiteral> values = reasoner.getDataPropertyValues(individual, dp);
116: for (OWLLiteral literal : values) {
117: axioms.add(axiomAdapter.createAxiom(subject,
118: Assertion.createDataPropertyAssertion(dp.getIRI().toURI(), true), literal));
119: }
120: });
121: ontology.objectPropertiesInSignature().forEach(op -> {
122: final Assertion opAss = Assertion.createObjectPropertyAssertion(op.getIRI().toURI(), true);
123: reasoner.getObjectPropertyValues(individual, op).entities()
124: .forEach(ind -> axioms
125: .add(axiomAdapter.createAxiom(subject, opAss, NamedResource.create(ind.getIRI().toURI()))));
126: });
127: return axioms;
128: }
129: }