From: <lor...@us...> - 2011-12-07 21:50:18
|
Revision: 3487 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3487&view=rev Author: lorenz_b Date: 2011-12-07 21:50:12 +0000 (Wed, 07 Dec 2011) Log Message: ----------- Started greedy algorithm implementation to build cohaerent ontology. Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java Added: trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java 2011-12-07 21:50:12 UTC (rev 3487) @@ -0,0 +1,99 @@ +package org.dllearner.utilities; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.commons.collections15.BidiMap; +import org.apache.commons.collections15.bidimap.DualHashBidiMap; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.model.AxiomType; +import org.semanticweb.owlapi.model.OWLAxiom; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.semanticweb.owlapi.reasoner.OWLReasoner; + +public class GreedyCohaerencyExtractor { + + public GreedyCohaerencyExtractor() { + // TODO Auto-generated constructor stub + } + + public OWLOntology getCoharentOntology(OWLOntology ontology) throws OWLOntologyCreationException{ + BidiMap<AxiomType<? extends OWLAxiom>, Integer> axiomType2CountMap = getAxiomTypeCount(ontology); + + Map<AxiomType<? extends OWLAxiom>, List<OWLAxiom>> axiomType2AxiomsMap = new HashMap<AxiomType<? extends OWLAxiom>, List<OWLAxiom>>(); + for(AxiomType<? extends OWLAxiom> type : AxiomType.AXIOM_TYPES){ + axiomType2AxiomsMap.put(type, new ArrayList<OWLAxiom>(ontology.getAxioms(type))); + } + + int lcm = lcm(new ArrayList<Integer>(axiomType2CountMap.values())); + + OWLOntologyManager man = OWLManager.createOWLOntologyManager(); + OWLOntology cohaerentOntology = man.createOntology(); + + for(int i = 0; i < lcm; i++){ + for(Entry<AxiomType<? extends OWLAxiom>, Integer> entry : axiomType2CountMap.entrySet()){ + if((i % entry.getValue()) == 0){ + man.addAxiom(cohaerentOntology, axiomType2AxiomsMap.get(entry.getKey()).remove(0)); + } + } + } + return cohaerentOntology; + } + + public OWLOntology getCoharentOntology(OWLReasoner reasoner) throws OWLOntologyCreationException{ + return getCoharentOntology(reasoner.getRootOntology()); + } + + private BidiMap<AxiomType<? extends OWLAxiom>, Integer> getAxiomTypeCount(OWLOntology ontology){ + BidiMap<AxiomType<? extends OWLAxiom>, Integer> axiomType2CountMap = new DualHashBidiMap<AxiomType<? extends OWLAxiom>, Integer>(); + + for(AxiomType<? extends OWLAxiom> type : AxiomType.AXIOM_TYPES){ + axiomType2CountMap.put(type, ontology.getAxiomCount(type)); + } + + return axiomType2CountMap; + } + + private int lcm(int x1,int x2) { + if(x1<=0 || x2<=0) { + throw new IllegalArgumentException("Cannot compute the least "+ + "common multiple of two "+ + "numbers if one, at least,"+ + "is negative."); + } + int max,min; + if (x1>x2) { + max = x1; + min = x2; + } else { + max = x2; + min = x1; + } + for(int i=1; i<=min; i++) { + if( (max*i)%min == 0 ) { + return i*max; + } + } + throw new Error("Cannot find the least common multiple of numbers "+ + x1+" and "+x2); + } + + private int lcm(List<Integer> values) { + if(values.size() == 1){ + return values.get(0); + } else { + List<Integer> list = new ArrayList<Integer>(); + list.add(lcm(values.get(0), values.get(1))); + if(values.size() > 2){ + list.addAll(values.subList(2, values.size())); + } + return lcm(list); + } + } + +} Property changes on: trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java ___________________________________________________________________ Added: svn:mime-type + text/plain This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2011-12-07 22:23:10
|
Revision: 3488 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3488&view=rev Author: lorenz_b Date: 2011-12-07 22:23:04 +0000 (Wed, 07 Dec 2011) Log Message: ----------- Continued greedy algorithm implementation to build cohaerent ontology. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java Modified: trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java 2011-12-07 21:50:12 UTC (rev 3487) +++ trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java 2011-12-07 22:23:04 UTC (rev 3488) @@ -16,6 +16,8 @@ import org.semanticweb.owlapi.model.OWLOntologyManager; import org.semanticweb.owlapi.reasoner.OWLReasoner; +import com.clarkparsia.modularity.IncrementalClassifier; + public class GreedyCohaerencyExtractor { public GreedyCohaerencyExtractor() { @@ -23,6 +25,9 @@ } public OWLOntology getCoharentOntology(OWLOntology ontology) throws OWLOntologyCreationException{ + IncrementalClassifier reasoner = new IncrementalClassifier(ontology); + reasoner.classify(); + BidiMap<AxiomType<? extends OWLAxiom>, Integer> axiomType2CountMap = getAxiomTypeCount(ontology); Map<AxiomType<? extends OWLAxiom>, List<OWLAxiom>> axiomType2AxiomsMap = new HashMap<AxiomType<? extends OWLAxiom>, List<OWLAxiom>>(); @@ -33,12 +38,22 @@ int lcm = lcm(new ArrayList<Integer>(axiomType2CountMap.values())); OWLOntologyManager man = OWLManager.createOWLOntologyManager(); + man.addOntologyChangeListener(reasoner); OWLOntology cohaerentOntology = man.createOntology(); + boolean isCohaerent = true; for(int i = 0; i < lcm; i++){ - for(Entry<AxiomType<? extends OWLAxiom>, Integer> entry : axiomType2CountMap.entrySet()){ - if((i % entry.getValue()) == 0){ - man.addAxiom(cohaerentOntology, axiomType2AxiomsMap.get(entry.getKey()).remove(0)); + if(isCohaerent){ + for(Entry<AxiomType<? extends OWLAxiom>, Integer> entry : axiomType2CountMap.entrySet()){ + if((i % entry.getValue()) == 0){ + OWLAxiom ax = axiomType2AxiomsMap.get(entry.getKey()).remove(0); + man.addAxiom(cohaerentOntology, ax); + isCohaerent = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().isEmpty(); + if(!isCohaerent){ + man.removeAxiom(cohaerentOntology, ax); + break; + } + } } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2012-02-27 16:02:38
|
Revision: 3591 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3591&view=rev Author: lorenz_b Date: 2012-02-27 16:02:32 +0000 (Mon, 27 Feb 2012) Log Message: ----------- Continued. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java Modified: trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java 2012-02-27 13:00:31 UTC (rev 3590) +++ trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java 2012-02-27 16:02:32 UTC (rev 3591) @@ -31,6 +31,10 @@ private static final double STEP_SIZE = 0.001; private static final int ALLOWED_UNSATISFIABLE_CLASSES = 5; + private OWLOntologyManager manager; + private OWLOntology cohaerentOntology; + private IncrementalClassifier reasoner; + public GreedyCohaerencyExtractor() { // TODO Auto-generated constructor stub } @@ -55,11 +59,11 @@ } - OWLOntologyManager man = OWLManager.createOWLOntologyManager(); - OWLOntology cohaerentOntology = man.createOntology(); + manager = OWLManager.createOWLOntologyManager(); + cohaerentOntology = manager.createOntology(); - IncrementalClassifier reasoner = new IncrementalClassifier(cohaerentOntology); - man.addOntologyChangeListener(reasoner); + reasoner = new IncrementalClassifier(cohaerentOntology); + manager.addOntologyChangeListener(reasoner); reasoner.classify(); @@ -80,14 +84,20 @@ // break; // } // } - Set<OWLAxiom> toAdd = new HashSet<OWLAxiom>(axiomType2AxiomsMap.get(type[i]).subList(0, x)); - man.addAxioms(cohaerentOntology, toAdd); + + /*Set<OWLAxiom> toAdd = new HashSet<OWLAxiom>(axiomType2AxiomsMap.get(type[i]).subList(0, x)); + manager.addAxioms(cohaerentOntology, toAdd); axiomType2AxiomsMap.get(type[i]).removeAll(toAdd); isCohaerent = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() <= ALLOWED_UNSATISFIABLE_CLASSES; if(!isCohaerent){ - man.removeAxioms(cohaerentOntology, toAdd);System.out.println("Incohaerency detected"); + manager.removeAxioms(cohaerentOntology, toAdd);System.out.println("Incohaerency detected"); break; - } + }*/ + + List<OWLAxiom> toAdd = axiomType2AxiomsMap.get(type[i]).subList(0, x); + addAxioms(toAdd); + axiomType2AxiomsMap.get(type[i]).removeAll(toAdd); + cnt[i] = cnt[i] - x; } } @@ -95,7 +105,7 @@ } try { - man.saveOntology(cohaerentOntology, new RDFXMLOntologyFormat(), new BufferedOutputStream(new FileOutputStream(new File("coherent.owl")))); + manager.saveOntology(cohaerentOntology, new RDFXMLOntologyFormat(), new BufferedOutputStream(new FileOutputStream(new File("coherent.owl")))); } catch (OWLOntologyStorageException e) { e.printStackTrace(); } catch (FileNotFoundException e) { @@ -105,6 +115,36 @@ return cohaerentOntology; } + private Set<OWLAxiom> addAxioms(List<OWLAxiom> axioms){ + Set<OWLAxiom> addedAxioms = new HashSet<OWLAxiom>(); + + Set<OWLAxiom> axiomSet = new HashSet<OWLAxiom>(axioms); + manager.addAxioms(cohaerentOntology, axiomSet); + reasoner.classify(); + boolean isCohaerent = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() <= ALLOWED_UNSATISFIABLE_CLASSES; + if(!isCohaerent){ + System.out.println("Incohaerency detected. Splitting..."); + manager.removeAxioms(cohaerentOntology, axiomSet); + if(axioms.size() == 1){ + return addedAxioms; + } + + int size = axioms.size(); + int pivot = size/2; + + List<OWLAxiom> left = axioms.subList(0, pivot); + List<OWLAxiom> right = axioms.subList(pivot, size-1); + + addedAxioms.addAll(addAxioms(left)); + addedAxioms.addAll(addAxioms(right)); + + } else { + addedAxioms.addAll(axioms); + } + + return addedAxioms; + } + public OWLOntology getCoharentOntology(OWLReasoner reasoner) throws OWLOntologyCreationException{ return getCoharentOntology(reasoner.getRootOntology()); } @@ -125,7 +165,7 @@ public static void main(String[] args) throws Exception{ OWLOntologyManager man = OWLManager.createOWLOntologyManager(); - OWLOntology schema = man.loadOntologyFromOntologyDocument(new File("/home/lorenz/arbeit/dbpedia_0.75_no_datapropaxioms.owl")); + OWLOntology schema = man.loadOntologyFromOntologyDocument(new File("/home/lorenz/arbeit/papers/ESWC2012/dbpedia_0.75_no_datapropaxioms.owl")); GreedyCohaerencyExtractor ge = new GreedyCohaerencyExtractor(); ge.getCoharentOntology(schema); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2012-02-28 13:07:54
|
Revision: 3593 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3593&view=rev Author: lorenz_b Date: 2012-02-28 13:07:43 +0000 (Tue, 28 Feb 2012) Log Message: ----------- Updated algorithm. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java Modified: trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java 2012-02-28 02:25:27 UTC (rev 3592) +++ trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java 2012-02-28 13:07:43 UTC (rev 3593) @@ -1,9 +1,12 @@ package org.dllearner.utilities; +import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -14,6 +17,12 @@ import org.apache.commons.collections15.BidiMap; import org.apache.commons.collections15.bidimap.DualHashBidiMap; +import org.apache.commons.compress.compressors.CompressorStreamFactory; +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.FileAppender; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.SimpleLayout; import org.semanticweb.owlapi.apibinding.OWLManager; import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; import org.semanticweb.owlapi.model.AxiomType; @@ -28,31 +37,37 @@ public class GreedyCohaerencyExtractor { - private static final double STEP_SIZE = 0.001; + private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(JustificationBasedCoherentOntologyExtractor.class); + + private double stepSize = 0.001; private static final int ALLOWED_UNSATISFIABLE_CLASSES = 5; private OWLOntologyManager manager; - private OWLOntology cohaerentOntology; + private OWLOntology coherentOntology; private IncrementalClassifier reasoner; public GreedyCohaerencyExtractor() { // TODO Auto-generated constructor stub } - public OWLOntology getCoharentOntology(OWLOntology ontology) throws OWLOntologyCreationException{ + public OWLOntology getCoherentOntology(OWLOntology ontology, String target, double stepSize) throws OWLOntologyCreationException{ + stepSize = stepSize/100; BidiMap<AxiomType<? extends OWLAxiom>, Integer> axiomType2CountMap = getAxiomTypeCount(ontology); Map<AxiomType<? extends OWLAxiom>, List<OWLAxiom>> axiomType2AxiomsMap = new HashMap<AxiomType<? extends OWLAxiom>, List<OWLAxiom>>(); for(AxiomType<? extends OWLAxiom> type : AxiomType.AXIOM_TYPES){ axiomType2AxiomsMap.put(type, new ArrayList<OWLAxiom>(ontology.getAxioms(type))); } - System.out.println(ontology.getLogicalAxiomCount()); - double[] stepSize = new double[axiomType2CountMap.entrySet().size()]; + //omit annotation axioms here + axiomType2AxiomsMap.remove(AxiomType.ANNOTATION_ASSERTION); + + logger.info("Source ontology contains " + ontology.getLogicalAxiomCount() + " logical axioms."); + double[] stepSizeArray = new double[axiomType2CountMap.entrySet().size()]; double[] cnt = new double[axiomType2CountMap.entrySet().size()]; AxiomType[] type = new AxiomType[axiomType2CountMap.entrySet().size()]; int i=0; for(Entry<AxiomType<? extends OWLAxiom>, Integer> entry : axiomType2CountMap.entrySet()){ - stepSize[i] = STEP_SIZE * entry.getValue(); + stepSizeArray[i] = stepSize * entry.getValue(); type[i] = entry.getKey(); cnt[i] = 0; i++; @@ -60,71 +75,67 @@ manager = OWLManager.createOWLOntologyManager(); - cohaerentOntology = manager.createOntology(); + coherentOntology = manager.createOntology(); - reasoner = new IncrementalClassifier(cohaerentOntology); - manager.addOntologyChangeListener(reasoner); + reasoner = new IncrementalClassifier(coherentOntology); + reasoner.setMultiThreaded(false); +// manager.addOntologyChangeListener(reasoner); reasoner.classify(); - boolean isCohaerent = true; - for(double j = 0; j < 1; j += STEP_SIZE){System.out.println(j); - if(isCohaerent){ - for(i = 0; i < stepSize.length; i++){ - cnt[i] = cnt[i] + stepSize[i]; + boolean isCoherent = true; + for(double j = 0; j < 1; j += stepSize){//increase by stepsize p until 100% + if(isCoherent){ + for(i = 0; i < stepSizeArray.length; i++){//for each axiomtype + cnt[i] = cnt[i] + stepSizeArray[i];//sum up value which was computed by p * #axioms int x = (int)cnt[i]; - System.out.println("Adding " + x + " " + type[i] + " axioms from " + axiomType2CountMap.get(type[i])); -// System.out.println(axiomType2AxiomsMap.get(type[i]).size()); -// for(int k = 0; k < x; k++){ -// OWLAxiom ax = axiomType2AxiomsMap.get(type[i]).remove(0); -// man.addAxiom(cohaerentOntology, ax); -// isCohaerent = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().isEmpty(); -// if(!isCohaerent){ -// man.removeAxiom(cohaerentOntology, ax); + if(x > 0){ + logger.info("Adding " + x + " " + type[i] + " axioms from " + axiomType2CountMap.get(type[i])); + Set<OWLAxiom> toAdd = new HashSet<OWLAxiom>(axiomType2AxiomsMap.get(type[i]).subList(0, x)); + manager.addAxioms(coherentOntology, toAdd); + axiomType2AxiomsMap.get(type[i]).removeAll(toAdd); + isCoherent = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() <= ALLOWED_UNSATISFIABLE_CLASSES; + if(!isCoherent){ + manager.removeAxioms(coherentOntology, toAdd); + logger.info("Incoherency detected. Undoing changes."); + isCoherent = true; // break; -// } -// } + } + } - /*Set<OWLAxiom> toAdd = new HashSet<OWLAxiom>(axiomType2AxiomsMap.get(type[i]).subList(0, x)); - manager.addAxioms(cohaerentOntology, toAdd); - axiomType2AxiomsMap.get(type[i]).removeAll(toAdd); - isCohaerent = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() <= ALLOWED_UNSATISFIABLE_CLASSES; - if(!isCohaerent){ - manager.removeAxioms(cohaerentOntology, toAdd);System.out.println("Incohaerency detected"); - break; - }*/ - - List<OWLAxiom> toAdd = axiomType2AxiomsMap.get(type[i]).subList(0, x); + //same procedure with divide and conquer optimization + /*List<OWLAxiom> toAdd = axiomType2AxiomsMap.get(type[i]).subList(0, x); addAxioms(toAdd); axiomType2AxiomsMap.get(type[i]).removeAll(toAdd); + */ cnt[i] = cnt[i] - x; } } - System.out.println(cohaerentOntology.getLogicalAxiomCount()); + logger.info("Coherent ontology contains " + coherentOntology.getLogicalAxiomCount() + " logical axioms."); } try { - manager.saveOntology(cohaerentOntology, new RDFXMLOntologyFormat(), new BufferedOutputStream(new FileOutputStream(new File("coherent.owl")))); + manager.saveOntology(coherentOntology, new RDFXMLOntologyFormat(), new BufferedOutputStream(new FileOutputStream(new File(target)))); } catch (OWLOntologyStorageException e) { e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } - return cohaerentOntology; + return coherentOntology; } private Set<OWLAxiom> addAxioms(List<OWLAxiom> axioms){ Set<OWLAxiom> addedAxioms = new HashSet<OWLAxiom>(); Set<OWLAxiom> axiomSet = new HashSet<OWLAxiom>(axioms); - manager.addAxioms(cohaerentOntology, axiomSet); + manager.addAxioms(coherentOntology, axiomSet); reasoner.classify(); boolean isCohaerent = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() <= ALLOWED_UNSATISFIABLE_CLASSES; if(!isCohaerent){ System.out.println("Incohaerency detected. Splitting..."); - manager.removeAxioms(cohaerentOntology, axiomSet); + manager.removeAxioms(coherentOntology, axiomSet); if(axioms.size() == 1){ return addedAxioms; } @@ -145,8 +156,8 @@ return addedAxioms; } - public OWLOntology getCoharentOntology(OWLReasoner reasoner) throws OWLOntologyCreationException{ - return getCoharentOntology(reasoner.getRootOntology()); + public OWLOntology getCoherentOntology(OWLReasoner reasoner, String target, double stepSize) throws OWLOntologyCreationException{ + return getCoherentOntology(reasoner.getRootOntology(), target, stepSize); } private BidiMap<AxiomType<? extends OWLAxiom>, Integer> getAxiomTypeCount(OWLOntology ontology){ @@ -164,11 +175,30 @@ } public static void main(String[] args) throws Exception{ + Logger.getRootLogger().setLevel(Level.INFO); + Logger.getRootLogger().removeAllAppenders(); + Logger.getRootLogger().addAppender(new ConsoleAppender(new SimpleLayout())); + Logger.getRootLogger().addAppender(new FileAppender(new SimpleLayout(), "log/greedy_out.log")); + + if(args.length != 3){ + System.out.println("USAGE: GreedyCoherencyExtractor <incoherent.owl> <target.owl> <stepsizeInPercent>"); + System.exit(0); + } + String filename = args[0]; + String target = args[1]; + double stepSize = Double.parseDouble(args[2]); + + System.out.println("Loading ontology..."); + InputStream is = new BufferedInputStream(new FileInputStream(filename)); + if(args[0].endsWith("bz2")){ + is = new CompressorStreamFactory().createCompressorInputStream("bzip2", is); + } OWLOntologyManager man = OWLManager.createOWLOntologyManager(); - OWLOntology schema = man.loadOntologyFromOntologyDocument(new File("/home/lorenz/arbeit/papers/ESWC2012/dbpedia_0.75_no_datapropaxioms.owl")); + OWLOntology schema = man.loadOntologyFromOntologyDocument(is); + System.out.println("...done."); GreedyCohaerencyExtractor ge = new GreedyCohaerencyExtractor(); - ge.getCoharentOntology(schema); + ge.getCoherentOntology(schema, target, stepSize); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2012-02-28 13:39:32
|
Revision: 3594 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3594&view=rev Author: lorenz_b Date: 2012-02-28 13:39:21 +0000 (Tue, 28 Feb 2012) Log Message: ----------- Fixed bug. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java Modified: trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java 2012-02-28 13:07:43 UTC (rev 3593) +++ trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java 2012-02-28 13:39:21 UTC (rev 3594) @@ -58,8 +58,6 @@ for(AxiomType<? extends OWLAxiom> type : AxiomType.AXIOM_TYPES){ axiomType2AxiomsMap.put(type, new ArrayList<OWLAxiom>(ontology.getAxioms(type))); } - //omit annotation axioms here - axiomType2AxiomsMap.remove(AxiomType.ANNOTATION_ASSERTION); logger.info("Source ontology contains " + ontology.getLogicalAxiomCount() + " logical axioms."); double[] stepSizeArray = new double[axiomType2CountMap.entrySet().size()]; @@ -195,6 +193,7 @@ } OWLOntologyManager man = OWLManager.createOWLOntologyManager(); OWLOntology schema = man.loadOntologyFromOntologyDocument(is); + man.removeAxioms(schema, schema.getAxioms(AxiomType.ANNOTATION_ASSERTION)); System.out.println("...done."); GreedyCohaerencyExtractor ge = new GreedyCohaerencyExtractor(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lor...@us...> - 2012-02-28 15:46:10
|
Revision: 3596 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3596&view=rev Author: lorenz_b Date: 2012-02-28 15:46:01 +0000 (Tue, 28 Feb 2012) Log Message: ----------- Added property coherency check into script. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java Modified: trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java 2012-02-28 15:32:59 UTC (rev 3595) +++ trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java 2012-02-28 15:46:01 UTC (rev 3596) @@ -12,6 +12,8 @@ import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.SortedSet; +import java.util.TreeSet; import java.util.Map.Entry; import java.util.Set; @@ -27,6 +29,8 @@ import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; import org.semanticweb.owlapi.model.AxiomType; import org.semanticweb.owlapi.model.OWLAxiom; +import org.semanticweb.owlapi.model.OWLDataFactory; +import org.semanticweb.owlapi.model.OWLObjectProperty; import org.semanticweb.owlapi.model.OWLOntology; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyManager; @@ -40,18 +44,22 @@ private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(JustificationBasedCoherentOntologyExtractor.class); private double stepSize = 0.001; - private static final int ALLOWED_UNSATISFIABLE_CLASSES = 5; + private int allowedUnsatClasses = 5; + private int allowedUnsatProperties = 5; private OWLOntologyManager manager; private OWLOntology coherentOntology; + private OWLDataFactory factory; private IncrementalClassifier reasoner; public GreedyCohaerencyExtractor() { // TODO Auto-generated constructor stub } - public OWLOntology getCoherentOntology(OWLOntology ontology, String target, double stepSize) throws OWLOntologyCreationException{ + public OWLOntology getCoherentOntology(OWLOntology ontology, String target, double stepSize, int allowedUnsatClasses, int allowedUnsatProperties) throws OWLOntologyCreationException{ stepSize = stepSize/100; + this.allowedUnsatClasses = allowedUnsatClasses; + this.allowedUnsatProperties = allowedUnsatProperties; BidiMap<AxiomType<? extends OWLAxiom>, Integer> axiomType2CountMap = getAxiomTypeCount(ontology); Map<AxiomType<? extends OWLAxiom>, List<OWLAxiom>> axiomType2AxiomsMap = new HashMap<AxiomType<? extends OWLAxiom>, List<OWLAxiom>>(); @@ -73,6 +81,7 @@ manager = OWLManager.createOWLOntologyManager(); + factory = manager.getOWLDataFactory(); coherentOntology = manager.createOntology(); reasoner = new IncrementalClassifier(coherentOntology); @@ -92,7 +101,7 @@ Set<OWLAxiom> toAdd = new HashSet<OWLAxiom>(axiomType2AxiomsMap.get(type[i]).subList(0, x)); manager.addAxioms(coherentOntology, toAdd); axiomType2AxiomsMap.get(type[i]).removeAll(toAdd); - isCoherent = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() <= ALLOWED_UNSATISFIABLE_CLASSES; + isCoherent = isCoherent(); if(!isCoherent){ manager.removeAxioms(coherentOntology, toAdd); logger.info("Incoherency detected. Undoing changes."); @@ -124,14 +133,35 @@ return coherentOntology; } + private boolean isCoherent(){ + return (reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() <= allowedUnsatClasses) + && (getUnsatisfiableObjectProperties(reasoner).size() <= allowedUnsatProperties); + } + + private Set<OWLObjectProperty> getUnsatisfiableObjectProperties(IncrementalClassifier reasoner){ + logger.info("Computing unsatisfiable object properties..."); + long startTime = System.currentTimeMillis(); + SortedSet<OWLObjectProperty> properties = new TreeSet<OWLObjectProperty>(); + OWLDataFactory f = OWLManager.createOWLOntologyManager().getOWLDataFactory(); + for(OWLObjectProperty p : reasoner.getRootOntology().getObjectPropertiesInSignature()){ +// boolean satisfiable = reasoner.isSatisfiable(f.getOWLObjectExactCardinality(1, p)); + boolean satisfiable = reasoner.isSatisfiable(f.getOWLObjectSomeValuesFrom(p, factory.getOWLThing())); + if(!satisfiable){ + properties.add(p); + } + } + logger.info("...done in " + (System.currentTimeMillis()-startTime) + "ms."); + return properties; + } + private Set<OWLAxiom> addAxioms(List<OWLAxiom> axioms){ Set<OWLAxiom> addedAxioms = new HashSet<OWLAxiom>(); Set<OWLAxiom> axiomSet = new HashSet<OWLAxiom>(axioms); manager.addAxioms(coherentOntology, axiomSet); reasoner.classify(); - boolean isCohaerent = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom().size() <= ALLOWED_UNSATISFIABLE_CLASSES; - if(!isCohaerent){ + boolean isCoherent = isCoherent(); + if(!isCoherent){ System.out.println("Incohaerency detected. Splitting..."); manager.removeAxioms(coherentOntology, axiomSet); if(axioms.size() == 1){ @@ -154,8 +184,8 @@ return addedAxioms; } - public OWLOntology getCoherentOntology(OWLReasoner reasoner, String target, double stepSize) throws OWLOntologyCreationException{ - return getCoherentOntology(reasoner.getRootOntology(), target, stepSize); + public OWLOntology getCoherentOntology(OWLReasoner reasoner, String target, double stepSize, int allowedUnsatClasses, int allowedUnsatProperties) throws OWLOntologyCreationException{ + return getCoherentOntology(reasoner.getRootOntology(), target, stepSize, allowedUnsatClasses, allowedUnsatProperties); } private BidiMap<AxiomType<? extends OWLAxiom>, Integer> getAxiomTypeCount(OWLOntology ontology){ @@ -178,13 +208,15 @@ Logger.getRootLogger().addAppender(new ConsoleAppender(new SimpleLayout())); Logger.getRootLogger().addAppender(new FileAppender(new SimpleLayout(), "log/greedy_out.log")); - if(args.length != 3){ - System.out.println("USAGE: GreedyCoherencyExtractor <incoherent.owl> <target.owl> <stepsizeInPercent>"); + if(args.length != 5){ + System.out.println("USAGE: GreedyCoherencyExtractor <incoherent.owl> <target.owl> <stepsizeInPercent> <nrOfallowedUnsatClasses> <nrOfallowedUnsatProperties>"); System.exit(0); } String filename = args[0]; String target = args[1]; double stepSize = Double.parseDouble(args[2]); + int nrOfallowedUnsatClasses = Integer.parseInt(args[3]); + int nrOfallowedUnsatProperties = Integer.parseInt(args[4]); System.out.println("Loading ontology..."); InputStream is = new BufferedInputStream(new FileInputStream(filename)); @@ -197,7 +229,7 @@ System.out.println("...done."); GreedyCohaerencyExtractor ge = new GreedyCohaerencyExtractor(); - ge.getCoherentOntology(schema, target, stepSize); + ge.getCoherentOntology(schema, target, stepSize, nrOfallowedUnsatClasses, nrOfallowedUnsatProperties); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |