Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d454ab6daa524bd9a864959056c5b517b3d461ec (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*******************************************************************************
 * Copyright (c) 2012 protos software gmbh (http://www.protos.de).
 * All rights reserved. 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:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.core.etmap.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.etrice.core.etmap.eTMap.Mapping;
import org.eclipse.etrice.core.etmap.eTMap.MappingModel;
import org.eclipse.etrice.core.etmap.eTMap.SubSystemMapping;
import org.eclipse.etrice.core.etmap.eTMap.ThreadMapping;
import org.eclipse.etrice.core.etphys.eTPhys.ExecMode;
import org.eclipse.etrice.core.etphys.eTPhys.NodeRef;
import org.eclipse.etrice.core.etphys.eTPhys.PhysicalThread;
import org.eclipse.etrice.core.genmodel.etricegen.AbstractInstance;
import org.eclipse.etrice.core.genmodel.etricegen.ActorInstance;
import org.eclipse.etrice.core.genmodel.etricegen.InstanceBase;
import org.eclipse.etrice.core.genmodel.etricegen.Root;
import org.eclipse.etrice.core.genmodel.etricegen.StructureInstance;
import org.eclipse.etrice.core.genmodel.etricegen.SubSystemInstance;
import org.eclipse.etrice.core.genmodel.etricegen.SystemInstance;
import org.eclipse.etrice.core.genmodel.fsm.IDiagnostician;
import org.eclipse.etrice.core.room.ActorInstanceMapping;
import org.eclipse.etrice.core.room.LogicalThread;
import org.eclipse.etrice.core.room.RefPath;
import org.eclipse.etrice.core.room.RefSegment;
import org.eclipse.etrice.core.room.SubSystemClass;

/**
 * @author Henrik Rentz-Reichert
 *
 */
public class ETMapUtil {

	public static class MappedThread {
		private PhysicalThread thread = null;
		private boolean implicit = false;
		private boolean asParent = false;
		
		public MappedThread(PhysicalThread thread) {
			this(thread, false, false);
		}
		
		public MappedThread(PhysicalThread thread, boolean implicit, boolean asParent) {
			super();
			this.thread = thread;
			this.implicit = implicit;
			this.asParent = asParent;
		}

		public PhysicalThread getThread() {
			return thread;
		}

		public boolean isImplicit() {
			return implicit;
		}

		public boolean isAsParent() {
			return asParent;
		}
	}
	
	@SuppressWarnings("serial")
	private static class PathToPThread extends HashMap<String, MappedThread> {}
	
	@SuppressWarnings("serial")
	private static class PathToNodeRef extends HashMap<String, NodeRef> {}
	
	@SuppressWarnings("serial")
	private static class LThreadToPThread extends HashMap<LogicalThread, PhysicalThread> {}
	
	@SuppressWarnings("serial")
	private static class NodeRefToSubSysInstances extends HashMap<NodeRef, List<String>> {
		void put(NodeRef nr, String path) {
			List<String> list = get(nr);
			if (list==null) {
				list = new ArrayList<String>();
				put(nr, list);
			}
			list.add(path);
		}
	}
	
	private static PathToPThread path2pthread = new PathToPThread();
	private static PathToNodeRef path2ndref = new PathToNodeRef();
	private static NodeRefToSubSysInstances ndref2ssipaths = new NodeRefToSubSysInstances();
	private static HashSet<NodeRef> noderefs = new HashSet<NodeRef>();
	
	public static Collection<NodeRef> getNodeRefs() {
		return noderefs;
	}
	
	public static List<String> getSubSystemInstancePaths(NodeRef nr) {
		return ndref2ssipaths.get(nr);
	}
	
	public static NodeRef getNodeRef(AbstractInstance si) {
		String path = si.getPath();
		NodeRef nodeRef = path2ndref.get(path);
		return nodeRef;
	}
	
	public static MappedThread getMappedThread(AbstractInstance ai) {
		String path = ai.getPath();
		MappedThread thread = path2pthread.get(path);
		return thread;
	}
	
	public static Set<PhysicalThread> getUsedThreads(NodeRef nr, SubSystemInstance ssi){
		Set<PhysicalThread> usedThreads = new HashSet<PhysicalThread>();
		for(ActorInstance ai : ssi.getAllContainedInstances())
			usedThreads.add(getMappedThread(ai).getThread());
		usedThreads.retainAll(nr.getType().getThreads());
		
		return usedThreads;
	}
	
	public static String dumpMappings() {
		StringBuilder result = new StringBuilder();
		
		ArrayList<String> keys = new ArrayList<String>(path2pthread.keySet());
		Collections.sort(keys);
		
		for (String key : keys) {
			NodeRef node = path2ndref.get(key);
			MappedThread thread = path2pthread.get(key);
			result.append("\n"+key+"\t -> "+node.getName()+":"+thread.getThread().getName());
			if (thread.isImplicit())
				result.append(" (implicit)");
		}
		
		return result.toString();
	}
	
	public static void processModels(Root genmodel, ResourceSet rs, IDiagnostician diagnostician) {
		path2pthread.clear();
		path2ndref.clear();
		ndref2ssipaths.clear();
		noderefs.clear();
		
		for (Resource res : rs.getResources()) {
			if (!res.getContents().isEmpty()) {
				if (res.getContents().get(0) instanceof MappingModel) {
					processMappings(genmodel, (MappingModel) res.getContents().get(0));
				}
			}
		}
		
		for (Entry<String, MappedThread> p2t : path2pthread.entrySet()) {
			String path = p2t.getKey();
			StructureInstance si = genmodel.getInstance(path);
			if (si instanceof ActorInstance) {
				PhysicalThread thread = p2t.getValue().getThread();
				switch (((ActorInstance) si).getActorClass().getCommType()) {
				case EVENT_DRIVEN:
					if (thread.getExecmode()==ExecMode.POLLED) {
						error(diagnostician, path, "event driven", thread, "polled thread");
					}
					break;
				case DATA_DRIVEN:
					if (thread.getExecmode()==ExecMode.BLOCKED) {
						error(diagnostician, path, "data driven", thread, "blocked thread");
					}
					break;
				case ASYNCHRONOUS:
					if (thread.getExecmode()==ExecMode.BLOCKED) {
						error(diagnostician, path, "asynchronous", thread, "blocked thread");
					}
					else if (thread.getExecmode()==ExecMode.POLLED) {
						error(diagnostician, path, "asynchronous", thread, "polled thread");
					}
					break;
				case SYNCHRONOUS:
					// not implemented yet
					break;
				}
			}
		}
	}

	/**
	 * @param diagnostician
	 * @param inst
	 * @param inst_comm_type
	 * @param thread
	 * @param thread_comm_type
	 */
	private static void error(IDiagnostician diagnostician, String inst, String inst_comm_type, PhysicalThread thread, String thread_comm_type) {
		NodeRef nr = path2ndref.get(inst);
		diagnostician.error(inst_comm_type+" actor instance '"+inst+"' mapped to "
				+thread_comm_type+" '"+thread.getName()+"' of node '"+nr.getName()+"'", null, null);
	}
	
	private static void processMappings(Root genmodel, MappingModel mdl) {
		for (Mapping mp : mdl.getMappings()) {
			for (SubSystemMapping ssmp : mp.getSubsysMappings()) {
				String path = InstanceBase.pathDelim+mp.getLogicalSys().getName()
						+InstanceBase.pathDelim+ssmp.getLogicalSubSys().getName();
				
				path2ndref.put(path, ssmp.getNode());
				ndref2ssipaths.put(ssmp.getNode(), path);
				noderefs.add(ssmp.getNode());
				
				LThreadToPThread lthread2pthread = new LThreadToPThread();
				for (ThreadMapping tmp : ssmp.getThreadMappings()) {
					lthread2pthread.put(tmp.getLogicalThread(), tmp.getPhysicalThread());
				}
				
				SubSystemClass subsys = ssmp.getLogicalSubSys().getType();
				createThreadMappings(subsys.getActorInstanceMappings(), path, lthread2pthread);
			}
		}
		addImplicitMappings(genmodel);
	}

	private static void createThreadMappings(
			EList<ActorInstanceMapping> mappings,
			String parentPath,
			LThreadToPThread lthread2pthread) {
		for (ActorInstanceMapping aim : mappings) {
			String path = parentPath+getPath(aim.getPath());
			path2pthread.put(path, new MappedThread(lthread2pthread.get(aim.getThread())));
			
			// recursion
			createThreadMappings(aim.getActorInstanceMappings(), path, lthread2pthread);
		}
	}
	
	private static void addImplicitMappings(Root genmodel) {
		for (SystemInstance si : genmodel.getSystemInstances()) {
			for (SubSystemInstance ssi : si.getInstances()) {
				NodeRef node = path2ndref.get(ssi.getPath());
				if (node!=null) {
					PhysicalThread dflt = getDefaultThread(node);
					addImplicitMappings(ssi, dflt, node);
				}
			}
		}
	}

	private static void addImplicitMappings(StructureInstance si, PhysicalThread dflt, NodeRef node) {
		for (AbstractInstance ai : si.getInstances()) {
			String path = ai.getPath();
			path2ndref.put(path, node);
			MappedThread thread = path2pthread.get(path);
			if (thread==null) {
				thread = new MappedThread(dflt, true, true);
				path2pthread.put(path, thread);
			}
			else if (thread.getThread()==dflt) {
				thread.asParent = true;
			}
			
			// recursion
			if (ai instanceof StructureInstance) {
				addImplicitMappings((StructureInstance) ai, thread.getThread(), node);
			}
		}
	}
	
	private static PhysicalThread getDefaultThread(NodeRef node) {
		for (PhysicalThread thread : node.getType().getThreads()) {
			if (thread.isDefault())
				return thread;
		}
		return null;
	}
	
	private static String getPath(RefPath path) {
		StringBuilder result = new StringBuilder();
		
		for (RefSegment ref : path.getRefs()) {
			result.append(InstanceBase.pathDelim+ref.toString());
		}
		
		return result.toString();
	}
	
}

Back to the top