|
From: <jen...@us...> - 2008-08-06 10:10:26
|
Revision: 1055
http://dl-learner.svn.sourceforge.net/dl-learner/?rev=1055&view=rev
Author: jenslehmann
Date: 2008-08-06 09:53:31 +0000 (Wed, 06 Aug 2008)
Log Message:
-----------
code style and logging improvements
Modified Paths:
--------------
trunk/doc/checkstyle.xml
trunk/src/dl-learner/org/dllearner/core/Component.java
trunk/src/dl-learner/org/dllearner/core/ComponentManager.java
trunk/src/dl-learner/org/dllearner/core/ComponentPool.java
trunk/src/dl-learner/org/dllearner/core/EvaluatedDescription.java
trunk/src/dl-learner/org/dllearner/core/owl/KBElement.java
Modified: trunk/doc/checkstyle.xml
===================================================================
--- trunk/doc/checkstyle.xml 2008-08-05 14:56:56 UTC (rev 1054)
+++ trunk/doc/checkstyle.xml 2008-08-06 09:53:31 UTC (rev 1055)
@@ -85,7 +85,6 @@
<property name="header" value="/** * Copyright (C) 2007-2008, 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/>. * */"/>
<property name="ignoreLines" value="20"/>
</module>
- <module name="AbstractClassName"/>
</module>
<module name="JavadocPackage"/>
<module name="NewlineAtEndOfFile"/>
Modified: trunk/src/dl-learner/org/dllearner/core/Component.java
===================================================================
--- trunk/src/dl-learner/org/dllearner/core/Component.java 2008-08-05 14:56:56 UTC (rev 1054)
+++ trunk/src/dl-learner/org/dllearner/core/Component.java 2008-08-06 09:53:31 UTC (rev 1055)
@@ -57,6 +57,13 @@
* Method to be called after the component has been configured.
* Implementation of components can overwrite this method to
* perform setup and initialisation tasks for this component.
+ *
+ * @throws ComponentInitException This exception is thrown if any
+ * exceptions occur within the initialisation process of this
+ * component. As component developer, you are encouraged to
+ * rethrow occuring exception as ComponentInitException and
+ * giving an error message as well as the actualy exception by
+ * using the constructor {@link ComponentInitException#ComponentInitException(String, Throwable)}.
*/
public abstract void init() throws ComponentInitException;
@@ -66,7 +73,16 @@
* perform an action (usually setting an internal variable to
* an appropriate value).
*
+ * @param <T> Type of the config entry (Integer, String etc.).
* @param entry A configuration entry.
+ * @throws InvalidConfigOptionValueException This exception is thrown if the
+ * value of the config entry is not valid. For instance, a config option
+ * may only accept values, which are within intervals 0.1 to 0.3 or 0.5 to 0.8.
+ * If the value is outside of those intervals, an exception is thrown. Note
+ * that many of the common cases are already caught in the constructor of
+ * ConfigEntry (for instance for a {@link DoubleConfigOption} you can specify
+ * an interval for the value). This means that, as a component developer, you
+ * often do not need to implement further validity checks.
*/
public abstract <T> void applyConfigEntry(ConfigEntry<T> entry) throws InvalidConfigOptionValueException;
Modified: trunk/src/dl-learner/org/dllearner/core/ComponentManager.java
===================================================================
--- trunk/src/dl-learner/org/dllearner/core/ComponentManager.java 2008-08-05 14:56:56 UTC (rev 1054)
+++ trunk/src/dl-learner/org/dllearner/core/ComponentManager.java 2008-08-06 09:53:31 UTC (rev 1055)
@@ -60,7 +60,7 @@
* @author Jens Lehmann
*
*/
-public class ComponentManager {
+public final class ComponentManager {
private static Logger logger = Logger
.getLogger(ComponentManager.class);
@@ -95,14 +95,15 @@
};
- @SuppressWarnings( { "unchecked" })
+ @SuppressWarnings("unchecked")
private ComponentManager() {
// read in components file
List<String> componentsString;
- if(componentClasses.length > 0)
+ if(componentClasses.length > 0) {
componentsString = Arrays.asList(componentClasses);
- else
+ } else {
componentsString = readComponentsFile();
+ }
// component list
components = new TreeSet<Class<? extends Component>>(classComparator);
@@ -120,13 +121,13 @@
Component.class);
components.add(component);
- if (KnowledgeSource.class.isAssignableFrom(component))
+ if (KnowledgeSource.class.isAssignableFrom(component)) {
knowledgeSources.add((Class<? extends KnowledgeSource>) component);
- else if (ReasonerComponent.class.isAssignableFrom(component))
+ } else if (ReasonerComponent.class.isAssignableFrom(component)) {
reasonerComponents.add((Class<? extends ReasonerComponent>) component);
- else if (LearningProblem.class.isAssignableFrom(component))
+ } else if (LearningProblem.class.isAssignableFrom(component)) {
learningProblems.add((Class<? extends LearningProblem>) component);
- else if (LearningAlgorithm.class.isAssignableFrom(component)) {
+ } else if (LearningAlgorithm.class.isAssignableFrom(component)) {
Class<? extends LearningAlgorithm> learningAlgorithmClass = (Class<? extends LearningAlgorithm>) component;
learningAlgorithms.add(learningAlgorithmClass);
Collection<Class<? extends LearningProblem>> problems = (Collection<Class<? extends LearningProblem>>) invokeStaticMethod(
@@ -157,8 +158,9 @@
// make config options accessible by name
Map<String, ConfigOption<?>> byName = new HashMap<String, ConfigOption<?>>();
- for (ConfigOption<?> option : options)
+ for (ConfigOption<?> option : options) {
byName.put(option.getName(), option);
+ }
componentOptionsByName.put(component, byName);
}
@@ -172,8 +174,9 @@
* @return The singleton <code>ComponentManager</code> instance.
*/
public static ComponentManager getInstance() {
- if(cm == null)
+ if(cm == null) {
cm = new ComponentManager();
+ }
return cm;
}
@@ -203,8 +206,9 @@
while ((line = br.readLine()) != null) {
if (!(line.startsWith("#") || line.startsWith("//") || line.startsWith("%") || line
- .length() <= 1))
+ .length() <= 1)) {
componentStrings.add(line);
+ }
}
in.close();
@@ -220,11 +224,12 @@
* value is correct, it is preferable to create a ConfigEntry object and
* apply it to the component (no type checking necessary).
*
+ * @param <T> Type of the config option (Integer, String etc.).
* @param component A component.
* @param optionName The name of the config option.
* @param value The value of the config option.
*/
- @SuppressWarnings( { "unchecked" })
+ @SuppressWarnings("unchecked")
public <T> void applyConfigEntry(Component component, String optionName, T value) {
logger.trace(component);
logger.trace(optionName);
@@ -258,11 +263,13 @@
System.out.println("Warning: value " + value + " is not valid for option "
+ optionName + " in component " + component);
}
- } else
- System.out.println("Warning: undefined option " + optionName + " in component "
+ } else {
+ logger.warn("Warning: undefined option " + optionName + " in component "
+ component);
- } else
- System.out.println("Warning: unregistered component " + component);
+ }
+ } else {
+ logger.warn("Warning: unregistered component " + component);
+ }
}
/**
@@ -276,10 +283,10 @@
public <T> boolean applyConfigEntry(Component component, ConfigEntry<T> entry) {
try {
component.applyConfigEntry(entry);
- pool.addConfigEntry(component,entry,true);
+ pool.addConfigEntry(component, entry, true);
return true;
} catch (InvalidConfigOptionValueException e) {
- pool.addConfigEntry(component,entry,false);
+ pool.addConfigEntry(component, entry, false);
e.printStackTrace();
return false;
}
@@ -287,13 +294,16 @@
/**
* Factory method for creating a knowledge source.
+ *
+ * @param <T> The type of this method is a subclass of knowledge source.
* @param source A registered knowledge source component.
* @return An instance of the given knowledge source class.
*/
public <T extends KnowledgeSource> T knowledgeSource(Class<T> source) {
- if (!knowledgeSources.contains(source))
- System.err.println("Warning: knowledge source " + source
+ if (!knowledgeSources.contains(source)) {
+ logger.warn("Warning: knowledge source " + source
+ " is not a registered knowledge source component.");
+ }
T ks = invokeConstructor(source, new Class[] {}, new Object[] {});
pool.registerComponent(ks);
@@ -329,9 +339,10 @@
*/
public <T extends ReasonerComponent> T reasoner(Class<T> reasoner,
Set<KnowledgeSource> sources) {
- if (!reasonerComponents.contains(reasoner))
+ if (!reasonerComponents.contains(reasoner)) {
System.err.println("Warning: reasoner component " + reasoner
+ " is not a registered reasoner component.");
+ }
T rc = invokeConstructor(reasoner, new Class[] { Set.class },
new Object[] { sources });
@@ -363,9 +374,10 @@
* @return A learning problem component.
*/
public <T extends LearningProblem> T learningProblem(Class<T> lpClass, ReasoningService reasoner) {
- if (!learningProblems.contains(lpClass))
+ if (!learningProblems.contains(lpClass)) {
System.err.println("Warning: learning problem " + lpClass
+ " is not a registered learning problem component.");
+ }
T lp = invokeConstructor(lpClass, new Class[] { ReasoningService.class },
new Object[] { reasoner });
@@ -385,16 +397,18 @@
* the learning algorithm are not compatible.
*/
public <T extends LearningAlgorithm> T learningAlgorithm(Class<T> laClass, LearningProblem lp, ReasoningService rs) throws LearningProblemUnsupportedException {
- if (!learningAlgorithms.contains(laClass))
+ if (!learningAlgorithms.contains(laClass)) {
System.err.println("Warning: learning algorithm " + laClass
+ " is not a registered learning algorithm component.");
+ }
// find the right constructor: use the one that is registered and
// has the class of the learning problem as a subclass
Class<? extends LearningProblem> constructorArgument = null;
for (Class<? extends LearningProblem> problemClass : algorithmProblemsMapping.get(laClass)) {
- if (problemClass.isAssignableFrom(lp.getClass()))
+ if (problemClass.isAssignableFrom(lp.getClass())) {
constructorArgument = problemClass;
+ }
}
if (constructorArgument == null) {
@@ -447,10 +461,11 @@
*/
public <T> T getConfigOptionValue(Component component, ConfigOption<T> option) {
T object = pool.getLastValidConfigValue(component, option);
- if(object==null)
+ if(object==null) {
return option.getDefaultValue();
- else
+ } else {
return object;
+ }
}
/**
@@ -485,40 +500,44 @@
doc += "*********************\n";
doc += "* Knowledge Sources *\n";
doc += "*********************\n\n";
- for(Class<? extends Component> component : knowledgeSources)
+ for(Class<? extends Component> component : knowledgeSources) {
doc += getComponentConfigString(component);
+ }
doc += "*************\n";
doc += "* Reasoners *\n";
doc += "*************\n\n";
- for(Class<? extends Component> component : reasonerComponents)
+ for(Class<? extends Component> component : reasonerComponents) {
doc += getComponentConfigString(component);
+ }
doc += "*********************\n";
doc += "* Learning Problems *\n";
doc += "*********************\n\n";
- for(Class<? extends Component> component : learningProblems)
+ for(Class<? extends Component> component : learningProblems) {
doc += getComponentConfigString(component);
+ }
doc += "***********************\n";
doc += "* Learning Algorithms *\n";
doc += "***********************\n\n";
- for(Class<? extends Component> component : learningAlgorithms)
+ for(Class<? extends Component> component : learningAlgorithms) {
doc += getComponentConfigString(component);
+ }
Files.createFile(file, doc);
}
private String getComponentConfigString(Class<? extends Component> component) {
- String componentDescription = "component: " + invokeStaticMethod(component,"getName") + " (" + component.getName() + ")";
+ String componentDescription = "component: " + invokeStaticMethod(component, "getName") + " (" + component.getName() + ")";
String str = componentDescription + "\n";
- String CLI = Start.getCLIMapping(component.getSuperclass().getSimpleName()+"");
+ String cli = Start.getCLIMapping(component.getSuperclass().getSimpleName()+"");
String usage ="";
Map<Class<? extends Component>, String> m=Start.createComponentPrefixMapping();
for (Class<? extends Component> c : m.keySet()) {
- if(c.getCanonicalName().equals(component.getCanonicalName()))
- { usage=m.get(c);
+ if(c.getCanonicalName().equals(component.getCanonicalName())) {
+ usage=m.get(c);
}
}
@@ -526,14 +545,12 @@
str += "=";
}
str += "\n\n";
- str += "CLI usage: "+CLI+" = "+usage+";\n\n";
+ str += "CLI usage: "+cli+" = "+usage+";\n\n";
for(ConfigOption<?> option : componentOptions.get(component)) {
- str += option.toString() +
- "CLI usage: "+usage+"."+
- option.getName()+" = "+option.getDefaultValue()+
- ";\n\n";
+ str += option.toString() + "CLI usage: "+usage+"."
+ + option.getName()+" = "+option.getDefaultValue()+";\n\n";
}
return str+"\n";
}
@@ -593,9 +610,10 @@
* @return A list of available configuration options of the specified component.
*/
public static List<ConfigOption<?>> getConfigOptions(Class<? extends Component> componentClass) {
- if (!components.contains(componentClass))
+ if (!components.contains(componentClass)) {
System.err.println("Warning: component " + componentClass
+ " is not a registered component. [ComponentManager.getConfigOptions]");
+ }
return componentOptions.get(componentClass);
}
Modified: trunk/src/dl-learner/org/dllearner/core/ComponentPool.java
===================================================================
--- trunk/src/dl-learner/org/dllearner/core/ComponentPool.java 2008-08-05 14:56:56 UTC (rev 1054)
+++ trunk/src/dl-learner/org/dllearner/core/ComponentPool.java 2008-08-06 09:53:31 UTC (rev 1055)
@@ -1,5 +1,5 @@
/**
- * Copyright (C) 2007, Jens Lehmann
+ * Copyright (C) 2007-2008, Jens Lehmann
*
* This file is part of DL-Learner.
*
@@ -24,57 +24,95 @@
import java.util.List;
import java.util.Map;
+import org.apache.log4j.Logger;
import org.dllearner.core.config.ConfigEntry;
import org.dllearner.core.config.ConfigOption;
/**
- * Stores all live components and the configuration options, which were
- * applied to them.
+ * Stores all live components and the configuration options, which were applied
+ * to them. This allows to detect, which components are currently active, which
+ * values are assigned to specific options, and to collect statistics (e.g. in
+ * a web service scenario).
*
* @author Jens Lehmann
- *
+ *
*/
-public class ComponentPool {
+public final class ComponentPool {
- // stores all components, which are live (components which are
+ private static Logger logger = Logger
+ .getLogger(ComponentPool.class);
+
+ // stores all components, which are live (components which are
// no longer used have to be deregistered)
private List<Component> components = new LinkedList<Component>();
-
- // stores the last value which was set for a particular
+
+ // stores the last value which was set for a particular
// config option
- private Map<Component,Map<ConfigOption<?>,Object>> lastValidConfigValue = new HashMap<Component,Map<ConfigOption<?>,Object>>();
+ private Map<Component, Map<ConfigOption<?>, Object>> lastValidConfigValue = new HashMap<Component, Map<ConfigOption<?>, Object>>();
// complete history of all made config entries for a component
- private Map<Component,List<ConfigEntry<?>>> configEntryHistory = new HashMap<Component,List<ConfigEntry<?>>>();
-
+ private Map<Component, List<ConfigEntry<?>>> configEntryHistory = new HashMap<Component, List<ConfigEntry<?>>>();
+
+ /**
+ * Registers a component instance in the pool.
+ * @param component The component to add to the pool.
+ */
public void registerComponent(Component component) {
components.add(component);
- Map<ConfigOption<?>,Object> emptyMap = new HashMap<ConfigOption<?>,Object>();
+ Map<ConfigOption<?>, Object> emptyMap = new HashMap<ConfigOption<?>, Object>();
lastValidConfigValue.put(component, emptyMap);
configEntryHistory.put(component, new LinkedList<ConfigEntry<?>>());
+ logger.debug("Component instance " + component + " added to component pool.");
}
+ /**
+ * Unregisters a component instance. This method should be used if the
+ * component will not be used anymore. It frees the memory for
+ * storing the component and its configuration options.
+ * @param component The component to remove from the pool.
+ */
public void unregisterComponent(Component component) {
configEntryHistory.remove(component);
lastValidConfigValue.remove(component);
components.remove(component);
+ logger.debug("Component instance " + component + " removed from component pool.");
}
- @SuppressWarnings({"unchecked"})
- public <T> T getLastValidConfigValue(Component component, ConfigOption<T> option) {
+ /**
+ * Gets the last valid config value set for this component.
+ * @param <T> The type of the value of the config option (String, Integer etc.).
+ * @param component The component to query.
+ * @param option The option for which one wants to get the value.
+ * @return The last value set for this option or null if the value hasn't been
+ * set using the {@link ComponentManager}. In this case, the value is
+ * usually at the default value (or has been set internally surpassing the
+ * component architecture, which is not recommended).
+ */
+ @SuppressWarnings("unchecked")
+ protected <T> T getLastValidConfigValue(Component component, ConfigOption<T> option) {
return (T) lastValidConfigValue.get(component).get(option);
}
-
- public void addConfigEntry(Component component, ConfigEntry<?> entry, boolean valid) {
+
+ /**
+ * Add a config entry change for the specified component.
+ * @param component The component, where the config entry has been set.
+ * @param entry The set config entry.
+ * @param valid A boolean value indicating whether the value was valid or not.
+ */
+ protected void addConfigEntry(Component component, ConfigEntry<?> entry, boolean valid) {
configEntryHistory.get(component).add(entry);
- if(valid)
+ if (valid) {
lastValidConfigValue.get(component).put(entry.getOption(), entry.getValue());
+ }
+ logger.trace("Config entry " + entry + " has been set for component " + component + " (validity: " + valid + ").");
}
-
- // unregisters all components
- public void clearComponents() {
+
+ /**
+ * Unregisters all components.
+ */
+ protected void clearComponents() {
components = new LinkedList<Component>();
- lastValidConfigValue = new HashMap<Component,Map<ConfigOption<?>,Object>>();
- configEntryHistory = new HashMap<Component,List<ConfigEntry<?>>>();
+ lastValidConfigValue = new HashMap<Component, Map<ConfigOption<?>, Object>>();
+ configEntryHistory = new HashMap<Component, List<ConfigEntry<?>>>();
}
-
+
}
Modified: trunk/src/dl-learner/org/dllearner/core/EvaluatedDescription.java
===================================================================
--- trunk/src/dl-learner/org/dllearner/core/EvaluatedDescription.java 2008-08-05 14:56:56 UTC (rev 1054)
+++ trunk/src/dl-learner/org/dllearner/core/EvaluatedDescription.java 2008-08-06 09:53:31 UTC (rev 1055)
@@ -83,6 +83,7 @@
/**
* @see org.dllearner.core.owl.Description#getLength()
+ * @return Length of the description.
*/
public int getDescriptionLength() {
return description.getLength();
@@ -185,8 +186,9 @@
// as arguments and does not use toString)
private static JSONArray getJSONArray(Set<Individual> individuals) {
JSONArray j = new JSONArray();
- for(Individual i : individuals)
+ for(Individual i : individuals) {
j.put(i.getName());
+ }
return j;
}
}
Modified: trunk/src/dl-learner/org/dllearner/core/owl/KBElement.java
===================================================================
--- trunk/src/dl-learner/org/dllearner/core/owl/KBElement.java 2008-08-05 14:56:56 UTC (rev 1054)
+++ trunk/src/dl-learner/org/dllearner/core/owl/KBElement.java 2008-08-06 09:53:31 UTC (rev 1055)
@@ -10,6 +10,16 @@
*/
public interface KBElement {
+ /**
+ * Gets the length of this knowledge base element. For instance,
+ * A AND B should have length 3 (as three constructs are involved).
+ * There are different ways to define the length of an axiom,
+ * class description etc., but this method provides a straightforward
+ * definition of it.
+ *
+ * @return The syntactic length of the KB element, defined as the
+ * number of syntactic constructs not including brackets.
+ */
public int getLength();
public String toString(String baseURI, Map<String,String> prefixes);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|