-
Notifications
You must be signed in to change notification settings - Fork 14
/
ExplicitAxiomLoader.java
130 lines (113 loc) · 6.69 KB
/
ExplicitAxiomLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Copyright (C) 2019 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.owlapi;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
import org.semanticweb.owlapi.model.*;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class ExplicitAxiomLoader implements AxiomLoader {
private static final Assertion UNSPECIFIED_ASSERTION = Assertion.createUnspecifiedPropertyAssertion(false);
private final OWLOntology ontology;
private final OWLDataFactory dataFactory;
private final OwlapiAdapter adapter;
private final AxiomAdapter axiomAdapter;
private Map<URI, Assertion> assertionMap;
ExplicitAxiomLoader(OwlapiAdapter adapter, OntologySnapshot snapshot) {
this.adapter = adapter;
this.ontology = snapshot.getOntology();
this.dataFactory = snapshot.getDataFactory();
this.axiomAdapter = new AxiomAdapter(dataFactory);
}
@Override
public Collection<Axiom<?>> loadAxioms(NamedResource subject, Set<Assertion> assertions) {
this.assertionMap = new HashMap<>(assertions.size());
assertions.forEach(a -> assertionMap.put(a.getIdentifier(), a));
final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
final Collection<Axiom<?>> axioms = new ArrayList<>();
if (assertions.contains(Assertion.createClassAssertion(false))) {
axioms.addAll(adapter.getTypesHandler().getTypes(subject, null, false));
}
// This involves a lot of filtering, perhaps we should use EntitySearcher and look for values of concrete properties
final Stream<OWLDataPropertyAssertionAxiom> dpAssertions = ontology.dataPropertyAssertionAxioms(individual);
axioms.addAll(dataPropertyValuesToAxioms(subject, dpAssertions));
final Stream<OWLObjectPropertyAssertionAxiom> opAssertions = ontology.objectPropertyAssertionAxioms(individual);
axioms.addAll(objectPropertyValuesToAxioms(subject, opAssertions));
final Stream<OWLAnnotationAssertionAxiom> apAssertions = ontology
.annotationAssertionAxioms(individual.getIRI());
axioms.addAll(annotationPropertyValuesToAxioms(subject, apAssertions));
return axioms;
}
private Collection<Axiom<?>> dataPropertyValuesToAxioms(NamedResource subject,
Stream<OWLDataPropertyAssertionAxiom> axioms) {
return axioms.filter(this::shouldLoadDataPropertyValue)
.map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
.collect(Collectors.toList());
}
private boolean shouldLoadDataPropertyValue(OWLDataPropertyAssertionAxiom axiom) {
final OWLLiteral value = axiom.getObject();
final IRI dpIri = axiom.getProperty().asOWLDataProperty().getIRI();
final Optional<Assertion> assertion = assertionForAxiom(dpIri);
return assertion.isPresent() && OwlapiUtils.doesLanguageMatch(value, assertion.get());
}
private Optional<Assertion> assertionForAxiom(IRI propertyIri) {
if (!doesPropertyExist(propertyIri)) {
return Optional.empty();
}
final URI dpUri = propertyIri.toURI();
// Note: I don't really like the fact that we are basing this on a randomly generated identifier of the unspecified
// property. Perhaps the strategy of using unspecified properties should be revisited.
return Optional.of(assertionMap.containsKey(dpUri) ? assertionMap.get(dpUri) :
assertionMap.get(UNSPECIFIED_ASSERTION.getIdentifier()));
}
private boolean doesPropertyExist(IRI o) {
return assertionMap.containsKey(o.toURI()) || assertionMap.containsKey(UNSPECIFIED_ASSERTION.getIdentifier());
}
private Collection<Axiom<?>> objectPropertyValuesToAxioms(NamedResource subject,
Stream<OWLObjectPropertyAssertionAxiom> axiomStream) {
return axiomStream.filter(axiom ->
doesPropertyExist(axiom.getProperty().asOWLObjectProperty().getIRI()))
.map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
.collect(Collectors.toList());
}
private Collection<Axiom<?>> annotationPropertyValuesToAxioms(NamedResource subject,
Stream<OWLAnnotationAssertionAxiom> axioms) {
return axioms.filter(this::shouldLoadAnnotationPropertyValue)
.map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
.collect(Collectors.toList());
}
private boolean shouldLoadAnnotationPropertyValue(OWLAnnotationAssertionAxiom axiom) {
final OWLAnnotationValue value = axiom.getValue();
final IRI apIri = axiom.getProperty().asOWLAnnotationProperty().getIRI();
final Optional<Assertion> assertion = assertionForAxiom(apIri);
return assertion.isPresent() && (!value.asLiteral().isPresent() ||
OwlapiUtils.doesLanguageMatch(value.asLiteral().get(), assertion.get()));
}
@Override
public Collection<Axiom<?>> loadPropertyAxioms(NamedResource subject) {
final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
final Collection<Axiom<?>> axioms = new ArrayList<>();
ontology.dataPropertyAssertionAxioms(individual)
.forEach(assertion -> axioms.add(axiomAdapter.toAxiom(subject, assertion, false)));
ontology.objectPropertyAssertionAxioms(individual)
.forEach(assertion -> axioms.add(axiomAdapter.toAxiom(subject, assertion, false)));
ontology.annotationAssertionAxioms(individual.getIRI())
.forEach(assertion -> axioms.add(axiomAdapter.toAxiom(subject, assertion, false)));
return axioms;
}
}