Search in sources :

Example 6 with SimulationEngine

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

the class CounterexampleLoaderHelper method receiveCex.

public void receiveCex(final ComponentImplementation compImpl, Property property, EObject agreeProperty, final Counterexample cex, final Map<String, EObject> refMap, final Mode mode) {
    // Launch the simulation
    final SimulationService simulationService = Objects.requireNonNull(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(SimulationService.class), "Unable to retrieve simulation service");
    final SimulationLaunchShortcut launchShortcut = new SimulationLaunchShortcut();
    try {
        final boolean isInductiveCex = property instanceof UnknownProperty;
        final ILaunch launch = launchShortcut.launch(compImpl, (isInductiveCex ? mode.inductiveEngineTypeId : mode.engineTypeId), ILaunchManager.RUN_MODE);
        // Get the simulation engine
        final SimulationEngine simulationEngine = getSimulationEngine(launch);
        if (simulationEngine instanceof AGREESimulationEngine) {
            final AGREESimulationEngine agreeSimulationEngine = (AGREESimulationEngine) simulationEngine;
            final SimulationUIService simulationUIService = Objects.requireNonNull(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(SimulationUIService.class), "Unable to retrieve simulation UI service");
            final Map<String, Object> signalNameToSimStateElementMap = buildAgreeNameToSimulationStateElementMap(agreeSimulationEngine);
            simulateCounterexample(cex, 0, signalNameToSimStateElementMap, agreeSimulationEngine, simulationService, simulationUIService);
        }
    } catch (final Exception e) {
        simulationService.getExceptionHandler().handleException(e);
    }
}
Also used : AGREESimulationEngine(edu.uah.rsesc.aadlsimulator.agree.engine.AGREESimulationEngine) SimulationEngine(edu.uah.rsesc.aadlsimulator.SimulationEngine) SimulationUIService(edu.uah.rsesc.aadlsimulator.ui.services.SimulationUIService) UnknownProperty(jkind.results.UnknownProperty) AGREESimulationEngine(edu.uah.rsesc.aadlsimulator.agree.engine.AGREESimulationEngine) ILaunch(org.eclipse.debug.core.ILaunch) EObject(org.eclipse.emf.ecore.EObject) SimulationLaunchShortcut(edu.uah.rsesc.aadlsimulator.ui.launch.SimulationLaunchShortcut) SimulationService(edu.uah.rsesc.aadlsimulator.services.SimulationService) PartInitException(org.eclipse.ui.PartInitException)

Example 7 with SimulationEngine

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

the class DefaultSimulationService method createEngine.

@Override
public SimulationEngine createEngine(final EngineType engineType, final SystemInstance systemInstance) {
    Objects.requireNonNull(engineType, "engineType must not be null");
    Objects.requireNonNull(engineType, "systemInstance must not be null");
    if (!(engineType instanceof DefaultEngineType)) {
        throw new IllegalArgumentException("engineType must be an engine type provided by this simulation service");
    }
    final DefaultEngineType defaultEngineType = (DefaultEngineType) engineType;
    final SimulationEngine newSimulationEngine = defaultEngineType.factory.create(systemInstance, masterExceptionHandler);
    // Notify listeners of the new simulation engine
    for (final SimulationEngineChangeListener l : simulationEngineChangeListeners) {
        l.onSimulationEngineCreated(newSimulationEngine);
    }
    return newSimulationEngine;
}
Also used : SimulationEngine(edu.uah.rsesc.aadlsimulator.SimulationEngine) SimulationEngineChangeListener(edu.uah.rsesc.aadlsimulator.services.SimulationEngineChangeListener)

Example 8 with SimulationEngine

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

the class SimulationLaunchConfigurationDelegate method launch.

@Override
public void launch(final ILaunchConfiguration configuration, final String mode, final ILaunch launch, final IProgressMonitor monitor) throws CoreException {
    SimulationService simulationService = null;
    try {
        simulationService = EclipseContextFactory.getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext()).get(SimulationService.class);
        Objects.requireNonNull(configuration, "configuration must not be null");
        // Find the component implementation
        final String componentImplementationName = configuration.getAttribute(SimulationLaunchConfigurationAttributes.COMPONENT_IMPLEMENTATION_NAME, (String) null);
        final String projectName = configuration.getAttribute(SimulationLaunchConfigurationAttributes.PROJECT_NAME, (String) null);
        final IProject project = projectName == null ? null : findProject(projectName);
        final EObject obj = componentImplementationName == null ? null : findComponentImplementation(project, componentImplementationName);
        if (obj instanceof ComponentImplementation) {
            // Refresh the component implementation
            final ComponentImplementation ci = refreshComponentImplementation((ComponentImplementation) obj);
            // (Re)instantiate the model
            final SystemInstance systemInstance = InstantiateModel.buildInstanceModelFile(ci);
            // Get the selected simulation engine type.
            final String selectedEngineTypeId = Objects.requireNonNull(configuration.getAttribute(SimulationLaunchConfigurationAttributes.ENGINE_TYPE_ID, (String) null), "Simulation Engine must be specified");
            final EngineType engineType = Objects.requireNonNull(simulationService.getEngineTypeById(selectedEngineTypeId), "Unable to find specified simulation engine");
            final SimulationEngine newEngine = simulationService.createEngine(engineType, systemInstance);
            final SimulationService simService = simulationService;
            simulationService.addSimulationEngineChangeListener(new SimulationEngineChangeListener() {

                @Override
                public void onSimulationEngineCreated(final SimulationEngine engine) {
                }

                @Override
                public void onSimulationEngineDisposed(final SimulationEngine engine) {
                    if (engine == newEngine) {
                        DebugPlugin.getDefault().getLaunchManager().removeLaunch(launch);
                        simService.removeSimulationEngineChangeListener(this);
                    }
                }
            });
            if (launch instanceof SimulationLaunch) {
                ((SimulationLaunch) launch).setSimulationEngine(newEngine);
            }
        } else {
            simulationService.getExceptionHandler().handleException(new CoreException(new Status(IStatus.ERROR, FrameworkUtil.getBundle(getClass()).getSymbolicName(), "Unable to find Component Implementation.")));
        }
    } catch (final Exception e) {
        DebugPlugin.getDefault().getLaunchManager().removeLaunch(launch);
        if (simulationService == null) {
            throw new RuntimeException(e);
        } else {
            simulationService.getExceptionHandler().handleException(e);
        }
    }
}
Also used : ComponentImplementation(org.osate.aadl2.ComponentImplementation) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) SimulationEngine(edu.uah.rsesc.aadlsimulator.SimulationEngine) CoreException(org.eclipse.core.runtime.CoreException) SystemInstance(org.osate.aadl2.instance.SystemInstance) EObject(org.eclipse.emf.ecore.EObject) SimulationEngineChangeListener(edu.uah.rsesc.aadlsimulator.services.SimulationEngineChangeListener) EngineType(edu.uah.rsesc.aadlsimulator.services.EngineType) SimulationService(edu.uah.rsesc.aadlsimulator.services.SimulationService)

Example 9 with SimulationEngine

use of edu.uah.rsesc.aadlsimulator.SimulationEngine 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 10 with SimulationEngine

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

Aggregations

SimulationEngine (edu.uah.rsesc.aadlsimulator.SimulationEngine)11 SimulationUIService (edu.uah.rsesc.aadlsimulator.ui.services.SimulationUIService)6 IStatus (org.eclipse.core.runtime.IStatus)6 Status (org.eclipse.core.runtime.Status)6 SimulatorState (edu.uah.rsesc.aadlsimulator.ui.services.SimulatorState)5 ExecutionException (org.eclipse.core.commands.ExecutionException)5 SimulationService (edu.uah.rsesc.aadlsimulator.services.SimulationService)4 SimulatorStateListener (edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener)3 Shell (org.eclipse.swt.widgets.Shell)3 SimulationEngineState (edu.uah.rsesc.aadlsimulator.SimulationEngineState)2 SimulationEngineChangeListener (edu.uah.rsesc.aadlsimulator.services.SimulationEngineChangeListener)2 SelectStepsDialog (edu.uah.rsesc.aadlsimulator.ui.dialogs.SelectStepsDialog)2 StepForwardCanceledException (edu.uah.rsesc.aadlsimulator.ui.services.StepForwardCanceledException)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 Job (org.eclipse.core.runtime.jobs.Job)2 EObject (org.eclipse.emf.ecore.EObject)2 GraphicalEditorService (org.osate.ge.services.GraphicalEditorService)2 NotificationHandler (edu.uah.rsesc.aadlsimulator.NotificationHandler)1 Rational (edu.uah.rsesc.aadlsimulator.Rational)1 SimulationEngineNotification (edu.uah.rsesc.aadlsimulator.SimulationEngineNotification)1