Ejemplo n.º 1
0
 @Override
 public synchronized void loadScenario(String scenarioName) {
   Scenario scenario = null;
   for (Scenario scen : this.scenarios) {
     if (scenarioName.equals(scen.getName())) {
       scenario = scen;
       break;
     }
   }
   if (scenario != null) loadScenario(scenario);
 }
Ejemplo n.º 2
0
  private synchronized void loadScenario(Scenario scenario) {
    if (this.simulation.getState() != EngineState.STOPPED
        && this.simulation.getState() != EngineState.CRASHED) return;

    boolean found = false;
    for (Scenario scen : this.scenarios) {
      if (scenario.equals(scen)) {
        found = true;
        break;
      }
    }
    if (!found) throw new RuntimeException("Scenario " + scenario.getName() + " does not exist");

    if (this.context != null) {
      this.context.close();
    }
    try {
      this.graph.clear();
    } catch (Exception err) {
      logger.error("Error clearing graph", err);
    }
    String[] configs = {SCENARIO_FOLDER + "/" + scenario.getName()};
    this.context = new ClassPathXmlApplicationContext(configs, this.applicationContext);

    if (!this.currentScenario.equals(found) || this.scenarioParameters.isEmpty()) {
      this.scenarioParameters.clear();

      String[] names = this.context.getBeanNamesForType(Object.class);
      for (String beanName : names) {
        Object bean = this.context.getBean(beanName);
        ArrayList<java.lang.reflect.Field> fields = new ArrayList<java.lang.reflect.Field>();
        Class<?> clazz = bean.getClass();
        do {
          for (java.lang.reflect.Field field : clazz.getDeclaredFields()) {
            fields.add(field);
          }
          clazz = clazz.getSuperclass();
        } while (clazz != null);
        clazz = bean.getClass();
        for (java.lang.reflect.Field field : fields) {
          String name = field.getName();
          SimulationParameter annotation = field.getAnnotation(SimulationParameter.class);
          if (annotation != null) {
            Object value = null;
            try {
              Method getter;
              Class<?> fieldType = field.getType();
              if (fieldType.equals(Boolean.class) || fieldType.equals(boolean.class)) {
                getter = bean.getClass().getMethod("is" + this.capitalize(name));
              } else {
                getter = bean.getClass().getMethod("get" + this.capitalize(name));
              }
              value = getter.invoke(bean);
            } catch (Exception e) {
              throw new RuntimeException(e);
            }
            ConfigurableObject cfgObj = this.scenarioParameters.get(beanName);
            if (cfgObj == null) {
              cfgObj = new ConfigurableObject(beanName, clazz.getName());
              this.scenarioParameters.put(beanName, cfgObj);
            }
            ScenarioParameter parameter = new ScenarioParameter(name, value, annotation.label());
            if (!Double.isNaN(annotation.from()) && !Double.isNaN(annotation.to())) {
              parameter.setFrom(annotation.from());
              parameter.setTo(annotation.to());
            }
            if (!Double.isNaN(annotation.step())) {
              parameter.setStep(annotation.step());
            }
            cfgObj.addParameter(parameter);
          }
        }
      }
    }

    this.currentScenario = scenario;
    this.scenarioLoaded = true;
  }