use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.
the class AgreeCalendarUtils method getMNCalendar.
public static Node getMNCalendar(String nodeName, int max, int min) {
if (max < min || max < 1 || min < 1) {
throw new AgreeException("Malformed quasi-synchronous constraint");
}
List<VarDecl> inputs = new ArrayList<>();
inputs.add(new VarDecl("p", NamedType.BOOL));
inputs.add(new VarDecl("q", NamedType.BOOL));
List<VarDecl> outputs = new ArrayList<>();
outputs.add(new VarDecl("ok", NamedType.BOOL));
List<VarDecl> locals = new ArrayList<>();
List<Equation> eqs = new ArrayList<>();
Expr pId = new IdExpr("p");
Expr qId = new IdExpr("q");
for (int i = 0; i < max; i++) {
VarDecl countVar = new VarDecl("c_q_" + i, NamedType.INT);
IdExpr curId = new IdExpr("c_q_" + i);
IdExpr preId = new IdExpr("c_q_" + (i - 1));
locals.add(countVar);
Expr minExpr = new IntExpr(BigInteger.valueOf(min));
Expr zeroExpr = new IntExpr(BigInteger.ZERO);
Expr oneExpr = new IntExpr(BigInteger.ONE);
Expr minThenPre = new UnaryExpr(UnaryOp.PRE, preId);
minThenPre = new BinaryExpr(minExpr, BinaryOp.ARROW, minThenPre);
Expr elseExpr = new UnaryExpr(UnaryOp.PRE, curId);
elseExpr = new BinaryExpr(minExpr, BinaryOp.ARROW, elseExpr);
Expr thenExpr = (i == 0) ? oneExpr : new BinaryExpr(minThenPre, BinaryOp.PLUS, oneExpr);
Expr condExpr = new BinaryExpr(pId, BinaryOp.AND, qId);
Expr expr = new IfThenElseExpr(condExpr, thenExpr, elseExpr);
thenExpr = (i == 0) ? zeroExpr : minThenPre;
condExpr = new BinaryExpr(pId, BinaryOp.AND, new UnaryExpr(UnaryOp.NOT, qId));
expr = new IfThenElseExpr(condExpr, thenExpr, expr);
thenExpr = new UnaryExpr(UnaryOp.PRE, curId);
thenExpr = new BinaryExpr(minExpr, BinaryOp.ARROW, thenExpr);
thenExpr = new BinaryExpr(thenExpr, BinaryOp.PLUS, oneExpr);
condExpr = new BinaryExpr(qId, BinaryOp.AND, new UnaryExpr(UnaryOp.NOT, pId));
expr = new IfThenElseExpr(condExpr, thenExpr, expr);
eqs.add(new Equation(curId, expr));
}
Expr okExpr = new IdExpr("c_q_" + (max - 1));
okExpr = new UnaryExpr(UnaryOp.PRE, okExpr);
okExpr = new BinaryExpr(new IntExpr(BigInteger.valueOf(2 * min)), BinaryOp.ARROW, okExpr);
okExpr = new BinaryExpr(okExpr, BinaryOp.GREATEREQUAL, new IntExpr(BigInteger.valueOf(min)));
okExpr = new BinaryExpr(pId, BinaryOp.IMPLIES, okExpr);
eqs.add(new Equation(new IdExpr("ok"), okExpr));
NodeBuilder builder = new NodeBuilder(nodeName);
builder.addInputs(inputs);
builder.addOutputs(outputs);
builder.addLocals(locals);
builder.addEquations(eqs);
return builder.build();
}
use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.
the class AgreeCalendarUtils method getDFANode.
public static Node getDFANode(String name, int synchrony) {
if (synchrony <= 0) {
throw new AgreeException("Attempt to use quasi-synchrony of value: " + synchrony);
}
// VarDecl rVar = new VarDecl("_r",
// new SubrangeIntType(BigInteger.valueOf(-synchrony),
// BigInteger.valueOf(synchrony)));
VarDecl rVar = new VarDecl("_r", NamedType.INT);
IdExpr r = new IdExpr(rVar.id);
VarDecl rIsBoundedVar = new VarDecl("_r_is_bounded", NamedType.BOOL);
IdExpr rIsBounded = new IdExpr(rIsBoundedVar.id);
VarDecl outVar = new VarDecl("_out", NamedType.BOOL);
IdExpr out = new IdExpr(outVar.id);
VarDecl clkVar0 = new VarDecl("_clk0", NamedType.BOOL);
IdExpr p = new IdExpr(clkVar0.id);
VarDecl clkVar1 = new VarDecl("_clk1", NamedType.BOOL);
IdExpr q = new IdExpr(clkVar1.id);
List<VarDecl> inputs = new ArrayList<>();
inputs.add(clkVar0);
inputs.add(clkVar1);
List<VarDecl> outputs = new ArrayList<>();
outputs.add(outVar);
List<VarDecl> locals = new ArrayList<>();
locals.add(rVar);
locals.add(rIsBoundedVar);
Expr intZeroExpr = new IntExpr(BigInteger.ZERO);
Expr intOneExpr = new IntExpr(BigInteger.ONE);
Expr intNegOneExpr = new IntExpr(BigInteger.valueOf(-1));
Expr intSyncValExpr = new IntExpr(BigInteger.valueOf(synchrony));
Expr intNegSyncValxpr = new IntExpr(BigInteger.valueOf(-synchrony));
// (0 -> pre r)
Expr rPreExpr = new BinaryExpr(new IntExpr(BigInteger.ZERO), BinaryOp.ARROW, new UnaryExpr(UnaryOp.PRE, r));
// (0 -> pre r) < 0
Expr rPreLTExpr = new BinaryExpr(rPreExpr, BinaryOp.LESS, intZeroExpr);
// (0 -> pre r) > 0
Expr rPreGTExpr = new BinaryExpr(rPreExpr, BinaryOp.GREATER, intZeroExpr);
// (0 -> pre r) + 1
Expr rPrePlus = new BinaryExpr(rPreExpr, BinaryOp.PLUS, intOneExpr);
// (0 -> pre r) - 1
Expr rPreMinus = new BinaryExpr(rPreExpr, BinaryOp.MINUS, intOneExpr);
// if (0 -> pre r) < 0 then 1 else ((0 -> pre r) + 1)
Expr ifExpr0 = new IfThenElseExpr(rPreLTExpr, intOneExpr, rPrePlus);
// if (0 -> pre r) > 0 then -1 else ((0 -> pre r) - 1)
Expr ifExpr1 = new IfThenElseExpr(rPreGTExpr, intNegOneExpr, rPreMinus);
// if q then (if (0 -> pre r) > 0 then -1 else ((0 -> pre r) - 1)) else
// (0 -> pre r)
Expr ifExprClk1 = new IfThenElseExpr(q, ifExpr1, rPreExpr);
// if p then (if (0 -> pre r) < 0 then 1 else ((0 -> pre r) + 1))
// else if q then (if (0 -> pre r) > 0 then -1 else ((0 -> pre r) - 1))
// else (0 -> pre r);
Expr ifExprClk0 = new IfThenElseExpr(p, ifExpr0, ifExprClk1);
// if p and q then 0
// else if p then (if (0 -> pre r) < 0 then 1 else ((0 -> pre r) + 1))
// else if q then (if (0 -> pre r) > 0 then -1 else ((0 -> pre r) - 1))
// else (0 -> pre r);
Expr rExpr = new IfThenElseExpr(new BinaryExpr(p, BinaryOp.AND, q), intZeroExpr, ifExprClk0);
// ((0 -> pre r) >= 2 and p)
Expr condExpr0 = new BinaryExpr(new BinaryExpr(rPreExpr, BinaryOp.GREATEREQUAL, intSyncValExpr), BinaryOp.AND, p);
// ((0 -> pre r) <= -2 and q)
Expr condExpr1 = new BinaryExpr(new BinaryExpr(rPreExpr, BinaryOp.LESSEQUAL, intNegSyncValxpr), BinaryOp.AND, q);
// not (((0 -> pre r) >= 2 and p) or ((0 -> pre r) <= -2 and q))
Expr outExpr = new UnaryExpr(UnaryOp.NOT, new BinaryExpr(condExpr0, BinaryOp.OR, condExpr1));
// r <= 2 and r >= -2
Expr rIsBoundedExpr = new BinaryExpr(new BinaryExpr(r, BinaryOp.LESSEQUAL, intSyncValExpr), BinaryOp.AND, new BinaryExpr(r, BinaryOp.GREATEREQUAL, intNegSyncValxpr));
List<Equation> equations = new ArrayList<>();
equations.add(new Equation(r, rExpr));
equations.add(new Equation(rIsBounded, rIsBoundedExpr));
equations.add(new Equation(out, outExpr));
// TODO we used to have the subnode property in here, but it messes up kind2
List<String> properties = new ArrayList<>();
// properties.add(rIsBounded.id);
NodeBuilder builder = new NodeBuilder(name);
builder.addInputs(inputs);
builder.addOutputs(outputs);
builder.addLocals(locals);
builder.addEquations(equations);
builder.addProperties(properties);
return builder.build();
}
Aggregations