From: <jen...@us...> - 2011-08-16 15:57:17
|
Revision: 3053 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3053&view=rev Author: jenslehmann Date: 2011-08-16 15:57:11 +0000 (Tue, 16 Aug 2011) Log Message: ----------- - drafted methods for handling dependencies between components (based on reflection and existence of appropriate constructors) - started script generating HTML documentation for new annotation bases component system Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/core/AnnComponentManager.java trunk/scripts/src/main/java/org/dllearner/scripts/DocumentationHTMLGenerator.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-16 14:48:58 UTC (rev 3052) +++ trunk/components-core/src/main/java/org/dllearner/core/AnnComponentManager.java 2011-08-16 15:57:11 UTC (rev 3053) @@ -19,12 +19,16 @@ */ package org.dllearner.core; +import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import java.util.Map; +import org.apache.commons.lang.ClassUtils; + /** * Component manager for the new (as of 2011) annotation based configuration * system. @@ -100,7 +104,6 @@ */ public Collection<Class<? extends Component>> getComponents() { return components; -// return new LinkedList<Class<? extends Component>>(components); } /** @@ -114,7 +117,89 @@ return componentNames; } + public boolean isCompatible() { + return false; + } + + // gets all components which this component can be plugged into + public Collection<Class<? extends Component>> getPluggableComponents(Class<? extends Component> component) { + Collection<Class<? extends Component>> pluggableComponents = new LinkedList<Class<? extends Component>>(); + for(Class<? extends Component> comp : components) { + if(isPluggable(comp, component)) { + pluggableComponents.add(comp); + } + } + return pluggableComponents; + } + + // should return true if there exists a constructor in "compound" which can take + // "component" as argument (in any argument positions) + public boolean isPluggable(Class<? extends Component> compound, Class<? extends Component> argument) { + try { + Constructor<?>[] constructors = compound.getDeclaredConstructors(); + for(Constructor<?> constructor : constructors) { + Class<?>[] paraTypes = constructor.getParameterTypes(); + for(Class<?> paraType : paraTypes) { + if(ClassUtils.isAssignable(argument, paraType)) { + return true; + } + } + } + } catch (SecurityException e) { + e.printStackTrace(); + } + return false; + } + + public boolean isCompatible(Class<? extends Component> compound, Class<? extends Component>... arguments) { + if(areValidComponentConstructorArguments(arguments)) { + throw new Error("Please order arguments by their class names."); + } + return hasMatchingConstructor(compound, arguments); + } + + private boolean hasMatchingConstructor(Class<? extends Component> compound, Class<? extends Component>... arguments) { + try { + Constructor<?>[] constructors = compound.getDeclaredConstructors(); + for(Constructor<?> constructor : constructors) { + if(ClassUtils.isAssignable(arguments, constructor.getParameterTypes())) { + return true; + } + } + } catch (SecurityException e) { + e.printStackTrace(); + } + return false; + } + /** + * Components in DL-Learner can be plugged together by invoking an appropriate + * constructor. For efficiency reasons, they should be ordered by class + * names. This method allows to test this convention. + * + * Please note that components may have additional further constructors, but + * if a constructor has exclusively components as parameters, then it is + * required that they are ordered by class name. + * + * TODO: Possibly, we can replace our naive constructor detection code with + * a better implementation, which can detect whether an appropriate + * constructor exists even without fixing the order of arguments. (E.g. checking + * assignability for each parameter and argument; putting it into a matrix + * and then checking whether there is a row/column with only 1s.) + * + * @param arguments Argument classes. + * @return True of the order of arguments is correct and false otherwise. + */ + public boolean areValidComponentConstructorArguments(Class<? extends Component>... arguments) { + for(int i=0; i<arguments.length; i++) { + if(arguments[i].getName().compareTo(arguments[i+1].getName())<0) { + return false; + } + } + return true; + } + + /** * Returns the name of a DL-Learner component. * @param component * @return Name of the component. Modified: trunk/scripts/src/main/java/org/dllearner/scripts/DocumentationHTMLGenerator.java =================================================================== --- trunk/scripts/src/main/java/org/dllearner/scripts/DocumentationHTMLGenerator.java 2011-08-16 14:48:58 UTC (rev 3052) +++ trunk/scripts/src/main/java/org/dllearner/scripts/DocumentationHTMLGenerator.java 2011-08-16 15:57:11 UTC (rev 3053) @@ -55,7 +55,21 @@ StringBuffer sb = new StringBuffer(); sb.append(getHeader()); + // heading + sb.append("<h1>DL-Learner Components</h1>\n"); + // generate component overview + sb.append("<ul>\n"); + for(Entry<String, Class<? extends Component>> compEntry : componentNamesInv.entrySet()) { + sb.append("<li><a href=\"#" + compEntry.getValue() + "\">"+compEntry.getKey()+"</a></li>\n"); + } + sb.append("</ul>\n"); + + // generate actual documentation per component + for(Entry<String, Class<? extends Component>> compEntry : componentNamesInv.entrySet()) { + sb.append("<a name=\"#" + compEntry.getValue() + "\" /><h2>"+compEntry.getKey()+"</h2>\n"); + } + sb.append(getFooter()); } @@ -64,7 +78,7 @@ } private String getFooter() { - return "</body>"; + return "</body></html>"; } public static void main(String[] args) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |