use of jkind.lustre.visitors.AstMapVisitor in project AGREE by loonwerks.
the class RemoveCondacts method clockNodeCalls.
private Node clockNodeCalls(Node node, final IdExpr clock) {
return (Node) node.accept(new AstMapVisitor() {
@Override
public Expr visit(NodeCallExpr e) {
List<Expr> args = new ArrayList<>();
args.add(clock);
args.addAll(visitExprs(e.args));
Node clocked = createClockedNode(e.node);
return new NodeCallExpr(clocked.id, args);
}
@Override
public Expr visit(CondactExpr e) {
NodeCallExpr call = (NodeCallExpr) super.visit(e.call);
List<Expr> args = new ArrayList<>();
args.add(new BinaryExpr(e.clock.accept(this), BinaryOp.AND, clock));
args.addAll(e.call.args);
args.addAll(visitExprs(e.args));
Node condact = createCondactNode(call.node);
return new NodeCallExpr(condact.id, args);
}
});
}
use of jkind.lustre.visitors.AstMapVisitor in project AGREE by loonwerks.
the class RemoveCondacts method removeCondacts.
private Node removeCondacts(Node node) {
return (Node) node.accept(new AstMapVisitor() {
@Override
public Expr visit(CondactExpr e) {
NodeCallExpr call = (NodeCallExpr) e.call.accept(this);
List<Expr> args = new ArrayList<>();
args.add(e.clock.accept(this));
args.addAll(e.call.args);
args.addAll(visitExprs(e.args));
Node condact = createCondactNode(call.node);
return new NodeCallExpr(condact.id, args);
}
});
}
use of jkind.lustre.visitors.AstMapVisitor 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.visitors.AstMapVisitor 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.visitors.AstMapVisitor in project AGREE by loonwerks.
the class CreateSimulationProperties 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 SimulationProgramBuilder builder = new SimulationProgramBuilder(program);
final Node mainNode = lustreProgram.getMainNode();
// Build a new main node which includes support statements
final NodeBuilder lustreNodeBuilder = new NodeBuilder(mainNode);
// Create simulation properties for each local variable that references a lemma, assume, guarantee, or assert statement
lustreNodeBuilder.clearIvc();
final Map<String, SimulationProperty> idToSimulationPropertyMap = new HashMap<>();
// Create a mapping from the component instance/variable reference to a collection of Lustre Id's that will be used to create the simulation properties
final Map<ComponentInstance, Map<EObject, Collection<String>>> componentInstanceToReferenceToVarIdMap = new HashMap<>();
for (final VarDecl local : mainNode.locals) {
if (local instanceof AgreeVar) {
final AgreeVar var = (AgreeVar) local;
if (var.reference instanceof LemmaStatement || var.reference instanceof AssumeStatement || var.reference instanceof GuaranteeStatement || var.reference instanceof AssertStatement) {
final boolean createSimProp = (!(var.reference instanceof AssumeStatement) || var.compInst == var.compInst.getSystemInstance()) || var instanceof SimulationPropertyVar;
if (createSimProp) {
Map<EObject, Collection<String>> referenceToVarIdMap = componentInstanceToReferenceToVarIdMap.get(var.compInst);
if (referenceToVarIdMap == null) {
referenceToVarIdMap = new HashMap<>();
componentInstanceToReferenceToVarIdMap.put(var.compInst, referenceToVarIdMap);
}
Collection<String> varIds = referenceToVarIdMap.get(var.reference);
if (varIds == null) {
varIds = new HashSet<>();
referenceToVarIdMap.put(var.reference, varIds);
}
varIds.add(var.id);
}
}
}
}
// Create the simulation properties.
// A single simulation property will be created for each component instance, reference combination.
int propertyIndex = 0;
for (final Entry<ComponentInstance, Map<EObject, Collection<String>>> componentInstanceToReferenceToVarIdMapEntry : componentInstanceToReferenceToVarIdMap.entrySet()) {
final ComponentInstance propComponentInstance = componentInstanceToReferenceToVarIdMapEntry.getKey();
for (Entry<EObject, Collection<String>> referenceToVarIdsEntry : componentInstanceToReferenceToVarIdMapEntry.getValue().entrySet()) {
final EObject propReference = referenceToVarIdsEntry.getKey();
final Collection<String> propLustreIds = referenceToVarIdsEntry.getValue();
final String propertyDesc = getDescription(propComponentInstance, propReference);
final String enablementVariableId;
// Only properties which cause the simulation to be halted may be disabled.
// That is: only top-level assumptions and non-top-level guarantees may be disabled
final boolean disableable = (propReference instanceof GuaranteeStatement && propComponentInstance != propComponentInstance.getSystemInstance()) || (propReference instanceof AssumeStatement && propComponentInstance == propComponentInstance.getSystemInstance());
if (disableable) {
enablementVariableId = propertyEnablementPrefix + propertyIndex;
lustreNodeBuilder.addLocal(new VarDecl(enablementVariableId, NamedType.BOOL));
} else {
enablementVariableId = null;
}
final SimulationProperty simProp = new SimulationProperty(propLustreIds, propertyDesc, propReference, enablementVariableId);
builder.addSimulationProperty(simProp);
propertyIndex++;
for (final String propLustreId : propLustreIds) {
idToSimulationPropertyMap.put(propLustreId, simProp);
lustreNodeBuilder.addIvc(propLustreId);
}
}
}
// Edit main node to make simulation properties inside of assertions to be disableable
final AstMapVisitor propertyVarDisableTransformation = new AstMapVisitor() {
@Override
public Expr visit(final IdExpr e) {
final SimulationProperty simProp = idToSimulationPropertyMap.get(e.id);
if (simProp != null && simProp.getEnablementVariableId() != null) {
return new BinaryExpr(new UnaryExpr(UnaryOp.NOT, new IdExpr(simProp.getEnablementVariableId())), BinaryOp.OR, e);
}
return super.visit(e);
}
@Override
public Equation visit(final Equation e) {
// Don't transform the equations for simulation properties
if (e.lhs.size() == 1 && idToSimulationPropertyMap.containsKey(e.lhs.get(0))) {
return e;
}
return super.visit(e);
}
};
// Transform assertions and equations
lustreNodeBuilder.clearAssertions();
for (final Expr assertion : mainNode.assertions) {
lustreNodeBuilder.addAssertion(assertion.accept(propertyVarDisableTransformation));
}
lustreNodeBuilder.clearEquations();
for (final Equation eq : mainNode.equations) {
lustreNodeBuilder.addEquation((Equation) eq.accept(propertyVarDisableTransformation));
}
// Build a new program with the new main node
final ProgramBuilder lustreProgramBuilder = new ProgramBuilder(lustreProgram);
lustreProgramBuilder.clearNodes();
lustreProgramBuilder.addNode(lustreNodeBuilder.build());
builder.setLustreProgram(lustreProgramBuilder.build());
return builder.build();
}
Aggregations