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
.ArrayList
;
12 import java
.util
.Collections
;
13 import java
.util
.HashMap
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import org
.eclipse
.osgi
.util
.NLS
;
21 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
22 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.Activator
;
23 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.TmfXmlScenarioHistoryBuilder
.ScenarioStatusType
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
27 import org
.w3c
.dom
.Element
;
28 import org
.w3c
.dom
.NodeList
;
30 import com
.google
.common
.collect
.ImmutableList
;
31 import com
.google
.common
.collect
.ImmutableMap
;
34 * This Class implements a state machine (FSM) tree in the XML-defined state
37 * @author Jean-Christian Kouame
39 public class TmfXmlFsm
{
41 private final Map
<String
, TmfXmlState
> fStatesMap
;
42 private final List
<TmfXmlScenario
> fActiveScenariosList
;
43 private final List
<TmfXmlBasicTransition
> fPreconditions
;
44 private final String fId
;
45 private final ITmfXmlModelFactory fModelFactory
;
46 private final IXmlStateSystemContainer fContainer
;
47 private final String fFinalStateId
;
48 private final String fAbandonStateId
;
49 private final boolean fInstanceMultipleEnabled
;
50 private final String fInitialStateId
;
51 private int fTotalScenarios
;
54 * Factory to create a {@link TmfXmlFsm}
57 * The factory used to create XML model elements
59 * The XML root of this fsm
61 * The state system container this fsm belongs to
62 * @return The new {@link TmfXmlFsm}
64 public static TmfXmlFsm
create(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
) {
65 String id
= node
.getAttribute(TmfXmlStrings
.ID
);
66 boolean instanceMultipleEnabled
= node
.getAttribute(TmfXmlStrings
.MULTIPLE
).isEmpty() ?
true : Boolean
.parseBoolean(node
.getAttribute(TmfXmlStrings
.MULTIPLE
));
67 final List
<@NonNull TmfXmlBasicTransition
> preconditions
= new ArrayList
<>();
69 // Get the preconditions
70 NodeList nodesPreconditions
= node
.getElementsByTagName(TmfXmlStrings
.PRECONDITION
);
71 for (int i
= 0; i
< nodesPreconditions
.getLength(); i
++) {
72 preconditions
.add(new TmfXmlBasicTransition(((Element
) NonNullUtils
.checkNotNull(nodesPreconditions
.item(i
)))));
75 // Get the initial state and the preconditions
76 String initialState
= node
.getAttribute(TmfXmlStrings
.INITIAL
);
77 if (initialState
.isEmpty()) {
78 NodeList nodesInitialState
= node
.getElementsByTagName(TmfXmlStrings
.INITIAL
);
79 if (nodesInitialState
.getLength() == 1) {
80 NodeList nodesTransition
= ((Element
) nodesInitialState
.item(0)).getElementsByTagName(TmfXmlStrings
.TRANSITION
);
81 if (nodesInitialState
.getLength() != 1) {
82 throw new IllegalArgumentException("initial state : there should be one and only one initial state."); //$NON-NLS-1$
84 initialState
= ((Element
) nodesTransition
.item(0)).getAttribute(TmfXmlStrings
.TARGET
);
88 Map
<@NonNull String
, @NonNull TmfXmlState
> statesMap
= new HashMap
<>();
90 NodeList nodesState
= node
.getElementsByTagName(TmfXmlStrings
.STATE
);
91 for (int i
= 0; i
< nodesState
.getLength(); i
++) {
92 Element element
= (Element
) NonNullUtils
.checkNotNull(nodesState
.item(i
));
93 TmfXmlState state
= modelFactory
.createState(element
, container
, null);
94 statesMap
.put(state
.getId(), state
);
96 // If the initial state was not already set, we use the first state
97 // declared in the fsm description as initial state
98 if (initialState
.isEmpty()) {
99 initialState
= state
.getId();
103 if (initialState
.isEmpty()) {
104 throw new IllegalStateException("No initial state has been declared in fsm " + id
); //$NON-NLS-1$
107 // Get the FSM final state
108 String finalStateId
= TmfXmlStrings
.NULL
;
109 NodeList nodesFinalState
= node
.getElementsByTagName(TmfXmlStrings
.FINAL
);
110 if (nodesFinalState
.getLength() == 1) {
111 final Element finalElement
= NonNullUtils
.checkNotNull((Element
) nodesFinalState
.item(0));
112 finalStateId
= finalElement
.getAttribute(TmfXmlStrings
.ID
);
113 if (!finalStateId
.isEmpty()) {
114 TmfXmlState finalState
= modelFactory
.createState(finalElement
, container
, null);
115 statesMap
.put(finalState
.getId(), finalState
);
119 // Get the FSM abandon state
120 String abandonStateId
= TmfXmlStrings
.NULL
;
121 NodeList nodesAbandonState
= node
.getElementsByTagName(TmfXmlStrings
.ABANDON_STATE
);
122 if (nodesAbandonState
.getLength() == 1) {
123 final Element abandonElement
= NonNullUtils
.checkNotNull((Element
) nodesAbandonState
.item(0));
124 abandonStateId
= abandonElement
.getAttribute(TmfXmlStrings
.ID
);
125 if (!abandonStateId
.isEmpty()) {
126 TmfXmlState abandonState
= modelFactory
.createState(abandonElement
, container
, null);
127 statesMap
.put(abandonState
.getId(), abandonState
);
130 return new TmfXmlFsm(modelFactory
, container
, id
, instanceMultipleEnabled
, initialState
, finalStateId
, abandonStateId
, preconditions
, statesMap
);
133 private TmfXmlFsm(ITmfXmlModelFactory modelFactory
, IXmlStateSystemContainer container
, String id
, boolean multiple
,
134 String initialState
, String finalState
, String abandonState
, List
<TmfXmlBasicTransition
> preconditions
,
135 Map
<String
, TmfXmlState
> states
) {
136 fModelFactory
= modelFactory
;
138 fContainer
= container
;
140 fInstanceMultipleEnabled
= multiple
;
141 fInitialStateId
= initialState
;
142 fFinalStateId
= finalState
;
143 fAbandonStateId
= abandonState
;
144 fPreconditions
= ImmutableList
.copyOf(preconditions
);
145 fStatesMap
= ImmutableMap
.copyOf(states
);
146 fActiveScenariosList
= new ArrayList
<>();
152 * @return the id of this fsm
154 public String
getId() {
159 * Get the initial state ID of this fsm
161 * @return the id of the initial state of this finite state machine
163 public String
getInitialStateId() {
164 return fInitialStateId
;
168 * Get the final state ID of this fsm
170 * @return the id of the final state of this finite state machine
172 public String
getFinalStateId() {
173 return fFinalStateId
;
177 * Get the abandon state ID fo this fsm
179 * @return the id of the abandon state of this finite state machine
181 public String
getAbandonStateId() {
182 return fAbandonStateId
;
186 * Get the states table of this fsm in map
188 * @return The map containing all state definition for this fsm
190 public Map
<String
, TmfXmlState
> getStatesMap() {
191 return Collections
.unmodifiableMap(fStatesMap
);
195 * Process the active event and determine the next step of this fsm
198 * The event to process
200 * The list of possible transitions of the state machine
201 * @param scenarioInfo
202 * The active scenario details.
203 * @return A pair containing the next state of the state machine and the
206 public @Nullable TmfXmlStateTransition
next(ITmfEvent event
, Map
<String
, TmfXmlTransitionValidator
> tests
, TmfXmlScenarioInfo scenarioInfo
) {
207 boolean matched
= false;
208 TmfXmlStateTransition stateTransition
= null;
209 TmfXmlState state
= fStatesMap
.get(scenarioInfo
.getActiveState());
211 /** FIXME: This logging should be replaced by something the user will see, this is XML debugging information! */
212 Activator
.logError(NLS
.bind(Messages
.TmfXmlFsm_StateUndefined
, scenarioInfo
.getActiveState(), getId()));
215 for (int i
= 0; i
< state
.getTransitionList().size() && !matched
; i
++) {
216 stateTransition
= state
.getTransitionList().get(i
);
217 matched
= stateTransition
.test(event
, scenarioInfo
, tests
);
219 return matched ? stateTransition
: null;
225 * Validate the preconditions of this fsm. If not validate, the fsm will
226 * skip the active event.
231 * The transition inputs
232 * @return True if one of the precondition is validated, false otherwise
234 private boolean validatePreconditions(ITmfEvent event
, Map
<String
, TmfXmlTransitionValidator
> tests
) {
235 if (fPreconditions
.isEmpty()) {
238 for (TmfXmlBasicTransition precondition
: fPreconditions
) {
239 if (precondition
.test(event
, null, tests
)) {
247 * Handle the current event
251 * @param transitionMap
252 * The transitions of the pattern
254 public void handleEvent(ITmfEvent event
, Map
<String
, TmfXmlTransitionValidator
> transitionMap
) {
255 if (!validatePreconditions(event
, transitionMap
)) {
258 for (Iterator
<TmfXmlScenario
> currentItr
= fActiveScenariosList
.iterator(); currentItr
.hasNext();) {
259 TmfXmlScenario scenario
= currentItr
.next();
260 // Remove inactive scenarios or handle the active ones.
261 if (!scenario
.isActive()) {
264 handleScenario(scenario
, event
);
270 * Abandon all ongoing scenarios
272 public void dispose() {
273 for (TmfXmlScenario scenario
: fActiveScenariosList
) {
274 if (scenario
.isActive()) {
280 private static void handleScenario(TmfXmlScenario scenario
, ITmfEvent event
) {
281 if (scenario
.isActive()) {
282 scenario
.handleEvent(event
);
287 * Create a new scenario of this fsm
290 * The current event, null if not
291 * @param eventHandler
292 * The event handler this fsm belongs
294 * True to force the creation of the scenario, false otherwise
296 public synchronized void createScenario(@Nullable ITmfEvent event
, TmfXmlPatternEventHandler eventHandler
, boolean force
) {
297 if (force
|| isNewScenarioAllowed()) {
298 TmfXmlScenario scenario
= new TmfXmlScenario(event
, eventHandler
, fId
, fContainer
, fModelFactory
);
300 fActiveScenariosList
.add(scenario
);
305 * Check if we have the right to create a new scenario. A new scenario could
306 * be created if it is not the first scenario of an FSM and the FSM is not a
307 * singleton and the status of the last created scenario is not PENDING.
309 * @return True if the start of a new scenario is allowed, false otherwise
311 public synchronized boolean isNewScenarioAllowed() {
312 return fTotalScenarios
> 0
313 && !fActiveScenariosList
.get(fActiveScenariosList
.size() - 1).getScenarioInfos().getStatus().equals(ScenarioStatusType
.PENDING
)
314 && fInstanceMultipleEnabled
;