From: <lor...@us...> - 2009-06-15 13:22:33
|
Revision: 1799 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=1799&view=rev Author: lorenz_b Date: 2009-06-15 13:22:29 +0000 (Mon, 15 Jun 2009) Log Message: ----------- continued new explanation generator Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/tools/ore/explanation/SyntacticRelevanceBasedExplanationGenerator.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/tools/ore/explanation/HittingSet.java Added: trunk/src/dl-learner/org/dllearner/tools/ore/explanation/HittingSet.java =================================================================== --- trunk/src/dl-learner/org/dllearner/tools/ore/explanation/HittingSet.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/tools/ore/explanation/HittingSet.java 2009-06-15 13:22:29 UTC (rev 1799) @@ -0,0 +1,17 @@ +package org.dllearner.tools.ore.explanation; + +import java.util.HashSet; +import java.util.Set; + +import org.semanticweb.owl.model.OWLAxiom; + +public class HittingSet extends HashSet<OWLAxiom> { + + public HittingSet(Set<OWLAxiom> axioms){ + addAll(axioms); + } + + public HittingSet(OWLAxiom axiom){ + add(axiom); + } +} Modified: trunk/src/dl-learner/org/dllearner/tools/ore/explanation/SyntacticRelevanceBasedExplanationGenerator.java =================================================================== --- trunk/src/dl-learner/org/dllearner/tools/ore/explanation/SyntacticRelevanceBasedExplanationGenerator.java 2009-06-15 08:31:17 UTC (rev 1798) +++ trunk/src/dl-learner/org/dllearner/tools/ore/explanation/SyntacticRelevanceBasedExplanationGenerator.java 2009-06-15 13:22:29 UTC (rev 1799) @@ -22,18 +22,25 @@ import org.semanticweb.owl.model.OWLOntologyChangeException; import org.semanticweb.owl.model.OWLOntologyCreationException; import org.semanticweb.owl.model.OWLOntologyManager; -import org.semanticweb.owl.model.OWLOntologyStorageException; import org.semanticweb.owl.model.RemoveAxiom; -import org.semanticweb.owl.model.UnknownOWLOntologyException; +import uk.ac.manchester.cs.owl.dlsyntax.DLSyntaxObjectRenderer; + import com.clarkparsia.explanation.PelletExplanation; public class SyntacticRelevanceBasedExplanationGenerator { private Reasoner reasoner; + private Reasoner localReasoner; private OWLOntologyManager manager; private OWLOntology ontology; + public static enum Strategie{ + All_Just_Relevance, + CM_Just_Relevance; + } + private Strategie strategie; + private static Logger logger = Logger.getRootLogger(); public SyntacticRelevanceBasedExplanationGenerator(Reasoner reasoner, OWLOntologyManager manager){ @@ -48,7 +55,8 @@ } - public Set<Set<OWLAxiom>> getExplanations(OWLClass unsat){ + public Set<Set<OWLAxiom>> getExplanations(OWLClass unsat, Strategie strategie){ + this.strategie = strategie; return computeRelevantJustifications(unsat); } @@ -57,7 +65,7 @@ OWLOntology ont = null; Set<Set<OWLAxiom>> justifications = new HashSet<Set<OWLAxiom>>(); OWLOntologyManager man = OWLManager.createOWLOntologyManager(); - Reasoner reasoner = new PelletReasonerFactory().createReasoner(man); + localReasoner = new PelletReasonerFactory().createReasoner(man); int k = 1; try { ont = man.createOntology(URI.create("file:/home/lorenz/test.owl")); @@ -65,12 +73,12 @@ // TODO Auto-generated catch block e.printStackTrace(); } - reasoner.loadOntology(ont); + localReasoner.loadOntology(ont); Set<OWLAxiom> relevant = getSyntacticRelevantAxioms(unsat, k); - logger.debug("step " + k + ": selected axioms: " + relevant); - Set<OWLAxiom> hittingSets = new HashSet<OWLAxiom>(); - Set<Set<OWLAxiom>> hittingSetLocal = new HashSet<Set<OWLAxiom>>(); + + Set<HittingSet> hittingSets = new HashSet<HittingSet>(); + Set<HittingSet> hittingSetLocal = new HashSet<HittingSet>(); try { man.addAxioms(ont, relevant); } catch (OWLOntologyChangeException e) { @@ -82,6 +90,7 @@ while(!relevant.isEmpty()){ + logger.debug("step " + k + ": selected axioms: " + relevant); try { man.addAxioms(ont, relevant); } catch (OWLOntologyChangeException e) { @@ -90,12 +99,57 @@ } reasoner.refresh(); if(!hittingSetLocal.isEmpty()){ + for(HittingSet hit : hittingSetLocal){ + try { + for(OWLAxiom ax : hit){ + manager.applyChange(new RemoveAxiom(ontology, ax)); + } + reasoner.refresh(); + + if(reasoner.isSatisfiable(unsat)){ + hittingSets.add(hit); + } + manager.addAxioms(ontology, hit); + reasoner.refresh(); + } catch (OWLOntologyChangeException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + hittingSetLocal.removeAll(hittingSets); + if( (!strategie.equals(Strategie.All_Just_Relevance) && !hittingSets.isEmpty()) || (hittingSetLocal.isEmpty()) ){ + logger.debug("early termination"); + return justifications; + } + Set<HittingSet> temp = new HashSet<HittingSet>(hittingSetLocal); + for(HittingSet hit : temp){ + try { + for(OWLAxiom ax : hit){ + man.applyChange(new RemoveAxiom(ont, ax)); + } + List<Set<? extends Set<OWLAxiom>>> result = computeJustifications(unsat, ont); + justifications.addAll(result.get(0)); + Set<HittingSet> localTemp = (Set<HittingSet>)result.get(1); + for(HittingSet h : localTemp){ + h.addAll(hit); + hittingSetLocal.add(h); + } + hittingSetLocal.remove(hit); + man.addAxioms(ont, hit); + } catch (OWLOntologyChangeException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + } - } else if(!reasoner.isSatisfiable(unsat)){ - System.out.println(reasoner.getLoadedOntologies() + "" + reasoner.isSatisfiable(unsat)); - List<Set<Set<OWLAxiom>>> result = computeJustifications(unsat, ont); + } else if(!localReasoner.isSatisfiable(unsat)){ + + List<Set<? extends Set<OWLAxiom>>> result = computeJustifications(unsat, ont); + logger.debug("step " + k + ": justifications computed : " + result.get(0)); justifications.addAll(result.get(0)); - hittingSetLocal.addAll(result.get(1)); + hittingSetLocal.addAll((Set<HittingSet>)result.get(1)); } k++; relevant = getSyntacticRelevantAxioms(unsat, k); @@ -103,40 +157,43 @@ return justifications; } - private List<Set<Set<OWLAxiom>>> computeJustifications(OWLClass unsat, OWLOntology ont){ + private List<Set<? extends Set<OWLAxiom>>> computeJustifications(OWLClass unsat, OWLOntology ont){ Set<Set<OWLAxiom>> justifications = new HashSet<Set<OWLAxiom>>(); PelletExplanation expGen = new PelletExplanation(manager, Collections.singleton(ont)); - Set<Set<OWLAxiom>> hittingSets = new HashSet<Set<OWLAxiom>>(); - Set<Set<OWLAxiom>> hittingSets1 = new HashSet<Set<OWLAxiom>>(); + Set<HittingSet> hittingSets = new HashSet<HittingSet>(); + Set<HittingSet> hittingSets1 = new HashSet<HittingSet>(); Set<OWLAxiom> justification = expGen.getUnsatisfiableExplanation(unsat); - System.out.println(justification); + justifications.add(justification); for(OWLAxiom ax : justification){ - hittingSets1.add(Collections.singleton(ax)); + hittingSets1.add(new HittingSet(ax)); } while(true){ - Set<Set<OWLAxiom>> hittingSets2 = new HashSet<Set<OWLAxiom>>(); - for(Set<OWLAxiom> axioms : hittingSets1){ + Set<HittingSet> hittingSets2 = new HashSet<HittingSet>(); + for(HittingSet hit : hittingSets1){ try { - for(OWLAxiom ax : axioms){ + for(OWLAxiom ax : hit){ manager.applyChange(new RemoveAxiom(ont, ax)); } + localReasoner.refresh(); - if(reasoner.isSatisfiable(unsat)){ - hittingSets.add(axioms); + if(localReasoner.isSatisfiable(unsat)){ + hittingSets.add(hit); + logger.debug("found local hitting set: " + hit); } else { - hittingSets2.add(axioms); + hittingSets2.add(hit); } - manager.addAxioms(ont, axioms); + manager.addAxioms(ont, hit); + localReasoner.refresh(); } catch (OWLOntologyChangeException e) { // TODO Auto-generated catch block e.printStackTrace(); } } - if(hittingSets1.isEmpty() || hittingSets2.isEmpty()){ - List<Set<Set<OWLAxiom>>> result = new ArrayList<Set<Set<OWLAxiom>>>(); + if( (!strategie.equals(Strategie.All_Just_Relevance) && !hittingSets.isEmpty() ) || hittingSets1.isEmpty() || hittingSets2.isEmpty()){ + List<Set<? extends Set<OWLAxiom>>> result = new ArrayList<Set<? extends Set<OWLAxiom>>>(); result.add(justifications); result.add(hittingSets); return result; @@ -151,7 +208,7 @@ Set<OWLAxiom> just = expGen.getUnsatisfiableExplanation(unsat); justifications.add(just); for(OWLAxiom a : just){ - Set<OWLAxiom> temp = new HashSet<OWLAxiom>(axioms); + HittingSet temp = new HittingSet(axioms); temp.add(a); hittingSets1.add(temp); } @@ -167,37 +224,36 @@ - public Set<OWLAxiom> getSyntacticRelevantAxioms(OWLClass cl, int k){ - - Set<OWLAxiom> relevantAxioms = new HashSet<OWLAxiom>(); - - if(k == 1){ - for(OWLAxiom ax : ontology.getLogicalAxioms()){ - if(ax.getSignature().contains(cl)){ + public Set<OWLAxiom> getSyntacticRelevantAxioms(OWLClass cl, int k) { + + Set<OWLAxiom> relevantAxioms = new HashSet<OWLAxiom>(); + + if (k == 1) { + for (OWLAxiom ax : ontology.getLogicalAxioms()) { + if (ax.getSignature().contains(cl)) { relevantAxioms.add(ax); } } - + } else { Set<OWLAxiom> axioms = getSyntacticRelevantAxioms(cl, k - 1); - - for(OWLAxiom ax1 : axioms){ - - for(OWLAxiom ax2 : ontology.getLogicalAxioms()){ - - if(areSyntacticRelevant(ax1, ax2)){ - + + for (OWLAxiom ax1 : axioms) { + + for (OWLAxiom ax2 : ontology.getLogicalAxioms()) { + + if (areSyntacticRelevant(ax1, ax2)) { + relevantAxioms.add(ax2); } } } - for(int i = k - 1; i>= 1 ;i--){ - relevantAxioms.removeAll(getSyntacticRelevantAxioms(cl,i)); + for (int i = k - 1; i >= 1; i--) { + relevantAxioms.removeAll(getSyntacticRelevantAxioms(cl, i)); } - - + } - + return relevantAxioms; } @@ -208,7 +264,7 @@ public static void main(String[] args){ URI file = URI.create("file:examples/ore/koala.owl"); String base = "http://protege.stanford.edu/plugins/owl/owl-library/koala.owl"; - URI classURI = URI.create(base + "#Koala"); + URI classURI = URI.create(base + "#KoalaWithPhD"); try { OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); @@ -216,37 +272,73 @@ OWLClass cl = factory.getOWLClass(classURI); OWLOntology ontology = manager.loadOntologyFromPhysicalURI(file); - OWLClass cl1 = factory.getOWLClass(URI.create("Manager")); - OWLClass cl2 = factory.getOWLClass(URI.create("Employee")); - OWLClass cl3 = factory.getOWLClass(URI.create("JobPosition")); - OWLClass cl4 = factory.getOWLClass(URI.create("Leader")); - OWLClass cl5 = factory.getOWLClass(URI.create("Situation")); - OWLClass cl6 = factory.getOWLClass(URI.create("Happening")); - OWLClass cl7 = factory.getOWLClass(URI.create("Patent")); - OWLIndividual ind = factory.getOWLIndividual(URI.create("lectureship")); +// OWLClass cl1 = factory.getOWLClass(URI.create("Manager")); +// OWLClass cl2 = factory.getOWLClass(URI.create("Employee")); +// OWLClass cl3 = factory.getOWLClass(URI.create("JobPosition")); +// OWLClass cl4 = factory.getOWLClass(URI.create("Leader")); +// OWLClass cl5 = factory.getOWLClass(URI.create("Situation")); +// OWLClass cl6 = factory.getOWLClass(URI.create("Happening")); +// OWLClass cl7 = factory.getOWLClass(URI.create("Patent")); +// OWLIndividual ind = factory.getOWLIndividual(URI.create("lectureship")); +// Set<OWLAxiom> examples = new HashSet<OWLAxiom>(); +// examples.add(factory.getOWLSubClassAxiom(cl1, cl2)); +// examples.add(factory.getOWLSubClassAxiom(cl2, cl3)); +// examples.add(factory.getOWLSubClassAxiom(cl4, cl3)); +// examples.add(factory.getOWLSubClassAxiom(cl3, cl5)); +// examples.add(factory.getOWLSubClassAxiom(cl5, cl6)); +// examples.add(factory.getOWLSubClassAxiom(cl4, factory.getOWLObjectComplementOf(cl7))); +// examples.add(factory.getOWLSubClassAxiom(cl6, factory.getOWLObjectComplementOf(cl1))); +// examples.add(factory.getOWLSubClassAxiom(cl3, factory.getOWLObjectComplementOf(cl2))); +// examples.add(factory.getOWLClassAssertionAxiom(ind, cl3)); +// OWLOntology example = manager.createOntology(examples); + + OWLClass u = factory.getOWLClass(URI.create("U")); + OWLClass a = factory.getOWLClass(URI.create("A")); + OWLClass b = factory.getOWLClass(URI.create("B")); + OWLClass c = factory.getOWLClass(URI.create("C")); + OWLClass d = factory.getOWLClass(URI.create("D")); + OWLClass e = factory.getOWLClass(URI.create("E")); + OWLClass f = factory.getOWLClass(URI.create("F")); + OWLClass g = factory.getOWLClass(URI.create("G")); + OWLClass h = factory.getOWLClass(URI.create("H")); + OWLClass k = factory.getOWLClass(URI.create("K")); Set<OWLAxiom> examples = new HashSet<OWLAxiom>(); - examples.add(factory.getOWLSubClassAxiom(cl1, cl2)); - examples.add(factory.getOWLSubClassAxiom(cl2, cl3)); - examples.add(factory.getOWLSubClassAxiom(cl4, cl3)); - examples.add(factory.getOWLSubClassAxiom(cl3, cl5)); - examples.add(factory.getOWLSubClassAxiom(cl5, cl6)); - examples.add(factory.getOWLSubClassAxiom(cl4, factory.getOWLObjectComplementOf(cl7))); - examples.add(factory.getOWLSubClassAxiom(cl6, factory.getOWLObjectComplementOf(cl1))); - examples.add(factory.getOWLSubClassAxiom(cl3, factory.getOWLObjectComplementOf(cl2))); - examples.add(factory.getOWLClassAssertionAxiom(ind, cl3)); - + examples.add( factory.getOWLSubClassAxiom(u, a)); + examples.add( factory.getOWLSubClassAxiom(u, factory.getOWLObjectComplementOf(a))); + examples.add( factory.getOWLSubClassAxiom(u, c)); + examples.add( factory.getOWLSubClassAxiom(c, factory.getOWLObjectComplementOf(b))); + examples.add( factory.getOWLSubClassAxiom(a, b)); + examples.add( factory.getOWLSubClassAxiom(u, g)); + examples.add( factory.getOWLSubClassAxiom(g, e)); + examples.add( factory.getOWLSubClassAxiom(u, f)); + examples.add( factory.getOWLSubClassAxiom(f, factory.getOWLObjectComplementOf(e))); + examples.add( factory.getOWLSubClassAxiom(u, d)); + examples.add( factory.getOWLSubClassAxiom(d, e)); + examples.add( factory.getOWLSubClassAxiom(c, k)); + examples.add( factory.getOWLSubClassAxiom(k, factory.getOWLObjectComplementOf(h))); + examples.add( factory.getOWLSubClassAxiom(b, h)); OWLOntology example = manager.createOntology(examples); + + Reasoner reasoner = new PelletReasonerFactory().createReasoner(manager); - reasoner.loadOntologies(Collections.singleton(ontology)); + reasoner.loadOntologies(Collections.singleton(example)); SyntacticRelevanceBasedExplanationGenerator expGen = new SyntacticRelevanceBasedExplanationGenerator(reasoner, manager); + DLSyntaxObjectRenderer renderer = new DLSyntaxObjectRenderer(); + int i = 1; + for(Set<OWLAxiom> explanation : expGen.getExplanations(u, Strategie.CM_Just_Relevance)){ + System.out.println("Explanation " + i); + i++; + for(OWLAxiom ax : explanation){ + System.out.println(renderer.render(ax)); + } + } + - System.out.println(expGen.getExplanations(cl)); - } catch (OWLOntologyCreationException e) { // TODO Auto-generated catch block e.printStackTrace(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |