Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0fb1b823b079631cd4f6fd4b33a291e3a87c0436 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*******************************************************************************
 * 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.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.EList;
import org.eclipse.etrice.abstractexec.behavior.util.AbstractExecutionUtil;
import org.eclipse.etrice.core.genmodel.etricegen.ActiveTrigger;
import org.eclipse.etrice.core.genmodel.etricegen.ExpandedActorClass;
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.Transition;

public class SemanticsCheck {
	private Queue<StateGraphNode> queue;
	private Set<StateGraphNode> visited;
	private ExpandedActorClass xpAct;
	private HashMap<StateGraphItem, ActiveRules> mapToRules = new HashMap<StateGraphItem, ActiveRules>();
	private ActionCodeAnalyzer codeAnalyzer;
	private HashMap<StateGraphItem, List<HandledMessage>> mapToWarnings = new HashMap<StateGraphItem, List<HandledMessage>>();
	private static boolean traceChecks = false;
	private static int traceLevel = 0;
	static {
		if (Activator.getDefault().isDebugging()) {
			String value = Platform.getDebugOption("org.eclipse.etrice.abstractexec.behavior/trace/checks");
			if (value != null && value.equalsIgnoreCase(Boolean.toString(true))) {
				traceChecks = true;
			}
			value = Platform.getDebugOption("org.eclipse.etrice.abstractexec.behavior/trace/checks/level");
			if (value != null) {
				traceLevel = Integer.parseInt(value);
			}
		}
	}

	private static final int TRACE_RESULT = 1;
	private static final int TRACE_DETAILS = 2;
	
	public SemanticsCheck(ExpandedActorClass xpac) {
		queue = new LinkedList<StateGraphNode>();
		xpAct = xpac;
		visited = new HashSet<StateGraphNode>();
		codeAnalyzer = new ActionCodeAnalyzer(xpac.getActorClass());
	}

	public void checkSemantics() {
		if (traceChecks)
			System.out.println("checkSemantics: check of ActorClass "+xpAct.getActorClass().getName());
		
		StateGraph graph = xpAct.getStateMachine();
		ActiveRules localRules = new ActiveRules();
		localRules.buildInitLocalRules(xpAct);
		addStartingPoints(graph, localRules);
		doTraversal();
		
		if (traceChecks) {
			if (traceLevel>=TRACE_RESULT)
				printRules();
			
			System.out.println("checkSemantics: done with check of ActorClass "+xpAct.getActorClass().getName());
		}
	}

	private void addStartingPoints(StateGraph graph, ActiveRules localRules) {
		EList<Transition> transitions = graph.getTransitions();
		for (Transition trans : transitions)
			if (trans instanceof InitialTransition) {
				StateGraphNode cur = AbstractExecutionUtil.getInstance().getRoomHelpers().getNode(trans.getTo());
				List<HandledMessage> msgList = codeAnalyzer.analyze(trans.getAction());
				if (cur instanceof State) {
					msgList.addAll(codeAnalyzer.analyze(((State) cur).getEntryCode()));
				}
				List<HandledMessage> wrongMsgList = localRules.consumeMessages(msgList);
				addToWarning(trans, wrongMsgList);
				boolean rulesChanged = false;
				if (mapToRules.containsKey(cur)) {
					rulesChanged = mapToRules.get(cur).merge(localRules);
				} else {
					mapToRules.put(cur, localRules);
					rulesChanged = true;
				}
				if (!visited.contains(cur) || rulesChanged)
					queue.add(cur);

				break;
			}
	}

	private void doTraversal() {
		while (!queue.isEmpty()) {
			Visit(queue.poll());
		}
	}

	private void Visit(StateGraphNode node) {
		visited.add(node);
		if (node instanceof State) {
			State st = (State) node;
			if (AbstractExecutionUtil.getInstance().getRoomHelpers().hasDirectSubStructure(st)) {
				addStartingPoints(st.getSubgraph(), mapToRules.get(st));
			}
			else {
				for (ActiveTrigger trigger : xpAct.getActiveTriggers(st)) {
					if (traceChecks && traceLevel>=TRACE_DETAILS) {
						System.out.println("  Currently visiting: " + st.getName());
						System.out.println("  Trigger: " + trigger.getMsg().getName());
					}
					
					for (Transition trans : trigger.getTransitions()) {
						StateGraphNode target = AbstractExecutionUtil.getInstance().getRoomHelpers().getNode(trans.getTo());
						List<HandledMessage> msgList = new LinkedList<HandledMessage>();
						// create a list of codes here in the order
						// trigger, exit, action, entry
						msgList.add(new HandledMessage(trigger.getIfitem(), trigger.getMsg(), trigger));
						StateGraph triggerContext = (StateGraph) trans.eContainer();
						State exitCalled = st;
						while (true) {
							// this is where all the exit code is added
							msgList.addAll(codeAnalyzer.analyze(exitCalled.getExitCode()));
							if (exitCalled.eContainer() == triggerContext)
								break;
							exitCalled = (State) exitCalled.eContainer().eContainer();
						}
						msgList.addAll(codeAnalyzer.analyze(trans.getAction()));
						if (target instanceof State) {
							msgList.addAll(codeAnalyzer.analyze(((State) target).getEntryCode()));
						}
						ActiveRules tempRule = mapToRules.get(node).createCopy();
						
						if (traceChecks && traceLevel>=TRACE_DETAILS) {
							System.out.println("  Messages in msglist before consuming: ");
							for (HandledMessage msg : msgList) {
								System.out.println("  Msg: "+ msg.getMsg().getName());
							}
						}
						if (traceChecks && traceLevel>=TRACE_DETAILS) {
							System.out.println("  rules before consuming message list : ");
							printRules();
						}
						List<HandledMessage> wrongMsgList = tempRule.consumeMessages(msgList);
						addToWarning(node, wrongMsgList);
															
						if (traceChecks && traceLevel>=TRACE_DETAILS)
							System.out.println("  Messages consumed");
						
						addAndMergeRules(target, tempRule);
						
						if (traceChecks && traceLevel>=TRACE_DETAILS) {
							System.out.println("  rules after consuming and merging message list : ");
							printRules();
						}

					}
				}
			}
		} else {
			/*
			 * If the current node is an Entry/Exit/Transition/Choice pt , then
			 * only the action code in the outgoing transition needs to be
			 * considered For this, a copy of the ActiveRules of the current
			 * node is created and is checked against each outgoing transition
			 * for Rule changes If there is any rule change or if the
			 * destination state hasn't been visited then the destination rules
			 * are merged with the current rules and destination node is added
			 * to the current queue.
			 */
			for (Transition trans : xpAct.getOutgoingTransitions(node)) {
				ActiveRules tempRule = mapToRules.get(node).createCopy();
				List<HandledMessage> msgList = codeAnalyzer.analyze(trans.getAction());
				StateGraphNode target = AbstractExecutionUtil.getInstance().getRoomHelpers().getNode(trans.getTo());
				if (target instanceof State) {
					msgList.addAll(codeAnalyzer.analyze(((State) target).getEntryCode()));
				}
				List<HandledMessage> wrongMsgList = tempRule.consumeMessages(msgList);
				addToWarning(node, wrongMsgList);
				addAndMergeRules(target, tempRule);
			}
		}
	}

	private void addToWarning(StateGraphItem item,
			List<HandledMessage> wrongMsgList) {
		if (mapToWarnings.containsKey(item)) {
			mapToWarnings.get(item).addAll(wrongMsgList);
		} else {
			mapToWarnings.put(item, wrongMsgList);
		}
	}
	private void addAndMergeRules(StateGraphNode target, ActiveRules tempRule) {
		boolean rulesChanged = false;
		if (mapToRules.containsKey(target)) {
			rulesChanged = mapToRules.get(target).merge(tempRule);
		} else {
			mapToRules.put(target, tempRule);
			rulesChanged = true;
		}
		if (!visited.contains(target) || rulesChanged) {
			queue.add(target);
		}

	}

	public void printRules() {
		System.out.println("  Current Rules: ");
		System.out.println("    MapToRules size: " + this.mapToRules.size());
		for (StateGraphItem item : this.mapToRules.keySet()) {
			System.out.println("    Rules for " + item.getName() + " : ");
			mapToRules.get(item).print();
		}
	}

	public ActiveRules getActiveRules(StateGraphItem item) {
		return mapToRules.get(item);
	}
	public List<HandledMessage> getWarningMsg (StateGraphItem item)
	{
		return mapToWarnings.get(item);
	}
}

Back to the top