Search in sources :

Example 6 with SimulationService

use of edu.uah.rsesc.aadlsimulator.services.SimulationService 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;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IStatus(org.eclipse.core.runtime.IStatus) SelectStepsDialog(edu.uah.rsesc.aadlsimulator.ui.dialogs.SelectStepsDialog) StepForwardCanceledException(edu.uah.rsesc.aadlsimulator.ui.services.StepForwardCanceledException) SimulatorStateListener(edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener) SimulatorState(edu.uah.rsesc.aadlsimulator.ui.services.SimulatorState) ExecutionException(org.eclipse.core.commands.ExecutionException) StepForwardCanceledException(edu.uah.rsesc.aadlsimulator.ui.services.StepForwardCanceledException) SimulationUIService(edu.uah.rsesc.aadlsimulator.ui.services.SimulationUIService) SimulationEngine(edu.uah.rsesc.aadlsimulator.SimulationEngine) Shell(org.eclipse.swt.widgets.Shell) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Job(org.eclipse.core.runtime.jobs.Job) SimulationService(edu.uah.rsesc.aadlsimulator.services.SimulationService)

Aggregations

SimulationService (edu.uah.rsesc.aadlsimulator.services.SimulationService)6 SimulationEngine (edu.uah.rsesc.aadlsimulator.SimulationEngine)4 IStatus (org.eclipse.core.runtime.IStatus)4 Status (org.eclipse.core.runtime.Status)4 SimulationUIService (edu.uah.rsesc.aadlsimulator.ui.services.SimulationUIService)3 EngineType (edu.uah.rsesc.aadlsimulator.services.EngineType)2 SimulatorState (edu.uah.rsesc.aadlsimulator.ui.services.SimulatorState)2 ExecutionException (org.eclipse.core.commands.ExecutionException)2 EObject (org.eclipse.emf.ecore.EObject)2 AGREESimulationEngine (edu.uah.rsesc.aadlsimulator.agree.engine.AGREESimulationEngine)1 SimulationEngineChangeListener (edu.uah.rsesc.aadlsimulator.services.SimulationEngineChangeListener)1 SelectStepsDialog (edu.uah.rsesc.aadlsimulator.ui.dialogs.SelectStepsDialog)1 StopHandler (edu.uah.rsesc.aadlsimulator.ui.handlers.StopHandler)1 SimulationLaunchShortcut (edu.uah.rsesc.aadlsimulator.ui.launch.SimulationLaunchShortcut)1 SimulatorStateListener (edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener)1 StepForwardCanceledException (edu.uah.rsesc.aadlsimulator.ui.services.StepForwardCanceledException)1 UnknownProperty (jkind.results.UnknownProperty)1 IProject (org.eclipse.core.resources.IProject)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1