use of jkind.lustre.builders.NodeBuilder 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();
}
use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.
the class LustreAstBuilder method getAssumeGuaranteeLustreProgram.
public static Program getAssumeGuaranteeLustreProgram(AgreeProgram agreeProgram) {
nodes = new ArrayList<>();
uninterpretedFcns = new ArrayList<>();
AgreeNode flatNode = flattenAgreeNode(agreeProgram, agreeProgram.topNode, "_TOP__");
List<Expr> assertions = new ArrayList<>();
List<VarDecl> locals = new ArrayList<>();
List<VarDecl> inputs = new ArrayList<>();
List<Equation> equations = new ArrayList<>();
List<String> properties = new ArrayList<>();
List<String> ivcs = new ArrayList<>();
int j = 0;
for (AgreeStatement assumption : flatNode.assumptions) {
String assumName = assumeSuffix + j++;
locals.add(new AgreeVar(assumName, NamedType.BOOL, assumption.reference, flatNode.compInst, null));
IdExpr assumId = new IdExpr(assumName);
equations.add(new Equation(assumId, assumption.expr));
assertions.add(assumId);
// Else add the defined ivc list.
if (flatNode.getFaultTreeFlag() == false) {
ivcs.add(assumId.id);
}
}
for (AgreeStatement assertion : flatNode.assertions) {
assertions.add(assertion.expr);
}
// add assumption and monolithic lemmas first (helps with proving)
for (AgreeVar var : flatNode.outputs) {
if (var.reference instanceof AssumeStatement || var.reference instanceof LemmaStatement) {
properties.add(var.id);
}
inputs.add(var);
}
// add property that all assumption history is true
Expr assumeConj = new BoolExpr(true);
for (AgreeNode subNode : agreeProgram.topNode.subNodes) {
assumeConj = new BinaryExpr(new IdExpr(subNode.id + "__" + assumeHistSufix), BinaryOp.AND, assumeConj);
}
AgreeVar assumeHistVar = new AgreeVar(assumeHistSufix, NamedType.BOOL, agreeProgram.topNode.compInst.getComponentClassifier(), agreeProgram.topNode.compInst, null);
locals.add(assumeHistVar);
equations.add(new Equation(new IdExpr(assumeHistVar.id), assumeConj));
properties.add(assumeHistVar.id);
int k = 0;
for (AgreeStatement patternPropState : flatNode.patternProps) {
String patternVarName = patternPropSuffix + k++;
locals.add(new AgreeVar(patternVarName, NamedType.BOOL, patternPropState, flatNode.compInst, null));
equations.add(new Equation(new IdExpr(patternVarName), patternPropState.expr));
properties.add(patternVarName);
}
int lemmaCount = 0;
for (AgreeStatement lemma : flatNode.lemmas) {
String lemmaName = lemmaSuffix + lemmaCount++;
locals.add(new AgreeVar(lemmaName, NamedType.BOOL, lemma.reference, flatNode.compInst, null));
equations.add(new Equation(new IdExpr(lemmaName), lemma.expr));
properties.add(lemmaName);
}
int i = 0;
for (AgreeStatement guarantee : flatNode.guarantees) {
String guarName = guarSuffix + i++;
locals.add(new AgreeVar(guarName, NamedType.BOOL, guarantee.reference, flatNode.compInst, null));
equations.add(new Equation(new IdExpr(guarName), guarantee.expr));
properties.add(guarName);
}
if (flatNode.getFaultTreeFlag()) {
ivcs.addAll(agreeProgram.topNode.getivcElements());
}
for (AgreeVar var : flatNode.inputs) {
inputs.add(var);
}
for (AgreeVar var : flatNode.locals) {
locals.add(var);
}
equations.addAll(flatNode.localEquations);
assertions.add(AgreeRealtimeCalendarBuilder.getTimeConstraint(flatNode.eventTimes));
NodeBuilder builder = new NodeBuilder("main");
builder.addInputs(inputs);
builder.addLocals(locals);
builder.addEquations(equations);
builder.addProperties(properties);
builder.addAssertions(assertions);
builder.addIvcs(ivcs);
Node main = builder.build();
nodes.add(main);
nodes.addAll(agreeProgram.globalLustreNodes);
nodes.add(getHistNode());
// add realtime constraint nodes
nodes.addAll(AgreeRealtimeCalendarBuilder.getRealTimeNodes());
List<TypeDef> types = AgreeUtils.getLustreTypes(agreeProgram);
uninterpretedFcns.addAll(agreeProgram.uninterpretedFunctions);
Program program = new ProgramBuilder().addTypes(types).addFunctions(uninterpretedFcns).addNodes(nodes).setMain(main.id).build();
return program;
}
use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.
the class LustreAstBuilder method getConsistencyLustreNode.
protected static Node getConsistencyLustreNode(AgreeNode agreeNode, boolean withAssertions) {
final String stuffPrefix = "__STUFF";
List<Expr> assertions = new ArrayList<>();
List<VarDecl> locals = new ArrayList<>();
List<VarDecl> inputs = new ArrayList<>();
List<Equation> equations = new ArrayList<>();
List<String> properties = new ArrayList<>();
List<String> ivcs = new ArrayList<>();
Expr stuffConj = new BoolExpr(true);
int stuffAssumptionIndex = 0;
for (AgreeStatement assumption : agreeNode.assumptions) {
AgreeVar stuffAssumptionVar = new AgreeVar(stuffPrefix + assumeSuffix + stuffAssumptionIndex++, NamedType.BOOL, assumption.reference, agreeNode.compInst, null);
locals.add(stuffAssumptionVar);
ivcs.add(stuffAssumptionVar.id);
IdExpr stuffAssumptionId = new IdExpr(stuffAssumptionVar.id);
equations.add(new Equation(stuffAssumptionId, assumption.expr));
stuffConj = LustreExprFactory.makeANDExpr(stuffConj, stuffAssumptionId);
}
int stuffGuaranteeIndex = 0;
for (AgreeStatement guarantee : agreeNode.guarantees) {
AgreeVar stuffGuaranteeVar = new AgreeVar(stuffPrefix + guarSuffix + stuffGuaranteeIndex++, NamedType.BOOL, guarantee.reference, agreeNode.compInst, null);
locals.add(stuffGuaranteeVar);
ivcs.add(stuffGuaranteeVar.id);
IdExpr stuffGuaranteeId = new IdExpr(stuffGuaranteeVar.id);
equations.add(new Equation(stuffGuaranteeId, guarantee.expr));
stuffConj = LustreExprFactory.makeANDExpr(stuffConj, stuffGuaranteeId);
}
if (withAssertions) {
equations.addAll(agreeNode.localEquations);
} else {
for (AgreeEquation eq : agreeNode.localEquations) {
if (AgreeUtils.referenceIsInContract(eq.reference, agreeNode.compInst)) {
equations.add(eq);
}
}
}
// TODO should we include lemmas in the consistency check?
// for(AgreeStatement guarantee : agreeNode.lemmas){
// histConj = new BinaryExpr(histConj, BinaryOp.AND, guarantee.expr);
// }
int stuffAssertionIndex = 0;
if (withAssertions) {
for (AgreeStatement assertion : agreeNode.assertions) {
AgreeVar stuffAssertionVar = new AgreeVar(stuffPrefix + assertSuffix + stuffAssertionIndex++, NamedType.BOOL, assertion.reference, null, null);
locals.add(stuffAssertionVar);
IdExpr stuffAssertionId = new IdExpr(stuffAssertionVar.id);
equations.add(new Equation(stuffAssertionId, assertion.expr));
stuffConj = LustreExprFactory.makeANDExpr(stuffConj, stuffAssertionId);
}
} else {
// equations and type equations. That would clear this up.
for (AgreeStatement assertion : agreeNode.assertions) {
if (AgreeUtils.referenceIsInContract(assertion.reference, agreeNode.compInst)) {
AgreeVar stuffAssertionVar = new AgreeVar(stuffPrefix + assertSuffix + stuffAssertionIndex++, NamedType.BOOL, assertion.reference, null, null);
locals.add(stuffAssertionVar);
IdExpr stuffAssertionId = new IdExpr(stuffAssertionVar.id);
equations.add(new Equation(stuffAssertionId, assertion.expr));
stuffConj = LustreExprFactory.makeANDExpr(stuffConj, stuffAssertionId);
}
}
}
// add realtime constraints
Set<AgreeVar> eventTimes = new HashSet<>();
if (withAssertions) {
eventTimes.addAll(agreeNode.eventTimes);
} else {
for (AgreeVar eventVar : agreeNode.eventTimes) {
if (AgreeUtils.referenceIsInContract(eventVar.reference, agreeNode.compInst)) {
eventTimes.add(eventVar);
}
}
}
assertions.add(AgreeRealtimeCalendarBuilder.getTimeConstraint(eventTimes));
for (AgreeVar var : agreeNode.inputs) {
inputs.add(var);
}
for (AgreeVar var : agreeNode.outputs) {
inputs.add(var);
}
for (AgreeVar var : agreeNode.locals) {
if (withAssertions) {
locals.add(var);
} else {
if (AgreeUtils.referenceIsInContract(var.reference, agreeNode.compInst)) {
locals.add(var);
}
}
}
EObject classifier = agreeNode.compInst.getComponentClassifier();
AgreeVar countVar = new AgreeVar("__COUNT", NamedType.INT, null, null, null);
AgreeVar stuffVar = new AgreeVar(stuffPrefix, NamedType.BOOL, null, null, null);
AgreeVar histVar = new AgreeVar("__HIST", NamedType.BOOL, null, null, null);
AgreeVar propVar = new AgreeVar("__PROP", NamedType.BOOL, classifier, agreeNode.compInst, null);
locals.add(countVar);
locals.add(stuffVar);
locals.add(histVar);
locals.add(propVar);
IdExpr countId = new IdExpr(countVar.id);
IdExpr stuffId = new IdExpr(stuffVar.id);
IdExpr histId = new IdExpr(histVar.id);
IdExpr propId = new IdExpr(propVar.id);
equations.add(new Equation(stuffId, stuffConj));
Expr histExpr = new UnaryExpr(UnaryOp.PRE, histId);
histExpr = LustreExprFactory.makeANDExpr(histExpr, stuffId);
histExpr = new BinaryExpr(stuffId, BinaryOp.ARROW, histExpr);
equations.add(new Equation(histId, histExpr));
Expr countExpr = new UnaryExpr(UnaryOp.PRE, countId);
countExpr = new BinaryExpr(countExpr, BinaryOp.PLUS, new IntExpr(BigInteger.ONE));
countExpr = new BinaryExpr(new IntExpr(BigInteger.ZERO), BinaryOp.ARROW, countExpr);
equations.add(new Equation(countId, countExpr));
IPreferenceStore prefs = Activator.getDefault().getPreferenceStore();
int consistDetph = prefs.getInt(PreferenceConstants.PREF_CONSIST_DEPTH);
Expr propExpr = new BinaryExpr(countId, BinaryOp.EQUAL, new IntExpr(BigInteger.valueOf(consistDetph)));
propExpr = new BinaryExpr(propExpr, BinaryOp.AND, histId);
equations.add(new Equation(propId, new UnaryExpr(UnaryOp.NOT, propExpr)));
properties.add(propId.id);
NodeBuilder builder = new NodeBuilder("consistency");
builder.addInputs(inputs);
builder.addLocals(locals);
builder.addEquations(equations);
builder.addProperties(properties);
builder.addAssertions(assertions);
builder.addIvcs(ivcs);
Node node = builder.build();
return node;
}
use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.
the class LustreAstBuilder method getHistNode.
protected static Node getHistNode() {
NodeBuilder builder = new NodeBuilder(historyNodeName);
builder.addInput(new VarDecl("input", NamedType.BOOL));
builder.addOutput(new VarDecl("hist", NamedType.BOOL));
IdExpr histId = new IdExpr("hist");
IdExpr inputId = new IdExpr("input");
Expr preHist = new UnaryExpr(UnaryOp.PRE, histId);
Expr histExpr = new BinaryExpr(preHist, BinaryOp.AND, inputId);
histExpr = new BinaryExpr(inputId, BinaryOp.ARROW, histExpr);
builder.addEquation(new Equation(histId, histExpr));
return builder.build();
}
use of jkind.lustre.builders.NodeBuilder in project AGREE by loonwerks.
the class LustreContractAstBuilder method getLustreNode.
protected static Node getLustreNode(AgreeNode agreeNode, String nodePrefix) {
List<VarDecl> inputs = new ArrayList<>();
List<VarDecl> outputs = new ArrayList<>();
List<VarDecl> locals = new ArrayList<>();
List<Equation> equations = new ArrayList<>();
List<Expr> assertions = new ArrayList<>();
List<Expr> requires = new ArrayList<>();
List<Expr> ensures = new ArrayList<>();
for (AgreeStatement statement : agreeNode.assumptions) {
requires.add(statement.expr);
}
for (AgreeStatement statement : agreeNode.lemmas) {
ensures.add(statement.expr);
}
for (AgreeStatement statement : agreeNode.guarantees) {
ensures.add(statement.expr);
}
for (AgreeStatement statement : agreeNode.assertions) {
assertions.add(statement.expr);
if (AgreeUtils.referenceIsInContract(statement.reference, agreeNode.compInst)) {
ensures.add(statement.expr);
}
}
// gather the remaining inputs
for (AgreeVar var : agreeNode.inputs) {
inputs.add(var);
}
for (AgreeVar var : agreeNode.outputs) {
outputs.add(var);
}
// Contract contract = new Contract(nodePrefix + agreeNode.id, requires, ensures);
Contract contract = new Contract(requires, ensures);
NodeBuilder builder = new NodeBuilder(nodePrefix + agreeNode.id);
builder.addInputs(inputs);
builder.addOutputs(outputs);
builder.addLocals(locals);
builder.addEquations(equations);
builder.addAssertions(assertions);
builder.setContract(contract);
return builder.build();
}
Aggregations