context.getRepository().setSimulationInfo(new SimulationInfo(System.currentTimeMillis(), processId, numberOfAllInstances, interval));
final ReleaseId releaseId = createKJarWithMultipleResources(processId,
new String[]{bpmn2Container}, new ResourceType[]{ResourceType.BPMN2});
SimulationFluent f = new DefaultSimulationFluent(){
final KieServices kieServices = KieServices.Factory.get();
final KieContainer kieContainer = kieServices.newKieContainer( releaseId );
@Override
public KieSessionSimulationFluent newKieSession(final ReleaseId releaseId, final String id) {
assureActiveStep();
activeKieSessionId = id == null ? DEFAULT_ID : id;
addCommand( new NewKieSessionCommand(null, null) {
@Override
public KieSession execute(Context context) {
return id != null ? kieContainer.newKieSession( id ) : kieContainer.newKieSession();
}
});
addCommand( new SetVariableCommandFromLastReturn( StatefulKnowledgeSession.class.getName() ) );
return new DefaultStatefulKnowledgeSessionSimFluent( this );
}
};
// @formatter:off
int counter = 0;
int remainingInstances = numberOfAllInstances;
for (SimulationPath path : paths) {
// only paths that can be started are considered
if (!path.isStartable()) {
continue;
}
double probability = path.getProbability();
f.newPath("path" + counter);
int instancesOfPath = 1;
// count how many instances/steps should current path have
if (numberOfAllInstances > 1) {
instancesOfPath = (int) Math.round((numberOfAllInstances * probability));
// ensure that we won't exceed total number of instance due to rounding
if (instancesOfPath > remainingInstances) {
instancesOfPath = remainingInstances;
}
remainingInstances -= instancesOfPath;
for (int i = 0; i < instancesOfPath; i++) {
f.newStep( interval * i )
.newKieSession( releaseId, null)
.end()
.addCommand(new SimulateProcessPathCommand(processId, context, path))
.addCommand( new SetVariableCommandFromLastReturn( StatefulKnowledgeSession.class.getName() ))
.addCommand(new DisposeCommand());
}
} else {
f.newStep(interval)
.newKieSession(releaseId, null)
.end()
.addCommand(new SimulateProcessPathCommand(processId, context, path))
.addCommand( new SetVariableCommandFromLastReturn( StatefulKnowledgeSession.class.getName() ))
.addCommand(new DisposeCommand());
break;
}
counter++;
// currently standalone paths within single definition are not supported
// if (probability == 1) {
// // in case given path has probability of 100% there is a need to reset the remaining instances
// // as this is standalone process path
// remainingInstances = numberOfAllInstances;
// }
}
f.runSimulation();
// @formatter:on
context.getRepository().getSimulationInfo().setEndTime(context.getMaxEndTime());
return context.getRepository();