use of jkind.lustre.BoolExpr 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.BoolExpr in project AGREE by loonwerks.
the class AgreePatternTranslator method translatePatternEventProperty.
private Expr translatePatternEventProperty(AgreeCauseEffectPattern pattern, AgreeNodeBuilder builder, IdExpr causeId, IdExpr effectId) {
EObject varReference = pattern.reference;
AgreeVar timerVar = new AgreeVar(TIMER_PREFIX + patternIndex, NamedType.REAL, varReference);
AgreeVar runVar = new AgreeVar(RUNNING_PREFIX + patternIndex, NamedType.BOOL, varReference);
AgreeVar recordVar = new AgreeVar(RECORD_PREFIX + patternIndex, NamedType.BOOL, varReference);
builder.addLocal(timerVar);
builder.addLocal(runVar);
builder.addInput(recordVar);
IdExpr timerId = new IdExpr(timerVar.id);
IdExpr runId = new IdExpr(runVar.id);
IdExpr recordId = new IdExpr(recordVar.id);
// run = record -> if pre(run) and e and l <= timer <= h then
// false
// else
// if record then
// true
// else
// pre(run)
Expr preRun = new UnaryExpr(UnaryOp.PRE, runId);
{
Expr if2 = new IfThenElseExpr(recordId, new BoolExpr(true), preRun);
BinaryOp left = getIntervalLeftOp(pattern.effectInterval);
BinaryOp right = getIntervalRightOp(pattern.effectInterval);
Expr timerLow = new BinaryExpr(pattern.effectInterval.low, left, timerId);
Expr timerHigh = new BinaryExpr(timerId, right, pattern.effectInterval.high);
Expr cond1 = new BinaryExpr(preRun, BinaryOp.AND, effectId);
cond1 = new BinaryExpr(cond1, BinaryOp.AND, timerLow);
cond1 = new BinaryExpr(cond1, BinaryOp.AND, timerHigh);
Expr if1 = new IfThenElseExpr(cond1, new BoolExpr(false), if2);
Expr runExpr = new BinaryExpr(recordId, BinaryOp.ARROW, if1);
builder.addLocalEquation(new AgreeEquation(runId, runExpr, varReference));
}
// timer = (0 -> if pre(run) then pre(timer) + (t - pre(t)) else 0)
{
Expr preTimer = new UnaryExpr(UnaryOp.PRE, timerId);
Expr preT = new UnaryExpr(UnaryOp.PRE, timeExpr);
Expr elapsed = new BinaryExpr(timeExpr, BinaryOp.MINUS, preT);
Expr total = new BinaryExpr(preTimer, BinaryOp.PLUS, elapsed);
Expr timerExpr = new IfThenElseExpr(preRun, total, new RealExpr(BigDecimal.ZERO));
timerExpr = new BinaryExpr(new RealExpr(BigDecimal.ZERO), BinaryOp.ARROW, timerExpr);
builder.addLocalEquation(new AgreeEquation(timerId, timerExpr, varReference));
}
// property that should be true for timer to help induction
{
Expr expr = new BinaryExpr(timerId, BinaryOp.GREATEREQUAL, new RealExpr(BigDecimal.ZERO));
builder.addAssertion(new AgreeStatement(null, expr, varReference));
}
// record => cause and not (e and (l = 0))
{
Expr causeExpr;
if (pattern.effectInterval.type == IntervalType.OPEN_LEFT || pattern.effectInterval.type == IntervalType.OPEN) {
causeExpr = causeId;
} else {
Expr eAndLZero = new BinaryExpr(pattern.effectInterval.low, BinaryOp.EQUAL, new RealExpr(BigDecimal.ZERO));
eAndLZero = new BinaryExpr(effectId, BinaryOp.AND, eAndLZero);
Expr notEAndLZero = new UnaryExpr(UnaryOp.NOT, eAndLZero);
causeExpr = new BinaryExpr(causeId, BinaryOp.AND, notEAndLZero);
}
Expr recordExpr = new BinaryExpr(recordId, BinaryOp.IMPLIES, causeExpr);
AgreeStatement statement = new AgreeStatement(null, recordExpr, varReference);
builder.addAssertion(statement);
}
// lemma to help induction
AgreeVar timeOfCause = getTimeOf(causeId.id, builder, pattern);
AgreeVar timeOfEffect = getTimeOf(effectId.id, builder, pattern);
// Expr expr = expr("(timer > 0.0 => timeOfCause > 0.0) and "
// + "(timeOfEffect < timeOfCause => timer <= time - timeOfCause) and "
// + "(cause => timeOfCause = time) and"
// + "(true -> ((pre (timeOfEffect - low > timeOfCause)) => timer =
// 0.0))",
// to("timer", timerVar),
// to("timeOfCause", timeOfCause),
// to("time", timeExpr),
// to("cause", causeId),
// to("timeOfEffect", timeOfEffect),
// to("low", pattern.effectInterval.low));
Expr expr = expr("(timer > 0.0 => timeOfCause >= 0.0) and " + "(timer <= time) and" + "(timeOfEffect >= timeOfCause and timer <= high and timeOfEffect >= time - timer + low => not run) and" + "(true -> (pre(timeOfEffect >= timeOfCause + low and timeOfEffect <= timeOfCause + high and timer <= high) => timer = 0.0)) and" + "(timer = 0.0 or timer >= time - timeOfCause)", to("timer", timerVar), to("timeOfCause", timeOfCause), to("timeOfEffect", timeOfEffect), to("time", timeExpr), to("low", pattern.effectInterval.low), to("high", pattern.effectInterval.high), to("run", runVar));
builder.addPatternProp(new AgreeStatement("Timer Lemma for Pattern " + patternIndex, expr, pattern));
// timer <= h
BinaryOp right = getIntervalRightOp(pattern.effectInterval);
return new BinaryExpr(timerId, right, pattern.effectInterval.high);
}
use of jkind.lustre.BoolExpr in project AGREE by loonwerks.
the class AgreePatternTranslator method addPatternConstraintProperty.
private void addPatternConstraintProperty(AgreeCauseEffectPattern pattern, AgreeNodeBuilder builder, IdExpr causeId, IdExpr effectId) {
AgreeVar newCause = new AgreeVar(NEW_CAUSE_PREFIX + causeId.id + patternIndex, NamedType.BOOL, pattern);
builder.addLocal(newCause);
AgreeVar timeCauseVar = getTimeOf(causeId.id, builder, pattern);
IdExpr timeCauseId = new IdExpr(timeCauseVar.id);
IdExpr newCauseId = new IdExpr(newCause.id);
Expr preTimeCause = new UnaryExpr(UnaryOp.PRE, timeCauseId);
Expr newCauseExpr = new BinaryExpr(preTimeCause, BinaryOp.NOTEQUAL, timeCauseId);
newCauseExpr = new BinaryExpr(newCauseExpr, BinaryOp.AND, new BinaryExpr(preTimeCause, BinaryOp.GREATEREQUAL, new RealExpr(BigDecimal.ZERO)));
builder.addLocalEquation(new AgreeEquation(newCauseId, newCauseExpr, pattern));
if (pattern.effectType == TriggerType.EVENT) {
AgreeVar timeEffectVar = getTimeOf(effectId.id, builder, pattern);
IdExpr timeEffectId = new IdExpr(timeEffectVar.id);
Expr preTimeCausePlusL = new BinaryExpr(preTimeCause, BinaryOp.PLUS, pattern.effectInterval.low);
BinaryOp left = getIntervalLeftOp(pattern.effectInterval);
Expr inInterval = new BinaryExpr(preTimeCausePlusL, left, timeEffectId);
Expr propExpr = new BinaryExpr(newCauseId, BinaryOp.IMPLIES, inInterval);
propExpr = new BinaryExpr(new BoolExpr(true), BinaryOp.ARROW, propExpr);
AgreeStatement statement = new AgreeStatement(" pattern " + patternIndex + " in bounds", propExpr, pattern);
builder.addPatternProp(statement);
} else {
AgreeVar timeEndVar = new AgreeVar(END_INTERVAL + patternIndex, NamedType.REAL, pattern);
builder.addLocal(timeEndVar);
Equation eq = equation("timeEnd = timeCause + h;", to("timeEnd", timeEndVar), to("timeCause", timeCauseId), to("h", pattern.effectInterval.high));
builder.addLocalEquation(new AgreeEquation(eq, pattern));
Expr expr = expr("true -> (newCause => pre(timeEnd) < time)", to("timeEnd", timeEndVar), to("newCause", newCauseId), to("time", timeExpr));
AgreeStatement statement = new AgreeStatement(" pattern " + patternIndex + " in bounds", expr, pattern);
builder.addPatternProp(statement);
}
}
use of jkind.lustre.BoolExpr 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.BoolExpr in project AGREE by loonwerks.
the class LustreAstBuilder method getClockExpr.
protected static Expr getClockExpr(AgreeNode agreeNode, AgreeNode subNode) {
IdExpr clockId = new IdExpr(subNode.clockVar.id);
switch(agreeNode.timing) {
case SYNC:
return new BoolExpr(true);
case ASYNC:
return clockId;
case LATCHED:
Expr preClock = new UnaryExpr(UnaryOp.PRE, clockId);
Expr notClock = new UnaryExpr(UnaryOp.NOT, clockId);
Expr andExpr = new BinaryExpr(preClock, BinaryOp.AND, notClock);
Expr clockExpr = new BinaryExpr(new BoolExpr(false), BinaryOp.ARROW, andExpr);
return clockExpr;
default:
throw new AgreeException("unhandled timing type: '" + agreeNode.timing + "");
}
}
Aggregations