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
.Arrays
;
13 import java
.util
.List
;
15 import java
.util
.regex
.Pattern
;
17 import org
.eclipse
.jdt
.annotation
.NonNull
;
18 import org
.eclipse
.jdt
.annotation
.Nullable
;
19 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
20 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
21 import org
.w3c
.dom
.Element
;
24 * Implementation of a basic transition in the XML file
26 * @author Jean-Christian Kouame
29 public class TmfXmlBasicTransition
{
31 private static final Pattern WILDCARD_PATTERN
= Pattern
.compile("\\*"); //$NON-NLS-1$
33 private final List
<String
> fCond
;
34 private final List
<Pattern
> fAcceptedEvents
;
41 * the XML basic transition element
43 public TmfXmlBasicTransition(Element element
) {
44 final @NonNull String events
= element
.getAttribute(TmfXmlStrings
.EVENT
);
45 fAcceptedEvents
= new ArrayList
<>();
46 for (String eventName
: Arrays
.asList(events
.split(TmfXmlStrings
.OR_SEPARATOR
))) {
47 String name
= WILDCARD_PATTERN
.matcher(eventName
).replaceAll(".*"); //$NON-NLS-1$
48 fAcceptedEvents
.add(Pattern
.compile(name
));
50 final @NonNull String conditions
= element
.getAttribute(TmfXmlStrings
.COND
);
51 fCond
= conditions
.isEmpty() ?
new ArrayList
<>() : Arrays
.asList(conditions
.split(TmfXmlStrings
.AND_SEPARATOR
));
55 * Validate the transition with the current event
60 * The active scenario details.
62 * The map of test in the XML file
63 * @return true if the transition is validate false if not
65 public boolean test(ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
, Map
<String
, TmfXmlTransitionValidator
> tests
) {
66 if (!validateEvent(event
)) {
70 for (String cond
: fCond
) {
71 TmfXmlTransitionValidator test
= tests
.get(cond
);
73 throw new IllegalStateException("Failed to find cond " + cond
); //$NON-NLS-1$
75 if (!test
.test(event
, scenarioInfo
)) {
82 private boolean validateEvent(ITmfEvent event
) {
83 String eventName
= event
.getName();
86 * This validates the event name with the accepted regular expressions
88 for (Pattern nameRegex
: fAcceptedEvents
) {
89 if (nameRegex
.matcher(eventName
).matches()) {