org.semanticweb.HermiT.hierarchy.HierarchySearch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.semanticweb.hermit Show documentation
Show all versions of org.semanticweb.hermit Show documentation
HermiT is reasoner for ontologies written using the Web
Ontology Language (OWL). Given an OWL file, HermiT can determine whether or
not the ontology is consistent, identify subsumption relationships between
classes, and much more.
This is the maven build of HermiT and is designed for people who wish to use
HermiT from within the OWL API. It is now versioned in the main HermiT
version repository, although not officially supported by the HermiT
developers.
The version number of this package is a composite of the HermiT version and
an value representing releases of this packaged version. So, 1.3.7.1 is the
first release of the mavenized version of HermiT based on the 1.3.7 release
of HermiT.
This package includes the Jautomata library
(http://jautomata.sourceforge.net/), and builds with it directly. This
library appears to be no longer under active development, and so a "fork"
seems appropriate. No development is intended or anticipated on this code
base.
/* Copyright 2009 by the Oxford University Computing Laboratory
This file is part of HermiT.
HermiT is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
HermiT 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with HermiT. If not, see .
*/
package org.semanticweb.HermiT.hierarchy;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
public class HierarchySearch {
public static HierarchyNode findPosition(Relation hierarchyRelation,E element,HierarchyNode topNode,HierarchyNode bottomNode) {
Set> parentNodes=findParents(hierarchyRelation,element,topNode);
Set> childNodes=findChildren(hierarchyRelation,element,bottomNode,parentNodes);
if (parentNodes.equals(childNodes)) {
assert parentNodes.size()==1 && childNodes.size()==1;
return parentNodes.iterator().next();
}
else {
Set equivalentElements=new HashSet();
equivalentElements.add(element);
return new HierarchyNode(element,equivalentElements,parentNodes,childNodes);
}
}
protected static Set> findParents(final Relation hierarchyRelation,final E element,HierarchyNode topNode) {
return search(
new SearchPredicate>() {
public Set> getSuccessorElements(HierarchyNode u) {
return u.m_childNodes;
}
public Set> getPredecessorElements(HierarchyNode u) {
return u.m_parentNodes;
}
public boolean trueOf(HierarchyNode u) {
return hierarchyRelation.doesSubsume(u.getRepresentative(),element);
}
},Collections.singleton(topNode),null);
}
protected static Set> findChildren(final Relation hierarchyRelation,final E element,HierarchyNode bottomNode,Set> parentNodes) {
if (parentNodes.size()==1 && hierarchyRelation.doesSubsume(element,parentNodes.iterator().next().getRepresentative()))
return parentNodes;
else {
// We now determine the set of nodes that are descendants of each node in parentNodes
Iterator> parentNodesIterator=parentNodes.iterator();
Set> marked=new HashSet>(parentNodesIterator.next().getDescendantNodes());
while (parentNodesIterator.hasNext()) {
Set> freshlyMarked=new HashSet>();
Set> visited=new HashSet>();
Queue> toProcess=new LinkedList>();
toProcess.add(parentNodesIterator.next());
while (!toProcess.isEmpty()) {
HierarchyNode currentNode=toProcess.remove();
for (HierarchyNode childNode : currentNode.m_childNodes)
if (marked.contains(childNode))
freshlyMarked.add(childNode);
else if (visited.add(childNode))
toProcess.add(childNode);
}
toProcess.addAll(freshlyMarked);
while (!toProcess.isEmpty()) {
HierarchyNode currentNode=toProcess.remove();
for (HierarchyNode childNode : currentNode.m_childNodes)
if (freshlyMarked.add(childNode))
toProcess.add(childNode);
}
marked=freshlyMarked;
}
// Determine the subset of marked that is directly above the bottomNode and that is below the current element.
Set> aboveBottomNodes=new HashSet>();
for (HierarchyNode node : marked)
if (node.m_childNodes.contains(bottomNode) && hierarchyRelation.doesSubsume(element,node.getRepresentative()))
aboveBottomNodes.add(node);
// If this set is empty, then we omit the bottom search phase.
if (aboveBottomNodes.isEmpty()) {
Set> childNodes=new HashSet>();
childNodes.add(bottomNode);
return childNodes;
}
else {
return search(
new SearchPredicate>() {
public Set> getSuccessorElements(HierarchyNode u) {
return u.m_parentNodes;
}
public Set> getPredecessorElements(HierarchyNode u) {
return u.m_childNodes;
}
public boolean trueOf(HierarchyNode u) {
return hierarchyRelation.doesSubsume(element,u.getRepresentative());
}
},aboveBottomNodes,marked);
}
}
}
public static Set search(SearchPredicate searchPredicate,Collection startSearch,Set possibilities) {
SearchCache cache=new SearchCache(searchPredicate,possibilities);
Set result=new HashSet();
Set visited=new HashSet(startSearch);
Queue toProcess=new LinkedList(startSearch);
while (!toProcess.isEmpty()) {
U current=toProcess.remove();
boolean foundSubordinateElement=false;
Set subordinateElements=searchPredicate.getSuccessorElements(current);
for (U subordinateElement : subordinateElements)
if (cache.trueOf(subordinateElement)) {
foundSubordinateElement=true;
if (visited.add(subordinateElement))
toProcess.add(subordinateElement);
}
if (!foundSubordinateElement)
result.add(current);
}
return result;
}
public static interface Relation {
boolean doesSubsume(U parent,U child);
}
public static interface SearchPredicate {
Set getSuccessorElements(U u);
Set getPredecessorElements(U u);
boolean trueOf(U u);
}
protected static final class SearchCache {
protected final SearchPredicate m_searchPredicate;
protected final Set m_possibilities;
protected final Set m_positives;
protected final Set m_negatives;
public SearchCache(SearchPredicate f,Set possibilities) {
m_searchPredicate=f;
m_possibilities=possibilities;
m_positives=new HashSet();
m_negatives=new HashSet();
}
public boolean trueOf(U element) {
if (m_positives.contains(element))
return true;
else if (m_negatives.contains(element) || (m_possibilities!=null && !m_possibilities.contains(element)))
return false;
else {
for (U superordinateElement : m_searchPredicate.getPredecessorElements(element)) {
if (!trueOf(superordinateElement)) {
m_negatives.add(element);
return false;
}
}
if (m_searchPredicate.trueOf(element)) {
m_positives.add(element);
return true;
}
else {
m_negatives.add(element);
return false;
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy