Commit | Line | Data |
---|---|---|
3a5f73a1 JCK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 Ecole Polytechnique de Montreal, Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | ******************************************************************************/ | |
9 | package org.eclipse.tracecompass.tmf.analysis.xml.core.model; | |
10 | ||
11 | import java.util.List; | |
12 | ||
13 | import org.eclipse.jdt.annotation.NonNull; | |
14 | import org.eclipse.jdt.annotation.Nullable; | |
15 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.pattern.stateprovider.XmlPatternStateProvider; | |
16 | import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlScenarioHistoryBuilder.ScenarioStatusType; | |
17 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer; | |
18 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
19 | ||
20 | /** | |
21 | * This Class implements a Scenario in the XML-defined state system | |
22 | * | |
23 | * @since 2.0 | |
24 | */ | |
25 | public class TmfXmlScenario { | |
26 | ||
27 | private final IXmlStateSystemContainer fContainer; | |
28 | private final TmfXmlFsm fFsm; | |
29 | private TmfXmlPatternEventHandler fPatternHandler; | |
30 | private TmfXmlScenarioInfo fScenarioInfo; | |
31 | TmfXmlScenarioHistoryBuilder fHistoryBuilder; | |
32 | ||
33 | /** | |
34 | * Constructor | |
35 | * | |
36 | * @param event | |
37 | * The event at which this scenario is created | |
38 | * @param patternHandler | |
39 | * The filter handler | |
40 | * @param fsmId | |
41 | * the id of the fsm executed by this scenario | |
42 | * @param container | |
43 | * The state system container this scenario belongs to | |
44 | * @param modelFactory | |
45 | * The model factory | |
46 | */ | |
47 | public TmfXmlScenario(@Nullable ITmfEvent event, TmfXmlPatternEventHandler patternHandler, String fsmId, IXmlStateSystemContainer container, ITmfXmlModelFactory modelFactory) { | |
48 | TmfXmlFsm fsm = patternHandler.getFsm(fsmId); | |
49 | if (fsm == null) { | |
50 | throw new IllegalArgumentException(fsmId + "has not been declared."); //$NON-NLS-1$ | |
51 | } | |
52 | fFsm = fsm; | |
53 | fContainer = container; | |
54 | fHistoryBuilder = ((XmlPatternStateProvider) container).getHistoryBuilder(); | |
55 | fPatternHandler = patternHandler; | |
56 | int quark = fHistoryBuilder.assignScenarioQuark(fContainer, fsmId); | |
57 | int statusQuark = fHistoryBuilder.getScenarioStatusQuark(fContainer, quark); | |
58 | fScenarioInfo = new TmfXmlScenarioInfo(fFsm.getInitialStateId(), ScenarioStatusType.PENDING, quark, statusQuark, fFsm); | |
59 | fHistoryBuilder.update(fContainer, fScenarioInfo, event); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Get this scenario infos | |
64 | * | |
65 | * @return The scenario info | |
66 | */ | |
67 | public TmfXmlScenarioInfo getScenarioInfos() { | |
68 | return fScenarioInfo; | |
69 | } | |
70 | ||
71 | /** | |
72 | * Cancel the execution of this scenario | |
73 | */ | |
74 | public void cancel() { | |
75 | fScenarioInfo.setStatus(ScenarioStatusType.ABANDONED); | |
76 | if (fScenarioInfo.getStatus() != ScenarioStatusType.PENDING) { | |
77 | fHistoryBuilder.completeScenario(fContainer, fScenarioInfo, null); | |
78 | } | |
79 | } | |
80 | ||
81 | /** | |
82 | * Test if the scenario is active or not | |
83 | * | |
84 | * @return True if the scenario is active, false otherwise | |
85 | */ | |
86 | public boolean isActive() { | |
87 | return fScenarioInfo.getStatus().equals(ScenarioStatusType.PENDING) || fScenarioInfo.getStatus().equals(ScenarioStatusType.IN_PROGRESS); | |
88 | } | |
89 | ||
90 | /** | |
91 | * Handle the ongoing event | |
92 | * | |
93 | * @param event | |
94 | * The ongoing event | |
95 | */ | |
96 | public void handleEvent(ITmfEvent event) { | |
97 | ||
98 | TmfXmlStateTransition out = fFsm.next(event, fPatternHandler.getTestMap(), fScenarioInfo); | |
99 | if (out == null) { | |
100 | return; | |
101 | } | |
102 | ||
103 | // Processing the actions in the transition | |
104 | final List<String> actions = out.getAction(); | |
105 | for (String actionId : actions) { | |
106 | ITmfXmlAction action = fPatternHandler.getActionMap().get(actionId); | |
107 | if (action != null) { | |
108 | action.execute(event, fScenarioInfo); | |
109 | } else { | |
110 | throw new IllegalStateException("Action " + actionId + " cannot be found."); //$NON-NLS-1$ //$NON-NLS-2$ | |
111 | } | |
112 | } | |
113 | ||
114 | // Change the activeState | |
115 | final @NonNull String nextState = out.getTarget(); | |
116 | if (fScenarioInfo.getStatus().equals(ScenarioStatusType.PENDING)) { | |
117 | fScenarioInfo.setStatus(ScenarioStatusType.IN_PROGRESS); | |
118 | fHistoryBuilder.startScenario(fContainer, fScenarioInfo, event); | |
119 | } else if (nextState.equals(fFsm.getAbandonStateId())) { | |
120 | fScenarioInfo.setStatus(ScenarioStatusType.ABANDONED); | |
121 | fHistoryBuilder.completeScenario(fContainer, fScenarioInfo, event); | |
122 | } else if (nextState.equals(fFsm.getFinalStateId())) { | |
123 | fScenarioInfo.setStatus(ScenarioStatusType.MATCHED); | |
124 | fHistoryBuilder.completeScenario(fContainer, fScenarioInfo, event); | |
125 | } | |
126 | fScenarioInfo.setActiveState(nextState); | |
127 | fHistoryBuilder.update(fContainer, fScenarioInfo, event); | |
128 | } | |
129 | ||
130 | } |