use of edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener in project AGREE by loonwerks.
the class SimulatePossibilitiesHandler method openChart.
private void openChart(final IProgressMonitor monitor, final Shell shell, final Object simControlLock, final SimulationUIService simulationUIService, final SimulationEngine simulationEngine, final List<SimulationEngineState> simulationEngineStates, final SimulationEngineState initialState) {
simulationEngine.queueNotification(new NotificationHandler() {
@Override
public void handleNotification(final SimulationEngineNotification notification) {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
try {
if (!simulationEngineStates.isEmpty() && !monitor.isCanceled()) {
// Listening for cancellation
final SimulatorStateListener simListener = new SimulatorStateListener() {
@Override
public void onSimulatorStateChanged(SimulatorState simulatorState) {
if (simulationUIService.getCurrentState().getEngineState() == null) {
if (shell != null && !shell.isDisposed()) {
// Close SimulatingPossibilitiesView on cancel
shell.close();
// Notify on cancel
synchronized (simControlLock) {
simControlLock.notify();
}
}
}
}
};
simulationUIService.addStateChangeListener(simListener);
try {
// Notify to complete progress monitor
synchronized (simControlLock) {
simControlLock.notify();
}
monitor.done();
// Lock simulation while chart is open
simulationUIService.lockUserInterface();
final SimulatePossibilitiesChartDialog view = new SimulatePossibilitiesChartDialog(shell, simulationUIService, simulationEngineStates);
final SetValueSelectionAdapter setValueSelectionAdapter = view.getSetValueSelectionAdapter();
final SimulationEngineState returnedState = setValueSelectionAdapter.getSelectedState();
if (returnedState != null) {
setSelectedState(view, returnedState);
}
} finally {
simulationUIService.removeStateChangeListener(simListener);
}
}
} finally {
simulationUIService.unlockUserInterface();
}
}
private void setSelectedState(final SimulatePossibilitiesChartDialog view, final SimulationEngineState returnedState) {
final SimulationEngine originalSimulationEngine = simulationUIService.getCurrentState().getSimulationEngine();
for (final ChartElement chartElement : view.getChartElements()) {
final Object simulationStateElement = chartElement.getSimulationStateElement();
if (simulationStateElement != null) {
final Object value = returnedState.getElementValue(returnedState.getNumberOfFrames() - 1, simulationStateElement);
if (value != null) {
if (value instanceof BigInteger) {
final IntegerLiteral il = InputConstraintFactory.eINSTANCE.createIntegerLiteral();
il.setValue((BigInteger) value);
originalSimulationEngine.setInputConstraint(simulationStateElement, il);
} else if (value instanceof Rational) {
final Rational rational = (Rational) value;
final BinaryExpression be = ExpressionFactory.createFraction(rational.numerator, rational.denominator);
originalSimulationEngine.setInputConstraint(simulationStateElement, be);
} else if (value instanceof Boolean) {
final BooleanLiteral bl = InputConstraintFactory.eINSTANCE.createBooleanLiteral();
bl.setValue((Boolean) value);
originalSimulationEngine.setInputConstraint(simulationStateElement, bl);
} else {
throw new RuntimeException("Unhandled Type: " + returnedState.getElementType(simulationStateElement));
}
}
}
}
simulationUIService.stepForward();
originalSimulationEngine.resetInputConstraints();
for (final ChartElement chartElement : view.getChartElements()) {
final Object simulationStateElement = chartElement.getSimulationStateElement();
if (simulationStateElement != null) {
originalSimulationEngine.setInputConstraint(simulationStateElement, initialState.getElementInputConstraintForNextFrame(simulationStateElement));
}
}
}
});
}
});
}
use of edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener in project AGREE by loonwerks.
the class SimulatePossibilitiesHandler method execute.
public Object execute(ExecutionEvent execEvent) throws ExecutionException {
try {
final SimulationUIService simulationUIService = (SimulationUIService) Objects.requireNonNull(PlatformUI.getWorkbench().getService(SimulationUIService.class), "unable to get simulation UI service");
final Shell shell = new Shell(Display.getDefault().getActiveShell(), SWT.SHELL_TRIM & ~SWT.MIN);
// Notify the UI service to prepare to step forward. This will validate input constraints which are being edited
simulationUIService.prepareToStepForward();
final List<SimulationEngineState> simulationEngineStates = new ArrayList<>();
final SelectStepsDialog dlg = new SelectStepsDialog(new Shell(Display.getDefault(), SWT.NONE));
final int returnCode = dlg.open();
// Object to control when job is done/canceled
final Object simControlLock = new Object();
if (returnCode == 0 && dlg.getSteps() != null) {
simulationUIService.lockUserInterface();
new Job("Simulate Possibilities") {
@Override
protected IStatus run(IProgressMonitor monitor) {
final SimulationEngine simulationEngine = simulationUIService.getCurrentState().getSimulationEngine().fork();
try {
final SimulationEngineState initialState = simulationEngine.getCurrentState();
final SimulatorStateListener canceledListener = new SimulatorStateListener() {
@Override
public void onSimulatorStateChanged(SimulatorState simulatorState) {
// Check if canceled
if (simulationUIService.getCurrentState().getEngineState() == null) {
// notify to exit job
synchronized (simControlLock) {
simControlLock.notify();
}
}
}
};
simulationUIService.addStateChangeListener(canceledListener);
try {
monitor.beginTask("Simulate", dlg.getSteps());
runSimulation(monitor, simControlLock, simulationUIService, simulationEngine, simulationEngineStates, dlg);
openChart(monitor, shell, simControlLock, simulationUIService, simulationEngine, simulationEngineStates, initialState);
synchronized (simControlLock) {
simControlLock.wait();
}
return monitor.isCanceled() ? Status.CANCEL_STATUS : Status.OK_STATUS;
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (final Exception e) {
throw e;
} finally {
simulationUIService.unlockUserInterface();
simulationUIService.removeStateChangeListener(canceledListener);
}
} finally {
simulationEngine.dispose();
}
}
}.schedule();
}
} catch (StepForwardCanceledException ex) {
final Status status = new Status(IStatus.ERROR, FrameworkUtil.getBundle(getClass()).getSymbolicName(), "Error preparing to step forward", ex);
StatusManager.getManager().handle(status, StatusManager.SHOW);
return null;
} catch (final Exception ex) {
final Status status = new Status(IStatus.ERROR, FrameworkUtil.getBundle(getClass()).getSymbolicName(), "Error", ex);
StatusManager.getManager().handle(status, StatusManager.SHOW | StatusManager.LOG);
}
return null;
}
use of edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener in project AGREE by loonwerks.
the class StepForwardMultipleTimesHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
final SimulationUIService simulationUIService = (SimulationUIService) Objects.requireNonNull(PlatformUI.getWorkbench().getService(SimulationUIService.class), "Unable to get simulation UI service");
final SimulationService simulationService = Objects.requireNonNull((SimulationService) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(SimulationService.class), "Unable to retrieve simulation service");
final SimulationEngine simulationEngine = simulationUIService.getCurrentState().getSimulationEngine();
// Notify the UI service to prepare to step forward. This will validate input constraints which are being edited
try {
simulationUIService.prepareToStepForward();
} catch (StepForwardCanceledException ex) {
final Status status = new Status(IStatus.ERROR, FrameworkUtil.getBundle(getClass()).getSymbolicName(), "Error preparing to step forward", ex);
StatusManager.getManager().handle(status, StatusManager.SHOW);
return null;
}
final SelectStepsDialog dlg = new SelectStepsDialog(new Shell(Display.getDefault(), SWT.NONE));
final int returnCode = dlg.open();
final Object simControlLock = new Object();
if (returnCode == 0 && dlg.getSteps() != null) {
simulationUIService.lockUserInterface();
new Job("Simulating") {
@Override
protected IStatus run(IProgressMonitor monitor) {
final SimulatorStateListener canceledListener = new SimulatorStateListener() {
@Override
public void onSimulatorStateChanged(SimulatorState simulatorState) {
// Check if canceled
if (simulatorState.getEngineState() == null) {
// notify to exit job
synchronized (simControlLock) {
simControlLock.notify();
}
}
}
};
simulationUIService.addStateChangeListener(canceledListener);
try {
monitor.beginTask("Simulate Steps", dlg.getSteps());
runSimulation(monitor, simControlLock, simulationUIService, simulationService, simulationEngine, dlg);
synchronized (simControlLock) {
simControlLock.wait();
}
return monitor.isCanceled() ? Status.CANCEL_STATUS : Status.OK_STATUS;
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (final Exception e) {
throw e;
} finally {
simulationUIService.unlockUserInterface();
simulationUIService.removeStateChangeListener(canceledListener);
simulationEngine.clearPendingCommands();
}
}
}.schedule();
}
} catch (final Exception ex) {
final Status status = new Status(IStatus.ERROR, FrameworkUtil.getBundle(getClass()).getSymbolicName(), "Error", ex);
StatusManager.getManager().handle(status, StatusManager.SHOW | StatusManager.LOG);
}
return null;
}
use of edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener in project AGREE by loonwerks.
the class DefaultSimulationUIService method updateCurrentState.
private void updateCurrentState() {
// Update the simulator state variable
if (currentActiveSimulation == null) {
simulatorState = new SimpleSimulatorState(null, null, SimulatorState.NO_FRAME_INDEX_SELECTED);
} else {
simulatorState = new SimpleSimulatorState(currentActiveSimulation.engine, currentActiveSimulation.state, currentActiveSimulation.selectedFrameIndex);
}
// Notify listeners in the display thread. In cases where many state changes occur in a short amount of time, do not notify UI listeners of all changes.
// Guaranteed to notify listeners of the first and last change in a sequence.
final Runnable notificationRunnable = new Runnable() {
@Override
public void run() {
updatePending = false;
// Notify simulation engine change listeners
for (final SimulatorStateListener l : stateChangeListeners) {
l.onSimulatorStateChanged(simulatorState);
}
}
};
if (!updatePending) {
updatePending = true;
Display.getDefault().asyncExec(notificationRunnable);
}
}
use of edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener in project AGREE by loonwerks.
the class OpenVariablesChartHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
final SimulationUIService simulationUIService = (SimulationUIService) Objects.requireNonNull(PlatformUI.getWorkbench().getService(SimulationUIService.class), "unable to get simulation UI service");
// Open Value Visualization Chart
simulationUIService.getCurrentState().getSimulationEngine().queueNotification(new NotificationHandler() {
@Override
public void handleNotification(final SimulationEngineNotification notification) {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
if (simulationUIService.getCurrentState().getEngineState().getNumberOfFrames() != 0) {
simulationUIService.lockUserInterface();
final Shell shell = new Shell(Display.getDefault().getActiveShell(), SWT.SHELL_TRIM & ~SWT.MIN);
// Listening for cancellation
final SimulatorStateListener simCanceledListener = new SimulatorStateListener() {
@Override
public void onSimulatorStateChanged(SimulatorState simulatorState) {
if (simulationUIService.getCurrentState().getEngineState() == null) {
if (shell != null && !shell.isDisposed()) {
// Close view on cancel
shell.close();
}
}
}
};
simulationUIService.addStateChangeListener(simCanceledListener);
try {
new VariablesChartDialog(shell, simulationUIService);
} finally {
simulationUIService.removeStateChangeListener(simCanceledListener);
simulationUIService.unlockUserInterface();
}
}
}
});
}
});
} catch (final Exception ex) {
final Status status = new Status(IStatus.ERROR, FrameworkUtil.getBundle(getClass()).getSymbolicName(), "Error", ex);
StatusManager.getManager().handle(status, StatusManager.SHOW | StatusManager.LOG);
}
return null;
}
Aggregations