From 53680982da7898ae889870da416205a85f0bffad Mon Sep 17 00:00:00 2001 From: Jens Elkner Date: Wed, 1 Jun 2016 11:57:24 +0200 Subject: [PATCH 1/2] OWL2/java: reformat wrt. java code conventions, only. --- .../de/unibremen/informatik/Fact/Fact.java | 97 +- .../informatik/FactProver/FactProver.java | 970 +++++++++--------- .../informatik/FactProver/Prove.java | 46 +- .../de/unibremen/informatik/OWL2Parser.java | 683 ++++++------ .../informatik/locality/LocalityChecker.java | 210 ++-- 5 files changed, 946 insertions(+), 1060 deletions(-) diff --git a/OWL2/java/de/unibremen/informatik/Fact/Fact.java b/OWL2/java/de/unibremen/informatik/Fact/Fact.java index 1afc14e4f6..71a7bda006 100644 --- a/OWL2/java/de/unibremen/informatik/Fact/Fact.java +++ b/OWL2/java/de/unibremen/informatik/Fact/Fact.java @@ -1,63 +1,42 @@ package de.unibremen.informatik.Fact; -import uk.ac.manchester.cs.factplusplus.owlapiv3.*; -import uk.ac.manchester.cs.factplusplus.*; -import org.semanticweb.owlapi.reasoner.*; - import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.OWLXMLOntologyFormat; -import org.semanticweb.owlapi.model.*; -import org.semanticweb.owlapi.util.DefaultPrefixManager; - -import java.net.URI; -import java.util.*; - -class Fact -{ - - public static void main (String[] args) - { - if (args.length < 1) - { - System.out.println("owl_fact "); - System.exit(1); - } - - try - { - OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); - - FaCTPlusPlusReasonerFactory f = new FaCTPlusPlusReasonerFactory(); - - IRI physicalIRI = IRI.create(args[0]); - OWLOntology ontology = manager.loadOntologyFromOntologyDocument(physicalIRI); - - OWLReasoner reasoner = f.createReasoner(ontology); - - Boolean cons = reasoner.isConsistent(); - if (cons) - { - System.out.println("consistent"); - System.exit(10); - } - else - { - System.out.println("inconsistent"); - System.exit(20); - } - } - catch (OWLOntologyCreationException e) - { - System.out.println("The ontology could not be created: " + - e.getMessage()); - System.exit(1); - } - catch (Exception e) - { - System.out.println("Exception:" + e.getMessage()); - System.exit(1); - } - - } - +import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.semanticweb.owlapi.reasoner.OWLReasoner; + +import uk.ac.manchester.cs.factplusplus.owlapiv3.FaCTPlusPlusReasonerFactory; + +class Fact { + public static void main(String[] args) { + if (args.length < 1) { + System.out.println("owl_fact "); + System.exit(1); + } + try { + OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); + FaCTPlusPlusReasonerFactory f = new FaCTPlusPlusReasonerFactory(); + IRI physicalIRI = IRI.create(args[0]); + OWLOntology ontology = + manager.loadOntologyFromOntologyDocument(physicalIRI); + OWLReasoner reasoner = f.createReasoner(ontology); + Boolean cons = reasoner.isConsistent(); + if (cons) { + System.out.println("consistent"); + System.exit(10); + } else { + System.out.println("inconsistent"); + System.exit(20); + } + } catch (OWLOntologyCreationException e) { + System.out.println("The ontology could not be created: " + + e.getMessage()); + System.exit(1); + } catch (Exception e) { + System.out.println("Exception:" + e.getMessage()); + System.exit(1); + } + } } diff --git a/OWL2/java/de/unibremen/informatik/FactProver/FactProver.java b/OWL2/java/de/unibremen/informatik/FactProver/FactProver.java index cbbf1ab164..9bda25ad05 100644 --- a/OWL2/java/de/unibremen/informatik/FactProver/FactProver.java +++ b/OWL2/java/de/unibremen/informatik/FactProver/FactProver.java @@ -1,531 +1,477 @@ package de.unibremen.informatik.FactProver; -import uk.ac.manchester.cs.factplusplus.owlapiv3.*; -import uk.ac.manchester.cs.factplusplus.*; -import uk.ac.manchester.cs.factplusplus.FaCTPlusPlus; +import java.util.Iterator; +import java.util.Set; +import java.util.TreeSet; -import org.semanticweb.owlapi.reasoner.*; import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.model.*; - -import java.net.URI; -import java.util.*; -import org.semanticweb.owlapi.reasoner.NodeSet; +import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; +import org.semanticweb.owlapi.model.OWLAnnotationAxiom; +import org.semanticweb.owlapi.model.OWLAnnotationPropertyDomainAxiom; +import org.semanticweb.owlapi.model.OWLAnnotationPropertyRangeAxiom; +import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLAxiom; +import org.semanticweb.owlapi.model.OWLClass; +import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; +import org.semanticweb.owlapi.model.OWLClassAxiom; +import org.semanticweb.owlapi.model.OWLClassExpression; +import org.semanticweb.owlapi.model.OWLDataProperty; +import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; +import org.semanticweb.owlapi.model.OWLDataPropertyAxiom; +import org.semanticweb.owlapi.model.OWLDataPropertyCharacteristicAxiom; +import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom; +import org.semanticweb.owlapi.model.OWLDataPropertyExpression; +import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom; +import org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom; +import org.semanticweb.owlapi.model.OWLDeclarationAxiom; +import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom; +import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom; +import org.semanticweb.owlapi.model.OWLDisjointDataPropertiesAxiom; +import org.semanticweb.owlapi.model.OWLDisjointObjectPropertiesAxiom; +import org.semanticweb.owlapi.model.OWLDisjointUnionAxiom; +import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom; +import org.semanticweb.owlapi.model.OWLEquivalentDataPropertiesAxiom; +import org.semanticweb.owlapi.model.OWLEquivalentObjectPropertiesAxiom; +import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom; +import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLIndividual; +import org.semanticweb.owlapi.model.OWLIndividualAxiom; +import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom; +import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLLiteral; +import org.semanticweb.owlapi.model.OWLLogicalAxiom; +import org.semanticweb.owlapi.model.OWLNamedIndividual; +import org.semanticweb.owlapi.model.OWLNaryAxiom; +import org.semanticweb.owlapi.model.OWLNaryClassAxiom; +import org.semanticweb.owlapi.model.OWLNaryIndividualAxiom; +import org.semanticweb.owlapi.model.OWLNaryPropertyAxiom; +import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom; +import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom; +import org.semanticweb.owlapi.model.OWLObjectProperty; +import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; +import org.semanticweb.owlapi.model.OWLObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLObjectPropertyCharacteristicAxiom; +import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom; +import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; +import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.semanticweb.owlapi.model.OWLPropertyAssertionAxiom; +import org.semanticweb.owlapi.model.OWLPropertyAxiom; +import org.semanticweb.owlapi.model.OWLPropertyDomainAxiom; +import org.semanticweb.owlapi.model.OWLPropertyRangeAxiom; +import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLSameIndividualAxiom; +import org.semanticweb.owlapi.model.OWLSubAnnotationPropertyOfAxiom; +import org.semanticweb.owlapi.model.OWLSubClassOfAxiom; +import org.semanticweb.owlapi.model.OWLSubDataPropertyOfAxiom; +import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom; +import org.semanticweb.owlapi.model.OWLSubPropertyAxiom; +import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom; +import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom; +import org.semanticweb.owlapi.model.OWLUnaryPropertyAxiom; +import org.semanticweb.owlapi.model.SWRLRule; +import org.semanticweb.owlapi.reasoner.BufferingMode; import org.semanticweb.owlapi.reasoner.Node; +import org.semanticweb.owlapi.reasoner.NodeSet; +import org.semanticweb.owlapi.reasoner.SimpleConfiguration; -public class FactProver -{ +import uk.ac.manchester.cs.factplusplus.FaCTPlusPlusException; +import uk.ac.manchester.cs.factplusplus.owlapiv3.FaCTPlusPlusReasoner; +public class FactProver { private IRI iri; private FaCTPlusPlusReasoner reasoner; - private OWLAxiom goal; + private OWLAxiom goal; - public FactProver(IRI iri) throws OWLOntologyCreationException, - FaCTPlusPlusException + public FactProver(IRI iri) + throws OWLOntologyCreationException, FaCTPlusPlusException { this.iri = iri; OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); - OWLOntology ontology = manager.loadOntologyFromOntologyDocument(this.iri); - this.reasoner = new FaCTPlusPlusReasoner(ontology,new SimpleConfiguration(),BufferingMode.BUFFERING); - + OWLOntology ontology = + manager.loadOntologyFromOntologyDocument(this.iri); + this.reasoner = new FaCTPlusPlusReasoner(ontology, + new SimpleConfiguration(), BufferingMode.BUFFERING); } - public void loadGoal(IRI giri) throws OWLOntologyCreationException - { - OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); - OWLOntology ontology = manager.loadOntologyFromOntologyDocument(giri); - - Set axioms = ontology.getAxioms(); - - for(Iterator i = axioms.iterator(); i.hasNext();) { - OWLAxiom ax = i.next(); - if (ax instanceof OWLDeclarationAxiom) - i.remove(); - } - - if (axioms.size() == 1) - { - Iterator it = axioms.iterator(); - this.goal = it.next(); - } - else - { - throw new OWLOntologyCreationException - ("Too many axioms for conjecture"); - } - } - - public boolean prove() throws FaCTPlusPlusException - { - if (goal instanceof OWLObjectPropertyAssertionAxiom) - { - OWLObjectPropertyAssertionAxiom g = - (OWLObjectPropertyAssertionAxiom) goal; - OWLIndividual object = g.getObject(); - OWLIndividual subject = g.getSubject(); - OWLObjectPropertyExpression prop = g.getProperty(); - NodeSet ind = reasoner.getObjectPropertyValues (object.asOWLNamedIndividual(),prop); - - return ind.containsEntity(subject.asOWLNamedIndividual()); - } - if (goal instanceof OWLClassAssertionAxiom) - { - OWLClassAssertionAxiom g = (OWLClassAssertionAxiom) goal; - OWLClassExpression cls = g.getClassExpression(); - OWLIndividual indi = g.getIndividual(); - NodeSet n = reasoner.getTypes(indi.asOWLNamedIndividual(),true); - return n.containsEntity(cls.asOWLClass()); - } - if (goal instanceof OWLDataPropertyAssertionAxiom) - { - OWLDataPropertyAssertionAxiom g = - (OWLDataPropertyAssertionAxiom) goal; - OWLDataProperty prop = g.getProperty().asOWLDataProperty(); - OWLLiteral obj = g.getObject(); - OWLIndividual sub = g.getSubject(); - Set litset = reasoner.getDataPropertyValues(sub.asOWLNamedIndividual(),prop); - return litset.contains(obj); - } - if (goal instanceof OWLEquivalentClassesAxiom) - { - OWLEquivalentClassesAxiom g = (OWLEquivalentClassesAxiom) goal; - OWLClassExpression[] cls = g.getClassExpressions().toArray - (new OWLClassExpression[g.getClassExpressions().size()]); - boolean res = true; - for (int i=0; i < cls.length; i++) - { - for (int j=0; j < cls.length; j++) - { - if (i != j) - { - Node c = reasoner.getEquivalentClasses(cls[i]); - res &= c.contains(cls[j].asOWLClass()); - } - } - } - return res; - } - if (goal instanceof OWLSubClassOfAxiom) - { - OWLSubClassOfAxiom g = (OWLSubClassOfAxiom) goal; - OWLClassExpression sub = g.getSubClass(); - OWLClassExpression top = g.getSuperClass(); - NodeSet c = reasoner.getSubClasses(top,true); - return c.containsEntity(sub.asOWLClass()); - } - if (goal instanceof OWLDataPropertyRangeAxiom) - { - throw new FaCTPlusPlusException - ("Fact++ cannot determine data ranges!"); - } - if (goal instanceof OWLDataPropertyDomainAxiom) - { - OWLDataPropertyDomainAxiom g = (OWLDataPropertyDomainAxiom) goal; - OWLDataProperty prop = g.getProperty().asOWLDataProperty(); - OWLClassExpression d = g.getDomain(); - boolean res = false; - - NodeSet dom = reasoner.getDataPropertyDomains(prop,true); - Iterator> it = dom.iterator(); + public void loadGoal(IRI giri) throws OWLOntologyCreationException { + OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); + OWLOntology ontology = manager.loadOntologyFromOntologyDocument(giri); + Set axioms = ontology.getAxioms(); + for (Iterator i = axioms.iterator(); i.hasNext();) { + OWLAxiom ax = i.next(); + if (ax instanceof OWLDeclarationAxiom) + i.remove(); + } + if (axioms.size() == 1) { + Iterator it = axioms.iterator(); + this.goal = it.next(); + } else { + throw new OWLOntologyCreationException("Too many axioms for conjecture"); + } + } - while (it.hasNext()) { - Node cur = it.next(); - Iterator itt = cur.iterator(); - while (itt.hasNext()) { - OWLClass c = itt.next(); - Node cl = reasoner.getEquivalentClasses(c); - if (cl.contains(d.asOWLClass())) + public boolean prove() throws FaCTPlusPlusException { + if (goal instanceof OWLObjectPropertyAssertionAxiom) { + OWLObjectPropertyAssertionAxiom g = + (OWLObjectPropertyAssertionAxiom) goal; + OWLIndividual object = g.getObject(); + OWLIndividual subject = g.getSubject(); + OWLObjectPropertyExpression prop = g.getProperty(); + NodeSet ind = reasoner + .getObjectPropertyValues(object.asOWLNamedIndividual(), prop); + return ind.containsEntity(subject.asOWLNamedIndividual()); + } + if (goal instanceof OWLClassAssertionAxiom) { + OWLClassAssertionAxiom g = (OWLClassAssertionAxiom) goal; + OWLClassExpression cls = g.getClassExpression(); + OWLIndividual indi = g.getIndividual(); + NodeSet n = + reasoner.getTypes(indi.asOWLNamedIndividual(), true); + return n.containsEntity(cls.asOWLClass()); + } + if (goal instanceof OWLDataPropertyAssertionAxiom) { + OWLDataPropertyAssertionAxiom g = + (OWLDataPropertyAssertionAxiom) goal; + OWLDataProperty prop = g.getProperty().asOWLDataProperty(); + OWLLiteral obj = g.getObject(); + OWLIndividual sub = g.getSubject(); + Set litset = reasoner + .getDataPropertyValues(sub.asOWLNamedIndividual(), prop); + return litset.contains(obj); + } + if (goal instanceof OWLEquivalentClassesAxiom) { + OWLEquivalentClassesAxiom g = (OWLEquivalentClassesAxiom) goal; + OWLClassExpression[] cls = g.getClassExpressions() + .toArray(new OWLClassExpression[g.getClassExpressions().size()]); + boolean res = true; + for (int i = 0; i < cls.length; i++ ) { + for (int j = 0; j < cls.length; j++ ) { + if (i != j) { + Node c = + reasoner.getEquivalentClasses(cls[i]); + res &= c.contains(cls[j].asOWLClass()); + } + } + } + return res; + } + if (goal instanceof OWLSubClassOfAxiom) { + OWLSubClassOfAxiom g = (OWLSubClassOfAxiom) goal; + OWLClassExpression sub = g.getSubClass(); + OWLClassExpression top = g.getSuperClass(); + NodeSet c = reasoner.getSubClasses(top, true); + return c.containsEntity(sub.asOWLClass()); + } + if (goal instanceof OWLDataPropertyRangeAxiom) { + throw new FaCTPlusPlusException("Fact++ cannot determine data ranges!"); + } + if (goal instanceof OWLDataPropertyDomainAxiom) { + OWLDataPropertyDomainAxiom g = (OWLDataPropertyDomainAxiom) goal; + OWLDataProperty prop = g.getProperty().asOWLDataProperty(); + OWLClassExpression d = g.getDomain(); + boolean res = false; + NodeSet dom = reasoner.getDataPropertyDomains(prop, true); + Iterator> it = dom.iterator(); + while (it.hasNext()) { + Node cur = it.next(); + Iterator itt = cur.iterator(); + while (itt.hasNext()) { + OWLClass c = itt.next(); + Node cl = reasoner.getEquivalentClasses(c); + if (cl.contains(d.asOWLClass())) + res = true; + } + } + return res; + } + if (goal instanceof OWLSubDataPropertyOfAxiom) { + OWLSubDataPropertyOfAxiom g = (OWLSubDataPropertyOfAxiom) goal; + OWLDataProperty sub = g.getSubProperty().asOWLDataProperty(); + OWLDataProperty sup = g.getSuperProperty().asOWLDataProperty(); + boolean res = false; + NodeSet sups = + reasoner.getSuperDataProperties(sub, true); + Iterator> it = sups.iterator(); + while (it.hasNext()) { + Node cur = it.next(); + if (cur.contains(sup)) res = true; } - } - - return res; - } - if (goal instanceof OWLSubDataPropertyOfAxiom) - { - OWLSubDataPropertyOfAxiom g = (OWLSubDataPropertyOfAxiom) goal; - OWLDataProperty sub = g.getSubProperty().asOWLDataProperty(); - OWLDataProperty sup = g.getSuperProperty().asOWLDataProperty(); - boolean res = false; - - NodeSet sups = reasoner.getSuperDataProperties(sub,true); - Iterator> it = sups.iterator(); - while (it.hasNext()) - { - Node cur = it.next(); - if (cur.contains(sup)) - res = true; - } - return res; - } - if (goal instanceof OWLAsymmetricObjectPropertyAxiom) - { - throw new FaCTPlusPlusException - ("Fact++ cannot determine antisymmetry!"); - } - - if (goal instanceof OWLAnnotationAssertionAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLAnnotationAssertionAxiom!"); - } - - if (goal instanceof OWLAnnotationAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLAnnotationAxiom!"); - } - - if (goal instanceof OWLAnnotationPropertyDomainAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLAnnotationPropertyDomainAxiom!"); - } - - if (goal instanceof OWLAnnotationPropertyRangeAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLAnnotationPropertyRangeAxiom!"); - } - - if (goal instanceof OWLClassAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLClassAxiom!"); - } - - if (goal instanceof OWLDataPropertyAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLDataPropertyAxiom!"); - } - - if (goal instanceof OWLDataPropertyCharacteristicAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLDataPropertyCharacteristicAxiom!"); - } - - if (goal instanceof OWLDatatypeDefinitionAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLDatatypeDefinitionAxiom!"); - } - - if (goal instanceof OWLDeclarationAxiom) - { - return true; - } - - if (goal instanceof OWLDifferentIndividualsAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLDifferentIndividualsAxiom!"); - } - - if (goal instanceof OWLDisjointClassesAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLDisjointClassesAxiom!"); - } - - if (goal instanceof OWLDisjointDataPropertiesAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLDisjointDataPropertiesAxiom!"); - } - - if (goal instanceof OWLDisjointObjectPropertiesAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLDisjointObjectPropertiesAxiom!"); - } - - if (goal instanceof OWLDisjointUnionAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLDisjointUnionAxiom!"); - } - - if (goal instanceof OWLIndividualAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLIndividualAxiom!"); - } - - if (goal instanceof OWLLogicalAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLLogicalAxiom!"); - } - - if (goal instanceof OWLNaryAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLNaryAxiom!"); - } - - if (goal instanceof OWLNaryClassAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLNaryClassAxiom!"); - } - - if (goal instanceof OWLNaryIndividualAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLNaryIndividualAxiom!"); - } - - if (goal instanceof OWLNaryPropertyAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLNaryPropertyAxiom!"); - } - - if (goal instanceof OWLObjectPropertyAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLObjectPropertyAxiom!"); - } - - if (goal instanceof OWLObjectPropertyCharacteristicAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLObjectPropertyCharacteristicAxiom!"); - } - - if (goal instanceof OWLPropertyAssertionAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLPropertyAssertionAxiom!"); - } - - if (goal instanceof OWLPropertyAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLPropertyAxiom!"); - } - - if (goal instanceof OWLPropertyDomainAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLPropertyDomainAxiom!"); - } - - if (goal instanceof OWLPropertyRangeAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLPropertyRangeAxiom!"); - } - - if (goal instanceof OWLSameIndividualAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLSameIndividualAxiom!"); - } - - if (goal instanceof OWLSubAnnotationPropertyOfAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLSubAnnotationPropertyOfAxiom!"); - } - - if (goal instanceof OWLSubPropertyAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLSubPropertyAxiom!"); - } - - if (goal instanceof OWLSubPropertyChainOfAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLSubPropertyChainOfAxiom!"); - } - - if (goal instanceof OWLUnaryPropertyAxiom) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process OWLUnaryPropertyAxiom!"); - } - - if (goal instanceof SWRLRule) - { - throw new FaCTPlusPlusException - ("FaCT++ cannot process SWRLRule!"); - } - - if (goal instanceof OWLEquivalentDataPropertiesAxiom) - { - OWLEquivalentDataPropertiesAxiom g = - (OWLEquivalentDataPropertiesAxiom) goal; - Set props = g.getProperties(); - TreeSet p = new TreeSet(); - Iterator it = props.iterator(); - p.clear(); - while (it.hasNext()) - { - p.add(it.next().asOWLDataProperty()); - } - boolean res = true; - Iterator itt = p.iterator(); - while (itt.hasNext()) - { - Node cur = reasoner.getEquivalentDataProperties(itt.next()); - Set scur = cur.getEntities(); - res &= scur.containsAll(props); - } - return res; - } - if (goal instanceof OWLEquivalentObjectPropertiesAxiom) - { - OWLEquivalentObjectPropertiesAxiom g = (OWLEquivalentObjectPropertiesAxiom) goal; - Set props = g.getProperties(); - TreeSet p = new TreeSet(); - Iterator it = props.iterator(); - p.clear(); - while (it.hasNext()) - { - p.add(it.next()); - } - boolean res = true; - Iterator itt = p.iterator(); - while (itt.hasNext()) - { - Node cur = reasoner.getEquivalentObjectProperties(itt.next()); - Set scur = cur.getEntities(); - res &= scur.containsAll(props); - } - return res; - } - if (goal instanceof OWLFunctionalDataPropertyAxiom) - { - OWLFunctionalDataPropertyAxiom g = (OWLFunctionalDataPropertyAxiom) goal; - OWLDataProperty prop = g.getProperty().asOWLDataProperty(); - return prop.isFunctional(reasoner.getRootOntology()); - } - if (goal instanceof OWLFunctionalObjectPropertyAxiom) - { - OWLFunctionalObjectPropertyAxiom g= (OWLFunctionalObjectPropertyAxiom) goal; - OWLObjectProperty prop = g.getProperty().asOWLObjectProperty(); - return prop.isFunctional(reasoner.getRootOntology()); - } - if (goal instanceof OWLInverseFunctionalObjectPropertyAxiom) - { - OWLInverseFunctionalObjectPropertyAxiom g = - (OWLInverseFunctionalObjectPropertyAxiom) goal; - OWLObjectProperty prop = g.getProperty().asOWLObjectProperty(); - return prop.isInverseFunctional(reasoner.getRootOntology()); - } - if (goal instanceof OWLInverseObjectPropertiesAxiom) - { - OWLInverseObjectPropertiesAxiom g = (OWLInverseObjectPropertiesAxiom) goal; - OWLObjectPropertyExpression fst = g.getFirstProperty(); - OWLObjectPropertyExpression snd = g.getSecondProperty(); - Node is = reasoner.getInverseObjectProperties(fst); - boolean res = false; - if (is.contains(snd)) {res=true;} - return res; - } - if (goal instanceof OWLIrreflexiveObjectPropertyAxiom) - { - OWLIrreflexiveObjectPropertyAxiom g = (OWLIrreflexiveObjectPropertyAxiom) goal; - return g.getProperty().asOWLObjectProperty().isIrreflexive(reasoner.getRootOntology()); - } - if (goal instanceof OWLNegativeDataPropertyAssertionAxiom) - { - OWLNegativeDataPropertyAssertionAxiom g = (OWLNegativeDataPropertyAssertionAxiom) goal; - OWLIndividual sub = g.getSubject(); - OWLLiteral obj = g.getObject(); - OWLDataProperty p = g.getProperty().asOWLDataProperty(); - Set slit = reasoner.getDataPropertyValues(sub.asOWLNamedIndividual(),p); - return slit.contains(obj); - } - if (goal instanceof OWLNegativeObjectPropertyAssertionAxiom) - { - OWLNegativeObjectPropertyAssertionAxiom g = - (OWLNegativeObjectPropertyAssertionAxiom) goal; - OWLIndividual sub = g.getSubject(); - OWLIndividual obj = g.getObject(); - OWLObjectProperty p = g.getProperty().asOWLObjectProperty(); - - NodeSet ind = reasoner.getObjectPropertyValues (obj.asOWLNamedIndividual(),p); - - return ind.containsEntity(sub.asOWLNamedIndividual()); - - } - if (goal instanceof OWLObjectPropertyAssertionAxiom) - { - OWLObjectPropertyAssertionAxiom g = - (OWLObjectPropertyAssertionAxiom) goal; - OWLIndividual sub = g.getSubject(); - OWLIndividual obj = g.getObject(); - OWLObjectProperty p = g.getProperty().asOWLObjectProperty(); - - NodeSet ind = reasoner.getObjectPropertyValues(obj.asOWLNamedIndividual(),p); - - return ind.containsEntity(sub.asOWLNamedIndividual()); - } - if (goal instanceof OWLObjectPropertyDomainAxiom) - { - OWLObjectPropertyDomainAxiom g = (OWLObjectPropertyDomainAxiom) goal; - OWLObjectPropertyExpression prop = g.getProperty(); - OWLClassExpression dom = g.getDomain(); - NodeSet doms = reasoner.getObjectPropertyDomains(prop,true); - boolean res = false; - Iterator> it = doms.iterator(); - while (it.hasNext()) - { - Node cur = it.next(); - if (cur.contains(dom.asOWLClass())){res = true;} - } - return res; - } - if (goal instanceof OWLObjectPropertyRangeAxiom) - { - OWLObjectPropertyRangeAxiom g = (OWLObjectPropertyRangeAxiom) goal; - OWLObjectPropertyExpression prop = g.getProperty(); - OWLClass ran = g.getRange().asOWLClass(); - NodeSet rns = reasoner.getObjectPropertyRanges(prop,true); - return rns.containsEntity(ran); - } - if (goal instanceof OWLSubObjectPropertyOfAxiom) - { - OWLSubObjectPropertyOfAxiom g = - (OWLSubObjectPropertyOfAxiom) goal; - OWLObjectPropertyExpression sub = g.getSubProperty(); - OWLObjectPropertyExpression sup = g.getSuperProperty(); - NodeSet subs = reasoner.getSubObjectProperties(sup,true); - boolean res = false; - Iterator> it = subs.iterator(); - while (it.hasNext()) - { - Node cur = it.next(); - if (cur.contains(sub)) {res = true;} - } - return res; - } - if (goal instanceof OWLReflexiveObjectPropertyAxiom) - { - OWLReflexiveObjectPropertyAxiom g = (OWLReflexiveObjectPropertyAxiom) goal; - OWLObjectProperty prop = g.getProperty().asOWLObjectProperty(); - return prop.isReflexive(reasoner.getRootOntology()); - } - if (goal instanceof OWLSymmetricObjectPropertyAxiom) - { - OWLSymmetricObjectPropertyAxiom g = (OWLSymmetricObjectPropertyAxiom) goal; - OWLObjectProperty prop = g.getProperty().asOWLObjectProperty(); - return prop.isSymmetric(reasoner.getRootOntology()); - } - if (goal instanceof OWLTransitiveObjectPropertyAxiom) - { - OWLTransitiveObjectPropertyAxiom g = - (OWLTransitiveObjectPropertyAxiom) goal; - OWLObjectProperty prop = g.getProperty().asOWLObjectProperty(); - return prop.isTransitive(reasoner.getRootOntology()); - } - System.out.println(goal); - throw new FaCTPlusPlusException - ("No reasoner found, sorry!"); - } + return res; + } + if (goal instanceof OWLAsymmetricObjectPropertyAxiom) { + throw new FaCTPlusPlusException("Fact++ cannot determine antisymmetry!"); + } + if (goal instanceof OWLAnnotationAssertionAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLAnnotationAssertionAxiom!"); + } + if (goal instanceof OWLAnnotationAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLAnnotationAxiom!"); + } + if (goal instanceof OWLAnnotationPropertyDomainAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLAnnotationPropertyDomainAxiom!"); + } + if (goal instanceof OWLAnnotationPropertyRangeAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLAnnotationPropertyRangeAxiom!"); + } + if (goal instanceof OWLClassAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLClassAxiom!"); + } + if (goal instanceof OWLDataPropertyAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLDataPropertyAxiom!"); + } + if (goal instanceof OWLDataPropertyCharacteristicAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLDataPropertyCharacteristicAxiom!"); + } + if (goal instanceof OWLDatatypeDefinitionAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLDatatypeDefinitionAxiom!"); + } + if (goal instanceof OWLDeclarationAxiom) { + return true; + } + if (goal instanceof OWLDifferentIndividualsAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLDifferentIndividualsAxiom!"); + } + if (goal instanceof OWLDisjointClassesAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLDisjointClassesAxiom!"); + } + if (goal instanceof OWLDisjointDataPropertiesAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLDisjointDataPropertiesAxiom!"); + } + if (goal instanceof OWLDisjointObjectPropertiesAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLDisjointObjectPropertiesAxiom!"); + } + if (goal instanceof OWLDisjointUnionAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLDisjointUnionAxiom!"); + } + if (goal instanceof OWLIndividualAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLIndividualAxiom!"); + } + if (goal instanceof OWLLogicalAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLLogicalAxiom!"); + } + if (goal instanceof OWLNaryAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLNaryAxiom!"); + } + if (goal instanceof OWLNaryClassAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLNaryClassAxiom!"); + } + if (goal instanceof OWLNaryIndividualAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLNaryIndividualAxiom!"); + } + if (goal instanceof OWLNaryPropertyAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLNaryPropertyAxiom!"); + } + if (goal instanceof OWLObjectPropertyAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLObjectPropertyAxiom!"); + } + if (goal instanceof OWLObjectPropertyCharacteristicAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLObjectPropertyCharacteristicAxiom!"); + } + if (goal instanceof OWLPropertyAssertionAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLPropertyAssertionAxiom!"); + } + if (goal instanceof OWLPropertyAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLPropertyAxiom!"); + } + if (goal instanceof OWLPropertyDomainAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLPropertyDomainAxiom!"); + } + if (goal instanceof OWLPropertyRangeAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLPropertyRangeAxiom!"); + } + if (goal instanceof OWLSameIndividualAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLSameIndividualAxiom!"); + } + if (goal instanceof OWLSubAnnotationPropertyOfAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLSubAnnotationPropertyOfAxiom!"); + } + if (goal instanceof OWLSubPropertyAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLSubPropertyAxiom!"); + } + if (goal instanceof OWLSubPropertyChainOfAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLSubPropertyChainOfAxiom!"); + } + if (goal instanceof OWLUnaryPropertyAxiom) { + throw new FaCTPlusPlusException("FaCT++ cannot process OWLUnaryPropertyAxiom!"); + } + if (goal instanceof SWRLRule) { + throw new FaCTPlusPlusException("FaCT++ cannot process SWRLRule!"); + } + if (goal instanceof OWLEquivalentDataPropertiesAxiom) { + OWLEquivalentDataPropertiesAxiom g = + (OWLEquivalentDataPropertiesAxiom) goal; + Set props = g.getProperties(); + TreeSet p = new TreeSet(); + Iterator it = props.iterator(); + p.clear(); + while (it.hasNext()) { + p.add(it.next().asOWLDataProperty()); + } + boolean res = true; + Iterator itt = p.iterator(); + while (itt.hasNext()) { + Node cur = + reasoner.getEquivalentDataProperties(itt.next()); + Set scur = cur.getEntities(); + res &= scur.containsAll(props); + } + return res; + } + if (goal instanceof OWLEquivalentObjectPropertiesAxiom) { + OWLEquivalentObjectPropertiesAxiom g = + (OWLEquivalentObjectPropertiesAxiom) goal; + Set props = g.getProperties(); + TreeSet p = + new TreeSet(); + Iterator it = props.iterator(); + p.clear(); + while (it.hasNext()) { + p.add(it.next()); + } + boolean res = true; + Iterator itt = p.iterator(); + while (itt.hasNext()) { + Node cur = + reasoner.getEquivalentObjectProperties(itt.next()); + Set scur = cur.getEntities(); + res &= scur.containsAll(props); + } + return res; + } + if (goal instanceof OWLFunctionalDataPropertyAxiom) { + OWLFunctionalDataPropertyAxiom g = + (OWLFunctionalDataPropertyAxiom) goal; + OWLDataProperty prop = g.getProperty().asOWLDataProperty(); + return prop.isFunctional(reasoner.getRootOntology()); + } + if (goal instanceof OWLFunctionalObjectPropertyAxiom) { + OWLFunctionalObjectPropertyAxiom g = + (OWLFunctionalObjectPropertyAxiom) goal; + OWLObjectProperty prop = g.getProperty().asOWLObjectProperty(); + return prop.isFunctional(reasoner.getRootOntology()); + } + if (goal instanceof OWLInverseFunctionalObjectPropertyAxiom) { + OWLInverseFunctionalObjectPropertyAxiom g = + (OWLInverseFunctionalObjectPropertyAxiom) goal; + OWLObjectProperty prop = g.getProperty().asOWLObjectProperty(); + return prop.isInverseFunctional(reasoner.getRootOntology()); + } + if (goal instanceof OWLInverseObjectPropertiesAxiom) { + OWLInverseObjectPropertiesAxiom g = + (OWLInverseObjectPropertiesAxiom) goal; + OWLObjectPropertyExpression fst = g.getFirstProperty(); + OWLObjectPropertyExpression snd = g.getSecondProperty(); + Node is = + reasoner.getInverseObjectProperties(fst); + boolean res = false; + if (is.contains(snd)) { + res = true; + } + return res; + } + if (goal instanceof OWLIrreflexiveObjectPropertyAxiom) { + OWLIrreflexiveObjectPropertyAxiom g = + (OWLIrreflexiveObjectPropertyAxiom) goal; + return g.getProperty().asOWLObjectProperty() + .isIrreflexive(reasoner.getRootOntology()); + } + if (goal instanceof OWLNegativeDataPropertyAssertionAxiom) { + OWLNegativeDataPropertyAssertionAxiom g = + (OWLNegativeDataPropertyAssertionAxiom) goal; + OWLIndividual sub = g.getSubject(); + OWLLiteral obj = g.getObject(); + OWLDataProperty p = g.getProperty().asOWLDataProperty(); + Set slit = + reasoner.getDataPropertyValues(sub.asOWLNamedIndividual(), p); + return slit.contains(obj); + } + if (goal instanceof OWLNegativeObjectPropertyAssertionAxiom) { + OWLNegativeObjectPropertyAssertionAxiom g = + (OWLNegativeObjectPropertyAssertionAxiom) goal; + OWLIndividual sub = g.getSubject(); + OWLIndividual obj = g.getObject(); + OWLObjectProperty p = g.getProperty().asOWLObjectProperty(); + NodeSet ind = + reasoner.getObjectPropertyValues(obj.asOWLNamedIndividual(), p); + return ind.containsEntity(sub.asOWLNamedIndividual()); + } + if (goal instanceof OWLObjectPropertyAssertionAxiom) { + OWLObjectPropertyAssertionAxiom g = + (OWLObjectPropertyAssertionAxiom) goal; + OWLIndividual sub = g.getSubject(); + OWLIndividual obj = g.getObject(); + OWLObjectProperty p = g.getProperty().asOWLObjectProperty(); + NodeSet ind = + reasoner.getObjectPropertyValues(obj.asOWLNamedIndividual(), p); + return ind.containsEntity(sub.asOWLNamedIndividual()); + } + if (goal instanceof OWLObjectPropertyDomainAxiom) { + OWLObjectPropertyDomainAxiom g = + (OWLObjectPropertyDomainAxiom) goal; + OWLObjectPropertyExpression prop = g.getProperty(); + OWLClassExpression dom = g.getDomain(); + NodeSet doms = + reasoner.getObjectPropertyDomains(prop, true); + boolean res = false; + Iterator> it = doms.iterator(); + while (it.hasNext()) { + Node cur = it.next(); + if (cur.contains(dom.asOWLClass())) { + res = true; + } + } + return res; + } + if (goal instanceof OWLObjectPropertyRangeAxiom) { + OWLObjectPropertyRangeAxiom g = (OWLObjectPropertyRangeAxiom) goal; + OWLObjectPropertyExpression prop = g.getProperty(); + OWLClass ran = g.getRange().asOWLClass(); + NodeSet rns = + reasoner.getObjectPropertyRanges(prop, true); + return rns.containsEntity(ran); + } + if (goal instanceof OWLSubObjectPropertyOfAxiom) { + OWLSubObjectPropertyOfAxiom g = (OWLSubObjectPropertyOfAxiom) goal; + OWLObjectPropertyExpression sub = g.getSubProperty(); + OWLObjectPropertyExpression sup = g.getSuperProperty(); + NodeSet subs = + reasoner.getSubObjectProperties(sup, true); + boolean res = false; + Iterator> it = subs.iterator(); + while (it.hasNext()) { + Node cur = it.next(); + if (cur.contains(sub)) { + res = true; + } + } + return res; + } + if (goal instanceof OWLReflexiveObjectPropertyAxiom) { + OWLReflexiveObjectPropertyAxiom g = + (OWLReflexiveObjectPropertyAxiom) goal; + OWLObjectProperty prop = g.getProperty().asOWLObjectProperty(); + return prop.isReflexive(reasoner.getRootOntology()); + } + if (goal instanceof OWLSymmetricObjectPropertyAxiom) { + OWLSymmetricObjectPropertyAxiom g = + (OWLSymmetricObjectPropertyAxiom) goal; + OWLObjectProperty prop = g.getProperty().asOWLObjectProperty(); + return prop.isSymmetric(reasoner.getRootOntology()); + } + if (goal instanceof OWLTransitiveObjectPropertyAxiom) { + OWLTransitiveObjectPropertyAxiom g = + (OWLTransitiveObjectPropertyAxiom) goal; + OWLObjectProperty prop = g.getProperty().asOWLObjectProperty(); + return prop.isTransitive(reasoner.getRootOntology()); + } + System.out.println(goal); + throw new FaCTPlusPlusException("No reasoner found, sorry!"); + } } diff --git a/OWL2/java/de/unibremen/informatik/FactProver/Prove.java b/OWL2/java/de/unibremen/informatik/FactProver/Prove.java index 91aea5d462..746f40dc42 100644 --- a/OWL2/java/de/unibremen/informatik/FactProver/Prove.java +++ b/OWL2/java/de/unibremen/informatik/FactProver/Prove.java @@ -1,43 +1,33 @@ package de.unibremen.informatik.FactProver; -import java.net.URI; import org.semanticweb.owlapi.model.IRI; public class Prove { - public static void main (String[] args) - { - if (args.length < 2) - { - System.out.println("owl_fact_prover "); - System.exit(1); - } - - try - { + public static void main(String[] args) { + if (args.length < 2) { + System.out.println("owl_fact_prover "); + System.exit(1); + } + try { IRI physicalIRI = IRI.create(args[0]); - IRI goalIRI = IRI.create(args[1]); + IRI goalIRI = IRI.create(args[1]); FactProver prover = new FactProver(physicalIRI); System.out.println("FactProver: " + prover + "\n"); System.out.println("PhysicalIRI: " + physicalIRI + "\n"); System.out.println("GoalIRI: " + goalIRI + "\n"); - prover.loadGoal(goalIRI); - boolean res = prover.prove(); - if (res) - { - System.out.println("proved"); - System.exit(10); - } - else - { - System.out.println("disproved"); - System.exit(20); - } - } - catch (Exception e) - { + prover.loadGoal(goalIRI); + boolean res = prover.prove(); + if (res) { + System.out.println("proved"); + System.exit(10); + } else { + System.out.println("disproved"); + System.exit(20); + } + } catch (Exception e) { System.out.println(e.getMessage()); - System.exit(1); + System.exit(1); } } } diff --git a/OWL2/java/de/unibremen/informatik/OWL2Parser.java b/OWL2/java/de/unibremen/informatik/OWL2Parser.java index d7e9730246..817986ea11 100644 --- a/OWL2/java/de/unibremen/informatik/OWL2Parser.java +++ b/OWL2/java/de/unibremen/informatik/OWL2Parser.java @@ -1,4 +1,3 @@ - import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; @@ -32,346 +31,344 @@ public class OWL2Parser { - private static enum OPTION { - OWL_XML, MANCHESTER, RDF_XML, OBO, TURTLE - } - - private static Boolean quick = false; - private static Boolean cyclic = false; - protected static final Set missingImports = new HashSet(); - private static Set ontologies; - private static final Set exported = new HashSet(); - private static OWLOntologyManager manager = setupManagerWithMissingImportListener(); - private static OWLOntologyIRIMapperImpl mapper = new OWLOntologyIRIMapperImpl(); - private static URI uri = null; - - public static void main(String[] args) { - // A simple example of how to load and save an ontology - try { - OWLOutputHandler out = new OWLOutputHandler(); - parseArgs(args, out); - URLConnection con = uri.toURL().openConnection(); - con.addRequestProperty("Accept", "text/plain"); - StreamDocumentSource sds = new StreamDocumentSource( - con.getInputStream(), IRI.create(uri)); - OWLOntologyLoaderConfiguration config = new OWLOntologyLoaderConfiguration(); - config = config - .setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT); - OWLOntology ontology = manager.loadOntologyFromOntologyDocument( - sds, config); - if (!missingImports.isEmpty()) { - IRI ontohub = IRI.create("https://ontohub.org/external/"); - for (IRI mi : missingImports) { - mapper.addMapping( - mi, - ontohub.resolve(mi.toURI().getHost() - + mi.toURI().getPath())); - } - // reset the manager. clear out imports to avoid duplicates - manager = setupManagerWithMissingImportListener(); - manager.addIRIMapper(mapper); - // collect missing imports again to report them in output file. - missingImports.clear(); - ontology = manager - .loadOntologyFromOntologyDocument(sds, config); - } - ontologies = getImports(ontology, new HashSet()); - out._open(); - if (cyclic) { - String str = ontology.getOntologyID().getOntologyIRI() - .toString(); - String merged_name = str + ".merged.owl"; // we must make a new - // name! - // System.out.println("NAME: " + merged_name + "\n"); - IRI mergedOntologyIRI = IRI.create(merged_name); - // System.out.println("MERGED_IRI " + mergedOntologyIRI + "\n"); - OWLOntology merged; - // Axioms can be excluded when 'quick' Option selected - if (quick) { - merged = manager.createOntology(mergedOntologyIRI); - } else { - OWLOntologyMerger merger = new OWLOntologyMerger(manager); - merged = merger.createMergedOntology(manager, - mergedOntologyIRI); - } - out.renderUsingOption(merged); - } else { - ontologies.add(ontology); - exportImports(out); - } - out._close(); - } catch (Exception ex) { - System.err.println("OWL parse error: " + ex.getMessage()); - ex.printStackTrace(); - } - } - - // print usage information screen - private static void showHelpScreen() { - System.out.println("Usage: processor [] \n" - + "_>_options_<______________\n" - + " | -o ..write output to file\n" - + " - - - tp <- type [: owl xml omn rdf obo dol ttl]\n" - + " - - - fn <- filename\n" - + " | -o-sys ..write output to system.out\n" - + " - - - - - tp <- type\n" - + " | -qk ..internal(!) sets 'quick' option\n" - + " | -h ..this helptext"); - } - - // parse arguments according to option set - // fails for unknown or incomplete arguments, or when IRI is not set - private static void parseArgs(String[] args, OWLOutputHandler out) - throws Exception { - String inp = "", msg = "OWL2Parser.parseArgs: "; - if (args.length == 0) { - throw new Exception(msg + "no arguments provided"); - } - for (int i = 0; i < args.length; i++) { - String arg = args[i].toLowerCase(); - if (arg.startsWith("-")) { - if (arg.equals("-o")) { - if (!(i < args.length - 2)) { - throw new Exception( - msg - + "insufficient arguments (-o [format] [filename])"); - } - out.add(parseOption(args[++i].toLowerCase(), msg), - args[++i]); - } else if (arg.equals("-o-sys")) { - if (!(i < args.length - 1)) { - throw new Exception(msg - + "insufficient arguments (-o-sys [format])"); - } - out.add(parseOption(args[++i].toLowerCase(), msg)); - } else if (arg.equals("-qk") || arg.equals("-q")) { - quick = true; - } else if (arg.equals("-h") || arg.equals("--help")) { - showHelpScreen(); - } else { - throw new Exception(msg + "unknown command <" + arg + ">"); - } - } else { - if (!inp.equals("")) { - throw new Exception(msg + "ambigious IRI"); - } - inp = args[i]; // read again to undo earlier 'toLowerCase' - if (arg.startsWith("http:") || arg.startsWith("https:")) { - uri = new URI(inp); - } else { - uri = new File(inp).toURI(); - } - } - } - if (inp.equals("")) { - throw new Exception(msg + "IRI not set"); - } - } - - private static OPTION parseOption(String opt, String err) throws Exception { - if (opt.equals("xml") || opt.equals("owl")) { - return OPTION.OWL_XML; - } else if (opt.equals("omn")) { - return OPTION.MANCHESTER; - } else if (opt.equals("rdf")) { - return OPTION.RDF_XML; - } else if (opt.equals("obo")) { - return OPTION.OBO; - } else if (opt.equals("ttl")) { - return OPTION.TURTLE; - } - throw new Exception(err + "unrecognized owl-format: " + opt); - } - - private static OWLOntologyManager setupManagerWithMissingImportListener() { - OWLOntologyManager mgr = OWLManager.createOWLOntologyManager(); - mgr.addMissingImportListener(new HasMissingImports()); - return mgr; - } - - private static class HasMissingImports implements MissingImportListener { - - @Override - public void importMissing(MissingImportEvent event) { - missingImports.add(event.getImportedOntologyURI()); - } - } - - private static Set getImports(OWLOntology ontology, - Set stop) { - Set s = new HashSet(); - Set next = new HashSet(stop); - next.add(ontology); - for (OWLOntology imported : ontology.getDirectImports()) { - if (cyclic || next.contains(imported)) { - cyclic = true; - } else if (!s.contains(imported)) { - Set i = getImports(imported, next); - s.add(imported); - s.addAll(i); - } - } - return s; - } - - private static void exportImports(OWLOutputHandler out) throws IOException { - Boolean changed; - do { - changed = false; - for (OWLOntology onto : ontologies) { - if (exported.containsAll(onto.getDirectImports())) { - if (!exported.contains(onto)) { - changed = exported.add(onto); - out.renderUsingOption(onto); - } - } - } - } while (changed); - } - - // output handler allows and handles a list of output-requests - protected static class OWLOutputHandler { - - static ArrayList writer = new ArrayList(); - - void add(OPTION op) throws Exception { - writer.add(new OWLOntologyWriter(op)); - } - - void add(OPTION op, String filename) throws Exception { - writer.add(new OWLOntologyWriter(filename, op)); - } - - void _open() throws IOException { - for (OWLOntologyWriter out : writer) { - out._open(); - } - } - - void _close() throws IOException { - for (OWLOntologyWriter out : writer) { - out._close(); - } - } - - // function need to be called once to render and close every output - void renderUsingOption(OWLOntology onto) throws IOException { - for (OWLOntologyWriter out : writer) { - out.renderUsingOption(onto); - } - } - } - - // custem OntolgyWriter bundles a BufferedWriter with an OWL output format - protected static class OWLOntologyWriter extends BufferedWriter { - - protected static OPTION option; - - OWLOntologyWriter(String fn, OPTION op) throws Exception { - super(new FileWriter(fn)); - option = op; - } - - OWLOntologyWriter(OPTION op) throws Exception { - super(new OutputStreamWriter(System.out)); - option = op; - } - - void writeMissingImports() throws IOException { - for (IRI mi : missingImports) { - write("" + mi + "\n"); - } - } - - void _open() throws IOException { - if (option == OPTION.OWL_XML) { - write("\n"); - } - } - - void _close() throws IOException { - if (option == OPTION.OWL_XML) { - writeMissingImports(); - write("\n\n"); - } - _close(this); - } - - void _close(Writer pointer) throws IOException { - pointer.flush(); - pointer.close(); - } - - void renderUsingOption(OWLOntology onto) throws IOException { - switch (option) { - case OWL_XML: - renderAsXml(onto); - break; - case MANCHESTER: - renderAsOmn(onto); - break; - case RDF_XML: - renderAsRdf(onto); - break; - // TODO: the below still need implementation! - case OBO: - renderAsXml(onto); - break; - case TURTLE: - renderAsXml(onto); - break; - } - } - - void renderAsOmn(OWLOntology onto) { - try { - ManchesterOWLSyntaxRenderer omnrend = new ManchesterOWLSyntaxRenderer(); - omnrend.render(onto, this); - } catch (OWLRendererException ex) { - System.err.println("Error by ManchesterParser!"); - ex.printStackTrace(); - } - } - - void renderAsXml(OWLOntology onto) { - try { - OWLXMLRenderer xmlren = new OWLXMLRenderer(); - File tempFile = File.createTempFile("owlTemp_1", ".xml"); - FileWriter buf = new FileWriter(tempFile); - if (quick) { - onto.getOWLOntologyManager().removeAxioms(onto, - onto.getAxioms()); - } - xmlren.render(onto, buf); - _close(buf); - BufferedReader rBuf = new BufferedReader(new FileReader( - tempFile)); - rBuf.readLine(); // ignore the first line containing - while (rBuf.ready()) { - append(rBuf.readLine()).append("\n"); - } - rBuf.close(); - tempFile.deleteOnExit(); - append("\n"); - } catch (Exception ex) { - System.err.println("Error by XMLParser!"); - ex.printStackTrace(); - } - } - - void renderAsRdf(OWLOntology onto) { - try { - RDFXMLRenderer rdfrend = new RDFXMLRenderer(onto, this); - rdfrend.render(); - } catch (IOException ex) { - System.err.println("Error by RDFParser!"); - ex.printStackTrace(); - } - } - } + private static enum OPTION { + OWL_XML, MANCHESTER, RDF_XML, OBO, TURTLE + } + + private static Boolean quick = false; + private static Boolean cyclic = false; + protected static final Set missingImports = new HashSet(); + private static Set ontologies; + private static final Set exported = new HashSet(); + private static OWLOntologyManager manager = + setupManagerWithMissingImportListener(); + private static OWLOntologyIRIMapperImpl mapper = + new OWLOntologyIRIMapperImpl(); + private static URI uri = null; + + public static void main(String[] args) { + // A simple example of how to load and save an ontology + try { + OWLOutputHandler out = new OWLOutputHandler(); + parseArgs(args, out); + URLConnection con = uri.toURL().openConnection(); + con.addRequestProperty("Accept", "text/plain"); + StreamDocumentSource sds = + new StreamDocumentSource(con.getInputStream(), IRI.create(uri)); + OWLOntologyLoaderConfiguration config = + new OWLOntologyLoaderConfiguration(); + config = config + .setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT); + OWLOntology ontology = + manager.loadOntologyFromOntologyDocument(sds, config); + if ( !missingImports.isEmpty()) { + IRI ontohub = IRI.create("https://ontohub.org/external/"); + for (IRI mi : missingImports) { + mapper.addMapping(mi, ontohub.resolve(mi.toURI().getHost() + + mi.toURI().getPath())); + } + // reset the manager. clear out imports to avoid duplicates + manager = setupManagerWithMissingImportListener(); + manager.addIRIMapper(mapper); + // collect missing imports again to report them in output file. + missingImports.clear(); + ontology = + manager.loadOntologyFromOntologyDocument(sds, config); + } + ontologies = getImports(ontology, new HashSet()); + out._open(); + if (cyclic) { + String str = ontology.getOntologyID().getOntologyIRI().toString(); + // we must make a new name + String merged_name = str + ".merged.owl"; + // System.out.println("NAME: " + merged_name + "\n"); + IRI mergedOntologyIRI = IRI.create(merged_name); + // System.out.println("MERGED_IRI " + mergedOntologyIRI + "\n"); + OWLOntology merged; + // Axioms can be excluded when 'quick' Option selected + if (quick) { + merged = manager.createOntology(mergedOntologyIRI); + } else { + OWLOntologyMerger merger = new OWLOntologyMerger(manager); + merged = + merger.createMergedOntology(manager, mergedOntologyIRI); + } + out.renderUsingOption(merged); + } else { + ontologies.add(ontology); + exportImports(out); + } + out._close(); + } catch (Exception ex) { + System.err.println("OWL parse error: " + ex.getMessage()); + ex.printStackTrace(); + } + } + + // print usage information screen + private static void showHelpScreen() { + System.out.println("Usage: processor [] \n" + + "_>_options_<______________\n" + + " | -o ..write output to file\n" + + " - - - tp <- type [: owl xml omn rdf obo dol ttl]\n" + + " - - - fn <- filename\n" + + " | -o-sys ..write output to system.out\n" + + " - - - - - tp <- type\n" + + " | -qk ..internal(!) sets 'quick' option\n" + + " | -h ..this helptext"); + } + + // parse arguments according to option set + // fails for unknown or incomplete arguments, or when IRI is not set + private static void parseArgs(String[] args, OWLOutputHandler out) + throws Exception + { + String inp = "", msg = "OWL2Parser.parseArgs: "; + if (args.length == 0) { + throw new Exception(msg + "no arguments provided"); + } + for (int i = 0; i < args.length; i++ ) { + String arg = args[i].toLowerCase(); + if (arg.startsWith("-")) { + if (arg.equals("-o")) { + if (!(i < args.length - 2)) { + throw new Exception(msg + + "insufficient arguments (-o [format] [filename])"); + } + out.add(parseOption(args[++i].toLowerCase(), msg), args[++i]); + } else if (arg.equals("-o-sys")) { + if (!(i < args.length - 1)) { + throw new Exception(msg + + "insufficient arguments (-o-sys [format])"); + } + out.add(parseOption(args[ ++i].toLowerCase(), msg)); + } else if (arg.equals("-qk") || arg.equals("-q")) { + quick = true; + } else if (arg.equals("-h") || arg.equals("--help")) { + showHelpScreen(); + } else { + throw new Exception(msg + "unknown command <" + arg + ">"); + } + } else { + if (!inp.equals("")) { + throw new Exception(msg + "ambigious IRI"); + } + inp = args[i]; // read again to undo earlier 'toLowerCase' + if (arg.startsWith("http:") || arg.startsWith("https:")) { + uri = new URI(inp); + } else { + uri = new File(inp).toURI(); + } + } + } + if (inp.equals("")) { + throw new Exception(msg + "IRI not set"); + } + } + + private static OPTION parseOption(String opt, String err) throws Exception { + if (opt.equals("xml") || opt.equals("owl")) { + return OPTION.OWL_XML; + } else if (opt.equals("omn")) { + return OPTION.MANCHESTER; + } else if (opt.equals("rdf")) { + return OPTION.RDF_XML; + } else if (opt.equals("obo")) { + return OPTION.OBO; + } else if (opt.equals("ttl")) { + return OPTION.TURTLE; + } + throw new Exception(err + "unrecognized owl-format: " + opt); + } + + private static OWLOntologyManager setupManagerWithMissingImportListener() { + OWLOntologyManager mgr = OWLManager.createOWLOntologyManager(); + mgr.addMissingImportListener(new HasMissingImports()); + return mgr; + } + + private static class HasMissingImports implements MissingImportListener { + @Override + public void importMissing(MissingImportEvent event) { + missingImports.add(event.getImportedOntologyURI()); + } + } + + private static Set getImports(OWLOntology ontology, + Set stop) + { + Set s = new HashSet(); + Set next = new HashSet(stop); + next.add(ontology); + for (OWLOntology imported : ontology.getDirectImports()) { + if (cyclic || next.contains(imported)) { + cyclic = true; + } else if (!s.contains(imported)) { + Set i = getImports(imported, next); + s.add(imported); + s.addAll(i); + } + } + return s; + } + + private static void exportImports(OWLOutputHandler out) throws IOException { + Boolean changed; + do { + changed = false; + for (OWLOntology onto : ontologies) { + if (exported.containsAll(onto.getDirectImports())) { + if (!exported.contains(onto)) { + changed = exported.add(onto); + out.renderUsingOption(onto); + } + } + } + } while (changed); + } + + // output handler allows and handles a list of output-requests + protected static class OWLOutputHandler { + static ArrayList writer = + new ArrayList(); + + void add(OPTION op) throws Exception { + writer.add(new OWLOntologyWriter(op)); + } + + void add(OPTION op, String filename) throws Exception { + writer.add(new OWLOntologyWriter(filename, op)); + } + + void _open() throws IOException { + for (OWLOntologyWriter out : writer) { + out._open(); + } + } + + void _close() throws IOException { + for (OWLOntologyWriter out : writer) { + out._close(); + } + } + + // function need to be called once to render and close every output + void renderUsingOption(OWLOntology onto) throws IOException { + for (OWLOntologyWriter out : writer) { + out.renderUsingOption(onto); + } + } + } + + // custem OntolgyWriter bundles a BufferedWriter with an OWL output format + protected static class OWLOntologyWriter extends BufferedWriter { + protected static OPTION option; + + OWLOntologyWriter(String fn, OPTION op) throws Exception { + super(new FileWriter(fn)); + option = op; + } + + OWLOntologyWriter(OPTION op) throws Exception { + super(new OutputStreamWriter(System.out)); + option = op; + } + + void writeMissingImports() throws IOException { + for (IRI mi : missingImports) { + write("" + mi + "\n"); + } + } + + void _open() throws IOException { + if (option == OPTION.OWL_XML) { + write("\n"); + } + } + + void _close() throws IOException { + if (option == OPTION.OWL_XML) { + writeMissingImports(); + write("\n\n"); + } + _close(this); + } + + void _close(Writer pointer) throws IOException { + pointer.flush(); + pointer.close(); + } + + void renderUsingOption(OWLOntology onto) throws IOException { + switch (option) { + case OWL_XML: + renderAsXml(onto); + break; + case MANCHESTER: + renderAsOmn(onto); + break; + case RDF_XML: + renderAsRdf(onto); + break; + // TODO: the below still need implementation! + case OBO: + renderAsXml(onto); + break; + case TURTLE: + renderAsXml(onto); + break; + } + } + + void renderAsOmn(OWLOntology onto) { + try { + ManchesterOWLSyntaxRenderer omnrend = + new ManchesterOWLSyntaxRenderer(); + omnrend.render(onto, this); + } catch (OWLRendererException ex) { + System.err.println("Error by ManchesterParser!"); + ex.printStackTrace(); + } + } + + void renderAsXml(OWLOntology onto) { + try { + OWLXMLRenderer xmlren = new OWLXMLRenderer(); + File tempFile = File.createTempFile("owlTemp_1", ".xml"); + FileWriter buf = new FileWriter(tempFile); + if (quick) { + onto.getOWLOntologyManager() + .removeAxioms(onto,onto.getAxioms()); + } + xmlren.render(onto, buf); + _close(buf); + BufferedReader rBuf = + new BufferedReader(new FileReader(tempFile)); + rBuf.readLine(); // ignore the first line containing + while (rBuf.ready()) { + append(rBuf.readLine()).append("\n"); + } + rBuf.close(); + tempFile.deleteOnExit(); + append("\n"); + } catch (Exception ex) { + System.err.println("Error by XMLParser!"); + ex.printStackTrace(); + } + } + + void renderAsRdf(OWLOntology onto) { + try { + RDFXMLRenderer rdfrend = new RDFXMLRenderer(onto, this); + rdfrend.render(); + } catch (IOException ex) { + System.err.println("Error by RDFParser!"); + ex.printStackTrace(); + } + } + } } diff --git a/OWL2/java/de/unibremen/informatik/locality/LocalityChecker.java b/OWL2/java/de/unibremen/informatik/locality/LocalityChecker.java index d86da51d6b..0fb32c9205 100644 --- a/OWL2/java/de/unibremen/informatik/locality/LocalityChecker.java +++ b/OWL2/java/de/unibremen/informatik/locality/LocalityChecker.java @@ -1,130 +1,104 @@ package de.unibremen.informatik.locality; -import com.clarkparsia.owlapi.modularity.locality.LocalityClass; -import com.clarkparsia.owlapi.modularity.locality.LocalityEvaluator; -import com.clarkparsia.owlapi.modularity.locality.SyntacticLocalityEvaluator; - -import org.semanticweb.owlapi.apibinding.OWLManager; -import org.semanticweb.owlapi.io.OWLXMLOntologyFormat; -import org.semanticweb.owlapi.model.*; - -import java.net.URI; - -import java.util.Set; import java.util.Iterator; +import java.util.Set; +import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.io.ToStringRenderer; +import org.semanticweb.owlapi.model.IRI; +import org.semanticweb.owlapi.model.OWLAxiom; +import org.semanticweb.owlapi.model.OWLEntity; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; -public class LocalityChecker { - - private static Set sign; - private static Set axioms; - private static ToStringRenderer out; - - public static void main(String[] args) - { - if (args.length != 3) - { - System.out.println("Usage: LocalityChecker " + - " "); - System.exit(1); - } - - LocalityClass cl = com.clarkparsia.owlapi.modularity.locality.LocalityClass.BOTTOM_BOTTOM; +import com.clarkparsia.owlapi.modularity.locality.LocalityClass; +import com.clarkparsia.owlapi.modularity.locality.SyntacticLocalityEvaluator; - if (args[2].equals("TOP_BOTTOM")) - { - cl = com.clarkparsia.owlapi.modularity.locality.LocalityClass.TOP_BOTTOM; - } - else if (args[2].equals("TOP_TOP")) - { - cl = com.clarkparsia.owlapi.modularity.locality.LocalityClass.TOP_TOP; - } - else - { - cl = com.clarkparsia.owlapi.modularity.locality.LocalityClass.BOTTOM_BOTTOM; - } +public class LocalityChecker { + private static Set sign; + private static Set axioms; + private static ToStringRenderer out; - try - { - loader(args[0], args[1]); - //print(); - boolean local = checker(cl); - System.out.print("Result: "); - if (local) - { - System.out.println("LOCAL"); - System.exit(10); - } - else - { - System.out.println("NON-LOCAL"); - System.exit(20); - } - } - catch (OWLOntologyCreationException e) - { - System.out.println("The ontology could not be created: " + - e.getMessage()); - System.exit(1); - } - } + public static void main(String[] args) { + if (args.length != 3) { + System.out.println("Usage: LocalityChecker " + + " "); + System.exit(1); + } + LocalityClass cl = LocalityClass.BOTTOM_BOTTOM; + if (args[2].equals("TOP_BOTTOM")) { + cl = LocalityClass.TOP_BOTTOM; + } else if (args[2].equals("TOP_TOP")) { + cl = LocalityClass.TOP_TOP; + } else { + cl = LocalityClass.BOTTOM_BOTTOM; + } + try { + loader(args[0], args[1]); + // print(); + boolean local = checker(cl); + System.out.print("Result: "); + if (local) { + System.out.println("LOCAL"); + System.exit(10); + } else { + System.out.println("NON-LOCAL"); + System.exit(20); + } + } catch (OWLOntologyCreationException e) { + System.out.println("The ontology could not be created: " + + e.getMessage()); + System.exit(1); + } + } - private static Boolean checker(LocalityClass cl) - { - boolean local = true; - out = org.semanticweb.owlapi.io.ToStringRenderer.getInstance(); - SyntacticLocalityEvaluator eval = - new SyntacticLocalityEvaluator(cl); - //let's try the bottom evaluator first - Iterator it = axioms.iterator(); - while (it.hasNext()) - { - OWLAxiom elem = it.next(); - boolean l = eval.isLocal(elem, sign); - if (!l) - { - System.out.print("Non-local axiom: "); - System.out.println(out.getRendering(elem)); - System.out.println(""); - } - local = local && l; - } - return local; - } + private static Boolean checker(LocalityClass cl) { + boolean local = true; + out = org.semanticweb.owlapi.io.ToStringRenderer.getInstance(); + SyntacticLocalityEvaluator eval = new SyntacticLocalityEvaluator(cl); + // let's try the bottom evaluator first + Iterator it = axioms.iterator(); + while (it.hasNext()) { + OWLAxiom elem = it.next(); + boolean l = eval.isLocal(elem, sign); + if (!l) { + System.out.print("Non-local axiom: "); + System.out.println(out.getRendering(elem)); + System.out.println(""); + } + local = local && l; + } + return local; + } - private static void print() - { - System.out.println("Axioms:"); - Iterator it = axioms.iterator(); - while (it.hasNext()) - { - OWLAxiom elem = it.next(); - System.out.println(elem); - } - System.out.println("\nSignature:"); - Iterator itE = sign.iterator(); - while (itE.hasNext()) - { - OWLEntity elemE = itE.next(); - System.out.println(elemE); - } - } + private static void print() { + System.out.println("Axioms:"); + Iterator it = axioms.iterator(); + while (it.hasNext()) { + OWLAxiom elem = it.next(); + System.out.println(elem); + } + System.out.println("\nSignature:"); + Iterator itE = sign.iterator(); + while (itE.hasNext()) { + OWLEntity elemE = itE.next(); + System.out.println(elemE); + } + } - private static void loader(String onto, String sig) throws OWLOntologyCreationException - { - OWLOntologyManager manager = - OWLManager.createOWLOntologyManager(); - OWLOntologyManager signMan = - OWLManager.createOWLOntologyManager(); - IRI physicalIRI = IRI.create(onto); - IRI signIRI = IRI.create(sig); - OWLOntology ontology = - manager.loadOntologyFromOntologyDocument(physicalIRI); - OWLOntology signOnto = - manager.loadOntologyFromOntologyDocument(signIRI); - sign = signOnto.getSignature(); - axioms = ontology.getAxioms(); - } - + private static void loader(String onto, String sig) + throws OWLOntologyCreationException + { + OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); + OWLOntologyManager signMan = OWLManager.createOWLOntologyManager(); + IRI physicalIRI = IRI.create(onto); + IRI signIRI = IRI.create(sig); + OWLOntology ontology = + manager.loadOntologyFromOntologyDocument(physicalIRI); + OWLOntology signOnto = + manager.loadOntologyFromOntologyDocument(signIRI); + sign = signOnto.getSignature(); + axioms = ontology.getAxioms(); + } } From 47bb42d5a2ea6356bfa21924a004637803bb440f Mon Sep 17 00:00:00 2001 From: Jens Elkner Date: Wed, 1 Jun 2016 13:48:31 +0200 Subject: [PATCH 2/2] OWL2Parser: fix http redirects --- .../de/unibremen/informatik/OWL2Parser.java | 74 ++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/OWL2/java/de/unibremen/informatik/OWL2Parser.java b/OWL2/java/de/unibremen/informatik/OWL2Parser.java index 817986ea11..87cb7c2926 100644 --- a/OWL2/java/de/unibremen/informatik/OWL2Parser.java +++ b/OWL2/java/de/unibremen/informatik/OWL2Parser.java @@ -6,7 +6,10 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URI; +import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.HashSet; @@ -45,20 +48,87 @@ private static enum OPTION { private static OWLOntologyIRIMapperImpl mapper = new OWLOntologyIRIMapperImpl(); private static URI uri = null; + private static int maxRedirects = 0; + + private static void checkMaxRedirects() { + String t = System.getProperty("http.maxRedirects", null); + if (t == null || t.isEmpty()) { + maxRedirects = 20; + return; + } + try { + int n = Integer.parseInt(t, 10); + if (n > 0) { + maxRedirects = n; + } + } catch (NumberFormatException nfe) { + System.err.println("Ignoring invalid system property " + + "'http.maxRedirects' - invalid number '" + t + "'"); + } + } + + private static URLConnection getConnection(URL url, int redirCount) + throws IOException + { + URLConnection c = url.openConnection(); + if (!(c instanceof HttpURLConnection)) { + return c; + } + HttpURLConnection con = (HttpURLConnection) c; + con.addRequestProperty("Accept", "text/plain"); + con.setConnectTimeout(300 * 1000); // 5 min timeout + int status = con.getResponseCode(); + if (status == HttpURLConnection.HTTP_MOVED_TEMP + || status == HttpURLConnection.HTTP_MOVED_PERM + || status == HttpURLConnection.HTTP_SEE_OTHER) + { + if (redirCount > maxRedirects) { + System.err.println("Max. redirects (" + maxRedirects + + ") reached for " + url); + return null; + } + String loc = null; + try { + loc = con.getHeaderField("Location"); + if (loc == null) { + System.err.println("Invalid redirect by '" + url + + "' (missing Location header)."); + return null; + } + url = new URL(loc); + } catch (MalformedURLException mue) { + System.err.println("'" + url + "' redirects to an unsupported " + + "resource (" + loc + ")"); + return null; + } + con.disconnect(); + return getConnection(url, redirCount + 1); + } else if (status != HttpURLConnection.HTTP_OK) { + System.err.println("URL '" + url + "' is not OK (status " + status + + ")."); + return null; + } + return con; + } public static void main(String[] args) { // A simple example of how to load and save an ontology try { OWLOutputHandler out = new OWLOutputHandler(); parseArgs(args, out); - URLConnection con = uri.toURL().openConnection(); - con.addRequestProperty("Accept", "text/plain"); + checkMaxRedirects(); + URLConnection con = getConnection(uri.toURL(), 0); + if (con == null) { + System.err.println("Nothing to parse - exiting."); + System.exit(1); + } StreamDocumentSource sds = new StreamDocumentSource(con.getInputStream(), IRI.create(uri)); OWLOntologyLoaderConfiguration config = new OWLOntologyLoaderConfiguration(); config = config .setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT); + config = config.setFollowRedirects(true); OWLOntology ontology = manager.loadOntologyFromOntologyDocument(sds, config); if ( !missingImports.isEmpty()) {