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
.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
.internal
.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
28 public class TmfXmlBasicTransition
{
30 private static final Pattern WILDCARD_PATTERN
= Pattern
.compile("\\*"); //$NON-NLS-1$
32 private final List
<String
> fCond
;
33 private final List
<Pattern
> fAcceptedEvents
;
40 * the XML basic transition element
42 public TmfXmlBasicTransition(Element element
) {
43 final @NonNull String events
= element
.getAttribute(TmfXmlStrings
.EVENT
);
44 fAcceptedEvents
= new ArrayList
<>();
45 for (String eventName
: Arrays
.asList(events
.split(TmfXmlStrings
.OR_SEPARATOR
))) {
46 String name
= WILDCARD_PATTERN
.matcher(eventName
).replaceAll(".*"); //$NON-NLS-1$
47 fAcceptedEvents
.add(Pattern
.compile(name
));
49 final @NonNull String conditions
= element
.getAttribute(TmfXmlStrings
.COND
);
50 fCond
= conditions
.isEmpty() ?
new ArrayList
<>() : Arrays
.asList(conditions
.split(TmfXmlStrings
.AND_SEPARATOR
));
54 * Validate the transition with the current event
59 * The active scenario details.
61 * The map of test in the XML file
62 * @return true if the transition is validate false if not
64 public boolean test(ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
, Map
<String
, TmfXmlTransitionValidator
> tests
) {
65 if (!validateEvent(event
)) {
69 for (String cond
: fCond
) {
70 TmfXmlTransitionValidator test
= tests
.get(cond
);
72 throw new IllegalStateException("Failed to find cond " + cond
); //$NON-NLS-1$
74 if (!test
.test(event
, scenarioInfo
)) {
81 private boolean validateEvent(ITmfEvent event
) {
82 String eventName
= event
.getName();
85 * This validates the event name with the accepted regular expressions
87 for (Pattern nameRegex
: fAcceptedEvents
) {
88 if (nameRegex
.matcher(eventName
).matches()) {