use of jkind.lustre.values.Value in project AGREE by loonwerks.
the class TcgXmlWriter method writeSignal.
private void writeSignal(int k, Signal<Value> signal) throws Exception {
String name = escapeXml(signal.getName());
// TODO: fix this with the type map!
Type type;
if (signal.getValues().isEmpty()) {
throw new TcgException("Unable to assertain signal type in XmlWriter");
} else {
Map.Entry<Integer, Value> entry = signal.getValues().entrySet().iterator().next();
Value v = entry.getValue();
if (v instanceof IntegerValue) {
type = NamedType.INT;
} else if (v instanceof RealValue) {
type = NamedType.REAL;
} else if (v instanceof BooleanValue) {
type = NamedType.BOOL;
} else {
throw new TcgException("Unexpected signal type in XmlWriter");
}
}
out.println(" <Signal name=\"" + name + "\" type=\"" + type + "\">");
for (int i = 0; i < k; i++) {
out.println(" <Value time=\"" + i + "\">" + formatValue(signal.getValue(i)) + "</Value>");
}
out.println(" </Signal>");
}
use of jkind.lustre.values.Value in project AGREE by loonwerks.
the class CounterexampleLoaderHelper method simulateCounterexample.
private void simulateCounterexample(final Counterexample cex, final int startStepIndex, final Map<String, Object> signalNameToSimStateElementMap, final AGREESimulationEngine simulationEngine, final SimulationService simulationService, final SimulationUIService simulationUIService) {
// For each frame in the counter example, step through the simulation and constrain the arguments to match those of the counter example
for (int stepIndex = startStepIndex; stepIndex < cex.getLength(); stepIndex++) {
simulationEngine.resetInputConstraints();
// Constraint simulation variables based on the counterexample
for (final Entry<String, Value> sv : cex.getStep(stepIndex).entrySet()) {
final String signalName = sv.getKey();
final Object stateElement = signalNameToSimStateElementMap.get(signalName);
// Throw exception if the state element corresponding to the signal in the counterexample was not found unless the signal corresponds to a clock or a specification statement
if (stateElement == null) {
// TODO: Cleanup
// Ignore variables that do not have state elements. Need a way to verify that they can be safely ignored. Clocks may be ignored if they aren't
// used. Variables for SpecStatements are okay to ignore but we don't have a way of getting the reference in all cases.
// In the case of a monolithic analysis, the references for all layers are not contained in the reference map
/*
final String renamedClockIdSuffix = agreeSimulationEngine.getSimulationProgram().getAgreeRenaming().forceRename(AgreeASTBuilder.clockIDSuffix);
final boolean isClock = signalName.endsWith(renamedClockIdSuffix);
final EObject ref = refMap.get(signalName);
if(!isClock && !(ref instanceof SpecStatement)) {
//throw new RuntimeException("Unable to find state element for signal '" + signalName + "'");
}
*/
} else {
// Don't include hidden variables
if (!simulationEngine.getCurrentState().isElementHidden(stateElement)) {
// Constrain the next value of the variable to match the value contained in the counter example
final InputConstraint ic = lustreValueToInputConstraint(sv.getValue());
simulationEngine.setInputConstraint(stateElement, ic);
}
}
}
simulationEngine.stepForward();
}
simulationEngine.queueNotification(notification -> {
// Check that the number of simulated frames matches the expected number. It should match the length of the counter example
if (cex.getLength() == notification.getEngineState().getNumberOfFrames()) {
simulationEngine.resetInputConstraints();
} else {
// Determine whether there are unsatisfied properties that are halting the simulation
boolean hasUnsatisfiedProperty = false;
for (final SimulationProperty simProp : simulationEngine.getSimulationProgram().getSimulationProperties()) {
if (notification.getEngineState().getPropertyStatus(simProp) == AGREEPropertyStatus.UNSATISFIED_ERROR) {
hasUnsatisfiedProperty = true;
}
}
if (hasUnsatisfiedProperty) {
Display.getDefault().syncExec(() -> {
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
final IWorkbenchPage activePage = window.getActivePage();
if (activePage != null) {
try {
activePage.showView(UIConstants.PROPERTIES_VIEW_ID);
} catch (final PartInitException e) {
// Ignore
}
}
final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
final String errorMsg = "One or more properties could not be satisfied. Disable one or more properties from the Simulation Properties view and select Retry to continue.";
class RetryDialog extends MessageDialog {
public RetryDialog() {
super(shell, "Unable to Simulate Counterexample", null, errorMsg, MessageDialog.ERROR, new String[] { "Retry", "End Simulation" }, 0);
setShellStyle(SWT.CLOSE | SWT.MODELESS | SWT.BORDER | SWT.TITLE);
setBlockOnOpen(false);
}
@Override
protected void buttonPressed(final int buttonId) {
switch(buttonId) {
case // Retry
0:
// Try to resume the simulation
simulationEngine.stepBackward();
simulateCounterexample(cex, notification.getEngineState().getNumberOfFrames() - 1, signalNameToSimStateElementMap, simulationEngine, simulationService, simulationUIService);
break;
case // End Simulation
1:
simulationService.dispose(simulationEngine);
break;
}
super.buttonPressed(buttonId);
}
}
final MessageDialog dlg = new RetryDialog();
dlg.open();
});
} else {
final StatusAdapter statusAdapter = new StatusAdapter(new Status(IStatus.WARNING, FrameworkUtil.getBundle(getClass()).getSymbolicName(), "Simulation halted before every step in the counterexample was simulated.", new RuntimeException("The number of simulated frames does not match the length of the counterexample.")));
statusAdapter.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, "Unable to Simulate Counterexample");
StatusManager.getManager().handle(statusAdapter, StatusManager.SHOW);
}
}
});
}
use of jkind.lustre.values.Value in project AGREE by loonwerks.
the class FrameAssertionHelper method addNextFrameAssertions.
/**
* Adds the frame specific assertions for the next frame to an assertion collection. Does not add assertions related to property enablement.
* @param program
* @param lastResults
* @param assertions is the collection to which assertions will be added.
*/
public static void addNextFrameAssertions(final SimulationProgram program, final SimulationFrameResults lastResults, final Collection<Expr> assertions) {
if (lastResults == null) {
for (final Expr c : program.getInitialConstraints()) {
assertions.add(c);
}
} else {
for (final CarryVariable cv : program.getCarryVariables()) {
final Value value = lastResults.getValue(cv.getOutputVariableExpression().toString());
if (value == null) {
throw new RuntimeException("Unable to get value for: " + cv.getOutputVariableExpression().toString());
}
final Expr valueExpression = valueToExpr(value);
assertions.add(new BinaryExpr(cv.getInputVariableExpression(), BinaryOp.EQUAL, valueExpression));
}
}
}
use of jkind.lustre.values.Value in project AGREE by loonwerks.
the class TcgXmlReader method getSignal.
private Signal<Value> getSignal(Element signalElement) {
String name = signalElement.getAttribute("name");
String type = signalElement.getAttribute("type");
if (type.contains("subrange ")) {
type = "int";
}
Signal<Value> signal = new Signal<>(name);
for (Element valueElement : getElements(signalElement, "Value")) {
int time = Integer.parseInt(valueElement.getAttribute(getTimeAttribute()));
signal.putValue(time, getValue(valueElement, type));
}
return signal;
}
use of jkind.lustre.values.Value in project AGREE by loonwerks.
the class SymbolicValue method applyBinaryOp.
public SymbolicValue applyBinaryOp(final BinaryOp op, SymbolicValue value) {
final Value newCoefficient;
final Variable newVariable;
final Value newConstant;
if (op == BinaryOp.EQUAL) {
// Equality comparison is only supported when the variables are the same
if (variable == value.variable) {
boolean equal = Objects.equals(coefficient, value.coefficient) && Objects.equals(variable, value.variable) && Objects.equals(constant, value.constant);
return new SymbolicValue(null, null, equal ? BooleanValue.TRUE : BooleanValue.FALSE);
} else {
return null;
}
} else if (op == BinaryOp.LESS || op == BinaryOp.LESSEQUAL || op == BinaryOp.GREATER || op == BinaryOp.GREATEREQUAL) {
// Comparison operators are only supported when the symbolic value are constant values
if (variable == null && value.variable == null && constant != null && value.constant != null) {
final Value result = constant.applyBinaryOp(op, value.constant);
if (result instanceof BooleanValue) {
return new SymbolicValue(null, null, (BooleanValue) result);
}
}
return null;
} else if (op == BinaryOp.PLUS || op == BinaryOp.MINUS) {
if (variable == null && value.variable == null) {
newCoefficient = null;
newVariable = null;
} else if (variable != null && value.variable != null) {
if (variable == value.variable) {
newCoefficient = coefficient.applyBinaryOp(op, value.coefficient);
newVariable = variable;
} else {
// Unsupported
return null;
}
} else {
if (variable == null) {
// value.variable != null
if (op == BinaryOp.PLUS) {
newCoefficient = value.coefficient;
} else {
newCoefficient = value.coefficient.applyUnaryOp(UnaryOp.NEGATIVE);
}
newVariable = value.variable;
} else {
newCoefficient = coefficient;
newVariable = variable;
}
}
if (constant == null && value.constant == null) {
newConstant = null;
} else if (constant != null && value.constant != null) {
newConstant = constant.applyBinaryOp(op, value.constant);
} else {
if (constant == null) {
// value.constant != null
if (op == BinaryOp.PLUS) {
newConstant = value.constant;
} else {
newConstant = value.constant.applyUnaryOp(UnaryOp.NEGATIVE);
}
} else {
newConstant = constant;
}
}
} else if (op == BinaryOp.MULTIPLY || op == BinaryOp.DIVIDE) {
if (variable == null && value.variable == null) {
newCoefficient = null;
newVariable = null;
} else if (variable != null && value.variable != null) {
// At most one may have a variable id
return null;
} else {
if (variable == null) {
// value.variable != null
newCoefficient = value.coefficient.applyBinaryOp(op, constant);
newVariable = value.variable;
} else {
newCoefficient = coefficient.applyBinaryOp(op, value.constant);
newVariable = variable;
}
}
// Since multiplying values which both contain coefficients are not allowed, at least one value must have a constant
if (constant != null && value.constant != null) {
newConstant = constant.applyBinaryOp(op, value.constant);
} else {
newConstant = null;
}
} else if (op == BinaryOp.AND) {
if (variable == null && value.variable == null) {
newCoefficient = null;
newVariable = null;
newConstant = constant.applyBinaryOp(op, value.constant);
} else if (variable != null && value.variable != null) {
if (variable == value.variable && coefficient.equals(value.coefficient)) {
newCoefficient = coefficient;
newVariable = variable;
newConstant = null;
} else {
// Unsupported
return null;
}
} else {
if (variable == null) {
// value.variable != null
if (constant == BooleanValue.TRUE) {
newCoefficient = value.coefficient;
newVariable = value.variable;
newConstant = null;
} else {
newCoefficient = null;
newVariable = null;
newConstant = BooleanValue.FALSE;
}
} else {
if (value.constant == BooleanValue.TRUE) {
newCoefficient = coefficient;
newVariable = variable;
newConstant = null;
} else {
newCoefficient = null;
newVariable = null;
newConstant = BooleanValue.FALSE;
}
}
}
} else if (op == BinaryOp.OR) {
if (variable == null && value.variable == null) {
newCoefficient = null;
newVariable = null;
newConstant = constant.applyBinaryOp(op, value.constant);
} else if (variable != null && value.variable != null) {
if (variable == value.variable && coefficient.equals(value.coefficient)) {
newCoefficient = coefficient;
newVariable = variable;
newConstant = null;
} else {
// Unsupported
return null;
}
} else {
if (variable == null) {
// value.variable != null
if (constant == BooleanValue.TRUE) {
newCoefficient = null;
newVariable = null;
newConstant = BooleanValue.TRUE;
} else {
newCoefficient = value.coefficient;
newVariable = value.variable;
newConstant = null;
}
} else {
if (value.constant == BooleanValue.TRUE) {
newCoefficient = null;
newVariable = null;
newConstant = BooleanValue.TRUE;
} else {
newCoefficient = coefficient;
newVariable = variable;
newConstant = null;
}
}
}
} else {
return null;
}
// This will occur if unsupported operations are performed on the coefficient.
if ((newCoefficient == null) != (newVariable == null)) {
return null;
}
return new SymbolicValue(newCoefficient, newVariable, newConstant);
}
Aggregations