<commit_before>package de.uni_potsdam.hpi.bpt.bp2014.jcore;

import de.uni_potsdam.hpi.bpt.bp2014.database.DbDataObject;
import de.uni_potsdam.hpi.bpt.bp2014.database.DbEventMapping;
import de.uni_potsdam.hpi.bpt.bp2014.database.DbFragment;
import de.uni_potsdam.hpi.bpt.bp2014.database.DbScenario;
import de.uni_potsdam.hpi.bpt.bp2014.database.DbScenarioInstance;
import de.uni_potsdam.hpi.bpt.bp2014.database.DbTerminationCondition;
import de.uni_potsdam.hpi.bpt.bp2014.jcore.controlnodes.AbstractControlNodeInstance;
import de.uni_potsdam.hpi.bpt.bp2014.jcore.controlnodes.AbstractEvent;
import de.uni_potsdam.hpi.bpt.bp2014.jcore.controlnodes.ActivityInstance;
import de.uni_potsdam.hpi.bpt.bp2014.jcore.controlnodes.GatewayInstance;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Represents a scenario instance.
 * The constructor looks for an scenario instance in the database or creates a new one.
 * The constructor also initialize the fragment instances and data object instances.
 * The scenario instance has Lists for all enabled, control flow enabled, data flow enabled,
 * running and terminated activity instances, fragment instances and all data object instances.
 * The scenario instance provide methods for the administration of the data object instances
 */
public class ScenarioInstance {
	private final int scenarioInstanceId;
	private final int scenarioId;
	private final String name;
	/**
	 * database connection objects.
	 */
	private final DbScenarioInstance dbScenarioInstance = new DbScenarioInstance();
	private final DbFragment dbFragment = new DbFragment();
	private final DbScenario dbScenario = new DbScenario();
	/**
	 * Lists to save all fragments and all control nodes sorted by state.
	 */
	private LinkedList<AbstractControlNodeInstance> controlNodeInstances = new LinkedList<>();
	private LinkedList<AbstractControlNodeInstance> enabledControlNodeInstances
			= new LinkedList<>();
	private LinkedList<AbstractControlNodeInstance> controlFlowEnabledControlNodeInstances
			= new LinkedList<>();
	private LinkedList<AbstractControlNodeInstance> dataEnabledControlNodeInstances
			= new LinkedList<>();
	private LinkedList<AbstractControlNodeInstance> runningControlNodeInstances
			= new LinkedList<>();
	private LinkedList<AbstractControlNodeInstance> terminatedControlNodeInstances
			= new LinkedList<>();

	private LinkedList<FragmentInstance> fragmentInstances = new LinkedList<>();
	private LinkedList<AbstractControlNodeInstance> referentialRunningControlNodeInstances
			= new LinkedList<>();
	private LinkedList<GatewayInstance> executingGateways = new LinkedList<>();
	private Map<Integer, DataAttributeInstance> dataAttributeInstances = new HashMap<>();


    private DataManager dataManager;

    private boolean canTerminate;
    private TerminationCondition terminationCondition;
	/**
	 * Creates and initializes a new scenario instance from database.
	 * Reads the information for an existing scenario instance from the database.
	 * If there is no match in the database it creates a new scenario instance.
	 *
	 * @param scenarioId         This is the database id from the scenario.
	 * @param scenarioInstanceId This is the database id from the scenario instance.
	 */
	public ScenarioInstance(int scenarioId, int scenarioInstanceId) {

        this.name = dbScenario.getScenarioName(scenarioId);
		this.scenarioId = scenarioId;
		this.terminationCondition = new TerminationCondition(this);

        if (dbScenarioInstance.existScenario(scenarioId, scenarioInstanceId)) {
			//creates an existing Scenario Instance using the database information
			this.scenarioInstanceId = scenarioInstanceId;
		} else {
			this.scenarioInstanceId = dbScenarioInstance.createNewScenarioInstance(
					scenarioId);
		}
        // Create the data manager after setting the scenario instance id but before
        // initializing fragments
        this.dataManager = new DataManager(this);

        if (dbScenarioInstance.getTerminated(this.scenarioInstanceId) == 0) {
			this.initializeFragments();
		}

        this.startAutomaticControlNodes();
        canTerminate = checkTerminationCondition();
	}

	/**<commit_msg>Moved instantiation of DataManager in ScenarioInstance up to fix restinterface errors.<commit_after>package de.uni_potsdam.hpi.bpt.bp2014.jcore;

import de.uni_potsdam.hpi.bpt.bp2014.database.DbDataObject;
import de.uni_potsdam.hpi.bpt.bp2014.database.DbEventMapping;
import de.uni_potsdam.hpi.bpt.bp2014.database.DbFragment;
import de.uni_potsdam.hpi.bpt.bp2014.database.DbScenario;
import de.uni_potsdam.hpi.bpt.bp2014.database.DbScenarioInstance;
import de.uni_potsdam.hpi.bpt.bp2014.database.DbTerminationCondition;
import de.uni_potsdam.hpi.bpt.bp2014.jcore.controlnodes.AbstractControlNodeInstance;
import de.uni_potsdam.hpi.bpt.bp2014.jcore.controlnodes.AbstractEvent;
import de.uni_potsdam.hpi.bpt.bp2014.jcore.controlnodes.ActivityInstance;
import de.uni_potsdam.hpi.bpt.bp2014.jcore.controlnodes.GatewayInstance;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Represents a scenario instance.
 * The constructor looks for an scenario instance in the database or creates a new one.
 * The constructor also initialize the fragment instances and data object instances.
 * The scenario instance has Lists for all enabled, control flow enabled, data flow enabled,
 * running and terminated activity instances, fragment instances and all data object instances.
 * The scenario instance provide methods for the administration of the data object instances
 */
public class ScenarioInstance {
	private final int scenarioInstanceId;
	private final int scenarioId;
	private final String name;
	/**
	 * database connection objects.
	 */
	private final DbScenarioInstance dbScenarioInstance = new DbScenarioInstance();
	private final DbFragment dbFragment = new DbFragment();
	private final DbScenario dbScenario = new DbScenario();
	/**
	 * Lists to save all fragments and all control nodes sorted by state.
	 */
	private LinkedList<AbstractControlNodeInstance> controlNodeInstances = new LinkedList<>();
	private LinkedList<AbstractControlNodeInstance> enabledControlNodeInstances
			= new LinkedList<>();
	private LinkedList<AbstractControlNodeInstance> controlFlowEnabledControlNodeInstances
			= new LinkedList<>();
	private LinkedList<AbstractControlNodeInstance> dataEnabledControlNodeInstances
			= new LinkedList<>();
	private LinkedList<AbstractControlNodeInstance> runningControlNodeInstances
			= new LinkedList<>();
	private LinkedList<AbstractControlNodeInstance> terminatedControlNodeInstances
			= new LinkedList<>();

	private LinkedList<FragmentInstance> fragmentInstances = new LinkedList<>();
	private LinkedList<AbstractControlNodeInstance> referentialRunningControlNodeInstances
			= new LinkedList<>();
	private LinkedList<GatewayInstance> executingGateways = new LinkedList<>();
	private Map<Integer, DataAttributeInstance> dataAttributeInstances = new HashMap<>();


    private DataManager dataManager;

    private boolean canTerminate;
    private TerminationCondition terminationCondition;
	/**
	 * Creates and initializes a new scenario instance from database.
	 * Reads the information for an existing scenario instance from the database.
	 * If there is no match in the database it creates a new scenario instance.
	 *
	 * @param scenarioId         This is the database id from the scenario.
	 * @param scenarioInstanceId This is the database id from the scenario instance.
	 */
	public ScenarioInstance(int scenarioId, int scenarioInstanceId) {

        this.name = dbScenario.getScenarioName(scenarioId);
		this.scenarioId = scenarioId;
		this.terminationCondition = new TerminationCondition(this);

        if (dbScenarioInstance.existScenario(scenarioId, scenarioInstanceId)) {
			//creates an existing Scenario Instance using the database information
			this.scenarioInstanceId = scenarioInstanceId;
		} else {
			this.scenarioInstanceId = dbScenarioInstance.createNewScenarioInstance(
					scenarioId);
		}
        this.startAutomaticControlNodes();

		this.dataManager = new DataManager(this);

		if (dbScenarioInstance.getTerminated(this.scenarioInstanceId) == 0) {
			this.initializeFragments();
		}

		this.startAutomaticControlNodes();
        canTerminate = checkTerminationCondition();
	}

	/**
{'fraction_non_alphanumeric': 0.07602690367523421, 'fraction_numerical': 0.01080951237088638, 'mean_word_length': 5.351639969488939, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6962489', 'n_tokens_mistral': 2372, 'n_tokens_neox': 2185, 'n_words': 621}