Skip to content

Commit 2488223

Browse files
committed
ontapi: [#55] add axioms(OWLObject, Imports) impls
1 parent bbca7ea commit 2488223

File tree

2 files changed

+177
-35
lines changed

2 files changed

+177
-35
lines changed

src/main/java/com/github/owlcs/ontapi/BaseOntologyModelImpl.java

Lines changed: 102 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,22 @@ public <T extends OWLAxiom> Stream<T> axioms(AxiomType<T> axiomType, Imports imp
654654
*/
655655
@Override
656656
public Stream<OWLClassAxiom> axioms(OWLClass clazz) {
657-
Stream<? extends OWLClassAxiom> subClassOf = base.listOWLSubClassOfAxiomsBySubject(clazz);
658-
Stream<? extends OWLClassAxiom> disjointUnion = base.listOWLDisjointUnionAxioms(clazz);
659-
Stream<? extends OWLClassAxiom> disjoint = base.listOWLDisjointClassesAxioms(clazz);
660-
Stream<? extends OWLClassAxiom> equivalent = base.listOWLEquivalentClassesAxioms(clazz);
657+
return listOWLAxioms(base, clazz);
658+
}
659+
660+
@Override
661+
public Stream<OWLClassAxiom> axioms(OWLClass clazz, Imports imports) {
662+
if (imports == Imports.INCLUDED && !config.useContentCache()) {
663+
return listOWLAxioms(getFullGraphModel(), clazz);
664+
}
665+
return imports.stream(this).flatMap(o -> o.axioms(clazz));
666+
}
667+
668+
private static Stream<OWLClassAxiom> listOWLAxioms(InternalGraphModel model, OWLClass clazz) {
669+
Stream<? extends OWLClassAxiom> subClassOf = model.listOWLSubClassOfAxiomsBySubject(clazz);
670+
Stream<? extends OWLClassAxiom> disjointUnion = model.listOWLDisjointUnionAxioms(clazz);
671+
Stream<? extends OWLClassAxiom> disjoint = model.listOWLDisjointClassesAxioms(clazz);
672+
Stream<? extends OWLClassAxiom> equivalent = model.listOWLEquivalentClassesAxioms(clazz);
661673
return Stream.of(subClassOf, disjointUnion, disjoint, equivalent).flatMap(Function.identity());
662674
}
663675

@@ -674,28 +686,39 @@ public Stream<OWLClassAxiom> axioms(OWLClass clazz) {
674686
* <li>7) Any property characteristic axiom (i.e. Functional, Symmetric, Reflexive etc.) whose subject is the specified property</li>
675687
* <li>8) Inverse properties axioms that contain the specified property</li>
676688
* </ul>
677-
* <b>Note: either condition *3* or OWL-API-5.1.4 implementation (owlapi-impl) are wrong as shown by tests.</b>
678689
*
679690
* @param property The property whose defining axioms are to be retrieved
680691
* @return A {@code Stream} of object property axioms that describe the specified property
681692
*/
682693
@Override
683694
public Stream<OWLObjectPropertyAxiom> axioms(OWLObjectPropertyExpression property) {
684-
Stream<? extends OWLObjectPropertyAxiom> subPropertyOf = base.listOWLSubObjectPropertyOfAxiomsBySubject(property);
695+
return listOWLAxioms(base, property);
696+
}
697+
698+
@Override
699+
public Stream<OWLObjectPropertyAxiom> axioms(OWLObjectPropertyExpression property, Imports imports) {
700+
if (imports == Imports.INCLUDED && !config.useContentCache()) {
701+
return listOWLAxioms(getFullGraphModel(), property);
702+
}
703+
return imports.stream(this).flatMap(o -> o.axioms(property));
704+
}
705+
706+
private static Stream<OWLObjectPropertyAxiom> listOWLAxioms(InternalGraphModel model, OWLObjectPropertyExpression property) {
707+
Stream<? extends OWLObjectPropertyAxiom> subPropertyOf = model.listOWLSubObjectPropertyOfAxiomsBySubject(property);
685708
Stream<? extends OWLObjectPropertyAxiom> nary = Stream.of(
686-
base.listOWLEquivalentObjectPropertiesAxioms(property),
687-
base.listOWLDisjointObjectPropertiesAxioms(property),
688-
base.listOWLInverseObjectPropertiesAxioms(property)).flatMap(Function.identity());
709+
model.listOWLEquivalentObjectPropertiesAxioms(property),
710+
model.listOWLDisjointObjectPropertiesAxioms(property),
711+
model.listOWLInverseObjectPropertiesAxioms(property)).flatMap(Function.identity());
689712
Stream<? extends OWLObjectPropertyAxiom> unary = Stream.of(
690-
base.listOWLObjectPropertyDomainAxioms(property),
691-
base.listOWLObjectPropertyRangeAxioms(property),
692-
base.listOWLTransitiveObjectPropertyAxioms(property),
693-
base.listOWLIrreflexiveObjectPropertyAxioms(property),
694-
base.listOWLReflexiveObjectPropertyAxioms(property),
695-
base.listOWLSymmetricObjectPropertyAxioms(property),
696-
base.listOWLAsymmetricObjectPropertyAxioms(property),
697-
base.listOWLFunctionalObjectPropertyAxioms(property),
698-
base.listOWLInverseFunctionalObjectPropertyAxioms(property)).flatMap(Function.identity());
713+
model.listOWLObjectPropertyDomainAxioms(property),
714+
model.listOWLObjectPropertyRangeAxioms(property),
715+
model.listOWLTransitiveObjectPropertyAxioms(property),
716+
model.listOWLIrreflexiveObjectPropertyAxioms(property),
717+
model.listOWLReflexiveObjectPropertyAxioms(property),
718+
model.listOWLSymmetricObjectPropertyAxioms(property),
719+
model.listOWLAsymmetricObjectPropertyAxioms(property),
720+
model.listOWLFunctionalObjectPropertyAxioms(property),
721+
model.listOWLInverseFunctionalObjectPropertyAxioms(property)).flatMap(Function.identity());
699722
return Stream.of(subPropertyOf, nary, unary).flatMap(Function.identity());
700723
}
701724

@@ -716,14 +739,26 @@ public Stream<OWLObjectPropertyAxiom> axioms(OWLObjectPropertyExpression propert
716739
*/
717740
@Override
718741
public Stream<OWLDataPropertyAxiom> axioms(OWLDataProperty property) {
719-
Stream<? extends OWLDataPropertyAxiom> subPropertyOf = base.listOWLSubDataPropertyOfAxiomsBySubject(property);
742+
return listOWLAxioms(base, property);
743+
}
744+
745+
@Override
746+
public Stream<OWLDataPropertyAxiom> axioms(OWLDataProperty property, Imports imports) {
747+
if (imports == Imports.INCLUDED && !config.useContentCache()) {
748+
return listOWLAxioms(getFullGraphModel(), property);
749+
}
750+
return imports.stream(this).flatMap(o -> o.axioms(property));
751+
}
752+
753+
private static Stream<OWLDataPropertyAxiom> listOWLAxioms(InternalGraphModel model, OWLDataProperty property) {
754+
Stream<? extends OWLDataPropertyAxiom> subPropertyOf = model.listOWLSubDataPropertyOfAxiomsBySubject(property);
720755
Stream<? extends OWLDataPropertyAxiom> nary = Stream.of(
721-
base.listOWLEquivalentDataPropertiesAxioms(property),
722-
base.listOWLDisjointDataPropertiesAxioms(property)).flatMap(Function.identity());
756+
model.listOWLEquivalentDataPropertiesAxioms(property),
757+
model.listOWLDisjointDataPropertiesAxioms(property)).flatMap(Function.identity());
723758
Stream<? extends OWLDataPropertyAxiom> unary = Stream.of(
724-
base.listOWLDataPropertyDomainAxioms(property),
725-
base.listOWLDataPropertyRangeAxioms(property),
726-
base.listOWLFunctionalDataPropertyAxioms(property)).flatMap(Function.identity());
759+
model.listOWLDataPropertyDomainAxioms(property),
760+
model.listOWLDataPropertyRangeAxioms(property),
761+
model.listOWLFunctionalDataPropertyAxioms(property)).flatMap(Function.identity());
727762
return Stream.of(subPropertyOf, nary, unary).flatMap(Function.identity());
728763
}
729764

@@ -741,10 +776,22 @@ public Stream<OWLDataPropertyAxiom> axioms(OWLDataProperty property) {
741776
*/
742777
@Override
743778
public Stream<OWLAnnotationAxiom> axioms(OWLAnnotationProperty property) {
779+
return listOWLAxioms(base, property);
780+
}
781+
782+
@Override
783+
public Stream<OWLAnnotationAxiom> axioms(OWLAnnotationProperty property, Imports imports) {
784+
if (imports == Imports.INCLUDED && !config.useContentCache()) {
785+
return listOWLAxioms(getFullGraphModel(), property);
786+
}
787+
return imports.stream(this).flatMap(o -> o.axioms(property));
788+
}
789+
790+
private static Stream<OWLAnnotationAxiom> listOWLAxioms(InternalGraphModel model, OWLAnnotationProperty property) {
744791
return Stream.of(
745-
base.listOWLSubAnnotationPropertyOfAxiomsBySubject(property),
746-
base.listOWLAnnotationPropertyDomainAxioms(property),
747-
base.listOWLAnnotationPropertyRangeAxioms(property)).flatMap(Function.identity());
792+
model.listOWLSubAnnotationPropertyOfAxiomsBySubject(property),
793+
model.listOWLAnnotationPropertyDomainAxioms(property),
794+
model.listOWLAnnotationPropertyRangeAxioms(property)).flatMap(Function.identity());
748795
}
749796

750797
/**
@@ -765,14 +812,26 @@ public Stream<OWLAnnotationAxiom> axioms(OWLAnnotationProperty property) {
765812
*/
766813
@Override
767814
public Stream<OWLIndividualAxiom> axioms(OWLIndividual individual) {
768-
Stream<? extends OWLIndividualAxiom> classAssertion = base.listOWLClassAssertionAxioms(individual);
769-
Stream<? extends OWLIndividualAxiom> nary = Stream.concat(base.listOWLSameIndividualAxioms(individual),
770-
base.listOWLDifferentIndividualsAxioms(individual));
815+
return listOWLAxioms(base, individual);
816+
}
817+
818+
@Override
819+
public Stream<OWLIndividualAxiom> axioms(OWLIndividual individual, Imports imports) {
820+
if (imports == Imports.INCLUDED && !config.useContentCache()) {
821+
return listOWLAxioms(getFullGraphModel(), individual);
822+
}
823+
return imports.stream(this).flatMap(o -> o.axioms(individual));
824+
}
825+
826+
private static Stream<OWLIndividualAxiom> listOWLAxioms(InternalGraphModel model, OWLIndividual individual) {
827+
Stream<? extends OWLIndividualAxiom> classAssertion = model.listOWLClassAssertionAxioms(individual);
828+
Stream<? extends OWLIndividualAxiom> nary = Stream.concat(model.listOWLSameIndividualAxioms(individual),
829+
model.listOWLDifferentIndividualsAxioms(individual));
771830
Stream<? extends OWLIndividualAxiom> propertyAssertion = Stream.of(
772-
base.listOWLObjectPropertyAssertionAxioms(individual),
773-
base.listOWLDataPropertyAssertionAxioms(individual),
774-
base.listOWLNegativeObjectPropertyAssertionAxioms(individual),
775-
base.listOWLNegativeDataPropertyAssertionAxioms(individual)).flatMap(Function.identity());
831+
model.listOWLObjectPropertyAssertionAxioms(individual),
832+
model.listOWLDataPropertyAssertionAxioms(individual),
833+
model.listOWLNegativeObjectPropertyAssertionAxioms(individual),
834+
model.listOWLNegativeDataPropertyAssertionAxioms(individual)).flatMap(Function.identity());
776835
return Stream.of(classAssertion, nary, propertyAssertion).flatMap(Function.identity());
777836
}
778837

@@ -781,6 +840,14 @@ public Stream<OWLDatatypeDefinitionAxiom> axioms(OWLDatatype datatype) {
781840
return base.listOWLDatatypeDefinitionAxioms(datatype);
782841
}
783842

843+
@Override
844+
public Stream<OWLDatatypeDefinitionAxiom> axioms(OWLDatatype datatype, Imports imports) {
845+
if (imports == Imports.INCLUDED && !config.useContentCache()) {
846+
return getFullGraphModel().listOWLDatatypeDefinitionAxioms(datatype);
847+
}
848+
return imports.stream(this).flatMap(o -> o.axioms(datatype));
849+
}
850+
784851
@SuppressWarnings("unchecked")
785852
@Override
786853
public <A extends OWLAxiom> Stream<A> axioms(OWLAxiomSearchFilter filter, Object key) {
@@ -791,7 +858,7 @@ public <A extends OWLAxiom> Stream<A> axioms(OWLAxiomSearchFilter filter, Object
791858
* The generic search method: results all axioms which refer the given object.
792859
* This method may walk over the whole axiom cache in the {@link #base internal model} or read graph directly,
793860
* as it sees fit.
794-
* Functionally it differs from the original OWL-API method: it can handle a wider class of cases.
861+
* Functionally, it differs from the original OWL-API method: it can handle a wider class of cases.
795862
* For internal usage only.
796863
*
797864
* @param type {@link Class Class&lt;OWLAxiom&gt;}, not null, type of axiom

src/test/java/com/github/owlcs/ontapi/tests/model/InfOntModelTest.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.github.owlcs.ontapi.tests.model;
22

3+
import com.github.owlcs.ontapi.BlankNodeId;
34
import com.github.owlcs.ontapi.DataFactory;
45
import com.github.owlcs.ontapi.OntManagers;
56
import com.github.owlcs.ontapi.Ontology;
67
import com.github.owlcs.ontapi.OntologyManager;
78
import org.apache.jena.ontapi.OntSpecification;
9+
import org.apache.jena.ontapi.model.OntDataRange;
10+
import org.apache.jena.ontapi.model.OntIndividual;
811
import org.apache.jena.ontapi.model.OntModel;
912
import org.apache.jena.vocabulary.OWL;
1013
import org.apache.jena.vocabulary.RDFS;
@@ -85,6 +88,78 @@ void testListAllAxiomsAndGetCounts() {
8588
Assertions.assertEquals(1, o1.getAxiomCount(AxiomType.EQUIVALENT_CLASSES, Imports.INCLUDED));
8689
}
8790

91+
@Test
92+
void testListAxiomsByObject() {
93+
OntologyManager om = OntManagers.createDirectManager();
94+
DataFactory df = om.getOWLDataFactory();
95+
om.getOntologyConfigurator().setSpecification(OntSpecification.OWL2_FULL_MEM_MICRO_RULES_INF);
96+
97+
OntModel m1 = om.createGraphModel("http://a#A");
98+
OntModel m2 = om.createGraphModel("http://b#B");
99+
m1.addImport(m2);
100+
101+
m2.createOntClass("http://b#C1")
102+
.addEquivalentClass(
103+
m2.createOntClass("http://b#C2")
104+
);
105+
m1.createOntClass("http://a#C1");
106+
107+
m2.createIndividual("http://b#i1");
108+
m1.createOntClass("http://a#C1");
109+
m1.createObjectProperty("http://a#p2");
110+
m1.createAnnotationProperty("http://a#p3").addSubProperty(
111+
m1.createAnnotationProperty("http://a#p4")
112+
);
113+
m1.createDataProperty("http://a#p3").addSubProperty(
114+
m2.createDataProperty("http://b#p1")
115+
);
116+
OntIndividual i1 = m1.createIndividual("http://b#i1", m2.getOntClass("http://b#C2"));
117+
OntIndividual i2 = m2.createIndividual(null, m2.getOntClass("http://b#C1"));
118+
i1.addSameAsStatement(i2);
119+
OntDataRange d1 = m2.createDataOneOf(m2.createTypedLiteral(1), m2.createTypedLiteral(2));
120+
OntDataRange d2 = m1.createDatatype("1&2").addEquivalentClass(d1);
121+
122+
Ontology o1 = Objects.requireNonNull(om.getOntology(IRI.create("http://a#A")));
123+
124+
// SubClassOf(<http://b#C2> <http://b#C1>)
125+
// SubClassOf(<http://b#C2> owl:Thing)
126+
// SubClassOf(<http://b#C2> <http://b#C2>)
127+
// SubClassOf(<http://b#C2> rdfs:Resource)
128+
// EquivalentClasses(<http://b#C1> <http://b#C2>)
129+
// EquivalentClasses(<http://b#C2>)
130+
// EquivalentClasses(<http://b#C1> <http://b#C2>)
131+
// EquivalentClasses(<http://b#C2>)
132+
Assertions.assertEquals(8, o1.axioms(df.getOWLClass("http://b#C2"), Imports.INCLUDED).count());
133+
134+
// ObjectPropertyDomain(<http://a#p2> owl:Thing)
135+
// ObjectPropertyDomain(<http://a#p2> rdfs:Resource)
136+
// ObjectPropertyRange(<http://a#p2> owl:Thing)
137+
// ObjectPropertyRange(<http://a#p2> rdfs:Resource)
138+
Assertions.assertEquals(4, o1.axioms(df.getOWLObjectProperty("http://a#p2"), Imports.INCLUDED).count());
139+
140+
// SubDataPropertyOf(<http://b#p1> <http://a#p3>)
141+
// SubDataPropertyOf(<http://b#p1> <http://b#p1>)
142+
// EquivalentDataProperties(<http://b#p1>)
143+
Assertions.assertEquals(3, o1.axioms(df.getOWLDataProperty("http://b#p1"), Imports.INCLUDED).count());
144+
145+
// SubAnnotationPropertyOf(<http://a#p4> <http://a#p3>)
146+
// SubAnnotationPropertyOf(<http://a#p4> <http://a#p4>)
147+
Assertions.assertEquals(2, o1.axioms(df.getOWLAnnotationProperty("http://a#p4"), Imports.INCLUDED).count());
148+
149+
// ClassAssertion(<http://b#C1> _:c9613ce1-e0bd-4e2c-8855-cffc04961da4)
150+
// ClassAssertion(<http://b#C2> _:c9613ce1-e0bd-4e2c-8855-cffc04961da4)
151+
// ClassAssertion(owl:Thing _:c9613ce1-e0bd-4e2c-8855-cffc04961da4)
152+
// ClassAssertion(rdfs:Resource _:c9613ce1-e0bd-4e2c-8855-cffc04961da4)
153+
// SameIndividual(<http://b#i1> _:c9613ce1-e0bd-4e2c-8855-cffc04961da4)
154+
// SameIndividual(<http://b#i1> _:c9613ce1-e0bd-4e2c-8855-cffc04961da4)
155+
// ObjectPropertyAssertion(owl:sameAs _:c9613ce1-e0bd-4e2c-8855-cffc04961da4 <http://b#i1>)
156+
Assertions.assertEquals(7, o1.axioms(df.getOWLAnonymousIndividual(BlankNodeId.of(i2.asNode())), Imports.INCLUDED).count());
157+
158+
// DatatypeDefinition(<1&2> DataOneOf("1"^^xsd:int "2"^^xsd:int))
159+
// DatatypeDefinition(<1&2> <1&2>)
160+
Assertions.assertEquals(2, o1.axioms(df.getOWLDatatype(d2.getURI()), Imports.INCLUDED).count());
161+
}
162+
88163
@Test
89164
void testListSignature1() {
90165
OntologyManager om = OntManagers.createDirectManager();

0 commit comments

Comments
 (0)