From: <sha...@us...> - 2011-08-27 20:07:16
|
Revision: 3140 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3140&view=rev Author: shadowtm Date: 2011-08-27 20:07:08 +0000 (Sat, 27 Aug 2011) Log Message: ----------- Moved over to the confparser3 version of configuration syntax - also made Map and Set property editors. Created Property Editors for Sets of Individuals on the PosNegLP class. Modified Paths: -------------- trunk/components-core/src/main/java/org/dllearner/configuration/IConfiguration.java trunk/components-core/src/main/java/org/dllearner/core/AnnComponentManager.java trunk/interfaces/src/main/java/org/dllearner/cli/ConfFileOption2.java trunk/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedBeanDefinitionRegistryPostProcessor.java trunk/interfaces/src/main/java/org/dllearner/configuration/spring/DefaultApplicationContextBuilder.java trunk/interfaces/src/main/java/org/dllearner/confparser2/ConfParserConfiguration.java trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParser.java trunk/interfaces/src/main/java/org/dllearner/confparser3/conf3.jj trunk/interfaces/src/main/resources/org/dllearner/configuration/spring/bean-post-processor-configuration.xml trunk/interfaces/src/test/java/org/dllearner/cli/FatherCLITest.java trunk/interfaces/src/test/java/org/dllearner/configuration/spring/ConfigurationBasedPropertyOverrideConfigurerTest.java trunk/interfaces/src/test/resources/org/dllearner/configuration/spring/configurationBasedPropertyOverrideConfigurer.conf Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/configuration/IConfigurationProperty.java trunk/interfaces/src/main/java/org/dllearner/configuration/spring/CustomPropertyEditorRegistrar.java trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParserConfiguration.java trunk/interfaces/src/main/java/org/dllearner/confparser3/IndividualCollectionEditor.java trunk/interfaces/src/main/java/org/dllearner/confparser3/MapEditor.java trunk/interfaces/src/main/java/org/dllearner/confparser3/SetEditor.java Removed Paths: ------------- trunk/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedPropertyOverrideConfigurer.java Modified: trunk/components-core/src/main/java/org/dllearner/configuration/IConfiguration.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/configuration/IConfiguration.java 2011-08-27 14:39:48 UTC (rev 3139) +++ trunk/components-core/src/main/java/org/dllearner/configuration/IConfiguration.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -94,4 +94,7 @@ public String getBaseDir(); + public Collection<IConfigurationProperty> getConfigurationOptions(String beanName); + + } Added: trunk/components-core/src/main/java/org/dllearner/configuration/IConfigurationProperty.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/configuration/IConfigurationProperty.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/configuration/IConfigurationProperty.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -0,0 +1,49 @@ +package org.dllearner.configuration; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 8/27/11 + * Time: 8:57 AM + * + * Respresents a Configuraiton Option setting. + */ +public interface IConfigurationProperty { + + /** + * Get the Name of this Property + * + * @return The Name of this property. + */ + public String getName(); + + + /** + * Get the String representation of the value of this property. + * + * @return The String representation of the value of this property. + */ + public String getValue(); + + /** + * Get the type of this value. + * + * @return The type of this value. + */ + public Class getType(); + + + /** + * Does this property represent a bean reference? + * + * @return True if it does. + */ + public boolean isBeanReference(); + + /** + * Does this property represent a collection of bean references? + * + * @return True if it does. + */ + public boolean isBeanReferenceCollection(); +} 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-27 14:39:48 UTC (rev 3139) +++ trunk/components-core/src/main/java/org/dllearner/core/AnnComponentManager.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -80,6 +80,7 @@ "org.dllearner.learningproblems.PosNegLPStandard", "org.dllearner.learningproblems.PosOnlyLP", "org.dllearner.reasoning.FastInstanceChecker", + "org.dllearner.reasoning.OWLAPIReasoner", "org.dllearner.algorithms.ocel.OCEL", "org.dllearner.algorithms.ocel.MultiHeuristic", "org.dllearner.refinementoperators.RhoDRDown", Modified: trunk/interfaces/src/main/java/org/dllearner/cli/ConfFileOption2.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/cli/ConfFileOption2.java 2011-08-27 14:39:48 UTC (rev 3139) +++ trunk/interfaces/src/main/java/org/dllearner/cli/ConfFileOption2.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -20,6 +20,8 @@ package org.dllearner.cli; +import org.dllearner.configuration.IConfigurationProperty; + /** * Programmatic representation of an option setting in a conf file: * bean.property = value; @@ -29,10 +31,12 @@ * @author Jens Lehmann * */ -public class ConfFileOption2 { +public class ConfFileOption2 implements IConfigurationProperty{ // a boolean flag which indicates whether it is a reference to a bean (or set/list of beans) private boolean isBeanRef; + + private boolean isBeanReferenceCollection; private String beanName; @@ -101,5 +105,34 @@ public void setBeanRef(boolean isBeanRef) { this.isBeanRef = isBeanRef; } - + + @Override + public String getName() { + return getPropertyName(); + } + + @Override + public String getValue() { + return getPropertyValue(); + } + + @Override + public boolean isBeanReference() { + return isBeanRef(); + } + + @Override + public Class getType() { + return getPropertyType(); + } + + @Override + public boolean isBeanReferenceCollection() { + return isBeanReferenceCollection; + } + + public void setBeanReferenceCollection(boolean beanReferenceCollection) { + isBeanReferenceCollection = beanReferenceCollection; + } + } Modified: trunk/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedBeanDefinitionRegistryPostProcessor.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedBeanDefinitionRegistryPostProcessor.java 2011-08-27 14:39:48 UTC (rev 3139) +++ trunk/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedBeanDefinitionRegistryPostProcessor.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -1,23 +1,27 @@ package org.dllearner.configuration.spring; import org.dllearner.configuration.IConfiguration; +import org.dllearner.configuration.IConfigurationProperty; import org.dllearner.kb.KBFile; import org.dllearner.kb.OWLFile; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; +import org.springframework.beans.factory.support.ManagedSet; import java.util.Collection; +import java.util.StringTokenizer; /** * Created by IntelliJ IDEA. * User: Chris * Date: 8/22/11 * Time: 6:38 AM - * + * <p/> * This class is used to insert BeanDefinitions that are declared in the configuration file that * do not exist in the existing registry (ie. they aren't declared in the spring XML file). */ @@ -29,23 +33,24 @@ this.configuration = configuration; } + /** + * Ensure that BeanDefinitions exist for all referenced beans in the configuration. + * + * @param registry The Bean registry to use. + * @throws BeansException + */ @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { - Collection<String> beanNames = configuration.getBeanNames(); + final Collection<String> beanNames = configuration.getBeanNames(); for (String beanName : beanNames) { - if(!registry.containsBeanDefinition(beanName)){ + if (!registry.containsBeanDefinition(beanName)) { Class beanClass = configuration.getClass(beanName); BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(beanClass); BeanDefinition definition = builder.getBeanDefinition(); - /** Add Base Directory */ - if(beanClass.isAssignableFrom(KBFile.class) || beanClass.isAssignableFrom(OWLFile.class)){ - definition.getPropertyValues().addPropertyValue("baseDir",configuration.getBaseDir()); - } - - registry.registerBeanDefinition(beanName,definition); + registry.registerBeanDefinition(beanName, definition); } } @@ -53,6 +58,59 @@ @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { - /** Do nothing here */ + + final Collection<String> beanNames = configuration.getBeanNames(); + + + for (String beanName : beanNames) { + + BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); + + Collection<IConfigurationProperty> properties = configuration.getConfigurationOptions(beanName); + + for (IConfigurationProperty property : properties) { + + Object value = property.getValue(); + //Process Single Bean References + if (property.isBeanReference()) { + BeanDefinition referencedBean = beanFactory.getBeanDefinition(property.getValue()); + value = referencedBean; + } + + //Process collections of bean references + if(property.isBeanReferenceCollection()){ + StringTokenizer tokenizer = new StringTokenizer(property.getValue(),"{,} ",false); + Collection<RuntimeBeanReference> beanReferences = new ManagedSet<RuntimeBeanReference>(); + while(tokenizer.hasMoreTokens()){ + String referencedBeanName = tokenizer.nextToken(); + beanReferences.add(new RuntimeBeanReference(referencedBeanName)); + } + value = beanReferences; + } + + addBaseDirectoryIfNeeded(beanDefinition); + + beanDefinition.getPropertyValues().add(property.getName(), value); + } + } } + + /** + * Add Base Directory Value to Beans which need it. + * + * @param beanDefinition The curren Bean Definition + */ + private void addBaseDirectoryIfNeeded(BeanDefinition beanDefinition) { + Class beanClass = null; + try { + beanClass = Class.forName(beanDefinition.getBeanClassName()); + } catch (ClassNotFoundException e) { + throw new RuntimeException("Can't find class " + beanDefinition.getBeanClassName()); + } + /** Add Base Directory */ + if (beanClass.isAssignableFrom(KBFile.class) || beanClass.isAssignableFrom(OWLFile.class)) { + beanDefinition.getPropertyValues().add("baseDir", configuration.getBaseDir()); + } + } + } Deleted: trunk/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedPropertyOverrideConfigurer.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedPropertyOverrideConfigurer.java 2011-08-27 14:39:48 UTC (rev 3139) +++ trunk/interfaces/src/main/java/org/dllearner/configuration/spring/ConfigurationBasedPropertyOverrideConfigurer.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -1,209 +0,0 @@ -package org.dllearner.configuration.spring; - -import org.dllearner.configuration.IConfiguration; -import org.springframework.beans.PropertyValue; -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.beans.factory.config.PropertyOverrideConfigurer; -import org.springframework.beans.factory.config.RuntimeBeanReference; -import org.springframework.beans.factory.support.ManagedSet; - -import java.util.*; - -/** - * Created by IntelliJ IDEA. - * User: Chris - * Date: 8/18/11 - * Time: 9:12 PM - * <p/> - * Extending the Custom Property Override Configurer so that we can use our IConfiguration objects - * in conjunction with a Spring Based Configuration. - */ -public class ConfigurationBasedPropertyOverrideConfigurer extends PropertyOverrideConfigurer { - - - private final IConfiguration configuration; - private boolean ignoreInvalidKeys = false; - private List<String> componentKeyPrefixes = new ArrayList<String>(); - - /** - * Primary Constructor. - * - * @param configuration The DL-Learner Configuration object. - * @param ignoreInvalidKeys Consult the PropertyOverrideConfigurer documentation for the usage of this variable. - */ - public ConfigurationBasedPropertyOverrideConfigurer(IConfiguration configuration, boolean ignoreInvalidKeys) { - super(); - this.configuration = configuration; - this.ignoreInvalidKeys = ignoreInvalidKeys; - setIgnoreInvalidKeys(false); - } - - @Override - protected void applyPropertyValue(ConfigurableListableBeanFactory factory, String beanName, String property, String value) { - - BeanDefinition bd = getBeanDefinition(factory, beanName); - - Object obj = buildObject(beanName, property); - - applyPropertyValue(factory, beanName, property, bd, obj); - - - } - - /** - * Build the object represented by beanName.property as it is represented in the configuration. - * - * @param beanName The bean name of the object to build - * @param property The property name of the object to build - * @return The object represented by beanName.property - */ - protected Object buildObject(String beanName, String property) { - StringBuilder objKey = buildObjectKey(beanName, property); - return configuration.getObjectValue(objKey.toString()); - } - - /** - * Get the Bean Definition for a particular bean. - * - * @param factory The factory to get the bean from. - * @param beanName The bean to get. - * @return The bean definition of beanName - */ - protected BeanDefinition getBeanDefinition(ConfigurableListableBeanFactory factory, String beanName) { - BeanDefinition bd = factory.getBeanDefinition(beanName); - while (bd.getOriginatingBeanDefinition() != null) { - bd = bd.getOriginatingBeanDefinition(); - } - return bd; - } - - /** - * Apply obj to a property value. - * @param factory - * @param beanName - * @param property - * @param bd - * @param obj - */ - private void applyPropertyValue(ConfigurableListableBeanFactory factory, String beanName, String property, BeanDefinition bd, Object obj) { - /** Check if Object represents a ReferencedBean */ - String referencedBeanName = getReferencedBeanName(obj); - if (referencedBeanName != null && !referencedBeanName.isEmpty()) { - applyBeanReferencePropertyValue(factory, beanName, property, referencedBeanName); - } else { - //TODO I don't like this code - refactor to make it more elegant later - if(obj instanceof Collection){ - /** Now check for a Collection of component references */ - Collection collection = (Collection) obj; - - /** TODO: Now we only work with Sets as that is what comes from the configuration - but we should probably support other collection types as well */ - /** The managed set is the key to getting this to work - there are other managed objects we could use in the future */ - Collection components = new ManagedSet(); - - for (Object o : collection) { - if(o instanceof String){ - referencedBeanName = getReferencedBeanName(o); - if(referencedBeanName != null && !referencedBeanName.isEmpty()){ - components.add(new RuntimeBeanReference(referencedBeanName)); - } - } - } - if (components.isEmpty()) { - applyRegularPropertyValue(property, bd, obj); - } else { - bd.getPropertyValues().addPropertyValue(property, components); - } - - }else{ - /** We have a regular property value */ - applyRegularPropertyValue(property, bd, obj); - } - } - } - - private void applyRegularPropertyValue(String property, BeanDefinition bd, Object obj) { - PropertyValue pv = new PropertyValue(property, obj); - pv.setOptional(ignoreInvalidKeys); - bd.getPropertyValues().addPropertyValue(pv); - } - - /** - * Apply a bean reference to beanName.property in the given factory. - * - * @param factory The factory to use. - * @param beanName The bean name - * @param property The property name. - * @param referencedBean The referenced bean to set as bean.property in factory. - */ - private void applyBeanReferencePropertyValue(ConfigurableListableBeanFactory factory, String beanName, String property, String referencedBean) { - BeanDefinition bd = getBeanDefinition(factory, beanName); - /** You have to get the bean definition of the referenced bean here - don't try to get the bean itself or you'll get a objects which aren't completely initialized yet */ - Object obj = factory.getBeanDefinition(referencedBean); - bd.getPropertyValues().addPropertyValue(property, obj); - } - - /** - * Build the object key which is just name beanName.property or beanName if property is null. - * @param beanName First part of the key. - * @param property Second part of the key (after the period). - * @return The object key. - */ - private StringBuilder buildObjectKey(String beanName, String property) { - StringBuilder objName = new StringBuilder(); - objName.append(beanName); - if (property != null && !property.isEmpty()) { - objName.append("."); - objName.append(property); - } - return objName; - } - - /** - * Determine if value is a name of a referenced bean. - * <p/> - * This will return the name of the referenced bean if it does. If not, it will return null. - * - * @param object The object to check. - * @return True if we need to do custom loading of this value, false if we can use the parent. - */ - protected String getReferencedBeanName(Object object) { - - String result = null; - - boolean found = false; - - Iterator<String> itr = getComponentKeyPrefixes().iterator(); - - if (object instanceof String) { - String value = (String) object; - while (!found && itr.hasNext()) { - String prefix = itr.next(); - if (value.startsWith(prefix)) { - found = true; - result = value.substring(prefix.length()); - } - } - } - return result; - - } - - /** - * Get the list of prefixes that cause us to do custom loading. - * - * @return The list of prefixes that cause us to do custom loading. - */ - public List<String> getComponentKeyPrefixes() { - return componentKeyPrefixes; - } - - /** - * Set the list of prefixes that cause us to do custom loading. - * - * @param componentKeyPrefixes the list of prefixes that cause us to do custom loading. - */ - public void setComponentKeyPrefixes(List<String> componentKeyPrefixes) { - this.componentKeyPrefixes = componentKeyPrefixes; - } -} Added: trunk/interfaces/src/main/java/org/dllearner/configuration/spring/CustomPropertyEditorRegistrar.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/configuration/spring/CustomPropertyEditorRegistrar.java (rev 0) +++ trunk/interfaces/src/main/java/org/dllearner/configuration/spring/CustomPropertyEditorRegistrar.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -0,0 +1,43 @@ +package org.dllearner.configuration.spring; + +import org.dllearner.confparser3.IndividualCollectionEditor; +import org.dllearner.confparser3.MapEditor; +import org.dllearner.confparser3.SetEditor; +import org.dllearner.learningproblems.PosNegLP; +import org.springframework.beans.BeanWrapper; +import org.springframework.beans.PropertyEditorRegistrar; +import org.springframework.beans.PropertyEditorRegistry; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 8/27/11 + * Time: 12:38 PM + * <p/> + * This is where we will register custom property editors for properties which can't be configured by the standard + * PropertyEditors. + */ +public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar { + + @Override + public void registerCustomEditors(PropertyEditorRegistry registry) { + + if (registry instanceof BeanWrapper) { + Object wrappedInstance = ((BeanWrapper) registry).getWrappedInstance(); + if (wrappedInstance instanceof PosNegLP) { + registry.registerCustomEditor(Collection.class, "positiveExamples", new IndividualCollectionEditor()); + registry.registerCustomEditor(Collection.class, "negativeExamples", new IndividualCollectionEditor()); + } + } + + //Wrappers for all beans + registry.registerCustomEditor(Map.class,new MapEditor()); + registry.registerCustomEditor(Set.class,new SetEditor()); + + + } +} Modified: trunk/interfaces/src/main/java/org/dllearner/configuration/spring/DefaultApplicationContextBuilder.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/configuration/spring/DefaultApplicationContextBuilder.java 2011-08-27 14:39:48 UTC (rev 3139) +++ trunk/interfaces/src/main/java/org/dllearner/configuration/spring/DefaultApplicationContextBuilder.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -29,10 +29,6 @@ // Post Processors BeanDefinitionRegistryPostProcessor beanDefinitionRegistryPostProcessor = new ConfigurationBasedBeanDefinitionRegistryPostProcessor(configuration); - ConfigurationBasedPropertyOverrideConfigurer configurer = new ConfigurationBasedPropertyOverrideConfigurer(configuration, false); - configurer.setProperties(configuration.getProperties()); - configurer.getComponentKeyPrefixes().addAll(componentKeyPrefixes); - //These files need to be loaded first List<Resource> allSpringConfigFiles = new ArrayList<Resource>(); allSpringConfigFiles.add(new ClassPathResource("/org/dllearner/configuration/spring/bean-post-processor-configuration.xml")); @@ -48,7 +44,6 @@ // These post processors run before object instantiation context.addBeanFactoryPostProcessor(beanDefinitionRegistryPostProcessor); - context.addBeanFactoryPostProcessor(configurer); //Instantiate and initialize the beans. context.refresh(); Modified: trunk/interfaces/src/main/java/org/dllearner/confparser2/ConfParserConfiguration.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser2/ConfParserConfiguration.java 2011-08-27 14:39:48 UTC (rev 3139) +++ trunk/interfaces/src/main/java/org/dllearner/confparser2/ConfParserConfiguration.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -2,6 +2,7 @@ import org.dllearner.cli.ConfFileOption; import org.dllearner.configuration.IConfiguration; +import org.dllearner.configuration.IConfigurationProperty; import org.dllearner.core.AnnComponentManager; import org.dllearner.core.Component; import org.dllearner.utilities.datastructures.StringTuple; @@ -182,4 +183,9 @@ public String getBaseDir() { return baseDir; } + + @Override + public Collection<IConfigurationProperty> getConfigurationOptions(String beanName) { + throw new RuntimeException("Don't use this class - use the one in the confparser3 package."); + } } Modified: trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParser.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParser.java 2011-08-27 14:39:48 UTC (rev 3139) +++ trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParser.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -96,6 +96,7 @@ ConfFileOption2 option = new ConfFileOption2(); boolean isBeanRef = false; + boolean isBeanCollection = false; String beanName; String propertyName = ""; String propertyValue = ""; @@ -164,7 +165,7 @@ switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case 17: jj_consume_token(17); - val = new HashSet(); propertyType = Set.class; propertyValue = "-"; isBeanRef = true; + val = new HashSet(); propertyType = Set.class; propertyValue = "-"; isBeanCollection = true; break; case 14: jj_consume_token(14); @@ -182,7 +183,7 @@ tmp = Id(); values.add(tmp); propertyValue += tmp; jj_consume_token(15); - val = values; propertyType = Set.class; propertyValue = "{"+ propertyValue + "}"; isBeanRef = true; + val = values; propertyType = Set.class; propertyValue = "{"+ propertyValue + "}"; isBeanCollection = true; break; default: jj_la1[3] = jj_gen; @@ -228,6 +229,7 @@ } } option.setBeanRef(isBeanRef); + option.setBeanReferenceCollection(isBeanCollection); option.setBeanName(beanName); if(containsSubOption) { option.setPropertyName(propertyName); Added: trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParserConfiguration.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParserConfiguration.java (rev 0) +++ trunk/interfaces/src/main/java/org/dllearner/confparser3/ConfParserConfiguration.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -0,0 +1,125 @@ +package org.dllearner.confparser3; + +import org.dllearner.cli.ConfFileOption2; +import org.dllearner.configuration.IConfiguration; +import org.dllearner.configuration.IConfigurationProperty; +import org.dllearner.core.AnnComponentManager; +import org.dllearner.core.Component; +import org.springframework.core.io.Resource; + +import java.io.IOException; +import java.util.*; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 8/27/11 + * Time: 7:21 AM + * <p/> + * Conf Parser Based implementation. + */ +public class ConfParserConfiguration implements IConfiguration { + + private final ConfParser parser; + private final String baseDir; + private final String typeProperty = "type"; + + + public ConfParserConfiguration(Resource source) { + try { + baseDir = source.getFile().getAbsoluteFile().getParent(); + parser = new ConfParser(source.getInputStream()); + parser.Start(); + } catch (ParseException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public Object getObjectValue(String key) { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public Properties getProperties() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public Collection<String> getBeanNames() { + Set<String> result = new HashSet<String>(); + Map<String,List<ConfFileOption2>> beans = parser.getConfOptionsByBean(); + result.addAll(beans.keySet()); + return result; + } + + @Override + public Class getClass(String beanName) { + + List<ConfFileOption2> confOptions = parser.getConfOptionsByBean(beanName); + + ConfFileOption2 option = null; + for (ConfFileOption2 confOption : confOptions) { + if(typeProperty.equalsIgnoreCase(confOption.getPropertyName())){ + option = confOption; + } + } + + if(option == null){ + throw new RuntimeException("No type property set for bean: " + beanName); + } + + Class<?> result = null; + + String value = (String) option.getPropertyValue(); + // 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(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.getPropertyValue()); + } + return result; + } + + @Override + public Set<String> getPositiveExamples() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public Set<String> getNegativeExamples() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public String getBaseDir() { + return baseDir; + } + + @Override + public Collection<IConfigurationProperty> getConfigurationOptions(String beanName) { + List<ConfFileOption2> confFileOptions = parser.getConfOptionsByBean(beanName); + Collection<IConfigurationProperty> result = new ArrayList<IConfigurationProperty>(); + + for (ConfFileOption2 confFileOption : confFileOptions) { + + if (!typeProperty.equalsIgnoreCase(confFileOption.getPropertyName())) { + result.add(confFileOption); + } + } + return result; + } +} Added: trunk/interfaces/src/main/java/org/dllearner/confparser3/IndividualCollectionEditor.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser3/IndividualCollectionEditor.java (rev 0) +++ trunk/interfaces/src/main/java/org/dllearner/confparser3/IndividualCollectionEditor.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -0,0 +1,39 @@ +package org.dllearner.confparser3; + +import org.dllearner.core.owl.Individual; + +import java.beans.PropertyEditorSupport; +import java.util.Collection; +import java.util.StringTokenizer; +import java.util.TreeSet; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 8/27/11 + * Time: 11:42 AM + * + * Property Editor for Collections of Individuals. + */ +public class IndividualCollectionEditor extends PropertyEditorSupport { + + public IndividualCollectionEditor() { + } + + @Override + public void setAsText(String text) throws IllegalArgumentException { + setValue(convert(text)); + } + + + // @Override + protected Collection<Individual> convert(String value) { + Collection<Individual> result = new TreeSet<Individual>(); + StringTokenizer tokenizer = new StringTokenizer(value, "{}\", "); + while (tokenizer.hasMoreElements()) { + result.add(new Individual(tokenizer.nextToken())); + } + return result; + } + +} Added: trunk/interfaces/src/main/java/org/dllearner/confparser3/MapEditor.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser3/MapEditor.java (rev 0) +++ trunk/interfaces/src/main/java/org/dllearner/confparser3/MapEditor.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -0,0 +1,38 @@ +package org.dllearner.confparser3; + +import java.beans.PropertyEditorSupport; +import java.util.HashMap; +import java.util.Map; +import java.util.StringTokenizer; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 8/27/11 + * Time: 1:41 PM + * + * Convert our string structure to a Map + */ +public class MapEditor extends PropertyEditorSupport{ + + + @Override + public void setAsText(String text) throws IllegalArgumentException { + + StringTokenizer tokenizer = new StringTokenizer(text,"[(,\") ]"); + + if((tokenizer.countTokens() % 2) != 0){ + throw new RuntimeException("Expected an even number of tokens, check your map syntax: " + text); + } + + Map<String, String> result = new HashMap<String, String>(); + + while (tokenizer.hasMoreTokens()){ + String key = tokenizer.nextToken(); + String value = tokenizer.nextToken(); + result.put(key,value); + } + + setValue(result); + } +} Added: trunk/interfaces/src/main/java/org/dllearner/confparser3/SetEditor.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser3/SetEditor.java (rev 0) +++ trunk/interfaces/src/main/java/org/dllearner/confparser3/SetEditor.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -0,0 +1,29 @@ +package org.dllearner.confparser3; + +import java.beans.PropertyEditorSupport; +import java.util.*; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 8/27/11 + * Time: 1:57 PM + * <p/> + * Property Editor for Sets. + */ +public class SetEditor extends PropertyEditorSupport { + + @Override + public void setAsText(String text) throws IllegalArgumentException { + + StringTokenizer tokenizer = new StringTokenizer(text, "[(,\") ]"); + + Set<String> result = new HashSet<String>(); + + while (tokenizer.hasMoreTokens()) { + result.add(tokenizer.nextToken()); + } + + setValue(result); + } +} Modified: trunk/interfaces/src/main/java/org/dllearner/confparser3/conf3.jj =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser3/conf3.jj 2011-08-27 14:39:48 UTC (rev 3139) +++ trunk/interfaces/src/main/java/org/dllearner/confparser3/conf3.jj 2011-08-27 20:07:08 UTC (rev 3140) @@ -144,6 +144,7 @@ ConfFileOption2 option = new ConfFileOption2(); boolean isBeanRef = false; + boolean isBeanCollection = false; String beanName; String propertyName = ""; String propertyValue = ""; @@ -172,11 +173,11 @@ tmp=String() {values.add(tmp); propertyValue += "\"" + tmp + "\"";} "}" { propertyType = Set.class; propertyValue = "{"+ propertyValue + "}";; val = values; } // empty bean set - | "-" { val = new HashSet(); propertyType = Set.class; propertyValue = "-"; isBeanRef = true;} + | "-" { val = new HashSet(); propertyType = Set.class; propertyValue = "-"; isBeanCollection = true;} // set with several elements which are not quoted | "{" ( LOOKAHEAD(4) tmp=Id() { values.add(tmp); propertyValue += tmp + ", "; } "," )* ( tmp=Id()) {values.add(tmp); propertyValue += tmp;} "}" - { val = values; propertyType = Set.class; propertyValue = "{"+ propertyValue + "}"; isBeanRef = true;} + { val = values; propertyType = Set.class; propertyValue = "{"+ propertyValue + "}"; isBeanCollection = true;} // empty list | LOOKAHEAD("[" "]") "[" "]" { val = new LinkedList(); propertyType = List.class; propertyValue = "[]";} // a list with several elements, which tuples @@ -191,6 +192,7 @@ // <CONF_END> { option.setBeanRef(isBeanRef); + option.setBeanReferenceCollection(isBeanCollection); option.setBeanName(beanName); if(containsSubOption) { option.setPropertyName(propertyName); @@ -214,7 +216,7 @@ } String ComplexId() : -{ +{ Token t1,t2; } { Modified: trunk/interfaces/src/main/resources/org/dllearner/configuration/spring/bean-post-processor-configuration.xml =================================================================== --- trunk/interfaces/src/main/resources/org/dllearner/configuration/spring/bean-post-processor-configuration.xml 2011-08-27 14:39:48 UTC (rev 3139) +++ trunk/interfaces/src/main/resources/org/dllearner/configuration/spring/bean-post-processor-configuration.xml 2011-08-27 20:07:08 UTC (rev 3140) @@ -4,4 +4,15 @@ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.dllearner.configuration.spring.ComponentInitializationBeanPostProcessor"/> + + <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> + <property name="propertyEditorRegistrars"> + <list> + <ref bean="customPropertyEditorRegistrar"/> + </list> + </property> + </bean> + + <bean id="customPropertyEditorRegistrar" + class="org.dllearner.configuration.spring.CustomPropertyEditorRegistrar"/> </beans> \ No newline at end of file Modified: trunk/interfaces/src/test/java/org/dllearner/cli/FatherCLITest.java =================================================================== --- trunk/interfaces/src/test/java/org/dllearner/cli/FatherCLITest.java 2011-08-27 14:39:48 UTC (rev 3139) +++ trunk/interfaces/src/test/java/org/dllearner/cli/FatherCLITest.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -5,7 +5,7 @@ import org.dllearner.configuration.IConfiguration; import org.dllearner.configuration.spring.ApplicationContextBuilder; import org.dllearner.configuration.spring.DefaultApplicationContextBuilder; -import org.dllearner.confparser2.ConfParserConfiguration; +import org.dllearner.confparser3.ConfParserConfiguration; import org.dllearner.learningproblems.PosNegLPStandard; import org.junit.BeforeClass; import org.junit.Test; @@ -34,7 +34,7 @@ ApplicationContextBuilder builder = new DefaultApplicationContextBuilder(); /** The DL-Learner Config File */ - Resource confFile = new FileSystemResource("../examples/family/father.conf"); + Resource confFile = new FileSystemResource("../examples/family/father_new.conf"); // confFile.getAbsoluteFile().getParent( //Component Key Prefixes Modified: trunk/interfaces/src/test/java/org/dllearner/configuration/spring/ConfigurationBasedPropertyOverrideConfigurerTest.java =================================================================== --- trunk/interfaces/src/test/java/org/dllearner/configuration/spring/ConfigurationBasedPropertyOverrideConfigurerTest.java 2011-08-27 14:39:48 UTC (rev 3139) +++ trunk/interfaces/src/test/java/org/dllearner/configuration/spring/ConfigurationBasedPropertyOverrideConfigurerTest.java 2011-08-27 20:07:08 UTC (rev 3140) @@ -2,7 +2,7 @@ import junit.framework.Assert; import org.dllearner.configuration.IConfiguration; -import org.dllearner.confparser2.ConfParserConfiguration; +import org.dllearner.confparser3.ConfParserConfiguration; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -90,6 +90,7 @@ Assert.assertEquals(secondBean.getIntValue(), (Integer) 85); Assert.assertEquals(secondBean.getDoubleValue(), (Double) 178.5); Assert.assertTrue(secondBean.getSetValue().contains("e")); + Assert.assertTrue(secondBean.getSetValue().contains("f")); Assert.assertTrue(secondBean.getMapValue().get("f").equals("g")); Assert.assertTrue(secondBean.getComponent() != null); Assert.assertTrue(secondBean.getComponentSet().size() == 2); Modified: trunk/interfaces/src/test/resources/org/dllearner/configuration/spring/configurationBasedPropertyOverrideConfigurer.conf =================================================================== --- trunk/interfaces/src/test/resources/org/dllearner/configuration/spring/configurationBasedPropertyOverrideConfigurer.conf 2011-08-27 14:39:48 UTC (rev 3139) +++ trunk/interfaces/src/test/resources/org/dllearner/configuration/spring/configurationBasedPropertyOverrideConfigurer.conf 2011-08-27 20:07:08 UTC (rev 3140) @@ -5,7 +5,7 @@ // a single line comment, which is ignored testBean.simpleValue="simple value example" // simple value -testBean.component=component:secondBean // I wonder whether we even need the component keyword or could just write beanName.property=:test; +testBean.component=secondBean // I wonder whether we even need the component keyword or could just write beanName.property=:test; testBean.intValue = 23 // an integer value @@ -17,20 +17,17 @@ // Second Bean Definition - to show loading of a referenced bean secondBean.simpleValue="second bean example" // simple value -secondBean.component=component:thirdBean //Another Bean +secondBean.component=thirdBean //Another Bean secondBean.intValue = 85 // an integer value secondBean.doubleValue = 178.5 // a double value -secondBean.setValue={"e"} // a set (list is not implemented, but can be done analogously) +secondBean.setValue={"e","f"} // a set (list is not implemented, but can be done analogously) secondBean.mapValue=[("f","g"),("c","d")] // a map (we can use whatever syntax we like, this is the existing one) -secondBean.componentSet={"component:thirdBean","component:fourthBean"} +secondBean.componentSet={thirdBean,fourthBean} thirdBean.intValue=3 -thirdBean.component=component:fourthBean +thirdBean.component=fourthBean fourthBean.simpleValue="Fourth Bean - not specified in xml" fourthBean.type="org.dllearner.configuration.spring.TestBean" -// you can also specify positive and negative examples -// (not sure if we really need that) -+"http://example.org/pos1/" --"http://example.org/neg1/" \ No newline at end of file + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |