use of de.hpi.bpt.scylla.simulation.ProcessInstance in project scylla by bptlab.
the class BatchTBPlugin method eventRoutine.
@SuppressWarnings("unchecked")
@Override
public void eventRoutine(TaskBeginEvent event, ProcessInstance processInstance) throws ScyllaRuntimeException {
// System.out.println(event + " with display name " + event.getDisplayName() + " || " + event.getNextEventMap() + " and source " + event.getSource());
BatchPluginUtils pluginInstance = BatchPluginUtils.getInstance();
pluginInstance.logTaskEventForNonResponsiblePI(event, processInstance);
ProcessSimulationComponents desmojObjects = event.getDesmojObjects();
// SimulationModel model = (SimulationModel) desmojEvent.getModel();
int nodeId = event.getNodeId();
ProcessModel processModel = processInstance.getProcessModel();
ProcessInstance parentProcessInstance = processInstance.getParent();
if (parentProcessInstance != null) {
int parentNodeId = processModel.getNodeIdInParent();
BatchCluster cluster = pluginInstance.getRunningCluster(parentProcessInstance, parentNodeId);
// If we are the representative (first executed) process instance we add the setUp time for this task
if (cluster != null && parentProcessInstance == cluster.getResponsibleProcessInstance()) {
// therefore we fist take a sample of the setUp distribution
double setUpTimeToAdd = desmojObjects.getSetUpDistributionSample(nodeId);
TimeUnit unit = desmojObjects.getSetUpDistributionTimeUnit(nodeId);
TimeSpan setUpTimeToAddAsTimeSpan = new TimeSpan(setUpTimeToAdd, unit);
// get the old value (this will always be the entry 0 in our map, because it's always the next)
double standardTime = event.getTimeSpanToNextEventMap().get(0).getTimeAsDouble(TimeUnit.SECONDS);
// and overwrite the time to the next task in the timeSpanToNextEventMap (=set the calculated time as the new time)
TimeSpan timeForTaskWithSetUp = new TimeSpan(standardTime + setUpTimeToAddAsTimeSpan.getTimeAsDouble(TimeUnit.SECONDS), TimeUnit.SECONDS);
event.getTimeSpanToNextEventMap().put(0, timeForTaskWithSetUp);
}
}
// SimulationConfiguration simulationConfiguration = desmojObjects.getSimulationConfiguration();
/*Map<Integer, BatchActivity> batchActivities = (Map<Integer, BatchActivity>) simulationConfiguration
.getExtensionValue(getName(), "batchActivities");*/
Map<Integer, BatchActivity> batchActivities = processModel.getBatchActivities();
if (batchActivities.containsKey(nodeId) && processModel.getSubProcesses().containsKey(nodeId)) {
// subprocess plugin wants to schedule BPMNStartEvents for subprocess
// we prevent it
Map<Integer, ScyllaEvent> nextEventMap = event.getNextEventMap();
Map<Integer, TimeSpan> timeSpanToNextEventMap = event.getTimeSpanToNextEventMap();
for (Integer indexOfSubprocessBPMNStartEvent : nextEventMap.keySet()) {
ScyllaEvent eventToSchedule = nextEventMap.get(indexOfSubprocessBPMNStartEvent);
if (eventToSchedule instanceof BPMNStartEvent || eventToSchedule instanceof TaskTerminateEvent) {
nextEventMap.remove(indexOfSubprocessBPMNStartEvent);
timeSpanToNextEventMap.remove(indexOfSubprocessBPMNStartEvent);
break;
}
}
}
}
use of de.hpi.bpt.scylla.simulation.ProcessInstance in project scylla by bptlab.
the class BatchTCPlugin method eventRoutine.
@Override
public void eventRoutine(TaskCancelEvent event, ProcessInstance processInstance) throws ScyllaRuntimeException {
BatchPluginUtils pluginInstance = BatchPluginUtils.getInstance();
pluginInstance.logTaskEventForNonResponsiblePI(event, processInstance);
ProcessInstance parentProcessInstance = processInstance.getParent();
if (parentProcessInstance != null) {
ProcessModel processModel = processInstance.getProcessModel();
int parentNodeId = processModel.getNodeIdInParent();
BatchCluster cluster = pluginInstance.getRunningCluster(parentProcessInstance, parentNodeId);
if (cluster != null) {
List<TaskTerminateEvent> parentalEndEvents = cluster.getParentalEndEvents();
for (TaskTerminateEvent pee : parentalEndEvents) {
TaskCancelEvent cancelEvent = new TaskCancelEvent(pee.getModel(), pee.getSource(), pee.getSimulationTimeOfSource(), pee.getDesmojObjects(), pee.getProcessInstance(), pee.getNodeId());
cancelEvent.schedule(pee.getProcessInstance());
}
parentalEndEvents.clear();
pluginInstance.setClusterToTerminated(parentProcessInstance, parentNodeId);
}
}
}
use of de.hpi.bpt.scylla.simulation.ProcessInstance in project scylla by bptlab.
the class BoundaryEventPluginUtils method createTimerBoundaryEvents.
// I did not touch this for now. Hopefully could be deleted in future.
private void createTimerBoundaryEvents(SimulationModel model, BoundaryObject bo, double startOfInterval, double endOfInterval) throws ScyllaRuntimeException {
double beginTimeOfTask = bo.getBeginTimeOfTask();
ProcessSimulationComponents desmojObjects = bo.getDesmojObjects();
ProcessModel processModel = desmojObjects.getProcessModel();
Map<Integer, EventType> eventTypes = processModel.getEventTypes();
Map<Integer, Boolean> cancelActivities = processModel.getCancelActivities();
List<Integer> referenceToBoundaryEvents = bo.getReferenceToBoundaryEvents();
for (Integer nId : referenceToBoundaryEvents) {
boolean timerEventIsInterrupting = false;
EventType eventType = eventTypes.get(nId);
if (eventType == EventType.BOUNDARY) {
Map<EventDefinitionType, Map<String, String>> eventDefinitions = processModel.getEventDefinitions().get(nId);
Map<String, String> definitionAttributes = eventDefinitions.get(EventDefinitionType.TIMER);
if (definitionAttributes != null) {
// if boundary event is timer event
double timeUntilWhenTimerEventsAreCreated = bo.getTimeUntilWhenTimerEventsAreCreated();
if (definitionAttributes.get("timeDuration") != null) {
// ISO 8601 duration
String timeDuration = definitionAttributes.get("timeDuration");
if (beginTimeOfTask != timeUntilWhenTimerEventsAreCreated) {
// timer event has already been created once, skip
continue;
}
Duration javaDuration = Duration.parse(timeDuration);
double duration = javaDuration.get(ChronoUnit.SECONDS);
if (duration == 0) {
continue;
}
double timeToSchedule = beginTimeOfTask + duration;
if (timeToSchedule < endOfInterval) {
String displayName = processModel.getDisplayNames().get(nId);
if (displayName == null) {
displayName = processModel.getIdentifiers().get(nId);
}
String source = bo.getSource();
ProcessInstance processInstance = bo.getProcessInstance();
TimeInstant timeInstant = new TimeInstant(startOfInterval, TimeUnit.SECONDS);
BPMNIntermediateEvent event = new BPMNIntermediateEvent(model, source, timeInstant, desmojObjects, processInstance, nId);
bo.getBoundaryEventsToSchedule().computeIfAbsent(timeToSchedule, k -> new ArrayList<BPMNIntermediateEvent>());
bo.getBoundaryEventsToSchedule().get(timeToSchedule).add(event);
String message = "Schedule boundary timer event: " + displayName;
bo.getMessagesOfBoundaryEventsToSchedule().computeIfAbsent(timeToSchedule, k -> new ArrayList<String>());
bo.getMessagesOfBoundaryEventsToSchedule().get(timeToSchedule).add(message);
// timeUntilWhenTimerEventsAreCreated = timeToSchedule;
}
// TODO fix boundary
timeUntilWhenTimerEventsAreCreated = timeToSchedule;
} else if (definitionAttributes.get("timeCycle") != null) {
// ISO 8601 repeating time interval:
String timeCycle = definitionAttributes.get("timeCycle");
// Rn/[ISO 8601 duration] where n
// (optional) for number of
// recurrences
// ["Rn"], "[ISO 8601 duration]"]
String[] recurrencesAndDuration = timeCycle.split("/");
String recurrencesString = recurrencesAndDuration[0];
String timeDurationString = recurrencesAndDuration[1];
Integer recurrencesMax = null;
if (recurrencesString.length() > 1) {
recurrencesMax = Integer.parseInt(recurrencesString.substring(1, recurrencesString.length()));
timerEventIsInterrupting = cancelActivities.get(nId);
if (timerEventIsInterrupting) {
recurrencesMax = 1;
}
}
Duration javaDuration = Duration.parse(timeDurationString);
double duration = javaDuration.get(ChronoUnit.SECONDS);
if (duration == 0 || recurrencesMax != null && recurrencesMax == 0) {
continue;
}
double timeToSchedule = beginTimeOfTask;
int actualNumberOfOccurrences = 0;
boolean recurrencesMaxExceeded = false;
while (timeToSchedule <= timeUntilWhenTimerEventsAreCreated) {
timeToSchedule += duration;
actualNumberOfOccurrences++;
if (recurrencesMax != null && actualNumberOfOccurrences > recurrencesMax) {
recurrencesMaxExceeded = true;
break;
}
}
if (recurrencesMaxExceeded) {
continue;
}
while (timeToSchedule <= endOfInterval) {
// add as many timer events for scheduling as possible (lots of them if timer event is
// non-interrupting,
// only one if it is interrupting
String displayName = processModel.getDisplayNames().get(nId);
if (displayName == null) {
displayName = processModel.getIdentifiers().get(nId);
}
String source = bo.getSource();
ProcessInstance processInstance = bo.getProcessInstance();
TimeInstant timeInstant = new TimeInstant(startOfInterval, TimeUnit.SECONDS);
BPMNIntermediateEvent event = new BPMNIntermediateEvent(model, source, timeInstant, desmojObjects, processInstance, nId);
bo.getBoundaryEventsToSchedule().computeIfAbsent(timeToSchedule, k -> new ArrayList<BPMNIntermediateEvent>());
bo.getBoundaryEventsToSchedule().get(timeToSchedule).add(event);
String message = "Schedule boundary timer event: " + displayName;
bo.getMessagesOfBoundaryEventsToSchedule().computeIfAbsent(timeToSchedule, k -> new ArrayList<String>());
bo.getMessagesOfBoundaryEventsToSchedule().get(timeToSchedule).add(message);
actualNumberOfOccurrences++;
if (recurrencesMax != null && actualNumberOfOccurrences == recurrencesMax) {
// recurrencesMaxExceeded = true;
break;
}
timeToSchedule += duration;
}
timeUntilWhenTimerEventsAreCreated = timeToSchedule;
} else {
// TODO support timeDate attributes?
String identifier = processModel.getIdentifiers().get(nId);
DebugLogger.log("Timer event " + identifier + " has no timer definition, skip.");
continue;
}
bo.setTimeUntilWhenTimerEventsAreCreated(timeUntilWhenTimerEventsAreCreated);
}
}
}
}
use of de.hpi.bpt.scylla.simulation.ProcessInstance in project scylla by bptlab.
the class BoundaryEventPluginUtils method createNonTimerBoundaryEvents.
private void createNonTimerBoundaryEvents(SimulationModel model, BoundaryObject bo, double startOfInterval, double endOfInterval) throws ScyllaRuntimeException {
double timeUntilWhenNonTimerEventsAreCreated = bo.getTimeUntilWhenNonTimerEventsAreCreated();
if (!bo.isGenerateMoreNonTimerBoundaryEvents() || timeUntilWhenNonTimerEventsAreCreated >= endOfInterval) {
return;
}
ProcessSimulationComponents desmojObjects = bo.getDesmojObjects();
ProcessModel processModel = desmojObjects.getProcessModel();
Map<Integer, EventType> eventTypes = processModel.getEventTypes();
Map<Integer, Boolean> cancelActivities = processModel.getCancelActivities();
int nodeId = bo.getNodeId();
while (timeUntilWhenNonTimerEventsAreCreated < endOfInterval) {
// If the parent task has not already ended...
// simulation configuration defines probability of firing boundary events
Map<Integer, Object> branchingDistributions = desmojObjects.getExtensionDistributions().get(PLUGIN_NAME);
@SuppressWarnings("unchecked") DiscreteDistEmpirical<Integer> distribution = (DiscreteDistEmpirical<Integer>) branchingDistributions.get(nodeId);
if (distribution == null) {
// There are no non-timer boundary events at this task...
bo.setGenerateMoreNonTimerBoundaryEvents(false);
return;
}
// decide on next node
model.skipTraceNote();
Integer nodeIdOfElementToSchedule = distribution.sample();
// System.out.println("Choosed: "+processModel.getIdentifiers().get(nodeIdOfElementToSchedule)+" "+processModel.getIdentifiers().get(nodeId));
if (nodeIdOfElementToSchedule == nodeId) {
// No next boundary non-timer event, finish
bo.setGenerateMoreNonTimerBoundaryEvents(false);
return;
} else {
// There are boundary events
EventType eventType = eventTypes.get(nodeIdOfElementToSchedule);
if (eventType == EventType.BOUNDARY) {
// Determine whether the boundary event to schedule is an interrupting one.
boolean eventIsInterrupting = cancelActivities.get(nodeIdOfElementToSchedule);
// Get time relative to the start of the task when this boundary event will trigger.
double relativeTimeToTrigger = desmojObjects.getDistributionSample(nodeIdOfElementToSchedule);
if (relativeTimeToTrigger == 0) {
// If this happens something is wrong anyways...
continue;
}
// Add the relative time of this boundary event, to determine when no more events are scheduled.
TimeUnit unit = desmojObjects.getDistributionTimeUnit(nodeIdOfElementToSchedule);
TimeSpan durationAsTimeSpan = new TimeSpan(relativeTimeToTrigger, unit);
timeUntilWhenNonTimerEventsAreCreated += durationAsTimeSpan.getTimeAsDouble(TimeUnit.SECONDS);
// Took this message sending part out. It was just to complicated for boundary events, fixed to their parent task.
// Furthermore it is not needed anymore.
/*String message = null;
boolean showInTrace = model.traceIsOn();
Map<EventDefinitionType, Map<String, String>> definitions = processModel.getEventDefinitions().get(nodeIdOfElementToSchedule);
String displayName = processModel.getDisplayNames().get(nodeIdOfElementToSchedule);
if (displayName == null) {
displayName = processModel.getIdentifiers().get(nodeIdOfElementToSchedule);
}
for (EventDefinitionType definition : definitions.keySet()) {
if (definition == EventDefinitionType.MESSAGE) {
message = "Schedule boundary message event: " + displayName;
}
else if (definition == EventDefinitionType.CONDITIONAL) {
message = "Schedule boundary conditional event: " + displayName;
}
else if (definition == EventDefinitionType.SIGNAL) {
message = "Schedule boundary signal event: " + displayName;
}
else if (definition == EventDefinitionType.ESCALATION) {
message = "Schedule boundary escalation event: " + displayName;
}
else {
if (eventIsInterrupting) {
if (definition == EventDefinitionType.ERROR) {
message = "Schedule boundary error event: " + displayName;
}
else if (definition == EventDefinitionType.COMPENSATION) {
message = "Schedule boundary compensation event: " + displayName;
}
else if (definition == EventDefinitionType.CANCEL) {
message = "Schedule boundary cancel event: " + displayName;
}
}
else {
SimulationUtils.sendElementNotSupportedTraceNote(model, processModel, displayName,
nodeIdOfElementToSchedule);
SimulationUtils.abort(model, processInstance, nodeId, showInTrace);
String identifier = processModel.getIdentifiers().get(nodeIdOfElementToSchedule);
throw new ScyllaRuntimeException("BPMNEvent " + identifier + " not supported.");
}
}*
bo.getMessagesOfBoundaryEventsToSchedule().computeIfAbsent(timeUntilWhenNonTimerEventsAreCreated,
k -> new ArrayList<String>());
bo.getMessagesOfBoundaryEventsToSchedule().get(timeUntilWhenNonTimerEventsAreCreated)
.add(message);
}
*/
String source = bo.getSource();
ProcessInstance processInstance = bo.getProcessInstance();
TimeInstant timeInstant = new TimeInstant(startOfInterval, TimeUnit.SECONDS);
// And create the event with the time it should trigger.
BPMNIntermediateEvent event = new BPMNIntermediateEvent(model, source, timeInstant, desmojObjects, processInstance, nodeIdOfElementToSchedule);
bo.getBoundaryEventsToSchedule().computeIfAbsent(timeUntilWhenNonTimerEventsAreCreated, k -> new ArrayList<BPMNIntermediateEvent>());
bo.getBoundaryEventsToSchedule().get(timeUntilWhenNonTimerEventsAreCreated).add(event);
if (eventIsInterrupting) {
// If the element is interrupting, finish and clean up
// boundaryObjects.values().remove(bo);
bo.setGenerateMoreNonTimerBoundaryEvents(false);
break;
}
}
}
}
bo.setTimeUntilWhenNonTimerEventsAreCreated(timeUntilWhenNonTimerEventsAreCreated);
}
use of de.hpi.bpt.scylla.simulation.ProcessInstance in project scylla by bptlab.
the class BoundaryEventPluginUtils method initializeBoundaryObject.
// This sets all necessary values for the current boundary object and stores them in the global boundary objects map.
void initializeBoundaryObject(double beginTimeOfTask, ScyllaEvent desmojEvent, List<Integer> referenceToBoundaryEvents) {
ProcessInstance processInstance = desmojEvent.getProcessInstance();
int nodeId = desmojEvent.getNodeId();
ProcessSimulationComponents desmojObjects = desmojEvent.getDesmojObjects();
String eventName = desmojEvent.getName();
String source = desmojEvent.getSource();
BoundaryObject bo = new BoundaryObject(source, beginTimeOfTask, processInstance, nodeId, desmojObjects, referenceToBoundaryEvents);
boundaryObjects.put(eventName, bo);
}
Aggregations