Search in sources :

Example 1 with SimulationEngineNotification

use of edu.uah.rsesc.aadlsimulator.SimulationEngineNotification in project AGREE by loonwerks.

the class SimulatePossibilitiesHandler method runSimulation.

private void runSimulation(final IProgressMonitor monitor, final Object simControlLock, final SimulationUIService simulationUIService, final SimulationEngine simulationEngine, final List<SimulationEngineState> simulationEngineStates, final SelectStepsDialog dlg) {
    // Step forward
    for (int i = 0; i < dlg.getSteps(); i++) {
        simulationEngine.stepForward();
        // Add state to list
        simulationEngine.queueNotification(new NotificationHandler() {

            @Override
            public void handleNotification(final SimulationEngineNotification notification) {
                if (monitor.isCanceled()) {
                    synchronized (simControlLock) {
                        simControlLock.notify();
                    }
                    return;
                }
                final SimulationEngineState simulationState = simulationEngine.getCurrentState();
                simulationEngineStates.add(simulationState);
                // Update monitor
                monitor.subTask("Simulating step " + simulationEngineStates.size() + " of " + dlg.getSteps());
                monitor.worked(1);
            }
        });
        // Step backward
        simulationEngine.stepBackward();
    }
}
Also used : SimulationEngineState(edu.uah.rsesc.aadlsimulator.SimulationEngineState) NotificationHandler(edu.uah.rsesc.aadlsimulator.NotificationHandler) SimulationEngineNotification(edu.uah.rsesc.aadlsimulator.SimulationEngineNotification)

Example 2 with SimulationEngineNotification

use of edu.uah.rsesc.aadlsimulator.SimulationEngineNotification 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));
                        }
                    }
                }
            });
        }
    });
}
Also used : SetValueSelectionAdapter(edu.uah.rsesc.aadlsimulator.ui.dialogs.SimulatePossibilitiesChartDialog.SetValueSelectionAdapter) Rational(edu.uah.rsesc.aadlsimulator.Rational) SimulationEngineState(edu.uah.rsesc.aadlsimulator.SimulationEngineState) SimulatorStateListener(edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener) ChartElement(edu.uah.rsesc.aadlsimulator.ui.dialogs.ChartHelper.ChartElement) BooleanLiteral(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BooleanLiteral) SimulatorState(edu.uah.rsesc.aadlsimulator.ui.services.SimulatorState) SimulationEngine(edu.uah.rsesc.aadlsimulator.SimulationEngine) BinaryExpression(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BinaryExpression) NotificationHandler(edu.uah.rsesc.aadlsimulator.NotificationHandler) SimulationEngineNotification(edu.uah.rsesc.aadlsimulator.SimulationEngineNotification) BigInteger(java.math.BigInteger) SimulatePossibilitiesChartDialog(edu.uah.rsesc.aadlsimulator.ui.dialogs.SimulatePossibilitiesChartDialog) IntegerLiteral(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntegerLiteral)

Example 3 with SimulationEngineNotification

use of edu.uah.rsesc.aadlsimulator.SimulationEngineNotification in project AGREE by loonwerks.

the class StepForwardMultipleTimesHandler method runSimulation.

protected void runSimulation(final IProgressMonitor monitor, final Object simControlLock, final SimulationUIService simulationUIService, final SimulationService simulationService, final SimulationEngine simulationEngine, final SelectStepsDialog dlg) {
    for (int i = 1; i <= dlg.getSteps(); i++) {
        // Step forward
        simulationEngine.stepForward();
        final String subTaskMsg = "Simulating step " + i + " of " + dlg.getSteps();
        // Add state to list
        simulationEngine.queueNotification(new NotificationHandler() {

            @Override
            public void handleNotification(final SimulationEngineNotification notification) {
                if (monitor.isCanceled() || !notification.getEngineState().canStepForward()) {
                    if (!notification.getEngineState().canStepForward()) {
                        final Status status = new Status(IStatus.ERROR, FrameworkUtil.getBundle(getClass()).getSymbolicName(), "Simulation has halted.");
                        StatusManager.getManager().handle(status, StatusManager.SHOW);
                    }
                    simulationEngine.clearPendingCommands();
                    synchronized (simControlLock) {
                        simControlLock.notify();
                    }
                    return;
                }
                // Update monitor
                monitor.subTask(subTaskMsg);
                monitor.worked(1);
            }
        });
    }
    simulationEngine.queueNotification(new NotificationHandler() {

        @Override
        public void handleNotification(final SimulationEngineNotification notification) {
            monitor.done();
            synchronized (simControlLock) {
                simControlLock.notify();
            }
        }
    });
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) NotificationHandler(edu.uah.rsesc.aadlsimulator.NotificationHandler) SimulationEngineNotification(edu.uah.rsesc.aadlsimulator.SimulationEngineNotification)

Example 4 with SimulationEngineNotification

use of edu.uah.rsesc.aadlsimulator.SimulationEngineNotification 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;
}
Also used : SimulationUIService(edu.uah.rsesc.aadlsimulator.ui.services.SimulationUIService) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) Shell(org.eclipse.swt.widgets.Shell) VariablesChartDialog(edu.uah.rsesc.aadlsimulator.ui.dialogs.VariablesChartDialog) SimulatorStateListener(edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener) NotificationHandler(edu.uah.rsesc.aadlsimulator.NotificationHandler) SimulatorState(edu.uah.rsesc.aadlsimulator.ui.services.SimulatorState) SimulationEngineNotification(edu.uah.rsesc.aadlsimulator.SimulationEngineNotification) ExecutionException(org.eclipse.core.commands.ExecutionException)

Aggregations

NotificationHandler (edu.uah.rsesc.aadlsimulator.NotificationHandler)4 SimulationEngineNotification (edu.uah.rsesc.aadlsimulator.SimulationEngineNotification)4 SimulationEngineState (edu.uah.rsesc.aadlsimulator.SimulationEngineState)2 SimulatorState (edu.uah.rsesc.aadlsimulator.ui.services.SimulatorState)2 SimulatorStateListener (edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 Rational (edu.uah.rsesc.aadlsimulator.Rational)1 SimulationEngine (edu.uah.rsesc.aadlsimulator.SimulationEngine)1 ChartElement (edu.uah.rsesc.aadlsimulator.ui.dialogs.ChartHelper.ChartElement)1 SimulatePossibilitiesChartDialog (edu.uah.rsesc.aadlsimulator.ui.dialogs.SimulatePossibilitiesChartDialog)1 SetValueSelectionAdapter (edu.uah.rsesc.aadlsimulator.ui.dialogs.SimulatePossibilitiesChartDialog.SetValueSelectionAdapter)1 VariablesChartDialog (edu.uah.rsesc.aadlsimulator.ui.dialogs.VariablesChartDialog)1 SimulationUIService (edu.uah.rsesc.aadlsimulator.ui.services.SimulationUIService)1 BinaryExpression (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BinaryExpression)1 BooleanLiteral (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.BooleanLiteral)1 IntegerLiteral (edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.IntegerLiteral)1 BigInteger (java.math.BigInteger)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 Shell (org.eclipse.swt.widgets.Shell)1