Skip to content

Commit

Permalink
ont-api: add ClassSearcher (graph optimization) - the facility to ret…
Browse files Browse the repository at this point in the history
…rieve OWLClass signature (issue #15)
  • Loading branch information
sszuev committed Apr 28, 2020
1 parent ca982bb commit 123f0f0
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 84 deletions.
62 changes: 48 additions & 14 deletions src/main/java/com/github/owlcs/ontapi/internal/InternalModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,14 @@ public class InternalModel extends OntGraphModelImpl
protected final ByObject<OWLAxiom, IRI> byIRI = new ByIRI();
// Other searchers
protected final ByObject<OWLDeclarationAxiom, OWLEntity> declarationsByEntity = new DeclarationByEntity();
protected final ByObject<OWLAnnotationAssertionAxiom, OWLAnnotationSubject> annotationAssertionBySubject = new AnnotationAssertionBySubject();
protected final ByObject<OWLSubClassOfAxiom, OWLClass> subClassOfBySubject = new SubClassOfBySubject();
protected final ByObject<OWLEquivalentClassesAxiom, OWLClass> equivalentClassesByOperand = new EquivalentClassesByClass();
protected final ByObject<OWLAnnotationAssertionAxiom, OWLAnnotationSubject> annotationAssertionsBySubject
= new AnnotationAssertionBySubject();
protected final ByObject<OWLEquivalentClassesAxiom, OWLClass> equivalentClassesByOperand
= new EquivalentClassesByClass();

// To search OWLObjects
protected final ObjectSearcher<OWLClass> classSearcher = new ClassSearcher();

/**
* Constructs a model instance.
Expand Down Expand Up @@ -475,7 +480,7 @@ public Stream<OWLEntity> 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));
Expand Down Expand Up @@ -631,7 +636,7 @@ public Stream<OWLDeclarationAxiom> 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:
Expand All @@ -650,10 +655,10 @@ public Stream<OWLDeclarationAxiom> listOWLDeclarationAxioms(OWLEntity e) {
public Stream<OWLAnnotationAssertionAxiom> 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)));
}

Expand All @@ -666,7 +671,7 @@ public Stream<OWLAnnotationAssertionAxiom> listOWLAnnotationAssertionAxioms(OWLA
*/
public Stream<OWLSubClassOfAxiom> 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)
Expand All @@ -683,7 +688,7 @@ public Stream<OWLSubClassOfAxiom> listOWLSubClassOfAxioms(OWLClass sub) {
*/
public Stream<OWLEquivalentClassesAxiom> 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)
Expand Down Expand Up @@ -746,6 +751,7 @@ public Stream<OWLAxiom> 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()) {
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -1373,12 +1381,8 @@ protected <O extends OWLObject> ObjectMap<O> getComponentCache(OWLComponentType
* @see OWLComponentType
*/
protected ObjectMap<OWLObject> createComponentObjectMap(OWLComponentType key) {
// todo: replace parsing the content cache with the direct graph reading
InternalObjectFactory df = getObjectFactory();
OntModel m = getSearchModel();
Supplier<Iterator<ONTObject<OWLObject>>> loader = () -> selectContentObjects(key)
.flatMap(x -> key.select(x, m, df)).iterator();
InternalConfig conf = getConfig();
Supplier<Iterator<ONTObject<OWLObject>>> 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
Expand All @@ -1389,6 +1393,36 @@ protected ObjectMap<OWLObject> 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<ONTObject<OWLObject>> 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}.
*
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/github/owlcs/ontapi/internal/ObjectSearcher.java
Original file line number Diff line number Diff line change
@@ -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 <O> - {@link OWLObject} subtype
* @see AxiomTranslator
*/
public interface ObjectSearcher<O extends OWLObject> {

/**
* 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<ONTObject<O>> listObjects(OntModel model,
InternalObjectFactory factory,
InternalConfig config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,5 +22,5 @@
* Created by @ssz on 18.04.2020.
*/
@SuppressWarnings("SameParameterValue")
abstract class BaseByObject<A extends OWLAxiom, O extends OWLObject> extends BaseSearcher implements ByObject<A, O> {
abstract class BaseByObject<A extends OWLAxiom, O extends OWLObject> extends WithRootSearcher implements ByObject<A, O> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
public class ByClass extends WithCardinality<OWLClass> {

private static final Set<Class<? extends OntClass.CardinalityRestrictionCE<?, ?>>> OBJECT_CARDINALITY_TYPES =
public static final Set<Class<? extends OntClass.CardinalityRestrictionCE<?, ?>>> OBJECT_CARDINALITY_TYPES =
Stream.of(OntClass.ObjectMaxCardinality.class, OntClass.ObjectMinCardinality.class, OntClass.ObjectCardinality.class)
.collect(Iter.toUnmodifiableSet());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -157,68 +157,6 @@ protected ExtendedIterator<? extends AxiomTranslator<OWLAxiom>> 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<OntStatement> 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<OntStatement> getRootStatements(OntModel model, OntStatement statement) {
Set<OntStatement> roots = new HashSet<>();
Set<Resource> seen = new HashSet<>();
Set<OntStatement> 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<OntStatement> 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}.
*
Expand Down
Loading

0 comments on commit 123f0f0

Please # to comment.