1 /*******************************************************************************
2 * Copyright (c) 2016 Ecole Polytechnique de Montreal, Ericsson
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
;
11 import java
.util
.List
;
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
;
22 * This Class implements a Scenario in the XML-defined state system
24 public class TmfXmlScenario
{
26 private final IXmlStateSystemContainer fContainer
;
27 private final TmfXmlFsm fFsm
;
28 private TmfXmlPatternEventHandler fPatternHandler
;
29 private TmfXmlScenarioInfo fScenarioInfo
;
30 TmfXmlScenarioHistoryBuilder fHistoryBuilder
;
36 * The event at which this scenario is created
37 * @param patternHandler
40 * the id of the fsm executed by this scenario
42 * The state system container this scenario belongs to
46 public TmfXmlScenario(@Nullable ITmfEvent event
, TmfXmlPatternEventHandler patternHandler
, String fsmId
, IXmlStateSystemContainer container
, ITmfXmlModelFactory modelFactory
) {
47 TmfXmlFsm fsm
= patternHandler
.getFsm(fsmId
);
49 throw new IllegalArgumentException(fsmId
+ "has not been declared."); //$NON-NLS-1$
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
);
62 * Get this scenario infos
64 * @return The scenario info
66 public TmfXmlScenarioInfo
getScenarioInfos() {
71 * Cancel the execution of this scenario
73 public void cancel() {
74 fScenarioInfo
.setStatus(ScenarioStatusType
.ABANDONED
);
75 if (fScenarioInfo
.getStatus() != ScenarioStatusType
.PENDING
) {
76 fHistoryBuilder
.completeScenario(fContainer
, fScenarioInfo
, null);
81 * Test if the scenario is active or not
83 * @return True if the scenario is active, false otherwise
85 public boolean isActive() {
86 return fScenarioInfo
.getStatus() == ScenarioStatusType
.IN_PROGRESS
;
90 * Test if the scenario is pending or not
92 * @return True if the scenario is pending, false otherwise
94 public boolean isPending() {
95 return fScenarioInfo
.getStatus() == ScenarioStatusType
.PENDING
;
99 * Handle the ongoing event
104 public void handleEvent(ITmfEvent event
) {
106 TmfXmlStateTransition out
= fFsm
.next(event
, fPatternHandler
.getTestMap(), fScenarioInfo
);
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
);
119 Activator
.logError("Action " + actionId
+ " cannot be found."); //$NON-NLS-1$ //$NON-NLS-2$
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
);
136 fScenarioInfo
.setActiveState(nextState
);
137 fHistoryBuilder
.update(fContainer
, fScenarioInfo
, event
);