Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d21579e052499e026a9f7fca59cab6ef104215b3 (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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
/**
 *
 */
package javagen.umlparser;

import java.util.Arrays;
import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.uml2.uml.BehavioredClassifier;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.InterfaceRealization;
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.PackageableElement;
import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Realization;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * Utility methods.
 * <ul>
 * <li>getXxx() methods lookup for the element and create it if not found.</li>
 * <li>lookup() methods lookup for the element.</li>
 * <li>xxx( Resource, ...) methods lookup from the model container.</li>
 * <li>xxx( Package, ...) methods lookup from the specified package?</li>
 * </ul>
 *
 * The following methods can be used:
 * <ul>
 * <li>package.getPackagedElement( name, b, type, createOnDemand) : PackageableElement</li>
 * <li>package.getNestPacke()</li>
 * <li>package.getOwnedType(name, b, type, createOnDemand) : Type</li>
 * </ul>
 *
 * @author dumoulin
 *
 */
public class UmlUtils {

	private static final String WILDCARD = "*";

	/**
	 * Get the qualified name from a name.
	 * Name is splitted arround '.'
	 *
	 * @param name
	 * @return
	 */
	public static List<String> toQualifiedName(String qname) {
		String[] splittedName = qname.split("\\.");
		return Arrays.asList(splittedName);
	}

	/**
	 * Get the qualified name from a name.
	 * Name is splitted arround '/'
	 *
	 * @param name
	 * @return
	 */
	public static List<String> slashNameToQualifiedName(String qname) {
		String[] splittedName = qname.split("/");
		return Arrays.asList(splittedName);
	}

	/**
	 * Lookup for the specified package in the parent package. Create it if not found.
	 *
	 * @param parent
	 *            the containing package
	 * @param name
	 *            Package to get
	 * @return The requested package (never null)
	 */
	public static Package getPackage(Package parent, String name) {
		// Get or create an instance of the specified package.
		Package p = (Package) parent.getPackagedElement(name, false, UMLPackage.eINSTANCE.getPackage(), true);
		return p;
	}

	/**
	 * Lookup for the specified package in the parent package. Create it if not found.
	 *
	 * @param parent
	 *            the containing package
	 * @param name
	 *            Package to get
	 * @return The requested package (never null)
	 */
	public static Package getModel(Package parent, String name) {

		Package p = lookupPackage(parent, name);
		if (p == null)
		{
			// Create as model
			p = (Package) parent.getPackagedElement(name, false, UMLPackage.eINSTANCE.getModel(), true);
		}
		return p;
	}

	/**
	 * Lookup for the specified package in the parent package. Do not create it.
	 *
	 * @param parent
	 *            the containing package
	 * @param name
	 *            Package to get
	 * @return The requested package (never null)
	 */
	private static Package lookupPackage(Package parent, String name) {
		// Get or create an instance of the specified package.
		Package p = (Package) parent.getPackagedElement(name, false, UMLPackage.eINSTANCE.getPackage(), false);
		return p;
	}

	/**
	 * Lookup for the specified Namespace in the parent Namespace. Do not create it.
	 *
	 * @param parent
	 *            the containing package
	 * @param name
	 *            Package to get
	 * @return The requested package (never null)
	 */
	private static Namespace lookupNamespace(Namespace parent, String name) {
		// Get or create an instance of the specified package.
		NamedElement foundElement = parent.getOwnedMember(name);
		if (foundElement instanceof Namespace) {
			return (Namespace) foundElement;
		}
		// Not found
		return null;
	}


	/**
	 * Get the package containing the last package of the qualifiedName.
	 * Create any missing package if needed
	 * Do not lookup for the last element.
	 * If there is no scope package in the qualifiedName, return the root.
	 *
	 * @param root
	 * @param qualifiedName
	 * @return The containing package (never null)
	 */
	public static Package getContainingPackage(Package root, List<String> qualifiedName) {
		Package cur = root;
		for (int i = 0; i < qualifiedName.size() - 1; i++) {
			String name = qualifiedName.get(i);
			cur = getPackage(cur, name);
			// if(cur==null)
			// return null;
		}
		return cur;
	}

	/**
	 * Get the package containing the last package of the qualifiedName.
	 * Create any missing package if needed
	 * Do not lookup for the last element.
	 * If there is no scope package in the qualifiedName, return the root.
	 *
	 * @param root
	 * @param qualifiedName
	 * @return The containing package (never null)
	 */
	private static Package lookupContainingPackage(Package root, List<String> qualifiedName) {
		Package cur = root;
		for (int i = 0; i < qualifiedName.size() - 1; i++) {
			String name = qualifiedName.get(i);
			cur = lookupPackage(cur, name);
			if (cur == null) {
				return null;
			}
		}
		return cur;
	}

	/**
	 * Get the Namespace containing the last namespace of the qualifiedName.
	 * Do not lookup for the last element.
	 * If there is no scope package in the qualifiedName, return the root.
	 *
	 * @param parent
	 * @param qualifiedName
	 * @return The containing package (never null)
	 */
	private static Namespace lookupContainingNamespace(Namespace parent, List<String> qualifiedName) {
		Namespace cur = parent;
		for (int i = 0; i < qualifiedName.size() - 1; i++) {
			String name = qualifiedName.get(i);
			cur = lookupNamespace(parent, name);
			if (cur == null) {
				return null;
			}
		}
		return cur;
	}

	/**
	 * Get the package containing the {@value #WILDCARD}.
	 * Do not create intermediate packages.
	 * Do not lookup for the last element.
	 * The qualified name contains a {@value #WILDCARD}, lookup the package just before it.
	 *
	 * @param root
	 * @param qualifiedName
	 * @return The containing package or null if not found.
	 */
	public static Package lookupPackageBeforeWildcard(Package root, List<String> qualifiedName) {
		Package cur = root;
		for (int i = 0; i < qualifiedName.size(); i++) {
			String name = qualifiedName.get(i);
			// Stop if we encounter the wilcard.
			if (WILDCARD.equals(name)) {
				break;
			}

			cur = lookupPackage(cur, name);
			if (cur == null) {
				return null;
			}
		}
		return cur;
	}


	/**
	 * Get the package containing the last package of the qualifiedName.
	 * Create any missing package if needed
	 * Do not lookup for the last element.
	 * If there is no scope package in the qualifiedName, return the root.
	 *
	 * @param model
	 * @param qualifiedName
	 * @return The containing package (never null)
	 */
	public static Package getContainingPackage(Resource model, List<String> qualifiedName) {
		Package cur = getNamedElement(model, UMLPackage.eINSTANCE.getPackage(), qualifiedName.get(0));

		for (int i = 1; i < qualifiedName.size() - 1; i++) {
			String name = qualifiedName.get(i);
			cur = getPackage(cur, name);
			// if(cur==null)
			// return null;
		}
		return cur;
	}

	/**
	 * Get the package containing the last package of the qualifiedName.
	 * Create any missing package if needed
	 * Do not lookup for the last element.
	 * If there is no scope package in the qualifiedName, return the root.
	 *
	 * @param model
	 * @param qualifiedName
	 * @return The containing package or null if not found.
	 */
	private static Package lookupContainingPackage(Resource model, List<String> qualifiedName) {
		Package cur = lookupNamedElement(model, UMLPackage.eINSTANCE.getPackage(), qualifiedName.get(0));
		if (cur == null) {
			return null;
		}

		for (int i = 1; i < qualifiedName.size() - 1; i++) {
			String name = qualifiedName.get(i);
			cur = lookupPackage(cur, name);
			if (cur == null) {
				return null;
			}
		}
		return cur;
	}

	/**
	 * Get the Package corresponding to the qname.
	 * Create all missing package
	 *
	 * @param qualifiedName
	 * @return The requested package (never null)
	 */
	public static Package getPackage(Resource model, List<String> qualifiedName) {
		Package cur = getNamedElement(model, UMLPackage.eINSTANCE.getPackage(), qualifiedName.get(0));

		for (int i = 1; i < qualifiedName.size(); i++) {
			String name = qualifiedName.get(i);
			cur = getPackage(cur, name);
			// if(cur==null)
			// return null;
		}
		return cur;
	}

	/**
	 * Lookup the Package corresponding to the qname in specified model resource..
	 *
	 * @param qualifiedName
	 * @return The requested package or null if not found
	 */
	public static Package lookupPackage(Resource model, List<String> qualifiedName) {
		Package cur = lookupNamedElement(model, UMLPackage.eINSTANCE.getPackage(), qualifiedName.get(0));
		if (cur == null) {
			return null;
		}

		for (int i = 1; i < qualifiedName.size(); i++) {
			String name = qualifiedName.get(i);
			cur = lookupPackage(cur, name);
			if (cur == null) {
				return null;
			}
		}
		return cur;
	}

	/**
	 * Get the Package corresponding to the qname.
	 * Create all missing package
	 *
	 * @param parent
	 * @param qualifiedName
	 * @return The requested package (never null)
	 */
	public static Package getPackage(Package parent, List<String> qualifiedName) {
		Package cur = parent;
		for (int i = 0; i < qualifiedName.size(); i++) {
			String name = qualifiedName.get(i);
			cur = getPackage(cur, name);
			// if(cur==null)
			// return null;
		}
		return cur;
	}

	/**
	 * Get the Model corresponding to the qname.
	 * Create all missing package.
	 *
	 * @param parent
	 * @param qualifiedName
	 * @return The requested package (never null)
	 */
	public static Package getModel(Package parent, List<String> qualifiedName) {
		Package cur = parent;

		// Get the intermediate as Package
		int i = 0;
		for (i = 0; i < qualifiedName.size() - 1; i++) {
			String name = qualifiedName.get(i);
			cur = getPackage(cur, name);
		}
		// Get the last one as Model
		String name = qualifiedName.get(i);
		cur = getModel(cur, name);

		return cur;
	}

	/**
	 * Get the Package corresponding to the qname.
	 *
	 * @param parent
	 * @param qualifiedName
	 * @return The requested package or null if not found
	 */
	public static Package lookupPackage(Package parent, List<String> qualifiedName) {
		Package cur = parent;
		for (int i = 0; i < qualifiedName.size(); i++) {
			String name = qualifiedName.get(i);
			cur = lookupPackage(cur, name);
			if (cur == null) {
				return null;
			}
		}
		return cur;
	}

	/**
	 * get (lookup or create) the specified NamedElement with the requested type
	 *
	 * @param <R>
	 *            should be equals to expected type.
	 * @param model
	 * @param expectedType
	 * @param name
	 * @return
	 */
	protected static <R extends NamedElement> R getNamedElement(Resource model, EClass expectedType, String name) {

		for (Object cur : model.getContents()) {
			// System.out.println("Compare " + cur.getClass().getName() + "-->" + name );
			if (expectedType.getInstanceClass().isInstance(cur) && name.equals(((NamedElement) cur).getName())) {
				// System.out.println("found !!!");
				return (R) cur;
			}
		}

		// Create it
		R res = (R) UMLFactory.eINSTANCE.create(expectedType);
		res.setName(name);
		model.getContents().add(res);
		return res;
	}

	/**
	 * Lookup for the specified NamedElement with the requested type from the model container.
	 *
	 * @param <R>
	 * @param model
	 * @param expectedType
	 * @param name
	 * @return
	 */
	protected static <R extends NamedElement> R lookupNamedElement(Resource model, EClass expectedType, String name) {

		for (Object cur : model.getContents()) {
			if (expectedType.isInstance(cur) && name.equals(((NamedElement) cur).getName())) {
				return (R) cur;
			}
		}

		return null;
	}


	/**
	 * Lookup or create the requested qualifiedName. Create it with the specified type.
	 *
	 * @param parentPackage
	 * @param generalQualifiedName
	 * @param expectedType
	 * @return
	 */
	public static <R extends Type> R getClassifier(Package parent, List<String> qualifiedName, EClass expectedType) {
		// Get or create containing packages
		Package p = getContainingPackage(parent, qualifiedName);
		// Use the last name to create the element
		return (R) p.getOwnedType(qualifiedName.get(qualifiedName.size() - 1), false, expectedType, true);
	}

	/**
	 * Lookup the corresponding classifier to the qname in specified model resource..
	 *
	 * @param qualifiedName
	 * @return The requested package or null if not found
	 */
	public static <R extends Type> R lookupClassifier(Resource model, List<String> qualifiedName, EClass expectedType) {
		Package p = lookupContainingPackage(model, qualifiedName);
		if (p == null) {
			return null;
		}

		return (R) p.getOwnedType(qualifiedName.get(qualifiedName.size() - 1), false, expectedType, false);
	}

	/**
	 * Lookup for the classifier with specified qualifiedName in specified model resource..
	 *
	 * @param qualifiedName
	 * @return The requested package or null if not found
	 */
	public static <R extends Type> R lookupClassifierWithWildcard(Resource model, List<String> qualifiedName, EClass expectedType) {
		Package p = lookupContainingPackage(model, qualifiedName);
		if (p == null) {
			return null;
		}

		return (R) p.getOwnedType(qualifiedName.get(qualifiedName.size() - 1), false, expectedType, false);
	}


	/**
	 * Lookup for the classifier with specified qualifiedName in specified model resource..
	 * Don't Create it .
	 * The qualifiedName can contain a wildcard "*".
	 *
	 * @param parentPackage
	 * @param generalQualifiedName
	 * @param expectedType
	 * @return Found classifier or null
	 */
	public static <R extends Type> R lookupClassifierWithWildcard(Package parent, List<String> qualifiedName, EClass expectedType) {

		int wildcardIndex = qualifiedName.indexOf(WILDCARD);
		// If there is no wildcard, use more simple implementation.
		if (wildcardIndex < 0) {
			return lookupClassifier(parent, qualifiedName, expectedType);
		}

		// Get containing package till the WILDCARD
		Package p = lookupPackageBeforeWildcard(parent, qualifiedName);
		// If not found, return
		if (p == null) {
			return null;
		}

		// Use the last name to lookup the element
		// Look for existing packages at the place of the wildcard.
		List<Package> existingPackages = p.getNestedPackages();
		// Get the remaining path from ]index, size]
		List<String> remainingPath = qualifiedName.subList(wildcardIndex + 1, qualifiedName.size());
		// Now continue searching from the existing packages
		for (Package nestedPackage : existingPackages) {

			R r = lookupClassifier(nestedPackage, qualifiedName, expectedType);
			if (r != null) {
				return r;
			}
		}

		// Nothing found
		return null;
	}

	/**
	 * Lookup the requested qualifiedName. Don't Create it .
	 *
	 * @param parentPackage
	 * @param generalQualifiedName
	 * @param expectedType
	 * @return Found classifier or null
	 */
	public static <R extends Type> R lookupClassifier(Package parent, List<String> qualifiedName, EClass expectedType) {
		// Get containing package
		Package p = lookupContainingPackage(parent, qualifiedName);
		if (p == null) {
			return null;
		}
		// Use the last name to create the element
		return (R) p.getOwnedType(qualifiedName.get(qualifiedName.size() - 1), false, expectedType, false);
	}

	/**
	 * Lookup the requested qualifiedName. Don't Create it .
	 *
	 * @param parentPackage
	 * @param generalQualifiedName
	 * @param expectedType
	 * @return Found classifier or null
	 */
	public static <R extends Classifier> R lookupClassifier(Namespace parent, List<String> qualifiedName, EClass expectedType) {
		// Get containing package
		Namespace p = lookupContainingNamespace(parent, qualifiedName);
		if (p == null) {
			return null;
		}

		parent = p;
		String shortname = qualifiedName.get(qualifiedName.size() - 1);
		Classifier result;

		if (parent instanceof Package)
		{
			result = (Classifier) ((Package) parent).getOwnedType(shortname, false, expectedType, false);

		}
		else if (parent instanceof Interface)
		{
			result = ((Interface) parent).getNestedClassifier(shortname, false, expectedType, false);
		}
		else if (parent instanceof Class)
		{
			result = ((Class) parent).getNestedClassifier(shortname, false, expectedType, false);
		}
		else
		{
			// Should never happen !
			return null;
		}

		return (R) result;
	}

	/**
	 * Lookup for the specified package in the parent package. Create it if not found.
	 *
	 * @param parent
	 * @param name
	 *            Package to found
	 * @return
	 */
	public static Class getClass(Package parent, String name) {
		// Get or create an instance of the specified package.
		Class p = (Class) parent.getOwnedType(name, false, UMLPackage.eINSTANCE.getClass_(), true);
		return p;
	}

	/**
	 * Get the specified class from the specified parent.
	 * Missing package and the class are created if needed.
	 *
	 * @param parent
	 * @param qualifiedName
	 * @return
	 */
	public static Class getClass(Package parent, List<String> qualifiedName) {
		Package p = getContainingPackage(parent, qualifiedName);
		// Use the last name to create the element
		return getClass(p, qualifiedName.get(qualifiedName.size() - 1));
	}

	/**
	 *
	 * @param enclosingParents
	 *            list of enclosing parent, from the most outerside to the most inner side.
	 * @param name
	 * @return
	 */
	public static Class getClass(List<Namespace> enclosingParents, String name) {

		EClass type = UMLPackage.eINSTANCE.getClass_();
		Class result = (Class) getClassifier(enclosingParents, name, type);

		return result;
	}

	/**
	 * Get or create a Classifier by its name. The type of the classifier can be Class or Interface
	 * Lookup is done in the provided namespaces, using the short name.
	 * The classifier is expected to be in the directly enclosing parent, but lookup is done in all enclosing parents.
	 * If the position is not the one expected, the found classifier is moved to the enclosing parent.
	 *
	 * If not found, create the classifier in the directly enclosing namespace.
	 * If found, correct the classifier parent to be the directly enclosing namespace. Eventually correct the type
	 * if it doesn't match.
	 *
	 * @param enclosingParents
	 * @param name
	 * @param type
	 * @return
	 */
	private static Classifier getClassifier(List<Namespace> enclosingParents, String name, EClass type) {
		Classifier result = null;
		int parentsCount = enclosingParents.size();
		// Get the direct parent
		Namespace parent = enclosingParents.get(parentsCount - 1);

		// Look in enclosing parents
		for (int i = parentsCount - 1; i >= 0; i--)
		{
			Namespace namespace = enclosingParents.get(i);

			// Lookup for the exact type
			result = (Classifier) namespace.getOwnedMember(name, false, type);
			if (result == null)
			{
				// Lookup for the other type
				result = (Classifier) namespace.getOwnedMember(name, false, UMLPackage.eINSTANCE.getClassifier());
			}

			// Here, the result should have the correct type
			if (result != null)
			{
				// Change type if needed
				if (result.eClass() != type) {
					System.err.println("Classifier type need to be corrected for (" + result.getQualifiedName() + ")");
					// Correct the classifier type
					result = (Classifier) transformInto(result, type);
				}
				// Check if parent need to be corrected
				if (result.getOwner() != parent) {

					// Correct the parent
					setClassifierOwner(result, parent);
					// Get the transformed element.
				}

				// Found, return it
				return result;
			}
		}

		// Not found, create in the direct parent.

		if (parent instanceof Package)
		{
			result = (Classifier) ((Package) parent).getOwnedType(name, false, type, true);

		}
		else if (parent instanceof Interface)
		{
			result = ((Interface) parent).createNestedClassifier(name, type);
		}
		else if (parent instanceof Class)
		{
			result = ((Class) parent).createNestedClassifier(name, type);
		}
		else
		{
			// Should never happen !
			// The first namespace is always the package.
			Package parentPackage = (Package) enclosingParents.get(0);
			result = (Classifier) parentPackage.getOwnedType(name, false, type, true);
		}
		return result;
	}

	/**
	 * Change the owner of the classifier
	 * 
	 * @param classifier
	 *            The classifier to change the parent
	 * @param newParent
	 *            The parent to set.
	 */
	private static void setClassifierOwner(Classifier classifier, Namespace newParent) {
		// Not found, create in the direct parent.
		if (newParent instanceof Package)
		{
			classifier.setPackage((Package) newParent);

		}
		else if (newParent instanceof Interface)
		{

			((Interface) newParent).getNestedClassifiers().add(classifier);
		}
		else if (newParent instanceof Class)
		{
			((Class) newParent).getNestedClassifiers().add(classifier);
		}
		else
		{
			// Should never happen !
			// The first namespace is always the package.
			System.err.println("Don't know how to change classifier owner for " + newParent);
		}
	}

	/**
	 * Transform the element into the specified type
	 * 
	 * @param toTransform
	 *            Classifier to transform (Class or interface)
	 * @param type
	 *            Type to transform into
	 */
	private static EObject transformInto(Classifier toTransform, EClass type) {

		GenericTransformer transformer = new GenericTransformer(toTransform);
		EObject result = transformer.transform(type);
		return result;

	}

	/**
	 * Get or create a guessed Classifier by its name. The type of the classifier can be Class or Interface.
	 * A guessed classifier is a classifier specified by an attribute, a parameter, a return type ...
	 * First, lookup for a Classifier with the same name, and return it regardless of its type.
	 * If nothing is found, create a classifier with the specified type.
	 *
	 *
	 * If not found, create the classifier in the package (enclosingParent[0]).
	 * If found, return it.
	 *
	 * @param enclosingParents
	 * @param name
	 *            The short name
	 * @param type
	 *            the proposed type to use as a hint for creation.
	 * @return
	 */
	public static Classifier getGuessedClassifier(List<Namespace> enclosingParents, List<String> name, EClass type) {
		Classifier result = null;
		int parentsCount = enclosingParents.size();
		// Get the direct parent
		Namespace parent = enclosingParents.get(parentsCount - 1);

		// Look in enclosing parents
		for (int i = parentsCount - 1; i >= 0; i--)
		{
			Namespace namespace = enclosingParents.get(i);

			// Lookup for any type
			result = lookupClassifier(namespace, name, UMLPackage.eINSTANCE.getClassifier());
			// result = (Classifier)namespace.getOwnedMember(name, false, UMLPackage.eINSTANCE.getClassifier());
			if (result != null)
			{
				return result;
			}
		}

		// Not found, create in the direct parent.
		// The first namespace is always the package.
		Package parentPackage = (Package) enclosingParents.get(0);
		result = (Classifier) getClassifier(parentPackage, name, type);
		// result = (Classifier) ((Package)parentPackage).getOwnedType(name, false, type, true);

		return result;
	}

	/**
	 *
	 * @param parent
	 * @param name
	 * @return
	 */
	public static Interface getInterface(Package parent, String name) {
		Interface p = (Interface) parent.getOwnedType(name, false, UMLPackage.eINSTANCE.getInterface(), true);
		return p;
	}

	/**
	 *
	 * @param enclosingParents
	 *            list of enclosing parent, from the most outerside to the most inner side.
	 * @param name
	 * @return
	 */
	public static Interface getInterface(List<Namespace> enclosingParents, String name) {
		return (Interface) getClassifier(enclosingParents, name, UMLPackage.eINSTANCE.getInterface());
	}

	/**
	 * Create a Property and add it to the parent.
	 *
	 * @param parent
	 * @param name
	 * @param arrayCount
	 * @return
	 */
	public static Property createProperty(Classifier parent, Type type, String name, int arrayCount) {
		Property p;
		if (parent instanceof Class) {
			p = createProperty((Class) parent, type, name, arrayCount);
		} else if (parent instanceof Interface) {
			p = createProperty((Interface) parent, type, name, arrayCount);
		} else {
			return null;
		}

		p.setIsUnique(false);
		return p;
	}

	/**
	 * Create a property for the Class
	 *
	 * @param parent
	 * @param type
	 * @param name
	 * @param arrayCount
	 * @return
	 */
	public static Property createProperty(Class parent, Type type, String name, int arrayCount) {
		return parent.getOwnedAttribute(name, type, false, UMLPackage.eINSTANCE.getProperty(), true);
	}

	/**
	 * Create a property for the Class
	 *
	 * @param parent
	 * @param type
	 * @param name
	 * @param arrayCount
	 * @return
	 */
	public static Property createProperty(Interface parent, Type type, String name, int arrayCount) {
		return parent.getOwnedAttribute(name, type, false, UMLPackage.eINSTANCE.getProperty(), true);
	}

	/**
	 * Create a Generalization relation between the specified Classifier
	 *
	 * @param child
	 * @param general
	 */
	public static void getGeneralization(Classifier child, Classifier general) {
		child.getGeneralization(general, true);
		// child.createGeneralization(general);
	}

	/**
	 * Create a InterfaceRealization relation between the specified Classifier
	 *
	 * @param child
	 * @param general
	 */
	public static void getInterfaceRealization(BehavioredClassifier child, Interface general) {
		Realization res = lookupInterfaceRealization(child, general);
		if (res == null) {
			createInterfaceRealization(child, general);
		}
	}

	/**
	 * Lookup for the specified realization inside the package
	 *
	 * @param parent
	 * @param child
	 * @param general
	 * @return
	 */
	private static InterfaceRealization lookupInterfaceRealization(Classifier child, Classifier general) {
		for (Dependency ele : child.getClientDependencies()) {
			if (ele instanceof InterfaceRealization) {
				InterfaceRealization real = (InterfaceRealization) ele;
				if (real.getClients().contains(child) && real.getSuppliers().contains(general)) {
					return real;
				}
			}
		}
		return null;
	}

	/**
	 * Create a Generalization relation between the specified Classifier
	 *
	 * @param child
	 * @param general
	 */
	public static void createInterfaceRealization(BehavioredClassifier child, Interface general) {
		InterfaceRealization res = UMLFactory.eINSTANCE.createInterfaceRealization();

		res.setContract(general);
		res.setImplementingClassifier(child);

		// res.getClients().add(child);
		// res.getSuppliers().add(general);
		res.setName(child.getName() + " implements " + general.getName());
		// child.createGeneralization(general);
		// child.getClientDependencies().add(res);
	}


	/**
	 * Create a Generalization relation between the specified Classifier
	 *
	 * @param child
	 * @param general
	 */
	public static void getRealization(Package parent, Classifier child, Classifier general) {
		Realization res = lookupRealization(parent, child, general);
		if (res == null) {
			createRealization(parent, child, general);
		}
	}

	/**
	 * Lookup for the specified realization inside the package
	 *
	 * @param parent
	 * @param child
	 * @param general
	 * @return
	 */
	private static Realization lookupRealization(Package parent, Classifier child, Classifier general) {
		for (PackageableElement ele : parent.getPackagedElements()) {
			if (ele instanceof Realization) {
				Realization real = (Realization) ele;
				if (real.getClients().contains(child) && real.getSuppliers().contains(general)) {
					return real;
				}
			}
		}
		return null;
	}

	/**
	 * Create a Generalization relation between the specified Classifier
	 *
	 * @param child
	 * @param general
	 */
	public static void createRealization(Package parent, Classifier child, Classifier general) {
		Realization res = UMLFactory.eINSTANCE.createRealization();
		res.getClients().add(child);
		res.getSuppliers().add(general);
		res.setName(child.getName() + " extends " + general.getName());
		parent.getPackagedElements().add(res);
	}

	/**
	 * Create a primitive type.
	 *
	 * @param root
	 * @param typeName
	 */
	public static PrimitiveType getPrimitive(Package parent, String typeName) {
		return (PrimitiveType) parent.getOwnedType(typeName, true, UMLPackage.eINSTANCE.getPrimitiveType(), true);

	}

	/**
	 * Get the specified class from the specified parent.
	 * Missing package and the class are created if needed.
	 *
	 * @param parent
	 * @param qualifiedName
	 * @return
	 */
	public static PrimitiveType getPrimitive(Package parent, List<String> qualifiedName) {
		Package p = getContainingPackage(parent, qualifiedName);
		// Use the last name to create the element
		return getPrimitive(p, qualifiedName.get(qualifiedName.size() - 1));
	}

	/**
	 * Get the specified operation from the provided type.
	 *
	 * @param classifier
	 * @param name
	 * @return
	 */
	public static Operation getOperation(Classifier classifier, String name) {
		// TODO Auto-generated method stub
		List<Operation> opers = classifier.getOperations();
		for (Operation op : opers) {
			if (name.equals(op.getName())) {
				return op;
			}
		}

		Operation res = createOperation(classifier, name);
		return res;
	}

	/**
	 * Create an operation with the specified name.
	 *
	 * @param classifier
	 * @param name
	 * @return
	 */
	public static Operation createOperation(Classifier classifier, String name) {
		// Not found, create it
		Operation res = UMLFactory.eINSTANCE.createOperation();
		res.setName(name);
		if (classifier instanceof Class) {
			((Class) classifier).getOwnedOperations().add(res);
		} else if (classifier instanceof Interface) {
			((Interface) classifier).getOwnedOperations().add(res);
		} else if (classifier instanceof DataType) {
			((DataType) classifier).getOwnedOperations().add(res);
		}
		return res;
	}



}

Back to the top