Esempio n. 1
0
  public StateType makeStateType(Component cpt, HashMap<String, Double> fixedHM)
      throws ContentError, ParseError {

    StateType ret = new StateType(cpt.getID(), cpt.getComponentType().getName());

    ret.setSimultaneous(simultaneous);

    for (ParamValue pv : cpt.getParamValues()) {
      String qn = pv.getName();
      double qv = pv.getDoubleValue();
      ret.addFixed(qn, qv);
      fixedHM.put(qn, qv);
    }

    for (RequiredVar rv : p_requiredVars) {
      ret.addIndependentVariable(rv.getName(), rv.getDimensionString());
    }

    for (ExposedVar ev : p_exposedVars) {
      ret.addExposedVariable(ev.getName(), ev.getDimensionString());
    }

    HashSet<StateVariable> varHS = new HashSet<StateVariable>();
    for (StateVariable sv : stateVariables) {
      varHS.add(sv);
      ret.addStateVariable(sv.getName(), sv.getDimensionString());
      if (sv.hasExposure()) {
        ret.addExposureMapping(sv.getName(), sv.getExposure().getName());
      }
    }

    for (InstanceProperty ip : cpt.getComponentType().getInstancePropertys()) {
      String pnm = ip.getName();
      ret.addExposureMapping(pnm, pnm);
      ret.addInstanceProperty(ip);
    }

    /*
    for (ExternalQuantity equan : externalQuantitys) {
     String qn = equan.getName();
     double qv = PathEvaluator.getValue(cpt, equan.getPath());
     ret.addFixed(qn, qv);
     fixedHM.put(qn, qv); // MUSTDO we don't need both of these:
     // either Ext quans shouldn't say they are fixed and we should use ret.addFixed, or it should
     // and use fixedHM.put.
    }
    */

    for (DerivedVariable dv : derivedVariables) {
      if (dv.hasExpression()) {

        DoubleEvaluator db = dv.getParseTree().makeFloatFixedEvaluator(fixedHM);

        ret.addExpressionDerived(dv.getName(), db, dv.getDimensionString());

      } else if (dv.hasSelection()) {
        ret.addPathDerived(
            dv.getName(),
            dv.getPath(),
            dv.getFunc(),
            dv.isRequired(),
            dv.getReduce(),
            dv.getDimensionString());

      } else {
        throw new ContentError("Derived variable needs as selection or an expression");
      }
      if (dv.hasExposure()) {
        ret.addExposureMapping(dv.getName(), dv.getExposure().getName());
      }
    }

    for (ConditionalDerivedVariable cdv : conditionalDerivedVariables) {
      DoubleEvaluator db = cdv.makeFloatFixedEvaluator(fixedHM);

      ret.addExpressionDerived(cdv.getName(), db, cdv.getDimensionString());

      if (cdv.hasExposure()) {
        ret.addExposureMapping(cdv.getName(), cdv.getExposure().getName());
      }
    }

    for (TimeDerivative sd : timeDerivatives) {
      StateVariable sv = sd.getStateVariable();
      varHS.remove(sv);

      ParseTree pt = sd.getParseTree();
      DoubleEvaluator db = pt.makeFloatFixedEvaluator(fixedHM);
      ret.addRate(sv.getName(), db, sd.getDimensionString());
    }

    for (OnStart os : onStarts) {
      ActionBlock ea = os.makeEventAction(fixedHM);
      ret.addInitialization(ea);
    }

    for (OnEvent oe : onEvents) {
      EventAction er = new EventAction(oe.getPortName());
      ActionBlock ea = oe.makeEventAction(fixedHM);
      er.setAction(ea);
      if (ea == null) {
        throw new ContentError("Null action block from OnEvent " + oe);
      }
      ret.addEventResponse(er);
    }
    if (regimes.size() > 0) {
      for (EventPort p : r_type.getEventPorts()) {
        if (p.isDirectionIn()) {
          if (onEvents.hasName(p.getName())) {
            // OK, the existing action will also send the event on to the active regime
          } else {
            EventAction er = new EventAction(p.getName());
            ret.addEventResponse(er);
          }
        }
      }
    }

    for (OnCondition oc : onConditions) {

      ParseTree pt = oc.getParseTree();
      BooleanEvaluator bb = pt.makeBooleanFixedEvaluator(fixedHM);

      ConditionAction cr = new ConditionAction(bb);
      ActionBlock ea = oc.makeEventAction(fixedHM);
      cr.setAction(ea);
      ret.addConditionResponse(cr);
    }

    for (KineticScheme ks : kineticSchemes) {
      ArrayList<Component> states = cpt.getChildrenAL(ks.getNodesName());
      ArrayList<Component> rates = cpt.getChildrenAL(ks.getEdgesName());

      KScheme scheme = ks.makeKScheme(states, rates);

      ret.addKScheme(scheme);
    }

    for (EventPort p : r_type.getEventPorts()) {
      if (p.isDirectionIn()) {
        ret.addInputPort(p.getName());
      }
      // TODO - also need output ports done the same way, in case send action is in a sub-regime
    }

    for (Regime reg : regimes) {
      ComponentRegime crb = reg.makeComponentRegime(ret, cpt, copyFixed(fixedHM));
      ret.addComponentRegime(crb);
    }

    // TODO - could do something with children of parent type here, but don't have to as they
    // come in again via the structure of the component itself.

    /*
    for (StateVariable sv : varHS) {
    // E.info("sv without derivative " + sv + " in mcb for " + cpt.getID() + " " + cpt.hashCode());
    }
    */

    ret.fix();
    return ret;
  }
Esempio n. 2
0
  private void realResolve(Lems lems, Parser parser) throws ContentError, ParseError {
    if (lems.looseResolving()) {
      // then we expose anything with a name that matches the name of an exposure
      HashSet<String> expHS = new HashSet<String>();
      for (Exposure exp : getComponentType().getExposures()) {
        expHS.add(exp.getName());
      }

      for (StateVariable sv : stateVariables) {
        if (sv.exposure == null && expHS.contains(sv.name)) {
          sv.exposure = sv.name;
          E.shortWarning(
              "Implicitly exposing a state variable because its name matches an exposure: " + sv);
        }
      }
      for (DerivedVariable dv : derivedVariables) {
        if (dv.exposure == null && expHS.contains(dv.name)) {
          dv.exposure = dv.name;
          E.shortWarning(
              "Implicitly exposing a derived variable because its name matches an exposure: " + dv);
        }
      }

      for (ConditionalDerivedVariable dv : conditionalDerivedVariables) {
        if (dv.exposure == null && expHS.contains(dv.name)) {
          dv.exposure = dv.name;
          E.shortWarning(
              "Implicitly exposing a derived variable because its name matches an exposure: " + dv);
        }
      }
    }

    HashMap<String, Integer> exposedHM = new HashMap<String, Integer>();

    for (Requirement req : getComponentType().getRequirements()) {
      p_requiredVars.add(new RequiredVar(req.getName(), req.getDimension()));
    }

    for (Exposure exp : getComponentType().getExposures()) {
      p_exposedVars.add(new ExposedVar(exp.getName(), exp.getDimension()));
      exposedHM.put(exp.getName(), 0);
    }

    checkMakeValHM();

    addToMap(lems.getGlobals(), valHM);

    addToMap(lems.getConstantValueds(), valHM);

    for (DerivedVariable dvar : derivedVariables) {
      dvar.resolve(lems, lems.getDimensions(), r_type, valHM, parser);
      if (dvar.hasExposure()) {
        countExposure(dvar.getExposure(), exposedHM);
      }
    }

    for (ConditionalDerivedVariable dvar : conditionalDerivedVariables) {
      dvar.resolve(lems, lems.getDimensions(), r_type, valHM, parser);
      if (dvar.hasExposure()) {
        countExposure(dvar.getExposure(), exposedHM);
      }
    }

    for (StateVariable sv : stateVariables) {
      sv.resolve(r_type, lems.getDimensions());
      if (sv.hasExposure()) {
        countExposure(sv.getExposure(), exposedHM);
      }
    }

    for (StateScalarField scf : stateScalarFields) {
      scf.resolve(r_type, lems.getDimensions());
    }

    for (DerivedScalarField dcf : derivedScalarFields) {
      dcf.resolve(r_type, lems.getDimensions());
    }

    for (DerivedPunctateField dpf : derivedPunctateFields) {
      dpf.resolve(r_type, lems.getDimensions());
    }

    for (TimeDerivative sd : timeDerivatives) {
      sd.resolve(stateVariables, valHM, parser);
    }

    for (OnStart os : onStarts) {
      os.resolve(this, stateVariables, valHM, parser);
    }

    for (OnEvent oe : onEvents) {
      oe.resolve(this, stateVariables, valHM, parser);
    }

    for (OnCondition oc : onConditions) {
      oc.resolve(this, stateVariables, valHM, parser);
    }

    for (Regime reg : regimes) {
      reg.setBehavior(this);
      reg.resolve(stateVariables, lems, parser, exposedHM);
    }

    for (KineticScheme ks : kineticSchemes) {
      ks.resolve(r_type);
    }

    for (String enm : exposedHM.keySet()) {
      if (exposedHM.get(enm) == 0) {
        E.oneLineWarning(
            "No internal variable is linked to the exposure: "
                + enm
                + " for ComponentType "
                + getComponentType().getName());
      }
    }
  }