Search in sources :

Example 1 with NamedType

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));
        }
    }
}
Also used : ArrayType(jkind.lustre.ArrayType) NamedType(jkind.lustre.NamedType) RecordType(jkind.lustre.RecordType) Type(jkind.lustre.Type) ArrayType(jkind.lustre.ArrayType) RecordType(jkind.lustre.RecordType) BinaryExpr(jkind.lustre.BinaryExpr) RecordAccessExpr(jkind.lustre.RecordAccessExpr) ArrayAccessExpr(jkind.lustre.ArrayAccessExpr) RecordUpdateExpr(jkind.lustre.RecordUpdateExpr) ArrayUpdateExpr(jkind.lustre.ArrayUpdateExpr) ArrayExpr(jkind.lustre.ArrayExpr) Expr(jkind.lustre.Expr) RecordExpr(jkind.lustre.RecordExpr) IntExpr(jkind.lustre.IntExpr) NamedType(jkind.lustre.NamedType) BinaryExpr(jkind.lustre.BinaryExpr)

Example 2 with NamedType

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);
            }
        }
    }
}
Also used : Type(jkind.lustre.Type) NamedType(jkind.lustre.NamedType) NamedType(jkind.lustre.NamedType)

Example 3 with NamedType

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());
}
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 4 with NamedType

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;
}
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)

Example 5 with NamedType

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();
        }
    }
}
Also used : Type(jkind.lustre.Type) NamedType(jkind.lustre.NamedType) NamedType(jkind.lustre.NamedType)

Aggregations

NamedType (jkind.lustre.NamedType)6 Type (jkind.lustre.Type)6 Expr (jkind.lustre.Expr)3 RecordType (jkind.lustre.RecordType)3 IntExpr (jkind.lustre.IntExpr)2 RecordAccessExpr (jkind.lustre.RecordAccessExpr)2 RecordExpr (jkind.lustre.RecordExpr)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 MATLABPort (com.rockwellcollins.atc.agree.codegen.ast.MATLABPort)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