It is efficient to design generic plug-in types that are configured by properties at run-time. For example, a single ALU plugin may be used for various datapath widths, and a single MUX plugin may use various numbers of control bits and datapath widths.
This example illustrates a plug-in with a user-configurable property: the user may choose a bitwidth of either 3 or 5 input/output pins (again, merely copying its input to its output). The plug-in author can program any number of such properties. Property management is accomplished using the component's action menu and its property API.
package plugins.tutorial;
import java.io.IOException;
import java.util.Properties;
import dlsim.document.DLPlugIn;
/**
* Plugin03: sample plugin using default view
* 3 inputs, 3 outputs initially
* Menu-option for 3 or 5 inputs/outputs
* copies input to corresponding output
* all inputs/outputs are bundled and labeled
* Pins are number consecutively: 0, 1, 2 are input; 3, 4, 5 are output
*
* @author rms
*
*/
public class Plugin03 extends DLPlugIn {
private static final String SIZEPROP = "Size";
public static PluginInfo info =
new PluginInfo("My Fourth PlugIn",
"Writes input to output", "3 or 5 bits");
public Plugin03() throws IOException {
super(SIZEPROP, "3");
construct();
}
public Plugin03(Properties props) throws IOException {
super(props);
construct();
}
public void construct() {
setActions("size = 3", "size = 5");
int size = getIntegerProperty(SIZEPROP);
pinConfig(size);
}
/*
* Common setup routine for 3 or 5 bits
*/
private void pinConfig(int size) {
setInputSize(size);
setOutputSize(size);
setInMap(size);
setOutMap(size);
setLabels("in("+size+")", "out("+size+")");
setActionEnabled((size == 5) ? 0 : 1, true);
setActionEnabled((size == 5) ? 1 : 0, false);
}
/*
* Handler for menu selection
*
*/
public void performAction(int n) {
int size = (n == 0) ? 3 : 5;
pinConfig(size);
setProperty(SIZEPROP, size);
reInit();
}
public void evalState(int source, int val) {
putState(source+getInputSize(), val);
}
}
To manage properties, note the following: When using persistent properties we must add a second constructor accepting a java.util.Properties argument that it passes to super.
The two constructors should proceed identically after the call to super. In this case the common object construction method construct is called.
To set properties:
public void setProperty(String property, String value)
public void setProperty(String property, Integer value)
public void setProperty(String property, Long value)
public void setProperty(String property, Float value)
public void setProperty(String property, Double value)To get properties
public String getProperty(String property)
public Integer getIntegerProperty(String property)
public Long getLongProperty(String property)
public Float getFloatProperty(String property)
public Double getDoubleProperty(String property)
Other highlights:
Run DLSim 3 (using "ant run") and build the circuit shown below using Plugin01.
The circuit shows the action menu. Note the user defined actions "size = 3" and "size = 5".![]()
Exercise