From: <jen...@us...> - 2011-08-22 13:56:11
|
Revision: 3093 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3093&view=rev Author: jenslehmann Date: 2011-08-22 13:56:05 +0000 (Mon, 22 Aug 2011) Log Message: ----------- refined method for obtaining the class from the value of .type in conf files Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/core/AnnComponentManager.java trunk/interfaces/src/main/java/org/dllearner/confparser2/ConfParserConfiguration.java Modified: trunk/components-core/src/main/java/org/dllearner/core/AnnComponentManager.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/core/AnnComponentManager.java 2011-08-22 13:55:55 UTC (rev 3092) +++ trunk/components-core/src/main/java/org/dllearner/core/AnnComponentManager.java 2011-08-22 13:56:05 UTC (rev 3093) @@ -30,6 +30,8 @@ import java.util.List; import java.util.Map; +import org.apache.commons.collections15.BidiMap; +import org.apache.commons.collections15.bidimap.DualHashBidiMap; import org.apache.commons.lang.ClassUtils; /** @@ -71,19 +73,22 @@ "org.dllearner.algorithms.SimpleSubclassLearner", } )); private static Collection<Class<? extends Component>> components; - private static Map<Class<? extends Component>, String> componentNames; + private static BidiMap<Class<? extends Component>, String> componentNames; + private static BidiMap<Class<? extends Component>, String> componentNamesShort; private static AnnComponentManager cm = null; private AnnComponentManager() { // conversion of class strings to objects components = new HashSet<Class<? extends Component>>(); - componentNames = new HashMap<Class<? extends Component>, String>(); + componentNames = new DualHashBidiMap<Class<? extends Component>, String>(); + componentNamesShort = new DualHashBidiMap<Class<? extends Component>, String>(); for (String componentClassName : componentClassNames) { try { Class<? extends Component> component = Class.forName(componentClassName).asSubclass(Component.class); components.add(component); componentNames.put(component, getName(component)); + componentNamesShort.put(component, getShortName(component)); } catch (ClassNotFoundException e) { e.printStackTrace(); } @@ -112,16 +117,27 @@ } /** - * Convenience methed, which returns a list of components along with + * Convenience method, which returns a list of components along with * their name. * * @return A map where the key is the class of the component and the * value is its name. */ - public Map<Class<? extends Component>, String> getComponentsNamed() { + public BidiMap<Class<? extends Component>, String> getComponentsNamed() { return componentNames; } + /** + * Convenience method, which returns a list of components along with + * their name. + * + * @return A map where the key is the class of the component and the + * value is its name. + */ + public BidiMap<Class<? extends Component>, String> getComponentsNamedShort() { + return componentNamesShort; + } + public boolean isCompatible() { return false; } Modified: trunk/interfaces/src/main/java/org/dllearner/confparser2/ConfParserConfiguration.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser2/ConfParserConfiguration.java 2011-08-22 13:55:55 UTC (rev 3092) +++ trunk/interfaces/src/main/java/org/dllearner/confparser2/ConfParserConfiguration.java 2011-08-22 13:56:05 UTC (rev 3093) @@ -2,6 +2,8 @@ import org.dllearner.cli.ConfFileOption; import org.dllearner.configuration.IConfiguration; +import org.dllearner.core.AnnComponentManager; +import org.dllearner.core.Component; import org.dllearner.utilities.datastructures.StringTuple; import org.springframework.core.io.Resource; @@ -149,16 +151,27 @@ } @Override - public Class getClass(String beanName) { - Class result = null; - /** TODO Do something that isn't hard coded like this or use a more specific name than type. The result of this must be the class - so however we get it doesn't matter */ + public Class<?> getClass(String beanName) { + Class<?> result = null; ConfFileOption option = parser.getConfOptionsByName(beanName + ".type"); + String value = (String) option.getValue(); + // first option: use long name of @ComponentAnn annotation + Class<? extends Component> classFromName = AnnComponentManager.getInstance().getComponentsNamed().getKey(value); + if(classFromName != null) { + return classFromName; + } + // second option: use short name of @ComponentAnn annotation + Class<? extends Component> classFromShortName = AnnComponentManager.getInstance().getComponentsNamedShort().getKey(value); + if(classFromShortName != null) { + return classFromShortName; + } + // third option: use specified class name try { - result = Class.forName((String) option.getValue()); + result = Class.forName(value); } catch (ClassNotFoundException e) { + // if all methods fail, throw an exception throw new RuntimeException("Problem getting class type for bean: " + beanName + " - trying to instantiate class: " + option.getValue()); } - return result; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |