org.semanticweb.HermiT.hierarchy.HierarchyPrinterFSS 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.hierarchy;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.semanticweb.HermiT.Prefixes;
import org.semanticweb.HermiT.model.AtomicConcept;
import org.semanticweb.HermiT.model.AtomicRole;
import org.semanticweb.HermiT.model.InverseRole;
import org.semanticweb.HermiT.model.Role;
/**HierarchyPrinterFSS.*/
public class HierarchyPrinterFSS {
protected final PrintWriter m_out;
protected final String m_defaultPrefixIRI;
protected final Set m_prefixIRIs;
protected Prefixes m_prefixes;
/**
* @param out out
* @param defaultPrefixIRI defaultPrefixIRI
*/
public HierarchyPrinterFSS(PrintWriter out,String defaultPrefixIRI) {
m_out=out;
m_defaultPrefixIRI=defaultPrefixIRI;
m_prefixIRIs=new TreeSet<>();
m_prefixIRIs.add(defaultPrefixIRI);
m_prefixIRIs.add(Prefixes.s_semanticWebPrefixes.get("owl:"));
}
/**
* @param atomicConcepts atomicConcepts
*/
public void loadAtomicConceptPrefixIRIs(Collection atomicConcepts) {
for (AtomicConcept atomicConcept : atomicConcepts) {
String uri=atomicConcept.getIRI();
int hashIndex=uri.indexOf('#');
if (hashIndex!=-1) {
String prefixIRI=uri.substring(0,hashIndex+1);
String localName=uri.substring(hashIndex+1);
if (Prefixes.isValidLocalName(localName))
m_prefixIRIs.add(prefixIRI);
}
}
}
/**
* @param atomicRoles atomicRoles
*/
public void loadAtomicRolePrefixIRIs(Collection atomicRoles) {
for (AtomicRole atomicRole : atomicRoles) {
String uri=atomicRole.getIRI();
int hashIndex=uri.indexOf('#');
if (hashIndex!=-1) {
String prefixIRI=uri.substring(0,hashIndex+1);
String localName=uri.substring(hashIndex+1);
if (Prefixes.isValidLocalName(localName))
m_prefixIRIs.add(prefixIRI);
}
}
}
/**
* Start printing.
*/
public void startPrinting() {
String owlPrefixIRI=Prefixes.s_semanticWebPrefixes.get("owl:");
m_prefixes=new Prefixes();
m_prefixes.declareDefaultPrefix(m_defaultPrefixIRI);
m_prefixes.declarePrefix("owl:",owlPrefixIRI);
int index=1;
for (String prefixIRI : m_prefixIRIs)
if (!m_defaultPrefixIRI.equals(prefixIRI) && !owlPrefixIRI.equals(prefixIRI)) {
String prefixName="a"+(index++)+":";
m_prefixes.declarePrefix(prefixName,prefixIRI);
}
for (Map.Entry entry : m_prefixes.getPrefixIRIsByPrefixName().entrySet())
if (!"owl:".equals(entry.getKey()))
m_out.println("Prefix("+entry.getKey()+"=<"+entry.getValue()+">)");
m_out.println();
m_out.println("Ontology(<"+m_prefixes.getPrefixIRIsByPrefixName().get(":")+">");
m_out.println();
}
/**
* @param atomicConceptHierarchy atomicConceptHierarchy
*/
public void printAtomicConceptHierarchy(Hierarchy atomicConceptHierarchy) {
Hierarchy sortedAtomicConceptHierarchy=atomicConceptHierarchy.transform(new IdentityTransformer(),AtomicConceptComparator.INSTANCE);
AtomicConceptPrinter atomicConceptPrinter=new AtomicConceptPrinter(sortedAtomicConceptHierarchy.getBottomNode());
sortedAtomicConceptHierarchy.traverseDepthFirst(atomicConceptPrinter);
atomicConceptPrinter.printNode(0,sortedAtomicConceptHierarchy.getBottomNode(),null,true);
}
/**
* @param roleHierarchy roleHierarchy
* @param objectProperties objectProperties
*/
public void printRoleHierarchy(Hierarchy extends Role> roleHierarchy,boolean objectProperties) {
Hierarchy sortedRoleHierarchy=roleHierarchy.transform(new IdentityTransformer(),RoleComparator.INSTANCE);
RolePrinter rolePrinter=new RolePrinter(sortedRoleHierarchy,objectProperties);
sortedRoleHierarchy.traverseDepthFirst(rolePrinter);
rolePrinter.printNode(0,sortedRoleHierarchy.getBottomNode(),null,true);
}
/**
* End printing.
*/
public void endPrinting() {
m_out.println();
m_out.println(")");
m_out.flush();
}
protected class AtomicConceptPrinter implements Hierarchy.HierarchyNodeVisitor {
protected final HierarchyNode m_bottomNode;
public AtomicConceptPrinter(HierarchyNode bottomNode) {
m_bottomNode=bottomNode;
}
@Override
public boolean redirect(HierarchyNode[] nodes) {
return true;
}
@Override
public void visit(int level,HierarchyNode node,HierarchyNode parentNode,boolean firstVisit) {
if (!node.equals(m_bottomNode))
printNode(level,node,parentNode,firstVisit);
}
public void printNode(int level,HierarchyNode node,HierarchyNode parentNode,boolean firstVisit) {
Set equivalences=node.getEquivalentElements();
boolean printSubClasOf=(parentNode!=null);
boolean printEquivalences=firstVisit && equivalences.size()>1;
boolean printDeclarations=false;
if (firstVisit)
for (AtomicConcept atomicConcept : equivalences)
if (needsDeclaration(atomicConcept)) {
printDeclarations=true;
break;
}
if (printSubClasOf || printEquivalences || printDeclarations) {
for (int i=2*level;i>0;--i)
m_out.print(' ');
boolean afterWS=true;
if (printSubClasOf) {
m_out.print("SubClassOf( ");
print(node.getRepresentative());
m_out.print(' ');
assert parentNode!=null;
print(parentNode.getRepresentative());
m_out.print(" )");
afterWS=false;
}
if (printEquivalences) {
if (!afterWS)
m_out.print(' ');
m_out.print("EquivalentClasses(");
for (AtomicConcept atomicConcept : equivalences) {
m_out.print(' ');
print(atomicConcept);
}
m_out.print(" )");
afterWS=false;
}
if (printDeclarations)
for (AtomicConcept atomicConcept : equivalences)
if (needsDeclaration(atomicConcept)) {
if (!afterWS)
m_out.print(' ');
m_out.print("Declaration( Class( ");
print(atomicConcept);
m_out.print(" ) )");
afterWS=false;
}
m_out.println();
}
}
protected void print(AtomicConcept atomicConcept) {
m_out.print(m_prefixes.abbreviateIRI(atomicConcept.getIRI()));
}
protected boolean needsDeclaration(AtomicConcept atomicConcept) {
return !AtomicConcept.NOTHING.equals(atomicConcept) && !AtomicConcept.THING.equals(atomicConcept);
}
}
protected class RolePrinter implements Hierarchy.HierarchyNodeVisitor {
protected final Hierarchy m_hierarchy;
protected final boolean m_objectProperties;
public RolePrinter(Hierarchy hierarchy,boolean objectProperties) {
m_hierarchy=hierarchy;
m_objectProperties=objectProperties;
}
@Override
public boolean redirect(HierarchyNode[] nodes) {
return true;
}
@Override
public void visit(int level,HierarchyNode node,HierarchyNode parentNode,boolean firstVisit) {
if (!node.equals(m_hierarchy.getBottomNode()))
printNode(level,node,parentNode,firstVisit);
}
public void printNode(int level,HierarchyNode node,HierarchyNode parentNode,boolean firstVisit) {
Set equivalences=node.getEquivalentElements();
boolean printSubPropertyOf=(parentNode!=null);
boolean printEquivalences=firstVisit && equivalences.size()>1;
boolean printDeclarations=false;
if (firstVisit)
for (Role role : equivalences)
if (needsDeclaration(role)) {
printDeclarations=true;
break;
}
if (printSubPropertyOf || printEquivalences || printDeclarations) {
for (int i=2*level;i>0;--i)
m_out.print(' ');
boolean afterWS=true;
if (printSubPropertyOf) {
assert parentNode!=null;
if (m_objectProperties)
m_out.print("SubObjectPropertyOf( ");
else
m_out.print("SubDataPropertyOf( ");
print(node.getRepresentative());
m_out.print(' ');
print(parentNode.getRepresentative());
m_out.print(" )");
afterWS=false;
}
if (printEquivalences) {
if (!afterWS)
m_out.print(' ');
if (m_objectProperties)
m_out.print("EquivalentObjectProperties(");
else
m_out.print("EquivalentDataProperties(");
for (Role role : equivalences) {
m_out.print(' ');
print(role);
}
m_out.print(" )");
afterWS=false;
}
if (printDeclarations)
for (Role role : equivalences)
if (needsDeclaration(role)) {
if (!afterWS)
m_out.print(' ');
m_out.print("Declaration( ");
if (m_objectProperties)
m_out.print("ObjectProperty( ");
else
m_out.print("DataProperty( ");
print(role);
m_out.print(" ) )");
afterWS=false;
}
m_out.println();
}
}
protected void print(Role role) {
if (role instanceof AtomicRole)
m_out.print(m_prefixes.abbreviateIRI(((AtomicRole)role).getIRI()));
else {
m_out.print("ObjectInverseOf( ");
print(((InverseRole)role).getInverseOf());
m_out.print(" )");
}
}
protected void print(AtomicRole atomicRole) {
m_out.print(m_prefixes.abbreviateIRI(atomicRole.getIRI()));
}
protected boolean needsDeclaration(Role role) {
return !AtomicRole.BOTTOM_OBJECT_ROLE.equals(role) && !AtomicRole.TOP_OBJECT_ROLE.equals(role) && !AtomicRole.BOTTOM_DATA_ROLE.equals(role) && !AtomicRole.TOP_DATA_ROLE.equals(role) && role instanceof AtomicRole;
}
}
protected static class RoleComparator implements Comparator {
public static final RoleComparator INSTANCE=new RoleComparator();
@Override
public int compare(Role role1,Role role2) {
int comparison=getRoleClass(role1)-getRoleClass(role2);
if (comparison!=0)
return comparison;
comparison=getRoleDirection(role1)-getRoleDirection(role2);
if (comparison!=0)
return comparison;
return getInnerAtomicRole(role1).getIRI().compareTo(getInnerAtomicRole(role2).getIRI());
}
protected int getRoleClass(Role role) {
if (AtomicRole.BOTTOM_OBJECT_ROLE.equals(role))
return 0;
else if (AtomicRole.TOP_OBJECT_ROLE.equals(role))
return 1;
else if (AtomicRole.BOTTOM_DATA_ROLE.equals(role))
return 2;
else if (AtomicRole.TOP_DATA_ROLE.equals(role))
return 3;
else
return 4;
}
protected AtomicRole getInnerAtomicRole(Role role) {
if (role instanceof AtomicRole)
return (AtomicRole)role;
else
return ((InverseRole)role).getInverseOf();
}
protected int getRoleDirection(Role role) {
return role instanceof AtomicRole ? 0 : 1;
}
}
protected static class AtomicConceptComparator implements Comparator {
public static final AtomicConceptComparator INSTANCE=new AtomicConceptComparator();
@Override
public int compare(AtomicConcept atomicConcept1,AtomicConcept atomicConcept2) {
int comparison=getAtomicConceptClass(atomicConcept1)-getAtomicConceptClass(atomicConcept2);
if (comparison!=0)
return comparison;
return atomicConcept1.getIRI().compareTo(atomicConcept2.getIRI());
}
protected int getAtomicConceptClass(AtomicConcept atomicConcept) {
if (AtomicConcept.NOTHING.equals(atomicConcept))
return 0;
else if (AtomicConcept.THING.equals(atomicConcept))
return 1;
else
return 2;
}
}
protected class IdentityTransformer implements Hierarchy.Transformer {
@Override
public E transform(E object) {
return object;
}
@Override
public E determineRepresentative(E oldRepresentative,Set newEquivalentElements) {
return ((SortedSet)newEquivalentElements).first();
}
}
}