From: <sha...@us...> - 2011-08-19 23:24:46
|
Revision: 3074 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3074&view=rev Author: shadowtm Date: 2011-08-19 23:24:40 +0000 (Fri, 19 Aug 2011) Log Message: ----------- Added an IConfiguration interface to encapsulate the information needed for a DLLearner configuration. Made an implementation of this that wrapped the ConfParser. Added a test case to exercise all functionality of this implementation. Added Paths: ----------- trunk/components-core/src/main/java/org/dllearner/configuration/ trunk/components-core/src/main/java/org/dllearner/configuration/IConfiguration.java trunk/components-core/src/test/resources/ trunk/interfaces/src/main/java/org/dllearner/confparser2/ConfParserConfiguration.java trunk/interfaces/src/test/java/org/dllearner/confparser2/ trunk/interfaces/src/test/java/org/dllearner/confparser2/ConfParserConfigurationTest.java trunk/interfaces/src/test/resources/ trunk/interfaces/src/test/resources/org/ trunk/interfaces/src/test/resources/org/dllearner/ trunk/interfaces/src/test/resources/org/dllearner/confparser2/ trunk/interfaces/src/test/resources/org/dllearner/confparser2/confParserConfigurationTest.conf Added: trunk/components-core/src/main/java/org/dllearner/configuration/IConfiguration.java =================================================================== --- trunk/components-core/src/main/java/org/dllearner/configuration/IConfiguration.java (rev 0) +++ trunk/components-core/src/main/java/org/dllearner/configuration/IConfiguration.java 2011-08-19 23:24:40 UTC (rev 3074) @@ -0,0 +1,56 @@ +package org.dllearner.configuration; + +import java.util.Properties; +import java.util.Set; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 8/18/11 + * Time: 9:45 PM + * + * This interface defines our interaction with a DL-Learner specific configuration. + * + */ +public interface IConfiguration { + + /** + * Get the object for the given key. + * + * @param key The key of the object to retrieve. + * @return The Object representation of the value keyed by key. + */ + public Object getObjectValue(String key); + + /** + * Get a Properties object describing all of the properties which this configuration object + * knows about. + * + * As Properties are basically Map<String,String> objects, you can use getObjectValue(String key). To + * get the Object. + * + * @return A Properties Object. + */ + public Properties getProperties(); + + /** + * Get the set of positive examples associated with this configuration. + * + * Never returns null, only an empty set if there are no positive examples. + * + * @return The set of positive examples associated with this configuration. + */ + public Set<String> getPositiveExamples(); + + /** + * Get the set of negative examples associated with this configuration. + * + * Never returns null, only an empty set if there are no negative examples. + * + * @return The set of negative examples associated with this configuration. + */ + public Set<String> getNegativeExamples(); + + + +} Added: trunk/interfaces/src/main/java/org/dllearner/confparser2/ConfParserConfiguration.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/confparser2/ConfParserConfiguration.java (rev 0) +++ trunk/interfaces/src/main/java/org/dllearner/confparser2/ConfParserConfiguration.java 2011-08-19 23:24:40 UTC (rev 3074) @@ -0,0 +1,116 @@ +package org.dllearner.confparser2; + +import org.dllearner.cli.ConfFileOption; +import org.dllearner.configuration.IConfiguration; +import org.dllearner.utilities.datastructures.StringTuple; +import org.springframework.core.io.Resource; + +import java.io.IOException; +import java.util.*; + + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 8/19/11 + * Time: 2:30 PM + * + * Conf Parser based implementation of the IConfiguration interface. + * + * We use the ConfParser to read DL-Learn conf files. + */ +public class ConfParserConfiguration implements IConfiguration { + + private final ConfParser parser; + + public ConfParserConfiguration(Resource source){ + try { + 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 name) { + + Object result = null; + try { + ConfFileOption confOption = parser.getConfOptionsByName(name); + + result = confOption.getValue(); + result = convert(result); + } catch (Exception e) { + throw new RuntimeException("Problem with creating object named: " + name, e); + } + return result; + } + + /** + * Convert if we have certain types. + * + * @param input The object to convert if necessary. + * @return The converted version of input + */ + protected Object convert(Object input) { + Object result = input; + + /** Convert to a map if we have a list of String Tuples */ + if(input instanceof List){ + List set = (List) input; + Iterator iterator = set.iterator(); + if(iterator.hasNext()){ + Object firstElement = iterator.next(); + if(firstElement instanceof StringTuple){ + result = convertToMap(iterator, (StringTuple) firstElement); + } + } + + } + return result; + } + + private Map<String,String> convertToMap(Iterator iterator, StringTuple firstElement) { + + /** Convert to map */ + Map<String,String> result = new HashMap<String, String>(); + result.put(firstElement.a,firstElement.b); + + /** Add the rest of the tuples */ + while(iterator.hasNext()){ + StringTuple nextTuple = (StringTuple)iterator.next(); + result.put(nextTuple.a,nextTuple.b); + } + return result; + } + + @Override + public Properties getProperties() { + Properties props = new Properties(); + + List<ConfFileOption> options = parser.getConfOptions(); + for (ConfFileOption option : options) { + String fullName = option.getFullName(); + String stringValue = option.getValue().toString(); + try { + props.setProperty(fullName, stringValue); + } catch (Exception e) { + throw new RuntimeException("Problem with property name: " + fullName + " and value " + stringValue,e); + } + } + return props; + } + + @Override + public Set<String> getPositiveExamples() { + return parser.getPositiveExamples(); + } + + @Override + public Set<String> getNegativeExamples() { + return parser.getNegativeExamples(); + } +} Added: trunk/interfaces/src/test/java/org/dllearner/confparser2/ConfParserConfigurationTest.java =================================================================== --- trunk/interfaces/src/test/java/org/dllearner/confparser2/ConfParserConfigurationTest.java (rev 0) +++ trunk/interfaces/src/test/java/org/dllearner/confparser2/ConfParserConfigurationTest.java 2011-08-19 23:24:40 UTC (rev 3074) @@ -0,0 +1,123 @@ +package org.dllearner.confparser2; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; + +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +/** + * Created by IntelliJ IDEA. + * User: Chris + * Date: 8/19/11 + * Time: 4:26 PM + * <p/> + * Basic Test + */ +public class ConfParserConfigurationTest { + + private ConfParserConfiguration configuration; + + @Before + public void setUp() throws Exception { + Resource confFile = new ClassPathResource("/org/dllearner/confparser2/confParserConfigurationTest.conf"); + + configuration = new ConfParserConfiguration(confFile); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testStringValueProperty() throws Exception { + + Object simpleValue = configuration.getObjectValue("testBean.simpleValue"); + Assert.assertTrue(simpleValue instanceof String); + Assert.assertEquals("simple value example", simpleValue); + + } + + @Test + public void testComponentValueProperty() throws Exception { + + Object value = configuration.getObjectValue("testBean.component"); + /** We expect a string back here */ + Assert.assertTrue(value instanceof String); + Assert.assertEquals("component:test", value); + + } + + @Test + public void testIntValueProperty() throws Exception { + + Object value = configuration.getObjectValue("testBean.intValue"); + /** We expect a string back here */ + Assert.assertTrue(value instanceof Integer); + Assert.assertEquals(23, value); + + } + + @Test + public void testDoubleValueProperty() throws Exception { + + Object value = configuration.getObjectValue("testBean.doubleValue"); + /** We expect a string back here */ + Assert.assertTrue(value instanceof Double); + Assert.assertEquals(78.5d, (Double) value, .001); + + } + + @Test + public void testSetValueProperty() throws Exception { + + Object value = configuration.getObjectValue("testBean.setValue"); + /** We expect a string back here */ + Assert.assertTrue(value instanceof Set); + Set<String> set = (Set<String>) value; + Assert.assertTrue(set.size() == 1); + Assert.assertTrue(set.contains("a")); + + } + + @Test + public void testMapValueProperty() throws Exception { + + Object value = configuration.getObjectValue("testBean.mapValue"); + /** We expect a string back here */ + Assert.assertTrue(value instanceof Map); + Map<String, String> set = (Map<String, String>) value; + Assert.assertTrue(set.size() == 2); + Assert.assertEquals(set.get("a"), "b"); + Assert.assertEquals(set.get("c"), "d"); + } + + @Test + public void testGetProperties() throws Exception { + Properties properties = configuration.getProperties(); + Assert.assertTrue(properties.size() == 6); + Assert.assertTrue(properties.get("testBean.intValue").equals("23")); + + } + + @Test + public void testGetNegativeExamples(){ + Set<String> negativeExamples = configuration.getNegativeExamples(); + Assert.assertTrue(negativeExamples.size() == 1); + Assert.assertTrue(negativeExamples.contains("http://example.org/neg1/")); + } + + + @Test + public void testGetPositiveExamples(){ + Set<String> negativeExamples = configuration.getPositiveExamples(); + Assert.assertTrue(negativeExamples.size() == 1); + Assert.assertTrue(negativeExamples.contains("http://example.org/pos1/")); + } +} Added: trunk/interfaces/src/test/resources/org/dllearner/confparser2/confParserConfigurationTest.conf =================================================================== --- trunk/interfaces/src/test/resources/org/dllearner/confparser2/confParserConfigurationTest.conf (rev 0) +++ trunk/interfaces/src/test/resources/org/dllearner/confparser2/confParserConfigurationTest.conf 2011-08-19 23:24:40 UTC (rev 3074) @@ -0,0 +1,21 @@ +/** + * A multi-line comment, which is ignored. + */ + +// a single line comment, which is ignored +testBean.simpleValue="simple value example"; // simple value + +testBean.component=component:test; // I wonder whether we even need the component keyword or could just write beanName.property=:test; + +testBean.intValue = 23; // an integer value + +testBean.doubleValue = 78.5; // a double value + +testBean.setValue={"a"}; // a set (list is not implemented, but can be done analogously) + +testBean.mapValue=[("a","b"),("c","d")]; // a map (we can use whatever syntax we like, this is the existing one) + +// 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. |