Search in sources :

Example 6 with ResourceObject

use of de.hpi.bpt.scylla.simulation.ResourceObject in project scylla by bptlab.

the class DateTimeUtils method getTaskTerminationTime.

/**
 * Calculates the relative end time of a task. Consideres timetables of resources instances. If any resource
 * instance is idle, the duration is extended by the idle time.
 *
 * @param timeSpan
 *            the original duration of the task without any interruptions
 * @param presentTime
 *            current simulation time
 * @param tuple
 *            resource instances assigned to the task
 * @param event
 *            source event (for logging purposes)
 * @return the end time of the task
 */
public static TimeInstant getTaskTerminationTime(TimeSpan timeSpan, TimeInstant presentTime, ResourceObjectTuple tuple, ScyllaEvent event) {
    SimulationModel model = (SimulationModel) event.getModel();
    ProcessInstance processInstance = event.getProcessInstance();
    ProcessModel processModel = processInstance.getProcessModel();
    String source = event.getSource();
    String taskName = event.getDisplayName();
    int nodeId = event.getNodeId();
    String processScopeNodeId = SimulationUtils.getProcessScopeNodeId(processModel, nodeId);
    Set<String> resources = new HashSet<String>();
    Set<ResourceObject> resourceObjects = tuple.getResourceObjects();
    for (ResourceObject res : resourceObjects) {
        String resourceName = res.getResourceType() + "_" + res.getId();
        resources.add(resourceName);
    }
    // start
    long duration = timeSpan.getTimeRounded(timeUnit);
    if (duration == 0) {
        return presentTime;
    }
    ZonedDateTime dateTime = DateTimeUtils.getDateTime(presentTime);
    List<TimetableItem> timetable = tuple.getSharedTimetable();
    if (timetable == null) {
        return new TimeInstant(presentTime.getTimeRounded(timeUnit) + duration);
    }
    Integer index = null;
    for (int i = 0; i < timetable.size(); i++) {
        TimetableItem item = timetable.get(i);
        if (isWithin(dateTime, item)) {
            index = i;
            break;
        }
    }
    long timePassed = 0;
    while (timePassed < duration) {
        TimetableItem item = timetable.get(index);
        DayOfWeek untilWeekday = item.getWeekdayTo();
        LocalTime untilTime = item.getEndTime();
        ZonedDateTime dateTimeUntilEnd = getNextOrSameZonedDateTime(dateTime, untilWeekday, untilTime);
        long durationUntilEnd = chronoUnit.between(dateTime, dateTimeUntilEnd);
        long amountToAdd;
        if (timePassed + durationUntilEnd >= duration) {
            // task completes in current timetable item
            amountToAdd = duration - timePassed;
        } else {
            // until end of timetable item
            amountToAdd = durationUntilEnd;
        }
        timePassed += amountToAdd;
        dateTime = dateTime.plus(amountToAdd, chronoUnit);
        if (timePassed + durationUntilEnd < duration) {
            // task is not completed in current timetable item, so jump to the start of the next timetable item
            if (model.isOutputLoggingOn()) {
                // log idle during use
                ResourceStatus status = ResourceStatus.IN_USE_IDLE;
                long timeRelativeToStart = getTimeInstant(dateTime).getTimeRounded(timeUnit);
                ResourceInfo info = new ResourceInfo(timeRelativeToStart, status, processInstance, nodeId);
                for (ResourceObject obj : tuple.getResourceObjects()) {
                    String resourceType = obj.getResourceType();
                    String resourceId = obj.getId();
                    model.addResourceInfo(resourceType, resourceId, info);
                }
                ProcessNodeTransitionType transition = ProcessNodeTransitionType.PAUSE;
                ProcessNodeInfo nodeInfo = new ProcessNodeInfo(nodeId, processScopeNodeId, source, timeRelativeToStart, taskName, resources, transition);
                model.addNodeInfo(processModel, processInstance, nodeInfo);
            }
            index++;
            if (index == timetable.size()) {
                index = 0;
            }
            TimetableItem nextItem = timetable.get(index);
            untilWeekday = nextItem.getWeekdayFrom();
            untilTime = nextItem.getBeginTime();
            dateTime = getNextZonedDateTime(dateTime, untilWeekday, untilTime);
            if (model.isOutputLoggingOn()) {
                // log back to work
                ResourceStatus status = ResourceStatus.IN_USE;
                long timeRelativeToStart = getTimeInstant(dateTime).getTimeRounded(timeUnit);
                ResourceInfo info = new ResourceInfo(timeRelativeToStart, status, processInstance, nodeId);
                for (ResourceObject obj : tuple.getResourceObjects()) {
                    String resourceType = obj.getResourceType();
                    String resourceId = obj.getId();
                    model.addResourceInfo(resourceType, resourceId, info);
                }
                ProcessNodeTransitionType transition = ProcessNodeTransitionType.RESUME;
                ProcessNodeInfo nodeInfo = new ProcessNodeInfo(nodeId, processScopeNodeId, source, timeRelativeToStart, taskName, resources, transition);
                model.addNodeInfo(processModel, processInstance, nodeInfo);
            }
        }
    }
    TimeInstant timeInstant = getTimeInstant(dateTime);
    return timeInstant;
}
Also used : ProcessModel(de.hpi.bpt.scylla.model.process.ProcessModel) ResourceObject(de.hpi.bpt.scylla.simulation.ResourceObject) DayOfWeek(java.time.DayOfWeek) ResourceInfo(de.hpi.bpt.scylla.logger.ResourceInfo) LocalTime(java.time.LocalTime) ProcessNodeInfo(de.hpi.bpt.scylla.logger.ProcessNodeInfo) ZonedDateTime(java.time.ZonedDateTime) ProcessNodeTransitionType(de.hpi.bpt.scylla.logger.ProcessNodeTransitionType) ResourceStatus(de.hpi.bpt.scylla.logger.ResourceStatus) ProcessInstance(de.hpi.bpt.scylla.simulation.ProcessInstance) TimetableItem(de.hpi.bpt.scylla.model.global.resource.TimetableItem) SimulationModel(de.hpi.bpt.scylla.simulation.SimulationModel) TimeInstant(desmoj.core.simulator.TimeInstant) HashSet(java.util.HashSet)

Example 7 with ResourceObject

use of de.hpi.bpt.scylla.simulation.ResourceObject in project scylla by bptlab.

the class BatchPluginUtils method logTaskEventForNonResponsiblePI.

// If the execution type is parallel this makes the entry for the not really simulated process instances for tasks
void logTaskEventForNonResponsiblePI(TaskEvent event, ProcessInstance processInstance) throws ScyllaRuntimeException {
    ProcessInstance parentProcessInstance = processInstance.getParent();
    if (parentProcessInstance != null) {
        ProcessModel processModel = processInstance.getProcessModel();
        int parentNodeId = processModel.getNodeIdInParent();
        BatchPluginUtils pluginInstance = BatchPluginUtils.getInstance();
        BatchCluster cluster = pluginInstance.getRunningCluster(parentProcessInstance, parentNodeId);
        if (cluster != null && cluster.getBatchActivity().getExecutionType().equals(BatchClusterExecutionType.PARALLEL)) {
            SimulationModel model = (SimulationModel) event.getModel();
            long timestamp = Math.round(model.presentTime().getTimeRounded(DateTimeUtils.getReferenceTimeUnit()));
            String taskName = event.getDisplayName();
            int nodeId = event.getNodeId();
            String processScopeNodeId = SimulationUtils.getProcessScopeNodeId(processModel, nodeId);
            String source = event.getSource();
            ProcessNodeTransitionType transition;
            Set<String> resources = new HashSet<String>();
            if (event instanceof TaskEnableEvent) {
                transition = ProcessNodeTransitionType.ENABLE;
            } else if (event instanceof TaskBeginEvent) {
                transition = ProcessNodeTransitionType.BEGIN;
                Set<ResourceObject> resourceObjects = processInstance.getAssignedResources().get(source).getResourceObjects();
                for (ResourceObject res : resourceObjects) {
                    String resourceName = res.getResourceType() + "_" + res.getId();
                    resources.add(resourceName);
                }
                tasksAndResources.put(source, resources);
            } else if (event instanceof TaskCancelEvent) {
                transition = ProcessNodeTransitionType.CANCEL;
                resources = tasksAndResources.get(source);
                tasksAndResources.remove(source);
            } else if (event instanceof TaskTerminateEvent) {
                transition = ProcessNodeTransitionType.TERMINATE;
                resources = tasksAndResources.get(source);
                tasksAndResources.remove(source);
            } else {
                throw new ScyllaRuntimeException("Task event type not supported.");
            }
            int sourceSuffix = 0;
            List<ProcessInstance> processInstances = cluster.getProcessInstances();
            for (ProcessInstance pi : processInstances) {
                if (!processInstance.getParent().equals(pi)) {
                    // the source attribute comes from an event, but we did not really simulate the events for the
                    // non-responsible process instances, so we mock a source attribute value
                    String mockSource = source + "##" + ++sourceSuffix;
                    ProcessNodeInfo info;
                    info = new ProcessNodeInfo(nodeId, processScopeNodeId, mockSource, timestamp, taskName, resources, transition);
                    model.addNodeInfo(processModel, pi, info);
                }
            }
        }
    }
}
Also used : ScyllaRuntimeException(de.hpi.bpt.scylla.exception.ScyllaRuntimeException) ProcessModel(de.hpi.bpt.scylla.model.process.ProcessModel) ResourceObject(de.hpi.bpt.scylla.simulation.ResourceObject) HashSet(java.util.HashSet) Set(java.util.Set) ProcessNodeInfo(de.hpi.bpt.scylla.logger.ProcessNodeInfo) ProcessNodeTransitionType(de.hpi.bpt.scylla.logger.ProcessNodeTransitionType) ProcessInstance(de.hpi.bpt.scylla.simulation.ProcessInstance) SimulationModel(de.hpi.bpt.scylla.simulation.SimulationModel) HashSet(java.util.HashSet)

Aggregations

ProcessNodeInfo (de.hpi.bpt.scylla.logger.ProcessNodeInfo)7 ProcessNodeTransitionType (de.hpi.bpt.scylla.logger.ProcessNodeTransitionType)7 ResourceObject (de.hpi.bpt.scylla.simulation.ResourceObject)7 ProcessModel (de.hpi.bpt.scylla.model.process.ProcessModel)5 SimulationModel (de.hpi.bpt.scylla.simulation.SimulationModel)5 HashSet (java.util.HashSet)4 ResourceInfo (de.hpi.bpt.scylla.logger.ResourceInfo)3 ResourceStatus (de.hpi.bpt.scylla.logger.ResourceStatus)3 TimeInstant (desmoj.core.simulator.TimeInstant)3 ProcessInstance (de.hpi.bpt.scylla.simulation.ProcessInstance)2 FileOutputStream (java.io.FileOutputStream)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 TimeUnit (java.util.concurrent.TimeUnit)2 Document (org.jdom2.Document)2 Element (org.jdom2.Element)2 XMLOutputter (org.jdom2.output.XMLOutputter)2 ScyllaRuntimeException (de.hpi.bpt.scylla.exception.ScyllaRuntimeException)1 TimetableItem (de.hpi.bpt.scylla.model.global.resource.TimetableItem)1