use of jkind.lustre.IdExpr 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();
}
use of jkind.lustre.IdExpr in project AGREE by loonwerks.
the class AgreeUtils method logCycleWarning.
// warns the user if there is a cycle
public static void logCycleWarning(List<Equation> eqs, AgreeRenaming agreeRename, boolean throwException) {
Map<String, Set<String>> idGraph = new HashMap<>();
List<String> ids = new ArrayList<>();
AgreeCycleVisitor visitor = new AgreeCycleVisitor();
for (Equation eq : eqs) {
for (IdExpr id : eq.lhs) {
ids.add(id.id);
idGraph.put(id.id, eq.expr.accept(visitor));
}
}
Set<String> discovered = new HashSet<>();
StringBuilder exceptionStr = new StringBuilder();
for (String id : ids) {
if (discovered.contains(id)) {
continue;
}
List<String> cycle = cycleWarning_Helper(id, new HashSet<String>(), idGraph);
if (cycle != null) {
discovered.addAll(cycle);
String aadlString = agreeRename.rename(id);
StringBuilder cycleStr = new StringBuilder("Possible cycle: " + aadlString);
String sep = " -> ";
for (String cycleId : cycle) {
cycleStr.append(sep);
aadlString = agreeRename.rename(cycleId);
cycleStr.append(aadlString);
}
AgreeLogger.logWarning(cycleStr.toString());
exceptionStr.append(cycleStr);
}
}
if (throwException && !discovered.isEmpty()) {
throw new AgreeCombinationalCycleException(exceptionStr.toString());
}
}
use of jkind.lustre.IdExpr in project AGREE by loonwerks.
the class AgreeInlineLatchedConnections method addLatchedInputEqs.
private void addLatchedInputEqs(AgreeNodeBuilder builder, AgreeNode subNode) {
for (AgreeVar var : subNode.inputs) {
AgreeVar latchVar = new AgreeVar(subNode.id + "__" + var.id + LATCHED_SUFFIX, var.type, var.reference, var.compInst, var.featInst);
builder.addLocal(latchVar);
Expr clockExpr = new IdExpr(subNode.id + AgreeASTBuilder.clockIDSuffix);
String sourceVarName = subNode.id + "__" + var.id;
Equation latchEq = equation("latchVar = " + sourceVarName + " -> if (pre (not clockVar)) and clockVar then " + sourceVarName + " else pre latchVar;", to("latchVar", latchVar), to("clockVar", clockExpr));
builder.addLocalEquation(new AgreeEquation(latchEq, null));
}
}
use of jkind.lustre.IdExpr in project AGREE by loonwerks.
the class AgreeMakeClockedLustreNodes method getClockedNode.
private Node getClockedNode(String node) {
Node clockedNode = clockedNodeMap.get(node);
if (clockedNode == null) {
Node originalNode = null;
for (Node progNode : origProgram.globalLustreNodes) {
if (progNode.id == node) {
originalNode = progNode;
break;
}
}
NodeBuilder builder = new NodeBuilder(originalNode);
builder.setId(clockedNodePrefix + originalNode.id);
builder.clearEquations();
builder.clearInputs();
builder.addInput(new VarDecl(clockVarName, NamedType.BOOL));
builder.addInput(new VarDecl(initVarName, NamedType.BOOL));
builder.addInputs(originalNode.inputs);
AgreeMakeClockedLustreNodes visitor = new AgreeMakeClockedLustreNodes(this, originalNode);
for (Equation eq : originalNode.equations) {
Expr newExpr = eq.expr.accept(visitor);
// this will make an unguarded pre expression, but any non initialized
// outputs should be masked by the init expression in the calling agree node
newExpr = new IfThenElseExpr(new IdExpr(clockVarName), newExpr, new UnaryExpr(UnaryOp.PRE, eq.lhs.get(0)));
builder.addEquation(new Equation(eq.lhs, newExpr));
clockedNodeMap.putAll(visitor.clockedNodeMap);
}
builder.addLocals(visitor.stateVars);
builder.addEquations(visitor.stateVarEqs);
clockedNode = builder.build();
clockedNodeMap.put(node, clockedNode);
}
return clockedNode;
}
use of jkind.lustre.IdExpr 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));
}
Aggregations