Search in sources :

Example 6 with RecordType

use of jkind.lustre.RecordType in project AMASE by loonwerks.

the class AsymFaultASTBuilder method createEquationsForCommNode.

/**
 * Creates equation between fault nominal output and input and an equation
 * regarding the comm node output and the fault node output.
 *
 * @param newNode NodeBuilder for comm node that needs these equations added.
 * @param faults List of faults to traverse for this comm node.
 * @param type The type on the agree node output.
 * @param nodeName The name of this comm node.
 * @return NodeBuilder with these equations added.
 */
private NodeBuilder createEquationsForCommNode(NodeBuilder newNode, List<Fault> faults, Type type, String nodeName) {
    // assign __GUARANTEE0 : fault__nominal__output = input
    // Simple case is when it is bool, real. Otherwise if it is a record
    // type, we need to access the field of the fault node input and append
    // that to the expression (i.e. __fault__nominal__output.VAL = input.VAL)
    String dotField = "";
    if (type instanceof RecordType) {
        for (Expr faultOutput : faults.get(0).faultOutputMap.keySet()) {
            if (faultOutput instanceof RecordAccessExpr) {
                RecordAccessExpr rac = (RecordAccessExpr) faultOutput;
                dotField = "." + rac.field;
            }
        }
    }
    IdExpr faultNominalOut = new IdExpr("__fault__nominal__output" + dotField);
    Expr binEx = new BinaryExpr(faultNominalOut, BinaryOp.EQUAL, new IdExpr("input" + dotField));
    IdExpr guar = new IdExpr("__GUARANTEE0");
    // This links fault nominal with node input :
    // assert (__fault__nominal__output.NODE_VAL = input.NODE_VAL)
    newNode.addEquation(guar, binEx);
    BinaryExpr binAssumeAndTrue = super.createAssumeHistStmt(guar);
    Expr assertStmt = null;
    IdExpr output = null;
    // If record type, need to reference "output.VAL"
    if (type instanceof RecordType) {
        output = new IdExpr("output" + dotField);
    } else {
        output = new IdExpr("output");
    }
    // Go through trigger list and add a nested if-then-else stmt
    // that enforces mutual exclusivity.
    Expr nested = createNestedIfThenElseExpr(triggerList, new IdExpr("__fault__nominal__output"), 0);
    BinaryExpr outputEqualsValout = new BinaryExpr(output, BinaryOp.EQUAL, nested);
    BinaryExpr finalExpr1 = new BinaryExpr(binAssumeAndTrue, BinaryOp.AND, outputEqualsValout);
    // Before finishing the assert, check to see if we have safetyEqAsserts in the fault
    // and add those to the finalExpr with "and"
    assertStmt = addSafetyEqStmts(newNode, faults, new BoolExpr(true));
    BinaryExpr finalExpr2 = new BinaryExpr(finalExpr1, BinaryOp.AND, assertStmt);
    // Assert:
    // __ASSERT = (output = (fault_node_val_out))
    // and (__ASSUME__HIST => (__GUARANTEE0 and true)))
    newNode.addEquation(new IdExpr("__ASSERT"), finalExpr2);
    // Construct the node call expression
    // If record type, add to fault nominal expression
    constructNodeCalls(newNode, dotField);
    triggerList.clear();
    return newNode;
}
Also used : BoolExpr(jkind.lustre.BoolExpr) RecordAccessExpr(jkind.lustre.RecordAccessExpr) RecordType(jkind.lustre.RecordType) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) RecordAccessExpr(jkind.lustre.RecordAccessExpr) Expr(jkind.lustre.Expr) IdExpr(jkind.lustre.IdExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) IdExpr(jkind.lustre.IdExpr) BinaryExpr(jkind.lustre.BinaryExpr)

Example 7 with RecordType

use of jkind.lustre.RecordType in project AMASE by loonwerks.

the class FaultASTBuilder method createEquationsForCommNode.

/**
 * creates equations for new communication node.
 *
 * @param node NodeBuilder
 * @param fault Fault
 * @param type Type on faulty output
 * @param nodeName Name of the communication node
 * @return NodeBuilder with equations added
 */
private NodeBuilder createEquationsForCommNode(NodeBuilder node, Fault fault, Type type, String nodeName) {
    // assign __GUARANTEE0 : fault__nominal__output = input
    // Simple case is when it is bool, real. Otherwise if it is a record
    // type, we need to access the field of the fault node input and append
    // that to the expression (i.e. __fault__nominal__output.VAL = input.VAL)
    String field = "";
    String dotField = "";
    if (type instanceof RecordType) {
        for (Expr faultOutput : fault.faultOutputMap.keySet()) {
            if (faultOutput instanceof RecordAccessExpr) {
                RecordAccessExpr rac = (RecordAccessExpr) faultOutput;
                dotField = "." + rac.field;
                field = rac.field;
            }
        }
    }
    IdExpr faultNominalOut = new IdExpr("__fault__nominal__output" + dotField);
    Expr binEx = new BinaryExpr(faultNominalOut, BinaryOp.EQUAL, new IdExpr("input" + dotField));
    IdExpr guar = new IdExpr("__GUARANTEE0");
    // This links fault nominal with node input :
    // assert (__fault__nominal__output.NODE_VAL = input.NODE_VAL)
    node.addEquation(guar, binEx);
    BinaryExpr binAssumeAndTrue = createAssumeHistStmt(guar);
    // output = (fault_node_val_out)
    // If record type, need to reference "output.VAL"
    IdExpr toAssign = null;
    toAssign = new IdExpr(getFaultNodeOutputId(fault));
    IdExpr output;
    if (type instanceof RecordType) {
        output = new IdExpr("output" + dotField);
    } else {
        output = new IdExpr("output");
    }
    // output = val_out
    BinaryExpr outputEqualsValout = new BinaryExpr(output, BinaryOp.EQUAL, toAssign);
    // Final expression
    BinaryExpr finalExpr = new BinaryExpr(binAssumeAndTrue, BinaryOp.AND, outputEqualsValout);
    // Before finishing the assert, check to see if we have safetyEqAsserts in the fault
    // and add those to the finalExpr with "and"
    addSafetyEqAssertStmts(node, fault, finalExpr);
    // Construct the node call expression
    // If record type, add to fault nominal expression
    constructNodeCallExpr(node, fault, dotField);
    return node;
}
Also used : RecordAccessExpr(jkind.lustre.RecordAccessExpr) RecordType(jkind.lustre.RecordType) RecordAccessExpr(jkind.lustre.RecordAccessExpr) TupleExpr(jkind.lustre.TupleExpr) Expr(jkind.lustre.Expr) NodeCallExpr(jkind.lustre.NodeCallExpr) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) IdExpr(jkind.lustre.IdExpr) IdExpr(jkind.lustre.IdExpr) BinaryExpr(jkind.lustre.BinaryExpr)

Example 8 with RecordType

use of jkind.lustre.RecordType 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 9 with RecordType

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

the class AGREESimulationStateElementFactory method addChildElementsForVariable.

private static void addChildElementsForVariable(final Collection<AGREESimulationStateElement> elements, final AGREESimulationStateElement parent, final String variableName, jkind.lustre.Type variableType, final Expr lustreExpr, final Map<String, jkind.lustre.Type> typeIdToTypeMap, final FeatureInstance featureInstance, final EObject declReference, final boolean hidden) {
    assert elements != null;
    assert variableName != null;
    assert variableType != null;
    assert lustreExpr != null;
    variableType = resolveType(variableType, typeIdToTypeMap);
    if (variableType == NamedType.INT || variableType == NamedType.REAL || variableType == NamedType.BOOL) {
        elements.add(new AGREESimulationStateElement(parent, variableName, lustreTypeToSimType(variableType), lustreExpr, featureInstance, declReference, hidden));
    } else if (variableType instanceof RecordType) {
        final AGREESimulationStateElement newElement = new AGREESimulationStateElement(parent, variableName, edu.uah.rsesc.aadlsimulator.VariableType.NONE, null, featureInstance, declReference, hidden);
        final RecordType recordType = (RecordType) variableType;
        final List<AGREESimulationStateElement> recordElements = new ArrayList<AGREESimulationStateElement>();
        for (final Entry<String, jkind.lustre.Type> field : recordType.fields.entrySet()) {
            addChildElementsForVariable(recordElements, newElement, field.getKey(), field.getValue(), new RecordAccessExpr(lustreExpr, field.getKey()), typeIdToTypeMap, null, null, hidden);
        }
        newElement.setChildren(recordElements);
        elements.add(newElement);
    } else if (variableType instanceof ArrayType) {
        final ArrayType arrayType = (ArrayType) variableType;
        final Type elementType = arrayType.base;
        for (int i = 0; i < arrayType.size; i++) {
            // Offset array origin since AGREE/AADL arrays are one based and JKind arrays are 0 based
            final String indexStr = "[" + (i + 1) + "]";
            addChildElementsForVariable(elements, parent, variableName + indexStr, elementType, new ArrayAccessExpr(lustreExpr, i), typeIdToTypeMap, featureInstance, declReference, hidden);
        }
    } else {
        throw new RuntimeException("Unsupported variable type: " + variableType);
    }
}
Also used : RecordAccessExpr(jkind.lustre.RecordAccessExpr) ArrayAccessExpr(jkind.lustre.ArrayAccessExpr) ArrayType(jkind.lustre.ArrayType) Entry(java.util.Map.Entry) SimulationProgramType(edu.uah.rsesc.aadlsimulator.agree.SimulationProgramType) RecordType(jkind.lustre.RecordType) Type(jkind.lustre.Type) NamedType(jkind.lustre.NamedType) ArrayType(jkind.lustre.ArrayType) RecordType(jkind.lustre.RecordType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with RecordType

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

the class TypeTable method getLustreType.

public Type getLustreType(AgreeTypeSystem.TypeDef agreeType) {
    if (agreeType == AgreeTypeSystem.Prim.IntTypeDef) {
        return NamedType.INT;
    } else if (agreeType == AgreeTypeSystem.Prim.RealTypeDef) {
        return NamedType.REAL;
    } else if (agreeType == AgreeTypeSystem.Prim.BoolTypeDef) {
        return NamedType.BOOL;
    } else if (agreeType instanceof AgreeTypeSystem.RangeIntTypeDef) {
        return NamedType.INT;
    } else if (agreeType instanceof AgreeTypeSystem.RangeRealTypeDef) {
        return NamedType.REAL;
    } else if (agreeType instanceof AgreeTypeSystem.RecordTypeDef) {
        String name = ((AgreeTypeSystem.RecordTypeDef) agreeType).name.replace("::", "__").replace(".", "__");
        Map<String, AgreeTypeSystem.TypeDef> agreeFields = ((AgreeTypeSystem.RecordTypeDef) agreeType).fields;
        Map<String, Type> lustreFields = new HashMap<>();
        for (Entry<String, AgreeTypeSystem.TypeDef> entry : agreeFields.entrySet()) {
            String key = entry.getKey();
            Type lt = updateLustreTypeMap(entry.getValue());
            if (lt != null) {
                lustreFields.put(key, lt);
            }
        }
        RecordType lustreRecType = new RecordType(name, lustreFields);
        return lustreRecType;
    } else if (agreeType instanceof AgreeTypeSystem.EnumTypeDef) {
        String name = ((AgreeTypeSystem.EnumTypeDef) agreeType).name.replace("::", "__").replace(".", "__");
        List<String> enumValues = new ArrayList<String>();
        for (String raw : ((AgreeTypeSystem.EnumTypeDef) agreeType).values) {
            String enumValue = raw.replace("::", "__").replace(".", "__");
            enumValues.add(enumValue);
        }
        EnumType lustreEnumType = new EnumType(name, enumValues);
        return lustreEnumType;
    } else if (agreeType instanceof AgreeTypeSystem.ArrayTypeDef) {
        AgreeTypeSystem.TypeDef agreeBaseType = ((AgreeTypeSystem.ArrayTypeDef) agreeType).stemType;
        int dimension = ((AgreeTypeSystem.ArrayTypeDef) agreeType).size;
        Type lustreBaseType = updateLustreTypeMap(agreeBaseType);
        if (lustreBaseType != null) {
            ArrayType lustreArrayType = new ArrayType(lustreBaseType, dimension);
            return lustreArrayType;
        }
    }
    // Jkind does not reason over this.
    return null;
}
Also used : ArrayList(java.util.ArrayList) ArrayType(jkind.lustre.ArrayType) AgreeTypeSystem(com.rockwellcollins.atc.agree.AgreeTypeSystem) Entry(java.util.Map.Entry) EnumType(jkind.lustre.EnumType) NamedType(jkind.lustre.NamedType) ArrayType(jkind.lustre.ArrayType) RecordType(jkind.lustre.RecordType) Type(jkind.lustre.Type) RecordType(jkind.lustre.RecordType) EnumType(jkind.lustre.EnumType) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

RecordType (jkind.lustre.RecordType)11 Type (jkind.lustre.Type)9 NamedType (jkind.lustre.NamedType)8 ArrayList (java.util.ArrayList)5 EnumType (jkind.lustre.EnumType)5 Expr (jkind.lustre.Expr)5 RecordAccessExpr (jkind.lustre.RecordAccessExpr)5 Entry (java.util.Map.Entry)4 ArrayType (jkind.lustre.ArrayType)3 BinaryExpr (jkind.lustre.BinaryExpr)3 BoolExpr (jkind.lustre.BoolExpr)3 IdExpr (jkind.lustre.IdExpr)3 AgreeNode (com.rockwellcollins.atc.agree.analysis.ast.AgreeNode)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ArrayAccessExpr (jkind.lustre.ArrayAccessExpr)2 IntExpr (jkind.lustre.IntExpr)2 Node (jkind.lustre.Node)2 NodeCallExpr (jkind.lustre.NodeCallExpr)2 RecordExpr (jkind.lustre.RecordExpr)2