Skip to content

Commit 433d5b0

Browse files
committed
ont-api: add OWLOntology#objectPropertyRangeAxioms(OWLObjectExpression) optimization (issue #12) + tests
1 parent fa1bbd5 commit 433d5b0

File tree

7 files changed

+107
-15
lines changed

7 files changed

+107
-15
lines changed

src/main/java/com/github/owlcs/ontapi/internal/InternalModel.java

+11
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ public class InternalModel extends OntGraphModelImpl
162162
= new DataAssertionBySubject();
163163
protected final ByObjectSearcher<OWLObjectPropertyAssertionAxiom, OWLIndividual> objectAssertionsBySubject
164164
= new ObjectAssertionBySubject();
165+
protected final ByObjectSearcher<OWLObjectPropertyRangeAxiom, OWLObjectPropertyExpression> objectPropertyRangeByProperty
166+
= new ObjectPropertyRangeBySubject();
165167
protected final ByObjectSearcher<OWLSubClassOfAxiom, OWLClass> subClassOfBySubject = new SubClassOfBySubject();
166168
protected final ByObjectSearcher<OWLSubClassOfAxiom, OWLClass> subClassOfByObject = new SubClassOfByObject();
167169
protected final ByObjectSearcher<OWLEquivalentClassesAxiom, OWLClass> equivalentClassesByClass
@@ -679,6 +681,15 @@ public Stream<OWLObjectPropertyAssertionAxiom> listOWLObjectPropertyAssertionAxi
679681
return listOWLAxioms(objectAssertionsBySubject, OWLObjectPropertyAssertionAxiom.class, subject, config);
680682
}
681683

684+
@Override
685+
public Stream<OWLObjectPropertyRangeAxiom> listOWLObjectPropertyRangeAxioms(OWLObjectPropertyExpression subject) {
686+
InternalConfig config = getConfig();
687+
if (!ObjectPropertyRangeBySubject.isSupported(subject) || !useAxiomsSearchOptimization(config)) {
688+
return ListAxioms.super.listOWLObjectPropertyRangeAxioms(subject);
689+
}
690+
return listOWLAxioms(objectPropertyRangeByProperty, OWLObjectPropertyRangeAxiom.class, subject, config);
691+
}
692+
682693
@Override
683694
public Stream<OWLClassAssertionAxiom> listOWLClassAssertionAxioms(OWLIndividual subject) {
684695
InternalConfig config = getConfig();

src/main/java/com/github/owlcs/ontapi/internal/ListAxioms.java

+7
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ default Stream<OWLObjectPropertyDomainAxiom> listOWLObjectPropertyDomainAxioms(O
186186
return listOWLPropertyAxioms(OWLObjectPropertyDomainAxiom.class, subject);
187187
}
188188

189+
/**
190+
* Lists {@link OWLObjectPropertyRangeAxiom ObjectPropertyRange Axiom}s
191+
* by the given {@link OWLObjectPropertyExpression property}-subject.
192+
*
193+
* @param subject {@link OWLObjectPropertyExpression}, not {@code null}
194+
* @return a {@code Stream} of {@link OWLObjectPropertyRangeAxiom}s
195+
*/
189196
default Stream<OWLObjectPropertyRangeAxiom> listOWLObjectPropertyRangeAxioms(OWLObjectPropertyExpression subject) {
190197
return listOWLPropertyAxioms(OWLObjectPropertyRangeAxiom.class, subject);
191198
}

src/main/java/com/github/owlcs/ontapi/internal/axioms/ObjectPropertyRangeTranslator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Class<OntObjectProperty> getView() {
4949
}
5050

5151
@Override
52-
protected boolean filter(OntStatement statement, AxiomsSettings config) {
52+
public boolean filter(OntStatement statement, AxiomsSettings config) {
5353
return super.filter(statement, config) && statement.getObject().canAs(OntClass.class);
5454
}
5555

src/main/java/com/github/owlcs/ontapi/internal/searchers/axioms/BaseByObject.java

+12
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,19 @@ static Resource asResource(OWLClassExpression clazz) {
6262
throw new OntApiException.Unsupported("Unsupported class-expression " + clazz);
6363
}
6464

65+
static Resource asResource(OWLObjectPropertyExpression property) {
66+
if (property.isOWLObjectProperty()) return asResource((OWLEntity) property.asOWLObjectProperty());
67+
if (property instanceof ONTExpressionImpl) {
68+
return new ResourceImpl(((ONTExpressionImpl<?>) property).asNode(), null);
69+
}
70+
throw new OntApiException.Unsupported("Unsupported object-expression " + property);
71+
}
72+
6573
public static boolean isSupported(OWLClassExpression clazz) {
6674
return clazz.isOWLClass() || clazz instanceof ONTExpressionImpl;
6775
}
76+
77+
public static boolean isSupported(OWLObjectPropertyExpression property) {
78+
return property.isOWLObjectProperty() || property instanceof ONTExpressionImpl;
79+
}
6880
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the ONT API.
3+
* The contents of this file are subject to the LGPL License, Version 3.0.
4+
* Copyright (c) 2020, The University of Manchester, owl.cs group.
5+
*
6+
* 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.
7+
* 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.
8+
* You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
9+
*
10+
* Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above.
11+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
12+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
13+
*/
14+
15+
package com.github.owlcs.ontapi.internal.searchers.axioms;
16+
17+
import com.github.owlcs.ontapi.config.AxiomsSettings;
18+
import com.github.owlcs.ontapi.internal.ONTObject;
19+
import com.github.owlcs.ontapi.internal.ONTObjectFactory;
20+
import com.github.owlcs.ontapi.internal.OWLTopObjectType;
21+
import com.github.owlcs.ontapi.internal.axioms.ObjectPropertyRangeTranslator;
22+
import com.github.owlcs.ontapi.jena.model.OntModel;
23+
import org.apache.jena.util.iterator.ExtendedIterator;
24+
import org.apache.jena.vocabulary.RDFS;
25+
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
26+
import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom;
27+
28+
/**
29+
* Created by @ssz on 17.05.2020.
30+
*/
31+
public class ObjectPropertyRangeBySubject extends BaseByObject<OWLObjectPropertyRangeAxiom, OWLObjectPropertyExpression> {
32+
private static final ObjectPropertyRangeTranslator TRANSLATOR = getTranslator(OWLTopObjectType.OBJECT_PROPERTY_RANGE);
33+
34+
@Override
35+
public ExtendedIterator<ONTObject<OWLObjectPropertyRangeAxiom>> listONTAxioms(OWLObjectPropertyExpression subject,
36+
OntModel model,
37+
ONTObjectFactory factory,
38+
AxiomsSettings config) {
39+
return translate(TRANSLATOR, listBySubjectAndPredicate(model, asResource(subject), RDFS.range)
40+
.filterKeep(s -> TRANSLATOR.filter(s, config)), factory, config);
41+
}
42+
}

src/main/java/com/github/owlcs/ontapi/internal/searchers/axioms/SubClassOfByOperand.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public final ExtendedIterator<ONTObject<OWLSubClassOfAxiom>> listONTAxioms(OWLCl
3939
OntModel model,
4040
ONTObjectFactory factory,
4141
AxiomsSettings config) {
42-
return translate(TRANSLATOR, listStatements(model, asResource(clazz))
43-
.filterKeep(TRANSLATOR::filter), factory, config);
42+
return translate(TRANSLATOR, listStatements(model, asResource(clazz)).filterKeep(TRANSLATOR::filter), factory, config);
4443
}
4544
}

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

+33-12
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ public void testObjectPropertyAssertionAxioms() {
9898
x -> Stream.concat(x.individualsInSignature(), x.anonymousIndividuals()));
9999
}
100100

101+
@Test
102+
public void testObjectPropertyRangeAxioms() {
103+
data.doTest(T.OBJECT_PROPERTY_RANGE_BY_SUBJECT, HasObjectPropertiesInSignature::objectPropertiesInSignature);
104+
}
105+
101106
@Test
102107
public void testClassAssertionAxiomsForIndividual() {
103108
data.doTest(T.CLASS_ASSERTION_BY_SUBJECT,
@@ -120,7 +125,8 @@ enum TestData {
120125
T.DATA_PROPERTY_ASSERTION_BY_SUBJECT.of(),
121126
T.OBJECT_PROPERTY_ASSERTION_BY_SUBJECT.of(),
122127
T.CLASS_ASSERTION_BY_SUBJECT.of(-3513486065L),
123-
T.CLASS_ASSERTION_BY_OBJECT.of(-3513486065L)
128+
T.CLASS_ASSERTION_BY_OBJECT.of(-3513486065L),
129+
T.OBJECT_PROPERTY_RANGE_BY_SUBJECT.of(-5171741903L)
124130
),
125131
FAMILY(ModelData.FAMILY,
126132
T.DECLARATIONS.of(34226271096L),
@@ -132,7 +138,8 @@ enum TestData {
132138
T.DATA_PROPERTY_ASSERTION_BY_SUBJECT.of(-46062903685L),
133139
T.OBJECT_PROPERTY_ASSERTION_BY_SUBJECT.of(-44647629109L),
134140
T.CLASS_ASSERTION_BY_SUBJECT.of(-2344424939L),
135-
T.CLASS_ASSERTION_BY_OBJECT.of(-2344424939L)
141+
T.CLASS_ASSERTION_BY_OBJECT.of(-2344424939L),
142+
T.OBJECT_PROPERTY_RANGE_BY_SUBJECT.of(11112139379L)
136143
),
137144
PEOPLE(ModelData.PEOPLE,
138145
T.DECLARATIONS.of(-31040926516L),
@@ -144,7 +151,8 @@ enum TestData {
144151
T.DATA_PROPERTY_ASSERTION_BY_SUBJECT.of(),
145152
T.OBJECT_PROPERTY_ASSERTION_BY_SUBJECT.of(-1008566325L),
146153
T.CLASS_ASSERTION_BY_SUBJECT.of(295642609L),
147-
T.CLASS_ASSERTION_BY_OBJECT.of(295642609L) // see https://github.com/owlcs/owlapi/issues/930
154+
T.CLASS_ASSERTION_BY_OBJECT.of(295642609L), // see https://github.com/owlcs/owlapi/issues/930
155+
T.OBJECT_PROPERTY_RANGE_BY_SUBJECT.of(-2697317876L)
148156
),
149157
CAMERA(ModelData.CAMERA,
150158
T.DECLARATIONS.of(2967944221L),
@@ -156,7 +164,8 @@ enum TestData {
156164
T.DATA_PROPERTY_ASSERTION_BY_SUBJECT.of(),
157165
T.OBJECT_PROPERTY_ASSERTION_BY_SUBJECT.of(),
158166
T.CLASS_ASSERTION_BY_SUBJECT.of(-546744276L),
159-
T.CLASS_ASSERTION_BY_OBJECT.of(-546744276L)
167+
T.CLASS_ASSERTION_BY_OBJECT.of(-546744276L),
168+
T.OBJECT_PROPERTY_RANGE_BY_SUBJECT.of(-4417906154L)
160169
),
161170
KOALA(ModelData.KOALA,
162171
T.DECLARATIONS.of(6488467972L),
@@ -168,7 +177,8 @@ enum TestData {
168177
T.DATA_PROPERTY_ASSERTION_BY_SUBJECT.of(),
169178
T.OBJECT_PROPERTY_ASSERTION_BY_SUBJECT.of(),
170179
T.CLASS_ASSERTION_BY_SUBJECT.of(-6315703213L),
171-
T.CLASS_ASSERTION_BY_OBJECT.of(-6315703213L)
180+
T.CLASS_ASSERTION_BY_OBJECT.of(-6315703213L),
181+
T.OBJECT_PROPERTY_RANGE_BY_SUBJECT.of(166159343L)
172182
),
173183
TRAVEL(ModelData.TRAVEL,
174184
T.DECLARATIONS.of(-25825023334L),
@@ -180,7 +190,8 @@ enum TestData {
180190
T.DATA_PROPERTY_ASSERTION_BY_SUBJECT.of(),
181191
T.OBJECT_PROPERTY_ASSERTION_BY_SUBJECT.of(1580148819L),
182192
T.CLASS_ASSERTION_BY_SUBJECT.of(156661309L),
183-
T.CLASS_ASSERTION_BY_OBJECT.of(156661309L)
193+
T.CLASS_ASSERTION_BY_OBJECT.of(156661309L),
194+
T.OBJECT_PROPERTY_RANGE_BY_SUBJECT.of(3821903939L)
184195
),
185196
WINE(ModelData.WINE,
186197
T.DECLARATIONS.of(20065711780L),
@@ -192,7 +203,8 @@ enum TestData {
192203
T.DATA_PROPERTY_ASSERTION_BY_SUBJECT.of(2039350484L),
193204
T.OBJECT_PROPERTY_ASSERTION_BY_SUBJECT.of(24229827352L),
194205
T.CLASS_ASSERTION_BY_SUBJECT.of(-13302103928L),
195-
T.CLASS_ASSERTION_BY_OBJECT.of(-13302103928L)
206+
T.CLASS_ASSERTION_BY_OBJECT.of(-13302103928L),
207+
T.OBJECT_PROPERTY_RANGE_BY_SUBJECT.of(-3535340229L)
196208
),
197209
FOOD(ModelData.FOOD,
198210
T.DECLARATIONS.of(6794851452L),
@@ -204,7 +216,8 @@ enum TestData {
204216
T.DATA_PROPERTY_ASSERTION_BY_SUBJECT.of(),
205217
T.OBJECT_PROPERTY_ASSERTION_BY_SUBJECT.of(),
206218
T.CLASS_ASSERTION_BY_SUBJECT.of(696330992L),
207-
T.CLASS_ASSERTION_BY_OBJECT.of(696330992L)
219+
T.CLASS_ASSERTION_BY_OBJECT.of(696330992L),
220+
T.OBJECT_PROPERTY_RANGE_BY_SUBJECT.of(-144528768L)
208221
),
209222
NCBITAXON_CUT(ModelData.NCBITAXON_CUT,
210223
T.DECLARATIONS.of(244310200631L),
@@ -216,7 +229,8 @@ enum TestData {
216229
T.DATA_PROPERTY_ASSERTION_BY_SUBJECT.of(-22700580140L),
217230
T.OBJECT_PROPERTY_ASSERTION_BY_SUBJECT.of(),
218231
T.CLASS_ASSERTION_BY_SUBJECT.of(),
219-
T.CLASS_ASSERTION_BY_OBJECT.of()
232+
T.CLASS_ASSERTION_BY_OBJECT.of(),
233+
T.OBJECT_PROPERTY_RANGE_BY_SUBJECT.of(-5754775670L)
220234
),
221235
HP_CUT(ModelData.HP_CUT,
222236
T.DECLARATIONS.of(-14640456193L),
@@ -228,7 +242,8 @@ enum TestData {
228242
T.DATA_PROPERTY_ASSERTION_BY_SUBJECT.of(),
229243
T.OBJECT_PROPERTY_ASSERTION_BY_SUBJECT.of(),
230244
T.CLASS_ASSERTION_BY_SUBJECT.of(),
231-
T.CLASS_ASSERTION_BY_OBJECT.of()
245+
T.CLASS_ASSERTION_BY_OBJECT.of(),
246+
T.OBJECT_PROPERTY_RANGE_BY_SUBJECT.of()
232247
),
233248
FAMILY_PEOPLE_UNION(ModelData.FAMILY_PEOPLE_UNION,
234249
T.DECLARATIONS.of(-637777500L),
@@ -240,7 +255,8 @@ enum TestData {
240255
T.DATA_PROPERTY_ASSERTION_BY_SUBJECT.of(-2596921457L),
241256
T.OBJECT_PROPERTY_ASSERTION_BY_SUBJECT.of(),
242257
T.CLASS_ASSERTION_BY_SUBJECT.of(-25907713L),
243-
T.CLASS_ASSERTION_BY_OBJECT.of(-25907713L)
258+
T.CLASS_ASSERTION_BY_OBJECT.of(-25907713L),
259+
T.OBJECT_PROPERTY_RANGE_BY_SUBJECT.of()
244260
),
245261
;
246262
private final ModelData resource;
@@ -328,7 +344,12 @@ Stream<? extends OWLObject> listAxioms(OWLOntology ont, OWLObject param) {
328344
return ont.classAssertionAxioms((OWLClassExpression) param);
329345
}
330346
},
331-
;
347+
OBJECT_PROPERTY_RANGE_BY_SUBJECT {
348+
@Override
349+
Stream<? extends OWLObject> listAxioms(OWLOntology ont, OWLObject param) {
350+
return ont.objectPropertyRangeAxioms((OWLObjectPropertyExpression) param);
351+
}
352+
};
332353

333354
private ByPrimitiveTester of() {
334355
return of(0);

0 commit comments

Comments
 (0)