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().equals(ScenarioStatusType
.PENDING
) || fScenarioInfo
.getStatus().equals(ScenarioStatusType
.IN_PROGRESS
);
90 * Handle the ongoing event
95 public void handleEvent(ITmfEvent event
) {
97 TmfXmlStateTransition out
= fFsm
.next(event
, fPatternHandler
.getTestMap(), fScenarioInfo
);
102 fFsm
.setEventConsumed(true);
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
);
110 Activator
.logError("Action " + actionId
+ " cannot be found."); //$NON-NLS-1$ //$NON-NLS-2$
115 // Change the activeState
116 final @NonNull String nextState
= out
.getTarget();
117 if (fScenarioInfo
.getStatus().equals(ScenarioStatusType
.PENDING
)) {
118 fScenarioInfo
.setStatus(ScenarioStatusType
.IN_PROGRESS
);
119 fHistoryBuilder
.startScenario(fContainer
, fScenarioInfo
, event
);
120 } else if (nextState
.equals(fFsm
.getAbandonStateId())) {
121 fScenarioInfo
.setStatus(ScenarioStatusType
.ABANDONED
);
122 fHistoryBuilder
.completeScenario(fContainer
, fScenarioInfo
, event
);
123 } else if (nextState
.equals(fFsm
.getFinalStateId())) {
124 fScenarioInfo
.setStatus(ScenarioStatusType
.MATCHED
);
125 fHistoryBuilder
.completeScenario(fContainer
, fScenarioInfo
, event
);
127 fScenarioInfo
.setActiveState(nextState
);
128 fHistoryBuilder
.update(fContainer
, fScenarioInfo
, event
);