org.semanticweb.HermiT.tableau.HyperresolutionManager 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 a value representing the OWLAPI release it is compatible with. Note that the group id for the upstream HermiT is com.hermit-reasoner, while this fork is released under net.sourceforge.owlapi.
This fork exists to allow HermiT users to use newer OWLAPI versions than the ones supported by the original HermiT codebase.
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.
The newest version!
/* Copyright 2008, 2009, 2010 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.tableau;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.semanticweb.HermiT.model.Atom;
import org.semanticweb.HermiT.model.AtomicConcept;
import org.semanticweb.HermiT.model.AtomicRole;
import org.semanticweb.HermiT.model.DLClause;
import org.semanticweb.HermiT.model.DLPredicate;
import org.semanticweb.HermiT.model.NodeIDLessEqualThan;
import org.semanticweb.HermiT.model.NodeIDsAscendingOrEqual;
import org.semanticweb.HermiT.model.Term;
import org.semanticweb.HermiT.model.Variable;
/**
* Applies the rules during the expansion of a tableau.
*/
public final class HyperresolutionManager implements Serializable {
private static final long serialVersionUID=-4880817508962130189L;
protected final ExtensionManager m_extensionManager;
protected final ExtensionTable.Retrieval[] m_deltaOldRetrievals;
protected final ExtensionTable.Retrieval m_binaryTableRetrieval;
protected final Map m_tupleConsumersByDeltaPredicate;
protected final Map m_atomicRoleTupleConsumersUnguarded;
protected final Map> m_atomicRoleTupleConsumersByGuardConcept1;
protected final Map> m_atomicRoleTupleConsumersByGuardConcept2;
protected final Object[][] m_buffersToClear;
protected final UnionDependencySet[] m_unionDependencySetsToClear;
protected final Object[] m_valuesBuffer;
protected final int m_maxNumberOfVariables;
/**
* @param tableau tableau
* @param dlClauses dlClauses
*/
public HyperresolutionManager(Tableau tableau,Collection dlClauses) {
InterruptFlag interruptFlag=tableau.m_interruptFlag;
m_extensionManager=tableau.m_extensionManager;
m_tupleConsumersByDeltaPredicate=new HashMap<>();
m_atomicRoleTupleConsumersUnguarded=new HashMap<>();
m_atomicRoleTupleConsumersByGuardConcept1=new HashMap<>();
m_atomicRoleTupleConsumersByGuardConcept2=new HashMap<>();
// Index DL clauses by body
Map> dlClausesByBody=new HashMap<>();
for (DLClause dlClause : dlClauses) {
DLClauseBodyKey key=new DLClauseBodyKey(dlClause);
List dlClausesForKey=dlClausesByBody.get(key);
if (dlClausesForKey==null) {
dlClausesForKey=new ArrayList<>();
dlClausesByBody.put(key,dlClausesForKey);
}
dlClausesForKey.add(dlClause);
interruptFlag.checkInterrupt();
}
// Compile the DL clauses
Map retrievalsByArity=new HashMap<>();
DLClauseEvaluator.BufferSupply bufferSupply=new DLClauseEvaluator.BufferSupply();
Map noTermsToNodes=Collections.emptyMap();
DLClauseEvaluator.ValuesBufferManager valuesBufferManager=new DLClauseEvaluator.ValuesBufferManager(dlClauses,noTermsToNodes);
DLClauseEvaluator.GroundDisjunctionHeaderManager groundDisjunctionHeaderManager=new DLClauseEvaluator.GroundDisjunctionHeaderManager();
Map unionDependencySetsBySize=new HashMap<>();
ArrayList guardingAtomicConceptAtoms1=new ArrayList<>();
ArrayList guardingAtomicConceptAtoms2=new ArrayList<>();
for (Map.Entry> entry : dlClausesByBody.entrySet()) {
DLClause bodyDLClause=entry.getKey().m_dlClause;
BodyAtomsSwapper bodyAtomsSwapper=new BodyAtomsSwapper(bodyDLClause);
for (int bodyAtomIndex=0;bodyAtomIndex compiledDLClauseInfos=m_atomicRoleTupleConsumersByGuardConcept1.get(deltaAtomicRole);
if (compiledDLClauseInfos==null) {
compiledDLClauseInfos=new HashMap<>();
m_atomicRoleTupleConsumersByGuardConcept1.put(deltaAtomicRole,compiledDLClauseInfos);
}
for (Atom guardingAtom : guardingAtomicConceptAtoms1) {
AtomicConcept atomicConcept=(AtomicConcept)guardingAtom.getDLPredicate();
CompiledDLClauseInfo optimizedTupleConsumer=new CompiledDLClauseInfo(evaluator,compiledDLClauseInfos.get(atomicConcept));
compiledDLClauseInfos.put(atomicConcept,optimizedTupleConsumer);
}
}
if (!guardingAtomicConceptAtoms2.isEmpty()) {
Map compiledDLClauseInfos=m_atomicRoleTupleConsumersByGuardConcept2.get(deltaAtomicRole);
if (compiledDLClauseInfos==null) {
compiledDLClauseInfos=new HashMap<>();
m_atomicRoleTupleConsumersByGuardConcept2.put(deltaAtomicRole,compiledDLClauseInfos);
}
for (Atom guardingAtom : guardingAtomicConceptAtoms2) {
AtomicConcept atomicConcept=(AtomicConcept)guardingAtom.getDLPredicate();
CompiledDLClauseInfo optimizedTupleConsumer=new CompiledDLClauseInfo(evaluator,compiledDLClauseInfos.get(atomicConcept));
compiledDLClauseInfos.put(atomicConcept,optimizedTupleConsumer);
}
}
if (guardingAtomicConceptAtoms1.isEmpty() && guardingAtomicConceptAtoms2.isEmpty()) {
CompiledDLClauseInfo unguardedTupleConsumer=new CompiledDLClauseInfo(evaluator,m_atomicRoleTupleConsumersUnguarded.get(deltaAtomicRole));
m_atomicRoleTupleConsumersUnguarded.put(deltaAtomicRole,unguardedTupleConsumer);
}
}
bufferSupply.reuseBuffers();
interruptFlag.checkInterrupt();
}
}
m_deltaOldRetrievals=new ExtensionTable.Retrieval[retrievalsByArity.size()];
retrievalsByArity.values().toArray(m_deltaOldRetrievals);
m_binaryTableRetrieval=m_extensionManager.getExtensionTable(2).createRetrieval(new boolean[] { false,true },ExtensionTable.View.EXTENSION_THIS);
m_buffersToClear=bufferSupply.getAllBuffers();
m_unionDependencySetsToClear=new UnionDependencySet[unionDependencySetsBySize.size()];
unionDependencySetsBySize.values().toArray(m_unionDependencySetsToClear);
m_valuesBuffer=valuesBufferManager.m_valuesBuffer;
m_maxNumberOfVariables=valuesBufferManager.m_maxNumberOfVariables;
}
protected static void getAtomicRoleClauseGuards(DLClause swappedDLClause,List guardingAtomicConceptAtoms1,List guardingAtomicConceptAtoms2) {
guardingAtomicConceptAtoms1.clear();
guardingAtomicConceptAtoms2.clear();
Atom deltaOldAtom=swappedDLClause.getBodyAtom(0);
Variable X=deltaOldAtom.getArgumentVariable(0);
Variable Y=deltaOldAtom.getArgumentVariable(1);
for (int bodyIndex=1;bodyIndex=0;--retrievalIndex)
m_deltaOldRetrievals[retrievalIndex].clear();
m_binaryTableRetrieval.clear();
for (int bufferIndex=m_buffersToClear.length-1;bufferIndex>=0;--bufferIndex) {
Object[] buffer=m_buffersToClear[bufferIndex];
for (int index=buffer.length-1;index>=0;--index)
buffer[index]=null;
}
for (int unionDependencySetIndex=m_unionDependencySetsToClear.length-1;unionDependencySetIndex>=0;--unionDependencySetIndex) {
DependencySet[] dependencySets=m_unionDependencySetsToClear[unionDependencySetIndex].m_dependencySets;
for (int dependencySetIndex=dependencySets.length-1;dependencySetIndex>=0;--dependencySetIndex)
dependencySets[dependencySetIndex]=null;
}
for (int variableIndex=0;variableIndex((Node)deltaOldTupleBuffer[1]).getNumberOfPositiveAtomicConcepts()+((Node)deltaOldTupleBuffer[2]).getNumberOfPositiveAtomicConcepts()+(unguardedCompiledDLClauseInfo==null ? 0 : unguardedCompiledDLClauseInfo.m_indexInList)) {
applyUnoptimized=false;
while (unguardedCompiledDLClauseInfo!=null && !m_extensionManager.containsClash()) {
unguardedCompiledDLClauseInfo.m_evaluator.evaluate();
unguardedCompiledDLClauseInfo=unguardedCompiledDLClauseInfo.m_next;
}
if (!m_extensionManager.containsClash()) {
Map compiledDLClauseInfos=m_atomicRoleTupleConsumersByGuardConcept1.get(deltaOldPredicate);
if (compiledDLClauseInfos!=null) {
m_binaryTableRetrieval.getBindingsBuffer()[1]=deltaOldTupleBuffer[1];
m_binaryTableRetrieval.open();
Object[] binaryTableTupleBuffer=m_binaryTableRetrieval.getTupleBuffer();
while (!m_binaryTableRetrieval.afterLast() && !m_extensionManager.containsClash()) {
Object atomicConceptObject=binaryTableTupleBuffer[0];
if (atomicConceptObject instanceof AtomicConcept) {
CompiledDLClauseInfo optimizedCompiledDLClauseInfo=compiledDLClauseInfos.get(atomicConceptObject);
while (optimizedCompiledDLClauseInfo!=null && !m_extensionManager.containsClash()) {
optimizedCompiledDLClauseInfo.m_evaluator.evaluate();
optimizedCompiledDLClauseInfo=optimizedCompiledDLClauseInfo.m_next;
}
}
m_binaryTableRetrieval.next();
}
}
}
if (!m_extensionManager.containsClash()) {
Map compiledDLClauseInfos=m_atomicRoleTupleConsumersByGuardConcept2.get(deltaOldPredicate);
if (compiledDLClauseInfos!=null) {
m_binaryTableRetrieval.getBindingsBuffer()[1]=deltaOldTupleBuffer[2];
m_binaryTableRetrieval.open();
Object[] binaryTableTupleBuffer=m_binaryTableRetrieval.getTupleBuffer();
while (!m_binaryTableRetrieval.afterLast() && !m_extensionManager.containsClash()) {
Object atomicConceptObject=binaryTableTupleBuffer[0];
if (atomicConceptObject instanceof AtomicConcept) {
CompiledDLClauseInfo optimizedCompiledDLClauseInfo=compiledDLClauseInfos.get(atomicConceptObject);
while (optimizedCompiledDLClauseInfo!=null && !m_extensionManager.containsClash()) {
optimizedCompiledDLClauseInfo.m_evaluator.evaluate();
optimizedCompiledDLClauseInfo=optimizedCompiledDLClauseInfo.m_next;
}
}
m_binaryTableRetrieval.next();
}
}
}
}
}
if (applyUnoptimized) {
while (unoptimizedCompiledDLClauseInfo!=null && !m_extensionManager.containsClash()) {
unoptimizedCompiledDLClauseInfo.m_evaluator.evaluate();
unoptimizedCompiledDLClauseInfo=unoptimizedCompiledDLClauseInfo.m_next;
}
}
deltaOldRetrieval.next();
}
}
}
protected static final class CompiledDLClauseInfo {
protected final DLClauseEvaluator m_evaluator;
protected final CompiledDLClauseInfo m_next;
protected final int m_indexInList;
public CompiledDLClauseInfo(DLClauseEvaluator evaluator,CompiledDLClauseInfo next) {
m_evaluator=evaluator;
m_next=next;
if (m_next==null)
m_indexInList=1;
else
m_indexInList=m_next.m_indexInList+1;
}
}
/**BodyAtomsSwapper.*/
public static final class BodyAtomsSwapper {
protected final DLClause m_dlClause;
protected final List m_nodeIDComparisonAtoms;
protected final boolean[] m_usedAtoms;
protected final List m_reorderedAtoms;
protected final Set m_boundVariables;
/**
* @param dlClause dl clause
*/
public BodyAtomsSwapper(DLClause dlClause) {
m_dlClause=dlClause;
m_nodeIDComparisonAtoms=new ArrayList<>(m_dlClause.getBodyLength());
m_usedAtoms=new boolean[m_dlClause.getBodyLength()];
m_reorderedAtoms=new ArrayList<>(m_dlClause.getBodyLength());
m_boundVariables=Collections.newSetFromMap(new IdentityHashMap<>());
}
/**
* @param bodyIndex bodyIndex
* @return swapped dl clause
*/
public DLClause getSwappedDLClause(int bodyIndex) {
m_nodeIDComparisonAtoms.clear();
for (int index=m_usedAtoms.length-1;index>=0;--index) {
m_usedAtoms[index]=false;
Atom atom=m_dlClause.getBodyAtom(index);
if (NodeIDLessEqualThan.INSTANCE.equals(atom.getDLPredicate()))
m_nodeIDComparisonAtoms.add(atom);
}
m_reorderedAtoms.clear();
m_boundVariables.clear();
Atom atom=m_dlClause.getBodyAtom(bodyIndex);
atom.getVariables(m_boundVariables);
m_reorderedAtoms.add(atom);
m_usedAtoms[bodyIndex]=true;
while (m_reorderedAtoms.size()!=m_usedAtoms.length) {
Atom bestAtom=null;
int bestAtomIndex=-1;
int bestAtomGoodness=-1000;
for (int index=m_usedAtoms.length-1;index>=0;--index)
if (!m_usedAtoms[index]) {
atom=m_dlClause.getBodyAtom(index);
int atomGoodness=getAtomGoodness(atom);
if (atomGoodness>bestAtomGoodness) {
bestAtom=atom;
bestAtomGoodness=atomGoodness;
bestAtomIndex=index;
}
}
assert bestAtom!=null;
m_reorderedAtoms.add(bestAtom);
m_usedAtoms[bestAtomIndex]=true;
bestAtom.getVariables(m_boundVariables);
m_nodeIDComparisonAtoms.remove(bestAtom);
}
Atom[] bodyAtoms=new Atom[m_reorderedAtoms.size()];
m_reorderedAtoms.toArray(bodyAtoms);
return m_dlClause.getChangedDLClause(null,bodyAtoms);
}
protected int getAtomGoodness(Atom atom) {
if (NodeIDLessEqualThan.INSTANCE.equals(atom.getDLPredicate())) {
if (m_boundVariables.contains(atom.getArgumentVariable(0)) && m_boundVariables.contains(atom.getArgumentVariable(1)))
return 1000;
else
return -2000;
}
else if (atom.getDLPredicate() instanceof NodeIDsAscendingOrEqual) {
int numberOfUnboundVariables=0;
for (int argumentIndex=atom.getArity()-1;argumentIndex>=0;--argumentIndex) {
Term argument=atom.getArgument(argumentIndex);
if (argument instanceof Variable) {
if (!m_boundVariables.contains(argument))
numberOfUnboundVariables++;
}
}
if (numberOfUnboundVariables>0)
return -5000;
else
return 5000;
}
else {
int numberOfBoundVariables=0;
int numberOfUnboundVariables=0;
for (int argumentIndex=atom.getArity()-1;argumentIndex>=0;--argumentIndex) {
Term argument=atom.getArgument(argumentIndex);
if (argument instanceof Variable) {
if (m_boundVariables.contains(argument))
numberOfBoundVariables++;
else
numberOfUnboundVariables++;
}
}
int goodness=numberOfBoundVariables*100-numberOfUnboundVariables*10;
if (atom.getDLPredicate().getArity()==2 && numberOfUnboundVariables==1 && !m_nodeIDComparisonAtoms.isEmpty()) {
Variable unboundVariable=atom.getArgumentVariable(0);
if (m_boundVariables.contains(unboundVariable))
unboundVariable=atom.getArgumentVariable(1);
// At this point, unboundVariable must be really unbound because
// we have already established that numberOfUnboundVariables==1.
for (int compareAtomIndex=m_nodeIDComparisonAtoms.size()-1;compareAtomIndex>=0;--compareAtomIndex) {
Atom compareAtom=m_nodeIDComparisonAtoms.get(compareAtomIndex);
Variable argument0=compareAtom.getArgumentVariable(0);
Variable argument1=compareAtom.getArgumentVariable(1);
if ((m_boundVariables.contains(argument0) || unboundVariable.equals(argument0)) && (m_boundVariables.contains(argument1) || unboundVariable.equals(argument1))) {
goodness+=5;
break;
}
}
}
return goodness;
}
}
}
protected static final class DLClauseBodyKey {
protected final DLClause m_dlClause;
protected final int m_hashCode;
public DLClauseBodyKey(DLClause dlClause) {
m_dlClause=dlClause;
int hashCode=0;
for (int atomIndex=0;atomIndex