Search in sources :

Example 1 with StepForwardCanceledException

use of edu.uah.rsesc.aadlsimulator.ui.services.StepForwardCanceledException in project AGREE by loonwerks.

the class DefaultSimulationUIService method stepForward.

@Override
public void stepForward() {
    Objects.requireNonNull(currentActiveSimulation, "No active simulation");
    try {
        prepareToStepForward();
        currentActiveSimulation.engine.stepForward();
    } catch (StepForwardCanceledException ex) {
        final Status status = new Status(IStatus.ERROR, FrameworkUtil.getBundle(getClass()).getSymbolicName(), "Unable to Step Forward", ex);
        StatusManager.getManager().handle(status, StatusManager.SHOW);
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) StepForwardCanceledException(edu.uah.rsesc.aadlsimulator.ui.services.StepForwardCanceledException)

Example 2 with StepForwardCanceledException

use of edu.uah.rsesc.aadlsimulator.ui.services.StepForwardCanceledException 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;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) SelectStepsDialog(edu.uah.rsesc.aadlsimulator.ui.dialogs.SelectStepsDialog) SimulationEngineState(edu.uah.rsesc.aadlsimulator.SimulationEngineState) SimulatorStateListener(edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener) StepForwardCanceledException(edu.uah.rsesc.aadlsimulator.ui.services.StepForwardCanceledException) ArrayList(java.util.ArrayList) 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)

Example 3 with StepForwardCanceledException

use of edu.uah.rsesc.aadlsimulator.ui.services.StepForwardCanceledException 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)

Example 4 with StepForwardCanceledException

use of edu.uah.rsesc.aadlsimulator.ui.services.StepForwardCanceledException in project AGREE by loonwerks.

the class DefaultSimulationUIService method prepareToStepForward.

public void prepareToStepForward() throws StepForwardCanceledException {
    final BeforeStepForwardEvent beforeStepForwardEvent = new BeforeStepForwardEvent();
    eventBroker.send(SimulatorUIEvents.BEFORE_STEP_FORWARD, beforeStepForwardEvent);
    if (beforeStepForwardEvent.isStepCanceled()) {
        throw new StepForwardCanceledException(beforeStepForwardEvent.getCancellationReason());
    }
}
Also used : BeforeStepForwardEvent(edu.uah.rsesc.aadlsimulator.ui.events.BeforeStepForwardEvent) StepForwardCanceledException(edu.uah.rsesc.aadlsimulator.ui.services.StepForwardCanceledException)

Aggregations

StepForwardCanceledException (edu.uah.rsesc.aadlsimulator.ui.services.StepForwardCanceledException)4 IStatus (org.eclipse.core.runtime.IStatus)3 Status (org.eclipse.core.runtime.Status)3 SimulationEngine (edu.uah.rsesc.aadlsimulator.SimulationEngine)2 SelectStepsDialog (edu.uah.rsesc.aadlsimulator.ui.dialogs.SelectStepsDialog)2 SimulationUIService (edu.uah.rsesc.aadlsimulator.ui.services.SimulationUIService)2 SimulatorState (edu.uah.rsesc.aadlsimulator.ui.services.SimulatorState)2 SimulatorStateListener (edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener)2 ExecutionException (org.eclipse.core.commands.ExecutionException)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 Job (org.eclipse.core.runtime.jobs.Job)2 Shell (org.eclipse.swt.widgets.Shell)2 SimulationEngineState (edu.uah.rsesc.aadlsimulator.SimulationEngineState)1 SimulationService (edu.uah.rsesc.aadlsimulator.services.SimulationService)1 BeforeStepForwardEvent (edu.uah.rsesc.aadlsimulator.ui.events.BeforeStepForwardEvent)1 ArrayList (java.util.ArrayList)1