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. |