Search in sources :

Example 1 with Rational

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

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

the class AGREESimulationState method convertLustreValue.

/**
 * Converts a JKind API Lustre value to types that are independent of the JKind API. In cases where there isn't an equivalent type in the standard
 * Java API, the original type will be preserved. Throws an
 * @param value non-null value object.
 * @throws RuntimeException if the value is not of a supported type.
 * @return
 */
private Object convertLustreValue(final Value value) {
    assert value != null;
    if (value instanceof ArrayValue) {
        final ArrayValue arrayValue = (ArrayValue) value;
        final ArrayList<Object> newList = new ArrayList<Object>(arrayValue.elements.size());
        for (final Value childValue : arrayValue.elements) {
            newList.add(convertLustreValue(childValue));
        }
        return newList;
    } else if (value instanceof BooleanValue) {
        return ((BooleanValue) value).value;
    } else if (value instanceof EnumValue) {
        return ((EnumValue) value).value;
    } else if (value instanceof IntegerValue) {
        return ((IntegerValue) value).value;
    } else if (value instanceof RealValue) {
        final BigFraction fraction = ((RealValue) value).value;
        return new Rational(fraction.getNumerator(), fraction.getDenominator());
    } else if (value instanceof RecordValue) {
        final RecordValue recordValue = (RecordValue) value;
        final Map<String, Object> newMap = new HashMap<String, Object>();
        for (final Entry<String, Value> entry : recordValue.fields.entrySet()) {
            newMap.put(entry.getKey(), convertLustreValue(entry.getValue()));
        }
        return newMap;
    } else if (value instanceof TupleValue) {
        final TupleValue tupleValue = (TupleValue) value;
        final ArrayList<Object> newList = new ArrayList<Object>(tupleValue.elements.size());
        for (final Value childValue : tupleValue.elements) {
            newList.add(convertLustreValue(childValue));
        }
        return newList;
    } else {
        throw new RuntimeException("Unhandled case. Value is of type: " + value.getClass());
    }
}
Also used : RealValue(jkind.lustre.values.RealValue) BigFraction(jkind.util.BigFraction) Rational(edu.uah.rsesc.aadlsimulator.Rational) EnumValue(jkind.lustre.values.EnumValue) IntegerValue(jkind.lustre.values.IntegerValue) ArrayList(java.util.ArrayList) RecordValue(jkind.lustre.values.RecordValue) TupleValue(jkind.lustre.values.TupleValue) Entry(java.util.Map.Entry) BooleanValue(jkind.lustre.values.BooleanValue) TupleValue(jkind.lustre.values.TupleValue) IntegerValue(jkind.lustre.values.IntegerValue) BooleanValue(jkind.lustre.values.BooleanValue) RealValue(jkind.lustre.values.RealValue) RecordValue(jkind.lustre.values.RecordValue) Value(jkind.lustre.values.Value) EnumValue(jkind.lustre.values.EnumValue) ArrayValue(jkind.lustre.values.ArrayValue) EObject(org.eclipse.emf.ecore.EObject) InstanceObject(org.osate.aadl2.instance.InstanceObject) ArrayValue(jkind.lustre.values.ArrayValue) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Rational (edu.uah.rsesc.aadlsimulator.Rational)2 NotificationHandler (edu.uah.rsesc.aadlsimulator.NotificationHandler)1 SimulationEngine (edu.uah.rsesc.aadlsimulator.SimulationEngine)1 SimulationEngineNotification (edu.uah.rsesc.aadlsimulator.SimulationEngineNotification)1 SimulationEngineState (edu.uah.rsesc.aadlsimulator.SimulationEngineState)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 SimulatorState (edu.uah.rsesc.aadlsimulator.ui.services.SimulatorState)1 SimulatorStateListener (edu.uah.rsesc.aadlsimulator.ui.services.SimulatorStateListener)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 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ArrayValue (jkind.lustre.values.ArrayValue)1 BooleanValue (jkind.lustre.values.BooleanValue)1