Search in sources :

Example 1 with MATLABPort

use of com.rockwellcollins.atc.agree.codegen.ast.MATLABPort in project AGREE by loonwerks.

the class MdlScriptCreator method preparePorts.

private void preparePorts() {
    for (MATLABPort port : ports) {
        if (port.direction.equals("in")) {
            inportnames.add(port.name);
            inporttypes.add(portTypeNameUpdate(port.type));
        } else {
            outportnames.add(port.name);
            outporttypes.add(portTypeNameUpdate(port.type));
        }
        // add the bus def to busdefs (no redundant bus defs)
        if (port.fields != null) {
            busdefs.put(portTypeNameUpdate(port.type), port.fields);
        }
    }
}
Also used : MATLABPort(com.rockwellcollins.atc.agree.codegen.ast.MATLABPort)

Example 2 with MATLABPort

use of com.rockwellcollins.atc.agree.codegen.ast.MATLABPort 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;
}
Also used : MATLABPersistentVarInit(com.rockwellcollins.atc.agree.codegen.ast.MATLABPersistentVarInit) FeatureInstance(org.osate.aadl2.instance.FeatureInstance) NamedType(jkind.lustre.NamedType) ArrayList(java.util.ArrayList) MATLABAssignment(com.rockwellcollins.atc.agree.codegen.ast.MATLABAssignment) Entry(java.util.Map.Entry) RecordType(jkind.lustre.RecordType) StringNaturalOrdering(jkind.util.StringNaturalOrdering) MATLABPersistentVarDecl(com.rockwellcollins.atc.agree.codegen.ast.MATLABPersistentVarDecl) VarDecl(jkind.lustre.VarDecl) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr) LustreToMATLABExprVisitor(com.rockwellcollins.atc.agree.codegen.visitors.LustreToMATLABExprVisitor) MATLABLocalBusVarInit(com.rockwellcollins.atc.agree.codegen.ast.MATLABLocalBusVarInit) MATLABTypeCastExprVisitor(com.rockwellcollins.atc.agree.codegen.visitors.MATLABTypeCastExprVisitor) MATLABPersistentVarDecl(com.rockwellcollins.atc.agree.codegen.ast.MATLABPersistentVarDecl) MATLABExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr) LustreToMATLABTypeVisitor(com.rockwellcollins.atc.agree.codegen.visitors.LustreToMATLABTypeVisitor) MATLABPort(com.rockwellcollins.atc.agree.codegen.ast.MATLABPort) Equation(jkind.lustre.Equation) TreeMap(java.util.TreeMap) MATLABPrimaryFunction(com.rockwellcollins.atc.agree.codegen.ast.MATLABPrimaryFunction) AgreeVar(com.rockwellcollins.atc.agree.analysis.ast.AgreeVar) MATLABType(com.rockwellcollins.atc.agree.codegen.ast.MATLABType) MATLABAssumption(com.rockwellcollins.atc.agree.codegen.ast.MATLABAssumption) RecordType(jkind.lustre.RecordType) Type(jkind.lustre.Type) NamedType(jkind.lustre.NamedType) MATLABType(com.rockwellcollins.atc.agree.codegen.ast.MATLABType) MATLABIdExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr) MATLABExpr(com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr) Expr(jkind.lustre.Expr) MATLABStatement(com.rockwellcollins.atc.agree.codegen.ast.MATLABStatement) MATLABFunction(com.rockwellcollins.atc.agree.codegen.ast.MATLABFunction) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) MATLABProperty(com.rockwellcollins.atc.agree.codegen.ast.MATLABProperty)

Aggregations

MATLABPort (com.rockwellcollins.atc.agree.codegen.ast.MATLABPort)2 AgreeVar (com.rockwellcollins.atc.agree.analysis.ast.AgreeVar)1 MATLABAssignment (com.rockwellcollins.atc.agree.codegen.ast.MATLABAssignment)1 MATLABAssumption (com.rockwellcollins.atc.agree.codegen.ast.MATLABAssumption)1 MATLABFunction (com.rockwellcollins.atc.agree.codegen.ast.MATLABFunction)1 MATLABLocalBusVarInit (com.rockwellcollins.atc.agree.codegen.ast.MATLABLocalBusVarInit)1 MATLABPersistentVarDecl (com.rockwellcollins.atc.agree.codegen.ast.MATLABPersistentVarDecl)1 MATLABPersistentVarInit (com.rockwellcollins.atc.agree.codegen.ast.MATLABPersistentVarInit)1 MATLABPrimaryFunction (com.rockwellcollins.atc.agree.codegen.ast.MATLABPrimaryFunction)1 MATLABProperty (com.rockwellcollins.atc.agree.codegen.ast.MATLABProperty)1 MATLABStatement (com.rockwellcollins.atc.agree.codegen.ast.MATLABStatement)1 MATLABType (com.rockwellcollins.atc.agree.codegen.ast.MATLABType)1 MATLABExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABExpr)1 MATLABIdExpr (com.rockwellcollins.atc.agree.codegen.ast.expr.MATLABIdExpr)1 LustreToMATLABExprVisitor (com.rockwellcollins.atc.agree.codegen.visitors.LustreToMATLABExprVisitor)1 LustreToMATLABTypeVisitor (com.rockwellcollins.atc.agree.codegen.visitors.LustreToMATLABTypeVisitor)1 MATLABTypeCastExprVisitor (com.rockwellcollins.atc.agree.codegen.visitors.MATLABTypeCastExprVisitor)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1