Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d0150b23072fd6fc5d78e41adf18c8489e285ac6 (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
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
/*******************************************************************************
 * Copyright (c) 2013, 2020 Willink Transformations and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v20.html
 *
 * Contributors:
 *   E.D.Willink - Initial API and implementation
 *******************************************************************************/
package org.eclipse.qvtd.codegen.qvti.java;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
import org.eclipse.emf.codegen.util.CodeGenUtil;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ocl.examples.codegen.cgmodel.CGAccumulator;
import org.eclipse.ocl.examples.codegen.cgmodel.CGCachedOperation;
import org.eclipse.ocl.examples.codegen.cgmodel.CGClass;
import org.eclipse.ocl.examples.codegen.cgmodel.CGCollectionExp;
import org.eclipse.ocl.examples.codegen.cgmodel.CGEcorePropertyCallExp;
import org.eclipse.ocl.examples.codegen.cgmodel.CGElement;
import org.eclipse.ocl.examples.codegen.cgmodel.CGElementId;
import org.eclipse.ocl.examples.codegen.cgmodel.CGExecutorProperty;
import org.eclipse.ocl.examples.codegen.cgmodel.CGExecutorType;
import org.eclipse.ocl.examples.codegen.cgmodel.CGGuardExp;
import org.eclipse.ocl.examples.codegen.cgmodel.CGIterator;
import org.eclipse.ocl.examples.codegen.cgmodel.CGLetExp;
import org.eclipse.ocl.examples.codegen.cgmodel.CGNavigationCallExp;
import org.eclipse.ocl.examples.codegen.cgmodel.CGOperation;
import org.eclipse.ocl.examples.codegen.cgmodel.CGOppositePropertyCallExp;
import org.eclipse.ocl.examples.codegen.cgmodel.CGPackage;
import org.eclipse.ocl.examples.codegen.cgmodel.CGParameter;
import org.eclipse.ocl.examples.codegen.cgmodel.CGShadowExp;
import org.eclipse.ocl.examples.codegen.cgmodel.CGShadowPart;
import org.eclipse.ocl.examples.codegen.cgmodel.CGTypeId;
import org.eclipse.ocl.examples.codegen.cgmodel.CGUnboxExp;
import org.eclipse.ocl.examples.codegen.cgmodel.CGValuedElement;
import org.eclipse.ocl.examples.codegen.cgmodel.CGVariable;
import org.eclipse.ocl.examples.codegen.cgmodel.CGVariableExp;
import org.eclipse.ocl.examples.codegen.generator.TypeDescriptor;
import org.eclipse.ocl.examples.codegen.java.CG2JavaVisitor;
import org.eclipse.ocl.examples.codegen.java.JavaConstants;
import org.eclipse.ocl.examples.codegen.java.JavaLocalContext;
import org.eclipse.ocl.examples.codegen.java.JavaStream;
import org.eclipse.ocl.examples.codegen.java.JavaStream.SubStream;
import org.eclipse.ocl.examples.codegen.java.types.BoxedDescriptor;
import org.eclipse.ocl.examples.codegen.utilities.CGUtil;
import org.eclipse.ocl.pivot.CompleteClass;
import org.eclipse.ocl.pivot.DataType;
import org.eclipse.ocl.pivot.Element;
import org.eclipse.ocl.pivot.NamedElement;
import org.eclipse.ocl.pivot.NavigationCallExp;
import org.eclipse.ocl.pivot.Operation;
import org.eclipse.ocl.pivot.Parameter;
import org.eclipse.ocl.pivot.Property;
import org.eclipse.ocl.pivot.ShadowPart;
import org.eclipse.ocl.pivot.TypedElement;
import org.eclipse.ocl.pivot.Type;
import org.eclipse.ocl.pivot.VariableDeclaration;
import org.eclipse.ocl.pivot.VariableExp;
import org.eclipse.ocl.pivot.evaluation.Executor;
import org.eclipse.ocl.pivot.ids.ClassId;
import org.eclipse.ocl.pivot.ids.CollectionTypeId;
import org.eclipse.ocl.pivot.ids.ElementId;
import org.eclipse.ocl.pivot.ids.IdResolver;
import org.eclipse.ocl.pivot.ids.PropertyId;
import org.eclipse.ocl.pivot.ids.TypeId;
import org.eclipse.ocl.pivot.internal.complete.CompleteModelInternal;
import org.eclipse.ocl.pivot.internal.library.executor.AbstractEvaluationOperation;
import org.eclipse.ocl.pivot.internal.manager.PivotMetamodelManager;
import org.eclipse.ocl.pivot.library.LibraryProperty;
import org.eclipse.ocl.pivot.library.oclany.OclElementOclContainerProperty;
import org.eclipse.ocl.pivot.oclstdlib.OCLstdlibPackage;
import org.eclipse.ocl.pivot.utilities.ClassUtil;
import org.eclipse.ocl.pivot.utilities.LabelUtil;
import org.eclipse.ocl.pivot.utilities.NameUtil;
import org.eclipse.ocl.pivot.utilities.PivotUtil;
import org.eclipse.ocl.pivot.utilities.TreeIterable;
import org.eclipse.ocl.pivot.utilities.ValueUtil;
import org.eclipse.qvtd.codegen.qvti.analyzer.QVTiAS2CGVisitor;
import org.eclipse.qvtd.codegen.qvti.analyzer.QVTiAnalyzer;
import org.eclipse.qvtd.codegen.qvticgmodel.CGConnectionAssignment;
import org.eclipse.qvtd.codegen.qvticgmodel.CGConnectionVariable;
import org.eclipse.qvtd.codegen.qvticgmodel.CGEcoreContainerAssignment;
import org.eclipse.qvtd.codegen.qvticgmodel.CGEcorePropertyAssignment;
import org.eclipse.qvtd.codegen.qvticgmodel.CGEcoreRealizedVariable;
import org.eclipse.qvtd.codegen.qvticgmodel.CGFunction;
import org.eclipse.qvtd.codegen.qvticgmodel.CGFunctionCallExp;
import org.eclipse.qvtd.codegen.qvticgmodel.CGFunctionParameter;
import org.eclipse.qvtd.codegen.qvticgmodel.CGGuardVariable;
import org.eclipse.qvtd.codegen.qvticgmodel.CGMapping;
import org.eclipse.qvtd.codegen.qvticgmodel.CGMappingCall;
import org.eclipse.qvtd.codegen.qvticgmodel.CGMappingCallBinding;
import org.eclipse.qvtd.codegen.qvticgmodel.CGMappingExp;
import org.eclipse.qvtd.codegen.qvticgmodel.CGMappingLoop;
import org.eclipse.qvtd.codegen.qvticgmodel.CGMiddlePropertyAssignment;
import org.eclipse.qvtd.codegen.qvticgmodel.CGMiddlePropertyCallExp;
import org.eclipse.qvtd.codegen.qvticgmodel.CGPropertyAssignment;
import org.eclipse.qvtd.codegen.qvticgmodel.CGRealizedVariable;
import org.eclipse.qvtd.codegen.qvticgmodel.CGRealizedVariablePart;
import org.eclipse.qvtd.codegen.qvticgmodel.CGSequence;
import org.eclipse.qvtd.codegen.qvticgmodel.CGSpeculateExp;
import org.eclipse.qvtd.codegen.qvticgmodel.CGSpeculatePart;
import org.eclipse.qvtd.codegen.qvticgmodel.CGTransformation;
import org.eclipse.qvtd.codegen.qvticgmodel.CGTypedModel;
import org.eclipse.qvtd.codegen.qvticgmodel.util.QVTiCGModelVisitor;
import org.eclipse.qvtd.codegen.utilities.QVTiCGUtil;
import org.eclipse.qvtd.pivot.qvtbase.Function;
import org.eclipse.qvtd.pivot.qvtbase.Transformation;
import org.eclipse.qvtd.pivot.qvtbase.TypedModel;
import org.eclipse.qvtd.pivot.qvtimperative.AppendParameterBinding;
import org.eclipse.qvtd.pivot.qvtimperative.BufferStatement;
import org.eclipse.qvtd.pivot.qvtimperative.ConnectionVariable;
import org.eclipse.qvtd.pivot.qvtimperative.EntryPoint;
import org.eclipse.qvtd.pivot.qvtimperative.GuardParameter;
import org.eclipse.qvtd.pivot.qvtimperative.GuardParameterBinding;
import org.eclipse.qvtd.pivot.qvtimperative.ImperativeTransformation;
import org.eclipse.qvtd.pivot.qvtimperative.LoopParameterBinding;
import org.eclipse.qvtd.pivot.qvtimperative.Mapping;
import org.eclipse.qvtd.pivot.qvtimperative.MappingCall;
import org.eclipse.qvtd.pivot.qvtimperative.MappingParameterBinding;
import org.eclipse.qvtd.pivot.qvtimperative.NewStatement;
import org.eclipse.qvtd.pivot.qvtimperative.NewStatementPart;
import org.eclipse.qvtd.pivot.qvtimperative.ObservableStatement;
import org.eclipse.qvtd.pivot.qvtimperative.SetStatement;
import org.eclipse.qvtd.pivot.qvtimperative.evaluation.QVTiModelsManager;
import org.eclipse.qvtd.pivot.qvtimperative.evaluation.EntryPointAnalysis;
import org.eclipse.qvtd.pivot.qvtimperative.evaluation.EntryPointsAnalysis;
import org.eclipse.qvtd.pivot.qvtimperative.evaluation.TypedModelAnalysis;
import org.eclipse.qvtd.pivot.qvtimperative.utilities.QVTimperativeUtil;
import org.eclipse.qvtd.runtime.evaluation.AbstractComputation;
import org.eclipse.qvtd.runtime.evaluation.AbstractInvocation;
import org.eclipse.qvtd.runtime.evaluation.AbstractSimpleInvocation;
import org.eclipse.qvtd.runtime.evaluation.AbstractTransformer;
import org.eclipse.qvtd.runtime.evaluation.Connection;
import org.eclipse.qvtd.runtime.evaluation.Interval;
import org.eclipse.qvtd.runtime.evaluation.InvalidEvaluationException;
import org.eclipse.qvtd.runtime.evaluation.InvocationConstructor;
import org.eclipse.qvtd.runtime.evaluation.InvocationFailedException;
import org.eclipse.qvtd.runtime.evaluation.ModeFactory;
import org.eclipse.qvtd.runtime.evaluation.SlotState;
import org.eclipse.qvtd.runtime.evaluation.TransformationExecutor;
import org.eclipse.qvtd.runtime.evaluation.Transformer;
import org.eclipse.qvtd.runtime.internal.evaluation.AbstractComputationConstructor;
import org.eclipse.qvtd.runtime.internal.evaluation.AbstractInvocationConstructor;
import org.eclipse.qvtd.runtime.internal.evaluation.RuntimeModelsManager;

import com.google.common.collect.Iterables;

/**
 * A QVTiCG2JavaVisitor supports generation of Java code from an optimized QVTi CG transformation tree.
 */
public class QVTiCG2JavaVisitor extends CG2JavaVisitor<@NonNull QVTiCodeGenerator> implements QVTiCGModelVisitor<@NonNull Boolean>
{
	protected /*static*/ class AllInstancesAnalysis extends TypedModelAnalysis
	{
		private @NonNull String @Nullable [] names = null;
		private @Nullable String extentOppositesName = null;

		public AllInstancesAnalysis(@NonNull EntryPointsAnalysis entryPointsAnalysis, @NonNull TypedModel typedModel, @NonNull Set<@NonNull CompleteClass> allInstancesCompleteClasses) {
			super(entryPointsAnalysis, typedModel, allInstancesCompleteClasses);
			ClassId extentClassId = QVTiModelsManager.EXTENT_CLASSID;
			Map<@NonNull Property, @NonNull Integer> opposites = entryPointsAnalysis.getCaches();
			for (@NonNull Property property : opposites.keySet()) {
				org.eclipse.ocl.pivot.Class owningClass = PivotUtil.getOwningClass(property);
				TypeId typeId = owningClass.getTypeId();
				if (typeId == extentClassId) {
					Integer index = opposites.get(property);
					assert index != null;
					Map<@NonNull Property, @NonNull String> oppositeProperties = qvtiGlobalContext.getOppositeProperties();
					assert oppositeProperties != null;
					extentOppositesName = oppositeProperties.get(property);
					//	extentOppositesName = "xyzzy";
				}
			}
		}

		public @Nullable String getExtentOppositesName() {
			return extentOppositesName;
		}

		protected @NonNull Map<@NonNull CompleteClass, @NonNull Integer> getInstancesCompleteClass2index() {
			return instancesCompleteClass2index;
		}

		protected @NonNull Map<@NonNull CompleteClass, @Nullable List<@NonNull CompleteClass>> getInstancesCompleteClassAnalysis() {
			return instancesCompleteClassAnalysis;
		}

		protected @NonNull String @NonNull [] getNames() {
			return ClassUtil.nonNullState(names);
		}

		protected @NonNull List<@NonNull CompleteClass> getSortedCompleteClasses() {
			return sortedCompleteClasses;
		}

		public void setNames(@NonNull String[] names) {
			this.names = names;
		}
	}

	/**
	 * A CachedInstance identifies the characteristics of a shared mapping invocation.
	 */
	private class CachedInstance
	{
		private org.eclipse.ocl.pivot.@NonNull Class asClass;
		private @NonNull CGExecutorType cgExecutorType;
		private @NonNull List<@NonNull CGExecutorProperty> cgProperties;
		private int modelIndex;

		protected CachedInstance(org.eclipse.ocl.pivot.@NonNull Class asClass, @NonNull CGExecutorType cgExecutorType,
				@NonNull List<@NonNull CGExecutorProperty> cgProperties, int modelIndex) {
			this.asClass = asClass;
			this.cgExecutorType = cgExecutorType;
			this.cgProperties = cgProperties;
			this.modelIndex = modelIndex;
		}

		public @NonNull Boolean doCachedInstance() {
			String instanceClassName = getNativeInstanceClassName(cgExecutorType);
			js.append("public class ");
			js.append(instanceClassName);
			js.append(" extends ");
			js.appendClassReference(null, AbstractEvaluationOperation.class);
			js.pushClassBody(instanceClassName);
			doCachedInstanceBasicEvaluate();
			js.append("\n");
			doCachedInstanceEvaluate();
			js.popClassBody(false);
			//
			js.append("\n");
			doCachedInstanceClassInstance();
			return true;
		}

		protected void doCachedInstanceBasicEvaluate() {
			js.append("@Override\n");
			js.append("public ");
			js.appendClassReference(false, Object.class);
			js.append(" basicEvaluate(");
			js.appendClassReference(true, Executor.class);
			js.append(" ");
			js.append(qvtiGlobalContext.getExecutorName());
			js.append(", ");
			js.appendClassReference(true, TypedElement.class);
			js.append(" ");
			js.append("caller");
			js.append(", ");
			js.appendClassReference(false, Object.class);
			js.append(" ");
			js.appendIsRequired(true);
			js.append(" [] ");
			js.append(JavaConstants.SOURCE_AND_ARGUMENT_VALUES_NAME);
			js.append(") {\n");
			js.pushIndentation(null);

			EClass eClass = (EClass)asClass.getESObject();
			assert eClass != null;
			String createMethodName = "create" + eClass.getName();
			boolean canSetNonNull = false;
			EPackage ePackage = eClass.getEPackage();
			String javaClass;
			if (ePackage != null) {
				Class<?> factoryClass = genModelHelper.getEcoreFactoryClass(ePackage);
				if (factoryClass != null) {
					javaClass = factoryClass.getName();
					Method factoryMethod = context.getLeastDerivedMethod(factoryClass, createMethodName);
					if (factoryMethod != null) {
						if (context.getIsNonNull(factoryMethod) == Boolean.TRUE) {
							canSetNonNull = true;
						}
					}
				}
				else {
					javaClass = genModelHelper.getQualifiedFactoryInterfaceName(ePackage);
				}
			}
			else {
				javaClass = null;
			}
			//
			String instanceName = "instance";
			js.appendTypeDeclaration(cgExecutorType);
			js.append(" " + instanceName + " = ");
			js.appendClassReference(null, javaClass);
			js.append(".eINSTANCE.");
			js.append(createMethodName);
			js.append("();\n");



			Map<@NonNull Property, @NonNull String> oppositeProperties = qvtiGlobalContext.getOppositeProperties();
			int i = 0;
			for (@NonNull CGExecutorProperty cgProperty : cgProperties) {
				Property asProperty = QVTiCGUtil.getAST(cgProperty);
				String setAccessor = genModelHelper.getSetAccessor((EStructuralFeature)asProperty.getESObject());
				if (cgProperty.getASTypeId() instanceof CollectionTypeId) {
					js.append("@SuppressWarnings(\"unchecked\") ");
				}
				else if (cgProperty.isRequired()) {
					if (js.appendSuppressWarningsNull(false)) {
						js.append(" ");
					}
				}
				js.appendTypeDeclaration(cgProperty);
				js.append(" value" + i + " = (");
				js.appendTypeDeclaration(cgProperty);
				js.append(")");
				js.append(JavaConstants.SOURCE_AND_ARGUMENT_VALUES_NAME);
				js.append("[" + i + "];\n");

				js.append(instanceName + "." + setAccessor + "(value" + i);
				js.append(");\n");

				if (oppositeProperties != null) {
					Property iProperty = QVTiCGUtil.getAST(cgProperty);
					String cacheName = oppositeProperties.get(iProperty);
					if (cacheName != null) {
						js.append(cacheName);
						js.append(".put(value" + i);
						js.append(", ");
						//						js.appendReferenceTo(outerTypeDescriptor, slotValue);
						js.append(instanceName);
						js.append(");\n");
					}
				}


				i++;


			}


			js.append(qvtiGlobalContext.getModelsName());
			js.append("[" + modelIndex + "].add(");
			js.append(instanceName);
			js.append(", false);\n");



			js.append("return " + instanceName + ";\n");
			js.popIndentation();
			js.append("}\n");
		}

		protected void doCachedInstanceEvaluate() {
			js.append("public ");
			js.appendClassReference(true, cgExecutorType);
			js.append(" ");
			js.append(globalContext.getEvaluateName());
			js.append("(");
			boolean isFirst = true;
			for (@NonNull CGExecutorProperty cgProperty : cgProperties) {
				if (!isFirst) {
					js.append(", ");
				}
				js.appendDeclaration(cgProperty);
				isFirst = false;
			}
			js.append(") {\n");
			js.pushIndentation(null);
			js.append("return (");
			js.appendClassReference(true, cgExecutorType);
			js.append(")");
			js.append(qvtiGlobalContext.getEvaluationCacheName());
			js.append(".");
			js.append(globalContext.getGetCachedEvaluationResultName());
			js.append("(this, caller, new ");
			js.appendClassReference(false, Object.class);
			js.append("[]{");
			isFirst = true;
			for (@NonNull CGExecutorProperty cgProperty : cgProperties) {
				if (!isFirst) {
					js.append(", ");
				}
				js.appendValueName(cgProperty);
				isFirst = false;
			}
			js.append("});\n");
			js.popIndentation();
			js.append("}\n");
		}

		protected void doCachedInstanceClassInstance() {
			String instanceClassName = getNativeInstanceClassName(cgExecutorType);
			js.append("protected final ");
			js.appendIsRequired(true);
			js.append(" ");
			js.append(instanceClassName);
			js.append(" ");
			js.append(getNativeInstanceInstanceName(cgExecutorType));
			js.append(" = new ");
			js.append(instanceClassName);
			js.append("();\n");
		}

		public void check(@NonNull List<@NonNull CGExecutorProperty> cgProperties, int modelIndex) {
			// TODO Auto-generated method stub
			//		List<@NonNull CGExecutorProperty> oldProperties = cachedInstances.put(cgType, cgProperties);
			//		if (oldProperties != null) {
			//			assert oldProperties.equals(cgProperties);
			//		}

		}
	}

	/**
	 * The run-time API version.
	 *
	 * @noreference this is solely for development usage.
	 */
	public static int RUN_TIME_EVALUATOR_API_VERSION = Transformer.RUN_TIME_EVALUATOR_API_VERSION_1_1_0_2;

	protected final @NonNull QVTiAnalyzer analyzer;
	protected final @NonNull QVTiGlobalContext qvtiGlobalContext;
	protected final @NonNull CGPackage cgPackage;
	protected final @Nullable Iterable<@NonNull CGValuedElement> sortedGlobals;
	protected boolean isGeneratedDebug = false;
	protected boolean isIncremental = false;
	protected boolean alwaysUseClasses = false;
	protected boolean useGot = true;

	/* Non-null while wrapping a function-implemented mapping in a SimpleInvocation */
	private @Nullable Mapping invocationWrapper = null;

	public QVTiCG2JavaVisitor(@NonNull QVTiCodeGenerator codeGenerator, @NonNull CGPackage cgPackage,
			@Nullable Iterable<@NonNull CGValuedElement> sortedGlobals) {
		super(codeGenerator);
		this.analyzer = codeGenerator.getAnalyzer();
		this.qvtiGlobalContext = getGlobalContext();
		this.cgPackage = cgPackage;
		this.sortedGlobals = sortedGlobals;
		this.isGeneratedDebug = codeGenerator.getOptions().isGeneratedDebug();
		this.isIncremental = codeGenerator.getOptions().isIncremental();
		this.alwaysUseClasses = isIncremental;
		this.useGot = isIncremental;
	}

	protected void appendConnectionBinding(@NonNull CGMappingCallBinding cgMappingCallBinding) {
		TypeDescriptor checkedType = needsTypeCheck(cgMappingCallBinding);
		if (checkedType != null) {
			needsTypeCheck(cgMappingCallBinding);
			js.append("(");
			js.appendClassReference(null, checkedType);
			js.append(")");
		}
		js.appendValueName(cgMappingCallBinding.getOwnedValue());
	}

	protected void appendEcoreSet(@NonNull CGValuedElement cgSlot, @NonNull EStructuralFeature eStructuralFeature, @NonNull CGValuedElement cgInit, boolean isPartial) {
		EClassifier eType = eStructuralFeature.getEType();
		String instanceClassName = eType.getInstanceClassName();
		if (eStructuralFeature.isMany()) {
			String getAccessor = genModelHelper.getGetAccessor(eStructuralFeature);
			//
			js.appendValueName(cgSlot);
			js.append(".");
			js.append(getAccessor);
			js.append("().");
			if (isPartial) {
				js.append("add(");
				if (instanceClassName != null) {
					js.appendEcoreValue(instanceClassName, cgInit);
				}
				else {
					js.appendAtomicReferenceTo(cgInit);
				}
			}
			else {
				js.append( "addAll(");		// FIXME may need to loop addAll manually
				js.appendAtomicReferenceTo(cgInit);
			}
			js.append(");\n");
		}
		else {
			String setAccessor = genModelHelper.getSetAccessor(eStructuralFeature);
			//
			js.appendValueName(cgSlot);
			js.append(".");
			js.append(setAccessor);
			js.append("(");
			if (instanceClassName != null) {
				js.appendEcoreValue(instanceClassName, cgInit);
			}
			else {
				js.appendAtomicReferenceTo(cgInit);
			}
			js.append(");\n");
		}
	}

	@Override		// FIXME promote to OCL
	protected void appendGuardFailure(@NonNull CGGuardExp cgGuardExp) {
		js.append("throw new ");
		js.appendClassReference(null, InvalidEvaluationException.class);
		js.append("(");
		js.appendString("Null " + cgGuardExp.getMessage());
		js.append(");\n");
	}

	protected void appendModelIndex(@Nullable CGTypedModel cgTypedModel) {
		if (cgTypedModel == null) {
			js.append("-1/*null*/");
		}
		else {
			js.append(cgTypedModel.getModelIndex() + "/*" + cgTypedModel.getName() + "*/");
		}
	}

	protected void appendModelReference(@Nullable CGTypedModel cgTypedModel) {
		js.append(qvtiGlobalContext.getModelsName());
		js.append("[");
		appendModelIndex(cgTypedModel);
		js.append("]");
	}

	protected void appendQualifiedLiteralName(@NonNull EStructuralFeature eStructuralFeature) {
		EClass eContainingClass = ClassUtil.nonNullState(eStructuralFeature.getEContainingClass());
		EPackage ePackage = ClassUtil.nonNullState(eContainingClass.getEPackage());
		js.appendClassReference(null, genModelHelper.getQualifiedPackageInterfaceName(ePackage));
		js.append(".Literals.");
		js.append(genModelHelper.getEcoreLiteralName(eStructuralFeature));
	}

	protected void appendThis(@NonNull CGElement cgElement) {
		for (EObject eObject = cgElement; eObject != null; eObject = eObject.eContainer()) {
			if (eObject instanceof CGMapping) {
				js.appendThis(getMappingName((CGMapping)eObject));		// + ".this"
				return;
			}
			if (eObject instanceof CGFunction) {
				js.appendThis(getFunctionName((CGFunction)eObject));		// + ".this"
				return;
			}
			if (eObject instanceof CGClass) {
				js.appendThis(ClassUtil.nonNullState(((CGClass)eObject).getName()));		// + ".this"
				return;
			}
		}
		assert false;
		js.appendThis("");		// "this"
		return;
	}

	protected void doAddRealization(@NonNull CGRealizedVariable cgRealizedVariable) {
		boolean isShared = cgRealizedVariable.getOwnedParts().size() > 0;
		CGTypedModel cgTypedModel = cgRealizedVariable.getTypedModel();
		NewStatement asNewStatement = QVTiCGUtil.getAST(cgRealizedVariable);
		//
		if (!isShared) {
			appendModelReference(cgTypedModel);
			js.append(".add(");
			js.appendValueName(cgRealizedVariable);
			js.append(", ");
			js.appendBooleanString(asNewStatement.isIsContained());
			js.append(");\n");
		}
		//
		if (isGeneratedDebug) {
			js.append("if (debugCreations) {\n");
			js.pushIndentation(null);
			js.appendClassReference(null, AbstractTransformer.class);
			js.append(".CREATIONS.println(\"created \"");
			js.append(" + toDebugString(");
			js.append(getResolvedName(cgRealizedVariable));
			js.append(")");
			js.append(");\n");
			js.popIndentation();
			js.append("}\n");
		}
		//
		if (isIncremental) {
			js.append(qvtiGlobalContext.getObjectManagerName());
			js.append(".created(");
			appendThis(cgRealizedVariable);
			js.append(", ");
			js.appendValueName(cgRealizedVariable);
			js.append(");\n");
		}
	}

	protected @Nullable List<@Nullable AllInstancesAnalysis> doAllInstances(@NonNull EntryPointsAnalysis entryPointsAnalysis) {
		CompleteModelInternal completeModel = environmentFactory.getCompleteModel();
		Set<@NonNull CompleteClass> allInstancesCompleteClasses = new HashSet<>();
		for (@NonNull CompleteClass allInstancesCompleteClass : entryPointsAnalysis.getAllInstancesCompleteClasses()) {
			allInstancesCompleteClasses.add(allInstancesCompleteClass);
		}
		if (allInstancesCompleteClasses.size() <= 0) {
			return null;
		}
		List<@Nullable AllInstancesAnalysis> allInstancesAnalyses = new ArrayList<>();
		int typedModelNumber = 0;
		for (@NonNull TypedModel typedModel : QVTimperativeUtil.getModelParameters(entryPointsAnalysis.getTransformation())) {
			Set<@NonNull CompleteClass> allInstancesCompleteClasses2 = new HashSet<>();
			for (org.eclipse.ocl.pivot.@NonNull Class usedClass : QVTimperativeUtil.getUsedClasses(typedModel)) {
				allInstancesCompleteClasses2.add(completeModel.getCompleteClass(usedClass));
			}
			allInstancesCompleteClasses2.retainAll(allInstancesCompleteClasses);
			if (!allInstancesCompleteClasses2.isEmpty()) {
				AllInstancesAnalysis allInstancesAnalysis = new AllInstancesAnalysis(entryPointsAnalysis, typedModel, allInstancesCompleteClasses2);
				Map<@NonNull CompleteClass, @NonNull Integer> instancesClass2index = allInstancesAnalysis.getInstancesCompleteClass2index();
				List<@NonNull CompleteClass> sortedCompleteClasses = allInstancesAnalysis.getSortedCompleteClasses();
				Map<@NonNull CompleteClass, @Nullable List<@NonNull CompleteClass>> instancesClassAnalysis = allInstancesAnalysis.getInstancesCompleteClassAnalysis();
				//
				//	Emit the ClassId array
				//
				js.append("/*\n");
				js.append(" * Array of the ClassIds of each class for which allInstances() may be invoked. Array index is the ClassIndex for TypedModel " + typedModelNumber + ".\n");
				js.append(" */\n");
				String classIndex2classIdName = qvtiGlobalContext.getClassIndex2classId(typedModelNumber);
				js.append("private static final ");
				js.appendClassReference(true, ClassId.class);
				js.append(" ");
				js.appendIsRequired(true);
				js.append(" [] ");
				js.append(classIndex2classIdName);
				js.append(" = new ");
				js.appendClassReference(true, ClassId.class);
				js.append("[]{\n");
				js.pushIndentation(null);
				for (int i = 0; i < sortedCompleteClasses.size(); i++) {
					CompleteClass instancesClass = sortedCompleteClasses.get(i);
					CGTypeId cgTypeId = getCodeGenerator().getAnalyzer().getTypeId(instancesClass.getPrimaryClass().getTypeId());
					int startLength = js.length();
					js.appendValueName(cgTypeId);
					if ((i+1) < sortedCompleteClasses.size()) {
						js.append(",");
					}
					for (int j = js.length() - startLength; j < 40; j++) {
						js.append(" ");
					}
					js.append("// " + i + " => " + instancesClass.getName() + "\n");
				}
				js.popIndentation();
				js.append("};\n");
				//
				//	Emit the classIndex2allClassIndexes array of arrays
				//
				String classIndex2allClassIndexes = qvtiGlobalContext.getClassIndex2allClassIndexes(typedModelNumber);
				js.append("\n");
				js.append("/*\n");
				js.append(" * Mapping from each TypedModel " + typedModelNumber + " ClassIndex to all the ClassIndexes\n");
				js.append(" * to which an object of the outer index may contribute results to an allInstances() invocation.\n");
				js.append(" * Non trivial inner arrays arise when one ClassId is a derivation of another and so an\n");
				js.append(" * instance of the derived classId contributes to derived and inherited ClassIndexes.\n");
				js.append(" */\n");
				js.append("private final static int ");
				js.appendIsRequired(true);
				js.append(" [] ");
				js.appendIsRequired(true);
				js.append(" [] ");
				js.append(classIndex2allClassIndexes);
				js.append(" = new int ");
				js.appendIsRequired(true);
				js.append(" [] ");
				js.appendIsRequired(true);
				js.append(" [] {\n");
				js.pushIndentation(null);
				for (int i = 0; i < sortedCompleteClasses.size(); i++) {
					CompleteClass instancesClass = sortedCompleteClasses.get(i);
					List<@NonNull CompleteClass> superInstancesClasses = ClassUtil.nonNullState(instancesClassAnalysis.get(instancesClass));
					int startLength = js.length();
					js.append("{");
					boolean isFirst = true;
					for (@NonNull CompleteClass superInstancesClass : superInstancesClasses) {
						if (!isFirst) {
							js.append(",");
						}
						js.append("" + instancesClass2index.get(superInstancesClass));
						isFirst = false;
					}
					js.append("}");
					if ((i+1) < sortedCompleteClasses.size()) {
						js.append(",");
					}
					for (int j = js.length() - startLength; j < 32; j++) {
						js.append(" ");
					}
					js.append("// " + i + " : ");
					js.append(instancesClass.getName());
					js.append(" -> {");
					isFirst = true;
					for (@NonNull CompleteClass superInstancesClass : superInstancesClasses) {
						if (!isFirst) {
							js.append(",");
						}
						js.append(superInstancesClass.getName());
						isFirst = false;
					}
					js.append("}\n");
				}
				js.popIndentation();
				js.append("};\n");
				allInstancesAnalysis.setNames(new @NonNull String[]{ classIndex2classIdName, classIndex2allClassIndexes});
				allInstancesAnalyses.add(allInstancesAnalysis);
			}
			else {
				allInstancesAnalyses.add(null);
			}
			typedModelNumber++;
		}
		return allInstancesAnalyses;
	}

	protected void doAssigned(@NonNull CGEcoreContainerAssignment cgPropertyAssignment) {
		EStructuralFeature eStructuralFeature = QVTiCGUtil.getEStructuralFeature(cgPropertyAssignment);
		CGValuedElement cgSlot = getExpression(QVTiCGUtil.getOwnedSlotValue(cgPropertyAssignment));
		CGValuedElement cgInit = getExpression(QVTiCGUtil.getOwnedInitValue(cgPropertyAssignment));
		if (isIncremental || ((SetStatement)cgPropertyAssignment.getAst()).isIsNotify()) {
			js.append(qvtiGlobalContext.getObjectManagerName());
			js.append(".assigned(");
			if (isIncremental) {
				appendThis(cgPropertyAssignment);
				js.append(", ");
			}
			js.appendValueName(cgInit);
			js.append(", ");
			appendQualifiedLiteralName(eStructuralFeature);
			js.append(", ");
			js.appendValueName(cgSlot);
			js.append(", false);\n");
		}
	}

	protected void doAssigned(@NonNull CGEcorePropertyAssignment cgPropertyAssignment, boolean isPartial) {
		EStructuralFeature eStructuralFeature = QVTiCGUtil.getEStructuralFeature(cgPropertyAssignment);
		CGValuedElement cgSlot = getExpression(QVTiCGUtil.getOwnedSlotValue(cgPropertyAssignment));
		CGValuedElement cgInit = getExpression(QVTiCGUtil.getOwnedInitValue(cgPropertyAssignment));
		if (isIncremental || ((SetStatement)cgPropertyAssignment.getAst()).isIsNotify()) {
			js.append(qvtiGlobalContext.getObjectManagerName());
			js.append(".assigned(");
			if (isIncremental) {
				appendThis(cgPropertyAssignment);
				js.append(", ");
			}
			js.appendValueName(cgSlot);
			js.append(", ");
			appendQualifiedLiteralName(eStructuralFeature);
			js.append(", ");
			js.appendValueName(cgInit);
			js.append(", ");
			js.appendBooleanString(eStructuralFeature.isMany() && isPartial);
			js.append(");\n");
		}
	}

	private void doAssigned(@NonNull CGGuardVariable cgGuardVariable, @NonNull EStructuralFeature eStructuralFeature, CGValuedElement cgInit) {
		CGValuedElement cgSlot = cgGuardVariable;
		//		CGValuedElement cgInit = getExpression(QVTiCGUtil.getOwnedInitValue(cgPropertyAssignment));
		//		if (isIncremental || ((SetStatement)cgGuardVariable.getAst()).isIsNotify()) {
		js.append(qvtiGlobalContext.getObjectManagerName());
		js.append(".assigned(");
		if (isIncremental) {
			appendThis(cgGuardVariable);
			js.append(", ");
		}
		js.appendValueName(cgSlot);
		js.append(", ");
		appendQualifiedLiteralName(eStructuralFeature);
		js.append(", ");
		js.appendValueName(cgInit);
		js.append(", false);\n");
		//		}
	}

	protected void doConstructor(@NonNull CGTransformation cgTransformation, @Nullable String oppositeName, @Nullable List<@Nullable AllInstancesAnalysis> allInstancesAnalyses) {
		//		String evaluatorName = ((QVTiGlobalContext)globalContext).getEvaluatorParameter().getName();
		String evaluatorName = qvtiGlobalContext.getExecutorName();
		String className = cgTransformation.getName();
		String transformationName = qvtiGlobalContext.getTransformationNameResolution().getResolvedName();
		Iterable<@NonNull CGTypedModel> cgTypedModels = QVTiCGUtil.getOwnedTypedModels(cgTransformation);
		//
		js.append("protected final ");
		js.appendIsRequired(true);
		js.append(" ");
		js.append(className);
		js.append(" ");
		js.append(transformationName);
		js.append(" = this;\n");
		js.append("\n");
		//
		js.append("public " + className + "(final ");
		js.appendClassReference(true, TransformationExecutor.class);
		js.append(" ");
		js.append(evaluatorName);
		js.append(") {\n");
		js.pushIndentation(null);
		js.append("super(");
		js.append(evaluatorName);
		js.append(", ");
		js.appendIntegerString(Iterables.size(cgTypedModels));
		js.append(");\n");
		if (oppositeName != null) {
			js.append("initOpposites(");
			js.append(oppositeName);
			js.append(");\n");
		}
		int modelNumber = 0;
		for (@NonNull CGTypedModel cgTypedModel : cgTypedModels) {
			js.append("initModel(");
			js.appendIntegerString(modelNumber);
			js.append(", ");
			String name = cgTypedModel.getName();
			js.appendString(name != null ? name : "");
			js.append(")");
			if (allInstancesAnalyses != null) {
				AllInstancesAnalysis allInstancesAnalysis = allInstancesAnalyses.get(modelNumber);
				if (allInstancesAnalysis != null) {
					js.append(".initClassIds(");
					js.append(allInstancesAnalysis.getNames()[0]);
					js.append(", ");
					js.append(allInstancesAnalysis.getNames()[1]);
					js.append(")");
					int extentClassIndex = allInstancesAnalysis.getExtentClassIndex();
					if (extentClassIndex >= 0) {
						js.append(".initExtent(");
						js.appendIntegerString(extentClassIndex);
						js.append(", ");
						String extentOppositesName = allInstancesAnalysis.getExtentOppositesName();
						js.append(extentOppositesName != null ? extentOppositesName : "null");
						js.append(")");
					}
				}
			}
			js.append(";\n");
			modelNumber++;
		}
		js.append("initConnections();\n");
		/*		ImperativeTransformation transformation = QVTiCGUtil.getAST(cgTransformation);
		EntryPointsAnalysis entryPointsAnalysis = context.getEntryPointsAnalysis(transformation);
		for (@NonNull EntryPointAnalysis entryPointAnalysis : entryPointsAnalysis.getEntryPointAnalyses()) {
			Iterable<@NonNull EAttribute> eAttributes = entryPointAnalysis.getSpeculatedEAttributes();
			if ((eAttributes != null) && !Iterables.isEmpty(eAttributes)) {
				js.append("initSpeculatedEAttributes(");
				js.appendString(entryPointAnalysis.getEntryPoint().getName());
				for (@NonNull EAttribute eAttribute : eAttributes) {
					js.append(", ");
					appendQualifiedLiteralName(eAttribute);
				}
				js.append(");\n");
			}
		} */
		//		doMappingConstructorInitializers(cgTransformation);
		//		doFunctionConstructorInitializers(cgTransformation);
		js.popIndentation();
		js.append("}\n");
	}

	/*	protected void doCreateInterval(@NonNull CGTransformation cgTransformation) {
		js.append("@Override\n");
		js.append("protected ");
		js.appendClassReference(true, Interval.class);
		js.append(" createInterval(int intervalIndex) {\n");
		js.pushIndentation(null);
		js.append("switch (intervalIndex) {\n");
		js.append("}\n");
		js.append("return new ");
		js.appendClassReference(null, DefaultInterval.class);
		js.append("(invocationManager, intervalIndex);\n");
		js.popIndentation();
		js.append("}\n");
	} */

	/*	protected void doCreateIncrementalManagers() {
		js.append("@Override\n");
		js.append("protected ");
		js.appendClassReference(true, InvocationManager.class);
		js.append(" createInvocationManager() {\n");
		js.pushIndentation(null);
		js.append("return new ");
		js.appendClassReference(null, IncrementalInvocationManager.class);
		js.append("(");
		js.append(qvtiGlobalContext.getExecutorName());
		js.append(");\n");
		js.popIndentation();
		js.append("}\n");
		js.append("\n");
		js.append("@Override\n");
		js.append("protected ");
		js.appendClassReference(true, ObjectManager.class);
		js.append(" createObjectManager() {\n");
		js.pushIndentation(null);
		js.append("return new ");
		js.appendClassReference(null, IncrementalObjectManager.class);
		js.append("((");
		js.appendClassReference(null, IncrementalInvocationManager.class);
		js.append(")invocationManager);\n");
		js.popIndentation();
		js.append("}\n");
	} */

	protected boolean doCreateRealizedVariable(@NonNull CGRealizedVariable cgRealizedVariable) {
		if (isIncremental) {
			js.appendClassReference(null, cgRealizedVariable);
			js.append(" ");
			js.appendValueName(cgRealizedVariable);
			js.append(" = this.");
			js.appendValueName(cgRealizedVariable);
			js.append(";\n");
			js.append("if (");
			js.appendValueName(cgRealizedVariable);
			js.append(" == null) {\n");
			js.pushIndentation(null);
		}
		boolean flowContinues = cgRealizedVariable.accept(this).booleanValue();
		if (flowContinues) {
			doAddRealization(cgRealizedVariable);
			//
			if (isIncremental) {
				js.append("assert ");
				js.appendValueName(cgRealizedVariable);
				js.append(" != null;\n");
				js.append("this.");
				js.appendValueName(cgRealizedVariable);
				js.append(" = ");
				js.appendValueName(cgRealizedVariable);
				js.append(";\n");
			}
		}
		if (isIncremental) {
			js.popIndentation();
			js.append("}\n");
		}
		return flowContinues;
	}

	/**
	 * Create a Ecore Class, returning
	 * false if an exception thrown for an abstract class
	 * null
	 * @param cgElement
	 * @param eClass
	 * @return
	 */
	protected boolean doEcoreCreateClass(@NonNull CGValuedElement cgElement, @NonNull EClass eClass, boolean setClassNonNull) {
		if (eClass.isAbstract()) {
			CGMapping cgMapping = QVTiCGUtil.getContainingCGMapping(cgElement);
			js.append("throw new ");
			js.appendClassReference(null, InvalidEvaluationException.class);
			js.append("(\"");
			js.append("Cannot create an instance of the abstract EClass ");
			js.append(LabelUtil.getLabel(eClass));
			js.append(" in ");
			js.append(LabelUtil.getLabel(cgMapping));
			js.append("\");\n");
			return false;
		}
		String createMethodName = "create" + eClass.getName();
		boolean canSetNonNull = false;
		EPackage ePackage = eClass.getEPackage();
		String javaClass;
		if (ePackage != null) {
			Class<?> factoryClass = genModelHelper.getEcoreFactoryClass(ePackage);
			if (factoryClass != null) {
				javaClass = factoryClass.getName();
				Method factoryMethod = context.getLeastDerivedMethod(factoryClass, createMethodName);
				if (factoryMethod != null) {
					if (context.getIsNonNull(factoryMethod) == Boolean.TRUE) {
						canSetNonNull = true;
					}
				}
			}
			else {
				javaClass = genModelHelper.getQualifiedFactoryInterfaceName(ePackage);
			}
		}
		else {
			javaClass = null;
		}
		//
		CGMapping cgMapping = QVTiCGUtil.basicGetContainingCGMapping(cgElement);
		if ((cgMapping == null) || !useClass(cgMapping) || !isIncremental) {
			js.append("final ");
			if (!canSetNonNull) {
				js.appendSuppressWarningsNull(false);
			}
			js.appendClassReference(true, cgElement);
			js.append(" ");
		}
		js.appendValueName(cgElement);
		js.append(" = ");
		boolean hasParts = false;
		if (cgElement instanceof CGRealizedVariable) {
			CGRealizedVariable cgRealizedVariable = (CGRealizedVariable)cgElement;
			NewStatement iNewStatement = QVTiCGUtil.getAST(cgRealizedVariable);
			List<NewStatementPart> ownedParts = iNewStatement.getOwnedParts();
			if (ownedParts.size() > 0) {
				hasParts = true;
				assert cgMapping != null;
				js.append(getNativeInstanceInstanceName(cgRealizedVariable.getExecutorType()));
				js.append(".evaluate(");
				boolean isFirst = true;
				for (@NonNull CGRealizedVariablePart cgPart : cgRealizedVariable.getOwnedParts()) {
					if (!isFirst) {
						js.append(", ");
					}
					js.appendValueName(cgPart.getInit());
					isFirst = false;
				}
				js.append(")");
				js.append(";\n");
			}
		}
		if (!hasParts) {
			js.appendClassReference(null, javaClass);
			js.append(".eINSTANCE.");
			js.append(createMethodName);
			js.append("();\n");
		}
		//		js.append("assert ");
		//		js.appendValueName(cgElement);
		//		js.append(" != null;\n");
		//
		if (setClassNonNull) {
			((CGVariable)cgElement).setNonNull();
		}
		return true;
	}

	protected boolean doEcoreCreateDataType(@NonNull CGValuedElement cgElement, @NonNull EDataType eDataType, @NonNull CGValuedElement cgInit) {
		//
		//	Availability of a GenPackage is mandatory since we must have an EFactory.createFromString method to do the construction.
		//
		final Class<?> javaClass = eDataType.getInstanceClass();
		if (javaClass == null) {
			throw new IllegalStateException("No Java class for " + cgElement + " in QVTiCG2JavaVisitor.doEcoreCreateDataType()");
		}
		final EPackage ePackage = eDataType.getEPackage();
		String nsURI = ePackage.getNsURI();
		if (nsURI == null) {
			throw new IllegalStateException("No EPackage NsURI for " + cgElement + " in QVTiCG2JavaVisitor.doEcoreCreateDataType()");
		}
		GenPackage genPackage = environmentFactory.getMetamodelManager().getGenPackage(nsURI);
		if (genPackage == null) {
			throw new IllegalStateException("No GenPackage for " + cgElement + " in QVTiCG2JavaVisitor.doEcoreCreateDataType()");
		}
		final String eFactoryName = genPackage.getQualifiedFactoryInterfaceName();
		final String ePackageName = genPackage.getQualifiedPackageInterfaceName();
		final String dataTypeName = CodeGenUtil.upperName(eDataType.getName());
		ClassLoader classLoader = eDataType.getClass().getClassLoader();
		Class<?> factoryClass;
		Class<?> packageClass;
		try {
			factoryClass = classLoader.loadClass(eFactoryName);
			packageClass = classLoader.loadClass(ePackageName);
		}
		catch (ClassNotFoundException e) {
			throw new IllegalStateException("Load class failure for " + cgElement + " in QVTiCG2JavaVisitor.doEcoreCreateDataType()", e);
		}
		//



		String createMethodName = qvtiGlobalContext.getCreateFromStringName().getResolvedName();
		boolean doSetNonNull = false;
		//		String javaClass2;
		//		Class<?> factoryClass2 = genModelHelper.getEcoreFactoryClass(ePackage);
		if (factoryClass != null) {
			//			javaClass2 = factoryClass.getName();
			Method factoryMethod = context.getLeastDerivedMethod(factoryClass, createMethodName);
			if (factoryMethod != null) {
				if (context.getIsNonNull(factoryMethod) == Boolean.TRUE) {
					doSetNonNull = true;
				}
			}
		}
		//		else {
		//			javaClass2 = genModelHelper.getQualifiedFactoryInterfaceName(ePackage);
		//		}
		//
		js.appendDeclaration(cgElement);
		js.append(" = ");
		js.append("(");
		js.appendClassReference(null, javaClass);
		js.append(")");
		js.appendClassReference(null, factoryClass);
		js.append(".eINSTANCE.");
		js.append(createMethodName);
		js.append("(");
		js.appendClassReference(null, packageClass);
		js.append(".Literals." + dataTypeName + ", ");
		js.appendValueName(cgInit);
		js.append(");\n");
		js.append("assert ");
		js.appendValueName(cgElement);
		js.append(" != null;\n");
		//
		return doSetNonNull;
	}

	protected boolean doFunctionBody(@NonNull CGFunction cgFunction) {
		CGValuedElement body = getExpression(cgFunction.getBody());
		ElementId elementId = cgFunction.getTypeId().getElementId();
		js.append(" {\n");
		js.pushIndentation(null);
		//		if (isIncremental) {
		//			js.append("super(\"");
		//			js.append(getFunctionName(cgFunction));
		//			js.append("\");\n");
		//		}
		//					js.appendCastParameters(localContext2, cgParameters);
		//					JavaDependencyVisitor dependencyVisitor = new JavaDependencyVisitor(localContext2, null);
		//					dependencyVisitor.visit(body);
		//					dependencyVisitor.visitAll(localContext2.getLocalVariables());
		//					Iterable<CGValuedElement> sortedDependencies = dependencyVisitor.getSortedDependencies();
		//					for (CGValuedElement cgElement : sortedDependencies) {
		//						if (!cgElement.isInlined() && cgElement.isConstant() && !cgElement.isGlobal()) {
		//							cgElement.accept(this);
		//						}
		//					}
		// FIXME merge locals into AST as LetExps.
		if (cgFunction.getBody() != null) {
			if (!js.appendLocalStatements(body)) {
				return false;
			}
			js.append("return ");
			js.appendValueName(body);
			js.append(";\n");
		}
		else {
			TypeId asTypeId = cgFunction.getASTypeId();
			if (asTypeId == TypeId.STRING) {			// FIXME Fudge for body-less functions
				js.append("return \"\";\n");
			}
			else if (asTypeId == TypeId.REAL) {			// FIXME Fudge for body-less functions
				js.append("return 0;\n");
			}
			else if (asTypeId == TypeId.INTEGER) {			// FIXME Fudge for body-less functions
				js.append("return 0;\n");
			}
			else if (asTypeId instanceof CollectionTypeId) {			// FIXME Fudge for body-less functions
				if (js.isUseNullAnnotations()) {
					js.appendSuppressWarningsNull(false);
					js.appendIsRequired(true);
					js.append(" ");
				}
				if (elementId != null) {
					TypeDescriptor javaTypeDescriptor = context.getUnboxedDescriptor(elementId);
					js.appendClassReference(null, javaTypeDescriptor);
				}
				String emptyListName = qvtiGlobalContext.getEmptyListName();
				js.append(" " + emptyListName + " = ");
				js.appendClassReference(null, Collections.class);
				js.append("." + emptyListName + "();\n");
				js.append("return " + emptyListName + ";\n");
			}
			else {			// FIXME Fudge for body-less functions
				js.append("return \"\";\n");
			}
		}
		js.popIndentation();
		js.append("}\n");
		return true;
	}

	protected boolean doFunctionBody(@NonNull CGFunction cgFunction, @NonNull String cachedResultName) {
		String functionName = getFunctionName(cgFunction);
		CGValuedElement cgBody = cgFunction.getBody();
		ElementId elementId = cgFunction.getTypeId().getElementId();
		// FIXME merge locals into AST as LetExps.
		if (cgBody != null) {
			CGValuedElement body = getExpression(cgBody);
			if (!js.appendLocalStatements(body)) {
				return false;
			}
			js.appendThis(functionName);
			js.append("." + cachedResultName + " = ");
			js.appendValueName(body);
			js.append(";\n");
		}
		/*	else if (QVTiCGUtil.getAST(cgFunction).getImplementationClass() != null) { -- Java Class has synthesized CGLibraryOperationCallExp
			final CGTypeId resultType = cgFunction.getTypeId();
			Function asFunction = QVTiCGUtil.getAST(cgFunction);
			TypeDescriptor functionTypeDescriptor = context.getTypeDescriptor(cgFunction).getEcoreDescriptor(context, null);
			//	js.append("/* " + localContext.getIdResolverVariable(cgFunction) + "* /");

			functionTypeDescriptor.appendBox(js, localContext, cgFunction, cgFunction);
			/*	js.appendClassReference(null, ValueUtil.class);
			js.append(".createSetValue(");
			js.appendValueName(resultType);
			js.append(", "); * /
			js.append(asFunction.getImplementationClass());
			js.append(".INSTANCE.evaluate(");
			js.append(qvtiGlobalContext.getExecutorName());
			js.append(", ");
			js.appendValueName(resultType);
			for (@NonNull CGParameter cgParameter : QVTiCGUtil.getParameters(cgFunction)) {
				js.append(", ");
				js.appendValueName(cgParameter);
			}
			js.append(");\n");
			js.appendThis(functionName);
			js.append("." + instanceName + " = ");
			js.appendValueName(cgFunction);
			js.append(";\n");
		} */
		else {
			TypeId asTypeId = cgFunction.getASTypeId();
			if (asTypeId == TypeId.STRING) {			// FIXME Fudge for body-less functions
				js.appendThis(functionName);
				js.append("." + cachedResultName + " = \"\";\n");
			}
			else if (asTypeId == TypeId.REAL) {			// FIXME Fudge for body-less functions
				js.appendThis(functionName);
				js.append("." + cachedResultName + " = 0;\n");
			}
			else if (asTypeId == TypeId.INTEGER) {			// FIXME Fudge for body-less functions
				js.appendThis(functionName);
				js.append("." + cachedResultName + " = 0;\n");
			}
			else if (asTypeId instanceof CollectionTypeId) {			// FIXME Fudge for body-less functions
				if (js.isUseNullAnnotations()) {
					js.appendSuppressWarningsNull(false);
					js.appendIsRequired(true);
					js.append(" ");
				}
				if (elementId != null) {
					TypeDescriptor javaTypeDescriptor = context.getUnboxedDescriptor(elementId);
					js.appendClassReference(null, javaTypeDescriptor);
				}
				String emptyListName = qvtiGlobalContext.getEmptyListName();
				js.append(" " + emptyListName + " = ");
				js.appendClassReference(null, Collections.class);
				js.append("." + emptyListName + "();\n");
				js.appendThis(functionName);
				js.append("." + cachedResultName + " = " + emptyListName + ";\n");
			}
			else {			// FIXME Fudge for body-less functions
				js.appendThis(functionName);
				js.append("." + cachedResultName + " = \"\";\n");
			}
		}
		return true;
	}

	protected boolean doFunctionBody2(@NonNull CGFunction cgFunction, @NonNull CGShadowExp cgShadowExp, @NonNull String cachedResultName) {
		Function function = QVTiCGUtil.getAST(cgFunction);
		ImperativeTransformation transformation = QVTimperativeUtil.getContainingTransformation(function);
		EntryPointsAnalysis entryPointsAnalysis = context.getEntryPointsAnalysis(transformation);
		String functionName = getFunctionName(cgFunction);
		js.append(" {\n");
		js.pushIndentation(null);
		if (isIncremental) {
			js.append("super(\"");
			js.append(functionName);
			js.append("\");\n");
		}
		EClassifier eClassifier = ClassUtil.nonNullState(cgShadowExp.getEcoreClassifier());
		if (eClassifier instanceof EDataType) {
			CGShadowPart cgShadowPart = ClassUtil.nullFree(cgShadowExp.getParts()).get(0);
			CGValuedElement cgInit = ClassUtil.nonNullState(cgShadowPart.getInit());
			if (!js.appendLocalStatements(cgInit)) {
				return false;
			}
			doEcoreCreateDataType(cgShadowExp, (EDataType)eClassifier, cgInit);
		}
		else if (eClassifier instanceof EClass) {
			if (!doEcoreCreateClass(cgShadowExp, (EClass)eClassifier, false)) {
				return false;
			}
			int index = 0;
			for (@NonNull CGShadowPart cgShadowPart : ClassUtil.nullFree(cgShadowExp.getParts())) {
				Property asProperty = ClassUtil.nonNullState(((ShadowPart)cgShadowPart.getAst()).getReferredProperty());
				EStructuralFeature eStructuralFeature = ClassUtil.nonNullState(getESObject(asProperty));
				js.appendValueName(cgShadowExp);
				js.append(".");
				if (eStructuralFeature.isMany()) {
					String getAccessor = genModelHelper.getGetAccessor(eStructuralFeature);
					//
					js.append(getAccessor);
					js.append("().addAll");
				}
				else {
					String setAccessor = genModelHelper.getSetAccessor(eStructuralFeature);
					//
					js.append(setAccessor);
				}
				js.append("(");
				int finalI = index++;
				SubStream castBody = new SubStream() {
					@Override
					public void append() {
						js.append("boundValues[" + finalI + "]");
					}
				};
				js.appendClassCast(cgShadowPart, castBody);
				js.append(");\n");
			}
		}
		//
		js.appendThis(functionName);
		js.append(".");
		js.append(cachedResultName);
		js.append(" = ");
		js.appendValueName(cgShadowExp);
		js.append(";\n");
		//
		EPackage ePackage = eClassifier.getEPackage();
		TypedModel bestOutputTypedModel = null;
		TypedModel bestMiddleTypedModel = null;
		TypedModel bestInputTypedModel = null;
		for (@NonNull TypedModel typedModel : QVTimperativeUtil.getModelParameters(entryPointsAnalysis.getTransformation())) {
			TypedModel imperativeTypedModel = null;
			for (org.eclipse.ocl.pivot.Package usedPackage : typedModel.getUsedPackage()) {
				if (usedPackage.getESObject() == ePackage) {
					imperativeTypedModel = typedModel;
				}
			}
			if (imperativeTypedModel != null) {
				if (QVTimperativeUtil.isOutput(imperativeTypedModel)) {
					bestOutputTypedModel = imperativeTypedModel;
				}
				else if (!QVTimperativeUtil.isInput(imperativeTypedModel)) {
					bestMiddleTypedModel = imperativeTypedModel;
				}
				else {
					bestInputTypedModel = imperativeTypedModel;
				}
			}
		}
		TypedModel asTypedModel = null;
		if (bestOutputTypedModel != null) {
			asTypedModel = bestOutputTypedModel;
		}
		else if (bestMiddleTypedModel != null) {
			asTypedModel = bestMiddleTypedModel;
		}
		else if (bestInputTypedModel != null) {
			asTypedModel = bestInputTypedModel;
		}
		if ((eClassifier instanceof EClass) && (asTypedModel != null)) {			// FIXME Why are shadow objects put in a model at all -- testQVTrCompiler_SeqToStm_CG requires it
			CGTypedModel cgTypedModel = context.getAnalyzer().getTypedModel(asTypedModel);
			appendModelReference(cgTypedModel);
			js.append(".add(");
			js.appendValueName(cgShadowExp);
			js.append(");\n");
		}
		//
		js.popIndentation();
		js.append("}\n");
		return true;
	}

	protected void doFunctionConstructor(@NonNull CGFunction cgFunction, @NonNull String cachedResultName) {
		String functionName = getFunctionName(cgFunction);
		String selfName = cgFunction.getVariantResolvedName(getCodeGenerator().getSELF_NameVariant());
		CGClass cgClass = ClassUtil.nonNullState(CGUtil.getContainingClass(cgFunction));
		List<@NonNull CGParameter> cgParameters = ClassUtil.nullFree(cgFunction.getParameters());
		CGValuedElement cgBody = cgFunction.getBody();
		if (cgBody != null) {
			js.appendCommentWithOCL(null, cgBody.getAst());
		}
		if (js.isUseNullAnnotations()) {
			js.append("@SuppressWarnings(\"null\")\n");		// Accurate casts are too hard
		}
		js.append("public ");
		js.append(functionName);
		js.append("(/*Nullable*/ Object ");
		js.appendIsRequired(true);
		js.append(" [] boundValues) {\n");
		js.pushIndentation(null);
		if (isIncremental) {
			js.append("super(\"");
			js.append(functionName);
			js.append("\");\n");
		}
		js.appendThis(functionName);
		js.append("." + selfName + " = (");
		js.appendClassReference(cgClass);
		js.append(")boundValues[0];\n");
		int i = 1;
		for (@NonNull CGParameter cgParameter : cgParameters) {
			String valueName = getResolvedName(cgParameter);
			js.appendThis(functionName);
			js.append(".");
			js.append(valueName);
			js.append(" = ");
			int finalI = i++;
			SubStream castBody = new SubStream() {
				@Override
				public void append() {
					js.append("boundValues[" + finalI + "]");
				}
			};
			js.appendClassCast(cgParameter, castBody);
			js.append(";\n");
		}
		doFunctionBody(cgFunction, cachedResultName);
		js.popIndentation();
		js.append("}\n");
	}

	protected void doFunctionConstructor(@NonNull CGFunction cgFunction, @NonNull CGShadowExp cgShadowExp, @NonNull String cachedResultName) {
		//		List<@NonNull CGParameter> cgParameters = ClassUtil.nullFree(cgFunction.getParameters());
		//		if (js.isUseNullAnnotations()) {
		//			js.append("@SuppressWarnings(\"null\")\n");		// Accurate casts are too hard
		//		}
		js.append("public ");
		js.append(getFunctionName(cgFunction));
		js.append("(/*Nullable*/ Object ");
		js.appendIsRequired(true);
		js.append(" [] boundValues) ");
		/*		int i = 0;
		for (@NonNull CGParameter cgParameter : cgParameters) {
			String valueName = getResolvedName(cgParameter);
			js.append(valueName);
			js.append(" = ");
//							js.appendClassCast(cgFreeVariable);
			if (cgParameter instanceof CGConnectionVariable) {
				js.append("(");
				js.appendClassReference(null, cgParameter);
				js.append(".Accumulator)");						// FIXME Embed properly as a nested typeid
			}
			else{
				js.appendClassCast(cgParameter);
			}
			js.append("boundValues[" + i++);
			js.append("];\n");
		} */
		doFunctionBody2(cgFunction, cgShadowExp, cachedResultName);
	}

	protected void doFunctionConstructorConstants(/*@NonNull*/ List<@NonNull CGOperation> cgOperations) {
		for (@NonNull CGOperation cgOperation : cgOperations) {
			if (cgOperation instanceof CGFunction) {
				CGFunction cgFunction = (CGFunction)cgOperation;
				if (useClass(cgFunction) || useCache(cgFunction)) {
					String functionName = getFunctionName(cgFunction);
					js.append("protected final ");
					js.appendClassReference(true, AbstractComputationConstructor.class);
					js.append(" " + getFunctionCtorName(cgFunction) + " = new ");
					js.appendClassReference(null, AbstractComputationConstructor.class);
					js.append("(idResolver)\n");
					js.append("{\n");
					js.pushIndentation(null);
					js.append("@Override\n");
					js.append("public ");
					js.appendIsRequired(true);
					js.append(" " + functionName + " newInstance(");
					js.appendClassReference(false, Object.class);
					js.append(" ");
					js.appendIsRequired(true);
					js.append(" [] values) {\n");
					js.pushIndentation(null);
					js.append("return new " + functionName + "(values);\n");
					js.popIndentation();
					js.append("}\n");
					js.popIndentation();
					js.append("};\n\n");
				}
			}
		}
	}

	/*	protected void doFunctionConstructorInitializers(@NonNull CGTransformation cgTransformation) {
		String className = cgTransformation.getName();
		for (@NonNull CGOperation cgOperation : ClassUtil.nullFree(cgTransformation.getOperations())) {
			if (cgOperation instanceof CGFunction) {
				CGFunction cgFunction = (CGFunction) cgOperation;
				if ((useClass(cgFunction) != null) || useCache(cgFunction)) {
					js.append(getFunctionCtorName(cgFunction) + " = ");
					js.appendClassReference(ClassUtil.class);
					js.append(".nonNullState(" + getFunctionName(cgFunction) + ".class.getConstructor(" + className + ".class, " + "Object[].class));\n");
				}
			}
		}
	} */

	protected void doFunctionGetInstance(@NonNull CGFunction cgFunction, @NonNull String cachedResultName) {
		js.append("@Override\n");
		js.append("public ");
		js.appendIsRequired(false);
		js.append(" Object");
		//		js.appendTypeDeclaration(ClassUtil.nonNullState(cgFunction.getBody()));
		js.append(" getResult() {\n");
		js.pushIndentation(null);
		js.append("return " + cachedResultName + ";\n");
		js.popIndentation();
		js.append("}\n");
	}

	protected void doFunctionIsEqual(@NonNull CGFunction cgFunction) {
		String functionName = getFunctionName(cgFunction);
		String selfName = cgFunction.getVariantResolvedName(getCodeGenerator().getSELF_NameVariant());
		js.append("@Override\n");
		js.append("public boolean isEqual(");
		js.appendClassReference(true, IdResolver.class);
		js.append(" idResolver, ");
		js.appendIsRequired(false);
		js.append(" Object ");
		js.appendIsRequired(true);
		js.append(" [] thoseValues) {\n");
		js.pushIndentation(null);
		js.append("return ");
		js.appendThis(functionName);
		js.append("." + selfName + " == thoseValues[0]");
		int index = 1;
		for (@NonNull CGParameter cgParameter : ClassUtil.nullFree(cgFunction.getParameters())) {
			js.append("\n\t&& ");
			js.append("idResolver.oclEquals(");	// FIXME oclEquals / ==
			js.appendThis(functionName);
			js.append(".");
			js.appendValueName(cgParameter);
			js.append(", thoseValues[" + index++ + "])");
		}
		js.append(";\n");
		js.popIndentation();
		js.append("}\n");
	}

	protected void doFunctionIsEqual(@NonNull CGShadowExp cgShadowExp, @NonNull String instanceName) {
		js.append("@Override\n");
		js.append("public boolean isEqual(");
		js.appendClassReference(true, IdResolver.class);
		js.append(" idResolver, ");
		js.appendIsRequired(false);
		js.append(" Object ");
		js.appendIsRequired(true);
		js.append(" [] thoseValues) {\n");
		js.pushIndentation(null);
		js.append("return ");
		int index = 0;
		for (@NonNull CGShadowPart cgShadowPart : ClassUtil.nullFree(cgShadowExp.getParts())) {
			if (index > 0) {
				js.append("\n\t&& ");
			}
			js.append("idResolver.oclEquals(");	// FIXME oclEquals / ==
			js.append(instanceName);
			js.append(".");
			Property asProperty = ClassUtil.nonNullState(((ShadowPart)cgShadowPart.getAst()).getReferredProperty());
			EStructuralFeature eStructuralFeature = ClassUtil.nonNullState(getESObject(asProperty));
			String getAccessor;
			if (eStructuralFeature == OCLstdlibPackage.Literals.OCL_ELEMENT__OCL_CONTAINER) {
				getAccessor = "eContainer";
			}
			else {
				getAccessor = genModelHelper.getGetAccessor(eStructuralFeature);
			}
			js.append(getAccessor);
			js.append("(), thoseValues[" + index++ + "])");
		}
		js.append(";\n");
		js.popIndentation();
		js.append("}\n");
	}

	protected void doGetting(@NonNull CGNavigationCallExp cgPropertyCallExp, @NonNull EStructuralFeature eStructuralFeature, boolean isOpposite) {
		Element asPropertyCallExp = cgPropertyCallExp.getAst();
		CGMapping cgMapping = QVTiCGUtil.basicGetContainingCGMapping(cgPropertyCallExp);
		Mapping asMapping = cgMapping != null ? (Mapping) cgMapping.getAst() : null;
		CGValuedElement source = getExpression(cgPropertyCallExp.getSource());
		boolean isHazardous = false;
		if ((asMapping != null) && (asPropertyCallExp instanceof NavigationCallExp)) {
			isHazardous = isHazardous2((NavigationCallExp) asPropertyCallExp);
		}
		if (isHazardous) {
			//
			js.append(qvtiGlobalContext.getObjectManagerName());
			js.append(".getting(");
			js.appendValueName(source);
			js.append(", ");
			appendQualifiedLiteralName(eStructuralFeature);
			js.append(", ");
			js.appendBooleanString(isOpposite);
			js.append(");\n");
		}
	}

	protected void doGot(@NonNull CGNavigationCallExp cgPropertyCallExp, @NonNull CGValuedElement source, @NonNull EStructuralFeature eStructuralFeature) {
		if (useGot) {
			//
			String objectManagerName = qvtiGlobalContext.getObjectManagerName();
			js.append(objectManagerName);
			js.append(".got(");
			//			if (localPrefix != null) {
			//				js.append(localPrefix);
			//				js.append(".");
			//			}
			appendThis(cgPropertyCallExp);
			js.append(", ");
			js.appendValueName(source);
			js.append(", ");
			if (!(cgPropertyCallExp instanceof CGOppositePropertyCallExp)) {
				appendQualifiedLiteralName(eStructuralFeature);
			}
			else {
				EReference eOpposite = ((EReference)eStructuralFeature).getEOpposite();
				if (eOpposite != null) {
					appendQualifiedLiteralName(eOpposite);
				}
				else {
					js.append(objectManagerName);
					js.append(".getEOppositeReference(");
					appendQualifiedLiteralName(eStructuralFeature);
					js.append(")");
				}
			}
			js.append(", ");
			js.appendValueName(cgPropertyCallExp);
			js.append(");\n");
		}
	}

	private void doInstanceCaches(@NonNull CGTransformation cgTransformation) {
		Map<org.eclipse.ocl.pivot.@NonNull Class, @NonNull CachedInstance> cachedInstances = new HashMap<>();
		for (@NonNull EObject element : new TreeIterable(cgTransformation, false)) {
			if (element instanceof CGRealizedVariable) {
				CGRealizedVariable cgRealizedVariable = (CGRealizedVariable)element;
				List<CGRealizedVariablePart> ownedParts = cgRealizedVariable.getOwnedParts();
				if (ownedParts.size() > 0) {
					CGExecutorType cgExecutorType = cgRealizedVariable.getExecutorType();
					org.eclipse.ocl.pivot.@NonNull Class asClass = (org.eclipse.ocl.pivot.Class) cgExecutorType.getAst();
					List<@NonNull CGExecutorProperty> cgProperties = new ArrayList<>();
					for (CGRealizedVariablePart ownedPart : ownedParts) {
						cgProperties.add(ownedPart.getExecutorProperty());
					}
					Collections.sort(cgProperties, NameUtil.NAMEABLE_COMPARATOR);
					NewStatement iNewStatement = QVTiCGUtil.getAST(cgRealizedVariable);
					TypedModel asTypedModel = ClassUtil.nonNullState(iNewStatement.getReferredTypedModel());
					CGTypedModel cgTypedModel = ClassUtil.nonNullState(analyzer.getTypedModel(asTypedModel));
					int modelIndex = cgTypedModel.getModelIndex();
					CachedInstance cachedInstance = cachedInstances.get(asClass);
					if (cachedInstance == null) {
						cachedInstance = new CachedInstance(asClass, cgExecutorType, cgProperties, modelIndex);
						cachedInstances.put(asClass, cachedInstance);
					}
					else {
						cachedInstance.check(cgProperties, modelIndex);
					}
				}
			}
		}
		List<org.eclipse.ocl.pivot.@NonNull Class> asClasses = new ArrayList<>(cachedInstances.keySet());
		Collections.sort(asClasses, NameUtil.NAMEABLE_COMPARATOR);
		for (org.eclipse.ocl.pivot.@NonNull Class asClass : asClasses) {
			CachedInstance cachedInstance = cachedInstances.get(asClass);
			assert cachedInstance != null;
			cachedInstance.doCachedInstance();
		}
	}

	protected void doInvocationWrapperPrefix(@NonNull Mapping invocationWrapper) {
		Integer firstPass = invocationWrapper.getFirstPass();
		if (firstPass == null) {
			js.append("invocationManager.flush();\n");	// Legacy support for auto-allocated pass numbers.
		}
		js.append("new ");
		js.appendClassReference(null, AbstractSimpleInvocation.class);
		js.append("(lazyCreateInterval(");
		//	js.appendIntegerString(firstPass != null ? firstPass : -1);
		js.append(Integer.toString(firstPass != null ? firstPass : -1));
		js.append("/*.." + invocationWrapper.getLastPass() + "*/");
		js.append("), ");
		js.appendString(PivotUtil.getName(invocationWrapper));
		js.append(") {\n");
		js.pushIndentation(null);
		js.append("@Override\n");
		js.append("public boolean execute() {\n");
		js.pushIndentation(null);
	}

	protected void doInvocationWrapperSuffix(@NonNull Mapping invocationWrapper) {
		js.append("return true;\n");
		js.popIndentation();
		js.append("}\n");
		js.popIndentation();
		js.append("};\n");
		Integer firstPass = invocationWrapper.getFirstPass();
		if (firstPass == null) {
			js.append("invocationManager.flush();\n");	// Legacy support for auto-allocated pass numbers.
		}
	}

	protected void doIsEqual(@NonNull List<@NonNull ? extends CGParameter> cgFreeVariables) {
		js.append("@Override\n");
		js.append("public boolean isEqual(");
		js.appendClassReference(true, IdResolver.class);
		js.append(" idResolver, ");
		js.appendIsRequired(true);
		js.append(" Object ");
		js.appendIsRequired(true);
		js.append(" [] thoseValues) {\n");
		js.pushIndentation(null);
		js.append("return ");
		if (cgFreeVariables.size() > 0) {
			int index = 0;
			for (@NonNull CGParameter cgFreeVariable : cgFreeVariables) {
				if (index > 0) {
					js.append("\n\t&& ");
				}
				js.append("idResolver.oclEquals(");
				js.append(getResolvedName(cgFreeVariable));
				js.append(", thoseValues[" + index++ + "])");
			}
		}
		else {
			js.append("true");
		}
		js.append(";\n");
		js.popIndentation();
		js.append("}\n");
	}

	protected void doMappingBody(@NonNull CGMapping cgMapping, @Nullable Iterable<@NonNull CGGuardVariable> cgGuardVariables) {
		CGValuedElement cgBody = cgMapping.getOwnedBody();
		js.append(" {\n");
		js.pushIndentation(null);
		if ((cgGuardVariables != null) && isGeneratedDebug) {
			js.append("if (debugInvocations) {\n");
			js.pushIndentation(null);
			js.appendClassReference(null, AbstractTransformer.class);
			js.append(".INVOCATIONS.println(\"invoke " + getMappingName(cgMapping) + "\"");
			for (@NonNull CGGuardVariable cgGuardVariable : cgGuardVariables) {
				if (!(cgGuardVariable instanceof CGConnectionVariable)) {
					js.append(" +\n\t\"\\n\\t");
					//	js.append(cgGuardVariable.getClass().getSimpleName());
					//	js.append(", ");
					js.append("\\\"" + cgGuardVariable.getName() + "\\\":\"");
					js.append(" + toDebugString(");
					js.append(getResolvedName(cgGuardVariable));
					js.append(")");
					Element ast = cgGuardVariable.getAst();
					if (ast instanceof TypedElement) {
						org.eclipse.ocl.pivot.Class type = PivotUtil.getClass((TypedElement)ast);
						Property trace2dispatcherProperty = NameUtil.getNameable(PivotUtil.getOwnedProperties(type), "dispatcher");
						if (trace2dispatcherProperty != null) {
							js.append(" +\n\t\"\\n\\t");
							js.append("\\\"dispatcher\\\":\"");
							js.append(" + toDebugString(");
							js.append(getResolvedName(cgGuardVariable));
							js.append(".getDispatcher())");
							for (Property dispatcherProperty : PivotUtil.getOwnedProperties(PivotUtil.getClass(trace2dispatcherProperty))) {
								String name = PivotUtil.getName(dispatcherProperty);
								if ((name.length() >= 2) && (name.charAt(0) == 'd') && Character.isDigit(name.charAt(1))) {
									js.append(" +\n\t\"\\n\\t");
									js.append("\\\"dispatcher." + name + "\\\":\"");
									js.append(" + toDebugString(");
									js.append(getResolvedName(cgGuardVariable));
									String prefix = dispatcherProperty.getTypeId() == TypeId.BOOLEAN ? "is" : "get";		// FIXME Use GenModel
									js.append(".getDispatcher()." + prefix + Character.toUpperCase(name.charAt(0)) + name.substring(1) + "())");

								}
							}
						}
					}
				}
				//				}
			}
			js.append(");\n");
			js.popIndentation();
			js.append("}\n");
		}
		//		if (cgBody.isInvalid()) {
		//			js.append("return handleExecutionFailure(\"" + getMappingName(cgMapping) + "\", ");
		//			js.appendValueName(cgBody);
		//			js.append(");\n");
		//		}
		//		else {
		//			js.append("try {\n");
		//			js.pushIndentation(null);
		if (!cgBody.isInlined()) {
			cgBody.accept(this);
		}
		//		if (cgGuardVariables != null)  {
		for (@NonNull CGGuardVariable cgGuardVariable : QVTiCGUtil.getOwnedGuardVariables(cgMapping)) {
			VariableDeclaration asGuardVariable = QVTiCGUtil.getAST(cgGuardVariable);
			if (asGuardVariable instanceof GuardParameter) {
				GuardParameter asGuardParameter = (GuardParameter)asGuardVariable;
				Property successProperty = asGuardParameter.getSuccessProperty();
				if (successProperty != null) {
					EStructuralFeature eStructuralFeature = ClassUtil.nonNullState((EStructuralFeature) successProperty.getESObject());
					String setAccessor = genModelHelper.getSetAccessor(eStructuralFeature);
					//
					js.appendValueName(cgGuardVariable);
					js.append(".");
					js.append(setAccessor);
					js.append("(");
					js.appendValueName(cgBody);
					js.append(");\n");
					//	js.append("if (");
					//	js.appendValueName(cgBody);
					//	js.append(") {\n");
					//	js.pushIndentation(null);
					doAssigned(cgGuardVariable, eStructuralFeature, cgBody);
					//	js.popIndentation();
					//	js.append("}\n");
				}
			}
		}
		//		}
		if (cgGuardVariables != null)  {
			if (isGeneratedDebug) {
				js.append("if (debugInvocations) {\n");
				js.pushIndentation(null);
				js.appendClassReference(null, AbstractTransformer.class);
				js.append(".INVOCATIONS.println((");
				js.appendValueName(cgBody);
				js.append(" ? \"done \"  : \"fail \") + \"" + getMappingName(cgMapping) + "\");\n");
				js.popIndentation();
				js.append("}\n");
			}
		}
		js.append("return ");
		js.appendValueName(cgBody);
		js.append(";\n");
		//			js.popIndentation();
		//			js.append("} catch (Throwable e) {\n");
		//			js.pushIndentation(null);
		//			js.append("return handleExecutionFailure(\"" + getMappingName(cgMapping) + "\", e);\n");
		//			js.popIndentation();
		//			js.append("}\n");
		//		}
		js.popIndentation();
		js.append("}\n");
	}

	public @NonNull Boolean doMappingCall_Class(@NonNull CGMappingCall cgMappingCall) {
		MappingCall pMappingCall = QVTiCGUtil.getAST(cgMappingCall);
		Mapping pReferredMapping = QVTimperativeUtil.getReferredMapping(pMappingCall);
		CGMapping cgReferredMapping = analyzer.getMapping(pReferredMapping);
		assert cgReferredMapping != null;
		Iterable<@NonNull CGMappingCallBinding> cgMappingCallBindings = QVTiCGUtil.getOwnedMappingCallBindings(cgMappingCall);
		//
		//	Set loopVariable non-null if it needs to be type-checked and cast to a narrower type.
		//
		for (@NonNull CGMappingCallBinding cgMappingCallBinding : cgMappingCallBindings) {
			CGValuedElement ownedValue = cgMappingCallBinding.getOwnedValue();
			TypeDescriptor checkedType = needsTypeCheck(cgMappingCallBinding);
			if (checkedType != null) {
				js.append("if (");
				js.appendValueName(ownedValue);
				js.append(" instanceof ");
				js.appendClassReference(null, checkedType);
				js.append(") {\n");
				js.pushIndentation(null);
			}
			else if (!ownedValue.isNonNull()) {
				Element asMappingParameterBinding = cgMappingCallBinding.getAst();
				if (!(asMappingParameterBinding instanceof GuardParameterBinding)) {		// FIXME this should be part of isNonNull
					js.append("if (");
					js.appendValueName(ownedValue);
					js.append(" != null) {\n");
					js.pushIndentation(null);
				}
			}
		}
		//
		//	Emit the mapping call.
		//
		Iterable<@NonNull CGMappingCallBinding> iterateBindings = getIterateBindings(cgMappingCallBindings);
		String mappingCtorName = getMappingCtorName(cgReferredMapping);
		if (iterateBindings == null) {
			for (CGMappingCallBinding cgMappingCallBinding : cgMappingCallBindings) {
				Element ast = cgMappingCallBinding.getAst();
				js.append(mappingCtorName);
				js.append(".");
				js.append(ast instanceof AppendParameterBinding ? "addAppendedConnection" : "addConsumedConnection");
				js.append("(");
				appendConnectionBinding(cgMappingCallBinding);
				js.append(");\n");
			}
		}
		else {
			js.append(mappingCtorName);
			js.append(".invoke(");
			boolean isFirst = true;
			for (@NonNull CGMappingCallBinding cgMappingCallBinding : cgMappingCallBindings) {
				if (!isFirst) {
					js.append(", ");
				}
				TypeDescriptor checkedType = needsTypeCheck(cgMappingCallBinding);
				if (checkedType != null) {
					js.append("(");
					js.appendClassReference(null, checkedType);
					js.append(")");
				}
				js.appendValueName(cgMappingCallBinding.getOwnedValue());
				isFirst = false;
			}
			js.append(");\n");
		}
		//
		//	End the type check.
		//
		for (@NonNull CGMappingCallBinding cgMappingCallBinding : cgMappingCallBindings) {
			TypeDescriptor checkedType = needsTypeCheck(cgMappingCallBinding);
			if (checkedType != null) {
				js.popIndentation();
				js.append("}\n");
			}
			else if (!cgMappingCallBinding.getOwnedValue().isNonNull()) {
				Element asMappingParameterBinding = cgMappingCallBinding.getAst();
				if (!(asMappingParameterBinding instanceof GuardParameterBinding)) {		// FIXME this should be part of isNonNull
					js.popIndentation();
					js.append("}\n");
				}
			}
		}
		return true;
	}

	public @NonNull Boolean doMappingCall_Function(@NonNull CGMappingCall cgMappingCall) {
		MappingCall pMappingCall = QVTiCGUtil.getAST(cgMappingCall);
		Mapping pReferredMapping = QVTimperativeUtil.getReferredMapping(pMappingCall);
		if (invocationWrapper == null) {
			doInvocationWrapperPrefix(pReferredMapping);
		}
		CGMapping cgReferredMapping = analyzer.getMapping(pReferredMapping);
		assert cgReferredMapping != null;
		Iterable<@NonNull CGMappingCallBinding> cgMappingCallBindings = QVTiCGUtil.getOwnedMappingCallBindings(cgMappingCall);
		//
		//	Set loopVariable non-null if it needs to be type-checked and cast to a narrower type.
		//
		for (@NonNull CGMappingCallBinding cgMappingCallBinding : cgMappingCallBindings) {
			MappingParameterBinding asMappingParameterBinding = (MappingParameterBinding)cgMappingCallBinding.getAst();
			if (asMappingParameterBinding instanceof AppendParameterBinding) {
			}
			else if (asMappingParameterBinding instanceof GuardParameterBinding) {
				js.append("for (");
				js.appendClassReference(Boolean.TRUE, cgMappingCallBinding);
				js.append(" ");
				js.appendValueName(cgMappingCallBinding);
				js.append(" : ");
				js.appendValueName(cgMappingCallBinding.getOwnedValue());
				js.append(".typedIterable(");
				js.appendClassReference(null, cgMappingCallBinding);
				js.append(".class)");
				js.append(") {\n");
				js.pushIndentation(null);
				// FIXME typeCheck
			}
			else {
				TypeDescriptor checkedType = needsTypeCheck(cgMappingCallBinding);
				if (checkedType != null) {
					js.append("if (");
					js.appendValueName(cgMappingCallBinding.getOwnedValue());
					js.append(" instanceof ");
					js.appendClassReference(null, checkedType);
					js.append(") {\n");
					js.pushIndentation(null);
				}
				else if (!cgMappingCallBinding.getOwnedValue().isNonNull()) {
					js.append("if (");
					js.appendValueName(cgMappingCallBinding.getOwnedValue());
					js.append(" != null) {\n");
					js.pushIndentation(null);
				}
			}
		}
		//
		//	Emit the mapping call.
		//
		js.append(getMappingName(cgReferredMapping) + "(");
		boolean isFirst = true;
		for (@NonNull CGMappingCallBinding cgMappingCallBinding : cgMappingCallBindings) {
			if (!isFirst) {
				js.append(", ");
			}
			TypeDescriptor checkedType = needsTypeCheck(cgMappingCallBinding);
			if (checkedType != null) {
				js.append("(");
				js.appendClassReference(null, checkedType);
				js.append(")");
			}
			MappingParameterBinding asMappingParameterBinding = (MappingParameterBinding)cgMappingCallBinding.getAst();
			if (asMappingParameterBinding instanceof GuardParameterBinding) {
				js.appendValueName(cgMappingCallBinding);
			}
			else {
				js.appendValueName(cgMappingCallBinding.getOwnedValue());
			}
			isFirst = false;
		}
		js.append(");\n");
		//
		//	End the type check.
		//
		for (@NonNull CGMappingCallBinding cgMappingCallBinding : cgMappingCallBindings) {
			MappingParameterBinding asMappingParameterBinding = (MappingParameterBinding)cgMappingCallBinding.getAst();
			if (asMappingParameterBinding instanceof AppendParameterBinding) {
			}
			else if (asMappingParameterBinding instanceof GuardParameterBinding) {
				js.popIndentation();
				js.append("}\n");
			}
			else {TypeDescriptor checkedType = needsTypeCheck(cgMappingCallBinding);
			if (checkedType != null) {
				js.popIndentation();
				js.append("}\n");
			}
			else if (!cgMappingCallBinding.getOwnedValue().isNonNull()) {
				js.popIndentation();
				js.append("}\n");
			}
			}
		}
		if (invocationWrapper == null) {
			doInvocationWrapperSuffix(pReferredMapping);
		}
		return true;
	}

	protected void doMappingConnectionVariable(@NonNull CGGuardVariable cgFreeVariable) {
		if (cgFreeVariable instanceof CGConnectionVariable) {
			js.append("final ");
			js.appendClassReference(true, isIncremental ? Connection.Incremental.class : Connection.class);
			js.append(" ");
			js.append(getResolvedName(cgFreeVariable));
		}
		else{
			js.getBoxedTypeRepresentation().appendDeclaration(cgFreeVariable);
		}
	}

	protected void doMappingConstructor(@NonNull CGMapping cgMapping) {
		String constructorName = qvtiGlobalContext.getConstructorName();
		Iterable<@NonNull CGGuardVariable> cgGuardVariables = QVTiCGUtil.getOwnedGuardVariables(cgMapping);
		//		if (js.isUseNullAnnotations()) {
		//			js.append("@SuppressWarnings(\"null\")\n");		// Accurate casts are too hard
		//		}
		js.append("public ");
		js.append(getMappingName(cgMapping));
		js.append("(");
		js.appendClassReference(true, isIncremental ? InvocationConstructor.Incremental.class : InvocationConstructor.class);
		js.append(" ");
		js.append(constructorName);
		if (isIncremental) {
			js.append(", int ");
			js.append(qvtiGlobalContext.getInvocationHashCodeName());
		}
		js.append(", ");
		js.appendIsRequired(true);
		js.append(" Object ");
		js.appendIsRequired(true);
		js.append(" [] boundValues) {\n");
		js.pushIndentation(null);
		//		if (isIncremental) {
		js.append("super(");
		js.append(constructorName);
		if (isIncremental) {
			js.append(", ");
			js.append(qvtiGlobalContext.getInvocationHashCodeName());
		}
		js.append(");\n");
		//
		int i = 0;
		for (@NonNull CGGuardVariable cgGuardVariable : cgGuardVariables) {
			String valueName = getResolvedName(cgGuardVariable);
			js.append(valueName);
			js.append(" = ");
			//							js.appendClassCast(cgFreeVariable);
			int finalI = i++;
			SubStream castBody = new SubStream() {
				@Override
				public void append() {
					js.append("boundValues[" + finalI + "]");
				}
			};
			if (cgGuardVariable instanceof CGConnectionVariable) {
				js.append("(");
				//				js.appendClassReference(null, cgFreeVariable);
				//				js.append(".Accumulator)");						// FIXME Embed properly as a nested typeid
				js.appendClassReference(null, isIncremental ? Connection.Incremental.class : Connection.class);
				js.append(")");						// FIXME Embed properly as a nested typeid
				castBody.append();
			}
			else{
				js.appendClassCast(cgGuardVariable, castBody);
			}
			js.append(";\n");
		}
		js.popIndentation();
		js.append("}\n");
	}

	protected void doMappingConstructorConstants(/*@NonNull*/ List<@NonNull CGMapping> cgMappings) {
		for (@NonNull CGMapping cgMapping : cgMappings) {
			if (useClass(cgMapping)) {// && (isIncremental || (cgMapping.getFreeVariables().size() > 0))) {
				Mapping asMapping = QVTiCGUtil.getAST(cgMapping);
				Class<?> constructorClass = isIncremental ? AbstractInvocationConstructor.Incremental.class : AbstractInvocationConstructor.class;
				js.append("protected final ");
				js.appendClassReference(true, constructorClass);
				js.append(" " + getMappingCtorName(cgMapping) + " = new ");
				js.appendClassReference(null, constructorClass);
				js.append("(invocationManager, ");
				js.appendString(QVTiCGUtil.getName(cgMapping));
				if (!isIncremental) {
					js.append(", ");
					js.appendBooleanString(QVTiCGUtil.getAST(cgMapping).isIsStrict());
				}
				js.append(", lazyCreateInterval(");
				Integer firstPass = asMapping.getFirstPass();
				//	js.appendIntegerString(firstPass != null ? firstPass : -1);
				js.append(Integer.toString(firstPass != null ? firstPass : -1));
				js.append("))\n");
				js.append("{\n");
				js.pushIndentation(null);
				js.append("@Override\n");
				js.append("public ");
				js.appendIsRequired(true);
				js.append(" " + getMappingName(cgMapping) + " newInstance(");
				if (isIncremental) {
					js.append("int ");
					js.append(qvtiGlobalContext.getInvocationHashCodeName());
					js.append(", ");
				}
				js.appendClassReference(true, Object.class);
				js.append(" ");
				js.appendIsRequired(true);
				js.append(" [] values) {\n");
				js.pushIndentation(null);
				js.append("return new " + getMappingName(cgMapping) + "(");
				js.append("this");
				if (isIncremental) {
					js.append(", ");
					js.append(qvtiGlobalContext.getInvocationHashCodeName());
				}
				js.append(", ");
				js.append("values);\n");
				js.popIndentation();
				js.append("}\n");
				js.popIndentation();
				js.append("};\n\n");
			}
		}
	}

	protected void doMappingDestroy(@NonNull CGMapping cgMapping) {
		js.append("/*\n");
		js.append(" * Eliminate all trace of the construction and execution of this invocation.\n");
		js.append(" */\n");
		js.append("@Override\n");
		js.append("public synchronized void destroy() {\n");
		js.pushIndentation(null);
		js.append("/*\n");
		js.append(" * Remove this invocation from the invocation cache.\n");
		js.append(" * Revoke all object property assignments.\n");
		js.append(" * Revoke all consumed input objects.\n");
		js.append(" */\n");
		js.append("super.destroy();\n");
		/*		Iterable<@NonNull CGGuardVariable> ownedGuardVariables = QVTiCGUtil.getOwnedGuardVariables(cgMapping);
		if (!Iterables.isEmpty(ownedGuardVariables)) {
			boolean firstConsume = true;
			int i = 0;
			for (@NonNull CGGuardVariable cgGuardVariable : ownedGuardVariables) {
				if (!(cgGuardVariable instanceof CGConnectionVariable)) {
					if (firstConsume) {
						js.append("/*\n");
						js.append(" * Revoke all consumed input objects.\n");
						js.append(" * /\n");
						js.appendClassReference(List.class, Connection.Incremental.class);
						js.append(" consumedConnections = ");
						js.append(QVTiGlobalContext.CONSTRUCTOR_NAME);
						js.append(".getConsumedConnections();\n");
						firstConsume = false;
					}
					js.append("consumedConnections.get(" + i++ + ").revokeConsumer(");
					js.appendValueName(cgGuardVariable);
					js.append(", this);\n");
				}
			}
		} */
		boolean firstAppend = true;
		for (@NonNull EObject eObject : new TreeIterable(cgMapping, false)) {
			if (eObject instanceof CGConnectionAssignment) {
				if (firstAppend) {
					js.append("/*\n");
					js.append(" * Revoke all appended output objects.\n");
					js.append(" */\n");
					firstAppend = false;
				}
				CGConnectionAssignment cgConnectionAssignment = (CGConnectionAssignment)eObject;
				js.append("if (this.");
				js.appendValueName(cgConnectionAssignment);
				js.append(" != null) {\n");
				js.pushIndentation(null);
				js.append("this.");
				js.appendValueName(cgConnectionAssignment.getConnectionVariable());
				js.append(".revoke(");
				js.appendValueName(cgConnectionAssignment);
				js.append(");\n");
				js.append("this.");
				js.appendValueName(cgConnectionAssignment);
				js.append(" = null;\n");
				js.popIndentation();
				js.append("}\n");
			}
		}
		Iterable<@NonNull CGRealizedVariable> ownedRealizedVariables = QVTiCGUtil.getOwnedRealizedVariables(cgMapping);
		if (!Iterables.isEmpty(ownedRealizedVariables)) {
			js.append("/*\n");
			js.append(" * Destroy all created objects.\n");
			js.append(" */\n");
			for (@NonNull CGRealizedVariable cgRealizedVariable : ownedRealizedVariables) {
				CGTypedModel cgTypedModel = cgRealizedVariable.getTypedModel();
				//
				js.appendClassReference(null, cgRealizedVariable);
				js.append(" ");
				js.appendValueName(cgRealizedVariable);
				js.append(" = this.");
				js.appendValueName(cgRealizedVariable);
				js.append(";\n");
				js.append("if (");
				js.appendValueName(cgRealizedVariable);
				js.append(" != null) {\n");
				js.pushIndentation(null);
				js.append("((");
				js.appendClassReference(null, RuntimeModelsManager.Model.Incremental.class);
				js.append(")");
				appendModelReference(cgTypedModel);
				js.append(").remove(");
				js.appendValueName(cgRealizedVariable);
				js.append(");\n");
				js.append(qvtiGlobalContext.getObjectManagerName());
				js.append(".destroyed(");
				js.appendValueName(cgRealizedVariable);
				js.append(");\n");
				js.append("this.");
				js.appendValueName(cgRealizedVariable);
				js.append(" = null;\n");
				js.popIndentation();
				js.append("}\n");
			}
		}
		js.popIndentation();
		js.append("}\n");
	}

	protected boolean doMappingFields(@NonNull CGMapping cgMapping) {
		boolean needsNewLine = false;
		for (@NonNull CGGuardVariable cgVariable : ClassUtil.nullFree(cgMapping.getOwnedGuardVariables())) {
			js.append("protected ");
			doMappingConnectionVariable(cgVariable);
			js.append(";\n");
			needsNewLine = true;
		}
		if (isIncremental) {
			for (@NonNull CGRealizedVariable cgVariable : QVTiCGUtil.getOwnedRealizedVariables(cgMapping)) {
				js.append("protected ");
				js.appendClassReference(false, cgVariable);
				js.append(" ");
				js.appendValueName(cgVariable);
				if (isIncremental) {
					js.append(" = null");
				}
				js.append(";\n");
				needsNewLine = true;
			}
			for (@NonNull EObject eObject : new TreeIterable(cgMapping, false)) {
				if (eObject instanceof CGConnectionAssignment) {
					CGConnectionAssignment cgConnectionAssignment = (CGConnectionAssignment)eObject;
					js.append("protected ");
					js.appendClassReference(false, Object.class);
					js.append(" ");
					js.appendValueName(cgConnectionAssignment);
					js.append(" = null;\n");
					needsNewLine = true;
				}
			}
		}
		return needsNewLine;
	}

	protected void doMappingGetBoundValue(@NonNull CGMapping cgMapping) {
		Iterable<@NonNull CGGuardVariable> cgGuardVariables = QVTiCGUtil.getOwnedGuardVariables(cgMapping);
		js.append("/*\n");
		js.append(" * Return the index'th bound value.\n");
		js.append(" */\n");
		js.append("@Override\n");
		js.append("public ");
		js.appendClassReference(true, Object.class);
		js.append(" getBoundValue(int index) {\n");
		js.pushIndentation(null);
		js.append("switch(index) {\n");
		js.pushIndentation(null);
		int i = 0;
		for (@NonNull CGGuardVariable cgGuardVariable : cgGuardVariables) {
			String valueName = getResolvedName(cgGuardVariable);
			js.append("case " + i++ + ": return ");
			js.append(valueName);
			js.append(";\n");
		}
		js.popIndentation();
		js.append("}\n");
		js.append("throw new ");
		js.appendClassReference(null, IllegalArgumentException.class);
		js.append("();\n");
		js.popIndentation();
		js.append("}\n");
	}

	protected void doMappingGetBoundValues(@NonNull CGMapping cgMapping) {
		Iterable<@NonNull CGGuardVariable> cgGuardVariables = QVTiCGUtil.getOwnedGuardVariables(cgMapping);
		js.append("/*\n");
		js.append(" * Return the number of bound values.\n");
		js.append(" */\n");
		js.append("@Override\n");
		js.append("public int getBoundValues() {\n");
		js.pushIndentation(null);
		js.append("return " + Iterables.size(cgGuardVariables) + ";\n");
		js.popIndentation();
		js.append("}\n");
	}

	protected void doMappingSuccess(@NonNull CGMappingExp cgMappingExp) {
		//		CGMapping cgMapping = QVTiCGUtil.getContainingCGMapping(cgMappingExp);
		//		CGValuedElement cgBody = cgMapping.getOwnedBody();
		//		CGGuardVariable cgTraceParameter = QVTiCGUtil.getTraceParameter(cgMapping);
		//		if (cgTraceParameter == null) {
		js.appendDeclaration(cgMappingExp);
		js.append(" = ");
		js.appendClassReference(null, ValueUtil.class);
		js.append(".TRUE_VALUE;\n");
		//		}
		//		else {
		//			js.appendDeclaration(cgMappingExp);
		//			js.append(" = ");
		//			//			js.appendValueName(cgBody);
		//			//			js.append(" && ");
		//			js.appendName(qvtiGlobalContext.getObjectManagerName());
		//			js.append(".addSpeculation(");
		//			js.appendValueName(cgTraceParameter);
		//			for (@NonNull CGGuardVariable cgGuardVariable : QVTiCGUtil.getOwnedGuardVariables(cgMapping)) {
		//				js.append(", ");
		//				js.appendValueName(cgGuardVariable);
		//			}
		//			js.append(");\n");
		/*			js.append("if (");
			js.appendValueName(cgMappingExp);
			js.append(") {\n");
			js.pushIndentation(null);
			if (isGeneratedDebug) {
				js.append("if (debugInvocations) {\n");
				js.pushIndentation(null);
				js.appendClassReference(null, AbstractTransformer.class);
				js.append(".INVOCATIONS.println(\"done " + getMappingName(cgMapping) + "\");\n");
				js.popIndentation();
				js.append("}\n");
			}
			js.popIndentation();
			js.append("}\n");
			js.append("else {\n");
			js.pushIndentation(null);
			if (isGeneratedDebug) {
				js.append("if (debugInvocations) {\n");
				js.pushIndentation(null);
				js.appendClassReference(null, AbstractTransformer.class);
				js.append(".INVOCATIONS.println(\"speculating " + getMappingName(cgMapping) + "\");\n");
				js.popIndentation();
				js.append("}\n");
			}
			js.popIndentation();
			js.append("}\n"); */
		/*			js.append("if (debugInvocations) {\n");
			js.pushIndentation(null);
			js.appendClassReference(null, AbstractTransformer.class);
			js.append(".INVOCATIONS.println((");
			js.appendValueName(cgMappingExp);
			js.append(" ? \"done \"  : \"speculating \") + \"" + getMappingName(cgMapping) + "\");\n");
			js.popIndentation();
			js.append("}\n"); */
		//		}
	}

	protected void doOppositeCaches(@NonNull EntryPointsAnalysis entryPointsAnalysis) {
		Map<@NonNull Property, @NonNull Integer> opposites = entryPointsAnalysis.getCaches();
		if (opposites.size() <= 0) {
			return;
		}
		js.append("\n/*\n * Property-source to Property-target unnavigable navigation caches\n */\n");
		Map<@NonNull String, @NonNull Property> key2property = new HashMap<>();
		for (Map.Entry<@NonNull Property, @NonNull Integer> entry : opposites.entrySet()) {
			Property property = entry.getKey();
			String name = qvtiGlobalContext.addOppositeProperty(property);

			key2property.put(name, property);
		}
		List<String> sortedKeys = new ArrayList<>(key2property.keySet());
		Collections.sort(sortedKeys);
		for (String key : sortedKeys) {
			Property property = key2property.get(key);
			assert property != null;
			TypeDescriptor outerTypeDescriptor = context.getBoxedDescriptor(property.getOwningClass().getTypeId());
			TypeDescriptor middleTypeDescriptor = context.getBoxedDescriptor(PivotUtil.getElementalType(PivotUtil.getType(property)).getTypeId());
			js.append("protected final ");
			js.appendIsRequired(true);
			js.append(" ");
			js.appendClassReference(null, Map.class, false, middleTypeDescriptor, outerTypeDescriptor);
			js.append(" ");
			js.append(key);
			js.append(" = new ");
			js.appendClassReference(null, HashMap.class, false, new @NonNull TypeDescriptor[] {});
			js.append("();\n");
		}
	}

	protected @Nullable String doOppositePropertyIds(@NonNull EntryPointsAnalysis entryPointsAnalysis) {
		// This code is no longer used, and since it is not used it generates undefined references
		// It appears to have 'worked' only because a duplicate incomplete TransformationAnalysis was in use.
		Map<@NonNull Property, @NonNull Integer> opposites = entryPointsAnalysis.getCaches();
		if (opposites.size() <= 0) {
			return null;
		}
		Property dummyProperty = opposites.keySet().iterator().next();
		List<@NonNull Property> sortedList = new ArrayList<>();
		for (int i = 0; i < opposites.size();i++) {
			sortedList.add(dummyProperty);
		}
		for (Map.Entry<@NonNull Property, @NonNull Integer> entry : opposites.entrySet()) {
			sortedList.set(entry.getValue().intValue(), entry.getKey());
		}
		//
		//	Emit the ClassId array
		//
		js.append("/*\n");
		js.append(" * Array of the source PropertyIds of each Property for which unnavigable opposite property navigation may occur.\n");
		js.append(" */\n");
		String oppositeIndex2propertyIdName = qvtiGlobalContext.getOppositeIndex2propertyIdName();
		js.append("private static final ");
		js.appendClassReference(true, PropertyId.class);
		js.append(" ");
		js.appendIsRequired(true);
		js.append(" [] ");
		js.append(oppositeIndex2propertyIdName);
		js.append(" = new ");
		js.appendClassReference(true, PropertyId.class);
		js.append("[]{\n");
		js.pushIndentation(null);
		for (int i = 0; i < sortedList.size(); i++) {
			Property property = sortedList.get(i);
			CGElementId cgPropertyId = analyzer.getElementId(property.getPropertyId());
			js.appendValueName(cgPropertyId);
			if ((i+1) < sortedList.size()) {
				js.append(",");
			}
			js.append("\t\t// " + i + " => " + property.getName() + "\n");
		}
		js.popIndentation();
		js.append("};\n");
		return oppositeIndex2propertyIdName;
	}

	protected void doRun(@NonNull CGTransformation cgTransformation, @Nullable List<@Nullable AllInstancesAnalysis> allInstancesAnalyses) {
		CompleteModelInternal completeModel = environmentFactory.getCompleteModel();
		Map<@NonNull TypedModel, @NonNull CGTypedModel> asTypedModel2cgTypedModel = new HashMap<>();
		ImperativeTransformation asTransformation = QVTiCGUtil.getAST(cgTransformation);
		List<@NonNull TypedModel> asTypedModels = QVTimperativeUtil.Internal.getModelParameterList(asTransformation);
		for (@NonNull CGTypedModel cgTypedModel : QVTiCGUtil.getOwnedTypedModels(cgTransformation)) {
			TypedModel asTypedModel = QVTiCGUtil.getAST(cgTypedModel);
			asTypedModel2cgTypedModel.put(asTypedModel, cgTypedModel);
		}
		List<@NonNull CGMapping> cgRootMappings = new ArrayList<>();
		//		CGMapping cgRootMapping = NameUtil.getNameable(cgTransformation.getOwnedMappings(), QVTscheduleConstants.ROOT_MAPPING_NAME);	// Obsolete relic
		for (@NonNull CGMapping cgMapping : QVTiCGUtil.getOwnedMappings(cgTransformation)) {
			Mapping asMapping = QVTiCGUtil.getAST(cgMapping);
			if (asMapping instanceof EntryPoint) {
				cgRootMappings.add(cgMapping);
			}
		}
		Collections.sort(cgRootMappings, new Comparator<@NonNull CGMapping>() {

			@Override
			public int compare(@NonNull CGMapping o1, @NonNull CGMapping o2) {
				EntryPoint asEntryPoint1 = (EntryPoint) QVTiCGUtil.getAST(o1);
				EntryPoint asEntryPoint2 = (EntryPoint) QVTiCGUtil.getAST(o2);
				List<TypedModel> asOutputTypedModels1 = asEntryPoint1.getOutputTypedModels();
				List<TypedModel> asOutputTypedModels2 = asEntryPoint2.getOutputTypedModels();
				TypedModel asTypedModel1 = asOutputTypedModels1.size() > 0 ? asOutputTypedModels1.get(0) : null;
				TypedModel asTypedModel2 = asOutputTypedModels2.size() > 0 ? asOutputTypedModels2.get(0) : null;
				int index1 = asTypedModels.indexOf(asTypedModel1);
				int index2 = asTypedModels.indexOf(asTypedModel2);
				return index1 - index2;
			}});
		boolean isMultiDirectional = cgRootMappings.size() > 1;
		js.append("@Override\n");
		js.append("public boolean run(");
		js.appendClassReference(true, String.class);
		js.append(" targetName");
		js.append(") {\n");
		js.pushIndentation(null);
		if (!isMultiDirectional) {
			js.append("return run();\n");
			js.popIndentation();
			js.append("}\n");
			js.append("\n");
			js.append("@Override\n");
			js.append("public boolean run() {\n");
			js.pushIndentation(null);
		}
		if (isMultiDirectional) {
			js.append("switch (targetName) {\n");
			js.pushIndentation(null);
		}
		for (@NonNull CGMapping cgRootMapping : cgRootMappings) {
			EntryPoint asEntryPoint = (EntryPoint) QVTiCGUtil.getAST(cgRootMapping);
			if (isMultiDirectional) {
				List<TypedModel> asOutputTypedModels = asEntryPoint.getOutputTypedModels();
				if (asOutputTypedModels.size() > 0) {
					js.append("case \"");
					js.append(asEntryPoint.getTargetName());
					js.append("\": {\n");
					js.pushIndentation(null);
				}
				else {
					continue;		// Avoid generating code for not-enforceable TypedModel
				}
			}
			ImperativeTransformation transformation = QVTiCGUtil.getAST(cgTransformation);
			EntryPointsAnalysis entryPointsAnalysis = context.getEntryPointsAnalysis(transformation);
			EntryPointAnalysis entryPointAnalysis = entryPointsAnalysis.getEntryPointAnalysis(asEntryPoint);
			Iterable<@NonNull EAttribute> eAttributes = entryPointAnalysis.getSpeculatedEAttributes();
			if ((eAttributes != null) && !Iterables.isEmpty(eAttributes)) {
				js.append("initSpeculatedEAttributes(");
				boolean isFirst = true;
				for (@NonNull EAttribute eAttribute : eAttributes) {
					if (!isFirst) {
						js.append(",\n\t\t\t\t\t\t");
					}
					else {
						isFirst = false;
					}
					appendQualifiedLiteralName(eAttribute);
				}
				js.append(");\n");
			}
			for (@NonNull CGGuardVariable cgGuardVariable : QVTiCGUtil.getOwnedGuardVariables(cgRootMapping)) {
				//			js.appendDeclaration(cgGuardVariable);
				js.append("final ");
				js.appendClassReference(true, Connection.class);
				js.append(" ");
				js.appendValueName(cgGuardVariable);
				js.append(" = ");
				js.append(qvtiGlobalContext.getModelsName());
				js.append("[");
				VariableDeclaration asGuardVariable = QVTiCGUtil.getAST(cgGuardVariable);
				Type type = QVTimperativeUtil.getType(asGuardVariable);
				org.eclipse.ocl.pivot.Package asPackage = PivotUtil.getContainingPackage(type);
				assert asPackage != null;
				AllInstancesAnalysis allInstancesAnalysis = null;
				CGTypedModel cgTypedModel = null;
				for (@NonNull TypedModel asTypedModel : QVTimperativeUtil.getInputTypedModels(asEntryPoint)) {
					if (asTypedModel.getUsedPackage().contains(asPackage)) {
						assert cgTypedModel == null;
						cgTypedModel = asTypedModel2cgTypedModel.get(asTypedModel);
						assert cgTypedModel != null;
					}
				}
				if (cgTypedModel != null) {
					appendModelIndex(cgTypedModel);
					assert allInstancesAnalyses != null;
					allInstancesAnalysis = allInstancesAnalyses.get(cgTypedModel.getModelIndex());
				}
				js.append("].getConnection(");
				assert allInstancesAnalysis != null;
				CompleteClass completeType = completeModel.getCompleteClass(type);
				Integer classIndex = allInstancesAnalysis.getInstancesCompleteClass2index().get(completeType);
				js.append(classIndex + "/*" + type + "*/");
				js.append(");\n");
			}
			if (isIncremental || useClass(cgRootMapping)) {
				js.append(getMappingCtorName(cgRootMapping) + ".invoke(");
			}
			else {
				js.append("return ");
				js.append(getMappingName(cgRootMapping));
				js.append("(");
			}
			boolean isFirst = true;
			for (@NonNull CGGuardVariable cgGuardVariable : QVTiCGUtil.getOwnedGuardVariables(cgRootMapping)) {
				if (!isFirst) {
					js.append(", ");
				}
				js.appendValueName(cgGuardVariable);
				isFirst = false;
			}
			js.append(")");
			if (isIncremental || useClass(cgRootMapping)) {
				js.append(";\n");
				js.append("return invocationManager.flush();\n");
			}
			else {
				js.append(" && invocationManager.flush();\n");
			}
			if (isMultiDirectional) {
				js.popIndentation();
				js.append("}\n");
			}
		}
		if (isMultiDirectional) {
			js.append("default: {\n");
			js.pushIndentation(null);
			js.append("throwInvalidEvaluationException(\"Unsupported target name \'\'{0}\'\'\", targetName);\n");
			js.append("return false;\n");
			js.popIndentation();
			js.append("}\n");
			js.popIndentation();
			js.append("}\n");
		}
		js.popIndentation();
		js.append("}\n");
	}

	protected void doTransformationExecution(@NonNull CGTransformation cgTransformation) {
		ImperativeTransformation iTransformation = QVTiCGUtil.getAST(cgTransformation);
		Type contextType = iTransformation.getContextType();
		if (contextType != null) {
			EClass eClass = (EClass) contextType.getESObject();
			if (eClass != null) {										// QVTc / manual QVTi has no trace EClass
				String createMethodName = "create" + eClass.getName();
				EPackage ePackage = eClass.getEPackage();
				assert ePackage != null;
				String javaFactory = genModelHelper.getQualifiedFactoryInterfaceName(ePackage);
				String javaClass = genModelHelper.getEcoreInterfaceClassifierName(eClass);
				js.append("private ");
				js.appendClassReference(false, javaClass);
				String transformationExecutionName = qvtiGlobalContext.getTransformationExecutionName();
				js.append(" " + transformationExecutionName + " = null;\n");
				js.append("\n");
				js.append("public ");
				js.appendClassReference(true, javaClass);
				js.append(" " + qvtiGlobalContext.getGetTransformationExecutionName() + "() {\n");
				js.pushIndentation(null);
				js.append("if (" + transformationExecutionName + " == null) {\n");
				js.pushIndentation(null);
				js.append(transformationExecutionName + " = ");
				js.appendClassReference(null, javaFactory);
				js.append(".eINSTANCE.");
				js.append(createMethodName);
				js.append("();\n");
				js.popIndentation();
				js.append("}\n");
				js.append("return " + transformationExecutionName + ";\n");
				js.popIndentation();
				js.append("}\n");
				js.append("\n");
			}
		}
	}

	protected @NonNull Class<? extends Transformer> getAbstractTransformationExecutorClass() {
		return isIncremental ? AbstractTransformer.Incremental.class : AbstractTransformer.class;
	}

	@Override
	public @NonNull QVTiAnalyzer getAnalyzer() {
		return (QVTiAnalyzer) super.getAnalyzer();
	}

	private EObject getContainer(EObject eObject) {
		EObject eContainer = eObject.eContainer();
		if (eContainer != null) {
			return eContainer;
		}
		for (Adapter eAdapter : eObject.eAdapters()) {
			if (eAdapter instanceof QVTiAS2CGVisitor.InlinedBodyAdapter) {
				return ((QVTiAS2CGVisitor.InlinedBodyAdapter)eAdapter).getOperationCallExp();
			}
		}
		return null;
	}

	@Override
	protected @Nullable EStructuralFeature getESObject(@NonNull Property asProperty) {
		EObject esObject = asProperty.getESObject();
		if (esObject instanceof EStructuralFeature) {
			return (EStructuralFeature)esObject;
		}
		Property oppositeProperty = asProperty.getOpposite();
		if (oppositeProperty == null) {
			return null;
		}
		if (!oppositeProperty.isIsComposite()) {
			PivotMetamodelManager metamodelManager = environmentFactory.getMetamodelManager();
			LibraryProperty libraryProperty = metamodelManager.getImplementation(null, null, asProperty);
			if (!(libraryProperty instanceof OclElementOclContainerProperty)) {
				return null;
			}
		}
		return OCLstdlibPackage.Literals.OCL_ELEMENT__OCL_CONTAINER;
	}

	protected @NonNull String getFunctionCtorName(@NonNull CGFunction cgFunction) {
		return JavaStream.convertToJavaIdentifier("FTOR_" + cgFunction.getName());
	}

	/*	protected @NonNull String getFunctionInstanceName(@NonNull CGFunction cgFunction) {
		JavaLocalContext<@NonNull ?> functionContext = ClassUtil.nonNullState(qvtiGlobalContext.getLocalContext(cgFunction));
		Object instanceKey = cgFunction.getBody();
		if (instanceKey == null) {
			instanceKey = QVTiCGUtil.getAST(cgFunction).getImplementationClass();
		}
		//	return functionContext.getNameManager().declareStandardName((CGValuedElement) instanceKey, "instance");
		return cgFunction.getVariantResolvedName(getCodeGenerator().getINSTANCE_NameVariant());
		//	nameResolution.addNameVariant(getCodeGenerator().getINSTANCE_NameVariant());
		//	return "XXX=instance";			// XXX
	} */

	protected @NonNull String getFunctionName(@NonNull CGFunction cgFunction) {
		return JavaStream.convertToJavaIdentifier("FUN_" + cgFunction.getName());
	}

	protected @NonNull QVTiGlobalContext getGlobalContext() {
		return (QVTiGlobalContext)globalContext;
	}

	private @Nullable Mapping getInvocationWrapper(@NonNull CGValuedElement cgValue) {
		Mapping mapping = null;
		if (cgValue instanceof CGSequence) {
			for (@NonNull CGValuedElement cgStatement : QVTiCGUtil.getOwnedStatements((CGSequence) cgValue)) {
				mapping = getInvocationWrapper(cgStatement);
				if (mapping != null) {
					return mapping;
				}
			}
		}
		else if (cgValue instanceof CGMappingLoop) {
			return getInvocationWrapper(QVTiCGUtil.getBody((CGMappingLoop)cgValue));
		}
		else if (cgValue instanceof CGLetExp) {
			return getInvocationWrapper(QVTiCGUtil.getIn((CGLetExp)cgValue));
		}
		else if (cgValue instanceof CGMappingCall) {
			MappingCall pMappingCall = QVTiCGUtil.getAST((CGMappingCall)cgValue);
			Mapping pReferredMapping = QVTimperativeUtil.getReferredMapping(pMappingCall);
			CGMapping cgReferredMapping = analyzer.getMapping(pReferredMapping);
			assert cgReferredMapping != null;
			if (!useClass(cgReferredMapping)) {
				return pReferredMapping;
			}
		}
		return null;
	}

	private @Nullable Iterable<@NonNull CGMappingCallBinding> getIterateBindings(@NonNull Iterable<@NonNull CGMappingCallBinding> cgMappingCallBindings) {
		List<@NonNull CGMappingCallBinding> bindings = null;
		for (@NonNull CGMappingCallBinding cgMappingCallBinding : cgMappingCallBindings) {
			Element ast = cgMappingCallBinding.getAst();
			if (ast instanceof LoopParameterBinding) {
				if (bindings == null) {
					bindings = new ArrayList<>();
				}
				bindings.add(cgMappingCallBinding);
			}
		}
		return bindings;
	}

	protected @NonNull String getMappingCtorName(@NonNull CGMapping cgMapping) {
		return JavaStream.convertToJavaIdentifier("CTOR_" + cgMapping.getName());
	}

	protected @NonNull String getMappingName(@NonNull CGMapping cgMapping) {
		return JavaStream.convertToJavaIdentifier("MAP_" + cgMapping.getName());
	}

	protected @NonNull String getNativeInstanceClassName(@NonNull CGExecutorType cgType) {
		Element ast = cgType.getAst();
		return JavaStream.convertToJavaIdentifier("ICACHE_" + ((NamedElement)ast).getName());
	}

	protected @NonNull String getNativeInstanceInstanceName(@NonNull CGExecutorType cgType) {
		return "INSTANCE_" + getNativeInstanceClassName(cgType);
	}

	@Override
	protected @NonNull String getResolvedName(@NonNull CGValuedElement cgElement) {
		if (cgElement instanceof CGVariableExp) {
			CGVariable cgVariable = ((CGVariableExp)cgElement).getReferredVariable();
			if (cgVariable != null) {
				Element asVariable = cgVariable.getAst();
				if (asVariable instanceof Parameter) {
					EObject asContainer = asVariable.eContainer();
					if (asContainer instanceof TypedModel) {
						Transformation asTransformation = ((TypedModel)asContainer).getTransformation();
						if (asTransformation != null) {
							int index = asTransformation.getModelParameter().indexOf(asContainer);
							String name = qvtiGlobalContext.getModelsName() + "[" + index + "/*" + ((TypedModel)asContainer).getName() + "*/]";
							return name;
						}
					}
				}
			}
		}
		return super.getResolvedName(cgElement);
	}

	@Deprecated
	protected @NonNull String getThisName(@NonNull CGElement cgElement) {
		for (EObject eObject = cgElement; eObject != null; eObject = eObject.eContainer()) {
			if (eObject instanceof CGMapping) {
				return getMappingName((CGMapping)eObject);		// + ".this"
			}
			if (eObject instanceof CGFunction) {
				return getFunctionName((CGFunction)eObject);		// + ".this"
			}
			if (eObject instanceof CGClass) {
				return ClassUtil.nonNullState(((CGClass)eObject).getName());		// + ".this"
			}
		}
		assert false;
		return "";		// "this" */
	}

	private boolean isConnection(CGValuedElement source) {
		return (source.getAst() instanceof VariableExp) && (((VariableExp)source.getAst()).getReferredVariable() instanceof ConnectionVariable);
	}

	private boolean isHazardous2(@NonNull NavigationCallExp asNavigationCallExp) {
		for (EObject eObject = asNavigationCallExp; eObject != null; eObject = getContainer(eObject)) {
			if (eObject instanceof ObservableStatement) {
				List<Property> observedProperties = ((ObservableStatement)eObject).getObservedProperties();
				Property navigatedProperty = PivotUtil.getReferredProperty(asNavigationCallExp);
				if (observedProperties.contains(navigatedProperty)) {
					return true;
				}
				Property navigatedOppositeProperty = navigatedProperty.getOpposite();
				if (observedProperties.contains(navigatedOppositeProperty)) {
					return true;
				}
				break;
			}
		}
		return false;
	}

	protected @Nullable TypeDescriptor needsTypeCheck(@NonNull CGMappingCallBinding cgMappingCallBinding) {
		MappingParameterBinding mappingParameterBinding = (MappingParameterBinding)cgMappingCallBinding.getAst();
		VariableDeclaration boundVariable = mappingParameterBinding.getBoundVariable();
		assert boundVariable != null;
		if (boundVariable instanceof ConnectionVariable) {
			return null;
		}
		CGValuedElement value = cgMappingCallBinding.getOwnedValue();
		TypeDescriptor argumentTypeDescriptor = context.getTypeDescriptor(cgMappingCallBinding);
		TypeId pivotTypeId = value.getASTypeId();
		if (pivotTypeId instanceof CollectionTypeId) {
			pivotTypeId = ((CollectionTypeId)pivotTypeId).getElementTypeId();
		}
		TypeDescriptor iteratorTypeDescriptor = context.getBoxedDescriptor(ClassUtil.nonNullState(pivotTypeId));
		if (argumentTypeDescriptor.isAssignableFrom(iteratorTypeDescriptor)) {
			return null;
		}
		else {
			return argumentTypeDescriptor;
		}
	}

	protected boolean useCache(@NonNull CGFunction cgFunction) {
		Element ast = cgFunction.getAst();
		return !(ast instanceof Operation) || !((Operation)ast).isIsTransient();
	}

	protected @Nullable CGShadowExp useClassToCreateObject(@NonNull CGFunction cgFunction) {
		CGValuedElement cgBody = cgFunction.getBody();
		while (cgBody instanceof CGLetExp) {
			cgBody = ((CGLetExp)cgBody).getIn();
		}
		if (cgBody instanceof CGShadowExp) {			// QVTr Key
			if (!(((TypedElement)cgBody.getAst()).getType() instanceof DataType))
				return (CGShadowExp)cgBody;		// FIXME replace with clearer strategy
		}
		return null;
	}

	protected boolean useClass(@NonNull CGFunction cgFunction) {
		return true;
	}

	protected boolean useClass(@NonNull CGMapping cgMapping) {
		if (alwaysUseClasses) {
			return true;
		}
		if (isIncremental) {
			return true;
		}
		if (cgMapping.isUseClass()) {
			return true;
		}
		return false;
	}

	@Override
	public @NonNull Boolean visitCGConnectionAssignment(@NonNull CGConnectionAssignment cgConnectionAssignment) {
		CGValuedElement initValue = QVTiCGUtil.getOwnedInitValue(cgConnectionAssignment);
		if (!js.appendLocalStatements(initValue)) {
			return false;
		}
		final String iteratorName = getVariantResolvedName(cgConnectionAssignment, context.getITER_NameVariant());
		TypeId concreteElementTypeId = cgConnectionAssignment.getConnectionVariable().getASTypeId();
		assert concreteElementTypeId != null;
		BoxedDescriptor concreteBoxedDescriptor = context.getBoxedDescriptor(concreteElementTypeId);
		BoxedDescriptor abstractBoxedDescriptor = concreteBoxedDescriptor;
		if (!(initValue.getASTypeId() instanceof CollectionTypeId)) {
			if (isIncremental) {
				js.appendValueName(cgConnectionAssignment);
				js.append(" = ");
			}
			js.appendReferenceTo(cgConnectionAssignment.getConnectionVariable());
			js.append(".appendElement(");
			js.appendValueName(initValue);
			js.append(");\n");
		}
		else {
			js.append("for (");
			js.appendClassReference(Boolean.TRUE, abstractBoxedDescriptor);
			js.append(" ");
			js.append(iteratorName);
			js.append(" : ");
			if (initValue.isBoxed()) {
				js.appendClassReference(null, ValueUtil.class);
				js.append(".typedIterable(");
				js.appendClassReference(null, abstractBoxedDescriptor);
				js.append(".class, ");
				js.appendValueName(initValue);
				js.append(")");
			}
			else {
				js.appendValueName(initValue);
			}
			js.append(") {\n");
			js.pushIndentation(null);
			if (concreteBoxedDescriptor != abstractBoxedDescriptor) {
				js.append("if (");
				js.append(iteratorName);
				js.append(" instanceof ");
				js.appendClassReference(null, concreteBoxedDescriptor);
				js.append(") {\n");
				js.pushIndentation(null);
			}
			js.appendReferenceTo(cgConnectionAssignment.getConnectionVariable());
			js.append(".add(");
			js.append(iteratorName);
			js.append(");\n");
			if (concreteBoxedDescriptor != abstractBoxedDescriptor) {
				js.popIndentation();
				js.append("}\n");
			}
			js.popIndentation();
			js.append("}\n");
		}
		return true;
	}

	@Override
	public @NonNull Boolean visitCGConnectionVariable(@NonNull CGConnectionVariable object) {
		return visitCGGuardVariable(object);
	}

	@Override
	public @NonNull Boolean visitCGEcoreContainerAssignment(@NonNull CGEcoreContainerAssignment cgPropertyAssignment) {
		//		Property referredProperty = cgPropertyAssignment.getReferredProperty();
		//		Property pivotProperty = cgPropertyCallExp.getReferredProperty();
		//		CGTypeId cgTypeId = analyzer.getTypeId(pivotProperty.getOwningType().getTypeId());
		//		JavaTypeDescriptor requiredTypeDescriptor = context.getJavaTypeDescriptor(cgTypeId, false);
		EStructuralFeature eStructuralFeature = QVTiCGUtil.getEStructuralFeature(cgPropertyAssignment);
		CGValuedElement cgSlot = getExpression(QVTiCGUtil.getOwnedSlotValue(cgPropertyAssignment));
		CGValuedElement cgInit = getExpression(QVTiCGUtil.getOwnedInitValue(cgPropertyAssignment));
		//		Class<?> requiredJavaClass = requiredTypeDescriptor.getJavaClass();
		//		Method leastDerivedMethod = requiredJavaClass != null ? getLeastDerivedMethod(requiredJavaClass, getAccessor) : null;
		//		Class<?> unboxedSourceClass = leastDerivedMethod != null ? leastDerivedMethod.getDeclaringClass() : requiredJavaClass;
		//
		if (!js.appendLocalStatements(cgSlot)) {
			return false;
		}
		if (!js.appendLocalStatements(cgInit)) {
			return false;
		}
		if (eStructuralFeature.isMany()) {
			String getAccessor = genModelHelper.getGetAccessor(eStructuralFeature);
			//
			js.appendValueName(cgInit);
			js.append(".");
			js.append(getAccessor);
			js.append("().add(");
			js.appendValueName(cgSlot);
			js.append(");\n");
		}
		else {
			String setAccessor = genModelHelper.getSetAccessor(eStructuralFeature);
			//
			js.appendValueName(cgInit);
			js.append(".");
			js.append(setAccessor);
			js.append("(");
			js.appendValueName(cgSlot);
			js.append(");\n");
		}
		doAssigned(cgPropertyAssignment);
		return true;
	}

	@Override
	public @NonNull Boolean visitCGEcorePropertyAssignment(@NonNull CGEcorePropertyAssignment cgPropertyAssignment) {
		//		Property referredProperty = cgPropertyAssignment.getReferredProperty();
		//		Property pivotProperty = cgPropertyCallExp.getReferredProperty();
		//		CGTypeId cgTypeId = analyzer.getTypeId(pivotProperty.getOwningType().getTypeId());
		//		JavaTypeDescriptor requiredTypeDescriptor = context.getJavaTypeDescriptor(cgTypeId, false);
		SetStatement asSetStatement = QVTiCGUtil.getAST(cgPropertyAssignment);
		EStructuralFeature eStructuralFeature = QVTiCGUtil.getEStructuralFeature(cgPropertyAssignment);
		CGValuedElement cgSlot = getExpression(QVTiCGUtil.getOwnedSlotValue(cgPropertyAssignment));
		CGValuedElement cgInit = getExpression(QVTiCGUtil.getOwnedInitValue(cgPropertyAssignment));
		//		Class<?> requiredJavaClass = requiredTypeDescriptor.getJavaClass();
		//		Method leastDerivedMethod = requiredJavaClass != null ? getLeastDerivedMethod(requiredJavaClass, getAccessor) : null;
		//		Class<?> unboxedSourceClass = leastDerivedMethod != null ? leastDerivedMethod.getDeclaringClass() : requiredJavaClass;
		//
		if (!js.appendLocalStatements(cgSlot)) {
			return false;
		}
		if (!js.appendLocalStatements(cgInit)) {
			return false;
		}
		boolean isPartial = asSetStatement.isIsPartial();
		appendEcoreSet(cgSlot, eStructuralFeature, cgInit, isPartial);
		doAssigned(cgPropertyAssignment, isPartial);
		return true;
	}

	@Override
	public @NonNull Boolean visitCGEcorePropertyCallExp(@NonNull CGEcorePropertyCallExp cgPropertyCallExp) {
		CGValuedElement cgSource = getExpression(cgPropertyCallExp.getSource());
		if (!js.appendLocalStatements(cgSource)) {
			return false;
		}
		ElementId sourceTypeId = cgSource.getTypeId().getElementId();
		ImperativeTransformation iTransformation = getAnalyzer().getCodeGenerator().getTransformation();
		org.eclipse.ocl.pivot.Class runtimeContextClass = QVTimperativeUtil.getRuntimeContextClass(iTransformation);
		TypeId runtimeContextTypeId = runtimeContextClass.getTypeId();
		if (sourceTypeId == runtimeContextTypeId) {		// FIXME make transformationInstance regular - cloned from appendCGEcorePropertyCallExp
			Property asProperty = ClassUtil.nonNullState(cgPropertyCallExp.getReferredProperty());
			EStructuralFeature eStructuralFeature = ClassUtil.nonNullState(getESObject(asProperty));
			String getAccessor = genModelHelper.getGetAccessor(eStructuralFeature);
			js.appendDeclaration(cgPropertyCallExp);
			js.append(" = " + qvtiGlobalContext.getGetTransformationExecutionName() + "().");
			js.append(getAccessor);
			js.append("();\n");
			return true;
		}
		EStructuralFeature eStructuralFeature = QVTiCGUtil.getEStructuralFeature(cgPropertyCallExp);
		doGetting(cgPropertyCallExp, eStructuralFeature, false);
		Boolean status = appendCGEcorePropertyCallExp(cgPropertyCallExp, cgSource);
		if (status != ValueUtil.TRUE_VALUE) {
			return status;
		}
		doGot(cgPropertyCallExp, cgSource, eStructuralFeature);
		return status;
	}

	@Override
	public @NonNull Boolean visitCGEcoreRealizedVariable(@NonNull CGEcoreRealizedVariable cgRealizedVariable) {
		EClassifier eClassifier = ClassUtil.nonNullState(cgRealizedVariable.getEClassifier());
		return doEcoreCreateClass(cgRealizedVariable, (EClass)eClassifier, true);
	}

	@Override
	public @NonNull Boolean visitCGFunction(@NonNull CGFunction cgFunction) {
		JavaLocalContext<@NonNull ?> localContext2 = globalContext.getLocalContext(cgFunction);
		if (localContext2 != null) {
			localContext = localContext2;
			//			localContext.
			try {
				List<CGParameter> cgParameters = cgFunction.getParameters();
				//
				js.appendCommentWithOCL(null, cgFunction.getAst());
				CGShadowExp cgShadowExp = useClassToCreateObject(cgFunction);
				String functionName = getFunctionName(cgFunction);
				String cachedResultName = cgFunction.getVariantResolvedName(getCodeGenerator().getCACHED_RESULT_NameVariant());
				if (cgShadowExp != null) {
					js.append("protected class ");
					js.append(functionName);
					js.append(" extends ");
					js.appendClassReference(null, isIncremental ? AbstractComputation.Incremental.class : AbstractComputation.class);
					js.pushClassBody(functionName);
					js.append("protected final ");
					js.appendTypeDeclaration(cgFunction);
					js.append(" " + cachedResultName + ";\n");
					js.append("\n");
					doFunctionConstructor(cgFunction, cgShadowExp, cachedResultName);
					js.append("\n");
					doFunctionGetInstance(cgFunction, cachedResultName);
					js.append("\n");
					doFunctionIsEqual(cgShadowExp, cachedResultName);
					js.popClassBody(false);
				}
				else if (useCache(cgFunction)) {
					String selfName = cgFunction.getVariantResolvedName(getCodeGenerator().getSELF_NameVariant());
					CGClass cgClass = ClassUtil.nonNullState(CGUtil.getContainingClass(cgFunction));
					js.append("protected class ");
					js.append(functionName);
					js.append(" extends ");
					js.appendClassReference(null, isIncremental ? AbstractComputation.Incremental.class : AbstractComputation.class);
					js.pushClassBody(functionName);
					js.append("protected final ");
					js.appendIsRequired(true);
					js.append(" ");
					js.appendClassReference(cgClass);
					js.append(" " + selfName + ";\n");
					for (@NonNull CGParameter cgParameter : ClassUtil.nullFree(cgFunction.getParameters())) {
						js.append("protected ");
						//						js.appendDeclaration(cgParameter);
						//						js.appendTypeDeclaration(cgParameter);
						boolean isPrimitive = js.isPrimitive(cgParameter);
						boolean isRequired = !isPrimitive && !cgParameter.isAssertedNonNull() && cgParameter.isNonNull() && !(cgParameter instanceof CGUnboxExp)/*|| cgElement.isRequired()*/;	// FIXME Ugh!
						js.appendIsCaught(cgParameter.isNonInvalid(), cgParameter.isCaught());
						js.append(" ");
						js.appendClassReference(isPrimitive ? null : isRequired ? true : null, cgParameter);
						js.append(" ");
						js.appendValueName(cgParameter);
						js.append(";\n");
					}
					//		CGValuedElement body = getExpression(cgFunction.getBody());
					//ElementId elementId = cgFunction.getTypeId().getElementId();

					js.append("protected final ");
					CGValuedElement cgBody = cgFunction.getBody();
					js.appendTypeDeclaration(cgBody != null ? cgBody : cgFunction);
					js.append(" " + cachedResultName + ";\n");
					js.append("\n");
					doFunctionConstructor(cgFunction, cachedResultName);
					js.append("\n");
					doFunctionGetInstance(cgFunction, cachedResultName);
					js.append("\n");
					doFunctionIsEqual(cgFunction);
					js.popClassBody(false);
				}
				else {
					//
					js.append("protected ");
					js.appendIsRequired(cgFunction.isRequired());
					//		js.append(" ");
					//		js.appendIsCaught(!cgOperation.isInvalid(), cgOperation.isInvalid());
					js.append(" ");
					ElementId elementId = cgFunction.getTypeId().getElementId();
					if (elementId != null) {
						TypeDescriptor javaTypeDescriptor = context.getUnboxedDescriptor(elementId);
						js.appendClassReference(null, javaTypeDescriptor);
					}
					js.append(" ");
					js.append(cgFunction.getName());
					js.append("(");
					boolean isFirst = true;
					for (@SuppressWarnings("null")@NonNull CGParameter cgParameter : cgParameters) {
						if (!isFirst) {
							js.append(", ");
						}
						js.appendDeclaration(cgParameter);
						isFirst = false;
					}
					js.append(")");
					return doFunctionBody(cgFunction);
				}
			}
			finally {
				localContext = null;
			}
		}
		return true;
	}

	@Override
	public @NonNull Boolean visitCGFunctionCallExp(@NonNull CGFunctionCallExp cgFunctionCallExp) {

		Operation pOperation = cgFunctionCallExp.getReferredOperation();
		CGFunction cgFunction = ClassUtil.nonNullState(cgFunctionCallExp.getFunction());
		boolean useClass = useClass(cgFunction);
		boolean useClassToCreateObject = useClassToCreateObject(cgFunction) != null;
		boolean useCache = useCache(cgFunction);
		boolean isIdentifiedInstance = useClass || useCache;
		List<CGValuedElement> cgArguments = cgFunctionCallExp.getArguments();
		List<Parameter> pParameters = pOperation.getOwnedParameters();
		//
		for (@SuppressWarnings("null")@NonNull CGValuedElement cgArgument : cgArguments) {
			CGValuedElement argument = getExpression(cgArgument);
			if (!js.appendLocalStatements(argument)) {
				return false;
			}
		}
		//
		js.appendDeclaration(cgFunctionCallExp);
		js.append(" = ");
		boolean needComma = false;
		if (isIdentifiedInstance) {
			js.append("((");
			js.append(getFunctionName(cgFunction));
			js.append(")");
			js.append(getFunctionCtorName(cgFunction));
			js.append(".getUniqueComputation(");
			if (useCache && !useClassToCreateObject) {
				CGClass cgClass = ClassUtil.nonNullState(cgFunction.getContainingClass());
				//				js.appendClassReference(cgClass);
				//				js.append(".this");
				appendThis(cgClass);
				needComma = true;
			}
		}
		else {
			js.append(pOperation.getName());
			js.append("(");
		}
		int iMax = Math.min(pParameters.size(), cgArguments.size());
		for (int i = 0; i < iMax; i++) {
			if (needComma) {
				js.append(", ");
			}
			CGValuedElement cgArgument = cgArguments.get(i);
			CGValuedElement argument = getExpression(cgArgument);
			Parameter pParameter = pParameters.get(i);
			//			CGTypeId cgParameterTypeId = analyzer.getTypeId(pParameter.getTypeId());
			TypeDescriptor parameterTypeDescriptor = context.getUnboxedDescriptor(pParameter.getTypeId());
			js.appendReferenceTo(parameterTypeDescriptor, argument);
			needComma = true;
		}
		js.append(")");
		if (isIdentifiedInstance) {
			js.append(")");
			String cachedResultName = cgFunction.getVariantResolvedName(getCodeGenerator().getCACHED_RESULT_NameVariant());
			js.append(".");
			js.append(cachedResultName);
		}
		js.append(";\n");
		return true;
	}

	@Override
	public @NonNull Boolean visitCGFunctionParameter(@NonNull CGFunctionParameter object) {
		return visitCGParameter(object);
	}

	@Override
	public @NonNull Boolean visitCGGuardVariable(@NonNull CGGuardVariable object) {
		return visitCGParameter(object);
	}

	@Override
	public @NonNull Boolean visitCGMapping(@NonNull CGMapping cgMapping) {
		JavaLocalContext<@NonNull ?> localContext2 = globalContext.getLocalContext(cgMapping);
		if (localContext2 != null) {
			localContext = localContext2;
			try {
				List<@NonNull CGGuardVariable> cgFreeVariables = ClassUtil.nullFree(cgMapping.getOwnedGuardVariables());
				//
				js.appendCommentWithOCL(null, cgMapping.getAst());
				String mappingName = getMappingName(cgMapping);
				if (useClass(cgMapping) /*&& (cgFreeVariables.size() > 0)*/) {
					js.append("protected class ");
					js.append(mappingName);
					js.append(" extends ");
					js.appendClassReference(null, isIncremental ? AbstractInvocation.Incremental.class : AbstractInvocation.class);
					js.pushClassBody(mappingName);
					boolean needsNewLine = doMappingFields(cgMapping);
					if (needsNewLine) {
						js.append("\n");
					}
					doMappingConstructor(cgMapping);
					if (isIncremental) {
						js.append("\n");
						doMappingDestroy(cgMapping);
					}
					js.append("\n");
					js.append("@Override\n");
					js.append("public boolean execute() ");
					doMappingBody(cgMapping, null);
					if (isIncremental) {
						js.append("\n");
						doMappingGetBoundValue(cgMapping);
						js.append("\n");
						doMappingGetBoundValues(cgMapping);
					}
					js.append("\n");
					doIsEqual(cgFreeVariables);
					js.popClassBody(false);
				}
				else {
					js.append("protected boolean " + mappingName + "(");
					boolean isFirst = true;
					for (@NonNull CGGuardVariable cgFreeVariable : cgFreeVariables) {
						if (!isFirst) {
							js.append(", ");
						}
						doMappingConnectionVariable(cgFreeVariable);
						isFirst = false;
					}
					js.append(") ");

					doMappingBody(cgMapping, cgFreeVariables);
				}
			}
			finally {
				localContext = null;
			}
		}
		return true;
	}

	@Override
	public @NonNull Boolean visitCGMappingCall(@NonNull CGMappingCall cgMappingCall) {
		MappingCall pMappingCall = QVTiCGUtil.getAST(cgMappingCall);
		Mapping pReferredMapping = QVTimperativeUtil.getReferredMapping(pMappingCall);
		CGMapping cgReferredMapping = analyzer.getMapping(pReferredMapping);
		if (cgReferredMapping == null) {
			return true;
		}
		List<CGMappingCallBinding> cgMappingCallBindings = cgMappingCall.getOwnedMappingCallBindings();
		for (@SuppressWarnings("null")@NonNull CGMappingCallBinding cgMappingCallBinding : cgMappingCallBindings) {
			CGValuedElement value = cgMappingCallBinding.getOwnedValue();
			if (value != null) {
				if (!js.appendLocalStatements(value)) {
					return false;
				}
			}
		}
		if (useClass(cgReferredMapping)) {
			return doMappingCall_Class(cgMappingCall);
		}
		else {
			return doMappingCall_Function(cgMappingCall);
		}
	}

	@Override
	public @NonNull Boolean visitCGMappingCallBinding(@NonNull CGMappingCallBinding object) {
		return true;
	}

	@Override
	public @NonNull Boolean visitCGMappingExp(@NonNull CGMappingExp cgMappingExp) {
		//		assert cgMappingExp.getPredicates().isEmpty();		// Get rewritten during JavaPre pass
		CGMapping cgMapping = QVTiCGUtil.getContainingCGMapping(cgMappingExp);
		Iterable<@NonNull CGAccumulator> cgAccumulators = QVTiCGUtil.getOwnedAccumulators(cgMappingExp);
		if (!Iterables.isEmpty(cgAccumulators)) {
			js.append("// interval variables\n");
			String modeFactoryName = "modeFactory";
			String rootIntervalName = "rootInterval";
			js.append("final ");
			js.appendClassReference(true, ModeFactory.class);
			js.append(" " + modeFactoryName + " = getModeFactory();\n");
			js.append("final ");
			js.appendClassReference(true, Interval.class);
			js.append(" " + rootIntervalName + " = lazyCreateInterval(0);\n");
			js.append("// connection variables\n");
			for (@NonNull CGAccumulator cgAccumulator : cgAccumulators) {
				Element ast = cgAccumulator.getAst();
				js.append("final ");
				//	js.appendClassReference(true, isIncremental ? Connection.Incremental.class : Connection.class);
				js.appendClassReference(true, Connection.class);
				js.append(" ");
				js.appendValueName(cgAccumulator);
				js.append(" = ");
				if ((ast instanceof BufferStatement) && (((BufferStatement)ast).getFirstPass() != null)) {
					js.append("lazyCreateInterval(");
					js.appendIntegerString(((BufferStatement)ast).getFirstPass());
					js.append(")");
				}
				else {
					js.append(rootIntervalName);
				}
				js.append(".createConnection(");
				js.appendString(QVTiCGUtil.getName(cgAccumulator));
				js.append(", ");
				js.appendValueName(cgAccumulator.getTypeId());
				js.append(", ");
				js.appendBooleanString((ast instanceof BufferStatement) && ((BufferStatement)ast).isIsStrict());
				js.append(", ");
				js.append(modeFactoryName);
				js.append(");\n");
				/*
				if ((ast instanceof ConnectionVariable) && (((ConnectionVariable)ast).getType() instanceof SetType)) {
					js.append("createUnenforcedSetAccumulatorValue(");
				}
				else {
					js.append("(");
					js.appendClassReference(null, cgAccumulator);
					js.append(".Accumulator)");
					js.appendClassReference(null, ValueUtil.class);
					js.append(".createCollectionAccumulatorValue(");
				}
				js.appendValueName(cgAccumulator.getTypeId());
				js.append(");\n"); */
				//
				CGValuedElement cgInit = cgAccumulator.getInit();
				if ((cgInit != null) && (!(cgInit instanceof CGCollectionExp) || !Iterables.isEmpty(((CGCollectionExp)cgInit).getParts()))) {
					if (!js.appendLocalStatements(cgInit)) {
						return false;
					}
					final String iteratorName = getVariantResolvedName(cgMappingExp, context.getITER_NameVariant());
					CollectionTypeId collectionTypeId = (CollectionTypeId)cgInit.getASTypeId();
					assert collectionTypeId != null;
					TypeId elementTypeId = collectionTypeId.getElementTypeId();
					BoxedDescriptor boxedDescriptor = context.getBoxedDescriptor(elementTypeId);
					js.append("for (");
					js.appendClassReference(Boolean.TRUE, boxedDescriptor);
					js.append(" ");
					js.append(iteratorName);
					js.append(" : ");
					js.appendClassReference(null, ValueUtil.class);
					js.append(".typedIterable(");
					js.appendClassReference(null, boxedDescriptor);
					js.append(".class, ");
					js.appendValueName(cgInit);
					js.append(")");
					//				js.appendReferenceTo(cgAccumulator.getInit());
					js.append(") {\n");
					js.pushIndentation(null);
					js.appendReferenceTo(cgAccumulator);
					js.append(".appendElement(");
					js.append(iteratorName);
					js.append(");\n");
					js.popIndentation();
					js.append("}\n");
				}
			}
		}
		Iterable<@NonNull CGRealizedVariable> cgRealizedVariables = QVTiCGUtil.getOwnedRealizedVariables(cgMapping);
		if (!Iterables.isEmpty(cgRealizedVariables)) {
			js.append("// creations\n");
			for (@NonNull CGRealizedVariable cgRealizedVariable : cgRealizedVariables) {
				if (!doCreateRealizedVariable(cgRealizedVariable)) {
					return false;
				}
			}
		}
		Iterable<@NonNull CGPropertyAssignment> cgPropertyAssignments = QVTiCGUtil.getOwnedAssignments(cgMapping);
		if (!Iterables.isEmpty(cgPropertyAssignments)) {
			js.append("// property assignments\n");
			for (@NonNull CGPropertyAssignment cgAssignment : cgPropertyAssignments) {
				if (!cgAssignment.accept(this)) {
					return false;
				}
			}
		}
		Iterable<@NonNull CGConnectionAssignment> cgConnectionAssignments = QVTiCGUtil.getOwnedConnectionAssignments(cgMapping);
		if (!Iterables.isEmpty(cgConnectionAssignments)) {
			js.append("// connection assignments\n");
			for (@NonNull CGConnectionAssignment cgConnectionAssignment : cgConnectionAssignments) {
				if (!cgConnectionAssignment.accept(this)) {
					return false;
				}
			}
		}
		CGValuedElement body = cgMappingExp.getOwnedBody();
		if (body != null) {
			js.append("// mapping statements\n");
			if (!body.accept(this)) {
				return false;
			}
		}
		doMappingSuccess(cgMappingExp);
		return true;
	}

	@Override
	public @NonNull Boolean visitCGMappingLoop(@NonNull CGMappingLoop cgMappingLoop) {
		CGValuedElement source = getExpression(cgMappingLoop.getSource());
		CGIterator iterator = cgMappingLoop.getIterators().get(0);
		CGValuedElement body = QVTiCGUtil.getBody(cgMappingLoop);
		if (!js.appendLocalStatements(source)) {
			return false;
		}
		Mapping thisInvocationWrapper = null;
		if (invocationWrapper == null) {
			invocationWrapper = thisInvocationWrapper = getInvocationWrapper(body);
			if (thisInvocationWrapper != null) {
				doInvocationWrapperPrefix(thisInvocationWrapper);
			}
		}
		js.append("for (");
		js.appendClassReference(Boolean.TRUE, iterator);
		js.append(" ");
		js.appendValueName(iterator);
		js.append(" : ");
		if (isConnection(source)) {
			js.appendValueName(source);
			js.append(".typedIterable(");
			js.appendClassReference(null, iterator);
			js.append(".class)");
		}
		else if (source.isBoxed()) {
			js.appendClassReference(null, ValueUtil.class);
			js.append(".typedIterable(");
			js.appendClassReference(null, iterator);
			js.append(".class, ");
			js.appendValueName(source);
			js.append(")");
		}
		else {
			js.appendValueName(source);
		}
		js.append(") {\n");
		js.pushIndentation(null);
		if (!iterator.isNonNull()) {
			js.append("if (");
			js.appendValueName(iterator);
			js.append(" != null) {\n");
			js.pushIndentation(null);
		}
		body.accept(this);
		if (!iterator.isNonNull()) {
			js.popIndentation();
			js.append("}\n");
		}
		js.popIndentation();
		js.append("}\n");
		boolean needsFlush = false;
		for (EObject eObject : new TreeIterable(body, false)) {
			if (eObject instanceof CGMappingCall) {
				CGMappingCall cgMappingCall = (CGMappingCall)eObject;
				MappingCall asMappingCall = QVTiCGUtil.getAST(cgMappingCall);
				Mapping pReferredMapping = QVTimperativeUtil.getReferredMapping(asMappingCall);
				CGMapping cgReferredMapping = analyzer.getMapping(pReferredMapping);
				if ((cgReferredMapping != null) && useClass(cgReferredMapping)) {
					needsFlush = true;
					break;
				}
			}
		}
		if (thisInvocationWrapper != null) {
			doInvocationWrapperSuffix(thisInvocationWrapper);
			invocationWrapper = null;
		}
		if (needsFlush) {
			js.append("//invocationManager.flush();\n");
		}
		return true;
	}

	@Override
	public @NonNull Boolean visitCGMiddlePropertyAssignment(@NonNull CGMiddlePropertyAssignment cgMiddlePropertyAssignment) {
		Property pReferredProperty = QVTiCGUtil.getReferredProperty(cgMiddlePropertyAssignment);
		assert !pReferredProperty.isIsImplicit();
		CGValuedElement slotValue = QVTiCGUtil.getOwnedSlotValue(cgMiddlePropertyAssignment);
		CGValuedElement initValue = QVTiCGUtil.getOwnedInitValue(cgMiddlePropertyAssignment);
		Map<@NonNull Property, @NonNull String> oppositeProperties = qvtiGlobalContext.getOppositeProperties();
		if (oppositeProperties != null) {
			String cacheName = oppositeProperties.get(pReferredProperty);
			if (cacheName != null) {
				TypeDescriptor outerTypeDescriptor = context.getBoxedDescriptor(pReferredProperty.getOwningClass().getTypeId());
				TypeDescriptor middleTypeDescriptor = context.getBoxedDescriptor(PivotUtil.getElementalType(PivotUtil.getType(pReferredProperty)).getTypeId());
				js.append(cacheName);
				js.append(".put(");
				js.appendReferenceTo(middleTypeDescriptor, initValue);
				js.append(", ");
				js.appendReferenceTo(outerTypeDescriptor, slotValue);
				js.append(");\n");
			}
		}
		return visitCGEcorePropertyAssignment(cgMiddlePropertyAssignment);
	}

	@Override
	public @NonNull Boolean visitCGMiddlePropertyCallExp(@NonNull CGMiddlePropertyCallExp cgPropertyCallExp) {
		Property asOppositeProperty = ClassUtil.nonNullModel(cgPropertyCallExp.getReferredProperty());
		Property asProperty = ClassUtil.nonNullModel(asOppositeProperty.getOpposite());
		assert !asProperty.isIsImplicit();
		CGValuedElement source = getExpression(cgPropertyCallExp.getSource());
		//
		if (!js.appendLocalStatements(source)) {
			return false;
		}
		//
		EStructuralFeature eStructuralFeature = ClassUtil.nonNullState((EStructuralFeature) asProperty.getESObject());
		doGetting(cgPropertyCallExp, eStructuralFeature, true);
		js.appendDeclaration(cgPropertyCallExp);
		js.append(" = ");
		Map<Property, String> oppositeProperties = qvtiGlobalContext.getOppositeProperties();
		if (oppositeProperties != null) {
			boolean isRequired = cgPropertyCallExp.isRequired();
			String cacheName = oppositeProperties.get(asProperty);
			if (cacheName != null) {
				SubStream castBody = new SubStream() {
					@Override
					public void append() {
						if (isRequired) {
							js.appendClassReference(null, ClassUtil.class);
							js.append(".nonNullState (");
						}
						js.append(cacheName);
						js.append(".get(");
						js.appendValueName(source);
						js.append(")");
						if (isRequired) {
							js.append(")");
						}
					}
				};
				if (asOppositeProperty.isIsMany()) {
					js.appendClassCast(cgPropertyCallExp, castBody);
				}
				else {
					castBody.append();
				}
			}
			js.append(";\n");
		}
		doGot(cgPropertyCallExp, source, eStructuralFeature);
		return true;
	}

	@Override
	public @NonNull Boolean visitCGPropertyAssignment(@NonNull CGPropertyAssignment cgPropertyAssignment) {
		CGExecutorProperty cgExecutorProperty = cgPropertyAssignment.getExecutorProperty();
		CGValuedElement slotValue = QVTiCGUtil.getOwnedSlotValue(cgPropertyAssignment);
		CGValuedElement initValue = QVTiCGUtil.getOwnedInitValue(cgPropertyAssignment);
		if (!js.appendLocalStatements(slotValue)) {
			return false;
		}
		if (!js.appendLocalStatements(initValue)) {
			return false;
		}
		js.appendReferenceTo(cgExecutorProperty);
		js.append(".initValue(");
		js.appendValueName(slotValue);
		js.append(", ");
		js.appendValueName(initValue);
		js.append(");\n");
		return true;
	}

	@Override
	public @NonNull Boolean visitCGRealizedVariable(@NonNull CGRealizedVariable cgRealizedVariable) {
		TypeId typeId = cgRealizedVariable.getASTypeId();
		if (typeId != null) {
			CGMapping cgMapping = QVTiCGUtil.getOwningMapping(cgRealizedVariable);
			if (useClass(cgMapping)) {
				js.appendValueName(cgRealizedVariable);
			}
			else {
				js.appendDeclaration(cgRealizedVariable);
			}
			js.append(" = ");
			js.appendReferenceTo(cgRealizedVariable.getExecutorType());
			js.append(".createInstance();\n");
		}
		return true;
	}

	@Override
	public @NonNull Boolean visitCGRealizedVariablePart(@NonNull CGRealizedVariablePart cgRealizedVariablePart) {
		CGValuedElement init = getExpression(cgRealizedVariablePart.getInit());
		if (!js.appendLocalStatements(init)) {
			return false;
		}
		//
		js.append(".initValue(");
		js.appendValueName(cgRealizedVariablePart.getOwningRealizedVariable());
		js.append(", ");
		js.appendValueName(init);
		js.append(");\n");
		return true;
	}

	@Override
	public @NonNull Boolean visitCGSequence(@NonNull CGSequence cgSequence) {
		for (@NonNull CGValuedElement cgStatement : ClassUtil.nullFree(cgSequence.getOwnedStatements())) {
			cgStatement.accept(this);
		}
		return true;
	}

	@Override
	public @NonNull Boolean visitCGSpeculateExp(@NonNull CGSpeculateExp cgSpeculateExp) {
		CGMapping cgMapping = QVTiCGUtil.getContainingCGMapping(cgSpeculateExp);
		for (@NonNull CGGuardVariable cgGuardVariable : QVTiCGUtil.getOwnedGuardVariables(cgMapping)) {
			VariableDeclaration asGuardVariable = QVTiCGUtil.getAST(cgGuardVariable);
			if (asGuardVariable instanceof GuardParameter) {
				GuardParameter asGuardParameter = (GuardParameter)asGuardVariable;
				Property successProperty = asGuardParameter.getSuccessProperty();
				if (successProperty != null) {
					String getSpeculationSlotStateName = qvtiGlobalContext.getGetSpeculationSlotStateName();
					String inputSpeculationSlotStateName = qvtiGlobalContext.getInputSpeculationSlotStateName();
					String inputSpeculationSlotStatusName = qvtiGlobalContext.getInputSpeculationSlotStatusName();
					String needsSpeculationName = qvtiGlobalContext.getNeedsSpeculationName();
					String outputSpeculationSlotStateName = qvtiGlobalContext.getOutputSpeculationSlotStateName();
					String outputSpeculationSlotStatusName = qvtiGlobalContext.getOutputSpeculationSlotStatusName();
					EStructuralFeature eStructuralFeature = ClassUtil.nonNullState((EStructuralFeature) successProperty.getESObject());
					String setAccessor = genModelHelper.getSetAccessor(eStructuralFeature);
					//
					js.appendClassReference(true, SlotState.Speculating.class);
					js.append(" " + outputSpeculationSlotStateName + " = ");
					js.append(qvtiGlobalContext.getObjectManagerName());
					js.append(".");
					js.append(getSpeculationSlotStateName);
					js.append("(");
					js.appendValueName(cgGuardVariable);
					js.append(", ");
					appendQualifiedLiteralName(eStructuralFeature);
					js.append(");\n");
					//
					js.appendClassReference(null, Boolean.class);
					js.append(" " + outputSpeculationSlotStatusName + " = " + outputSpeculationSlotStateName + "." + qvtiGlobalContext.getGetSpeculationStatusName() + "();\n");
					//
					js.append("if (" + outputSpeculationSlotStatusName + " != ");
					js.appendClassReference(null, ValueUtil.class);
					js.append(".TRUE_VALUE) {\n");
					js.pushIndentation(null);
					js.append("if (" + outputSpeculationSlotStatusName + " == ");
					js.appendClassReference(null, ValueUtil.class);
					js.append(".FALSE_VALUE) {\n");
					js.pushIndentation(null);
					js.appendValueName(cgGuardVariable);
					js.append(".");
					js.append(setAccessor);
					js.append("(");
					js.appendClassReference(null, ValueUtil.class);
					js.append(".FALSE_VALUE);\n");
					//	doAssigned(cgGuardVariable, eStructuralFeature, outputSpeculatingSlotStatus);
					js.append("return ");
					js.appendClassReference(null, ValueUtil.class);
					js.append(".FALSE_VALUE;\n");
					js.popIndentation();
					js.append("}\n");
					//
					js.appendClassReference(true, SlotState.Speculating.class);
					js.append(" " + inputSpeculationSlotStateName + ";\n");
					js.appendClassReference(null, Boolean.class);
					js.append(" " + inputSpeculationSlotStatusName + ";\n");
					js.append("boolean " + needsSpeculationName + " = false;\n");
					for (CGSpeculatePart cgSpeculatePart : cgSpeculateExp.getParts()) {
						//	if (cgInput instanceof CGEcorePropertyCallExp) {cgInput;
						CGValuedElement cgInputObject = cgSpeculatePart.getObjectExp();
						//	boolean isRequired = cgInputObject.isRequired();
						boolean isNonNull = cgInputObject.isNonNull();
						if (isNonNull) {
							js.append("if (");
							js.appendValueName(cgInputObject);
							js.append(" == null) {\n");
							js.pushIndentation(null);
							js.append("throw new ");
							js.appendClassReference(null, InvalidEvaluationException.class);
							js.append("(");
							js.appendString("Null " + cgInputObject/*.getMessage()*/ + " speculation source");
							js.append(");\n");
							js.popIndentation();
							js.append("}\n");
						}
						else {
							js.append("if (");
							js.appendValueName(cgInputObject);
							js.append(" != null) {\n");
							js.pushIndentation(null);
						}
						EStructuralFeature inputAttribute = cgSpeculatePart.getEStructuralFeature();
						String inputSetAccessor = genModelHelper.getSetAccessor(eStructuralFeature);
						//
						js.append(inputSpeculationSlotStateName);
						js.append(" = ");
						js.append(qvtiGlobalContext.getObjectManagerName());
						js.append(".");
						js.append(getSpeculationSlotStateName);
						js.append("(");
						js.appendValueName(cgInputObject);
						js.append(", ");
						appendQualifiedLiteralName(inputAttribute);
						js.append(");\n");
						js.append(inputSpeculationSlotStatusName);
						js.append(" = ");
						js.append(inputSpeculationSlotStateName);
						js.append(".");
						js.append(qvtiGlobalContext.getGetSpeculationStatusName());
						js.append("();\n");
						//
						js.append("if (" + inputSpeculationSlotStatusName + " != ");
						js.appendClassReference(null, ValueUtil.class);
						js.append(".TRUE_VALUE) {\n");
						js.pushIndentation(null);
						js.append("if (" + inputSpeculationSlotStatusName + " == ");
						js.appendClassReference(null, ValueUtil.class);
						js.append(".FALSE_VALUE) {\n");
						js.pushIndentation(null);
						js.appendValueName(cgInputObject);
						js.append(".");
						js.append(inputSetAccessor);
						js.append("(");
						js.appendClassReference(null, ValueUtil.class);
						js.append(".FALSE_VALUE);\n");
						//	doAssigned(cgInputObject, inputAttribute, outputSpeculatingSlotStatus);
						js.append("return ");
						js.appendClassReference(null, ValueUtil.class);
						js.append(".FALSE_VALUE;\n");
						js.popIndentation();
						js.append("}\n");

						js.append("if (" + outputSpeculationSlotStateName + " != " + inputSpeculationSlotStateName + ") {\n");
						js.pushIndentation(null);
						js.append(outputSpeculationSlotStateName + ".addInput(" + inputSpeculationSlotStateName + ");\n");
						js.append(needsSpeculationName + " = true;\n");
						js.popIndentation();
						js.append("}\n");
						//
						js.popIndentation();
						js.append("}\n");
						//
						if (!isNonNull) {
							js.popIndentation();
							js.append("}\n");
						}
						//	if (Telement2element != null) {
						//		SlotState.Speculating inputSpeculatingSlotState = objectManager.getSpeculatingSlotState(Telement2element, trace_Forward2ReversePackage.Literals.TELEMENT2ELEMENT__S0GLOBAL, outputSpeculatingSlotState);
						//		if (inputSpeculatingSlotState != outputSpeculatingSlotState) {			// Bypass the depends-on-self unit cycle
						//			needsSpeculation = true;
						//		}
						//	}
					}
					js.append("if (" + needsSpeculationName + ") {\n");
					js.pushIndentation(null);
					js.append("throw new ");
					js.appendClassReference(null, InvocationFailedException.class);
					js.append("(" + outputSpeculationSlotStateName + ", true);\n");
					js.popIndentation();
					js.append("}\n");
					js.popIndentation();
					js.append("}\n");
					break;		// Only one trace
				}
			}
		}

		js.append("boolean ");
		js.appendValueName(cgSpeculateExp);
		js.append(" = true;\n");

		CGValuedElement cgSpeculated = cgSpeculateExp.getSpeculated();
		if (cgSpeculated != null) {
			if (!js.appendLocalStatements(cgSpeculated)) {
				return false;
			}
		}
		return true;
	}

	@Override
	public @NonNull Boolean visitCGSpeculatePart(@NonNull CGSpeculatePart object) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public @NonNull Boolean visitCGTransformation(@NonNull CGTransformation cgTransformation) {
		js.appendClassHeader(cgTransformation.getContainingPackage());
		ImperativeTransformation transformation = QVTiCGUtil.getAST(cgTransformation);
		EntryPointsAnalysis entryPointsAnalysis = context.getEntryPointsAnalysis(transformation);
		//		this.entryPointsAnalysis = entryPointsAnalysis;
		String className = cgTransformation.getName();
		assert className != null;
		js.append("/**\n");
		js.append(" * The " + className + " transformation:\n");
		js.append(" * <p>\n");
		js.append(" * Construct with an evaluator\n");
		js.append(" * <br>\n");
		js.append(" * Populate each input model with {@link addRootEObjects(String,List)}\n");
		js.append(" * <br>\n");
		js.append(" * {@link run()}\n");
		js.append(" * <br>\n");
		js.append(" * Extract each output model with {@link getRootEObjects(String)}\n");
		js.append(" */\n");
		//		js.append("@SuppressWarnings({\"nls\",\"unused\"})\n");
		js.append("@SuppressWarnings(\"unused\")\n");
		js.append("public class " + className + " extends ");
		js.appendClassReference(null, getAbstractTransformationExecutorClass());
		js.pushClassBody(className);
		if (sortedGlobals != null) {
			for (CGValuedElement cgElement : sortedGlobals) {
				assert cgElement.isGlobal();
				cgElement.accept(this);
			}
		}
		doOppositeCaches(entryPointsAnalysis);
		js.append("\n");
		String oppositeIndex2propertyIdName = doOppositePropertyIds(entryPointsAnalysis);
		if (oppositeIndex2propertyIdName != null) {
			js.append("\n");
		}
		List<@NonNull CGMapping> cgMappings = ClassUtil.nullFree(cgTransformation.getOwnedMappings());
		List<CGOperation> cgOperations = cgTransformation.getOperations();
		doMappingConstructorConstants(cgMappings);
		doFunctionConstructorConstants(ClassUtil.nullFree(cgOperations));
		doInstanceCaches(cgTransformation);
		js.append("\n");
		List<@Nullable AllInstancesAnalysis> allInstancesAnalyses = doAllInstances(entryPointsAnalysis);
		js.append("\n");
		doConstructor(cgTransformation, oppositeIndex2propertyIdName, allInstancesAnalyses);
		js.append("\n");
		/*		if (isIncremental) {
				doCreateIncrementalManagers();
				js.append("\n");
			} */
		/*	doCreateInterval(cgTransformation);
			js.append("\n"); */
		doTransformationExecution(cgTransformation);
		doRun(cgTransformation, allInstancesAnalyses);
		//			break;
		//		}
		for (@NonNull CGOperation cgOperation : ClassUtil.nullFree(cgOperations)) {
			if (!(cgOperation instanceof CGCachedOperation)) {
				js.append("\n");
				cgOperation.accept(this);
			}
		}
		for (@NonNull CGOperation cgOperation : ClassUtil.nullFree(cgOperations)) {
			if ((cgOperation instanceof CGCachedOperation) && (((CGCachedOperation)cgOperation).getFinalOperations().size() <= 0)) {
				js.append("\n");
				cgOperation.accept(this);
			}
		}
		for (@NonNull CGOperation cgOperation : ClassUtil.nullFree(cgOperations)) {
			if ((cgOperation instanceof CGCachedOperation) && (((CGCachedOperation)cgOperation).getFinalOperations().size() > 0)) {
				js.append("\n");
				cgOperation.accept(this);
			}
		}
		for (@NonNull CGMapping cgMapping : ClassUtil.nullFree(cgTransformation.getOwnedMappings())) {
			js.append("\n");
			cgMapping.accept(this);
		}
		js.popClassBody(false);
		assert js.peekClassNameStack() == null;
		return true;
	}

	@Override
	public @NonNull Boolean visitCGTypedModel(@NonNull CGTypedModel object) {
		return true;
	}
}

Back to the top