Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2a2bfbc014c8b4491e5e979283028859d75bf7a9 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*******************************************************************************
 * Copyright (c) 2010 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 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * CONTRIBUTORS:
 * 		Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.core.validation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.etrice.core.fsm.fSM.MessageFromIf;
import org.eclipse.etrice.core.fsm.validation.FSMValidationUtil;
import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.ActorContainerClass;
import org.eclipse.etrice.core.room.ActorContainerRef;
import org.eclipse.etrice.core.room.ActorRef;
import org.eclipse.etrice.core.room.Binding;
import org.eclipse.etrice.core.room.BindingEndPoint;
import org.eclipse.etrice.core.room.CommunicationType;
import org.eclipse.etrice.core.room.CompoundProtocolClass;
import org.eclipse.etrice.core.room.ExternalPort;
import org.eclipse.etrice.core.room.GeneralProtocolClass;
import org.eclipse.etrice.core.room.InterfaceItem;
import org.eclipse.etrice.core.room.LayerConnection;
import org.eclipse.etrice.core.room.Port;
import org.eclipse.etrice.core.room.ProtocolClass;
import org.eclipse.etrice.core.room.RefSAPoint;
import org.eclipse.etrice.core.room.ReferenceType;
import org.eclipse.etrice.core.room.RelaySAPoint;
import org.eclipse.etrice.core.room.RoomModel;
import org.eclipse.etrice.core.room.SPP;
import org.eclipse.etrice.core.room.SPPoint;
import org.eclipse.etrice.core.room.ServiceImplementation;
import org.eclipse.etrice.core.room.StructureClass;
import org.eclipse.etrice.core.room.SubProtocol;
import org.eclipse.etrice.core.room.SubSystemClass;
import org.eclipse.etrice.core.room.util.CompoundProtocolHelpers;
import org.eclipse.etrice.core.room.util.CompoundProtocolHelpers.Match;
import org.eclipse.etrice.core.room.util.RoomHelpers;

import com.google.inject.Inject;

public class ValidationUtil extends FSMValidationUtil {
	
	public static final String CONNECTED_SUB_COMPONENT_PORTS_MUST_BE_CONJUGATED_TO_EACH_OTHER = "connected sub component ports must be conjugated to each other";
	
	@Inject
	private RoomHelpers roomHelpers;

	/**
	 * check if compound protocol is circular
	 * @param ref
	 * @param cpc
	 * @return <code>true</code> if cpc contains a cycle
	 */
	public boolean isCircular(CompoundProtocolClass ref, CompoundProtocolClass cpc) {
		Set<CompoundProtocolClass> visited = new HashSet<CompoundProtocolClass>();
		LinkedList<CompoundProtocolClass> stack = new LinkedList<CompoundProtocolClass>();
		visited.add(cpc);
		stack.push(ref);
		
		CompoundProtocolClass next;
		while(!stack.isEmpty()){
			next = stack.pop();
			if(next == cpc)
				return true;
			if(visited.contains(next))
				continue;
			for(SubProtocol subProtocol : next.getSubProtocols()){
				if(subProtocol.getProtocol() instanceof CompoundProtocolClass){	
					CompoundProtocolClass c = (CompoundProtocolClass)subProtocol.getProtocol();
					stack.push(c);
				}
			}
			visited.add(next);
		}
		
		return false;
	}
	
	/**
	 * returns true if this port is connectable inside its parent, i.e. an internal end port or a relay port
	 * 
	 * @param port
	 * @return ok if connectable
	 */
	public Result isConnectable(Port port, ActorContainerRef ref, StructureClass acc) {
		return isConnectable(port, ref, acc, null);
	}
	
	public Result isConnectable(Port port, ActorContainerRef ref, StructureClass acc, Binding exclude) {
		if (!isMultipleConnectable(port, ref) && isConnected(port, ref, acc, exclude))
			return Result.error("port with multiplicity 1 is already connected");

		if (acc instanceof ActorClass) {
			for (ExternalPort xp : ((ActorClass)acc).getExternalPorts()) {
				if (xp.getInterfacePort()==port)
					return Result.error("external end ports must not be connected");
			}
		}

		return Result.ok();
	}
	
	public boolean isMultipleConnectable(Port port, ActorContainerRef ref) {
		if (port.isReplicated())
			return true;

		if (ref!=null && ref instanceof ActorRef && ((ActorRef)ref).getMultiplicity()>1)
			return true;
		
		if (roomHelpers.isRelay(port) && port.getProtocol() instanceof CompoundProtocolClass)
			return true;
		
		if (port.getProtocol() instanceof ProtocolClass && ((ProtocolClass)port.getProtocol()).getCommType() == CommunicationType.DATA_DRIVEN) {
			if (ref == null) {
				// this port is local in the structure class
				if (roomHelpers.isRelay(port))
					return !port.isConjugated();
				else
					return port.isConjugated();
			}
			else
				return port.isConjugated();
		}

		return false;
	}

	public Result isValid(Binding bind) {
		return isConnectable(
				bind.getEndpoint1().getPort(), bind.getEndpoint1().getActorRef(),  bind.getEndpoint1().getSub(),
				bind.getEndpoint2().getPort(), bind.getEndpoint2().getActorRef(),  bind.getEndpoint2().getSub(),
				(StructureClass)bind.eContainer(), bind, true);
	}

	public Result isConnectable(BindingEndPoint ep1, BindingEndPoint ep2, StructureClass sc) {
		return isConnectable(ep1.getPort(), ep1.getActorRef(), ep1.getSub(), ep2.getPort(), ep2.getActorRef(), ep2.getSub(), sc);
	}
	
	public Result isConnectable(
			Port p1, ActorContainerRef ref1, SubProtocol sub1,
			Port p2, ActorContainerRef ref2, SubProtocol sub2, StructureClass sc) {
		return isConnectable(p1, ref1, sub1, p2, ref2, sub2, sc, null, true);
	}
	
	public Result isConnectable(
			Port p1, ActorContainerRef ref1, SubProtocol sub1,
			Port p2, ActorContainerRef ref2, SubProtocol sub2,
			StructureClass sc, Binding exclude,
			boolean checkCompound) {
		
		if (p1==p2)
			return Result.error("no self connection allowed, ports are identical");
		
		// check protocol compatibility
		boolean pc1extendsIncoming = false;
		boolean pc1extendsOutgoing = false;
		boolean pc2extendsIncoming = false;
		boolean pc2extendsOutgoing = false;
		{
			GeneralProtocolClass pc1 = p1.getProtocol();
			GeneralProtocolClass pc2 = p2.getProtocol();
			boolean compoundInvolved = pc1 instanceof CompoundProtocolClass || pc2 instanceof CompoundProtocolClass;
			if (pc1 instanceof CompoundProtocolClass && pc2 instanceof CompoundProtocolClass) {
				if (sub1!=null)
					pc1 = sub1.getProtocol();
				if (sub2!=null)
					pc2 = sub2.getProtocol();
				if (checkCompound && pc1!=pc2)
					return Result.error("(sub) protocols don't match");
			}
			else if (pc1 instanceof ProtocolClass && pc2 instanceof CompoundProtocolClass) {
				if (checkCompound) {
					if (sub2==null)
						return Result.error("specify a sub protocol at "+p2.getName());
					if (pc1!=sub2.getProtocol())
						return Result.error("sub protocol doesn't match");
				}
			}
			else if (pc1 instanceof CompoundProtocolClass && pc2 instanceof ProtocolClass) {
				if (checkCompound) {
					if (sub1==null)
						return Result.error("specify a sub protocol at "+p1.getName());
					if (pc2!=sub1.getProtocol())
						return Result.error("sub protocol doesn't match");
				}
			}
			else {
				if (pc1!=pc2) {
					if (compoundInvolved) {
						return Result.error("protocols don't match");
					}
					else {
						// under some circumstances the protocols are instances of GeneralProtocolClass
						// (why?)
						if (!(pc1 instanceof ProtocolClass && pc2 instanceof ProtocolClass))
							return Result.error("protocols don't match");
						
						if (roomHelpers.isDerivedFrom((ProtocolClass)pc1, (ProtocolClass)pc2)) {
							if (roomHelpers.getAllMessages((ProtocolClass)pc1,true).size() > roomHelpers.getAllMessages((ProtocolClass)pc2,true).size())
								pc1extendsIncoming = true;
							if (roomHelpers.getAllMessages((ProtocolClass)pc1,false).size()>roomHelpers.getAllMessages((ProtocolClass)pc2,false).size())
								pc1extendsOutgoing = true;
							if (pc1extendsIncoming && pc1extendsOutgoing)
								return Result.error("derived protocols not connectable (both directions extended)");

						}
						else if (roomHelpers.isDerivedFrom((ProtocolClass)pc2, (ProtocolClass)pc1)) {
							if (roomHelpers.getAllMessages((ProtocolClass)pc2,true).size()>roomHelpers.getAllMessages((ProtocolClass)pc1,true).size())
								pc2extendsIncoming = true;
							if (roomHelpers.getAllMessages((ProtocolClass)pc2,false).size()>roomHelpers.getAllMessages((ProtocolClass)pc1,false).size())	
								pc2extendsOutgoing = true;
							if (pc2extendsIncoming && pc2extendsOutgoing)
								return Result.error("derived protocols not connectable (both directions extended)");
						}
						else {
							if (pc1.getName().equals(pc2.getName())) {
								String ns1 = ((RoomModel)pc1.eContainer()).getName();
								String ns2 = ((RoomModel)pc2.eContainer()).getName();
								if (!ns1.equals(ns2))
									return Result.error("protocols don't match (same name, different name spaces)");
								
								return Result.error("protocols don't match (but have same name)");
							}
							return Result.error("protocols don't match");
						}
					}
				}
			}

			if (checkCompound || !compoundInvolved) {
				ProtocolClass spc1 = null;
				if (pc1 instanceof ProtocolClass)
					spc1 = (ProtocolClass) pc1;
				else if (sub1.getProtocol() instanceof ProtocolClass)
					spc1 = (ProtocolClass) sub1.getProtocol();
				ProtocolClass spc2 = null;
				if (pc2 instanceof ProtocolClass)
					spc2 = (ProtocolClass) pc2;
				else if (sub2.getProtocol() instanceof ProtocolClass)
					spc2 = (ProtocolClass) sub2.getProtocol();
				if (spc1!=null && spc2!=null)
					if (spc1.getCommType()!=spc2.getCommType())
						return Result.error("protocol communication types don't match");
			}
			if (compoundInvolved) {
				List<Match> matches = CompoundProtocolHelpers.getMatches(p1, ref1, p2, ref2, sc, exclude);
				if (matches.isEmpty())
					return Result.error("no matching sub protocol(s) found");
				if (matches.size()==1)
					if (exclude!=null) {
						if (matches.get(0).getLeft()!=exclude.getEndpoint1().getSub())
							return Result.error("sub protocol already connected");
						if (matches.get(0).getRight()!=exclude.getEndpoint2().getSub())
							return Result.error("sub protocol already connected");
					}
			}
			else {
				if (alreadyConnected(p1, ref1, p2, ref2, sc, exclude))
					return Result.error("ports are already bound");
			}
		}
		
		
		if (ref1==null && ref2==null)
			return Result.error("cannot connect two local ports");
		
		if (ref1!=null && ref2!=null) {
			if (ref1==ref2)
				return Result.error("ports of one ref must not be connected");

			// both ports are on references
			if (p1.isConjugated()==p2.isConjugated())
				return Result.error(CONNECTED_SUB_COMPONENT_PORTS_MUST_BE_CONJUGATED_TO_EACH_OTHER);
			
			if (p1.isConjugated() && pc1extendsIncoming)
				return Result.error("protocol extends incoming");
			if (p2.isConjugated() && pc2extendsIncoming)
				return Result.error("protocol extends incoming");
			if (!p1.isConjugated() && pc1extendsOutgoing)
				return Result.error("protocol extends outgoing");
			if (!p2.isConjugated() && pc2extendsOutgoing)
				return Result.error("protocol extends outgoing");
			
			Result result = isConnectable(p1, ref1, sc, exclude);
			if (!result.isOk())
				return result;
			result = isConnectable(p2, ref2, sc, exclude);
			if (!result.isOk())
				return result;
		}
		else {
			// one port is an internal end port or a relay port
			Port local = ref1==null? p1:p2;
			Port sub = ref1!=null? p1:p2;
			ActorContainerRef ref = ref1!=null? ref1:ref2;
			
			if (ref instanceof ActorRef && ((ActorRef) ref).getRefType()==ReferenceType.OPTIONAL) {
				if (((ActorRef) ref).getMultiplicity()==-1) {
					// the port must have multiplicity any
					if (local.getMultiplicity()!=-1)
						return Result.error("local port '"+local.getName()+"' must have multiplicity any");
				}
			}
			if (roomHelpers.isRelay(local)) {
				if (local.isConjugated()!=sub.isConjugated())
					return Result.error("relay port must have same direction as local port");

				if (local==p1) {
					if (!p1.isConjugated() && pc1extendsIncoming)
						return Result.error("protocol extends incoming");
					if (p2.isConjugated() && pc2extendsIncoming)
						return Result.error("protocol extends incoming");
					if (p1.isConjugated() && pc1extendsOutgoing)
						return Result.error("protocol extends outgoing");
					if (!p2.isConjugated() && pc2extendsOutgoing)
						return Result.error("protocol extends outgoing");
				}
				else {
					if (p1.isConjugated() && pc1extendsIncoming)
						return Result.error("protocol extends incoming");
					if (!p2.isConjugated() && pc2extendsIncoming)
						return Result.error("protocol extends incoming");
					if (!p1.isConjugated() && pc1extendsOutgoing)
						return Result.error("protocol extends outgoing");
					if (p2.isConjugated() && pc2extendsOutgoing)
						return Result.error("protocol extends outgoing");
				}
				ActorContainerClass acc = (ActorContainerClass) ref.eContainer();
				Result result = isConnectable(local, null, acc, exclude);
				if (!result.isOk())
					return result;
				result = isConnectable(sub, ref, acc, exclude);
				if (!result.isOk())
					return result;
			}
			else {
				// local port must be an internal end port
				
				if (local.isConjugated()==sub.isConjugated())
					return Result.error("internal end port must have opposite direction");
				
				if (p1.isConjugated() && pc1extendsIncoming)
					return Result.error("protocol extends incoming");
				if (p2.isConjugated() && pc2extendsIncoming)
					return Result.error("protocol extends incoming");
				if (!p1.isConjugated() && pc1extendsOutgoing)
					return Result.error("protocol extends outgoing");
				if (!p2.isConjugated() && pc2extendsOutgoing)
					return Result.error("protocol extends outgoing");

				Result result = isConnectable(sub, ref, sc, exclude);
				if (!result.isOk())
					return result;

				result = isConnectable(local, null, sc, exclude);
				if (!result.isOk())
					return result;
			}
		}
		
		return Result.ok();
	}

	/**
	 * @param p1
	 * @param ref1
	 * @param p2
	 * @param ref2
	 * @param sc
	 * @param exclude
	 * @return
	 */
	private boolean alreadyConnected(Port p1, ActorContainerRef ref1,
			Port p2, ActorContainerRef ref2, StructureClass sc, Binding exclude) {
		
		HashSet<String> bindings = new HashSet<String>();
		String key = getKey(p1, ref1, p2, ref2);
		bindings.add(key);
		for (Binding bind : sc.getBindings()) {
			if (bind==exclude)
				continue;

			key = getKey(bind.getEndpoint1().getPort(), bind.getEndpoint1().getActorRef(),
					bind.getEndpoint2().getPort(), bind.getEndpoint2().getActorRef());
			if (!bindings.add(key))
				return true;
		}
		return false;
	}

	private String getKey(Port p1, ActorContainerRef ref1, Port p2, ActorContainerRef ref2) {
		String ep1 = getEndpointName(p1, ref1);
		String ep2 = getEndpointName(p2, ref2);
		// we order endpoint names to be able to identify bindings with exchanged endpoints
		return (ep1.compareTo(ep2)>0)? ep1+ep2:ep2+ep1;
	}
	/**
	 * @param p1
	 * @param ref1
	 * @return
	 */
	private String getEndpointName(Port p1, ActorContainerRef ref1) {
		if (ref1==null)
			return p1.getName()+"#.";
		else
			return p1.getName()+"#"+ref1.getName();
	}

	public Result isFreeOfReferences(Port port) {
		Collection<Setting> refs = EcoreUtil.UsageCrossReferencer.find(port, port.eResource().getResourceSet());
		boolean bound = false;
		boolean usedByFSM = false;
		for (Setting ref : refs) {
			if (ref.getEObject() instanceof BindingEndPoint)
				bound = true;
			else if (ref.getEObject() instanceof MessageFromIf)
				usedByFSM = true;
		}
		
		if (bound && usedByFSM)
			return Result.error("port is bound and also used by state machine (triggers)");
		else if (bound)
			return Result.error("port is bound (may be externally)");
		else if (usedByFSM)
			return Result.error("port is used by state machine (triggers)");
		
		return Result.ok();
	}

	public boolean isReferencedAsReplicatedInModel(ActorClass ac) {
		Collection<Setting> refs = EcoreUtil.UsageCrossReferencer.find(ac, ac.eResource().getResourceSet());
		for (Setting ref : refs) {
			if (ref.getEObject() instanceof ActorRef)
				return ((ActorRef)ref.getEObject()).getMultiplicity()>1;
		}
		
		return false;
	}
	
	public boolean isConnected(Port port, ActorContainerRef ref, StructureClass sc) {
		return isConnected(port, ref, sc, null);
	}
	
	public boolean isConnected(Port port, ActorContainerRef ref, StructureClass sc, Binding exclude) {
		for (Binding bind : sc.getBindings()) {
			if (bind!=exclude) {
				if (isEndpoint(bind.getEndpoint1(), port, ref))
					return true;
				if (isEndpoint(bind.getEndpoint2(), port, ref))
					return true;
			}
		}
		
		if (sc instanceof ActorClass) {
			if (((ActorClass)sc).getActorBase()!=null)
				return isConnected(port, ref, ((ActorClass)sc).getActorBase(), exclude);
		}
		return false;
	}

	private boolean isEndpoint(BindingEndPoint ep, Port port, ActorContainerRef ref) {
		return ep.getActorRef()==ref && ep.getPort()==port;
	}

	public boolean isRelay(SPP spp) {
		ActorContainerClass acc = (ActorContainerClass) spp.eContainer();
		if (acc instanceof ActorClass) {
			ActorClass ac = (ActorClass) acc;
			for (ServiceImplementation svc : ac.getServiceImplementations()) {
				if (svc.getSpp()==spp)
					return false;
			}
		}
		return true;
	}
	
	public Result isValid(LayerConnection lc) {
		if (lc.getFrom() instanceof RelaySAPoint)
			return isConnectable(((RelaySAPoint)lc.getFrom()).getRelay(), null, lc.getTo().getService(), lc.getTo().getRef(), (StructureClass)lc.eContainer(), lc);
		else if (lc.getFrom() instanceof RefSAPoint)
			return isConnectable(null, ((RefSAPoint)lc.getFrom()).getRef(), lc.getTo().getService(), lc.getTo().getRef(), (StructureClass)lc.eContainer(), lc);
		else {
			assert(false): "unexpected sub type";
			return Result.error("internal error");
		}
	}
	
	public Result isConnectable(SPP src, ActorContainerRef srcRef,
			SPP tgt, ActorContainerRef tgtRef, StructureClass ac) {
		return isConnectable(src, srcRef, tgt, tgtRef, ac, null);
	}
	
	public Result isConnectable(SPP src, ActorContainerRef srcRef,
			SPP dst, ActorContainerRef dstRef, StructureClass sc, LayerConnection exclude) {

		if (sc==null) {
			return Result.error("internal error");
		}
		
		if ((src==null && srcRef==null) || (src!=null && srcRef!=null))
			return Result.error("source can be an own SPP _or_ a ref");
		
		if (dst==null || dstRef==null)
			return Result.error("destination must be an SPP on a ref");

		if (src!=null && isConnectedSrc(src, sc, exclude))
			return Result.error("source SPP is already connected");
		
		// the destination may be connected several times, so don't check this
		//		if (isConnectedDst(dst, dstRef, sc, exclude))
		//			return Result.error("destination SPP is already connected");
		
		
		return Result.ok();
	}

	public boolean isConnectableSrc(SPP src, ActorContainerRef ref,
			StructureClass sc) {
		return isConnectableSrc(src, ref, sc, null);
	}
	
	public boolean isConnectableSrc(SPP src, ActorContainerRef ref,
			StructureClass sc, LayerConnection exclude) {
		
		if (sc==null) {
			return false;
		}
		
		if ((src==null && ref==null) || (src!=null && ref!=null))
			return false;

		if (ref instanceof ActorRef)
			if (((ActorRef) ref).getMultiplicity()>1)
				return false;
		
		// in case of ref!=null no further checks possible
		// the connection is attached to an ActorContainerRef
		// which can be multiply connected
		
		if (src!=null) {
			if (isConnectedSrc(src, sc, exclude))
				return false;
		}
		
		return true;
	}

	public boolean isReferencedInModel(SPP spp) {
		Collection<Setting> refs = EcoreUtil.UsageCrossReferencer.find(spp, spp.eResource().getResourceSet());
		for (Setting ref : refs) {
			if (ref.getEObject() instanceof ServiceImplementation)
				return true;
			else if (ref.getEObject() instanceof RelaySAPoint)
				return true;
			else if (ref.getEObject() instanceof SPPoint)
				return true;
		}
		
		return false;
	}
	
	public boolean isConnectedSrc(SPP src, StructureClass sc) {
		return isConnectedSrc(src, sc, null);
	}
	
	public boolean isConnectedSrc(SPP src, StructureClass sc, LayerConnection exclude) {
		for (LayerConnection lc : sc.getConnections()) {
			if (lc!=exclude)
				if (lc.getFrom() instanceof RelaySAPoint) {
					if (((RelaySAPoint)lc.getFrom()).getRelay()==src)
						return true;
				}
		}
		
		if (sc instanceof ActorClass) {
			ActorClass ac = (ActorClass)sc;
			
			// check for attached services
			for (ServiceImplementation svc : ac.getServiceImplementations()) {
				if (svc.getSpp()==src)
					return true;
			}
			
			// recurse into base classes
			if (ac.getActorBase()!=null)
				return isConnectedSrc(src, ac.getActorBase(), exclude);
		}
		return false;
	}

	public boolean isConnectableDst(SPP src, ActorContainerRef ref,
			StructureClass sc) {
		return isConnectableDst(src, ref, sc, null);
	}
	
	public boolean isConnectableDst(SPP dst, ActorContainerRef ref,
			StructureClass sc, LayerConnection exclude) {
		
		if (sc==null) {
			return false;
		}
		
		if (dst==null || ref==null)
			return false;

		if (dst!=null) {
			if (isConnectedDst(dst, ref, sc, exclude))
				return false;
		}
		
		return true;
	}
	
	public boolean isConnectedDst(SPP src, ActorContainerRef acr, StructureClass sc) {
		return isConnectedDst(src, acr, sc, null);
	}
	
	public boolean isConnectedDst(SPP src, ActorContainerRef acr, StructureClass sc, LayerConnection exclude) {
		for (LayerConnection lc : sc.getConnections()) {
			if (lc!=exclude)
					if (lc.getTo().getService()==src && lc.getTo().getRef()==acr)
						return true;
		}
		
		if (sc instanceof ActorClass) {
			if (((ActorClass)sc).getActorBase()!=null)
				return isConnectedDst(src, acr, ((ActorClass)sc).getActorBase(), exclude);
		}
		return false;
	}

	public Result isUniqueName(InterfaceItem item) {
		return isUniqueName(item, item.getName());
	}
	
	public Result isUniqueName(InterfaceItem item, String name) {
		if (name.isEmpty())
			return Result.error("name must not be empty");
		
		if (!isValidID(name))
			return Result.error("name is no valid ID");
		
		if (item.eContainer() instanceof ActorClass) {
			ArrayList<InterfaceItem> all = new ArrayList<InterfaceItem>();
			ActorClass ac = (ActorClass) item.eContainer();
			if (roomHelpers.isCircularClassHierarchy(ac))
				return Result.ok();
			
			do {
				all.addAll(ac.getInterfacePorts());
				all.addAll(ac.getInternalPorts());
				all.addAll(ac.getServiceProvisionPoints());
				all.addAll(ac.getServiceAccessPoints());
				
				ac = ac.getActorBase();
			}
			while (ac!=null);
			
			for (InterfaceItem ii : all) {
				if (ii!=item && ii.getName().equals(name)) {
					if (ii.eContainer()!=item.eContainer())
						return Result.error("name already used in base class "+((ActorClass)ii.eContainer()).getName());
					else
						return Result.error("name already used");
				}
			}
		}
		else if (item.eContainer() instanceof SubSystemClass) {
			SubSystemClass ssc = (SubSystemClass) item.eContainer();
			ArrayList<InterfaceItem> all = new ArrayList<InterfaceItem>();
			all.addAll(ssc.getServiceProvisionPoints());
			all.addAll(ssc.getRelayPorts());
			
			for (InterfaceItem ii : all) {
				if (ii!=item && ii.getName().equals(name)) {
					return Result.error("name already used");
				}
			}
		}
		
		return Result.ok();
	}
}

Back to the top