use of desmoj.core.simulator.TimeSpan in project scylla by bptlab.
the class ExclusiveGatewayEventPlugin method scheduleNextEvent.
private void scheduleNextEvent(GatewayEvent desmojEvent, ProcessInstance processInstance, ProcessModel processModel, Integer nextFlowId) {
Set<Integer> nodeIds = null;
try {
nodeIds = processModel.getTargetObjectIds(nextFlowId);
} catch (NodeNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (nodeIds.size() != 1) {
try {
throw new ScyllaValidationException("Flow " + nextFlowId + " does not connect to 1 node, but" + nodeIds.size() + " .");
} catch (ScyllaValidationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int nextNodeId = nodeIds.iterator().next();
Map<Integer, ScyllaEvent> nextEventMap = desmojEvent.getNextEventMap();
List<Integer> indicesOfEventsToKeep = new ArrayList<Integer>();
for (int index : nextEventMap.keySet()) {
ScyllaEvent eventCandidate = nextEventMap.get(index);
int nodeIdOfCandidate = eventCandidate.getNodeId();
if (nodeIdOfCandidate == nextNodeId) {
indicesOfEventsToKeep.add(index);
break;
}
}
Map<Integer, TimeSpan> timeSpanToNextEventMap = desmojEvent.getTimeSpanToNextEventMap();
nextEventMap.keySet().retainAll(indicesOfEventsToKeep);
timeSpanToNextEventMap.keySet().retainAll(indicesOfEventsToKeep);
}
use of desmoj.core.simulator.TimeSpan in project scylla by bptlab.
the class ProcessInstance method cancel.
// public double getTimeRelativeToSubProcessStart() {
// return timeRelativeToSubProcessStart;
// }
//
// public void setTimeRelativeToSubProcessStart(double timeRelativeToSubProcessStart) {
// this.timeRelativeToSubProcessStart = timeRelativeToSubProcessStart;
// }
//
// public Double getEndTimeRelativeToSubProcessStart() {
// return endTimeRelativeToSubProcessStart;
// }
//
// public void setEndTimeRelativeToSubProcessStart(Double endTimeRelativeToSubProcessStart) {
// this.endTimeRelativeToSubProcessStart = endTimeRelativeToSubProcessStart;
// }
@Override
public void cancel() {
try {
Iterator<String> iterator = children.keySet().iterator();
while (iterator.hasNext()) {
String name = iterator.next();
ProcessInstance child = children.get(name);
child.cancel();
}
if (isScheduled()) {
List<EventAbstract> scheduledEvents = getScheduledEvents();
List<ScyllaEvent> rescheduledEvents = new ArrayList<ScyllaEvent>();
for (EventAbstract e : scheduledEvents) {
if (e instanceof TaskEvent) {
TaskEvent event = (TaskEvent) e;
TaskCancelEvent cancelEvent = new TaskCancelEvent(event.getModel(), event.getSource(), event.getSimulationTimeOfSource(), event.getDesmojObjects(), this, event.getNodeId());
rescheduledEvents.add(cancelEvent);
}
}
super.cancel();
for (ScyllaEvent event : rescheduledEvents) {
TimeSpan timeSpan = new TimeSpan(0);
SimulationUtils.scheduleEvent(event, timeSpan);
}
}
if (parent != null) {
parent.removeChild(this);
}
} catch (ScyllaRuntimeException e) {
throw new RuntimeException(e);
}
}
use of desmoj.core.simulator.TimeSpan in project scylla by bptlab.
the class SimulationModel method doInitialSchedules.
@Override
public void doInitialSchedules() {
TimeUnit timeUnit = DateTimeUtils.getReferenceTimeUnit();
for (String processId : pSimMap.keySet()) {
ProcessSimulationComponents desmojObj = pSimMap.get(processId);
// schedule for end of simulation (if defined), aligned to global start time
ZonedDateTime endDateOfSimulationConf = desmojObj.getSimulationConfiguration().getEndDateTime();
Long endTimeRelativeToGlobalStart = Long.MAX_VALUE;
if (endDateOfSimulationConf != null) {
endTimeRelativeToGlobalStart = DateTimeUtils.getDuration(startDateTime, endDateOfSimulationConf);
ProcessSimulationStopEvent endEvent = new ProcessSimulationStopEvent(this, processId, traceIsOn());
// java.util.Date getTime() returns milliseconds
endEvent.schedule(new TimeSpan(endTimeRelativeToGlobalStart, timeUnit));
}
// schedule for start of simulation, aligned to global start time
ZonedDateTime startDateOfSimulationConf = desmojObj.getSimulationConfiguration().getStartDateTime();
long startTimeRelativeToGlobalStart = DateTimeUtils.getDuration(startDateTime, startDateOfSimulationConf);
ProcessModel processModel = desmojObj.getProcessModel();
int processInstanceId = desmojObj.incrementProcessInstancesStarted();
ProcessInstance firstProcessInstance = new ProcessInstance(this, processModel, processInstanceId, traceIsOn());
ProcessInstanceGenerationEvent startEvent = new ProcessInstanceGenerationEvent(this, processId, endTimeRelativeToGlobalStart, traceIsOn());
startEvent.schedule(firstProcessInstance, new TimeSpan(startTimeRelativeToGlobalStart, timeUnit));
}
if (endDateTime != null) {
// / java.util.Date getTime() returns milliseconds
long simulationDuration = DateTimeUtils.getDuration(startDateTime, endDateTime);
TimeInstant timeInstant = new TimeInstant(simulationDuration, DateTimeUtils.getReferenceTimeUnit());
for (String processId : pSimMap.keySet()) {
ProcessSimulationStopEvent abortEvent = new ProcessSimulationStopEvent(this, processId, traceIsOn());
abortEvent.schedule(timeInstant);
}
}
}
use of desmoj.core.simulator.TimeSpan in project scylla by bptlab.
the class GatewayEvent method eventRoutine.
@Override
public void eventRoutine(ProcessInstance processInstance) throws SuspendExecution {
SimulationModel model = (SimulationModel) getModel();
ProcessModel processModel = processInstance.getProcessModel();
try {
Set<Integer> idsOfPreviousNodes = processModel.getIdsOfPreviousNodes(nodeId);
Set<Integer> idsOfNextNodes = processModel.getIdsOfNextNodes(nodeId);
String message = null;
GatewayType type = processModel.getGateways().get(nodeId);
String convergeDivergeName = "Join and Split";
if (idsOfPreviousNodes.size() == 1) {
convergeDivergeName = "Split";
} else if (idsOfNextNodes.size() == 1) {
convergeDivergeName = "Join";
}
if (type == GatewayType.DEFAULT) {
message = "Default " + convergeDivergeName + " Gateway: " + displayName;
}
if (type == GatewayType.EXCLUSIVE) {
message = "Exclusive " + convergeDivergeName + " Gateway: " + displayName;
} else if (type == GatewayType.EVENT_BASED) {
message = "Eventbased " + convergeDivergeName + " Gateway: " + displayName;
} else if (type == GatewayType.INCLUSIVE) {
message = "Inclusive " + convergeDivergeName + " Gateway: " + displayName;
} else if (type == GatewayType.PARALLEL) {
message = "Parallel " + convergeDivergeName + " Gateway: " + displayName;
} else if (type == GatewayType.COMPLEX) {
message = "Complex " + convergeDivergeName + " Gateway: " + displayName;
} else {
// TODO write to log because element not supported
SimulationUtils.abort(model, processInstance, nodeId, traceIsOn());
return;
}
sendTraceNote(message);
// i.e. for splits: default behavior is the one of a parallel gateway
for (Integer nextNodeId : idsOfNextNodes) {
List<ScyllaEvent> events = SimulationUtils.createEventsForNextNode(this, pSimComponents, processInstance, nextNodeId);
// next DesmoJ event occurs immediately after start event
TimeSpan timeSpan = new TimeSpan(0);
for (ScyllaEvent event : events) {
int index = getNewEventIndex();
nextEventMap.put(index, event);
timeSpanToNextEventMap.put(index, timeSpan);
}
}
GatewayEventPluggable.runPlugins(this, processInstance);
scheduleNextEvents();
} catch (NodeNotFoundException | ScyllaValidationException | ScyllaRuntimeException e) {
System.err.println(e.getMessage());
e.printStackTrace();
SimulationUtils.abort(model, processInstance, nodeId, traceIsOn());
return;
}
}
use of desmoj.core.simulator.TimeSpan in project scylla by bptlab.
the class ResourceAvailabilityEvent method eventRoutine.
@Override
public void eventRoutine() throws SuspendExecution {
SimulationModel model = (SimulationModel) getModel();
TimeInstant currentSimulationTime = model.presentTime();
Set<String> resourceQueuesUpdated = new HashSet<String>();
String resourceType = resourceObject.getResourceType();
resourceQueuesUpdated.add(resourceType);
try {
ScyllaEvent eventFromQueue = QueueManager.getEventFromQueueReadyForSchedule(model, resourceQueuesUpdated);
while (eventFromQueue != null) {
SimulationUtils.scheduleEvent(eventFromQueue, new TimeSpan(0));
eventFromQueue = QueueManager.getEventFromQueueReadyForSchedule(model, resourceQueuesUpdated);
}
ResourceAvailabilityEventPluggable.runPlugins(this);
// schedule next ResourceAvailableEvent
ZonedDateTime currentDateTime = DateTimeUtils.getDateTime(currentSimulationTime);
boolean currentlyInTimetableItem = true;
SimulationUtils.scheduleNextResourceAvailableEvent(model, resourceObject, currentDateTime, currentlyInTimetableItem);
} catch (ScyllaRuntimeException e) {
throw new RuntimeException(e);
}
}
Aggregations