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);
}
}
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();
}
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;
}
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);
}
}
}
}
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);
}
Aggregations