Exemplo n.º 1
0
  @Override
  public Variable evaluate(SimulationStatus status) {

    Calculable startCalc = buildExpression(startBuilder);
    Calculable endCalc = buildExpression(endBuilder);
    if (startCalc == null || endCalc == null) {
      return new Variable("Unknown");
    }

    // Set the variables in the start and end calculators
    for (FlightDataType type : status.getFlightData().getTypes()) {
      double value = status.getFlightData().getLast(type);
      startCalc.setVariable(new Variable(type.getSymbol(), value));
      endCalc.setVariable(new Variable(type.getSymbol(), value));
    }

    // From the given datatype, get the time and function values and make an interpolator

    // Note: must get in a way that flight data system will figure out units. Otherwise there will
    // be a type conflict when we get the new data.
    FlightDataType type = FlightDataType.getType(null, getSymbol(), null);

    List<Double> data = status.getFlightData().get(type);
    List<Double> time = status.getFlightData().get(FlightDataType.TYPE_TIME);
    LinearInterpolator interp = new LinearInterpolator(time, data);

    // Evaluate the expression to get the start and end of the range
    double startTime, endTime;
    try {
      startTime = startCalc.calculate().getDoubleValue();
      startTime = MathUtil.clamp(startTime, 0, Double.MAX_VALUE);

      endTime = endCalc.calculate().getDoubleValue();
      endTime = MathUtil.clamp(endTime, 0, time.get(time.size() - 1));
    } catch (java.util.EmptyStackException e) {
      log.info(
          Markers.USER_MARKER,
          "Unable to calculate time index for range expression "
              + getSymbol()
              + " due to empty stack exception");
      return new Variable("Unknown");
    }

    // generate an array representing the range
    double step = status.getSimulationConditions().getSimulation().getOptions().getTimeStep();
    double[] t = ArrayUtils.range(startTime, endTime, step);
    double[] y = new double[t.length];
    int i = 0;
    for (double tval : t) {
      y[i] = interp.getValue(tval);
      i++;
    }

    Variable result;
    if (y.length == 0) {
      result = new Variable("Unknown");
    } else {
      result = new Variable(hash(), y, startTime, step);
    }

    return result;
  }