tmf.xml: Bug 500195. Modify initial state behavior
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / core / model / TmfXmlScenario.java
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.internal.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.Activator;
16 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.TmfXmlScenarioHistoryBuilder.ScenarioStatusType;
17 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
18 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.pattern.stateprovider.XmlPatternStateProvider;
19 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
20
21 /**
22 * This Class implements a Scenario in the XML-defined state system
23 */
24 public class TmfXmlScenario {
25
26 private final IXmlStateSystemContainer fContainer;
27 private final TmfXmlFsm fFsm;
28 private TmfXmlPatternEventHandler fPatternHandler;
29 private TmfXmlScenarioInfo fScenarioInfo;
30 TmfXmlScenarioHistoryBuilder fHistoryBuilder;
31
32 /**
33 * Constructor
34 *
35 * @param event
36 * The event at which this scenario is created
37 * @param patternHandler
38 * The filter handler
39 * @param fsmId
40 * the id of the fsm executed by this scenario
41 * @param container
42 * The state system container this scenario belongs to
43 * @param modelFactory
44 * The model factory
45 */
46 public TmfXmlScenario(@Nullable ITmfEvent event, TmfXmlPatternEventHandler patternHandler, String fsmId, IXmlStateSystemContainer container, ITmfXmlModelFactory modelFactory) {
47 TmfXmlFsm fsm = patternHandler.getFsm(fsmId);
48 if (fsm == null) {
49 throw new IllegalArgumentException(fsmId + "has not been declared."); //$NON-NLS-1$
50 }
51 fFsm = fsm;
52 fContainer = container;
53 fHistoryBuilder = ((XmlPatternStateProvider) container).getHistoryBuilder();
54 fPatternHandler = patternHandler;
55 int quark = fHistoryBuilder.assignScenarioQuark(fContainer, fsmId);
56 int statusQuark = fHistoryBuilder.getScenarioStatusQuark(fContainer, quark);
57 fScenarioInfo = new TmfXmlScenarioInfo(fFsm.getInitialStateId(), ScenarioStatusType.PENDING, quark, statusQuark, fFsm);
58 fHistoryBuilder.update(fContainer, fScenarioInfo, event);
59 }
60
61 /**
62 * Get this scenario infos
63 *
64 * @return The scenario info
65 */
66 public TmfXmlScenarioInfo getScenarioInfos() {
67 return fScenarioInfo;
68 }
69
70 /**
71 * Cancel the execution of this scenario
72 */
73 public void cancel() {
74 fScenarioInfo.setStatus(ScenarioStatusType.ABANDONED);
75 if (fScenarioInfo.getStatus() != ScenarioStatusType.PENDING) {
76 fHistoryBuilder.completeScenario(fContainer, fScenarioInfo, null);
77 }
78 }
79
80 /**
81 * Test if the scenario is active or not
82 *
83 * @return True if the scenario is active, false otherwise
84 */
85 public boolean isActive() {
86 return fScenarioInfo.getStatus() == ScenarioStatusType.IN_PROGRESS;
87 }
88
89 /**
90 * Test if the scenario is pending or not
91 *
92 * @return True if the scenario is pending, false otherwise
93 */
94 public boolean isPending() {
95 return fScenarioInfo.getStatus() == ScenarioStatusType.PENDING;
96 }
97
98 /**
99 * Handle the ongoing event
100 *
101 * @param event
102 * The ongoing event
103 */
104 public void handleEvent(ITmfEvent event) {
105
106 TmfXmlStateTransition out = fFsm.next(event, fPatternHandler.getTestMap(), fScenarioInfo);
107 if (out == null) {
108 return;
109 }
110
111 fFsm.setEventConsumed(true);
112 // Processing the actions in the transition
113 final List<String> actions = out.getAction();
114 for (String actionId : actions) {
115 ITmfXmlAction action = fPatternHandler.getActionMap().get(actionId);
116 if (action != null) {
117 action.execute(event, fScenarioInfo);
118 } else {
119 Activator.logError("Action " + actionId + " cannot be found."); //$NON-NLS-1$ //$NON-NLS-2$
120 return;
121 }
122 }
123
124 // Change the activeState
125 final @NonNull String nextState = out.getTarget();
126 if (fScenarioInfo.getStatus().equals(ScenarioStatusType.PENDING)) {
127 fScenarioInfo.setStatus(ScenarioStatusType.IN_PROGRESS);
128 fHistoryBuilder.startScenario(fContainer, fScenarioInfo, event);
129 } else if (nextState.equals(fFsm.getAbandonStateId())) {
130 fScenarioInfo.setStatus(ScenarioStatusType.ABANDONED);
131 fHistoryBuilder.completeScenario(fContainer, fScenarioInfo, event);
132 } else if (nextState.equals(fFsm.getFinalStateId())) {
133 fScenarioInfo.setStatus(ScenarioStatusType.MATCHED);
134 fHistoryBuilder.completeScenario(fContainer, fScenarioInfo, event);
135 }
136 fScenarioInfo.setActiveState(nextState);
137 fHistoryBuilder.update(fContainer, fScenarioInfo, event);
138 }
139
140 }
This page took 0.034901 seconds and 5 git commands to generate.