From 123f0f06f61e3dba87366d117368830f8111c5e7 Mon Sep 17 00:00:00 2001 From: szuev Date: Tue, 28 Apr 2020 21:34:01 +0300 Subject: [PATCH] ont-api: add ClassSearcher (graph optimization) - the facility to retrieve OWLClass signature (issue #15) --- .../owlcs/ontapi/internal/InternalModel.java | 62 +++++++--- .../owlcs/ontapi/internal/ObjectSearcher.java | 42 +++++++ .../internal/searchers/BaseByObject.java | 3 +- .../ontapi/internal/searchers/ByClass.java | 2 +- .../internal/searchers/ByPrimitive.java | 70 +---------- .../internal/searchers/ClassSearcher.java | 113 ++++++++++++++++++ .../internal/searchers/WithRootSearcher.java | 98 +++++++++++++++ .../tests/model/ReferencingAxiomsTest.java | 2 +- 8 files changed, 308 insertions(+), 84 deletions(-) create mode 100644 src/main/java/com/github/owlcs/ontapi/internal/ObjectSearcher.java create mode 100644 src/main/java/com/github/owlcs/ontapi/internal/searchers/ClassSearcher.java create mode 100644 src/main/java/com/github/owlcs/ontapi/internal/searchers/WithRootSearcher.java diff --git a/src/main/java/com/github/owlcs/ontapi/internal/InternalModel.java b/src/main/java/com/github/owlcs/ontapi/internal/InternalModel.java index d0f16bf28..ce7a64786 100644 --- a/src/main/java/com/github/owlcs/ontapi/internal/InternalModel.java +++ b/src/main/java/com/github/owlcs/ontapi/internal/InternalModel.java @@ -155,9 +155,14 @@ public class InternalModel extends OntGraphModelImpl protected final ByObject byIRI = new ByIRI(); // Other searchers protected final ByObject declarationsByEntity = new DeclarationByEntity(); - protected final ByObject annotationAssertionBySubject = new AnnotationAssertionBySubject(); protected final ByObject subClassOfBySubject = new SubClassOfBySubject(); - protected final ByObject equivalentClassesByOperand = new EquivalentClassesByClass(); + protected final ByObject annotationAssertionsBySubject + = new AnnotationAssertionBySubject(); + protected final ByObject equivalentClassesByOperand + = new EquivalentClassesByClass(); + + // To search OWLObjects + protected final ObjectSearcher classSearcher = new ClassSearcher(); /** * Constructs a model instance. @@ -475,7 +480,7 @@ public Stream listOWLEntities(IRI iri) { public boolean containsOWLDeclaration(OWLEntity e) { InternalConfig config = getConfig(); if (!config.isAllowReadDeclarations()) return false; - if (useSearchOptimization(config)) { + if (useAxiomsSearchOptimization(config)) { return getBaseGraph().contains(WriteHelper.toNode(e), RDF.type.asNode(), WriteHelper.getRDFType(e).asNode()); } return listOWLAxioms(OWLDeclarationAxiom.class).anyMatch(x -> x.getEntity().equals(e)); @@ -631,7 +636,7 @@ public Stream listOWLDeclarationAxioms(OWLEntity e) { // since there a lot of ways how to write the same amount of information via axioms. // This differs from OWL-API expectations, so need to perform traversing over whole cache // to get an axiom in the exactly same form as it has been specified manually: - if (!useSearchOptimization(config)) { + if (!useAxiomsSearchOptimization(config)) { return listOWLAxioms(OWLDeclarationAxiom.class).filter(a -> e.equals(a.getEntity())); } // in the case of a large ontology, the direct traverse over the graph works significantly faster: @@ -650,10 +655,10 @@ public Stream listOWLDeclarationAxioms(OWLEntity e) { public Stream listOWLAnnotationAssertionAxioms(OWLAnnotationSubject s) { InternalConfig config = getConfig(); if (!config.isLoadAnnotationAxioms()) return Stream.empty(); - if (!useSearchOptimization(config)) { + if (!useAxiomsSearchOptimization(config)) { return listOWLAxioms(OWLAnnotationAssertionAxiom.class).filter(a -> s.equals(a.getSubject())); } - return reduce(Iter.asStream(annotationAssertionBySubject.listAxioms(s, this::getSearchModel, getObjectFactory(), config) + return reduce(Iter.asStream(annotationAssertionsBySubject.listAxioms(s, this::getSearchModel, getObjectFactory(), config) .mapWith(ONTObject::getOWLObject))); } @@ -666,7 +671,7 @@ public Stream listOWLAnnotationAssertionAxioms(OWLA */ public Stream listOWLSubClassOfAxioms(OWLClass sub) { InternalConfig config = getConfig(); - if (!useSearchOptimization(config)) { + if (!useAxiomsSearchOptimization(config)) { return listOWLAxioms(OWLSubClassOfAxiom.class).filter(a -> Objects.equals(a.getSubClass(), sub)); } return reduce(Iter.asStream(subClassOfBySubject.listAxioms(sub, this::getSearchModel, getObjectFactory(), config) @@ -683,7 +688,7 @@ public Stream listOWLSubClassOfAxioms(OWLClass sub) { */ public Stream listOWLEquivalentClassesAxioms(OWLClass c) { InternalConfig config = getConfig(); - if (!useSearchOptimization(config)) { + if (!useAxiomsSearchOptimization(config)) { return listOWLAxioms(OWLEquivalentClassesAxiom.class).filter(a -> a.operands().anyMatch(c::equals)); } return reduce(Iter.asStream(equivalentClassesByOperand.listAxioms(c, this::getSearchModel, getObjectFactory(), config) @@ -746,6 +751,7 @@ public Stream listOWLAxioms(OWLPrimitive primitive) { * @param type {@link OWLComponentType} * @param config {@link InternalConfig} * @return boolean + * @see #useAxiomsSearchOptimization(InternalConfig) */ protected boolean useReferencingAxiomsSearchOptimization(OWLComponentType type, InternalConfig config) { if (!config.useContentCache()) { @@ -791,8 +797,10 @@ protected boolean useReferencingAxiomsSearchOptimization(OWLComponentType type, * * @param config {@link InternalConfig} * @return boolean + * @see #useObjectsSearchOptimization(InternalConfig) + * @see #useReferencingAxiomsSearchOptimization(OWLComponentType, InternalConfig) */ - protected boolean useSearchOptimization(InternalConfig config) { + protected boolean useAxiomsSearchOptimization(InternalConfig config) { return !config.useContentCache() || !hasManuallyAddedAxioms(); } @@ -1373,12 +1381,8 @@ protected ObjectMap getComponentCache(OWLComponentType * @see OWLComponentType */ protected ObjectMap createComponentObjectMap(OWLComponentType key) { - // todo: replace parsing the content cache with the direct graph reading - InternalObjectFactory df = getObjectFactory(); - OntModel m = getSearchModel(); - Supplier>> loader = () -> selectContentObjects(key) - .flatMap(x -> key.select(x, m, df)).iterator(); InternalConfig conf = getConfig(); + Supplier>> loader = () -> listOWLObjects(key, conf); if (!conf.useComponentCache()) { // todo: need a straight way to find ONTObject that present in the graph, // the default one is extremely inefficient @@ -1389,6 +1393,36 @@ protected ObjectMap createComponentObjectMap(OWLComponentType key) { return new CacheObjectMapImpl<>(loader, false, parallel, fastIterator); } + /** + * Lists all objects of the specified {@code type}. + * + * @param type {@link OWLComponentType} - owl object type, that is used in the object's cache + * @param conf {@link InternalConfig} + * @return an {@code Iterator} of {@link ONTObject} with the given type + */ + protected Iterator> listOWLObjects(OWLComponentType type, InternalConfig conf) { + InternalObjectFactory factory = getObjectFactory(); + OntModel model = getSearchModel(); + if (OWLComponentType.CLASS == type && useObjectsSearchOptimization(conf)) { + //noinspection rawtypes + return ((Iterator) classSearcher.listObjects(model, factory, conf)); + } + // if content cache is loaded its parsing is faster than graph-optimization + return selectContentObjects(type).flatMap(x -> type.select(x, model, factory)).iterator(); + } + + /** + * Answers {@code true} when need to use {@link ObjectSearcher} optimization. + * + * @param config {@link InternalConfig}, not {@code null} + * @return boolean + * @see #useAxiomsSearchOptimization(InternalConfig) + * @see #useReferencingAxiomsSearchOptimization(OWLComponentType, InternalConfig) + */ + protected boolean useObjectsSearchOptimization(InternalConfig config) { + return !config.useContentCache() || getContentStore().values().stream().anyMatch(x -> !x.isLoaded()); + } + /** * Creates a component store {@code Map}. * diff --git a/src/main/java/com/github/owlcs/ontapi/internal/ObjectSearcher.java b/src/main/java/com/github/owlcs/ontapi/internal/ObjectSearcher.java new file mode 100644 index 000000000..d15a7584d --- /dev/null +++ b/src/main/java/com/github/owlcs/ontapi/internal/ObjectSearcher.java @@ -0,0 +1,42 @@ +/* + * This file is part of the ONT API. + * The contents of this file are subject to the LGPL License, Version 3.0. + * Copyright (c) 2020, The University of Manchester, owl.cs group. + * + * 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. + * 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/. + * + * 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. + * 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 + * 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. + */ + +package com.github.owlcs.ontapi.internal; + +import com.github.owlcs.ontapi.jena.model.OntModel; +import org.apache.jena.util.iterator.ExtendedIterator; +import org.semanticweb.owlapi.model.OWLObject; + +/** + * An objects searcher. + * TODO: add contains (see issue #15) + * Created by @ssz on 19.04.2020. + * + * @param - {@link OWLObject} subtype + * @see AxiomTranslator + */ +public interface ObjectSearcher { + + /** + * Lists all objects from the specified {@code model} + * + * @param model {@link OntModel}, not {@code null} + * @param factory {@link InternalObjectFactory}, not {@code null} + * @param config {@link InternalConfig}, not {@code null} + * @return an {@link ExtendedIterator} over {@link O} wrapped with {@link ONTObject} + */ + ExtendedIterator> listObjects(OntModel model, + InternalObjectFactory factory, + InternalConfig config); +} diff --git a/src/main/java/com/github/owlcs/ontapi/internal/searchers/BaseByObject.java b/src/main/java/com/github/owlcs/ontapi/internal/searchers/BaseByObject.java index b91128aec..55aa395db 100644 --- a/src/main/java/com/github/owlcs/ontapi/internal/searchers/BaseByObject.java +++ b/src/main/java/com/github/owlcs/ontapi/internal/searchers/BaseByObject.java @@ -14,7 +14,6 @@ package com.github.owlcs.ontapi.internal.searchers; -import com.github.owlcs.ontapi.internal.BaseSearcher; import com.github.owlcs.ontapi.internal.ByObject; import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLObject; @@ -23,5 +22,5 @@ * Created by @ssz on 18.04.2020. */ @SuppressWarnings("SameParameterValue") -abstract class BaseByObject extends BaseSearcher implements ByObject { +abstract class BaseByObject extends WithRootSearcher implements ByObject { } diff --git a/src/main/java/com/github/owlcs/ontapi/internal/searchers/ByClass.java b/src/main/java/com/github/owlcs/ontapi/internal/searchers/ByClass.java index 7b979aaa9..9a937ef08 100644 --- a/src/main/java/com/github/owlcs/ontapi/internal/searchers/ByClass.java +++ b/src/main/java/com/github/owlcs/ontapi/internal/searchers/ByClass.java @@ -33,7 +33,7 @@ */ public class ByClass extends WithCardinality { - private static final Set>> OBJECT_CARDINALITY_TYPES = + public static final Set>> OBJECT_CARDINALITY_TYPES = Stream.of(OntClass.ObjectMaxCardinality.class, OntClass.ObjectMinCardinality.class, OntClass.ObjectCardinality.class) .collect(Iter.toUnmodifiableSet()); diff --git a/src/main/java/com/github/owlcs/ontapi/internal/searchers/ByPrimitive.java b/src/main/java/com/github/owlcs/ontapi/internal/searchers/ByPrimitive.java index e5b821f07..3494193cf 100644 --- a/src/main/java/com/github/owlcs/ontapi/internal/searchers/ByPrimitive.java +++ b/src/main/java/com/github/owlcs/ontapi/internal/searchers/ByPrimitive.java @@ -17,19 +17,19 @@ import com.github.owlcs.ontapi.internal.*; import com.github.owlcs.ontapi.jena.impl.PersonalityModel; import com.github.owlcs.ontapi.jena.impl.conf.OntPersonality; -import com.github.owlcs.ontapi.jena.model.*; +import com.github.owlcs.ontapi.jena.model.OntAnnotation; +import com.github.owlcs.ontapi.jena.model.OntModel; +import com.github.owlcs.ontapi.jena.model.OntObject; +import com.github.owlcs.ontapi.jena.model.OntStatement; import com.github.owlcs.ontapi.jena.utils.Iter; import org.apache.jena.graph.Node; import org.apache.jena.graph.NodeFactory; import org.apache.jena.rdf.model.RDFNode; import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.Statement; import org.apache.jena.util.iterator.ExtendedIterator; import org.semanticweb.owlapi.model.OWLAxiom; import org.semanticweb.owlapi.model.OWLPrimitive; -import java.util.HashSet; -import java.util.LinkedHashSet; import java.util.Set; import java.util.function.Supplier; @@ -157,68 +157,6 @@ protected ExtendedIterator> listTranslators( return listTranslators().filterKeep(t -> t.testStatement(statement, conf)); } - /** - * Lists all roots for the given statement. - * - * @param model {@link OntModel}, not {@code null} - * @param statement {@link Statement}, not {@code null} - * @return an {@code ExtendedIterator} of {@link Statement}s - */ - protected final ExtendedIterator listRootStatements(OntModel model, OntStatement statement) { - if (statement.getSubject().isURIResource()) { - return Iter.of(statement); - } - return Iter.create(getRootStatements(model, statement)); - } - - /** - * Returns a {@code Set} of root statements. - * Any statement has one or more roots or is a root itself. - * A statement with the predicate {@code rdf:type} is always a root. - * - * @param model {@link OntModel}, not {@code null} - * @param statement {@link Statement}, not {@code null} - * @return a {@code Set} of {@link Statement}s - */ - protected Set getRootStatements(OntModel model, OntStatement statement) { - Set roots = new HashSet<>(); - Set seen = new HashSet<>(); - Set candidates = new LinkedHashSet<>(); - candidates.add(statement); - while (!candidates.isEmpty()) { - OntStatement st = candidates.iterator().next(); - candidates.remove(st); - OntObject subject = st.getSubject(); - if (subject.isURIResource() || subject.canAs(OntIndividual.Anonymous.class)) { - roots.add(st); - continue; - } - int count = candidates.size(); - listByObject(model, subject).filterKeep(s -> s.getSubject().isURIResource() || seen.add(s.getSubject())) - .forEachRemaining(candidates::add); - if (count != candidates.size()) { - continue; - } - // no new candidates is found -> then it is root - listProperties(model, subject).forEachRemaining(roots::add); - } - return roots; - } - - /** - * Lists all related statements for the given root, which should be an anonymous resource. - * It is to find axiom-statement candidates, - * for example a statement with the predicate {@code owl:distinctMembers} is not an axiom-statement, but - * a statement with the same subject and {@code rdf:type} = {@code owl:AllDifferent} is an axiom-statement candidate. - * - * @param model {@link OntModel} - * @param root {@link OntObject} - an anonymous resource - * @return an {@link ExtendedIterator} of {@link OntStatement}s - */ - protected ExtendedIterator listProperties(OntModel model, OntObject root) { - return listBySubject(model, root); - } - /** * Lists all related statements for the given root, taking in account {@code owl:Axiom}s and {@code owl:Annotations}. * diff --git a/src/main/java/com/github/owlcs/ontapi/internal/searchers/ClassSearcher.java b/src/main/java/com/github/owlcs/ontapi/internal/searchers/ClassSearcher.java new file mode 100644 index 000000000..0e9e5953f --- /dev/null +++ b/src/main/java/com/github/owlcs/ontapi/internal/searchers/ClassSearcher.java @@ -0,0 +1,113 @@ +/* + * This file is part of the ONT API. + * The contents of this file are subject to the LGPL License, Version 3.0. + * Copyright (c) 2020, The University of Manchester, owl.cs group. + * + * 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. + * 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/. + * + * 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. + * 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 + * 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. + */ + +package com.github.owlcs.ontapi.internal.searchers; + +import com.github.owlcs.ontapi.OntApiException; +import com.github.owlcs.ontapi.internal.*; +import com.github.owlcs.ontapi.jena.impl.PersonalityModel; +import com.github.owlcs.ontapi.jena.impl.conf.OntPersonality; +import com.github.owlcs.ontapi.jena.model.OntModel; +import com.github.owlcs.ontapi.jena.model.OntStatement; +import com.github.owlcs.ontapi.jena.utils.Iter; +import com.github.owlcs.ontapi.jena.vocabulary.OWL; +import com.github.owlcs.ontapi.jena.vocabulary.RDF; +import org.apache.jena.rdf.model.RDFNode; +import org.apache.jena.rdf.model.Resource; +import org.apache.jena.rdf.model.Statement; +import org.apache.jena.util.iterator.ExtendedIterator; +import org.semanticweb.owlapi.model.OWLAxiom; +import org.semanticweb.owlapi.model.OWLClass; + +import java.util.HashSet; +import java.util.Set; + +/** + * Created by @ssz on 19.04.2020. + */ +public class ClassSearcher extends WithRootSearcher implements ObjectSearcher { + + private static final Set> TRANSLATORS = selectTranslators(OWLComponentType.CLASS); + + protected static OntPersonality.Builtins getBuiltins(OntModel m) { + return PersonalityModel.asPersonalityModel(m).getOntPersonality().getBuiltins(); + } + + @Override + public ExtendedIterator> listObjects(OntModel model, + InternalObjectFactory factory, + InternalConfig config) { + return listClasses(model, config).mapWith(x -> find(x, model, factory)); + } + + protected ONTObject find(String uri, OntModel model, InternalObjectFactory factory) { + if (factory instanceof ModelObjectFactory) { + return ((ModelObjectFactory) factory).getClass(uri); + } + return factory.getClass(OntApiException.mustNotBeNull(model.getOntClass(uri))); + } + + protected ExtendedIterator listClasses(OntModel m, InternalConfig conf) { + Set builtins = new HashSet<>(); + getBuiltins(m).getClasses() + .forEach(x -> { + if (containAxiom(listStatements(m, m.getResource(x.getURI())), conf)) { + builtins.add(x.getURI()); + } + }); + if (!builtins.contains(OWL.Thing.getURI())) { + if (containAxiom(Iter.flatMap(listImplicitStatements(m), s -> listRootStatements(m, s)), conf)) { + builtins.add(OWL.Thing.getURI()); + } + } + ExtendedIterator explicit = listByPredicateAndObject(m, RDF.type, OWL.Class) + .mapWith(x -> x.getSubject().getURI()) + .filterKeep(x -> x != null && !builtins.contains(x)); + ExtendedIterator res = Iter.concat(explicit, Iter.create(builtins)); + if (!m.independent()) { + ExtendedIterator shared = listClassesFromImports(m) + .filterKeep(x -> containAxiom(listStatements(m, m.getResource(x)), conf)); + res = Iter.concat(res, shared); + } + return res; + } + + protected ExtendedIterator listStatements(OntModel m, Resource clazz) { + return Iter.concat(listBySubject(m, clazz), Iter.flatMap(listByObject(m, clazz), s -> listRootStatements(m, s))); + } + + protected ExtendedIterator listImplicitStatements(OntModel m) { + return Iter.flatMap(Iter.of(OWL.cardinality, OWL.maxCardinality, OWL.minCardinality), p -> listByPredicate(m, p)) + .filterKeep(this::isCardinalityRestriction); + } + + protected ExtendedIterator listClassesFromImports(OntModel m) { + ExtendedIterator imports = Iter.create(m.imports().iterator()); + return Iter.distinct(Iter.flatMap(imports, i -> i.listStatements(null, RDF.type, OWL.Class)) + .mapWith(Statement::getSubject).filterKeep(RDFNode::isURIResource).mapWith(Resource::getURI)); + } + + protected boolean isCardinalityRestriction(OntStatement s) { + return ByClass.OBJECT_CARDINALITY_TYPES.stream().anyMatch(t -> s.getSubject().canAs(t)); + } + + protected boolean containAxiom(ExtendedIterator top, InternalConfig conf) { + return Iter.anyMatch(top, s -> Iter.findFirst(listTranslators(s, conf)).isPresent()); + } + + protected ExtendedIterator> listTranslators(OntStatement statement, + InternalConfig conf) { + return Iter.create(TRANSLATORS).filterKeep(t -> t.testStatement(statement, conf)); + } +} diff --git a/src/main/java/com/github/owlcs/ontapi/internal/searchers/WithRootSearcher.java b/src/main/java/com/github/owlcs/ontapi/internal/searchers/WithRootSearcher.java new file mode 100644 index 000000000..733346110 --- /dev/null +++ b/src/main/java/com/github/owlcs/ontapi/internal/searchers/WithRootSearcher.java @@ -0,0 +1,98 @@ +/* + * This file is part of the ONT API. + * The contents of this file are subject to the LGPL License, Version 3.0. + * Copyright (c) 2020, The University of Manchester, owl.cs group. + * + * 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. + * 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/. + * + * 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. + * 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 + * 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. + */ + +package com.github.owlcs.ontapi.internal.searchers; + +import com.github.owlcs.ontapi.internal.BaseSearcher; +import com.github.owlcs.ontapi.jena.model.OntIndividual; +import com.github.owlcs.ontapi.jena.model.OntModel; +import com.github.owlcs.ontapi.jena.model.OntObject; +import com.github.owlcs.ontapi.jena.model.OntStatement; +import com.github.owlcs.ontapi.jena.utils.Iter; +import org.apache.jena.rdf.model.Resource; +import org.apache.jena.rdf.model.Statement; +import org.apache.jena.util.iterator.ExtendedIterator; + +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.Set; + +/** + * Created by @ssz on 19.04.2020. + */ +public class WithRootSearcher extends BaseSearcher { + + /** + * Lists all roots for the given statement. + * + * @param model {@link OntModel}, not {@code null} + * @param statement {@link Statement}, not {@code null} + * @return an {@code ExtendedIterator} of {@link Statement}s + */ + protected final ExtendedIterator listRootStatements(OntModel model, OntStatement statement) { + if (statement.getSubject().isURIResource()) { + return Iter.of(statement); + } + return Iter.create(getRootStatements(model, statement)); + } + + /** + * Returns a {@code Set} of root statements. + * Any statement has one or more roots or is a root itself. + * A statement with the predicate {@code rdf:type} is always a root. + * + * @param model {@link OntModel}, not {@code null} + * @param statement {@link Statement}, not {@code null} + * @return a {@code Set} of {@link Statement}s + */ + protected Set getRootStatements(OntModel model, OntStatement statement) { + Set roots = new HashSet<>(); + Set seen = new HashSet<>(); + Set candidates = new LinkedHashSet<>(); + candidates.add(statement); + while (!candidates.isEmpty()) { + OntStatement st = candidates.iterator().next(); + candidates.remove(st); + OntObject subject = st.getSubject(); + if (subject.isURIResource() || subject.canAs(OntIndividual.Anonymous.class)) { + roots.add(st); + continue; + } + int count = candidates.size(); + listByObject(model, subject).filterKeep(s -> s.getSubject().isURIResource() || seen.add(s.getSubject())) + .forEachRemaining(candidates::add); + if (count != candidates.size()) { + continue; + } + // no new candidates is found -> then it is root + listProperties(model, subject).forEachRemaining(roots::add); + } + return roots; + } + + /** + * Lists all related statements for the given root, which should be an anonymous resource. + * It is to find axiom-statement candidates, + * for example a statement with the predicate {@code owl:distinctMembers} is not an axiom-statement, but + * a statement with the same subject and {@code rdf:type} = {@code owl:AllDifferent} is an axiom-statement candidate. + * + * @param model {@link OntModel} + * @param root {@link OntObject} - an anonymous resource + * @return an {@link ExtendedIterator} of {@link OntStatement}s + */ + protected ExtendedIterator listProperties(OntModel model, OntObject root) { + return listBySubject(model, root); + } + +} diff --git a/src/test/java/com/github/owlcs/ontapi/tests/model/ReferencingAxiomsTest.java b/src/test/java/com/github/owlcs/ontapi/tests/model/ReferencingAxiomsTest.java index 36bdbf43e..0911157d4 100644 --- a/src/test/java/com/github/owlcs/ontapi/tests/model/ReferencingAxiomsTest.java +++ b/src/test/java/com/github/owlcs/ontapi/tests/model/ReferencingAxiomsTest.java @@ -300,7 +300,7 @@ void testCounts(OWLOntology ont, Function Tester.this.referencingAxiomsCount(ont, x1)).sum(); + long res = primitives.stream().mapToLong(x -> referencingAxiomsCount(ont, x)).sum(); Assert.assertEquals(count, res); } }