use of desmoj.core.dist.DiscreteDistEmpirical 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 desmoj.core.dist.DiscreteDistEmpirical in project scylla by bptlab.
the class ExclusiveGatewayEventPlugin method eventRoutine.
@SuppressWarnings("unchecked")
@Override
public void eventRoutine(GatewayEvent desmojEvent, ProcessInstance processInstance) throws ScyllaRuntimeException {
SimulationModel model = (SimulationModel) desmojEvent.getModel();
ProcessModel processModel = processInstance.getProcessModel();
int nodeId = desmojEvent.getNodeId();
boolean showInTrace = desmojEvent.traceIsOn();
GatewayType type = processModel.getGateways().get(nodeId);
ProcessSimulationComponents desmojObjects = desmojEvent.getDesmojObjects();
try {
Set<Integer> idsOfNextNodes = processModel.getIdsOfNextNodes(nodeId);
if (idsOfNextNodes.size() > 1) {
// split
if (type == GatewayType.DEFAULT || type == GatewayType.EXCLUSIVE) {
Map<Integer, Object> branchingDistributions = desmojObjects.getExtensionDistributions().get(getName());
DiscreteDistEmpirical<Integer> distribution = (DiscreteDistEmpirical<Integer>) branchingDistributions.get(nodeId);
// decide on next node
if (distribution != null) {
// if a distribution is given take this
Integer nextFlowId = distribution.sample().intValue();
if (!processModel.getIdentifiers().keySet().contains(nextFlowId)) {
throw new ScyllaValidationException("Flow with id " + nextFlowId + " does not exist.");
}
scheduleNextEvent(desmojEvent, processInstance, processModel, nextFlowId);
} else {
// otherwise try to get information out of the describing branches and branch on the basis of this
Map<Class<?>, ArrayList<PluginWrapper>> a = PluginLoader.getDefaultPluginLoader().getExtensions();
Collection<ArrayList<PluginWrapper>> plugins = a.values();
Boolean dataObjectPluginOn = false;
for (ArrayList<PluginWrapper> plugin : plugins) {
for (PluginWrapper p : plugin) {
if (p.toString().equals("DataObjectSCParserPlugin")) {
dataObjectPluginOn = true;
}
}
}
if (dataObjectPluginOn) {
Object[] outgoingRefs = processModel.getGraph().getTargetObjects(nodeId).toArray();
Integer DefaultPath = null;
Boolean foundAWay = false;
for (Object or : outgoingRefs) {
// go through all outgoing references
if (or.equals(getKeyByValue(processModel.getIdentifiers(), processModel.getNodeAttributes().get(desmojEvent.getNodeId()).get("default")))) {
// if it's the default path jump it
DefaultPath = (Integer) or;
continue;
}
String[] conditions = processModel.getDisplayNames().get(or).split("&&");
Integer nextFlowId = (Integer) or;
List<Boolean> test = new ArrayList<>();
for (String condition : conditions) {
condition = condition.trim();
String field = null;
String value = null;
String comparison = null;
if (condition.contains("==")) {
field = condition.split("==")[0];
value = condition.split("==")[1];
// value = processModel.getDisplayNames().get(or).substring(2, processModel.getDisplayNames().get(or).length());
comparison = "equal";
} else if (condition.contains(">=")) {
field = condition.split(">=")[0];
value = condition.split(">=")[1];
comparison = "greaterOrEqual";
} else if (condition.contains("<=")) {
field = condition.split("<=")[0];
value = condition.split("<=")[1];
comparison = "lessOrEqual";
} else if (condition.contains("!=")) {
field = condition.split("!=")[0];
value = condition.split("!=")[1];
comparison = "notEqual";
} else if (condition.contains("=")) {
field = condition.split("=")[0];
value = condition.split("=")[1];
comparison = "equal";
} else if (condition.contains("<")) {
field = condition.split("<")[0];
value = condition.split("<")[1];
comparison = "less";
} else if (condition.contains(">")) {
field = condition.split(">")[0];
value = condition.split(">")[1];
comparison = "greater";
} else {
throw new ScyllaValidationException("Condition " + condition + " does not have a comparison-operator");
}
value = value.trim();
field = field.trim();
Object fieldValue = DataObjectField.getDataObjectValue(processInstance.getId(), field);
if (!isParsableAsLong(value) || !isParsableAsLong((String.valueOf(fieldValue)))) {
// try a long comparison
Integer comparisonResult = (String.valueOf(fieldValue)).trim().compareTo(String.valueOf(value));
if (comparison.equals("equal") && comparisonResult == 0) {
break;
} else if (comparison.equals("notEqual") && comparisonResult != 0) {
break;
} else {
test.add(false);
}
} else {
// otherwise do a string compare
Long LongValue = Long.valueOf(value);
Long dOValue = Long.valueOf((String.valueOf(fieldValue)));
Integer comparisonResult = (dOValue.compareTo(LongValue));
if (comparison.equals("equal") && comparisonResult == 0) {
} else if (comparison.equals("less") && comparisonResult < 0) {
} else if (comparison.equals("greater") && comparisonResult > 0) {
} else if (comparison.equals("greaterOrEqual") && comparisonResult >= 0) {
} else if (comparison.equals("lessOrEqual") && comparisonResult <= 0) {
} else {
test.add(false);
}
}
}
if (test.size() == 0) {
scheduleNextEvent(desmojEvent, processInstance, processModel, nextFlowId);
foundAWay = true;
}
}
if (!foundAWay && DefaultPath != null) {
scheduleNextEvent(desmojEvent, processInstance, processModel, DefaultPath);
} else if (!foundAWay && DefaultPath == null) {
// everything will be killed, logical error
throw new ScyllaValidationException("No Default Path for " + desmojEvent.getDisplayName() + " given and outgoing branches not complete. No branch matches, abort.");
}
} else {
Object[] outgoingRefs = processModel.getGraph().getTargetObjects(nodeId).toArray();
Integer DefaultPath = null;
for (Object or : outgoingRefs) {
// try to find default path
if (or.equals(getKeyByValue(processModel.getIdentifiers(), processModel.getNodeAttributes().get(desmojEvent.getNodeId()).get("default")))) {
DefaultPath = (Integer) or;
break;
}
}
if (DefaultPath != null) {
scheduleNextEvent(desmojEvent, processInstance, processModel, DefaultPath);
} else {
throw new ScyllaValidationException("No Distribution for " + desmojEvent.getDisplayName() + " given, no DefaultPath given and DataObject PlugIn not activated.");
}
}
}
}
}
} catch (NodeNotFoundException | ScyllaValidationException e) {
System.err.println(e.getMessage());
e.printStackTrace();
SimulationUtils.abort(model, processInstance, nodeId, showInTrace);
return;
}
}
use of desmoj.core.dist.DiscreteDistEmpirical in project scylla by bptlab.
the class ExclusiveGatewayDistributionConversionPlugin method convertToDesmoJDistributions.
@SuppressWarnings("unchecked")
@Override
public Map<Integer, Object> convertToDesmoJDistributions(ProcessSimulationComponents pSimComponents) {
Map<Integer, Object> branchingDistributionsExclusive = new HashMap<Integer, Object>();
SimulationConfiguration simulationConfiguration = pSimComponents.getSimulationConfiguration();
Map<Integer, BranchingBehavior> branchingBehaviors = (Map<Integer, BranchingBehavior>) simulationConfiguration.getExtensionValue(getName(), "branchingBehaviors");
Long randomSeed = simulationConfiguration.getRandomSeed();
ProcessModel processModel = pSimComponents.getProcessModel();
SimulationModel model = pSimComponents.getModel();
boolean showInReport = model.reportIsOn();
boolean showInTrace = model.traceIsOn();
for (Integer nodeId : branchingBehaviors.keySet()) {
BranchingBehavior branchingBehavior = branchingBehaviors.get(nodeId);
Map<Integer, Double> branchingProbabilities = branchingBehavior.getBranchingProbabilities();
String name = processModel.getModelScopeId() + "_" + nodeId.toString();
DiscreteDistEmpirical<Integer> desmojDist = new DiscreteDistEmpirical<Integer>(model, name, showInReport, showInTrace);
for (Integer nextNodeId : branchingProbabilities.keySet()) {
Double probability = branchingProbabilities.get(nextNodeId);
desmojDist.addEntry(nextNodeId, probability);
}
desmojDist.setSeed(randomSeed);
branchingDistributionsExclusive.put(nodeId, desmojDist);
}
return branchingDistributionsExclusive;
}
use of desmoj.core.dist.DiscreteDistEmpirical in project scylla by bptlab.
the class SimulationUtils method getDistribution.
public static NumericalDist<?> getDistribution(Distribution dist, SimulationModel model, String name, Integer nodeId, boolean showInReport, boolean showInTrace) throws InstantiationException {
if (dist instanceof BinomialDistribution) {
BinomialDistribution binDist = (BinomialDistribution) dist;
double probability = binDist.getProbability();
int amount = binDist.getAmount();
return new DiscreteDistBinomial(model, name, probability, amount, showInReport, showInTrace);
} else if (dist instanceof ConstantDistribution) {
ConstantDistribution conDist = (ConstantDistribution) dist;
double constantValue = conDist.getConstantValue();
return new DiscreteDistConstant<Number>(model, name, constantValue, showInReport, showInTrace);
} else if (dist instanceof EmpiricalDistribution) {
EmpiricalDistribution empDist = (EmpiricalDistribution) dist;
Map<Double, Double> entries = empDist.getEntries();
DiscreteDistEmpirical<Double> cde = new DiscreteDistEmpirical<Double>(model, name, showInReport, showInTrace);
for (Double value : entries.keySet()) {
Double frequency = entries.get(value);
cde.addEntry(value, frequency);
}
return cde;
} else if (dist instanceof EmpiricalStringDistribution) {
EmpiricalStringDistribution empDist = (EmpiricalStringDistribution) dist;
Map<Double, Double> entries = empDist.getEntries();
DiscreteDistEmpirical<Double> cde = new DiscreteDistEmpirical<Double>(model, name, showInReport, showInTrace);
for (Double value : entries.keySet()) {
Double frequency = entries.get(value);
cde.addEntry(value, frequency);
}
return cde;
} else if (dist instanceof ErlangDistribution) {
ErlangDistribution erlDist = (ErlangDistribution) dist;
double mean = erlDist.getMean();
long order = erlDist.getOrder();
return new ContDistErlang(model, name, order, mean, showInReport, showInTrace);
} else if (dist instanceof ExponentialDistribution) {
ExponentialDistribution expDist = (ExponentialDistribution) dist;
double mean = expDist.getMean();
return new ContDistExponential(model, name, mean, showInReport, showInTrace);
} else if (dist instanceof TriangularDistribution) {
TriangularDistribution triDist = (TriangularDistribution) dist;
double lower = triDist.getLower();
double upper = triDist.getUpper();
double peak = triDist.getPeak();
return new ContDistTriangular(model, name, lower, upper, peak, showInReport, showInTrace);
} else if (dist instanceof NormalDistribution) {
NormalDistribution norDist = (NormalDistribution) dist;
double mean = norDist.getMean();
double standardDeviation = norDist.getStandardDeviation();
return new ContDistNormal(model, name, mean, standardDeviation, showInReport, showInTrace);
} else if (dist instanceof PoissonDistribution) {
PoissonDistribution poiDist = (PoissonDistribution) dist;
double mean = poiDist.getMean();
return new DiscreteDistPoisson(model, name, mean, showInReport, showInTrace);
} else if (dist instanceof UniformDistribution) {
UniformDistribution uniDist = (UniformDistribution) dist;
double lower = uniDist.getLower();
double upper = uniDist.getUpper();
return new ContDistUniform(model, name, lower, upper, showInReport, showInTrace);
} else {
throw new InstantiationException("Distribution of node " + nodeId + " not supported.");
}
}
use of desmoj.core.dist.DiscreteDistEmpirical in project scylla by bptlab.
the class BoundaryDistributionConversionPlugin method convertToDesmoJDistributions.
// Took the branching behaviour also used in exclusive gateways. I did not touch it for now, have a look on it later. Should just be needed for event probability...
// TODO: Resolve redundancy.
@SuppressWarnings("unchecked")
@Override
public Map<Integer, Object> convertToDesmoJDistributions(ProcessSimulationComponents pSimComponents) {
Map<Integer, Object> boundaryEventDistributions = new HashMap<Integer, Object>();
SimulationConfiguration simulationConfiguration = pSimComponents.getSimulationConfiguration();
Long randomSeed = simulationConfiguration.getRandomSeed();
Map<Integer, BranchingBehavior> branchingBehaviors = (Map<Integer, BranchingBehavior>) simulationConfiguration.getExtensionValue(getName(), "branchingBehaviors");
ProcessModel processModel = pSimComponents.getProcessModel();
SimulationModel model = pSimComponents.getModel();
boolean showInReport = model.reportIsOn();
boolean showInTrace = model.traceIsOn();
for (Integer nodeId : branchingBehaviors.keySet()) {
BranchingBehavior branchingBehavior = branchingBehaviors.get(nodeId);
Map<Integer, Double> branchingProbabilities = branchingBehavior.getBranchingProbabilities();
String name = processModel.getModelScopeId() + "_" + nodeId.toString();
DiscreteDistEmpirical<Integer> desmojDist = new DiscreteDistEmpirical<Integer>(model, name, showInReport, showInTrace);
for (Integer nextNodeId : branchingProbabilities.keySet()) {
Double probability = branchingProbabilities.get(nextNodeId);
desmojDist.addEntry(nextNodeId, probability);
}
desmojDist.setSeed(randomSeed);
boundaryEventDistributions.put(nodeId, desmojDist);
}
return boundaryEventDistributions;
}
Aggregations