Search in sources :

Example 1 with TmfCommonXAxisModel

use of org.eclipse.tracecompass.tmf.core.model.TmfCommonXAxisModel in project tracecompass by tracecompass.

the class XYDataProviderBaseTest method extractModelFromChart.

/**
 * From a SWT Chart, this method extract a {@link TmfCommonXAxisModel} that
 * represents the chart. Since, we unfortunately have no mecanism to deserialize
 * with GSON, we have to compare strings. So, once the model is extract from the
 * Chart, we serialize it and compare with a string
 *
 * @param chart
 *            A SWT Chart
 * @param otherSeries
 *            Name of other series to extract from Chart
 * @return A {@link TmfCommonXAxisModel}
 */
protected TmfCommonXAxisModel extractModelFromChart(final Chart chart, String... otherSeries) {
    String mainSeriesName = getMainSeriesName();
    ISeries<Integer> mainSeries = chart.getSeriesSet().getSeries(mainSeriesName);
    if (mainSeries == null) {
        System.out.println("Main Series " + mainSeriesName + " not found in chart");
        return null;
    }
    /* X and Y Values shown in chart */
    double[] xMain = getXSeries(mainSeries);
    double[] yMain = getYSeries(mainSeries);
    Map<@NonNull String, @NonNull IYModel> yModels = new LinkedHashMap<>();
    yModels.put(mainSeriesName, new YModel(-1, mainSeriesName, Objects.requireNonNull(yMain)));
    for (String other : otherSeries) {
        if (other != null) {
            ISeries<Integer> series = chart.getSeriesSet().getSeries(other);
            if (series == null) {
                System.out.println("Series " + other + " not found in chart");
                return null;
            }
            /* X and Y Values shown in chart */
            double[] xSeries = getXSeries(series);
            double[] ySeries = getYSeries(series);
            /* Series should have the same x axis values, not finished updating all series*/
            if (!Arrays.equals(xMain, xSeries)) {
                System.out.println("Series don't currently have the same x axis values");
                return null;
            }
            yModels.put(other, new YModel(-1, other, Objects.requireNonNull(ySeries)));
        }
    }
    long[] x = Longs.toArray(Doubles.asList(xMain));
    assertNotNull(x);
    return new TmfCommonXAxisModel(getTitle(), x, yModels.values());
}
Also used : IYModel(org.eclipse.tracecompass.tmf.core.model.xy.IYModel) YModel(org.eclipse.tracecompass.tmf.core.model.YModel) TmfCommonXAxisModel(org.eclipse.tracecompass.tmf.core.model.TmfCommonXAxisModel) IYModel(org.eclipse.tracecompass.tmf.core.model.xy.IYModel) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with TmfCommonXAxisModel

use of org.eclipse.tracecompass.tmf.core.model.TmfCommonXAxisModel in project tracecompass by tracecompass.

the class XYDataProviderBaseTest method isChartDataValid.

/**
 * Based on a SWT Chart, we check if data shown is valid with a JSON file.
 * Comparison with the main series and other series if exists
 *
 * @param chart
 *            A SWT Chart
 * @param otherSeries
 *            An array of other series name to check other than the main series
 * @param expectedJson
 *            The path of the JSON file relative to this class's bundle
 * @return True if the serialized chart data matches the JSON file content
 */
protected boolean isChartDataValid(final Chart chart, String expectedJson, String... otherSeries) {
    /**
     * FIXME : Once CQ for Jackson is approved, use deserialization instead of
     * comparing strings
     */
    String expected = FileUtils.read(getFullPath(expectedJson));
    TmfCommonXAxisModel model = extractModelFromChart(chart, otherSeries);
    String current = fGson.toJson(model);
    return expected.equals(current);
}
Also used : TmfCommonXAxisModel(org.eclipse.tracecompass.tmf.core.model.TmfCommonXAxisModel)

Example 3 with TmfCommonXAxisModel

use of org.eclipse.tracecompass.tmf.core.model.TmfCommonXAxisModel in project tracecompass by tracecompass.

the class ExampleXYDataProvider method fetchXY.

@Override
public TmfModelResponse<ITmfXyModel> fetchXY(Map<String, Object> fetchParameters, @Nullable IProgressMonitor monitor) {
    ITmfStateSystem ss = getAnalysisModule().getStateSystem();
    if (ss == null) {
        return new TmfModelResponse<>(null, ITmfResponse.Status.FAILED, CommonStatusMessage.ANALYSIS_INITIALIZATION_FAILED);
    }
    Map<Integer, double[]> quarkToValues = new HashMap<>();
    // Prepare the quarks to display
    Collection<Long> selectedItems = DataProviderParameterUtils.extractSelectedItems(fetchParameters);
    if (selectedItems == null) {
        // No selected items, take them all
        selectedItems = fIDToDisplayQuark.keySet();
    }
    List<Long> times = getTimes(ss, DataProviderParameterUtils.extractTimeRequested(fetchParameters));
    for (Long id : selectedItems) {
        Integer quark = fIDToDisplayQuark.get(id);
        if (quark != null) {
            quarkToValues.put(quark, new double[times.size()]);
        }
    }
    long[] nativeTimes = new long[times.size()];
    for (int i = 0; i < times.size(); i++) {
        nativeTimes[i] = times.get(i);
    }
    // Query the state system to fill the array of values
    try {
        for (ITmfStateInterval interval : ss.query2D(quarkToValues.keySet(), times)) {
            if (monitor != null && monitor.isCanceled()) {
                return new TmfModelResponse<>(null, Status.CANCELLED, CommonStatusMessage.TASK_CANCELLED);
            }
            double[] row = quarkToValues.get(interval.getAttribute());
            Object value = interval.getValue();
            if (row != null && (value instanceof Number)) {
                Double dblValue = ((Number) value).doubleValue();
                for (int i = 0; i < times.size(); i++) {
                    Long time = times.get(i);
                    if (interval.getStartTime() <= time && interval.getEndTime() >= time) {
                        row[i] = dblValue;
                    }
                }
            }
        }
    } catch (IndexOutOfBoundsException | TimeRangeException | StateSystemDisposedException e) {
        return new TmfModelResponse<>(null, Status.FAILED, CommonStatusMessage.STATE_SYSTEM_FAILED);
    }
    List<IYModel> models = new ArrayList<>();
    for (Entry<Integer, double[]> values : quarkToValues.entrySet()) {
        models.add(new YModel(fIDToDisplayQuark.inverse().getOrDefault(values.getKey(), -1L), values.getValue()));
    }
    // $NON-NLS-1$
    return new TmfModelResponse<>(new TmfCommonXAxisModel("Example XY data provider", nativeTimes, models), Status.COMPLETED, CommonStatusMessage.COMPLETED);
}
Also used : YModel(org.eclipse.tracecompass.tmf.core.model.YModel) IYModel(org.eclipse.tracecompass.tmf.core.model.xy.IYModel) HashMap(java.util.HashMap) ITmfStateInterval(org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval) ArrayList(java.util.ArrayList) TmfModelResponse(org.eclipse.tracecompass.tmf.core.response.TmfModelResponse) StateSystemDisposedException(org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException) TmfCommonXAxisModel(org.eclipse.tracecompass.tmf.core.model.TmfCommonXAxisModel) ITmfStateSystem(org.eclipse.tracecompass.statesystem.core.ITmfStateSystem) TimeRangeException(org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException) IYModel(org.eclipse.tracecompass.tmf.core.model.xy.IYModel) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

TmfCommonXAxisModel (org.eclipse.tracecompass.tmf.core.model.TmfCommonXAxisModel)3 YModel (org.eclipse.tracecompass.tmf.core.model.YModel)2 IYModel (org.eclipse.tracecompass.tmf.core.model.xy.IYModel)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ITmfStateSystem (org.eclipse.tracecompass.statesystem.core.ITmfStateSystem)1 StateSystemDisposedException (org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException)1 TimeRangeException (org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException)1 ITmfStateInterval (org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval)1 TmfModelResponse (org.eclipse.tracecompass.tmf.core.response.TmfModelResponse)1