use of desmoj.core.simulator.TimeInstant in project scylla by bptlab.
the class BatchPluginUtils method assignToBatchCluster.
@SuppressWarnings("unchecked")
void assignToBatchCluster(ProcessInstance processInstance, int nodeId, TaskBeginEvent parentalBeginEvent) {
ProcessModel processModel = processInstance.getProcessModel();
ProcessSimulationComponents pSimComponents = parentalBeginEvent.getDesmojObjects();
/*Map<Integer, BatchActivity> batchActivities_old = (Map<Integer, BatchActivity>) pSimComponents.getSimulationConfiguration()
.getExtensionValue(PLUGIN_NAME, "batchActivities");*/
Map<Integer, BatchActivity> batchActivities = processModel.getBatchActivities();
BatchActivity activity = batchActivities.get(nodeId);
BatchCluster cluster = null;
// (1) select the right batch cluster
// (1a) check if there is already a batch with the data view
String processId = processModel.getId();
Map<Integer, List<BatchCluster>> batchClustersOfProcess = batchClusters.get(processId);
if (batchClustersOfProcess != null) {
List<BatchCluster> clusters = batchClustersOfProcess.get(nodeId);
if (clusters != null) {
for (BatchCluster bc : clusters) {
if (batchClusterHasNotStarted(bc.getState()) && isProcessInstanceMatchingToDataView(parentalBeginEvent, processInstance, bc)) {
cluster = bc;
// clusters.remove(bc);
break;
}
}
}
}
// (1b) if not, create a new one
if (cluster == null) {
Model model = processInstance.getModel();
boolean showInTrace = processInstance.traceIsOn();
Map<String, Object> dataView = this.getDataViewOfInstance(parentalBeginEvent, processInstance, activity);
TimeInstant currentSimulationTime = processInstance.presentTime();
cluster = new BatchCluster(model, currentSimulationTime, pSimComponents, activity, nodeId, dataView, showInTrace);
// schedule BatchClusterStart at current time plus maximum timeout
BatchClusterStartEvent clusterStartEvent = new BatchClusterStartEvent(model, cluster.getName(), showInTrace);
Duration timeout = activity.getActivationRule().getTimeOut(parentalBeginEvent, processInstance);
cluster.setCurrentTimeOut(timeout);
long timeoutInSeconds = timeout.get(ChronoUnit.SECONDS);
TimeSpan timeSpan = new TimeSpan(timeoutInSeconds, TimeUnit.SECONDS);
clusterStartEvent.schedule(cluster, timeSpan);
if (batchClustersOfProcess == null) {
batchClustersOfProcess = new HashMap<Integer, List<BatchCluster>>();
batchClusters.put(processId, batchClustersOfProcess);
List<BatchCluster> clusters = batchClustersOfProcess.get(nodeId);
if (clusters == null) {
clusters = new ArrayList<BatchCluster>();
batchClustersOfProcess.put(nodeId, clusters);
}
}
batchClustersOfProcess.get(nodeId).add(cluster);
}
// (2) add process instance to cluster
cluster.addProcessInstance(processInstance, parentalBeginEvent);
// TODO check whether timeout should be updated
if (cluster.getState() == BatchClusterState.INIT && cluster.getProcessInstances().size() > 1) {
// if the dueDate of the current instance is earlier as of the instances added before, the cluster begin event is rescheduled
Duration timeoutForCurrentInstance = activity.getActivationRule().getTimeOut(parentalBeginEvent, processInstance);
// testing
TimeUnit epsilon = TimeUnit.SECONDS;
Duration durationBtwClusterStartAndInstanceTaskBegin = Duration.ofSeconds((long) (parentalBeginEvent.getSimulationTimeOfSource().getTimeAsDouble(epsilon) - cluster.getCreationTime().getTimeAsDouble(epsilon)));
// System.out.println("InstanceEnable: "+parentalBeginEvent.getSimulationTimeOfSource().getTimeAsDouble(epsilon)+" ClusterCreation: "+cluster.getCreationTime().getTimeAsDouble(epsilon)+" Duration "+durationBtwClusterStartAndInstanceTaskBegin);
if (timeoutForCurrentInstance.plus(durationBtwClusterStartAndInstanceTaskBegin).compareTo(cluster.getCurrentTimeOut()) < 0) {
// set new timeout for the cluster for comparison
cluster.setCurrentTimeOut(timeoutForCurrentInstance);
// reschedule the cluster beginEvent
long timeoutInSeconds = timeoutForCurrentInstance.get(ChronoUnit.SECONDS);
TimeSpan timeSpan = new TimeSpan(timeoutInSeconds, TimeUnit.SECONDS);
BatchClusterStartEvent clusterStartEvent = (BatchClusterStartEvent) cluster.getScheduledEvents().get(0);
cluster.cancel();
clusterStartEvent.schedule(cluster, timeSpan);
}
}
if (cluster.getState() == BatchClusterState.MAXLOADED) {
// (2a) if bc is maxloaded, reschedule BatchClusterStart
// there is only one event already scheduled for the cluster which is the BatchClusterStart
BatchClusterStartEvent clusterStartEvent = (BatchClusterStartEvent) cluster.getScheduledEvents().get(0);
cluster.cancel();
// schedule for immediate execution
clusterStartEvent.schedule(cluster);
}
}
Aggregations