Search in sources :

Example 6 with SimulationConfiguration

use of de.hpi.bpt.scylla.model.configuration.SimulationConfiguration in project scylla by bptlab.

the class ProcessSimulationComponents method init.

public void init() {
    try {
        Map<Integer, TimeDistributionWrapper> arrivalRates = simulationConfiguration.getArrivalRates();
        Map<Integer, TimeDistributionWrapper> durations = simulationConfiguration.getDurations();
        Map<Integer, TimeDistributionWrapper> setUpDurations = simulationConfiguration.getSetUpDurations();
        Map<Integer, TimeDistributionWrapper> arrivalRatesAndDurations = new HashMap<Integer, TimeDistributionWrapper>();
        arrivalRatesAndDurations.putAll(arrivalRates);
        arrivalRatesAndDurations.putAll(durations);
        setSmallestTimeUnit(arrivalRatesAndDurations);
        setSmallestTimeUnit(setUpDurations);
        convertToDesmojDistributions(arrivalRatesAndDurations, false);
        convertToDesmojDistributions(setUpDurations, true);
        extensionDistributions = DistributionConversionPluggable.runPlugins(this);
        Map<Integer, SimulationConfiguration> configurationsOfSubProcesses = simulationConfiguration.getConfigurationsOfSubProcesses();
        for (Integer nodeId : configurationsOfSubProcesses.keySet()) {
            SimulationConfiguration scOfSubProcess = configurationsOfSubProcesses.get(nodeId);
            ProcessModel pmOfSubProcess = processModel.getSubProcesses().get(nodeId);
            ProcessSimulationComponents desmojObjects = new ProcessSimulationComponents(model, this, pmOfSubProcess, scOfSubProcess);
            desmojObjects.init();
            children.put(nodeId, desmojObjects);
        }
    } catch (InstantiationException e) {
        DebugLogger.error(e.getMessage());
        DebugLogger.error("Instantiation of simulation model failed.");
    }
}
Also used : ProcessModel(de.hpi.bpt.scylla.model.process.ProcessModel) SimulationConfiguration(de.hpi.bpt.scylla.model.configuration.SimulationConfiguration) HashMap(java.util.HashMap) TimeDistributionWrapper(de.hpi.bpt.scylla.model.configuration.distribution.TimeDistributionWrapper)

Example 7 with SimulationConfiguration

use of de.hpi.bpt.scylla.model.configuration.SimulationConfiguration in project scylla by bptlab.

the class SimulationManager method run.

/**
 * parses input, runs DesmoJ simulation experiment, writes BPS output logs
 */
public String run() {
    try {
        SAXBuilder builder = new SAXBuilder();
        if (globalConfigurationFilename == null || globalConfigurationFilename.isEmpty()) {
            throw new ScyllaValidationException("No global configuration provided.");
        } else {
            // parse global configuration XML
            Document gcDoc = builder.build(globalConfigurationFilename);
            Element gcRootElement = gcDoc.getRootElement();
            GlobalConfigurationParser globalConfigurationParser = new GlobalConfigurationParser(this);
            globalConfiguration = globalConfigurationParser.parse(gcDoc.getRootElement());
            String fileNameWithoutExtension = // filename.lastIndexOf("\\") +
            globalConfigurationFilename.substring(// 1,
            globalConfigurationFilename.lastIndexOf(Scylla.FILEDELIM) + 1, globalConfigurationFilename.lastIndexOf(".xml"));
            globalConfiguration.setFileNameWithoutExtension(fileNameWithoutExtension);
            // plugins to parse global configuration
            GlobalConfigurationParserPluggable.runPlugins(this, globalConfiguration, gcRootElement);
            DateTimeUtils.setZoneId(globalConfiguration.getZoneId());
        }
        CommonProcessElementsParser cpeParser = new CommonProcessElementsParser(this);
        for (String filename : processModelFilenames) {
            Document pmDoc = builder.build(filename);
            Element pmRootElement = pmDoc.getRootElement();
            // parse common process elements from XML (BPMN)
            CommonProcessElements commonProcessElementsFromFile = cpeParser.parse(pmRootElement);
            String fileNameWithoutExtension = // filename.lastIndexOf("\\") + 1,
            filename.substring(filename.lastIndexOf(Scylla.FILEDELIM) + 1, filename.lastIndexOf(".bpmn"));
            commonProcessElementsFromFile.setBpmnFileNameWithoutExtension(fileNameWithoutExtension);
            // plugins to parse common process elements
            CommonProcessElementsParserPluggable.runPlugins(this, commonProcessElementsFromFile, pmRootElement);
            // parse process model(s) from XML (BPMN)
            ProcessModelParser pmParser = new ProcessModelParser(this);
            pmParser.setCommonProcessElements(commonProcessElementsFromFile);
            ProcessModel processModelFromFile = pmParser.parse(pmDoc.getRootElement());
            String processId = processModelFromFile.getId();
            if (processModels.containsKey(processId)) {
                throw new ScyllaValidationException("Duplicate process model with id " + processId + ".");
            }
            // plugins to parse process model(s)
            ProcessModelParserPluggable.runPlugins(this, processModelFromFile, pmRootElement);
            processModels.put(processId, processModelFromFile);
            commonProcessElements.put(processId, commonProcessElementsFromFile);
        }
        SimulationConfigurationParser simParser = new SimulationConfigurationParser(this);
        // parse each simulation configuration XML
        for (String filename : simulationConfigurationFilenames) {
            Document scDoc = builder.build(filename);
            SimulationConfiguration simulationConfigurationFromFile = simParser.parse(scDoc.getRootElement());
            String processId = simulationConfigurationFromFile.getProcessModel().getId();
            if (simulationConfigurations.containsKey(processId)) {
                throw new ScyllaValidationException("Multiple simulation configurations for process with id " + processId + ".");
            }
            // plugins to parse simulation configuration
            SimulationConfigurationParserPluggable.runPlugins(this, simulationConfigurationFromFile, scDoc);
            simulationConfigurations.put(processId, simulationConfigurationFromFile);
        }
    } catch (JDOMException | IOException | ScyllaValidationException e) {
        DebugLogger.error(e.getMessage());
        e.printStackTrace();
    }
    // TODO validate resources in process models (i.e. check if they are all covered in resource data)
    TimeUnit epsilon = TimeUnit.SECONDS;
    DateTimeUtils.setReferenceTimeUnit(epsilon);
    String experimentName = Long.toString((new Date()).getTime());
    Experiment.setEpsilon(epsilon);
    Experiment exp = new Experiment(experimentName, experimentOutputFolder);
    exp.setShowProgressBar(false);
    // XXX each simulation configuration may have its own seed
    Long randomSeed = globalConfiguration.getRandomSeed();
    if (randomSeed != null) {
        exp.setSeedGenerator(randomSeed);
    } else {
        exp.setSeedGenerator((new Random()).nextLong());
    }
    SimulationModel sm = new SimulationModel(null, globalConfiguration, commonProcessElements, processModels, simulationConfigurations, enableBpsLogging, enableDesLogging);
    sm.connectToExperiment(exp);
    int lambda = 1;
    if (sm.getEndDateTime() != null) {
        // have to use time which is slightly after intended end time (epsilon)
        // otherwise the AbortProcessSimulationEvent(s) may not fire
        long simulationDuration = DateTimeUtils.getDuration(sm.getStartDateTime(), sm.getEndDateTime());
        TimeInstant simulationTimeInstant = new TimeInstant(simulationDuration + lambda, epsilon);
        exp.stop(simulationTimeInstant);
        exp.tracePeriod(new TimeInstant(0), simulationTimeInstant);
        exp.debugPeriod(new TimeInstant(0), simulationTimeInstant);
    } else {
        exp.traceOn(new TimeInstant(0));
        exp.debugOn(new TimeInstant(0));
    }
    if (!enableDesLogging) {
        exp.debugOff(new TimeInstant(0));
        exp.traceOff(new TimeInstant(0));
    }
    exp.start();
    exp.report();
    exp.finish();
    try {
        // log process execution
        // log resources, process, tasks
        StringBuilder strb = new StringBuilder(globalConfigurationFilename.substring(0, globalConfigurationFilename.lastIndexOf(Scylla.FILEDELIM) + 1));
        outputPath = strb.substring(0, strb.lastIndexOf(Scylla.FILEDELIM) + 1) + "output_" + new SimpleDateFormat("yy_MM_dd_HH_mm_ss").format(new Date()) + Scylla.FILEDELIM;
        File outputPathFolder = new File(outputPath);
        if (!outputPathFolder.exists())
            outputPathFolder.mkdir();
        OutputLoggerPluggable.runPlugins(sm, outputPath);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return outputPath;
}
Also used : ProcessModel(de.hpi.bpt.scylla.model.process.ProcessModel) Element(org.jdom2.Element) Document(org.jdom2.Document) ScyllaValidationException(de.hpi.bpt.scylla.exception.ScyllaValidationException) Random(java.util.Random) TimeUnit(java.util.concurrent.TimeUnit) SimulationConfigurationParser(de.hpi.bpt.scylla.parser.SimulationConfigurationParser) CommonProcessElements(de.hpi.bpt.scylla.model.process.CommonProcessElements) SAXBuilder(org.jdom2.input.SAXBuilder) Experiment(desmoj.core.simulator.Experiment) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) CommonProcessElementsParser(de.hpi.bpt.scylla.parser.CommonProcessElementsParser) Date(java.util.Date) SimulationConfiguration(de.hpi.bpt.scylla.model.configuration.SimulationConfiguration) ProcessModelParser(de.hpi.bpt.scylla.parser.ProcessModelParser) GlobalConfigurationParser(de.hpi.bpt.scylla.parser.GlobalConfigurationParser) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) SimulationModel(de.hpi.bpt.scylla.simulation.SimulationModel) TimeInstant(desmoj.core.simulator.TimeInstant)

Example 8 with SimulationConfiguration

use of de.hpi.bpt.scylla.model.configuration.SimulationConfiguration in project scylla by bptlab.

the class SimulationConfigurationParser method parseSimulationConfiguration.

private SimulationConfiguration parseSimulationConfiguration(Element sim, Namespace simNamespace, String processIdFromSimElement, ProcessModel processModel, Long randomSeed) throws ScyllaValidationException {
    Map<String, Resource> resources = simulationEnvironment.getGlobalConfiguration().getResources();
    if (processModel == null) {
        throw new ScyllaValidationException("Simulation configuration is for (sub)process '" + processIdFromSimElement + "', which is not found in the simulation environment.");
    }
    String processRef = processIdFromSimElement;
    String simId = null;
    Integer numberOfProcessInstances = null;
    ZonedDateTime startDateTime = null;
    ZonedDateTime endDateTime = null;
    if (processModel.getParent() == null) {
        List<Element> startEvents = sim.getChildren("startEvent", simNamespace);
        if (startEvents.size() == 0)
            throw new ScyllaValidationException("No definition of start event in simulation scenario.");
        else {
            for (Element el : startEvents) {
                // it is sufficient if at least one of the start events has an arrival rate defined
                if (el.getChild("arrivalRate", simNamespace) != null) {
                    break;
                }
                throw new ScyllaValidationException("No arrival rate defined in any of the start events in simulation scenario.");
            }
        }
        // store identifier of simulation configuration only if it is for top level process
        simId = sim.getAttributeValue("id");
        numberOfProcessInstances = Integer.valueOf(sim.getAttributeValue("processInstances"));
        startDateTime = DateTimeUtils.parse(sim.getAttributeValue("startDateTime"));
        String endDateTimeString = sim.getAttributeValue("endDateTime");
        if (endDateTimeString != null) {
            endDateTime = DateTimeUtils.parse(endDateTimeString);
        }
        String randomSeedString = sim.getAttributeValue("randomSeed");
        if (randomSeedString != null) {
            randomSeed = Long.valueOf(sim.getAttributeValue("randomSeed"));
            DebugLogger.log("Random seed for simulation configuration " + processRef + ": " + randomSeed);
        }
    }
    Map<Integer, TimeDistributionWrapper> arrivalRates = new HashMap<Integer, TimeDistributionWrapper>();
    Map<Integer, TimeDistributionWrapper> durations = new HashMap<Integer, TimeDistributionWrapper>();
    Map<Integer, TimeDistributionWrapper> setUpDurations = new HashMap<Integer, TimeDistributionWrapper>();
    Map<Integer, Set<ResourceReference>> resourceReferences = new HashMap<Integer, Set<ResourceReference>>();
    // gateways and events
    // Map<Integer, BranchingBehavior> branchingBehaviors = new HashMap<Integer, BranchingBehavior>();
    Map<Integer, SimulationConfiguration> configurationsOfSubProcesses = new HashMap<Integer, SimulationConfiguration>();
    // take resource definitions from process model
    Map<Integer, Set<String>> resourceReferencesFromProcessModel = processModel.getResourceReferences();
    for (Integer nodeId : resourceReferencesFromProcessModel.keySet()) {
        Set<String> resourceRefFromModel = resourceReferencesFromProcessModel.get(nodeId);
        Set<ResourceReference> resourceRefs = new HashSet<ResourceReference>();
        for (String resourceId : resourceRefFromModel) {
            int amount = 1;
            Map<String, String> assignmentDefinition = new HashMap<String, String>();
            ResourceReference resourceReference = new ResourceReference(resourceId, amount, assignmentDefinition);
            resourceRefs.add(resourceReference);
        }
        resourceReferences.put(nodeId, resourceRefs);
    }
    for (Element el : sim.getChildren()) {
        String elementName = el.getName();
        if (isKnownElement(elementName)) {
            if (elementName.equals("resources")) {
                // in parent process
                continue;
            }
            String identifier = el.getAttributeValue("id");
            if (identifier == null) {
                DebugLogger.log("Warning: Simulation configuration definition element '" + elementName + "' does not have an identifier, skip.");
                // no matching element in process, so skip definition
                continue;
            }
            Integer nodeId = processModel.getIdentifiersToNodeIds().get(identifier);
            if (nodeId == null) {
                DebugLogger.log("Simulation configuration definition for process element '" + identifier + "', but not available in process, skip.");
                // no matching element in process, so skip definition
                continue;
            }
            if (elementName.equals("startEvent")) {
                Element elem = el.getChild("arrivalRate", simNamespace);
                if (elem != null) {
                    TimeDistributionWrapper distribution = getTimeDistributionWrapper(elem, simNamespace);
                    arrivalRates.put(nodeId, distribution);
                }
            } else if (elementName.equals("task") || elementName.endsWith("Task") || elementName.equals("subProcess")) {
                Element durationElem = el.getChild("duration", simNamespace);
                if (durationElem != null) {
                    TimeDistributionWrapper distribution = getTimeDistributionWrapper(durationElem, simNamespace);
                    durations.put(nodeId, distribution);
                }
                Element setUpDurationElem = el.getChild("setUpDuration", simNamespace);
                if (setUpDurationElem != null) {
                    TimeDistributionWrapper distribution = getTimeDistributionWrapper(setUpDurationElem, simNamespace);
                    setUpDurations.put(nodeId, distribution);
                }
                Element resourcesElem = el.getChild("resources", simNamespace);
                if (resourcesElem != null) {
                    List<Element> resourceElements = resourcesElem.getChildren("resource", simNamespace);
                    // process model)
                    if (resourceElements.size() > 0) {
                        Set<ResourceReference> resourceRefs = new HashSet<ResourceReference>();
                        Set<String> resourceIdentifiers = new HashSet<String>();
                        for (Element elem : resourceElements) {
                            String resourceId = elem.getAttributeValue("id");
                            if (resources.get(resourceId) == null) {
                                throw new ScyllaValidationException("Simulation configuration " + simId + " refers to unknown resource " + resourceId + ".");
                            }
                            if (resourceIdentifiers.contains(resourceId)) {
                                throw new ScyllaValidationException("Simulation configuration " + simId + " defines multiple resources for task / subprocess " + identifier);
                            }
                            resourceIdentifiers.add(resourceId);
                            // XXX implementation currently supports more than one instance per resource. however
                            // BPMN standard does not support that.
                            // int amount = 1;
                            int amount = Integer.parseInt(elem.getAttributeValue("amount"));
                            Map<String, String> assignmentDefinition = new HashMap<String, String>();
                            Element assignmentDefinitionElement = elem.getChild("assignmentDefinition", simNamespace);
                            if (assignmentDefinitionElement != null) {
                                List<Element> adElements = assignmentDefinitionElement.getChildren(null, simNamespace);
                                for (Element adElem : adElements) {
                                    assignmentDefinition.put(adElem.getName(), adElem.getText());
                                }
                            }
                            ResourceReference resourceReference = new ResourceReference(resourceId, amount, assignmentDefinition);
                            resourceRefs.add(resourceReference);
                        }
                        resourceReferences.put(nodeId, resourceRefs);
                    }
                }
                if (elementName.equals("subProcess")) {
                    ProcessModel subProcess = processModel.getSubProcesses().get(nodeId);
                    String subProcessIdFromSimElement = el.getAttributeValue("id");
                    SimulationConfiguration simulationConfiguration = parseSimulationConfiguration(el, simNamespace, subProcessIdFromSimElement, subProcess, randomSeed);
                    configurationsOfSubProcesses.put(nodeId, simulationConfiguration);
                }
            } else {
                DebugLogger.log("Element " + el.getName() + " of simulation scenario is expected to be known, but not supported.");
            }
        } else {
            DebugLogger.log("Element " + el.getName() + " of simulation scenario not supported.");
        }
    }
    SimulationConfiguration simulationConfiguration = new SimulationConfiguration(simId, processModel, numberOfProcessInstances, startDateTime, endDateTime, randomSeed, arrivalRates, durations, setUpDurations, resourceReferences, configurationsOfSubProcesses);
    return simulationConfiguration;
}
Also used : ProcessModel(de.hpi.bpt.scylla.model.process.ProcessModel) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Element(org.jdom2.Element) ScyllaValidationException(de.hpi.bpt.scylla.exception.ScyllaValidationException) ZonedDateTime(java.time.ZonedDateTime) List(java.util.List) ResourceReference(de.hpi.bpt.scylla.model.configuration.ResourceReference) TimeDistributionWrapper(de.hpi.bpt.scylla.model.configuration.distribution.TimeDistributionWrapper) HashSet(java.util.HashSet) Resource(de.hpi.bpt.scylla.model.global.resource.Resource) SimulationConfiguration(de.hpi.bpt.scylla.model.configuration.SimulationConfiguration) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with SimulationConfiguration

use of de.hpi.bpt.scylla.model.configuration.SimulationConfiguration in project scylla by bptlab.

the class SimulationConfigurationParser method parse.

@Override
public SimulationConfiguration parse(Element rootElement) throws ScyllaValidationException {
    Namespace simNamespace = rootElement.getNamespace();
    List<Element> simElements = rootElement.getChildren("simulationConfiguration", simNamespace);
    if (simElements.isEmpty()) {
        throw new ScyllaValidationException("No simulation configuration in file.");
    } else if (simElements.size() > 1) {
        throw new ScyllaValidationException("Multiple simulation configurations in file. If you want to simulate mulitple scenarios, then store them in separate simulation configuration files.");
    }
    Element sim = simElements.get(0);
    String processRef = sim.getAttributeValue("processRef");
    ProcessModel processModel = simulationEnvironment.getProcessModels().get(processRef);
    Long randomSeed = simulationEnvironment.getGlobalConfiguration().getRandomSeed();
    SimulationConfiguration simulationConfiguration = parseSimulationConfiguration(sim, simNamespace, processRef, processModel, randomSeed);
    // }
    return simulationConfiguration;
}
Also used : ScyllaValidationException(de.hpi.bpt.scylla.exception.ScyllaValidationException) ProcessModel(de.hpi.bpt.scylla.model.process.ProcessModel) SimulationConfiguration(de.hpi.bpt.scylla.model.configuration.SimulationConfiguration) Element(org.jdom2.Element) Namespace(org.jdom2.Namespace)

Example 10 with SimulationConfiguration

use of de.hpi.bpt.scylla.model.configuration.SimulationConfiguration 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;
}
Also used : ProcessModel(de.hpi.bpt.scylla.model.process.ProcessModel) DiscreteDistEmpirical(desmoj.core.dist.DiscreteDistEmpirical) HashMap(java.util.HashMap) BranchingBehavior(de.hpi.bpt.scylla.model.configuration.BranchingBehavior) SimulationConfiguration(de.hpi.bpt.scylla.model.configuration.SimulationConfiguration) Map(java.util.Map) HashMap(java.util.HashMap) SimulationModel(de.hpi.bpt.scylla.simulation.SimulationModel)

Aggregations

SimulationConfiguration (de.hpi.bpt.scylla.model.configuration.SimulationConfiguration)10 ProcessModel (de.hpi.bpt.scylla.model.process.ProcessModel)10 SimulationModel (de.hpi.bpt.scylla.simulation.SimulationModel)6 HashMap (java.util.HashMap)6 Map (java.util.Map)5 Element (org.jdom2.Element)4 ScyllaValidationException (de.hpi.bpt.scylla.exception.ScyllaValidationException)3 BranchingBehavior (de.hpi.bpt.scylla.model.configuration.BranchingBehavior)3 TimeDistributionWrapper (de.hpi.bpt.scylla.model.configuration.distribution.TimeDistributionWrapper)3 Distribution (de.hpi.bpt.scylla.model.configuration.distribution.Distribution)2 DiscreteDistEmpirical (desmoj.core.dist.DiscreteDistEmpirical)2 TimeUnit (java.util.concurrent.TimeUnit)2 ResourceReference (de.hpi.bpt.scylla.model.configuration.ResourceReference)1 Resource (de.hpi.bpt.scylla.model.global.resource.Resource)1 CommonProcessElements (de.hpi.bpt.scylla.model.process.CommonProcessElements)1 CommonProcessElementsParser (de.hpi.bpt.scylla.parser.CommonProcessElementsParser)1 GlobalConfigurationParser (de.hpi.bpt.scylla.parser.GlobalConfigurationParser)1 ProcessModelParser (de.hpi.bpt.scylla.parser.ProcessModelParser)1 SimulationConfigurationParser (de.hpi.bpt.scylla.parser.SimulationConfigurationParser)1 BoolDistBernoulli (desmoj.core.dist.BoolDistBernoulli)1