Search in sources :

Example 1 with LE

use of gov.sandia.n2a.language.operator.LE in project n2a by frothga.

the class EquationSet method determineDuration.

/**
 *        Infers length of simulation based on contents of equation set, particularly
 *        an expression for top-level $p that involves $t.
 *        Depends on results of: determineTypes() -- to set up Variable.type member with usable values
 */
public void determineDuration() {
    Variable p = find(new Variable("$p", 0));
    if (p != null && p.equations.size() == 1) {
        Operator expression = p.equations.first().expression;
        Operator variable = null;
        Operator value = null;
        if (// only true if expression is not null
        expression instanceof LT || expression instanceof LE) {
            OperatorBinary comparison = (OperatorBinary) expression;
            variable = comparison.operand0;
            value = comparison.operand1;
        } else if (expression instanceof GT || expression instanceof GE) {
            OperatorBinary comparison = (OperatorBinary) expression;
            variable = comparison.operand1;
            value = comparison.operand0;
        }
        if (variable instanceof AccessVariable && ((AccessVariable) variable).name.equals("$t")) {
            Instance instance = new Instance() {

                // all AccessVariable objects will reach here first, and get back the Variable.type field
                public Type get(VariableReference r) throws EvaluationException {
                    return r.variable.type;
                }
            };
            Type result = value.eval(instance);
            if (result instanceof Scalar)
                setNamedValue("duration", new Double(((Scalar) result).value).toString());
        }
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) AccessVariable(gov.sandia.n2a.language.AccessVariable) AccessVariable(gov.sandia.n2a.language.AccessVariable) Instance(gov.sandia.n2a.language.type.Instance) LT(gov.sandia.n2a.language.operator.LT) GT(gov.sandia.n2a.language.operator.GT) OperatorBinary(gov.sandia.n2a.language.OperatorBinary) Scalar(gov.sandia.n2a.language.type.Scalar) Type(gov.sandia.n2a.language.Type) LE(gov.sandia.n2a.language.operator.LE) GE(gov.sandia.n2a.language.operator.GE)

Aggregations

AccessVariable (gov.sandia.n2a.language.AccessVariable)1 Operator (gov.sandia.n2a.language.Operator)1 OperatorBinary (gov.sandia.n2a.language.OperatorBinary)1 Type (gov.sandia.n2a.language.Type)1 GE (gov.sandia.n2a.language.operator.GE)1 GT (gov.sandia.n2a.language.operator.GT)1 LE (gov.sandia.n2a.language.operator.LE)1 LT (gov.sandia.n2a.language.operator.LT)1 Instance (gov.sandia.n2a.language.type.Instance)1 Scalar (gov.sandia.n2a.language.type.Scalar)1