use of jkind.lustre.BoolExpr 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.BoolExpr 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();
}
use of jkind.lustre.BoolExpr in project AGREE by loonwerks.
the class FrameAssertionHelper method valueToExpr.
private static Expr valueToExpr(final Value value) {
assert value != null;
if (value instanceof ArrayValue) {
final ArrayValue arrayValue = (ArrayValue) value;
final ArrayList<Expr> exprList = new ArrayList<Expr>(arrayValue.elements.size());
for (final Value childValue : arrayValue.elements) {
exprList.add(valueToExpr(childValue));
}
return new ArrayExpr(exprList);
} else if (value instanceof BooleanValue) {
return new BoolExpr(((BooleanValue) value).value);
} else if (value instanceof EnumValue) {
return new IdExpr(((EnumValue) value).value);
} else if (value instanceof IntegerValue) {
return new IntExpr(((IntegerValue) value).value);
} else if (value instanceof RealValue) {
final BigFraction fraction = ((RealValue) value).value;
return new BinaryExpr(new RealExpr(new BigDecimal(fraction.getNumerator())), BinaryOp.DIVIDE, new RealExpr(new BigDecimal(fraction.getDenominator())));
}
if (value instanceof TupleValue) {
final TupleValue tupleValue = (TupleValue) value;
final ArrayList<Expr> exprList = new ArrayList<Expr>(tupleValue.elements.size());
for (final Value childValue : tupleValue.elements) {
exprList.add(valueToExpr(childValue));
}
return new TupleExpr(exprList);
} else {
throw new RuntimeException("Unhandled case. Value is of type: " + value.getClass());
}
}
use of jkind.lustre.BoolExpr in project AGREE by loonwerks.
the class Simulation method executeFrame.
private SimulationFrameResults executeFrame(final List<Expr> assertions, final Set<SimulationProperty> disabledProperties) throws InterruptedException {
assert assertions != null;
// Build the final constrained lustre specification for the frame.
final ProgramBuilder programBuilder = new ProgramBuilder(program.getLustreProgram());
programBuilder.clearNodes();
final NodeBuilder nodeBuilder = new NodeBuilder(program.getLustreProgram().getMainNode());
// Add assignments for the sim assertions signal
// Actual assertions are not used because they can result in an inconsistent Lustre program which will prevent
// the set of support from being generated when using yices.
Expr prevSimAssertionExpr = new BoolExpr(true);
for (int assertionIndex = 0; assertionIndex < assertions.size(); assertionIndex++) {
final String simAssertionSignalId = CreateSimulationGuarantee.SIMULATION_ASSERTIONS_ID + assertionIndex;
final IdExpr simAssertionExpr = new IdExpr(simAssertionSignalId);
nodeBuilder.addLocal(new VarDecl(simAssertionSignalId, NamedType.BOOL));
nodeBuilder.addEquation(new Equation(simAssertionExpr, new BinaryExpr(prevSimAssertionExpr, BinaryOp.AND, assertions.get(assertionIndex))));
prevSimAssertionExpr = simAssertionExpr;
}
nodeBuilder.addEquation(new Equation(new IdExpr(CreateSimulationGuarantee.SIMULATION_ASSERTIONS_ID), prevSimAssertionExpr));
// Add assignments for property enablement variables
for (final SimulationProperty simProp : program.getSimulationProperties()) {
if (simProp.getEnablementVariableId() != null) {
nodeBuilder.addEquation(new Equation(new IdExpr(simProp.getEnablementVariableId()), new BoolExpr(disabledProperties.contains(simProp) ? false : true)));
}
}
// Build the lustre program for the frame
programBuilder.addNode(nodeBuilder.build());
final Program constrainedLustreProgram = programBuilder.build();
// Prepare to execute JKind
final KindApi api = PreferencesUtil.getKindApi();
// Enable IVC Reduction capability if using JKind
if (api instanceof JKindApi) {
final JKindApi jkindApi = (JKindApi) api;
jkindApi.setIvcReduction();
}
// Execute JKind
final JKindResult result = new JKindResult("Simulation");
// Lucas: This seems to be needed. If we do not add properties to the result explicitly,
// it looks like the result will grab the main property name with the main node prepended.
// This is causing an error when retrieving the property result in the
// if/then/else block structure below.
constrainedLustreProgram.getMainNode().properties.forEach(p -> result.addProperty(p));
System.out.println(constrainedLustreProgram.toString());
try {
final IProgressMonitor currentMonitor = new NullProgressMonitor();
api.execute(constrainedLustreProgram, result, currentMonitor);
// Create a model state from the results.
String simulationGuaranteeId = CreateSimulationGuarantee.SIMULATION_GUARANTEE_ID;
final PropertyResult propertyResult = result.getPropertyResult(simulationGuaranteeId);
final Property property = propertyResult.getProperty();
if (property == null) {
throw new AGREESimulatorException("Unexpected case. Unable to read property results", constrainedLustreProgram);
} else if (property instanceof InvalidProperty) {
final InvalidProperty invalidProperty = (InvalidProperty) property;
final Counterexample counterexample = invalidProperty.getCounterexample();
if (counterexample.getLength() != 1) {
throw new AGREESimulatorException("Unexpected case. Counterexample has " + counterexample.getLength() + " steps", constrainedLustreProgram);
}
SimulationState newState = SimulationState.WAITING_FOR_COMMANDS;
// and a counterexample will not have been generated. This should only occur when a disabled property, lemma, top-level guarantee, or a non-top-level assumption is false.
for (final SimulationProperty simulationProp : program.getSimulationProperties()) {
if (!disabledProperties.contains(simulationProp)) {
for (final String propLustreId : simulationProp.getLustreIds()) {
final Signal<BooleanValue> signal = counterexample.getBooleanSignal(propLustreId);
if (signal == null) {
throw new AGREESimulatorException("Unable to get signal for guarantee property: " + propLustreId, constrainedLustreProgram);
} else {
if (!signal.getValue(0).value) {
newState = SimulationState.WARNING_PROPERTY_NOT_SATISFIED;
break;
}
}
}
}
}
return new SimulationFrameResults(constrainedLustreProgram, counterexample, disabledProperties, newState);
} else if (property instanceof UnknownProperty) {
return new SimulationFrameResults(constrainedLustreProgram, assertions, disabledProperties, SimulationState.HALTED_UNABLE_TO_SATISFY_CONSTRAINTS);
} else if (property instanceof ValidProperty) {
return new SimulationFrameResults(constrainedLustreProgram, assertions, disabledProperties, ((ValidProperty) property).getIvc(), SimulationState.HALTED_UNABLE_TO_SATISFY_CONSTRAINTS);
} else {
throw new AGREESimulatorException("Unhandled case. Property is of type: " + property.getClass(), constrainedLustreProgram);
}
} catch (JKindException ex) {
if (ex.getCause() instanceof InterruptedException) {
throw (InterruptedException) ex.getCause();
}
throw new AGREESimulatorException(constrainedLustreProgram, ex, result.getText());
}
}
use of jkind.lustre.BoolExpr in project AGREE by loonwerks.
the class SimulationFrameResults method eval.
private Value eval(final Expr lustreExpr) {
if (lustreExpr instanceof BoolExpr) {
return BooleanValue.fromBoolean(((BoolExpr) lustreExpr).value);
} else if (lustreExpr instanceof IntExpr) {
return new IntegerValue(((IntExpr) lustreExpr).value);
} else if (lustreExpr instanceof RealExpr) {
final RealExpr realExpr = (RealExpr) lustreExpr;
return new RealValue(BigFraction.valueOf(realExpr.value));
} else if (lustreExpr instanceof BinaryExpr) {
final BinaryExpr binaryExpr = (BinaryExpr) lustreExpr;
final Value leftValue = eval(binaryExpr.left);
final Value rightValue = eval(binaryExpr.right);
if (leftValue == null || rightValue == null) {
return null;
}
return leftValue.applyBinaryOp(binaryExpr.op, rightValue);
} else if (lustreExpr instanceof UnaryExpr) {
final UnaryExpr unaryExpr = (UnaryExpr) lustreExpr;
final Value operandValue = eval(unaryExpr.expr);
if (operandValue == null) {
return null;
}
return operandValue.applyUnaryOp(unaryExpr.op);
}
return null;
}
Aggregations