use of jkind.lustre.IfThenElseExpr 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();
}
use of jkind.lustre.IfThenElseExpr 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();
}
use of jkind.lustre.IfThenElseExpr in project AGREE by loonwerks.
the class AgreeASTBuilder method caseIfThenElseExpr.
@Override
public Expr caseIfThenElseExpr(com.rockwellcollins.atc.agree.agree.IfThenElseExpr expr) {
Expr condExpr = doSwitch(expr.getA());
Expr thenExpr = doSwitch(expr.getB());
Expr elseExpr = doSwitch(expr.getC());
Expr result = new IfThenElseExpr(condExpr, thenExpr, elseExpr);
return result;
}
use of jkind.lustre.IfThenElseExpr in project AGREE by loonwerks.
the class AgreeCalendarUtils method queueMultiplexNode.
public static Node queueMultiplexNode(String nodeName, Type type, int numInputs) {
List<VarDecl> inputs = new ArrayList<>();
List<VarDecl> outputs = new ArrayList<>();
List<VarDecl> locals = new ArrayList<>();
List<IdExpr> clks = new ArrayList<>();
List<IdExpr> ins = new ArrayList<>();
List<Equation> eqs = new ArrayList<>();
for (int i = 0; i < numInputs; i++) {
IdExpr inId = new IdExpr("in" + i);
IdExpr clkId = new IdExpr("out" + i);
ins.add(inId);
clks.add(clkId);
inputs.add(new VarDecl(inId.id, type));
inputs.add(new VarDecl(clkId.id, NamedType.BOOL));
}
IdExpr output = new IdExpr("output");
outputs.add(new VarDecl(output.id, type));
// the output expression
// just an arbitrary value
Expr outExpr = ins.get(0);
for (int i = 0; i < numInputs; i++) {
outExpr = new IfThenElseExpr(clks.get(i), ins.get(i), outExpr);
}
Equation outEq = new Equation(output, outExpr);
eqs.add(outEq);
NodeBuilder builder = new NodeBuilder(nodeName);
builder.addInputs(inputs);
builder.addOutputs(outputs);
builder.addLocals(locals);
builder.addEquations(eqs);
return builder.build();
}
use of jkind.lustre.IfThenElseExpr 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();
}
Aggregations