Search in sources :

Example 66 with AgreeVar

use of com.rockwellcollins.atc.agree.analysis.ast.AgreeVar in project AGREE by loonwerks.

the class LustreContractAstBuilder method getLustreNode.

protected static Node getLustreNode(AgreeNode agreeNode, String nodePrefix) {
    List<VarDecl> inputs = new ArrayList<>();
    List<VarDecl> outputs = new ArrayList<>();
    List<VarDecl> locals = new ArrayList<>();
    List<Equation> equations = new ArrayList<>();
    List<Expr> assertions = new ArrayList<>();
    List<Expr> requires = new ArrayList<>();
    List<Expr> ensures = new ArrayList<>();
    for (AgreeStatement statement : agreeNode.assumptions) {
        requires.add(statement.expr);
    }
    for (AgreeStatement statement : agreeNode.lemmas) {
        ensures.add(statement.expr);
    }
    for (AgreeStatement statement : agreeNode.guarantees) {
        ensures.add(statement.expr);
    }
    for (AgreeStatement statement : agreeNode.assertions) {
        assertions.add(statement.expr);
        if (AgreeUtils.referenceIsInContract(statement.reference, agreeNode.compInst)) {
            ensures.add(statement.expr);
        }
    }
    // gather the remaining inputs
    for (AgreeVar var : agreeNode.inputs) {
        inputs.add(var);
    }
    for (AgreeVar var : agreeNode.outputs) {
        outputs.add(var);
    }
    // Contract contract = new Contract(nodePrefix + agreeNode.id, requires, ensures);
    Contract contract = new Contract(requires, ensures);
    NodeBuilder builder = new NodeBuilder(nodePrefix + agreeNode.id);
    builder.addInputs(inputs);
    builder.addOutputs(outputs);
    builder.addLocals(locals);
    builder.addEquations(equations);
    builder.addAssertions(assertions);
    builder.setContract(contract);
    return builder.build();
}
Also used : AgreeStatement(com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) TupleExpr(jkind.lustre.TupleExpr) UnaryExpr(jkind.lustre.UnaryExpr) CondactExpr(jkind.lustre.CondactExpr) Expr(jkind.lustre.Expr) IdExpr(jkind.lustre.IdExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) VarDecl(jkind.lustre.VarDecl) ArrayList(java.util.ArrayList) Equation(jkind.lustre.Equation) NodeBuilder(jkind.lustre.builders.NodeBuilder) AgreeNodeBuilder(com.rockwellcollins.atc.agree.analysis.ast.AgreeNodeBuilder) Contract(jkind.lustre.Contract) AgreeVar(com.rockwellcollins.atc.agree.analysis.ast.AgreeVar)

Example 67 with AgreeVar

use of com.rockwellcollins.atc.agree.analysis.ast.AgreeVar 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 68 with AgreeVar

use of com.rockwellcollins.atc.agree.analysis.ast.AgreeVar in project AGREE by loonwerks.

the class CreateSimulationProperties method transform.

public static SimulationProgram transform(final SimulationProgram program) {
    final Program lustreProgram = program.getLustreProgram();
    if (lustreProgram.nodes.size() != 1) {
        throw new IllegalArgumentException("Only lustre programs with exactly one node are supported");
    }
    final SimulationProgramBuilder builder = new SimulationProgramBuilder(program);
    final Node mainNode = lustreProgram.getMainNode();
    // Build a new main node which includes support statements
    final NodeBuilder lustreNodeBuilder = new NodeBuilder(mainNode);
    // Create simulation properties for each local variable that references a lemma, assume, guarantee, or assert statement
    lustreNodeBuilder.clearIvc();
    final Map<String, SimulationProperty> idToSimulationPropertyMap = new HashMap<>();
    // Create a mapping from the component instance/variable reference to a collection of Lustre Id's that will be used to create the simulation properties
    final Map<ComponentInstance, Map<EObject, Collection<String>>> componentInstanceToReferenceToVarIdMap = new HashMap<>();
    for (final VarDecl local : mainNode.locals) {
        if (local instanceof AgreeVar) {
            final AgreeVar var = (AgreeVar) local;
            if (var.reference instanceof LemmaStatement || var.reference instanceof AssumeStatement || var.reference instanceof GuaranteeStatement || var.reference instanceof AssertStatement) {
                final boolean createSimProp = (!(var.reference instanceof AssumeStatement) || var.compInst == var.compInst.getSystemInstance()) || var instanceof SimulationPropertyVar;
                if (createSimProp) {
                    Map<EObject, Collection<String>> referenceToVarIdMap = componentInstanceToReferenceToVarIdMap.get(var.compInst);
                    if (referenceToVarIdMap == null) {
                        referenceToVarIdMap = new HashMap<>();
                        componentInstanceToReferenceToVarIdMap.put(var.compInst, referenceToVarIdMap);
                    }
                    Collection<String> varIds = referenceToVarIdMap.get(var.reference);
                    if (varIds == null) {
                        varIds = new HashSet<>();
                        referenceToVarIdMap.put(var.reference, varIds);
                    }
                    varIds.add(var.id);
                }
            }
        }
    }
    // Create the simulation properties.
    // A single simulation property will be created for each component instance, reference combination.
    int propertyIndex = 0;
    for (final Entry<ComponentInstance, Map<EObject, Collection<String>>> componentInstanceToReferenceToVarIdMapEntry : componentInstanceToReferenceToVarIdMap.entrySet()) {
        final ComponentInstance propComponentInstance = componentInstanceToReferenceToVarIdMapEntry.getKey();
        for (Entry<EObject, Collection<String>> referenceToVarIdsEntry : componentInstanceToReferenceToVarIdMapEntry.getValue().entrySet()) {
            final EObject propReference = referenceToVarIdsEntry.getKey();
            final Collection<String> propLustreIds = referenceToVarIdsEntry.getValue();
            final String propertyDesc = getDescription(propComponentInstance, propReference);
            final String enablementVariableId;
            // Only properties which cause the simulation to be halted may be disabled.
            // That is: only top-level assumptions and non-top-level guarantees may be disabled
            final boolean disableable = (propReference instanceof GuaranteeStatement && propComponentInstance != propComponentInstance.getSystemInstance()) || (propReference instanceof AssumeStatement && propComponentInstance == propComponentInstance.getSystemInstance());
            if (disableable) {
                enablementVariableId = propertyEnablementPrefix + propertyIndex;
                lustreNodeBuilder.addLocal(new VarDecl(enablementVariableId, NamedType.BOOL));
            } else {
                enablementVariableId = null;
            }
            final SimulationProperty simProp = new SimulationProperty(propLustreIds, propertyDesc, propReference, enablementVariableId);
            builder.addSimulationProperty(simProp);
            propertyIndex++;
            for (final String propLustreId : propLustreIds) {
                idToSimulationPropertyMap.put(propLustreId, simProp);
                lustreNodeBuilder.addIvc(propLustreId);
            }
        }
    }
    // Edit main node to make simulation properties inside of assertions to be disableable
    final AstMapVisitor propertyVarDisableTransformation = new AstMapVisitor() {

        @Override
        public Expr visit(final IdExpr e) {
            final SimulationProperty simProp = idToSimulationPropertyMap.get(e.id);
            if (simProp != null && simProp.getEnablementVariableId() != null) {
                return new BinaryExpr(new UnaryExpr(UnaryOp.NOT, new IdExpr(simProp.getEnablementVariableId())), BinaryOp.OR, e);
            }
            return super.visit(e);
        }

        @Override
        public Equation visit(final Equation e) {
            // Don't transform the equations for simulation properties
            if (e.lhs.size() == 1 && idToSimulationPropertyMap.containsKey(e.lhs.get(0))) {
                return e;
            }
            return super.visit(e);
        }
    };
    // Transform assertions and equations
    lustreNodeBuilder.clearAssertions();
    for (final Expr assertion : mainNode.assertions) {
        lustreNodeBuilder.addAssertion(assertion.accept(propertyVarDisableTransformation));
    }
    lustreNodeBuilder.clearEquations();
    for (final Equation eq : mainNode.equations) {
        lustreNodeBuilder.addEquation((Equation) eq.accept(propertyVarDisableTransformation));
    }
    // Build a new program with the new main node
    final ProgramBuilder lustreProgramBuilder = new ProgramBuilder(lustreProgram);
    lustreProgramBuilder.clearNodes();
    lustreProgramBuilder.addNode(lustreNodeBuilder.build());
    builder.setLustreProgram(lustreProgramBuilder.build());
    return builder.build();
}
Also used : HashMap(java.util.HashMap) INode(org.eclipse.xtext.nodemodel.INode) Node(jkind.lustre.Node) NodeBuilder(jkind.lustre.builders.NodeBuilder) LemmaStatement(com.rockwellcollins.atc.agree.agree.LemmaStatement) VarDecl(jkind.lustre.VarDecl) EObject(org.eclipse.emf.ecore.EObject) ComponentInstance(org.osate.aadl2.instance.ComponentInstance) SimulationProperty(edu.uah.rsesc.aadlsimulator.agree.SimulationProperty) GuaranteeStatement(com.rockwellcollins.atc.agree.agree.GuaranteeStatement) Program(jkind.lustre.Program) SimulationProgram(edu.uah.rsesc.aadlsimulator.agree.SimulationProgram) AssumeStatement(com.rockwellcollins.atc.agree.agree.AssumeStatement) IdExpr(jkind.lustre.IdExpr) ProgramBuilder(jkind.lustre.builders.ProgramBuilder) SimulationProgramBuilder(edu.uah.rsesc.aadlsimulator.agree.SimulationProgramBuilder) BinaryExpr(jkind.lustre.BinaryExpr) Equation(jkind.lustre.Equation) UnaryExpr(jkind.lustre.UnaryExpr) AgreeVar(com.rockwellcollins.atc.agree.analysis.ast.AgreeVar) AstMapVisitor(jkind.lustre.visitors.AstMapVisitor) BinaryExpr(jkind.lustre.BinaryExpr) UnaryExpr(jkind.lustre.UnaryExpr) Expr(jkind.lustre.Expr) IdExpr(jkind.lustre.IdExpr) SimulationProgramBuilder(edu.uah.rsesc.aadlsimulator.agree.SimulationProgramBuilder) AssertStatement(com.rockwellcollins.atc.agree.agree.AssertStatement) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 69 with AgreeVar

use of com.rockwellcollins.atc.agree.analysis.ast.AgreeVar in project AGREE by loonwerks.

the class InlineNodeCalls method getTranslation.

private Map<String, IdExpr> getTranslation(String prefix, Node node) {
    Map<String, IdExpr> translation = new HashMap<>();
    for (VarDecl decl : Util.getVarDecls(node)) {
        String id = prefix + decl.id;
        if (decl instanceof SimulationPropertyVar) {
            final SimulationPropertyVar var = (SimulationPropertyVar) decl;
            newLocals.add(new SimulationPropertyVar(id, decl.type, var.reference, var.compInst, var.featInst));
        } else if (decl instanceof AgreeVar) {
            final AgreeVar var = (AgreeVar) decl;
            newLocals.add(new AgreeVar(id, decl.type, var.reference, var.compInst, var.featInst));
        } else {
            newLocals.add(new VarDecl(id, decl.type));
        }
        translation.put(decl.id, new IdExpr(id));
    }
    return translation;
}
Also used : IdExpr(jkind.lustre.IdExpr) HashMap(java.util.HashMap) VarDecl(jkind.lustre.VarDecl) AgreeVar(com.rockwellcollins.atc.agree.analysis.ast.AgreeVar)

Aggregations

AgreeVar (com.rockwellcollins.atc.agree.analysis.ast.AgreeVar)69 IdExpr (jkind.lustre.IdExpr)49 Expr (jkind.lustre.Expr)45 NodeCallExpr (jkind.lustre.NodeCallExpr)42 BinaryExpr (jkind.lustre.BinaryExpr)41 AgreeStatement (com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement)39 BoolExpr (jkind.lustre.BoolExpr)38 UnaryExpr (jkind.lustre.UnaryExpr)37 ArrayList (java.util.ArrayList)28 IfThenElseExpr (jkind.lustre.IfThenElseExpr)25 VarDecl (jkind.lustre.VarDecl)19 Equation (jkind.lustre.Equation)15 RealExpr (jkind.lustre.RealExpr)15 AgreeEquation (com.rockwellcollins.atc.agree.analysis.ast.AgreeEquation)14 AgreeNode (com.rockwellcollins.atc.agree.analysis.ast.AgreeNode)13 IntExpr (jkind.lustre.IntExpr)13 AgreeException (com.rockwellcollins.atc.agree.analysis.AgreeException)12 AgreeNodeBuilder (com.rockwellcollins.atc.agree.analysis.ast.AgreeNodeBuilder)12 NodeBuilder (jkind.lustre.builders.NodeBuilder)11 SafetyException (edu.umn.cs.crisys.safety.analysis.SafetyException)9