Search in sources :

Example 16 with NodeBuilder

use of jkind.lustre.builders.NodeBuilder 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 17 with NodeBuilder

use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.

the class LustreAstBuilder method removeProperties.

protected static Node removeProperties(Node node) {
    NodeBuilder builder = new NodeBuilder(node.id);
    builder.addInputs(node.inputs);
    builder.addOutputs(node.outputs);
    builder.addLocals(node.locals);
    builder.addEquations(node.equations);
    return builder.build();
}
Also used : NodeBuilder(jkind.lustre.builders.NodeBuilder) AgreeNodeBuilder(com.rockwellcollins.atc.agree.analysis.ast.AgreeNodeBuilder)

Example 18 with NodeBuilder

use of jkind.lustre.builders.NodeBuilder 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 19 with NodeBuilder

use of jkind.lustre.builders.NodeBuilder 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)

Example 20 with NodeBuilder

use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.

the class LustreContractAstBuilder method getContractLustreProgram.

public static Program getContractLustreProgram(AgreeProgram agreeProgram) {
    nodes = new ArrayList<>();
    List<TypeDef> types = AgreeUtils.getLustreTypes(agreeProgram);
    AgreeNode flatNode = flattenAgreeNodeKindContract(agreeProgram.topNode, "_TOP__");
    List<Expr> assertions = new ArrayList<>();
    List<VarDecl> locals = new ArrayList<>();
    List<VarDecl> inputs = new ArrayList<>();
    List<VarDecl> outputs = new ArrayList<>();
    List<Equation> equations = new ArrayList<>();
    List<String> properties = new ArrayList<>();
    List<Expr> requires = new ArrayList<>();
    List<Expr> ensures = new ArrayList<>();
    for (AgreeStatement assertion : flatNode.assertions) {
        assertions.add(assertion.expr);
    }
    for (AgreeStatement assumption : flatNode.assumptions) {
        requires.add(assumption.expr);
    }
    for (AgreeStatement guarantee : flatNode.lemmas) {
        ensures.add(guarantee.expr);
    }
    for (AgreeStatement guarantee : flatNode.guarantees) {
        ensures.add(guarantee.expr);
    }
    for (AgreeVar var : flatNode.inputs) {
        inputs.add(var);
    }
    for (AgreeVar var : flatNode.outputs) {
        outputs.add(var);
    }
    for (AgreeVar var : flatNode.outputs) {
        if (var.reference instanceof AssumeStatement || var.reference instanceof LemmaStatement) {
            throw new AgreeException("This shouldn't happen");
        }
    }
    Contract contract = new Contract(requires, ensures);
    NodeBuilder builder = new NodeBuilder("_TOP");
    builder.addInputs(inputs);
    builder.addOutputs(outputs);
    builder.addLocals(locals);
    builder.addEquations(equations);
    builder.addProperties(properties);
    builder.addAssertions(assertions);
    builder.setContract(contract);
    Node main = builder.build();
    nodes.addAll(agreeProgram.globalLustreNodes);
    nodes.add(main);
    Program program = new ProgramBuilder().addTypes(types).addNodes(nodes).setMain(main.id).build();
    return program;
}
Also used : AgreeNode(com.rockwellcollins.atc.agree.analysis.ast.AgreeNode) AgreeStatement(com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement) Program(jkind.lustre.Program) AgreeProgram(com.rockwellcollins.atc.agree.analysis.ast.AgreeProgram) AssumeStatement(com.rockwellcollins.atc.agree.agree.AssumeStatement) ProgramBuilder(jkind.lustre.builders.ProgramBuilder) AgreeNode(com.rockwellcollins.atc.agree.analysis.ast.AgreeNode) Node(jkind.lustre.Node) ArrayList(java.util.ArrayList) Equation(jkind.lustre.Equation) NodeBuilder(jkind.lustre.builders.NodeBuilder) AgreeNodeBuilder(com.rockwellcollins.atc.agree.analysis.ast.AgreeNodeBuilder) LemmaStatement(com.rockwellcollins.atc.agree.agree.LemmaStatement) AgreeVar(com.rockwellcollins.atc.agree.analysis.ast.AgreeVar) TypeDef(jkind.lustre.TypeDef) 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) AgreeException(com.rockwellcollins.atc.agree.analysis.AgreeException) Contract(jkind.lustre.Contract)

Aggregations

NodeBuilder (jkind.lustre.builders.NodeBuilder)37 Equation (jkind.lustre.Equation)30 VarDecl (jkind.lustre.VarDecl)30 IdExpr (jkind.lustre.IdExpr)29 BinaryExpr (jkind.lustre.BinaryExpr)28 Expr (jkind.lustre.Expr)26 UnaryExpr (jkind.lustre.UnaryExpr)24 BoolExpr (jkind.lustre.BoolExpr)23 NodeCallExpr (jkind.lustre.NodeCallExpr)22 ArrayList (java.util.ArrayList)18 IfThenElseExpr (jkind.lustre.IfThenElseExpr)17 IntExpr (jkind.lustre.IntExpr)15 Node (jkind.lustre.Node)11 AgreeVar (com.rockwellcollins.atc.agree.analysis.ast.AgreeVar)10 ProgramBuilder (jkind.lustre.builders.ProgramBuilder)10 Program (jkind.lustre.Program)9 AgreeNodeBuilder (com.rockwellcollins.atc.agree.analysis.ast.AgreeNodeBuilder)8 AgreeStatement (com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement)7 SimulationProgram (edu.uah.rsesc.aadlsimulator.agree.SimulationProgram)6 AgreeEquation (com.rockwellcollins.atc.agree.analysis.ast.AgreeEquation)5