-
Notifications
You must be signed in to change notification settings - Fork 10
/
StateMachineVariableCounter.java
292 lines (248 loc) · 13.4 KB
/
StateMachineVariableCounter.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
/*******************************************************************************
* Copyright (c) 2016 É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.internal.xaf.core.statemachine.variable;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.backend.StateMachineBackendAnalysis;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.constraint.Operator;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.constraint.StateMachineConstraint;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.constraint.ValueType;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.exception.StateMachineNoInvalidCaseException;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.exception.StateMachineNoValidCaseException;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.exception.StateMachineStateSystemNotFoundException;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.exception.StateMachineStateSystemTimeRangeException;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.exception.StateMachineTIDNotFoundException;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.exception.StateMachineUnexpectedEventException;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.exception.StateMachineVariableException;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.variable.StateMachineVariableAnalysis.DefaultInterruptionEventRunner;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.variable.StateMachineVariableAnalysis.InterruptionEventGetter;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.variable.StateMachineVariableAnalysis.InterruptionEventRunner;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.variable.StateMachineVariableAnalysis.ResponsibilityMapGetter;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.variable.utils.InterruptionEvent;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.variable.utils.InterruptionEventList;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.variable.utils.InterruptionIntervalSetDiff;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.variable.utils.InterruptionReason;
import org.eclipse.tracecompass.incubator.internal.xaf.core.statemachine.variable.utils.ResponsibilityMap;
import org.eclipse.tracecompass.incubator.internal.xaf.ui.Activator;
import org.eclipse.tracecompass.incubator.internal.xaf.ui.statemachine.StateMachineInstance;
import org.eclipse.tracecompass.incubator.internal.xaf.ui.statemachine.StateMachineInstance.InstanceStepInformation;
import org.eclipse.tracecompass.incubator.internal.xaf.ui.statemachine.StateMachineReport;
import org.eclipse.tracecompass.incubator.internal.xaf.ui.statemachine.StateMachineUtils.TimestampInterval;
import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
/**
* Class that represents a state machine variable of counter class
* @author Raphaël Beamonte
*/
public abstract class StateMachineVariableCounter extends StateMachineVariable {
/**
* @param name The name of the variable
* @param value The initial value of the variable
*/
@SuppressWarnings("rawtypes")
public StateMachineVariableCounter(String name, Comparable value) {
super(name, value);
}
/**
* @return The attribute of this variable in the backend
*/
public abstract String getCounterAttribute();
@Override
public Comparable getValue(ITmfEvent event, StateMachineInstance stateMachineInstance) throws StateMachineVariableException {
if (stateMachineInstance.getStateMachineInstanceGroup().getStateMachineBackendAnalysisModules().isEmpty()) {
throw new StateMachineVariableException("State system unavailable"); //$NON-NLS-1$
}
ITmfTimestamp intervalStart = (ITmfTimestamp) getValue();
ITmfTimestamp intervalEnd = event.getTimestamp();
int tid = stateMachineInstance.getVTid();
Integer counter = 0;
for (StateMachineBackendAnalysis kernelAnalysisModule : stateMachineInstance.getStateMachineInstanceGroup().getStateMachineBackendAnalysisModules()) {
Integer lcounter = kernelAnalysisModule.getCounterIntvl(tid, intervalStart.getValue(), intervalEnd.getValue(), getCounterAttribute());
if (lcounter != null) {
counter += lcounter;
}
}
return counter;
}
@Override
public Integer compareTo(Comparable toCompare, ITmfEvent event, StateMachineInstance stateMachineInstance) throws StateMachineVariableException {
Integer counter = (Integer)getValue(event, stateMachineInstance);
Integer valueComparaison = Integer.parseInt((String)toCompare);
Integer comparison = counter.compareTo(valueComparaison);
return comparison;
}
@Override
public Double getNumericalValue(Comparable format, ITmfEvent event, StateMachineInstance stateMachineInstance) {
try {
return ((Integer)getValue(event, stateMachineInstance)).doubleValue();
} catch (StateMachineVariableException e) {
return null;
}
}
@Override
public String getFormattedValue(Comparable format, ITmfEvent event, StateMachineInstance stateMachineInstance) {
Double delta;
try {
delta = ((Integer)getValue(event, stateMachineInstance)).doubleValue();
} catch (StateMachineVariableException e) {
delta = null;
}
return getFormattedValue(format, delta);
}
@Override
public String getFormattedValue(Comparable format, Double value) {
if (value == null) {
return "unavailable"; //$NON-NLS-1$
}
return String.valueOf(value);
}
private class DefaultCounterInterruptionEventGetter extends InterruptionEventGetter {
@Override
public InterruptionEventList execute(TimestampInterval ti, ITmfStateInterval si, ITmfEvent e) throws StateMachineStateSystemNotFoundException, StateMachineTIDNotFoundException, StateMachineStateSystemTimeRangeException, StateMachineUnexpectedEventException {
InterruptionEventList taskList = new InterruptionEventList();
if (ti.getStartTime().getValue() > si.getStartTime()) {
return taskList;
}
// Get the start and end of the state, given the start and end of the interval
long startTime = Math.max(si.getStartTime(), ti.getStartTime().getValue());
long endTime = startTime + 1;
InterruptionReason ir = getInterruptionReasonFromEvent(e);
// Add an InterruptionEvent to the taskList
taskList.events.add(new InterruptionEvent(e,
TmfTimestamp.create(startTime, ITmfTimestamp.NANOSECOND_SCALE),
TmfTimestamp.create(endTime, ITmfTimestamp.NANOSECOND_SCALE),
ir));
return taskList;
}
}
/**
* @return The interruption event getter for that variable
*/
public InterruptionEventGetter getInterruptionEventGetter() {
return new DefaultCounterInterruptionEventGetter();
}
/**
* @param e The event from which to extract the interruption reason
* @return The interruption reason computed from the event
* @throws StateMachineUnexpectedEventException If the kind of event received is abnormal
*/
protected abstract InterruptionReason getInterruptionReasonFromEvent(ITmfEvent e) throws StateMachineUnexpectedEventException;
/**
* @return The interruption event runner for that variable
*/
public InterruptionEventRunner getInterruptionEventRunner() {
return new DefaultInterruptionEventRunner(
this,
Objects.requireNonNull(getCounterAttribute()),
Objects.requireNonNull(getInterruptionEventGetter()),
new ResponsibilityMapGetter<InterruptionReason>() {
@Override
public ResponsibilityMap<InterruptionReason> execute(InterruptionIntervalSetDiff iisd) {
return iisd.getReasonCounterResponsibilityMap();
}
});
}
@Override
public StateMachineVariableAnalysis analyze(StateMachineConstraint constraint, List<InstanceStepInformation> invalidIsiList, List<InstanceStepInformation> validIsiList) {
// Treat only constants for now
// TODO: treat comparison between variables
if (constraint.getValueType() != ValueType.CONSTANT) {
Activator.logWarning("TODO"); //$NON-NLS-1$
return null;
}
// value of X
int X = Integer.parseInt(constraint.getValue());
boolean partial = (
(constraint.getOperator() == Operator.LT && X < 2)
|| (constraint.getOperator() == Operator.EQ && X == 0));
StateMachineVariableAnalysis smva = new StateMachineVariableAnalysis(
this,
constraint,
invalidIsiList,
validIsiList);
InterruptionEventRunner interruptionEventRunner = getInterruptionEventRunner();
StateMachineReport.R.println();
StateMachineReport.R.println_subsubsubsection(String.format("%s analysis: (%s)", //$NON-NLS-1$
getVariableType(),
(partial) ? "partial" : "full")); //$NON-NLS-1$ //$NON-NLS-2$
if (partial) {
try {
smva.doPartialAnalysis(interruptionEventRunner);
} catch (StateMachineNoInvalidCaseException e) {
Activator.logError(e.getMessage(), e);
return null;
}
} else {
if (validIsiList == null || validIsiList.isEmpty()) {
Activator.logWarning("No valid instance for a full analysis"); //$NON-NLS-1$
return null;
}
try {
smva.doAnalysis(interruptionEventRunner);
} catch (StateMachineNoValidCaseException | StateMachineNoInvalidCaseException e) {
Activator.logError(e.getMessage(), e);
return null;
}
}
StateMachineReport.R.inc();
StateMachineReport.R.printDropped(smva);
if (!partial) {
StateMachineReport.R.println(smva.getDifferentValidCases() +
" different valid situation" + (smva.getDifferentValidCases() > 1 ? "s" : "") + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
" for " + validIsiList.size() + //$NON-NLS-1$
" different valid case" + (validIsiList.size() > 1 ? "s" : "") + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"." //$NON-NLS-1$
);
}
StateMachineReport.R.println(smva.getDifferentInvalidCases() +
" different invalid situation" + (smva.getDifferentInvalidCases() > 1 ? "s" : "") + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
" for " + invalidIsiList.size() + //$NON-NLS-1$
" different invalid case" + (invalidIsiList.size() > 1 ? "s" : "") + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"." //$NON-NLS-1$
);
StateMachineReport.R.println();
ResponsibilityMap<InterruptionReason> analysisResponsibilityMap = smva.getAnalysisResponsibilityMap();
if (!partial) {
StateMachineReport.R.println("General computed difference between invalid and valid instances:"); //$NON-NLS-1$
}
String[] header = {"Responsible", "Responsibility"}; //$NON-NLS-1$ //$NON-NLS-2$
String[][] strs = new String[analysisResponsibilityMap.getCollection().size()][2];
int idx = 0;
int progressSize = 20;
for (Entry<InterruptionReason, Double> entry : analysisResponsibilityMap.getCollection()) {
double ratioValue = entry.getValue() * 100;
String symbol = StateMachineReport.progressBar(ratioValue, progressSize);
strs[idx][0] = entry.getKey().getTaskId();
strs[idx][1] = String.format("%.2f%% %s", ratioValue, symbol); //$NON-NLS-1$
idx++;
}
StateMachineReport.R.println_table(header, strs, Arrays.asList(new Integer[]{1}));
StateMachineReport.R.println();
StateMachineReport.R.dec();
return smva;
}
@Override
public String getVariableType() {
return this.getClass()
.getSimpleName()
.substring(StateMachineVariableCounter.class.getSimpleName().length());
}
@Override
public String toString() {
return String.format("(%s)%s", //$NON-NLS-1$
getVariableType(),
super.toString());
}
}