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
.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
.module
.IXmlStateSystemContainer
;
19 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
20 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.pattern
.stateprovider
.XmlPatternStateProvider
;
21 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
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
35 public class TmfXmlAction
implements ITmfXmlAction
{
37 private final IXmlStateSystemContainer fParent
;
38 private final String fId
;
39 private final List
<ITmfXmlAction
> fActionList
= new ArrayList
<>();
45 * The factory used to create XML model elements
47 * The XML root of this action
49 * The state system container this action belongs to
51 public TmfXmlAction(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
) {
53 fId
= NonNullUtils
.checkNotNull(node
.getAttribute(TmfXmlStrings
.ID
));
54 List
<@Nullable Element
> childElements
= XmlUtils
.getChildElements(node
);
55 for (Element child
: childElements
) {
56 final @NonNull Element nonNullChild
= NonNullUtils
.checkNotNull(child
);
57 switch (nonNullChild
.getNodeName()) {
58 case TmfXmlStrings
.STATE_CHANGE
:
59 fActionList
.add(new StateChange(modelFactory
, nonNullChild
, fParent
));
61 case TmfXmlStrings
.FSM_SCHEDULE_ACTION
:
62 fActionList
.add(new ScheduleNewScenario(modelFactory
, nonNullChild
, fParent
));
64 case TmfXmlStrings
.SEGMENT
:
65 fActionList
.add(new GeneratePatternSegment(modelFactory
, nonNullChild
, fParent
));
67 case TmfXmlStrings
.ACTION
:
68 fActionList
.add(new TmfXmlAction(modelFactory
, nonNullChild
, fParent
));
71 Activator
.logError("Invalid action type : " + nonNullChild
.getNodeName()); //$NON-NLS-1$
78 * Get the ID of this action
80 * @return The id of this action
82 public String
getId() {
87 public void execute(@NonNull ITmfEvent event
, TmfXmlScenarioInfo scenarioInfo
) {
88 // the order of the actions is important, do not parallelize.
89 for (ITmfXmlAction action
: fActionList
) {
90 action
.execute(event
, scenarioInfo
);
95 * Private class for an action that will create a state change in the state
98 private class StateChange
implements ITmfXmlAction
{
100 private final TmfXmlStateChange fStateChange
;
102 public StateChange(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer parent
) {
103 fStateChange
= modelFactory
.createStateChange(node
, parent
);
107 public void execute(@NonNull ITmfEvent event
, TmfXmlScenarioInfo scenarioInfo
) {
109 fStateChange
.handleEvent(event
, scenarioInfo
);
110 } catch (StateValueTypeException
| AttributeNotFoundException e
) {
111 Activator
.logError("Exception when executing action state change", e
); //$NON-NLS-1$
117 * Private class for an action that will instantiate and schedule a new instance of
120 private static class ScheduleNewScenario
implements ITmfXmlAction
{
125 * @param modelFactory
126 * The factory used to create XML model elements
128 * The XML root of this action
130 * The state system container this action belongs to
132 public ScheduleNewScenario(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
) {
136 public void execute(ITmfEvent event
, TmfXmlScenarioInfo scenarioInfo
) {
137 // TODO This action needs to be implemented
138 throw new UnsupportedOperationException("Schedule an FSM is not yet supported"); //$NON-NLS-1$
143 * Private class for an action that will generate pattern segment
145 private static class GeneratePatternSegment
implements ITmfXmlAction
{
147 private final TmfXmlPatternSegmentBuilder fSegmentBuilder
;
148 private final XmlPatternStateProvider fProvider
;
150 public GeneratePatternSegment(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer parent
) {
151 fProvider
= ((XmlPatternStateProvider
) parent
);
152 fSegmentBuilder
= modelFactory
.createPatternSegmentBuilder(node
, parent
);
156 public void execute(ITmfEvent event
, TmfXmlScenarioInfo scenarioInfo
) {
157 long ts
= fProvider
.getHistoryBuilder().getStartTime(fProvider
, scenarioInfo
, event
);
158 // FIXME Should the scale always be nanoseconds?
159 ITmfTimestamp start
= TmfTimestamp
.fromNanos(ts
);
160 ITmfTimestamp end
= event
.getTimestamp();
161 fSegmentBuilder
.generatePatternSegment(event
, start
, end
, scenarioInfo
);