use of gov.sandia.n2a.language.BuildMatrix in project n2a by frothga.
the class JobC method prepareDynamicObjects.
/**
* Build complex sub-expressions into a single local variable that can be referenced by the equation.
*/
public void prepareDynamicObjects(Operator op, final CRenderer context, final boolean init, final String pad) throws Exception {
// Pass 1 -- Strings and matrix expressions
Visitor visitor1 = new Visitor() {
public boolean visit(Operator op) {
if (op instanceof BuildMatrix) {
BuildMatrix m = (BuildMatrix) op;
int rows = m.getRows();
int cols = m.getColumns();
String matrixName = "Matrix" + matrixNames.size();
matrixNames.put(m, matrixName);
if (rows == 3 && cols == 1)
context.result.append(pad + "Vector3 " + matrixName + ";\n");
else
context.result.append(pad + "Matrix<float> " + matrixName + " (" + rows + ", " + cols + ");\n");
for (int r = 0; r < rows; r++) {
if (cols == 1) {
context.result.append(pad + matrixName + "[" + r + "] = ");
m.operands[0][r].render(context);
context.result.append(";\n");
} else {
for (int c = 0; c < cols; c++) {
context.result.append(pad + matrixName + "(" + r + "," + c + ") = ");
m.operands[c][r].render(context);
context.result.append(";\n");
}
}
}
return false;
}
if (op instanceof Add) {
Add a = (Add) op;
String stringName = stringNames.get(a);
if (stringName != null) {
context.result.append(pad + "String " + stringName + ";\n");
for (Operator o : flattenAdd(a)) {
context.result.append(pad + stringName + " += ");
o.render(context);
context.result.append(";\n");
}
return false;
}
}
return true;
}
};
op.visit(visitor1);
// Pass 2 -- Input functions
Visitor visitor2 = new Visitor() {
public boolean visit(Operator op) {
if (op instanceof ReadMatrix) {
ReadMatrix r = (ReadMatrix) op;
if (!(r.operands[0] instanceof Constant)) {
String matrixName = matrixNames.get(r);
String stringName = stringNames.get(r.operands[0]);
context.result.append(pad + "MatrixInput * " + matrixName + " = matrixHelper (" + stringName + ");\n");
}
return false;
}
if (op instanceof Input) {
Input i = (Input) op;
if (!(i.operands[0] instanceof Constant)) {
String inputName = inputNames.get(i);
String stringName = stringNames.get(i.operands[0]);
context.result.append(pad + "InputHolder * " + inputName + " = inputHelper (" + stringName + ");\n");
if (!context.global) {
context.result.append(pad + inputName + "->epsilon = " + resolve(context.bed.dt.reference, context, false) + " / 1000;\n");
}
// Detect time flag
String mode = "";
if (i.operands.length > 3) {
// just assuming it's a constant string
mode = i.operands[3].toString();
} else if (i.operands[1] instanceof Constant) {
Constant c = (Constant) i.operands[1];
if (c.value instanceof Text)
mode = c.toString();
}
if (mode.contains("time")) {
context.result.append(pad + inputName + "->time = true;\n");
}
}
// I/O functions can be nested
return true;
}
if (op instanceof Output) {
Output o = (Output) op;
if (!(o.operands[0] instanceof Constant)) {
String outputName = outputNames.get(o);
String stringName = stringNames.get(o.operands[0]);
context.result.append(pad + "OutputHolder * " + outputName + " = outputHelper (" + stringName + ");\n");
// Detect raw flag
if (o.operands.length > 3) {
Operator op3 = o.operands[3];
if (op3 instanceof Constant) {
if (op3.toString().contains("raw")) {
context.result.append(pad + outputName + "->raw = true;\n");
}
}
}
}
return true;
}
return true;
}
};
op.visit(visitor2);
}
Aggregations