From: <lor...@us...> - 2011-12-09 12:54:34
|
Revision: 3494 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3494&view=rev Author: lorenz_b Date: 2011-12-09 12:54:27 +0000 (Fri, 09 Dec 2011) Log Message: ----------- Started justification based coherent ontology extractor. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/utilities/CoherentOntologyExtractor.java trunk/components-core/src/main/java/org/dllearner/utilities/JustificationBasedCoherentOntologyExtractor.java trunk/components-core/src/main/java/org/dllearner/utilities/MapUtils.java Added: trunk/components-core/src/main/java/org/dllearner/utilities/CoherentOntologyExtractor.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/CoherentOntologyExtractor.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/utilities/CoherentOntologyExtractor.java 2011-12-09 12:54:27 UTC (rev 3494) @@ -0,0 +1,9 @@ +package org.dllearner.utilities; + +import org.semanticweb.owlapi.model.OWLOntology; + +public interface CoherentOntologyExtractor { + + OWLOntology getCoherentOntology(OWLOntology incoherentOntology); + +} 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-09 09:22:49 UTC (rev 3493) +++ trunk/components-core/src/main/java/org/dllearner/utilities/GreedyCohaerencyExtractor.java 2011-12-09 12:54:27 UTC (rev 3494) @@ -1,62 +1,107 @@ package org.dllearner.utilities; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import org.apache.commons.collections15.BidiMap; import org.apache.commons.collections15.bidimap.DualHashBidiMap; import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.io.RDFXMLOntologyFormat; 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.model.OWLOntologyStorageException; import org.semanticweb.owlapi.reasoner.OWLReasoner; import com.clarkparsia.modularity.IncrementalClassifier; public class GreedyCohaerencyExtractor { + private static final double STEP_SIZE = 0.001; + private static final int ALLOWED_UNSATISFIABLE_CLASSES = 5; + public GreedyCohaerencyExtractor() { // TODO Auto-generated constructor stub } 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>>(); 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()]; + 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(); + type[i] = entry.getKey(); + cnt[i] = 0; + i++; + } - int lcm = lcm(new ArrayList<Integer>(axiomType2CountMap.values())); OWLOntologyManager man = OWLManager.createOWLOntologyManager(); - man.addOntologyChangeListener(reasoner); OWLOntology cohaerentOntology = man.createOntology(); + IncrementalClassifier reasoner = new IncrementalClassifier(cohaerentOntology); + man.addOntologyChangeListener(reasoner); + reasoner.classify(); + + boolean isCohaerent = true; - for(int i = 0; i < lcm; i++){ + for(double j = 0; j < 1; j += STEP_SIZE){System.out.println(j); 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; - } + for(i = 0; i < stepSize.length; i++){ + cnt[i] = cnt[i] + stepSize[i]; + 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); +// break; +// } +// } + Set<OWLAxiom> toAdd = new HashSet<OWLAxiom>(axiomType2AxiomsMap.get(type[i]).subList(0, x)); + man.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"); + break; } + cnt[i] = cnt[i] - x; } } + System.out.println(cohaerentOntology.getLogicalAxiomCount()); + } + try { + man.saveOntology(cohaerentOntology, new RDFXMLOntologyFormat(), new BufferedOutputStream(new FileOutputStream(new File("coherent.owl")))); + } catch (OWLOntologyStorageException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } return cohaerentOntology; } @@ -68,47 +113,22 @@ 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)); + int cnt = ontology.getAxiomCount(type); + if(cnt > 0){ + axiomType2CountMap.put(type, Integer.valueOf(cnt)); + } + Set<? extends OWLAxiom> axioms = ontology.getAxioms(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); - } + 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")); + + GreedyCohaerencyExtractor ge = new GreedyCohaerencyExtractor(); + ge.getCoharentOntology(schema); } } Added: trunk/components-core/src/main/java/org/dllearner/utilities/JustificationBasedCoherentOntologyExtractor.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/JustificationBasedCoherentOntologyExtractor.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/utilities/JustificationBasedCoherentOntologyExtractor.java 2011-12-09 12:54:27 UTC (rev 3494) @@ -0,0 +1,139 @@ +package org.dllearner.utilities; + +import java.io.File; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.mindswap.pellet.RBox; +import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.model.OWLAxiom; +import org.semanticweb.owlapi.model.OWLClass; +import org.semanticweb.owlapi.model.OWLEntity; +import org.semanticweb.owlapi.model.OWLLogicalAxiom; +import org.semanticweb.owlapi.model.OWLOntology; +import org.semanticweb.owlapi.model.OWLOntologyCreationException; +import org.semanticweb.owlapi.model.OWLOntologyManager; +import org.semanticweb.owlapi.model.RemoveAxiom; + +import uk.ac.manchester.cs.owlapi.modularity.ModuleType; + +import com.clarkparsia.modularity.IncrementalClassifier; +import com.clarkparsia.modularity.ModularityUtils; +import com.clarkparsia.owlapi.explanation.PelletExplanation; +import com.clarkparsia.owlapiv3.OntologyUtils; + +public class JustificationBasedCoherentOntologyExtractor implements CoherentOntologyExtractor{ + + private static final int NUMBER_OF_JUSTIFICATIONS = 1; +// private PelletReasoner reasoner; + private IncrementalClassifier reasoner; + + private OWLOntology incoherentOntology; + private OWLOntology ontology; + + static {PelletExplanation.setup();} + + @Override + public OWLOntology getCoherentOntology(OWLOntology ontology) { + this.ontology = ontology; + this.incoherentOntology = getOntologyWithoutAnnotations(ontology); + +// reasoner = PelletReasonerFactory.getInstance().createNonBufferingReasoner(incoherentOntology); +// reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY); + reasoner = new IncrementalClassifier(incoherentOntology); + reasoner.classify(); + + OWLOntologyManager man = incoherentOntology.getOWLOntologyManager(); +// man.addOntologyChangeListener(reasoner); + + Set<OWLClass> unsatClasses = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom(); + + //if the ontology is not incoherent we return it here + if(unsatClasses.isEmpty()){ + return incoherentOntology; + } + + while(!unsatClasses.isEmpty()){ + //for each unsatisfiable class we compute n justifications here and count how often each axiom occurs globally + Map<OWLAxiom, Integer> axiom2CountMap = new HashMap<OWLAxiom, Integer>(); + for(OWLClass unsatClass : unsatClasses){ + Set<Set<OWLAxiom>> explanations = computeExplanations(unsatClass); + for(Set<OWLAxiom> explanation : explanations){ + for(OWLAxiom ax : explanation){ + Integer cnt = axiom2CountMap.get(ax); + if(cnt == null){ + cnt = 0; + } + cnt = cnt + 1; + axiom2CountMap.put(ax, cnt); + } + } + } + //get a sorted list of entries with the highest axiom count first + List<Entry<OWLAxiom, Integer>> sortedEntries = MapUtils.sortByValues(axiom2CountMap); + for(Entry<OWLAxiom, Integer> entry : sortedEntries){ + System.out.println(entry.getKey() + ":" + entry.getValue()); + } + //we remove the most occuring axiom + OWLAxiom toRemove = sortedEntries.get(0).getKey(); + man.removeAxiom(incoherentOntology, toRemove); + man.applyChange(new RemoveAxiom(incoherentOntology, toRemove)); + reasoner.classify(); + unsatClasses = reasoner.getUnsatisfiableClasses().getEntitiesMinusBottom(); + } + System.out.println(incoherentOntology.getLogicalAxiomCount()); + + return getOntologyWithAnnotations(incoherentOntology); + } + + private OWLOntology getOntologyWithoutAnnotations(OWLOntology ontology){ + try { + OWLOntologyManager man = ontology.getOWLOntologyManager(); + OWLOntology ontologyWithoutAnnotations = ontology.getOWLOntologyManager().createOntology(); + for(OWLAxiom ax : ontology.getLogicalAxioms()){ + man.addAxiom(ontologyWithoutAnnotations, ax.getAxiomWithoutAnnotations()); + } + return ontologyWithoutAnnotations; + } catch (OWLOntologyCreationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + + private OWLOntology getOntologyWithAnnotations(OWLOntology ontologyWithOutAnnotations){ + OWLOntologyManager man = ontology.getOWLOntologyManager(); + for (Iterator<OWLLogicalAxiom> iterator = ontology.getLogicalAxioms().iterator(); iterator.hasNext();) { + OWLLogicalAxiom axiom = iterator.next(); + if(!ontologyWithOutAnnotations.containsAxiomIgnoreAnnotations(axiom)){ + man.removeAxiom(ontology, axiom); + } + } + return ontology; + } + + private Set<Set<OWLAxiom>> computeExplanations(OWLClass unsatClass){ + OWLOntology module = OntologyUtils.getOntologyFromAxioms( + ModularityUtils.extractModule(incoherentOntology, Collections.singleton((OWLEntity)unsatClass), ModuleType.TOP_OF_BOT)); + PelletExplanation expGen = new PelletExplanation(module); + return expGen.getUnsatisfiableExplanations(unsatClass, NUMBER_OF_JUSTIFICATIONS); + } + + public static void main(String[] args) throws Exception{ + Logger.getLogger(RBox.class.getName()).setLevel(Level.OFF); + OWLOntologyManager man = OWLManager.createOWLOntologyManager(); + OWLOntology schema = man.loadOntologyFromOntologyDocument(new File("../components-core/cohaerent.owl")); +// OWLOntology schema = man.loadOntologyFromOntologyDocument(new File("/home/lorenz/arbeit/dbpedia_0.75_no_datapropaxioms.owl")); + + JustificationBasedCoherentOntologyExtractor extractor = new JustificationBasedCoherentOntologyExtractor(); + OWLOntology coherentOntology = extractor.getCoherentOntology(schema);System.out.println(coherentOntology.getLogicalAxiomCount()); + } + +} Added: trunk/components-core/src/main/java/org/dllearner/utilities/MapUtils.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/MapUtils.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/utilities/MapUtils.java 2011-12-09 12:54:27 UTC (rev 3494) @@ -0,0 +1,41 @@ +package org.dllearner.utilities; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +public class MapUtils { + + /** + * Returns a list of entries sorted by the values descending. + * @param map + * @return + */ + public static <K, V extends Comparable<V>> List<Entry<K, V>> sortByValues(Map<K, V> map){ + return sortByValues(map, false); + } + + /** + * Returns a list of entries sorted by the values either ascending or descending. + * @param map + * @return + */ + public static <K, V extends Comparable<V>> List<Entry<K, V>> sortByValues(Map<K, V> map, final boolean ascending){ + List<Entry<K, V>> entries = new ArrayList<Entry<K, V>>(map.entrySet()); + Collections.sort(entries, new Comparator<Entry<K, V>>() { + + @Override + public int compare(Entry<K, V> o1, Entry<K, V> o2) { + if(ascending){ + return o1.getValue().compareTo(o2.getValue()); + } else { + return o2.getValue().compareTo(o1.getValue()); + } + } + }); + return entries; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |