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

org.semanticweb.owl.simpleowlapi.SimpleOWLAPIFactory Maven / Gradle / Ivy

package org.semanticweb.owl.simpleowlapi;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.formats.ManchesterSyntaxDocumentFormat;
import org.semanticweb.owlapi.manchestersyntax.renderer.ManchesterOWLSyntaxOWLObjectRendererImpl;
import org.semanticweb.owlapi.manchestersyntax.renderer.ParserException;
import org.semanticweb.owlapi.model.AxiomType;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom;
import org.semanticweb.owlapi.model.parameters.Imports;
import org.semanticweb.owlapi.reasoner.OWLReasonerFactory;

import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory;

import org.semanticweb.HermiT.ReasonerFactory;
import org.semanticweb.elk.owlapi.ElkReasonerFactory;

import uk.ac.manchester.cs.jfact.JFactFactory;
import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
import uk.ac.manchester.cs.owl.owlapi.OWLDeclarationAxiomImpl;

/**
simpleOWLAPI is a light-weight wrapper for the OWLAPI enabling more concise OWL ontology development.

Copyright (C) <2020>  Kody Moodley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see .
*/

/** Core class for simpleOWLAPI which provides access to all methods for constructing and editing OWL ontologies. 
 * @author Kody Moodley
 * @author https://sites.google.com/site/kodymoodley/
 * @version 0.0.1
*/
public class SimpleOWLAPIFactory
{
    /** reference to current instance of SimpleOWLAPIFactory
    */
	private static SimpleOWLAPIFactory obj;
    /** instance of OWLOntologyManager
    */
	private static OWLOntologyManager ontologyManager;
    /** flag to keep track of whether the parser should render OWL entities using their full IRI or just a short form human-readable label.
    */
	private static boolean fullIRIRendering;
    /** reference to currently selected OWL reasoner: default is JFACT
    */
	private SelectedReasoner selectedReasoner = SelectedReasoner.JFACT; // default reasoner is JFact
    /** reference to an OWLReasonerFactory object
    */
	private OWLReasonerFactory reasonerFactory;
    /** reference to the currently selected OWLOntology
    */
	private static OWLOntology selectedOntology;
    /** reference to the IRI of the currently selected OWLOntology
    */
	private static IRI selectedOntologyIRI;
    /** OWLDataFactory instance
    */
	private static OWLDataFactoryImpl dataFactory;
    /** Parser instance
    */
	private static Parser parser = Parser.getInstance();
    /** SimpleOWLReasoner instance
    */
	public SimpleOWLReasoner owlReasoner;
    /** ManchesterOWLSyntaxOWLObjectRendererImpl instance (allows rendering of OWL entities using short form human-readable label) 
    */
	private static ManchesterOWLSyntaxOWLObjectRendererImpl renderer = Parser.renderer;

	/** Constructor for SimpleOWLAPIFactory
	 * @param selectedReasoner a SelectedReasoner instance 
	*/
	private SimpleOWLAPIFactory(SelectedReasoner selectedReasoner) {
		ontologyManager=OWLManager.createOWLOntologyManager();
		setOWLReasoner(selectedReasoner);
		fullIRIRendering = false;
		dataFactory = new OWLDataFactoryImpl();
	}

	/** Static method to get hold of a SimpleOWLAPIFactory instance
	 * @return Returns an instance of SimpleOWLAPIFactory using default reasoner JFACT 
	*/
	public static synchronized SimpleOWLAPIFactory getInstance() 
	{ 
		if (obj==null){
			obj = getInstance(SelectedReasoner.JFACT);
		}
		return obj; 
	}

	/** Static method to get hold of a SimpleOWLAPIFactory instance
	 * @param selectedReasoner the reasoner to use with this SimpleOWLAPIFactory instance 
	 * @return Returns an instance of SimpleOWLAPIFactory that uses the specified reasoner 
	*/
	public static synchronized SimpleOWLAPIFactory getInstance(SelectedReasoner selectedReasoner) 
	{ 
		if (obj==null){
			obj = new SimpleOWLAPIFactory(selectedReasoner); 
		}
		return obj; 
	}

	/** Set or initialise the OWLReasonerFactory implementation to use for this SimpleOWLAPIFactory instance, based on the reasoner selected
	 * @param selectedReasoner the reasoner to use with this SimpleOWLAPIFactory instance 
	*/
	public void setOWLReasoner(SelectedReasoner selectedReasoner)
	{
		if (selectedReasoner == SelectedReasoner.HERMIT)
		{
			this.selectedReasoner = SelectedReasoner.HERMIT;
			this.reasonerFactory = new ReasonerFactory();
		}
		if (selectedReasoner == SelectedReasoner.JFACT)
		{
			this.selectedReasoner = SelectedReasoner.JFACT;
			this.reasonerFactory = new JFactFactory();
		}
		if (selectedReasoner == SelectedReasoner.PELLET)
		{
			this.selectedReasoner = SelectedReasoner.PELLET;
			this.reasonerFactory = PelletReasonerFactory.getInstance();
		}
		if (selectedReasoner == SelectedReasoner.ELK)
		{
			this.selectedReasoner = SelectedReasoner.ELK;
			this.reasonerFactory = new ElkReasonerFactory();
		}
		
		if (selectedOntology != null)
			owlReasoner = new SimpleOWLReasoner(reasonerFactory, selectedOntology, parser, selectedReasoner);
	}

	/** Set the type of rendering for OWL entities whenever the client prints a class, property, axiom etc. Either the full IRI or the short form label
	 * @param option true if the client wishes to print full IRIs for OWL entities, false if the client wants to print only short form labels for OWL entities 
	*/
	public void setFullIRIRendering(boolean option)
	{ 
		fullIRIRendering = option;
	}

	/** Creates a new OWL ontology given a string representation of an IRI
	 * @param iriStr a string representation of an IRI 
	 * @return an OWLOntology object 
	 * @throws org.semanticweb.owlapi.model.OWLOntologyCreationException if the IRI is invalid
	*/
	public OWLOntology createOntology(String iriStr) throws OWLOntologyCreationException
	{
		// Ontology IRI
		IRI ontologyIRI = IRI.create(iriStr);
		// Create a fresh ontology
		OWLOntology ontology = ontologyManager.createOntology(ontologyIRI);
		System.out.println("Created ontology: " + ontologyIRI);

		if (selectedOntology==null){
			selectedOntology = ontology;
			selectedOntologyIRI = ontologyIRI;
			owlReasoner = null;
			owlReasoner = new SimpleOWLReasoner(reasonerFactory, selectedOntology, parser, selectedReasoner);//SimpleOWLReasoner.getInstance(reasonerFactory, selectedOntology, parser, selectedReasoner);
		}

		return ontology;
	}

	/** Sets the currently selected (active) ontology to be the one denoted by the input IRI string 
	 * @param iriStr a string representation of an IRI for an ontology
	*/
	public void setOntology(String iriStr)
	{
		selectedOntology = ontologyManager.getOntology(IRI.create(iriStr));
		if (selectedOntology != null) 
		{
			System.out.println("Selected ontology is: " + selectedOntology.getOntologyID().getOntologyIRI().get().toString());
			selectedOntologyIRI = selectedOntology.getOntologyID().getDefaultDocumentIRI().get();
			owlReasoner = null;
			owlReasoner = new SimpleOWLReasoner(reasonerFactory, selectedOntology, parser, selectedReasoner);
		}
		else {
			System.out.println("SimpleOWLAPI ERROR: Ontology <" + iriStr + "> does not exist!");
		}
	}

	/** Sets the currently selected (active) ontology to be the one denoted by the input OWLOntology reference 
	 * @param ontology an OWLOntology object
	*/
	public void setOntology(OWLOntology ontology)
	{
		if (ontology != null) 
		{
			selectedOntology = ontology;
			selectedOntologyIRI = selectedOntology.getOntologyID().getOntologyIRI().get();
			System.out.println("Selected ontology is: " + selectedOntology.getOntologyID().getOntologyIRI().get().toString());
			owlReasoner = null;
			owlReasoner = new SimpleOWLReasoner(reasonerFactory, selectedOntology, parser, selectedReasoner);
		}
		else {
			System.out.println("SimpleOWLAPI ERROR: Invalid or non-existent ontology!");
		}
	}

	/** Prints the IRI string of the currently selected ontology 
	*/
	public void getOntology()
	{
		if (selectedOntology != null) {
			System.out.println("Selected ontology is: " + selectedOntology.getOntologyID().getOntologyIRI().get().toString());
		}
		else {
			if (ontologyManager.getOntologies().size() > 0) {
				Iterator ontIter = ontologyManager.getOntologies().iterator();
				while (ontIter.hasNext()) {
					OWLOntology currentOnt = ontIter.next();
					if (currentOnt.getOntologyID().getDefaultDocumentIRI().isPresent())
						selectedOntology = currentOnt;
				}
				System.out.println("Selected ontology is: " + selectedOntology.getOntologyID().getOntologyIRI().get().toString());
			}
			else {
				System.out.println("SimpleOWLAPI Error: There are no ontologies in this context! Use the createOntology(...) method to create one.");
			}
		}
	}
	
	/** Prints the IRI strings of each ontology that has been created so far using this instance of SimpleOWLAPIFactory  
	*/
	public void getOntologies()
	{
		if (ontologyManager.getOntologies().size() > 0) {
			int idx = 1;
			for (OWLOntology o: ontologyManager.getOntologies()) {
				if (o.getOntologyID().getDefaultDocumentIRI().isPresent()) {
					System.out.println(idx + ". " + o.getOntologyID().getDefaultDocumentIRI().get());
					idx++;
				}
			}
		}
		else {
			System.out.println("SimpleOWLAPI Error: ontology list is empty! Please create an ontology first.");
		}
	}

	/** Creates a new class name, adds this class to the parser's vocabulary and the currently selected ontology, and prints out the class to the console
	 * @param classname A string representation of a class name  
	*/
	public void createClass(String classname)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add classes to! First create an ontology.");
		}
		else {
			OWLClass c = dataFactory.getOWLClass(IRI.create(selectedOntologyIRI.toString() + classname));
			parser.addVocab(c);
			OWLDeclarationAxiomImpl a = new OWLDeclarationAxiomImpl(c, new HashSet());
			OWLAxiom class_declaration = a.getAxiomWithoutAnnotations();
			ontologyManager.addAxiom(selectedOntology, class_declaration);
			if (fullIRIRendering)
				System.out.println("Class: " + c);
			else
				System.out.println("Class: " + renderer.render(c));
		}
	}
	
	/** Creates a new object property, adds this object property to the parser's vocabulary and the currently selected ontology, and prints out the object property to the console
	 * @param opropname A string representation of an object property
	*/	
	public void createOProperty(String opropname)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add properties to! First create an ontology.");
		}
		else {
			createOProperty(opropname, 0, 0, 0);
		}
	}

	/** Creates a new object property, adds this object property to the parser's vocabulary and the currently selected ontology, and prints out the object property to the console
	 * @param opropname A string representation of an object property
	 * @param trans an integer value representing whether the object property should be made transitive or not. 1 for transitive and 0 for not making it transitive
	 * @param ref an integer value representing whether the object property should be made reflexive or not. 1 for reflexive and 0 for not making it reflexive, 2 for making it irreflexive
	 * @param sym an integer value representing whether the object property should be made symmetric or not. 1 for symmetric and 0 for not making it symmetric, 2 for making it asymmetric
	*/	
	public void createOProperty(String opropname, int trans, int ref, int sym)
	{
		if (trans == 1){
			OWLObjectProperty r = dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + opropname));
			parser.addVocab(r);
			OWLTransitiveObjectPropertyAxiom t = dataFactory.getOWLTransitiveObjectPropertyAxiom(r, new HashSet());
			ontologyManager.addAxiom(selectedOntology, t);
			if (fullIRIRendering)
				System.out.println("ObjectProperty: " + r);
			else
				System.out.println("ObjectProperty: " + renderer.render(r));
		}
		if (ref == 1){
			OWLObjectProperty r = dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + opropname));
			parser.addVocab(r);
			OWLReflexiveObjectPropertyAxiom t = dataFactory.getOWLReflexiveObjectPropertyAxiom(r, new HashSet());
			ontologyManager.addAxiom(selectedOntology, t);
			if (fullIRIRendering)
				System.out.println("ObjectProperty: " + r);
			else
				System.out.println("ObjectProperty: " + renderer.render(r));
		}
		if (ref == 2){
			OWLObjectProperty r = dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + opropname));
			parser.addVocab(r);
			OWLIrreflexiveObjectPropertyAxiom t = dataFactory.getOWLIrreflexiveObjectPropertyAxiom(r, new HashSet());
			ontologyManager.addAxiom(selectedOntology, t);
			if (fullIRIRendering)
				System.out.println("ObjectProperty: " + r);
			else
				System.out.println("ObjectProperty: " + renderer.render(r));
		}
		if (sym == 1){
			OWLObjectProperty r = dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + opropname));
			parser.addVocab(r);
			OWLSymmetricObjectPropertyAxiom t = dataFactory.getOWLSymmetricObjectPropertyAxiom(r, new HashSet());
			ontologyManager.addAxiom(selectedOntology, t);
			if (fullIRIRendering)
				System.out.println("ObjectProperty: " + r);
			else
				System.out.println("ObjectProperty: " + renderer.render(r));
		}
		if (sym == 2){
			OWLObjectProperty r = dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + opropname));
			parser.addVocab(r);
			OWLAsymmetricObjectPropertyAxiom t = dataFactory.getOWLAsymmetricObjectPropertyAxiom(r, new HashSet());
			ontologyManager.addAxiom(selectedOntology, t);
			if (fullIRIRendering)
				System.out.println("ObjectProperty: " + r);
			else
				System.out.println("ObjectProperty: " + renderer.render(r));
		}
		if ((trans == 0) && (ref == 0) && (sym == 0)) {
			OWLObjectProperty r = dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + opropname));
			parser.addVocab(r);
			OWLDeclarationAxiomImpl a = new OWLDeclarationAxiomImpl(r, new HashSet());
			OWLAxiom role_declaration = a.getAxiomWithoutAnnotations();
			ontologyManager.addAxiom(selectedOntology, role_declaration);
			if (fullIRIRendering)
				System.out.println("ObjectProperty: " + r);
			else
				System.out.println("ObjectProperty: " + renderer.render(r));
		}
	}
	
	/** Creates a new data property, adds this data property to the parser's vocabulary and the currently selected ontology, and prints out the data property to the console
	 * @param dpropname A string representation of a data property  
	*/	
	public void createDProperty(String dpropname) 
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add properties to! First create an ontology.");
		}
		else {
			OWLDataProperty dprop = dataFactory.getOWLDataProperty(IRI.create(selectedOntologyIRI.toString() + dpropname));
			parser.addVocab(dprop);
			if (fullIRIRendering)
				System.out.println("DataProperty: " + dprop);
			else
				System.out.println("DataProperty: " + renderer.render(dprop));
		}
	}
	
	/** Creates a new individual name, adds this individual name to the parser's vocabulary and the currently selected ontology, and prints out the individual name to the console
	 * @param individualname A string representation of a individual name  
	*/	
	public void createIndividual(String individualname)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add individuals to! First create an ontology.");
		}
		else {
			OWLNamedIndividual i = dataFactory.getOWLNamedIndividual(IRI.create(selectedOntologyIRI.toString() + individualname));
			parser.addVocab(i);
			OWLDeclarationAxiomImpl a = new OWLDeclarationAxiomImpl(i, new HashSet());
			OWLAxiom individual_declaration = a.getAxiomWithoutAnnotations();
			ontologyManager.addAxiom(selectedOntology, individual_declaration);
			if (fullIRIRendering)
				System.out.println("Individual: " + i);
			else
				System.out.println("Individual: " + renderer.render(i));
		}
	}

	/** Creates a new OWLAxiom, adds this axiom to the currently selected ontology, and prints out the axiom to the console
	 * @param axiomStr A string representation of the axiom in Manchester OWL syntax
	 * @return An OWLAxiom object representing the axiom in the input string
	*/	
	public OWLAxiom createAxiom(String axiomStr)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add axioms to! First create an ontology.");
			return null;
		}
		else {
			try {
				parser.setString(axiomStr);
				OWLAxiom axiom = parser.getParser().parseAxiom();
				ontologyManager.addAxiom(selectedOntology, axiom);
				if (fullIRIRendering)
					System.out.println("OWLAxiom: " + axiom);
				else
					System.out.println("OWLAxiom: " + renderer.render(axiom));
				return axiom;
			}
			catch (ParserException pe) {
				System.out.println("SimpleOWLAPI PARSER ERROR: There is something wrong with your axiom expression. One reason could be that you did not create some entities (class, property or individual names) that are referenced in this axiom you are attempting to create.");
				return null;
			}
		}
	}

	/** Creates an anonymous class expression and prints out the class expression to the console
	 * @param classExpressionStr A string representation of the class expression in Manchester OWL syntax
	 * @return An OWLClassExpression object representing the class in the input string 
	*/	
	public OWLClassExpression createClassExpression(String classExpressionStr)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add classes to! First create an ontology.");
			return null;
		}
		else {
			parser.setString(classExpressionStr);
			OWLClassExpression clsEx = parser.getParser().parseClassExpression();
			if (fullIRIRendering)
				System.out.println("OWLClassExpression: " + clsEx);
			else
				System.out.println("OWLClassExpression: " + renderer.render(clsEx));
			return clsEx;
		}
	}
	
	/** Creates multiple class names, adds them to the parser vocabulary and the currently selected ontology
	 * @param classnames A single space separated list of class names
	*/	
	public void createClasses(String classnames)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add properties to! First create an ontology.");
		}
		else {
			String [] classes = classnames.split(" ");
			if (classes.length == 0){
				System.out.println("SimpleOWLAPI PARSER ERROR: incorrect syntax for creating class names. String requires more than 1 token (class names) each separated by single spaces");
			}
			else {
				for (String c: classes)
					createClass(c);
			}
		}
	}

	/** Creates multiple object properties, adds them to the parser vocabulary and the currently selected ontology
	 * @param opropsstr A single space separated list of object properties
	*/	
	public void createOProperties(String opropsstr)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add properties to! First create an ontology.");
		}
		else {
			String [] oprops = opropsstr.split(" ");
			if (oprops.length == 0){
				System.out.println("SimpleOWLAPI PARSER ERROR: incorrect syntax for creating object property names. String requires more than 1 token (object property names) each separated by single spaces");
			}
			else {
				for (String o: oprops)
					createOProperty(o);
			}
		}
	}

	/** Creates multiple individual names, adds them to the parser vocabulary and the currently selected ontology
	 * @param indnames A single space separated list of individual names
	*/	
	public void createIndividuals(String indnames)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add properties to! First create an ontology.");
		}
		else {
			String [] inds = indnames.split(" ");
			if (inds.length == 0){
				System.out.println("SimpleOWLAPI PARSER ERROR: incorrect syntax for creating individual names. String requires more than 1 token (individual names) each separated by single spaces");
			}
			else {
				for (String i: inds)
					createIndividual(i);
			}
		}
	}

	/** Creates multiple data properties, adds them to the parser vocabulary and the currently selected ontology
	 * @param dpropsstr A single space separated list of data properties
	*/	
	public void createDProperties(String dpropsstr)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add properties to! First create an ontology.");
		}
		else {
			String [] dprops = dpropsstr.split(" ");
			if (dprops.length == 0){
				System.out.println("SimpleOWLAPI PARSER ERROR: incorrect syntax for creating data property names. String requires more than 1 token (data property names) each separated by single spaces");
			}
			else {
				for (String d: dprops)
					createDProperty(d);
			}
		}
	}

	/** Makes an existing object property in the ontology transitive
	 * @param opropname A string representation of an object property
	*/	
	public void makeTransitive(String opropname)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add properties to! First create an ontology.");
		}
		else {
			OWLObjectProperty r = dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + opropname));
			if (selectedOntology.getSignature().contains(r)) {
				OWLTransitiveObjectPropertyAxiom t = dataFactory.getOWLTransitiveObjectPropertyAxiom(r, new HashSet());
				ontologyManager.addAxiom(selectedOntology, t);
				if (fullIRIRendering)
					System.out.println(t);
				else
					System.out.println(renderer.render(t));
			}
			else {
				System.out.println("SimpleOWLAPI ERROR: " + renderer.render(r) + "does not appear in the selected ontology!");
			}
		}
	}

	/** Makes an existing object property in the ontology symmetric
	 * @param opropname A string representation of an object property
	*/	
	public void makeSymmetric(String opropname)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add properties to! First create an ontology.");
		}
		else {
			OWLObjectProperty r = dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + opropname));
			if (selectedOntology.getSignature().contains(r)) {
				OWLSymmetricObjectPropertyAxiom s = dataFactory.getOWLSymmetricObjectPropertyAxiom(r, new HashSet());
				ontologyManager.addAxiom(selectedOntology, s);
				if (fullIRIRendering)
					System.out.println(s);
				else
					System.out.println(renderer.render(s));
			}
			else {
				System.out.println("SimpleOWLAPI ERROR: " + renderer.render(r) + "does not appear in the selected ontology!");
			}
		}
	}

	/** Makes an existing object property in the ontology reflexive
	 * @param opropname A string representation of an object property
	*/		
	public void makeReflexive(String opropname)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add properties to! First create an ontology.");
		}
		else {
			OWLObjectProperty r = dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + opropname));
			if (selectedOntology.getSignature().contains(r)) {
				OWLReflexiveObjectPropertyAxiom re = dataFactory.getOWLReflexiveObjectPropertyAxiom(r, new HashSet());
				ontologyManager.addAxiom(selectedOntology, re);
				if (fullIRIRendering)
					System.out.println(re);
				else
					System.out.println(renderer.render(re));
			}
			else {
				System.out.println("SimpleOWLAPI ERROR: " + renderer.render(r) + "does not appear in the selected ontology!");
			}
		}
	}

	/** Makes an existing object property in the ontology irreflexive
	 * @param opropname A string representation of an object property
	*/			
	public void makeIRReflexive(String opropname)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add properties to! First create an ontology.");
		}
		else {
			OWLObjectProperty r = dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + opropname));
			if (selectedOntology.getSignature().contains(r)) {
				OWLIrreflexiveObjectPropertyAxiom irr = dataFactory.getOWLIrreflexiveObjectPropertyAxiom(r, new HashSet());
				ontologyManager.addAxiom(selectedOntology, irr);
				if (fullIRIRendering)
					System.out.println(irr);
				else
					System.out.println(renderer.render(irr));
			}
			else {
				System.out.println("SimpleOWLAPI ERROR: " + renderer.render(r) + "does not appear in the selected ontology!");
			}
		}
	}

	/** Makes an existing object property in the ontology asymmetric
	 * @param opropname A string representation of an object property
	*/			
	public void makeAntiSymmetric(String opropname)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to add properties to! First create an ontology.");
		}
		else {
			OWLObjectProperty r = dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + opropname));
			if (selectedOntology.getSignature().contains(r)) {
				OWLAsymmetricObjectPropertyAxiom a = dataFactory.getOWLAsymmetricObjectPropertyAxiom(r, new HashSet());
				ontologyManager.addAxiom(selectedOntology, a);
				if (fullIRIRendering)
					System.out.println(a);
				else
					System.out.println(renderer.render(a));
			}
			else {
				System.out.println("SimpleOWLAPI ERROR: " + renderer.render(r) + "does not appear in the selected ontology!");
			}
		}
	}

	//    // Create role assertion axiom
	//    public OWLAxiom createOPropertyAssertion(String axiomStr)
	//    {
	//        String [] parts = axiomStr.split(" ");
	//        if (parts.length != 3){
	//            System.out.println("Parser error: incorrect syntax for role assertion. requires exactly three tokens separated by single spaces");
	//            return null;
	//        }
	//        else{
	//            OWLObjectPropertyAssertionAxiom a = dataFactory.getOWLObjectPropertyAssertionAxiom(dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + parts[1])), dataFactory.getOWLNamedIndividual(IRI.create(selectedOntologyIRI.toString() + parts[0])), dataFactory.getOWLNamedIndividual(IRI.create(selectedOntologyIRI.toString() + parts[2])));
	//            ontologyManager.addAxiom(selectedOntology, a);
	//            if (fullIRIRendering)
	//                System.out.println("ObjectPropertyAssertion: " + a);
	//            else
	//                System.out.println("ObjectPropertyAssertion: " + renderer.render(a));
	//            return a;
	//        }
	//    }

	/** Makes all the individual names in the ontology refer to different individuals (simulate the unique name assumption)
	 * @return An OWLAxiom which enforces that all individual names in the ontology refer to different individuals (simulating the unique name assumption)
	*/		
	public OWLAxiom allIndividualsDifferent(){
		Set inds = new HashSet();
		inds = selectedOntology.getIndividualsInSignature();//.individualsInSignature(Imports.EXCLUDED).forEach(c->inds.add(c));

		if (inds.size() < 2){
			System.out.println("Error: requires more than 1 individual name in the ontology.");
			return null;
		}
		else{
			OWLAxiom a = dataFactory.getOWLDifferentIndividualsAxiom(inds, new HashSet()); 
			ontologyManager.addAxiom(selectedOntology, a);
			if (fullIRIRendering)
				System.out.println(a);
			else
				System.out.println(renderer.render(a));
			return a;
		}
	}

	/** Makes the specified individual names in the ontology refer to different individuals (simulate the unique name assumption for a subset of individual names)
	 * @param differentIndividualsStr a single-space separated list of individual names e.g. "john mary mark" (no quotes). 
	 * @return An OWLAxiom which enforces that the specified individual names refer to different individuals (simulating the unique name assumption for the given individual names)
	*/		
	public OWLAxiom differentIndividuals(String differentIndividualsStr){
		String [] parts = differentIndividualsStr.split(" ");
		Set inds = new HashSet();
		for (String s: parts){
			inds.add(dataFactory.getOWLNamedIndividual(IRI.create(selectedOntologyIRI.toString() + s)));
		}

		if (parts.length < 2){
			System.out.println("SimpleOWLAPI PARSER ERROR: incorrect syntax for different individuals axiom. requires more than 1 token (individual name) separated by single spaces");
			return null;
		}
		else{
			OWLAxiom a = dataFactory.getOWLDifferentIndividualsAxiom(inds, new HashSet()); 
			ontologyManager.addAxiom(selectedOntology, a);
			if (fullIRIRendering)
				System.out.println(a);
			else
				System.out.println(renderer.render(a));
			return a;
		}
	}

	/** Prints a grouped list of the main logical entities in the ontology to console (classes, properties, individuals, TBox, ABox, RBox etc.)
	*/
	public void printOntology(){
		Set signature = selectedOntology.getSignature();
		Set rbox = selectedOntology.getRBoxAxioms(Imports.EXCLUDED);
		Set tbox = selectedOntology.getTBoxAxioms(Imports.EXCLUDED);
		Set abox = selectedOntology.getABoxAxioms(Imports.EXCLUDED);

		Set clses = new HashSet();
		Set oprops = new HashSet();
		Set dprops = new HashSet();
		Set inds = new HashSet();

		for (OWLEntity e: signature) {
			if (e.isOWLClass())
				clses.add(e);
			if (e.isOWLObjectProperty()) 
				oprops.add(e);
			if (e.isOWLNamedIndividual()) 
				inds.add(e);	
			if (e.isOWLDataProperty()) 
				dprops.add(e);
		}

		System.out.println();

		System.out.println("Classes:");
		System.out.println("--------");

		for (OWLEntity c: clses){
			System.out.println(renderer.render(c));
		}

		System.out.println();

		System.out.println("Object properties:");
		System.out.println("------------------");

		for (OWLEntity r: oprops){
			System.out.println(renderer.render(r));
		}

		System.out.println();

		System.out.println("Data properties:");
		System.out.println("----------------");

		for (OWLEntity r: dprops){
			System.out.println(renderer.render(r));
		}

		System.out.println();

		System.out.println("Individuals:");
		System.out.println("------------");

		for (OWLEntity i: inds){
			System.out.println(renderer.render(i));
		}

		System.out.println();

		System.out.println("TBox:");
		System.out.println("-----");

		for (OWLAxiom a : tbox)
			System.out.println(renderer.render(a));

		System.out.println();

		System.out.println("ABox:");
		System.out.println("-----");

		for (OWLAxiom a : abox)
			System.out.println(renderer.render(a));    

		System.out.println();

		System.out.println("RBox:");
		System.out.println("-----");

		for (OWLAxiom a : rbox)
			System.out.println(renderer.render(a)); 
	}

	/** Prints an OWLAxiom object to console output
	 * @param axiom OWLAxiom object to print to console
	*/
	public void print(OWLAxiom axiom){
		if (fullIRIRendering)
			System.out.println(axiom);
		else
			System.out.println(renderer.render(axiom));
	}

	/** Prints OWLOntology metrics to console output e.g. number of axioms, subclass axioms etc.
	*/
	public void printOntologyStats(){
		System.out.println();
		String ontIRI = selectedOntology.getOntologyID().getDefaultDocumentIRI().get().toString();
		System.out.println("Stats for Ontology: " + ontIRI);
		for (int i = 0; i < ontIRI.length()+20;i++)
			System.out.print("-");
		System.out.println();
		// Number of axioms and constructs in ontology
		System.out.println("Number of axioms: " + selectedOntology.getAxiomCount());
		System.out.println("Number of logical axioms: " + selectedOntology.getLogicalAxiomCount());
		System.out.println("Number of classes: " + selectedOntology.getClassesInSignature(Imports.EXCLUDED).size());
		System.out.println("Number of object properties: " + selectedOntology.getObjectPropertiesInSignature(Imports.EXCLUDED).size());
		System.out.println("Number of data properties: " + selectedOntology.getDataPropertiesInSignature(Imports.EXCLUDED).size());
		System.out.println("Number of individuals: " + selectedOntology.getIndividualsInSignature(Imports.EXCLUDED).size());
		// Number of axioms of a specific type in ontology
		System.out.println("Number of SubClassOf axioms: " + selectedOntology.getAxioms(AxiomType.SUBCLASS_OF).size());
		System.out.println("Number of EquivalentClasses axioms: " + selectedOntology.getAxioms(AxiomType.EQUIVALENT_CLASSES).size());
		System.out.println("Number of DisjointClasses axioms: " + selectedOntology.getAxioms(AxiomType.DISJOINT_CLASSES).size());
		System.out.println("Number of Class assertions: " + selectedOntology.getAxioms(AxiomType.CLASS_ASSERTION).size());
		System.out.println("Number of Object property assertions: " + selectedOntology.getAxioms(AxiomType.OBJECT_PROPERTY_ASSERTION).size());
		System.out.println("Number of Data property assertions: " + selectedOntology.getAxioms(AxiomType.DATA_PROPERTY_ASSERTION).size());
	}

	/** Removes class name from the parser vocabulary and currently selected ontology
	 * @param classname A string representation of the class name to remove  
	*/	
	public void removeClass(String classname) {
		OWLClass c = dataFactory.getOWLClass(IRI.create(selectedOntologyIRI.toString() + classname));
		OWLDeclarationAxiomImpl a = new OWLDeclarationAxiomImpl(c, new HashSet());
		ontologyManager.removeAxiom(selectedOntology, a);
	}
	
	/** Removes multiple class names from the parser vocabulary and currently selected ontology
	 * @param classnames A single space separated list of class names to remove from the ontology e.g. "Student Lecturer Person"   
	*/
	public void removeClasses(String classnames)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to remove classes from!");
		}
		else {
			String [] classes = classnames.split(" ");
			if (classes.length == 0){
				System.out.println("SimpleOWLAPI PARSER ERROR: incorrect syntax for removing class names. String requires more than 1 token (class names) each separated by single spaces");
			}
			else {
				for (String c: classes)
					removeClass(c);
			}
		}
	}

	/** Removes an object property from the parser vocabulary and currently selected ontology
	 * @param opropname A string representation of the object property to remove  
	*/	
	public void removeOProperty(String opropname) {
		OWLObjectProperty r = dataFactory.getOWLObjectProperty(IRI.create(selectedOntologyIRI.toString() + opropname));
		OWLDeclarationAxiomImpl a = new OWLDeclarationAxiomImpl(r, new HashSet());
		ontologyManager.removeAxiom(selectedOntology, a);
	}
	
	/** Removes a data property from the parser vocabulary and currently selected ontology
	 * @param dpropname A string representation of the data property to remove  
	*/	
	public void removeDProperty(String dpropname) {
		OWLDataProperty r = dataFactory.getOWLDataProperty(IRI.create(selectedOntologyIRI.toString() + dpropname));
		OWLDeclarationAxiomImpl a = new OWLDeclarationAxiomImpl(r, new HashSet());
		ontologyManager.removeAxiom(selectedOntology, a);
	}
	
	
	/** Removes multiple object properties from the parser vocabulary and currently selected ontology
	 * @param opropnames A single space separated list of object properties to remove from the ontology e.g. "marriedTo hasTopping teachesCourse"  
	*/	
	public void removeOProperties(String opropnames)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to remove properties from!");
		}
		else {
			String [] oprops = opropnames.split(" ");
			if (oprops.length == 0){
				System.out.println("SimpleOWLAPI PARSER ERROR: incorrect syntax for removing properties. String requires more than 1 token (property names) each separated by single spaces");
			}
			else {
				for (String o: oprops)
					removeOProperty(o);
			}
		}
	}
	
	/** Removes multiple data properties from the parser vocabulary and currently selected ontology
	 * @param dpropnames A single space separated list of data properties to remove from the ontology e.g. "hasHeight hasSalary hasConcentration"  
	*/	
	public void removeDProperties(String dpropnames)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to remove properties from!");
		}
		else {
			String [] dprops = dpropnames.split(" ");
			if (dprops.length == 0){
				System.out.println("SimpleOWLAPI PARSER ERROR: incorrect syntax for removing properties. String requires more than 1 token (property names) each separated by single spaces");
			}
			else {
				for (String d: dprops)
					removeDProperty(d);
			}
		}
	}

	/** Removes individual name from the parser vocabulary and currently selected ontology
	 * @param individualname A string representation of the object property to remove  
	*/	
	public void removeIndividual(String individualname) {
		OWLNamedIndividual i = dataFactory.getOWLNamedIndividual(IRI.create(selectedOntologyIRI.toString() + individualname));
		OWLDeclarationAxiomImpl a = new OWLDeclarationAxiomImpl(i, new HashSet());
		ontologyManager.removeAxiom(selectedOntology, a);
	}
	
	/** Removes multiple individual names from the parser vocabulary and currently selected ontology
	 * @param indnames A single space separated list of individual names to remove from the ontology e.g. "john mary mark"  
	*/	
	public void removeIndividuals(String indnames)
	{
		if (selectedOntology == null) {
			System.out.println("SimpleOWLAPI ERROR: There is no ontology to remove properties from!");
		}
		else {
			String [] inds = indnames.split(" ");
			if (inds.length == 0){
				System.out.println("SimpleOWLAPI PARSER ERROR: incorrect syntax for removing individuals. String requires more than 1 token (individual names) each separated by single spaces");
			}
			else {
				for (String i: inds)
					removeIndividual(i);
			}
		}
	}
	
	/** Removes axiom from the currently selected ontology
	 * @param axiomStr A string representation of the axiom to remove in Manchester OWL syntax  
	*/
	public void removeAxiom(String axiomStr) {
		parser.setString(axiomStr);
		OWLAxiom axiom = parser.getParser().parseAxiom();
		ontologyManager.removeAxiom(selectedOntology, axiom);
	}

	/** Removes axiom from the currently selected ontology
	 * @param a an OWLAxiom object reference for the axiom to remove  
	*/
	public void removeAxiom(OWLAxiom a) {
		ontologyManager.removeAxiom(selectedOntology, a);
	}
	
	/** Removes a set of axioms from the currently selected ontology
	 * @param a a HashSet of OWLAxiom objects to remove from the ontology  
	*/
	public void removeAxioms(Set a) {
		ontologyManager.removeAxioms(selectedOntology, a);
	}

	/** Removes all axioms (logical and otherwise) from the ontology  
	*/
	public void resetOntology(){
		ontologyManager.removeAxioms(selectedOntology, selectedOntology.getAxioms());
	}

	/** Loads an OWL ontology from local file into the SimpleOWLAPIFactory instance. This ontology becomes the currently selected ontology.
	 * @param filepath a string representation of the relative path to the local ontology file on the local machine
	 * @return an OWLOntology instance 
	 * @throws org.semanticweb.owlapi.model.OWLOntologyCreationException if the filepath is invalid
	*/
	public OWLOntology loadFromFile(String filepath) throws OWLOntologyCreationException {
		File file = new File(filepath);
		OWLOntology ontology = ontologyManager.loadOntologyFromOntologyDocument(file);
		
		// add new ontology signature to parser vocabulary so we can use Manchester OWL strings to manipulate and query it
		for (OWLEntity e: ontology.getSignature()) {
			parser.addVocab(e);
		}
		
		selectedOntology = ontology;
		selectedOntologyIRI = ontology.getOntologyID().getDefaultDocumentIRI().get();
		owlReasoner = null;
		owlReasoner = new SimpleOWLReasoner(reasonerFactory, selectedOntology, parser, selectedReasoner);
		return ontology;
	}

	/** Loads an OWL ontology from a remote URL into the SimpleOWLAPIFactory instance. This ontology becomes the currently selected ontology.
	 * @param url a string representation of the URL to the ontology on the Web
	 * @return an OWLOntology instance 
	 * @throws org.semanticweb.owlapi.model.OWLOntologyCreationException if the remote URL is invalid
	*/
	public OWLOntology loadFromURL(String url) throws OWLOntologyCreationException {
		IRI remoteOntologyIRI = IRI.create(url);
		OWLOntology ontology = ontologyManager.loadOntology(remoteOntologyIRI);
		
		// add new ontology signature to parser vocabulary so we can use Manchester OWL strings to manipulate and query it
		for (OWLEntity e: ontology.getSignature()) {
			parser.addVocab(e);
		}
		
		selectedOntology = ontology;
		selectedOntologyIRI = ontology.getOntologyID().getDefaultDocumentIRI().get();
		owlReasoner = null;
		owlReasoner = new SimpleOWLReasoner(reasonerFactory, selectedOntology, parser, selectedReasoner);
		return ontology;
	}

	/** Saves the currently selected ontology to a local file in Manchester OWL syntax 
	 * @param filepath a string representation of the path and filename to save the ontology to 
	 * @throws java.io.FileNotFoundException if the filepath is invalid
	 * @throws org.semanticweb.owlapi.model.OWLOntologyStorageException if there is a serialisation error when saving the ontology to disk
	*/
	public void saveOntology(String filepath) throws OWLOntologyStorageException, FileNotFoundException{
		FileOutputStream fout=new FileOutputStream(filepath);    
		ontologyManager.saveOntology(selectedOntology, new ManchesterSyntaxDocumentFormat(), fout);
	}
	
	/** Removes an ontology from the current context (simpleOWLAPIFactory instance)  
	 * @param iriStr the IRI string of the ontology to remove 
	*/
	public void removeOntology(String iriStr){
		IRI ontIRI = IRI.create(iriStr);
		
		if (ontologyManager.contains(ontIRI))
			ontologyManager.removeOntology(ontologyManager.getOntology(ontIRI));
		
		if (ontologyManager.getOntologies().size() > 0) {
			Iterator ontIter = ontologyManager.getOntologies().iterator();
			while (ontIter.hasNext()) {
				OWLOntology currentOnt = ontIter.next();
				if (currentOnt.getOntologyID().getDefaultDocumentIRI().isPresent())
					selectedOntology = currentOnt;
			}
		}
	}
	
	/** Removes an ontology from the current context (simpleOWLAPIFactory instance)  
	 * @param ontology the OWLOntology object to remove 
	*/
	public void removeOntology(OWLOntology ontology){
		if (ontology != null && ontologyManager.contains(ontology)) {
			ontologyManager.removeOntology(ontology);
		}
		
		if (ontologyManager.getOntologies().size() > 0) {
			Iterator ontIter = ontologyManager.getOntologies().iterator();
			while (ontIter.hasNext()) {
				OWLOntology currentOnt = ontIter.next();
				if (currentOnt.getOntologyID().getDefaultDocumentIRI().isPresent())
					selectedOntology = currentOnt;
			}
		}
	}
	
	/** Removes selected ontology from the current context (simpleOWLAPIFactory instance)  
	*/
	public void removeOntology(){
		if (selectedOntology != null && ontologyManager.contains(selectedOntology)) {
			ontologyManager.removeOntology(selectedOntology);
		}
		
		if (ontologyManager.getOntologies().size() > 0) {
			Iterator ontIter = ontologyManager.getOntologies().iterator();
			while (ontIter.hasNext()) {
				OWLOntology currentOnt = ontIter.next();
				if (currentOnt.getOntologyID().getDefaultDocumentIRI().isPresent())
					selectedOntology = currentOnt;
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy