Search in sources :

Example 16 with Equation

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

the class AgreeMakeClockedLustreNodes method visit.

@Override
public Expr visit(UnaryExpr e) {
    if (e.op == UnaryOp.PRE) {
        IdExpr stateVarId = stateVarMap.get(e.toString());
        if (stateVarId != null) {
            return stateVarId;
        }
        stateVarId = new IdExpr(statVarPrefix + numStateVars++);
        stateVarMap.put(e.toString(), stateVarId);
        Expr stateVarExpr = new UnaryExpr(UnaryOp.PRE, e.expr.accept(this));
        stateVarExpr = expr("if clk then stateVarExpr else (pre stateVar)", to("stateVar", stateVarId), to("stateVarExpr", stateVarExpr), to("clk", clockVarName));
        stateVars.add(new AgreeVar(stateVarId.id, e.accept(typeReconstructor), null, null, null));
        stateVarEqs.add(new Equation(stateVarId, stateVarExpr));
        return stateVarId;
    }
    return new UnaryExpr(e.op, e.expr.accept(this));
}
Also used : IdExpr(jkind.lustre.IdExpr) BinaryExpr(jkind.lustre.BinaryExpr) UnaryExpr(jkind.lustre.UnaryExpr) Expr(jkind.lustre.Expr) IdExpr(jkind.lustre.IdExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) Equation(jkind.lustre.Equation) UnaryExpr(jkind.lustre.UnaryExpr) AgreeVar(com.rockwellcollins.atc.agree.analysis.ast.AgreeVar)

Example 17 with Equation

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

the class AgreeNodeToLustreContract method translateNode.

private static Node translateNode(AgreeNode agreeNode) {
    List<VarDecl> inputs = new ArrayList<>();
    List<VarDecl> locals = new ArrayList<>();
    List<Equation> eqs = new ArrayList<>();
    List<Expr> assertions = new ArrayList<>();
    List<String> properties = new ArrayList<>();
    inputs.addAll(agreeNode.inputs);
    inputs.addAll(agreeNode.outputs);
    inputs.addAll(agreeNode.locals);
    eqs.addAll(nodeAssertsToEqs(agreeNode));
    // right now the AGREE AST just has assertions over input variables
    // this step allows us to inline some of these as local variables
    // to the node
    List<IdExpr> ids = gatherIds(eqs);
    List<VarDecl> inlinedVars = new ArrayList<>();
    for (VarDecl var : inputs) {
        for (IdExpr id : ids) {
            if (id.id.equals(var.id)) {
                inlinedVars.add(var);
            }
        }
    }
    inputs.removeAll(inlinedVars);
    locals.addAll(inlinedVars);
    for (AgreeStatement statement : agreeNode.assumptions) {
        assertions.add(statement.expr);
    }
    int i = 0;
    for (AgreeStatement statement : agreeNode.guarantees) {
        String guarName = "__GUARANTEE" + i++;
        properties.add(guarName);
        locals.add(new VarDecl(guarName, NamedType.BOOL));
        eqs.add(new Equation(new IdExpr(guarName), statement.expr));
    }
    NodeBuilder builder = new NodeBuilder(agreeNode.id);
    builder.addInputs(inputs);
    builder.addLocals(locals);
    builder.addEquations(eqs);
    builder.addProperties(properties);
    builder.addAssertions(assertions);
    return builder.build();
}
Also used : AgreeStatement(com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement) IdExpr(jkind.lustre.IdExpr) ArrayList(java.util.ArrayList) Equation(jkind.lustre.Equation) NodeBuilder(jkind.lustre.builders.NodeBuilder) BinaryExpr(jkind.lustre.BinaryExpr) TupleExpr(jkind.lustre.TupleExpr) Expr(jkind.lustre.Expr) IdExpr(jkind.lustre.IdExpr) VarDecl(jkind.lustre.VarDecl)

Example 18 with Equation

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

the class AgreeNodeToLustreContract method nodeAssertsToEqs.

private static List<Equation> nodeAssertsToEqs(AgreeNode agreeNode) {
    List<Equation> eqs = new ArrayList<>();
    for (AgreeStatement statement : agreeNode.assertions) {
        if (AgreeUtils.referenceIsInContract(statement.reference, agreeNode.compInst)) {
            BinaryExpr expr = (BinaryExpr) statement.expr;
            if ((expr.op != BinaryOp.EQUAL)) {
                throw new AgreeException("Unexpected expression type in AGREE AST asssertion");
            }
            Expr leftExpr = expr.left;
            Expr rightExpr = expr.right;
            List<IdExpr> ids = new ArrayList<>();
            if (leftExpr instanceof TupleExpr) {
                TupleExpr tuple = (TupleExpr) leftExpr;
                for (Expr element : tuple.elements) {
                    ids.add((IdExpr) element);
                }
            } else {
                ids.add((IdExpr) leftExpr);
            }
            eqs.add(new Equation(ids, rightExpr));
        }
    }
    return eqs;
}
Also used : AgreeStatement(com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement) BinaryExpr(jkind.lustre.BinaryExpr) TupleExpr(jkind.lustre.TupleExpr) Expr(jkind.lustre.Expr) IdExpr(jkind.lustre.IdExpr) IdExpr(jkind.lustre.IdExpr) BinaryExpr(jkind.lustre.BinaryExpr) ArrayList(java.util.ArrayList) Equation(jkind.lustre.Equation) AgreeException(com.rockwellcollins.atc.agree.analysis.AgreeException) TupleExpr(jkind.lustre.TupleExpr)

Example 19 with Equation

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

the class LustreAstBuilder method getLustreNode.

protected static Node getLustreNode(AgreeNode agreeNode, String nodePrefix) {
    List<VarDecl> inputs = new ArrayList<>();
    List<VarDecl> locals = new ArrayList<>();
    List<Equation> equations = new ArrayList<>();
    List<Expr> assertions = new ArrayList<>();
    List<String> ivcs = agreeNode.getivcElements();
    List<String> properties = new ArrayList<>();
    // add assumption history variable
    IdExpr assumHist = new IdExpr(assumeHistSufix);
    inputs.add(new AgreeVar(assumHist.id, NamedType.BOOL, null, agreeNode.compInst, null));
    int i = 0;
    for (AgreeStatement statement : agreeNode.assumptions) {
        String inputName = assumeSuffix + i++;
        inputs.add(new AgreeVar(inputName, NamedType.BOOL, statement.reference, agreeNode.compInst, null));
        IdExpr assumeId = new IdExpr(inputName);
        assertions.add(new BinaryExpr(assumeId, BinaryOp.EQUAL, statement.expr));
    }
    int j = 0;
    for (AgreeStatement statement : agreeNode.lemmas) {
        String inputName = lemmaSuffix + j++;
        inputs.add(new AgreeVar(inputName, NamedType.BOOL, statement.reference, agreeNode.compInst, null));
        properties.add(inputName);
        IdExpr lemmaId = new IdExpr(inputName);
        assertions.add(new BinaryExpr(lemmaId, BinaryOp.EQUAL, statement.expr));
    }
    int k = 0;
    Expr guarConjExpr = new BoolExpr(true);
    for (AgreeStatement statement : agreeNode.guarantees) {
        String inputName = guarSuffix + k++;
        locals.add(new AgreeVar(inputName, NamedType.BOOL, statement.reference, agreeNode.compInst, null));
        IdExpr guarId = new IdExpr(inputName);
        equations.add(new Equation(guarId, statement.expr));
        if (agreeNode.getFaultTreeFlag() == false) {
            ivcs.add(guarId.id);
        } else {
            // check if it's leaf node
            if (!agreeNode.compInst.getComponentInstances().isEmpty()) {
                ivcs.add(guarId.id);
            }
        }
        guarConjExpr = LustreExprFactory.makeANDExpr(guarId, guarConjExpr);
    }
    // assert that if the assumptions have held historically, then the
    // gurantees hold
    assertions.add(new BinaryExpr(assumHist, BinaryOp.IMPLIES, guarConjExpr));
    for (AgreeStatement statement : agreeNode.assertions) {
        assertions.add(statement.expr);
    }
    // create properties for the patterns
    int l = 0;
    for (AgreeStatement patternPropState : agreeNode.patternProps) {
        String patternVarName = patternPropSuffix + l++;
        inputs.add(new AgreeVar(patternVarName, NamedType.BOOL, patternPropState, agreeNode.compInst, null));
        assertions.add(new BinaryExpr(new IdExpr(patternVarName), BinaryOp.EQUAL, patternPropState.expr));
    }
    Expr assertExpr = new BoolExpr(true);
    for (Expr expr : assertions) {
        assertExpr = LustreExprFactory.makeANDExpr(expr, assertExpr);
    }
    String outputName = "__ASSERT";
    List<VarDecl> outputs = new ArrayList<>();
    outputs.add(new VarDecl(outputName, NamedType.BOOL));
    equations.add(new Equation(new IdExpr(outputName), assertExpr));
    // gather the remaining inputs
    for (AgreeVar var : agreeNode.inputs) {
        inputs.add(var);
    }
    for (AgreeVar var : agreeNode.outputs) {
        inputs.add(var);
    }
    for (AgreeVar var : agreeNode.locals) {
        locals.add(var);
    }
    for (AgreeEquation equation : agreeNode.localEquations) {
        equations.add(equation);
    }
    NodeBuilder builder = new NodeBuilder(nodePrefix + agreeNode.id);
    builder.addInputs(inputs);
    builder.addOutputs(outputs);
    builder.addLocals(locals);
    builder.addEquations(equations);
    builder.addProperties(properties);
    builder.addIvcs(ivcs);
    return builder.build();
}
Also used : BoolExpr(jkind.lustre.BoolExpr) AgreeStatement(com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement) IdExpr(jkind.lustre.IdExpr) BinaryExpr(jkind.lustre.BinaryExpr) ArrayList(java.util.ArrayList) Equation(jkind.lustre.Equation) AgreeEquation(com.rockwellcollins.atc.agree.analysis.ast.AgreeEquation) AgreeEquation(com.rockwellcollins.atc.agree.analysis.ast.AgreeEquation) NodeBuilder(jkind.lustre.builders.NodeBuilder) AgreeNodeBuilder(com.rockwellcollins.atc.agree.analysis.ast.AgreeNodeBuilder) AgreeVar(com.rockwellcollins.atc.agree.analysis.ast.AgreeVar) UnaryExpr(jkind.lustre.UnaryExpr) Expr(jkind.lustre.Expr) IntExpr(jkind.lustre.IntExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) IdExpr(jkind.lustre.IdExpr) VarDecl(jkind.lustre.VarDecl)

Example 20 with Equation

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

the class LustreAstBuilder method getRealizabilityLustreProgram.

// private static AgreeProgram translate(AgreeProgram program){
// return AgreeInlineLatchedConnections.translate(program);
// }
public static Program getRealizabilityLustreProgram(AgreeProgram agreeProgram) {
    List<TypeDef> types = AgreeUtils.getLustreTypes(agreeProgram);
    List<Expr> assertions = new ArrayList<>();
    List<VarDecl> locals = new ArrayList<>();
    List<VarDecl> inputs = new ArrayList<>();
    List<Equation> equations = new ArrayList<>();
    List<String> properties = new ArrayList<>();
    AgreeNode topNode = agreeProgram.topNode;
    for (AgreeStatement assumption : topNode.assumptions) {
        assertions.add(assumption.expr);
    }
    int i = 0;
    for (AgreeStatement guarantee : topNode.guarantees) {
        String guarName = guarSuffix + i++;
        locals.add(new AgreeVar(guarName, NamedType.BOOL, guarantee.reference, topNode.compInst, null));
        equations.add(new Equation(new IdExpr(guarName), guarantee.expr));
        properties.add(guarName);
    }
    List<String> inputStrs = new ArrayList<>();
    for (AgreeVar var : topNode.inputs) {
        inputs.add(var);
        inputStrs.add(var.id);
    }
    for (AgreeVar var : topNode.outputs) {
        inputs.add(var);
    }
    // and type equations. This would clear this up
    for (AgreeStatement statement : topNode.assertions) {
        if (AgreeUtils.referenceIsInContract(statement, topNode.compInst)) {
            // this is a strange hack we have to do. we have to make
            // equation and property
            // statements not assertions. They should all be binary
            // expressions with an
            // equals operator. We will need to removing their corresponding
            // variable
            // from the inputs and add them to the local variables
            BinaryExpr binExpr;
            IdExpr varId;
            try {
                binExpr = (BinaryExpr) statement.expr;
                varId = (IdExpr) binExpr.left;
            } catch (ClassCastException e) {
                // some equation variables are assertions for
                // subrange types. do not translate these to
                // local equations. Just add them to assertions
                assertions.add(statement.expr);
                continue;
            }
            boolean found = false;
            int index;
            for (index = 0; index < inputs.size(); index++) {
                VarDecl var = inputs.get(index);
                if (var.id.equals(varId.id)) {
                    found = true;
                    break;
                }
            }
            if (!found || binExpr.op != BinaryOp.EQUAL) {
                throw new AgreeException("Something went very wrong with the lustre generation in the realizability analysis");
            }
            locals.add(inputs.remove(index));
            equations.add(new Equation(varId, binExpr.right));
        }
    }
    NodeBuilder builder = new NodeBuilder("main");
    builder.addInputs(inputs);
    builder.addLocals(locals);
    builder.addEquations(equations);
    builder.addProperties(properties);
    builder.addAssertions(assertions);
    builder.setRealizabilityInputs(inputStrs);
    Node main = builder.build();
    List<Node> nodes = new ArrayList<>();
    nodes.add(main);
    nodes.addAll(agreeProgram.globalLustreNodes);
    List<Function> uFunctions = new ArrayList<>();
    uFunctions.addAll(agreeProgram.uninterpretedFunctions);
    Program program = new ProgramBuilder().addTypes(types).addFunctions(uFunctions).addNodes(nodes).setMain(main.id).build();
    return program;
}
Also used : AgreeStatement(com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement) Node(jkind.lustre.Node) AgreeNode(com.rockwellcollins.atc.agree.analysis.ast.AgreeNode) ArrayList(java.util.ArrayList) NodeBuilder(jkind.lustre.builders.NodeBuilder) AgreeNodeBuilder(com.rockwellcollins.atc.agree.analysis.ast.AgreeNodeBuilder) Function(jkind.lustre.Function) TypeDef(jkind.lustre.TypeDef) VarDecl(jkind.lustre.VarDecl) AgreeNode(com.rockwellcollins.atc.agree.analysis.ast.AgreeNode) Program(jkind.lustre.Program) AgreeProgram(com.rockwellcollins.atc.agree.analysis.ast.AgreeProgram) IdExpr(jkind.lustre.IdExpr) ProgramBuilder(jkind.lustre.builders.ProgramBuilder) BinaryExpr(jkind.lustre.BinaryExpr) Equation(jkind.lustre.Equation) AgreeEquation(com.rockwellcollins.atc.agree.analysis.ast.AgreeEquation) AgreeVar(com.rockwellcollins.atc.agree.analysis.ast.AgreeVar) UnaryExpr(jkind.lustre.UnaryExpr) Expr(jkind.lustre.Expr) IntExpr(jkind.lustre.IntExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) IdExpr(jkind.lustre.IdExpr) AgreeException(com.rockwellcollins.atc.agree.analysis.AgreeException)

Aggregations

Equation (jkind.lustre.Equation)52 IdExpr (jkind.lustre.IdExpr)48 Expr (jkind.lustre.Expr)43 BinaryExpr (jkind.lustre.BinaryExpr)40 NodeCallExpr (jkind.lustre.NodeCallExpr)35 UnaryExpr (jkind.lustre.UnaryExpr)35 VarDecl (jkind.lustre.VarDecl)34 BoolExpr (jkind.lustre.BoolExpr)33 NodeBuilder (jkind.lustre.builders.NodeBuilder)30 ArrayList (java.util.ArrayList)27 IfThenElseExpr (jkind.lustre.IfThenElseExpr)26 IntExpr (jkind.lustre.IntExpr)20 AgreeVar (com.rockwellcollins.atc.agree.analysis.ast.AgreeVar)14 TupleExpr (jkind.lustre.TupleExpr)12 Node (jkind.lustre.Node)11 AgreeStatement (com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement)10 RealExpr (jkind.lustre.RealExpr)10 AgreeEquation (com.rockwellcollins.atc.agree.analysis.ast.AgreeEquation)9 RecordAccessExpr (jkind.lustre.RecordAccessExpr)8 AgreeNodeBuilder (com.rockwellcollins.atc.agree.analysis.ast.AgreeNodeBuilder)7