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
.tmf
.analysis
.xml
.core
.model
;
11 import java
.util
.ArrayList
;
12 import java
.util
.List
;
14 import org
.eclipse
.jdt
.annotation
.NonNull
;
15 import org
.eclipse
.jdt
.annotation
.Nullable
;
16 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
17 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.Activator
;
18 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.pattern
.stateprovider
.XmlPatternStateProvider
;
19 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
20 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
21 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
22 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
23 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
27 import org
.w3c
.dom
.Element
;
30 * This Class implements an action tree in the XML-defined state system.
31 * An action is a collection of {@link ITmfXmlAction} that are executed when necessary.
33 * @author Jean-Christian Kouame
36 public class TmfXmlAction
implements ITmfXmlAction
{
38 private final IXmlStateSystemContainer fParent
;
39 private final String fId
;
40 private final List
<ITmfXmlAction
> fActionList
= new ArrayList
<>();
46 * The factory used to create XML model elements
48 * The XML root of this action
50 * The state system container this action belongs to
52 public TmfXmlAction(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
) {
54 fId
= NonNullUtils
.checkNotNull(node
.getAttribute(TmfXmlStrings
.ID
));
55 List
<@Nullable Element
> childElements
= XmlUtils
.getChildElements(node
);
56 for (Element child
: childElements
) {
57 final @NonNull Element nonNullChild
= NonNullUtils
.checkNotNull(child
);
58 switch (nonNullChild
.getNodeName()) {
59 case TmfXmlStrings
.STATE_CHANGE
:
60 fActionList
.add(new StateChange(modelFactory
, nonNullChild
, fParent
));
62 case TmfXmlStrings
.FSM_SCHEDULE_ACTION
:
63 fActionList
.add(new ScheduleNewScenario(modelFactory
, nonNullChild
, fParent
));
65 case TmfXmlStrings
.SEGMENT
:
66 fActionList
.add(new GeneratePatternSegment(modelFactory
, nonNullChild
, fParent
));
68 case TmfXmlStrings
.ACTION
:
69 fActionList
.add(new TmfXmlAction(modelFactory
, nonNullChild
, fParent
));
72 Activator
.logError("Invalid action type : " + nonNullChild
.getNodeName()); //$NON-NLS-1$
79 * Get the ID of this action
81 * @return The id of this action
83 public String
getId() {
88 public void execute(@NonNull ITmfEvent event
, TmfXmlScenarioInfo scenarioInfo
) {
89 // the order of the actions is important, do not parallelize.
90 for (ITmfXmlAction action
: fActionList
) {
91 action
.execute(event
, scenarioInfo
);
96 * Private class for an action that will create a state change in the state
99 private class StateChange
implements ITmfXmlAction
{
101 private final TmfXmlStateChange fStateChange
;
103 public StateChange(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer parent
) {
104 fStateChange
= modelFactory
.createStateChange(node
, parent
);
108 public void execute(@NonNull ITmfEvent event
, TmfXmlScenarioInfo scenarioInfo
) {
110 fStateChange
.handleEvent(event
, scenarioInfo
);
111 } catch (StateValueTypeException
| AttributeNotFoundException e
) {
112 Activator
.logError("Exception when executing action state change", e
); //$NON-NLS-1$
118 * Private class for an action that will instantiate and schedule a new instance of
121 private static class ScheduleNewScenario
implements ITmfXmlAction
{
126 * @param modelFactory
127 * The factory used to create XML model elements
129 * The XML root of this action
131 * The state system container this action belongs to
133 public ScheduleNewScenario(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
) {
137 public void execute(ITmfEvent event
, TmfXmlScenarioInfo scenarioInfo
) {
138 // TODO This action needs to be implemented
139 throw new UnsupportedOperationException("Schedule an FSM is not yet supported"); //$NON-NLS-1$
144 * Private class for an action that will generate pattern segment
146 private static class GeneratePatternSegment
implements ITmfXmlAction
{
148 private final TmfXmlPatternSegmentBuilder fSegmentBuilder
;
149 private final XmlPatternStateProvider fProvider
;
151 public GeneratePatternSegment(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer parent
) {
152 fProvider
= ((XmlPatternStateProvider
) parent
);
153 fSegmentBuilder
= modelFactory
.createPatternSegmentBuilder(node
, parent
);
157 public void execute(ITmfEvent event
, TmfXmlScenarioInfo scenarioInfo
) {
158 long ts
= fProvider
.getHistoryBuilder().getStartTime(fProvider
, scenarioInfo
, event
);
159 // FIXME Should the scale always be nanoseconds?
160 ITmfTimestamp start
= TmfTimestamp
.fromNanos(ts
);
161 ITmfTimestamp end
= event
.getTimestamp();
162 fSegmentBuilder
.generatePatternSegment(event
, start
, end
, scenarioInfo
);