private void runExperimentSeries(
      ScenarioInstance scenarioInstance,
      Map<MeasurementSpecification, List<ExperimentSeriesDefinition>> experimentSeries) {
    boolean mecAcquired = false;
    try {
      experimentController.acquireMEController();
      mecAcquired = true;

      for (MeasurementSpecification measSpec : experimentSeries.keySet()) {
        experimentController.initialize(
            EngineTools.getConstantParameterValues(measSpec.getInitializationAssignemts()),
            scenarioInstance.getScenarioDefinition().getMeasurementEnvironmentDefinition());

        for (ExperimentSeriesDefinition esd : experimentSeries.get(measSpec)) {
          ExperimentSeries series = scenarioInstance.getExperimentSeries(esd.getName());
          if (series == null) {
            series = EntityFactory.createExperimentSeries(esd);
            scenarioInstance.getExperimentSeriesList().add(series);
            series.setScenarioInstance(scenarioInstance);

            persistenceProvider.store(series);
          }

          experimentSeriesManager.runExperimentSeries(series);
        }
      }

    } catch (Exception e) {
      throw new RuntimeException("Failed executing experiments!", e);
    } finally {
      if (mecAcquired) {
        experimentController.releaseMEController();
      }
    }
  }
  /** @return a dummy scenario instance */
  public static ScenarioInstance createDummyScenarioInstance() {
    scenarioDefinition = loadScenarioDefinition();
    ScenarioInstance scenarioInstance =
        EntityFactory.createScenarioInstance(scenarioDefinition, "Dummy");

    for (ExperimentSeries es : createDummyExperimentSeries()) {
      scenarioInstance.getExperimentSeriesList().add(es);
      es.setScenarioInstance(scenarioInstance);
    }
    return scenarioInstance;
  }
  private ExperimentSeries getSeries(ScenarioInstance instance, String name)
      throws DataNotFoundException {
    for (ExperimentSeries series : instance.getExperimentSeriesList()) {
      if (series.getName().equals(name)) {
        return series;
      }
    }

    throw new DataNotFoundException("No ExperimentSeries '" + name + "' found..");
  }
  @Override
  public ScenarioInstance run(
      ScenarioDefinition scenarioDefinition, Collection<String> experimentSeriesNames) {
    if (experimentSeriesNames == null || experimentSeriesNames.isEmpty()) {
      throw new RuntimeException("List of experiment series names is empty or null!");
    }

    ScenarioInstance scenarioInstance = retrieveScenarioInstance(scenarioDefinition);
    scenarioInstance.getScenarioDefinition().getAllExperimentSeriesDefinitions();

    Map<MeasurementSpecification, List<ExperimentSeriesDefinition>> experimentSeries =
        new HashMap<MeasurementSpecification, List<ExperimentSeriesDefinition>>();

    for (MeasurementSpecification ms : scenarioDefinition.getMeasurementSpecifications()) {
      List<ExperimentSeriesDefinition> currentExpSeriesList =
          new ArrayList<ExperimentSeriesDefinition>();
      for (ExperimentSeriesDefinition esd : ms.getExperimentSeriesDefinitions()) {

        for (String expSeriesName : experimentSeriesNames) {
          if ((ms.getName() + "." + esd.getName()).equals(expSeriesName)) {
            currentExpSeriesList.add(esd);
          }
        }
      }
      if (!currentExpSeriesList.isEmpty()) {
        experimentSeries.put(ms, currentExpSeriesList);
      }
    }

    runExperimentSeries(scenarioInstance, experimentSeries);

    try {
      ScenarioInstance loadedScenario =
          persistenceProvider.loadScenarioInstance(scenarioInstance.getPrimaryKey());
      return loadedScenario;
    } catch (DataNotFoundException e) {
      LOGGER.error(
          "Cannot load the scenario from the persistnce provider. Something is seriously gone wrong.");
      throw new RuntimeException("Something went wrong");
    }
  }
  /**
   * Creates a SharedScenarioInstance out of a ScenarioInstance which can send to the FrontEnd.
   *
   * @param instance
   * @return
   */
  private SharedScenarioInstance convertInstance(ScenarioInstance instance) {
    SharedScenarioInstance retInstance = new SharedScenarioInstance();

    retInstance.setScenarioName(instance.getName());
    retInstance.setControllerUrl(instance.getMeasurementEnvironmentUrl());

    for (ExperimentSeries series : instance.getExperimentSeriesList()) {
      SharedExperimentSeries sharedSeries = new SharedExperimentSeries();
      sharedSeries.setExperimentName(series.getName());

      for (ExperimentSeriesRun run : series.getExperimentSeriesRuns()) {
        SharedExperimentRuns sharedRun = new SharedExperimentRuns();
        sharedRun.setTimestamp(run.getTimestamp());
        sharedRun.setLabel(run.getLabel());

        sharedSeries.addExperimentRun(sharedRun);
      }

      retInstance.addExperimentSeries(sharedSeries);
    }

    return retInstance;
  }