use of jkind.lustre.NamedType in project AGREE by loonwerks.
the class ExpressionFlattener method flattenEqualsExpression.
private void flattenEqualsExpression(final BinaryExpr expr, final List<BinaryExpr> results) {
// Assume that both sides of the binary expression are the same type
final Type leftType = expr.left.accept(typeReconstructor);
if (leftType instanceof ArrayType) {
flattenArrayEquality(expr, (ArrayType) leftType, results);
} else if (leftType instanceof RecordType) {
flattenRecordEquality(expr, (RecordType) leftType, results);
} else if (leftType instanceof NamedType) {
// Flatten both sides of the expression
final Expr newLeft = flattenExpression(expr.left);
final Expr newRight = flattenExpression(expr.right);
if (newLeft != null && newRight != null) {
results.add(new BinaryExpr(newLeft, expr.op, newRight));
}
}
}
use of jkind.lustre.NamedType in project AGREE by loonwerks.
the class VariableMap method addEquality.
private void addEquality(final Expr idExpr1, final Expr idExpr2) {
Objects.requireNonNull(idExpr1, "id1 must not be null");
Objects.requireNonNull(idExpr2, "id2 must not be null");
markVariableMapAsDirty();
final Type id1ExprType = idExpr1.accept(typeReconstructor);
if (id1ExprType instanceof NamedType) {
// Named type equality
final String id1 = idExpr1.toString();
final String id2 = idExpr2.toString();
final Variable var1 = variables.get(id1);
final Variable var2 = variables.get(id2);
if (var1 == null && var2 == null) {
final Variable newVar = new Variable((NamedType) id1ExprType);
newVar.ids.add(id1);
newVar.ids.add(id2);
variables.put(id1, newVar);
variables.put(id2, newVar);
} else if (var1 == null) {
var2.ids.add(id1);
variables.put(id1, var2);
} else if (var2 == null) {
var1.ids.add(id2);
variables.put(id2, var1);
} else {
// Decide which variable to keep
final Variable keepVar;
final Variable removeVar;
if (var1.ids.size() <= var2.ids.size()) {
keepVar = var1;
removeVar = var2;
} else {
keepVar = var2;
removeVar = var1;
}
// Merge the remove var fields into the keep var
keepVar.ids.addAll(removeVar.ids);
keepVar.relatedExpressions.addAll(removeVar.relatedExpressions);
// Update the variable map to only reference the keep var
for (final String removeId : removeVar.ids) {
variables.put(removeId, keepVar);
}
}
}
}
use of jkind.lustre.NamedType in project AGREE by loonwerks.
the class AgreeUtils method getInitValueFromType.
public static Expr getInitValueFromType(Type type) {
if (type instanceof NamedType) {
return getInitValueFromType((NamedType) type);
}
if (type instanceof RecordType) {
RecordType recordType = (RecordType) type;
Map<String, Expr> fieldMap = new HashMap<>();
for (Entry<String, Type> entry : recordType.fields.entrySet()) {
Expr subExpr = getInitValueFromType(entry.getValue());
fieldMap.put(entry.getKey(), subExpr);
}
return new RecordExpr(recordType.id, fieldMap);
}
throw new AgreeException("AGREE cannot figure out an initial type for Lustre type: " + type.getClass());
}
use of jkind.lustre.NamedType 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 jkind.lustre.NamedType in project AGREE by loonwerks.
the class VariableMap method addRelatedExpression.
private void addRelatedExpression(final Expr idExpr, final BinaryExpr relatedExpr) {
Objects.requireNonNull(relatedExpr, "relatedExpr must not be null");
final Type type = idExpr.accept(typeReconstructor);
if (type instanceof NamedType) {
// Only add the related expression if the expression can be used to solve the variable.
if ((type != NamedType.BOOL && !leftOfCurrentRelatedExpressionIsBoolean) || (type == NamedType.BOOL && leftOfCurrentRelatedExpressionIsBoolean)) {
final Variable var = getOrCreate(idExpr, (NamedType) type);
var.relatedExpressions.add(relatedExpr);
markVariableMapAsDirty();
}
}
}
Aggregations