org.semanticweb.HermiT.model.DLOntology 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.model;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.semanticweb.HermiT.Prefixes;
/**
* Represents a DL ontology as a set of rules.
*/
public class DLOntology implements Serializable {
private static final long serialVersionUID=3189937959595369812L;
protected static final String CRLF="\n";
protected final String m_ontologyIRI;
protected final Collection m_dlClauses;
protected final Set m_positiveFacts;
protected final Set m_negativeFacts;
protected final boolean m_hasInverseRoles;
protected final boolean m_hasAtMostRestrictions;
protected final boolean m_hasNominals;
protected final boolean m_hasDatatypes;
protected final boolean m_isHorn;
protected final Set m_allAtomicConcepts;
protected final int m_numberOfExternalConcepts;
protected final Set m_allAtomicObjectRoles;
protected final Set m_allComplexObjectRoles;
protected final Set m_allAtomicDataRoles;
protected final Set m_allUnknownDatatypeRestrictions;
protected final Set m_definedDatatypeIRIs;
protected final Set m_allIndividuals;
protected final Set m_allDescriptionGraphs;
protected final Map>> m_dataPropertyAssertions;
/**
* @param ontologyIRI ontologyIRI
* @param dlClauses dlClauses
* @param positiveFacts positiveFacts
* @param negativeFacts negativeFacts
* @param atomicConcepts atomicConcepts
* @param atomicObjectRoles atomicObjectRoles
* @param allComplexObjectRoles allComplexObjectRoles
* @param atomicDataRoles atomicDataRoles
* @param allUnknownDatatypeRestrictions allUnknownDatatypeRestrictions
* @param definedDatatypeIRIs definedDatatypeIRIs
* @param individuals individuals
* @param hasInverseRoles hasInverseRoles
* @param hasAtMostRestrictions hasAtMostRestrictions
* @param hasNominals hasNominals
* @param hasDatatypes hasDatatypes
*/
public DLOntology(String ontologyIRI,Collection dlClauses,Set positiveFacts,Set negativeFacts, Set atomicConcepts,
Set atomicObjectRoles,Set allComplexObjectRoles,Set atomicDataRoles,
Set allUnknownDatatypeRestrictions,Set definedDatatypeIRIs,Set individuals,
boolean hasInverseRoles,boolean hasAtMostRestrictions,boolean hasNominals,boolean hasDatatypes) {
m_ontologyIRI=ontologyIRI;
m_dlClauses=dlClauses;
m_positiveFacts=positiveFacts;
m_negativeFacts=negativeFacts;
m_hasInverseRoles=hasInverseRoles;
m_hasAtMostRestrictions=hasAtMostRestrictions;
m_hasNominals=hasNominals;
m_hasDatatypes=hasDatatypes;
if (atomicConcepts==null)
m_allAtomicConcepts=new TreeSet<>(Comparator.comparing(AtomicConcept::getIRI));
else {
m_allAtomicConcepts=Collections.newSetFromMap(new IdentityHashMap<>());
m_allAtomicConcepts.addAll(atomicConcepts);
}
int numberOfExternalConcepts=0;
for (AtomicConcept c : m_allAtomicConcepts)
if (!Prefixes.isInternalIRI(c.getIRI()))
numberOfExternalConcepts++;
m_numberOfExternalConcepts=numberOfExternalConcepts;
if (atomicObjectRoles==null)
m_allAtomicObjectRoles=new TreeSet<>(Comparator.comparing(AtomicRole::getIRI));
else
m_allAtomicObjectRoles=atomicObjectRoles;
if (allComplexObjectRoles==null)
m_allComplexObjectRoles=new HashSet<>();
else
m_allComplexObjectRoles=allComplexObjectRoles;
if (atomicDataRoles==null)
m_allAtomicDataRoles=new TreeSet<>(Comparator.comparing(AtomicRole::getIRI));
else
m_allAtomicDataRoles=atomicDataRoles;
if (allUnknownDatatypeRestrictions==null)
m_allUnknownDatatypeRestrictions=new HashSet<>();
else
m_allUnknownDatatypeRestrictions=allUnknownDatatypeRestrictions;
if (definedDatatypeIRIs==null)
m_definedDatatypeIRIs=new HashSet<>();
else
m_definedDatatypeIRIs=definedDatatypeIRIs;
if (individuals==null)
m_allIndividuals=new TreeSet<>(Comparator.comparing(Individual::getIRI));
else
m_allIndividuals=individuals;
m_allDescriptionGraphs=new HashSet<>();
boolean isHorn=true;
for (DLClause dlClause : m_dlClauses) {
if (dlClause.getHeadLength()>1)
isHorn=false;
for (int bodyIndex=dlClause.getBodyLength()-1;bodyIndex>=0;--bodyIndex) {
DLPredicate dlPredicate=dlClause.getBodyAtom(bodyIndex).getDLPredicate();
addDLPredicate(dlPredicate);
}
for (int headIndex=dlClause.getHeadLength()-1;headIndex>=0;--headIndex) {
DLPredicate dlPredicate=dlClause.getHeadAtom(headIndex).getDLPredicate();
addDLPredicate(dlPredicate);
}
}
m_isHorn=isHorn;
m_dataPropertyAssertions=new HashMap<>();
for (Atom atom : m_positiveFacts) {
addDLPredicate(atom.getDLPredicate());
for (int i=0;i> individualsToConstants;
if (m_dataPropertyAssertions.containsKey(atomicRole))
individualsToConstants=m_dataPropertyAssertions.get(atomicRole);
else {
individualsToConstants=new HashMap<>();
m_dataPropertyAssertions.put(atomicRole,individualsToConstants);
}
Set constants;
if (individualsToConstants.containsKey(sourceIndividual))
constants=individualsToConstants.get(sourceIndividual);
else {
constants=new HashSet<>();
individualsToConstants.put(sourceIndividual,constants);
}
constants.add((Constant)possibleConstant);
}
}
}
for (Atom atom : m_negativeFacts) {
addDLPredicate(atom.getDLPredicate());
for (int i=0;i getAllAtomicConcepts() {
return m_allAtomicConcepts;
}
/**
* @param concept concept
* @return true if contains atomic concept
*/
public boolean containsAtomicConcept(AtomicConcept concept) {
return m_allAtomicConcepts.contains(concept);
}
/**
* @return umber of external concepts
*/
public int getNumberOfExternalConcepts() {
return m_numberOfExternalConcepts;
}
/**
* @return all atomic object roles
*/
public Set getAllAtomicObjectRoles() {
return m_allAtomicObjectRoles;
}
/**
* @param role role
* @return true if object role contained
*/
public boolean containsObjectRole(AtomicRole role) {
return m_allAtomicObjectRoles.contains(role);
}
/**
* @return all complex object roles
*/
public Set getAllComplexObjectRoles() {
return m_allComplexObjectRoles;
}
/**
* @param role role
* @return true if complex
*/
public boolean isComplexObjectRole(Role role) {
return m_allComplexObjectRoles.contains(role);
}
/**
* @return all atomic data roles
*/
public Set getAllAtomicDataRoles() {
return m_allAtomicDataRoles;
}
/**
* @param role role
* @return true if contains data role
*/
public boolean containsDataRole(AtomicRole role) {
return m_allAtomicDataRoles.contains(role);
}
/**
* @return all unknown data restrictions
*/
public Set getAllUnknownDatatypeRestrictions() {
return m_allUnknownDatatypeRestrictions;
}
/**
* @return all individuals
*/
public Set getAllIndividuals() {
return m_allIndividuals;
}
/**
* @param individual individual
* @return true if contains individual
*/
public boolean containsIndividual(Individual individual) {
return m_allIndividuals.contains(individual);
}
/**
* @return description graphs
*/
public Set getAllDescriptionGraphs() {
return m_allDescriptionGraphs;
}
/**
* @return dl clauses
*/
public Collection getDLClauses() {
return m_dlClauses;
}
/**
* @return positive facts
*/
public Set getPositiveFacts() {
return m_positiveFacts;
}
/**
* @return data assertions
*/
public Map>> getDataPropertyAssertions() {
return m_dataPropertyAssertions;
}
/**
* @return negative facts
*/
public Set getNegativeFacts() {
return m_negativeFacts;
}
/**
* @return true if inverse roles
*/
public boolean hasInverseRoles() {
return m_hasInverseRoles;
}
/**
* @return true if at most restrictions
*/
public boolean hasAtMostRestrictions() {
return m_hasAtMostRestrictions;
}
/**
* @return true if has nominals
*/
public boolean hasNominals() {
return m_hasNominals;
}
/**
* @return true if has datatypes
*/
public boolean hasDatatypes() {
return m_hasDatatypes;
}
/**
* @return true if has unknown datatypes restrictions
*/
public boolean hasUnknownDatatypeRestrictions() {
return !m_allUnknownDatatypeRestrictions.isEmpty();
}
/**
* @return true if Horn ontology
*/
public boolean isHorn() {
return m_isHorn;
}
/**
* @return datatypes
*/
public Set getDefinedDatatypeIRIs() {
return m_definedDatatypeIRIs;
}
protected Set getBodyOnlyAtomicConcepts() {
Set bodyOnlyAtomicConcepts=new HashSet<>(m_allAtomicConcepts);
for (DLClause dlClause : m_dlClauses)
for (int headIndex=0;headIndex computeGraphAtomicRoles() {
Set graphAtomicRoles=new HashSet<>();
for (DescriptionGraph descriptionGraph : m_allDescriptionGraphs)
for (int edgeIndex=0;edgeIndex roles) {
for (int atomIndex=0;atomIndex roles) {
boolean change=false;
for (int atomIndex=0;atomIndex entry : prefixes.getPrefixIRIsByPrefixName().entrySet()) {
StringBuilder.append(" ").append(entry.getKey()).append(" = <").append(entry.getValue()).append('>').append(CRLF);
}
StringBuilder.append("]").append(CRLF).append("Deterministic DL-clauses: [").append(CRLF);
int numDeterministicClauses=0;
for (DLClause dlClause : m_dlClauses)
if (dlClause.getHeadLength()<=1) {
numDeterministicClauses++;
StringBuilder.append(" ").append(dlClause.toString(prefixes)).append(CRLF);
}
StringBuilder.append("]").append(CRLF).append("Disjunctive DL-clauses: [").append(CRLF);
int numNondeterministicClauses=0;
int numDisjunctions=0;
for (DLClause dlClause : m_dlClauses)
if (dlClause.getHeadLength()>1) {
numNondeterministicClauses++;
numDisjunctions+=dlClause.getHeadLength();
StringBuilder.append(" ").append(dlClause.toString(prefixes)).append(CRLF);
}
StringBuilder.append("]").append(CRLF).append("ABox: [").append(CRLF);
for (Atom atom : m_positiveFacts) {
StringBuilder.append(" ").append(atom.toString(prefixes)).append(CRLF);
}
for (Atom atom : m_negativeFacts) {
StringBuilder.append(" !").append(atom.toString(prefixes)).append(CRLF);
}
StringBuilder.append("]").append(CRLF).append("Statistics: [").append(CRLF)
.append(" Number of deterministic clauses: " + numDeterministicClauses).append(CRLF)
.append(" Number of nondeterministic clauses: " + numNondeterministicClauses).append(CRLF)
.append(" Number of disjunctions: " + numDisjunctions).append(CRLF)
.append(" Number of positive facts: " + m_positiveFacts.size()).append(CRLF)
.append(" Number of negative facts: " + m_negativeFacts.size()).append(CRLF).append("]");
return StringBuilder.toString();
}
/**
* @return statistics
*/
public String getStatistics() {
return getStatistics(null,null,null);
}
protected String getStatistics(Integer _numDeterministicClauses, Integer _numNondeterministicClauses, Integer _numDisjunctions) {
int numDeterministicClauses;int numNondeterministicClauses; int numDisjunctions;
if (_numDeterministicClauses==null || _numNondeterministicClauses==null || _numDisjunctions==null) {
numDeterministicClauses=0;
numNondeterministicClauses=0;
numDisjunctions=0;
for (DLClause dlClause : m_dlClauses) {
if (dlClause.getHeadLength()<=1)
numDeterministicClauses++;
else {
numNondeterministicClauses++;
numDisjunctions+=dlClause.getHeadLength();
}
}
}else {
numDeterministicClauses=_numDeterministicClauses.intValue();
numNondeterministicClauses=_numNondeterministicClauses.intValue();
numDisjunctions=_numDisjunctions.intValue();
}
StringBuilder StringBuilder=new StringBuilder("DL clauses statistics: [").append(CRLF)
.append(" Number of deterministic clauses: " ).append( numDeterministicClauses).append(CRLF)
.append(" Number of nondeterministic clauses: " ).append( numNondeterministicClauses).append(CRLF)
.append(" Overall number of disjunctions: " ).append( numDisjunctions).append(CRLF)
.append(" Number of positive facts: " ).append( m_positiveFacts.size()).append(CRLF)
.append(" Number of negative facts: " ).append( m_negativeFacts.size()).append(CRLF)
.append(" Inverses: " ).append( this.hasInverseRoles()).append(CRLF)
.append(" At-Mosts: " ).append( this.hasAtMostRestrictions()).append(CRLF)
.append(" Datatypes: " ).append( this.hasDatatypes()).append(CRLF)
.append(" Nominals: " ).append( this.hasNominals()).append(CRLF)
.append(" Number of atomic concepts: " ).append( m_allAtomicConcepts.size()).append(CRLF)
.append(" Number of object properties: ").append( m_allAtomicObjectRoles.size()).append(CRLF)
.append(" Number of data properties: ").append( m_allAtomicDataRoles.size()).append(CRLF)
.append(" Number of individuals: " ).append( m_allIndividuals.size()).append(CRLF).append("]");
return StringBuilder.toString();
}
@Override
public String toString() {
return toString(Prefixes.STANDARD_PREFIXES);
}
/**
* @param outputStream outputStream
* @throws IOException if reading fails
*/
public void save(OutputStream outputStream) throws IOException {
ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(this);
objectOutputStream.flush();
}
/**
* @param inputStream inputStream
* @return ontology
* @throws IOException if class not found
*/
public static DLOntology load(InputStream inputStream) throws IOException {
try {
ObjectInputStream objectInputStream=new ObjectInputStream(inputStream);
return (DLOntology)objectInputStream.readObject();
}
catch (ClassNotFoundException e) {
throw new IOException(e);
}
}
}