From: <jen...@us...> - 2011-09-02 11:30:00
|
Revision: 3229 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3229&view=rev Author: jenslehmann Date: 2011-09-02 11:29:54 +0000 (Fri, 02 Sep 2011) Log Message: ----------- used BeanUtils to enable access to private Java Bean fields; parameter output in Enrichment script now working Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/algorithms/celoe/CELOE.java trunk/components-core/src/main/java/org/dllearner/core/config/ConfigHelper.java trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java Added Paths: ----------- trunk/components-core/src/test/java/org/dllearner/test/junit/ConfigOptionTest.java Modified: trunk/components-core/src/main/java/org/dllearner/algorithms/celoe/CELOE.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/algorithms/celoe/CELOE.java 2011-09-02 10:53:32 UTC (rev 3228) +++ trunk/components-core/src/main/java/org/dllearner/algorithms/celoe/CELOE.java 2011-09-02 11:29:54 UTC (rev 3229) @@ -167,7 +167,8 @@ private int maxClassDescriptionTests = 0; - private int maxExecutionTimeInSeconds = 100; + @org.dllearner.core.config.ConfigOption(defaultValue = "10", name = "maxExecutionTimeInSeconds", description = "maximum execution of the algorithm in seconds") + private int maxExecutionTimeInSeconds = 10; private boolean terminateOnNoiseReached = false; Modified: trunk/components-core/src/main/java/org/dllearner/core/config/ConfigHelper.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/config/ConfigHelper.java 2011-09-02 10:53:32 UTC (rev 3228) +++ trunk/components-core/src/main/java/org/dllearner/core/config/ConfigHelper.java 2011-09-02 11:29:54 UTC (rev 3229) @@ -31,6 +31,8 @@ import org.dllearner.algorithms.properties.ObjectPropertyDomainAxiomLearner; import org.dllearner.core.Component; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.BeansException; public class ConfigHelper { @@ -97,6 +99,7 @@ * @param component The component to analyse. * @return All config options of the component with their respective value. */ + @Deprecated public static Map<ConfigOption,String> getConfigOptionValuesString(Component component) { Map<ConfigOption,String> optionValues = new HashMap<ConfigOption,String>(); List<Field> fields = getAllFields(component);//getConfigOptions(component).getClass().getDeclaredFields(); @@ -133,11 +136,18 @@ ConfigOption option = field.getAnnotation(ConfigOption.class); if(option != null) { try { - optionValues.put(option, field.get(component)); + // we invoke the public getter instead of accessing a private field (may cause problem with SecurityManagers) + // use Spring BeanUtils TODO: might be unnecessarily slow because we already have the field? + Object value = BeanUtils.getPropertyDescriptor(component.getClass(), field.getName()).getReadMethod().invoke(component); + optionValues.put(option, value); } catch (IllegalArgumentException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); + } catch (BeansException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); } } } Added: trunk/components-core/src/test/java/org/dllearner/test/junit/ConfigOptionTest.java =================================================================== --- trunk/components-core/src/test/java/org/dllearner/test/junit/ConfigOptionTest.java (rev 0) +++ trunk/components-core/src/test/java/org/dllearner/test/junit/ConfigOptionTest.java 2011-09-02 11:29:54 UTC (rev 3229) @@ -0,0 +1,59 @@ +/** + * Copyright (C) 2007-2011, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package org.dllearner.test.junit; + +import static org.junit.Assert.*; + +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.dllearner.algorithms.celoe.CELOE; +import org.dllearner.core.config.ConfigHelper; +import org.dllearner.core.config.ConfigOption; +import org.junit.Test; + +/** + * + * @author Jens Lehmann + * + */ +public class ConfigOptionTest { + + @Test + public void testConfigOption() { + List<ConfigOption> configOptions = ConfigHelper.getConfigOptions(CELOE.class); + assertFalse(configOptions.isEmpty()); + + CELOE celoe = new CELOE(); + celoe.setMaxExecutionTimeInSeconds(10); + Map<ConfigOption,Object> optionValues = ConfigHelper.getConfigOptionValues(celoe); + boolean found = false; + for(Entry<ConfigOption,Object> entry : optionValues.entrySet()) { + System.out.println(entry.getKey() + " " + entry.getValue()); + if(entry.getKey().name().equals("maxExecutionTimeInSeconds")) { + found = true; + assertTrue(Integer.valueOf(entry.getValue().toString())==10); + } + } + assertTrue(found); + + } + +} Modified: trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2011-09-02 10:53:32 UTC (rev 3228) +++ trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2011-09-02 11:29:54 UTC (rev 3229) @@ -143,9 +143,9 @@ // since otherwise we run into memory problems for full enrichment private Class<? extends LearningAlgorithm> algorithm; private List<EvaluatedAxiom> axioms; - private Map<ConfigOption,String> parameters; + private Map<ConfigOption,Object> parameters; - public AlgorithmRun(Class<? extends LearningAlgorithm> algorithm, List<EvaluatedAxiom> axioms, Map<ConfigOption,String> parameters) { + public AlgorithmRun(Class<? extends LearningAlgorithm> algorithm, List<EvaluatedAxiom> axioms, Map<ConfigOption,Object> parameters) { this.algorithm = algorithm; this.axioms = axioms; this.parameters = parameters; @@ -159,7 +159,7 @@ return axioms; } - public Map<ConfigOption, String> getParameters() { + public Map<ConfigOption, Object> getParameters() { return parameters; } } @@ -403,7 +403,7 @@ } System.out.println(prettyPrint(learnedAxioms)); - algorithmRuns.add(new AlgorithmRun(CELOE.class, learnedAxioms, ConfigHelper.getConfigOptionValuesString(la))); + algorithmRuns.add(new AlgorithmRun(CELOE.class, learnedAxioms, ConfigHelper.getConfigOptionValues(la))); return learnedAxioms; } @@ -440,7 +440,7 @@ .getCurrentlyBestEvaluatedAxioms(nrOfAxiomsToLearn, threshold); System.out.println(prettyPrint(learnedAxioms)); - algorithmRuns.add(new AlgorithmRun(learner.getClass(), learnedAxioms, ConfigHelper.getConfigOptionValuesString(learner))); + algorithmRuns.add(new AlgorithmRun(learner.getClass(), learnedAxioms, ConfigHelper.getConfigOptionValues(learner))); return learnedAxioms; } @@ -468,11 +468,11 @@ /* * Generates list of OWL axioms. */ - private List<OWLAxiom> toRDF(List<EvaluatedAxiom> evalAxioms, Class<? extends LearningAlgorithm> algorithm, Map<ConfigOption,String> parameters, SparqlEndpointKS ks){ + private List<OWLAxiom> toRDF(List<EvaluatedAxiom> evalAxioms, Class<? extends LearningAlgorithm> algorithm, Map<ConfigOption,Object> parameters, SparqlEndpointKS ks){ return toRDF(evalAxioms, algorithm, parameters, ks, null); } - private List<OWLAxiom> toRDF(List<EvaluatedAxiom> evalAxioms, Class<? extends LearningAlgorithm> algorithm, Map<ConfigOption,String> parameters, SparqlEndpointKS ks, String defaultNamespace){ + private List<OWLAxiom> toRDF(List<EvaluatedAxiom> evalAxioms, Class<? extends LearningAlgorithm> algorithm, Map<ConfigOption,Object> parameters, SparqlEndpointKS ks, String defaultNamespace){ if(defaultNamespace == null || defaultNamespace.isEmpty()){ defaultNamespace = DEFAULT_NS; } @@ -513,13 +513,13 @@ axioms.add(ax); //add Parameters to algorithm run instance OWLIndividual paramInd; - for(Entry<ConfigOption, String> entry : parameters.entrySet()){ + for(Entry<ConfigOption, Object> entry : parameters.entrySet()){ paramInd = f.getOWLNamedIndividual(IRI.create(generateId())); ax = f.getOWLClassAssertionAxiom(EnrichmentVocabulary.Parameter, paramInd); axioms.add(ax); ax = f.getOWLDataPropertyAssertionAxiom(EnrichmentVocabulary.parameterName, paramInd, entry.getKey().name()); axioms.add(ax); - ax = f.getOWLDataPropertyAssertionAxiom(EnrichmentVocabulary.parameterValue, paramInd, entry.getValue()); + ax = f.getOWLDataPropertyAssertionAxiom(EnrichmentVocabulary.parameterValue, paramInd, entry.getValue().toString()); axioms.add(ax); } //add used input to algorithm run instance This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |