Skip to content

Commit 6beef23

Browse files
committed
ont-api: add OWLOntology#disjointClassesAxioms(OWLClass) graph-optimization (issue #12)
1 parent 9c1ccc0 commit 6beef23

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

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

+20-5
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public class InternalModel extends OntGraphModelImpl
162162
protected final ByObjectSearcher<OWLSubClassOfAxiom, OWLClass> subClassOfBySubject = new SubClassOfBySubject();
163163
protected final ByObjectSearcher<OWLSubClassOfAxiom, OWLClass> subClassOfByObject = new SubClassOfByObject();
164164
protected final ByObjectSearcher<OWLEquivalentClassesAxiom, OWLClass> equivalentClassesByClass = new EquivalentClassesByClass();
165+
protected final ByObjectSearcher<OWLDisjointClassesAxiom, OWLClass> disjointClassesByClass = new DisjointClassesByClass();
165166

166167
// To search OWLObjects
167168
protected final ObjectsSearcher<OWLClass> classSearcher = new ClassSearcher();
@@ -709,7 +710,15 @@ public Stream<OWLEquivalentClassesAxiom> listOWLEquivalentClassesAxioms(OWLClass
709710
* @return {@code Stream} of {@link OWLDisjointClassesAxiom}s
710711
*/
711712
public Stream<OWLDisjointClassesAxiom> listOWLDisjointClassesAxioms(OWLClass clazz) {
712-
return listOWLAxioms(OWLDisjointClassesAxiom.class).filter(x -> x.operands().anyMatch(clazz::equals));
713+
InternalConfig config = getConfig();
714+
// bad performance of the graph-reading way if
715+
// there is a []-list with many disjoint classes (i.e. owl:AllDisjointClasses) =>
716+
// it is faster to use parsing of cached axioms (i.e. a classic way), if the cache is present
717+
if (!config.useContentCache() ||
718+
(!hasManuallyAddedAxioms() && !containsLocal(null, RDF.type, OWL.AllDisjointClasses))) {
719+
return listOWLAxioms(disjointClassesByClass, OWLDisjointClassesAxiom.class, clazz, config);
720+
}
721+
return listOWLNaryAxiomAxiomsByOperand(clazz, OWLDisjointClassesAxiom.class);
713722
}
714723

715724
/**
@@ -720,20 +729,26 @@ public Stream<OWLDisjointClassesAxiom> listOWLDisjointClassesAxioms(OWLClass cla
720729
* @param searcher - {@link ByObjectSearcher} for {@link A} and {@link K}
721730
* @param <A> - subtype of {@link OWLNaryAxiom}
722731
* @param <K> - subtype of {@link OWLObject}
723-
* @return {@code Stream} of {@link A}s
732+
* @return a {@code Stream} of {@link A}s
724733
* @see AbstractNaryTranslator#axioms(OntModel)
725734
*/
726-
public <A extends OWLNaryAxiom<? super K>,
735+
@SuppressWarnings("SameParameterValue")
736+
protected <A extends OWLNaryAxiom<? super K>,
727737
K extends OWLObject> Stream<A> listOWLNaryAxiomAxiomsByOperand(K operand,
728738
Class<A> type,
729739
ByObjectSearcher<A, K> searcher) {
730740
InternalConfig config = getConfig();
731741
if (!useAxiomsSearchOptimization(config)) {
732-
return listOWLAxioms(type).filter(a -> a.operands().anyMatch(operand::equals));
742+
return listOWLNaryAxiomAxiomsByOperand(operand, type);
733743
}
734744
return listOWLAxioms(searcher, type, operand, config);
735745
}
736746

747+
protected <A extends OWLNaryAxiom<? super K>,
748+
K extends OWLObject> Stream<A> listOWLNaryAxiomAxiomsByOperand(K operand, Class<A> type) {
749+
return listOWLAxioms(type).filter(a -> a.operands().anyMatch(operand::equals));
750+
}
751+
737752
/**
738753
* Auxiliary method to extract axioms from the graph.
739754
* Note: the method returns non-cached axioms.
@@ -869,7 +884,7 @@ protected boolean useReferencingAxiomsSearchOptimization(OWLComponentType type,
869884
}
870885

871886
/**
872-
* Answers {@code true} if need to use {@link ByObjectSearcher}-search optimization.
887+
* Answers {@code true} if need to use {@link ByObjectSearcher}-search optimization instead of parsing cache.
873888
*
874889
* @param config {@link InternalConfig}
875890
* @return boolean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.DisjointClassesTranslator;
22+
import com.github.owlcs.ontapi.jena.model.*;
23+
import com.github.owlcs.ontapi.jena.utils.Iter;
24+
import com.github.owlcs.ontapi.jena.utils.OntModels;
25+
import com.github.owlcs.ontapi.jena.vocabulary.OWL;
26+
import org.apache.jena.util.iterator.ExtendedIterator;
27+
import org.semanticweb.owlapi.model.OWLClass;
28+
import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom;
29+
30+
/**
31+
* Created by @ssz on 09.05.2020.
32+
*/
33+
public class DisjointClassesByClass extends BaseByObject<OWLDisjointClassesAxiom, OWLClass> {
34+
public static final DisjointClassesTranslator TRANSLATOR = toTranslator(OWLTopObjectType.DISJOINT_CLASSES);
35+
36+
protected ExtendedIterator<OntStatement> listStatements(OntModel model, OntClass clazz) {
37+
ExtendedIterator<OntStatement> simple = listBySubjectAndPredicate(model, clazz, OWL.disjointWith)
38+
.andThen(listByPredicateAndObject(model, OWL.disjointWith, clazz));
39+
ExtendedIterator<OntStatement> bulk = OntModels.listLocalObjects(model, OntDisjoint.Classes.class)
40+
.filterKeep(x -> x.getList().contains(clazz))
41+
.mapWith(OntObject::getMainStatement);
42+
return Iter.concat(simple, bulk);
43+
}
44+
45+
@Override
46+
public ExtendedIterator<ONTObject<OWLDisjointClassesAxiom>> listONTAxioms(OWLClass clazz,
47+
OntModel model,
48+
ONTObjectFactory factory,
49+
AxiomsSettings config) {
50+
ExtendedIterator<OntStatement> res = listStatements(model, model.getOntClass(clazz.getIRI().getIRIString()))
51+
.filterKeep(s -> TRANSLATOR.testStatement(s, config));
52+
return translate(TRANSLATOR, res, factory, config);
53+
}
54+
}

src/main/java/com/github/owlcs/ontapi/jena/impl/OntGraphModelImpl.java

+14
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,20 @@ public OntStatementImpl asStatement(Triple triple) {
641641
return OntStatementImpl.createOntStatementImpl(triple, this);
642642
}
643643

644+
/**
645+
* Determines if the given {@code (s, p, o)} pattern is present in the base graph,
646+
* with {@code null} allowed to represent a wildcard match.
647+
*
648+
* @param s - {@link Resource} - the subject of the statement tested ({@code null} as wildcard)
649+
* @param p - {@link Property} - the predicate of the statement tested ({@code null} as wildcard)
650+
* @param o - {@link RDFNode} - the object of the statement tested ({@code null} as wildcard)
651+
* @return boolean
652+
* @see Model#contains(Resource, Property, RDFNode)
653+
*/
654+
public boolean containsLocal(Resource s, Property p, RDFNode o) {
655+
return getBaseGraph().contains(asNode(s), asNode(p), asNode(o));
656+
}
657+
644658
/**
645659
* Wraps the existing given {@link RDFList []-list} as {@link OntList ONT-list}.
646660
*

0 commit comments

Comments
 (0)