Library for making life easier when dealing with Weka option handling.
There are two classes that help with Weka's option handling:
weka.core.WekaOptionUtils-- simplifies the code in thelistOptions,setOptionsandgetOptionsmethods.weka.core.GenerateOptionHandler-- uses a JSON definition from a config file to generate an abstract class that manages all the option handling and the user only needs to subclass it and implement the actual functionality.
Below is an example for a java.io.File option called model, with the -model
command-line flag. The tip text from the GUI is also used in the listOptions
method which generates the help on the command-line.
public static final String MODEL = "model";
protected File m_Model = getDefaultModel();
public Enumeration listOptions() {
Vector result = new Vector();
WekaOptionUtils.addOption(result, modelTipText(), "" + getDefaultModel(), MODEL);
WekaOptionUtils.add(result, super.listOptions());
return WekaOptionUtils.toEnumeration(result);
}
public void setOptions(String[] options) throws Exception {
setModel(WekaOptionUtils.parse(options, MODEL, getDefaultModel()));
super.setOptions(options);
}
public String[] getOptions() {
List<String> result = new ArrayList<>();
WekaOptionUtils.add(result, MODEL, getModel());
WekaOptionUtils.add(result, super.getOptions());
return WekaOptionUtils.toArray(result);
}
protected File getDefaultModel() {
return new File(".");
}
public void setModel(File value) {
m_Model = value;
}
public File getModel() {
return m_Model;
}
public String modelTipText() {
return "The model file to load and use.";
}The weka.core.GenerateOptionHandler class allows you to process one or more
JSON config files to generate abstract classes implementing all the option
handling.
usage: weka.core.GenerateOptionHandler
[-h] --configuration JSON [JSON ...] --output-dir DIR
[--add-package-structure] [--generate-dirs] [--verbose]
optional arguments:
-h, --help show this help message and exit
--configuration JSON [JSON ...]
The JSON file with the class/option
specifications.
--output-dir DIR The output directory for the generated class,
above the top-level package.
--add-package-structure
If enabled, the package structure gets added to
the output filename.
--generate-dirs If enabled, any missing output directories get
generated.
--verbose If enabled, outputs verbose debugging output.
The JSON file structure is very simple:
name: the name of the class, egMySVMpackage(optional): the Java package for the class, egweka.classifiers.functionsprefix(optional): prefix to use for the class, egAbstractsuffix(optional): prefix to use for the class, egBasesuperclass: the superclass for this class, egweka.classifiers.AbstractClassifierimplement(optional): array of other interfaces to implement, egweka.core.OptionHandlerauthor: the author to be used in the Javadocorganization: the organization that owns the copyrightoptions(optional): array of options
An option itself has the following properties:
property: the Java Bean property name (starts with lower case letter), egcapacitytype: the string denoting the type (primitive or class), egdoubleflag(optional): the string to use as command-line option (all lower case, no leading-), egcapacity; if left empty, will get automatically generated from the property name (multiClassStrategy->multi-class-strategy)default: the Java code snippet string with the default value, eg1.0ornew some.pkg.SomeClass()constraint(optional): the Java code snippet representing a boolean evaluation to guard accepting the value presented to thesetmethod (the parameter name used by thesetmethod is alwaysvalue), egvalue >= 0help: the help string to display in the user interface and to list on the commandline
This example configuration generates this Java source file.
In case you include this artifact in your Maven project, you can add a build
configuration that will regenerate your abstract classes using mvn exec:java:
...
<build>
...
<plugins>
...
<!-- for generating the base classes: mvn exec:java -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>weka.core.GenerateOptionHandler</mainClass>
<workingDirectory>.</workingDirectory>
<arguments>
<argument>--output-dir</argument>
<argument>${project.basedir}/src/main/java</argument>
<argument>--configuration</argument>
<argument>${project.basedir}/src/main/resources/mysvm.json</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
...Click on one of the following links to get to the corresponding release page:
Add the following dependency in your pom.xml to include the package:
<dependency>
<groupId>com.github.fracpete</groupId>
<artifactId>weka-option-utils</artifactId>
<version>2022.5.23</version>
<type>jar</type>
<exclusions>
<exclusion>
<groupId>nz.ac.waikato.cms.weka</groupId>
<artifactId>weka-dev</artifactId>
</exclusion>
</exclusions>
</dependency>