use of de.hpi.bpt.scylla.simulation.ProcessInstance in project scylla by bptlab.
the class SubprocessTBPlugin method eventRoutine.
@Override
public void eventRoutine(TaskBeginEvent desmojEvent, ProcessInstance processInstance) throws ScyllaRuntimeException {
ProcessModel processModel = processInstance.getProcessModel();
int nodeId = desmojEvent.getNodeId();
ProcessModel subProcess = processModel.getSubProcesses().get(nodeId);
if (subProcess != null) {
int indexOfTaskTerminateEvent = 0;
desmojEvent.getTimeSpanToNextEventMap().remove(indexOfTaskTerminateEvent);
TaskTerminateEvent event = (TaskTerminateEvent) desmojEvent.getNextEventMap().get(indexOfTaskTerminateEvent);
String name = processInstance.getName();
SubprocessPluginUtils pluginInstance = SubprocessPluginUtils.getInstance();
Map<Integer, TaskTerminateEvent> eventsOnHoldMap = pluginInstance.getEventsOnHold().get(name);
if (eventsOnHoldMap == null) {
pluginInstance.getEventsOnHold().put(name, new HashMap<Integer, TaskTerminateEvent>());
}
pluginInstance.getEventsOnHold().get(name).put(nodeId, event);
desmojEvent.getNextEventMap().remove(indexOfTaskTerminateEvent);
String source = desmojEvent.getSource();
ProcessSimulationComponents desmojObjects = desmojEvent.getDesmojObjects();
SimulationModel model = (SimulationModel) desmojEvent.getModel();
TimeInstant currentSimulationTime = model.presentTime();
boolean showInTrace = model.traceIsOn();
int processInstanceId = processInstance.getId();
try {
ProcessSimulationComponents desmojObjectsOfSubProcess = desmojObjects.getChildren().get(nodeId);
Integer startNodeId = subProcess.getStartNode();
ProcessInstance subProcessInstance = new ProcessInstance(model, subProcess, processInstanceId, showInTrace);
subProcessInstance.setParent(processInstance);
ScyllaEvent subProcessEvent = new BPMNStartEvent(model, source, currentSimulationTime, desmojObjectsOfSubProcess, subProcessInstance, startNodeId);
TimeSpan timeSpan = new TimeSpan(0);
int index = desmojEvent.getNewEventIndex();
desmojEvent.getNextEventMap().put(index, subProcessEvent);
desmojEvent.getTimeSpanToNextEventMap().put(index, timeSpan);
} catch (NodeNotFoundException | MultipleStartNodesException | NoStartNodeException e) {
DebugLogger.error(e.getMessage());
DebugLogger.log("Start node of process model " + subProcess.getId() + " not found.");
throw new ScyllaRuntimeException("Start node of process model " + subProcess.getId() + " not found.");
}
}
}
use of de.hpi.bpt.scylla.simulation.ProcessInstance in project scylla by bptlab.
the class SubprocessTCPlugin method eventRoutine.
@Override
public void eventRoutine(TaskCancelEvent desmojEvent, ProcessInstance processInstance) throws ScyllaRuntimeException {
ProcessModel processModel = processInstance.getProcessModel();
if (processModel.getParent() != null) {
int nodeIdInParent = processModel.getNodeIdInParent();
ProcessInstance parentProcessInstance = processInstance.getParent();
String parentProcessInstanceName = parentProcessInstance.getName();
SubprocessPluginUtils pluginInstance = SubprocessPluginUtils.getInstance();
Map<Integer, TaskTerminateEvent> eventsOnHoldMap = pluginInstance.getEventsOnHold().get(parentProcessInstanceName);
TaskTerminateEvent event = eventsOnHoldMap.get(nodeIdInParent);
if (event != null) {
SimulationModel model = (SimulationModel) event.getModel();
String source = event.getSource();
TaskCancelEvent cancelEvent = new TaskCancelEvent(model, source, event.getSimulationTimeOfSource(), event.getDesmojObjects(), event.getProcessInstance(), event.getNodeId());
cancelEvent.schedule(parentProcessInstance, new TimeSpan(0));
eventsOnHoldMap.remove(nodeIdInParent);
}
}
}
use of de.hpi.bpt.scylla.simulation.ProcessInstance in project scylla by bptlab.
the class ProcessInstanceGenerationEvent method eventRoutine.
// TODO XSD validation
// TODO fixed cost per task
@Override
public void eventRoutine(ProcessInstance processInstance) throws SuspendExecution {
this.processInstance = processInstance;
SimulationModel model = (SimulationModel) getModel();
TimeInstant currentSimulationTime = model.presentTime();
TimeUnit timeUnit = DateTimeUtils.getReferenceTimeUnit();
long currentTime = currentSimulationTime.getTimeRounded(timeUnit);
if (currentTime >= endTimeRelativeToGlobalStart) {
// if the end time is reached
return;
}
boolean showInTrace = traceIsOn();
String name = getName();
ProcessSimulationComponents desmojObjects = model.getDesmojObjectsMap().get(processId);
ProcessModel processModel = desmojObjects.getProcessModel();
try {
Integer startNodeId = processModel.getStartNode();
timeSpanToStartEvent = new TimeSpan(0);
ProcessInstanceGenerationEventPluggable.runPlugins(this, processInstance);
BPMNStartEvent event = new BPMNStartEvent(model, name, currentSimulationTime, desmojObjects, processInstance, startNodeId);
int processInstanceId = desmojObjects.incrementProcessInstancesStarted();
// schedule next process instance start event
if (processInstanceId <= desmojObjects.getSimulationConfiguration().getNumberOfProcessInstances()) {
double duration = desmojObjects.getDistributionSample(startNodeId);
TimeUnit unit = desmojObjects.getDistributionTimeUnit(startNodeId);
ProcessInstance nextProcessInstance = new ProcessInstance(model, processModel, processInstanceId, showInTrace);
timeSpanToNextProcessInstance = new TimeSpan(duration, unit);
ProcessInstanceGenerationEvent nextEvent = new ProcessInstanceGenerationEvent(model, processId, endTimeRelativeToGlobalStart, showInTrace);
nextEvent.schedule(nextProcessInstance, timeSpanToNextProcessInstance);
}
// schedule for start of simulation
event.schedule(processInstance, timeSpanToStartEvent);
} catch (NodeNotFoundException | MultipleStartNodesException | NoStartNodeException | ScyllaRuntimeException e) {
DebugLogger.error(e.getMessage());
e.printStackTrace();
DebugLogger.log("Error during instantiation of process model " + processModel.getId() + ".");
// no node initialized, use zero
int nodeId = 0;
SimulationUtils.abort(model, processInstance, nodeId, showInTrace);
}
}
use of de.hpi.bpt.scylla.simulation.ProcessInstance 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;
}
use of de.hpi.bpt.scylla.simulation.ProcessInstance in project scylla by bptlab.
the class BatchPluginUtils method setClusterToTerminated.
void setClusterToTerminated(ProcessInstance processInstance, int nodeId) {
ProcessModel processModel = processInstance.getProcessModel();
String processId = processModel.getId();
if (batchClusters.containsKey(processId)) {
Map<Integer, List<BatchCluster>> batchClustersOfProcess = batchClusters.get(processId);
if (batchClustersOfProcess.containsKey(nodeId)) {
List<BatchCluster> clusters = batchClustersOfProcess.get(nodeId);
for (BatchCluster bc : clusters) {
List<ProcessInstance> clusterProcessInstances = bc.getProcessInstances();
if (bc.getState() == BatchClusterState.RUNNING && clusterProcessInstances.contains(processInstance)) {
bc.setState(BatchClusterState.TERMINATED);
// return clusters.remove(bc);
}
}
}
}
// return false;
}
Aggregations