Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2b51bf36c3f167b41ebca89f8506a0b50fcdc297 (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
241
242
243
244
245
246
247
248
249
250
251
/*******************************************************************************
 * 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.List;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.genmodel.base.NullDiagnostician;
import org.eclipse.etrice.core.genmodel.base.NullLogger;
import org.eclipse.etrice.core.genmodel.builder.GeneratorModelBuilder;
import org.eclipse.etrice.core.genmodel.etricegen.ActiveTrigger;
import org.eclipse.etrice.core.genmodel.etricegen.ExpandedActorClass;
import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.DetailCode;
import org.eclipse.etrice.core.room.GeneralProtocolClass;
import org.eclipse.etrice.core.room.InterfaceItem;
import org.eclipse.etrice.core.room.MessageFromIf;
import org.eclipse.etrice.core.room.ProtocolClass;
import org.eclipse.etrice.core.room.State;
import org.eclipse.etrice.core.room.StateGraphItem;
import org.eclipse.etrice.core.room.Trigger;
import org.eclipse.etrice.core.room.TriggeredTransition;
import org.eclipse.etrice.core.room.util.RoomHelpers;
import org.eclipse.etrice.core.validation.IRoomValidator;
import org.eclipse.etrice.core.validation.ValidationUtil;
import org.eclipse.xtext.validation.ValidationMessageAcceptor;

/**
 * @author rentzhnr
 * 
 */
public class AbstractExecutionValidator implements IRoomValidator {

	// c&p in tests
	public static String DIAG_CODE_VIOLATION_TRIGGER = "etrice.violation_trigger";
	public static String DIAG_CODE_VIOLATION_MESSAGESEND = "etrice.violation_messagesend";
	public static String DIAG_CODE_MISSING_TRIGGER = "etrice.receive_message";
	public static String DIAG_CODE_MISSING_MESSAGESEND = "etrice.send_message";
	
	private static boolean traceExec = false;
	private static String traceName = "";
	static {
		if (Activator.getDefault().isDebugging()) {
			String value = Platform
					.getDebugOption("org.eclipse.etrice.abstractexec.behavior/trace/abstractexec");
			if (value != null && value.equalsIgnoreCase(Boolean.toString(true))) {
				traceExec = true;
			}
			traceName = Platform
					.getDebugOption("org.eclipse.etrice.abstractexec.behavior/trace/abstractexec/name");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.etrice.core.validation.IRoomValidator#validate(org.eclipse
	 * .emf.ecore.EObject,
	 * org.eclipse.xtext.validation.ValidationMessageAcceptor)
	 */
	@Override
	public void validate(EObject object,
			ValidationMessageAcceptor messageAcceptor) {

		if (!(object instanceof ActorClass))
			return;
		
		ActorClass ac = (ActorClass) object;
		
		if (traceExec) {
			if (!(traceName.isEmpty() || ac.getName().equals(traceName)))
				return;
			System.out.println("AbstractExecutionValidator checking class "
					+ ac.getName());
		}
		
		if (ValidationUtil.isCircularClassHierarchy(ac))
			// is checked elsewhere
			return;

		boolean oneProtocolsWithSemantics = false;
		List<InterfaceItem> ifItems = RoomHelpers.getAllInterfaceItems(ac);
		for (InterfaceItem item : ifItems) {
			GeneralProtocolClass pc = item.getGeneralProtocol();
			if (!(pc instanceof ProtocolClass))
				continue;

			if (traceExec)
				System.out.println("  Checking protocolClass " + pc.getName()
						+ " for semantics");
			if (((ProtocolClass) pc).getSemantics() != null) {
				oneProtocolsWithSemantics = true;
				if (traceExec)
					System.out
							.println("  Will execute because semantics defined for "
									+ pc.getName());
				break;
			}
		}
		if (oneProtocolsWithSemantics) {
			// begin abstract execution on state machine of expanded actor class
			System.out
					.println("  Reached where at least one interface items has semantics");
			NullDiagnostician diagnostician = new NullDiagnostician();
			GeneratorModelBuilder builder = new GeneratorModelBuilder(
					new NullLogger(), diagnostician);
			ExpandedActorClass xpac = builder.createExpandedActorClass(ac);

			if (xpac != null && !diagnostician.isFailed()) {
				SemanticsCheck checker = new SemanticsCheck(xpac);
				checker.checkSemantics();

				if (traceExec)
					System.out.println("  Rule checking for "
							+ xpac.getActorClass().getName() + " is over");

				TreeIterator<EObject> it = xpac.getStateMachine()
						.eAllContents();
				while (it.hasNext()) {
					EObject obj = it.next();
					if (obj instanceof State) {
						ProposalGenerator propGen = new ProposalGenerator(xpac,
								checker);
						State st = (State) obj;
						propGen.createProposals(st);
						createMarkersForProposals(propGen, messageAcceptor, st,
								xpac);
					}
					// the following part takes care of all the warnings
					if (obj instanceof StateGraphItem) {
						StateGraphItem item = (StateGraphItem) obj;
						createMarkersForWarnings(checker, messageAcceptor,
								item, xpac);
					}
				}
				if (traceExec)
					System.out
							.println("AbstractExecutionValidator done checking class "
									+ ac.getName());
			}
			else
				if(traceExec)
					System.out.println("No checking because internal generation failed");
		}
	}

	@Override
	public String getName() {
		return "Abstract FSM Execution Validator";
	}

	@Override
	public String getDescription() {
		return "This validator checks the state machine against the protocol semantics of its ports.";
	}

	private void createMarkersForProposals(ProposalGenerator propGen,
			ValidationMessageAcceptor messageAcceptor, State st,
			ExpandedActorClass xpac) {
		List<MessageFromIf> incoming = propGen.getIncomingProposals();
		EObject orig = xpac.getOrig(st);
		EObject container = orig.eContainer();
		@SuppressWarnings("unchecked")
		int idx = ((List<? extends EObject>) container.eGet(orig
				.eContainingFeature())).indexOf(orig);

		for (MessageFromIf msg : incoming) {
			messageAcceptor.acceptWarning("State should handle the message "
					+ msg.getMessage().getName() + " from port "
					+ msg.getFrom().getName() + " ", container,
					orig.eContainingFeature(), idx, DIAG_CODE_MISSING_TRIGGER,
					st.getName());
		}
		List<MessageFromIf> outgoing = propGen.getOutgoingProposals();

		for (MessageFromIf msg : outgoing) {
			messageAcceptor.acceptInfo("State should send the message "
					+ msg.getMessage().getName() + " to port "
					+ msg.getFrom().getName() + " ", container,
					orig.eContainingFeature(), idx, DIAG_CODE_MISSING_MESSAGESEND,
					st.getName());

		}

	}

	private void createMarkersForWarnings(SemanticsCheck checker,
			ValidationMessageAcceptor messageAcceptor, StateGraphItem item,
			ExpandedActorClass xpac) {
		List<HandledMessage> warningList = checker.getWarningMsg(item);
		if (traceExec && warningList != null) {
			System.out.println("Messages in the warning list for item "
					+ item.getName());
		}
		if (warningList != null)
			for (HandledMessage msg : warningList) {
				EObject origin = msg.getOrigin();
				if (origin instanceof ActiveTrigger) {
					ActiveTrigger trigger = (ActiveTrigger) origin;
					for (TriggeredTransition trans : trigger.getTransitions()) {
						// have to translate back the transition to our original
						// model
						TriggeredTransition orig = (TriggeredTransition) xpac
								.getOrig(trans);
						for (Trigger trig : orig.getTriggers()) {
							for (MessageFromIf mif : trig.getMsgFromIfPairs()) {
								// messages haven't been copied, so all point to
								// the same objects and we can just compare
								// pointers
								if (mif.getMessage() == msg.getMsg()
										&& mif.getFrom() == msg.getIfitem()) {
									messageAcceptor
											.acceptWarning(
													"The message violates the semantic rule",
													trig,
													mif.eContainingFeature(),
													trig.getMsgFromIfPairs()
															.indexOf(trig),
													DIAG_CODE_VIOLATION_TRIGGER, trigger
															.getMsg().getName());
								}
							}
						}
					}
				} else if (origin instanceof DetailCode) {
					DetailCode dc = (DetailCode) origin;
					EObject orig = xpac.getOrig(dc);
					messageAcceptor.acceptWarning(
							"The message violates the semantic rule",
							orig.eContainer(), orig.eContainingFeature(),
							ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
							DIAG_CODE_VIOLATION_MESSAGESEND);

				}
			}
	}
}

Back to the top