Search in sources :

Example 1 with RecordExpr

use of jkind.lustre.RecordExpr 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());
}
Also used : RecordType(jkind.lustre.RecordType) EnumType(jkind.lustre.EnumType) ComponentType(org.osate.aadl2.ComponentType) Type(jkind.lustre.Type) NamedType(jkind.lustre.NamedType) RecordType(jkind.lustre.RecordType) RecordAccessExpr(jkind.lustre.RecordAccessExpr) Expr(jkind.lustre.Expr) IntExpr(jkind.lustre.IntExpr) RecordExpr(jkind.lustre.RecordExpr) BoolExpr(jkind.lustre.BoolExpr) RealExpr(jkind.lustre.RealExpr) IdExpr(jkind.lustre.IdExpr) HashMap(java.util.HashMap) NamedType(jkind.lustre.NamedType) RecordExpr(jkind.lustre.RecordExpr)

Example 2 with RecordExpr

use of jkind.lustre.RecordExpr 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);
}
Also used : MATLABLocalBusVarInit(com.rockwellcollins.atc.agree.codegen.ast.MATLABLocalBusVarInit) MATLABExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr) TreeMap(java.util.TreeMap) MATLABType(com.rockwellcollins.atc.agree.codegen.ast.MATLABType) Entry(java.util.Map.Entry) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr) MATLABTypeInitExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeInitExpr) RecordAccessExpr(jkind.lustre.RecordAccessExpr) TupleExpr(jkind.lustre.TupleExpr) UnaryExpr(jkind.lustre.UnaryExpr) MATLABUnaryExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABUnaryExpr) RecordUpdateExpr(jkind.lustre.RecordUpdateExpr) CondactExpr(jkind.lustre.CondactExpr) MATLABBinaryExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBinaryExpr) MATLABExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr) Expr(jkind.lustre.Expr) CastExpr(jkind.lustre.CastExpr) IntExpr(jkind.lustre.IntExpr) MATLABDoubleExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABDoubleExpr) MATLABBusElementExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBusElementExpr) MATLABTypeCastExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeCastExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) RecordExpr(jkind.lustre.RecordExpr) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) RealExpr(jkind.lustre.RealExpr) MATLABBoolExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBoolExpr) ArrayAccessExpr(jkind.lustre.ArrayAccessExpr) ArrayExpr(jkind.lustre.ArrayExpr) MATLABIntExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIntExpr) MATLABArrayAccessExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABArrayAccessExpr) IdExpr(jkind.lustre.IdExpr) ArrayUpdateExpr(jkind.lustre.ArrayUpdateExpr) FunctionCallExpr(jkind.lustre.FunctionCallExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) StringNaturalOrdering(jkind.util.StringNaturalOrdering) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr)

Example 3 with RecordExpr

use of jkind.lustre.RecordExpr 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);
}
Also used : MATLABLocalBusVarInit(com.rockwellcollins.atc.agree.codegen.ast.MATLABLocalBusVarInit) MATLABExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr) IdExpr(jkind.lustre.IdExpr) TreeMap(java.util.TreeMap) MATLABType(com.rockwellcollins.atc.agree.codegen.ast.MATLABType) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr) MATLABTypeInitExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeInitExpr) RecordAccessExpr(jkind.lustre.RecordAccessExpr) TupleExpr(jkind.lustre.TupleExpr) UnaryExpr(jkind.lustre.UnaryExpr) MATLABUnaryExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABUnaryExpr) RecordUpdateExpr(jkind.lustre.RecordUpdateExpr) CondactExpr(jkind.lustre.CondactExpr) MATLABBinaryExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBinaryExpr) MATLABExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr) Expr(jkind.lustre.Expr) CastExpr(jkind.lustre.CastExpr) IntExpr(jkind.lustre.IntExpr) MATLABDoubleExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABDoubleExpr) MATLABBusElementExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBusElementExpr) MATLABTypeCastExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeCastExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) RecordExpr(jkind.lustre.RecordExpr) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) RealExpr(jkind.lustre.RealExpr) MATLABBoolExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBoolExpr) ArrayAccessExpr(jkind.lustre.ArrayAccessExpr) ArrayExpr(jkind.lustre.ArrayExpr) MATLABIntExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIntExpr) MATLABArrayAccessExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABArrayAccessExpr) IdExpr(jkind.lustre.IdExpr) ArrayUpdateExpr(jkind.lustre.ArrayUpdateExpr) FunctionCallExpr(jkind.lustre.FunctionCallExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) StringNaturalOrdering(jkind.util.StringNaturalOrdering) RecordUpdateExpr(jkind.lustre.RecordUpdateExpr) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr) RecordExpr(jkind.lustre.RecordExpr)

Example 4 with RecordExpr

use of jkind.lustre.RecordExpr in project AGREE by loonwerks.

the class ExpressionFlattener method flattenRecordExpression.

private static void flattenRecordExpression(final Expr expr, final RecordType recordType, final Map<String, Expr> results) {
    if (expr instanceof RecordUpdateExpr) {
        final RecordUpdateExpr updateExpr = (RecordUpdateExpr) expr;
        // Handle base record
        flattenRecordExpression(updateExpr.record, recordType, results);
        // Handle the update
        results.put(updateExpr.field, updateExpr.value);
    } else if (expr instanceof RecordExpr) {
        final RecordExpr recordExpr = (RecordExpr) expr;
        results.putAll(recordExpr.fields);
    } else {
        // IdExpr
        for (final String field : recordType.fields.keySet()) {
            results.put(field, new RecordAccessExpr(expr, field));
        }
    }
}
Also used : RecordAccessExpr(jkind.lustre.RecordAccessExpr) RecordUpdateExpr(jkind.lustre.RecordUpdateExpr) RecordExpr(jkind.lustre.RecordExpr)

Aggregations

RecordAccessExpr (jkind.lustre.RecordAccessExpr)4 RecordExpr (jkind.lustre.RecordExpr)4 BoolExpr (jkind.lustre.BoolExpr)3 Expr (jkind.lustre.Expr)3 IdExpr (jkind.lustre.IdExpr)3 IntExpr (jkind.lustre.IntExpr)3 RealExpr (jkind.lustre.RealExpr)3 RecordUpdateExpr (jkind.lustre.RecordUpdateExpr)3 MATLABLocalBusVarInit (com.rockwellcollins.atc.agree.codegen.ast.MATLABLocalBusVarInit)2 MATLABType (com.rockwellcollins.atc.agree.codegen.ast.MATLABType)2 MATLABArrayAccessExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABArrayAccessExpr)2 MATLABBinaryExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBinaryExpr)2 MATLABBoolExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBoolExpr)2 MATLABBusElementExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABBusElementExpr)2 MATLABDoubleExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABDoubleExpr)2 MATLABExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr)2 MATLABIdExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr)2 MATLABIntExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIntExpr)2 MATLABTypeCastExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeCastExpr)2 MATLABTypeInitExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABTypeInitExpr)2