From: <jen...@us...> - 2008-02-27 15:08:28
|
Revision: 655 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=655&view=rev Author: jenslehmann Date: 2008-02-27 07:08:19 -0800 (Wed, 27 Feb 2008) Log Message: ----------- - preliminary datatype domain/range reasoning (bug in OWL API detected) - new refinement operator (and unit tests for it) started Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/core/ReasoningService.java trunk/src/dl-learner/org/dllearner/examples/Carcinogenesis.java trunk/src/dl-learner/org/dllearner/reasoning/FastInstanceChecker.java trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java trunk/src/dl-learner/org/dllearner/refinementoperators/RhoDRDown.java trunk/src/dl-learner/org/dllearner/test/OWLAPIBugDemo.java trunk/src/dl-learner/org/dllearner/test/junit/AllTestsRunner.java trunk/src/dl-learner/org/dllearner/test/junit/ReasonerTests.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/test/junit/RefinementOperatorTests.java Modified: trunk/src/dl-learner/org/dllearner/core/ReasoningService.java =================================================================== --- trunk/src/dl-learner/org/dllearner/core/ReasoningService.java 2008-02-27 14:10:02 UTC (rev 654) +++ trunk/src/dl-learner/org/dllearner/core/ReasoningService.java 2008-02-27 15:08:19 UTC (rev 655) @@ -27,6 +27,7 @@ import java.util.SortedSet; import java.util.TreeSet; +import org.dllearner.core.owl.DataRange; import org.dllearner.core.owl.DatatypeProperty; import org.dllearner.core.owl.NamedClass; import org.dllearner.core.owl.Description; @@ -446,6 +447,42 @@ return reasoner.getIndividuals(); } + public Description getDomain(ObjectProperty objectProperty) { + try { + return reasoner.getDomain(objectProperty); + } catch (ReasoningMethodUnsupportedException e) { + handleExceptions(e); + return null; + } + } + + public Description getDomain(DatatypeProperty datatypeProperty) { + try { + return reasoner.getDomain(datatypeProperty); + } catch (ReasoningMethodUnsupportedException e) { + handleExceptions(e); + return null; + } + } + + public Description getRange(ObjectProperty objectProperty) { + try { + return reasoner.getRange(objectProperty); + } catch (ReasoningMethodUnsupportedException e) { + handleExceptions(e); + return null; + } + } + + public DataRange getRange(DatatypeProperty datatypeProperty) { + try { + return reasoner.getRange(datatypeProperty); + } catch (ReasoningMethodUnsupportedException e) { + handleExceptions(e); + return null; + } + } + public ReasonerType getReasonerType() { return reasoner.getReasonerType(); } Modified: trunk/src/dl-learner/org/dllearner/examples/Carcinogenesis.java =================================================================== --- trunk/src/dl-learner/org/dllearner/examples/Carcinogenesis.java 2008-02-27 14:10:02 UTC (rev 654) +++ trunk/src/dl-learner/org/dllearner/examples/Carcinogenesis.java 2008-02-27 15:08:19 UTC (rev 655) @@ -173,7 +173,9 @@ kbString += "OPDOMAIN(" + getURI2("hasBond") + ") = " + getURI2("Compound") + ".\n"; kbString += "OPRANGE(" + getURI2("hasBond") + ") = " + getURI2("Bond") + ".\n"; kbString += "OPDOMAIN(" + getURI2("inBond") + ") = " + getURI2("Bond") + ".\n"; - kbString += "OPRANGE(" + getURI2("inBond") + ") = " + getURI2("Atom") + ".\n"; + kbString += "OPRANGE(" + getURI2("inBond") + ") = " + getURI2("Atom") + ".\n"; + kbString += "OPDOMAIN(" + getURI2("hasStructure") + ") = " + getURI2("Compound") + ".\n"; + kbString += "OPRANGE(" + getURI2("hasStructure") + ") = " + getURI2("Structure") + ".\n"; KB kb2 = KBParser.parseKBFile(kbString); kb.addKB(kb2); Modified: trunk/src/dl-learner/org/dllearner/reasoning/FastInstanceChecker.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/FastInstanceChecker.java 2008-02-27 14:10:02 UTC (rev 654) +++ trunk/src/dl-learner/org/dllearner/reasoning/FastInstanceChecker.java 2008-02-27 15:08:19 UTC (rev 655) @@ -408,4 +408,19 @@ return rc.getPrefixes(); } + @Override + public Description getDomain(ObjectProperty objectProperty) { + return rc.getDomain(objectProperty); + } + + @Override + public Description getDomain(DatatypeProperty datatypeProperty) { + return rc.getDomain(datatypeProperty); + } + + @Override + public Description getRange(ObjectProperty objectProperty) { + return rc.getRange(objectProperty); + } + } Modified: trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java 2008-02-27 14:10:02 UTC (rev 654) +++ trunk/src/dl-learner/org/dllearner/reasoning/OWLAPIReasoner.java 2008-02-27 15:08:19 UTC (rev 655) @@ -209,8 +209,8 @@ OWLOntologyFormat format = manager.getOntologyFormat(ontology); if(format instanceof NamespaceOWLOntologyFormat) { prefixes = ((NamespaceOWLOntologyFormat)format).getNamespacesByPrefixMap(); - prefixes.remove(""); baseURI = prefixes.get(""); + prefixes.remove(""); } } catch (OWLOntologyCreationException e) { @@ -541,16 +541,51 @@ // instead of only one description (probably there can be several // domain axiom for one property and the inner set is a conjunction // of descriptions (?)) - OWLDescription d = reasoner.getDomains(prop).iterator().next().iterator().next(); -// OWLAPIDescriptionConvertVisitor.getOWLDescription(d); + // Answer: this function is just horribly broken in OWL API + Set<Set<OWLDescription>> set = reasoner.getDomains(prop); + if(set.size()==0) + return new Thing(); + OWLClass oc = (OWLClass) set.iterator().next(); + return new NamedClass(oc.getURI().toString()); } catch (OWLReasonerException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new Error(e); } - return null; } @Override + public Description getDomain(DatatypeProperty datatypeProperty) { + OWLDataProperty prop = getOWLAPIDescription(datatypeProperty); + try { + // TODO: look up why OWL API return a two dimensional set here + // instead of only one description (probably there can be several + // domain axiom for one property and the inner set is a conjunction + // of descriptions (?)) + // Answer: this function is just horribly broken in OWL API + Set<Set<OWLDescription>> set = reasoner.getDomains(prop); + if(set.size()==0) + return new Thing(); + OWLClass oc = (OWLClass) set.iterator().next(); + return new NamedClass(oc.getURI().toString()); + } catch (OWLReasonerException e) { + throw new Error(e); + } + } + + @Override + public Description getRange(ObjectProperty objectProperty) { + OWLObjectProperty prop = getOWLAPIDescription(objectProperty); + try { + Set<OWLDescription> set = reasoner.getRanges(prop); + if(set.size()==0) + return new Thing(); + OWLClass oc = (OWLClass) set.iterator().next(); + return new NamedClass(oc.getURI().toString()); + } catch (OWLReasonerException e) { + throw new Error(e); + } + } + + @Override public Map<Individual, SortedSet<Individual>> getRoleMembers(ObjectProperty atomicRole) { OWLObjectProperty prop = getOWLAPIDescription(atomicRole); Map<Individual, SortedSet<Individual>> map = new TreeMap<Individual, SortedSet<Individual>>(); Modified: trunk/src/dl-learner/org/dllearner/refinementoperators/RhoDRDown.java =================================================================== --- trunk/src/dl-learner/org/dllearner/refinementoperators/RhoDRDown.java 2008-02-27 14:10:02 UTC (rev 654) +++ trunk/src/dl-learner/org/dllearner/refinementoperators/RhoDRDown.java 2008-02-27 15:08:19 UTC (rev 655) @@ -19,11 +19,21 @@ */ package org.dllearner.refinementoperators; +import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.TreeMap; import org.dllearner.algorithms.refinement.RefinementOperator; +import org.dllearner.core.ReasoningService; +import org.dllearner.core.owl.DatatypeProperty; import org.dllearner.core.owl.Description; +import org.dllearner.core.owl.NamedClass; +import org.dllearner.core.owl.Nothing; +import org.dllearner.core.owl.ObjectProperty; +import org.dllearner.core.owl.SubsumptionHierarchy; +import org.dllearner.core.owl.Thing; /** * A downward refinement operator, which makes use of domains @@ -38,6 +48,31 @@ */ public class RhoDRDown implements RefinementOperator { + private ReasoningService rs; + + // hierarchies + private SubsumptionHierarchy subHierarchy; + + // domains and ranges + private Map<ObjectProperty,Description> opDomains = new TreeMap<ObjectProperty,Description>(); + private Map<DatatypeProperty,Description> dpDomains = new TreeMap<DatatypeProperty,Description>(); + private Map<ObjectProperty,Description> opRanges = new TreeMap<ObjectProperty,Description>(); + + public RhoDRDown(ReasoningService rs) { + this.rs = rs; + subHierarchy = rs.getSubsumptionHierarchy(); + + // query reasoner for domains and ranges + // (because they are used often in the operator) + for(ObjectProperty op : rs.getAtomicRoles()) { + opDomains.put(op, rs.getDomain(op)); + opRanges.put(op, rs.getRange(op)); + } + for(DatatypeProperty dp : rs.getDatatypeProperties()) { + dpDomains.put(dp, rs.getDomain(dp)); + } + } + /* (non-Javadoc) * @see org.dllearner.algorithms.refinement.RefinementOperator#refine(org.dllearner.core.owl.Description) */ @@ -48,10 +83,31 @@ /* (non-Javadoc) * @see org.dllearner.algorithms.refinement.RefinementOperator#refine(org.dllearner.core.owl.Description, int, java.util.List) */ - public Set<Description> refine(Description concept, int maxLength, + public Set<Description> refine(Description description, int maxLength, List<Description> knownRefinements) { + return refine(description, maxLength, knownRefinements, new Thing()); + } + + public Set<Description> refine(Description description, int maxLength, + List<Description> knownRefinements, Description currDomain) { + // TODO: check whether using list or set makes more sense + // here; and whether HashSet or TreeSet should be used + Set<Description> refinements = new HashSet<Description>(); - return null; + // .. do most general rules here ... + // (easier because it may be possible to add return + // statements instead of going through the complete + // function) + + if(description instanceof Thing) { + refinements.addAll(subHierarchy.getMoreSpecialConcepts(description)); + } else if(description instanceof Nothing) { + // cannot be further refined + } else if(description instanceof NamedClass) { + refinements.addAll(subHierarchy.getMoreSpecialConcepts(description)); + } + + return refinements; } - + } Modified: trunk/src/dl-learner/org/dllearner/test/OWLAPIBugDemo.java =================================================================== --- trunk/src/dl-learner/org/dllearner/test/OWLAPIBugDemo.java 2008-02-27 14:10:02 UTC (rev 654) +++ trunk/src/dl-learner/org/dllearner/test/OWLAPIBugDemo.java 2008-02-27 15:08:19 UTC (rev 655) @@ -1,6 +1,7 @@ package org.dllearner.test; import org.semanticweb.owl.apibinding.OWLManager; +import org.semanticweb.owl.inference.OWLReasoner; import org.semanticweb.owl.model.*; import org.semanticweb.owl.util.SimpleURIMapper; @@ -48,6 +49,26 @@ AddAxiom addAxiom2 = new AddAxiom(ontology, axiom2); manager.applyChange(addAxiom2); + // add property p with domain c + OWLObjectProperty p = factory.getOWLObjectProperty(URI.create(ontologyURI + "#p")); + OWLAxiom axiom3 = factory.getOWLObjectPropertyDomainAxiom(p, c); + AddAxiom addAxiom3 = new AddAxiom(ontology, axiom3); + manager.applyChange(addAxiom3); + + Set<OWLOntology> ontologies = new HashSet<OWLOntology>(); + ontologies.add(ontology); + + OWLReasoner reasoner = new org.mindswap.pellet.owlapi.Reasoner(manager); + reasoner.loadOntologies(ontologies); + + // class cast exception + Set<Set<OWLDescription>> test = reasoner.getDomains(p); + OWLClass oc = (OWLClass) test.iterator().next(); + System.out.println(oc); +// for(Set<OWLDescription> test2 : test) { +// System.out.println(test2); +// } + // save ontology manager.saveOntology(ontology); } Modified: trunk/src/dl-learner/org/dllearner/test/junit/AllTestsRunner.java =================================================================== --- trunk/src/dl-learner/org/dllearner/test/junit/AllTestsRunner.java 2008-02-27 14:10:02 UTC (rev 654) +++ trunk/src/dl-learner/org/dllearner/test/junit/AllTestsRunner.java 2008-02-27 15:08:19 UTC (rev 655) @@ -47,7 +47,8 @@ logger.setLevel(Level.INFO); JUnitCore.main("org.dllearner.test.junit.ComponentTests", - "org.dllearner.test.junit.ReasonerTests"); + "org.dllearner.test.junit.ReasonerTests", + "org.dllearner.test.junit.RefinementOperatorTests"); } } Modified: trunk/src/dl-learner/org/dllearner/test/junit/ReasonerTests.java =================================================================== --- trunk/src/dl-learner/org/dllearner/test/junit/ReasonerTests.java 2008-02-27 14:10:02 UTC (rev 654) +++ trunk/src/dl-learner/org/dllearner/test/junit/ReasonerTests.java 2008-02-27 15:08:19 UTC (rev 655) @@ -48,7 +48,7 @@ private static Logger logger = Logger.getLogger(ReasonerTests.class); - private KB getSimpleKnowledgeBase() { + public KB getSimpleKnowledgeBase() { String kb = "person SUB TOP."; kb += "man SUB person."; kb += "man SUB male."; Added: trunk/src/dl-learner/org/dllearner/test/junit/RefinementOperatorTests.java =================================================================== --- trunk/src/dl-learner/org/dllearner/test/junit/RefinementOperatorTests.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/test/junit/RefinementOperatorTests.java 2008-02-27 15:08:19 UTC (rev 655) @@ -0,0 +1,77 @@ +/** + * Copyright (C) 2007-2008, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +package org.dllearner.test.junit; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.Set; + +import org.dllearner.core.ComponentInitException; +import org.dllearner.core.ComponentManager; +import org.dllearner.core.KnowledgeSource; +import org.dllearner.core.ReasonerComponent; +import org.dllearner.core.ReasoningService; +import org.dllearner.core.owl.Description; +import org.dllearner.kb.OWLFile; +import org.dllearner.parser.KBParser; +import org.dllearner.parser.ParseException; +import org.dllearner.reasoning.OWLAPIReasoner; +import org.dllearner.refinementoperators.RhoDRDown; +import org.junit.Test; + +/** + * A suite of JUnit tests related to refinement operators. + * + * @author Jens Lehmann + * + */ +public class RefinementOperatorTests { + + private String baseURI; + + @Test + public void rhoDRDownTest() { + try { + String file = "examples/carcinogenesis/pte.owl"; + ComponentManager cm = ComponentManager.getInstance(); + KnowledgeSource ks = cm.knowledgeSource(OWLFile.class); + cm.applyConfigEntry(ks, "url", new File(file).toURI().toString()); + ks.init(); + ReasonerComponent rc = cm.reasoner(OWLAPIReasoner.class, ks); + rc.init(); + baseURI = rc.getBaseURI(); + ReasoningService rs = cm.reasoningService(rc); + RhoDRDown op = new RhoDRDown(rs); + Description concept = KBParser.parseConcept(uri("Compound")); + Set<Description> results = op.refine(concept, 4, null); + System.out.println(results); + assertTrue(results.size()==4); + } catch(ComponentInitException e) { + e.printStackTrace(); + } catch (ParseException e) { + e.printStackTrace(); + } + } + + private String uri(String name) { + return "\""+baseURI+name+"\""; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |