Search in sources :

Example 36 with Operator

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

the class Variable method deepCopy.

/**
 *        Replaces each variable in a given subset, such that the result can be
 *        modified by optimization procedures without damaging the original equation set.
 */
public static void deepCopy(List<Variable> list) {
    // the same members as the original variables.
    try {
        for (int i = 0; i < list.size(); i++) list.set(i, (Variable) list.get(i).clone());
    } catch (CloneNotSupportedException e) {
    }
    // optimization procedures.
    for (Variable v : list) {
        TreeSet<EquationEntry> newEquations = new TreeSet<EquationEntry>();
        for (EquationEntry e : v.equations) newEquations.add(e.deepCopy(v));
        v.equations = newEquations;
        if (v.attributes != null)
            v.attributes = new HashSet<String>(v.attributes);
        v.usedBy = null;
        v.uses = null;
    }
    // External dependencies won't be counted, but also won't be changed.
    class DependencyTransformer implements Transformer {

        public Variable v;

        public Operator transform(Operator op) {
            if (op instanceof AccessVariable) {
                AccessVariable av = (AccessVariable) op;
                Variable listVariable = EquationSet.find(av.reference.variable, list);
                if (listVariable != null) {
                    av.reference = new VariableReference();
                    av.reference.variable = listVariable;
                    v.addDependencyOn(listVariable);
                    return av;
                }
            }
            return null;
        }
    }
    DependencyTransformer xform = new DependencyTransformer();
    for (Variable v : list) {
        xform.v = v;
        v.transform(xform);
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) AccessVariable(gov.sandia.n2a.language.AccessVariable) Transformer(gov.sandia.n2a.language.Transformer) AccessVariable(gov.sandia.n2a.language.AccessVariable) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet)

Example 37 with Operator

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

the class Log method determineExponentNext.

public void determineExponentNext() {
    Operator op = operands[0];
    op.exponentNext = op.exponent;
    op.determineExponentNext();
}
Also used : Operator(gov.sandia.n2a.language.Operator)

Example 38 with Operator

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

the class Output method isStringExpression.

/**
 *        Determines if the given operator is a string expression.
 *        This is the case if any operator it depends on is a string.
 *        If an operand is a variable, then its equations must contain a string.
 */
public static boolean isStringExpression(Variable v, Operator op) {
    class StringVisitor implements Visitor {

        boolean foundString;

        Variable from;

        public StringVisitor(Variable from) {
            this.from = from;
            from.visited = null;
        }

        public boolean visit(Operator op) {
            if (op instanceof Constant) {
                Constant c = (Constant) op;
                if (c.value instanceof Text)
                    foundString = true;
                return false;
            }
            if (op instanceof AccessVariable) {
                AccessVariable av = (AccessVariable) op;
                Variable v = av.reference.variable;
                // Prevent infinite recursion
                Variable p = from;
                while (p != null) {
                    if (p == v)
                        return false;
                    p = p.visited;
                }
                v.visited = from;
                from = v;
                v.visit(this);
                from = v.visited;
                return false;
            }
            // no reason to dig any further
            if (foundString)
                return false;
            // Add is the only operator that can propagate string values. All other operators and functions return scalars or matrices.
            return op instanceof Add;
        }
    }
    StringVisitor visitor = new StringVisitor(v);
    op.visit(visitor);
    return visitor.foundString;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Add(gov.sandia.n2a.language.operator.Add) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) Text(gov.sandia.n2a.language.type.Text)

Example 39 with Operator

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

the class Output method determineVariableName.

// This method should be called by analysis, with v set to the variable that holds this equation.
public void determineVariableName(Variable v) {
    // Ensure that the first operand is a file name.
    // If no file name is specified, stdout is used. We get the same effect by specifying the empty string, so insert it here.
    // This makes parameter processing much simpler elsewhere.
    int length = operands.length;
    if (length > 0 && !isStringExpression(v, operands[0])) {
        variableName = variableName0;
        Operator[] newOperands = new Operator[length + 1];
        for (int i = 0; i < length; i++) newOperands[i + 1] = operands[i];
        newOperands[0] = new Constant(new Text());
        operands = newOperands;
    } else {
        variableName = variableName1;
    }
    hasColumnName = operands.length > 2 && !(operands[2] instanceof Constant && ((Constant) operands[2]).value.toString().isEmpty());
    if (// Column name not specified
    !hasColumnName) {
        if (variableName == null)
            variableName = v.nameString();
        EquationSet container = v.container;
        if (// regular part
        container.connectionBindings == null) {
            dependOnIndex(v, container);
        } else // connection
        {
            // depend on all endpoints
            for (ConnectionBinding c : container.connectionBindings) {
                dependOnIndex(v, c.endpoint);
            }
        }
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) Constant(gov.sandia.n2a.language.Constant) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding) Text(gov.sandia.n2a.language.type.Text)

Example 40 with Operator

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

the class Output method determineExponent.

/**
 *        Depends on determineVariableName() to ensure that file name is in operands[0].
 */
public void determineExponent(ExponentContext context) {
    Operator op = operands[1];
    op.determineExponent(context);
    // In case column index is computed.
    if (operands.length > 2)
        operands[2].determineExponent(context);
    updateExponent(context, op.exponent, op.center);
}
Also used : Operator(gov.sandia.n2a.language.Operator)

Aggregations

Operator (gov.sandia.n2a.language.Operator)104 AccessVariable (gov.sandia.n2a.language.AccessVariable)41 Constant (gov.sandia.n2a.language.Constant)37 Visitor (gov.sandia.n2a.language.Visitor)26 Variable (gov.sandia.n2a.eqset.Variable)17 ArrayList (java.util.ArrayList)16 Type (gov.sandia.n2a.language.Type)14 Scalar (gov.sandia.n2a.language.type.Scalar)14 Output (gov.sandia.n2a.language.function.Output)13 EquationSet (gov.sandia.n2a.eqset.EquationSet)12 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)10 Input (gov.sandia.n2a.language.function.Input)10 Text (gov.sandia.n2a.language.type.Text)10 Matrix (gov.sandia.n2a.language.type.Matrix)9 TreeSet (java.util.TreeSet)9 ReadMatrix (gov.sandia.n2a.language.function.ReadMatrix)8 Add (gov.sandia.n2a.language.operator.Add)8 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)7 Event (gov.sandia.n2a.language.function.Event)7 Instance (gov.sandia.n2a.language.type.Instance)7