All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.semanticweb.HermiT.hierarchy.DeterministicClassification Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.3.8.4
Show newest version
/* 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.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import org.semanticweb.HermiT.model.Atom;
import org.semanticweb.HermiT.model.AtomicConcept;
import org.semanticweb.HermiT.model.Individual;
import org.semanticweb.HermiT.tableau.ExtensionTable;
import org.semanticweb.HermiT.tableau.Node;
import org.semanticweb.HermiT.tableau.ReasoningTaskDescription;
import org.semanticweb.HermiT.tableau.Tableau;

public class DeterministicClassification {
    protected final Tableau m_tableau;
    protected final ClassificationProgressMonitor m_progressMonitor;
    protected final AtomicConcept m_topElement;
    protected final AtomicConcept m_bottomElement;
    protected final Set m_elements;

    public DeterministicClassification(Tableau tableau,ClassificationProgressMonitor progressMonitor,AtomicConcept topElement,AtomicConcept bottomElement,Set elements) {
        m_tableau=tableau;
        m_progressMonitor=progressMonitor;
        m_topElement=topElement;
        m_bottomElement=bottomElement;
        m_elements=elements;
    }
    public Hierarchy classify() {
        if (!m_tableau.isDeterministic())
            throw new IllegalStateException("Internal error: DeterministicClassificationManager can be used only with a deterministic tableau.");
        Individual freshIndividual=Individual.createAnonymous("fresh-individual");
        if (!m_tableau.isSatisfiable(true,Collections.singleton(Atom.create(m_topElement,freshIndividual)),null,null,null,null,ReasoningTaskDescription.isConceptSatisfiable(m_topElement)))
            return Hierarchy.emptyHierarchy(m_elements,m_topElement,m_bottomElement);
        Map> allSubsumers=new HashMap>();
        for (AtomicConcept element : m_elements) {
            Set subsumers;
            Map nodesForIndividuals=new HashMap();
            nodesForIndividuals.put(freshIndividual,null);
            if (!m_tableau.isSatisfiable(true,Collections.singleton(Atom.create(element,freshIndividual)),null,null,null,nodesForIndividuals,ReasoningTaskDescription.isConceptSatisfiable(element)))
                subsumers=m_elements;
            else {
                subsumers=new HashSet();
                subsumers.add(m_topElement);
                ExtensionTable.Retrieval retrieval=m_tableau.getExtensionManager().getBinaryExtensionTable().createRetrieval(new boolean[] { false,true },ExtensionTable.View.TOTAL);
                retrieval.getBindingsBuffer()[1]=nodesForIndividuals.get(freshIndividual).getCanonicalNode();
                retrieval.open();
                while (!retrieval.afterLast()) {
                    Object subsumer=retrieval.getTupleBuffer()[0];
                    if (subsumer instanceof AtomicConcept && m_elements.contains(subsumer))
                        subsumers.add((AtomicConcept)subsumer);
                    retrieval.next();
                }
            }
            allSubsumers.put(element,new GraphNode(element,subsumers));
            m_progressMonitor.elementClassified(element);
        }
        return buildHierarchy(m_topElement,m_bottomElement,allSubsumers);
    }
    public static  Hierarchy buildHierarchy(T topElement,T bottomElement,Map> graphNodes) {
        HierarchyNode topNode=new HierarchyNode(topElement);
        HierarchyNode bottomNode=new HierarchyNode(bottomElement);
        Hierarchy hierarchy=new Hierarchy(topNode,bottomNode);
        // Compute SCCs (strongly connected components), create hierarchy nodes, and topologically order them
        List> topologicalOrder=new ArrayList>();
        visit(new Stack>(),new DFSIndex(),graphNodes,graphNodes.get(bottomElement),hierarchy,topologicalOrder);
        // Process the nodes in the topological order
        Map,Set>> reachableFrom=new HashMap,Set>>();
        List> allSuccessors=new ArrayList>();
        for (int index=0;index node=topologicalOrder.get(index);
            Set> reachableFromNode=new HashSet>();
            reachableFromNode.add(node);
            reachableFrom.put(node,reachableFromNode);
            allSuccessors.clear();
            for (T element : node.m_equivalentElements) {
                GraphNode graphNode=graphNodes.get(element);
                for (T successor : graphNode.m_successors) {
                    GraphNode successorGraphNode=graphNodes.get(successor);
                    if (successorGraphNode!=null)
                        allSuccessors.add(successorGraphNode);
                }
            }
            Collections.sort(allSuccessors,TopologicalOrderComparator.INSTANCE);
            for (int successorIndex=allSuccessors.size()-1;successorIndex>=0;--successorIndex) {
                GraphNode successorGraphNode=allSuccessors.get(successorIndex);
                HierarchyNode successorNode=hierarchy.m_nodesByElements.get(successorGraphNode.m_element);
                if (!reachableFromNode.contains(successorNode)) {
                    node.m_parentNodes.add(successorNode);
                    successorNode.m_childNodes.add(node);
                    reachableFromNode.add(successorNode);
                    reachableFromNode.addAll(reachableFrom.get(successorNode));
                }
            }
        }
        return hierarchy;
    }
    protected static  void visit(Stack> stack,DFSIndex dfsIndex,Map> graphNodes,GraphNode graphNode,Hierarchy hierarchy,List> topologicalOrder) {
        graphNode.m_dfsIndex=dfsIndex.m_value++;
        graphNode.m_SCChead=graphNode;
        stack.push(graphNode);
        for (T successor : graphNode.m_successors) {
            GraphNode successorGraphNode=graphNodes.get(successor);
            if (successorGraphNode!=null) {
                if (successorGraphNode.notVisited())
                    visit(stack,dfsIndex,graphNodes,successorGraphNode,hierarchy,topologicalOrder);
                if (!successorGraphNode.isAssignedToSCC() && successorGraphNode.m_SCChead.m_dfsIndex equivalentElements=new HashSet();
            GraphNode poppedNode;
            do {
                poppedNode=stack.pop();
                poppedNode.m_topologicalOrderIndex=nextTopologicalOrderIndex;
                equivalentElements.add(poppedNode.m_element);

            } while (poppedNode!=graphNode);
            HierarchyNode hierarchyNode;
            if (equivalentElements.contains(hierarchy.getTopNode().m_representative))
                hierarchyNode=hierarchy.getTopNode();
            else if (equivalentElements.contains(hierarchy.getBottomNode().m_representative))
                hierarchyNode=hierarchy.getBottomNode();
            else
                hierarchyNode=new HierarchyNode(graphNode.m_element);
            for (T element : equivalentElements) {
                hierarchyNode.m_equivalentElements.add(element);
                hierarchy.m_nodesByElements.put(element,hierarchyNode);
            }
            topologicalOrder.add(hierarchyNode);
        }
    }

    public static class GraphNode {
        public final T m_element;
        public final Set m_successors;
        public int m_dfsIndex;
        public GraphNode m_SCChead;
        public int m_topologicalOrderIndex;

        public GraphNode(T element,Set successors) {
            m_element=element;
            m_successors=successors;
            m_dfsIndex=-1;
            m_SCChead=null;
            m_topologicalOrderIndex=-1;
        }
        public boolean notVisited() {
            return m_dfsIndex==-1;
        }
        public boolean isAssignedToSCC() {
            return m_topologicalOrderIndex!=-1;
        }
    }

    protected static class TopologicalOrderComparator implements Comparator> {
        public static final TopologicalOrderComparator INSTANCE=new TopologicalOrderComparator();

        public int compare(GraphNode o1,GraphNode o2) {
            return o1.m_topologicalOrderIndex-o2.m_topologicalOrderIndex;
        }

    }

    protected static class DFSIndex {
        public int m_value;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy