<commit_before>package org.jbpm.simulation.impl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bpsim.*;
import org.eclipse.bpmn2.BoundaryEvent;
import org.eclipse.bpmn2.BusinessRuleTask;
import org.eclipse.bpmn2.CancelEventDefinition;
import org.eclipse.bpmn2.CompensateEventDefinition;
import org.eclipse.bpmn2.ConditionalEventDefinition;
import org.eclipse.bpmn2.Definitions;
import org.eclipse.bpmn2.EndEvent;
import org.eclipse.bpmn2.ErrorEventDefinition;
import org.eclipse.bpmn2.EventDefinition;
import org.eclipse.bpmn2.ExclusiveGateway;
import org.eclipse.bpmn2.ExtensionAttributeValue;
import org.eclipse.bpmn2.FlowElement;
import org.eclipse.bpmn2.FlowElementsContainer;
import org.eclipse.bpmn2.InclusiveGateway;
import org.eclipse.bpmn2.IntermediateCatchEvent;
import org.eclipse.bpmn2.IntermediateThrowEvent;
import org.eclipse.bpmn2.LinkEventDefinition;
import org.eclipse.bpmn2.ManualTask;
import org.eclipse.bpmn2.MessageEventDefinition;
import org.eclipse.bpmn2.ParallelGateway;
import org.eclipse.bpmn2.Process;
import org.eclipse.bpmn2.ReceiveTask;
import org.eclipse.bpmn2.Relationship;
import org.eclipse.bpmn2.RootElement;
import org.eclipse.bpmn2.ScriptTask;
import org.eclipse.bpmn2.SendTask;
import org.eclipse.bpmn2.ServiceTask;
import org.eclipse.bpmn2.SignalEventDefinition;
import org.eclipse.bpmn2.StartEvent;
import org.eclipse.bpmn2.TimerEventDefinition;
import org.eclipse.bpmn2.UserTask;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.jbpm.simulation.SimulationDataProvider;
import org.jbpm.simulation.util.BPMN2Utils;
import org.jbpm.simulation.util.SimulationConstants;
import org.kie.api.definition.process.Node;

public class BPMN2SimulationDataProvider implements SimulationDataProvider {
    private Definitions def;

    public BPMN2SimulationDataProvider(Definitions def) {
        this.def = def;
    }
    
    public BPMN2SimulationDataProvider(String bpmn2xml) {
    	this.def = BPMN2Utils.getDefinitions(new ByteArrayInputStream(getBytes(bpmn2xml)));
    }
    
	public BPMN2SimulationDataProvider(InputStream bpmn2Stream) {
		this.def = BPMN2Utils.getDefinitions(bpmn2Stream);
	}
    
    public Map<String, Object> getSimulationDataForNode(Node node) {
        String nodeId = (String) node.getMetaData().get("UniqueId");
        return getSimulationDataForNode(nodeId);
    }
    public Map<String, Object> getSimulationDataForNode(
            String nodeId) {
        Map<String, Object> properties = new HashMap<String, Object>();
        Scenario scenario = getDefaultScenario(def);
        if(scenario != null) {
        	String baseTimeUnitValue = "";
        	String baseCurrencyUnitValue = "";
        	if(scenario.getScenarioParameters() != null) {
        		baseCurrencyUnitValue = scenario.getScenarioParameters().getBaseCurrencyUnit();
        		baseTimeUnitValue = scenario.getScenarioParameters().getBaseTimeUnit().getName();
            }
        	
        	if(scenario.getElementParameters() != null) {
        		for(ElementParameters eleType : scenario.getElementParameters()) {
        			if(eleType.getElementRef().equals(nodeId)) {
        				if(eleType.getControlParameters() != null && eleType.getControlParameters().getProbability() != null) {
        					FloatingParameterType valType = (FloatingParameterType) eleType.getControlParameters().getProbability().getParameterValue().get(0);
                			properties.put("probability", valType.getValue());
        				}
        				if(eleType.getTimeParameters() != null) {
        					if(eleType.getTimeParameters().getProcessingTime() != null) {
        						Parameter processingTime = eleType.getTimeParameters().getProcessingTime();
                    			ParameterValue paramValue =  processingTime.getParameterValue().get(0);
                    			if(paramValue instanceof NormalDistributionType) {
                    				NormalDistributionType ndt = (NormalDistributionType) paramValue;
                    				properties.put("mean", ndt.getMean());
                    				properties.put("standarddeviation", ndt.getStandardDeviation());
                    				properties.put("distributiontype", "normal");
                    			} else if(paramValue instanceof UniformDistributionType) {
                    				UniformDistributionType udt = (UniformDistributionType) paramValue;
                    				properties.put("max", udt.getMax());
                    				properties.put("min", udt.getMin());
                    				properties.put("distributiontype", "uniform");
                                // random distribution not supported in bpsim 1.0
//                    			} else if(paramValue instanceof RandomDistributionType) {
//                    				RandomDistributionType rdt = (RandomDistributionType) paramValue;
//                    				properties.put("max", rdt.getMax());
//                    				properties.put("min", rdt.getMin());
//                    				properties.put("distributiontype", "random");
                    			} else if(paramValue instanceof PoissonDistributionType) {
                    				PoissonDistributionType pdt = (PoissonDistributionType) paramValue;
                    				properties.put("mean", pdt.getMean());
                    				properties.put("distributiontype", "poisson");
                    			}
                                properties.put("timeunit", baseTimeUnitValue);

                                if(eleType.getTimeParameters().getWaitTime() != null) {
                                    FloatingParameterType waittimeType = (FloatingParameterType) eleType.getTimeParameters().getWaitTime().getParameterValue().get(0);
                                    properties.put("waittime", waittimeType.getValue());<commit_msg>BZ-1017578 - Simulation engine starts less instances than is set if the process contains a parallel gateway<commit_after>package org.jbpm.simulation.impl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bpsim.*;
import org.eclipse.bpmn2.BoundaryEvent;
import org.eclipse.bpmn2.BusinessRuleTask;
import org.eclipse.bpmn2.CancelEventDefinition;
import org.eclipse.bpmn2.CompensateEventDefinition;
import org.eclipse.bpmn2.ConditionalEventDefinition;
import org.eclipse.bpmn2.Definitions;
import org.eclipse.bpmn2.EndEvent;
import org.eclipse.bpmn2.ErrorEventDefinition;
import org.eclipse.bpmn2.EventDefinition;
import org.eclipse.bpmn2.ExclusiveGateway;
import org.eclipse.bpmn2.ExtensionAttributeValue;
import org.eclipse.bpmn2.FlowElement;
import org.eclipse.bpmn2.FlowElementsContainer;
import org.eclipse.bpmn2.InclusiveGateway;
import org.eclipse.bpmn2.IntermediateCatchEvent;
import org.eclipse.bpmn2.IntermediateThrowEvent;
import org.eclipse.bpmn2.LinkEventDefinition;
import org.eclipse.bpmn2.ManualTask;
import org.eclipse.bpmn2.MessageEventDefinition;
import org.eclipse.bpmn2.ParallelGateway;
import org.eclipse.bpmn2.Process;
import org.eclipse.bpmn2.ReceiveTask;
import org.eclipse.bpmn2.Relationship;
import org.eclipse.bpmn2.RootElement;
import org.eclipse.bpmn2.ScriptTask;
import org.eclipse.bpmn2.SendTask;
import org.eclipse.bpmn2.SequenceFlow;
import org.eclipse.bpmn2.ServiceTask;
import org.eclipse.bpmn2.SignalEventDefinition;
import org.eclipse.bpmn2.StartEvent;
import org.eclipse.bpmn2.TimerEventDefinition;
import org.eclipse.bpmn2.UserTask;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.jbpm.simulation.SimulationDataProvider;
import org.jbpm.simulation.util.BPMN2Utils;
import org.jbpm.simulation.util.SimulationConstants;
import org.kie.api.definition.process.Node;

public class BPMN2SimulationDataProvider implements SimulationDataProvider {
    private Definitions def;

    public BPMN2SimulationDataProvider(Definitions def) {
        this.def = def;
    }
    
    public BPMN2SimulationDataProvider(String bpmn2xml) {
    	this.def = BPMN2Utils.getDefinitions(new ByteArrayInputStream(getBytes(bpmn2xml)));
    }
    
	public BPMN2SimulationDataProvider(InputStream bpmn2Stream) {
		this.def = BPMN2Utils.getDefinitions(bpmn2Stream);
	}
    
    public Map<String, Object> getSimulationDataForNode(Node node) {
        String nodeId = (String) node.getMetaData().get("UniqueId");
        return getSimulationDataForNode(nodeId);
    }
    public Map<String, Object> getSimulationDataForNode(
            String nodeId) {
        Map<String, Object> properties = new HashMap<String, Object>();
        Scenario scenario = getDefaultScenario(def);
        if(scenario != null) {
        	String baseTimeUnitValue = "";
        	String baseCurrencyUnitValue = "";
        	if(scenario.getScenarioParameters() != null) {
        		baseCurrencyUnitValue = scenario.getScenarioParameters().getBaseCurrencyUnit();
        		baseTimeUnitValue = scenario.getScenarioParameters().getBaseTimeUnit().getName();
            }
        	
        	if(scenario.getElementParameters() != null) {
        		for(ElementParameters eleType : scenario.getElementParameters()) {
        			if(eleType.getElementRef().equals(nodeId)) {
        				if(eleType.getControlParameters() != null && eleType.getControlParameters().getProbability() != null) {

                            FlowElement element = null;
                            for (RootElement root : def.getRootElements()) {
                                if (root instanceof Process) {
                                    element = findElementInContainer((FlowElementsContainer) root, nodeId);
                                    if (element != null && element instanceof SequenceFlow) {
                                        element = ((SequenceFlow)element).getSourceRef();
                                    }
                                    break;
                                }
                            }
                            if (element != null && element instanceof ParallelGateway) {
                                // probability should be ignored for parallel gateways so use default value
                                properties.put("probability", 100.0);
                            } else {
                                FloatingParameterType valType = (FloatingParameterType) eleType.getControlParameters().getProbability().getParameterValue().get(0);
                                properties.put("probability", valType.getValue());
                            }
        				}
        				if(eleType.getTimeParameters() != null) {
        					if(eleType.getTimeParameters().getProcessingTime() != null) {
        						Parameter processingTime = eleType.getTimeParameters().getProcessingTime();
                    			ParameterValue paramValue =  processingTime.getParameterValue().get(0);
                    			if(paramValue instanceof NormalDistributionType) {
                    				NormalDistributionType ndt = (NormalDistributionType) paramValue;
                    				properties.put("mean", ndt.getMean());
                    				properties.put("standarddeviation", ndt.getStandardDeviation());
                    				properties.put("distributiontype", "normal");
                    			} else if(paramValue instanceof UniformDistributionType) {
                    				UniformDistributionType udt = (UniformDistributionType) paramValue;
                    				properties.put("max", udt.getMax());
                    				properties.put("min", udt.getMin());
                    				properties.put("distributiontype", "uniform");
                                // random distribution not supported in bpsim 1.0
//                    			} else if(paramValue instanceof RandomDistributionType) {
//                    				RandomDistributionType rdt = (RandomDistributionType) paramValue;
//                    				properties.put("max", rdt.getMax());
//                    				properties.put("min", rdt.getMin());
//                    				properties.put("distributiontype", "random");
                    			} else if(paramValue instanceof PoissonDistributionType) {
                    				PoissonDistributionType pdt = (PoissonDistributionType) paramValue;
                    				properties.put("mean", pdt.getMean());
                    				properties.put("distributiontype", "poisson");
                    			}
                                properties.put("timeunit", baseTimeUnitValue);

                                if(eleType.getTimeParameters().getWaitTime() != null) {
                                    FloatingParameterType waittimeType = (FloatingParameterType) eleType.getTimeParameters().getWaitTime().getParameterValue().get(0);
                                    properties.put("waittime", waittimeType.getValue());
{'fraction_non_alphanumeric': 0.09686411149825784, 'fraction_numerical': 0.008207510646535037, 'mean_word_length': 2.6017847183491356, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5940256', 'n_tokens_mistral': 3673, 'n_tokens_neox': 3446, 'n_words': 552}