/
DataDrivenValueScript.java
160 lines (145 loc) · 6.53 KB
/
DataDrivenValueScript.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
/*******************************************************************************
* Copyright (c) 2017, 2022 É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.internal.tmf.analysis.xml.core.fsm.model.values;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.function.Function;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator;
import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.fsm.model.runtime.DataDrivenScenarioInfo;
import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.fsm.module.IAnalysisDataContainer;
import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
import org.mozilla.javascript.RhinoException;
/**
* A value that resolves to the result of a scripts
*
* @author Geneviève Bastien
* @author Abdelrahmane Berhil
*/
public class DataDrivenValueScript extends DataDrivenValue {
/** the default script engine */
public static final String DEFAULT_SCRIPT_ENGINE = "rhino"; //$NON-NLS-1$
private final Map<String, DataDrivenValue> fValues;
private final String fScriptEngine;
private final String fScript;
/**
* Constructor
*
* @param mappingGroupId
* The ID of the mapping group to use to map the retrieved value to
* another value
* @param forcedType
* The desired type of the value
* @param values
* A mapping of keys in the script with the values with which to
* replace to keys at runtime
* @param script
* The script to run
* @param scriptEngine
* The script engine. By default, it is {@link #DEFAULT_SCRIPT_ENGINE} (javascript)
*/
public DataDrivenValueScript(@Nullable String mappingGroupId, ITmfStateValue.Type forcedType, Map<String, DataDrivenValue> values, String script, String scriptEngine) {
super(mappingGroupId, forcedType);
fScriptEngine = !scriptEngine.isEmpty() ? scriptEngine : DEFAULT_SCRIPT_ENGINE;
fValues = values;
fScript = script;
}
@Override
protected @Nullable Object resolveValue(int quark, IAnalysisDataContainer container) {
return executeScript(sv -> sv.resolveValue(quark, container), container);
}
@Override
protected @Nullable Object resolveValue(ITmfEvent event, int quark, DataDrivenScenarioInfo scenarioInfo, IAnalysisDataContainer container) {
return executeScript(sv -> sv.resolveValue(event, quark, scenarioInfo, container), container);
}
private @Nullable Object executeScript(Function<DataDrivenValue, @Nullable Object> function, IAnalysisDataContainer container) {
Object result = null;
if (!fScriptEngine.equals(DEFAULT_SCRIPT_ENGINE)) {
// Other script engines (e.g. Nashorn for Java version < 15)
ScriptEngine engine = null;
engine = container.getScriptEngine(fScriptEngine);
if (engine == null) {
ScriptEngineManager manager = new ScriptEngineManager();
engine = manager.getEngineByName(fScriptEngine);
if (engine != null) {
container.setScriptengine(fScriptEngine, engine);
}
}
if (engine != null) {
for (Entry<String, DataDrivenValue> entry : fValues.entrySet()) {
String stateValueId = Objects.requireNonNull(entry.getKey());
DataDrivenValue stateValue = Objects.requireNonNull(entry.getValue());
Object value = function.apply(stateValue);
engine.put(stateValueId, value);
}
try {
result = engine.eval(fScript);
} catch (ScriptException e) {
Activator.logError("Script execution failed", e); //$NON-NLS-1$
return TmfStateValue.nullValue();
}
return result;
}
Activator.logWarning("Unknown script engine: " + fScriptEngine + ". Trying rhino instead."); //$NON-NLS-1$ //$NON-NLS-2$
}
return executeScriptRhino(function);
}
private @Nullable Object executeScriptRhino(Function<DataDrivenValue, @Nullable Object> function) {
// Note: In case of performance issues, implement caching of the engine
RhinoScriptEngine rhino = new RhinoScriptEngine();
rhino.setupEngine();
Object result = null;
try {
for (Entry<String, DataDrivenValue> entry : fValues.entrySet()) {
String stateValueId = Objects.requireNonNull(entry.getKey());
DataDrivenValue stateValue = Objects.requireNonNull(entry.getValue());
Object value = function.apply(stateValue);
if (value != null) {
rhino.put(stateValueId, value);
}
}
result = rhino.execute(fScript);
} catch (RhinoException e) {
Activator.logError("Script execution failed", e); //$NON-NLS-1$
return TmfStateValue.nullValue();
} finally {
rhino.teardownEngine();
}
return result;
}
@Override
public String toString() {
return "TmfXmlValueScript: " + fScript + " -> " + fValues; //$NON-NLS-1$ //$NON-NLS-2$
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), fValues, fScriptEngine, fScript);
}
@Override
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof DataDrivenValueScript)) {
return false;
}
if (!super.equals(obj)) {
return false;
}
DataDrivenValueScript other = (DataDrivenValueScript) obj;
return Objects.equals(fValues, other.fValues) &&
Objects.equals(fScriptEngine, other.fScriptEngine) &&
Objects.equals(fScript, other.fScript);
}
}