EnvLoadingVisitor.java 102 KB
    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
package jcircus.visitor;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.swing.JOptionPane;







//import jcircus.environment.ChanMSEnv;
import jcircus.environment.ChanInfoEnv;
import jcircus.environment.ChanUseEnv;
import jcircus.environment.Environment;
import jcircus.environment.ProcChanEnv;
import jcircus.environment.NameTypeEnv;
import jcircus.environment.ProcChanUseEnv;
import jcircus.exceptions.ChanDefOtherChanSyncException;
import jcircus.exceptions.ChanDefOtherChanUseException;
import jcircus.exceptions.ChanSyncUnificationException;
import jcircus.exceptions.ChanUseUnificationException;
import jcircus.exceptions.DifferentCardinalitiesException;
import jcircus.exceptions.InvalidFormatCommException;
import jcircus.exceptions.runtime.InvalidSubTypeException;
import jcircus.exceptions.MoreThanOneWriterException;
import jcircus.exceptions.runtime.UnrecoveredErrorException;
import jcircus.exceptions.runtime.VisitingMethodNotDefinedException;
import jcircus.generators.GCArrayGenerator;
import jcircus.generators.GPSEGenerator;
import jcircus.newutil.ChanSetUtil;
import jcircus.newutil.ChoiceUtil;
import jcircus.newutil.DeclUtil;
import jcircus.newutil.ExprUtil;
import jcircus.newutil.PrinterUtil;
import jcircus.parallelism.CopyProcessAnn;
//import jcircus.interleaving.OfferedEventsInfo;
//import jcircus.parallelismandrenaming.Index2PrefActEnv;
import jcircus.parallelism.ParallelismUpdater;
import jcircus.parallelism.FriendshipSets;
import jcircus.util.CircusType;
import jcircus.util.Constants;
import jcircus.util.Error;
import jcircus.util.ErrorMessage;
//import jcircus.util.GuardsGenerator;
import jcircus.util.IdCircusProcessGenerator;
import jcircus.util.ChanUse;
import jcircus.util.MathToolkitConstants;
import jcircus.util.NameType;
import jcircus.util.ChanSync;
import jcircus.util.Util;
import jcircus.util.annotations.IdCircusProcessAnn;
import net.sourceforge.czt.base.ast.ListTerm;
import net.sourceforge.czt.base.ast.Term;
import net.sourceforge.czt.base.impl.ListTermImpl;
import net.sourceforge.czt.circus.ast.Action1;
import net.sourceforge.czt.circus.ast.Action2;
import net.sourceforge.czt.circus.ast.ActionD;
import net.sourceforge.czt.circus.ast.ActionIte;
import net.sourceforge.czt.circus.ast.ActionPara;
import net.sourceforge.czt.circus.ast.AssignmentCommand;
import net.sourceforge.czt.circus.ast.BasicAction;
//import net.sourceforge.czt.circus.ast.BasicChannelSet;
import net.sourceforge.czt.circus.ast.BasicChannelSetExpr; //Sam
import net.sourceforge.czt.circus.ast.BasicProcess;
import net.sourceforge.czt.circus.ast.CallAction;
import net.sourceforge.czt.circus.ast.CallProcess;
//import net.sourceforge.czt.circus.ast.CallType; //Substitu�do por CallUsage
import net.sourceforge.czt.circus.ast.CallUsage;
import net.sourceforge.czt.circus.ast.ChannelDecl;
import net.sourceforge.czt.circus.ast.ChannelPara;
import net.sourceforge.czt.circus.ast.ChannelSet;
import net.sourceforge.czt.circus.ast.ChannelSetPara;
import net.sourceforge.czt.circus.ast.CircusAction;
import net.sourceforge.czt.circus.ast.CircusProcess;
//import net.sourceforge.czt.circus.ast.CommType; //deixa em stand by tamb�m
import net.sourceforge.czt.circus.ast.CommUsage; //deixa em stand by tamb�m
import net.sourceforge.czt.circus.ast.Communication;
import net.sourceforge.czt.circus.ast.DotField;
import net.sourceforge.czt.circus.ast.ExtChoiceAction;
import net.sourceforge.czt.circus.ast.Field;
import net.sourceforge.czt.circus.ast.GuardedAction;
import net.sourceforge.czt.circus.ast.HideProcess;
import net.sourceforge.czt.circus.ast.IfGuardedCommand;
import net.sourceforge.czt.circus.ast.InputField;
import net.sourceforge.czt.circus.ast.MuAction;
import net.sourceforge.czt.circus.ast.NameSetPara;
//import net.sourceforge.czt.circus.ast.OutputField;
import net.sourceforge.czt.circus.ast.CircusActionList;
import net.sourceforge.czt.circus.ast.CircusChannelSet;
import net.sourceforge.czt.circus.ast.CircusCommunicationList;
import net.sourceforge.czt.circus.ast.CircusFieldList;
import net.sourceforge.czt.circus.ast.CommPattern;
import net.sourceforge.czt.circus.ast.CommunicationList;
import net.sourceforge.czt.circus.ast.FieldList;
import net.sourceforge.czt.circus.ast.InterleaveAction;
import net.sourceforge.czt.circus.ast.OutputFieldAnn; //By Sam's
import net.sourceforge.czt.circus.ast.ParAction;
import net.sourceforge.czt.circus.ast.ParProcess;
import net.sourceforge.czt.circus.ast.ParallelAction;
import net.sourceforge.czt.circus.ast.ParamAction;
import net.sourceforge.czt.circus.ast.ParamProcess;
import net.sourceforge.czt.circus.ast.PrefixingAction;
import net.sourceforge.czt.circus.ast.Process1;
import net.sourceforge.czt.circus.ast.Process2;
import net.sourceforge.czt.circus.ast.ProcessD;
import net.sourceforge.czt.circus.ast.ProcessIdx;
import net.sourceforge.czt.circus.ast.ProcessIte;
import net.sourceforge.czt.circus.ast.ProcessPara;
//import net.sourceforge.czt.circus.ast.RefChannelSet;
import net.sourceforge.czt.circus.ast.ChannelSet; //Added by Sam, mas n�o sei, tou em d�vida em rela��o ao de cima
import net.sourceforge.czt.circus.ast.RenameProcess;
import net.sourceforge.czt.circus.ast.VarDeclCommand;
import net.sourceforge.czt.circus.impl.ActionParaImpl;
import net.sourceforge.czt.circus.impl.BasicProcessImpl;
import net.sourceforge.czt.z.ast.ApplExpr; /*********/
import net.sourceforge.czt.z.ast.AxPara;
import net.sourceforge.czt.z.ast.BindExpr; /*********/
import net.sourceforge.czt.z.ast.Box;
import net.sourceforge.czt.z.ast.Branch;
import net.sourceforge.czt.z.ast.CondExpr; /*********/
import net.sourceforge.czt.z.ast.ConstDecl;
import net.sourceforge.czt.z.ast.Decl;
import net.sourceforge.czt.z.ast.DeclList;
import net.sourceforge.czt.z.ast.ExprList;
import net.sourceforge.czt.z.ast.LatexMarkupPara;
import net.sourceforge.czt.z.ast.Name;
import net.sourceforge.czt.z.ast.NameList;
import net.sourceforge.czt.z.ast.NarrPara;
import net.sourceforge.czt.z.ast.NarrSect;
import net.sourceforge.czt.z.ast.ParaList;
import net.sourceforge.czt.z.ast.ProdExpr; /*********/
import net.sourceforge.czt.z.ast.Sect;
import net.sourceforge.czt.z.ast.SetExpr;
import net.sourceforge.czt.z.ast.ZBranchList;
import net.sourceforge.czt.z.ast.ZDeclList; //Acrescentado por quem?? Sam!
import net.sourceforge.czt.z.ast.ZExprList;
import net.sourceforge.czt.z.ast.ZFreetypeList;
import net.sourceforge.czt.z.ast.ZName/*DeclName*/;
import net.sourceforge.czt.z.ast.Expr; /*********/
import net.sourceforge.czt.z.ast.Expr0N; /*********/
import net.sourceforge.czt.z.ast.Expr1; /*********/
import net.sourceforge.czt.z.ast.Expr2; /*********/
import net.sourceforge.czt.z.ast.ExprPred; /*********/
import net.sourceforge.czt.z.ast.Fact;
import net.sourceforge.czt.z.ast.FreePara;
import net.sourceforge.czt.z.ast.Freetype;
import net.sourceforge.czt.z.ast.MemPred;
import net.sourceforge.czt.z.ast.ZParaList;
//import net.sourceforge.czt.z.ast.NameExprPair; //Como diabos vai ser esse????????????????
import net.sourceforge.czt.z.ast.NegPred;
import net.sourceforge.czt.z.ast.NumExpr; /*********/
import net.sourceforge.czt.z.ast.Para;
import net.sourceforge.czt.z.ast.Pred;
import net.sourceforge.czt.z.ast.Pred2;
import net.sourceforge.czt.z.ast.RefExpr; /*********/
import net.sourceforge.czt.z.ast.ZNameList;
import net.sourceforge.czt.z.ast.ZSchText;
//import net.sourceforge.czt.z.ast.RefName; //Provavelmente aqui � s� comentar, pq o ZName j� est� declarado
import net.sourceforge.czt.z.ast.SchExpr; /*********/
import net.sourceforge.czt.z.ast.SchText;
import net.sourceforge.czt.z.ast.Spec;
import net.sourceforge.czt.z.ast.VarDecl;
import net.sourceforge.czt.z.ast.ZSect;
import net.sourceforge.czt.z.util.Factory;
import jcircus.util.SignatureExpression;
import net.sourceforge.czt.z.ast.NameTypePair;
import net.sourceforge.czt.z.ast.Signature;
import net.sourceforge.czt.z.impl.LatexMarkupParaImpl;
import net.sourceforge.czt.z.impl.NarrParaImpl;
import net.sourceforge.czt.z.impl.NarrSectImpl;
import net.sourceforge.czt.z.impl.ZParaListImpl;
import net.sourceforge.czt.z.impl.ZSectImpl;
//import newjcircusutil.barrier.G4BGenerator;
//import newjcircusutil.barrier.GBGenerator;
import jcircus.complementaryenvs.CallProc2ChannelSetEnv;
import jcircus.complementaryenvs.ChanComplexCommEnv;
import jcircus.complementaryenvs.ChanDimEnv;
import jcircus.complementaryenvs.ChanDotFieldEnv;
import jcircus.complementaryenvs.ChanExtChoiceEnv;
import jcircus.complementaryenvs.ChanForcedInterleavingEnv;
import jcircus.complementaryenvs.ChanTransMSEnv;
import jcircus.complementaryenvs.PId2PNameEnv;
import jcircus.complementaryenvs.ParProcessEnv;
import jcircus.complementaryenvs.ProcCreateMainEnv;
import jcircus.complementaryenvs.ProcMuIndexEnv;
import jcircus.complementaryenvs.ProcName2ProcChanEnv;
import jcircus.complementaryenvs.TypeSizeEnv;
//import jcircus.complementaryenvs.MinMaxEnv;
import jcircus.complementaryenvs.ProcProcessParaEnv;
//import jcircus.complementaryenvs.Proc2HasIdEnv;
import jcircus.complexcomms.*;

/**
* EnvLoadingVisitor.java
*
* Performs the pre-processing of the Circus specification.
*
* @author Angela Freitas, updated by Samuel Barrocas
*
*/
//public class EnvLoadingVisitor implements EnvLoadingVisitorInterface { //By Angela
public class EnvLoadingVisitor implements EnvLoadingVisitorInterface {

//Sam's
private ProcMuIndexEnv _muActs2Indexes = new ProcMuIndexEnv ();
public ProcMuIndexEnv getMuActs2Indexes () {
return this._muActs2Indexes;
}
int mucounter = 0;
Spec spec;
String currentProcessName;
String projectDir;
String projectName;
Vector <AbsCommValuesGenerator> absGens = new Vector <AbsCommValuesGenerator> ();
Vector <CCMapsGenerator> ccMapsGens = new Vector <CCMapsGenerator> ();
public Vector <AbsCommValuesGenerator> getAbsGens () {
return this.absGens;
}
public Vector <CCMapsGenerator> getccMapsGens () {
return this.ccMapsGens;
}
public String getProjectDir () {
return this.projectDir;
}
public String getProjectName () {
return this.projectName;
}
public String getSpecNameOnTranslation () {
return this.specNameOnTranslation;
}
String specNameOnTranslation;
ProcessPara processPara; //Sam's

private ZName/*DeclName*/ _currentProc = null;
private ZName/*DeclName*/ _currentAct = null;
private String _stateName = null;
private Environment _env; // Environment for channels and types.
private boolean _extChoice = false; // Flag that indicates an external choice.
private String _currentRecAct;
private Factory _factory;
private boolean _globalScope;
private List<Error> _errors;

private ProcCreateMainEnv pcme;
public ProcCreateMainEnv getProcCreateMainEnv () {
return this.pcme;
}
public void setProcCreateMainEnv (ProcCreateMainEnv pcmeparam) {
this.pcme = pcmeparam;
}
private ChanTransMSEnv chanMSEnv = new ChanTransMSEnv (); //Sam
public ChanTransMSEnv getChanMultiSyncEnv () { //Sam
return this.chanMSEnv;
}
private ParProcessEnv parprocEnv = new ParProcessEnv ();
public ParProcessEnv getEnvLoadingParProcessEnv () {
return this.parprocEnv;
}
private PId2PNameEnv p2pId = new PId2PNameEnv ();
public PId2PNameEnv getPId2PNameEnv () {
return this.p2pId;
}
private ProcName2ProcChanEnv pn2pce = new ProcName2ProcChanEnv ();
public ProcName2ProcChanEnv getProcName2ProcChanEnv () {
return this.pn2pce;
}
private CallProc2ChannelSetEnv cp2cse = new CallProc2ChannelSetEnv ();
public CallProc2ChannelSetEnv getCallProc2ChannelSetEnv () {
return this.cp2cse;
}
//private Proc2HasIdEnv proc2HasIdEnv = new Proc2HasIdEnv(); //Sam's
public void addProcToProcess1Env (ProcessPara processPara) {
CircusProcess process = processPara.getCircusProcess();
if (process instanceof ParProcess) {
this.parprocEnv.put(processPara.getZName().toString(), true);
}
else {
this.parprocEnv.put(processPara.getZName().toString(), false);
}
}
private ProcProcessParaEnv procProcessParaEnv = new ProcProcessParaEnv (); //By Samuel Barrocas, 16/12/2010, 17:25hs.
public ProcProcessParaEnv getProcProcessParaEnv () { //By Samuel Barrocas, 16/12/2010, 17:25hs
return this.procProcessParaEnv;
}
public void setProcProcessParaEnv (ProcProcessParaEnv env) { //By Samuel Barrocas, 16/12/2010, 17:25hs
this.procProcessParaEnv = env;
}
private ChanDotFieldEnv chanDotFieldEnv = new ChanDotFieldEnv ();
public ChanDotFieldEnv getChanDotFieldEnv () {
return this.chanDotFieldEnv;
}
//private int barrierDim; //Sam's. Sam comment: Aqui acho que vou tirar depois
private boolean alreadyDefinedId = false;

private int nCommValues; //Sam's
private ChanDimEnv chanDimEnv = new ChanDimEnv (); //Sam's
//private MinMaxEnv minMaxEnv = new MinMaxEnv (); //Sam's
//private ChanMSEnv chanMSEnv = new ChanMSEnv (); //Sam's

Channel2TypeEnvironment chanTypeEnv = new Channel2TypeEnvironment ();
public Channel2TypeEnvironment getChanTypeEnv4CC () {
return this.chanTypeEnv;
}
private int minComm, maxAbsValueComm;
public int getMinComm () {
return this.minComm;
}
public int getMaxAbsValueComm () {
return this.maxAbsValueComm;
}
public ChanDimEnv getChanDimEnv () { //Sam's
return this.chanDimEnv;
}
private ChanForcedInterleavingEnv cfie = new ChanForcedInterleavingEnv ();
private ChanComplexCommEnv ccce = new ChanComplexCommEnv ();
public ChanForcedInterleavingEnv getChanForcedInterleavingEnv () {
return this.cfie;
}
public ChanComplexCommEnv getChanComplexCommEnv () {
return this.ccce;
}
private ChanExtChoiceEnv cece = new ChanExtChoiceEnv (); //Sam's
public void setChanExtChoiceEnv (ChanExtChoiceEnv cece) { //Sam's
this.cece = cece;
}
public ChanExtChoiceEnv getChanExtChoiceEnv () {
return this.cece;
}
TypeSizeEnv tse = new TypeSizeEnv ();
public TypeSizeEnv getTypeSizeEnv () {
return this.tse;
}
//private Index2PrefActEnv pa2ie;

/*public Index2PrefActEnv getPrefAct2IndexEnv () {
return this.pa2ie;
}*/
boolean useBarriers;
/**
* Return the list of errors.
*/
public List<Error> getErrors() {
return this._errors;
}
/**
* Methods to report errors.
*/

private void reportError(Error error) {
_errors.add(error);
}
private void reportFatalError(Error error) throws UnrecoveredErrorException {
reportError(error);
throw new UnrecoveredErrorException("Error during the environment loading.");
}

private void reportFatalError(Error error, Exception e) throws UnrecoveredErrorException {
reportError(error);
throw new UnrecoveredErrorException("Error during the environment loading.", e);
}
/**
* Constructor for class EnvLoadingVisitor.
*
*/
boolean bench;
public EnvLoadingVisitor(Environment Environment, String projectDir, String projectName, String specNameOnTranslation /*<-- Sam*/, boolean useBarriers, boolean bench) { //Construtor de EnvLoadingVisitor
this.bench = bench;
_env = Environment;
_factory = new Factory();
_globalScope = true;
_stateName = null;
_errors = new ArrayList();
this.projectDir = projectDir;
this.projectName = projectName;
this.specNameOnTranslation = specNameOnTranslation;
loadMathToolkit();
this.minComm = 0;
this.useBarriers = useBarriers;
}

public EnvLoadingVisitor(Environment environment) {
this (environment, "", "", "", true, false);
}
/**
* Just in case this visitor is called for a node that has not been considered yet.
*
* @param term
* @return
*/
public Object visitTerm(Term term) {
throw new VisitingMethodNotDefinedException(term.getClass(), this.getClass());
}
/** **************************************************************************************************************
* Term A Tree.
* ***************************************************************************************************************
*/
/**
*
*/

public Object visitSpec(Spec spec) { //Visita a especificacao. Aqui eh o ponto mais alto da hierarquia de visitadores
this.spec = spec;
System.out.print ("");
ZSect zSect;
if (spec.getSect().get(0) instanceof NarrSect)
zSect = (ZSect) spec.getSect().get(1);
else
zSect = (ZSect) spec.getSect().get(0);
ZParaList paras = (ZParaList) zSect.getParaList();
for (int i = 0; i < paras.size(); i++) {
if (!(paras.get(i) instanceof NarrPara || paras.get(i) instanceof LatexMarkupPara)) {
Para para = (Para) paras.get(i); //Aqui vai invocar os visitadores dos par�grafos que nao forem narrativos (com texto), ou de tags de Latex
para.accept(this);
}
}
return null;
}
public Object visitLatexMarkupPara (LatexMarkupPara lmp) {
ListTerm directive = lmp.getDirective();
return null;
}
public Object visitLatexMarkupParaImpl (LatexMarkupParaImpl lmpi) {
return null;
}
/**
*
*/
public Object visitFreetype(Freetype freetype) { //Visita os parágrafos de tipos livres.

ZBranchList branches = (ZBranchList)(freetype.getBranchList());

ZName declNameFreetype = freetype.getZName();

this.tse.put(declNameFreetype.toString(), branches.size());
// Add this freeType to the freeTypeEnvironment
_env.freeTypesEnvironmentAdd(freetype); //Adiciona o tipo livre � lista de tipos livres

// Declare the free type in the TypeEnvironment
CircusType circusType = new CircusType(_factory.createPowerExpr(
_factory.createRefExpr(_factory.createZName(declNameFreetype.toString())))); //Sam's

//Util.createCircusType(this.factory_.createPowerExpr(), this.environment_);
this._env.declareName(
this._factory.createZName(declNameFreetype.toString()),
NameType.FreeType,
circusType);

for (int i = 0; i < branches.size(); i++) {

Branch branch = (Branch) branches.get(i);

ZName declNameBranch = branch.getZName(); //retorna sam1 ou sam2
Expr expr = branch.getExpr();
Expr type;
RefExpr refExprNameFreeType = this._factory.createRefExpr(
//this._factory.createRefName(declNameFreetype)); //�ngela's
this._factory.createZName(declNameFreetype)); // Meuzis, Sam's

if (expr != null) {
List<Object> list = new ArrayList<Object>();
list.add(expr);
list.add(declNameFreetype);
type = this._factory.createPowerExpr(
this._factory.createProdExpr (expr, refExprNameFreeType)
);
//new PowerSet(new CartesianProduct(list));
} else {
// RefExpr
type = refExprNameFreeType;
}
// Only the first element will the its type
// (the free type) added to the type list
boolean b = (i == 0) ? true : false;
// Declare the branch of the free type in the TypeEnvironment
this._env.declareName(
//this._factory.createDeclName(declNameBranch.toString()), //Angela's
this._factory.createZName(declNameBranch.toString()), //Sam's
NameType.ElemFreeType, new CircusType(type), b);
System.out.print("");
}
return null;
}
/**
* Visits a schema text and returns its signature.
*
*/
public Object visitSchText(SchText schText) {

//JOptionPane.showMessageDialog (null, "Visitou SchText");
Signature result = this._factory.createSignature(Util.createEmptyListTerm());
//ListTerm decls = schText.getDecl(); //By Angela!
/*ListTerm*/ZDeclList decls = ((ZSchText)schText).getZDeclList().getDecl(); //By euzis, modificando a linha acima
// Pred pred = schText.getPred(); //By Angela!!
Pred pred = ((ZSchText)schText).getPred(); //By euzis, modificando a linha de cima!!
ListTerm nameTypePairs;
// Gets the annotation
NameType nameType = Util.getAndRemoveNameTypeAnn(schText);
// Open the scope of the schema
_env.openScope();
if(decls != null) {
for (int i = 0; i < decls.size(); i++) {
/**
* If this is a SchText for a schema (NameType == StateComp)
*
*/
Decl decl = (Decl) decls.get(i);

// Adds annotation
Util.addNameTypeAnn(decl, nameType);
// Visits the declaration
Signature signature = (Signature) decl.accept(this);
// Add to the signature
//if (signature.getNameTypePair() != null) //Acrescentado por Sam
result.getNameTypePair().addAll(signature.getNameTypePair()); //DESCOMENTAR DEPOIS!!!
System.out.println ("Passou por aqui");
}
}
if (pred != null) {
pred.accept(this);
}
// Close the scope of the schema
_env.closeScope();
return result;
}
/** **************************************************************************************************************
* Decl Tree.
* ***************************************************************************************************************
*/
/**
*
*/

public int exhibitOptionPane4MaxAbsValueComm (Expr expr) {
boolean shown = false;
int maxabscomm = -1;
if (expr instanceof ProdExpr) {
ZExprList exprList = ((ProdExpr)expr).getZExprList();
int size = exprList.size();
for (int i = 0; i < size; i++) {
Expr subexpr = exprList.get(i);
if (subexpr instanceof RefExpr) {
if (!shown && ((RefExpr)subexpr).getZName().toString().equals("ℕ")) {
String str = JOptionPane.showInputDialog("Enter maximum absolute value to be communicated:");
maxabscomm = Integer.parseInt (str);
System.out.println ("Time Millis 1 ::: " + System.currentTimeMillis());
}
else {
maxabscomm = tse.get(((RefExpr)subexpr).getZName().toString());
System.out.println ("Time Millis 1 ::: " + System.currentTimeMillis());
}
}
else if (subexpr instanceof SetExpr) {
SetExpr setexpr = (SetExpr)subexpr;
maxabscomm = setexpr.getZExprList().size();
}
}
}
else if (expr instanceof RefExpr) {
if (!shown && ((RefExpr)expr).getZName().toString().equals("ℕ")) {
String str = JOptionPane.showInputDialog("Enter maximum absolute value to be communicated:");
System.out.println ("Time Millis 1 ::: " + System.currentTimeMillis());
maxabscomm = Integer.parseInt(str);
shown = true;
}
else {
maxabscomm = tse.get(((RefExpr)expr).getZName().toString());
System.out.println ("Time Millis 1 ::: " + System.currentTimeMillis());
}
}
else if (expr instanceof SetExpr) {
SetExpr setexpr = (SetExpr)expr;
maxabscomm = setexpr.getZExprList().size();
System.out.println ("Time Millis 1 ::: " + System.currentTimeMillis());
}
return maxabscomm;
}

public int dim = 0;

public Object visitChannelDecl(ChannelDecl channelDecl) { //Visita uma declara��o de canal.
if (channelDecl.getExpr() instanceof RefExpr) {
dim = 1;
}
else if (channelDecl.getExpr() instanceof ProdExpr)
dim =
((((ZExprList)((ProdExpr)channelDecl.getExpr()).getExprList()).size()) > dim)? ((ZExprList)((ProdExpr)channelDecl.getExpr()).getExprList()).size() : dim;
else {/*TODO to be done...*/}

//GCArrayGenerator.generate (dim, projectDir + "\\" + projectName + "\\src\\" + projectName + "\\typing\\GCArray.java", specNameOnTranslation);
//(new CCEnvsGenerator("paper", "src", projectDir)).generateClasses();

ZNameList genNames = channelDecl.getZGenFormals(); //Sam's
System.out.print("");
//ZNameList decl = channelDecl.getZChannelNameList(); //Aqui vai retornar uma lista com todos os canais declarados. Ex: [, chan, chan2, chan3, chan4]

CircusType circusType;

ZNameList declNames = channelDecl.getZChannelNameList(); //By Sam, o de cima modificado
Expr expr = channelDecl.getExpr(); //Aqui � o tipo do canal: Ex: |N (naturais)
if (!this.bench)
this.maxAbsValueComm = exhibitOptionPane4MaxAbsValueComm (expr);
else
this.maxAbsValueComm = 3;
System.out.print("");
this.tse.put("_IntegerType_", this.maxAbsValueComm - this.minComm + 1);
for (int i = 0; i < declNames.size(); i++) { //Sam
ZName declName = (ZName) declNames.get(i); //Sam
this.chanMSEnv.update(declName.toString(), false); //Sam
Vector <Vector <BigInteger>> vv = CCUtil.toVVBigInteger(expr, this.minComm, this.maxAbsValueComm, _env, this.tse);
chanTypeEnv.put(declName.toString(), vv); //Sam
chanTypeEnv.putType(declName.toString(), expr);
chanDimEnv.put (declName.toString(), ExprUtil.sizeOfExpression (expr)); //Sam's
AbsCommValuesGenerator acvg = new AbsCommValuesGenerator ("paper", "src", projectDir, specNameOnTranslation, declName.toString(), vv);
this.absGens.addElement(acvg);
}

if (expr != null) {

if (genNames != null && genNames.size() > 0) {

// Transforms the ListTerm into a list of String
List<String> genNamesString = new ArrayList<String>();
for (int i = 0; i < genNames.size(); i++) {
genNamesString.add(genNames.get(i).toString());
}
circusType = new CircusType(genNamesString, expr);
} else {
circusType = new CircusType(expr);
System.out.print ("");
}

} else {
// Single sync channel
circusType = CircusType.createSyncType();
}
/*
for (int i = 0; i < ((ZNameList)declNames.get(1)).size(); i++) {

ZName declName = (ZName) ((ZNameList)declNames.get(1)).get(i);
//ACIMA: os get(i) acima s�o cada canal, de chan1, at� chan4
this._env.declareName(declName, NameType.Channel, circusType);
}
*/
for (int i = 0; i < declNames.size(); i++) {
ZName declName = (ZName) declNames.get(i);
//ACIMA: os get(i) acima s�o cada canal, de chan1, at� chan4
this._env.declareName(declName, NameType.Channel, circusType);
}

// } else { //Comentado por Samuel
// A channel declaration can only be of type VarDecl
// This must not happen because the parser must not let this pass;
// it's good to check again here anyway.
// Note: better to be a invalid format exception
//throw new InvalidSubTypeException(decl.getClass());
// } //Comentado por Samuel
return null;
}

/**
*
*/
public Object visitVarDecl(VarDecl varDecl) { //Declara��o de vari�vel em defini��o axiom�tica. //VarDecl [x : SAM]

//ListTerm declNames = varDecl.getDeclName(); //By Angela
ZNameList declNames = varDecl.getName(); //Nome da vari�vel da declara��o. No caso de exemplo_czt, retorna "x"
Expr expr = varDecl.getExpr(); //Retorna SAM

// Visits the expression
expr.accept(this);

// Creates a CircusType
CircusType circusType = new CircusType(expr);

// Gets the IdType annotation
NameType nameType = Util.getAndRemoveNameTypeAnn(varDecl);

// Declares each name in the list
for (int i = 0; i < declNames.size(); i++) {

/*DeclName*/ZName declName = /*(DeclName)*/ (ZName) declNames.get(i); //Sam comment: Acho que aqui, cada get(i) retorna um nome da lista de nomes da declara��o de vari�veis. Para exemplo_czt, aqui retorna x

// Declares in the current scope
_env.declareName(declName, nameType, circusType);

// Add the type annotation
Util.addCircusTypeAnn(declName, circusType);
}
return null;
}
/**
* Returns the signature
*/
public Object visitConstDecl(ConstDecl constDecl) { //A SER VERIFICADO DEPOIS
//JOptionPane.showMessageDialog (null, "Visitou ConstDecl");

/*DeclName*/ZName declName = (ZName) constDecl.getName(); //By Sam, modificado do de cima
Expr expr = constDecl.getExpr();

// Visits the expression
expr.accept(this);

// Creates a circus type
CircusType circusType = new CircusType(expr);

// Gets the IdType annotation
NameType nameType = Util.getAndRemoveNameTypeAnn(constDecl);
// Declares in the current scope
circusType = this._env.declareName(declName, nameType, circusType);

// Add the type annotation
Util.addCircusTypeAnn(declName, circusType);

return null;
}
/** **************************************************************************************************************
* Para Tree.
* @return null.
* ***************************************************************************************************************
*/
/**
*
*/
public Object visitChannelPara(ChannelPara channelPara) { //Funciona como esperado
//ListTerm decls = channelPara.getChannelDecl(); //By Angela
ZDeclList decls = (ZDeclList) channelPara.getDeclList(); //Retorna um ZDeclList //By Sam, modified do de cima
for (int i = 0; i < decls.size(); i++) {
//Decl decl = (Decl) decls.get(i); //By Angela
Decl decl = (Decl) decls.get(i); //Retorna um ChannelDeclImpl //By Sam
decl.accept(this);
}
return null;
}
/**
*
*/
public Object visitChannelSetPara(ChannelSetPara channelSetPara) { //A ser verificado depois!!!

//DeclName declName = channelSetPara.getName(); //By Angela!!
ZName declName = (ZName) channelSetPara.getName(); //By Sam, modificado do de cima
ChannelSet channelSet = channelSetPara.getChannelSet();

Set<String> set = (Set<String>) channelSet.accept(this); //Angela's
//CircusChannelSet<String> set = (CircusChannelSet<String>) channelSet.accept(this); //Sam's
_env.declareChannelSet(declName.toString(), set);

return null;
}
/**
*
*/

public Object visitProcessPara(ProcessPara processPara) {

//DeclName declName = processPara.getName(); //By Angela!!
ZName declName = (ZName) processPara.getName(); //Retorna P
CircusProcess circusProcess = processPara.getCircusProcess();

this.addProcToProcess1Env (processPara); //By Samuel Barrocas (01/12/2010, 02:09hs da manh�)
this.processPara = processPara; //By Samuel Barrocas (09/12/2010, 20:07hs)

// Sets the name of the current process
_currentProc = declName;

// Here we enter in the scope of a process
_globalScope = false;

// Hiding is permitted only here
if (circusProcess instanceof HideProcess) {
Util.addHideOkAnn((HideProcess) circusProcess);
}
this.currentProcessName = declName.toString();
// Visits the process
/*if (currentProcessName.equals("Cassino")) {
BasicProcess bp = (BasicProcess) circusProcess;
ParallelismPrinter.printAction (bp.getMainAction());
}*/
if (declName.toString().equals ("Table")) {
System.out.print ("");
}
ProcChanEnv procChanEnv = (ProcChanEnv) circusProcess.accept(this);

this.pn2pce.put(declName.toString(), procChanEnv); //By Samuel Barrocas, 19/12/2010, 06:43hs

// Adds the annotation
Util.addProcChanEnvAnn (processPara, procChanEnv);

/***SAM, 30/11/2010, 03:21hs da manha, a seguir****/
ChanUseEnv chanUseEnvVis = procChanEnv.getChanUseEnvVis();
Iterator <String> iteratorVisible = chanUseEnvVis.iteratorKeys();
iteratorVisible = chanUseEnvVis.iteratorKeys();
while (iteratorVisible.hasNext()) {
String channelName = (String) iteratorVisible.next();
ProcChanUseEnv pcuseenv = procChanEnv.getChanInfoEnv().get(channelName);
if (procChanEnv.getChanInfoEnv().get(channelName).isMultiSync2(this.pcme.get(declName.toString())))
this.chanMSEnv.update (channelName, true);
}
/***SAM, 30/11/2010, 03:21hs da manha, acima****/

this.procProcessParaEnv.put(processPara.getZName().toString(), processPara); //By Samuel Barrocas, 16/12/2010, 17:40hs
this.p2pId.put(Util.getIdCircusProcessAnn(processPara.getCircusProcess()).getId(), processPara.getZName().toString());

// Adds the annotation
Util.addChannelMSEnvAnn(processPara, procChanEnv.getChanInfoEnv());

// TODO: Tirar isto?
_env.declareProcess(declName.toString(), procChanEnv);

// Here we return to the global scope
_globalScope = true;

// Sets the name of the current process to null
_currentProc = null;

return null;
}

/**
*
*/
public Object visitFreePara(FreePara freePara) { //Este est� Ok, funcionando como esperado
ZFreetypeList freeTypes = (ZFreetypeList) freePara.getFreetypeList(); //Retorna ZFreeTypeListImpl
for (int i = 0; i < freeTypes.size(); i++) {
Freetype freetype = (Freetype) freeTypes.get(i); //Retorna FreeTypeImpl
freetype.accept(this);
}
return null;
}

/**
*
*/
public Object visitActionPara(ActionPara actionPara) { //Est� Ok, funcionando corretamente
ProcChanEnv result = null;
mucounter = 0;
ZName actionName = (ZName) actionPara.getName(); //Retorna "Action"
CircusAction circusAction = actionPara.getCircusAction(); //Retorna PrefixingActionImpl

_currentAct = actionName;

//JOptionPane.showMessageDialog (null, "EnvLoadingVisitor linha 857: \n\n" + PrinterUtil.printAction(circusAction));
result = (ProcChanEnv) circusAction.accept(this);

//if (result instanceof ParAction)
//ProcFriendshipEnv.printSetOfSets(((ProcChanEnv)((ParAction)result)).getChanInfoEnv().getFriendship("a"));

//actionPara.setCircusAction(ParallelismVisitor.updatedAction (circusAction, processPara.getCircusProcess(), result.getChanInfoEnv().getChanProcFriendshipEnvMap()));

_currentAct = null;

return result;
}
public Object visitNameSetPara(NameSetPara NameSetPara) { //N�o precisa verificar
return null;
}

public Object visitAxPara(AxPara axPara) {

Box box = axPara.getBox();
NameList genParams = axPara.getNameList(); // not dealing with generics for the moment

SchText schText = axPara.getSchText(); //Retorna ZSchTextImpl

// Validates the value of box
assert(box.equals(Box.AxBox) || box.equals(Box.OmitBox) ||
box.equals(Box.SchBox)): "box = " + box.toString();

if (box.equals(Box.AxBox)
) {

// Axiomatic or horizontal definition
NameType nameType;
if (this._globalScope) {
nameType = NameType.GlobalConstant;
} else {
nameType = NameType.LocalConstant;
}
ZDeclList decls = (ZDeclList)((ZSchText) schText).getDeclList();
Pred pred = ((ZSchText) schText).getPred();

_env.openScope();
for (int i=0; i<decls.size(); i++) {
Decl decl = (Decl) decls.get(i); //Retorna VarDeclImpl
Util.addNameTypeAnn(decl, nameType);
//JOptionPane.showMessageDialog (null, "Visitou " + decls.get(i).getClass());
decl.accept(this);
}
if (pred != null) {
pred.accept(this);
}
Signature signature = _env.getCurrentScopeAsSig();

_env.closeScope();

//Declarations of constants are always brought to outer scope.

defineSignature(signature, NameType.StateComponent);
} else if (box.equals(Box.SchBox)
|| box.equals(Box.OmitBox) //Linha acrescentada por SAMUEL
) { //Trecho de codigo que nao estah sendo executado
ZName schName;
if ((((ZSchText) schText).getZDeclList()).get(0) instanceof ConstDecl)
schName = (ZName) ((ConstDecl)(((ZSchText) schText).getZDeclList()).get(0)).getName();
else {
schName = (ZName) ((VarDecl)(((ZSchText) schText).getZDeclList()).get(0)).getZNameList().get(0);
}
System.out.print ("");
//Gambiarra para resolver o problema de cast em T_FCSMod.tex
//Esse if-else nao eh definitivo,
//pode ser estendido o quanto for necessario, para satisfazer
//as condicoes de cast
int declListOuExprList = 0;
Term/*ZDeclList*/ decls;
Pred pred = null;
if (((ZSchText) schText).getZDeclList().get(0) instanceof ConstDecl) {
if (((ConstDecl)((ZSchText) schText).getZDeclList().get(0)).getExpr() instanceof RefExpr) {
declListOuExprList = 2;
decls = (ZExprList)((RefExpr)((ConstDecl)((ZSchText) schText).getZDeclList().get(0)).getExpr()).getZExprList();
}
else {
declListOuExprList = 1;
decls = (ZDeclList)( (SchExpr)((ConstDecl)((ZSchText) schText).getZDeclList().get(0)).getExpr() ).getZSchText().getZDeclList();
pred = ((SchExpr)((ConstDecl)((ZSchText) schText).getZDeclList().get(0)).getExpr()).getZSchText().getPred();
}
}
else {//if (((ZSchText) schText).getZDeclList().get(0) instanceof VarDecl) {
if (((VarDecl)((ZSchText) schText).getZDeclList().get(0)).getExpr() instanceof RefExpr) {
declListOuExprList = 2;
decls = (ZExprList)((RefExpr)((VarDecl)((ZSchText) schText).getZDeclList().get(0)).getExpr()).getZExprList();
}
else {
declListOuExprList = 1;
decls = (ZDeclList)( (SchExpr)((VarDecl)((ZSchText) schText).getZDeclList().get(0)).getExpr() ).getZSchText().getZDeclList();
pred = ((SchExpr)((VarDecl)((ZSchText) schText).getZDeclList().get(0)).getExpr()).getZSchText().getPred();
}
}
//Acima, gambiarra para resolver o problema de cast em T_FCSMod.tex

_env.openScope();
//Abaixo, codigo modificado em consequencia da gambi
if (declListOuExprList == 1){
for (int i=0; i<((ZDeclList)decls).size(); i++) {
Decl decl = (Decl) ((ZDeclList)decls).get(i);
Util.addNameTypeAnn(decl, NameType.StateComponent);
decl.accept(this);
}
}
else /*(if declListOuExprList == 2)*/ {
for (int i=0; i<((ZExprList)decls).size(); i++) {
Decl decl = (Decl) ((ZExprList)decls).get(i);
Util.addNameTypeAnn(decl, NameType.StateComponent);
decl.accept(this);
}
}
//Acima, codigo modificado em consequencia da gambi

if (pred != null) {
pred.accept(this);
}
Signature signature = _env.getCurrentScopeAsSig();
_env.closeScope();

// Declare name of schema in current scope
_env.declareName(schName, NameType.SchemaName,
new CircusType(new SignatureExpression(signature)));
//Brings the declarations from the schema only if this schema
//is used to declare state.
if (_stateName != null && _stateName.equals(schName.toString())) {
defineSignature(signature, NameType.StateComponent);
}
}

return null;
}
/** **************************************************************************************************************
* ChannelSet Tree.
* @return Set<String>
* ***************************************************************************************************************
*/
/**
*
*/
//public Object visitBasicChannelSet(/*BasicChannelSet*/BasicChannelSetExpr basicChannelSet) { //Ainda a ser verificado

public Object visitChannelSet(/*BasicChannelSet*/ChannelSet basicChannelSet) { //Ainda a ser verificado

Set<String> r = new LinkedHashSet<String>();
//ListTerm list = basicChannelSet.getRefName(); //Angela's
CircusCommunicationList list;
ZName refName;
if (((CircusChannelSet)basicChannelSet).getExpr() instanceof RefExpr) {
//IMPORTANTE: O conte�do desse if corresponde ao conte�do do visitador visitRefChannelSet, no JCircus de Angela
refName =
(ZName)(/*(BasicChannelSetExpr)*/ (RefExpr)(
(CircusChannelSet)basicChannelSet).getExpr()
).getName(); //Checar esse cara depois
Set<String> result = null;
result = this._env.getChannelSet(refName.toString());
return result;
}
else {
//O conte�do deste bloco � o corresponde ao conte�do do visitador visitBasicChannelSet,no JCircus de Angela
list =
(CircusCommunicationList)((BasicChannelSetExpr)(
(CircusChannelSet)basicChannelSet).getExpr()
).getCommunicationList(); //Checar esse cara depois
for (int i = 0; i < list.size(); i++) {
r.add(list.get(i).toString());
}
return r;
}
}
/**
*
*/
//public Object visitRefChannelSet (/*RefChannelSet*/ChannelSet refChannelSet) {
//visitChannelSet, na verdade, vai substituir visitBasicChannelSet!!!!!
public Object visitBasicChannelSetExpr (/*RefChannelSet*/BasicChannelSetExpr refChannelSet) { //Ainda a ser verificado
//Com T_ActMod, nao da para testar esse, pois nao passa por aqui
Set<String> result = null;
//RefName refName = refChannelSet.getRefName(); //Angela's
ZName refName = ((ChannelSetPara) refChannelSet).getZName(); //Sam's

result = this._env.getChannelSet(refName.toString());
return result;
}

/** **************************************************************************************************************
* NameSet Tree.
* ***************************************************************************************************************
*/
/** **************************************************************************************************************
* CircusProcess Tree.
* ***************************************************************************************************************
*/
/**
* The methods of these tree always return a ProcessChannelEnvironment.
*/
/**
*
*/
/*public ActionParaImpl getMainAction (BasicProcess bp) {
Para mainAction;
ZParaList paras = bp.getZParaList();
for (int i = 0; i < paras.size(); i++) {
if (((Para)paras.get(i)).)
}
}*/

public Object visitBasicProcess(BasicProcess basicProcess) {
this.mucounter = 0;
ProcChanEnv result = new ProcChanEnv();
ZName stateSchemaRefName = null;
AxPara statePara = basicProcess.getStatePara(); //AxParaImpl
if (statePara != null) {
ZSchText stateText = statePara.getZSchText();
ZDeclList stateVars = stateText.getZDeclList();
Decl declOfState = stateVars.get(0);
if (declOfState instanceof VarDecl) {
ZNameList namesAtState = ((VarDecl)declOfState).getZNameList();
stateSchemaRefName = (ZName) namesAtState.get(0);
}
else {//if (declOfState instanceof ConstDecl) {
ZName nameAtState = ((ConstDecl)declOfState).getZName();
stateSchemaRefName = nameAtState;
}
}
CircusAction mainAction = basicProcess.getMainAction(); //Angela's
ZParaList actionParas = basicProcess.getZParaList(); //Angela's
ZParaList zParas = basicProcess.getZParaList();
//ZName stateSchemaRefName = (ZName) ((ConstDecl)((ZSchText) basicProcess.getStatePara().getSchText()).getZDeclList().get(0)).getName();

//try {
// Set global variable
if (stateSchemaRefName != null) {
_stateName = stateSchemaRefName.toString();
}

// Visit the zParas
for (int i = 0; i < zParas.size(); i++) { //Acrescentado o CAST ZParaList
if (zParas.get(i) instanceof AxPara || zParas.get(i) instanceof FreePara) {
Para para = (Para) zParas.get(i); //Acrescentado o CAST ZParaList
para.accept(this);
System.out.print ("");
}
}

// Visit the nameSetParas
/*for (int i = 0; i < nameSetParas.size(); i++) {

NameSetPara nameSetPara = (NameSetPara) nameSetParas.get(i); //Angela's
nameSetPara.accept(this);
}*/

if (stateSchemaRefName != null && !stateSchemaRefName.toString().contains ("$$defaultSt")) {

// This process defines a state

// Inserts type annotation in the state
CircusType circusType = this._env.getCircusType(stateSchemaRefName.toString());
Util.addCircusTypeAnn(stateSchemaRefName, circusType);

// Brings all the declarations in the state to this scope (the type
//of the state will always be a signature (schema type))
Signature signature = null;
//if (circusType != null)
signature = ((SignatureExpression) circusType.getExpression()).getSignature();
this.defineSignature(signature, NameType.StateComponent); //DESCOMENTAR
}//*/

ProcChanEnv procChanEnv = null;
// Visits the circusActions
for (int i = 0; i < ((ZParaList) actionParas).size() - 1; i++) { //acrescentei o CAST e o -1
if ((actionParas.get(i) instanceof ActionPara)/* && nao � a acao principal, de circspot*/) {
System.out.print("");
ActionPara actionPara = (ActionPara) ((ZParaList) actionParas).get(i); //Acrescentei o CAST de ZParaList
CircusAction circusAction = actionPara.getCircusAction();

// Visits the circus action only if it is not the state
procChanEnv = (ProcChanEnv) actionPara.accept(this);
result = result.merge(procChanEnv, false, new Vector <String> ());
}
}

// Visits the main action
procChanEnv = (ProcChanEnv) mainAction.accept(this);
result = result.merge(procChanEnv, false, new Vector <String> ());

// Add id annotation
int id = IdCircusProcessGenerator.getGenerator().getNextId();
IdCircusProcessAnn ann = new IdCircusProcessAnn(id);
Util.addIdCircusProcessAnn(basicProcess, ann);

// Updates MultiSyncEnv
result.transform(new Integer (id), this.currentProcessName); //Comentado por Samuel em 06/05/2013

// Set the flag to say that this is a environment for a basic process
result.setBasicProcess();
//}
/*catch (MoreThanOneWriterException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.PAR_WRITERS, params), e);
} catch (ChanUseUnificationException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.CHANUSE_UNIF, params), e);
} catch (DifferentCardinalitiesException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.DIFF_CARD, params), e);
} catch (ChanSyncUnificationException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.CHANSYNC_UNIF, params), e);
}
*/
// Set global variable
_stateName = null;

return result;
}
/**
*
*/
public Object visitCallProcess(CallProcess callProcess) {
ProcChanEnv result = null;
//CallProcess c = null;
//c.getCallExpr();
//RefName refName = callProcess.getRefName(); //Angela's
//CallType callType = callProcess.getCallType(); //Angela's
Name refName = callProcess.getCallExpr().getName(); //Sam's
CallUsage callType = callProcess.getUsage(); //Parameterised

// Validate the value of callType
// assert(callType == CallType.Param || callType == CallType.Gen || //Daqui pra baixo tudo Angela's, at� o �ltimo comentado
// callType == CallType.Index || callType == CallType.Normal):
// "callType = " + callType.toString();

// if (callType == CallType.Normal) {

assert(callType == CallUsage.Parameterised || //Daqui pra baixo ate o correspondente de cima, Sam's
callType == CallUsage.Indexed ):
"callType = " + callType.toString();

if (callType == CallUsage.Parameterised) { //Sam's, correspondente ao CallType.Normal

// Gets the environment of the process
// Is this really necessary? A CallProcess will be translated as another process ...
result = this._env.getProcessChannelEnvironment(refName.toString());

//} else if (callType == CallType.Param || callType == CallType.Gen || callType == CallType.Index) { //Angela's
} else { //Sam's

// ListTerm params = callProcess.getExprList(); //Angela's
ListTerm params = (ListTerm) ((RefExpr) callProcess).getExprList(); //Sam's
// Visit the parameters
for (int i = 0; i < params.size(); i++) {
Expr expr = (Expr) params.get(i);
expr.accept(this);
}
// Gets the environment of the process
result = this._env.getProcessChannelEnvironment(refName.toString());
}

// Add id annotation
IdCircusProcessAnn ann = new IdCircusProcessAnn(IdCircusProcessGenerator.getGenerator().getNextId());

Util.addIdCircusProcessAnn(callProcess, ann);
//this.procProcessParaEnv.put(Util.getIdCircusProcessAnn(processPara.getCircusProcess()).getId(), processPara.getCircusProcess()); //By Samuel Barrocas, 16/12/2010, 17:40hs
this.p2pId.put(ann.getId(), callProcess.getCallExpr().getZName().toString());

if (result.isBasicProcess() /*&& callProcess.getAnn(CopyProcessAnn.class) != null*/) {

// If this is a call to a basic process, I need to substitute the ids
// of the basic process in ChanSyncEnv by the ids of this callProcess
/** Isto estava errado!
MultiSyncEnv multiSyncEnv = result.getMultiSyncEnv();
multiSyncEnv = multiSyncEnv.replaceId(ann.getId());
result.setMultiSyncEnv(multiSyncEnv);
*/
result = result.replaceIdInChanInfoEnv(ann.getId(), refName.toString());
}
return result;
}
/**
*
*/
public Object visitProcess1(Process1 process1) {
ProcChanEnv result;
CircusProcess circusProcess = process1.getCircusProcess();
result = (ProcChanEnv) circusProcess.accept(this);
// Add id annotation
IdCircusProcessAnn ann = new IdCircusProcessAnn(
IdCircusProcessGenerator.getGenerator().getNextId());
Util.addIdCircusProcessAnn(process1, ann);
return result;
}
/**
*
*/
public Object visitRenameProcess(RenameProcess renameProcess) {
ProcChanEnv result = null;
//Com T_Act.tex, n�o pode ser testado
CircusProcess circusProcess = renameProcess.getCircusProcess();
//ListTerm oldChannels = renameProcess.getOldNames(); //Angela's
//ListTerm newChannels = renameProcess.getNewNames(); //Angela's

ZNameList oldChannels = (ZNameList) renameProcess.getAssignmentPairs().getLHS(); //By Sam, mas ainda falta modificar!!!
ZExprList newChannels = (ZExprList) renameProcess.getAssignmentPairs().getRHS(); //By Sam, mas ainda falta modificar!!!
result = (ProcChanEnv) circusProcess.accept(this);
result = result.substitute(newChannels, oldChannels);

// Add id annotation
IdCircusProcessAnn ann = new IdCircusProcessAnn(
IdCircusProcessGenerator.getGenerator().getNextId());

Util.addIdCircusProcessAnn(renameProcess, ann);
return result;
}
/**
*
*/
public Object visitHideProcess(HideProcess hideProcess) {
ProcChanEnv result = null;
CircusProcess circusProcess = hideProcess.getCircusProcess();
ChannelSet channelSet = hideProcess.getChannelSet();

// Check if this hiding is legal
if (Util.getHideOkAnn(hideProcess) == null) {
Object[] params = new Object[] { _currentProc };
reportError(new Error(ErrorMessage.HIDE_PROC_ILLEGAL, params));
}
Set<String> channelNames = (Set<String>) channelSet.accept(this);
result = (ProcChanEnv) circusProcess.accept(this);
result = result.hide(channelNames);

// Add id annotation
IdCircusProcessAnn ann = new IdCircusProcessAnn(
IdCircusProcessGenerator.getGenerator().getNextId());

Util.addIdCircusProcessAnn(hideProcess, ann);
this.alreadyDefinedId = false;
return result;
}
/**
*
*/
public Object visitProcessD(ProcessD processD) {
ProcChanEnv result;
//Com T_ActMod, nao pode ser testado
//ListTerm decls = processD.getDecl(); //By Angela
ZDeclList decls = (ZDeclList) processD.getDeclList(); //By Sam
CircusProcess circusProcess = processD.getCircusProcess();
NameType nameType;
if (processD instanceof ProcessIte || processD instanceof ProcessIdx) {
// TODO: Think about the NameType in this case.
nameType = NameType.LocalVariable;
} else if (processD instanceof ParamProcess) {
nameType = NameType.ProcessParam;
} else {
throw new InvalidSubTypeException(processD.getClass());
}
// Open a new scope to declare the block of variables
_env.openScope();
for (int i = 0; i < decls.size(); i++) {
Decl decl = (Decl) decls.get(i);
// Add IdType annotation to the declaration
Util.addNameTypeAnn(decl, nameType);

//if (((VarDecl)decl).getName().get(0).toString().equals("ident")) {
//System.out.print("");
//}
// Visits the declaration
decl.accept(this);
}
result = (ProcChanEnv) circusProcess.accept(this);
// Close scope
_env.closeScope();
// Add id annotation
/*IdCircusProcessAnn ann = new IdCircusProcessAnn(
IdCircusProcessGenerator.getGenerator().getNextId());*/

Util.addIdCircusProcessAnn(processD, Util.getIdCircusProcessAnn(processD.getCircusBasicProcess())/*ann*/);
this.alreadyDefinedId = false;
return result;
}
/**
*
* @param process2
* @return
*/
public Object visitProcess2(Process2 process2) {
ProcChanEnv result = null;
ProcChanEnv procChanLeft = (ProcChanEnv) process2.getLeftProcess().accept(this); //By Sam, modified do de cima
ProcChanEnv procChanRight = (ProcChanEnv) process2.getRightProcess().accept(this); //By Sam, modified do de cima
result = procChanLeft.merge(procChanRight, false, new Vector <String> ());

// Add id annotation
IdCircusProcessAnn ann = new IdCircusProcessAnn(
IdCircusProcessGenerator.getGenerator().getNextId());

Util.addIdCircusProcessAnn(process2, ann);

return result;
}
/**
* Special case for parallelism
*/
public Object visitParProcess(ParProcess parProcess) {
ProcChanEnv result = null;
IdCircusProcessAnn ann;
ProcChanEnv procChanLeft = (ProcChanEnv) parProcess.getLeftProcess().accept(this);
ProcChanEnv procChanRight = (ProcChanEnv) parProcess.getRightProcess().accept(this);
//procChanLeft.setChannelSet(InterleavingVisitor.channelSet(parProcess)); //By Samuel Barrocas
//procChanRight.setChannelSet(InterleavingVisitor.channelSet(parProcess)); //By Samuel Barrocas
Vector <String> chanSet = ChanSetUtil.channelSet(parProcess, this.spec); //Samuel Barrocas
result = procChanLeft.merge(procChanRight, true, chanSet);
result.setChannelSet (ChanSetUtil.channelSet(parProcess, this.spec)); //By Samuel Barrocas

// Add id annotation
ann = new IdCircusProcessAnn(
IdCircusProcessGenerator.getGenerator().getNextId()
);
Util.addIdCircusProcessAnn(parProcess, ann);
this.cp2cse.put(Util.getIdCircusProcessAnn (parProcess.getLeftProcess()).getId(), ChanSetUtil.channelSet(parProcess, this.spec));
this.cp2cse.put(Util.getIdCircusProcessAnn (parProcess.getRightProcess()).getId(), ChanSetUtil.channelSet(parProcess, this.spec));
System.out.print("");
//}
/*catch (MoreThanOneWriterException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.PAR_WRITERS, params), e);
} catch (ChanUseUnificationException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.CHANUSE_UNIF, params), e);
} catch (ChanSyncUnificationException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.CHANSYNC_UNIF, params), e);
} catch (DifferentCardinalitiesException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
reportFatalError(new Error(ErrorMessage.DIFF_CARD, params), e);
}*/
return result;
}

/** **************************************************************************************************************
* CircusAction Tree.
* ***************************************************************************************************************
*/

/**
*
*/
public Object visitBasicAction(BasicAction basicAction) {
return new ProcChanEnv();
}
/**
*
*/
public Object visitAction1(Action1 action1) {
ProcChanEnv result;
CircusAction circusAction = action1.getCircusAction();
result = (ProcChanEnv) circusAction.accept(this);
return result;
}
/**
*
*/
private int countParams (NameTypeEnv nameTypeEnv) { //Samuel, 12/09/2013
StringBuilder code = new StringBuilder();
//Iterator<DeclName> keys = nameTypeEnv.iteratorKeys(); //Angela's
Iterator<ZName> keys = nameTypeEnv.iteratorKeys(); //Sam's
String comma = "";
int counter = 0;
while (keys.hasNext()) {
ZName declName = keys.next(); //Sam's
NameType nameType = (NameType) declName.getAnn(NameType.class);

if (nameType.equals( NameType.LocalVariable) ||
nameType.equals( NameType.ActionParam)) {

CircusType circusType = nameTypeEnv.getCircusType(declName.toString());
// Code for declaration of parameter
counter++;
}
}
return counter;
}

public void putParamSizeOnMu (String muname, NameTypeEnv localenv) {
int nparams = countParams (localenv);
this._muActs2Indexes.putnparams(this.currentProcessName, muname, nparams);
}

public Object visitMuAction (MuAction muAction) { /* RecursiveAction */
ProcChanEnv result = null;

//DeclName declName = muAction.getName(); //By Angela
ZName declName = (ZName) muAction.getName(); //By Sam, modificado do de cima
NameTypeEnv localEnvironment = this._env.localEnvironment();

CircusAction circusAction = muAction.getCircusAction();

// Add annotation
Util.addNameTypeEnvAnn(muAction, localEnvironment);

if (!this._muActs2Indexes.containsKey(this.currentProcessName, declName.toString())) {
this._muActs2Indexes.putmu(this.currentProcessName, declName.toString(), mucounter);
putParamSizeOnMu (declName.toString(), localEnvironment);
mucounter++;
}

// Open scope for what ? Declaration of DeclName possibly.
_env.openScope();

result = (ProcChanEnv) circusAction.accept(this);

// Close scope
_env.closeScope();

return result;
}
/**
*
*/
public Object visitActionD(ActionD actionD) {
ProcChanEnv result = null;

//ListTerm decls = actionD.getDeclList(); //Original, da Angela
ZDeclList decls = (ZDeclList) actionD.getDeclList(); //By Sam, modificado do de cima
CircusAction circusAction = actionD.getCircusAction();

// Add annotation of local environment
NameTypeEnv localEnvironment = this._env.localEnvironment();
Util.addNameTypeEnvAnn(actionD, localEnvironment);

NameType nameType;
if (actionD instanceof ActionIte) {
nameType = NameType.LocalVariable; //?

} else if (actionD instanceof ParamAction) {
nameType = NameType.ActionParam;
} else {
throw new InvalidSubTypeException(actionD.getClass());
}
// Open scope
_env.openScope();
for (int i = 0; i < decls.size(); i++) {
Decl decl = (Decl) decls.get(i);
// Adds the annotation
Util.addNameTypeAnn(decl, nameType);
// Visits the declaration
decl.accept(this);
//this.environment_.declareInCurrentScope(decl, nameType);
}

result = (ProcChanEnv) circusAction.accept(this);

// Close scope
_env.closeScope();
return result;
}
/**
*
*/
public Object visitGuardedAction(GuardedAction guardedAction) {
ProcChanEnv result = null;
Pred pred = guardedAction.getPred();
CircusAction circusAction = guardedAction.getCircusAction();
pred.accept(this);
result = (ProcChanEnv) circusAction.accept(this);
return result;
}
/**
*
*/
public Object visitAction2(Action2 action2) {
ProcChanEnv result = new ProcChanEnv();
result = (ProcChanEnv) action2.getLeftAction().accept(this);
//try {

result = result.merge((ProcChanEnv) action2.getRightAction().accept(this), false, new Vector <String> ());

//}
/*catch (MoreThanOneWriterException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.PAR_WRITERS, params), e);
} catch (ChanUseUnificationException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.CHANUSE_UNIF, params), e);

} catch (ChanSyncUnificationException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.CHANSYNC_UNIF, params), e);
} catch (DifferentCardinalitiesException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
reportFatalError(new Error(ErrorMessage.DIFF_CARD, params), e);

}*/
return result;
}
/**
*
*/
//OS M�TODOS ABAIXO FORAM ORIGINALMENTE IMPLEMENTADOS PELA ANGELA FREITAS, MAS APENAS EM TranslatorVisitor.
//OS COPIEI E COLEI AQUI PARA AUXILIAR NA CONSTRUCAO DE ChanExtChoiceEnv, no visitador de ExtChoiceAction
private String initialChannelFromAction(CircusAction circusAction) {
String code = "";
// TODO: the rest of the types of CircusActions, including RefName
// (create an action environment, to getCircusType the
// Action from its declName)
if (circusAction instanceof GuardedAction) {
// Recursive case
code = initialChannelFromAction(((GuardedAction) circusAction).getCircusAction());
} else if (circusAction instanceof PrefixingAction) {
// Base case
Communication communication = ((PrefixingAction) circusAction).getCommunication();
// Code for channel
//code = communication.getChanName().toString(); //angela
code = communication.getChannelExpr().getZName().toString(); //sam
}
/*else if (circusAction instanceof ExtChoiceAction) {
}*/
else {
System.out.println (PrinterUtil.printAction (circusAction, spec));
// Error
Object[] params = new Object[] {
_currentProc, _currentAct };
reportError(new Error(ErrorMessage.EXTCH_BRANCH, params));
}
return code;
}

private List<String> initialChannelsFromActionList(List<CircusAction> actions) {
List<String> result = new ArrayList();

for (int i = 0; i < actions.size(); i++) {
CircusAction circusAction = actions.get(i);
String channelName = initialChannelFromAction(circusAction);
result.add(channelName);
}
return result;
}
////////////////////////////////////////////////////////////////////////////////////
public Object visitExtChoiceAction(ExtChoiceAction extChoiceAction) {

// Special case. Need to classify the channels as alting channel input
ProcChanEnv result = new ProcChanEnv();

extChoiceAction = (ExtChoiceAction) ChoiceUtil.replaceGuardsOnExtChoice(extChoiceAction);
CircusAction leftAction = extChoiceAction.getLeftAction();
CircusAction rightAction = extChoiceAction.getRightAction();

if (!(leftAction instanceof ExtChoiceAction) && !(leftAction instanceof PrefixingAction)) {
if (!(rightAction instanceof ExtChoiceAction) && !(rightAction instanceof PrefixingAction)) {
System.out.print ("");
}
}
List<CircusAction> actions = Util.getActions(extChoiceAction, Constants.OP_EXTCHOICE);
List<String> channels = this.initialChannelsFromActionList(actions); //Sam's

for (int i = 0; i < channels.size(); i++) {
String channelName = channels.get(i);
this.cece.update(channelName, true);
}
for (int i = 0; i < actions.size(); i++) {

CircusAction circusAction = (CircusAction) actions.get(i);
// Sets the flag for external choice
_extChoice = true;
//try {
result = result.merge((ProcChanEnv) circusAction.accept(this), false, new Vector <String> ());

//}
/*catch (MoreThanOneWriterException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
reportFatalError(new Error(ErrorMessage.PAR_WRITERS, params), e);

} catch (ChanUseUnificationException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
reportFatalError(new Error(ErrorMessage.CHANUSE_UNIF, params), e);

} catch (ChanSyncUnificationException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.CHANSYNC_UNIF, params), e);
} catch (DifferentCardinalitiesException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
reportFatalError(new Error(ErrorMessage.DIFF_CARD, params), e);
}*/
}
return result;
}
/**
*
*/
public Object visitParAction(ParAction parAction) {
ProcChanEnv result = new ProcChanEnv(), result2 = new ProcChanEnv ();

Vector <String> chanSet = ChanSetUtil.channelSet(parAction, this.spec);

//HashMap <String, ProcFriendshipEnv> mapChanProcFriendshipEnv = ChanInfoEnv.mergeFriendshipBottomUp(parAction, chanSet, this);

// Add annotation
NameTypeEnv localEnvironment = this._env.localEnvironment();
Util.addNameTypeEnvAnn(parAction, localEnvironment);

result = (ProcChanEnv) parAction.getLeftAction().accept(this);
result2 = (ProcChanEnv) parAction.getRightAction().accept(this);
result = result.merge (
result2
/*(ProcChanEnv) parAction.getRightAction().accept(this)*/,
true,
chanSet);

//try {
//result = result.merge((ProcChanEnv) parAction.getRightAction().accept(this), true);
//}
/*catch (MoreThanOneWriterException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.PAR_WRITERS, params), e);
} catch (ChanUseUnificationException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.CHANUSE_UNIF, params), e);

} catch (ChanSyncUnificationException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.CHANSYNC_UNIF, params), e);
} catch (DifferentCardinalitiesException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
reportFatalError(new Error(ErrorMessage.DIFF_CARD, params), e);

}*/
return result;
}
/**
*
*/
//public Object visitCallAction(CallAction callAction) { //Angela's
public Object visitCallAction(CallAction callAction) {
ProcChanEnv result = new ProcChanEnv();

//RefName refName = callAction.getRefName(); //By Angela
//CallType callType = callAction.getCallType(); //By Angela

ZName refName = callAction.getZName(); //By Sam, modificado do de cima
//CallUsage callType = callAction.<alguma coisa> //By Sam, COMENTADO
// Validate the value of callType
//assert(callType == CallType.Param || callType == CallType.Gen || //Angela's
// assert(callType == CallUsage.Parameterised ||//Sam's
//callType == CallType.Index || callType == CallType.Normal): //Angela's
// callType == CallUsage.Indexed) : //Sam's
// "callType = " + callType.toString();

//if (callType == CallType.Normal) { //Angela's
// if (callType != CallUsage.Parameterised && //Sam's, essa e as pr�ximas 3 linhas!!!!!!!!
// callType != CallUsage.Indexed) { //Sam's
// Nothing to be done here.
// The RefName is a reference to another action (in this same
// process) which has already been visited or will be visited.
// This name can be a name of an action or a recursive call
// TODO: Check if this should be annotated as NameType.RecursiveName ?
//Util.addNameTypeAnn(refName, NameType.ActionName);
// } else if (callType == CallType.Param || //Daqui para baixo, at� o que t� comentado, � Angela's
// callType == CallType.Gen || callType == CallType.Index) {
//
// ListTerm params = callAction.getExprList();
// Expr expr;
// } else { //Sam's
ZExprList params = (ZExprList) callAction.getExprList(); //Sam's
Expr expr; //Sam's
// Visit the parameters
for (int i = 0; i < params.size(); i++) {
expr = (Expr) params.get(i);
expr.accept(this);
}
// This name can be a name of an action or a recursive call
// TODO: Check if this should be annotated as NameType.RecursiveName ?
Util.addNameTypeAnn(refName, NameType.ActionName);
//}
return result;
}
/**
*
*/
int counterHash = 0;
public Object visitPrefixingAction(PrefixingAction prefixingAction) {

ProcChanEnv result = null;

CircusAction circusAction = prefixingAction.getCircusAction();
Communication communication = prefixingAction.getCommunication();

// Open scope for the possible declaration of input variable
// ATTENTION: Originally, this was done before the visitation of the fields.
_env.openScope();

// Visits the communication
communication.accept(this);

ChanUse chanUse = ChanUse.Undefined;
ChanSync chanSync = ChanSync.C;

try {
chanUse = Util.getChanUseClassification(communication);
// Finds out the classification (ChanUse and ChanSync) of communication
if (this._extChoice) {
if (chanUse.equals(ChanUse.Output)) {
Object[] params = new Object[] {
_currentProc, _currentAct, communication./*getChanName()*/getChannelExpr().getName()
};
if (!useBarriers) {
reportFatalError(new Error(ErrorMessage.OUTPUT_EXT_CHOICE, params));
}
}
chanUse = ChanUse.Input;
this._extChoice = false;
} else {
chanUse = Util.getChanUseClassification(communication);
}
chanSync = Util.getChanSyncClassification(communication);

} catch (InvalidFormatCommException e) {
Object[] params = new Object[] {
_currentProc, _currentAct, communication./*getChanName()*/getChannelExpr().getName() };
if (!useBarriers || !ChanComplexCommEnv.isComplexComm(communication))
reportFatalError(new Error(ErrorMessage.CHAN_FORMAT, params), e);
}

// Includes as a visible channel
// (Gets the type of the channel in the environment)
CircusType circusTypeChannel = this._env.getCircusType(
communication./*getChanName()*/getChannelExpr().getName().toString());

// Visits the action
result = (ProcChanEnv) circusAction.accept(this);

/*Daqui para baixo, @author: Samuel Lincoln M. Barrocas*/
HashSet <Integer> hs = new HashSet <Integer> ();
hs.add(counterHash /*prefixingAction.hashCode()*/); //OBS: Usar o hashCode n�o est� dando certo... Parece que todas as a��es prefixadas t�m o mesmo c�digo hash!!
//this.pa2ie.put(counterHash, prefixingAction);

prefixingAction.getAnns().add(new Integer (counterHash));
counterHash++;

//ProcFriendshipEnv pfe = new ProcFriendshipEnv (hs);
//HashMap <String, ProcFriendshipEnv> map = new HashMap <String, ProcFriendshipEnv> ();
//map.put(communication.getChannelExpr().getName().toString(), pfe);
//result.setChanInfoEnv (new ChanInfoEnv ());
//result.getChanInfoEnv().setChanProcFriendshipEnvMap(map);
//System.out.println ();
/*Daqui para cima, @author: Samuel Lincoln M. Barrocas*/

// Closes the scope
_env.closeScope();

try {
result.includeVisible(communication./*getChanName()*/getChannelExpr().getName().toString(),
chanUse, chanSync, circusTypeChannel);
System.out.println ("includeVisible");
} catch (ChanDefOtherChanUseException e) {
System.out.println ("ChanDefOtherUse");
} catch (ChanDefOtherChanSyncException e) {
System.out.println ("ChanDefOtherSync");
}
return result;
}
/**
*
*/
public Object visitCommunication(Communication communication) {
// When the integration with the typechecker is done, it will
// not be necessary to add information about circus type only
// name type.
this.chanDotFieldEnv.put(communication);
ZName chanName = (ZName) communication.getChannelExpr().getName(); //Sam's
if (ChanComplexCommEnv.isComplexComm(communication)) { //Sam's
this.ccce.update(chanName.toString(), true);
}
CircusFieldList chanFields = communication.getCircusFieldList(); //Sam's
ZExprList genActuals = communication.getChannelExpr().getZExprList(); //Sam's
BigInteger multiSych = communication.getMultiSych(); //Sam's
CommPattern commType = communication.getCommPattern(); //Sam's
// Gets the type of the channel in the environment
CircusType circusTypeChannel
= this._env.getCircusType(chanName.toString());

// Visits all the fields
for (int i = 0; i < chanFields.size(); i++) {
Field field = (Field) chanFields.get(i);
field.accept(this);
}
int size = chanFields.size();
//this.chanDimEnv.put (chanName.toString(), chanFields.size()); //Sam's
System.out.print("");

// MOVED TO PREFIXING_ACTION
// Open scope for the possible declaration of input variable
// this.environment_.openScope();

// Gets the last parameter
if (chanFields.size() > 0) {
Field field = (Field) chanFields.get(chanFields.size()-1);

if (field instanceof InputField) {
//RefName refName = ((InputField) field).getVariable(); //Angela's
ZName refName = ((InputField) field).getVariableZName(); //Sam's
// Add a type annotation to the input variable
CircusType circusTypeVariable = Util.getLastTypeChannel(
circusTypeChannel, /*(ListTerm)*/genActuals); //Acrescentado o cast ListTerm

circusTypeVariable = this._env.declareName(
//this._factory.createDeclName(refName.toString()), //Angela's
this._factory.createZName(refName.toString()), //Sam's
NameType.LocalVariable,
circusTypeVariable);

Util.addCircusTypeAnn(refName, circusTypeVariable);

if (chanName.toString().equals ("out")) {
System.out.print ("");
}
}
}
return null;
}
/** **************************************************************************************************************
* Field Tree.
* ***************************************************************************************************************
*/
/**
*
*/
public Object visitInputField(InputField inputField) {
Pred restriction = inputField.getRestriction();
//RefName variable = inputField.getVariableName(); //Angela's
ZName variable = (ZName) inputField.getVariableName(); //Sam's
/**
* Declaration of the variable will not be done here.
*
* Instead, it is being done in visit (PrefixingAction), since only
* the last input variable needs to be visited (format requirement).
*/
// variable.addAnn(NameType.LocalVariable);
// this.environment_.declareName(new Name(
// variable.getName()), NameType.LocalVariable, circusTypeChannel);
if (restriction != null) {
restriction.accept(this);
}
return null;
}
/**
*
*/
// public Object visitOutputField(OutputField outputField) { //Angela's
public Object visitOutputField(OutputFieldAnn outputField) { //Sam's
//N�o passa por aqui por T_ActMod.tex
//Expr expr = outputField.getExpression(); //Angela's
Expr expr = ((CircusType) outputField).getExpression(); //Sam's
expr.accept(this);
return null;
}
/**
*
*/
public Object visitDotField(DotField dotField) {
//Expr expr = dotField.getExpression(); //Angela's
Expr expr = dotField.getExpr(); //Sam's
expr.accept(this);
return null;
}
/** **************************************************************************************************************
* Command Tree.
* ***************************************************************************************************************
*/
/**
*
*/
public Object visitAssignmentCommand(AssignmentCommand assignmentCommand) {
ProcChanEnv result = new ProcChanEnv();
/**
* This is result of a change in the AST. No more assignment pairs
* (see Manuela's email 13/08/2005
* - Circus.xsd e CircusTools.xsd)
*/
ZNameList leftVars = (ZNameList) assignmentCommand.getAssignmentPairs().getLHS(); //Sam's
ZExprList exprs = (ZExprList) assignmentCommand.getAssignmentPairs().getRHS(); //Sam's
for (int i = 0; i < leftVars.size(); i++) {

//assignPair = (AssignmentPair) assignmentPairs.get(i);

//RefName refName = (RefName) leftVars.get(i); //assignPair.getLHS(); //Angela's
ZName refName = (ZName) leftVars.get(i); //Sam's
Expr expr = (Expr) exprs.get(i); //assignPair.getRHS();

CircusType circusType = this._env.getCircusType(refName.toString());

// Add annotation of type to the variable
Util.addCircusTypeAnn(refName, circusType);

// Visits the expression
expr.accept(this);
}
return result;
}
/**
*
*/
public Object visitIfGuardedCommand(IfGuardedCommand ifGuardedCommand) {
ProcChanEnv result = new ProcChanEnv();
//ListTerm guardedActions = ifGuardedCommand.getGuardedAction(); //Angela's
CircusActionList guardedActions = (CircusActionList) ifGuardedCommand.getActionList(); //Sam's
for (int i = 0; i < guardedActions.size(); i++) {
GuardedAction guardedAction = (GuardedAction) guardedActions.get(i);
// Visits the guarded action
ProcChanEnv procChanEnv = (ProcChanEnv) guardedAction.accept(this);
//try {
result = result.merge(procChanEnv, false, new Vector <String> ());
//}
/*catch (MoreThanOneWriterException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.PAR_WRITERS, params), e);

} catch (ChanUseUnificationException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.CHANUSE_UNIF, params), e);

} catch (ChanSyncUnificationException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
if (!useBarriers)
reportFatalError(new Error(ErrorMessage.CHANSYNC_UNIF, params), e);
} catch (DifferentCardinalitiesException e) {
Object[] params = new Object[] {
_currentProc, _currentAct };
reportFatalError(new Error(ErrorMessage.DIFF_CARD, params), e);
}*/
}
return result;
}
/**
*
*/
public Object visitVarDeclCommand(VarDeclCommand varDeclCommand) {
ProcChanEnv result = new ProcChanEnv();

//ListTerm decls = varDeclCommand.getDeclarations(); //Angela's
ZDeclList decls = (ZDeclList) varDeclCommand.getDeclList(); //Sam's

CircusAction circusAction = varDeclCommand.getCircusAction();

// Open a new scope for block of variables:
_env.openScope();
int size;
if (decls == null) {
System.out.print ("");
}
if (decls != null)
size = decls.size();
else size = 0;
for (int i = 0; i < size; i++) {
Decl decl = (Decl) decls.get(i);
// Adds the annotation
Util.addNameTypeAnn(decl, NameType.LocalVariable);
DeclUtil.addNameTypeAnnToLocalVariablesOnDecl (decl, this._env);
// Visits
decl.accept(this);
}
result = (ProcChanEnv) circusAction.accept(this);
// Close scope
_env.closeScope();
return result;
}
/** **************************************************************************************************************
* Pred Tree.
* ***************************************************************************************************************
*/
/**
* OK
* @param pred
* @return
*/
public Object visitFact(Fact fact) {
return null;
}
public Object visitExprPred(ExprPred exprPred) {
Expr expr = exprPred.getExpr();
expr.accept(this);
return null;
}
public Object visitMemPred(MemPred memPred) {
Expr leftExpr = memPred.getLeftExpr();
Expr rightExpr = memPred.getRightExpr();
leftExpr.accept(this);
rightExpr.accept(this);
return null;
}
/**
*
*/
public Object visitNegPred(NegPred negPred) {
negPred.getPred().accept(this);
return null;
}
/**
*
*/
public Object visitPred2(Pred2 pred2) {
pred2.getLeftPred().accept(this);
pred2.getRightPred().accept(this);
return null;
}
/** **************************************************************************************************************
* Expr Tree.
* ***************************************************************************************************************
*/
/**
*
*/
public Object visitRefExpr(RefExpr refExpr) {

//RefName refName = refExpr.getRefName(); //Angela's
ZName refName = refExpr.getZName(); //Sam's

System.out.println ("________________\n" + refName.toString());

CircusType circusType;
if (refName.toString().equals("True")) {
circusType = this._env.getCircusType("true");
//Util.addCircusTypeAnn(refName, circusType);
}
else if (refName.toString().equals("False")) {
circusType = this._env.getCircusType("false");
//Util.addCircusTypeAnn(refName, circusType);
}
else {
circusType = this._env.getCircusType(refName.toString());
}
//circusType = this._env.getCircusType(refName.toString());
Util.addCircusTypeAnn(refName, circusType);
// Gets the name type annotation in the environment and inserts an annotation
NameType nameType;
if (this._env.getScopeStack().getNameType(refName.toString()) != null)
nameType = this._env.getNameType(refName.toString());
else if (this._env.isElementFreeType(refName)) {
nameType = NameType.ElemFreeType;
}
else{
nameType = NameType.ProcessParam;
}
Util.addNameTypeAnn(refName, nameType);

return null;
}
/**
*
*/
public Object visitNumExpr(NumExpr NumExpr) {
return null;
}
/**
*
*/
public Object visitExpr2(Expr2 expr2) {

expr2.getLeftExpr().accept(this);
expr2.getRightExpr().accept(this);
return null;
}
/**
*
*/
public Object visitExpr1(Expr1 expr1) {
expr1.getExpr().accept(this);
return null;
}
/**
*
*/
public Object visitExpr0N(Expr0N expr0N) {
//ListTerm exprs = expr0N.getExpr(); //Angela's
ZExprList exprs = (ZExprList) expr0N.getExprList(); //Sam's
for (int i = 0; i < exprs.size(); i++) {
Expr expr = (Expr) exprs.get(i);
expr.accept(this);
}
return null;
}
/**
*
*/
public Object visitBindExpr(BindExpr bindExpr) {
//ListTerm/*<NameExprPair>*/ pairs = bindExpr.getNameExprPair(); //Angela's
ListTerm pairs = (ListTerm) bindExpr.getZDeclList(); //VER DEPOIS COMO FAZER ESSE!!!!!!!!!
for (int i = 0; i < pairs.size(); i++) {
//NameExprPair pair = (NameExprPair) pairs.get(i); //Angela's
//DeclName declName = pair.getName(); //Angela's

RefExpr pair = (RefExpr) pairs.get(i); //Sam's VER DEPOIS COMO FAZER ESSE!!!!!!
ZName declName = (ZName) pair.getName(); //Sam's, fiz essa troca mas n�o sei se � assim!
//Expr expr = pair.getExpr(); //Angela's
Expr expr = (Expr) pair.getZExprList(); //Sam's, vamos ver como faz
// TODO: annotate the name
// Visits the expr
expr.accept(this);
}
return null;
}
/**
*
*/
public Object visitCondExpr(CondExpr condExpr) {
Pred pred = condExpr.getPred();
Expr leftExpr = condExpr.getLeftExpr();
Expr rightExpr = condExpr.getRightExpr();
pred.accept(this);
leftExpr.accept(this);
rightExpr.accept(this);
return null;
}
/**
*
*/
public Object visitSchExpr(SchExpr schExpr) {
//JOptionPane.showMessageDialog (null, "Visitou SchExpr");
SchText schText = schExpr.getSchText();
// TODO: Fix this gambi?
Util.addNameTypeAnn(schText, NameType.StateComponent);
// Visits the schema text
schText.accept(this);
return null;
}
/**
*
*/
public Object visitApplExpr(ApplExpr applExpr) {
Expr leftExpr = applExpr.getLeftExpr();
Expr rightExpr = applExpr.getRightExpr();
Boolean mixfix = applExpr.getMixfix();

if (leftExpr instanceof RefExpr) {
String name = ((RefExpr)leftExpr).getZName().toString();
//if (!name.contains("+") && !name.contains("-") && !name.contains("*") && !name.contains("/")) {
Util.addNameTypeAnn(leftExpr, NameType.ApplFunctName);
//}
}
leftExpr.accept(this);
rightExpr.accept(this);
return null;
}
/** **************************************************************************************************************
* Util methods
* ***************************************************************************************************************
*/
/**
* Declare all the names in the signature in the current scope.
*/
private void defineSignature(Signature signature, NameType nameType) {
ListTerm nameTypePairs = signature.getNameTypePair(); //[x: GIVEN SAM]
for (int i = 0; i < nameTypePairs.size(); i++) {
NameTypePair nameTypePair = (NameTypePair) nameTypePairs.get(i); //x : GIVEN SAM
//DeclName declName = nameTypePair.getName(); //Angela's
ZName declName = (ZName) nameTypePair.getName(); //Sam's
CircusType circusType = (CircusType) nameTypePair.getType(); //GIVEN SAM
_env.declareName(declName, nameType, circusType);
}
}
/**
* Declare in the environment some definitions of the mathematical toolkit.
*/
private void loadMathToolkit() {
// \nat
_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.NAT), //Angela's
this._factory.createZName(MathToolkitConstants.NAT), //Sam's
NameType.GlobalConstant,
CircusType.createCircusInteger(),
true); //addToTypeList
_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.ADD), //Angela's
this._factory.createZName(MathToolkitConstants.ADD), //Sam's
NameType.GlobalConstant,
CircusType.createCircusInteger());
//OBS: Daqui para baixo sempre haver� uma linha comentada por
_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.SUB),
this._factory.createZName(MathToolkitConstants.SUB),
NameType.GlobalConstant,
CircusType.createCircusInteger());

_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.MULT),
this._factory.createZName(MathToolkitConstants.MULT),
NameType.GlobalConstant,
CircusType.createCircusInteger());

_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.DIV),
this._factory.createZName(MathToolkitConstants.DIV),
NameType.GlobalConstant,
CircusType.createCircusInteger());

_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.MOD),
this._factory.createZName(MathToolkitConstants.MOD),
NameType.GlobalConstant,
CircusType.createCircusInteger());
_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.LESS_EQUAL),
this._factory.createZName(MathToolkitConstants.LESS_EQUAL),
NameType.GlobalConstant,
CircusType.createCircusInteger());

_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.GREATER_EQUAL),
this._factory.createZName(MathToolkitConstants.GREATER_EQUAL),
NameType.GlobalConstant,
CircusType.createCircusInteger());
_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.LESS),
this._factory.createZName(MathToolkitConstants.LESS),
NameType.GlobalConstant,
CircusType.createCircusInteger());
_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.GREATER),
this._factory.createZName(MathToolkitConstants.GREATER),
NameType.GlobalConstant,
CircusType.createCircusInteger());
_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.NOTEQUAL),
this._factory.createZName(MathToolkitConstants.NOTEQUAL),
NameType.GlobalConstant,
CircusType.createCircusInteger());
_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.NUM_RANGE),
this._factory.createZName(MathToolkitConstants.NUM_RANGE),
NameType.GlobalConstant,
CircusType.createCircusInteger());
_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.PFUNCTION),
this._factory.createZName(MathToolkitConstants.PFUNCTION),
NameType.GlobalConstant,
CircusType.createCircusInteger());

_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.PINJECTION),
this._factory.createZName(MathToolkitConstants.PINJECTION),
NameType.GlobalConstant,
CircusType.createCircusInteger());

_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.EMPTYSET),
this._factory.createZName(MathToolkitConstants.EMPTYSET),
NameType.GlobalConstant,
CircusType.createCircusInteger());

_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.UNION),
this._factory.createZName(MathToolkitConstants.UNION),
NameType.GlobalConstant,
CircusType.createCircusInteger());

_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.CARDINALITY),
this._factory.createZName(MathToolkitConstants.CARDINALITY),
NameType.GlobalConstant,
CircusType.createCircusInteger());

_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.SUBSETEQ),
this._factory.createZName(MathToolkitConstants.SUBSETEQ),
NameType.GlobalConstant,
CircusType.createCircusInteger());

_env.declareName(
//this._factory.createDeclName(MathToolkitConstants.SET_DIFF),
this._factory.createZName(MathToolkitConstants.SET_DIFF),
NameType.GlobalConstant,
CircusType.createCircusInteger());
}
/*@Override
public Object visitBasicChannelSetExpr(BasicChannelSetExpr arg0) {
// TODO Auto-generated method stub
return null;
}*/
/*@Override
public Object visitChannelSet(ChannelSet arg0) {
// TODO Auto-generated method stub
return arg0;
}*/
@Override
public Object visitOutputFieldAnn(OutputFieldAnn arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object visitNarrPara(NarrPara arg0) {
// TODO Auto-generated method stub
return null;
}
}