Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4f909b7972521267c9cb6557fd49e79e96565124 (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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*****************************************************************************
 * Copyright (c) 2011 CEA LIST.
 *
 *    
 * 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:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.alf.scoping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.alf.alf.AcceptClause;
import org.eclipse.papyrus.alf.alf.AcceptStatement;
import org.eclipse.papyrus.alf.alf.DocumentedStatement;
import org.eclipse.papyrus.alf.alf.InvocationOrAssignementOrDeclarationStatement;
import org.eclipse.papyrus.alf.alf.LocalNameDeclarationStatement;
import org.eclipse.papyrus.alf.alf.LoopVariableDefinition;
import org.eclipse.papyrus.alf.alf.SequenceExpansionExpression;
import org.eclipse.papyrus.alf.alf.Statement;
import org.eclipse.papyrus.alf.validation.AlfJavaValidator;
import org.eclipse.papyrus.alf.validation.typing.TypeExpression;
import org.eclipse.papyrus.alf.validation.typing.TypeUtils;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.EnumerationLiteral;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Namespace;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageImport;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Reception;
import org.eclipse.uml2.uml.Signal;
import org.eclipse.uml2.uml.VisibilityKind;

public abstract class AlfScopingTool {

	protected static List<EObject> removeDuplicateClassifiers(List<EObject> list) {
		List<EObject> intermediateFilteredList = new ArrayList<EObject>() ;

		HashMap<String, Classifier> classifiers = new HashMap<String, Classifier>() ;
		HashMap<String, Classifier> elementImports = new HashMap<String, Classifier>() ;
		for (EObject o : list) {
			if (o instanceof Classifier) {
				Classifier c = (Classifier) o ;
				if (classifiers.get(c.getQualifiedName()) == null) {
					classifiers.put(c.getQualifiedName(), c) ;
					intermediateFilteredList.add(c) ;
				}
				// else => nothing to be done, this is a duplicate
			}
			else if (o instanceof ElementImport) {
				ElementImport e = (ElementImport)o ;
				Classifier c = (Classifier)e.getImportedElement() ;
				if (elementImports.get(e.getAlias()) == null) {
					elementImports.put(e.getAlias(), c) ;
					intermediateFilteredList.add(e) ;
				}
				else {// need to check if element import aliases the same thing.
					Classifier alreadyInTheList = elementImports.get(e.getAlias()) ;
					if (! alreadyInTheList.getQualifiedName().equals(c.getQualifiedName())) {
						// The model is ill-formed, and there's no need to filter
						intermediateFilteredList.add(e) ;
					}
					// else => nothing to be done, this is a duplicate
				}
			}
		}

		// needs to make a second pass on the filtered list, to remove cases where aliases and names are the same, and represent the same element
		List<EObject> filteredList = new ArrayList<EObject>() ;
		for (int i = 0 ; i < intermediateFilteredList.size() ; i++) {
			String classifierName = (intermediateFilteredList.get(i) instanceof Classifier) ? 
					((Classifier)intermediateFilteredList.get(i)).getName() :
						((ElementImport)intermediateFilteredList.get(i)).getAlias()	;
					String classifierQualifiedName = (intermediateFilteredList.get(i) instanceof Classifier) ? 
							((Classifier)intermediateFilteredList.get(i)).getQualifiedName() :
								((Classifier)((ElementImport)intermediateFilteredList.get(i)).getImportedElement()).getQualifiedName() ;
							boolean duplicateFound = false ;
							for (int j = i + 1 ; j < intermediateFilteredList.size() && !duplicateFound ; j++) {
								String cddDuplicateClassifierName = (intermediateFilteredList.get(j) instanceof Classifier) ? 
										((Classifier)intermediateFilteredList.get(j)).getName() :
											((ElementImport)intermediateFilteredList.get(j)).getAlias()	;
										if (cddDuplicateClassifierName.equals(classifierName)) {
											String cddDuplicateClassifierQualifiedName = (intermediateFilteredList.get(j) instanceof Classifier) ? 
													((Classifier)intermediateFilteredList.get(j)).getQualifiedName() :
														((Classifier)((ElementImport)intermediateFilteredList.get(j)).getImportedElement()).getQualifiedName() ;
													if (cddDuplicateClassifierQualifiedName.equals(classifierQualifiedName)) {
														duplicateFound = true ;
													}
										}
							}
							if (!duplicateFound)
								filteredList.add(intermediateFilteredList.get(i)) ;
		}

		return filteredList ;
	}

	public abstract AlfPartialScope getVisibleVariablesOrParametersOrProperties(EObject context) ;


	public AlfPartialScope getVisibleOperationsOrBehaviors(EObject context) {
		AlfPartialScope.IGetNameStrategy nameStrategy = new OperationAndBehaviorNameStrategy() ;
		AlfPartialScope.IBuildScopeStrategy buildScopeStrategy = new OperationAndBehaviorBuildScopeStrategy() ;
		return new AlfPartialScope(nameStrategy, buildScopeStrategy, context) ;
	}

	public AlfPartialScope getVisibleBehaviors(EObject context) {
		AlfPartialScope.IGetNameStrategy nameStrategy = new BehaviorsNameStrategy() ;
		AlfPartialScope.IBuildScopeStrategy buildScopeStrategy = new BehaviorsBuildScopeStrategy() ;
		return new AlfPartialScope(nameStrategy, buildScopeStrategy, context) ;
	}

	public AlfPartialScope getVisibleClassifiers(EObject context) {
		AlfPartialScope.IGetNameStrategy nameStrategy = new ClassifiersNameStrategy() ;
		AlfPartialScope.IBuildScopeStrategy buildScopeStrategy = new ClassifiersBuildScopeStrategy() ;
		return new AlfPartialScope(nameStrategy, buildScopeStrategy, context) ;
	}

	public AlfPartialScope getVisiblePackages(EObject context) {
		if (context instanceof Package) {
			AlfPartialScope.IGetNameStrategy nameStrategy = new PackageNameStrategy() ;
			AlfPartialScope.IBuildScopeStrategy buildScopeStrategy = new PackagesBuildScopeStrategy() ;
			return new AlfPartialScope(nameStrategy, buildScopeStrategy, context) ;
		}
		AlfPartialScope.IGetNameStrategy nameStrategy = new PackageNameStrategy() ;
		AlfPartialScope.IBuildScopeStrategy buildScopeStrategy = new PackagesBuildScopeStrategy() ;
		return new AlfPartialScope(nameStrategy, buildScopeStrategy, context) ;
	}

	public boolean isAReturnStatementExpected(EObject context) {
		return false ;
	}

	public TypeExpression getExpectedReturnType(EObject context) {
		return null;
	}

	public AlfPartialScope getVisibleSignalReceptions(EObject context) {
		AlfPartialScope.IGetNameStrategy nameStrategy = new ReceptionsNameStrategy() ;
		AlfPartialScope.IBuildScopeStrategy buildScopeStrategy = new ReceptionsBuildScopeStrategy() ;
		return new AlfPartialScope(nameStrategy, buildScopeStrategy, context);
	}

	/*
	 * Strategies for operations
	 */
	protected class OperationAndBehaviorNameStrategy implements AlfPartialScope.IGetNameStrategy {
		public String getName(EObject element) {
			if (element instanceof Operation)
				return ((Operation)element).getName();
			else if (element instanceof Behavior)
				return ((Behavior)element).getName() ;
			else if (element instanceof ElementImport) {
				ElementImport eImport = (ElementImport)element ;
				if (eImport.getAlias() != null)
					return eImport.getAlias() ;
				else
					return ((Behavior)eImport.getImportedElement()).getName() ;
			}
			else if (element instanceof Reception) {
				return ((Reception)element).getName() ;
			}
			else
				return "Unexpected element kind..." ;
		}
	};

	protected class OperationAndBehaviorBuildScopeStrategy implements AlfPartialScope.IBuildScopeStrategy {
		public List<List<EObject>> buildScope(EObject contextElement) {
			List<List<EObject>> nestedScopes = new ArrayList<List<EObject>>() ;
			List<EObject> nestedList = new ArrayList<EObject>() ;

			// in the case where the context element is a Package or an ElementImport for a package, the scope can only contain behaviors
			Package potentialContextPackage = null ;
			if (contextElement instanceof Package) {
				potentialContextPackage = (Package)contextElement ;
			}
			else if (contextElement instanceof ElementImport) {
				ElementImport eImport = (ElementImport)contextElement ;
				if (eImport.getImportedElement() instanceof Package)
					potentialContextPackage = (Package)eImport.getImportedElement() ;
			}
			if (potentialContextPackage != null) {
				AlfPartialScope behaviorScope = AlfScopeProvider.scopingTool.getVisibleBehaviors(contextElement) ;
				for (List<EObject> scope : behaviorScope.getScopeDetails()) {
					nestedScopes.add(scope) ;
				}
				return nestedScopes ;
			}

			// At this point, we have identified that the context element is not a package.

			// First scoping levels concern operations
			// retrieves the contextClassier (i.e. the owner of the contextOperation, or contextElement)
			Classifier contextClassifier ;
			if (contextElement instanceof Classifier) {
				contextClassifier = (Classifier)contextElement ;
			}
			else if (contextElement instanceof ElementImport && ((ElementImport)contextElement).getImportedElement() instanceof Classifier) {
				contextClassifier = (Classifier) ((ElementImport)contextElement).getImportedElement() ;
			}
			else {
				contextClassifier = AlfJavaValidator.getContextClassifier() ;
			}

			// add all the operations owned by the context classifier at the first scoping level
			nestedList.addAll(contextClassifier.getOperations()) ;
			// adds also receptions
			if (contextClassifier instanceof org.eclipse.uml2.uml.Class) {
				nestedList.addAll(((Class)contextClassifier).getOwnedReceptions()) ;
			}

			nestedScopes.add(nestedList) ;
			// then builds other scoping levels based on context classifier inheritance hierarchy 
			List<Classifier> currentGenerals = new ArrayList<Classifier>() ;
			currentGenerals.addAll(contextClassifier.getGenerals()) ;
			List<Classifier> nextGenerals ;
			while (!currentGenerals.isEmpty()) {
				nextGenerals = new ArrayList<Classifier>() ;
				nestedList = new ArrayList<EObject>() ;
				for (Classifier general : currentGenerals) {
					nextGenerals.addAll(general.getGenerals()) ;
					for (Operation o : general.getOperations())
						nestedList.add(o) ;
							if (general instanceof Class) {
								for (Reception r : ((Class)general).getOwnedReceptions()) {
									nestedList.add(r) ;
								}
							}
				}
				nestedScopes.add(nestedList) ;
				currentGenerals = nextGenerals ;
			}

			// finally feeds last scoping levels with behaviors (only if contextElement is not a Class)
			if (! ((contextElement instanceof Classifier) || 
					((contextElement instanceof ElementImport && 
							((ElementImport)contextElement).getImportedElement() instanceof Classifier)))) {

				AlfPartialScope behaviorScope = AlfScopeProvider.scopingTool.getVisibleBehaviors(contextElement) ;

				for (List<EObject> scope : behaviorScope.getScopeDetails()) {
					nestedScopes.add(scope) ;
				}
			}

			return nestedScopes ;
		}
	}

	/*
	 * Strategies for Receptions
	 */
	protected class ReceptionsNameStrategy implements AlfPartialScope.IGetNameStrategy {
		public String getName(EObject element) {
			if (element instanceof Signal)
				return ((Signal)element).getName();
			else
				return "Unexpected element kind..." ;
		}
	};

	protected class ReceptionsBuildScopeStrategy implements AlfPartialScope.IBuildScopeStrategy {
		public List<List<EObject>> buildScope(EObject contextElement) {
			List<List<EObject>> nestedScopes = new ArrayList<List<EObject>>() ;
			List<EObject> nestedList = new ArrayList<EObject>() ;

			// retrieves the owner of the contextOperation
			Classifier contextClassifier = AlfJavaValidator.getContextClassifier() ;
			if (! (contextClassifier instanceof org.eclipse.uml2.uml.Class)) {
				nestedScopes.add(nestedList) ;
				return nestedScopes ;
			}

			// add all the signals for which a Reception is defined
			for (org.eclipse.uml2.uml.Feature f : contextClassifier.allFeatures()) {
				if (f instanceof Reception)
					if (((Reception)f).getSignal() != null)
						nestedList.add(((Reception)f).getSignal()) ;
			}

			nestedScopes.add(nestedList) ;

			return nestedScopes ;
		}
	}

	/*
	 * Strategies for Variables, Parameters and Properties
	 */
	protected class VariableOrParameterOrPropertyNameStrategy implements AlfPartialScope.IGetNameStrategy {
		public String getName(EObject element) {
			if (element instanceof Property)
				return ((Property)element).getName();
			else if (element instanceof LocalNameDeclarationStatement){
				return ((LocalNameDeclarationStatement)element).getVarName() ;
			}
			else if (element instanceof InvocationOrAssignementOrDeclarationStatement) {
				InvocationOrAssignementOrDeclarationStatement statement = (InvocationOrAssignementOrDeclarationStatement)element ;
				return statement.getVariableDeclarationCompletion() != null ?
						statement.getVariableDeclarationCompletion().getVariableName() :
							"" ;
			}
			else if (element instanceof LoopVariableDefinition) {
				return ((LoopVariableDefinition)element).getName() ;
			}
			else if (element instanceof AcceptClause) {
				return ((AcceptClause)element).getName() ;
			}
			else if (element instanceof AcceptStatement) {
				return ((AcceptStatement)element).getClause().getName() ;
			}
			else if (element instanceof SequenceExpansionExpression) {
				SequenceExpansionExpression expression = (SequenceExpansionExpression)element ;
				return expression.getName() != null ?
						expression.getName() :
							"" ;
			}
			return "unexpected kind ..." ;
		}
	}

	protected abstract class VariableOrPropertyOrParameterBuildScopeStrategy implements AlfPartialScope.IBuildScopeStrategy {
		public abstract List<List<EObject>> buildScope(EObject contextElement) ;

		public boolean containsALocalNameDeclaration(DocumentedStatement previousDocumentStatement) {
			Statement statement = previousDocumentStatement.getStatement() ;
			if (statement instanceof LocalNameDeclarationStatement)
				return true ;
			if (statement instanceof InvocationOrAssignementOrDeclarationStatement) {
				//TODO : handle cases with implicit declarations, e.g., v = 14 ; // v is a variable of type Integer
				InvocationOrAssignementOrDeclarationStatement cddDclStatement = (InvocationOrAssignementOrDeclarationStatement)statement;
				if (cddDclStatement.getVariableDeclarationCompletion() != null) {
					return true ;
				}
			}
			if (statement instanceof AcceptStatement) {
				AcceptStatement cddDclStatement = (AcceptStatement)statement ;
				if (cddDclStatement.getSimpleAccept() != null && cddDclStatement.getClause().getName() != null)
					return true ;
			}
			return false;
		}

	}
	
	/*
	 *  Strategies for Classifiers
	 */
	protected class ClassifiersNameStrategy implements AlfPartialScope.IGetNameStrategy {
		public String getName(EObject element) {
			if (element instanceof Classifier)
				return ((Classifier)element).getName() ;
			else if (element instanceof ElementImport) {
				ElementImport imported = (ElementImport)element ;
				return imported.getAlias() != null ? imported.getAlias() : ((Classifier)imported.getImportedElement()).getName() ;
			}
			else 
				return "Unexpected element kind..." ; 
		}
	};

	protected class ClassifiersBuildScopeStrategy implements AlfPartialScope.IBuildScopeStrategy {
		public List<List<EObject>> buildScope(EObject contextElement) {
			List<List<EObject>> nestedScopes = new ArrayList<List<EObject>>() ;
			List<EObject> nestedList = new ArrayList<EObject>() ;

			// first checks if contextElement represents a Package. In this case, builds a scope containing all classifiers visible from this package
			Package potentialContextPackage = null ;
			if (contextElement instanceof Package)
				potentialContextPackage = (Package)contextElement ;
			else if (contextElement instanceof ElementImport) {
				ElementImport eImport = (ElementImport)contextElement ;
				if (eImport.getImportedElement() instanceof Package)
					potentialContextPackage = (Package)eImport.getImportedElement() ;
			}
			if (potentialContextPackage != null) {
				List<EObject> importedClassifiers = processPublicallyImportedClassifiers(potentialContextPackage) ;
				importedClassifiers = removeDuplicateClassifiers(importedClassifiers) ;
				nestedScopes.add(importedClassifiers) ;
				return nestedScopes ;
			}

			// retrieves the owner of the contextOperation
			Classifier contextClassifier = AlfJavaValidator.getContextClassifier() ;

			// add the context classifier at the first scoping level
			nestedList.add(contextClassifier) ;
			// retrieves all package imports, and add all classifiers available at the root of this package
			for (PackageImport i : contextClassifier.getPackageImports()) {
				Package importedPackage = i.getImportedPackage() ;
				nestedList.addAll(processPublicallyImportedClassifiers(importedPackage)) ;
			}
			// retrieves all element imports. For those which concern a Classifier, either add the classifier if no alias is defined, or the element import if an alias is defined
			for (ElementImport i : contextClassifier.getElementImports()) {
				PackageableElement e = i.getImportedElement() ;
				if (e instanceof Classifier) {
					if (i.getAlias() != null)
						nestedList.add(i) ;
					else
						nestedList.add(e) ;
				}
			}
			nestedList = removeDuplicateClassifiers(nestedList) ;
			nestedScopes.add(nestedList) ;

			// Then process the implicit import of alf library
			nestedList = new ArrayList<EObject>() ;
			if (AlfJavaValidator.getAlfStandardLibrary() != null) {
				List<EObject> importedClassifiers = processPublicallyImportedClassifiers(AlfJavaValidator.getAlfStandardLibrary()) ;

				importedClassifiers.add(TypeUtils._Collection.extractActualType()) ;
				importedClassifiers.add(TypeUtils._Set.extractActualType()) ;
				importedClassifiers.add(TypeUtils._Bag.extractActualType()) ;
				importedClassifiers.add(TypeUtils._Queue.extractActualType()) ;
				importedClassifiers.add(TypeUtils._OrderedSet.extractActualType()) ;
				importedClassifiers.add(TypeUtils._List.extractActualType()) ;
				importedClassifiers.add(TypeUtils._Deque.extractActualType()) ;
				importedClassifiers.add(TypeUtils._Map.extractActualType()) ;
				//importedClassifiers.add(TypeUtils._Entry.extractActualType()) ;

				nestedList.addAll(removeDuplicateClassifiers(importedClassifiers)) ;
				nestedScopes.add(nestedList) ;
			}

			// Then process all classifiers at the same namespace level than the context classifier
			nestedList = new ArrayList<EObject>() ;
			Namespace namespaceOfContextClassifier = contextClassifier.getNamespace() ;
			for (Element e : namespaceOfContextClassifier.getOwnedElements()) {
				if (e instanceof Classifier && e != contextClassifier)
					nestedList.add(e) ;
			}
			nestedScopes.add(nestedList) ;

			return nestedScopes ;
		}

		private List<EObject> processPublicallyImportedClassifiers (Package p){
			List<EObject> importedClassifiers = new ArrayList<EObject>() ;
			for (NamedElement n : p.getOwnedMembers()) {
				if (n instanceof Classifier)
					if (((Classifier)n).getVisibility() != VisibilityKind.PRIVATE_LITERAL) {

						importedClassifiers.add(n) ;
					}
			}
			for (ElementImport eImport : p.getElementImports()) {
				if (eImport.getVisibility()!= VisibilityKind.PRIVATE_LITERAL) {
					PackageableElement element = eImport.getImportedElement() ;
					if (element instanceof Classifier) {

						if (eImport.getAlias() != null) {
							importedClassifiers.add(eImport) ;
						}
						else {
							importedClassifiers.add(element) ;
						}

					}
				}
			}
			for (PackageImport pImport : p.getPackageImports()) {
				if (pImport.getVisibility() != VisibilityKind.PRIVATE_LITERAL) {
					importedClassifiers.addAll(processPublicallyImportedClassifiers(pImport.getImportedPackage())) ;
				}
			}
			return importedClassifiers ;
		}
	}

	/*
	 * Strategies for Packages
	 */
	protected class PackageNameStrategy implements AlfPartialScope.IGetNameStrategy {
		public String getName(EObject element) {
			if (element instanceof Namespace)
				return ((Namespace)element).getName() ;
			else if (element instanceof ElementImport) {
				ElementImport eImport = (ElementImport)element ;
				return eImport.getAlias() != null ? eImport.getAlias() : eImport.getImportedElement().getName() ;
			}
			else
				return "Unexpected element kind..." ;
		}
	}

	protected class PackagesBuildScopeStrategy implements AlfPartialScope.IBuildScopeStrategy {
		public List<List<EObject>> buildScope(EObject contextElement) {

			List<List<EObject>> nestedScopes = new ArrayList<List<EObject>>() ;
			List<EObject> nestedList = new ArrayList<EObject>() ;

			if (contextElement instanceof Package) { // if the context element is a package, returns the scope related to this package.
				nestedScopes.add(processPublicallyImportedPackages((Package)contextElement)) ;
				return nestedScopes ;
			}

			Package root = (Package)AlfJavaValidator.getModel() ;

			// first process packages directly imported by the context classifier (either package import or element import)
			Classifier contextClassifier = AlfJavaValidator.getContextClassifier() ;

			nestedList.addAll(processPublicallyImportedPackages(contextClassifier)) ;
			nestedScopes.add(nestedList) ;

			// Then process the implicit import of alf library
			nestedList = new ArrayList<EObject>() ;
			if (AlfJavaValidator.getAlfStandardLibrary() != null) {
				nestedList.add(AlfJavaValidator.getAlfStandardLibrary()) ;
				nestedList.addAll(processPublicallyImportedPackages(AlfJavaValidator.getAlfStandardLibrary())) ;
				nestedScopes.add(nestedList) ;
			}

			// finally processes the root package
			nestedList = new ArrayList<EObject>() ;
			nestedList.add(root) ;
			nestedScopes.add(nestedList) ;

			return nestedScopes;
		}

		protected List<EObject> processPublicallyImportedPackages (Namespace namespace) {
			List<EObject> importedPackages = new ArrayList<EObject>() ;

			for (NamedElement ownedMember : namespace.getOwnedMembers()) {
				if (ownedMember.getVisibility() != VisibilityKind.PRIVATE_LITERAL && ownedMember instanceof Package)
					importedPackages.add(ownedMember) ;
			}
			for (ElementImport eImport : namespace.getElementImports()) {
				if (eImport.getVisibility() != VisibilityKind.PRIVATE_LITERAL ) {
					PackageableElement importedElement = eImport.getImportedElement() ;
					if (importedElement instanceof Package) {
						if (eImport.getAlias() != null)
							importedPackages.add(eImport) ;
						else
							importedPackages.add(importedElement) ;
					}
				}
			}
			for (PackageImport pImport : namespace.getPackageImports()) {
				if (pImport.getVisibility() != VisibilityKind.PRIVATE_LITERAL) {
					importedPackages.addAll(processPublicallyImportedPackages(pImport.getImportedPackage())) ;
				}
			}

			return importedPackages ;
		}
	}


	/*
	 * Strategies for Behaviors
	 */

	protected class BehaviorsNameStrategy implements AlfPartialScope.IGetNameStrategy {
		public String getName(EObject element) {
			if (element instanceof Behavior)
				return ((Behavior)element).getName() ;
			else if (element instanceof ElementImport) {
				ElementImport imported = (ElementImport)element ;
				return imported.getAlias() != null ? imported.getAlias() : ((Behavior)imported.getImportedElement()).getName() ;
			}
			else 
				return "Unexpected element kind..." ; 
		}
	};

	protected class BehaviorsBuildScopeStrategy implements AlfPartialScope.IBuildScopeStrategy {
		public List<List<EObject>> buildScope(EObject contextElement) {
			List<List<EObject>> nestedScopes = new ArrayList<List<EObject>>() ;
			List<EObject> nestedList = new ArrayList<EObject>() ;

			// first checks if contextElement represents a Package. In this case, builds a scope containing all behaviors visible from this package
			Package potentialContextPackage = null ;
			if (contextElement instanceof Package)
				potentialContextPackage = (Package)contextElement ;
			else if (contextElement instanceof ElementImport) {
				ElementImport eImport = (ElementImport)contextElement ;
				if (eImport.getImportedElement() instanceof Package)
					potentialContextPackage = (Package)eImport.getImportedElement() ;
			}
			if (potentialContextPackage != null) {
				nestedScopes.add(processPublicallyImportedBehaviors(potentialContextPackage)) ;
				return nestedScopes ;
			}

			// retrieves the contextOperation (i.e., the operation edited by the current editor)
			// retrieves the owner of the contextOperation
			Classifier contextClassifier = AlfJavaValidator.getContextClassifier() ;

			// retrieves all package imports, and add all behaviors available at the root of this package
			for (PackageImport i : contextClassifier.getPackageImports()) {
				Package importedPackage = i.getImportedPackage() ;
				nestedList.addAll(processPublicallyImportedBehaviors(importedPackage)) ;
			}
			// retrieves all element imports. For those which concern a Behavior, either add the behavior if no alias is defined, or the element import if an alias is defined
			for (ElementImport i : contextClassifier.getElementImports()) {
				PackageableElement e = i.getImportedElement() ;
				if (e instanceof Classifier) {
					if (i.getAlias() != null)
						nestedList.add(i) ;
					else
						nestedList.add(e) ;
				}
			}
			nestedList = removeDuplicateClassifiers(nestedList) ;
			nestedScopes.add(nestedList) ;

			// Then process the implicit import of alf library
			nestedList = new ArrayList<EObject>() ;
			if (AlfJavaValidator.getAlfStandardLibrary() != null) {
				List<EObject> importedClassifiers = processPublicallyImportedBehaviors(AlfJavaValidator.getAlfStandardLibrary()) ;
				importedClassifiers = removeDuplicateClassifiers(importedClassifiers) ;
				nestedList.addAll(importedClassifiers) ;
				nestedScopes.add(nestedList) ;
			}

			return nestedScopes ;
		}

		private List<EObject> processPublicallyImportedBehaviors (Package p){
			List<EObject> importedBehaviors = new ArrayList<EObject>() ;
			for (NamedElement n : p.getOwnedMembers()) {
				if (n instanceof Behavior) {
					if (((Behavior)n).getVisibility() != VisibilityKind.PRIVATE_LITERAL)
						importedBehaviors.add(n) ;
				}
				else if (n instanceof Package) {
					importedBehaviors.addAll(processPublicallyImportedBehaviors((Package)n)) ;
				}
			}
			for (ElementImport eImport : p.getElementImports()) {
				if (eImport.getVisibility()!= VisibilityKind.PRIVATE_LITERAL) {
					PackageableElement element = eImport.getImportedElement() ;
					if (element instanceof Behavior) {
						if (eImport.getAlias() != null)
							importedBehaviors.add(eImport) ;
						else
							importedBehaviors.add(element) ;
					}
				}
			}
			for (PackageImport pImport : p.getPackageImports()) {
				if (pImport.getVisibility() != VisibilityKind.PRIVATE_LITERAL) {
					importedBehaviors.addAll(processPublicallyImportedBehaviors(pImport.getImportedPackage())) ;
				}
			}
			return importedBehaviors ;
		}
	}



	protected class EnumerationLiteralNameStrategy implements AlfPartialScope.IGetNameStrategy {
		public String getName(EObject element) {
			if (element instanceof EnumerationLiteral) {
				return ((EnumerationLiteral)element).getName() ;
			}
			return "Unexpected element kind..." ;
		}
	}

	protected class EnumerationLiteralBuildScopeStrategy implements AlfPartialScope.IBuildScopeStrategy {
		public List<List<EObject>> buildScope(EObject contextElement) {
			List<List<EObject>> nestedScopes = new ArrayList<List<EObject>>() ;
			List<EObject> nestedList = new ArrayList<EObject>() ;

			if (!(contextElement instanceof Enumeration)) {
				nestedScopes.add(nestedList) ;
				return nestedScopes ;
			}
			Enumeration contextEnumeration = (Enumeration)contextElement ;

			// add all the literals owned by the context enumeration at the first scoping level
			nestedList.addAll(contextEnumeration.getOwnedLiterals()) ;
			nestedScopes.add(nestedList) ;
			// then builds other scoping levels based on context classifier inheritance hierarchy 

			List<Classifier> currentGenerals = new ArrayList<Classifier>() ;
			currentGenerals.addAll(contextEnumeration.getGenerals()) ;
			List<Classifier> nextGenerals ;
			while (!currentGenerals.isEmpty()) {
				nextGenerals = new ArrayList<Classifier>() ;
				nestedList = new ArrayList<EObject>() ;
				for (Classifier general : currentGenerals) {
					nextGenerals.addAll(general.getGenerals()) ;
					if (general instanceof Enumeration) {
						for (EnumerationLiteral enumLiteral : ((Enumeration)general).getOwnedLiterals())
							nestedList.add(enumLiteral) ;
					}
				}
				nestedScopes.add(nestedList) ;
								currentGenerals = nextGenerals ;
			}
			return nestedScopes ;
		}
	}


	public AlfPartialScope getVisibleEnumerationLiterals(EObject context) {
		AlfPartialScope.IGetNameStrategy nameStrategy = new EnumerationLiteralNameStrategy() ;
		AlfPartialScope.IBuildScopeStrategy buildScopeStrategy = new EnumerationLiteralBuildScopeStrategy() ;
		return new AlfPartialScope(nameStrategy, buildScopeStrategy, context) ;
	}

}

Back to the top