Search in sources :

Example 1 with UnaryExpr

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

the class RemoveCondacts method clockOutputs.

private Node clockOutputs(Node node, IdExpr clock) {
    NodeBuilder builder = new NodeBuilder(node);
    builder.clearOutputs();
    builder.addLocals(node.outputs);
    for (VarDecl output : node.outputs) {
        VarDecl dflt = new VarDecl(output.id + namePrefix + "default", output.type);
        builder.addInput(dflt);
        VarDecl clocked = new VarDecl(output.id + namePrefix + "clocked", output.type);
        builder.addOutput(clocked);
        // clocked = if clock then output else (default -> pre clocked)
        Equation eq = new Equation(new IdExpr(clocked.id), new IfThenElseExpr(clock, new IdExpr(output.id), new BinaryExpr(new IdExpr(dflt.id), BinaryOp.ARROW, new UnaryExpr(UnaryOp.PRE, new IdExpr(clocked.id)))));
        builder.addEquation(eq);
    }
    return builder.build();
}
Also used : IdExpr(jkind.lustre.IdExpr) VarDecl(jkind.lustre.VarDecl) BinaryExpr(jkind.lustre.BinaryExpr) Equation(jkind.lustre.Equation) NodeBuilder(jkind.lustre.builders.NodeBuilder) UnaryExpr(jkind.lustre.UnaryExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr)

Example 2 with UnaryExpr

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

the class RemoveCondacts method clockArrowsAndPres.

private Node clockArrowsAndPres(Node node, final IdExpr clock) {
    final VarDecl init = new VarDecl(namePrefix + "init", NamedType.BOOL);
    final List<Equation> preEquations = new ArrayList<>();
    final List<VarDecl> preLocals = new ArrayList<>();
    node = (Node) node.accept(new AstMapVisitor() {

        private int counter = 0;

        @Override
        public Expr visit(BinaryExpr e) {
            if (e.op == BinaryOp.ARROW) {
                return new IfThenElseExpr(new IdExpr(init.id), e.left.accept(this), e.right.accept(this));
            } else {
                return super.visit(e);
            }
        }

        @Override
        public Expr visit(UnaryExpr e) {
            if (e.op == UnaryOp.PRE) {
                String state = namePrefix + "state" + counter++;
                Type type = e.expr.accept(typeReconstructor);
                preLocals.add(new VarDecl(state, type));
                // state = if clock then expr else pre state
                preEquations.add(new Equation(new IdExpr(state), new IfThenElseExpr(clock, e.expr.accept(this), new UnaryExpr(UnaryOp.PRE, new IdExpr(state)))));
                return new UnaryExpr(UnaryOp.PRE, new IdExpr(state));
            } else {
                return super.visit(e);
            }
        }
    });
    NodeBuilder builder = new NodeBuilder(node);
    builder.addLocals(preLocals);
    builder.addLocal(init);
    builder.addEquations(preEquations);
    // init = true -> if pre clock then false else pre init
    builder.addEquation(new Equation(new IdExpr(init.id), new BinaryExpr(new BoolExpr(true), BinaryOp.ARROW, new IfThenElseExpr(new UnaryExpr(UnaryOp.PRE, clock), new BoolExpr(false), new UnaryExpr(UnaryOp.PRE, new IdExpr(init.id))))));
    return builder.build();
}
Also used : BoolExpr(jkind.lustre.BoolExpr) IdExpr(jkind.lustre.IdExpr) BinaryExpr(jkind.lustre.BinaryExpr) ArrayList(java.util.ArrayList) Equation(jkind.lustre.Equation) NodeBuilder(jkind.lustre.builders.NodeBuilder) UnaryExpr(jkind.lustre.UnaryExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) AstMapVisitor(jkind.lustre.visitors.AstMapVisitor) Type(jkind.lustre.Type) NamedType(jkind.lustre.NamedType) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) UnaryExpr(jkind.lustre.UnaryExpr) CondactExpr(jkind.lustre.CondactExpr) Expr(jkind.lustre.Expr) IdExpr(jkind.lustre.IdExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) VarDecl(jkind.lustre.VarDecl)

Example 3 with UnaryExpr

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

the class AgreeASTBuilder method casePrevExpr.

@Override
public Expr casePrevExpr(PrevExpr expr) {
    Expr delayExpr = doSwitch(expr.getDelay());
    Expr initExpr = doSwitch(expr.getInit());
    Expr preExpr = new UnaryExpr(UnaryOp.PRE, delayExpr);
    Expr res = new BinaryExpr(initExpr, BinaryOp.ARROW, preExpr);
    return res;
}
Also used : EnumLitExpr(com.rockwellcollins.atc.agree.agree.EnumLitExpr) IndicesExpr(com.rockwellcollins.atc.agree.agree.IndicesExpr) TimeRiseExpr(com.rockwellcollins.atc.agree.agree.TimeRiseExpr) RecordAccessExpr(jkind.lustre.RecordAccessExpr) FlatmapExpr(com.rockwellcollins.atc.agree.agree.FlatmapExpr) TimeFallExpr(com.rockwellcollins.atc.agree.agree.TimeFallExpr) RealLitExpr(com.rockwellcollins.atc.agree.agree.RealLitExpr) GetPropertyExpr(com.rockwellcollins.atc.agree.agree.GetPropertyExpr) Expr(jkind.lustre.Expr) CastExpr(jkind.lustre.CastExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) TimeOfExpr(com.rockwellcollins.atc.agree.agree.TimeOfExpr) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) RealExpr(jkind.lustre.RealExpr) ArrayExpr(jkind.lustre.ArrayExpr) PrevExpr(com.rockwellcollins.atc.agree.agree.PrevExpr) IdExpr(jkind.lustre.IdExpr) TimeExpr(com.rockwellcollins.atc.agree.agree.TimeExpr) FoldRightExpr(com.rockwellcollins.atc.agree.agree.FoldRightExpr) TagExpr(com.rockwellcollins.atc.agree.agree.TagExpr) EventExpr(com.rockwellcollins.atc.agree.agree.EventExpr) LatchedExpr(com.rockwellcollins.atc.agree.agree.LatchedExpr) NamedElmExpr(com.rockwellcollins.atc.agree.agree.NamedElmExpr) FunctionCallExpr(jkind.lustre.FunctionCallExpr) SelectionExpr(com.rockwellcollins.atc.agree.agree.SelectionExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) TupleExpr(jkind.lustre.TupleExpr) UnaryExpr(jkind.lustre.UnaryExpr) ArraySubExpr(com.rockwellcollins.atc.agree.agree.ArraySubExpr) IntExpr(jkind.lustre.IntExpr) PreExpr(com.rockwellcollins.atc.agree.agree.PreExpr) RecordLitExpr(com.rockwellcollins.atc.agree.agree.RecordLitExpr) ExistsExpr(com.rockwellcollins.atc.agree.agree.ExistsExpr) FoldLeftExpr(com.rockwellcollins.atc.agree.agree.FoldLeftExpr) RecordUpdateExpr(com.rockwellcollins.atc.agree.agree.RecordUpdateExpr) ForallExpr(com.rockwellcollins.atc.agree.agree.ForallExpr) ArrayAccessExpr(jkind.lustre.ArrayAccessExpr) ArrayUpdateExpr(com.rockwellcollins.atc.agree.agree.ArrayUpdateExpr) BoolLitExpr(com.rockwellcollins.atc.agree.agree.BoolLitExpr) NodeBodyExpr(com.rockwellcollins.atc.agree.agree.NodeBodyExpr) IntLitExpr(com.rockwellcollins.atc.agree.agree.IntLitExpr) CallExpr(com.rockwellcollins.atc.agree.agree.CallExpr) ArrayLiteralExpr(com.rockwellcollins.atc.agree.agree.ArrayLiteralExpr) BinaryExpr(jkind.lustre.BinaryExpr) UnaryExpr(jkind.lustre.UnaryExpr)

Example 4 with UnaryExpr

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

the class AgreeCalendarUtils method getExplicitCalendarNode.

public static Node getExplicitCalendarNode(String nodeName, List<IdExpr> calendar, List<Expr> clocks) {
    // filter the calendar if some clocks are not present
    List<IdExpr> filteredCalendar = new ArrayList<>();
    Map<String, List<Integer>> clockTickMap = new HashMap<>();
    for (IdExpr calId : calendar) {
        for (Expr clockExpr : clocks) {
            IdExpr clockId = (IdExpr) clockExpr;
            if (calId.id.equals(clockId.id)) {
                filteredCalendar.add(clockId);
                break;
            }
        }
    }
    int i = 0;
    for (IdExpr clockId : filteredCalendar) {
        List<Integer> ticks = clockTickMap.get(clockId.id);
        if (ticks == null) {
            ticks = new ArrayList<>();
            clockTickMap.put(clockId.id, ticks);
        }
        ticks.add(i++);
    }
    for (Expr clockExpr : clocks) {
        IdExpr clockId = (IdExpr) clockExpr;
        if (clockTickMap.get(clockId.id) == null) {
            throw new AgreeException("Clock Id '" + clockId.id + "' is not present in calendar statement");
        }
    }
    // add all of the clocks to to the inputs of the node
    List<VarDecl> inputs = new ArrayList<>();
    for (Expr clockExpr : clocks) {
        VarDecl input = new VarDecl(((IdExpr) clockExpr).id, NamedType.BOOL);
        inputs.add(input);
    }
    // the output is the variable asserting the calendar
    List<VarDecl> outputs = new ArrayList<>();
    IdExpr outputAssert = new IdExpr("__CALENDAR_ASSERTION");
    outputs.add(new VarDecl(outputAssert.id, NamedType.BOOL));
    // create a variable that counts through the calendar elements
    List<VarDecl> locals = new ArrayList<>();
    VarDecl clockCounterVar = new VarDecl("__CALANDER_COUNTER", NamedType.INT);
    locals.add(clockCounterVar);
    List<Equation> equations = new ArrayList<>();
    // create the expression for the counter variable
    IdExpr clockCountId = new IdExpr(clockCounterVar.id);
    IntExpr calendarSize = new IntExpr(BigInteger.valueOf(filteredCalendar.size() - 1));
    Expr preClockCount = new UnaryExpr(UnaryOp.PRE, clockCountId);
    Expr preLast = new BinaryExpr(preClockCount, BinaryOp.EQUAL, calendarSize);
    Expr prePlus = new BinaryExpr(preClockCount, BinaryOp.PLUS, new IntExpr(BigInteger.ONE));
    Expr ifClock = new IfThenElseExpr(preLast, new IntExpr(BigInteger.ZERO), prePlus);
    Expr clockArrow = new BinaryExpr(new IntExpr(BigInteger.ZERO), BinaryOp.ARROW, ifClock);
    Equation clockCountEq = new Equation(clockCountId, clockArrow);
    equations.add(clockCountEq);
    // create constraints for which calendar element is ticking
    Expr calendarConstraint = new BoolExpr(true);
    for (Expr clockExpr : clocks) {
        IdExpr clockId = (IdExpr) clockExpr;
        List<Integer> ticks = clockTickMap.get(clockId.id);
        Expr clockTicking = new BoolExpr(false);
        for (Integer tick : ticks) {
            Expr clockIsTickValue = new BinaryExpr(clockCountId, BinaryOp.EQUAL, new IntExpr(BigInteger.valueOf(tick.longValue())));
            clockTicking = new BinaryExpr(clockTicking, BinaryOp.OR, clockIsTickValue);
        }
        Expr ifExpr = new IfThenElseExpr(clockTicking, clockId, new UnaryExpr(UnaryOp.NOT, clockId));
        calendarConstraint = new BinaryExpr(calendarConstraint, BinaryOp.AND, ifExpr);
    }
    Equation outEq = new Equation(outputAssert, calendarConstraint);
    equations.add(outEq);
    NodeBuilder builder = new NodeBuilder(nodeName);
    builder.addInputs(inputs);
    builder.addOutputs(outputs);
    builder.addLocals(locals);
    builder.addEquations(equations);
    return builder.build();
}
Also used : BoolExpr(jkind.lustre.BoolExpr) IdExpr(jkind.lustre.IdExpr) HashMap(java.util.HashMap) BinaryExpr(jkind.lustre.BinaryExpr) ArrayList(java.util.ArrayList) Equation(jkind.lustre.Equation) NodeBuilder(jkind.lustre.builders.NodeBuilder) UnaryExpr(jkind.lustre.UnaryExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) BigInteger(java.math.BigInteger) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) UnaryExpr(jkind.lustre.UnaryExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) Expr(jkind.lustre.Expr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) IntExpr(jkind.lustre.IntExpr) IdExpr(jkind.lustre.IdExpr) VarDecl(jkind.lustre.VarDecl) ArrayList(java.util.ArrayList) List(java.util.List) IntExpr(jkind.lustre.IntExpr)

Example 5 with UnaryExpr

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

the class AgreeCalendarUtils method queueCircleNode.

public static Node queueCircleNode(String nodeName, Type type, int queueSize) {
    List<VarDecl> inputs = new ArrayList<>();
    List<VarDecl> outputs = new ArrayList<>();
    List<VarDecl> locals = new ArrayList<>();
    List<IdExpr> els = new ArrayList<>();
    List<Equation> eqs = new ArrayList<>();
    String elBase = "el";
    IdExpr elemIn = new IdExpr("el_in");
    IdExpr elemOut = new IdExpr("el_out");
    IdExpr insert = new IdExpr("insert");
    IdExpr remove = new IdExpr("remove");
    IdExpr output = new IdExpr("output");
    IdExpr input = new IdExpr("input");
    IdExpr numEls = new IdExpr("num_els");
    inputs.add(new VarDecl(input.id, type));
    inputs.add(new VarDecl(elemIn.id, NamedType.BOOL));
    inputs.add(new VarDecl(elemOut.id, NamedType.BOOL));
    outputs.add(new VarDecl(numEls.id, NamedType.INT));
    outputs.add(new VarDecl(output.id, type));
    locals.add(new VarDecl(insert.id, NamedType.INT));
    locals.add(new VarDecl(remove.id, NamedType.INT));
    for (int i = 0; i < queueSize; i++) {
        IdExpr el = new IdExpr(elBase + i);
        els.add(el);
        locals.add(new VarDecl(el.id, type));
    }
    // equations for insert
    Expr preInsert = new UnaryExpr(UnaryOp.PRE, insert);
    Expr preInsertMore = new BinaryExpr(preInsert, BinaryOp.PLUS, new IntExpr(BigInteger.ONE));
    Expr insertIf0 = new IfThenElseExpr(elemIn, preInsertMore, preInsert);
    Expr insertIfCond = new BinaryExpr(preInsert, BinaryOp.EQUAL, new IntExpr(BigInteger.valueOf(queueSize - 1)));
    insertIfCond = new BinaryExpr(elemIn, BinaryOp.AND, insertIfCond);
    Expr insertIf1 = new IfThenElseExpr(insertIfCond, new IntExpr(BigInteger.ZERO), insertIf0);
    Expr insertIf2 = new IfThenElseExpr(elemIn, new IntExpr(BigInteger.ONE), new IntExpr(BigInteger.ZERO));
    Expr insertExpr = new BinaryExpr(insertIf2, BinaryOp.ARROW, insertIf1);
    Equation insertEq = new Equation(insert, insertExpr);
    eqs.add(insertEq);
    // equations for remove
    Expr preRemove = new UnaryExpr(UnaryOp.PRE, remove);
    Expr preRemoveMore = new BinaryExpr(preRemove, BinaryOp.PLUS, new IntExpr(BigInteger.ONE));
    Expr removeIf0 = new IfThenElseExpr(elemOut, preRemoveMore, preRemove);
    Expr removeIfCond = new BinaryExpr(preRemove, BinaryOp.EQUAL, new IntExpr(BigInteger.valueOf(queueSize - 1)));
    removeIfCond = new BinaryExpr(elemOut, BinaryOp.AND, removeIfCond);
    Expr removeExpr = new IfThenElseExpr(removeIfCond, new IntExpr(BigInteger.ZERO), removeIf0);
    removeExpr = new BinaryExpr(new IntExpr(BigInteger.ZERO), BinaryOp.ARROW, removeExpr);
    Equation removeEq = new Equation(remove, removeExpr);
    eqs.add(removeEq);
    Expr preElemIn = new UnaryExpr(UnaryOp.PRE, elemIn);
    Expr preElemOut = new UnaryExpr(UnaryOp.PRE, elemOut);
    Expr preNumEls = new UnaryExpr(UnaryOp.PRE, numEls);
    Expr preNumElsMore = new BinaryExpr(preNumEls, BinaryOp.PLUS, new IntExpr(BigInteger.ONE));
    Expr preNumElsLessExpr = new BinaryExpr(preNumEls, BinaryOp.MINUS, new IntExpr(BigInteger.ONE));
    Expr numElsIf0 = new IfThenElseExpr(preElemIn, preNumElsMore, preNumEls);
    Expr numElsExpr = new IfThenElseExpr(preElemOut, preNumElsLessExpr, numElsIf0);
    numElsExpr = new BinaryExpr(new IntExpr(BigInteger.ZERO), BinaryOp.ARROW, numElsExpr);
    Equation numElsEq = new Equation(numEls, numElsExpr);
    eqs.add(numElsEq);
    // equation for the output
    Expr outputExpr = els.get(queueSize - 1);
    for (int i = 0; i < queueSize - 1; i++) {
        Expr cond = new BinaryExpr(preRemove, BinaryOp.EQUAL, new IntExpr(BigInteger.valueOf(i)));
        outputExpr = new IfThenElseExpr(cond, els.get(i), outputExpr);
    }
    outputExpr = new BinaryExpr(els.get(0), BinaryOp.ARROW, outputExpr);
    Equation outputEq = new Equation(output, outputExpr);
    eqs.add(outputEq);
    // equations for each queue element
    for (int i = 0; i < queueSize; i++) {
        Expr preEl = new UnaryExpr(UnaryOp.PRE, els.get(i));
        Expr cond = new UnaryExpr(UnaryOp.PRE, insert);
        cond = new BinaryExpr(cond, BinaryOp.EQUAL, new IntExpr(BigInteger.valueOf(i)));
        cond = new BinaryExpr(elemIn, BinaryOp.AND, cond);
        Expr elemIfExpr = new IfThenElseExpr(cond, input, preEl);
        Expr elExpr = new BinaryExpr(input, BinaryOp.ARROW, elemIfExpr);
        Equation elEq = new Equation(els.get(i), elExpr);
        eqs.add(elEq);
    }
    // queue properties:
    List<String> props = new ArrayList<>();
    // don't remove more than are present:
    // Expr propExpr0 = new BinaryExpr(preRemove, BinaryOp.EQUAL,
    // preInsert);
    // Expr propExpr1 = new BinaryExpr(remove, BinaryOp.EQUAL, preRemove);
    // Expr propImpl = new BinaryExpr(propExpr0, BinaryOp.IMPLIES,
    // propExpr1);
    // Expr propArrow = new BinaryExpr(remove, BinaryOp.LESSEQUAL, insert);
    // propArrow = new BinaryExpr(propArrow, BinaryOp.ARROW, propImpl);
    Expr propExpr = new BinaryExpr(numEls, BinaryOp.GREATEREQUAL, new IntExpr(BigInteger.ZERO));
    IdExpr propId0 = new IdExpr("__REMOVE_LTE_INSERT_" + nodeName);
    locals.add(new VarDecl(propId0.id, NamedType.BOOL));
    Equation propEq0 = new Equation(propId0, propExpr);
    eqs.add(propEq0);
    props.add(propId0.id);
    NodeBuilder builder = new NodeBuilder(nodeName);
    builder.addInputs(inputs);
    builder.addOutputs(outputs);
    builder.addLocals(locals);
    builder.addEquations(eqs);
    builder.addProperties(props);
    return builder.build();
}
Also used : IdExpr(jkind.lustre.IdExpr) BinaryExpr(jkind.lustre.BinaryExpr) ArrayList(java.util.ArrayList) Equation(jkind.lustre.Equation) NodeBuilder(jkind.lustre.builders.NodeBuilder) UnaryExpr(jkind.lustre.UnaryExpr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) UnaryExpr(jkind.lustre.UnaryExpr) NodeCallExpr(jkind.lustre.NodeCallExpr) Expr(jkind.lustre.Expr) IfThenElseExpr(jkind.lustre.IfThenElseExpr) IntExpr(jkind.lustre.IntExpr) IdExpr(jkind.lustre.IdExpr) VarDecl(jkind.lustre.VarDecl) IntExpr(jkind.lustre.IntExpr)

Aggregations

UnaryExpr (jkind.lustre.UnaryExpr)36 BinaryExpr (jkind.lustre.BinaryExpr)34 IdExpr (jkind.lustre.IdExpr)34 Expr (jkind.lustre.Expr)32 BoolExpr (jkind.lustre.BoolExpr)29 NodeCallExpr (jkind.lustre.NodeCallExpr)29 IfThenElseExpr (jkind.lustre.IfThenElseExpr)24 Equation (jkind.lustre.Equation)21 IntExpr (jkind.lustre.IntExpr)18 VarDecl (jkind.lustre.VarDecl)17 NodeBuilder (jkind.lustre.builders.NodeBuilder)15 AgreeVar (com.rockwellcollins.atc.agree.analysis.ast.AgreeVar)12 ArrayList (java.util.ArrayList)11 RealExpr (jkind.lustre.RealExpr)11 AgreeStatement (com.rockwellcollins.atc.agree.analysis.ast.AgreeStatement)9 ArrayAccessExpr (jkind.lustre.ArrayAccessExpr)6 Node (jkind.lustre.Node)6 RecordAccessExpr (jkind.lustre.RecordAccessExpr)6 AgreeEquation (com.rockwellcollins.atc.agree.analysis.ast.AgreeEquation)4 CondactExpr (jkind.lustre.CondactExpr)4