70d1b3675104c69b064f0af64481e62c51adfbc3
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / core / model / TmfXmlBasicTransition.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ecole Polytechnique de Montreal, Ericsson
3 *
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;
10
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.regex.Pattern;
16
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;
22
23 /**
24 * Implementation of a basic transition in the XML file
25 *
26 * @author Jean-Christian Kouame
27 */
28 public class TmfXmlBasicTransition {
29
30 private static final Pattern WILDCARD_PATTERN = Pattern.compile("\\*"); //$NON-NLS-1$
31
32 private final List<String> fCond;
33 private final List<Pattern> fAcceptedEvents;
34
35
36 /**
37 * Constructor
38 *
39 * @param element
40 * the XML basic transition element
41 */
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));
48 }
49 final @NonNull String conditions = element.getAttribute(TmfXmlStrings.COND);
50 fCond = conditions.isEmpty() ? new ArrayList<>() : Arrays.asList(conditions.split(TmfXmlStrings.AND_SEPARATOR));
51 }
52
53 /**
54 * Validate the transition with the current event
55 *
56 * @param event
57 * The active event
58 * @param scenarioInfo
59 * The active scenario details.
60 * @param tests
61 * The map of test in the XML file
62 * @return true if the transition is validate false if not
63 */
64 public boolean test(ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo, Map<String, TmfXmlTransitionValidator> tests) {
65 if (!validateEvent(event)) {
66 return false;
67 }
68
69 for (String cond : fCond) {
70 TmfXmlTransitionValidator test = tests.get(cond);
71 if (test == null) {
72 throw new IllegalStateException("Failed to find cond " + cond); //$NON-NLS-1$
73 }
74 if (!test.test(event, scenarioInfo)) {
75 return false;
76 }
77 }
78 return true;
79 }
80
81 private boolean validateEvent(ITmfEvent event) {
82 String eventName = event.getName();
83
84 /*
85 * This validates the event name with the accepted regular expressions
86 */
87 for (Pattern nameRegex : fAcceptedEvents) {
88 if (nameRegex.matcher(eventName).matches()) {
89 return true;
90 }
91 }
92 return false;
93 }
94 }
This page took 0.052326 seconds and 4 git commands to generate.