use of jkind.lustre.IdExpr in project AGREE by loonwerks.
the class AgreeASTBuilder method getAssignmentStatements.
private List<AgreeStatement> getAssignmentStatements(EList<SpecStatement> specs) {
List<AgreeStatement> assigns = new ArrayList<>();
for (SpecStatement spec : specs) {
if (spec instanceof AssignStatement) {
Expr expr = doSwitch(((AssignStatement) spec).getExpr());
NamedElement id = ((AssignStatement) spec).getId();
expr = new BinaryExpr(new IdExpr(id.getName()), BinaryOp.EQUAL, expr);
assigns.add(new AgreeStatement("", expr, spec));
}
}
return assigns;
}
use of jkind.lustre.IdExpr in project AGREE by loonwerks.
the class AgreeASTBuilder method caseEnumLitExpr.
@Override
public Expr caseEnumLitExpr(EnumLitExpr aadlEnum) {
NamedElement ne = aadlEnum.getEnumType().getElm();
String typeStr = ne.getQualifiedName().replace("::", "__").replace(".", dotChar);
return new IdExpr(typeStr + "_" + aadlEnum.getValue());
}
use of jkind.lustre.IdExpr in project AGREE by loonwerks.
the class AgreeASTBuilder method nodeEqToEq.
// helper method for above
private Equation nodeEqToEq(NodeEq nodeEq) {
Expr expr = doSwitch(nodeEq.getExpr());
List<IdExpr> ids = new ArrayList<>();
for (Arg arg : nodeEq.getLhs()) {
ids.add(new IdExpr(arg.getName()));
}
Equation eq = new Equation(ids, expr);
return eq;
}
use of jkind.lustre.IdExpr in project AGREE by loonwerks.
the class AgreeASTBuilder method caseNodeDef.
@Override
public Expr caseNodeDef(NodeDef expr) {
String nodeName = AgreeUtils.getNodeName(expr);
for (Node node : globalNodes) {
if (node.id.equals(nodeName)) {
return null;
}
}
List<VarDecl> inputs = agreeVarsFromArgs(expr.getArgs(), null);
List<VarDecl> outputs = agreeVarsFromArgs(expr.getRets(), null);
NodeBodyExpr body = expr.getNodeBody();
List<VarDecl> internals = agreeVarsFromArgs(body.getLocs(), null);
List<Equation> eqs = new ArrayList<>();
List<String> props = new ArrayList<>();
// TODO are node lemmas deprecated?
String lemmaName = "__nodeLemma";
int lemmaIndex = 0;
for (NodeStmt stmt : body.getStmts()) {
if (stmt instanceof NodeLemma) {
NodeLemma nodeLemma = (NodeLemma) stmt;
String propName = lemmaName + lemmaIndex++;
IdExpr eqId = new IdExpr(propName);
internals.add(new VarDecl(eqId.id, NamedType.BOOL));
Expr eqExpr = doSwitch(nodeLemma.getExpr());
Equation eq = new Equation(eqId, eqExpr);
eqs.add(eq);
props.add(eqId.id);
} else if (stmt instanceof NodeEq) {
eqs.add(nodeEqToEq((NodeEq) stmt));
}
}
NodeBuilder builder = new NodeBuilder(nodeName);
builder.addInputs(inputs);
builder.addOutputs(outputs);
builder.addLocals(internals);
builder.addEquations(eqs);
builder.addProperties(props);
Node n = builder.build();
addToNodeList(n);
return null;
}
use of jkind.lustre.IdExpr in project AGREE by loonwerks.
the class LustreCondactNodeVisitor method translate.
public static Node translate(AgreeProgram agreeProgram, AgreeNode agreeNode, Node node) {
if (node.outputs.size() != 1) {
throw new AgreeException("We expect that this node only has a single output representing " + "all constraints for the contract");
}
LustreCondactNodeVisitor visitor = new LustreCondactNodeVisitor(agreeProgram, node);
NodeBuilder builder = new NodeBuilder(node);
builder.clearEquations();
builder.addInput(new AgreeVar(clockVarName, NamedType.BOOL, null));
addTickedEq(builder);
addInitEq(builder);
Expr holdExpr = new BoolExpr(true);
// make clock hold exprs
for (AgreeVar var : agreeNode.outputs) {
Expr varId = new IdExpr(var.id);
Expr preVar = new UnaryExpr(UnaryOp.PRE, varId);
holdExpr = new BinaryExpr(holdExpr, BinaryOp.AND, new BinaryExpr(varId, BinaryOp.EQUAL, preVar));
}
holdExpr = new BinaryExpr(new BoolExpr(true), BinaryOp.ARROW, holdExpr);
for (int i = 0; i < agreeNode.assumptions.size(); i++) {
Expr varId = new IdExpr(LustreAstBuilder.assumeSuffix + i);
Expr preVar = new UnaryExpr(UnaryOp.PRE, varId);
preVar = new BinaryExpr(new BoolExpr(true), BinaryOp.ARROW, preVar);
holdExpr = new BinaryExpr(holdExpr, BinaryOp.AND, new BinaryExpr(varId, BinaryOp.EQUAL, preVar));
}
holdExpr = expr("(not clk => holdExpr)", to("clk", clockVarName), to("holdExpr", holdExpr));
// make the constraint for the initial outputs
Expr initConstr = expr("not ticked => initExpr", to("ticked", tickedVarName), to("initExpr", agreeNode.initialConstraint));
// re-write the old expression using the visitor
for (Equation eq : node.equations) {
if (eq.lhs.size() != 1) {
throw new AgreeException("we expect that all eqs have a single lhs now");
}
IdExpr var = eq.lhs.get(0);
boolean isLocal = false;
for (VarDecl local : node.locals) {
if (local.id.equals(var.id)) {
isLocal = true;
break;
}
}
if (isLocal) {
Expr newExpr = eq.expr.accept(visitor);
newExpr = new IfThenElseExpr(new IdExpr(clockVarName), newExpr, new UnaryExpr(UnaryOp.PRE, var));
builder.addEquation(new Equation(eq.lhs, newExpr));
} else {
// this is the only output
Expr newExpr = eq.expr.accept(visitor);
newExpr = new BinaryExpr(new IdExpr(clockVarName), BinaryOp.IMPLIES, newExpr);
builder.addEquation(new Equation(eq.lhs, new BinaryExpr(initConstr, BinaryOp.AND, new BinaryExpr(holdExpr, BinaryOp.AND, newExpr))));
}
}
// this var equations should be populated by the visitor call above
builder.addEquations(visitor.stateVarEqs);
builder.addLocals(visitor.stateVars);
return builder.build();
}
Aggregations