Menu

How To

Maciej Swiderski

How to use Simulation Engine ... from code

Simulation Engine could be used in two ways:

  • Make use of Simulation Provider
  • Assemble your simulation engine yourself

Make use of Simulation Provider

Since stand alone engine will not bring to much value - it is capable to build your own process definitions manually but this is more for test purpose rather real use case - it has to have an adapter available on runtime to be functional. Let's take Activiti as an example to illustrate how much code is needed to execute simulation of a give process definition (please not that it requires BPMN2.0)

First you have to build up your simulation context:

SimulationContext context = new SimulationContext();
SimulationContext.setContext(context);
context.setStaffPoolManager(ComponentFactory.getInstance(StaffPoolManager.class));

Next, some configuration is required to instruct about start and end time and how many instances of process definition should be created

Map<String, Object> configuration = new HashMap<String, Object>();
configuration.put(SimulationProvider.START_TIME_ATTR, new Date().getTime());
configuration.put(SimulationProvider.END_TIME_ATTR, new Date().getTime()+3600000);
configuration.put(SimulationProvider.INSTANCE_NUMBER_LIMITER_ATTR, 50);

Last part is to build Simulation Provider for selected adapter, run simulation and generate report

SimulationProvider provider = SimulationProvider.build("activiti", configuration);
int numberOfSimulatedDefinitions = provider.runSimulation("ExclusiveGatewayModel.bpmn20.xml", null);
Object report = provider.generateReport(0);
 ~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------

Assemble your simulation engine yourself
--------------------------
First, similar to SImulation Provider, SImulation Context must be created, the difference is that you set start, end time and number of instances on context directly

SimulationContext context = new SimulationContext();
SimulationContext.setContext(context);
context.setStaffPoolManager(ComponentFactory.getInstance(StaffPoolManager.class));
context.setSimulationStartTime(new Date().getTime());
context.setSimulationEndTime(new Date().getTime()+3600000);
context.setInstanceLimitation(50);

--------------------------------------

Next step is to assemble your process engine components manually and invoke engine stage operations

Load process definition

ProcessDefinitionLoader loader =
ComponentFactory.getInstance(ProcessDefinitionLoader.class, "activiti.simulation-activiti-def-loader");
loader.setDiagramResourceLocation("ExclusiveGatewayModel.bpmn20.xml");
loader.loadProcessDefinitions();
List<? extends ProcessDefinition> processDefinitions = loader.getProcessDefinitions();

--------------------------------------

Resolve alternative paths

ProcessPathResolver resolver = ComponentFactory.getInstance(ProcessPathResolver.class);
ProcessDefinition processDefinition = processDefinitions.get(0);
processDefinition.setProcessPaths(resolver.resolveProcessPaths(processDefinitions.get(0)));

--------------------------------------

Run simulation

SimulationStrategy timeBasedStrategy = ComponentFactory.getInstance(SimulationStrategy.class);
SimulationReporter result = timeBasedStrategy.simulate(processDefinition);

--------------------------------------

Generate report

SimulationReportGenerator reportGenerator = ComponentFactory.getInstance(SimulationReportGenerator.class, result);
Object report = reportGenerator.generateReportObject(null);
~~~~~~~~~~~~~~~~~~~~~~~~~


When to use which approach

I would recommend to use SimulationProvider if it is possible. It has main limitation - where there are more than one implementation of any of the core components (such as ProcessDefinitionLoader, ProcessPathResolver, etc) within one adapter then manual assembly must be used.


Related

Wiki: Simulation Engine

Discussion

Anonymous
Anonymous

Add attachments
Cancel