Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 43c1c91fd369d4034db8a95e46e5739aad02af88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
 * Copyright (c) 2012 Rohit Agrawal
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Rohit Agrawal (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.abstractexec.behavior;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.etrice.core.genmodel.etricegen.ActiveTrigger;
import org.eclipse.etrice.core.genmodel.etricegen.ExpandedActorClass;
import org.eclipse.etrice.core.room.EntryPoint;
import org.eclipse.etrice.core.room.InitialTransition;
import org.eclipse.etrice.core.room.State;
import org.eclipse.etrice.core.room.StateGraph;
import org.eclipse.etrice.core.room.StateGraphItem;
import org.eclipse.etrice.core.room.StateGraphNode;
import org.eclipse.etrice.core.room.TrPoint;
import org.eclipse.etrice.core.room.Transition;
import org.eclipse.etrice.core.room.TransitionPoint;
import org.eclipse.etrice.core.room.util.RoomHelpers;

public class ReachabilityCheck {
	Queue<StateGraphNode> queue;
	public Set<StateGraphItem> visited;
	private ExpandedActorClass xpAct;
	private Set<StateGraphItem> exitUsed;

	public ReachabilityCheck(ExpandedActorClass xpac) {
		queue = new LinkedList<StateGraphNode>();
		xpAct = xpac;
		visited = new HashSet<StateGraphItem>();
		exitUsed = new HashSet<StateGraphItem>();
	}

	public void computeReachability() {
		StateGraph graph = xpAct.getStateMachine();
		addStartingPoints(graph, true);
		doTraversal();
	}

	private void addStartingPoints(StateGraph graph, boolean add_initial) {

		EList<Transition> transitions = graph.getTransitions();
		EList<TrPoint> trPoint = graph.getTrPoints();
		if (add_initial)
			for (Transition trans : transitions)
				if (trans instanceof InitialTransition) {
					visited.add(trans);
					StateGraphNode cur = RoomHelpers.getNode(trans.getTo());
					if (!visited.contains(cur))
						queue.add(cur);
					break;
				}
		for (TrPoint tp : trPoint) {
			if (tp instanceof TransitionPoint && !visited.contains(tp)) {
				queue.add(tp);
			}
		}

		// add transition points
	}

	private void doTraversal() {
		while (!queue.isEmpty()) {
			StateGraphNode node = queue.poll();
			if (!visited.contains(node))
				Visit(node);
			// System.out.println("Visited node : " + node.getName());
		}
	}

	public boolean isExitUsed(StateGraphItem item) {
		return exitUsed.contains(item);
	}

	public boolean isReachable(StateGraphItem item) {

		return visited.contains(item);
	}

	private void Visit(StateGraphNode node) {
		visited.add(node);
		if (node instanceof State) {
			State st = (State) node;
			if (RoomHelpers.hasDirectSubStructure(st)) {
				addStartingPoints(st.getSubgraph(), true);
			} else {
				for (ActiveTrigger trigger : xpAct.getActiveTriggers(st)) {
					for (Transition trans : trigger.getTransitions()) {
						visited.add(trans);
						StateGraphNode target = RoomHelpers.getNode(trans.getTo());
						if (!visited.contains(target)) {
							queue.add(target);
							StateGraph triggerContext = (StateGraph) trans
									.eContainer();
							State exitCalled = st;
							while (true) {
								exitUsed.add(exitCalled);
								if (exitCalled.eContainer() == triggerContext)
									break;
								exitCalled = (State) exitCalled.eContainer()
										.eContainer();
							}
						}
					}
				}
			}
		} else {
			if (node instanceof EntryPoint) {
				// if container has no initial transition then mark it visited
				boolean markVisited = true;
				State container = (State) node.eContainer().eContainer();
				StateGraph parentGraph = container.getSubgraph();
				for (Transition tr : parentGraph.getTransitions())
					if (tr instanceof InitialTransition)
						markVisited = false;
				if (markVisited)
					visited.add(container);
			}
			for (Transition trans : xpAct.getOutgoingTransitions(node)) {
				visited.add(trans);
				StateGraphNode target = RoomHelpers.getNode(trans.getTo());
				if (!visited.contains(target)) {
					queue.add(target);

				}
			}
		}

	}

}

Back to the top