Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Desktop/dzialo/paste/HelloDoctor.activity/clips/_eclips_wrap.py
blob: a36c13cf0e4715f2bc0f5383a42e4a706b65d168 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
# _eclips_wrap.py
# environment aware functions for CLIPS, embedded in an Environment class

# (c) 2002-2008 Francesco Garosi/JKS
#  The Author's copyright is expressed through the following notice, thus
#  giving actual rights to copy and use this software to anyone, as expressed
#  in the license text.
#
# NOTICE:
# This software is released under the terms of the GNU Lesser General Public
#  license; a copy of the text has been released with this package (see file
#  license.py), and can be found on the GNU web site, at the following
#  address:
#
#           http://www.gnu.org/copyleft/lesser.html
#
#  Please refer to the license text for any license information. This notice
#  has to be considered part of the license, and should be kept on every copy
#  integral or modified, of the source files. The removal of the reference to
#  the license will be considered an infringement of the license itself.

"""clips - high-level interface to the CLIPS engine module
        (c) 2002-2008 Francesco Garosi/JKS
"""

# standard imports
import sys as _sys

import os as  _os
import types as _types

# the low-level module
import _clips as _c


# ========================================================================== #
# globals

# bring the CLIPS Exception object at top level
ClipsError = _c.ClipsError


# redeclare manifest constants here in order to avoid having to
#  reference the ones defined in te low-level module _clips

# check Python version, and issue an exception if not supported
if _sys.version[:3] < "2.4":
    raise _c.ClipsError("M99: Python 2.4 or higher required")


# these globals are redefined instead of reimported for sake of speed
LOCAL_SAVE = _c.LOCAL_SAVE
VISIBLE_SAVE = _c.VISIBLE_SAVE

WHEN_DEFINED = _c.WHEN_DEFINED
WHEN_ACTIVATED = _c.WHEN_ACTIVATED
EVERY_CYCLE = _c.EVERY_CYCLE

NO_DEFAULT = _c.NO_DEFAULT
STATIC_DEFAULT = _c.STATIC_DEFAULT
DYNAMIC_DEFAULT = _c.DYNAMIC_DEFAULT

DEPTH_STRATEGY = _c.DEPTH_STRATEGY
BREADTH_STRATEGY = _c.BREADTH_STRATEGY
LEX_STRATEGY = _c.LEX_STRATEGY
MEA_STRATEGY = _c.MEA_STRATEGY
COMPLEXITY_STRATEGY = _c.COMPLEXITY_STRATEGY
SIMPLICITY_STRATEGY = _c.SIMPLICITY_STRATEGY
RANDOM_STRATEGY = _c.RANDOM_STRATEGY

CONVENIENCE_MODE = _c.CONVENIENCE_MODE
CONSERVATION_MODE = _c.CONSERVATION_MODE


# import adequate symbols from _clips_wrap
from _clips_wrap import Nil, Integer, Float, String, Symbol, InstanceName, \
                        Multifield, _cl2py, _py2cl, _py2clsyntax, \
                        ClipsIntegerType, ClipsFloatType, ClipsStringType, \
                        ClipsSymbolType, ClipsInstanceNameType, \
                        ClipsMultifieldType, ClipsNilType, \
                        _setStockClasses, _accepts_method, _forces_method, \
                        AROUND, BEFORE, PRIMARY, AFTER



# environment class:
class Environment(object):
    """class representing an environment: implements all global classes"""

    def Activation(self, private_environment):
        environment_object = self
        class Activation(object):
            """high-level Activation class (represents: activation)"""
            __env = private_environment
            __envobject = environment_object
            def __init__(self, o):
                """create an Activation object (internal)"""
                if _c.isActivation(o):
                    self.__activation = o
                else:
                    raise _c.ClipsError("M01: cannot directly create Activation")
            def __str__(self):
                """string form of Activation"""
                return _c.env_getActivationName(self.__env, self.__activation)
            def __repr__(self):
                """representation of Activation"""
                return "<Activation '%s'>" % _c.env_getActivationName(self.__env, self.__activation)
            def __getstate__(self):
                raise _c.ClipsError("M03: cannot pickle objects of type '%s'"
                    % self.__class__.__name__)
            def Next(self):
                """return next Activation"""
                o = _c.env_getNextActivation(self.__env, self.__activation)
                if o:
                    return self.__envobject.Activation(o)
                else:
                    return None
            def PPForm(self):
                """return the pretty-print form of Activation"""
                return _c.env_getActivationPPForm(self.__env, self.__activation)
            def Remove(self):
                """remove this Activation"""
                _c.env_deleteActivation(self.__env, self.__activation)
            def __property_getName(self):
                return Symbol(_c.env_getActivationName(self.__env, self.__activation))
            Name = property(__property_getName, None, None,
                            "retrieve Activation name")
            def __property_setSalience(self, v):
                _c.env_setActivationSalience(self.__env, self.__activation, v)
            def __property_getSalience(self):
                return _c.env_getActivationSalience(self.__env, self.__activation)
            Salience = property(__property_getSalience, __property_setSalience,
                                None, "retrieve Activation salience")
        return Activation
    def Class(self, private_environment):
        environment_object = self
        class Class(object):
            """high-level Class class (represents: defclass)"""
            __env = private_environment
            __envobject = environment_object
            def __init__(self, o):
                """create a Class object (internal)"""
                if _c.isDefclass(o):
                    self.__defclass = o
                else:
                    raise _c.ClipsError("M01: cannot directly create Class")
                class __class_Slots:
                    """define a structure for Class Slots"""
                    def __init__(self, o):
                        self.__defclass = o
                    def __getstate__(self):
                        raise _c.ClipsError("M03: cannot pickle class slots")
                    def Names(self):
                        """return the list of Slot names"""
                        rv = self.__envobject._cl2py(_c.env_classSlots(self.__env, self.__defclass, 1))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    def NamesDefined(self):
                        """return the list of Slot names"""
                        rv = self.__envobject._cl2py(_c.env_classSlots(self.__env, self.__defclass, 0))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def AllowedValues(self, name):
                        """return allowed values for specified Slot"""
                        rv = self.__envobject._cl2py(_c.env_slotAllowedValues(self.__env, self.__defclass, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def AllowedClasses(self, name):
                        """return allowed classes for specified Slot"""
                        rv = self.__envobject._cl2py(_c.env_slotAllowedClasses(self.__env, self.__defclass, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def Cardinality(self, name):
                        """return cardinality for specified Slot"""
                        rv = self.__envobject._cl2py(_c.env_slotCardinality(self.__env, self.__defclass, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def DefaultValue(self, name):
                        """return default value for specified Slot"""
                        rv = self.__envobject._cl2py(_c.env_slotDefaultValue(self.__env, self.__defclass, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def Facets(self, name):
                        """return facet values for specified Slot"""
                        rv = self.__envobject._cl2py(_c.env_slotFacets(self.__env, self.__defclass, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def Range(self, name):
                        """return numeric range information of specified Slot"""
                        rv = self.__envobject._cl2py(_c.env_slotRange(self.__env, self.__defclass, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def Sources(self, name):
                        """return source class names for specified Slot"""
                        rv = self.__envobject._cl2py(_c.env_slotSources(self.__env, self.__defclass, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def Types(self, name):
                        """return names of primitive types for specified Slot"""
                        rv = self.__envobject._cl2py(_c.env_slotTypes(self.__env, self.__defclass, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def HasDirectAccess(self, name):
                        """return True if specified Slot is directly accessible"""
                        return bool(_c.env_slotDirectAccessP(self.__env, self.__defclass, name))
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def Exists(self, name):
                        """return True if specified Slot exists or is inherited"""
                        return bool(_c.env_slotExistP(self.__env, self.__defclass, name, 1))
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def ExistsDefined(self, name):
                        """return True if specified Slot is defined in this Class"""
                        return bool(_c.env_slotExistP(self.__env, self.__defclass, name, 0))
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def IsInitable(self, name):
                        """return True if specified Slot is initable"""
                        return bool(_c.env_slotInitableP(self.__env, self.__defclass, name))
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def IsPublic(self, name):
                        """return True if specified Slot is public"""
                        return bool(_c.env_slotPublicP(self.__env, self.__defclass, name))
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def IsWritable(self, name):
                        """return True if specified Slot is writable"""
                        return bool(_c.env_slotWritableP(self.__env, self.__defclass, name))
                self.__Slots = __class_Slots(self.__defclass)
                try:
                    self.__Slots._class_Slots__env = self.__env
                except AttributeError: pass
                try:
                    self.__Slots._class_Slots__envobject = self.__envobject
                except AttributeError: pass
            def __str__(self):
                """string form of Class"""
                return _c.env_getDefclassName(self.__env, self.__defclass)
            def __repr__(self):
                """representation of Class"""
                s = repr(self.__defclass)[1:-1]
                return "<Class '%s': %s>" % (
                    _c.env_getDefclassName(self.__env, self.__defclass), s)
            def __getstate__(self):
                raise _c.ClipsError("M03: cannot pickle objects of type '%s'"
                    % self.__class__.__name__)
            def Next(self):
                """return next Class"""
                o = _c.env_getNextDefclass(self.__env, self.__defclass)
                if o:
                    return self.__envobject.Class(o)
                else:
                    return None
            def PPForm(self):
                """return the pretty-print form of Class"""
                return _c.env_getDefclassPPForm(self.__env, self.__defclass)
            def Description(self):
                """return a summary of Class description"""
                _c.routerClear("temporary")
                _c.env_describeClass(self.__env, "temporary", self.__defclass)
                return _c.routerRead("temporary").strip()
            def IsSubclassOf(self, o):
                """test whether this Class is a subclass of specified Class"""
                return bool(_c.env_subclassP(self.__env, self.__defclass, o.__defclass))
            def IsSuperclassOf(self, o):
                """test whether this Class is a superclass of specified Class"""
                return bool(_c.env_superclassP(self.__env, self.__defclass, o.__defclass))
            def Subclasses(self, inherit=True):
                """return the names of subclasses"""
                return Multifield(
                    self.__envobject._cl2py(_c.env_classSubclasses(self.__env, self.__defclass, inherit)))
            def Superclasses(self, inherit=True):
                """return the names of superclasses"""
                return Multifield(
                    self.__envobject._cl2py(_c.env_classSuperclasses(self.__env, self.__defclass, inherit)))
            @_accepts_method((str, unicode))
            @_forces_method(str)
            def RawInstance(self, name):
                """create an empty Instance of this Class with specified name"""
                return self.__envobject.Instance(_c.env_createRawInstance(self.__env, self.__defclass, name))
            def InitialInstance(self):
                """return initial Instance of this Class"""
                try:
                    return self.__envobject.Instance(_c.env_getNextInstanceInClass(self.__env, self.__defclass))
                except:
                    raise _c.ClipsError("M02: could not find any Instance")
            def NextInstance(self, instance):
                """return next Instance of this Class"""
                i = _c.env_getNextInstanceInClass(self.__env, 
                    self.__defclass, instance._Instance__instance)
                if _c.isInstance(i):
                    return self.__envobject.Instance(i)
                else:
                    return None
            def InitialSubclassInstance(self):
                """return initial instance of this Class and subclasses"""
                try:
                    return self.__envobject.Instance(_c.env_getNextInstanceInClassAndSubclasses(self.__env, 
                        self.__defclass))
                except:
                    raise _c.ClipsError("M02: could not find any Instance")
            def NextSubclassInstance(self, instance):
                """return next instance of this Class and subclasses"""
                i = _c.env_getNextInstanceInClassAndSubclasses(self.__env, 
                    self.__defclass, instance._Instance__instance)
                if _c.isInstance(i):
                    return self.__envobject.Instance(i)
                else:
                    return None
            def Remove(self):
                """remove this Class"""
                _c.env_undefclass(self.__env, self.__defclass)
            @_accepts_method((str, unicode), (str, unicode), None)
            @_forces_method(str, str, None)
            def BuildSubclass(self, name, text="", comment=None):
                """build a subclass of this Class with specified name and body"""
                if comment:
                    cmtstr = '"%s"' % str(comment).replace('"', '\\"')
                else:
                    cmtstr = ""
                clname = _c.env_getDefclassName(self.__env, self.__defclass)
                cltext = "(is-a %s)" % clname + text
                construct = "(defclass %s %s %s)" % (name, cmtstr, cltext)
                _c.env_build(self.__env, construct)
                return self.__envobject.Class(_c.env_findDefclass(self.__env, name))
            @_accepts_method((str, unicode), (str, unicode))
            @_forces_method(str, str)
            def BuildInstance(self, name, overrides=""):
                """build an instance of this class overriding specified slots"""
                clname = _c.env_getDefclassName(self.__env, self.__defclass)
                cmdstr = "(%s of %s %s)" % (name, clname, overrides)
                return self.__envobject.Instance(_c.env_makeInstance(self.__env, cmdstr))
            def __property_getName(self):
                return Symbol(_c.env_getDefclassName(self.__env, self.__defclass))
            Name = property(__property_getName, None, None, "retrieve Class name")
            def __property_getModule(self):
                return Symbol(_c.env_defclassModule(self.__env, self.__defclass))
            Module = property(__property_getModule, None, None,
                              "retrieve Class Module name")
            def __property_getDeletable(self):
                return bool(_c.env_isDefclassDeletable(self.__env, self.__defclass))
            Deletable = property(__property_getDeletable, None, None,
                                 "verify if this Class can be deleted")
            def __property_getAbstract(self):
                return bool(_c.env_classAbstractP(self.__env, self.__defclass))
            Abstract = property(__property_getAbstract, None, None,
                                "verify if this Class is abstract or not")
            def __property_getReactive(self):
                return bool(_c.env_classReactiveP(self.__env, self.__defclass))
            Reactive = property(__property_getReactive, None, None,
                                "verify if this Class is reactive or not")
            def __property_setWatchSlots(self, v):
                _c.env_setDefclassWatchSlots(self.__env, v, self.__defclass)
            def __property_getWatchSlots(self):
                return bool(_c.env_getDefclassWatchSlots(self.__env, self.__defclass))
            WatchSlots = property(__property_getWatchSlots, __property_setWatchSlots,
                                  None, "set/retrieve Slot debug status")
            def __property_setWatchInstances(self, v):
                _c.env_setDefclassWatchInstances(self.__env, v, self.__defclass)
            def __property_getWatchInstances(self):
                return bool(_c.env_getDefclassWatchInstances(self.__env, self.__defclass))
            WatchInstances = property(__property_getWatchInstances,
                                      __property_setWatchInstances,
                                      None, "set/retrieve Instance debug status")
            def __property_getSlots(self): return self.__Slots
            Slots = property(__property_getSlots, None, None,
                             "Class Slots information")
            @_accepts_method((str, unicode), (str, unicode), (str, unicode), None, None)
            @_forces_method(str, str, str, None, None)
            def AddMessageHandler(self, name, args, text, htype=PRIMARY, comment=None):
                """build a MessageHandler for this class with arguments and body"""
                if comment:
                    cmtstr = '"%s"' % str(comment).replace('"', '\\"')
                else:
                    cmtstr = ""
                htype = htype.lower()
                if not htype in (AROUND, BEFORE, PRIMARY, AFTER):
                    raise ValueError("htype must be AROUND, BEFORE, PRIMARY or AFTER")
                if type(args) in (tuple, list):
                    sargs = " ".join(args)
                elif args is None:
                    sargs = ""
                else:
                    sargs = str(args)
                hclass = _c.env_getDefclassName(self.__env, self.__defclass)
                construct = "(defmessage-handler %s %s %s %s (%s) %s)" % (
                    hclass, name, htype, cmtstr, sargs, text)
                _c.env_build(self.__env, construct)
                return _c.env_findDefmessageHandler(self.__env, self.__defclass, name, htype)
            @_accepts_method((str, unicode), None)
            @_forces_method(str, None)
            def MessageHandlerIndex(self, name, htype=PRIMARY):
                """find the specified MessageHandler"""
                htype = htype.lower()
                if htype in (AROUND, BEFORE, PRIMARY, AFTER):
                    return _c.env_findDefmessageHandler(self.__env, self.__defclass, name, htype)
                else:
                    raise ValueError(
                        "htype must be in AROUND, BEFORE, PRIMARY, AFTER")
            def MessageHandlerName(self, index):
                """return name of specified MessageHandler"""
                return Symbol(_c.env_getDefmessageHandlerName(self.__env, self.__defclass, index))
            def MessageHandlerPPForm(self, index):
                """return the pretty-print form of specified MessageHandler"""
                return _c.env_getDefmessageHandlerPPForm(self.__env, self.__defclass, index)
            def MessageHandlerType(self, index):
                """return type of specified MessageHandler"""
                return _c.env_getDefmessageHandlerType(self.__env, self.__defclass, index)
            def MessageHandlerWatched(self, index):
                """return watch status of specified MessageHandler"""
                return bool(_c.env_getDefmessageHandlerWatch(self.__env, self.__defclass, index))
            def MessageHandlerDeletable(self, index):
                """return True if specified MessageHandler can be deleted"""
                return bool(_c.env_isDefmessageHandlerDeletable(self.__env, self.__defclass, index))
            def NextMessageHandlerIndex(self, index):
                """return index of next MessageHandler wrt. specified"""
                return _c.env_getNextDefmessageHandler(self.__env, self.__defclass, index)
            def RemoveMessageHandler(self, index):
                """remove the specified MessageHandler"""
                return _c.env_undefmessageHandler(self.__env, self.__defclass, index)
            def WatchMessageHandler(self, index):
                """watch specified MessageHandler"""
                return _c.env_setDefmessageHandlerWatch(self.__env, True, self.__defclass, index)
            def UnwatchMessageHandler(self, index):
                """unwatch specified MessageHandler"""
                return _c.env_setDefmessageHandlerWatch(self.__env, False, self.__defclass, index)
            def MessageHandlerList(self):
                """return list of MessageHandler constructs of this Class"""
                o = _c.env_getDefmessageHandlerList(self.__env, self.__defclass, False)
                li, rv = Multifield(self.__envobject._cl2py(o)), []
                l = len(li) / 3
                for x in range(0, l):
                    rv.append(Multifield([li[x * 3], li[x * 3 + 1], li[x * 3 + 2]]))
                return Multifield(rv)
            def AllMessageHandlerList(self):
                """return list of MessageHandlers of this Class and superclasses"""
                o = _c.env_getDefmessageHandlerList(self.__env, self.__defclass, True)
                li, rv = Multifield(self.__envobject._cl2py(o)), []
                l = len(li) / 3
                for x in range(0, l):
                    rv.append(Multifield([li[x * 3], li[x * 3 + 1], li[x * 3 + 2]]))
                return Multifield(rv)
            def PrintMessageHandlers(self):
                """print list of all MessageHandlers of this Class"""
                _c.routerClear("temporary")
                _c.env_listDefmessageHandlers(self.__env, "temporary", self.__defclass)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            def PrintAllMessageHandlers(self):
                """print list of MessageHandlers of this Class and superclasses"""
                _c.routerClear("temporary")
                _c.env_listDefmessageHandlers(self.__env, "temporary", self.__defclass, 1)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            @_accepts_method((str, unicode))
            @_forces_method(str)
            def PreviewSend(self, msgname):
                """print list of MessageHandlers suitable for specified message"""
                _c.routerClear("temporary")
                _c.env_previewSend(self.__env, "temporary", self.__defclass, msgname)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
        return Class
    def Deffacts(self, private_environment):
        environment_object = self
        class Deffacts(object):
            """high-level Deffacts class (represents: deffacts)"""
            __env = private_environment
            __envobject = environment_object
            def __init__(self, o):
                """create a Deffacts object (internal)"""
                if _c.isDeffacts(o):
                    self.__deffacts = o
                else:
                    raise _c.ClipsError("M01: cannot directly create Deffacts")
            def __str__(self):
                """string form of Deffacts"""
                return _c.env_getDeffactsName(self.__env, self.__deffacts)
            def __repr__(self):
                """representation of Deffacts"""
                s = repr(self.__deffacts)[1:-1]
                return "<Deffacts '%s': %s>" % (
                    _c.env_getDeffactsName(self.__env, self.__deffacts), s)
            def __getstate__(self):
                raise _c.ClipsError("M03: cannot pickle objects of type '%s'"
                      % self.__class__.__name__)
            def Next(self):
                """return next Deffacts"""
                o = _c.env_getNextDeffacts(self.__env, self.__deffacts)
                if(o):
                    return self.__envobject.Deffacts(o)
                else:
                    return None
            def PPForm(self):
                """return the pretty-print form of Deffacts"""
                return _c.env_getDeffactsPPForm(self.__env, self.__deffacts)
            def Remove(self):
                """remove Deffacts"""
                _c.env_undeffacts(self.__env, self.__deffacts)
            def __property_getName(self):
                return Symbol(_c.env_getDeffactsName(self.__env, self.__deffacts))
            Name = property(__property_getName, None, None, "retrieve Deffacts name")
            def __property_getModule(self):
                return Symbol(_c.env_deffactsModule(self.__env, self.__deffacts))
            Module = property(__property_getModule, None, None,
                              "retrieve Deffacts Module name")
            def __property_getDeletable(self):
                return bool(_c.env_isDeffactsDeletable(self.__env, self.__deffacts))
            Deletable = property(__property_getDeletable, None, None,
                                 "verify if this Deffacts can be deleted")
        return Deffacts
    def Definstances(self, private_environment):
        environment_object = self
        class Definstances(object):
            """high-level Definstances class (represents: definstances)"""
            __env = private_environment
            __envobject = environment_object
            def __init__(self, o):
                """create a Definstances object (internal)"""
                if _c.isDefinstances(o): self.__definstances = o
                else: raise _c.ClipsError("M01: cannot directly create Definstances")
            def __str__(self):
                """string form of Definstances"""
                return _c.env_getDefinstancesName(self.__env, self.__definstances)
            def __repr__(self):
                """representation of Definstances"""
                s = repr(self.__definstances)[1:-1]
                return "<Definstances '%s': %s>" % (
                    _c.env_getDefinstancesName(self.__env, self.__definstances), s)
            def __getstate__(self):
                raise _c.ClipsError("M03: cannot pickle objects of type '%s'"
                    % self.__class__.__name__)
            def Next(self):
                """return next Definstances"""
                o = _c.env_getNextDefinstances(self.__env, self.__definstances)
                if o:
                    return self.__envobject.Definstances(o)
                else:
                    return None
            def PPForm(self):
                """return the pretty-print form of Definstances"""
                return _c.env_getDefinstancesPPForm(self.__env, self.__definstances)
            def Remove(self):
                """delete this Definstances object"""
                _c.env_undefinstances(self.__env, self.__definstances)
            def __property_getModule(self):
                return Symbol(_c.env_definstancesModule(self.__env, self.__definstances))
            Module = property(__property_getModule, None, None,
                              "retrieve Definstances module")
            def __property_getName(self):
                return Symbol(_c.env_getDefinstancesName(self.__env, self.__definstances))
            Name = property(__property_getName, None, None,
                            "retrieve Definstances name")
            def __property_getDeletable(self):
                return bool(_c.env_isDefinstancesDeletable(self.__env, self.__definstances))
            Deletable = property(__property_getDeletable, None, None,
                                 "verify if this Definstances can be deleted")
        return Definstances
    def Fact(self, private_environment):
        environment_object = self
        class Fact(object):
            """high-level Fact class (represents: fact)"""
            __env = private_environment
            __envobject = environment_object
            def __init__(self, o):
                """create a Fact object"""
                class __fact_Slots:
                    """access fact Slots"""
                    def __init__(self, fo):
                        self.__fact = fo
                    @_accepts_method((str, unicode), None)
                    @_forces_method(str, None)
                    def __setitem__(self, name, v):
                        _c.env_putFactSlot(self.__env, self.__fact, name, self.__envobject._py2cl(v))
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def __getitem__(self, name):
                        if not name:
                            return self.__envobject._cl2py(_c.env_getFactSlot(self.__env, self.__fact))
                        else:
                            return self.__envobject._cl2py(_c.env_getFactSlot(self.__env, self.__fact, name))
                    def keys(self):
                        return self.__envobject._cl2py(_c.env_factSlotNames(self.__env, self.__fact))
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def has_key(self, k):
                        return k in map(str, self.__envobject._cl2py(_c.env_factSlotNames(self.__env, self.__fact)))
                    def __repr__(self):
                        return "<Fact '%s' Slots>" \
                               % _c.env_getFactPPForm(self.__env, self.__fact).split()[0]
                    def __getstate__(self):
                        raise _c.ClipsError("M03: cannot pickle fact slots")
                if _c.isFact(o):
                    self.__fact = o
                elif '_Fact__fact' in dir(o) and _c.isFact(o.__fact):
                    self.__fact = o.__fact
                elif _c.isDeftemplate(o):
                    self.__fact = _c.env_createFact(self.__env, o)
                elif '_Template__deftemplate' in dir(o) and \
                   _c.isDeftemplate(o._Template__deftemplate):
                    self.__fact = _c.env_createFact(self.__env, o._Template__deftemplate)
                elif type(o) == str:
                    try:
                        self.__fact = _c.env_assertString(self.__env, o)
                    except:
                        raise ValueError("invalid assertion string")
                else:
                    raise TypeError("argument should be Fact, Template or str")
                self.__Slots = __fact_Slots(self.__fact)
                try:
                    self.__Slots._fact_Slots__env = self.__env
                except AttributeError: pass
                try:
                    self.__Slots._fact_Slots__envobject = self.__envobject
                except AttributeError: pass
            def __str__(self):
                """string form of Fact"""
                return _c.env_getFactPPForm(self.__env, self.__fact).split()[0]
            def __repr__(self):
                """representation of Fact"""
                s = repr(self.__fact)[1:-1]
                return "<Fact '%s': %s>" % (
                    _c.env_getFactPPForm(self.__env, self.__fact).split()[0], s)
            def __getstate__(self):
                raise _c.ClipsError(
                    "M03: cannot pickle objects of type '%s'"
                    % self.__class__.__name__)
            def Assert(self):
                """assert this Fact"""
                self.__fact = _c.env_assertFact(self.__env, self.__fact)
            def Retract(self):
                """retract this Fact"""
                _c.env_retract(self.__env, self.__fact)
            def AssignSlotDefaults(self):
                """assign Fact Slot defaults"""
                _c.env_assignFactSlotDefaults(self.__env, self.__fact)
            def Next(self):
                """return next Fact"""
                o = _c.env_getNextFact(self.__env, self.__fact)
                if(o):
                    return self.__envobject.Fact(o)
                else:
                    return None
            def PPForm(self):
                """return the pretty-print form of Fact"""
                return _c.env_getFactPPForm(self.__env, self.__fact)
            def PPrint(self, ignoredefaults=True):
                """pretty-print fact, possibly including slot default values"""
                _c.routerClear("temporary")
                _c.env_ppFact(self.__env, self.__fact, "temporary", ignoredefaults)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            def CleanPPForm(self):
                """return the pretty-print form of Fact"""
                return _c.env_getFactPPForm(self.__env, self.__fact).split(None, 1)[1].strip()
            def __property_getRelation(self):
                return Symbol(
                    _c.env_getFactPPForm(self.__env, self.__fact).split(
                        None, 1)[1].strip()[1:-1].split(None, 1)[0])
            Relation = property(__property_getRelation, None, None,
                                "fact relation symbol")
            def __property_getImpliedSlots(self):
                try:
                    mli = self.__envobject._cl2py(_c.env_getFactSlot(self.__env, self.__fact))
                except:
                    mli = Multifield([])
                return mli
            ImpliedSlots = property(__property_getImpliedSlots, None, None,
                                    "list of implied Slots")
            def __property_getIndex(self):
                return _c.env_factIndex(self.__env, self.__fact)
            Index = property(__property_getIndex, None, None, "index of this Fact")
            def __property_getSlots(self):
                return self.__Slots
            Slots = property(__property_getSlots, None, None,
                             """Fact Slots dictionary""")
            def __property_getTemplate(self):
                return self.__envobject.Template(_c.env_factDeftemplate(self.__env, self.__fact))
            Template = property(__property_getTemplate, None, None,
                                """Template for this Fact""")
            def __property_getExists(self):
                return bool(_c.env_factExistp(self.__env, self.__fact))
            Exists = property(__property_getExists, None, None,
                              "determine if Fact has been asserted and not retracted")
        return Fact
    def Function(self, private_environment):
        environment_object = self
        class Function(object):
            """high-level Function class (represents: deffunction)"""
            __env = private_environment
            __envobject = environment_object
            def __init__(self, o):
                """create a Function object (internal)"""
                if _c.isDeffunction(o):
                    self.__deffunction = o
                else:
                    raise _c.ClipsError("M01: cannot directly create Function")
            def __str__(self):
                """string form of Function"""
                return _c.env_getDeffunctionName(self.__env, self.__deffunction)
            def __repr__(self):
                """representation of Function"""
                s = repr(self.__deffunction)[1:-1]
                return "<Function '%s': %s>" % (
                    _c.env_getDeffunctionName(self.__env, self.__deffunction), s)
            def __getstate__(self):
                raise _c.ClipsError("M03: cannot pickle objects of type '%s'"
                    % self.__class__.__name__)
            def Next(self):
                """return next Function"""
                o = _c.env_getNextDeffunction(self.__env, self.__deffunction)
                if o:
                    return self.__envobject.Function(o)
                else:
                    return None
            def PPForm(self):
                """return the pretty-print form of Function"""
                return _c.env_getDeffunctionPPForm(self.__env, self.__deffunction)
            def Remove(self):
                """remove this Function"""
                _c.env_undeffunction(self.__env, self.__deffunction)
            def Call(self, *args):
                """call this Function with given arguments"""
                func = _c.env_getDeffunctionName(self.__env, self.__deffunction)
                if args:
                    if(len(args) == 1 and type(args[0]) == str):
                        sargs = args[0]
                    else:
                        li = []
                        for x in args:
                            t1 = type(x)
                            if t1 in (ClipsIntegerType, ClipsFloatType,
                                      ClipsStringType, ClipsSymbolType, ClipsNilType,
                                      ClipsInstanceNameType, ClipsMultifieldType):
                                li.append(_py2clsyntax(x))
                            elif t1 in (int, long):
                                li.append(Integer(x).clsyntax())
                            elif t1 == float:
                                li.append(Float(x).clsyntax())
                            elif t1 in (str, unicode):
                                li.append(String(x).clsyntax())
                            elif isinstance(x, int):
                                li.append(Integer(x).clsyntax())
                            elif isinstance(x, long):
                                li.append(Integer(x).clsyntax())
                            elif isinstance(x, float):
                                li.append(Float(x).clsyntax())
                            elif isinstance(x, str):
                                li.append(String(x).clsyntax())
                            elif isinstance(x, unicode):
                                li.append(String(x).clsyntax())
                            else:
                                li.append(str(x))
                        sargs = " ".join(li)
                    return self.__envobject._cl2py(_c.env_functionCall(self.__env, func, sargs))
                else:
                    return self.__envobject._cl2py(_c.env_functionCall(self.__env, func))
            __call__ = Call
            def __property_getName(self):
                return Symbol(_c.env_getDeffunctionName(self.__env, self.__deffunction))
            Name = property(__property_getName, None, None, "retrieve Function name")
            def __property_getModule(self):
                return Symbol(_c.env_deffunctionModule(self.__env, self.__deffunction))
            Module = property(__property_getModule, None, None,
                              "retrieve Function Module name")
            def __property_getDeletable(self):
                return bool(_c.env_isDeffunctionDeletable(self.__env, self.__deffunction))
            Deletable = property(__property_getDeletable, None, None,
                                 "verify if this Function can be deleted")
            def __property_setWatch(self, v):
                _c.env_setDeffunctionWatch(self.__env, v, self.__deffunction)
            def __property_getWatch(self):
                return bool(_c.env_getDeffunctionWatch(self.__env, self.__deffunction))
            Watch = property(__property_getWatch, __property_setWatch,
                             None, "set/retrieve Function debug status")
        return Function
    def Generic(self, private_environment):
        environment_object = self
        class Generic(object):
            """high-level Generic class (represents: defgeneric)"""
            __env = private_environment
            __envobject = environment_object
            def __init__(self, o):
                """create a Generic function object (internal)"""
                if _c.isDefgeneric(o):
                    self.__defgeneric = o
                else:
                    raise _c.ClipsError("M01: cannot directly create Generic")
            def __str__(self):
                """string form of Generic"""
                return _c.env_getDefgenericName(self.__env, self.__defgeneric)
            def __repr__(self):
                """representation of Generic"""
                s = repr(self.__defgeneric)[1:-1]
                return "<Generic '%s': %s>" % (
                    _c.env_getDefgenericName(self.__env, self.__defgeneric), s)
            def __getstate__(self):
                raise _c.ClipsError("M03: cannot pickle objects of type '%s'"
                    % self.__class__.__name__)
            def Next(self):
                """return next Generic"""
                o = _c.env_getNextDefgeneric(self.__env, self.__defgeneric)
                if o:
                    return self.__envobject.Generic(o)
                else:
                    return None
            def PPForm(self):
                """return the pretty-print form of Generic"""
                return _c.env_getDefgenericPPForm(self.__env, self.__defgeneric)
            def Remove(self):
                """remove this Generic"""
                _c.env_undefgeneric(self.__env, self.__defgeneric)
            def Call(self, *args):
                """call this Generic with given arguments"""
                func = _c.env_getDefgenericName(self.__env, self.__defgeneric)
                if args:
                    if(len(args) == 1 and type(args[0]) in (str, unicode)):
                        sargs = str(args[0])
                    else:
                        li = []
                        for x in args:
                            t1 = type(x)
                            if t1 in (ClipsIntegerType, ClipsFloatType,
                                      ClipsStringType, ClipsSymbolType, ClipsNilType,
                                      ClipsInstanceNameType, ClipsMultifieldType):
                                li.append(_py2clsyntax(x))
                            elif t1 in (int, long):
                                li.append(Integer(int(x)).clsyntax())
                            elif t1 == float:
                                li.append(Float(x).clsyntax())
                            elif t1 in (str, unicode):
                                li.append(String(x).clsyntax())
                            elif isinstance(x, int):
                                li.append(Integer(x).clsyntax())
                            elif isinstance(x, long):
                                li.append(Integer(x).clsyntax())
                            elif isinstance(x, float):
                                li.append(Float(x).clsyntax())
                            elif isinstance(x, str):
                                li.append(String(x).clsyntax())
                            elif isinstance(x, unicode):
                                li.append(String(x).clsyntax())
                            else:
                                li.append(str(x))
                        sargs = " ".join(li)
                    return self.__envobject._cl2py(_c.env_functionCall(self.__env, func, sargs))
                else:
                    return self.__envobject._cl2py(_c.env_functionCall(self.__env, func))
            __call__ = Call
            def __property_getName(self):
                return Symbol(_c.env_getDefgenericName(self.__env, self.__defgeneric))
            Name = property(__property_getName, None, None, "retrieve Generic name")
            def __property_getModule(self):
                return Symbol(_c.env_defgenericModule(self.__env, self.__defgeneric))
            Module = property(__property_getModule, None, None,
                              "retrieve Generic Module name")
            def __property_getDeletable(self):
                return bool(_c.env_isDefgenericDeletable(self.__env, self.__defgeneric))
            Deletable = property(__property_getDeletable, None, None,
                                 "verify if this Generic can be deleted")
            def __property_setWatch(self, v):
                _c.env_setDefgenericWatch(self.__env, v, self.__defgeneric)
            def __property_getWatch(self):
                return bool(_c.env_getDefgenericWatch(self.__env, self.__defgeneric))
            Watch = property(__property_getWatch, __property_setWatch,
                             None, "set/retrieve Generic debug status")
            def MethodList(self):
                """return the list of Method indices for this Generic"""
                o = _c.env_getDefmethodList(self.__env, self.__defgeneric)
                li, mli = Multifield(self.__envobject._cl2py(o)), Multifield([])
                l = len(li) / 2
                for x in range(0, l):
                    mli.append(li[2 * x + 1])
                return mli
            def MethodDescription(self, midx):
                """return the synopsis of specified Method restrictions"""
                return _c.env_getDefmethodDescription(self.__env, midx, self.__defgeneric)
            def MethodPPForm(self, midx):
                """return the pretty-print form of specified Method"""
                return _c.env_getDefmethodPPForm(self.__env, midx, self.__defgeneric)
            def MethodRestrictions(self, midx):
                """return the restrictions of specified Method"""
                return Multifield(
                    self.__envobject._cl2py(_c.env_getMethodRestrictions(self.__env, midx, self.__defgeneric)))
            def InitialMethod(self):
                """return the index of first Method in this Generic"""
                try:
                    return _c.env_getNextDefmethod(self.__env, 0, self.__defgeneric)
                except:
                    raise _c.ClipsError("M02: could not find any Method")
            def NextMethod(self, midx):
                """return the index of next Method in this Generic"""
                return _c.env_getNextDefmethod(self.__env, midx, self.__defgeneric)
            def PrintMethods(self):
                """print out Method list for this Generic"""
                _c.routerClear("temporary")
                _c.env_listDefmethods(self.__env, "temporary", self.__defgeneric)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            @_accepts_method(None, None, (int, long), None)
            def AddMethod(self, restrictions, actions, midx=None, comment=None):
                """Add a method to this Generic, given restrictions and actions"""
                if comment:
                    cmtstr = '"%s"' % str(comment).replace('"', '\\"')
                else:
                    cmtstr = ""
                if midx:
                    indstr = str(midx)
                else:
                    indstr = ""
                if type(restrictions) in (tuple, list):
                    rstr = ""
                    for x in restrictions:
                        if type(x) not in (tuple, str, unicode):
                            raise TypeError("tuple or string expected as restriction")
                        if type(x) == str:
                            rstr += "(%s)" % x
                        elif type(x) == unicode:
                            rstr += "(%s)" % str(x)
                        else:
                            if len(x) < 2:
                                raise ValueError("tuple must be at least a pair")
                            v1, v2 = str(x[0]), []
                            for y in range(1, len(x)):
                                z = x[y]
                                if z == str:
                                    v2.append("STRING")
                                elif z == ClipsStringType:
                                    v2.append("STRING")
                                elif z == ClipsSymbolType:
                                    v2.append("SYMBOL")
                                elif z == ClipsInstanceNameType:
                                    v2.append("INSTANCE-NAME")
                                elif z == int:
                                    v2.append("INTEGER")
                                elif z == ClipsIntegerType:
                                    v2.append("INTEGER")
                                elif z == float:
                                    v2.append("FLOAT")
                                elif z == ClipsFloatType:
                                    v2.append("FLOAT")
                                elif z == list:
                                    v2.append("MULTIFIELD")
                                elif z == ClipsMultifieldType:
                                    v2.append("MULTIFIELD")
                                elif type(z) == str:
                                    v2.append(z)
                                elif type(z) == unicode:
                                    v2.append(str(z))
                                else:
                                    raise TypeError("unexpected value '%s'" % z)
                                rstr += "(%s %s)" % (v1, " ".join(v2))
                elif type(restrictions) == str:
                    rstr = restrictions
                else:
                    raise TypeError("tuple or string expected as restriction")
                _c.env_build(self.__env, "(defmethod %s %s %s (%s) %s)" % (
                    self.Name, indstr, cmtstr, rstr, actions))
            def RemoveMethod(self, midx):
                """remove specified Method"""
                _c.env_undefmethod(self.__env, midx, self.__defgeneric)
            def WatchMethod(self, midx):
                """activate watch on specified Method"""
                _c.env_setDefmethodWatch(self.__env, True, midx, self.__defgeneric)
            def UnwatchMethod(self, midx):
                """deactivate watch on specified Method"""
                _c.env_setDefmethodWatch(self.__env, False, midx, self.__defgeneric)
            def MethodWatched(self, midx):
                """test whether or not specified Method is being watched"""
                return bool(_c.env_getDefmethodWatch(self.__env, midx, self.__defgeneric))
            def MethodDeletable(self, midx):
                """test whether or not specified Method can be deleted"""
                return bool(_c.env_isDefmethodDeletable(self.__env, midx, self.__defgeneric))
        return Generic
    def Global(self, private_environment):
        environment_object = self
        class Global(object):
            """high-level Global class (represents: defglobal)"""
            __env = private_environment
            __envobject = environment_object
            def __init__(self, o):
                """create a Global object (internal)"""
                if _c.isDefglobal(o):
                    self.__defglobal = o
                else:
                    raise _c.ClipsError("M01: cannot directly create Global")
            def __str__(self):
                """string form of Global"""
                return _c.env_getDefglobalName(self.__env, self.__defglobal)
            def __repr__(self):
                """representation of Global"""
                s = repr(self.__defglobal)[1:-1]
                return "<Global '%s': %s>" % (
                    _c.env_getDefglobalName(self.__env, self.__defglobal), s)
            def __getstate__(self):
                raise _c.ClipsError("M03: cannot pickle objects of type '%s'"
                    % self.__class__.__name__)
            def Next(self):
                """return next Global"""
                o = _c.env_getNextDefglobal(self.__env, self.__defglobal)
                if o:
                    return self.__envobject.Global(o)
                else:
                    return None
            def PPForm(self):
                """return the pretty-print form of Global"""
                return _c.env_getDefglobalPPForm(self.__env, self.__defglobal)
            def ValueForm(self):
                """return a 'printed' form of Global value"""
                return _c.env_getDefglobalValueForm(self.__env, self.__defglobal)
            def Remove(self):
                """remove this Global"""
                _c.env_undefglobal(self.__env, self.__defglobal)
            def __property_getName(self):
                return Symbol(_c.env_getDefglobalName(self.__env, self.__defglobal))
            Name = property(__property_getName, None, None, "retrieve Global name")
            def __property_getModule(self):
                return Symbol(_c.env_defglobalModule(self.__env, self.__defglobal))
            Module = property(__property_getModule, None, None,
                              "retrieve Global Module name")
            def __property_setValue(self, v):
                _c.env_setDefglobalValue(self.__env, self.Name, self.__envobject._py2cl(v))
            def __property_getValue(self):
                return self.__envobject._cl2py(_c.env_getDefglobalValue(self.__env, self.Name))
            Value = property(__property_getValue, __property_setValue,
                             None, "set/retrieve Global value")
            def __property_getDeletable(self):
                return bool(_c.env_isDefglobalDeletable(self.__env, self.__defglobal))
            Deletable = property(__property_getDeletable, None, None,
                                 "verify if this Global can be deleted")
            def __property_setWatch(self, v):
                _c.env_setDefglobalWatch(self.__env, v, self.__defglobal)
            def __property_getWatch(self):
                return _c.env_getDefglobalWatch(self.__env, self.__defglobal)
            Watch = property(__property_getWatch, __property_setWatch,
                             None, "set/retrieve Global debug status")
        return Global
    def Instance(self, private_environment):
        environment_object = self
        class Instance(object):
            """high-level Instance class (represents: instance)"""
            __env = private_environment
            __envobject = environment_object
            def __init__(self, o):
                """create an Instance object (internal)"""
                class __instance_Slots:
                    """access instance Slots"""
                    def __init__(self, io):
                        self.__instance = io
                    @_accepts_method((str, unicode), None)
                    @_forces_method(str, None)
                    def __setitem__(self, name, v):
                        _c.env_directPutSlot(self.__env, self.__instance, name, self.__envobject._py2cl(v))
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def __getitem__(self, name):
                        return self.__envobject._cl2py(_c.env_directGetSlot(self.__env, self.__instance, name))
                    def keys(self):
                        return map(
                            str, list(self.__envobject.Instance(self.__instance).Class.Slots.Names()))
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def has_key(self, k):
                        return bool(
                            k in map(str, list(
                                self.__envobject.Instance(self.__instance).Class.Slots.Names())))
                    def __repr__(self):
                        return "<Instance [%s] Slots>" \
                               % _c.env_getInstanceName(self.__env, self.__instance)
                    def __getstate__(self):
                        raise _c.ClipsError("M03: cannot pickle instance slots")
                if _c.isInstance(o): self.__instance = o
                else:
                    raise _c.ClipsError("M01: cannot directly create Instance")
                self.__Slots = __instance_Slots(self.__instance)
                try:
                    self.__Slots._instance_Slots__env = self.__env
                except AttributeError: pass
                try:
                    self.__Slots._instance_Slots__envobject = self.__envobject
                except AttributeError: pass
            def __str__(self):
                """string form of Instance"""
                return _c.env_getInstanceName(self.__env, self.__instance)
            def __repr__(self):
                """representation of Instance"""
                s = repr(self.__instance)[1:-1]
                return "<Instance [%s]: %s>" % (
                    _c.env_getInstanceName(self.__env, self.__instance), s)
            def __getstate__(self):
                raise _c.ClipsError("M03: cannot pickle objects of type '%s'"
                    % self.__class__.__name__)
            def Next(self):
                """return next Instance"""
                o = _c.env_getNextInstance(self.__env, self.__instance)
                if o:
                    return self.__envobject.Instance(o)
                else:
                    return None
            def PPForm(self):
                """return the pretty-print form of Instance"""
                return _c.env_getInstancePPForm(self.__env, self.__instance)
            def IsValid(self):
                """determine if this Instance is still valid"""
                return bool(_c.env_validInstanceAddress(self.__env, self.__instance))
            def Remove(self):
                """remove this Instance"""
                _c.env_unmakeInstance(self.__env, self.__instance)
            def DirectRemove(self):
                """directly remove this Instance"""
                _c.env_deleteInstance(self.__env, self.__instance)
            @_accepts_method((str, unicode))
            @_forces_method(str)
            def GetSlot(self, slotname):
                """retrieve value of specified Slot"""
                return self.__envobject._cl2py(_c.env_directGetSlot(self.__env, self.__instance, slotname))
            SlotValue = GetSlot
            @_accepts_method((str, unicode), None)
            @_forces_method(str, None)
            def PutSlot(self, slotname, value):
                """set value of specified Slot"""
                _c.env_directPutSlot(self.__env, self.__instance, slotname, self.__envobject._py2cl(value))
            SetSlotValue = PutSlot
            @_accepts_method((str, unicode), None)
            @_forces_method(str, None)
            def Send(self, msg, args=None):
                """send specified message with the given arguments to Instance"""
                if args is not None:
                    t = type(args)
                    if t == str:
                        sargs = args
                    elif t == unicode:
                        sargs = str(args)
                    elif isinstance(args, str):
                        sargs = str(args)
                    elif isinstance(args, unicode):
                        sargs = str(args)
                    elif t in (ClipsIntegerType, ClipsFloatType, ClipsStringType,
                               ClipsSymbolType, ClipsNilType, ClipsInstanceNameType,
                               ClipsMultifieldType):
                        sargs = _py2clsyntax(args)
                    elif t in (tuple, list):
                        li = []
                        for x in args:
                            t1 = type(x)
                            if t1 in (ClipsIntegerType, ClipsFloatType,
                                      ClipsStringType, ClipsSymbolType, ClipsNilType,
                                      ClipsInstanceNameType, ClipsMultifieldType):
                                li.append(_py2clsyntax(x))
                            elif t1 in (int, long):
                                li.append(Integer(int(x)).clsyntax())
                            elif t1 == float:
                                li.append(Float(x).clsyntax())
                            elif t1 in (str, unicode):
                                li.append(String(x).clsyntax())
                            elif isinstance(x, int):
                                li.append(Integer(x).clsyntax())
                            elif isinstance(x, long):
                                li.append(Integer(x).clsyntax())
                            elif isinstance(x, float):
                                li.append(Float(x).clsyntax())
                            elif isinstance(x, str):
                                li.append(String(x).clsyntax())
                            elif isinstance(x, unicode):
                                li.append(String(x).clsyntax())
                            else:
                                li.append(str(x))
                        sargs = " ".join(li)
                    elif t in (int, long):
                        sargs = Integer(args).clsyntax()
                    elif t == float:
                        sargs = Float(args).clsyntax()
                    elif isinstance(args, str):
                        sargs = str(args)
                    elif isinstance(args, unicode):
                        sargs = str(args)
                    elif isinstance(args, int):
                        sargs = Integer(args).clsyntax()
                    elif isinstance(args, long):
                        sargs = Integer(args).clsyntax()
                    elif isinstance(args, float):
                        sargs = Float(args).clsyntax()
                    else:
                        sargs = str(args)
                    return self.__envobject._cl2py(_c.env_send(self.__env, self.__instance, msg, sargs))
                else:
                    return self.__envobject._cl2py(_c.env_send(self.__env, self.__instance, msg))
            def __property_getName(self):
                return InstanceName(_c.env_getInstanceName(self.__env, self.__instance))
            Name = property(__property_getName, None, None, "retrieve Instance name")
            def __property_getClass(self):
                return self.__envobject.Class(_c.env_getInstanceClass(self.__env, self.__instance))
            Class = property(__property_getClass, None, None,
                             "retrieve Instance class")
            def __property_getSlots(self): return self.__Slots
            Slots = property(__property_getSlots, None, None,
                             "Instance Slots information")
        return Instance
    def Module(self, private_environment):
        environment_object = self
        class Module(object):
            """high-level Module class (represents: defmodule)"""
            __env = private_environment
            __envobject = environment_object
            def __init__(self, o):
                """create a Module object (internal)"""
                if _c.isDefmodule(o):
                    self.__defmodule = o
                else: raise _c.ClipsError("M01: cannot directly create Module")
            def __str__(self):
                """string form of Module"""
                return _c.env_getDefmoduleName(self.__env, self.__defmodule)
            def __repr__(self):
                """representation of Module"""
                s = repr(self.__defmodule)[1:-1]
                return "<Module '%s': %s>" % (
                    _c.env_getDefmoduleName(self.__env, self.__defmodule), s)
            def __getstate__(self):
                raise _c.ClipsError("M03: cannot pickle objects of type '%s'"
                      % self.__class__.__name__)
            def Next(self):
                """return next Module"""
                o = _c.env_getNextDefmodule(self.__env, self.__defmodule)
                if(o):
                    return self.__envobject.Module(o)
                else:
                    return None
            def PPForm(self):
                """return the pretty-print form of Module"""
                return _c.env_getDefmodulePPForm(self.__env, self.__defmodule)
            def SetCurrent(self):
                """make this the current Module"""
                _c.env_setCurrentModule(self.__env, self.__defmodule)
            def SetFocus(self):
                """set focus to this Module"""
                _c.env_focus(self.__env, self.__defmodule)
            def __property_getName(self):
                return Symbol(_c.env_getDefmoduleName(self.__env, self.__defmodule))
            Name = property(__property_getName, None, None, "return Module name")
            @_accepts_method((str, unicode), (str, unicode), None)
            @_forces_method(str, str, None)
            def BuildTemplate(self, name, text, comment=None):
                """build a Template object with specified name and body"""
                if comment:
                    cmtstr = '"%s"' % str(comment).replace('"', '\\"')
                else:
                    cmtstr = ""
                mname = self.Name
                construct = "(deftemplate %s::%s %s %s)" % (mname, name, cmtstr, text)
                _c.env_build(self.__env, construct)
                return self.__envobject.Template(_c.env_findDeftemplate(self.__env, "%s::%s" % (mname, name)))
            def TemplateList(self):
                """return list of Template names"""
                o = _c.env_getDeftemplateList(self.__env, self.__defmodule)
                return Multifield(self.__envobject._cl2py(o))
            def PrintTemplates(self):
                """print Templates to standard output"""
                _c.routerClear("temporary")
                _c.env_listDeftemplates(self.__env, "temporary", self.__defmodule)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            def FactList(self):
                """return list of Facts in this Module"""
                o, li = _c.env_getFactList(self.__env, self.__defmodule), []
                if o is not None:
                    for x in o[1]:
                        if x[0] == _c.FACT_ADDRESS:
                            li.append(self.__envobject.Fact(x[1]))
                return li
            @_accepts_method((str, unicode), (str, unicode), None)
            @_forces_method(str, str, None)
            def BuildDeffacts(self, name, text, comment=None):
                """build a Deffacts object with specified name and body"""
                if comment:
                    cmtstr = '"%s"' % str(comment).replace('"', '\\"')
                else:
                    cmtstr = ""
                mname = self.Name
                construct = "(deffacts %s::%s %s %s)" % (mname, name, cmtstr, text)
                _c.env_build(self.__env, construct)
                return self.__envobject.Deffacts(_c.env_findDeffacts(self.__env, "%s::%s" % (mname, name)))
            def DeffactsList(self):
                """return a list of Deffacts names in this Module"""
                o = _c.env_getDeffactsList(self.__env, self.__defmodule)
                return Multifield(self.__envobject._cl2py(o))
            def PrintDeffacts(self):
                """print Deffacts to standard output"""
                _c.routerClear("temporary")
                _c.env_listDeffacts(self.__env, "temporary", self.__defmodule)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            @_accepts_method((str, unicode), (str, unicode), (str, unicode), None)
            @_forces_method(str, str, str, None)
            def BuildRule(self, name, lhs, rhs, comment=None):
                """build a Rule object with specified name and LHS/RHS"""
                if comment:
                    cmtstr = '"%s"' % str(comment).replace('"', '\\"')
                else:
                    cmtstr = ""
                mname = self.Name
                construct = "(defrule %s::%s %s %s => %s)" % (
                    mname, name, cmtstr, lhs, rhs)
                _c.env_build(self.__env, construct)
                return self.__envobject.Rule(_c.env_findDefrule(self.__env, "%s::%s" % (mname, name)))
            def RuleList(self):
                """return a list of Rule names in this Module"""
                o = _c.env_getDefruleList(self.__env, self.__defmodule)
                return Multifield(self.__envobject._cl2py(o))
            def PrintRules(self):
                """print Rules to standard output"""
                _c.routerClear("temporary")
                _c.env_listDefrules(self.__env, "temporary", self.__defmodule)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            def PrintBreakpoints(self):
                """print breakpoints to standard output"""
                _c.routerClear("temporary")
                _c.env_showBreaks(self.__env, "temporary", self.__defmodule)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            def PrintAgenda(self):
                """print Agenda Rules to standard output"""
                _c.routerClear("temporary")
                _c.env_agenda(self.__env, "temporary")
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            def RefreshAgenda(self):
                """refresh Agenda for this Module"""
                _c.env_refreshAgenda(self.__env, self.__defmodule)
            def ReorderAgenda(self):
                """reorder Agenda for this Module"""
                _c.env_reorderAgenda(self.__env, self.__defmodule)
            @_accepts_method((str, unicode), None)
            @_forces_method(str, None)
            def BuildGlobal(self, name, value=Nil):
                """build a Global variable with specified name and value"""
                mname = self.Name
                if type(value)  in (str, ClipsStringType):
                    value = '"%s"' % value
                construct = "(defglobal %s ?*%s* = %s)" % (mname, name, value)
                _c.env_build(self.__env, construct)
                return self.__envobject.Global(_c.env_findDefglobal(self.__env, "%s::%s" % (mname, name)))
            def GlobalList(self):
                """return the list of Global variable names"""
                o = _c.env_getDefglobalList(self.__env, self.__defmodule)
                return Multifield(self.__envobject._cl2py(o))
            def PrintGlobals(self):
                """print list of Global variables to standard output"""
                _c.routerClear("temporary")
                _c.env_listDefglobals(self.__env, "temporary", self.__defmodule)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            def ShowGlobals(self):
                """print list of Global variables and values to standard output"""
                _c.routerClear("temporary")
                _c.env_showDefglobals(self.__env, "temporary", self.__defmodule)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            @_accepts_method((str, unicode), None, (str, unicode), None)
            @_forces_method(str, None, str, None)
            def BuildFunction(self, name, args, text, comment=None):
                """build a Function with specified name, body and arguments"""
                if comment:
                    cmtstr = '"%s"' % str(comment).replace('"', '\\"')
                else:
                    cmtstr = ""
                mname = self.Name
                if type(args) in (tuple, list):
                    args = " ".join(args)
                elif args is None:
                    args = ""
                construct = "(deffunction %s::%s %s (%s) %s)" % (
                    mname, name, cmtstr, args, text)
                _c.env_build(self.__env, construct)
                return self.__envobject.Function(_c.env_findDeffunction(self.__env, "%s::%s" % (mname, name)))
            def FunctionList(self):
                """return the list of Function names"""
                o = _c.env_getDeffunctionList(self.__env, self.__defmodule)
                return Multifield(self.__envobject._cl2py(o))
            def PrintFunctions(self):
                """print list of Functions to standard output"""
                _c.routerClear("temporary")
                _c.env_listDeffunctions(self.__env, "temporary", self.__defmodule)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            @_accepts_method((str, unicode), None)
            @_forces_method(str, None)
            def BuildGeneric(self, name, comment=None):
                """build a Generic with specified name"""
                if comment:
                    cmtstr = '"%s"' % str(comment).replace('"', '\\"')
                else:
                    cmtstr = ""
                mname = self.Name
                construct = "(defgeneric %s::%s %s)" % (mname, name, cmtstr)
                _c.env_build(self.__env, construct)
                return self.__envobject.Generic(_c.env_findDefgeneric(self.__env, "%s::%s" % (mname, name)))
            def GenericList(self):
                """return the list of Generic names"""
                o = _c.env_getDefgenericList(self.__env, self.__defmodule)
                return Multifield(self.__envobject._cl2py(o))
            def PrintGenerics(self):
                """print list of Generics to standard output"""
                _c.routerClear("temporary")
                _c.env_listDefgenerics(self.__env, "temporary", self.__defmodule)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            @_accepts_method((str, unicode), (str, unicode), None)
            @_forces_method(str, str, None)
            def BuildClass(self, name, text, comment=None):
                """build a Class with specified name and body"""
                if comment:
                    cmtstr = '"%s"' % str(comment).replace('"', '\\"')
                else:
                    cmtstr = ""
                mname = self.Name
                construct = "(defclass %s::%s %s %s)" % (mname, name, cmtstr, text)
                _c.env_build(self.__env, construct)
                return self.__envobject.Class(_c.env_findDefclass(self.__env, "%s::%s" % (mname, name)))
            def ClassList(self):
                """return the list of Class names"""
                o = _c.env_getDefclassList(self.__env, self.__defmodule)
                return Multifield(self.__envobject._cl2py(o))
            def PrintClasses(self):
                """print list of Class to standard output"""
                _c.routerClear("temporary")
                _c.env_listDefclasses(self.__env, "temporary", self.__defmodule)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            @_accepts_method((str, unicode), None, None)
            @_forces_method(str, str, None)
            def BuildInstance(self, name, defclass, overrides=""):
                """build an Instance of given Class overriding specified Slots"""
                mname = self.Name
                cmdstr = "(%s::%s of %s %s)" % (mname, name, defclass, overrides)
                return self.__envobject.Instance(_c.env_makeInstance(self.__env, cmdstr))
            @_forces_method(str)
            def PrintInstances(self, classname=None):
                """print Instances to standard output"""
                _c.routerClear("temporary")
                if classname:
                    _c.env_instances(self.__env, "temporary", self.__defmodule, classname, False)
                else:
                    _c.env_instances(self.__env, "temporary", self.__defmodule)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            @_forces_method(str)
            def PrintSubclassInstances(self, classname):
                """print Instances to standard output"""
                _c.routerClear("temporary")
                _c.env_instances(self.__env, "temporary", self.__defmodule, classname, True)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            @_accepts_method((str, unicode), (str, unicode), None)
            @_forces_method(str, str, None)
            def BuildDefinstances(self, name, text, comment=None):
                """build a Definstances with specified name and body"""
                if comment:
                    cmtstr = '"%s"' % str(comment).replace('"', '\\"')
                else:
                    cmtstr = ""
                mname = self.Name
                construct = "(definstances %s::%s %s %s)" % (
                    mname, name, cmtstr, text)
                _c.env_build(self.__env, construct)
                return self.__envobject.Definstances(_c.env_findDefinstances(self.__env, name))
            def DefinstancesList(self):
                """retrieve list of all Definstances names"""
                o = _c.env_getDefinstancesList(self.__env, self.__defmodule)
                return Multifield(self.__envobject._cl2py(o))
            def PrintDefinstances(self):
                """print list of all Definstances to standard output"""
                _c.routerClear("temporary")
                _c.env_listDefinstances(self.__env, "temporary", self.__defmodule)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
        return Module
    def Rule(self, private_environment):
        environment_object = self
        class Rule(object):
            """high-level Rule class (represents: defrule)"""
            __env = private_environment
            __envobject = environment_object
            def __init__(self, o):
                """create a Rule object (internal)"""
                if _c.isDefrule(o):
                    self.__defrule = o
                else:
                    raise _c.ClipsError("M01: cannot directly create Rule")
            def __str__(self):
                """string form of Rule"""
                return _c.env_getDefruleName(self.__env, self.__defrule)
            def __repr__(self):
                """representation of Rule"""
                s = repr(self.__defrule)[1:-1]
                return "<Rule '%s': %s>" % (
                    _c.env_getDefruleName(self.__env, self.__defrule), s)
            def __getstate__(self):
                raise _c.ClipsError("M03: cannot pickle objects of type '%s'"
                    % self.__class__.__name__)
            def Next(self):
                """return next Rule"""
                o = _c.env_getNextDefrule(self.__env, self.__defrule)
                if o:
                    return self.__envobject.Rule(o)
                else:
                    return None
            def PPForm(self):
                """return the pretty-print form of Rule"""
                return _c.env_getDefrulePPForm(self.__env, self.__defrule)
            def Refresh(self):
                """refresh Rule"""
                _c.env_refresh(self.__env, self.__defrule)
            def PrintMatches(self):
                """print partial matches to standard output"""
                _c.routerClear("temporary")
                _c.env_matches(self.__env, "temporary", self.__defrule)
                s = _c.routerRead("temporary")
                if s:
                    _sys.stdout.write(s)
            def Remove(self):
                """remove Rule"""
                _c.env_undefrule(self.__env, self.__defrule)
            def __property_getName(self):
                return Symbol(_c.env_getDefruleName(self.__env, self.__defrule))
            Name = property(__property_getName, None, None, "retrieve Rule name")
            def __property_getModule(self):
                return Symbol(_c.env_defruleModule(self.__env, self.__defrule))
            Module = property(__property_getModule, None, None,
                              "retrieve Rule Module name")
            def __property_setBreak(self, v):
                if v:
                    _c.env_setBreak(self.__env, self.__defrule)
                else:
                    if _c.env_defruleHasBreakpoint(self.__env, self.__defrule):
                        _c.env_removeBreak(self.__env, self.__defrule)
            def __property_getBreak(self):
                return bool(_c.env_defruleHasBreakpoint(self.__env, self.__defrule))
            Breakpoint = property(__property_getBreak, __property_setBreak,
                                  None, "set or remove breakpoint from Rule")
            def __property_getDeletable(self):
                return bool(_c.env_isDefruleDeletable(self.__env, self.__defrule))
            Deletable = property(__property_getDeletable, None, None,
                                 "verify if this Rule can be deleted")
            def __property_setWatchActivations(self, v):
                _c.env_setDefruleWatchActivations(self.__env, self.__defrule, v)
            def __property_getWatchActivations(self):
                return bool(_c.env_getDefruleWatchActivations(self.__env, self.__defrule))
            WatchActivations = property(__property_getWatchActivations,
                                        __property_setWatchActivations,
                                        None, "Rule Activations debug status")
            def __property_setWatchFirings(self, v):
                _c.env_setDefruleWatchFirings(self.__env, self.__defrule, v)
            def __property_getWatchFirings(self):
                return bool(_c.env_getDefruleWatchFirings(self.__env, self.__defrule))
            WatchFirings = property(__property_getWatchFirings,
                                    __property_setWatchFirings,
                                    None, "Rule firings debug status")
        return Rule
    def Template(self, private_environment):
        environment_object = self
        class Template(object):
            """high-level Template class (represents: deftemplate)"""
            __env = private_environment
            __envobject = environment_object
            def __init__(self, o=None):
                """create a Template object (internal)"""
                if _c.isDeftemplate(o):
                    self.__deftemplate = o
                else:
                    raise _c.ClipsError("M01: cannot directly create Template")
                class __template_Slots:
                    """define a structure for Class Slots"""
                    def __init__(self, o):
                        self.__deftemplate = o
                    def __getstate__(self):
                        raise _c.ClipsError("M03: cannot pickle template slots")
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def AllowedValues(self, name):
                        """return allowed values for specified Slot"""
                        rv = self.__envobject._cl2py(
                            _c.env_deftemplateSlotAllowedValues(self.__env, self.__deftemplate, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def Cardinality(self, name):
                        """return cardinality for specified Slot"""
                        rv = self.__envobject._cl2py(
                            _c.env_deftemplateSlotCardinality(self.__env, self.__deftemplate, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def HasDefault(self, name):
                        """one of NO_DEFAULT, STATIC_DEFAULT or DYNAMIC_DEFAULT"""
                        return _c.env_deftemplateSlotDefaultP(self.__env, self.__deftemplate, name)
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def DefaultValue(self, name):
                        """return default value for specified Slot"""
                        rv = self.__envobject._cl2py(
                            _c.env_deftemplateSlotDefaultValue(self.__env, self.__deftemplate, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def Exists(self, name):
                        """return True if specified Slot exists"""
                        return bool(
                            _c.env_deftemplateSlotExistP(self.__env, self.__deftemplate, name))
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def IsMultifield(self, name):
                        """return True if specified Slot is a multifield one"""
                        return bool(
                            _c.env_deftemplateSlotMultiP(self.__env, self.__deftemplate, name))
                    def Names(self):
                        """return the list of Slot names"""
                        rv = self.__envobject._cl2py(_c.env_deftemplateSlotNames(self.__env, self.__deftemplate))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def Range(self, name):
                        """return numeric range information of specified Slot"""
                        rv = self.__envobject._cl2py(_c.env_deftemplateSlotRange(self.__env, self.__deftemplate, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def IsSinglefield(self, name):
                        """return True if specified Slot is a single field one"""
                        return bool(
                            _c.env_deftemplateSlotSingleP(self.__env, self.__deftemplate, name))
                    @_accepts_method((str, unicode))
                    @_forces_method(str)
                    def Types(self, name):
                        """return names of primitive types for specified Slot"""
                        rv = self.__envobject._cl2py(_c.env_deftemplateSlotTypes(self.__env, self.__deftemplate, name))
                        if type(rv) in (tuple, list):
                            return Multifield(rv)
                        else:
                            return rv
                self.__Slots = __template_Slots(self.__deftemplate)
                try:
                    self.__Slots._template_Slots__env = self.__env
                except AttributeError: pass
                try:
                    self.__Slots._template_Slots__envobject = self.__envobject
                except AttributeError: pass
            def __str__(self):
                """string form of Template"""
                return _c.env_getDeftemplateName(self.__env, self.__deftemplate)
            def __repr__(self):
                """representation of Template"""
                s = repr(self.__deftemplate)[1:-1]
                return "<Template '%s': %s>" % (
                    _c.env_getDeftemplateName(self.__env, self.__deftemplate), s)
            def __getstate__(self):
                raise _c.ClipsError(
                    "M03: cannot pickle objects of type '%s'"
                    % self.__class__.__name__)
            def Next(self):
                """return next Template"""
                o = _c.env_getNextDeftemplate(self.__env, self.__deftemplate)
                if(o):
                    return self.__envobject.Template(o)
                else:
                    return None
            def PPForm(self):
                """return the pretty-print form of Template"""
                return _c.env_getDeftemplatePPForm(self.__env, self.__deftemplate)
            def Remove(self):
                """remove Template"""
                _c.env_undeftemplate(self.__env, self.__deftemplate)
            def BuildFact(self):
                """create a fact from this Template without asserting it"""
                return self.__envobject.Fact(self.__deftemplate)
            def InitialFact(self):
                """find initial Fact for this Template"""
                return self.__envobject.Fact(_c.env_getNextFactInTemplate(self.__env, self.__deftemplate))
            def NextFact(self, fact):
                """find initial Fact for this Template"""
                return self.__envobject.Fact(
                    _c.env_getNextFactInTemplate(self.__env, self.__deftemplate, fact._Fact__fact))
            def __property_getDeletable(self):
                return bool(_c.env_isDeftemplateDeletable(self.__env, self.__deftemplate))
            Deletable = property(__property_getDeletable, None, None,
                                 "verify if this Template can be deleted")
            def __property_getName(self):
                return Symbol(_c.env_getDeftemplateName(self.__env, self.__deftemplate))
            Name = property(__property_getName, None, None, "retrieve Template name")
            def __property_getModule(self):
                return Symbol(_c.env_deftemplateModule(self.__env, self.__deftemplate))
            Module = property(__property_getModule, None, None,
                              "retrieve Template Module name")
            def __property_getSlots(self): return self.__Slots
            Slots = property(__property_getSlots, None, None,
                             "Template Slots information")
            def __property_setWatch(self, v):
                _c.env_setDeftemplateWatch(self.__env, v, self.__deftemplate)
            def __property_getWatch(self):
                return _c.env_getDeftemplateWatch(self.__env, self.__deftemplate)
            Watch = property(__property_getWatch, __property_setWatch,
                             None, "watch status of this Template")
        return Template
    def _clips_Debug(self, private_environment):
        environment_object = self
        class _clips_Debug(object):
            """object to enable/disable debugging features"""
            __env = private_environment
            __envobject = environment_object
            __created = False
            def __init__(self):
                """one-time initializer"""
                if(self.__created):
                    raise TypeError("cannot create this object twice")
                self.__created = True
                self.__watchitems = ['facts', 'rules', 'activations', 'compilations',
                                     'statistics', 'globals', 'slots', 'instances',
                                     'messages', 'message-handlers',
                                     'generic-functions', 'methods', 'deffunctions',]
            def __repr__(self):
                return "<Debug Management Object>"
            def __getstate__(self):
                raise _c.ClipsError("M03: cannot pickle debug status")
            def DribbleOn(self, fn):
                """enable dribble on given file"""
                _c.env_dribbleOn(self.__env, fn)
            def DribbleOff(self):
                """turn off dribble"""
                _c.env_dribbleOff(self.__env)
            def DribbleActive(self):
                """tell whether or not dribble is active"""
                return bool(_c.env_dribbleActive(self.__env))
            def __property_setFactsWatched(self, v):
                if(v):
                    _c.env_watch(self.__env, "facts")
                else:
                    _c.env_unwatch(self.__env, "facts")
            def __property_getFactsWatched(self):
                return bool(_c.env_getWatchItem(self.__env, "facts"))
            FactsWatched = property(__property_getFactsWatched,
                                    __property_setFactsWatched,
                                    None, "Facts watch status")
            def __property_setRulesWatched(self, v):
                if(v):
                    _c.env_watch(self.__env, "rules")
                else:
                    _c.env_unwatch(self.__env, "rules")
            def __property_getRulesWatched(self):
                return bool(_c.env_getWatchItem(self.__env, "rules"))
            RulesWatched = property(__property_getRulesWatched,
                                    __property_setRulesWatched,
                                    None, "Rules watch status")
            def __property_setActivationsWatched(self, v):
                if(v):
                    _c.env_watch(self.__env, "activations")
                else:
                    _c.env_unwatch(self.__env, "activations")
            def __property_getActivationsWatched(self):
                return bool(_c.env_getWatchItem(self.__env, "activations"))
            ActivationsWatched = property(__property_getActivationsWatched,
                                          __property_setActivationsWatched,
                                          None, "Activations watch status")
            def __property_setCompilationsWatched(self, v):
                if(v):
                    _c.env_watch(self.__env, "compilations")
                else:
                    _c.env_unwatch(self.__env, "compilations")
            def __property_getCompilationsWatched(self):
                return bool(_c.env_getWatchItem(self.__env, "compilations"))
            CompilationsWatched = property(__property_getCompilationsWatched,
                                           __property_setCompilationsWatched,
                                           None, "compilations watch status")
            def __property_setStatisticsWatched(self, v):
                if(v):
                    _c.env_watch(self.__env, "statistics")
                else:
                    _c.env_unwatch(self.__env, "statistics")
            def __property_getStatisticsWatched(self):
                return bool(_c.env_getWatchItem(self.__env, "statistics"))
            StatisticsWatched = property(__property_getStatisticsWatched,
                                         __property_setStatisticsWatched,
                                         None, "statistics watch status")
            def __property_setGlobalsWatched(self, v):
                if(v):
                    _c.env_watch(self.__env, "globals")
                else:
                    _c.env_unwatch(self.__env, "globals")
            def __property_getGlobalsWatched(self):
                return bool(_c.env_getWatchItem(self.__env, "globals"))
            GlobalsWatched = property(__property_getGlobalsWatched,
                                      __property_setGlobalsWatched,
                                      None, "Globals watch status")
            def __property_setSlotsWatched(self, v):
                if(v):
                    _c.env_watch(self.__env, "slots")
                else:
                    _c.env_unwatch(self.__env, "slots")
            def __property_getSlotsWatched(self):
                return bool(_c.env_getWatchItem(self.__env, "slots"))
            SlotsWatched = property(__property_getSlotsWatched,
                                    __property_setSlotsWatched,
                                    None, "Slots watch status")
            def __property_setMessagesWatched(self, v):
                if(v):
                    _c.env_watch(self.__env, "messages")
                else:
                    _c.env_unwatch(self.__env, "messages")
            def __property_getMessagesWatched(self):
                return bool(_c.env_getWatchItem(self.__env, "messages"))
            MessagesWatched = property(__property_getMessagesWatched,
                                       __property_setMessagesWatched,
                                       None, "messages watch status")
            def __property_setMessageHandlersWatched(self, v):
                if(v):
                    _c.env_watch(self.__env, "message-handlers")
                else:
                    _c.env_unwatch(self.__env, "message-handlers")
            def __property_getMessageHandlersWatched(self):
                return bool(_c.env_getWatchItem(self.__env, "message-handlers"))
            MessageHandlersWatched = property(__property_getMessageHandlersWatched,
                                              __property_setMessageHandlersWatched,
                                              None, "MessageHandlers watch status")
            def __property_setGenericFunctionsWatched(self, v):
                if(v):
                    _c.env_watch(self.__env, "generic-functions")
                else:
                    _c.env_unwatch(self.__env, "generic-functions")
            def __property_getGenericFunctionsWatched(self):
                return bool(_c.env_getWatchItem(self.__env, "generic-functions"))
            GenericFunctionsWatched = property(__property_getGenericFunctionsWatched,
                                               __property_setGenericFunctionsWatched,
                                               None, "Generic functions watch status")
            def __property_setMethodsWatched(self, v):
                if(v):
                    _c.env_watch(self.__env, "methods")
                else:
                    _c.env_unwatch(self.__env, "methods")
            def __property_getMethodsWatched(self):
                return bool(_c.env_getWatchItem(self.__env, "methods"))
            MethodsWatched = property(__property_getMethodsWatched,
                                      __property_setMethodsWatched,
                                      None, "Methods watch status")
            def __property_setFunctionsWatched(self, v):
                if(v):
                    _c.env_watch(self.__env, "deffunctions")
                else:
                    _c.env_unwatch(self.__env, "deffunctions")
            def __property_getFunctionsWatched(self):
                return bool(_c.env_getWatchItem(self.__env, "deffunctions"))
            FunctionsWatched = property(__property_getFunctionsWatched,
                                        __property_setFunctionsWatched,
                                        None, "Deffunctions watch status")
            def __property_setExternalTraceback(self, v):
                _c.setPrintExternalTraceback(bool(v))
            def __property_getExternalTraceback(self):
                return bool(_c.getPrintExternalTraceback())
            ExternalTraceback = property(__property_getExternalTraceback,
                                         __property_setExternalTraceback,
                                         None,
                                         "traceback of Python functions in CLIPS")
            def WatchAll(self):
                """watch all items"""
                for x in self.__watchitems:
                    _c.env_watch(self.__env, x)
            def UnwatchAll(self):
                """unwatch all items"""
                for x in self.__watchitems:
                    _c.env_unwatch(self.__env, x)
        return _clips_Debug
    def _clips_Status(self, private_environment):
        environment_object = self
        class _clips_Status(object):
            """object to access global status functions"""
            __env = private_environment
            __envobject = environment_object
            __created = False
            def __init__(self):
                """raise an exception if an object of this type has been created"""
                if(self.__created):
                    raise TypeError("cannot create this object twice")
                self.__created = True
            def __repr__(self):
                return "<Configuration Management Object>"
            def __getstate__(self):
                raise _c.ClipsError("M03: cannot pickle engine status")
            def __property_setFactDuplication(self, v):
                _c.env_setFactDuplication(self.__env, v)
            def __property_getFactDuplication(self, v):
                return bool(_c.env_getFactDuplication(self.__env))
            FactDuplication = property(
                __property_getFactDuplication,
                __property_setFactDuplication,
                None, "Fact duplication behaviour")
            def __property_setAutoFloatDividend(self, v):
                _c.env_setAutoFloatDividend(self.__env, v)
            def __property_getAutoFloatDividend(self):
                return bool(_c.env_getAutoFloatDividend(self.__env))
            AutoFloatDividend = property(
                __property_getAutoFloatDividend,
                __property_setAutoFloatDividend,
                None, "AutoFloatDividend behaviour")
            def __property_setDynamicConstraintChecking(self, v):
                _c.env_setDynamicConstraintChecking(self.__env, v)
            def __property_getDynamicConstraintChecking(self):
                return bool(_c.env_getDynamicConstraintChecking(self.__env))
            DynamicConstraintChecking = property(
                __property_getDynamicConstraintChecking,
                __property_setDynamicConstraintChecking,
                None, "Dynamic constraint checking behaviour")
            def __property_setSequenceOperatorRecognition(self, v):
                _c.env_setSequenceOperatorRecognition(self.__env, v)
            def __property_getSequenceOperatorRecognition(self):
                return bool(_c.env_getSequenceOperatorRecognition(self.__env))
            SequenceOperatorRecognition = property(
                __property_getSequenceOperatorRecognition,
                __property_setSequenceOperatorRecognition,
                None, "Sequence operator recognition behaviour")
            def __property_setStaticConstraintChecking(self, v):
                _c.env_setStaticConstraintChecking(self.__env, v)
            def __property_getStaticConstraintChecking(self):
                return bool(_c.env_getStaticConstraintChecking(self.__env))
            StaticConstraintChecking = property(
                __property_getStaticConstraintChecking,
                __property_setStaticConstraintChecking,
                None, "Static constraint checking behaviour")
            def __property_setIncrementalReset(self, v):
                _c.env_setIncrementalReset(self.__env, v)
            def __property_getIncrementalReset(self):
                return bool(_c.env_getIncrementalReset(self.__env))
            IncrementalReset = property(
                __property_getIncrementalReset,
                __property_setIncrementalReset,
                None, "Incremental reset behaviour")
            def __property_setResetGlobals(self, v):
                _c.env_setResetGlobals(self.__env, v)
            def __property_getResetGlobals(self):
                return bool(_c.env_getResetGlobals(self.__env))
            ResetGlobals = property(
                __property_getResetGlobals,
                __property_setResetGlobals,
                None, "ResetGlobals behaviour")
            def __property_setStrategy(self, v):
                _c.env_setStrategy(self.__env, v)
            def __property_getStrategy(self):
                return _c.env_getStrategy(self.__env)
            Strategy = property(
                __property_getStrategy,
                __property_setStrategy,
                None, "strategy behaviour")
            def __property_setSalienceEvaluation(self, v):
                _c.env_setSalienceEvaluation(self.__env, v)
            def __property_getSalienceEvaluation(self):
                return _c.env_getSalienceEvaluation(self.__env)
            SalienceEvaluation = property(
                __property_getSalienceEvaluation,
                __property_setSalienceEvaluation,
                None, "salience evaluation behaviour")
            def __property_setClassDefaultsMode(self, v):
                _c.env_setClassDefaultsMode(self.__env, v)
            def __property_getClassDefaultsMode(self):
                return _c.env_getClassDefaultsMode(self.__env)
            ClassDefaultsMode = property(
                __property_getClassDefaultsMode,
                __property_setClassDefaultsMode,
                None, "class defaults mode")
        return _clips_Status


    # constructor possibly sets the "borrowed" flag, to state that this
    #  is a Python class around an existing object: in this case the
    #  underlying CLIPS environment is not attempted to be destroyed on
    #  deletion
    def __init__(self, o=None):
        """environment constructor"""
        if o is None:
            self.__env = _c.createEnvironment()
            self.__borrowed = False
        else:
            if _c.isEnvironment(o):
                self.__env = o
                self.__borrowed = True
            else:
                raise TypeError("invalid argument for constructor")
        self.Activation = self.Activation(self.__env)
        self.Class = self.Class(self.__env)
        self.Deffacts = self.Deffacts(self.__env)
        self.Definstances = self.Definstances(self.__env)
        self.Fact = self.Fact(self.__env)
        self.Function = self.Function(self.__env)
        self.Generic = self.Generic(self.__env)
        self.Global = self.Global(self.__env)
        self.Instance = self.Instance(self.__env)
        self.Module = self.Module(self.__env)
        self.Rule = self.Rule(self.__env)
        self.Template = self.Template(self.__env)
        self._clips_Debug = self._clips_Debug(self.__env)
        self._clips_Status = self._clips_Status(self.__env)

        # if o is not None, then this is an internal object and its status
        #  should not be modified by the user, nor the stock objects be
        #  accessible for direct inspection or subclassing (as this could
        #  be the current environment and might be corrupted)
        if o is None:
            self.EngineConfig = self._clips_Status()
            self.DebugConfig = self._clips_Debug()

    def AgendaChanged(self):
        """test whether or not Agenda is changed since last call"""
        rv = bool(_c.env_getAgendaChanged(self.__env))
        _c.env_setAgendaChanged(self.__env, False)
        return rv
    def Assert(self, o):
        """assert a Fact from a string or constructed Fact object"""
        if '_Fact__fact' in dir(o) and _c.isFact(o._Fact__fact):
            return o.Assert()
        elif type(o) in (str, unicode):
            return self.Fact(_c.env_assertString(self.__env, str(o)))
        else:
            raise TypeError("expected a string or a Fact")
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def BLoad(self, filename):
        """binary load the constructs from a file"""
        _c.env_bload(self.__env, _os.path.normpath(filename))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def BLoadInstances(self, filename):
        """load Instances from binary file"""
        _c.env_binaryLoadInstances(self.__env, _os.path.normpath(filename))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def BSave(self, filename):
        """binary save constructs to a file"""
        _c.env_bsave(self.__env, _os.path.normpath(filename))
    @_accepts_method((str, unicode), None)
    @_forces_method(str, None)
    def BSaveInstances(self, filename, mode=LOCAL_SAVE):
        """save Instances to binary file"""
        _c.env_binarySaveInstances(self.__env, _os.path.normpath(filename), mode)
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def BatchStar(self, filename):
        """execute commands stored in file"""
        _c.env_batchStar(self.__env, _os.path.normpath(filename))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def BrowseClasses(self, classname):
        """print list of Classes that inherit from specified one"""
        _c.routerClear("temporary")
        defclass = _c.env_findDefclass(self.__env, str(classname))
        _c.env_browseClasses(self.__env, "temporary", defclass)
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def Build(self, construct):
        """build construct given in argument"""
        _c.env_build(self.__env, construct)
    @_accepts_method((str, unicode), (str, unicode), None)
    @_forces_method(str, str, None)
    def BuildClass(self, name, text, comment=None):
        """build a Class with specified name and body"""
        if comment:
            cmtstr = '"%s"' % str(comment).replace('"', '\\"')
        else:
            cmtstr = ""
        construct = "(defclass %s %s %s)" % (name, cmtstr, text)
        _c.env_build(self.__env, construct)
        return self.Class(_c.env_findDefclass(self.__env, name))
    @_accepts_method((str, unicode), (str, unicode), None)
    @_forces_method(str, str, None)
    def BuildDeffacts(self, name, text, comment=None):
        """build a Deffacts object with specified name and body"""
        if comment:
            cmtstr = '"%s"' % str(comment).replace('"', '\\"')
        else:
            cmtstr = ""
        construct = "(deffacts %s %s %s)" % (name, cmtstr, text)
        _c.env_build(self.__env, construct)
        return self.Deffacts(_c.env_findDeffacts(self.__env, name))
    @_accepts_method((str, unicode), (str, unicode), None)
    @_forces_method(str, str, None)
    def BuildDefinstances(self, name, text, comment=None):
        """build a Definstances with specified name and body"""
        if comment:
            cmtstr = '"%s"' % str(comment).replace('"', '\\"')
        else:
            cmtstr = ""
        construct = "(definstances %s %s %s)" % (name, cmtstr, text)
        _c.env_build(self.__env, construct)
        return self.Definstances(_c.env_findDefinstances(self.__env, name))
    @_accepts_method((str, unicode), None, (str, unicode), None)
    @_forces_method(str, None, str, None)
    def BuildFunction(self, name, args, text, comment=None):
        """build a Function with specified name, body and arguments"""
        if comment:
            cmtstr = '"%s"' % str(comment).replace('"', '\\"')
        else:
            cmtstr = ""
        if type(args) in (tuple, list):
            args = " ".join(args)
        elif args is None:
            args = ""
        construct = "(deffunction %s %s (%s) %s)" % (name, cmtstr, args, text)
        _c.env_build(self.__env, construct)
        return self.Function(_c.env_findDeffunction(self.__env, name))
    @_accepts_method((str, unicode), None)
    @_forces_method(str, None)
    def BuildGeneric(self, name, comment=None):
        """build a Generic with specified name and body"""
        if comment:
            cmtstr = '"%s"' % str(comment).replace('"', '\\"')
        else:
            cmtstr = ""
        construct = "(defgeneric %s %s)" % (name, cmtstr)
        _c.env_build(self.__env, construct)
        return self.Generic(_c.env_findDefgeneric(self.__env, name))
    @_accepts_method((str, unicode), None)
    @_forces_method(str, None)
    def BuildGlobal(self, name, value=Nil):
        """build a Global variable with specified name and body"""
        if type(value) in (str, unicode, ClipsStringType):
            value = '"%s"' % str(value)
        construct = "(defglobal ?*%s* = %s)" % (name, value)
        _c.env_build(self.__env, construct)
        return self.Global(_c.env_findDefglobal(self.__env, "%s" % name))
    @_accepts_method((str, unicode), None, (str, unicode))
    @_forces_method(str, str, str)
    def BuildInstance(self, name, defclass, overrides=""):
        """build an Instance of given class overriding specified slots"""
        cmdstr = "(%s of %s %s)" % (name, str(defclass), overrides)
        return self.Instance(_c.env_makeInstance(self.__env, cmdstr))
    @_accepts_method((str, unicode), None, None, (str, unicode), None, None)
    @_forces_method(str, str, None, str, None, None)
    def BuildMessageHandler(self, name, hclass, args, text, htype=PRIMARY, comment=None):
        """build a MessageHandler for specified class with arguments and body"""
        if comment:
            cmtstr = '"%s"' % str(comment).replace('"', '\\"')
        else: cmtstr = ""
        htype = htype.lower()
        if not htype in (AROUND, BEFORE, PRIMARY, AFTER):
            raise ValueError("htype must be in AROUND, BEFORE, PRIMARY, AFTER")
        if type(args) in (tuple, list):
            sargs = " ".join(args)
        elif args is None:
            sargs = ""
        else:
            sargs = str(args)
        construct = "(defmessage-handler %s %s %s %s (%s) %s)" % (
            hclass, name, htype, cmtstr, sargs, text)
        _c.env_build(self.__env, construct)
        defclass = _c.env_findDefclass(self.__env, hclass)
        return _c.env_findDefmessageHandler(self.__env, defclass, name, htype)
    @_accepts_method((str, unicode), (str, unicode), None)
    @_forces_method(str, str, None)
    def BuildModule(self, name, text="", comment=None):
        """build a Module with specified name and body"""
        if comment:
            cmtstr = '"%s"' % str(comment).replace('"', '\\"')
        else:
            cmtstr = ""
        construct = "(defmodule %s %s %s)" % (name, cmtstr, text)
        _c.env_build(self.__env, construct)
        return self.Module(_c.env_findDefmodule(self.__env, name))
    @_accepts_method((str, unicode), (str, unicode), (str, unicode), None)
    @_forces_method(str, str, str, None)
    def BuildRule(self, name, lhs, rhs, comment=None):
        """build a Rule object with specified name and body"""
        if comment:
            cmtstr = '"%s"' % str(comment).replace('"', '\\"')
        else:
            cmtstr = ""
        construct = "(defrule %s %s %s => %s)" % (name, cmtstr, lhs, rhs)
        _c.env_build(self.__env, construct)
        return self.Rule(_c.env_findDefrule(self.__env, name))
    @_accepts_method((str, unicode), (str, unicode), None)
    @_forces_method(str, str, None)
    def BuildTemplate(self, name, text, comment=None):
        """build a Template object with specified name and body"""
        if comment:
            cmtstr = '"%s"' % str(comment).replace('"', '\\"')
        else:
            cmtstr = ""
        construct = "(deftemplate %s %s %s)" % (name, cmtstr, text)
        _c.env_build(self.__env, construct)
        return self.Template(_c.env_findDeftemplate(self.__env, name))
    @_accepts_method((str, unicode), None)
    @_forces_method(str, None)
    def Call(self, func, args=None):
        """call a function with the given argument string or tuple"""
        if args is not None:
            t = type(args)
            if t == str:
                sargs = args
            if t == unicode:
                sargs = str(args)
            elif t in (ClipsIntegerType, ClipsFloatType, ClipsStringType,
                       ClipsSymbolType, ClipsNilType, ClipsInstanceNameType,
                       ClipsMultifieldType):
                sargs = _py2clsyntax(args)
            elif isinstance(args, str):
                sargs = str(args)
            elif isinstance(args, unicode):
                sargs = str(args)
            elif t in (tuple, list):
                li = []
                for x in args:
                    t1 = type(x)
                    if t1 in (ClipsIntegerType, ClipsFloatType, ClipsStringType,
                              ClipsSymbolType, ClipsNilType,
                              ClipsInstanceNameType, ClipsMultifieldType):
                        li.append(_py2clsyntax(x))
                    elif t1 in (int, long):
                        li.append(Integer(int(x)).clsyntax())
                    elif t1 == float:
                        li.append(Float(x).clsyntax())
                    elif t1 in (str, unicode):
                        li.append(String(x).clsyntax())
                    elif isinstance(x, int):
                        li.append(Integer(x).clsyntax())
                    elif isinstance(x, long):
                        li.append(Integer(x).clsyntax())
                    elif isinstance(x, float):
                        li.append(Float(x).clsyntax())
                    elif isinstance(x, str):
                        li.append(String(x).clsyntax())
                    elif isinstance(x, unicode):
                        li.append(String(x).clsyntax())
                    else:
                        li.append(str(x))
                sargs = " ".join(li)
            elif t in (int, long):
                sargs = Integer(int(args)).clsyntax()
            elif t == float:
                sargs = Float(args).clsyntax()
            elif isinstance(args, int):
                sargs = Integer(args).clsyntax()
            elif isinstance(args, long):
                sargs = Integer(args).clsyntax()
            elif isinstance(args, float):
                sargs = Float(args).clsyntax()
            else:
                sargs = str(args)
            return self._cl2py(_c.env_functionCall(self.__env, func, sargs))
        else:
            return self._cl2py(_c.env_functionCall(self.__env, func))
    def ClassList(self):
        """return the list of Class names"""
        o = _c.env_getDefclassList(self.__env)
        return Multifield(self._cl2py(o))
    def Clear(self):
        """clear Environment"""
        _c.env_clear(self.__env)
        if not 'self' in locals().keys():
            _setStockClasses()
    def ClearFocusStack(self):
        """clear focus stack"""
        _c.env_clearFocusStack(self.__env)
    def CurrentModule(self):
        """return current Module"""
        return self.Module(_c.env_getCurrentModule(self.__env))
    def DeffactsList(self):
        """return a list of Deffacts names in current module"""
        o = _c.env_getDeffactsList(self.__env)
        return Multifield(self._cl2py(o))
    def DefinstancesList(self):
        """retrieve list of all Definstances names"""
        o = _c.env_getDefinstancesList(self.__env)
        return Multifield(self._cl2py(o))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def Eval(self, expr):
        """evaluate expression passed as argument"""
        return self._cl2py(_c.env_eval(self.__env, expr))
    def FactList(self):
        """return list of Facts in current module"""
        o, li = _c.env_getFactList(self.__env), []
        if o is not None:
            for x in o[1]:
                if x[0] == _c.FACT_ADDRESS:
                    li.append(self.Fact(x[1]))
        return li
    def FactListChanged(self):
        """test whether Fact list is changed since last call"""
        rv = bool(_c.env_getFactListChanged(self.__env))
        _c.env_setFactListChanged(self.__env, False)
        return rv
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def FindClass(self, name):
        """find a Class by name"""
        return self.Class(_c.env_findDefclass(self.__env, name))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def FindDeffacts(self, s):
        """find a Deffacts by name"""
        try:
            return self.Deffacts(_c.env_findDeffacts(self.__env, s))
        except:
            raise _c.ClipsError("M02: could not find Deffacts '%s'" % s)
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def FindDefinstances(self, name):
        """find Definstances by name"""
        return self.Definstances(_c.env_findDefinstances(self.__env, name))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def FindFunction(self, name):
        """find a Function by name"""
        return self.Function(_c.env_findDeffunction(self.__env, name))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def FindGeneric(self, name):
        """find a Generic by name"""
        return self.Generic(_c.env_findDefgeneric(self.__env, name))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def FindGlobal(self, name):
        """find a Global variable by name"""
        return self.Global(_c.env_findDefglobal(self.__env, name))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def FindInstance(self, name):
        """find an Instance in all modules (including imported)"""
        return self.Instance(_c.env_findInstance(self.__env, name, True))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def FindInstanceLocal(self, name):
        """find an Instance in non imported modules"""
        return self.Instance(_c.env_findInstance(self.__env, name, False))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def FindModule(self, name):
        """find a Module by name"""
        return self.Module(_c.env_findDefmodule(self.__env, name))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def FindRule(self, s):
        """find a Rule by name"""
        try:
            return self.Rule(_c.env_findDefrule(self.__env, s))
        except:
            raise _c.ClipsError("M02: could not find defrule '%s'" % s)
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def FindTemplate(self, s):
        """find a Template by name"""
        return self.Template(_c.env_findDeftemplate(self.__env, s))
    def FocusStack(self):
        """return list of Module names in focus stack"""
        return self._cl2py(_c.env_getFocusStack(self.__env))
    def FunctionList(self):
        """return the list of Function names"""
        o = _c.env_getDeffunctionList(self.__env)
        return Multifield(self._cl2py(o))
    def GenericList(self):
        """return the list of Generic names"""
        o = _c.env_getDefgenericList(self.__env)
        return Multifield(self._cl2py(o))
    def GlobalList(self):
        """return the list of Global variable names"""
        o = _c.env_getDefglobalList(self.__env)
        return Multifield(self._cl2py(o))
    def GlobalsChanged(self):
        """test whether or not Global variables have changed since last call"""
        rv = bool(_c.env_getGlobalsChanged(self.__env))
        _c.env_setGlobalsChanged(self.__env, False)
        return rv
    def InitialActivation(self):
        """return first Activation object"""
        try:
            return self.Activation(_c.env_getNextActivation(self.__env))
        except:
            raise _c.ClipsError("M02: could not find any Activation")
    def InitialClass(self):
        """retrieve first Class"""
        try:
            return self.Class(_c.env_getNextDefclass(self.__env))
        except:
            raise _c.ClipsError("M02: could not find any Class")
    def InitialDeffacts(self):
        """return first Deffacts"""
        try:
            return self.Deffacts(_c.env_getNextDeffacts(self.__env))
        except:
            raise _c.ClipsError("M02: could not find any Deffacts")
    def InitialDefinstances(self):
        """retrieve first Definstances"""
        try:
            return self.Definstances(_c.env_getNextDefinstances(self.__env))
        except:
            raise _c.ClipsError("M02: could not find any Definstances")
    def InitialFact(self):
        """return first Fact in environment"""
        try:
            return self.Fact(_c.env_getNextFact(self.__env))
        except:
            raise _c.ClipsError("M02: could not find any Fact")
    def InitialFunction(self):
        """return first Function"""
        try:
            return self.Function(_c.env_getNextDeffunction(self.__env))
        except:
            raise _c.ClipsError("M02: could not find any Function")
    def InitialGeneric(self):
        """return first Generic"""
        try:
            return self.Generic(_c.env_getNextDefgeneric(self.__env))
        except:
            raise _c.ClipsError("M02: could not find any Generic")
    def InitialGlobal(self):
        """return first Global variable"""
        try:
            return self.Global(_c.env_getNextDefglobal(self.__env))
        except:
            raise _c.ClipsError("M02: could not find any Global")
    def InitialInstance(self):
        """retrieve first Instance"""
        try:
            return self.Instance(_c.env_getNextInstance(self.__env))
        except:
            raise _c.ClipsError("M02: could not find any Instance")
    def InitialModule(self):
        """return first Module"""
        try:
            return self.Module(_c.env_getNextDefmodule(self.__env))
        except:
            raise _c.ClipsError("M02: could not find any Module")
    def InitialRule(self):
        """return first Rule"""
        try:
            return self.Rule(_c.env_getNextDefrule(self.__env))
        except:
            raise _c.ClipsError("M02: could not find any Rule")
    def InitialTemplate(self):
        """return first Template in environment"""
        try:
            return self.Template(_c.env_getNextDeftemplate(self.__env))
        except:
            raise _c.ClipsError("M02: could not find any Template")
    def InstancesChanged(self):
        """test if Instances have changed since last call"""
        rv = bool(_c.env_getInstancesChanged(self.__env))
        _c.env_setInstancesChanged(self.__env, False)
        return rv
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def Load(self, filename):
        """load constructs from a file"""
        _c.env_load(self.__env, _os.path.normpath(filename))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def LoadFacts(self, filename):
        """load Facts from file"""
        _c.env_loadFacts(self.__env, _os.path.normpath(filename))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def LoadFactsFromString(self, s):
        """load Fact objects from a string"""
        _c.env_loadFactsFromString(self.__env, s)
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def LoadInstances(self, filename):
        """load Instances from file"""
        _c.env_loadInstances(self.__env, _os.path.normpath(filename))
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def LoadInstancesFromString(self, s):
        """load Instances from the specified string"""
        _c.env_loadInstancesFromString(self.__env, s)
    def MessageHandlerList(self):
        """return list of MessageHandler constructs"""
        o = _c.env_getDefmessageHandlerList(self.__env)
        li, rv = Multifield(self._cl2py(o)), []
        l = len(li) / 3
        for x in range(0, l):
            rv.append(Multifield([li[x * 3], li[x * 3 + 1], li[x * 3 + 2]]))
        return Multifield(rv)
    def MethodList(self):
        """return the list of all Methods"""
        o = self._cl2py(_c.env_getDefmethodList(self.__env))
        li = Multifield([])
        l = len(o) / 2
        for x in range(l):
            li.append(Multifield([o[2 * x], o[2 * x + 1]]))
        return li
    def ModuleList(self):
        """return the list of Module names"""
        o = _c.env_getDefmoduleList(self.__env)
        return Multifield(self._cl2py(o))
    def PopFocus(self):
        """pop focus"""
        _c.env_popFocus(self.__env)
    def PrintAgenda(self):
        """print Agenda Rules to standard output"""
        _c.routerClear("temporary")
        _c.env_agenda(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintBreakpoints(self):
        """print breakpoints to standard output"""
        _c.routerClear("temporary")
        _c.env_showBreaks(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintClasses(self):
        """print list of Classes to standard output"""
        _c.routerClear("temporary")
        _c.env_listDefclasses(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintDeffacts(self):
        """print Deffacts to standard output"""
        _c.routerClear("temporary")
        _c.env_listDeffacts(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintDefinstances(self):
        """print list of all Definstances to standard output"""
        _c.routerClear("temporary")
        _c.env_listDefinstances(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintFacts(self):
        """print Facts to standard output"""
        _c.routerClear("temporary")
        _c.env_facts(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintFocusStack(self):
        """print focus stack to standard output"""
        _c.routerClear("temporary")
        _c.env_listFocusStack(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintFunctions(self):
        """print list of Functions to standard output"""
        _c.routerClear("temporary")
        _c.env_listDeffunctions(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintGenerics(self):
        """print list of Generics to standard output"""
        _c.routerClear("temporary")
        _c.env_listDefgenerics(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintGlobals(self):
        """print list of Global variables to standard output"""
        _c.routerClear("temporary")
        _c.env_listDefglobals(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    @_forces_method(str)
    def PrintInstances(self, classname=None):
        """print Instances to standard output"""
        _c.routerClear("temporary")
        if classname:
            _c.env_instances(self.__env, "temporary", classname, False)
        else:
            _c.env_instances(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintMessageHandlers(self):
        """print list of all MessageHandlers"""
        _c.routerClear("temporary")
        _c.env_listDefmessageHandlers(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintModules(self):
        """print list of Modules to standard output"""
        _c.routerClear("temporary")
        _c.env_listDefmodules(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintRules(self):
        """print Rules to standard output"""
        _c.routerClear("temporary")
        _c.env_listDefrules(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    @_forces_method(str)
    def PrintSubclassInstances(self, classname):
        """print subclass Instances to standard output"""
        _c.routerClear("temporary")
        if classname:
            _c.env_instances(self.__env, "temporary", classname, True)
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def PrintTemplates(self):
        """print Templates to standard output"""
        _c.routerClear("temporary")
        _c.env_listDeftemplates(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def RefreshAgenda(self):
        """refresh Agenda Rules for current Module"""
        _c.env_refreshAgenda(self.__env)
    def ReorderAgenda(self):
        """reorder Agenda Rules for current Module"""
        _c.env_reorderAgenda(self.__env)
    def Reset(self):
        """reset Environment"""
        _c.env_reset(self.__env)
    def Clear(self):
        """clear Environment"""
        _c.env_clear(self.__env)
        if not 'self' in locals().keys():
            _setStockClasses()
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def RestoreInstancesFromString(self, s):
        """restore Instances from the specified string"""
        _c.env_restoreInstancesFromString(self.__env, s)
    def RuleList(self):
        """return a list of Rule names in current module"""
        o = _c.env_getDefruleList(self.__env)
        return Multifield(self._cl2py(o))
    def Run(self, limit=None):
        """execute Rules up to limit (if any)"""
        if limit is None:
            return _c.env_run(self.__env)
        else:
            return _c.env_run(self.__env, limit)
    @_accepts_method((str, unicode))
    @_forces_method(str)
    def Save(self, filename):
        """save constructs to a file"""
        _c.env_save(self.__env, _os.path.normpath(filename))
    @_accepts_method((str, unicode), (str, unicode))
    @_forces_method(str, str)
    def SaveFacts(self, filename, mode=LOCAL_SAVE):
        """save current Facts to file"""
        _c.env_saveFacts(self.__env, _os.path.normpath(filename), mode)
    @_accepts_method((str, unicode), None)
    @_forces_method(str, None)
    def SaveInstances(self, filename, mode=LOCAL_SAVE):
        """save Instances to file"""
        _c.env_saveInstances(self.__env, _os.path.normpath(filename), mode)
    @_accepts_method((str, unicode), None)
    @_forces_method(str, None)
    def SendCommand(self, command, verbose=False):
        """send a command to the engine as if typed at the CLIPS prompt"""
        _c.env_sendCommand(self.__env, command, verbose)
    def ShowGlobals(self):
        """print list of Global variables and values to standard output"""
        _c.routerClear("temporary")
        _c.env_showDefglobals(self.__env, "temporary")
        s = _c.routerRead("temporary")
        if s:
            _sys.stdout.write(s)
    def TemplateList(self):
        """return a list of Template names"""
        o = _c.env_getDeftemplateList(self.__env)
        return Multifield(self._cl2py(o))    # should be all strings
    def _cl2py(self, o):
        """convert a well-formed tuple to one of the CLIPS wrappers"""
        if o is None: return None
        elif type(o) == tuple and len(o) == 2:
            if o[0] == _c.INTEGER:
                return Integer(o[1])
            elif o[0] == _c.FLOAT:
                return Float(o[1])
            elif o[0] == _c.STRING:
                return String(o[1])
            elif o[0] == _c.INSTANCE_NAME:
                return InstanceName(o[1])
            elif o[0] == _c.SYMBOL:
                if o[1] == "nil":
                    return Nil
                else:
                    return Symbol(o[1])
            elif o[0] == _c.INSTANCE_ADDRESS:
                return self.Instance(o[1])
            elif o[0] == _c.FACT_ADDRESS:
                return self.Fact(o[1])
            elif o[0] == _c.MULTIFIELD:
                li = []
                for (x, v) in o[1]:
                    if x == _c.INTEGER:
                        li.append(Integer(v))
                    elif x == _c.FLOAT:
                        li.append(Float(v))
                    elif x == _c.STRING:
                        li.append(String(v))
                    elif x == _c.SYMBOL:
                        li.append(Symbol(v))
                    elif x == _c.INSTANCE_NAME:
                        li.append(InstanceName(v))
                    elif x == _c.INSTANCE_ADDRESS:
                        li.append(self.Instance(v))
                    elif x == _c.FACT_ADDRESS:
                        li.append(self.Fact(v))
                    else:
                        raise TypeError("list cannot be converted")
                return Multifield(li)
            else:
                raise TypeError("malformed tuple value")
        else:
            raise TypeError("wrong argument type")
    def _py2cl(self, o):
        """convert Python data to a well-formed tuple"""
        t1 = type(o)
        if t1 in (int, long):
            return (_c.INTEGER, int(o))
        elif t1 == float:
            return (_c.FLOAT, float(o))
        elif t1 in (str, unicode):
            return (_c.STRING, str(o))
        elif t1 in (ClipsIntegerType, ClipsFloatType, ClipsStringType,
                    ClipsSymbolType, ClipsInstanceNameType, ClipsNilType,
                    ClipsMultifieldType):
            return o.clrepr()
        elif t1 == self.Fact:
            return (_c.FACT_ADDRESS, o._Fact__fact)
        elif t1 == self.Instance:
            return (_c.INSTANCE_ADDRESS, o._Instance__instance)
        elif isinstance(o, int):
            return (_c.INTEGER, int(o))
        elif isinstance(o, long):
            return (_c.INTEGER, int(o))
        elif isinstance(o, float):
            return (_c.FLOAT, float(o))
        elif isinstance(o, str):
            return (_c.STRING, str(o))
        elif isinstance(o, unicode):
            return (_c.STRING, str(o))
        elif t1 in (list, tuple):
            li = []
            for x in o:
                t0 = type(x)
                if t0 in (int, long):
                    li.append((_c.INTEGER, int(x)))
                elif t0 == float:
                    li.append((_c.FLOAT, float(x)))
                elif t0 in (str, unicode):
                    li.append((_c.STRING, str(x)))
                elif t0 in (ClipsIntegerType, ClipsFloatType, ClipsStringType,
                            ClipsSymbolType, ClipsInstanceNameType, ClipsNilType):
                    li.append(x.clrepr())
                elif t0 == self.Fact:
                    li.append((_c.FACT_ADDRESS, o._Fact__fact))
                elif t0 == self.Instance:
                    li.append((_c.INSTANCE_ADDRESS, o._Instance__instance))
                elif isinstance(x, int):
                    li.append((_c.INTEGER, int(o)))
                elif isinstance(x, long):
                    li.append((_c.INTEGER, int(o)))
                elif isinstance(x, float):
                    li.append((_c.FLOAT, float(o)))
                elif isinstance(x, str):
                    li.append((_c.STRING, str(o)))
                elif isinstance(x, unicode):
                    li.append((_c.STRING, str(o)))
                else:
                    raise TypeError(
                        "list element of type %s cannot be converted" % t0)
            return (_c.MULTIFIELD, li)
        else:
            raise TypeError("value of type %s cannot be converted" % t1)


    def __del__(self):
        """environment destructor"""
        if not self.__borrowed:
            try:
                _c.destroyEnvironment(self.__env)
            except ClipsError:
                pass

    def __repr__(self):
        """representation of environment, borrowed by underlying object"""
        return "<Environment: " + repr(self.__env)[1:-1] + ">"

    def __property_getIndex(self):
        return _c.getEnvironmentIndex(self.__env)
    Index = property(__property_getIndex, None, None,
                     "Return index of this Environment")

    def SetCurrent(self):
        """Make this Environment the current Environment"""
        _c.setCurrentEnvironment(self.__env)
        _setStockClasses()



# A function that returns current Environment
def CurrentEnvironment():
    """Return current Environment"""
    cenv = _c.getCurrentEnvironment()
    env = Environment(cenv)
    env.EngineConfig = env._clips_Status()
    env.DebugConfig = env._clips_Debug()
    return env



# end.