Skip to content

Method: dataPropertyValuesToAxioms(NamedResource, Collection)

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.model.Assertion;
18: import cz.cvut.kbss.ontodriver.model.Axiom;
19: import cz.cvut.kbss.ontodriver.model.NamedResource;
20: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
21: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
22: import org.semanticweb.owlapi.model.*;
23:
24: import java.net.URI;
25: import java.util.*;
26: import java.util.stream.Collectors;
27:
28: class ExplicitAxiomLoader implements AxiomLoader {
29:
30: private static final Assertion UNSPECIFIED_ASSERTION = Assertion.createUnspecifiedPropertyAssertion(false);
31:
32: private final OWLOntology ontology;
33: private final OWLDataFactory dataFactory;
34:
35: private final OwlapiAdapter adapter;
36: private final AxiomAdapter axiomAdapter;
37: private final String language;
38:
39: private Map<URI, Assertion> assertionMap;
40:
41: ExplicitAxiomLoader(OwlapiAdapter adapter, OntologySnapshot snapshot) {
42: this.adapter = adapter;
43: this.ontology = snapshot.getOntology();
44: this.dataFactory = snapshot.getDataFactory();
45: this.axiomAdapter = new AxiomAdapter(dataFactory, adapter.getLanguage());
46: this.language = adapter.getLanguage();
47: }
48:
49: @Override
50: public Collection<Axiom<?>> loadAxioms(NamedResource subject, Set<Assertion> assertions) {
51: this.assertionMap = new HashMap<>(assertions.size());
52: assertions.forEach(a -> assertionMap.put(a.getIdentifier(), a));
53: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
54: final Collection<Axiom<?>> axioms = new ArrayList<>();
55: if (assertions.contains(Assertion.createClassAssertion(false))) {
56: axioms.addAll(adapter.getTypesHandler().getTypes(subject, null, false));
57: }
58: // This involves a lot of filtering, perhaps we should use EntitySearcher and look for values of concrete properties
59: final Collection<OWLDataPropertyAssertionAxiom> dpAssertions = ontology.getDataPropertyAssertionAxioms(
60: individual);
61: axioms.addAll(dataPropertyValuesToAxioms(subject, dpAssertions));
62: final Collection<OWLObjectPropertyAssertionAxiom> opAssertions = ontology.getObjectPropertyAssertionAxioms(
63: individual);
64: axioms.addAll(objectPropertyValuesToAxioms(subject, opAssertions));
65: final Collection<OWLAnnotationAssertionAxiom> apAssertions = ontology.getAnnotationAssertionAxioms(
66: individual.getIRI());
67: axioms.addAll(annotationPropertyValuesToAxioms(subject, apAssertions));
68: return axioms;
69: }
70:
71: private Collection<Axiom<?>> dataPropertyValuesToAxioms(NamedResource subject,
72: Collection<OWLDataPropertyAssertionAxiom> axioms) {
73: return axioms.stream().filter(this::shouldLoadDataPropertyValue)
74: .map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
75: .collect(Collectors.toList());
76: }
77:
78: private boolean shouldLoadDataPropertyValue(OWLDataPropertyAssertionAxiom axiom) {
79: final OWLLiteral value = axiom.getObject();
80: final IRI dpIri = axiom.getProperty().asOWLDataProperty().getIRI();
81: final boolean propertyExists = doesPropertyExist(dpIri);
82: if (!propertyExists) {
83: return false;
84: }
85: final URI dpUri = dpIri.toURI();
86: // Note: I don't really like the fact that we are basing this on a randomly generated identifier of the unspecified
87: // property. Perhaps the strategy of using unspecified properties should be revisited.
88: final Assertion assertion = assertionMap.containsKey(dpUri) ? assertionMap.get(dpUri) :
89: assertionMap.get(UNSPECIFIED_ASSERTION.getIdentifier());
90: return OwlapiUtils.doesLanguageMatch(value, assertion.hasLanguage() ? assertion.getLanguage() : language);
91: }
92:
93: private boolean doesPropertyExist(IRI o) {
94: return assertionMap.containsKey(o.toURI()) || assertionMap.containsKey(UNSPECIFIED_ASSERTION.getIdentifier());
95: }
96:
97: private Collection<Axiom<?>> objectPropertyValuesToAxioms(NamedResource subject,
98: Collection<OWLObjectPropertyAssertionAxiom> axioms) {
99: return axioms.stream().filter(axiom ->
100: doesPropertyExist(axiom.getProperty().asOWLObjectProperty().getIRI()))
101: .map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
102: .collect(Collectors.toList());
103: }
104:
105: private Collection<Axiom<?>> annotationPropertyValuesToAxioms(NamedResource subject,
106: Collection<OWLAnnotationAssertionAxiom> axioms) {
107: return axioms.stream().filter(this::shouldLoadAnnotationPropertyValue)
108: .map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
109: .collect(Collectors.toList());
110: }
111:
112: private boolean shouldLoadAnnotationPropertyValue(OWLAnnotationAssertionAxiom axiom) {
113: final OWLAnnotationValue value = axiom.getValue();
114: final IRI apIri = axiom.getProperty().asOWLAnnotationProperty().getIRI();
115: final boolean propertyExists = doesPropertyExist(apIri);
116: if (!propertyExists) {
117: return false;
118: }
119: final URI apUri = apIri.toURI();
120: final Assertion assertion = assertionMap.containsKey(apUri) ? assertionMap.get(apUri) :
121: assertionMap.get(UNSPECIFIED_ASSERTION.getIdentifier());
122: return !value.asLiteral().isPresent() || OwlapiUtils.doesLanguageMatch(value.asLiteral().get(),
123: assertion.hasLanguage() ? assertion.getLanguage() : language);
124: }
125:
126: @Override
127: public Collection<Axiom<?>> loadPropertyAxioms(NamedResource subject) {
128: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
129: final Collection<Axiom<?>> axioms = new ArrayList<>();
130: ontology.getDataPropertyAssertionAxioms(individual)
131: .forEach(assertion -> axioms.add(axiomAdapter.toAxiom(subject, assertion, false)));
132: ontology.getObjectPropertyAssertionAxioms(individual)
133: .forEach(assertion -> axioms.add(axiomAdapter.toAxiom(subject, assertion, false)));
134: ontology.getAnnotationAssertionAxioms(individual.getIRI())
135: .forEach(assertion -> axioms.add(axiomAdapter.toAxiom(subject, assertion, false)));
136: return axioms;
137: }
138: }