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