Implementation Example

Transportation Planning defines a PluginAccessor singleton. It contains the list of plugins. It provides a method to register plugins as well as a method to get plugins.

The following sample code is a Factory for an Operator class that can be overridden by an EPT.

public class OperatorFactory extends Factory {
 
public static Operator getOperator(String className, DataModel dataModel){
 
String operatorName = className;
 
PluginAccessorContent pac = getPluginAccessorContent(dataModel);
Operator operator = pac.getOperatorPlugin(operatorName);
 
if (operator == null) {
operator = createOperator(operatorName, dataModel);
}
 
return operator;
 
}
 
private static Operator createOperator(String className, DataModel dataModel) {
Operator operator = null;
switch(className) {
case UpdateVehicleOnMovement.CLASS_NAME: {
operator = new UpdateVehicleOnMovement();
break;
}
}
return operator;
}
 
}


Within the engine, every time we are going to use an Operator, it calls for the Factory. The Factory looks for the plugin. If the EPT registers the plugin through the PluginAccessor, then it returns the plugin, else it returns the default Operator.

The following Unit Test shows it off.


public void testProcessPlugin() throws Exception {
//Build and solve MIP model using primitive data type
//Create data model
DataModel dataModel = new DataModel();
//This is the instantiation and registering of the plug in
//This would have been done by a capping module or an EPT
TestUniversalProcess u1 = new TestUniversalProcess();
PluginAccessor.getInstance().getPluginAccessorContent().registerProcessPlugin(Process.UNIVERSAL_PROCESS, u1);
//This is how a strategy would call to get the algorithm. This
//is how the strategy is indirectly linked.
Process p = ProcessFactory.getProcess(Process.UNIVERSAL_PROCESS, dataModel);
//This step isn't really required but it shows how our new version
//of singleton is acting like the original.
UniversalProcess u2 = (UniversalProcess)p;
 
//We do not normally uses System out. Normally request LOG. But looking to
//get this result in console so I can just see what result is (unit tests are
//running the server, the logs aren't available)
//System.out.print(u2.toString());
 
assertTrue("Not the correct singleton", u2.toString().equalsIgnoreCase("simple overload of toString to prove the override"));
//don't forget to add any test plug ins.
PluginAccessor.getInstance().getPluginAccessorContent().removeProcessPlugin(Process.UNIVERSAL_PROCESS);
 
}