use of com.rockwellcollins.atc.agree.analysis.ast.AgreeEquation 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 com.rockwellcollins.atc.agree.analysis.ast.AgreeEquation 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 com.rockwellcollins.atc.agree.analysis.ast.AgreeEquation 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 com.rockwellcollins.atc.agree.analysis.ast.AgreeEquation in project AGREE by loonwerks.
the class LustreAstBuilder method flattenAgreeNode.
protected static AgreeNode flattenAgreeNode(AgreeProgram agreeProgram, AgreeNode agreeNode, String nodePrefix) {
List<AgreeVar> inputs = new ArrayList<>();
List<AgreeVar> outputs = new ArrayList<>();
List<AgreeVar> locals = new ArrayList<>();
List<AgreeStatement> patternProps = new ArrayList<>();
List<AgreeEquation> equations = new ArrayList<>();
List<AgreeStatement> assertions = new ArrayList<>();
Set<AgreeVar> timeEvents = new HashSet<>(agreeNode.eventTimes);
Expr someoneTicks = null;
for (AgreeNode subAgreeNode : agreeNode.subNodes) {
String prefix = subAgreeNode.id + AgreeASTBuilder.dotChar;
Expr clockExpr = getClockExpr(agreeNode, subAgreeNode);
if (someoneTicks == null) {
someoneTicks = clockExpr;
} else {
someoneTicks = new BinaryExpr(someoneTicks, BinaryOp.OR, clockExpr);
}
AgreeNode flatNode = flattenAgreeNode(agreeProgram, subAgreeNode, nodePrefix + subAgreeNode.id + AgreeASTBuilder.dotChar);
Node lustreNode = addSubNodeLustre(agreeProgram, agreeNode, nodePrefix, flatNode);
addInputsAndOutputs(agreeNode, inputs, outputs, patternProps, flatNode, lustreNode, prefix);
addTimeEvents(timeEvents, flatNode, prefix, assertions);
addNodeCall(agreeNode, assertions, prefix, clockExpr, lustreNode);
addHistoricalAssumptionConstraint(agreeNode, prefix, clockExpr, assertions, lustreNode);
}
if (agreeNode.timing == TimingModel.ASYNC) {
if (someoneTicks == null) {
throw new AgreeException("Somehow we generated a clock constraint without any clocks." + " Perhaps none of your subcomponents have an agree annex?");
}
assertions.add(new AgreeStatement("someone ticks", someoneTicks, null));
}
addConnectionConstraints(agreeNode, assertions);
// add any clock constraints
assertions.addAll(agreeNode.assertions);
assertions.add(new AgreeStatement("", agreeNode.clockConstraint, null));
inputs.addAll(agreeNode.inputs);
outputs.addAll(agreeNode.outputs);
locals.addAll(agreeNode.locals);
equations.addAll(agreeNode.localEquations);
patternProps.addAll(agreeNode.patternProps);
AgreeNodeBuilder builder = new AgreeNodeBuilder(agreeNode.id);
builder.addInput(inputs);
builder.addOutput(outputs);
builder.addLocal(locals);
builder.addLocalEquation(equations);
builder.addIvcElements(agreeNode.getivcElements());
builder.addSubNode(agreeNode.subNodes);
builder.addAssertion(assertions);
builder.addAssumption(agreeNode.assumptions);
builder.addGuarantee(agreeNode.guarantees);
builder.addLemma(agreeNode.lemmas);
builder.addPatternProp(patternProps);
builder.setClockConstraint(new BoolExpr(true));
builder.setInitialConstraint(agreeNode.initialConstraint);
builder.setClockVar(agreeNode.clockVar);
builder.setReference(agreeNode.reference);
builder.setTiming(null);
builder.addEventTime(timeEvents);
builder.setCompInst(agreeNode.compInst);
builder.setFaultTreeFlag(agreeNode.faultTreeFlag);
return builder.build();
}
Aggregations