From: <jen...@us...> - 2007-09-25 18:24:02
|
Revision: 154 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=154&view=rev Author: jenslehmann Date: 2007-09-25 11:23:59 -0700 (Tue, 25 Sep 2007) Log Message: ----------- continued implementing the new base structure Modified Paths: -------------- trunk/lib/components.ini trunk/src/dl-learner/org/dllearner/algorithms/RandomGuesser.java trunk/src/dl-learner/org/dllearner/core/ComponentManager.java trunk/src/dl-learner/org/dllearner/core/ComponentTest.java trunk/src/dl-learner/org/dllearner/core/InvalidConfigOptionValueException.java trunk/src/dl-learner/org/dllearner/core/LearningAlgorithmNew.java trunk/src/dl-learner/org/dllearner/core/ReasoningService.java trunk/src/dl-learner/org/dllearner/kb/OWLFile.java trunk/src/dl-learner/org/dllearner/reasoning/DIGReasonerNew.java Modified: trunk/lib/components.ini =================================================================== --- trunk/lib/components.ini 2007-09-25 14:38:43 UTC (rev 153) +++ trunk/lib/components.ini 2007-09-25 18:23:59 UTC (rev 154) @@ -1,3 +1,8 @@ // list of all components DL-Learner should use // (if you implement your own components add them here) +# knowledge sources +org.dllearner.kb.OWLFile +# reasoners +# learning problems org.dllearner.learningproblems.DefinitionLPTwoValued +# learning algorithms Modified: trunk/src/dl-learner/org/dllearner/algorithms/RandomGuesser.java =================================================================== --- trunk/src/dl-learner/org/dllearner/algorithms/RandomGuesser.java 2007-09-25 14:38:43 UTC (rev 153) +++ trunk/src/dl-learner/org/dllearner/algorithms/RandomGuesser.java 2007-09-25 18:23:59 UTC (rev 154) @@ -1,12 +1,21 @@ package org.dllearner.algorithms; +import java.util.Collection; +import java.util.LinkedList; + import org.dllearner.LearningProblem; import org.dllearner.Score; import org.dllearner.algorithms.gp.Program; import org.dllearner.algorithms.gp.GPUtilities; +import org.dllearner.core.ConfigEntry; +import org.dllearner.core.ConfigOption; +import org.dllearner.core.IntegerConfigOption; +import org.dllearner.core.InvalidConfigOptionValueException; +import org.dllearner.core.LearningAlgorithmNew; +import org.dllearner.core.LearningProblemNew; import org.dllearner.core.dl.Concept; -public class RandomGuesser implements LearningAlgorithm { +public class RandomGuesser extends LearningAlgorithmNew implements LearningAlgorithm { private Concept bestDefinition = null; private Score bestScore; @@ -15,6 +24,37 @@ private int numberOfTrees; private int maxDepth; + public RandomGuesser(LearningProblemNew lp) { + + } + + public static Collection<ConfigOption<?>> createConfigOptions() { + Collection<ConfigOption<?>> options = new LinkedList<ConfigOption<?>>(); + options.add(new IntegerConfigOption("numberOfTrees")); + options.add(new IntegerConfigOption("maxDepth")); + return options; + } + + /* (non-Javadoc) + * @see org.dllearner.core.Component#applyConfigEntry(org.dllearner.core.ConfigEntry) + */ + @Override + public <T> void applyConfigEntry(ConfigEntry<T> entry) throws InvalidConfigOptionValueException { + String name = entry.getOptionName(); + if (name.equals("numberOfTrees")) + numberOfTrees = (Integer) entry.getValue(); + else if(name.equals("maxDepth")) + maxDepth = (Integer) entry.getValue(); + } + + /* (non-Javadoc) + * @see org.dllearner.core.Component#init() + */ + @Override + public void init() { + // TODO Auto-generated method stub + + } /** * Generiert zufaellig Loesungen. @@ -26,6 +66,7 @@ this.maxDepth = maxDepth; } + @Override public void start() { // this.learningProblem = learningProblem; @@ -50,16 +91,21 @@ // System.out.println(bestScore); } + @Override public Score getSolutionScore() { return bestScore; } + @Override public Concept getBestSolution() { return bestDefinition; } + @Override public void stop() { // TODO Auto-generated method stub } + + } Modified: trunk/src/dl-learner/org/dllearner/core/ComponentManager.java =================================================================== --- trunk/src/dl-learner/org/dllearner/core/ComponentManager.java 2007-09-25 14:38:43 UTC (rev 153) +++ trunk/src/dl-learner/org/dllearner/core/ComponentManager.java 2007-09-25 18:23:59 UTC (rev 154) @@ -27,15 +27,16 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeSet; -import org.dllearner.kb.OWLFile; - /** * Central manager class for DL-Learner. * @@ -44,23 +45,30 @@ */ public class ComponentManager { + // these variables are valid for the complete lifetime of DL-Learner private static String componentsFile = "lib/components.ini"; - private static ComponentManager cm = new ComponentManager(); + private static Set<Class<? extends Component>> components; - private List<Class<? extends Component>> components; - // list of all configuration options of all components private Map<Class<? extends Component>,List<ConfigOption<?>>> componentOptions; private Map<Class<? extends Component>,Map<String,ConfigOption<?>>> componentOptionsByName; + private Comparator<Class<?>> classComparator = new Comparator<Class<?>>() { + + public int compare(Class<?> c1, Class<?> c2) { + return c1.getName().compareTo(c2.getName()); + } + + }; + @SuppressWarnings({"unchecked"}) private ComponentManager() { // read in components file List<String> componentsString = readComponentsFile(); // component list - components = new LinkedList<Class<? extends Component>>(); + components = new TreeSet<Class<? extends Component>>(classComparator); // create classes from strings for(String componentString : componentsString) { @@ -151,10 +159,10 @@ */ public <T> void applyConfigEntry(Component component, String optionName, T value) { // first we look whether the component is registered - if(components.contains(component)) { + if(components.contains(component.getClass())) { // look for a config option with the specified name - ConfigOption<?> option = (ConfigOption<?>) componentOptionsByName.get(component).get(optionName); + ConfigOption<?> option = (ConfigOption<?>) componentOptionsByName.get(component.getClass()).get(optionName); if(option!=null) { // check whether the given object has the correct type if(!option.checkType(value)) { @@ -204,9 +212,15 @@ return null; } + public <T extends ReasonerComponent> ReasoningService reasoningService(Class<T> reasoner, KnowledgeSource source) { + Set<KnowledgeSource> sources = new HashSet<KnowledgeSource>(); + sources.add(source); + return reasoningService(reasoner, sources); + } + public <T extends ReasonerComponent> ReasoningService reasoningService(Class<T> reasoner, Set<KnowledgeSource> sources) { try { - Constructor<T> constructor = reasoner.getConstructor(ReasonerComponent.class); + Constructor<T> constructor = reasoner.getConstructor(Set.class); T reasonerInstance = constructor.newInstance(sources); return new ReasoningService(reasonerInstance); } catch (IllegalArgumentException e) { @@ -232,9 +246,9 @@ return null; } - public <T extends LearningProblemNew> T learningProblem(Class<T> lp, ReasonerComponent reasoner) { + public <T extends LearningProblemNew> T learningProblem(Class<T> lp, ReasoningService reasoner) { try { - Constructor<T> constructor = lp.getConstructor(ReasonerComponent.class); + Constructor<T> constructor = lp.getConstructor(ReasoningService.class); return constructor.newInstance(reasoner); } catch (SecurityException e) { // TODO Auto-generated catch block @@ -259,4 +273,31 @@ return null; } + public <T extends LearningAlgorithmNew> T learningAlgorithm(Class<T> la, LearningProblemNew lp) { + try { + Constructor<T> constructor = la.getConstructor(LearningProblemNew.class); + return constructor.newInstance(lp); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return null; + } + } Modified: trunk/src/dl-learner/org/dllearner/core/ComponentTest.java =================================================================== --- trunk/src/dl-learner/org/dllearner/core/ComponentTest.java 2007-09-25 14:38:43 UTC (rev 153) +++ trunk/src/dl-learner/org/dllearner/core/ComponentTest.java 2007-09-25 18:23:59 UTC (rev 154) @@ -19,7 +19,13 @@ */ package org.dllearner.core; +import java.io.File; +import java.net.MalformedURLException; + +import org.dllearner.algorithms.RandomGuesser; import org.dllearner.kb.OWLFile; +import org.dllearner.learningproblems.DefinitionLPTwoValued; +import org.dllearner.reasoning.DIGReasonerNew; /** * Test for component based design. @@ -34,18 +40,35 @@ */ public static void main(String[] args) { + String example = null; + try { + example = new File("examples/father.owl").toURI().toURL().toString(); + } catch (MalformedURLException e) { + e.printStackTrace(); + System.exit(0); + } + // get singleton instance of component manager ComponentManager cm = ComponentManager.getInstance(); // create knowledge source KnowledgeSource source = cm.knowledgeSource(OWLFile.class); - cm.applyConfigEntry(source, "url", "father.owl"); + cm.applyConfigEntry(source, "url", example); + source.init(); - // ... to be continued ... + ReasoningService rs = cm.reasoningService(DIGReasonerNew.class, source); + rs.init(); - // ReasonerComponent reasoner = new ReasonerComponent(); - // ComponentManager.getInstance().learningProblem(LearningProblemNew.class, reasoner); - + LearningProblemNew lp = cm.learningProblem(DefinitionLPTwoValued.class, rs); + // ... add positive and negative examples here ... + lp.init(); + + LearningAlgorithmNew la = cm.learningAlgorithm(RandomGuesser.class, lp); + la.init(); + + // la.start(); + + System.out.println(la.getBestSolution()); } } Modified: trunk/src/dl-learner/org/dllearner/core/InvalidConfigOptionValueException.java =================================================================== --- trunk/src/dl-learner/org/dllearner/core/InvalidConfigOptionValueException.java 2007-09-25 14:38:43 UTC (rev 153) +++ trunk/src/dl-learner/org/dllearner/core/InvalidConfigOptionValueException.java 2007-09-25 18:23:59 UTC (rev 154) @@ -30,5 +30,9 @@ public InvalidConfigOptionValueException(ConfigOption<?> option, Object value) { super("The value " + value + " is not valid for the configuration option " + option + "."); } - + + public InvalidConfigOptionValueException(ConfigOption<?> option, Object value, String reason) { + super("The value " + value + " is not valid for the configuration option " + option + ". Reason: " + reason + "."); + } + } Modified: trunk/src/dl-learner/org/dllearner/core/LearningAlgorithmNew.java =================================================================== --- trunk/src/dl-learner/org/dllearner/core/LearningAlgorithmNew.java 2007-09-25 14:38:43 UTC (rev 153) +++ trunk/src/dl-learner/org/dllearner/core/LearningAlgorithmNew.java 2007-09-25 18:23:59 UTC (rev 154) @@ -19,12 +19,39 @@ */ package org.dllearner.core; +import org.dllearner.Score; +import org.dllearner.core.dl.Concept; + /** * @author Jens Lehmann * */ public abstract class LearningAlgorithmNew extends Component { + /** + * Starts the algorithm. + * + */ + public abstract void start(); + + /** + * Stops the algorithm gracefully. + * + */ + public abstract void stop(); + /** + * Every algorithm must be able to return the score of the + * best solution found. + * @return Best score. + */ + public abstract Score getSolutionScore(); + + /** + * Returns the best solutions obtained so far. + * @return Best solution. + */ + public abstract Concept getBestSolution(); + } Modified: trunk/src/dl-learner/org/dllearner/core/ReasoningService.java =================================================================== --- trunk/src/dl-learner/org/dllearner/core/ReasoningService.java 2007-09-25 14:38:43 UTC (rev 153) +++ trunk/src/dl-learner/org/dllearner/core/ReasoningService.java 2007-09-25 18:23:59 UTC (rev 154) @@ -95,11 +95,20 @@ public ReasoningService(Reasoner reasoner) { this.reasoner = reasoner; + resetStatistics(); + } + + public ReasoningService(ReasonerComponent reasoner) { + this.reasoner = reasoner; + } + + public void init() { + // temporary ugly hack to keep old version working + ((ReasonerComponent)reasoner).init(); + // Listenansicht atomicConceptsList = new LinkedList<AtomicConcept>(getAtomicConcepts()); - atomicRolesList = new LinkedList<AtomicRole>(getAtomicRoles()); - - resetStatistics(); + atomicRolesList = new LinkedList<AtomicRole>(getAtomicRoles()); } // zurücksetzen aller Statistiken (wenn z.B. vorher ein Satisfiability Check gemacht wird, @@ -417,7 +426,7 @@ public List<AtomicRole> getAtomicRolesList() { return atomicRolesList; } - + public long getInstanceCheckReasoningTimeNs() { return instanceCheckReasoningTimeNs; } Modified: trunk/src/dl-learner/org/dllearner/kb/OWLFile.java =================================================================== --- trunk/src/dl-learner/org/dllearner/kb/OWLFile.java 2007-09-25 14:38:43 UTC (rev 153) +++ trunk/src/dl-learner/org/dllearner/kb/OWLFile.java 2007-09-25 18:23:59 UTC (rev 154) @@ -19,8 +19,10 @@ */ package org.dllearner.kb; +import java.io.File; import java.net.MalformedURLException; import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.util.Collection; import java.util.LinkedList; @@ -60,13 +62,27 @@ String s = (String) entry.getValue(); try { url = new URL(s); + // File f = new File(url.toURI()); + //if(!f.canRead()) + // throw new InvalidConfigOptionValueException(entry.getOption(), entry.getValue()); } catch (MalformedURLException e) { - // e.printStackTrace(); - throw new InvalidConfigOptionValueException(entry.getOption(), entry.getValue()); - } + throw new InvalidConfigOptionValueException(entry.getOption(), entry.getValue(),"malformed URL " + s); + } //catch (URISyntaxException e) { + // TODO Auto-generated catch block + //e.printStackTrace(); + //} } } + /* (non-Javadoc) + * @see org.dllearner.core.Component#init() + */ + @Override + public void init() { + // TODO Auto-generated method stub + + } + /* * (non-Javadoc) * @@ -78,13 +94,6 @@ return OWLAPIDIGConverter.getTellsString(url, OntologyFileFormat.RDF_XML, kbURI); } - /* (non-Javadoc) - * @see org.dllearner.core.Component#init() - */ - @Override - public void init() { - // TODO Auto-generated method stub - - } + } Modified: trunk/src/dl-learner/org/dllearner/reasoning/DIGReasonerNew.java =================================================================== --- trunk/src/dl-learner/org/dllearner/reasoning/DIGReasonerNew.java 2007-09-25 14:38:43 UTC (rev 153) +++ trunk/src/dl-learner/org/dllearner/reasoning/DIGReasonerNew.java 2007-09-25 18:23:59 UTC (rev 154) @@ -43,19 +43,16 @@ import org.dllearner.core.InvalidConfigOptionValueException; import org.dllearner.core.KnowledgeSource; import org.dllearner.core.ReasonerComponent; -import org.dllearner.core.ReasoningMethodUnsupportedException; import org.dllearner.core.StringConfigOption; import org.dllearner.core.dl.AtomicConcept; import org.dllearner.core.dl.AtomicRole; import org.dllearner.core.dl.Bottom; import org.dllearner.core.dl.Concept; import org.dllearner.core.dl.Individual; -import org.dllearner.core.dl.KB; import org.dllearner.core.dl.Top; import org.dllearner.utilities.ConceptComparator; import org.dllearner.utilities.Helper; import org.dllearner.utilities.RoleComparator; -import org.dllearner.utilities.SortedSetTuple; import org.kr.dl.dig.v1_1.Concepts; import org.kr.dl.dig.v1_1.Csynonyms; import org.kr.dl.dig.v1_1.IdType; @@ -728,23 +725,6 @@ // kaon2Reasoner.saveOntology(file, format); } - /* (non-Javadoc) - * @see org.dllearner.core.Reasoner#doubleRetrieval(org.dllearner.core.dl.Concept) - */ - public SortedSetTuple<Individual> doubleRetrieval(Concept concept) - throws ReasoningMethodUnsupportedException { - // TODO Auto-generated method stub - return null; - } - - /* (non-Javadoc) - * @see org.dllearner.core.Reasoner#doubleRetrieval(org.dllearner.core.dl.Concept, org.dllearner.core.dl.Concept) - */ - public SortedSetTuple<Individual> doubleRetrieval(Concept concept, Concept adc) - throws ReasoningMethodUnsupportedException { - // TODO Auto-generated method stub - return null; - } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |