import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.io.*; import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.util.SimpleIRIMapper; import org.semanticweb.owlapi.util.DefaultPrefixManager; import org.semanticweb.owlapi.reasoner.*; import org.semanticweb.HermiT.Reasoner; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; import java.net.UnknownHostException; import java.util.Map; //import org.semanticweb.owl.inference.OWLReasonerFactory; //import org.semanticweb.owl.model.*; //import org.mindswap.pellet.owlapi.PelletReasonerFactory; import java.util.Iterator; import java.util.Set; import java.io.*; public class Obligation{ public static final String DOCUMENT_IRI = "file:ontology/OntologyOBL.owl"; public static void main(String[] args) { try { Writer output = null; File file = new File("System_state.txt"); output = new BufferedWriter(new FileWriter(file)); // Create our ontology manager in the usual way. OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); File file1 = new File("ontology/OntologyOBL.owl"); // Now load the local copy OWLOntology ontology = manager.loadOntologyFromOntologyDocument(file1); System.out.println("Loaded ontology: " + ontology); // We can always obtain the location where an ontology was loaded from IRI documentIRI = manager.getOntologyDocumentIRI(ontology); System.out.println(" from: " + documentIRI); // We need a data factory to create various object from. // Each ontology has a reference to a data factory that we can use. OWLDataFactory factory = manager.getOWLDataFactory(); // There are two ways we can create classes (and other entities). // The second is to use a prefix manager and specify abbreviated IRIs. // This is useful for creating lots of entities with the same prefix IRIs. // First create our prefix manager and specify that the default prefix IRI (bound to the empty prefix name) PrefixManager pm = new DefaultPrefixManager("http://www.people.lu.unisi.ch/fornaran/ontologies/OntologyOBL.owl#"); // Now we use the prefix manager and just specify an abbreviated IRI OWLClass Obligation = factory.getOWLClass(":Obligation", pm); OWLClass Cancelled = factory.getOWLClass(":Cancelled", pm); OWLClass KCancelled = factory.getOWLClass(":KCancelled", pm); OWLClass Fulfilled = factory.getOWLClass(":Fulfilled", pm); OWLClass KFulfilled = factory.getOWLClass(":KFulfilled", pm); OWLClass Activated = factory.getOWLClass(":Activated", pm); OWLClass Violated = factory.getOWLClass(":Violated", pm); OWLClass Elapsed = factory.getOWLClass(":Elapsed", pm); output.write("-----------------------------------------------------------------\n"); Set app= ontology.getEquivalentClassesAxioms(KCancelled); System.out.println("Equivalent classes axioms of class KCancelled: " + app); output.write("Equivalent classes axioms of class KCancelled: " + app +"\n"); app=ontology.getEquivalentClassesAxioms(KFulfilled); System.out.println("Equivalent classes axioms of class KFulfilled: " + app); output.write("Equivalent classes axioms of class KFulfilled: " + app + "\n"); //Set CancelledIndividuals; //Set FulfilledIndividuals; NodeSet individualsNodeSet; Set individuals; //Set individuals; OWLClassAssertionAxiom classAssertion; OWLAxiom axiom; OWLClassExpression description; int t=0; int tMax=5; //Create an array with the list of time events of the system String[] instants={"inst1","inst2","inst3","inst4","inst5"}; while (t0) { //remove the axioms that define KFulfilled for(OWLEquivalentClassesAxiom a: ontology.getEquivalentClassesAxioms(KFulfilled)){ RemoveAxiom removeAxiom= new RemoveAxiom(ontology,a); manager.applyChange(removeAxiom); } //create a class with all those individuals description = factory.getOWLObjectOneOf(individuals); axiom = factory.getOWLEquivalentClassesAxiom(KFulfilled, description); AddAxiom addAxiom = new AddAxiom(ontology, axiom); manager.applyChange(addAxiom); } app= ontology.getEquivalentClassesAxioms(KFulfilled); System.out.println("Equivalent classes axioms of class KFulfilled:\n" + app); output.write("Equivalent classes axioms of class KFulfilled:\n" + app +"\n"); //CLOSURE: assert that KCancelled is equivalent to the list of individuals that belong to the Cancelled class individualsNodeSet = reasoner.getInstances(Cancelled, false); individuals = individualsNodeSet.getFlattened(); if (individuals.size()>0) { //remove the axioms that define KFulfilled for(OWLEquivalentClassesAxiom a: ontology.getEquivalentClassesAxioms(KCancelled)){ RemoveAxiom removeAxiom= new RemoveAxiom(ontology,a); manager.applyChange(removeAxiom); } //create a class with all those individuals description = factory.getOWLObjectOneOf(individuals); axiom = factory.getOWLEquivalentClassesAxiom(KCancelled, description); AddAxiom addAxiom = new AddAxiom(ontology, axiom); manager.applyChange(addAxiom); } app= ontology.getEquivalentClassesAxioms(KCancelled); System.out.println("Equivalent classes axioms of class KCancelled:\n" + app); output.write("Equivalent classes axioms of class KCancelled:\n" + app +"\n"); t++; } output.close(); } catch (OWLException e) { e.printStackTrace(); } catch(UnsupportedOperationException exception) { System.out.println("Unsupported reasoner operation."); } catch(IOException exception) { System.out.println("problem with the output file"); } /*catch(OWLReasonerException ex) { System.out.println("Reasoner error: " + ex.getMessage()); } }*/ } }