Search in sources :

Example 21 with Operator

use of gov.sandia.n2a.language.Operator in project n2a by frothga.

the class Variable method dependsOnEvent.

protected boolean dependsOnEvent(Variable from) {
    // Prevent infinite recursion
    Variable p = from;
    while (p != null) {
        if (p == this)
            return false;
        p = p.visited;
    }
    visited = from;
    // Scan temporary variables we depend on
    if (uses != null) {
        for (Variable u : uses.keySet()) {
            if (!u.hasAttribute("temporary"))
                continue;
            if (u.dependsOnEvent(this))
                return true;
        }
    }
    // Scan equations
    class EventVisitor extends Visitor {

        boolean found;

        public boolean visit(Operator op) {
            if (op instanceof Event)
                found = true;
            return !found;
        }
    }
    EventVisitor visitor = new EventVisitor();
    visit(visitor);
    return visitor.found;
}
Also used : Operator(gov.sandia.n2a.language.Operator) AccessVariable(gov.sandia.n2a.language.AccessVariable) Visitor(gov.sandia.n2a.language.Visitor) Event(gov.sandia.n2a.language.function.Event)

Example 22 with Operator

use of gov.sandia.n2a.language.Operator in project n2a by frothga.

the class Min method simplify.

public Operator simplify(Variable from) {
    Operator result = super.simplify(from);
    if (result != this)
        return result;
    // Check if Min appears as an operand. If so, merge its operands into ours
    ArrayList<Operator> newOperands = new ArrayList<Operator>(operands.length);
    boolean changed = false;
    for (int i = 0; i < operands.length; i++) {
        Operator o = operands[i];
        if (o instanceof Min) {
            Min m = (Min) o;
            newOperands.addAll(Arrays.asList(m.operands));
            changed = true;
        } else {
            newOperands.add(o);
        }
    }
    if (changed) {
        from.changed = true;
        Min newMin = new Min();
        newMin.operands = newOperands.toArray(new Operator[0]);
        return result;
    }
    return this;
}
Also used : Operator(gov.sandia.n2a.language.Operator) ArrayList(java.util.ArrayList)

Example 23 with Operator

use of gov.sandia.n2a.language.Operator in project n2a by frothga.

the class OR method simplify.

public Operator simplify(Variable from) {
    Operator result = super.simplify(from);
    if (result != this)
        return result;
    if (operand0 instanceof Constant) {
        Type c0 = ((Constant) operand0).value;
        if (c0 instanceof Scalar) {
            from.changed = true;
            double value = ((Scalar) c0).value;
            if (value == 0)
                return operand1;
            else
                return new Constant(new Scalar(1));
        }
    } else if (operand1 instanceof Constant) {
        Type c1 = ((Constant) operand1).value;
        if (c1 instanceof Scalar) {
            from.changed = true;
            double value = ((Scalar) c1).value;
            if (value == 0)
                return operand0;
            else
                return new Constant(new Scalar(1));
        }
    }
    return this;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Type(gov.sandia.n2a.language.Type) Constant(gov.sandia.n2a.language.Constant) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 24 with Operator

use of gov.sandia.n2a.language.Operator in project n2a by frothga.

the class Scalar method convert.

/**
 *        General utility: given a string containing a number with units, convert to the scaled SI value.
 */
public static double convert(String expression) {
    try {
        Operator op = Operator.parse(expression);
        double sign = 1;
        if (op instanceof Negate) {
            op = ((Negate) op).operand;
            sign = -1;
        }
        if (!(op instanceof Constant))
            return 0;
        Type result = ((Constant) op).value;
        if (result instanceof Scalar)
            return ((Scalar) result).value * sign;
    } catch (ParseException e) {
    }
    return 0;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Type(gov.sandia.n2a.language.Type) Constant(gov.sandia.n2a.language.Constant) Negate(gov.sandia.n2a.language.operator.Negate) ParseException(gov.sandia.n2a.language.ParseException)

Example 25 with Operator

use of gov.sandia.n2a.language.Operator in project n2a by frothga.

the class ExportJob method analyze.

/**
 *        Find references to $index in connection endpoints, and set up info for ConnectionContext.
 */
public void analyze(EquationSet s) {
    for (EquationSet p : s.parts) analyze(p);
    for (final Variable v : s.variables) {
        Visitor visitor = new Visitor() {

            public boolean visit(Operator op) {
                if (op instanceof AccessVariable) {
                    VariableReference r = ((AccessVariable) op).reference;
                    Variable rv = r.variable;
                    if (rv.container != v.container && !r.resolution.isEmpty()) {
                        Object o = r.resolution.get(r.resolution.size() - 1);
                        if (o instanceof ConnectionBinding) {
                            ConnectionBinding c = (ConnectionBinding) o;
                            // This is somewhat of a hack, but ConnectionContext assumes the mappings A->0 and B->1.
                            if (c.alias.equals("A"))
                                r.index = 0;
                            if (c.alias.equals("B"))
                                r.index = 1;
                        }
                    }
                }
                return true;
            }
        };
        v.visit(visitor);
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) Visitor(gov.sandia.n2a.language.Visitor) AccessVariable(gov.sandia.n2a.language.AccessVariable) VariableReference(gov.sandia.n2a.eqset.VariableReference) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)

Aggregations

Operator (gov.sandia.n2a.language.Operator)27 AccessVariable (gov.sandia.n2a.language.AccessVariable)17 Constant (gov.sandia.n2a.language.Constant)14 Visitor (gov.sandia.n2a.language.Visitor)11 Type (gov.sandia.n2a.language.Type)10 Scalar (gov.sandia.n2a.language.type.Scalar)10 Variable (gov.sandia.n2a.eqset.Variable)9 ArrayList (java.util.ArrayList)7 EquationSet (gov.sandia.n2a.eqset.EquationSet)6 Output (gov.sandia.n2a.language.function.Output)6 Text (gov.sandia.n2a.language.type.Text)6 TreeSet (java.util.TreeSet)4 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)3 VariableReference (gov.sandia.n2a.eqset.VariableReference)3 Input (gov.sandia.n2a.language.function.Input)3 Symbol (gov.sandia.n2a.backend.xyce.netlist.Symbol)2 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)2 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)2 Function (gov.sandia.n2a.language.Function)2 ParseException (gov.sandia.n2a.language.ParseException)2