Search in sources :

Example 16 with AccessVariable

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

the class ExportJob method genericPart.

public void genericPart(MPart part, Element result, String... skip) {
    EquationSet partEquations = getEquations(part);
    List<Element> resultElements = new ArrayList<Element>();
    List<String> skipList = Arrays.asList(skip);
    for (MNode c : part) {
        MPart p = (MPart) c;
        String key = p.key();
        // Skip connection bindings. They are unpacked elsewhere.
        if (partEquations.findConnection(key) != null)
            continue;
        if (key.startsWith("$"))
            continue;
        if (skipList.contains(key))
            continue;
        if (p.isPart()) {
            genericPart(p, resultElements);
        } else {
            // We need to check two things:
            // * Has the variable been overridden after it was declared in the base part?
            // * Is it an expression or a constant?
            // An override that is an expression should trigger a LEMS extension part.
            // A constant that is either overridden or required should be emitted here.
            boolean expression = true;
            String value = p.get();
            Variable v = partEquations.find(new Variable(key));
            try {
                // This could convert an expression to a constant.
                Type evalue = v.eval(context);
                // TODO: if it is a simple AccessVariable, then it shouldn't be viewed as an expression.
                Operator op = Operator.parse(value);
                if (op instanceof Constant) {
                    // "direct" value
                    Type dvalue = ((Constant) op).value;
                    expression = !evalue.equals(dvalue);
                }
            } catch (Exception e) {
            }
            boolean overridden = p.isFromTopDocument() || isOverride(part.get("$inherit").replace("\"", ""), key);
            String name = sequencer.bestFieldName(result, p.get("$metadata", "backend.lems.param"));
            if (name.isEmpty())
                name = key;
            boolean required = sequencer.isRequired(result, name);
            if (required || (overridden && !expression)) {
                // biophysicalUnits() should return strings (non-numbers) unmodified
                result.setAttribute(name, biophysicalUnits(p.get()));
            }
        }
    }
    sequencer.append(result, resultElements);
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) MPart(gov.sandia.n2a.eqset.MPart) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) Constant(gov.sandia.n2a.language.Constant) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MNode(gov.sandia.n2a.db.MNode) IncommensurableException(javax.measure.IncommensurableException) UnconvertibleException(javax.measure.UnconvertibleException) ParseException(gov.sandia.n2a.language.ParseException) Type(gov.sandia.n2a.language.Type)

Aggregations

AccessVariable (gov.sandia.n2a.language.AccessVariable)16 Operator (gov.sandia.n2a.language.Operator)12 Variable (gov.sandia.n2a.eqset.Variable)7 Scalar (gov.sandia.n2a.language.type.Scalar)7 ArrayList (java.util.ArrayList)6 Constant (gov.sandia.n2a.language.Constant)5 Visitor (gov.sandia.n2a.language.Visitor)5 TreeSet (java.util.TreeSet)5 EquationSet (gov.sandia.n2a.eqset.EquationSet)4 Type (gov.sandia.n2a.language.Type)4 VariableReference (gov.sandia.n2a.eqset.VariableReference)3 Instance (gov.sandia.n2a.language.type.Instance)3 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)2 Transformer (gov.sandia.n2a.language.Transformer)2 Output (gov.sandia.n2a.language.function.Output)2 Text (gov.sandia.n2a.language.type.Text)2 InstanceTemporaries (gov.sandia.n2a.backend.internal.InstanceTemporaries)1 Population (gov.sandia.n2a.backend.internal.Population)1 Symbol (gov.sandia.n2a.backend.xyce.netlist.Symbol)1 XyceRenderer (gov.sandia.n2a.backend.xyce.netlist.XyceRenderer)1