http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/787864b6/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractClassOption.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractClassOption.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractClassOption.java new file mode 100644 index 0000000..1dd66c1 --- /dev/null +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractClassOption.java @@ -0,0 +1,236 @@ +package com.yahoo.labs.samoa.moa.options; + +/* + * #%L + * SAMOA + * %% + * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import java.io.File; +import com.github.javacliparser.AbstractOption; +import com.github.javacliparser.SerializeUtils; +import com.yahoo.labs.samoa.moa.core.ObjectRepository; +import com.yahoo.labs.samoa.moa.tasks.Task; +import com.yahoo.labs.samoa.moa.tasks.TaskMonitor; + +/** + * Abstract class option. + * + * @author Richard Kirkby ([email protected]) + * @version $Revision$ + */ +public abstract class AbstractClassOption extends AbstractOption { + + private static final long serialVersionUID = 1L; + + /** The prefix text to use to indicate file. */ + public static final String FILE_PREFIX_STRING = "file:"; + + /** The prefix text to use to indicate inmem. */ + public static final String INMEM_PREFIX_STRING = "inmem:"; + + /** The current object */ + protected Object currentValue; + + /** The class type */ + protected Class<?> requiredType; + + /** The default command line interface text. */ + protected String defaultCLIString; + + /** The null text. */ + protected String nullString; + + /** + * Creates a new instance of an abstract option given its class name, + * command line interface text, its purpose, its class type and its default + * command line interface text. + * + * @param name the name of this option + * @param cliChar the command line interface text + * @param purpose the text describing the purpose of this option + * @param requiredType the class type + * @param defaultCLIString the default command line interface text + */ + public AbstractClassOption(String name, char cliChar, String purpose, + Class<?> requiredType, String defaultCLIString) { + this(name, cliChar, purpose, requiredType, defaultCLIString, null); + } + + /** + * Creates a new instance of an abstract option given its class name, + * command line interface text, its purpose, its class type, default + * command line interface text, and its null text. + * + * @param name the name of this option + * @param cliChar the command line interface text + * @param purpose the text describing the purpose of this option + * @param requiredType the class type + * @param defaultCLIString the default command line interface text + * @param nullString the null text + */ + public AbstractClassOption(String name, char cliChar, String purpose, + Class<?> requiredType, String defaultCLIString, String nullString) { + super(name, cliChar, purpose); + this.requiredType = requiredType; + this.defaultCLIString = defaultCLIString; + this.nullString = nullString; + resetToDefault(); + } + + /** + * Sets current object. + * + * @param obj the object to set as current. + */ + public void setCurrentObject(Object obj) { + if (((obj == null) && (this.nullString != null)) + || this.requiredType.isInstance(obj) + || (obj instanceof String) + || (obj instanceof File) + || ((obj instanceof Task) && this.requiredType.isAssignableFrom(((Task) obj).getTaskResultType()))) { + this.currentValue = obj; + } else { + throw new IllegalArgumentException("Object not of required type."); + } + } + + /** + * Returns the current object. + * + * @return the current object + */ + public Object getPreMaterializedObject() { + return this.currentValue; + } + + /** + * Gets the class type of this option. + * + * @return the class type of this option + */ + public Class<?> getRequiredType() { + return this.requiredType; + } + + /** + * Gets the null string of this option. + * + * @return the null string of this option + */ + public String getNullString() { + return this.nullString; + } + + /** + * Gets a materialized object of this option. + * + * @param monitor the task monitor to use + * @param repository the object repository to use + * @return the materialized object + */ + public Object materializeObject(TaskMonitor monitor, + ObjectRepository repository) { + if ((this.currentValue == null) + || this.requiredType.isInstance(this.currentValue)) { + return this.currentValue; + } else if (this.currentValue instanceof String) { + if (repository != null) { + Object inmemObj = repository.getObjectNamed((String) this.currentValue); + if (inmemObj == null) { + throw new RuntimeException("No object named " + + this.currentValue + " found in repository."); + } + return inmemObj; + } + throw new RuntimeException("No object repository available."); + } else if (this.currentValue instanceof Task) { + Task task = (Task) this.currentValue; + Object result = task.doTask(monitor, repository); + return result; + } else if (this.currentValue instanceof File) { + File inputFile = (File) this.currentValue; + Object result = null; + try { + result = SerializeUtils.readFromFile(inputFile); + } catch (Exception ex) { + throw new RuntimeException("Problem loading " + + this.requiredType.getName() + " object from file '" + + inputFile.getName() + "':\n" + ex.getMessage(), ex); + } + return result; + } else { + throw new RuntimeException( + "Could not materialize object of required type " + + this.requiredType.getName() + ", found " + + this.currentValue.getClass().getName() + + " instead."); + } + } + + @Override + public String getDefaultCLIString() { + return this.defaultCLIString; + } + + /** + * Gets the command line interface text of the class. + * + * @param aClass the class + * @param requiredType the class type + * @return the command line interface text of the class + */ + public static String classToCLIString(Class<?> aClass, Class<?> requiredType) { + String className = aClass.getName(); + String packageName = requiredType.getPackage().getName(); + if (className.startsWith(packageName)) { + // cut off package name + className = className.substring(packageName.length() + 1, className.length()); + } else if (Task.class.isAssignableFrom(aClass)) { + packageName = Task.class.getPackage().getName(); + if (className.startsWith(packageName)) { + // cut off task package name + className = className.substring(packageName.length() + 1, + className.length()); + } + } + return className; + } + + @Override + public abstract String getValueAsCLIString(); + + @Override + public abstract void setValueViaCLIString(String s); + + //@Override + //public abstract JComponent getEditComponent(); + + /** + * Gets the class name without its package name prefix. + * + * @param className the name of the class + * @param expectedType the type of the class + * @return the class name without its package name prefix + */ + public static String stripPackagePrefix(String className, Class<?> expectedType) { + if (className.startsWith(expectedType.getPackage().getName())) { + return className.substring(expectedType.getPackage().getName().length() + 1); + } + return className; + } +}
http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/787864b6/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractOptionHandler.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractOptionHandler.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractOptionHandler.java new file mode 100644 index 0000000..22cc1f0 --- /dev/null +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/AbstractOptionHandler.java @@ -0,0 +1,193 @@ +package com.yahoo.labs.samoa.moa.options; + +/* + * #%L + * SAMOA + * %% + * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import com.github.javacliparser.Options; +import com.yahoo.labs.samoa.moa.AbstractMOAObject; +import com.yahoo.labs.samoa.moa.core.ObjectRepository; +import com.yahoo.labs.samoa.moa.tasks.NullMonitor; +import com.yahoo.labs.samoa.moa.tasks.TaskMonitor; + +/** + * Abstract Option Handler. All classes that have options in + * MOA extend this class. + * + * @author Richard Kirkby ([email protected]) + * @version $Revision: 7 $ + */ +public abstract class AbstractOptionHandler extends AbstractMOAObject implements + OptionHandler { + + private static final long serialVersionUID = 1L; + + /** Options to handle */ + //protected Options options; + + /** Dictionary with option texts and objects */ + //protected Map<String, Object> classOptionNamesToPreparedObjects; + + @Override + public String getPurposeString() { + return "Anonymous object: purpose undocumented."; + } + + @Override + public Options getOptions() { + /*if (this.options == null) { + this.options = new Options(); + if (this.config== null) { + this.config = new OptionsHandler(this, ""); + this.config.prepareForUse(); + } + Option[] myOptions = this.config.discoverOptionsViaReflection(); + for (Option option : myOptions) { + this.options.addOption(option); + } + } + return this.options;*/ + if ( this.config == null){ + this.config = new OptionsHandler(this, ""); + //this.config.prepareForUse(monitor, repository); + } + return this.config.getOptions(); + } + + @Override + public void prepareForUse() { + prepareForUse(new NullMonitor(), null); + } + + protected OptionsHandler config; + + @Override + public void prepareForUse(TaskMonitor monitor, ObjectRepository repository) { + //prepareClassOptions(monitor, repository); + if ( this.config == null){ + this.config = new OptionsHandler(this, ""); + this.config.prepareForUse(monitor, repository); + } + prepareForUseImpl(monitor, repository); + } + + /** + * This method describes the implementation of how to prepare this object for use. + * All classes that extends this class have to implement <code>prepareForUseImpl</code> + * and not <code>prepareForUse</code> since + * <code>prepareForUse</code> calls <code>prepareForUseImpl</code>. + * + * @param monitor the TaskMonitor to use + * @param repository the ObjectRepository to use + */ + protected abstract void prepareForUseImpl(TaskMonitor monitor, + ObjectRepository repository); + + @Override + public String getCLICreationString(Class<?> expectedType) { + return ClassOption.stripPackagePrefix(this.getClass().getName(), + expectedType) + + " " + getOptions().getAsCLIString(); + } + + @Override + public OptionHandler copy() { + return (OptionHandler) super.copy(); + } + + /** + * Gets the options of this class via reflection. + * + * @return an array of options + */ + /* protected Option[] discoverOptionsViaReflection() { + Class<? extends AbstractOptionHandler> c = this.getClass(); + Field[] fields = c.getFields(); + List<Option> optList = new LinkedList<Option>(); + for (Field field : fields) { + String fName = field.getName(); + Class<?> fType = field.getType(); + if (fType.getName().endsWith("Option")) { + if (Option.class.isAssignableFrom(fType)) { + Option oVal = null; + try { + field.setAccessible(true); + oVal = (Option) field.get(this); + } catch (IllegalAccessException ignored) { + // cannot access this field + } + if (oVal != null) { + optList.add(oVal); + } + } + } + } + return optList.toArray(new Option[optList.size()]); + }*/ + + /** + * Prepares the options of this class. + * + * @param monitor the TaskMonitor to use + * @param repository the ObjectRepository to use + */ + protected void prepareClassOptions(TaskMonitor monitor, + ObjectRepository repository) { + this.config.prepareClassOptions(monitor, repository); + }/* + this.classOptionNamesToPreparedObjects = null; + Option[] optionArray = getOptions().getOptionArray(); + for (Option option : optionArray) { + if (option instanceof ClassOption) { + ClassOption classOption = (ClassOption) option; + monitor.setCurrentActivity("Materializing option " + + classOption.getName() + "...", -1.0); + Object optionObj = classOption.materializeObject(monitor, + repository); + if (monitor.taskShouldAbort()) { + return; + } + if (optionObj instanceof OptionHandler) { + monitor.setCurrentActivity("Preparing option " + + classOption.getName() + "...", -1.0); + ((OptionHandler) optionObj).prepareForUse(monitor, + repository); + if (monitor.taskShouldAbort()) { + return; + } + } + if (this.classOptionNamesToPreparedObjects == null) { + this.classOptionNamesToPreparedObjects = new HashMap<String, Object>(); + } + this.classOptionNamesToPreparedObjects.put(option.getName(), + optionObj); + } + } + }*/ + + /** + * Gets a prepared option of this class. + * + * @param opt the class option to get + * @return an option stored in the dictionary + */ + protected Object getPreparedClassOption(ClassOption opt) { + return this.config.getPreparedClassOption(opt); + } +} http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/787864b6/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/ClassOption.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/ClassOption.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/ClassOption.java new file mode 100644 index 0000000..64eff95 --- /dev/null +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/ClassOption.java @@ -0,0 +1,175 @@ +package com.yahoo.labs.samoa.moa.options; + +/* + * #%L + * SAMOA + * %% + * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import java.io.File; +import com.github.javacliparser.Option; +import com.github.javacliparser.Options; +import com.yahoo.labs.samoa.moa.options.OptionHandler; +import com.yahoo.labs.samoa.moa.tasks.Task; + +/** + * Class option. + * + * @author Richard Kirkby ([email protected]) + * @version $Revision: 7 $ + */ +public class ClassOption extends AbstractClassOption { + + private static final long serialVersionUID = 1L; + + public ClassOption(String name, char cliChar, String purpose, + Class<?> requiredType, String defaultCLIString) { + super(name, cliChar, purpose, requiredType, defaultCLIString); + } + + public ClassOption(String name, char cliChar, String purpose, + Class<?> requiredType, String defaultCLIString, String nullString) { + super(name, cliChar, purpose, requiredType, defaultCLIString, nullString); + } + + @Override + public String getValueAsCLIString() { + if ((this.currentValue == null) && (this.nullString != null)) { + return this.nullString; + } + return objectToCLIString(this.currentValue, this.requiredType); + } + + @Override + public void setValueViaCLIString(String s) { + if ((this.nullString != null) + && ((s == null) || (s.length() == 0) || s.equals(this.nullString))) { + this.currentValue = null; + } else { + try { + this.currentValue = cliStringToObject(s, this.requiredType, + null); + } catch (Exception e) { + throw new IllegalArgumentException("Problems with option: " + getName(), e); + } + } + } + + public static String objectToCLIString(Object obj, Class<?> requiredType) { + if (obj == null) { + return ""; + } + if (obj instanceof File) { + return (FILE_PREFIX_STRING + ((File) obj).getPath()); + } + if (obj instanceof String) { + return (INMEM_PREFIX_STRING + obj); + } + String className = classToCLIString(obj.getClass(), requiredType); + if (obj instanceof OptionHandler) { + String subOptions = ((OptionHandler) obj).getOptions().getAsCLIString(); + if (subOptions.length() > 0) { + return (className + " " + subOptions); + } + } + return className; + } + + public static Object cliStringToObject(String cliString, + Class<?> requiredType, Option[] externalOptions) throws Exception { + if (cliString.startsWith(FILE_PREFIX_STRING)) { + return new File(cliString.substring(FILE_PREFIX_STRING.length())); + } + if (cliString.startsWith(INMEM_PREFIX_STRING)) { + return cliString.substring(INMEM_PREFIX_STRING.length()); + } + cliString = cliString.trim(); + int firstSpaceIndex = cliString.indexOf(' ', 0); + String className; + String classOptions; + if (firstSpaceIndex > 0) { + className = cliString.substring(0, firstSpaceIndex); + classOptions = cliString.substring(firstSpaceIndex + 1, cliString.length()); + classOptions = classOptions.trim(); + } else { + className = cliString; + classOptions = ""; + } + Class<?> classObject; + try { + classObject = Class.forName(className); + } catch (Throwable t1) { + try { + // try prepending default package + classObject = Class.forName(requiredType.getPackage().getName() + + "." + className); + } catch (Throwable t2) { + try { + // try prepending task package + classObject = Class.forName(Task.class.getPackage().getName() + + "." + className); + } catch (Throwable t3) { + throw new Exception("Class not found: " + className); + } + } + } + Object classInstance; + try { + classInstance = classObject.newInstance(); + } catch (Exception ex) { + throw new Exception("Problem creating instance of class: " + + className, ex); + } + if (requiredType.isInstance(classInstance) + || ((classInstance instanceof Task) && requiredType.isAssignableFrom(((Task) classInstance).getTaskResultType()))) { + Options options = new Options(); + if (externalOptions != null) { + for (Option option : externalOptions) { + options.addOption(option); + } + } + if (classInstance instanceof OptionHandler) { + Option[] objectOptions = ((OptionHandler) classInstance).getOptions().getOptionArray(); + for (Option option : objectOptions) { + options.addOption(option); + } + } + try { + options.setViaCLIString(classOptions); + } catch (Exception ex) { + throw new Exception("Problem with options to '" + + className + + "'." + + "\n\nValid options for " + + className + + ":\n" + + ((OptionHandler) classInstance).getOptions().getHelpString(), ex); + } finally { + options.removeAllOptions(); // clean up listener refs + } + } else { + throw new Exception("Class named '" + className + + "' is not an instance of " + requiredType.getName() + "."); + } + return classInstance; + } + + //@Override + //public JComponent getEditComponent() { + // return new ClassOptionEditComponent(this); + //} +} http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/787864b6/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionHandler.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionHandler.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionHandler.java new file mode 100644 index 0000000..b88cada --- /dev/null +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionHandler.java @@ -0,0 +1,79 @@ +package com.yahoo.labs.samoa.moa.options; + +/* + * #%L + * SAMOA + * %% + * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import com.github.javacliparser.Configurable; +import com.github.javacliparser.Options; +import com.yahoo.labs.samoa.moa.MOAObject; +import com.yahoo.labs.samoa.moa.core.ObjectRepository; +import com.yahoo.labs.samoa.moa.tasks.TaskMonitor; + +/** + * Interface representing an object that handles options or parameters. + * + * @author Richard Kirkby ([email protected]) + * @version $Revision: 7 $ + */ +public interface OptionHandler extends MOAObject, Configurable { + + /** + * Gets the purpose of this object + * + * @return the string with the purpose of this object + */ + public String getPurposeString(); + + /** + * Gets the options of this object + * + * @return the options of this object + */ + public Options getOptions(); + + /** + * This method prepares this object for use. + * + */ + public void prepareForUse(); + + /** + * This method prepares this object for use. + * + * @param monitor the TaskMonitor to use + * @param repository the ObjectRepository to use + */ + public void prepareForUse(TaskMonitor monitor, ObjectRepository repository); + + /** + * This method produces a copy of this object. + * + * @return a copy of this object + */ + @Override + public OptionHandler copy(); + + /** + * Gets the Command Line Interface text to create the object + * + * @return the Command Line Interface text to create the object + */ + public String getCLICreationString(Class<?> expectedType); +} http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/787864b6/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionsHandler.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionsHandler.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionsHandler.java new file mode 100644 index 0000000..c643115 --- /dev/null +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/options/OptionsHandler.java @@ -0,0 +1,202 @@ +package com.yahoo.labs.samoa.moa.options; + +/* + * #%L + * SAMOA + * %% + * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + + +import java.util.HashMap; +import com.github.javacliparser.JavaCLIParser; +import com.github.javacliparser.Option; +import com.yahoo.labs.samoa.moa.core.ObjectRepository; +import com.yahoo.labs.samoa.moa.tasks.NullMonitor; +import com.yahoo.labs.samoa.moa.tasks.TaskMonitor; + +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +/** + * + * @author abifet + */ +public class OptionsHandler extends JavaCLIParser { + + //public Object handler; + + public OptionsHandler(Object c, String cliString) { + super(c,cliString); + //this.handler = c; + //this.prepareForUse(); + /*int firstSpaceIndex = cliString.indexOf(' ', 0); + String classOptions; + String className; + if (firstSpaceIndex > 0) { + className = cliString.substring(0, firstSpaceIndex); + classOptions = cliString.substring(firstSpaceIndex + 1, cliString.length()); + classOptions = classOptions.trim(); + } else { + className = cliString; + classOptions = ""; + }*/ + //options.setViaCLIString(cliString); + } + + + //private static final long serialVersionUID = 1L; + + /** Options to handle */ + //protected Options options; + + /** Dictionary with option texts and objects */ + //protected Map<String, Object> classOptionNamesToPreparedObjects; + + + /*public String getPurposeString() { + return "Anonymous object: purpose undocumented."; + } + + public Options getOptions() { + if (this.options == null) { + this.options = new Options(); + Option[] myOptions = discoverOptionsViaReflection(); + for (Option option : myOptions) { + this.options.addOption(option); + } + } + return this.options; + }*/ + + public void prepareForUse() { + prepareForUse(new NullMonitor(), null); + } + + public void prepareForUse(TaskMonitor monitor, ObjectRepository repository) { + prepareClassOptions(monitor, repository); + //prepareForUseImpl(monitor, repository); + } + + /** + * This method describes the implementation of how to prepare this object for use. + * All classes that extends this class have to implement <code>prepareForUseImpl</code> + * and not <code>prepareForUse</code> since + * <code>prepareForUse</code> calls <code>prepareForUseImpl</code>. + * + * @param monitor the TaskMonitor to use + * @param repository the ObjectRepository to use + */ + //protected abstract void prepareForUseImpl(TaskMonitor monitor, + // ObjectRepository repository); + + /* public String getCLICreationString(Class<?> expectedType) { + return ClassOption.stripPackagePrefix(this.getClass().getName(), + expectedType) + + " " + getOptions().getAsCLIString(); + }*/ + + + /** + * Gets the options of this class via reflection. + * + * @return an array of options + */ + /*public Option[] discoverOptionsViaReflection() { + //Class<? extends AbstractOptionHandler> c = this.getClass(); + Class c = this.handler.getClass(); + Field[] fields = c.getFields(); + List<Option> optList = new LinkedList<Option>(); + for (Field field : fields) { + String fName = field.getName(); + Class<?> fType = field.getType(); + if (fType.getName().endsWith("Option")) { + if (Option.class.isAssignableFrom(fType)) { + Option oVal = null; + try { + field.setAccessible(true); + oVal = (Option) field.get(this.handler); + } catch (IllegalAccessException ignored) { + // cannot access this field + } + if (oVal != null) { + optList.add(oVal); + } + } + } + } + return optList.toArray(new Option[optList.size()]); + }*/ + + /** + * Prepares the options of this class. + * + * @param monitor the TaskMonitor to use + * @param repository the ObjectRepository to use + */ + public void prepareClassOptions(TaskMonitor monitor, + ObjectRepository repository) { + this.classOptionNamesToPreparedObjects = null; + Option[] optionArray = getOptions().getOptionArray(); + for (Option option : optionArray) { + if (option instanceof ClassOption) { + ClassOption classOption = (ClassOption) option; + monitor.setCurrentActivity("Materializing option " + + classOption.getName() + "...", -1.0); + Object optionObj = classOption.materializeObject(monitor, + repository); + if (monitor.taskShouldAbort()) { + return; + } + if (optionObj instanceof OptionHandler) { + monitor.setCurrentActivity("Preparing option " + + classOption.getName() + "...", -1.0); + ((OptionHandler) optionObj).prepareForUse(monitor, + repository); + if (monitor.taskShouldAbort()) { + return; + } + } + if (this.classOptionNamesToPreparedObjects == null) { + this.classOptionNamesToPreparedObjects = new HashMap<String, Object>(); + } + this.classOptionNamesToPreparedObjects.put(option.getName(), + optionObj); + } + } + } + + /** + * Gets a prepared option of this class. + * + * @param opt the class option to get + * @return an option stored in the dictionary + */ + public Object getPreparedClassOption(ClassOption opt) { + if (this.classOptionNamesToPreparedObjects == null) { + this.prepareForUse(); + } + return this.classOptionNamesToPreparedObjects.get(opt.getName()); + } + + //@Override + //public void getDescription(StringBuilder sb, int i) { + // throw new UnsupportedOperationException("Not supported yet."); + //} + +} http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/787864b6/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/ArffFileStream.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/ArffFileStream.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/ArffFileStream.java new file mode 100644 index 0000000..aa2717d --- /dev/null +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/ArffFileStream.java @@ -0,0 +1,199 @@ +package com.yahoo.labs.samoa.moa.streams; + +/* + * #%L + * SAMOA + * %% + * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; + +import com.github.javacliparser.FileOption; +import com.github.javacliparser.IntOption; +import com.yahoo.labs.samoa.instances.Instances; +import com.yahoo.labs.samoa.instances.InstancesHeader; +import com.yahoo.labs.samoa.moa.core.InputStreamProgressMonitor; +import com.yahoo.labs.samoa.moa.core.InstanceExample; +import com.yahoo.labs.samoa.moa.core.ObjectRepository; +import com.yahoo.labs.samoa.moa.options.AbstractOptionHandler; +import com.yahoo.labs.samoa.moa.tasks.TaskMonitor; + +/** + * Stream reader of ARFF files. + * + * @author Richard Kirkby ([email protected]) + * @version $Revision: 7 $ + */ +public class ArffFileStream extends AbstractOptionHandler implements InstanceStream { + + @Override + public String getPurposeString() { + return "A stream read from an ARFF file."; + } + + private static final long serialVersionUID = 1L; + + public FileOption arffFileOption = new FileOption("arffFile", 'f', + "ARFF file to load.", null, "arff", false); + + public IntOption classIndexOption = new IntOption( + "classIndex", + 'c', + "Class index of data. 0 for none or -1 for last attribute in file.", + -1, -1, Integer.MAX_VALUE); + + protected Instances instances; + + transient protected Reader fileReader; + + protected boolean hitEndOfFile; + + protected InstanceExample lastInstanceRead; + + protected int numInstancesRead; + + transient protected InputStreamProgressMonitor fileProgressMonitor; + + protected boolean hasStarted; + + public ArffFileStream() { + } + + public ArffFileStream(String arffFileName, int classIndex) { + this.arffFileOption.setValue(arffFileName); + this.classIndexOption.setValue(classIndex); + this.hasStarted = false; + restart(); + } + + @Override + public void prepareForUseImpl(TaskMonitor monitor, + ObjectRepository repository) { + //restart(); + this.hasStarted = false; + this.lastInstanceRead = null; + } + + @Override + public InstancesHeader getHeader() { + return new InstancesHeader(this.instances); + } + + @Override + public long estimatedRemainingInstances() { + double progressFraction = this.fileProgressMonitor.getProgressFraction(); + if ((progressFraction > 0.0) && (this.numInstancesRead > 0)) { + return (long) ((this.numInstancesRead / progressFraction) - this.numInstancesRead); + } + return -1; + } + + @Override + public boolean hasMoreInstances() { + return !this.hitEndOfFile; + } + + @Override + public InstanceExample nextInstance() { + if (this.lastInstanceRead == null) { + readNextInstanceFromFile(); + } + InstanceExample prevInstance = this.lastInstanceRead; + this.hitEndOfFile = !readNextInstanceFromFile(); + return prevInstance; + } + + @Override + public boolean isRestartable() { + return true; + } + + @Override + public void restart() { + try { + reset(); + //this.hitEndOfFile = !readNextInstanceFromFile(); + } catch (IOException ioe) { + throw new RuntimeException("ArffFileStream restart failed.", ioe); + } + } + + protected boolean readNextInstanceFromFile() { + boolean ret; + if (!this.hasStarted){ + try { + reset(); + ret = getNextInstanceFromFile(); + this.hitEndOfFile = !ret; + } catch (IOException ioe) { + throw new RuntimeException("ArffFileStream restart failed.", ioe); + } + this.hasStarted = true; + } else { + ret = getNextInstanceFromFile(); + } + return ret; + } + + @Override + public void getDescription(StringBuilder sb, int indent) { + // TODO Auto-generated method stub + } + + private void reset() throws IOException { + if (this.fileReader != null) { + this.fileReader.close(); + } + InputStream fileStream = new FileInputStream(this.arffFileOption.getFile()); + this.fileProgressMonitor = new InputStreamProgressMonitor( + fileStream); + this.fileReader = new BufferedReader(new InputStreamReader( + this.fileProgressMonitor)); + this.instances = new Instances(this.fileReader, 1, this.classIndexOption.getValue()); + if (this.classIndexOption.getValue() < 0) { + this.instances.setClassIndex(this.instances.numAttributes() - 1); + } else if (this.classIndexOption.getValue() > 0) { + this.instances.setClassIndex(this.classIndexOption.getValue() - 1); + } + this.numInstancesRead = 0; + this.lastInstanceRead = null; + } + + private boolean getNextInstanceFromFile() throws RuntimeException { + try { + if (this.instances.readInstance(this.fileReader)) { + this.lastInstanceRead = new InstanceExample(this.instances.instance(0)); + this.instances.delete(); // keep instances clean + this.numInstancesRead++; + return true; + } + if (this.fileReader != null) { + this.fileReader.close(); + this.fileReader = null; + } + return false; + } catch (IOException ioe) { + throw new RuntimeException( + "ArffFileStream failed to read instance from stream.", ioe); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/787864b6/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/ExampleStream.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/ExampleStream.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/ExampleStream.java new file mode 100644 index 0000000..dbdeba3 --- /dev/null +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/ExampleStream.java @@ -0,0 +1,79 @@ +package com.yahoo.labs.samoa.moa.streams; + +/* + * #%L + * SAMOA + * %% + * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import com.yahoo.labs.samoa.instances.InstancesHeader; +import com.yahoo.labs.samoa.moa.MOAObject; +import com.yahoo.labs.samoa.moa.core.Example; + +/** + * Interface representing a data stream of examples. + * + * @author Richard Kirkby ([email protected]) + * @version $Revision: 7 $ + */ +public interface ExampleStream<E extends Example> extends MOAObject { + + /** + * Gets the header of this stream. + * This is useful to know attributes and classes. + * InstancesHeader is an extension of weka.Instances. + * + * @return the header of this stream + */ + public InstancesHeader getHeader(); + + /** + * Gets the estimated number of remaining instances in this stream + * + * @return the estimated number of instances to get from this stream + */ + public long estimatedRemainingInstances(); + + /** + * Gets whether this stream has more instances to output. + * This is useful when reading streams from files. + * + * @return true if this stream has more instances to output + */ + public boolean hasMoreInstances(); + + /** + * Gets the next example from this stream. + * + * @return the next example of this stream + */ + public E nextInstance(); + + /** + * Gets whether this stream can restart. + * + * @return true if this stream can restart + */ + public boolean isRestartable(); + + /** + * Restarts this stream. It must be similar to + * starting a new stream from scratch. + * + */ + public void restart(); +} http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/787864b6/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/InstanceStream.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/InstanceStream.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/InstanceStream.java new file mode 100644 index 0000000..1166e81 --- /dev/null +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/InstanceStream.java @@ -0,0 +1,37 @@ +package com.yahoo.labs.samoa.moa.streams; + +/* + * #%L + * SAMOA + * %% + * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import com.yahoo.labs.samoa.moa.core.Example; +import com.yahoo.labs.samoa.instances.Instance; + +/** + * Interface representing a data stream of instances. + * + * @author Richard Kirkby ([email protected]) + * @version $Revision: 7 $ + */ +public interface InstanceStream extends ExampleStream<Example<Instance>> { + + + + +} http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/787864b6/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/clustering/ClusterEvent.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/clustering/ClusterEvent.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/clustering/ClusterEvent.java new file mode 100644 index 0000000..7062d99 --- /dev/null +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/clustering/ClusterEvent.java @@ -0,0 +1,50 @@ + +package com.yahoo.labs.samoa.moa.streams.clustering; + +/* + * #%L + * SAMOA + * %% + * Copyright (C) 2010 RWTH Aachen University, Germany + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import java.util.EventObject; + +public class ClusterEvent extends EventObject { + + private String type; + private String message; + private long timestamp; + + public ClusterEvent(Object source, long timestamp, String type, String message) { + super(source); + this.type = type; + this.message = message; + this.timestamp = timestamp; + } + + public String getMessage(){ + return message; + } + + public long getTimestamp(){ + return timestamp; + } + + public String getType(){ + return type; + } +} http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/787864b6/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/clustering/ClusterEventListener.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/clustering/ClusterEventListener.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/clustering/ClusterEventListener.java new file mode 100644 index 0000000..ff05afd --- /dev/null +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/clustering/ClusterEventListener.java @@ -0,0 +1,31 @@ + +package com.yahoo.labs.samoa.moa.streams.clustering; + +/* + * #%L + * SAMOA + * %% + * Copyright (C) 2010 RWTH Aachen University, Germany + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import java.util.EventListener; + +public interface ClusterEventListener extends EventListener { + + public void changeCluster(ClusterEvent e); + +} + http://git-wip-us.apache.org/repos/asf/incubator-samoa/blob/787864b6/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/clustering/ClusteringStream.java ---------------------------------------------------------------------- diff --git a/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/clustering/ClusteringStream.java b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/clustering/ClusteringStream.java new file mode 100644 index 0000000..6e38348 --- /dev/null +++ b/samoa-api/src/main/java/com/yahoo/labs/samoa/moa/streams/clustering/ClusteringStream.java @@ -0,0 +1,55 @@ + +package com.yahoo.labs.samoa.moa.streams.clustering; + +/* + * #%L + * SAMOA + * %% + * Copyright (C) 2010 RWTH Aachen University, Germany + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +import com.yahoo.labs.samoa.moa.options.AbstractOptionHandler; +import com.github.javacliparser.FloatOption; +import com.github.javacliparser.IntOption; +import com.yahoo.labs.samoa.moa.streams.InstanceStream; + +public abstract class ClusteringStream extends AbstractOptionHandler implements InstanceStream{ + public IntOption decayHorizonOption = new IntOption("decayHorizon", 'h', + "Decay horizon", 1000, 0, Integer.MAX_VALUE); + + public FloatOption decayThresholdOption = new FloatOption("decayThreshold", 't', + "Decay horizon threshold", 0.01, 0, 1); + + public IntOption evaluationFrequencyOption = new IntOption("evaluationFrequency", 'e', + "Evaluation frequency", 1000, 0, Integer.MAX_VALUE); + + public IntOption numAttsOption = new IntOption("numAtts", 'a', + "The number of attributes to generate.", 2, 0, Integer.MAX_VALUE); + + public int getDecayHorizon(){ + return decayHorizonOption.getValue(); + } + + public double getDecayThreshold(){ + return decayThresholdOption.getValue(); + } + + public int getEvaluationFrequency(){ + return evaluationFrequencyOption.getValue(); + } + + +}
