Search in sources :

Example 21 with TimeInstant

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);
    }
}
Also used : ProcessModel(de.hpi.bpt.scylla.model.process.ProcessModel) Duration(java.time.Duration) TimeSpan(desmoj.core.simulator.TimeSpan) ProcessSimulationComponents(de.hpi.bpt.scylla.simulation.ProcessSimulationComponents) ProcessModel(de.hpi.bpt.scylla.model.process.ProcessModel) SimulationModel(de.hpi.bpt.scylla.simulation.SimulationModel) Model(desmoj.core.simulator.Model) TimeUnit(java.util.concurrent.TimeUnit) ArrayList(java.util.ArrayList) List(java.util.List) ResourceObject(de.hpi.bpt.scylla.simulation.ResourceObject) TimeInstant(desmoj.core.simulator.TimeInstant)

Aggregations

TimeInstant (desmoj.core.simulator.TimeInstant)21 ProcessModel (de.hpi.bpt.scylla.model.process.ProcessModel)12 SimulationModel (de.hpi.bpt.scylla.simulation.SimulationModel)12 TimeSpan (desmoj.core.simulator.TimeSpan)10 TimeUnit (java.util.concurrent.TimeUnit)10 ScyllaRuntimeException (de.hpi.bpt.scylla.exception.ScyllaRuntimeException)8 ProcessInstance (de.hpi.bpt.scylla.simulation.ProcessInstance)8 ProcessSimulationComponents (de.hpi.bpt.scylla.simulation.ProcessSimulationComponents)6 List (java.util.List)6 ScyllaEvent (de.hpi.bpt.scylla.simulation.event.ScyllaEvent)5 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 ResourceObject (de.hpi.bpt.scylla.simulation.ResourceObject)4 HashMap (java.util.HashMap)4 ProcessNodeInfo (de.hpi.bpt.scylla.logger.ProcessNodeInfo)3 ProcessNodeTransitionType (de.hpi.bpt.scylla.logger.ProcessNodeTransitionType)3 ResourceInfo (de.hpi.bpt.scylla.logger.ResourceInfo)3 ResourceStatus (de.hpi.bpt.scylla.logger.ResourceStatus)3 MultipleStartNodesException (de.hpi.bpt.scylla.model.process.graph.exception.MultipleStartNodesException)3