-
Notifications
You must be signed in to change notification settings - Fork 10
/
DataProviderScriptingModule.java
459 lines (431 loc) · 23.6 KB
/
DataProviderScriptingModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*******************************************************************************
* Copyright (c) 2019 École Polytechnique de Montréal
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.incubator.scripting.core.data.provider;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.eclipse.ease.modules.ScriptParameter;
import org.eclipse.ease.modules.WrapToScript;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.incubator.internal.scripting.core.data.provider.ScriptedEntryDataModel;
import org.eclipse.tracecompass.incubator.internal.scripting.core.data.provider.ScriptedTimeGraphDataProvider;
import org.eclipse.tracecompass.incubator.internal.scripting.core.data.provider.ScriptingDataProviderManager;
import org.eclipse.tracecompass.incubator.scripting.core.analysis.ScriptedAnalysis;
import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.fsm.model.DataDrivenStateSystemPath;
import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.fsm.model.values.DataDrivenValueConstant;
import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.output.DataDrivenOutputEntry;
import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.output.DataDrivenTimeGraphProviderFactory;
import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.output.DataDrivenXYDataProvider.DisplayType;
import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.output.DataDrivenXYProviderFactory;
import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
import org.eclipse.tracecompass.tmf.core.model.timegraph.ITimeGraphArrow;
import org.eclipse.tracecompass.tmf.core.model.timegraph.ITimeGraphDataProvider;
import org.eclipse.tracecompass.tmf.core.model.timegraph.ITimeGraphEntryModel;
import org.eclipse.tracecompass.tmf.core.model.timegraph.ITimeGraphRowModel;
import org.eclipse.tracecompass.tmf.core.model.timegraph.TimeGraphArrow;
import org.eclipse.tracecompass.tmf.core.model.timegraph.TimeGraphEntryModel;
import org.eclipse.tracecompass.tmf.core.model.tree.ITmfTreeDataModel;
import org.eclipse.tracecompass.tmf.core.model.xy.ITmfTreeXYDataProvider;
import org.eclipse.tracecompass.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
/**
* Scripting module to create data providers from scripted analyzes. Data
* providers are used to define what and how data will be exported, either as
* views or other means of exportation.
* <p>
* Example scripts with data providers can be found here:
* </p>
* <ul>
* <li><a href="../../doc-files/scriptedDataProvider.js">A scripted time graph
* data provider</a> with script-defined entries and arrows</li>
* <li><a href="../../doc-files/basicAnalysis.js">A basic analysis</a> building
* an state system and showing its data in a time graph</li>
* <li><a href="../../doc-files/statisticsDensityXY.js">A script that uses the
* "Statistics" analysis module</a> of Trace Compass to display an XY chart
* showing the density of certain events in the trace.</li>
* </ul>
*
* @author Geneviève Bastien
*/
@SuppressWarnings("restriction")
public class DataProviderScriptingModule {
/** Path key to create data providers */
@WrapToScript
public static final String ENTRY_PATH = "path"; //$NON-NLS-1$
/** Display key to create data providers */
@WrapToScript
public static final String ENTRY_DISPLAY = "display"; //$NON-NLS-1$
/** Name key to create data providers */
@WrapToScript
public static final String ENTRY_NAME = "name"; //$NON-NLS-1$
/** Parent key to create data providers */
@WrapToScript
public static final String ENTRY_PARENT = "parent"; //$NON-NLS-1$
/** Id key to create data providers */
@WrapToScript
public static final String ENTRY_ID = "id"; //$NON-NLS-1$
/**
* Key for whether or not to display a value as a differential with previous
* value or use its absolute value.
*/
@WrapToScript
public static final String ENTRY_DELTA = "delta"; //$NON-NLS-1$
/** Quark key to create entries */
@WrapToScript
public static final String ENTRY_FIELD_QUARK = "quark"; //$NON-NLS-1$
/** Parent ID key to create entries */
@WrapToScript
public static final String ENTRY_FIELD_PARENT_ID = "parentId"; //$NON-NLS-1$
/**
* Create a data driven time graph provider from one of the built-in
* analysis's state system. This will use the specified data to get the
* entries and row data from the state system. When the data cannot be
* obtained in a straight-forward manner from the state system, the
* {@link #createScriptedTimeGraphProvider(ScriptedAnalysis, Function, Function, Function)}
* method can be used instead.
* <p>
* The possible keys for the data are:
* </p>
* <ul>
* <li>{@link #ENTRY_PATH}: MANDATORY, specifies the path in the state
* system (including wildcards) that is the root of the entries. For all
* root attributes, use '*'</li>
* <li>{@link #ENTRY_DISPLAY}: The path from the entry's root of the
* attribute to display. If not specified, the root attribute itself will be
* used</li>
* <li>{@link #ENTRY_NAME}: The path from the entry's root of the attribute
* that contains the name. If not specified, the name will be the
* attribute's name.</li>
* <li>{@link #ENTRY_ID}: The path from the entry's root of the attribute
* that contains an identifier for this entry. The identifier can be used to
* build hierarchies of entries using the {@link #ENTRY_PARENT}.</li>
* <li>{@link #ENTRY_PARENT}: The path from the entry's root of the
* attribute that contains the parent's ID. This data will be used along
* with the {@link #ENTRY_ID} to create a hierarchy between the entries.
* </li>
* </ul>
*
* @param analysis
* The analysis for which to create a time graph provider
* @param data
* The time graph provider data
* @param dpName
* The unique name of this data provider. If not set, the
* analysis name will be used. Output of this data provider will
* use this name as the title
* @return The time graph data provider
*/
@WrapToScript
public @Nullable ITimeGraphDataProvider<TimeGraphEntryModel> createTimeGraphProvider(IAnalysisModule analysis, Map<String, Object> data, @ScriptParameter(defaultValue = "") String dpName) {
ITmfTrace trace = null;
if (analysis instanceof TmfAbstractAnalysisModule) {
TmfAbstractAnalysisModule newAnalysis = (TmfAbstractAnalysisModule) analysis;
trace = newAnalysis.getTrace();
}
List<ITmfStateSystem> stateSystems = new ArrayList<>();
if (analysis instanceof ITmfAnalysisModuleWithStateSystems) {
ITmfAnalysisModuleWithStateSystems module = (ITmfAnalysisModuleWithStateSystems) analysis;
if (module.schedule().isOK() && module.waitForInitialization()) {
module.getStateSystems().forEach(stateSystems::add);
}
}
if (stateSystems.isEmpty() || trace == null) {
return null;
}
String dataProviderName = dpName.isEmpty() ? String.valueOf(analysis.getName()) : dpName;
return createTimeGraphProvider(trace, stateSystems, dataProviderName, data);
}
/**
* Create a data driven time graph provider from a scripted analysis's state
* system. This will use the specified data to get the entries and row data
* from the state system. When the data cannot be obtained in a
* straight-forward manner from the state system, the
* {@link #createScriptedTimeGraphProvider(ScriptedAnalysis, Function, Function, Function)}
* method can be used instead.
* <p>
* The possible keys for the data are:
* </p>
* <ul>
* <li>{@link #ENTRY_PATH}: MANDATORY, specifies the path in the state
* system (including wildcards) that is the root of the entries. For all
* root attributes, use '*'</li>
* <li>{@link #ENTRY_DISPLAY}: The path from the entry's root of the
* attribute to display. If not specified, the root attribute itself will be
* used</li>
* <li>{@link #ENTRY_NAME}: The path from the entry's root of the attribute
* that contains the name. If not specified, the name will be the
* attribute's name.</li>
* <li>{@link #ENTRY_ID}: The path from the entry's root of the attribute
* that contains an identifier for this entry. The identifier can be used to
* build hierarchies of entries using the {@link #ENTRY_PARENT}.</li>
* <li>{@link #ENTRY_PARENT}: The path from the entry's root of the
* attribute that contains the parent's ID. This data will be used along
* with the {@link #ENTRY_ID} to create a hierarchy between the entries.
* </li>
* </ul>
*
* @param analysis
* The analysis for which to create a time graph provider
* @param data
* The time graph provider data
* @param dpName
* The unique name of this data provider. If not set, the
* analysis name will be used. Output of this data provider will
* use this name as the title
* @return The time graph data provider
*/
@WrapToScript
public @Nullable ITimeGraphDataProvider<TimeGraphEntryModel> createTimeGraphProvider(ScriptedAnalysis analysis, Map<String, Object> data, @ScriptParameter(defaultValue = "") String dpName) {
ITmfStateSystemBuilder stateSystem = analysis.getStateSystem(true);
if (stateSystem == null) {
return null;
}
String dataProviderName = dpName.isEmpty() ? String.valueOf(analysis.getName()) : dpName;
return createTimeGraphProvider(analysis.getTrace(), Collections.singletonList(stateSystem), dataProviderName, data);
}
private static @Nullable ITimeGraphDataProvider<TimeGraphEntryModel> createTimeGraphProvider(ITmfTrace trace,
List<ITmfStateSystem> stateSystems, String analysisName, Map<String, Object> data) {
Object pathObj = data.get(ENTRY_PATH);
if (pathObj == null) {
return null;
}
String path = String.valueOf(pathObj);
Object displayObj = data.get(ENTRY_DISPLAY);
DataDrivenStateSystemPath display = new DataDrivenStateSystemPath(displayObj == null ? Collections.emptyList() : Collections.singletonList(new DataDrivenValueConstant(null, ITmfStateValue.Type.NULL, String.valueOf(displayObj))));
Object nameObj = data.get(ENTRY_NAME);
DataDrivenStateSystemPath name = (nameObj == null) ? null : new DataDrivenStateSystemPath(Collections.singletonList(new DataDrivenValueConstant(null, ITmfStateValue.Type.NULL, String.valueOf(nameObj))));
Object parentObj = data.get(ENTRY_PARENT);
DataDrivenStateSystemPath parent = (parentObj == null) ? null : new DataDrivenStateSystemPath(Collections.singletonList(new DataDrivenValueConstant(null, ITmfStateValue.Type.NULL, String.valueOf(parentObj))));
Object idObj = data.get(ENTRY_ID);
DataDrivenStateSystemPath id = new DataDrivenStateSystemPath(Collections.singletonList(new DataDrivenValueConstant(null, ITmfStateValue.Type.NULL, String.valueOf(idObj))));
DataDrivenOutputEntry entry = new DataDrivenOutputEntry(Collections.emptyList(), path, null, true,
display, id, parent, name, DisplayType.ABSOLUTE);
ITimeGraphDataProvider<TimeGraphEntryModel> provider = DataDrivenTimeGraphProviderFactory.create(trace, stateSystems, Collections.singletonList(entry), Collections.emptyList(), ScriptingDataProviderManager.PROVIDER_ID + ':' + analysisName);
ScriptingDataProviderManager.getInstance().registerDataProvider(trace, provider);
return provider;
}
/**
* Create a data driven xy data provider from one of the built-in analyses.
* This will use the specified data to get the entries from the state
* system.
*
* {@link #ENTRY_PATH}: MANDATORY, specifies the path in the state system
* (including wildcards) that is the root of the entries. For all root
* attributes, use '*'
*
* {@link #ENTRY_DISPLAY}: The path from the entry's root of the attribute
* to display. If not specified, the root attribute itself will be used
*
* {@link #ENTRY_NAME}: The path from the entry's root of the attribute that
* contains the name. If not specified, the name will be the attribute's
* name.
*
* {@link #ENTRY_ID}: The path from the entry's root of the attribute that
* contains an identifier for this entry. The identifier can be used to
* build hierarchies of entries using the {@link #ENTRY_PARENT}.
*
* {@link #ENTRY_PARENT}: The path from the entry's root of the attribute
* that contains the parent's ID. This data will be used along with the
* {@link #ENTRY_ID} to create a hierarchy between the entries.
*
* {@link #ENTRY_DELTA}: Specify the entry type. If it’s true, it will
* display the delta value of the entry otherwise it will show the absolute
* value of the entry.
*
* @param analysis
* The analysis for which to create a time graph provider
* @param data
* The XY chart data
* @param dpName
* The unique name of this data provider. If not set, the
* analysis name will be used. Output of this data provider will
* use this name as the title
* @return The XY data provider
*/
@WrapToScript
public @Nullable ITmfTreeXYDataProvider<ITmfTreeDataModel> createXYProvider(IAnalysisModule analysis, Map<String, Object> data, @ScriptParameter(defaultValue = "") String dpName) {
ITmfTrace trace = null;
if (analysis instanceof TmfAbstractAnalysisModule) {
TmfAbstractAnalysisModule newAnalysis = (TmfAbstractAnalysisModule) analysis;
trace = newAnalysis.getTrace();
}
List<ITmfStateSystem> stateSystems = new ArrayList<>();
if (analysis instanceof ITmfAnalysisModuleWithStateSystems) {
ITmfAnalysisModuleWithStateSystems module = (ITmfAnalysisModuleWithStateSystems) analysis;
if (module.schedule().isOK() && module.waitForInitialization()) {
module.getStateSystems().forEach(stateSystems::add);
}
}
if (stateSystems.isEmpty() || trace == null) {
return null;
}
String dataProviderName = dpName.isEmpty() ? String.valueOf(analysis.getName()) : dpName;
return createXYProvider(trace, stateSystems, dataProviderName, data);
}
/**
* Create a data driven xy data provider from a scripted analysis. This will
* use the specified data to get the entries from the state system.
*
* {@link #ENTRY_PATH}: MANDATORY, specifies the path in the state system
* (including wildcards) that is the root of the entries. For all root
* attributes, use '*'
*
* {@link #ENTRY_DISPLAY}: The path from the entry's root of the attribute
* to display. If not specified, the root attribute itself will be used
*
* {@link #ENTRY_NAME}: The path from the entry's root of the attribute that
* contains the name. If not specified, the name will be the attribute's
* name.
*
* {@link #ENTRY_ID}: The path from the entry's root of the attribute that
* contains an identifier for this entry. The identifier can be used to
* build hierarchies of entries using the {@link #ENTRY_PARENT}.
*
* {@link #ENTRY_PARENT}: The path from the entry's root of the attribute
* that contains the parent's ID. This data will be used along with the
* {@link #ENTRY_ID} to create a hierarchy between the entries.
*
* {@link #ENTRY_DELTA}: Specify the entry type. If it’s true, it will
* display the delta value of the entry otherwise it will show the absolute
* value of the entry.
*
* @param analysis
* The scripted analysis for which to create a time graph
* provider
* @param data
* The XY chart options
* @param dpName
* The unique name of this data provider. If not set, the
* analysis name will be used. Output of this data provider will
* use this name as the title
* @return The XY data provider
*/
@WrapToScript
public @Nullable ITmfTreeXYDataProvider<ITmfTreeDataModel> createXYProvider(ScriptedAnalysis analysis, Map<String, Object> data, @ScriptParameter(defaultValue = "") String dpName) {
ITmfStateSystemBuilder stateSystem = analysis.getStateSystem(true);
if (stateSystem == null) {
return null;
}
String dataProviderName = dpName.isEmpty() ? String.valueOf(analysis.getName()) : dpName;
return createXYProvider(analysis.getTrace(), Collections.singletonList(stateSystem), dataProviderName, data);
}
private static @Nullable ITmfTreeXYDataProvider<ITmfTreeDataModel> createXYProvider(ITmfTrace trace,
List<ITmfStateSystem> stateSystems, String analysisName, Map<String, Object> data) {
Object pathObj = data.get(ENTRY_PATH);
if (pathObj == null) {
return null;
}
String path = String.valueOf(pathObj);
Object displayObj = data.get(ENTRY_DISPLAY);
DataDrivenStateSystemPath display = new DataDrivenStateSystemPath(displayObj == null ? Collections.emptyList() : Collections.singletonList(new DataDrivenValueConstant(null, ITmfStateValue.Type.NULL, String.valueOf(displayObj))));
Object nameObj = data.get(ENTRY_NAME);
DataDrivenStateSystemPath name = (nameObj == null) ? null : new DataDrivenStateSystemPath(Collections.singletonList(new DataDrivenValueConstant(null, ITmfStateValue.Type.NULL, String.valueOf(nameObj))));
Object parentObj = data.get(ENTRY_PARENT);
DataDrivenStateSystemPath parent = (parentObj == null) ? null : new DataDrivenStateSystemPath(Collections.singletonList(new DataDrivenValueConstant(null, ITmfStateValue.Type.NULL, String.valueOf(parentObj))));
Object idObj = data.get(ENTRY_ID);
DataDrivenStateSystemPath id = new DataDrivenStateSystemPath(Collections.singletonList(new DataDrivenValueConstant(null, ITmfStateValue.Type.NULL, String.valueOf(idObj))));
Object isDeltaObj = data.get(ENTRY_DELTA);
DisplayType displayType = DisplayType.ABSOLUTE;
if (isDeltaObj != null && (Boolean) isDeltaObj) {
displayType = DisplayType.DELTA;
}
DataDrivenOutputEntry entry = new DataDrivenOutputEntry(Collections.emptyList(), path, null, true,
display, id, parent, name, displayType);
ITmfTreeXYDataProvider<ITmfTreeDataModel> provider = DataDrivenXYProviderFactory.create(trace, stateSystems, Collections.singletonList(entry), ScriptingDataProviderManager.PROVIDER_ID + ':' + analysisName);
ScriptingDataProviderManager.getInstance().registerDataProvider(trace, provider);
return provider;
}
/**
* Create a time graph entry. This method will typically be used by scripts
* to create entries for data provider generated by the
* {@link #createScriptedTimeGraphProvider(ScriptedAnalysis, Function, Function, Function)}
* method.
*
* @param name
* The name (label) of the entry
* @param data
* A map of fields to create the entry. Possible fields are
* {@link #ENTRY_FIELD_QUARK} (quark in the state system to use
* for the data. If not specified, it is expected the data
* provider will provide a method to retrieve the data), and
* {@link #ENTRY_FIELD_PARENT_ID} for the ID of the parent entry
* @return A new entry model
*/
@WrapToScript
public @Nullable ITmfTreeDataModel createEntry(String name, Map<String, Object> data) {
Object quarkObj = data.get(ENTRY_FIELD_QUARK);
int quark = (!(quarkObj instanceof Number)) ? ITmfStateSystem.INVALID_ATTRIBUTE : ((Number) quarkObj).intValue();
Object parentObj = data.get(ENTRY_FIELD_PARENT_ID);
int parent = (!(parentObj instanceof Number)) ? -1 : ((Number) parentObj).intValue();
return new ScriptedEntryDataModel(name, parent, quark);
}
/**
* Create an arrow for a time graph data provider. This method will
* typically be used by scripts to create entries for data provider
* generated by the
* {@link #createScriptedTimeGraphProvider(ScriptedAnalysis, Function, Function, Function)}
* method.
*
* @param sourceId
* the ID of the source entry of the arrow
* @param destinationId
* The ID of the destination entry of the arrow
* @param time
* The start time of the arrow
* @param duration
* The duration of the arrow
* @param value
* The value to associate with this arrow
* @return The new arrow
*/
@WrapToScript
public @Nullable ITimeGraphArrow createArrow(long sourceId, long destinationId, long time, long duration, int value) {
return new TimeGraphArrow(sourceId, destinationId, time, duration, value);
}
/**
* Create a data provider from scripted functions. The script itself is
* responsible for generating the entries, optionally row data and arrows.
* For a simple state system, the
* {@link #createTimeGraphProvider(ScriptedAnalysis, Map, String)} may be used
* instead
*
* @param analysis
* The analysis this data provider is for
* @param entryMethod
* The function this data provider will use to get the entries.
* This parameter is mandatory.
* @param rowModelMethod
* The function this data provider will use to get the row data
* for time ranges. If none is specified, the entries are
* expected to have a quark indicating which row in the state
* system to use for the data.
* @param arrowMethod
* The function this data provider will use to get the arrow data
* for time ranges. If none is specified, no arrows will be
* drawn.
* @return A time graph data provider
*/
@WrapToScript
public ITimeGraphDataProvider<ITimeGraphEntryModel> createScriptedTimeGraphProvider(ScriptedAnalysis analysis,
Function<Map<String, Object>, @Nullable List<ITimeGraphEntryModel>> entryMethod,
@Nullable Function<Map<String, Object>, @Nullable List<ITimeGraphRowModel>> rowModelMethod,
@Nullable Function<Map<String, Object>, @Nullable List<ITimeGraphArrow>> arrowMethod) {
ITimeGraphDataProvider<ITimeGraphEntryModel> provider = new ScriptedTimeGraphDataProvider(analysis, entryMethod, rowModelMethod, arrowMethod);
ScriptingDataProviderManager.getInstance().registerDataProvider(analysis.getTrace(), provider);
return provider;
}
}