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

org.semanticweb.HermiT.debugger.Printing 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 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.debugger;

import java.io.PrintWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.semanticweb.HermiT.model.AtLeastConcept;
import org.semanticweb.HermiT.model.AtomicConcept;
import org.semanticweb.HermiT.model.AtomicNegationConcept;
import org.semanticweb.HermiT.model.AtomicNegationDataRange;
import org.semanticweb.HermiT.model.AtomicRole;
import org.semanticweb.HermiT.model.Concept;
import org.semanticweb.HermiT.model.Constant;
import org.semanticweb.HermiT.model.ConstantEnumeration;
import org.semanticweb.HermiT.model.DataRange;
import org.semanticweb.HermiT.model.DatatypeRestriction;
import org.semanticweb.HermiT.model.DescriptionGraph;
import org.semanticweb.HermiT.model.ExistentialConcept;
import org.semanticweb.HermiT.model.ExistsDescriptionGraph;
import org.semanticweb.HermiT.model.InternalDatatype;
import org.semanticweb.HermiT.model.InverseRole;
import org.semanticweb.HermiT.model.Role;
import org.semanticweb.HermiT.tableau.ExtensionTable;
import org.semanticweb.HermiT.tableau.Node;
/**Printing.*/
public class Printing {
    /**
     * @param writer writer
     * @param number number
     * @param size size
     */
    public static void printPadded(PrintWriter writer,int number,int size) {
        printPadded(writer,String.valueOf(number),size);
    }
    /**
     * @param writer writer
     * @param string string
     * @param size size
     */
    public static void printPadded(PrintWriter writer,String string,int size) {
        for (int i=size-string.length();i>=0;--i)
            writer.print(' ');
        writer.print(string);
    }
    /**
     * @param collection collection
     * @param writer writer
     * @param  type
     */
    public static  void printCollection(Collection collection,PrintWriter writer) {
        for (T object : collection) {
            writer.print("    ");
            writer.print(object.toString());
            writer.println();
        }
    }
    /**
     * @param in1NotIn2 label 1
     * @param in2NotIn1 label 2
     * @param writer writer
     * @param  type
     * @param c1 first collection
     * @param c2 second collection
     */
    public static  void diffCollections(String in1NotIn2,String in2NotIn1,PrintWriter writer,Collection c1,Collection c2) {
        boolean window1Message=false;
        for (Object object : c1) {
            if (!c2.contains(object)) {
                if (!window1Message) {
                    writer.println("<<<  "+in1NotIn2+":");
                    window1Message=true;
                }
                writer.print("    ");
                writer.print(object.toString());
                writer.println();
            }
        }
        if (window1Message)
            writer.println("--------------------------------------------");
        boolean window2Message=false;
        for (Object object : c2) {
            if (!c1.contains(object)) {
                if (!window2Message) {
                    writer.println(">>>  "+in2NotIn1+":");
                    window2Message=true;
                }
                writer.print("    ");
                writer.print(object.toString());
                writer.println();
            }
        }
        if (window1Message)
            writer.println("--------------------------------------------");
    }
    /**
     * @param debugger debugger
     * @param node node
     * @param writer writer
     */
    public static void printNodeData(Debugger debugger,Node node,PrintWriter writer) {
        writer.print("Node ID:    ");
        writer.println(node.getNodeID());
        writer.print("Node Type:  ");
        writer.println(node.getNodeType());
        writer.print("Parent ID:  ");
        writer.println(node.getParent()==null ? "(root node)" : Integer.valueOf(node.getParent().getNodeID()));
        writer.print("Depth:      ");
        writer.println(node.getTreeDepth());
        writer.print("Status:     ");
        if (node.isActive())
            writer.println("active");
        else if (node.isMerged()) {
            Node mergeTarget=node.getMergedInto();
            while (mergeTarget!=null) {
                writer.print(" --> ");
                writer.print(mergeTarget.getNodeID());
                mergeTarget=mergeTarget.getMergedInto();
            }
            writer.println();
        }
        else
            writer.println("pruned");
        writer.print("Blocked:    ");
        writer.println(formatBlockingStatus(node));
        writer.print("Created as: ");
        Debugger.NodeCreationInfo nodeCreationInfo=debugger.getNodeCreationInfo(node);
        ExistentialConcept startExistential=nodeCreationInfo.m_createdByExistential;
        if (!(startExistential instanceof AtLeastConcept))
            writer.println("(root)");
        else
            writer.println(((AtLeastConcept)startExistential).getToConcept().toString(debugger.getPrefixes()));
        printConceptLabel(debugger,node,writer);
        printEdges(debugger,node,writer);
    }
    protected static String formatBlockingStatus(Node node) {
        if (!node.isBlocked())
            return "no";
        else if (node.isDirectlyBlocked())
            return "directly by "+(node.getBlocker()==Node.SIGNATURE_CACHE_BLOCKER ? "signature in cache" : Integer.valueOf(node.getBlocker().getNodeID()));
        else
            return "indirectly by "+(node.getBlocker()==Node.SIGNATURE_CACHE_BLOCKER ? "signature in cache" : Integer.valueOf(node.getBlocker().getNodeID()));
    }
    protected static void printConceptLabel(Debugger debugger,Node node,PrintWriter writer) {
        TreeSet atomicConceptsCore=new TreeSet<>(Printing::conceptCompare);
        TreeSet atomicConceptsNoncore=new TreeSet<>(Printing::conceptCompare);
        TreeSet existentialConcepts=new TreeSet<>(Printing::conceptCompare);
        TreeSet negativeConcepts=new TreeSet<>(Printing::conceptCompare);
        TreeSet dataRanges=new TreeSet<>(Printing::dataRangeCompare);
        ExtensionTable.Retrieval retrieval=debugger.getTableau().getExtensionManager().getBinaryExtensionTable().createRetrieval(new boolean[] { false,true },ExtensionTable.View.TOTAL);
        retrieval.getBindingsBuffer()[1]=node;
        retrieval.open();
        while (!retrieval.afterLast()) {
            Object potentialConcept=retrieval.getTupleBuffer()[0];
            if (potentialConcept instanceof AtomicNegationConcept)
                negativeConcepts.add((AtomicNegationConcept)potentialConcept);
            else if (potentialConcept instanceof AtomicConcept) {
                if (retrieval.isCore())
                    atomicConceptsCore.add((AtomicConcept)potentialConcept);
                else
                    atomicConceptsNoncore.add((AtomicConcept)potentialConcept);
            }
            else if (potentialConcept instanceof ExistentialConcept)
                existentialConcepts.add((ExistentialConcept)potentialConcept);
            else if (potentialConcept instanceof DataRange)
                dataRanges.add((DataRange)potentialConcept);
            else if (potentialConcept instanceof DescriptionGraph) {
                // ignore description graphs here
            }
            else
                throw new IllegalStateException("Found something in the label that is not a known type!");
            retrieval.next();
        }
        Set noConcepts=Collections.emptySet();
        if (!atomicConceptsCore.isEmpty()) {
            writer.print("-- Positive concept label (core part) -------");
            printConcepts(debugger,atomicConceptsCore,noConcepts,writer,3);
        }
        if (!atomicConceptsNoncore.isEmpty() || !existentialConcepts.isEmpty()) {
            writer.print("-- Positive concept label (noncore part) ----");
            printConcepts(debugger,atomicConceptsNoncore,noConcepts,writer,3);
            printConcepts(debugger,existentialConcepts,node.getUnprocessedExistentials(),writer,1);
        }
        if (!negativeConcepts.isEmpty()) {
            writer.print("-- Negative concept label -------------------");
            printConcepts(debugger,negativeConcepts,noConcepts,writer,3);
        }
        if (!dataRanges.isEmpty()) {
            writer.print("-- Data ranges label ------------------------");
            printDataRanges(debugger,dataRanges,writer,1);
        }
    }
    protected static void printEdges(Debugger debugger,Node node,PrintWriter writer) {
        Map> outgoingEdges=new TreeMap<>(Comparator.comparing(Node::getNodeID));
        ExtensionTable.Retrieval retrieval=debugger.getTableau().getExtensionManager().getTernaryExtensionTable().createRetrieval(new boolean[] { false,true,false },ExtensionTable.View.TOTAL);
        retrieval.getBindingsBuffer()[1]=node;
        retrieval.open();
        while (!retrieval.afterLast()) {
            Object atomicRoleObject=retrieval.getTupleBuffer()[0];
            if (atomicRoleObject instanceof AtomicRole) {
                AtomicRole atomicRole=(AtomicRole)retrieval.getTupleBuffer()[0];
                Node toNode=(Node)retrieval.getTupleBuffer()[2];
                Set set=outgoingEdges.get(toNode);
                if (set==null) {
                    set=new TreeSet<>(Printing::roleCompare);
                    outgoingEdges.put(toNode,set);
                }
                set.add(atomicRole);
            }
            retrieval.next();
        }
        if (!outgoingEdges.isEmpty()) {
            writer.println("-- Outgoing edges --------------------------------");
            printEdgeMap(debugger,outgoingEdges,writer);
        }
        Map> incomingEdges=new TreeMap<>(Comparator.comparing(Node::getNodeID));
        retrieval=debugger.getTableau().getExtensionManager().getTernaryExtensionTable().createRetrieval(new boolean[] { false,false,true },ExtensionTable.View.TOTAL);
        retrieval.getBindingsBuffer()[2]=node;
        retrieval.open();
        while (!retrieval.afterLast()) {
            Object atomicRoleObject=retrieval.getTupleBuffer()[0];
            if (atomicRoleObject instanceof AtomicRole) {
                AtomicRole atomicRole=(AtomicRole)retrieval.getTupleBuffer()[0];
                Node fromNode=(Node)retrieval.getTupleBuffer()[1];
                Set set=incomingEdges.get(fromNode);
                if (set==null) {
                    set=new TreeSet<>(Printing::roleCompare);
                    incomingEdges.put(fromNode,set);
                }
                set.add(atomicRole);
            }
            retrieval.next();
        }
        if (!incomingEdges.isEmpty()) {
            writer.println("-- Incoming edges --------------------------------");
            printEdgeMap(debugger,incomingEdges,writer);
        }
    }
    protected static void printConcepts(Debugger debugger,Set set,Collection markedElements,PrintWriter writer,int numberInRow) {
        int number=0;
        for (Concept concept : set) {
            if (number!=0)
                writer.print(", ");
            if ((number%numberInRow)==0) {
                writer.println();
                writer.print("    ");
            }
            writer.print(concept.toString(debugger.getPrefixes()));
            if (markedElements.contains(concept))
                writer.print(" (*)");
            number++;
        }
        writer.println();
    }
    protected static void printDataRanges(Debugger debugger,Set set,PrintWriter writer,int numberInRow) {
        int number=0;
        for (DataRange range : set) {
            if (number!=0)
                writer.print(", ");
            if ((number%numberInRow)==0) {
                writer.println();
                writer.print("    ");
            }
            writer.print(range.toString(debugger.getPrefixes()));
            number++;
        }
        writer.println();
    }
    protected static void printEdgeMap(Debugger debugger,Map> map,PrintWriter writer) {
        for (Map.Entry> entry : map.entrySet()) {
            writer.print("    ");
            writer.print(entry.getKey().getNodeID());
            writer.print(" -->");
            int number=0;
            for (AtomicRole atomicRole : entry.getValue()) {
                if (number!=0)
                    writer.print(", ");
                if ((number%3)==0) {
                    writer.println();
                    writer.print("        ");
                }
                writer.print(atomicRole.toString(debugger.getPrefixes()));
                number++;
            }
            writer.println();
        }
    }

    /**@param c1 c1
     * @param c2 c2
     * @return comparison*/
    public static int conceptCompare(Concept c1,Concept c2) {
        ConceptType type1=getConceptType(c1);
        ConceptType type2=getConceptType(c2);
        if (type1!=type2)
            return type1.getTypeIndex()-type2.getTypeIndex();
        switch (type1) {
        case AtomicConcept:
            return ((AtomicConcept)c1).getIRI().compareTo(((AtomicConcept)c2).getIRI());
        case AtLeastConcept:
                AtLeastConcept l1=(AtLeastConcept)c1;
                AtLeastConcept l2=(AtLeastConcept)c2;
                int comparison=roleCompare(l1.getOnRole(),l2.getOnRole());
                if (comparison!=0)
                    return comparison;
                return conceptCompare(l1.getToConcept(),l2.getToConcept());
        case ExistsDescriptionGraph:
                ExistsDescriptionGraph g1=(ExistsDescriptionGraph)c1;
                ExistsDescriptionGraph g2=(ExistsDescriptionGraph)c2;
                return g1.getDescriptionGraph().getName().compareTo(g2.getDescriptionGraph().getName());
        case AtomicNegationConcept:
            return ((AtomicNegationConcept)c1).getNegatedAtomicConcept().getIRI().compareTo(((AtomicNegationConcept)c2).getNegatedAtomicConcept().getIRI());
        default:
            throw new IllegalArgumentException();
        }
    }
    protected enum ConceptType {
        AtomicConcept(0),
        AtLeastConcept(1),
        ExistsDescriptionGraph(2), 
        AtomicNegationConcept(3);

        private final int m_typeIndex;
        ConceptType(int typeIndex) {
            m_typeIndex=typeIndex;
        }
        final int getTypeIndex() {
            return m_typeIndex;
        }
    }
    protected static ConceptType getConceptType(Concept c) {
        if (c instanceof AtomicConcept)
            return ConceptType.AtomicConcept;
        else if (c instanceof AtLeastConcept)
            return ConceptType.AtLeastConcept;
        else if (c instanceof ExistsDescriptionGraph)
            return ConceptType.ExistsDescriptionGraph;
        else if (c instanceof AtomicNegationConcept)
            return ConceptType.AtomicNegationConcept;
        else
            throw new IllegalArgumentException();
    }
    /**@param c1 c1
     * @param c2 c2
     * @return comparison*/
    public static int dataRangeCompare(DataRange c1,DataRange c2) {
        DataRangeType type1=getDataRangeType(c1);
        DataRangeType type2=getDataRangeType(c2);
        if (type1!=type2)
            return type1.getTypeIndex()-type2.getTypeIndex();
        switch (type1) {
        case DatatypeRestriction:
            return compareDatatypeRestrictions((DatatypeRestriction)c1,(DatatypeRestriction)c2);
        case ConstantEnumeration:
            return compareConstantEnumerations((ConstantEnumeration)c1,(ConstantEnumeration)c2);
        case AtomicNegationDataRange:
                AtomicNegationDataRange ndr1=(AtomicNegationDataRange)c1;
                AtomicNegationDataRange ndr2=(AtomicNegationDataRange)c2;
                return dataRangeCompare(ndr1.getNegatedDataRange(),ndr2.getNegatedDataRange());
        case InternalDatatype:
            return ((InternalDatatype)c1).getIRI().compareTo(((InternalDatatype)c2).getIRI());
        default:
            throw new IllegalArgumentException();
        }
    }
    protected enum DataRangeType {
        DatatypeRestriction(0),
        ConstantEnumeration(1),
        AtomicNegationDataRange(2), 
        InternalDatatype(3);

        private final int m_typeIndex;
        DataRangeType(int typeIndex) {
            m_typeIndex=typeIndex;
        }
        final int getTypeIndex() {
            return m_typeIndex;
        }
    }
    protected static DataRangeType getDataRangeType(DataRange dr) {
        if (dr instanceof DatatypeRestriction)
            return DataRangeType.DatatypeRestriction;
        else if (dr instanceof InternalDatatype)
            return DataRangeType.InternalDatatype; 
        else if (dr instanceof ConstantEnumeration)
            return DataRangeType.ConstantEnumeration;
        else if (dr instanceof AtomicNegationDataRange)
            return DataRangeType.AtomicNegationDataRange;
        else
            throw new IllegalArgumentException();
    }
    protected static int compareDatatypeRestrictions(DatatypeRestriction dr1,DatatypeRestriction dr2) {
        int comparison=dr1.getDatatypeURI().compareTo(dr2.getDatatypeURI());
        if (comparison!=0)
            return comparison;
        comparison=dr1.getNumberOfFacetRestrictions()-dr2.getNumberOfFacetRestrictions();
        if (comparison!=0)
            return comparison;
        for (int index=0;index




© 2015 - 2024 Weber Informatics LLC | Privacy Policy