use of jkind.lustre.Equation in project AGREE by loonwerks.
the class RemovePropertySatisficationRequirements method transform.
// private final static String assumeBaseId = "__ASSUME";
public static SimulationProgram transform(final SimulationProgram program) {
final SimulationProgramBuilder simulationProgramBuilder = new SimulationProgramBuilder(program);
final ProgramBuilder lustreProgramBuilder = new ProgramBuilder(program.getLustreProgram());
lustreProgramBuilder.clearNodes();
for (final Node node : program.getLustreProgram().nodes) {
final NodeBuilder nodeBuilder = new NodeBuilder(new AstMapVisitor() {
@Override
public Node visit(final Node n) {
return super.visit(n);
}
@Override
public Equation visit(final Equation e) {
// Force the assumption conjunction to be true
if (e.lhs.size() == 1 && e.lhs.get(0).id.equals(assumptionConjunctionId)) {
return new Equation(new IdExpr(assumptionConjunctionId), new BoolExpr(true));
}
final Equation result = super.visit(e);
return result;
}
}.visit(node));
lustreProgramBuilder.addNode(nodeBuilder.build());
}
simulationProgramBuilder.setLustreProgram(lustreProgramBuilder.build());
return simulationProgramBuilder.build();
}
use of jkind.lustre.Equation in project AGREE by loonwerks.
the class ReplaceFollowedByOperator method transform.
public static SimulationProgram transform(final SimulationProgram program) {
final Program lustreProgram = program.getLustreProgram();
if (lustreProgram.nodes.size() != 1) {
throw new IllegalArgumentException("Only lustre programs with exactly one node are supported");
}
final ReplaceFollowedByOperator visitor = new ReplaceFollowedByOperator();
final NodeBuilder nodeBuilder = new NodeBuilder(visitor.visit(lustreProgram.getMainNode()));
// Create variable for the step number
nodeBuilder.addInput(new VarDecl(stepVariableId, NamedType.INT));
// Add an output for the next step number
nodeBuilder.addOutput(new VarDecl(nextStepVariableId, NamedType.INT));
nodeBuilder.addEquation(new Equation(new IdExpr(nextStepVariableId), new BinaryExpr(new IdExpr(stepVariableId), BinaryOp.PLUS, new IntExpr(1))));
// Create the new lustre program using the new node
final ProgramBuilder lustreProgramBuilder = new ProgramBuilder(lustreProgram);
lustreProgramBuilder.clearNodes();
lustreProgramBuilder.addNode(nodeBuilder.build());
// Create the simulation program
final SimulationProgramBuilder simulationProgramBuilder = new SimulationProgramBuilder(program);
final Expr stepVariableExpr = new IdExpr(stepVariableId);
// Ensure that the initial step is greater than the first step when simulating inductive counterexamples
final int initialStepValue = program.getType().isInductive() ? firstStepValue + 1 : firstStepValue;
simulationProgramBuilder.addInitialConstraint(new BinaryExpr(stepVariableExpr, BinaryOp.EQUAL, new IntExpr(initialStepValue)));
simulationProgramBuilder.addCarryVariable(new CarryVariable(stepVariableExpr, new IdExpr(nextStepVariableId)));
simulationProgramBuilder.setLustreProgram(lustreProgramBuilder.build());
return simulationProgramBuilder.build();
}
use of jkind.lustre.Equation in project AGREE by loonwerks.
the class AgreeASTBuilder method caseFnDef.
@Override
public Expr caseFnDef(FnDef fnDef) {
String nodeName = AgreeUtils.getNodeName(fnDef).replace("::", "__");
for (Node node : globalNodes) {
if (node.id.equals(nodeName)) {
return null;
}
}
List<VarDecl> inputs = agreeVarsFromArgs(fnDef.getArgs(), null);
Expr bodyExpr = doSwitch(fnDef.getExpr());
// EGM: array-backend
// Type outType = getNamedType(AgreeTypeUtils.getTypeName(fnDef.getType(), typeMap, globalTypes));
Type outType = symbolTable.updateLustreTypeMap(AgreeTypeSystem.typeDefFromType(fnDef.getType()));
if (outType != null) {
VarDecl outVar = new VarDecl("_outvar", outType);
List<VarDecl> outputs = Collections.singletonList(outVar);
Equation eq = new Equation(new IdExpr("_outvar"), bodyExpr);
List<Equation> eqs = Collections.singletonList(eq);
NodeBuilder builder = new NodeBuilder(nodeName);
builder.addInputs(inputs);
builder.addOutputs(outputs);
builder.addEquations(eqs);
Node node = builder.build();
addToNodeList(node);
}
return null;
}
use of jkind.lustre.Equation 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.Equation 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