use of com.rockwellcollins.atc.agree.codegen.ast.MATLABType in project AGREE by loonwerks.
the class LustreToMATLABExprVisitor method visit.
@Override
public MATLABExpr visit(RecordExpr e) {
// Create a local variable with the record type id as prefix
localVarIndex++;
String localVarName = updateName(e.id + "_var" + localVarIndex, "");
// add assignment to assign the fields of the variable
// to the value specified in the RecordExpr
SortedMap<String, MATLABExpr> fields = new TreeMap<>(new StringNaturalOrdering());
Iterator<Entry<String, Expr>> iterator = e.fields.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, Expr> entry = iterator.next();
// get the type for the field
MATLABType type = recordTypeMap.get(e.id).get(updateName(entry.getKey(), e.id));
// conduct explicit type cast if a field value is a constant of double type or int type
MATLABTypeCastExprVisitor typeCastVisitor = new MATLABTypeCastExprVisitor(type);
MATLABExpr fieldExpr = typeCastVisitor.visit(entry.getValue().accept(this));
fields.put(updateName(entry.getKey(), e.id), fieldExpr);
}
localBusVarInits.add(new MATLABLocalBusVarInit(localVarName, null, fields));
// In the expression that uses the RecordExpr, just reference the local variable
return new MATLABIdExpr(localVarName);
}
use of com.rockwellcollins.atc.agree.codegen.ast.MATLABType in project AGREE by loonwerks.
the class LustreToMATLABExprVisitor method visit.
@Override
public MATLABExpr visit(UnaryExpr e) {
MATLABExpr expr = e.expr.accept(this);
String opName = e.op.name();
MATLABUnaryOp op = MATLABUnaryOp.fromName(opName);
if (op == null) {
if (opName.equals("PRE")) {
if (expr instanceof MATLABIdExpr) {
// function call for the following AGREE unary operator
// PRE ("pre");
String varName = ((MATLABIdExpr) expr).id;
String preVarName = updateName("pre_" + varName, "");
// no duplicate addition
if (!persistentVarMap.containsKey(preVarName)) {
// init based on varName
if (inputSet.contains(varName)) {
persistentVarInits.add(new MATLABPreInputVarInit(preVarName, varName));
persistentVarMap.put(preVarName, new MATLABIdExpr(varName));
} else // if the var is a local variable
// the init needs to be associated with a default value of the type
// instead of varName, as the varName may be assigned in the same equation
// the preVar first gets used
{
// add preVar init based on default value of the type
if (localVarTypeMap.containsKey(varName)) {
MATLABType type = localVarTypeMap.get(varName);
persistentVarInits.add(new MATLABPreLocalVarInit(preVarName, new MATLABTypeInitExpr(type)));
persistentVarMap.put(preVarName, new MATLABIdExpr(varName));
} else {
throw new IllegalArgumentException();
}
}
}
return new MATLABIdExpr(preVarName);
} else {
throw new IllegalArgumentException();
}
} else {
throw new IllegalArgumentException();
}
} else {
return new MATLABUnaryExpr(op, expr);
}
}
use of com.rockwellcollins.atc.agree.codegen.ast.MATLABType in project AGREE by loonwerks.
the class LustreToMATLABExprVisitor method visit.
@Override
public MATLABExpr visit(RecordUpdateExpr e) {
MATLABIdExpr recordIdExpr = (MATLABIdExpr) e.record.accept(this);
// Assign the specific field of the variable created from the recordExpr associated with it
// to the value specified in the RecordUpdateExpr
SortedMap<String, MATLABExpr> fields = new TreeMap<>(new StringNaturalOrdering());
// get the type for the field
Expr curExpr = e;
while (curExpr instanceof RecordUpdateExpr) {
curExpr = ((RecordUpdateExpr) curExpr).record;
}
MATLABType type = null;
String recordName = "";
if (curExpr instanceof RecordExpr) {
recordName = ((RecordExpr) curExpr).id;
if (recordTypeMap.get(recordName) != null) {
String fieldName = e.field;
fieldName = updateName(fieldName, recordName);
type = recordTypeMap.get(recordName).get(fieldName);
}
} else {
recordName = recordIdExpr.id;
}
// conduct explicit type cast if a field value is a constant of double type or int type
MATLABTypeCastExprVisitor typeCastVisitor = new MATLABTypeCastExprVisitor(type);
MATLABExpr fieldExpr = typeCastVisitor.visit(e.value.accept(this));
fields.put(updateName(e.field, recordName), fieldExpr);
String originalVar = null;
if (e.record instanceof IdExpr) {
originalVar = updateName(e.record.toString(), "");
} else {
originalVar = updateName(recordIdExpr.id.split("_var")[0] + "_var" + localVarIndex, "");
}
localVarIndex++;
String newVar = updateName(recordIdExpr.id.split("_var")[0] + "_var" + localVarIndex, "");
localBusVarInits.add(new MATLABLocalBusVarInit(originalVar, newVar, fields));
// In the expression that uses the RecordUpdateExpr, just reference the variable
return new MATLABIdExpr(newVar);
}
use of com.rockwellcollins.atc.agree.codegen.ast.MATLABType in project AGREE by loonwerks.
the class LustreToMATLABTranslator method translate.
public static MATLABPrimaryFunction translate(Node lustreNode, AgreeProgram agreeProgram) {
IPreferenceStore prefs = Activator.getDefault().getPreferenceStore();
intTypeStr = prefs.getString(PreferenceConstants.PREF_INT);
realTypeStr = prefs.getString(PreferenceConstants.PREF_REAL);
List<MATLABIdExpr> inputs = new ArrayList<>();
List<MATLABStatement> statements = new ArrayList<>();
List<MATLABFunction> functions = new ArrayList<>();
List<MATLABPersistentVarDecl> persistentVarDecl = new ArrayList<>();
List<MATLABPort> ports = new ArrayList<>();
LustreToMATLABExprVisitor exprVisitor = new LustreToMATLABExprVisitor();
LustreToMATLABTypeVisitor typeVisitor = new LustreToMATLABTypeVisitor();
// get function name
String functionName = "check_" + lustreNode.id;
// add record types
for (Type type : agreeProgram.globalTypes) {
if (type instanceof RecordType) {
RecordType recordType = (RecordType) type;
SortedMap<String, MATLABType> fields = new TreeMap<>(new StringNaturalOrdering());
Iterator<Entry<String, Type>> iterator = recordType.fields.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, Type> entry = iterator.next();
fields.put(exprVisitor.updateName(entry.getKey(), recordType.id), entry.getValue().accept(typeVisitor));
}
exprVisitor.recordTypeMap.put(recordType.id, fields);
}
}
// add input and output ports for subsystem to verify
for (VarDecl inputVar : lustreNode.inputs) {
String varName = null;
if (!inputVar.id.equals("time")) {
// update name
varName = exprVisitor.updateName(inputVar.id, "");
// add inputs
inputs.add(new MATLABIdExpr(varName));
// translate the Lustre expression to MATLAB expression
// add input Ids to inputList of the exprVisitor
// to help identify local variables
exprVisitor.inputSet.add(inputVar.id);
// get inputVar and type
AgreeVar agreeVar = (AgreeVar) inputVar;
Type type = agreeVar.type;
if (type instanceof RecordType || type instanceof NamedType) {
MATLABType portType = (agreeVar.type).accept(typeVisitor);
SortedMap<String, MATLABType> fields = null;
// get the fields if it is a RecordType
if (type instanceof RecordType) {
fields = exprVisitor.recordTypeMap.get(((RecordType) type).id);
}
// if it is from AADL feature, get the feature instance
if (agreeVar.featInst != null) {
FeatureInstance featInst = agreeVar.featInst;
ports.add(new MATLABPort(featInst.getName(), featInst.getDirection().getName(), portType, fields));
} else // if it is not from AADL feature, but an eq variable from
// AGREE
// set them as output variables from the subsystem
{
ports.add(new MATLABPort(inputVar.id, "out", portType, fields));
}
}
}
}
// add local variable and their types
for (VarDecl localVar : lustreNode.locals) {
// get local var Name and Type
exprVisitor.localVarTypeMap.put(exprVisitor.updateName(localVar.id, ""), localVar.type.accept(typeVisitor));
}
// translate equations to assignments
if (!lustreNode.equations.isEmpty()) {
for (Equation equation : lustreNode.equations) {
// get the variable to assign
String varId = exprVisitor.updateName(equation.lhs.get(0).id, "");
MATLABIdExpr varToAssign = new MATLABIdExpr(varId);
// get the type for the local variable
// MATLABType type = exprVisitor.localVarTypeMap.get(varId);
// translate expressions
MATLABExpr expr = exprVisitor.visit(equation.expr);
// conduct explicit type cast if it's a constant of double type or int type
// no need to type cast for assignment from an input variable
// or operations (including functions) involving known types
MATLABTypeCastExprVisitor typeCastVisitor = new MATLABTypeCastExprVisitor();
expr = typeCastVisitor.visit(expr);
// add any new preVar init from exprVisitor
Iterator<MATLABPersistentVarInit> persistentVarInitIterator = exprVisitor.persistentVarInits.iterator();
while (persistentVarInitIterator.hasNext()) {
MATLABPersistentVarInit persistentVarInit = persistentVarInitIterator.next();
// add new preVar init to the statements before the assignment
statements.add(persistentVarInit);
// remove the new preVar init from exprVisitor
persistentVarInitIterator.remove();
}
// add any new local Bus var init from exprVisitor
Iterator<MATLABLocalBusVarInit> busVarInitIterator = exprVisitor.localBusVarInits.iterator();
while (busVarInitIterator.hasNext()) {
MATLABLocalBusVarInit busVarInit = busVarInitIterator.next();
// add new local Bus var init to the statements before the assignment
statements.add(busVarInit);
// remove the new local Bus var init from exprVisitor
busVarInitIterator.remove();
}
// add assignment
MATLABAssignment assignment = new MATLABAssignment(varToAssign, expr);
statements.add(assignment);
}
}
// add persistentVar decl and assignments
Iterator<Entry<String, MATLABExpr>> it = exprVisitor.persistentVarMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, MATLABExpr> pair = it.next();
String varToAssign = pair.getKey();
MATLABExpr expr = pair.getValue();
statements.add(new MATLABAssignment(new MATLABIdExpr(varToAssign), expr));
persistentVarDecl.add(new MATLABPersistentVarDecl(varToAssign));
}
// translate assertions
for (Expr assertionExpr : lustreNode.assertions) {
MATLABExpr expr = exprVisitor.visit(assertionExpr);
// add assertions
MATLABAssumption assumption = new MATLABAssumption(expr);
statements.add(assumption);
}
// translate properties
for (String propertyStr : lustreNode.properties) {
propertyStr = exprVisitor.updateName(propertyStr, "");
MATLABProperty property = new MATLABProperty(propertyStr);
statements.add(property);
}
// add definitions for the functions that have been called
for (Map.Entry<String, MATLABFunction> functionEntry : exprVisitor.functionMap.entrySet()) {
MATLABFunction function = functionEntry.getValue();
if (function.functionCalled) {
functions.add(function);
}
}
// Create primary function AST
MATLABPrimaryFunction primaryFunction = new MATLABPrimaryFunction(functionName, inputs, persistentVarDecl, statements, functions, ports);
return primaryFunction;
}
use of com.rockwellcollins.atc.agree.codegen.ast.MATLABType in project AGREE by loonwerks.
the class LustreToMATLABExprVisitor method visit.
@Override
public MATLABExpr visit(BinaryExpr e) {
MATLABExpr leftExpr = e.left.accept(this);
String opName = e.op.name();
String opFuncStr = e.op.toString();
MATLABBinaryOp op = MATLABBinaryOp.fromName(opName);
MATLABExpr rightExpr = e.right.accept(this);
if (op == null) {
if (opName.equals("INT_DIVIDE")) {
MATLABType type = null;
switch(LustreToMATLABTranslator.intTypeStr) {
case PreferenceConstants.INT_INT8:
type = new MATLABInt8Type();
break;
case PreferenceConstants.INT_UINT8:
type = new MATLABUInt8Type();
break;
case PreferenceConstants.INT_INT16:
type = new MATLABInt16Type();
break;
case PreferenceConstants.INT_UINT16:
type = new MATLABUInt16Type();
break;
case PreferenceConstants.INT_INT32:
type = new MATLABInt32Type();
break;
case PreferenceConstants.INT_UINT32:
type = new MATLABUInt32Type();
break;
case PreferenceConstants.INT_INT64:
type = new MATLABInt64Type();
break;
case PreferenceConstants.INT_UINT64:
type = new MATLABUInt64Type();
break;
default:
throw new IllegalArgumentException("Unknown int type: " + LustreToMATLABTranslator.intTypeStr);
}
MATLABTypeCastExpr castLeftExpr = new MATLABTypeCastExpr(type, leftExpr);
MATLABTypeCastExpr castRightExpr = new MATLABTypeCastExpr(type, rightExpr);
MATLABBinaryOp castOp = MATLABBinaryOp.fromName("DIVIDE");
return new MATLABBinaryExpr(castLeftExpr, castOp, castRightExpr);
} else {
String functionName = null;
if (opName.equals("IMPLIES")) {
functionName = "impliesFunction";
// mark that this function is getting called
functionMap.get(functionName).functionCalled = true;
return new MATLABBinaryFunctionCall(functionMap.get(functionName).name, leftExpr, rightExpr);
} else if (opName.equals("ARROW")) {
functionName = "arrowFunction";
// mark that this function is getting called
functionMap.get(functionName).functionCalled = true;
String firstTimeVar = ((MATLABArrowFunction) functionMap.get(functionName)).firstTimeVar;
// no duplicate addition
if (!persistentVarMap.containsKey(firstTimeVar)) {
persistentVarMap.put(firstTimeVar, new MATLABBoolExpr(false));
persistentVarInits.add(new MATLABFirstTimeVarInit(firstTimeVar));
}
return new MATLABArrowFunctionCall(functionMap.get(functionName).name, firstTimeVar, leftExpr, rightExpr);
} else if (opName.equals("EQUAL")) {
return new MATLABBinaryFunctionCall("isequal", leftExpr, rightExpr);
} else {
return new MATLABBinaryFunctionCall(opFuncStr, leftExpr, rightExpr);
}
}
} else {
return new MATLABBinaryExpr(leftExpr, op, rightExpr);
}
}
Aggregations