Search in sources :

Example 1 with DoubleEvaluator

use of com.fathzer.soft.javaluator.DoubleEvaluator in project Roblu by wdavies973.

the class RCalculation method getValue.

/**
 * Process the equation and returns a value
 * @param metrics the metric list to process
 * @return the value
 */
public String getValue(ArrayList<RMetric> metrics) {
    if (calculation == null || calculation.equals("null"))
        return "Bad equation";
    try {
        String equation = calculation;
        // Substitute values in for the metric names
        for (RMetric metric : metrics) {
            // Skip the reference to "ourself"
            if (metric.getTitle().equals(title))
                continue;
            if (metric instanceof RCounter || metric instanceof RStopwatch || metric instanceof RSlider) {
                equation = equation.replaceAll(metric.getTitle(), metric.toString());
            } else if (metric instanceof RCalculation) {
                // This condition is required or this will overflow recursively
                if (equation.contains(metric.getTitle()))
                    equation = equation.replaceAll(metric.getTitle(), ((RCalculation) metric).getValue(metrics));
            }
        }
        // Trim
        equation = equation.trim();
        equation = equation.replaceAll(" ", "");
        Log.d("RBS", "Evaluating " + equation + ". Value: " + new DoubleEvaluator().evaluate(equation));
        // Process
        lastValue = Utils.round(new DoubleEvaluator().evaluate(equation), 2);
        return String.valueOf(lastValue);
    } catch (Exception e) {
        Log.d("RBS", "bad equation: ", e);
        return "Bad equation";
    }
}
Also used : DoubleEvaluator(com.fathzer.soft.javaluator.DoubleEvaluator)

Aggregations

DoubleEvaluator (com.fathzer.soft.javaluator.DoubleEvaluator)1