use of jkind.lustre.Expr 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.Expr 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.Expr in project AGREE by loonwerks.
the class FrameAssertionHelper method addNextFrameAssertions.
/**
* Adds the frame specific assertions for the next frame to an assertion collection. Does not add assertions related to property enablement.
* @param program
* @param lastResults
* @param assertions is the collection to which assertions will be added.
*/
public static void addNextFrameAssertions(final SimulationProgram program, final SimulationFrameResults lastResults, final Collection<Expr> assertions) {
if (lastResults == null) {
for (final Expr c : program.getInitialConstraints()) {
assertions.add(c);
}
} else {
for (final CarryVariable cv : program.getCarryVariables()) {
final Value value = lastResults.getValue(cv.getOutputVariableExpression().toString());
if (value == null) {
throw new RuntimeException("Unable to get value for: " + cv.getOutputVariableExpression().toString());
}
final Expr valueExpression = valueToExpr(value);
assertions.add(new BinaryExpr(cv.getInputVariableExpression(), BinaryOp.EQUAL, valueExpression));
}
}
}
use of jkind.lustre.Expr in project AGREE by loonwerks.
the class CreateLocalVariablesForPropertyExpressions method transform.
public static SimulationProgram transform(final SimulationProgram program) {
final Program lustreProgram = program.getLustreProgram();
final SimulationProgramBuilder builder = new SimulationProgramBuilder(program);
// Build mappings between Agree Statements, expressions, and Agree Nodes
final Map<Expr, AgreeStatement> exprToStatementMap = new HashMap<>();
final Map<AgreeStatement, AgreeNode> agreeStatementToAgreeNodeMap = new HashMap<>();
for (final AgreeNode agreeNode : program.getAllAgreeNodes()) {
for (final AgreeStatement statement : agreeNode.assertions) {
if (statement.reference instanceof AssertStatement) {
exprToStatementMap.put(statement.expr, statement);
agreeStatementToAgreeNodeMap.put(statement, agreeNode);
}
}
for (final AgreeStatement statement : agreeNode.assumptions) {
exprToStatementMap.put(statement.expr, statement);
agreeStatementToAgreeNodeMap.put(statement, agreeNode);
}
for (final AgreeStatement statement : agreeNode.guarantees) {
exprToStatementMap.put(statement.expr, statement);
agreeStatementToAgreeNodeMap.put(statement, agreeNode);
}
}
// Create local variables for assert statements, assumptions, and guarantees
final ProgramBuilder lustreBuilder = new ProgramBuilder(lustreProgram).clearNodes();
for (final Node lustreNode : lustreProgram.nodes) {
lustreBuilder.addNode(VariableCreator.transform(lustreNode, exprToStatementMap, agreeStatementToAgreeNodeMap));
}
builder.setLustreProgram(lustreBuilder.build());
return builder.build();
}
use of jkind.lustre.Expr in project AGREE by loonwerks.
the class AGREESimulationState method createEvaluator.
private Evaluator createEvaluator(final Collection<Expr> constraints, final Set<SimulationProperty> disabledProperties) {
try {
final SimulationFrameResults lastFrameResults = frameInfos.size() == 0 ? null : frameInfos.get(frameInfos.size() - 1).getFrameResults();
final List<Expr> assertions = new ArrayList<Expr>();
FrameAssertionHelper.addNextFrameAssertions(simulationProgram, lastFrameResults, assertions);
assertions.addAll(constraints);
// Add assertions for property enablement variables
for (final SimulationProperty simProp : simulationProgram.getSimulationProperties()) {
if (simProp.getEnablementVariableId() != null) {
assertions.add(new BinaryExpr(new IdExpr(simProp.getEnablementVariableId()), BinaryOp.EQUAL, new BoolExpr(disabledProperties.contains(simProp) ? false : true)));
}
}
// Create the new evaluator
return new Evaluator(baseEvaluator, assertions);
} catch (EvaluationException ex) {
return null;
}
}
Aggregations