Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 367b37f218c2efee91e7b77a7a3cc920c17fd11f (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
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
/*****************************************************************************
 * Copyright (c) 2010 Atos Origin.
 *
 *    
 * 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:
 *   Atos Origin - Initial API and implementation
 *	 Olivier Melois (Atos) : olivier.melois@atos.net - 371712
 *   Arthur Daussy (Atos) : arthur.daussy@atos.net - 371712
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.activity.helper;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.CopyCommand;
import org.eclipse.emf.edit.command.RemoveCommand;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.validation.AbstractModelConstraint;
import org.eclipse.emf.validation.EMFEventType;
import org.eclipse.emf.validation.IValidationContext;
import org.eclipse.emf.workspace.WorkspaceEditingDomainFactory;
import org.eclipse.gmf.runtime.common.core.util.Log;
import org.eclipse.gmf.runtime.diagram.ui.internal.DiagramUIPlugin;
import org.eclipse.gmf.runtime.diagram.ui.internal.DiagramUIStatusCodes;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.EditorUtils;
import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForEObject;
import org.eclipse.papyrus.uml.diagram.activity.commands.CreatePinToParameterLinkEAnnotation;
import org.eclipse.papyrus.uml.diagram.activity.edit.dialogs.ConfirmPinAndParameterSyncDialog;
import org.eclipse.papyrus.uml.diagram.activity.edit.dialogs.WarningAndCreateAttributeDialog;
import org.eclipse.papyrus.uml.diagram.activity.edit.dialogs.WarningAndCreateParameterDialog;
import org.eclipse.papyrus.uml.diagram.activity.edit.dialogs.WarningAndLinkDialog;
import org.eclipse.papyrus.uml.diagram.activity.edit.parts.BroadcastSignalActionEditPart;
import org.eclipse.papyrus.uml.diagram.activity.handlers.SynchronizePinsParametersHandler;
import org.eclipse.papyrus.uml.diagram.activity.helper.datastructure.LinkPinToParameter;
import org.eclipse.papyrus.uml.diagram.activity.part.CustomMessages;
import org.eclipse.papyrus.uml.diagram.activity.part.UMLDiagramEditorPlugin;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.uml2.common.util.CacheAdapter;
import org.eclipse.uml2.uml.Action;
import org.eclipse.uml2.uml.ActivityNode;
import org.eclipse.uml2.uml.AddStructuralFeatureValueAction;
import org.eclipse.uml2.uml.AddVariableValueAction;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.BroadcastSignalAction;
import org.eclipse.uml2.uml.CallAction;
import org.eclipse.uml2.uml.CallBehaviorAction;
import org.eclipse.uml2.uml.CallOperationAction;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.CreateObjectAction;
import org.eclipse.uml2.uml.DestroyObjectAction;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.InputPin;
import org.eclipse.uml2.uml.InvocationAction;
import org.eclipse.uml2.uml.LiteralInteger;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.ObjectNode;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.OutputPin;
import org.eclipse.uml2.uml.Parameter;
import org.eclipse.uml2.uml.ParameterDirectionKind;
import org.eclipse.uml2.uml.Pin;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.ReadStructuralFeatureAction;
import org.eclipse.uml2.uml.ReadVariableAction;
import org.eclipse.uml2.uml.SendObjectAction;
import org.eclipse.uml2.uml.SendSignalAction;
import org.eclipse.uml2.uml.Signal;
import org.eclipse.uml2.uml.StructuralFeature;
import org.eclipse.uml2.uml.StructuralFeatureAction;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.TypedElement;
import org.eclipse.uml2.uml.UMLFactory;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.ValueSpecification;
import org.eclipse.uml2.uml.Variable;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
 * The PinAndParameterSynchronizer is a validator (see corresponding extensions)
 * which ensure Pins and their corresponding (if a correspondance can be
 * established) Parameters are correctly synchronized
 * 
 */
public class PinAndParameterSynchronizer extends AbstractModelConstraint {

	private static final String RESULT_IN_READ_VARIABLE_ACTION = "result";

	private static final String VALUE_IN_ADD_VARIABLE_VALUE_ACTION = "value";

	private static final String INSERT_AT_IN_ADD_VARIABLE_VALUE_ACTION = "insertAt";

	private static final String TARGET_IN_DESTROY_OBJECT_ACTION = "target";

	private static final String VALUE_PIN_IN_STRUCTURAL_FEATURE_VALUE_ACTION = VALUE_IN_ADD_VARIABLE_VALUE_ACTION;

	private static final String RESULT_PIN_READ_SRTUCTURAL_ACTION = "result";

	private static final String OBJECT_PIN_IN_READS_STRUCTURAL_ACTION = "object";

	/** The label provider */
	private static final ILabelProvider labelProvider = new AdapterFactoryLabelProvider(UMLDiagramEditorPlugin.getInstance().getItemProvidersAdapterFactory());

	/** The constant to initialize target pin name */
	private static final String TARGET_PIN_INITIALIZATION_NAME = "target";

	/** The constant to initialize request pin name */
	private static final String REQUEST_PIN_INITIALIZATION_NAME = "request";

	/** The constant to initialize result pin name */
	private static final String RESULT_PIN_INITIALIZATION_NAME = "result";

	/**
	 * Validate modification and update associated elements if necessary
	 * 
	 * @see org.eclipse.emf.validation.AbstractModelConstraint#validate(org.eclipse.emf.validation.IValidationContext)
	 * 
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	@Override
	public IStatus validate(IValidationContext ctx) {
		try {
			EObject eObject = ctx.getTarget();
			// handle action creation separately not to confuse with case when
			// Behavior is modified
			/*********
			 * Done
			 *********/
			if((EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) && ctx.getFeatureNewValue() instanceof CallBehaviorAction) {
				// CallBehaviorAction created
				CompoundCommand cmd = getResetPinsCmd((CallAction)ctx.getFeatureNewValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
				/*********
				 * Done
				 *********/
			} else if((EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) && ctx.getFeatureNewValue() instanceof CallOperationAction) {
				// CallOperationAction created
				CompoundCommand cmd = getResetPinsCmd((CallAction)ctx.getFeatureNewValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
				/*********
				 * Done
				 *********/
			} else if((EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) && ctx.getFeatureNewValue() instanceof SendSignalAction) {
				// SendSignalAction created
				CompoundCommand cmd = getResetPinsCmd((SendSignalAction)ctx.getFeatureNewValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
				/*********
				 * Done
				 *********/
			} else if((EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) && ctx.getFeatureNewValue() instanceof SendObjectAction) {
				// SendObjectAction created
				CompoundCommand cmd = getResetPinsCmd((SendObjectAction)ctx.getFeatureNewValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
				/*********
				 * Done
				 *********/
			} else if((EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) && ctx.getFeatureNewValue() instanceof DestroyObjectAction) {
				// SendObjectAction created
				CompoundCommand cmd = getResetPinsCmd((DestroyObjectAction)ctx.getFeatureNewValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
				/*********
				 * Done
				 *********/
			} else if((EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) && (ctx.getFeatureNewValue() instanceof AddStructuralFeatureValueAction)) {
				// CreateObject Action created
				CompoundCommand cmd = getResetPinsCmd((AddStructuralFeatureValueAction)ctx.getFeatureNewValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
				/*********
				 * Done
				 *********/
			} else if((EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) && ctx.getFeatureNewValue() instanceof ReadStructuralFeatureAction) {
				// SendObjectAction created
				CompoundCommand cmd = getResetPinsCmd((ReadStructuralFeatureAction)ctx.getFeatureNewValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
				/*********
				 * Done
				 *********/
			} else if((EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) && ctx.getFeatureNewValue() instanceof AddVariableValueAction) {
				// SendObjectAction created
				CompoundCommand cmd = getResetPinsCmd((AddVariableValueAction)ctx.getFeatureNewValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
				/*********
				 * Done
				 *********/
			} else if((EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) && ctx.getFeatureNewValue() instanceof ReadVariableAction) {
				// SendObjectAction created
				CompoundCommand cmd = getResetPinsCmd((ReadVariableAction)ctx.getFeatureNewValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
				/*********
				 * Done
				 *********/
			} else if((EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) && ctx.getFeatureNewValue() instanceof BroadcastSignalAction) {
				// SendObjectAction created
				CompoundCommand cmd = getResetPinsCmd((InvocationAction)ctx.getFeatureNewValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
				/*********
				 * Done
				 *********/
			} else if((EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) && ctx.getFeatureNewValue() instanceof CreateObjectAction) {
				// CreateObject Action created
				CompoundCommand cmd = getResetPinsCmd((CreateObjectAction)ctx.getFeatureNewValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
			}
			/*********
			 * Done
			 *********/
			// handle Operation move for redefining target pin type
			if((EMFEventType.SET.equals(ctx.getEventType()) || EMFEventType.UNSET.equals(ctx.getEventType())) && UMLPackage.eINSTANCE.getOperation_Class().equals(ctx.getFeature()) && eObject instanceof Operation) {
				// Operation moved
				CompoundCommand cmd = getUpdateTargetPinsType((Operation)eObject);
				if(!cmd.isEmpty() && cmd.canExecute()) {
					if(askForValidation(getCallingActions((Operation)eObject))) {
						cmd.execute();
					} else {
						return ctx.createFailureStatus();
					}
				}
			}
			/*********
			 * Done
			 *********/
			if(eObject instanceof ValueSpecification) {
				// the value specification may be in an upperValue or lowerValue
				// replace values with appropriate ones
				EObject topValueSpec = eObject;
				while(topValueSpec.eContainer() instanceof ValueSpecification) {
					topValueSpec = topValueSpec.eContainer();
				}
				eObject = topValueSpec.eContainer();
			}
			/*********
			 * Done
			 *********/
			if(eObject instanceof Pin) {
				// special case for CallOperationAction target pin : no
				// synchronization, but type
				Element owner = ((Pin)eObject).getOwner();
				Pin target = null;
				if(owner instanceof CallOperationAction) {
					target = ((CallOperationAction)owner).getTarget();
				}
				if(target != null && target.equals(eObject)) {
					return handleTargetPinModification((Pin)eObject, ctx);
				} else {
					// Pin is modified, report modification on
					// Parameter/Property
					return handlePinModification((Pin)eObject, ctx);
				}
				/*********
				 * Done
				 *********/
			} else if(eObject instanceof Parameter) {
				// Parameter is modified, report modification on Pins
				return handleParameterModification((Parameter)eObject, ctx);
				/*********
				 * Done
				 *********/
			} else if(eObject instanceof Property) {
				// Property is modified, report modification on Pins
				return handlePropertyModification((Property)eObject, ctx);
				/*********
				 * Done
				 *********/
			} else if(eObject instanceof CallOperationAction) {
				// action is modified, ensure deleted/added Pin impact a
				// Parameter
				return handleCallOperationActionModification((CallOperationAction)eObject, ctx);
				/*********
				 * Done
				 *********/
			} else if(eObject instanceof Operation) {
				// Operation is modified, ensure deleted/added Parameter impact
				// Pins
				return handleOperationModification((Operation)eObject, ctx);
				/*********
				 * Done
				 *********/
			} else if(eObject instanceof CallBehaviorAction) {
				// action is modified, ensure deleted/added Pin impact a
				// Parameter
				return handleCallBehaviorActionModification((CallBehaviorAction)eObject, ctx);
				/*********
				 * Done
				 *********/
			} else if(eObject instanceof Behavior) {
				// Behavior is modified, ensure deleted/added Parameter impact
				// Pins
				return handleBehaviorModification((Behavior)eObject, ctx);
				/*********
				 * Done
				 *********/
			} else if(eObject instanceof SendSignalAction) {
				// action is modified, ensure deleted/added Pin impact a
				// Property
				return handleSendSignalActionModification((SendSignalAction)eObject, ctx);
				/*********
				 * Done
				 *********/
			} else if(eObject instanceof Signal) {
				// Signal is modified, ensure deleted/added Property impact Pins
				return handleSignalModification((Signal)eObject, ctx);
				/*********
				 * Done
				 *********/
			} else if(eObject instanceof SendObjectAction) {
				// action is modified, ensure deleted/added Pin are authorized
				return handleSendObjectActionModification((SendObjectAction)eObject, ctx);
				/*********
				 * Done
				 *********/
			} else if(eObject instanceof BroadcastSignalAction) {
				// action is modified, ensure deleted/added Pin impact a
				// Property
				return handleBroadcastSignalActionModification((BroadcastSignalAction)eObject, ctx);
			}
			return ctx.createSuccessStatus();
		} catch (RuntimeException rte) {
			// avoid throwing uncaught exception which would disable the
			// constraint
			Log.warning(DiagramUIPlugin.getInstance(), DiagramUIStatusCodes.IGNORED_EXCEPTION_WARNING, "Unexpected exception during Pin and Parameter synchronization : ", rte);
			// ensure that the constraint's failure does not prevent
			// modification
			return ctx.createSuccessStatus();
		}
	}

	/**
	 * Test if the Pin feature impacts the parameter or if the Parameter feature
	 * impacts pins
	 * 
	 * @param modifiedFeature
	 *        the feature to test
	 * @return true if the feature impacts the associated Parameter or Pin
	 *         objects
	 */
	protected boolean testPinOrParameterOrPropertyFeature(EStructuralFeature modifiedFeature) {
		boolean type = UMLPackage.eINSTANCE.getTypedElement_Type().equals(modifiedFeature);
		boolean ordering = UMLPackage.eINSTANCE.getMultiplicityElement_IsOrdered().equals(modifiedFeature);
		boolean multiplicity = UMLPackage.eINSTANCE.getMultiplicityElement_IsUnique().equals(modifiedFeature) || UMLPackage.eINSTANCE.getMultiplicityElement_Lower().equals(modifiedFeature) || UMLPackage.eINSTANCE.getMultiplicityElement_LowerValue().equals(modifiedFeature) || UMLPackage.eINSTANCE.getMultiplicityElement_Upper().equals(modifiedFeature) || UMLPackage.eINSTANCE.getMultiplicityElement_UpperValue().equals(modifiedFeature);
		boolean inAValueSpecification = ValueSpecification.class.isAssignableFrom(modifiedFeature.getContainerClass());
		return type || ordering || multiplicity || inAValueSpecification;
	}

	/**
	 * Test if the Action feature impacts the number of Pins and Parameters
	 * 
	 * @param modifiedFeature
	 *        the feature to test
	 * @return true if the feature impacts the number of Parameters or Pins
	 */
	protected boolean testActionFeature(EStructuralFeature modifiedFeature) {
		boolean input = UMLPackage.eINSTANCE.getInvocationAction_Argument().equals(modifiedFeature) || UMLPackage.eINSTANCE.getCallOperationAction_Target().equals(modifiedFeature);
		boolean output = UMLPackage.eINSTANCE.getCallAction_Result().equals(modifiedFeature);
		return input || output;
	}

	/**
	 * Ensure target Pin modification is correct
	 * 
	 * @param pin
	 *        modified pin
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleTargetPinModification(Pin pin, IValidationContext ctx) {
		// the type of the target pin can not be modified.
		if(UMLPackage.eINSTANCE.getTypedElement_Type().equals(ctx.getFeature())) {
			Element owner = pin.getOwner();
			if(owner instanceof CallOperationAction && ((CallOperationAction)owner).getOperation() != null) {
				proposeNavigation(((CallOperationAction)owner).getOperation());
				return ctx.createFailureStatus();
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Propose the user to create a parameter in the given element
	 * 
	 * @param element
	 *        element to navigate to or to create a parameter in
	 * @param preferredPinClass
	 *        the direction to select as default (or null)
	 */
	protected boolean proposeParameterCreation(final NamedElement element, final EClass preferredPinClass) {
		final String elementLabel = labelProvider.getText(element);
		final String message = NLS.bind(CustomMessages.PinAndParameterSynchronizer_UnauthorizedModification, elementLabel);
		final ParameterDirectionKind preferredDirection;
		if(UMLPackage.eINSTANCE.getOutputPin().isSuperTypeOf(preferredPinClass)) {
			preferredDirection = ParameterDirectionKind.OUT_LITERAL;
		} else {
			preferredDirection = ParameterDirectionKind.IN_LITERAL;
		}
		SafeDialogOpenerDuringValidation<Boolean> opener = new SafeDialogOpenerDuringValidation<Boolean>() {

			@Override
			protected Boolean openDialog() {
				WarningAndCreateParameterDialog dialog = new WarningAndCreateParameterDialog(new Shell(Display.getDefault()), CustomMessages.PinAndParameterSynchronizer_UnauthorizedModificationTitle, message, element, labelProvider, preferredDirection);
				boolean result = dialog.open() == Window.OK;
				if(result) {
					Parameter parameter = dialog.getParameter();
					handleParameterCreatedDuringValidation(parameter, preferredPinClass);
				}
				return result;
			}
		};
		return opener.execute();
	}

	/**
	 * Correct the model to add required pins, taking in account the parameter
	 * which has just been created with no validation feedback.
	 * 
	 * @param parameter
	 *        the created parameter
	 * @param preferredPinClass
	 *        the EClass the user would like to create a pin of
	 */
	protected void handleParameterCreatedDuringValidation(Parameter parameter, EClass preferredPinClass) {
		if(parameter != null) {
			Map<Integer, TypedElement> empty = Collections.emptyMap();
			CompoundCommand globalCmd = new CompoundCommand();
			// explore referencing actions
			List<InvocationAction> callingActions = getCallingActions(parameter.getOwner());
			switch(parameter.getDirection()) {
			case IN_LITERAL:
				for(InvocationAction action : callingActions) {
					if(action instanceof CallAction) {
						int index = action.getArguments().size();
						CompoundCommand cmd = getAddPinsCmd(action, Collections.singletonMap(index, (TypedElement)parameter), empty, preferredPinClass);
						globalCmd.append(cmd);
					}
				}
				break;
			case OUT_LITERAL:
			case RETURN_LITERAL:
				for(InvocationAction action : callingActions) {
					if(action instanceof CallAction) {
						int index = ((CallAction)action).getResults().size();
						CompoundCommand cmd = getAddPinsCmd(action, empty, Collections.singletonMap(index, (TypedElement)parameter), preferredPinClass);
						globalCmd.append(cmd);
					}
				}
				break;
			case INOUT_LITERAL:
				for(InvocationAction action : callingActions) {
					if(action instanceof CallAction) {
						int indexIn = action.getArguments().size();
						int indexOut = ((CallAction)action).getResults().size();
						CompoundCommand cmd = getAddPinsCmd(action, Collections.singletonMap(indexIn, (TypedElement)parameter), Collections.singletonMap(indexOut, (TypedElement)parameter), preferredPinClass);
						globalCmd.append(cmd);
					}
				}
				break;
			}
			if(!globalCmd.isEmpty() && globalCmd.canExecute()) {
				globalCmd.execute();
			}
		}
	}

	/**
	 * Propose the user to create an attribute in the given element
	 * 
	 * @param element
	 *        element to navigate to or to create an attribute in
	 * @param preferredPinClass
	 *        the direction to select as default (or null)
	 */
	protected boolean proposeAttributeCreation(final NamedElement element, final EClass preferredPinClass) {
		final String elementLabel = labelProvider.getText(element);
		final String message = NLS.bind(CustomMessages.PinAndParameterSynchronizer_UnauthorizedModification, elementLabel);
		SafeDialogOpenerDuringValidation<Boolean> opener = new SafeDialogOpenerDuringValidation<Boolean>() {

			@Override
			protected Boolean openDialog() {
				WarningAndCreateAttributeDialog dialog = new WarningAndCreateAttributeDialog(new Shell(Display.getDefault()), CustomMessages.PinAndParameterSynchronizer_UnauthorizedModificationTitle, message, element, labelProvider);
				boolean result = dialog.open() == Window.OK;
				if(result) {
					Property attribute = dialog.getAttribute();
					handlePropertyCreatedDuringValidation(attribute, preferredPinClass);
				}
				return result;
			}
		};
		return opener.execute();
	}

	/**
	 * Correct the model to add required pins, taking in account the property
	 * which has just been created with no validation feedback.
	 * 
	 * @param property
	 *        the created property
	 * @param preferredPinClass
	 *        the EClass the user would like to create a pin of
	 */
	protected void handlePropertyCreatedDuringValidation(Property property, EClass preferredPinClass) {
		if(property != null) {
			Map<Integer, Parameter> empty = Collections.emptyMap();
			CompoundCommand globalCmd = new CompoundCommand();
			// explore referencing actions
			List<InvocationAction> callingActions = getCallingActions(property.getOwner());
			for(InvocationAction action : callingActions) {
				if(action instanceof SendSignalAction || action instanceof BroadcastSignalActionEditPart) {
					int index = action.getArguments().size();
					CompoundCommand cmd = getAddPinsCmd(action, Collections.singletonMap(index, property), preferredPinClass);
					globalCmd.append(cmd);
				}
			}
			if(!globalCmd.isEmpty() && globalCmd.canExecute()) {
				globalCmd.execute();
			}
		}
	}

	/**
	 * Propose the user to navigate to the given element
	 * 
	 * @param element
	 *        element to navigate to
	 */
	protected void proposeNavigation(final NamedElement element) {
		final String elementLabel = labelProvider.getText(element);
		final String message = NLS.bind(CustomMessages.PinAndParameterSynchronizer_UnauthorizedModificationRedirection, elementLabel);
		SafeDialogOpenerDuringValidation<Void> opener = new SafeDialogOpenerDuringValidation<Void>() {

			@Override
			protected Void openDialog() {
				WarningAndLinkDialog dialog = new WarningAndLinkDialog(new Shell(Display.getDefault()), CustomMessages.PinAndParameterSynchronizer_UnauthorizedModificationTitle, message, element, elementLabel);
				dialog.open();
				return null;
			}
		};
		opener.execute();
	}

	/**
	 * Ensure Pin modification is in accordance with associated Parameter
	 * 
	 * @param pin
	 *        modified pin (not a target pin)
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handlePinModification(Pin pin, IValidationContext ctx) {
		if(EMFEventType.SET.equals(ctx.getEventType()) || EMFEventType.UNSET.equals(ctx.getEventType())) {
			if(testPinOrParameterOrPropertyFeature(ctx.getFeature())) {
				NamedElement invoked = getInvokedObject(pin);
				if(invoked != null) {
					proposeNavigation(invoked);
					return ctx.createFailureStatus();
				}
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Ensure Parameter modification is reported on associated Pins
	 * 
	 * @param parameter
	 *        modified parameter
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleParameterModification(Parameter parameter, IValidationContext ctx) {
		if(EMFEventType.SET.equals(ctx.getEventType()) || EMFEventType.UNSET.equals(ctx.getEventType())) {
			if(testPinOrParameterOrPropertyFeature(ctx.getFeature())) {
				// Update corresponding pins
				CompoundCommand cmd = getUpdatePinsCmd(getPins(parameter), parameter.getType(), parameter.isOrdered(), parameter.isUnique(), parameter.getLower(), parameter.getLowerValue(), parameter.getUpper(), parameter.getUpperValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					if(askForValidation(getCallingActions(parameter.getOwner()))) {
						cmd.execute();
					} else {
						return ctx.createFailureStatus();
					}
				}
			} else if(UMLPackage.eINSTANCE.getParameter_Direction().equals(ctx.getFeature())) {
				// Remove/Add corresponding pins with type in accordance to
				// direction
				for(Notification event : ctx.getAllEvents()) {
					if(UMLPackage.eINSTANCE.getParameter_Direction().equals(event.getFeature()) || UMLPackage.eINSTANCE.getBehavioralFeature_OwnedParameter().equals(event.getFeature())) {
						return changePinsBecauseOfParameterDirection(parameter, event, ctx);
					}
				}
			} else if(UMLPackage.eINSTANCE.getNamedElement_Name().equals(ctx.getFeature())) {
				// Synchronize the pin name if not set yet
				CompoundCommand cmd = getSetPinsNamesCmd(getPins(parameter), parameter.getName());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					if(askForValidation(getCallingActions(parameter.getOwner()))) {
						cmd.execute();
					} else {
						return ctx.createFailureStatus();
					}
				}
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Ensure Property modification is reported on associated Pins
	 * 
	 * @param property
	 *        modified property
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handlePropertyModification(Property property, IValidationContext ctx) {
		if(EMFEventType.SET.equals(ctx.getEventType()) || EMFEventType.UNSET.equals(ctx.getEventType())) {
			if(testPinOrParameterOrPropertyFeature(ctx.getFeature())) {
				// Update corresponding pins
				CompoundCommand cmd = getUpdatePinsCmd(getPins(property), property.getType(), property.isOrdered(), property.isUnique(), property.getLower(), property.getLowerValue(), property.getUpper(), property.getUpperValue());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					if(askForValidation(getCallingActions(property.getOwner()))) {
						cmd.execute();
					} else {
						return ctx.createFailureStatus();
					}
				}
			} else if(UMLPackage.eINSTANCE.getNamedElement_Name().equals(ctx.getFeature())) {
				// Synchronize the pin name if not set yet
				CompoundCommand cmd = getSetPinsNamesCmd(getPins(property), property.getName());
				if(!cmd.isEmpty() && cmd.canExecute()) {
					if(askForValidation(getCallingActions(property.getOwner()))) {
						cmd.execute();
					} else {
						return ctx.createFailureStatus();
					}
				}
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Change the Pins because parameter's direction has changed
	 * 
	 * @param parameter
	 *        the modified parameter
	 * @param event
	 *        the direction change event
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus changePinsBecauseOfParameterDirection(TypedElement parameter, Notification event, IValidationContext ctx) {
		// constants used for type safety
		List<Integer> emptyList = Collections.emptyList();
		Map<Integer, TypedElement> emptyMap = Collections.emptyMap();
		Object oldDir = event.getOldValue();
		Object newDir = event.getNewValue();
		int inIndex = getIndex(parameter, true);
		int outIndex = getIndex(parameter, false);
		CompoundCommand globalCmd = new CompoundCommand();
		List<InvocationAction> callingActions = getCallingActions(parameter.getOwner());
		if(ParameterDirectionKind.IN_LITERAL.equals(oldDir)) {
			if(ParameterDirectionKind.OUT_LITERAL.equals(newDir) || ParameterDirectionKind.RETURN_LITERAL.equals(newDir)) {
				// explore referencing actions to remove in and add out
				for(InvocationAction action : callingActions) {
					if(action instanceof CallAction) {
						CompoundCommand cmd = getRemovePinsCmd((CallAction)action, Collections.singletonList(inIndex), emptyList);
						globalCmd.append(cmd);
						cmd = getAddPinsCmd(action, emptyMap, Collections.singletonMap(outIndex, parameter), null);
						globalCmd.append(cmd);
					}
				}
			} else if(ParameterDirectionKind.INOUT_LITERAL.equals(newDir)) {
				// explore referencing actions to add out
				for(InvocationAction action : callingActions) {
					if(action instanceof CallAction) {
						CompoundCommand cmd = getAddPinsCmd(action, emptyMap, Collections.singletonMap(outIndex, parameter), null);
						globalCmd.append(cmd);
					}
				}
			}
		} else if(ParameterDirectionKind.OUT_LITERAL.equals(oldDir) || ParameterDirectionKind.RETURN_LITERAL.equals(oldDir)) {
			if(ParameterDirectionKind.IN_LITERAL.equals(newDir)) {
				// explore referencing actions to remove out and add in
				for(InvocationAction action : callingActions) {
					if(action instanceof CallAction) {
						CompoundCommand cmd = getRemovePinsCmd((CallAction)action, emptyList, Collections.singletonList(outIndex));
						globalCmd.append(cmd);
						cmd = getAddPinsCmd(action, Collections.singletonMap(inIndex, parameter), emptyMap, null);
						globalCmd.append(cmd);
					}
				}
			} else if(ParameterDirectionKind.INOUT_LITERAL.equals(newDir)) {
				// explore referencing actions to add in
				for(InvocationAction action : callingActions) {
					if(action instanceof CallAction) {
						CompoundCommand cmd = getAddPinsCmd(action, Collections.singletonMap(inIndex, parameter), emptyMap, null);
						globalCmd.append(cmd);
					}
				}
			}
		} else if(ParameterDirectionKind.INOUT_LITERAL.equals(oldDir)) {
			if(ParameterDirectionKind.IN_LITERAL.equals(newDir)) {
				// explore referencing actions to remove out
				for(InvocationAction action : callingActions) {
					if(action instanceof CallAction) {
						CompoundCommand cmd = getRemovePinsCmd((CallAction)action, emptyList, Collections.singletonList(outIndex));
						globalCmd.append(cmd);
					}
				}
			} else if(ParameterDirectionKind.OUT_LITERAL.equals(newDir) || ParameterDirectionKind.RETURN_LITERAL.equals(newDir)) {
				// explore referencing actions to remove in
				for(InvocationAction action : callingActions) {
					if(action instanceof CallAction) {
						CompoundCommand cmd = getRemovePinsCmd((CallAction)action, Collections.singletonList(inIndex), emptyList);
						globalCmd.append(cmd);
					}
				}
			}
		}
		if(!globalCmd.isEmpty() && globalCmd.canExecute()) {
			if(askForValidation(callingActions)) {
				globalCmd.execute();
			} else {
				return ctx.createFailureStatus();
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Get the list of actions invoking this element
	 * 
	 * @param element
	 *        an Operation or a Behavior or a Signal
	 * @return list of InvocationAction
	 */
	protected static List<InvocationAction> getCallingActions(Element element) {
		// explore referencing actions
		Set<InvocationAction> callingActions = new HashSet<InvocationAction>();
		if(element instanceof Behavior || element instanceof Operation) {
			Collection<Setting> references = CacheAdapter.getInstance().getNonNavigableInverseReferences(element);
			for(Setting ref : references) {
				EObject action = ref.getEObject();
				// parameter's owner is action's called Operation
				boolean operationCase = UMLPackage.eINSTANCE.getCallOperationAction_Operation().equals(ref.getEStructuralFeature());
				// parameter's owner is action's called Behavior
				boolean behaviorCase = UMLPackage.eINSTANCE.getCallBehaviorAction_Behavior().equals(ref.getEStructuralFeature());
				if((operationCase || behaviorCase) && action instanceof CallAction && action.eContainer() != null) {
					callingActions.add((CallAction)action);
				}
			}
		} else if(element instanceof Signal) {
			Collection<Setting> references = CacheAdapter.getInstance().getNonNavigableInverseReferences(element);
			for(Setting ref : references) {
				EObject action = ref.getEObject();
				// parameter's owner is action's sent Signal
				boolean signalCase = UMLPackage.eINSTANCE.getSendSignalAction_Signal().equals(ref.getEStructuralFeature());
				if(signalCase && action instanceof SendSignalAction && action.eContainer() != null) {
					callingActions.add((SendSignalAction)action);
				}
			}
		}
		return new ArrayList<InvocationAction>(callingActions);
	}

	/**
	 * Ensure CallOperationAction modification is reported on associated
	 * Operation
	 * 
	 * @param action
	 *        modified action
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleCallOperationActionModification(CallOperationAction action, IValidationContext ctx) {
		if(testTransformPinCase(ctx)) {
			return ctx.createSuccessStatus();
		} else if(testCustomModificationToValidPins(action, ctx)) {
			return ctx.createSuccessStatus();
		} else if(EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) {
			if(testActionFeature(ctx.getFeature()) && action.getOperation() != null) {
				if(action.getOperation() != null) {
					if(canCreateParameterFromCallAction(action)) {
						Object pin = ctx.getFeatureNewValue();
						boolean parameterCreated = proposeParameterCreation(action.getOperation(), ((EObject)pin).eClass());
						if(parameterCreated) {
							// remove the user-created value
							TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
							Command cmd = RemoveCommand.create(editingdomain, ctx.getFeatureNewValue());
							cmd.execute();
							return ctx.createSuccessStatus();
						}
					} else {
						/*
						 * No modification of parameters is allowed from the
						 * CallOperationAction. This means we can not add Pins
						 */
						proposeNavigation(action.getOperation());
					}
					return ctx.createFailureStatus();
				}
			}
		} else if(EMFEventType.REMOVE.equals(ctx.getEventType()) || EMFEventType.REMOVE_MANY.equals(ctx.getEventType())) {
			if(testActionFeature(ctx.getFeature()) && action.getOperation() != null) {
				/*
				 * Yet, no modification of parameters is allowed from the
				 * CallOperationAction. This means we can not remove Pins
				 */
				if(action.getOperation() != null) {
					proposeNavigation(action.getOperation());
					return ctx.createFailureStatus();
				}
			}
		} else if(EMFEventType.SET.equals(ctx.getEventType()) || EMFEventType.UNSET.equals(ctx.getEventType())) {
			if(UMLPackage.eINSTANCE.getCallOperationAction_Operation().equals(ctx.getFeature())) {
				/*
				 * The operation changes, so must the pins
				 */
				CompoundCommand cmd = getResetPinsCmd(action);
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
			}
			if(UMLPackage.eINSTANCE.getCallOperationAction_Target().equals(ctx.getFeature())) {
				/*
				 * Try to remove or assign target pin. This must not be
				 * authorized.
				 */
				final String msg = NLS.bind(CustomMessages.PinAndParameterSynchronizer_UndeleteablePinMessage, UMLPackage.eINSTANCE.getCallOperationAction_Target().getName());
				SafeDialogOpenerDuringValidation<Void> opener = new SafeDialogOpenerDuringValidation<Void>() {

					@Override
					protected Void openDialog() {
						MessageDialog.openWarning(new Shell(Display.getDefault()), CustomMessages.PinAndParameterSynchronizer_UndeleteablePinTitle, msg);
						return null;
					}
				};
				opener.execute();
				return ctx.createFailureStatus();
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * @param node
	 * @param ctx
	 * @return
	 */
	protected boolean testCustomModificationToValidPins(ActivityNode node, IValidationContext ctx) {
		// reflexive code to call each validation method matching the given object in : UMLValidationHelper.class
		// the method call rach public and static method
		IStatus status = null;
		Class<? extends UMLValidationHelper> aClass = UMLValidationHelper.class;
		Method[] methods = aClass.getDeclaredMethods();
		for(Method m : methods) {
			if(Modifier.isStatic(m.getModifiers()) && Modifier.isPublic(m.getModifiers()) && m.getReturnType() == IStatus.class) {
				if(m.isAnnotationPresent(PinAndParameterSynchronizeValidator.class) && m.getParameterTypes().length == 2) {
					if(m.getParameterTypes()[0].isInstance(node)) {
						try {
							status = (IStatus)m.invoke(aClass, node, ctx);
							if(status != null && !status.isOK()) {
								break;
							}
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		return status == null || status.isOK();
	}

	/**
	 * Ensure CallBehaviorAction modification is reported on associated Behavior
	 * 
	 * @param action
	 *        modified action
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleCallBehaviorActionModification(CallBehaviorAction action, IValidationContext ctx) {
		if(testTransformPinCase(ctx)) {
			return ctx.createSuccessStatus();
		} else if(EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) {
			if(testActionFeature(ctx.getFeature()) && action.getBehavior() != null) {
				if(action.getBehavior() != null) {
					if(canCreateParameterFromCallAction(action)) {
						Object pin = ctx.getFeatureNewValue();
						boolean parameterCreated = proposeParameterCreation(action.getBehavior(), ((EObject)pin).eClass());
						if(parameterCreated) {
							// remove the user-created value
							TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
							Command cmd = RemoveCommand.create(editingdomain, ctx.getFeatureNewValue());
							cmd.execute();
							return ctx.createSuccessStatus();
						}
					} else {
						/*
						 * No modification of parameters is allowed from the
						 * CallBehaviorAction. This means we can not add Pins
						 */
						proposeNavigation(action.getBehavior());
					}
					return ctx.createFailureStatus();
				}
			}
		} else if(EMFEventType.REMOVE.equals(ctx.getEventType()) || EMFEventType.REMOVE_MANY.equals(ctx.getEventType())) {
			if(testActionFeature(ctx.getFeature()) && action.getBehavior() != null) {
				/*
				 * Yet, no modification of parameters is allowed from the
				 * CallBehaviorAction. This means we can not remove Pins
				 */
				if(action.getBehavior() != null) {
					proposeNavigation(action.getBehavior());
					return ctx.createFailureStatus();
				}
			}
		} else if(EMFEventType.SET.equals(ctx.getEventType()) || EMFEventType.UNSET.equals(ctx.getEventType())) {
			if(UMLPackage.eINSTANCE.getCallBehaviorAction_Behavior().equals(ctx.getFeature())) {
				/*
				 * The behavior changes, so must the pins
				 */
				CompoundCommand cmd = getResetPinsCmd(action);
				if(action.getBehavior() != null) {
					String behaviorName = action.getBehavior().getName();
					if(behaviorName != null && !"".equals(behaviorName)) {
						// By the way, update the CallBehaviorAction name
						TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
						cmd.append(SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getNamedElement_Name(), behaviorName));
					}
				}
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Test if a parameter can be created from a call action, after appropriate
	 * warnings
	 * 
	 * @param action
	 *        the call action
	 * @return true if we can create a parameter
	 */
	protected boolean canCreateParameterFromCallAction(CallAction action) {
		return true;
	}

	/**
	 * Test if an attribute can be created from a send signal action, after
	 * appropriate warnings
	 * 
	 * @param action
	 *        the send signal action
	 * @return true if we can create an attribute
	 */
	protected boolean canCreateAttributesFromSendSignalAction(SendSignalAction action) {
		return true;
	}

	/**
	 * Ensure BroadcastSignalAction modification is reported on associated
	 * Signal
	 * 
	 * @param action
	 *        modified action
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleBroadcastSignalActionModification(BroadcastSignalAction action, IValidationContext ctx) {
		if(testTransformPinCase(ctx)) {
			return ctx.createSuccessStatus();
		} else if(EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) {
			if(testActionFeature(ctx.getFeature()) && action.getSignal() != null) {
				if(action.getSignal() != null) {
					Object pin = ctx.getFeatureNewValue();
					boolean attributeCreated = proposeAttributeCreation(action.getSignal(), ((EObject)pin).eClass());
					if(attributeCreated) {
						// remove the user-created value
						TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
						Command cmd = RemoveCommand.create(editingdomain, ctx.getFeatureNewValue());
						cmd.execute();
						return ctx.createSuccessStatus();
					}
					return ctx.createFailureStatus();
				}
			}
		} else if(EMFEventType.REMOVE.equals(ctx.getEventType()) || EMFEventType.REMOVE_MANY.equals(ctx.getEventType())) {
			if(testActionFeature(ctx.getFeature()) && action.getSignal() != null) {
				/*
				 * Yet, no modification of attributes is allowed from the
				 * BroadcastSignalAction. This means we can not remove Pins
				 */
				if(action.getSignal() != null) {
					proposeNavigation(action.getSignal());
					return ctx.createFailureStatus();
				}
			}
		} else if(EMFEventType.SET.equals(ctx.getEventType()) || EMFEventType.UNSET.equals(ctx.getEventType())) {
			if(UMLPackage.eINSTANCE.getBroadcastSignalAction_Signal().equals(ctx.getFeature())) {
				/*
				 * The signal changes, so must the pins
				 */
				CompoundCommand cmd = getResetPinsCmd(action);
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Ensure SendSignalAction modification is reported on associated Signal
	 * 
	 * @param action
	 *        modified action
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleSendSignalActionModification(SendSignalAction action, IValidationContext ctx) {
		if(testTransformPinCase(ctx)) {
			return ctx.createSuccessStatus();
		} else if(EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) {
			if(testActionFeature(ctx.getFeature()) && action.getSignal() != null) {
				if(action.getSignal() != null) {
					if(canCreateAttributesFromSendSignalAction(action)) {
						Object pin = ctx.getFeatureNewValue();
						boolean attributeCreated = proposeAttributeCreation(action.getSignal(), ((EObject)pin).eClass());
						if(attributeCreated) {
							// remove the user-created value
							TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
							Command cmd = RemoveCommand.create(editingdomain, ctx.getFeatureNewValue());
							cmd.execute();
							return ctx.createSuccessStatus();
						}
					} else {
						/*
						 * No modification of attributes is allowed from the
						 * SendSignalAction. This means we can not add Pins
						 */
						proposeNavigation(action.getSignal());
					}
					return ctx.createFailureStatus();
				}
			}
		} else if(EMFEventType.REMOVE.equals(ctx.getEventType()) || EMFEventType.REMOVE_MANY.equals(ctx.getEventType())) {
			if(testActionFeature(ctx.getFeature()) && action.getSignal() != null) {
				/*
				 * Yet, no modification of attributes is allowed from the
				 * SendSignalAction. This means we can not remove Pins
				 */
				if(action.getSignal() != null) {
					proposeNavigation(action.getSignal());
					return ctx.createFailureStatus();
				}
			}
		} else if(EMFEventType.SET.equals(ctx.getEventType()) || EMFEventType.UNSET.equals(ctx.getEventType())) {
			if(UMLPackage.eINSTANCE.getSendSignalAction_Signal().equals(ctx.getFeature())) {
				/*
				 * The signal changes, so must the pins
				 */
				CompoundCommand cmd = getResetPinsCmd(action);
				if(!cmd.isEmpty() && cmd.canExecute()) {
					cmd.execute();
				}
			}
			if(UMLPackage.eINSTANCE.getSendSignalAction_Target().equals(ctx.getFeature())) {
				/*
				 * Try to remove or assign target pin. This must not be
				 * authorized.
				 */
				final String msg = NLS.bind(CustomMessages.PinAndParameterSynchronizer_UndeleteablePinMessage, UMLPackage.eINSTANCE.getSendSignalAction_Target().getName());
				SafeDialogOpenerDuringValidation<Void> opener = new SafeDialogOpenerDuringValidation<Void>() {

					@Override
					protected Void openDialog() {
						MessageDialog.openWarning(new Shell(Display.getDefault()), CustomMessages.PinAndParameterSynchronizer_UndeleteablePinTitle, msg);
						return null;
					}
				};
				opener.execute();
				return ctx.createFailureStatus();
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Ensure SendObjectAction modification is authorized
	 * 
	 * @param action
	 *        modified action
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleSendObjectActionModification(SendObjectAction action, IValidationContext ctx) {
		if(testTransformPinCase(ctx)) {
			return ctx.createSuccessStatus();
		} else if(EMFEventType.SET.equals(ctx.getEventType()) || EMFEventType.UNSET.equals(ctx.getEventType())) {
			if(UMLPackage.eINSTANCE.getSendObjectAction_Target().equals(ctx.getFeature())) {
				/*
				 * Try to remove or assign target pin. This must not be
				 * authorized.
				 */
				final String msg = NLS.bind(CustomMessages.PinAndParameterSynchronizer_UndeleteablePinMessage, UMLPackage.eINSTANCE.getSendObjectAction_Target().getName());
				SafeDialogOpenerDuringValidation<Void> opener = new SafeDialogOpenerDuringValidation<Void>() {

					@Override
					protected Void openDialog() {
						MessageDialog.openWarning(new Shell(Display.getDefault()), CustomMessages.PinAndParameterSynchronizer_UndeleteablePinTitle, msg);
						return null;
					}
				};
				opener.execute();
				return ctx.createFailureStatus();
			}
			if(UMLPackage.eINSTANCE.getSendObjectAction_Request().equals(ctx.getFeature())) {
				/*
				 * Try to remove or assign target pin. This must not be
				 * authorized.
				 */
				final String msg = NLS.bind(CustomMessages.PinAndParameterSynchronizer_UndeleteablePinMessage, UMLPackage.eINSTANCE.getSendObjectAction_Request().getName());
				SafeDialogOpenerDuringValidation<Void> opener = new SafeDialogOpenerDuringValidation<Void>() {

					@Override
					protected Void openDialog() {
						MessageDialog.openWarning(new Shell(Display.getDefault()), CustomMessages.PinAndParameterSynchronizer_UndeleteablePinTitle, msg);
						return null;
					}
				};
				opener.execute();
				return ctx.createFailureStatus();
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Test whether the executing operation is a transform into on a pin
	 * 
	 * @param ctx
	 *        the validation context
	 * @return true if we are executing a transform into on a pin
	 */
	protected boolean testTransformPinCase(IValidationContext ctx) {
		List<Notification> events = ctx.getAllEvents();
		Object removed = null;
		Object added = null;
		if(events.size() == 1) {
			Notification set = events.get(0);
			if(Notification.SET == set.getEventType()) {
				removed = set.getOldValue();
				added = set.getNewValue();
			}
		} else if(events.size() == 2) {
			Notification remove = events.get(0);
			Notification add = events.get(1);
			if(Notification.REMOVE == remove.getEventType() && Notification.ADD == add.getEventType()) {
				if(remove.getPosition() == add.getPosition() && add.getFeature().equals(remove.getFeature())) {
					removed = remove.getOldValue();
					added = add.getNewValue();
				}
			} else if(Notification.REMOVE_MANY == remove.getEventType() && Notification.ADD_MANY == add.getEventType()) {
				if(Notification.NO_INDEX == remove.getPosition() && add.getFeature().equals(remove.getFeature())) {
					if(remove.getOldValue() instanceof List<?> && add.getNewValue() instanceof List<?>) {
						ArrayList<Object> removeList = new ArrayList<Object>((List<?>)remove.getOldValue());
						removeList.removeAll((List<?>)add.getNewValue());
						ArrayList<Object> addList = new ArrayList<Object>((List<?>)add.getNewValue());
						addList.removeAll((List<?>)remove.getOldValue());
						if(removeList.size() == 1 && addList.size() == 1) {
							removed = removeList.get(0);
							added = addList.get(0);
						}
					}
				}
			}
		}
		// check that replacing pin object is similar before concluding a
		// transform into
		if(removed instanceof Pin && added instanceof Pin) {
			Pin removedPin = (Pin)removed;
			Pin addedPin = (Pin)added;
			boolean similars = true;
			// test name
			similars &= removedPin.getName() == null || removedPin.getName().equals(addedPin.getName());
			// test type
			similars &= EcoreUtil.equals(removedPin.getType(), addedPin.getType());
			// test is ordered
			similars &= removedPin.isOrdered() == addedPin.isOrdered();
			// test multiplicity : is unique
			similars &= removedPin.isUnique() == addedPin.isUnique();
			// test multiplicity : lower value
			similars &= EcoreUtil.equals(removedPin.getLowerValue(), addedPin.getLowerValue());
			// test multiplicity : upper value
			similars &= EcoreUtil.equals(removedPin.getUpperValue(), addedPin.getUpperValue());
			return similars;
		}
		return false;
	}

	/**
	 * Ensure Operation modification is reported on associated actions
	 * 
	 * @param operation
	 *        modified operation
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleOperationModification(Operation operation, IValidationContext ctx) {
		if(EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) {
			if(UMLPackage.eINSTANCE.getBehavioralFeature_OwnedParameter().equals(ctx.getFeature())) {
				return handleParametersAdded(operation, ctx);
			}
		} else if(EMFEventType.REMOVE.equals(ctx.getEventType()) || EMFEventType.REMOVE_MANY.equals(ctx.getEventType())) {
			if(UMLPackage.eINSTANCE.getBehavioralFeature_OwnedParameter().equals(ctx.getFeature())) {
				return handleParametersRemoved(operation, ctx);
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Ensure Behavior modification is reported on associated actions
	 * 
	 * @param behavior
	 *        modified behavior
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleBehaviorModification(Behavior behavior, IValidationContext ctx) {
		if(EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) {
			if(UMLPackage.eINSTANCE.getBehavior_OwnedParameter().equals(ctx.getFeature())) {
				return handleParametersAdded(behavior, ctx);
			}
		} else if(EMFEventType.REMOVE.equals(ctx.getEventType()) || EMFEventType.REMOVE_MANY.equals(ctx.getEventType())) {
			if(UMLPackage.eINSTANCE.getBehavior_OwnedParameter().equals(ctx.getFeature())) {
				return handleParametersRemoved(behavior, ctx);
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Ensure Signal modification is reported on associated actions
	 * 
	 * @param signal
	 *        modified signal
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleSignalModification(Signal signal, IValidationContext ctx) {
		if(EMFEventType.ADD.equals(ctx.getEventType()) || EMFEventType.ADD_MANY.equals(ctx.getEventType())) {
			if(UMLPackage.eINSTANCE.getSignal_OwnedAttribute().equals(ctx.getFeature())) {
				return handleAttributesAdded(signal, ctx);
			}
		} else if(EMFEventType.REMOVE.equals(ctx.getEventType()) || EMFEventType.REMOVE_MANY.equals(ctx.getEventType())) {
			if(UMLPackage.eINSTANCE.getSignal_OwnedAttribute().equals(ctx.getFeature())) {
				return handleAttributesRemoved(signal, ctx);
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Ensure Pins corresponding to parameters are removed
	 * 
	 * @param element
	 *        modified operation or behavior
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleParametersRemoved(NamedElement element, IValidationContext ctx) {
		// construct the list of removed indexes and their direction
		Map<Integer, ParameterDirectionKind> removedParameterIndexes = new HashMap<Integer, ParameterDirectionKind>();
		for(Notification event : ctx.getAllEvents()) {
			if(UMLPackage.eINSTANCE.getBehavior_OwnedParameter().equals(event.getFeature()) || UMLPackage.eINSTANCE.getBehavioralFeature_OwnedParameter().equals(event.getFeature())) {
				Object removedValue = event.getOldValue();
				if(removedValue instanceof Parameter) {
					// handle parameter direction
					ParameterDirectionKind dir = ((Parameter)removedValue).getDirection();
					removedParameterIndexes.put(event.getPosition(), dir);
				} else if(removedValue instanceof List<?>) {
					List<?> col = (List<?>)removedValue;
					if(!col.isEmpty()) {
						for(int i = 0; i < col.size(); i++) {
							Object object = col.get(i);
							if(object instanceof Parameter) {
								removedParameterIndexes.put(i, ((Parameter)object).getDirection());
							}
						}
					}
				}
			}
		}
		List<Parameter> newParameters = Collections.emptyList();
		if(element instanceof Behavior) {
			newParameters = ((Behavior)element).getOwnedParameters();
		} else if(element instanceof Operation) {
			newParameters = ((Operation)element).getOwnedParameters();
		}
		List<Integer> removedInputPinIndexes = new LinkedList<Integer>();
		List<Integer> removedOutputPinIndexes = new LinkedList<Integer>();
		Iterator<Parameter> parametersIterator = newParameters.iterator();
		// iterate on the virtual list of old parameters
		// (correspondingParameterIndex) to deduce
		// pins indexes
		int correspondingParameterIndex = 0;
		int correspondingInputPinIndex = 0;
		int correspondingOutputPinIndex = 0;
		while(removedParameterIndexes.containsKey(correspondingParameterIndex) || parametersIterator.hasNext()) {
			if(removedParameterIndexes.containsKey(correspondingParameterIndex)) {
				// parameter removed, pin(s) removed
				switch(removedParameterIndexes.get(correspondingParameterIndex)) {
				case IN_LITERAL:
					removedInputPinIndexes.add(correspondingInputPinIndex);
					correspondingInputPinIndex++;
					break;
				case OUT_LITERAL:
				case RETURN_LITERAL:
					removedOutputPinIndexes.add(correspondingOutputPinIndex);
					correspondingOutputPinIndex++;
					break;
				case INOUT_LITERAL:
					// in-out parameter has two pins
					removedInputPinIndexes.add(correspondingInputPinIndex);
					correspondingInputPinIndex++;
					removedOutputPinIndexes.add(correspondingOutputPinIndex);
					correspondingOutputPinIndex++;
					break;
				}
			} else {
				// parameter not removed, pin(s) not removed
				Parameter nextParam = parametersIterator.next();
				switch(nextParam.getDirection()) {
				case IN_LITERAL:
					correspondingInputPinIndex++;
					break;
				case OUT_LITERAL:
				case RETURN_LITERAL:
					correspondingOutputPinIndex++;
					break;
				case INOUT_LITERAL:
					// in-out parameter has two pins
					correspondingInputPinIndex++;
					correspondingOutputPinIndex++;
					break;
				}
			}
			// explore next parameter
			correspondingParameterIndex++;
		}
		CompoundCommand globalCmd = new CompoundCommand();
		// explore referencing actions
		List<InvocationAction> callingActions = getCallingActions(element);
		for(InvocationAction action : callingActions) {
			if(action instanceof CallAction) {
				CompoundCommand cmd = getRemovePinsCmd((CallAction)action, removedInputPinIndexes, removedOutputPinIndexes);
				globalCmd.append(cmd);
			}
		}
		if(!globalCmd.isEmpty() && globalCmd.canExecute()) {
			if(askForValidation(callingActions)) {
				globalCmd.execute();
			} else {
				return ctx.createFailureStatus();
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Ensure Pins corresponding to parameters are added
	 * 
	 * @param element
	 *        modified operation or behavior
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleParametersAdded(NamedElement element, IValidationContext ctx) {
		// construct the list of added indexes and their direction
		List<Parameter> addedParameters = new LinkedList<Parameter>();
		for(Notification event : ctx.getAllEvents()) {
			if(UMLPackage.eINSTANCE.getBehavior_OwnedParameter().equals(event.getFeature()) || UMLPackage.eINSTANCE.getBehavioralFeature_OwnedParameter().equals(event.getFeature())) {
				Object addedValue = event.getNewValue();
				if(addedValue instanceof Parameter) {
					addedParameters.add((Parameter)addedValue);
				} else if(addedValue instanceof List<?>) {
					List<?> col = (List<?>)addedValue;
					if(!col.isEmpty()) {
						for(int i = 0; i < col.size(); i++) {
							Object object = col.get(i);
							if(object instanceof Parameter) {
								addedParameters.add((Parameter)object);
							}
						}
					}
				}
			}
		}
		List<Parameter> newParameters = Collections.emptyList();
		if(element instanceof Behavior) {
			newParameters = ((Behavior)element).getOwnedParameters();
		} else if(element instanceof Operation) {
			newParameters = ((Operation)element).getOwnedParameters();
		}
		Map<Integer, TypedElement> addedInputPinMap = new HashMap<Integer, TypedElement>();
		Map<Integer, TypedElement> addedOutputPinMap = new HashMap<Integer, TypedElement>();
		// iterate on the list of new parameters to deduce pins indexes
		int correspondingInputPinIndex = 0;
		int correspondingOutputPinIndex = 0;
		for(Parameter param : newParameters) {
			if(addedParameters.contains(param)) {
				// parameter added, pin(s) to add
				switch(param.getDirection()) {
				case IN_LITERAL:
					addedInputPinMap.put(correspondingInputPinIndex, param);
					correspondingInputPinIndex++;
					break;
				case OUT_LITERAL:
				case RETURN_LITERAL:
					addedOutputPinMap.put(correspondingOutputPinIndex, param);
					correspondingOutputPinIndex++;
					break;
				case INOUT_LITERAL:
					// in-out parameter has two pins
					addedInputPinMap.put(correspondingInputPinIndex, param);
					correspondingInputPinIndex++;
					addedOutputPinMap.put(correspondingOutputPinIndex, param);
					correspondingOutputPinIndex++;
					break;
				}
			} else {
				// parameter not added, pin already exists
				switch(param.getDirection()) {
				case IN_LITERAL:
					correspondingInputPinIndex++;
					break;
				case OUT_LITERAL:
				case RETURN_LITERAL:
					correspondingOutputPinIndex++;
					break;
				case INOUT_LITERAL:
					// in-out parameter has two pins
					correspondingInputPinIndex++;
					correspondingOutputPinIndex++;
					break;
				}
			}
			// explore next parameter
		}
		CompoundCommand globalCmd = new CompoundCommand();
		// explore referencing actions
		List<InvocationAction> callingActions = getCallingActions(element);
		for(InvocationAction action : callingActions) {
			if(action instanceof CallAction) {
				CompoundCommand cmd = getAddPinsCmd(action, addedInputPinMap, addedOutputPinMap, null);
				globalCmd.append(cmd);
			}
		}
		if(!globalCmd.isEmpty() && globalCmd.canExecute()) {
			if(askForValidation(callingActions)) {
				globalCmd.execute();
			} else {
				return ctx.createFailureStatus();
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Ensure Pins corresponding to parameters are removed
	 * 
	 * @param element
	 *        modified signal
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleAttributesRemoved(Signal element, IValidationContext ctx) {
		// construct the list of removed indexes and their direction
		Set<Integer> removedAttributeIndexes = new HashSet<Integer>();
		for(Notification event : ctx.getAllEvents()) {
			if(UMLPackage.eINSTANCE.getSignal_OwnedAttribute().equals(event.getFeature())) {
				Object removedValue = event.getOldValue();
				if(removedValue instanceof Property) {
					removedAttributeIndexes.add(event.getPosition());
				} else if(removedValue instanceof List<?>) {
					List<?> col = (List<?>)removedValue;
					if(!col.isEmpty()) {
						for(int i = 0; i < col.size(); i++) {
							Object object = col.get(i);
							if(object instanceof Property) {
								removedAttributeIndexes.add(i);
							}
						}
					}
				}
			}
		}
		// deduce pins indexes from old attributes indexes
		List<Integer> removedInputPinIndexes = new LinkedList<Integer>(removedAttributeIndexes);
		CompoundCommand globalCmd = new CompoundCommand();
		// explore referencing actions
		List<InvocationAction> callingActions = getCallingActions(element);
		for(InvocationAction action : callingActions) {
			if(action instanceof SendSignalAction) {
				CompoundCommand cmd = getRemovePinsCmd((SendSignalAction)action, removedInputPinIndexes);
				globalCmd.append(cmd);
			}
		}
		if(!globalCmd.isEmpty() && globalCmd.canExecute()) {
			if(askForValidation(callingActions)) {
				globalCmd.execute();
			} else {
				return ctx.createFailureStatus();
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Ensure Pins corresponding to attributes are added
	 * 
	 * @param element
	 *        modified signal
	 * @param ctx
	 *        validation context
	 * @return status
	 */
	protected IStatus handleAttributesAdded(Signal element, IValidationContext ctx) {
		// construct the list of added indexes and their direction
		List<Property> addedAttributes = new LinkedList<Property>();
		for(Notification event : ctx.getAllEvents()) {
			if(UMLPackage.eINSTANCE.getSignal_OwnedAttribute().equals(event.getFeature())) {
				Object addedValue = event.getNewValue();
				if(addedValue instanceof Property) {
					addedAttributes.add((Property)addedValue);
				} else if(addedValue instanceof List<?>) {
					List<?> col = (List<?>)addedValue;
					if(!col.isEmpty()) {
						for(int i = 0; i < col.size(); i++) {
							Object object = col.get(i);
							if(object instanceof Property) {
								addedAttributes.add((Property)object);
							}
						}
					}
				}
			}
		}
		List<Property> newAttributes = Collections.emptyList();
		if(element instanceof Signal) {
			newAttributes = element.getOwnedAttributes();
		}
		Map<Integer, Property> addedInputPinMap = new HashMap<Integer, Property>();
		// iterate on the list of added attributes to deduce pins indexes
		for(Property param : addedAttributes) {
			if(newAttributes.contains(param)) {
				addedInputPinMap.put(newAttributes.indexOf(param), param);
			}
		}
		CompoundCommand globalCmd = new CompoundCommand();
		// explore referencing actions
		List<InvocationAction> callingActions = getCallingActions(element);
		for(InvocationAction action : callingActions) {
			if(action instanceof SendSignalAction) {
				CompoundCommand cmd = getAddPinsCmd(action, addedInputPinMap, null);
				globalCmd.append(cmd);
			}
		}
		if(!globalCmd.isEmpty() && globalCmd.canExecute()) {
			if(askForValidation(callingActions)) {
				globalCmd.execute();
			} else {
				return ctx.createFailureStatus();
			}
		}
		return ctx.createSuccessStatus();
	}

	/**
	 * Get the command to update type of target input pins referring the
	 * operation
	 * 
	 * @param operation
	 *        the operation which parent type has changed
	 * @return the command
	 */
	protected CompoundCommand getUpdateTargetPinsType(Operation operation) {
		CompoundCommand globalCmd = new CompoundCommand();
		// get operation parent type
		Type type = null;
		Element owner = operation.getOwner();
		if(owner instanceof Type) {
			type = (Type)owner;
		}
		if(type != null) {// Get the editing domain
			TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
			// explore referencing actions
			for(InvocationAction action : getCallingActions(operation)) {// operation
																			// is
																			// action's
																			// called
																			// Operation
				if(action instanceof CallOperationAction) {
					InputPin targetPin = ((CallOperationAction)action).getTarget();
					if(targetPin != null) {
						Command cmd = SetCommand.create(editingdomain, targetPin, UMLPackage.eINSTANCE.getTypedElement_Type(), type);
						globalCmd.append(cmd);
					}
				}
			}
		}
		return globalCmd;
	}

	/**
	 * Get the command to remove pins linked with parameter at the given indexes
	 * 
	 * @param action
	 *        the CallOperationAction or CallBehaviorAction (no effect
	 *        otherwise)
	 * @param removedInputPinsIndexes
	 *        the indexes of input pins to remove (except target)
	 * @param removedOutputPinsIndexes
	 *        the indexes of output pins to remove
	 * @return the command to remove corresponding Pins
	 */
	protected CompoundCommand getRemovePinsCmd(CallAction action, List<Integer> removedInputPinsIndexes, List<Integer> removedOutputPinsIndexes) {
		CompoundCommand globalCmd = new CompoundCommand();
		// Get the editing domain
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		if(action instanceof CallBehaviorAction || action instanceof CallOperationAction) {
			ArrayList<InputPin> removedIn = new ArrayList<InputPin>(removedInputPinsIndexes.size());
			for(int i : removedInputPinsIndexes) {
				if(i < action.getArguments().size()) {
					removedIn.add(action.getArguments().get(i));
				}
			}
			if(!removedIn.isEmpty()) {
				Command cmd = RemoveCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getInvocationAction_Argument(), removedIn);
				globalCmd.append(cmd);
			}
			ArrayList<OutputPin> removedOut = new ArrayList<OutputPin>(removedInputPinsIndexes.size());
			for(int i : removedOutputPinsIndexes) {
				if(i < action.getResults().size()) {
					removedOut.add(action.getResults().get(i));
				}
			}
			if(!removedOut.isEmpty()) {
				Command cmd = RemoveCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getCallAction_Result(), removedOut);
				globalCmd.append(cmd);
			}
		}
		return globalCmd;
	}

	/**
	 * Get the command to remove pins linked with attribute at the given indexes
	 * 
	 * @param action
	 *        the SendSignalAction
	 * @param removedInputPinsIndexes
	 *        the indexes of input pins to remove (except target)
	 * @return the command to remove corresponding Pins
	 */
	protected CompoundCommand getRemovePinsCmd(SendSignalAction action, List<Integer> removedInputPinsIndexes) {
		CompoundCommand globalCmd = new CompoundCommand();
		// Get the editing domain
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		if(action instanceof SendSignalAction) {
			ArrayList<InputPin> removedIn = new ArrayList<InputPin>(removedInputPinsIndexes.size());
			for(int i : removedInputPinsIndexes) {
				if(i < action.getArguments().size()) {
					removedIn.add(action.getArguments().get(i));
				}
			}
			if(!removedIn.isEmpty()) {
				Command cmd = RemoveCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getInvocationAction_Argument(), removedIn);
				globalCmd.append(cmd);
			}
		}
		return globalCmd;
	}

	/**
	 * Get the command to add pins linked with parameter at the given indexes
	 * 
	 * @param invocationAction
	 *        the CallBehaviorAction or CallOperationAction (no effect
	 *        otherwise)
	 * @param addedInputPinMap
	 *        the indexes of input pins to add and parameters to copy
	 * @param addedOutputPinMap
	 *        the indexes of output pins to add and parameters to copy
	 * @param preferredPinClass
	 *        the EClass to use to create a new pin whenever possible (or
	 *        null)
	 * @return the command to add corresponding Pins
	 */
	protected static CompoundCommand getAddPinsCmd(InvocationAction invocationAction, Map<Integer, TypedElement> addedInputPinMap, Map<Integer, TypedElement> addedOutputPinMap, EClass preferredPinClass) {
		CompoundCommand globalCmd = new CompoundCommand();
		// Get the editing domain
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		if(invocationAction instanceof CallBehaviorAction || invocationAction instanceof CallOperationAction || invocationAction instanceof SendSignalAction || invocationAction instanceof BroadcastSignalAction) {
			/*
			 * An element can not be added at an index bigger than the size.
			 * Hence, add commands must be sorted according to the decreasing
			 * index and the index must be adapted taking in account elements
			 * that will be added. We use a bucket sort on indexes of both maps.
			 */
			int nextKey = addedInputPinMap.size() + invocationAction.getArguments().size();
			int numberOfPinsToAdd = addedInputPinMap.size();
			while(numberOfPinsToAdd > 0) {
				if(addedInputPinMap.containsKey(nextKey)) {
					numberOfPinsToAdd--;
					InputPin pin = createInputPin(addedInputPinMap.get(nextKey), preferredPinClass);
					// index at which pin is added must take in account other
					// pins added after
					int addIndex = nextKey - numberOfPinsToAdd;
					Command cmd = AddCommand.create(editingdomain, invocationAction, UMLPackage.eINSTANCE.getInvocationAction_Argument(), pin, addIndex);
					LinkPinToParameter link = new LinkPinToParameter(pin, addedInputPinMap.get(nextKey));
					CreatePinToParameterLinkEAnnotation linkCommand = new CreatePinToParameterLinkEAnnotation(EditorUtils.getTransactionalEditingDomain(), link);
					globalCmd.append(cmd);
					if(linkCommand != null && linkCommand.canExecute()) {
						globalCmd.append(linkCommand);
					}
				}
				nextKey--;
			}
			if(invocationAction instanceof CallAction) {
				CallAction callAction = (CallAction)invocationAction;
				nextKey = addedOutputPinMap.size() + callAction.getResults().size();
				numberOfPinsToAdd = addedOutputPinMap.size();
				while(numberOfPinsToAdd > 0) {
					if(addedOutputPinMap.containsKey(nextKey)) {
						numberOfPinsToAdd--;
						OutputPin pin = createOutputPin(addedOutputPinMap.get(nextKey));
						// index at which pin is added must take in account other
						// pins added after
						int addIndex = nextKey - numberOfPinsToAdd;
						Command cmd = AddCommand.create(editingdomain, callAction, UMLPackage.eINSTANCE.getCallAction_Result(), pin, addIndex);
						globalCmd.append(cmd);
						LinkPinToParameter link = new LinkPinToParameter(pin, addedOutputPinMap.get(nextKey));
						CreatePinToParameterLinkEAnnotation linkCommand = new CreatePinToParameterLinkEAnnotation(EditorUtils.getTransactionalEditingDomain(), link);
						if(linkCommand != null) {
							globalCmd.append(linkCommand);
						}
					}
					nextKey--;
				}
			}
		}
		return globalCmd;
	}

	/**
	 * Get the command to add pins linked with properties at the given indexes
	 * 
	 * @param action
	 *        the SendSignalAction
	 * @param addedInputPinMap
	 *        the indexes of input pins to add and properties to copy
	 * @param preferredPinClass
	 *        the EClass to use to create a new pin whenever possible (or
	 *        null)
	 * @return the command to add corresponding Pins
	 */
	public static CompoundCommand getAddPinsCmd(InvocationAction action, Map<Integer, Property> addedInputPinMap, EClass preferredPinClass) {
		CompoundCommand globalCmd = new CompoundCommand();
		// Get the editing domain
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		/*
		 * An element can not be added at an index bigger than the size. Hence,
		 * add commands must be sorted according to the decreasing index and the
		 * index must be adapted taking in account elements that will be added.
		 * We use a bucket sort on indexes of both maps.
		 */
		int nextKey = addedInputPinMap.size() + action.getArguments().size();
		int numberOfPinsToAdd = addedInputPinMap.size();
		while(numberOfPinsToAdd > 0) {
			if(addedInputPinMap.containsKey(nextKey)) {
				numberOfPinsToAdd--;
				InputPin pin = createInputPin(addedInputPinMap.get(nextKey), preferredPinClass);
				// index at which pin is added must take in account other pins
				// added after
				int addIndex = nextKey - numberOfPinsToAdd;
				Command cmd = AddCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getInvocationAction_Argument(), pin, addIndex);
				globalCmd.append(cmd);
				LinkPinToParameter link = new LinkPinToParameter(pin, addedInputPinMap.get(nextKey));
				CreatePinToParameterLinkEAnnotation linkCommand = new CreatePinToParameterLinkEAnnotation(WorkspaceEditingDomainFactory.INSTANCE.getEditingDomain(action.eResource().getResourceSet()), link);
				if(linkCommand != null && linkCommand.canExecute()) {
					globalCmd.append(linkCommand);
				}
			}
			nextKey--;
		}
		return globalCmd;
	}

	/**
	 * Create an output pin with valued copied from the parameter
	 * 
	 * @param typedElement
	 *        the reference parameter
	 */
	protected static OutputPin createOutputPin(TypedElement typedElement) {
		OutputPin pin = UMLFactory.eINSTANCE.createOutputPin();
		assignUpperBound(pin);
		// Initialize name
		pin.setName(typedElement.getName());
		// Synchronize type
		pin.setType(typedElement.getType());
		if(typedElement instanceof Property) {
			Property property = (Property)typedElement;
			// Synchronize is ordered
			pin.setIsOrdered(property.isOrdered());
			// Synchronize mutliplicity : is unique
			pin.setIsUnique(property.isUnique());
			// Synchronize mutliplicity : lower value
			ValueSpecification lowerValue = property.getLowerValue();
			if(lowerValue != null) {
				// use a copy command for new value
				Command copy = CopyCommand.create(EditorUtils.getTransactionalEditingDomain(), Collections.singleton(lowerValue));
				copy.execute();
				Collection<?> result = copy.getResult();
				Object valueToAffect = null;
				if(!result.isEmpty()) {
					valueToAffect = result.iterator().next();
				}
				if(valueToAffect instanceof ValueSpecification) {
					pin.setLowerValue((ValueSpecification)valueToAffect);
				}
			}
			// Synchronize mutliplicity : upper value
			ValueSpecification upperValue = property.getUpperValue();
			if(upperValue != null) {
				// use a copy command for new value
				Command copy = CopyCommand.create(EditorUtils.getTransactionalEditingDomain(), Collections.singleton(upperValue));
				copy.execute();
				Collection<?> result = copy.getResult();
				Object valueToAffect = null;
				if(!result.isEmpty()) {
					valueToAffect = result.iterator().next();
				}
				if(valueToAffect instanceof ValueSpecification) {
					pin.setUpperValue((ValueSpecification)valueToAffect);
				}
			}
		}
		return pin;
	}

	/**
	 * Create a target input pin, eventually from a given operation
	 * 
	 * @param operation
	 *        the invoked operation or null
	 */
	protected static InputPin createTargetPin(Operation operation) {
		InputPin pin = UMLFactory.eINSTANCE.createInputPin();
		assignUpperBound(pin);
		if(operation != null) {
			Element owningType = operation.getOwner();
			if(owningType instanceof Type) {
				pin.setType((Type)owningType);
			}
		}
		pin.setName(TARGET_PIN_INITIALIZATION_NAME);
		return pin;
	}

	/**
	 * Create a result input pin, eventually from a given operation
	 * 
	 * @param classifier
	 *        the to set the output type
	 */
	protected OutputPin createResultPin(Classifier classifier) {
		OutputPin pin = UMLFactory.eINSTANCE.createOutputPin();
		assignUpperBound(pin);
		if(classifier != null) {
			pin.setType(classifier);
		}
		pin.setName(RESULT_PIN_INITIALIZATION_NAME);
		return pin;
	}

	/**
	 * Create a result input pin, eventually from a given operation
	 * 
	 * @param var
	 *        the to set the output type
	 */
	protected OutputPin createResultPin(Variable var) {
		OutputPin pin = UMLFactory.eINSTANCE.createOutputPin();
		assignUpperBound(pin);
		if(var != null) {
			pin.setType(var.getType());
		}
		pin.setName(RESULT_IN_READ_VARIABLE_ACTION);
		return pin;
	}

	/**
	 * Create a request input pin
	 * 
	 * @param operation
	 *        the invoked operation or null
	 */
	public static InputPin createRequestPin() {
		InputPin pin = UMLFactory.eINSTANCE.createInputPin();
		assignUpperBound(pin);
		pin.setName(REQUEST_PIN_INITIALIZATION_NAME);
		return pin;
	}

	/**
	 * Create an input pin with valued copied from the property
	 * 
	 * @param typedElement
	 *        the reference property
	 * @param preferredPinClass
	 *        the EClass to use to create a new pin whenever possible (or
	 *        null)
	 */
	public static InputPin createInputPin(TypedElement typedElement, EClass preferredPinClass) {
		InputPin pin;
		if(UMLPackage.eINSTANCE.getValuePin().equals(preferredPinClass)) {
			pin = UMLFactory.eINSTANCE.createValuePin();
		} else if(UMLPackage.eINSTANCE.getActionInputPin().equals(preferredPinClass)) {
			pin = UMLFactory.eINSTANCE.createActionInputPin();
		} else {
			pin = UMLFactory.eINSTANCE.createInputPin();
		}
		assignUpperBound(pin);
		// Initialize name
		pin.setName(typedElement.getName());
		// Synchronize type
		pin.setType(typedElement.getType());
		if(pin instanceof Property) {
			Property property = (Property)pin;
			// Synchronize is ordered
			pin.setIsOrdered(property.isOrdered());
			// Synchronize multiplicity : is unique
			pin.setIsUnique(property.isUnique());
			// Synchronize multiplicity : lower value
			ValueSpecification lowerValue = property.getLowerValue();
			if(lowerValue != null) {
				// use a copy command for new value
				Command copy = CopyCommand.create(EditorUtils.getTransactionalEditingDomain(), Collections.singleton(lowerValue));
				copy.execute();
				Collection<?> result = copy.getResult();
				Object valueToAffect = null;
				if(!result.isEmpty()) {
					valueToAffect = result.iterator().next();
				}
				if(valueToAffect instanceof ValueSpecification) {
					pin.setLowerValue((ValueSpecification)valueToAffect);
				}
			}
			// Synchronize multiplicity : upper value
			ValueSpecification upperValue = property.getUpperValue();
			if(upperValue != null) {
				// use a copy command for new value
				Command copy = CopyCommand.create(EditorUtils.getTransactionalEditingDomain(), Collections.singleton(upperValue));
				copy.execute();
				Collection<?> result = copy.getResult();
				Object valueToAffect = null;
				if(!result.isEmpty()) {
					valueToAffect = result.iterator().next();
				}
				if(valueToAffect instanceof ValueSpecification) {
					pin.setUpperValue((ValueSpecification)valueToAffect);
				}
			}
		}
		return pin;
	}

	/**
	 * Create an input pin with valued copied from the parameter
	 * 
	 * @param parameter
	 *        the reference parameter
	 * @param preferredPinClass
	 *        the EClass to use to create a new pin whenever possible (or
	 *        null)
	 */
	protected static InputPin createInputPin(Parameter parameter, EClass preferredPinClass) {
		InputPin pin;
		if(UMLPackage.eINSTANCE.getValuePin().equals(preferredPinClass)) {
			pin = UMLFactory.eINSTANCE.createValuePin();
		} else if(UMLPackage.eINSTANCE.getActionInputPin().equals(preferredPinClass)) {
			pin = UMLFactory.eINSTANCE.createActionInputPin();
		} else {
			pin = UMLFactory.eINSTANCE.createInputPin();
		}
		assignUpperBound(pin);
		// Initialize name
		pin.setName(parameter.getName());
		// Synchronize type
		pin.setType(parameter.getType());
		// Synchronize is ordered
		pin.setIsOrdered(parameter.isOrdered());
		// Synchronize multiplicity : is unique
		pin.setIsUnique(parameter.isUnique());
		// Synchronize multiplicity : lower value
		ValueSpecification lowerValue = parameter.getLowerValue();
		if(lowerValue != null) {
			// use a copy command for new value
			Command copy = CopyCommand.create(EditorUtils.getTransactionalEditingDomain(), Collections.singleton(lowerValue));
			copy.execute();
			Collection<?> result = copy.getResult();
			Object valueToAffect = null;
			if(!result.isEmpty()) {
				valueToAffect = result.iterator().next();
			}
			if(valueToAffect instanceof ValueSpecification) {
				pin.setLowerValue((ValueSpecification)valueToAffect);
			}
		}
		// Synchronize multiplicity : upper value
		ValueSpecification upperValue = parameter.getUpperValue();
		if(upperValue != null) {
			// use a copy command for new value
			Command copy = CopyCommand.create(EditorUtils.getTransactionalEditingDomain(), Collections.singleton(upperValue));
			copy.execute();
			Collection<?> result = copy.getResult();
			Object valueToAffect = null;
			if(!result.isEmpty()) {
				valueToAffect = result.iterator().next();
			}
			if(valueToAffect instanceof ValueSpecification) {
				pin.setUpperValue((ValueSpecification)valueToAffect);
			}
		}
		return pin;
	}

	/**
	 * Get the command to reset all pins of the action.
	 * 
	 * @param action
	 *        action to reinitialize pins (AddStructuralFeatureValueAction)
	 * @return command
	 */
	protected CompoundCommand getResetPinsCmd(AddStructuralFeatureValueAction action) {
		// Get the editing domain
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		CompoundCommand globalCmd = new CompoundCommand();
		if(action.getValue() == null) {
			InputPin valuePin = createValuePinInAddStructuralFeatureAction(action);
			Command cmdValuePin = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getWriteStructuralFeatureAction_Value(), valuePin);
			globalCmd.append(cmdValuePin);
		}
		if(action.getObject() == null) {
			InputPin objectPin = createObjectPinInStructuralFeatureAction(action);
			Command cmd = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getStructuralFeatureAction_Object(), objectPin);
			globalCmd.append(cmd);
		}
		if(action.getResult() == null) {
			OutputPin resultPin = createResultPinInStructuralAction(action);
			Command cmdResultPin = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getWriteStructuralFeatureAction_Result(), resultPin);
			globalCmd.append(cmdResultPin);
		}
		return globalCmd;
	}

	/**
	 * Get the command to reset all pins of the action.
	 * 
	 * @param action
	 *        action to reinitialize pins (ReadVariableAction)
	 * @return command
	 */
	// Get the editing domain
	protected CompoundCommand getResetPinsCmd(ReadVariableAction action) {
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		CompoundCommand globalCmd = new CompoundCommand();
		if(action.getResult() == null) {
			OutputPin resultPin = createResultPin(action.getVariable());
			Command cmdResultPin = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getReadVariableAction_Result(), resultPin);
			globalCmd.append(cmdResultPin);
		}
		return globalCmd;
	}

	//	/**
	//	 * Get the command to reset all pins of the action.
	//	 * 
	//	 * @param action
	//	 *        action to reinitialize pins (BroadcastSignalAction)
	//	 * @return command
	//	 */
	//	protected CompoundCommand getResetPinsCmd(BroadcastSignalAction action) {
	//		// Get the editing domain
	//		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
	//		CompoundCommand globalCmd = new CompoundCommand();
	//		if(action.getSignal() != null) {
	//			if(action.getArguments().isEmpty()) {
	//				EList<Property> properties = action.getSignal().getAllAttributes();
	//				for(Property argument : properties) {
	//					InputPin argPin = UMLFactory.eINSTANCE.createInputPin();
	//					assignUpperBound(argPin);
	//					argPin.setName(argument.getName());
	//					argPin.setType(argument.getType());
	//					Command cmdArg = AddCommand.create(editingdomain, action, UMLPackage.Literals.INVOCATION_ACTION__ARGUMENT, Arrays.asList(argPin));
	//					globalCmd.append(cmdArg);
	//				}
	//			}
	//		}
	//		return globalCmd;
	//	}
	/**
	 * Get the command to reset all pins of the action.
	 * 
	 * @param action
	 *        action to reinitialize pins (AddVariableValueAction)
	 * @return command
	 */
	protected CompoundCommand getResetPinsCmd(AddVariableValueAction action) {
		// Get the editing domain
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		CompoundCommand globalCmd = new CompoundCommand();
		if(action.getValue() == null) {
			InputPin valuePin = createValuePinInAddVariableValueAction(action.getVariable());
			Command cmdValuePin = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getWriteVariableAction_Value(), valuePin);
			globalCmd.append(cmdValuePin);
		}
		if(action.getInsertAt() == null) {
			InputPin insertAtPin = UMLFactory.eINSTANCE.createInputPin();
			assignUpperBound(insertAtPin);
			insertAtPin.setName(INSERT_AT_IN_ADD_VARIABLE_VALUE_ACTION);
			Command cmd = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getAddVariableValueAction_InsertAt(), insertAtPin);
			globalCmd.append(cmd);
		}
		return globalCmd;
	}

	/**
	 * Get the command to reset all pins of the action.
	 * 
	 * @param action
	 *        action to reinitialize pins (AddStructuralFeatureValueAction)
	 * @return command
	 */
	public static CompoundCommand getResetPinsCmd(DestroyObjectAction action) {
		// Get the editing domain
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		CompoundCommand globalCmd = new CompoundCommand();
		if(action.getTarget() == null) {
			InputPin targetPin = UMLFactory.eINSTANCE.createInputPin();
			assignUpperBound(targetPin);
			targetPin.setName(TARGET_IN_DESTROY_OBJECT_ACTION);
			Command cmdTargetPin = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getDestroyObjectAction_Target(), targetPin);
			globalCmd.append(cmdTargetPin);
		}
		return globalCmd;
	}

	/**
	 * Create a Pin value for a Structural feature action
	 * 
	 * @param action
	 * @return
	 */
	public static InputPin createValuePinInAddStructuralFeatureAction(StructuralFeatureAction action) {
		InputPin pin = UMLFactory.eINSTANCE.createInputPin();
		assignUpperBound(pin);
		if(action != null) {
			StructuralFeature feature = action.getStructuralFeature();
			if(feature != null && feature.getType() != null) {
				Type owningType = feature.getType();
				if(owningType instanceof Type) {
					pin.setType(owningType);
				}
			}
		}
		pin.setName(VALUE_PIN_IN_STRUCTURAL_FEATURE_VALUE_ACTION);
		return pin;
	}

	/**
	 * Create a Pin value for a Structural feature action
	 * 
	 * @param action
	 * @return
	 */
	protected InputPin createValuePinInAddVariableValueAction(Variable var) {
		InputPin pin = UMLFactory.eINSTANCE.createInputPin();
		assignUpperBound(pin);
		if(var != null) {
			Type owningType = var.getType();
			if(owningType instanceof Type) {
				pin.setType(owningType);
			}
		}
		pin.setName(VALUE_IN_ADD_VARIABLE_VALUE_ACTION);
		return pin;
	}

	/**
	 * Get the command to reset all pins of the action.
	 * 
	 * @param action
	 *        action to reinitialize pins (SendObjectAction)
	 * @return command
	 */
	public static CompoundCommand getResetPinsCmd(SendObjectAction action) {
		// Get the editing domain
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		CompoundCommand globalCmd = new CompoundCommand();
		// add target pin
		if(action.getTarget() == null) {
			InputPin targetPin = createTargetPin(null);
			Command cmd = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getSendObjectAction_Target(), targetPin);
			globalCmd.append(cmd);
		}
		// add request pin
		if(action.getRequest() == null) {
			InputPin requestPin = createRequestPin();
			Command cmd = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getSendObjectAction_Request(), requestPin);
			globalCmd.append(cmd);
		}
		return globalCmd;
	}

	/**
	 * Get the command to reset all pins of the action.
	 * 
	 * @param action
	 *        action to reinitialize pins (ReadStructuralFeatureAction)
	 * @return command
	 */
	protected CompoundCommand getResetPinsCmd(ReadStructuralFeatureAction action) {
		// Get the editing domain
		TransactionalEditingDomain editingdomain;
		try {
			editingdomain = ServiceUtilsForEObject.getInstance().getTransactionalEditingDomain(action);
		} catch (ServiceException ex) {
			Log.error(DiagramUIPlugin.getInstance(), DiagramUIStatusCodes.IGNORED_EXCEPTION_WARNING, ex.getMessage(), ex);
			return null;
		}
		CompoundCommand globalCmd = new CompoundCommand();
		// add result pin
		if(action.getResult() == null) {
			OutputPin resultPin = createResultPinInStructuralAction(action);
			Command cmd = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getReadStructuralFeatureAction_Result(), resultPin);
			globalCmd.append(cmd);
		}
		// add object pin
		if(action.getObject() == null) {
			InputPin objectPin = createObjectPinInStructuralFeatureAction(action);
			Command cmd = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getStructuralFeatureAction_Object(), objectPin);
			globalCmd.append(cmd);
		}
		return globalCmd;
	}

	/**
	 * Create the object pin of an ReadStructuralAction
	 * 
	 * @param action
	 * @return
	 */
	public static InputPin createObjectPinInStructuralFeatureAction(StructuralFeatureAction action) {
		InputPin pin = UMLFactory.eINSTANCE.createInputPin();
		assignUpperBound(pin);
		if(action != null) {
			Type type = getTypeFromStructuralFeature(action);
			if(type != null) {
				pin.setType(type);
			}
		}
		pin.setName(OBJECT_PIN_IN_READS_STRUCTURAL_ACTION);
		return pin;
	}

	public static Type getTypeFromStructuralFeature(StructuralFeatureAction action) {
		Type type = null;
		StructuralFeature feature = action.getStructuralFeature();
		if(feature != null) {
			Element owner = feature.getOwner();
			if(owner != null) {
				if(feature.getFeaturingClassifiers().contains(owner)) {
					type = ((Type)owner);
				}
			}
		}
		return type;
	}

	/**
	 * Create a simple output pin for a ReadStructura feature FIXME set type
	 * 
	 * @param action
	 * @return
	 */
	public static OutputPin createResultPinInStructuralAction(StructuralFeatureAction action) {
		OutputPin pin = UMLFactory.eINSTANCE.createOutputPin();
		assignUpperBound(pin);
		Type type = getTypeFromStructuralFeature(action);
		if(type != null) {
			pin.setType(type);
		}
		pin.setName(RESULT_PIN_READ_SRTUCTURAL_ACTION);
		return pin;
	}

	private static void assignUpperBound(ObjectNode node) {
		LiteralInteger literal = UMLFactory.eINSTANCE.createLiteralInteger();
		literal.setValue(1);
		node.setUpperBound(literal);
	}

	/**
	 * Get the command to reset all pins of the action.
	 * 
	 * @param action
	 *        action to reinitialize pins (SendObjectAction)
	 * @return command
	 */
	protected CompoundCommand getResetPinsCmd(CreateObjectAction action) {
		// Get the editing domain
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		CompoundCommand globalCmd = new CompoundCommand();
		// add target pin
		if(action.getResult() == null) {
			OutputPin resultPin = createResultPin((Classifier)null);
			Command cmd = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getCreateObjectAction_Result(), resultPin);
			globalCmd.append(cmd);
		}
		return globalCmd;
	}

	//	/**
	//	 * Get the command to reset all pins of the action.
	//	 * 
	//	 * @param action
	//	 *        action to reinitialize pins (SendSignalAction)
	//	 * @return command
	//	 */
	//	public static CompoundCommand getResetPinsCmd(InvocationAction action) {
	//		if(!(action instanceof SendSignalAction || action instanceof BroadcastSignalAction)) {
	//			return null;
	//		}
	//		// Get the editing domain
	//		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
	//		CompoundCommand globalCmd = new CompoundCommand();
	//		// remove argument pins
	//		if(!action.getArguments().isEmpty()) {
	//			Command cmd = RemoveCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getInvocationAction_Argument(), action.getArguments());
	//			globalCmd.append(cmd);
	//		}
	//		// recover attributes
	//		List<Property> attributes = Collections.emptyList();
	//		Signal signal = action instanceof SendSignalAction ? ((SendSignalAction)action).getSignal() : ((BroadcastSignalAction)action).getSignal();
	//		if(signal != null) {
	//			attributes = signal.getOwnedAttributes();
	//		}
	//		// add pins corresponding to attributes
	//		Map<Integer, Property> inParameters = new HashMap<Integer, Property>();
	//		int inIndex = 0;
	//		for(Property att : attributes) {
	//			inParameters.put(inIndex, att);
	//			inIndex++;
	//		}
	//		if(!inParameters.isEmpty()) {
	//			Command cmd = getAddPinsCmd(action, inParameters, null);
	//			globalCmd.append(cmd);
	//		}
	//		if(action instanceof SendSignalAction) {
	//			SendSignalAction sendSignalAction = (SendSignalAction)action;
	//			// add target pin
	//			if(sendSignalAction.getTarget() == null) {
	//				InputPin targetPin = createTargetPin(null);
	//				Command cmd = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getSendSignalAction_Target(), targetPin);
	//				globalCmd.append(cmd);
	//			}
	//		}
	//		return globalCmd;
	//	}
	/**
	 * Retrieve the parameter linked
	 * 
	 * @param p
	 *        Pin where the EAnnotaion is stored
	 * @param xmiResource
	 * @return
	 */
	public static TypedElement getLinkedParemeter(Pin p, XMIResource xmiResource) {
		if(p != null && xmiResource != null) {
			EAnnotation eAnnotation = p.getEAnnotation(IPinToParameterLinkCommand.PIN_TO_PARAMETER_LINK);
			if(eAnnotation != null && !eAnnotation.getDetails().isEmpty()) {
				String id = eAnnotation.getDetails().get(0).getValue();
				EObject pa = xmiResource.getEObject(id);
				if(pa instanceof TypedElement) {
					return (TypedElement)pa;
				}
			}
		}
		return null;
	}

	/**
	 * Get the command to reset all pins of the action.
	 * 
	 * @param action
	 *        action to reinitialize pins (CallOperationAction or
	 *        CallBehaviorAction)
	 * @return command
	 */
	public static CompoundCommand getResetPinsCmd(InvocationAction action) {
		// Getting the editing domain
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		CompoundCommand globalCmd = new CompoundCommand();
		Element behaviorStructural = null;
		List<? extends TypedElement> parameters = Collections.emptyList();
		if(action instanceof CallBehaviorAction) {
			behaviorStructural = ((CallBehaviorAction)action).getBehavior();
			if(behaviorStructural!=null) {
				parameters = ((Behavior)behaviorStructural).getOwnedParameters();
			} else {
				return globalCmd;
			}
		} else if(action instanceof CallOperationAction) {
			behaviorStructural = ((CallOperationAction)action).getOperation();
			if(behaviorStructural!=null) {
				parameters = ((Operation)behaviorStructural).getOwnedParameters();	
			} else {
				return globalCmd;
			}
		} else if(action instanceof SendSignalAction) {
			behaviorStructural = ((SendSignalAction)action).getSignal();
			if(behaviorStructural!=null) {
				parameters = ((Signal)behaviorStructural).getOwnedAttributes();	
			} else {
				return globalCmd;
			}
		} else if(action instanceof BroadcastSignalAction) {
			Signal signal = ((BroadcastSignalAction)action).getSignal();
			if(signal != null) {
				behaviorStructural = signal;
				parameters = ((Signal)behaviorStructural).getOwnedAttributes();
			} else {
				parameters = Collections.emptyList();
			}
		}
		XMIResource xmiResource = getXMIResource(behaviorStructural);
		// Removing input pins that are not up to date.
		Collection<TypedElement> parameterWhichPinNotDeleted = new ArrayList<TypedElement>();
		Iterable<? extends Pin> allPins = Lists.newArrayList(action.getArguments());
		if(action instanceof CallAction) {
			allPins = Iterables.concat(allPins, ((CallAction)action).getResults());
		}
		List<Command> removesCommand = Lists.newArrayList();
		for(Pin pin : allPins) {
			if(SynchronizePinsParametersHandler.isUpToDate(pin, xmiResource)) {
				TypedElement pa = getLinkedParemeter(pin, xmiResource);
				parameterWhichPinNotDeleted.add(pa);
			} else {
				EReference feature = null;
				if(pin instanceof InputPin) {
					feature = UMLPackage.eINSTANCE.getInvocationAction_Argument();
				} else if(pin instanceof OutputPin) {
					feature = UMLPackage.eINSTANCE.getCallAction_Result();
				}
				//Removing the pin.
				Command cmd = RemoveCommand.create(editingdomain, action, feature, pin);
				if(cmd.canExecute()) {
					removesCommand.add(cmd);
				}
			}
		}
		//Splitting parameters
		Map<Integer, TypedElement> inParams = new HashMap<Integer, TypedElement>();
		Map<Integer, TypedElement> outParams = new HashMap<Integer, TypedElement>();
		splitParameters(parameters, parameterWhichPinNotDeleted, inParams, outParams, action);
		//Creating new pins.
		if(!inParams.isEmpty() || !outParams.isEmpty()) {
			Command cmd = getAddPinsCmd(action, inParams, outParams, null);
			globalCmd.append(cmd);
		}
		/*
		 * Append remove command after create command since create command calculate index of new pins before removing those pins
		 */
		for(Command rmComand : removesCommand) {
			globalCmd.append(rmComand);
		}
		/*
		 * No need to reset this pin
		 */
		if(action instanceof CallOperationAction) {
			// add target pin
			Operation operation = ((CallOperationAction)action).getOperation();
			if(operation != null) {
				InputPin targetPin = createTargetPin(operation);
				Command cmd = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getCallOperationAction_Target(), targetPin);
				globalCmd.append(cmd);
			}
		} else if(action instanceof SendSignalAction) {
			SendSignalAction sendSignalAction = (SendSignalAction)action;
			// add target pin
			if(sendSignalAction.getTarget() == null) {
				InputPin targetPin = createTargetPin(null);
				Command cmd = SetCommand.create(editingdomain, action, UMLPackage.eINSTANCE.getSendSignalAction_Target(), targetPin);
				globalCmd.append(cmd);
			}
		}
		return globalCmd;
	}

	/**
	 * Split a list of parameters in two lists : in and out parameters. If a parameter is to be ignored, then "null" is added to the
	 * corresponding list instead.
	 */
	public static void splitParameters(List<? extends TypedElement> allParams, Collection<? extends TypedElement> paramsToIgnore, Map<Integer, TypedElement> inParams, Map<Integer, TypedElement> outParams, Action action) {
		if(action instanceof CallAction) {
			Integer inIndex = 0;
			Integer outIndex = 0;
			for(TypedElement typeElem : allParams) {
				if(typeElem instanceof Parameter) {
					Parameter param = (Parameter)typeElem;
					ParameterDirectionKind direction = param.getDirection();
					//In
					if(direction == ParameterDirectionKind.IN_LITERAL || direction == ParameterDirectionKind.INOUT_LITERAL) {
						if(!paramsToIgnore.contains(param)) {
							inParams.put(inIndex, param);
						}
						inIndex++;
					}
					//Out
					if(direction == ParameterDirectionKind.OUT_LITERAL || direction == ParameterDirectionKind.INOUT_LITERAL || direction == ParameterDirectionKind.RETURN_LITERAL) {
						if(!paramsToIgnore.contains(param)) {
							outParams.put(outIndex, param);
						}
						outIndex++;
					}
				}
			}
		} else if(action instanceof InvocationAction) {
			Integer inIndex = 0;
			for(TypedElement typeElem : allParams) {
				if(!paramsToIgnore.contains(typeElem)) {
					inParams.put(inIndex, typeElem);
				}
				inIndex++;
			}
		}
	}

	/**
	 * Retrieves the XMIResource
	 * 
	 * @param behaviorStructural
	 * @return
	 */
	public static XMIResource getXMIResource(Element behaviorStructural) {
		XMIResource xmiResource = null;
		if(behaviorStructural != null) {
			Resource resource = behaviorStructural.eResource();
			if(resource instanceof XMIResource) {
				xmiResource = (XMIResource)resource;
			}
		}
		return xmiResource;
	}

	/**
	 * Get the command to update a pins list with the name if not set yet
	 * 
	 * @param pins
	 *        the list of pins to update
	 * @param name
	 *        the new name set on parameter
	 * @return the command to execute
	 */
	protected CompoundCommand getSetPinsNamesCmd(List<Pin> pins, String name) {
		CompoundCommand globalCmd = new CompoundCommand();
		if(pins == null || name == null || "".equals(name)) {
			return globalCmd;
		}
		// Get the editing domain
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		for(Pin pin : pins) {
			// erase the name only if null (not set, the user may have set an
			// empty string name)
			if(pin.getName() == null) {
				// add the command
				Command cmd = SetCommand.create(editingdomain, pin, UMLPackage.eINSTANCE.getNamedElement_Name(), name);
				globalCmd.append(cmd);
			}
		}
		return globalCmd;
	}

	/**
	 * Get the command to update a pins list with given values
	 * 
	 * @param pins
	 *        the list of pins to update
	 * @param type
	 *        the new type value
	 * @param ordered
	 *        the new isOrdered value
	 * @param unique
	 *        the new isUnique value
	 * @param lower
	 *        the new lower value
	 * @param lowerValue
	 *        the new lowerValue value
	 * @param upper
	 *        the new upper value
	 * @param upperValue
	 *        the new upperValue value
	 * @return the command to execute
	 */
	protected CompoundCommand getUpdatePinsCmd(List<Pin> pins, Type type, boolean ordered, boolean unique, int lower, ValueSpecification lowerValue, int upper, ValueSpecification upperValue) {
		CompoundCommand globalCmd = new CompoundCommand();
		if(pins == null) {
			return globalCmd;
		}
		// Get the editing domain
		TransactionalEditingDomain editingdomain = EditorUtils.getTransactionalEditingDomain();
		for(Pin pin : pins) {
			if(!EcoreUtil.equals(pin.getType(), type)) {
				// add the command
				Command cmd = SetCommand.create(editingdomain, pin, UMLPackage.eINSTANCE.getTypedElement_Type(), type);
				globalCmd.append(cmd);
			}
			if(pin.isOrdered() != ordered) {
				// add the command
				Command cmd = SetCommand.create(editingdomain, pin, UMLPackage.eINSTANCE.getMultiplicityElement_IsOrdered(), ordered);
				globalCmd.append(cmd);
			}
			if(pin.isUnique() != unique) {
				// add the command
				Command cmd = SetCommand.create(editingdomain, pin, UMLPackage.eINSTANCE.getMultiplicityElement_IsUnique(), unique);
				globalCmd.append(cmd);
			}
			// UMLPackage.eINSTANCE.getMultiplicityElement_Lower() is derived
			// from
			// UMLPackage.eINSTANCE.getMultiplicityElement_LowerValue()
			if(!EcoreUtil.equals(pin.getLowerValue(), lowerValue)) {
				Object affectedvalue = null;
				// Execute a copy command then add the set command
				if(lowerValue != null) {
					Command copy = CopyCommand.create(editingdomain, Collections.singleton(lowerValue));
					copy.execute();
					Collection<?> result = copy.getResult();
					if(!result.isEmpty()) {
						affectedvalue = result.iterator().next();
					}
				}
				Command cmd = SetCommand.create(editingdomain, pin, UMLPackage.eINSTANCE.getMultiplicityElement_LowerValue(), affectedvalue);
				globalCmd.append(cmd);
			}
			// UMLPackage.eINSTANCE.getMultiplicityElement_Upper() is derived
			// from
			// UMLPackage.eINSTANCE.getMultiplicityElement_UpperValue()
			if(!EcoreUtil.equals(pin.getUpperValue(), upperValue)) {
				Object affectedvalue = null;
				// Execute a copy command then add the set command
				if(upperValue != null) {
					Command copy = CopyCommand.create(editingdomain, Collections.singleton(upperValue));
					copy.execute();
					Collection<?> result = copy.getResult();
					if(!result.isEmpty()) {
						affectedvalue = result.iterator().next();
					}
				}
				Command cmd = SetCommand.create(editingdomain, pin, UMLPackage.eINSTANCE.getMultiplicityElement_UpperValue(), affectedvalue);
				globalCmd.append(cmd);
			}
		}
		return globalCmd;
	}

	/**
	 * Get the object invoked by the pin's parent action
	 * 
	 * @return invoked operation, invoked behavior or null
	 */
	static protected NamedElement getInvokedObject(Pin pin) {
		Element action = pin.getOwner();
		if(action instanceof CallOperationAction) {
			Operation operation = ((CallOperationAction)action).getOperation();
			return operation;
		} else if(action instanceof CallBehaviorAction) {
			Behavior behavior = ((CallBehaviorAction)action).getBehavior();
			return behavior;
		} else if(action instanceof SendSignalAction) {
			Signal signal = ((SendSignalAction)action).getSignal();
			return signal;
		}
		return null;
	}

	/**
	 * Get all Pins associated to the property (provided no pin or property has
	 * been added without synchronization)
	 * 
	 * @param property
	 *        the property
	 * @return the list of associated pins
	 */
	static protected List<Pin> getPins(Property property) {
		Element owner = property.getOwner();
		if(owner instanceof Signal) {
			// initialize listOfPins
			List<Pin> listOfPins = new LinkedList<Pin>();
			// get index of pins
			int inIndex = ((Signal)owner).getAttributes().indexOf(property);
			List<InvocationAction> callingActions = getCallingActions(owner);
			// inspect each referencing action
			for(InvocationAction action : callingActions) {
				// owner is action's sent Signal
				Pin pin = ((SendSignalAction)action).getArguments().get(inIndex);
				if(pin != null) {
					listOfPins.add(pin);
				}
			}
			return listOfPins;
		}
		return Collections.emptyList();
	}

	/**
	 * Get all Pins associated to the parameter (provided no pin or parameter
	 * has been added without synchronization)
	 * 
	 * @param parameter
	 *        the parameter
	 * @return the list of associated pins
	 */
	static protected List<Pin> getPins(Parameter parameter) {
		Element owner = parameter.getOwner();
		List<InvocationAction> callingActions = getCallingActions(owner);
		// initialize listOfPins
		List<Pin> listOfPins = new LinkedList<Pin>();
		// get index of pins
		int inIndex = -1;
		int outIndex = -1;
		switch(parameter.getDirection()) {
		case IN_LITERAL:
			inIndex = getIndex(parameter, true);
			break;
		case OUT_LITERAL:
		case RETURN_LITERAL:
			outIndex = getIndex(parameter, false);
			break;
		case INOUT_LITERAL:
			inIndex = getIndex(parameter, true);
			outIndex = getIndex(parameter, false);
			break;
		}
		if(owner instanceof Operation) {
			// inspect each referencing action
			for(InvocationAction action : callingActions) {
				// owner is action's called Operation
				switch(parameter.getDirection()) {
				case IN_LITERAL:
					Pin pin = ((CallOperationAction)action).getArguments().get(inIndex);
					if(pin != null) {
						listOfPins.add(pin);
					}
					break;
				case OUT_LITERAL:
				case RETURN_LITERAL:
					pin = ((CallOperationAction)action).getResults().get(outIndex);
					if(pin != null) {
						listOfPins.add(pin);
					}
					break;
				case INOUT_LITERAL:
					pin = ((CallOperationAction)action).getArguments().get(inIndex);
					if(pin != null) {
						listOfPins.add(pin);
					}
					pin = ((CallOperationAction)action).getResults().get(outIndex);
					if(pin != null) {
						listOfPins.add(pin);
					}
					break;
				}
			}
			return listOfPins;
		} else if(owner instanceof Behavior) {
			// inspect each referencing action
			for(InvocationAction action : callingActions) {
				// owner is action's called Behavior
				switch(parameter.getDirection()) {
				case IN_LITERAL:
					Pin pin = ((CallBehaviorAction)action).getArguments().get(inIndex);
					if(pin != null) {
						listOfPins.add(pin);
					}
					break;
				case OUT_LITERAL:
				case RETURN_LITERAL:
					pin = ((CallBehaviorAction)action).getResults().get(outIndex);
					if(pin != null) {
						listOfPins.add(pin);
					}
					break;
				case INOUT_LITERAL:
					pin = ((CallBehaviorAction)action).getArguments().get(inIndex);
					if(pin != null) {
						listOfPins.add(pin);
					}
					pin = ((CallBehaviorAction)action).getResults().get(outIndex);
					if(pin != null) {
						listOfPins.add(pin);
					}
					break;
				}
			}
			return listOfPins;
		}
		return Collections.emptyList();
	}

	/**
	 * Get the index (considering Parameters in or out parameters only) at which
	 * the parameter appears in its container. For convenience with Pin mapping,
	 * in-out parameters counted are in both solution. Note that this count does
	 * not take in account the searched for parameter direction. This means that
	 * this method works even if the searched parameter has a different
	 * direction than the one specified in in (usefull when direction changes).
	 * 
	 * @param typedElement
	 *        the searched parameter
	 * @param in
	 *        if true, compute position in Parameters of direction in, if
	 *        false, of direction out
	 * @return the position in which the parameter appears (0 based) or -1 if
	 *         failed
	 */
	static protected int getIndex(TypedElement typedElement, boolean in) {
		if(typedElement == null) {
			return -1;
		}
		Element owner = typedElement.getOwner();
		List<Parameter> parametersList = Collections.emptyList();
		if(owner instanceof Operation) {
			parametersList = ((Operation)owner).getOwnedParameters();
		} else if(owner instanceof Behavior) {
			parametersList = ((Behavior)owner).getOwnedParameters();
		}
		int index = 0;
		for(Parameter param : parametersList) {
			if(param.equals(typedElement)) {
				return index;
			}
			if(in && (ParameterDirectionKind.IN_LITERAL.equals(param.getDirection()) || ParameterDirectionKind.INOUT_LITERAL.equals(param.getDirection()))) {
				index++;
			} else if(!in && (ParameterDirectionKind.OUT_LITERAL.equals(param.getDirection()) || ParameterDirectionKind.RETURN_LITERAL.equals(param.getDirection()) || ParameterDirectionKind.INOUT_LITERAL.equals(param.getDirection()))) {
				index++;
			}
		}
		return -1;
	}

	/**
	 * Ask the user to validate all the implied modifications (parameters and
	 * all associated pins)
	 * 
	 * @param listOfActions
	 *        the list of impacted calling actions
	 * @return whether the user validates the modifications
	 */
	protected boolean askForValidation(final List<? extends NamedElement> listOfActions) {
		SafeDialogOpenerDuringValidation<Boolean> opener = new SafeDialogOpenerDuringValidation<Boolean>() {

			@Override
			protected Boolean openDialog() {
				return ConfirmPinAndParameterSyncDialog.openConfirmFromParameter(Display.getDefault().getActiveShell(), listOfActions, labelProvider);
			}
		};
		return opener.execute();
	}
}

Back to the top