示例#1
0
  public void visit(ASTEquation astEquation) {
    if (astEquation.getRhs().getType().isError()) {
      warn(
          ERROR_CODE
              + ": Error in Expression type calculation: "
              + astEquation.getRhs().getType().getError());

      return;
    }
    if (!astEquation.getEnclosingScope().isPresent()) {
      warn(ERROR_CODE + "Enclosing scope not present. Run ScopeCreator");
      return;
    }

    // Resolve LHS Variable
    String varName = astEquation.getLhs().getSimpleName();
    Scope enclosingScope = astEquation.getEnclosingScope().get();
    Optional<VariableSymbol> varSymbol = NESTMLSymbols.resolve(varName, enclosingScope);

    TypeSymbol varType;
    if (!varSymbol.isPresent()) {
      warn(ERROR_CODE + " Error while resolving the variable to be derived in ODE: " + varName);
      return;
    }
    // Derive varType
    varType = varSymbol.get().getType();

    if (varType.getType() != TypeSymbol.Type.UNIT && varType != getRealType()) {
      warn(
          ERROR_CODE
              + "Type of LHS Variable in ODE is neither a Unit nor real at: "
              + astEquation.get_SourcePositionStart()
              + ". Skipping.");
      return;
    }

    UnitRepresentation varUnit = new UnitRepresentation(varType.getName());
    UnitRepresentation derivedVarUnit =
        varUnit.deriveT(astEquation.getLhs().getDifferentialOrder().size());

    // get type of RHS expression
    TypeSymbol typeFromExpression = astEquation.getRhs().getType().getValue();

    if (typeFromExpression.getType() != TypeSymbol.Type.UNIT
        && typeFromExpression != getRealType()) {
      warn(
          ERROR_CODE
              + "Type of ODE is neither a Unit nor real at: "
              + astEquation.get_SourcePositionStart());
      return;
    }
    UnitRepresentation unitFromExpression = new UnitRepresentation(typeFromExpression.getName());
    // set any of the units to ignoreMagnitude
    unitFromExpression.setIgnoreMagnitude(true);
    // do the actual test:
    if (!unitFromExpression.equals(derivedVarUnit)) {
      // remove magnitude for clearer error message
      derivedVarUnit.setMagnitude(0);
      unitFromExpression.setMagnitude(0);
      warn(
          ERROR_CODE
              + "Type of (derived) variable "
              + astEquation.getLhs().toString()
              + " is: "
              + derivedVarUnit.prettyPrint()
              + ". This does not match Type of RHS expression: "
              + unitFromExpression.prettyPrint()
              + " at: "
              + astEquation.get_SourcePositionStart()
              + ". Magnitudes are ignored in ODE Expressions");
    }
  }