From: <lor...@us...> - 2012-02-08 11:49:26
|
Revision: 3576 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3576&view=rev Author: lorenz_b Date: 2012-02-08 11:49:16 +0000 (Wed, 08 Feb 2012) Log Message: ----------- Remove only axioms not contained in the original DBpedia ontology. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/utilities/JustificationBasedCoherentOntologyExtractor.java Modified: trunk/components-core/src/main/java/org/dllearner/utilities/JustificationBasedCoherentOntologyExtractor.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/utilities/JustificationBasedCoherentOntologyExtractor.java 2012-02-07 21:43:20 UTC (rev 3575) +++ trunk/components-core/src/main/java/org/dllearner/utilities/JustificationBasedCoherentOntologyExtractor.java 2012-02-08 11:49:16 UTC (rev 3576) @@ -4,6 +4,10 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Collections; @@ -20,6 +24,8 @@ import openlink.util.MD5; +import org.apache.commons.compress.compressors.CompressorException; +import org.apache.commons.compress.compressors.CompressorStreamFactory; import org.mindswap.pellet.RBox; import org.semanticweb.HermiT.Configuration; import org.semanticweb.HermiT.Reasoner; @@ -39,6 +45,7 @@ import org.semanticweb.owlapi.model.OWLOntologyStorageException; import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom; import org.semanticweb.owlapi.model.RemoveAxiom; +import org.semanticweb.owlapi.owllink.builtin.requests.LoadOntologies; import org.semanticweb.owlapi.reasoner.IllegalConfigurationException; import org.semanticweb.owlapi.reasoner.OWLReasoner; import org.semanticweb.owlapi.reasoner.OWLReasonerConfiguration; @@ -83,12 +90,15 @@ //whether to debug classes and properties in parallel private boolean computeParallel = false; + private OWLOntology dbpediaOntology; + public JustificationBasedCoherentOntologyExtractor() { try { md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } + dbpediaOntology = loadDBpediaOntology(); } static {PelletExplanation.setup();} @@ -185,19 +195,14 @@ } logger.info("...done in " + (System.currentTimeMillis()-startTime) + "ms."); - while(!unsatClasses.isEmpty()){ - //get frequency for each axiom - Map<OWLAxiom, Integer> axiom2CountMap = getAxiomFrequency(entity2Explanations); + if(computeParallel){ + cnt += unsatPropCnt; + } + + while(cnt >= 0){ + //we remove the most appropriate axiom from the ontology + removeAppropriateAxiom(); - //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 frequent axiom from the ontology - OWLAxiom toRemove = sortedEntries.get(0).getKey(); - removeAxiom(toRemove); - //recompute the unsatisfiable classes logger.info("Reclassifying..."); startTime = System.currentTimeMillis(); @@ -229,6 +234,9 @@ save("log/dbpedia_" + cnt + "cls" + unsatPropCnt + "prop.owl"); cnt = rootCnt + derivedCnt; unsatPropCnt = unsatObjectProperties.size(); + if(computeParallel){ + cnt += unsatPropCnt; + } } //recompute explanations if necessary @@ -251,15 +259,9 @@ entity2ModuleMap.putAll(extractModules(unsatObjectProperties)); while(!unsatObjectProperties.isEmpty()){ - //get frequency for each axiom - Map<OWLAxiom, Integer> axiom2CountMap = getAxiomFrequency(entity2Explanations); + //we remove the most appropriate axiom from the ontology + removeAppropriateAxiom(); - //get a sorted list of entries with the highest axiom count first - List<Entry<OWLAxiom, Integer>> sortedEntries = MapUtils.sortByValues(axiom2CountMap); - //we remove the most frequent axiom from the ontology - OWLAxiom toRemove = sortedEntries.get(0).getKey(); - removeAxiom(toRemove); - //recompute the unsatisfiable classes logger.info("Reclassifying..."); startTime = System.currentTimeMillis(); @@ -324,18 +326,9 @@ logger.info("...done in " + (System.currentTimeMillis()-startTime) + "ms."); while(!unsatClasses.isEmpty()){ - //get frequency for each axiom - Map<OWLAxiom, Integer> axiom2CountMap = getAxiomFrequency(entity2Explanations); + //we remove the most appropriate axiom from the ontology + removeAppropriateAxiom(); - //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 frequent axiom from the ontology - OWLAxiom toRemove = sortedEntries.get(0).getKey(); - removeAxiom(toRemove); - //recompute the unsatisfiable classes logger.info("Reclassifying..."); startTime = System.currentTimeMillis(); @@ -375,13 +368,24 @@ return getOntologyWithAnnotations(incoherentOntology); } - private void removeAxiom(OWLAxiom axiom){ - logger.info("Removing axiom " + axiom + "."); - manager.removeAxiom(incoherentOntology, axiom); - manager.addAxiom(diffOntology, axiom); - manager.applyChange(new RemoveAxiom(incoherentOntology, axiom)); - removeFromExplanations(entity2Explanations, axiom); - removeFromModules(axiom); + private void removeAppropriateAxiom(){ + //get frequency for each axiom + Map<OWLAxiom, Integer> axiom2CountMap = getAxiomFrequency(entity2Explanations); + //get a sorted list of entries with the highest axiom count first + List<Entry<OWLAxiom, Integer>> sortedEntries = MapUtils.sortByValues(axiom2CountMap); + //we remove the most frequent axiom from the ontology which is not contained in the original DBpedia ontology + for(Entry<OWLAxiom, Integer> e : sortedEntries){ + OWLAxiom axiom = e.getKey(); + if(!dbpediaOntology.containsAxiomIgnoreAnnotations(axiom)){ + logger.info("Removing axiom " + axiom + "."); + manager.removeAxiom(incoherentOntology, axiom); + manager.addAxiom(diffOntology, axiom); + manager.applyChange(new RemoveAxiom(incoherentOntology, axiom)); + removeFromExplanations(entity2Explanations, axiom); + removeFromModules(axiom); + return; + } + } } private void save(String fileName){ @@ -603,6 +607,28 @@ return module; } + private OWLOntology loadDBpediaOntology() { + long startTime = System.currentTimeMillis(); + logger.info("Loading DBpedia reference ontology..."); + OWLOntology ontology = null; + try { + URL dbpediaURL = new URL("http://downloads.dbpedia.org/3.7/dbpedia_3.7.owl.bz2"); + InputStream is = dbpediaURL.openStream(); + is = new CompressorStreamFactory().createCompressorInputStream("bzip2", is); + ontology = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(is); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (OWLOntologyCreationException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (CompressorException e) { + e.printStackTrace(); + } + logger.info("...done in " + (System.currentTimeMillis()-startTime) + "ms."); + return ontology; + } + // private Map<OWLClass, OWLOntology> extractModules(Set<OWLClass> classes){ // Map<OWLClass, OWLOntology> cls2ModuleMap = new HashMap<OWLClass, OWLOntology>(); // for(OWLClass cls : classes){ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |