1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!-- $Id: tutorial.html,v 1.8 2001/03/09 13:21:23 neumann Exp $ -->
<HTML>
<HEAD>
<TITLE>XOTcl - Tutorial</TITLE>
<META NAME="GENERATOR" CONTENT="StarOffice/5.1 (Linux)">
<META NAME="CREATED" CONTENT="19991111;20552300">
<META NAME="CHANGEDBY" CONTENT="Gustaf ">
<META NAME="CHANGED" CONTENT="19991111;22461500">
</HEAD>
<BODY BGCOLOR="#ffffff">
<TABLE COLS=2 WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#000055" STYLE="page-break-before: always">
<TR>
<TD WIDTH=75%>
<P><FONT COLOR="#ffffff"><FONT FACE="Arial, Helvetica"><FONT SIZE=6>XOTcl
- Tutorial - Index </FONT></FONT></FONT>
</P>
</TD>
<TD>
<IMG SRC="logo-100.jpg" NAME="Graphic1" ALIGN=RIGHT WIDTH=102 HEIGHT=42 BORDER=0></TD>
</TR>
</TABLE>
<p align=right>Version: 0.84</p>
<UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#introduction">Introduction
</A>
</P>
<UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#langOverview">Language Overview </A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#soccerClub"> Introductory Overview Example: Soccer Club</A>
</P>
</UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#object_class_system">Object
and Class System </A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#basic">Basic
Functionalities</A>
</P>
<UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#object">Objects </A>
</P>
<UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#data_on_obj">Data on
objects </A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#obj_methods">Methods
on Objects</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#obj_info">Information
on Objects</A>
</P>
</UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#classes">Classes </A>
</P>
<UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#class_instance">Creating
Classes and deriving Instances</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#class_methods">Methods
in Classes</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#class_info">Information
on Classes</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#class_inheritance">Inheritance</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#class_destroy">Destruction
of Classes</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#class_method_chaining">Method
Chaining</A>
</P>
</UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#class_dynamics">Dynamic
Class and Superclass Relationships</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#meta-classes">Meta-Classes</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#destroy-logic">Create, Destroy, and Recreate Scheme</A>
</P>
</UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#interceptors">Message
Interception Techniques</A></P>
<UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#filter">Filters</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#per-obj-mixins">Per-Object
Mixins</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#per-class-mixins">Per-Class
Mixins</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#callstack_info">Callstack Information</A>
</P>
</UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#nesting">Class Nesting
and Dynamic Object Aggregations</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#assertions">Assertions</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#additional-functionalities">Additional
Functionalities</A>
</P>
<UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#abstract-classes">Abstract
Classes</A>
</P>
<LI><P STYLE="margin-bottom: 0in" ><A HREF="#parameter">Parameter</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#autonames">Automatic Name Creation</A>
</P>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#meta-data">Meta-Data</A>
</P>
</UL>
<LI><P STYLE="margin-bottom: 0in"><A HREF="#references">References</A>
</P>
</UL>
<TABLE COLS=2 WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#000055">
<TR>
<TD WIDTH=75%>
<P><A NAME="introduction"></A><FONT COLOR="#ffffff"><FONT FACE="Arial, Helvetica"><FONT SIZE=6>Introduction
</FONT></FONT></FONT>
</P>
</TD>
<TD>
<IMG SRC="logo-100.jpg" NAME="Graphic2" ALIGN=RIGHT WIDTH=102 HEIGHT=42 BORDER=0></TD>
</TR>
</TABLE>
<H2><A NAME="langOverview"></A> <BR>Language Overview
</H2>
<P>XOTcl <a href="#xotcl">[Neumann and Zdun 2000a]</a> is an extension
to the object-oriented scripting language OTcl <a
href="#otcl">[Wetherall and Lindblad 1995]</a> which itself extends
Tcl <a href="#tcl">[Ousterhout 1990]</a> (Tool Command Language) with
object-orientation. XOTcl is a value-added replacement for OTcl and
does not require OTcl to compile. It runs in its own shell, called
<TT>xotclsh</TT>. This shell takes the commands (similar to
<TT>tclsh</TT>) one by one from a file or the console. It interprets
them using the Tcl interpreter. For that reason all Tcl-commands
remain available (and are also applicable on the extension constructs).
</P>
<P>A central property of Tcl is, that it uses strings solely for the
representation of data. Internally it uses an dynamic type system with
automatic conversion (which enables efficient type handling). For
that reason all components (e.g. written in C) once integrated in Tcl
automatically fit together and the components can be reused in
unpredicted situations without change. The evolving <EM>component
frameworks</EM> have proven to provide a high degree of code reuse, a
rapid application development, and an ease of use. The application
developer may concentrate on the application task solely, rather than
investing efforts in fitting components together. Therefore, in
certain applications scripting languages like Tcl are very useful for
a fast and high-quality development of software (see <a
href="#ousterhout">[Ousterhout 1998]</a> for more details).
</P>
<P>Tcl is equipped with appropriate functionalities for the easy
gluing of components, like dynamic typing, dynamic extensibility, and
read/write introspection. OTcl is an object-oriented extension to Tcl,
which encourages a Tcl-like programming style and is composed of
language constructs with properties similar to Tcl. It offers an
object-orientation with encapsulation of data and operation without
protection mechanisms and single and multiple inheritance.
Furthermore it enables to change the relationships dynamically, offers
read/write introspection, has a three level class system based on
meta-classes and offers method chaining. These abilities are
integrated in XOTcl with only slight changes to OTcl visible to the
programmer.
</P>
<P>The XOTcl extension aims at complexity and adaptability issues that
may occur in context of large (object-oriented) software structures
and in the context of component glueing. In particular we added the
following support:
</P>
<UL>
<LI><P STYLE="margin-bottom: 0in"><I>Filters</I> as a means of
abstractions over method invocations to implement large program
structures, like design patterns.
</P>
<LI><P STYLE="margin-bottom: 0in"><I>Per-object mixins</I>, as a
means to give an object access to several different supplemental
classes, which may be changed dynamically.
</P>
<LI><P STYLE="margin-bottom: 0in"><I>Per-class mixins</I>, as a
means to give a class access to several different supplemental
classes, which may be changed dynamically.
</P>
<LI><P STYLE="margin-bottom: 0in"><I>Dynamic Object Aggregations</I>,
to provide dynamic aggregations through nested namespaces.
</P>
<LI><P STYLE="margin-bottom: 0in"><I>Nested Classes</I>, to reduce
the interference of independently developed program structures.
</P>
<LI><P STYLE="margin-bottom: 0in"><I>Assertions</I>, to reduce the
interface and the reliability problems caused by dynamic typing and,
therefore, to ease the combination of components.
</P>
<LI><P><I>Meta-data and Automatic Documentation</I>, to enhance self-documentation of objects
and classes.
</P>
</UL>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><A NAME="features"></A><A NAME="1176"></A>
  
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><STRONG>Figure 1:</STRONG>
Language Extensions of XOTcl
</P>
<CENTER>
<TABLE WIDTH=455 BORDER=0 CELLPADDING=2 CELLSPACING=0>
<COL WIDTH=451>
<TR>
<TD WIDTH=451>
<P><IMG SRC="features.gif" NAME="Graphic3" ALIGN=BOTTOM WIDTH=451 HEIGHT=378 BORDER=0></P>
</TD>
</TR>
</TABLE>
</CENTER>
<H2><A NAME="soccerClub"></A> <BR>Introductory Overview Example: Soccer Club
</H2>
<p>
To give you an impression of the language before we go into the
details of the language construct, we present in this section a
simple, introductory example. It shall demonstrate the basic
language constructs on the example of a soccer club (the full code can
be found in the <tt>xotcl/src/scripts/soccerClub.xotcl</tt> file. All
the characters in this example are fictitious, and any resemblance to
actual persons, living or deceased, is coincidental.
</p>
<p>
In XOTcl we do not have to provide a file description as a comment,
but we can use the <tt>@</tt> object, which is used generally to
provide any kind of information, metadata, and documentation on a
running program. Here, we just give a file description. Then the <tt>
makeDoc.xotcl</tt> tool can automatically document the program file for
us.
</p>
<PRE STYLE="margin-bottom: 0.2in">
@ @File {
description {
This is a simple introductory example for the language XOTcl.
It demonstrates the basic language constructs on the example of
a soccer club.
}
}
</PRE>
<p>
All things and entities in XOTcl are objects, a special kind of objects
are classes. These define common properties for other objects. For a
soccer club, we firstly require a common class for all kinds of members.
</p>
<p>
Common to all members is that they have a name. Common properties defined
across all instances of a class are called 'Parameters' in XOTcl.
</p>
<PRE STYLE="margin-bottom: 0.2in">
Class ClubMember -parameter {{name ""}}
</pre>
<p>
A special club member is a <tt>Player</tt>. Derived classes can be build with
inheritance (specified through <tt>superclass</tt>). Players may have a
<tt>playerRole</tt> (defaults to NONE).
</p>
<PRE STYLE="margin-bottom: 0.2in">
Class Player -superclass ClubMember -parameter {{playerRole NONE}}
</pre>
<p>
Other club member types are trainers, player-trainers, and presidents:
</p>
<PRE STYLE="margin-bottom: 0.2in">
Class Trainer -superclass ClubMember
Class President -superclass ClubMember
</pre>
<p>
The PlayerTrainer uses multiple inheritances by being both a player
and a trainer:
</p>
<PRE STYLE="margin-bottom: 0.2in">
Class PlayerTrainer -superclass {Player Trainer}
</pre>
<p>
Now we define the SoccerTeam class:
</p>
<PRE STYLE="margin-bottom: 0.2in">
Class SoccerTeam -parameter {name location type}
</pre>
<p>
We may add a player. This is done by a method. Instance methods are
in XOTcl defined with <tt>instproc</tt>. All club members are aggregated in
the team (denoted by :: namespace syntax).
</p>
<PRE STYLE="margin-bottom: 0.2in">
SoccerTeam instproc newPlayer args {
# we use a unique autoname for the object to prevent name
# collisions, like <soccerteam>::player01, <soccerteam>::player02, ...
eval Player [self]::[[self] autoname player%02d] $args
}
</pre>
<p>
A player can be transfered to another team. The player object does
not change internally (e.g. the playerRole stays the same). Therefore we
<tt>move</tt> it to the destination team.
</p>
<PRE STYLE="margin-bottom: 0.2in">
SoccerTeam instproc transferPlayer {playername destinationTeam} {
# We use the aggregation introspection option <tt>children</tt> in order
# to get all club members
foreach player [[self] info children] {
# But we only remove matching playernames of type "Player". We do
# not want to remove another club member type who has the same
# name.
if {[$player istype Player] && [$player name] == $playername} {
# We simply 'move' the player object to the destination team.
# Again we use a unique autoname in the new scope
$player move [set destinationTeam]::[$destinationTeam autoname player%02d]
}
}
}
</pre>
<p>
Finally we define two convenience to print the members/players to
the stdout with puts.
</p>
<PRE STYLE="margin-bottom: 0.2in">
SoccerTeam instproc printMembers {} {
puts "Members of [[self] name]:"
foreach m [[self] info children] {puts " [$m name]"}
}
SoccerTeam instproc printPlayers {} {
puts "Players of [[self] name]:"
foreach m [[self] info children] {
if {[$m istype Player]} {puts " [$m name]"}
}
}
</pre>
<p>
Now let us build to example soccer team objects.
</p>
<PRE STYLE="margin-bottom: 0.2in">
SoccerTeam chelsea -name "Chelsea FC" -location "Chelsea"
SoccerTeam bayernMunich -name "F.C. Bayern Mnchen" -location "Munich"
</pre>
<p>
With <tt>addPlayer</tt> we can create new aggregated player objects
<p></p>
Let us start some years in the past, when "Franz Beckenbauer" was
still a player.
</p>
<PRE STYLE="margin-bottom: 0.2in">
set fb [bayernMunich newPlayer -name "Franz Beckenbauer" \
-playerRole PLAYER]
</pre>
<p>
<tt>playerRole</tt> may not take any value. It may either be NONE, PLAYER,
or GOALY ... such rules may be given as assertions (here: an instinvar
gives an invariant covering all instances of a class). In XOTcl
the rules are syntactically identical to <tt>if</tt> statements:
</p>
<PRE STYLE="margin-bottom: 0.2in">
Player instinvar {
{[[self] set playerRole] == "NONE" ||
[[self] set playerRole] == "PLAYER" ||
[[self] set playerRole] == "GOALY"}
}
</pre>
<p>
If we break the invariant and turn assertions checking on, we should
get an error message:
</p>
<PRE STYLE="margin-bottom: 0.2in">
$fb check all
if {[catch {$fb set playerRole SINGER} errMsg]} {
puts "CATCHED EXCEPTION: playerRole has either to be NONE, PLAYER, or TRAINER"
# turn assertion checking off again and reset to PLAYER
$fb check {}
$fb set playerRole PLAYER
}
</pre>
<p>
But soccer players may play quite different, orthogonal
roles. E.g. Franz Beckenbauer was also a singer (a remarkably bad
one). However, we can not simply add such orthogonal, extrinsic
extensions with multiple inheritance or delegation. Otherwise we
would have either to build a lot of unnecessary helper classes, like
PlayerSinger, PlayerTrainerSinger, etc., or we would have to build
such helper objects. This either leads to an unwanted combinatorial
explosion of class or object number
</p><p>
Here we can use a per-object mixin, which is a language construct
that expresses that a class is used as a role or as an extrinsic
extension to an object.
</p><p>
First we just define the Singer class.
</p>
<PRE STYLE="margin-bottom: 0.2in">
Class Singer
Singer instproc sing text {
puts "[[self] name] sings: $text, lala."
}
</pre>
<p>
Now we register this class as a per-object mixin on the player object:
</p>
<PRE STYLE="margin-bottom: 0.2in">
$fb mixin Singer
</pre>
<p>
And now Franz Beckenbauer is able to sing:
</p>
<PRE STYLE="margin-bottom: 0.2in">
$fb sing "lali"
</pre>
<p>
But Franz Beckenbauer has already retired. When a player retires, we
have an intrinsic change of the classification. He *is* not a player
anymore. But still he has the same name, is club member, and
is a singer (brrrrrr).
</p><p>
Before we perform the class change, we extend the Player class to
support it. I.e. the playerRole is not valid after class change
anymore (we unset the instance variable).
</p>
<PRE STYLE="margin-bottom: 0.2in">
Player instproc class args {
[self] unset playerRole
next
}
</pre>
<p>
Now we can re-class the player object to its new class (now Franz
Beckenbauer is President of Bayern Munich.
</p>
<PRE STYLE="margin-bottom: 0.2in">
$fb class President
# Check that the playerRole isn't there anymore.
if {[catch {$fb set playerRole} errMsg]} {
puts "CATCHED EXCEPTION: The player role doesn't exist anymore (as it should be after the class change)"
}
</pre>
<p>
But still Franz Beckenbauer can entertain us with what he believes
is singing:
</p>
<PRE STYLE="margin-bottom: 0.2in">
$fb sing "lali"
</pre>
<p>
Now we define some new players for Bayern Munich:
</p>
<PRE STYLE="margin-bottom: 0.2in">
bayernMunich newPlayer -name "Oliver Kahn" -playerRole GOALY
bayernMunich newPlayer -name "Giovanne Elber" -playerRole PLAYER
</pre>
<p>
If we enlist the players of Munich Franz Beckenbauer is not enlisted
anymore:
</p>
<PRE STYLE="margin-bottom: 0.2in">
bayernMunich printPlayers
</pre>
<p>
But as a president he still appears in the list of members:
</p>
<PRE STYLE="margin-bottom: 0.2in">
bayernMunich printMembers
</pre>
<p>
Now consider an orthonogal extension of a transfer list. Every
transfer in the system should be notified. But since the transfer
list is orthogonal to SoccerTeams we do not want to interfere with
the existing implementation at all. Moreover, the targeted kind of
extension has also to work on all subclasses of SoccerTeam. Firstly, we
just create the extension as an ordinary class:
</p>
<PRE STYLE="margin-bottom: 0.2in">
Class TransferObserver
TransferObserver instproc transferPlayer {pname destinationTeam} {
puts "Player '$pname' is transfered to Team '[$destinationTeam name]'"
next
}
</pre>
<p>
Now we can apply the class as a per-class mixin, which functions
exactly like a per-object mixin, but on all instances of a class and
its subclasses. The <tt>next</tt> primitive ensures that the original
method on <tt>SoccerTeam</tt> is called after notifying the transfer (with
puts to stdout):
</p>
<PRE STYLE="margin-bottom: 0.2in">
SoccerTeam instmixin TransferObserver
</pre>
<p>
If we perform a transfer of one of the players, he is moved to the new
club and the transfer is reported to the stdout:
</p>
<PRE STYLE="margin-bottom: 0.2in">
bayernMunich transferPlayer "Giovanne Elber" chelsea
</pre>
<p>
Finally we verify the transfer by printing the players:
</p>
<PRE STYLE="margin-bottom: 0.2in">
chelsea printPlayers
bayernMunich printPlayers
</pre>
<p>
<P><BR><BR>
</P>
<TABLE COLS=2 WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#000055">
<TR>
<TD WIDTH=75%>
<P><A NAME="object_class_system"></A><FONT COLOR="#ffffff"><FONT FACE="Arial, Helvetica"><FONT SIZE=6>Object
and Class System </FONT></FONT></FONT>
</P>
</TD>
<TD>
<IMG SRC="logo-100.jpg" NAME="Graphic4" ALIGN=RIGHT WIDTH=102 HEIGHT=42 BORDER=0></TD>
</TR>
</TABLE>
<P>In XOTcl every object is associated with a class over the <TT>class</TT>
relationship. Classes are special objects with the purpose of
managing other objects. ``Managing'' means that a class controls the
creation and destruction of its instances and that it contains a
repository of methods (``instprocs'') accessible for the instances.
Object-specific methods are called ``procs'', instance methods are
called ``instprocs''.
</P>
<P>The instance methods common to all objects are defined in the root
class <TT>Object</TT> (predefined or user-defined). Since a class is a
special (managing) kind of object it is managed itself by a special
class called ``meta-class'' (which manages itself). One interesting
aspect of meta-classes is that by providing a constructor
pre-configured classes can be derived. New user-defined meta-classes
can be derived from the predefined meta-class <TT>Class</TT> in order
to restrict or enhance the abilities of the classes that they manage.
Therefore meta-classes can be used to instantiate large program
structures, like some design patterns (see <a
href="#xotcl-filter">[Neumann and Zdun 1999a]</a> for more
details). The meta-class may hold the generic parts of the
structures. Since a meta-class is an entity of the program, it is
possible to collect these in pattern libraries for later reuse easily.
</P>
<P>XOTcl supports single and multiple inheritance. Classes are ordered
by the relationship <TT>superclass</TT> in a directed acyclic
graph. The root of the class hierarchy is the class <TT>Object</TT>.
A single object can be instantiated directly from this class. An
inherent problem of multiple inheritance is the problem of name
resolution, when for example two super-classes contain an instance
method with the same name. XOTcl provides an intuitive and unambiguous
approach for name resolution by defining the precedence order along a
linear ``<EM>next-path</EM>'' incorporating the class and mixin
hierarchies, which is modeled after CLOS. A method can invoke
explicitly the shadowed methods by the predefined command
<TT>next</TT>. When this command is executed a shadowed method is
``mixed into'' the execution of the current method. Method chaining
without explicit naming of the targeted method is very important for
languages supporting a dynamic class system, because one cannot always
predict which classes are currently participating in the inheritance
hierarchy at design time (often necessary in inheritance models, like
C++).
</P>
<P STYLE="margin-bottom: 0in">An important feature of all XOTcl
objects is the read/write introspection. The reading introspection
abilities of XOTcl are packed compactly into the <TT>info</TT>
instance method which is available for objects and classes. All
obtained information can be changed at run-time dynamically with
immediate effect. Unlike languages with a static class concept, XOTcl
supports dynamic class/superclass relationships. At any time the class
graph may be changed entirely using the <TT>superclass</TT> method, or
an object may change its class through the <TT>class</TT> method. This
feature can be used for an implementation of a life-cycle or other
intrinsic changes of object properties (in contrast to extrinsic
properties e.g. modeled through roles and implemented through
per-object and per-class mixins <a href="#xotcl-mixin">[Neumann and
Zdun 1999c]</a> ) . These changes can be achieved without loosing the
object's identity, its inner state, and its per-object behavior (procs
and per-object mixins).
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><A NAME="features1"></A><A NAME="11761"></A>
  
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><STRONG>Figure 2:</STRONG>
Object and Class System
</P>
<CENTER>
<TABLE WIDTH=469 BORDER=0 CELLPADDING=2 CELLSPACING=0>
<COL WIDTH=465>
<TR>
<TD WIDTH=465>
<P ALIGN=CENTER><IMG SRC="obj_class_system.gif" NAME="Graphic5" ALIGN=BOTTOM WIDTH=467 HEIGHT=144 BORDER=0></P>
</TD>
</TR>
</TABLE>
</CENTER>
<TABLE COLS=2 WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#000055">
<TR>
<TD WIDTH=75%>
<P><A NAME="basic"></A><FONT COLOR="#ffffff"><FONT FACE="Arial, Helvetica"><FONT SIZE=6>Basic
Functionalities </FONT></FONT></FONT>
</P>
</TD>
<TD>
<IMG SRC="logo-100.jpg" NAME="Graphic6" ALIGN=RIGHT WIDTH=102 HEIGHT=42 BORDER=0></TD>
</TR>
</TABLE>
<H2><A NAME="object"></A> <BR>Objects
</H2>
<P>Initially XOTcl offers two new commands: <TT>Object</TT> and
<TT>Class</TT>. They represent hooks to the features of the language.
This section discusses both of them in detail and shows how they
function in the context of XOTcl. Note, that even if most of this is
compatible to OTcl, a few changes occur. For this reason, this
section is no introduction to plain OTcl. The <TT>Object</TT> command
provides access to the <TT>Object</TT> class, which holds the common
features of all objects, and allows us to define new objects. Objects
are always instances of classes, therefore, objects defined with the
<TT>Object</TT> command are (initially) instances of the <TT>Object</TT>
class. But since they have no user-defined type, they may be referred
to as <EM>singular objects</EM>. As all other objects they may be
specialized by object-operations and -data.
</P>
<P>The object command has the following syntax:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> Object objName ?args?</em></PRE><P>
A command of this form is a short-cut for a message to the <TT>create</TT>
instance method (forwarded automatically by the <TT>unknown</TT>
mechanism, which is invoked every time the message dispatch system
discovers an unknown message):
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> Object create objName ?args?</em></PRE><P>
It creates a new object of type <TT>Object</TT> with the name <TT>objName</TT>
(in fact it invokes a <TT>create</TT> call on the <TT>Object</TT> class).
<TT>objName</TT> becomes a new command, which allows us to access the
created object. Similiar to the <TT>Object</TT> command it may be
used like a normal Tcl-command (using sub-commands to access the
object's methods). Therefore, this form of access is called
<EM>object-command</EM> approach. A simple example is an object which
holds the information of a kitchen. It is created by:
</P>
<PRE STYLE="margin-bottom: 0.2in"> Object kitchen</PRE><P>
An object creation calls the constructor <TT>init</TT> of the
object's class. The destruction of an object is handled by the
<TT>destroy</TT> instance method. The general syntax of <TT>destroy
</TT>is:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> objName destroy</em></PRE><P>
E.g. the kitchen object is destroyed by:
</P>
<PRE STYLE="margin-bottom: 0.2in"> kitchen destroy</PRE><P>
To invoke a user-defined destruction process, it is possible to
overload this instance method in every class derived from object.
</P>
<H3><A NAME="data_on_obj"></A>Data on Objects
</H3>
<P>The <TT>Object</TT> class provides a range of operations to manage
objects, including those to manipulate data-structures on the
objects. They are similiar to the same-named Tcl-commands:
</P>
<PRE><em> objName set varname ?value?
objName unset v1 ?v2 ... vn?
</em></PRE>
<P>
The <TT>set</TT> instance method with given <TT>value</TT> option
allows us to manipulate an object-variable's value or to create a new
one, if the variable <TT>varname</TT> does not exist on the object so
far. Without <TT>value</TT> option the <TT>set</TT> operation queries
the variable and returns it's value, if the variable exists,
otherwise it produces an error message. The <TT>unset</TT> operation
deletes one or optionally a set of variables from an object. For
example the <TT>kitchen</TT> object can store information on the
color of the wall-paper by:
</P>
<PRE STYLE="margin-bottom: 0.2in"> kitchen set wallPaperColor white</PRE><P>
Similiar to Tcl-variables the object variables are dynamical; they
may be set at run-time when they are needed and unset when they
become obsolete. E.g. the persons in the kitchen may be stored in an
array. If there are no persons in the kitchen the array is deleted:
</P>
<PRE> # Peter enters the kitchen to cook
kitchen set persons(cook) Peter
...
# Marion enters the kitchen to take one of the seats
kitchen set persons(seat1) Marion
...
# Both Peter and Marion leave the kitchen
# the array is deleted by unset
kitchen unset persons</PRE><P>
Since XOTcl variables are internally realized through Tcl-variables
they may be treated like all Tcl-variables. For that reason they have
all Tcl-variable abilities, including the possibility to handle them
as lists or arrays (as seen in the last example). The <TT>array</TT>
command of Tcl is mapped to an XOTcl-command directly. An
object-oriented call to an object of the form
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> objName array option ary args</em></PRE><P>
forwards its arguments to an <TT>array</TT> Tcl-command for the
object´s instance variable <TT>ary</TT>. It could be used like
the same-named Tcl-command, e.g. the command
</P>
<PRE STYLE="margin-bottom: 0.2in"> kitchen array names persons</PRE><P>
returns all indexes currently stored in the <TT>persons</TT> array.
</P>
<P>Similarly Tcl´s <TT>incr</TT> command is mapped to the
object system. A call with the syntax:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> objName incr varName ?value?</em></PRE><P>
increments <TT>varName</TT> with the given value (or without given
value with 1).
</P>
<H3><A NAME="obj_methods"></A>Methods on Objects
</H3>
<P>Methods in XOTcl resemble Tcl-procedures. On objects one can define
object-specific methods, called procs. Instance methods which are
defined on classes are called instprocs. A new proc is defined using
the <TT>proc</TT> instance method of the class <TT>Object</TT>:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> objName proc name args body</em></PRE><P>
The arguments of the <TT>proc</TT> instance method specify the name,
the arguments as a Tcl-list, and the body of the new proc. All of them
must be given, only one of <TT>args</TT> and <TT>body</TT> may be
empty. An example proc would be a method to let persons enter the
kitchen:
</P>
<PRE> kitchen proc enter {name} {
[self] set persons($name) [clock seconds]
}</PRE><P>
Here the predefined <TT>self</TT> command is used in one of three
possible ways, which allow us to access useful information when working
with XOTcl-methods, these are in particular:
</P>
<UL>
<LI><P STYLE="margin-bottom: 0in"><TT>self</TT> - returns the name
of the object, which is currently in execution. This command is
similar to <TT>this</TT> in C++. It is automatically generated on
each object. If it is called from outside of a proc, it returns the
error message ``<TT>Can't find self</TT>''.
</P>
<LI><P STYLE="margin-bottom: 0in"><TT>self class</TT> - the self
command with a given argument <TT>class</TT> returns the name of the
class, which holds the currently executing instproc. Note, that this
may be different to the class of the current object. If it is called
from a proc it returns an empty string.
</P>
<LI><P><TT>self proc</TT> - the self command with a given argument
<TT>proc</TT> returns the name of the currently executing proc or
instproc.
</P>
</UL>
<P>Note, that there is a difference to the realisation of these
object informations to OTcl. XOTcl uses commands in order to make
XOTcl-methods compatible to Tcl-procedures and accessible via
namespace-paths. OTcl uses the three variables <TT>self</TT>, <TT>class</TT>
and <TT>proc</TT>, which are filled automatically with proper values
by the interpreter each time a method is called. To gain
compatibility a compilation option <TT>AUTOVARS</TT> is available
which provides these variables additionally (if the option is defined
when compiling XOTcl). The default is, that the option is turned off.
</P>
<P>Each XOTcl-method has its own scope for definition of local
variables for the executing method. In most cases when a method uses
object-variables, it is likely that the programmer wants to make one
or more of these variables part of the method's scope. Then the
Tcl-command for variable handling, like <TT>set</TT>, <TT>lindex</TT>,
<TT>array</TT>, ... work also on these variables. The
<TT>instvar</TT> instance method links a variable to the scope of
an executing method. It has the syntax:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> objName instvar v1 ?v2 ... vn?</em></PRE>
<P>
It makes the variables <tt>v1 ... vn</tt>, which must
be variables of the object, part of the current method's scope. A
special syntax is:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> {varName aliasName}</em></PRE>
<P>
for one of the variables. This gives the variable with the name
<TT>varName</TT> the alias <TT>aliasName</TT>. This way the variables
can be linked to the methods scope, even if a variable with that name
already exists in the scope. Now the <TT>enter</TT> method can be
adapted slightly and a <TT>leave</TT> method can be added, which uses
Tcl's <FONT FACE="courier, monospace">info</FONT> command to check
whether the named person is in the object's <TT>persons</TT> array. To
demonstrate the alias-syntax this is done with the <TT>persons</TT>
array and the alias <TT>p</TT>.
</P>
<PRE> kitchen proc enter {name} {
[self] instvar persons
set persons($name) [clock seconds]
}
kitchen proc leave {name} {
[self] instvar {persons p}
if {[info exists p($name)]} {
puts "$name leaves after [expr {[clock seconds]-$p($name)}] seconds"
unset p($name)
} else {
puts "$name is not in the room"
}
}</PRE><H3>
<A NAME="obj_info"></A>Information on Objects
</H3>
<P STYLE="margin-bottom: 0in">XOTcl offers reading and writing
introspection. The reading introspection abilities are packed
compactly into the <TT>info</TT> instance method which is available
for objects and classes (there are special info options for object
aggregations, nested classes, mixins, filters, meta-data and
assertions, which are explained separately in the following
sections). The <TT>info</TT> instance method's options, from the view
of an object, are summarized in the following table. They are
identically to the OTcl info options on objects.
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><A NAME="table_oinfo"></A> 
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><STRONG>The object info
options</STRONG></P>
<CENTER>
<TABLE BORDER=1 CELLPADDING=2 CELLSPACING=3 FRAME=VOID RULES=COLS>
<TBODY>
<TR>
<TD COLSPAN=4></TD>
</TR>
<TR>
<TD ROWSPAN=7 VALIGN=TOP></TD>
<TD>
<P ALIGN=LEFT><TT>objName info args method </TT>
</P>
</TD>
<TD>
<P ALIGN=LEFT>Returns the arguments of the specified method.</P>
</TD>
<TD ROWSPAN=7 VALIGN=TOP></TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><TT>objName info body method </TT>
</P>
</TD>
<TD>
<P ALIGN=LEFT>Returns the body of the specified method.</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><TT>objName info class ?classname? </TT>
</P>
</TD>
<TD>
<P ALIGN=LEFT>Returns the name
of the class of the current
object, if classname was not
specified. Otherwise it
returns 1 if classname matches
the object's class and 0 if
not.
</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><TT>objName info commands ?pattern?</TT></P>
</TD>
<TD>
<P ALIGN=LEFT>Returns all
commands defined on the object
if <TT>pattern</TT> was not
specified. Otherwise it
returns all commands that
match the pattern.</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><TT>objName info default method arg var</TT></P>
</TD>
<TD>
<P ALIGN=LEFT>Returns 1 if the
argument <TT>arg</TT> of the
method <TT>method</TT> has a
default value, otherwise 0. If
the default value exists it is
stored in <TT>var</TT>.</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><TT>objName info procs ?pattern? </TT>
</P>
</TD>
<TD>
<P ALIGN=LEFT>Returns all
procs defined on the object if
<TT>pattern</TT> was not
specified, otherwise it
returns all procs that match
the pattern.</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><TT>objName info vars ?pattern? </TT>
</P>
</TD>
<TD>
<P ALIGN=LEFT>Returns all variables defined on the object if
<TT>pattern</TT>was not specified, otherwise it returns all
variables that match the pattern.</P>
</TD>
</TR>
<TR>
<TD COLSPAN=4></TD>
</TR>
</TBODY>
</TABLE>
</CENTER>
<P><BR><BR>
</P>
<P>For example on the <TT>kitchen</TT> object
</P>
<PRE STYLE="margin-bottom: 0.2in"> kitchen info procs</PRE><P>
returns <TT>enter</TT> and <TT>leave</TT> as a Tcl-list since these
are the procs defined on the object.
</P>
<H2><A NAME="classes"></A>Classes
</H2>
<H3><A NAME="class_instance"></A>Creating Classes and deriving
Instances
</H3>
<P>There are different ways to create a class in XOTcl. They have in
common that they derive the new class from a meta-class. Initially the
<TT>Class</TT> command provides access to the meta-class
<TT>Class</TT>, which holds the features common to all classes. It
also allows one to derive new meta-classes. The common way to create a
new class is:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> Class ClassName ?args?</em></PRE>
<P>
Similar to the object short form, this is a short form of a call to
the <TT>create</TT> instance method of the meta-class <TT>Class</TT>,
which is also executed by the standard <TT>unknown</TT> mechanism.
This mechanism is always triggered when XOTcl does not know a method
called on an object. Supposed that there is no method with the name
<TT>ClassName</TT>, defined on the class-object of <TT>Class</TT>,
XOTcl looks up the method <TT>unknown</TT> (which is found on the
Class <TT>Object</TT>) and executes it. The standard unknown-mechanism
of XOTcl calls <TT>create</TT> with all arguments stepping one step
to the right; in the general case:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> Class create ClassName ?args?</em></PRE><P>
This may also be called directly. Besides the indirection when using
<TT>unknown</TT>, in most cases there is no difference in the action
performed: Firstly the memory is allocated, using the <TT>alloc</TT>
instance method, afterwards the constructor <TT>init</TT> is called
on the creating object, which is in this case the class-object of the
meta-class <TT>Class</TT>. In seldom cases the programmer may want to
suppress the <TT>init</TT> call. To do so the <TT>alloc</TT> instance
method may also be called directly:
</P>
<PRE><em>
Class alloc ClassName ?args?
</em></PRE>
<P>
As seen in the preceding section objects are created in the same way.
The difference was, that the command <TT>Object</TT>, which accesses
a class, instead of the command <TT>Class</TT>, which accesses a
meta-class, was used. The user-defined classes may also be used in
the same way to create new objects:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> ClassName objName ?args?</em></PRE>
<P>
Resembling the creation of classes this creates an object <TT>objName</TT>
of type <TT>ClassName</TT> using the <TT>unknown</TT> mechanism. That
means the <TT>create</TT> instance method of the class is called. If
there is no other instance method defined on the class-path so far
(which would mean, an user defined creation process is invoked), the
<TT>create</TT> instance method of the class <TT>Object</TT> is
invoked. This method is similar to the <TT>create</TT> method of the
meta-class <TT>Class</TT>. It firstly calls the <TT>alloc</TT>
instance method on its (of the <TT>Class</TT> class) which allocates
memory for the object, and makes it an instance of it's class.
Afterwards a call to the constructor <TT>init</TT> is invoked.
</P>
<P>Now we can specify the object for the kitchen by the class to
which it belongs. In this case a kitchen is an instance of a room.
</P>
<PRE> Class Room
Room kitchen</PRE><P>
A <TT>set</TT> call on a class creates an instance variable on the
class-object. This variable is unique for all instances, therefore,
it may be referred to as a class variable.
</P>
<H3><A NAME="class_methods"></A>Methods in Classes
</H3>
<P>Methods in classes are called instprocs. Instprocs are reachable
for the class-object and all other instances of the class. The syntax
for defining an instproc is:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> ClassName instproc procname args body</em></PRE>
<P>
It is similar to the definition of procs on objects, but uses the
keyword <TT>instproc</TT> to distinguish between the methods defined
on the class-object and those defined on the class. Since all rooms
(in the modeled world) have ceilings, we may want to define a simple
convenience instproc, which is able to set the color:
</P>
<PRE> Room instproc setCeilingColor color {
[self] set ceilingColor $color
}</PRE><P>
A special instproc, the constructor <TT>init</TT>, was mentioned
already. Now we are able to define such an instproc. Defined on a
class it is responsible for all initialisation tasks, which needed to
be performed, when constructing a new instance object of the class.
The constructor of the <TT>Room</TT> can initialize a variable for
the color, in which the ceiling is painted, to white as default,
since this is the color of ceilings without painting.
</P>
<PRE> Room instproc init args {
[self] setCeilingColor white
next
}
</PRE>
<P>
After this definition, all instances derived from the <TT>Room</TT>
class have an instance variable <TT>ceilingColor</TT> with the value
<TT>white</TT>. The <TT>args</TT> argument used here is a special
argument in Tcl which allows us to use a list of arguments which may
change its length from call to call.
</P>
<H3><A NAME="class_info"></A>Information on Classes
</H3>
<P STYLE="margin-bottom: 0in">Resembling to objects, information on
classes may be gained through the <TT>info</TT> instance method of the
meta-class <TT>Class</TT>. Note that this instance method does not
only support the class info options, but also the object info options,
since the accessing command refers to the class-object, which itself
is an object and, therefore, offers its informations. The following
table summarizes the additional info options available on classes.
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><STRONG>The class info
options</STRONG></P>
<CENTER>
<TABLE BORDER=1 CELLPADDING=2 CELLSPACING=3 FRAME=VOID RULES=COLS>
<TBODY>
<TR>
<TD COLSPAN=4></TD>
</TR>
<TR>
<TD ROWSPAN=7 VALIGN=TOP></TD>
<TD>
<P ALIGN=LEFT><TT>ClassName info heritage ?pattern? </TT>
</P>
</TD>
<TD>
<P ALIGN=LEFT>Returns a list of all classes in the precedence
order of the class hierarchy matching <TT>pattern</TT> or of
all, if <TT>pattern</TT> was not specified.</P>
</TD>
<TD ROWSPAN=7 VALIGN=TOP></TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><TT>ClassName info instances ?pattern? </TT>
</P>
</TD>
<TD>
<P ALIGN=LEFT>Returns a list of the instances of the class
matching <TT>pattern</TT> or of all, if <TT>pattern</TT> was not
specified.
</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><TT>ClassName info instargs method</TT></P>
</TD>
<TD>
<P ALIGN=LEFT>Returns the arguments of the specified method.</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><TT>ClassName info instbody method</TT></P>
</TD>
<TD>
<P ALIGN=LEFT>Returns the body of the specified method.</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><TT>ClassName info instcommands ?pattern?</TT></P>
</TD>
<TD>
<P ALIGN=LEFT>Returns all commands defined on the class, if
<TT>pattern</TT> was not specified, otherwise it returns all
commands that match the pattern.</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><TT>ClassName info subclass ?classname? </TT>
</P>
</TD>
<TD>
<P ALIGN=LEFT>Returns a list of all subclasses of the class, if
<TT>classname</TT> was not specified, otherwise it returns 1 if
<TT>classname</TT> is a subclass and 0 if not.</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><TT>ClassName info superclass ?classname? </TT>
</P>
</TD>
<TD>
<P ALIGN=LEFT>Returns a list of all super-classes of the class,
if <TT>classname</TT> was not specified, otherwise it returns 1
if <TT>classname</TT> is a superclass and 0 if not.</P>
</TD>
</TR>
<TR>
<TD COLSPAN=4></TD>
</TR>
</TBODY>
</TABLE>
</CENTER>
<P><BR><BR>
</P>
<H3><A NAME="class_inheritance"></A>Inheritance
</H3>
<P>Besides encapsulation of operations and state in objects, a second
central ability of object-orientation is inheritance. XOTcl supports
single and multiple inheritance with a directed acyclic class
graph. Automatically each new class created by the instance methods
<TT>create</TT> and <TT>alloc</TT> of <TT>Class</TT> inherits from
<TT>Object</TT>. Therefore, it is ensured that all instances of the
new class have access to the common features of objects stored in the
class <TT>Object</TT>.
</P>
<P>To specify further inheritance relationships the instance methods
<TT>superclass</TT> of <TT>Class</TT> is used:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> ClassName superclass classList</em></PRE><P>
E.g. in the example a kitchen may be seen as a
special room:
</P>
<PRE> Class Room
Class Kitchen -superclass Room</PRE><P>
Now all instances of <TT>Kitchen</TT> are able to access the
operations stored in the <TT>Room</TT> and in the <TT>Kitchen</TT> class.
Note the transition the kitchen was going through: firstly it was a
singular object, then it was an object with a user-defined class,
and now it is a class. This is possible (and not senseless) because
of the per-object specialisation ability and the dual shape of a
class, which is at the same time object and class. Both lead to a
seamless connection of the run-time properties (the object features)
and their descriptive properties (the class features). It is possible
to avoid the strict distinction between them, known from static typed
languages, like C++, Java, etc.
</p>
<p>Moreover, since the syntaxes of constructs expressing the same
concern are nearly identical, we can refactor a solution with very few
changes to the alternative. We will see similar "ease of refactoring"
throughout the XOTcl language. E.g., we can also easily refactor the
class hierarchies or exchange class hierarchies against mixin
solutions with only slight changes in the code.
</P>
<P>Besides single inheritance, as seen, XOTcl provides also multiple
inheritance. This is syntactically solved by giving the <TT>superclass</TT>
instance method a list of classes instead of a single class as
argument.
</P>
<PRE> Class Room
Class 4WallsRoom -superclass Room
Class CookingPlace
Class Kitchen -superclass {4WallsRoom CookingPlace}
</PRE><P>
Now the kitchen class is specialized a bit more. It is a special room
which has four walls <EM>and</EM> it is a cooking place. Multiple
inheritance, as seen here, is as simple to apply as single
inheritance.
</P><P>
Most often when the disadvantages of multiple inheritance are
discussed, the name resolution along the class graph is considered as
the biggest problem. The question is, which method is to be chosen and
which path through class graph is to be taken, if more then one method
of the specified name exist on the class graph.
</P>
<P ALIGN=LEFT STYLE="margin-bottom: 0in">In the example such questions
would arise for an object of the <TT>Kitchen</TT> class, if two
same-named methods are defined on <TT>CookingPlace</TT> and
<TT>4WallsRoom</TT> or if a method of the class <TT>Object</TT> is
called, which is reachable through two paths (along
<TT>CookingPlace</TT> or <TT>Room</TT>).
</P>
<P ALIGN=LEFT STYLE="margin-bottom: 0in">Often - e.g. in the
inheritance model of C++ - the path through the graph is not clearly
determined and/or the rules are too complicated to be understood on
the first glance. The programmer often can only determine by trial
which method is found firstly. Than an explicit naming of the class is
necessary, which means storage of non-local information in
sub-classes. Often different compilers of one language behave
differently. All these issues make code reuse difficult. Moreover
understandability and portability are reduced.
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><BR>
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><STRONG>Figure 3:</STRONG>
The example classes and the following next-path</P>
<CENTER>
<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0>
<COL>
<TR>
<TD>
<IMG SRC="next-path.gif" NAME="Graphic13" ALIGN=LEFT BORDER=0><BR CLEAR=LEFT></TD>
</TR>
</TABLE>
</CENTER>
<P>XOTcl goes an intuitive and unambiguous way to solve this problem.
It resolutes the precedence order along a ``<EM>next-path</EM>''.
Firstly the class of the object is searched, which is <TT>Kitchen</TT>
in example. Then the super-classes are searched in definition order,
which means at first <TT>4WallsRoom</TT>, then <TT>CookingPlace</TT>.
Each branch is searched completely, before changing to the next
branch. That means, <TT>Room</TT> is searched, before the
<TT>CookingPlace</TT> branch is visited. At last the top of the
hierarchy, the class <TT>Object</TT>, is searched.
</P>
<P>The usage of <TT>next</TT> in XOTcl is different to OTcl: In OTcl
it is always necessary to provide the full argument list for every
invocation explicitly. In XOTcl, a call of <TT>next</TT> without
arguments can be used to call the shadowed methods with the same
arguments (which is the most common case). When arguments should be
changed for the shadowed methods, they must be provided explicitly in
XOTcl as well. In the rare case that the shadowed method should
receive no argument, the flag <TT>--noArgs</TT> must be used.
</P>
<H3><A NAME="class_destroy"></A>Destruction of Classes
</H3>
<P>Classes are destroyed by the destruction of the class-object using
the <TT>destroy</TT> method of the <TT>Object</TT> class. The
destruction of super-classes does not destroy the sub-classes. The
super-class is simply removed from the sub-classes' super-class
lists. All classes have the super-class <TT>Object</TT>, if no
super-class is specified. Therefore, if all super-classes are
destroyed or removed, the new super-class is <TT>Object</TT>, not: no
super-class. The destruction of the class of an object does neither
delete the object nor leave it without class. In XOTcl a deleted class
leaves it's instances with the class <TT>Object</TT>.
</P>
<P>So all empty class- and superclass-relationships are automatically
reseted to <TT>Object</TT>. Note, that this are differences to OTcl,
where the destruction of an class destroys all instances and an empty
super-class list remains empty.
</P>
<H3><A NAME="class_method_chaining"></A>Method Chaining
</H3>
<P>A special feature of XOTcl is the method chaining without explicit
naming of the ``mix-in''-method. It allows one to mix the same-named
superclass methods into the current method (modelled after CLOS). The
previously described next-path is the basis for this functionality.
At the point marked by a call to the <TT>next</TT> primitive of XOTcl
the next shadowed method on the next path is searched and, when it is
found, it is mixed into the execution of the current method. When no
method is found, the call of <TT>next</TT> returns an empty string,
otherwise it returns the result of the called method. Note, that the
realization through a primitive command -- similar to the <TT>self</TT>
command -- is a difference to OTcl, where <TT>next</TT> is realized
through an instance method of <TT>Object</TT>. The syntax is:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> next ?arguments|--noArgs?</em></PRE><P>
Note, that also the usage of <TT>next</TT> in XOTcl is different to
OTcl, since the <TT>next</TT> call without arguments in OTcl means
per default that no arguments are passed. But most often all
arguments are passed through to the shadowed method (since it will
most likely have the same signature like its precessedor). When all
variables should be passed through, in OTcl it is necessary for
correct variable substitution to use:
</P>
<PRE STYLE="margin-bottom: 0.2in"> eval $self next $args</PRE><P>
To avoid such difficulties, we made the passing of all arguments the
default case; a simple
</P>
<PRE STYLE="margin-bottom: 0.2in"> next</PRE><P>
performs the task of passing all arguments to the shadowed methods.
These arguments are called the <EM>standard arguments</EM>. If the
standard argument feature should not be used, optionally arguments
can be given or the flag <TT>--noArgs</TT> could be set as sole
argument, which means that the shadowed method is called with no
arguments.
</P>
<P>
E.g. the following <tt> next </tt> call ignores the standard arguments
and sends the arguments 1 and 2 instead:
</P>
<PRE STYLE="margin-bottom: 0.2in"> next 1 2</PRE><P>
<P>As an example all classes involved in the previous example should
get a constructor instance method, which simply sets an instance
variable on the object:
</P>
<PRE> Room instproc init args {
[self] set roomNumber 0
next
}
4WallsRoom instproc init args {
[self] set doorPosition 0
next
}
CookingPlace instproc init args {
[self] set stoveType electric
next
}
Kitchen instproc init args {
[self] set cookName -
next
}</PRE><P>
After creation an object of class <TT>Kitchen</TT> gets automatically
four instance variables <TT>cookName</TT>, <TT>roomNumber</TT>,
<TT>doorPosition</TT> and <TT>stoveType</TT> setted up with default
values in this order (since this is the order of the classes in the
next-path). Note, that the order is important, because one missing
next call, in one of the <TT>init</TT> methods, means that succeeding
<TT>init</TT> methods will not be executed. This mechanism functions
equally on all kinds of instprocs, not only on constructors.
</P>
<P>The constructors use the <TT>args</TT> argument, which allows us to
give a list of variable length as arguments. To ensure reusability of
our classes the constructors should use <TT>args</TT> in most cases,
since they may pass through arguments for constructors further up the
class hierarchy.
</P>
<P>If a <TT>proc</TT> with the searched name exists on the object it
overshadows all instprocs. A <TT>next</TT> call in a proc leads to
the normal next-paths search, starting with the object's class.
</P>
<H2><A NAME="class_dynamics"></A>Dynamic Class and Superclass
Relationships
</H2>
<P>Another property of XOTcl that distinguishes it from statically typed
languages are dynamics of class relationships. The realization of the
definition of super-classes as seen above with the <TT>superclass</TT>
method suggests already, that it is not only available at the class
definition time. In the above example its appended to the class
definition with "<TT>-superclass</TT>" as a short syntax
for method invocation at defintion time (all other avaiable methods
can also be called with a preceding dash ("-") appended
to definitions).
</P>
<P>At any time the class graph may be changed entirely using the
<tt>superclass</tt> method. Suppose the rooms and kitchens created in
modelling of a house should be displayed to a screen, but it is not
determined, whether the user of the system has the possiblities for
graphical outputs. Two classes <TT>TextOutput</TT> and
<TT>GraphicalOutput</TT> may be defined, which handle the output. Both
have an instproc <TT>paint</TT> which does the painting of the virtual
world on the chosen display type. The common output requirements are
handled by a derived class <TT>VirtualWorldOutput</TT> which calls the
<TT>paint</TT> method of the superclass using <TT>next</TT>. In
statically typed languages it would need more sophisticated constructs
to change the output class at run-time. E.g. a delegation to another
object handling the intrinsic task of the output object would be
introduced solely for the purpose of configuring the output
form. With a dynamic class system we can use the <TT>superclass</TT>
method to do so easily:
</P>
<PRE> Class TextOutput
TextOutput instproc paint args {
# do the painting ...
}
Class GraphicalOutput
GraphicalOutput instproc paint args {
# do the painting ...
}
# initially we use textual output
Class VirtualWorldOutput -superclass TextOutput
VirtualWorldOutput instproc paint args {
# do the common computations for painting ...
next; # and call the actual output
}
# users decides to change to graphical output
VirtualWorldOutput superclass GraphicalOutput
</PRE>
<P>
Sometimes, such a change to new intrinsic properties should not happen
for all instances of a class (or the class hierarchy), but only for
one specific object. Then the usage of a dynamic super-class
relationship is a too coarse-grained means. A second form of such
dynamics is the changing of the relationship between object and
class. This means, objects can also change their class dynamically at
run-time. This feature may be used to model a life-cycle of an object,
without loosing the object's identity, inner state or
per-object-specializations through procs. The <TT>class</TT> instance
method enables this functionality.
</P>
<P>An example would be an agent for the virtual world. Agents may be
placeholders for persons, who interactively travel the world, or
programs, which act automatically. When a person decides at run-time
to give a task it has performed formerly by hand to an automatic
agent, the agents nature changes from interactive agent to automatic
agent, but the identity and the local state (that means the parts of
the task, that are already fulfilled by the person) stay the same.
This is a scenario for changing class relationships, e.g.:
</P>
<PRE> Class Agent
Class AutomaticAgent -superclass Agent
Class InteractiveAgent -superclass Agent
# create a new agent for a person
InteractiveAgent agent1
# the person does something ...
#and decides the change to an automatic agent
agent1 class AutomaticAgent</PRE>
<H2>
<A NAME="meta-classes"></A>Meta-Classes
</H2>
<P>As seen already, a special kind of classes are meta-classes. They
are classes (and like all XOTcl classes also objects) which contain
the features common to all derived classes. We have already shown the
meta-class <TT>Class</TT>, which we was used in the previous sections
to create new classes.
</P>
<P>A new meta-class can be derived from an existing meta-class by
defining <TT>Class</TT> as superclass. Beside that a meta-class
creation is a normal class-definition:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> Class myMetaClass -superclass Class</em></PRE><P>
This defines a new meta-class <TT>myMetaClass</TT>, which has all the
abilities of meta-classes. That means, that the programmer is able to
specify new class features or override old ones. Afterwards he may
instantiate these into new classes.
</P>
<P>This is a very powerful language feature, since it allows one to give
some classes further abilities than the others (or to restrict
classes). This way large programm structures, like certain design
pattern parts, may be instantiated. Meta-classes hold the common
abstract parts of the structures. They allow one to form libraries of
such structures very easily.
</P>
<P>
As a simple example we can derive a new meta-class
<TT>NoClassInfo</TT> from <tt>Class</tt>. Afterwards we override the
<tt>info</tt> method of <tt>Class</tt>. Thus the classes created with
<TT>NoClassInfo</TT>, have an <TT>info</TT> option that only produces
an error message. All classes created with <TT>NoClassInfo</TT>, like
<TT>Agent</TT> in the example below, are not capable of accessing the class
<tt>info</tt> method anymore:
</P>
<PRE> Class NoClassInfo -superclass Class
# redefine info ability
NoClassInfo instproc info args {
return "No class info avaiable"
}
# derive agent class from meta-class, which
# can not access class info
NoClassInfo Agent</PRE><P>
Now a call like:
</P>
<PRE STYLE="margin-bottom: 0.2in"> Agent info superclass</PRE><P>
returns in the error message.
</P>
<H2>
<A NAME="destroy-logic"></A>Create, Destroy, and Recreate Scheme
</H2>
<P>
<P>
XOTcl allows since version 0.84 for a flexible destroy and recreate scheme.
"create" and "alloc" are both Class instprocs handling creation for their
instances. I.e.:
</P>
<PRE>
className alloc [self]
</PRE>
and
<PRE>
className create [self]
</PRE>
<P>
are used for creating an instance. A similar method "instdestroy"
exists on Class that handles physical destruction of an object. The
method "destroy" on Object which lets an object destroy itself in fact
has the following behavior:
</P>
<PRE>
Object instproc destroy args {
[[self] info class] instdestroy [self]
}
</PRE>
<P>
However, this behavior is not implemented in XOTcl, but in C.
"create" distinguishes between the following situations:
</P>
<ul>
<li> <em>Creating a new object:</em> Firstly, call alloc. Then start <em>doInitializations.</em>
<li> <em>Recreating an existing object:</em> When an object does exist, it is
recreate in the following way:
<pre>
[self] class <givenClass>
[self] cleanup ;# resets "self" into a state
as after original "init"
<givenClass> recreate [self]
</pre>
<P>
"recreate" is a method that can be customized e.g. by overloading or
interception. Its default behaviour is: <em>doInitializations</em>
</P>
</ul>
In both cases, <em>doInitializations</em> is called in C and has the
following default behavior:
<ul>
<li> Search for parameter default values,
<li> Call "-" initialization methods,
<li> Call the constructor "init."
</ul>
<P>
Each step has a method call that can be changed, intercepted, etc. Of
course, cleanup, recreate, instdestroy, etc. can also be overloaded or
intercepted.
</P>
<TABLE COLS=2 WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#000055">
<TR>
<TD WIDTH=75%>
<P><A NAME="interceptors"></A><FONT
COLOR="#ffffff"><FONT FACE="Arial, Helvetica"><FONT SIZE=6>Message
Interception Techniques
</FONT></FONT></FONT>
</P>
</TD>
<TD>
<IMG SRC="logo-100.jpg" NAME="Graphic7" ALIGN=RIGHT WIDTH=102 HEIGHT=42 BORDER=0></TD>
</TR>
</TABLE>
<P>Even though object-orientation orders program structures around
data, objects are characterized primarily by their behavior.
Object-oriented programming style encourages the access of
encapsulated data only through the methods of an object, since this
enables data abstractions. A method invocation can be interpreted as a
message exchange between the calling and the called object.
Therefore, objects are at runtime only traceable through their message
exchanges. At this point the message interceptors can be applied to
catch and manipulate all incoming and outgoing messages of an
object.
<P>
</P>Generally interceptors can be applied to attach additional or
extrinsic concerns to an object or a class or a class hierarchy. For
instance roles or aspects can be implemented this way on various
levels of scale.
</P>
<P>We have already discussed some interception techniques
implicitly. E.g., the <tt>unknown</tt> mechanism intercepts messages
that have not be found on the object. It can be used as a very useful
programming technique, e.g., the define a default behavior for an
object. The interceptors presented in this section have a different
character: They are applied before/after the original method <em>even
if the method is defined for the target object</em>. Thus these
interception techniques may be applied
</P>
<P>We will discuss the message interceptors in this section in
detail. The table below gives an impression, when which interceptor
may be applied.
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><STRONG>Message
Interceptors Overview</STRONG></P>
<CENTER>
<TABLE BORDER=1 CELLPADDING=2 CELLSPACING=3 FRAME=VOID RULES=COLS>
<TBODY>
<TR>
<TD COLSPAN=7></TD>
</TR>
<TR>
<TD ROWSPAN=6 VALIGN=TOP></TD>
<TD>
<P ALIGN=LEFT><em> </em>
</P>
</TD>
<TD>
<P ALIGN=LEFT><strong>Applied When</strong</P>
</TD>
<TD>
<P ALIGN=LEFT><strong>Primary Target Structure</strong</P>
</TD>
<TD>
<P ALIGN=LEFT><strong>Coverage</strong</P>
</TD>
<TD ROWSPAN=6 VALIGN=TOP></TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><em> Filter</em>
</P>
</TD>
<TD>
<P ALIGN=LEFT>before/after a call
</P>
</TD>
<TD>
<P ALIGN=LEFT> class and class
hierarchies
</P>
</TD>
<TD>
<P ALIGN=LEFT>all methods
</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><em> Per-Object Mixin</em>
</P>
</TD>
<TD>
<P ALIGN=LEFT> before/after a call
</P>
</TD>
<TD>
<P ALIGN=LEFT> object-specific
</P>
</TD>
<TD>
<P ALIGN=LEFT> specific methods
</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><em> Per-Class Mixin</em>
</P>
</TD>
<TD>
<P ALIGN=LEFT> before/after a call
</P>
</TD>
<TD>
<P ALIGN=LEFT> class
</P>
</TD>
<TD>
<P ALIGN=LEFT> specific methods
</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>
<P ALIGN=LEFT><em> Unknown Mechanism</em>
</P>
</TD>
<TD>
<P ALIGN=LEFT> after method
was not found
</P>
</TD>
<TD>
<P ALIGN=LEFT> object
</P>
</TD>
<TD>
<P ALIGN=LEFT> all unknown calls
</P>
</TD>
</TR>
<TR>
<TD COLSPAN=7></TD>
</TR>
</TBODY>
</TABLE>
</CENTER>
<br>
<H2><A NAME="filter"></A>Filter
</H2>
<P>The filter (see <a href="#xotcl-filter">[Neumann and Zdun
1999a]</a> for more details) is an approach to manage large program
structures at the scale of several classes or class hierarchies.
It is a very general mechanism which can be used in various
application areas. E.g. a very powerful programming language support
for certain design patterns is easily achievable, but there are also
several other domains which are covered, like tracing of program
structures, self documentation at run-time, re-interpretation of the
running program, etc.
</P>
<P>A <I>filter</I> is a special instance method that is registered
for a class <I>C</I>. Every time an object of class <I>C</I> receives
a message, the <I>filter</I> method is invoked automatically.
</P>
<H3><A NAME="filter_usage"></A>Usage of Filters
</H3>
<P>All messages to objects of the filtered class must go through the
filter, before they reach their destination object. A simple example
would be a sole filter on the class of the object. To define such a
filter two steps are necessary. Firstly an filter-<TT>instproc</TT>
has to be defined, then the filter has to be registered. The filter
<TT>instproc</TT> (like the methods on the per-object mixin and
per-class mixin) consists of three parts which are all optional. A
filter instproc has the following form:
</P>
<PRE><EM> ClassName instproc FilterName args {
pre-part
next
post-part
}
</em></PRE>
<P>
When a filter comes to execution at first the actions in the <EM>pre-part</EM>
are processed. Afterwards the filter is free in what it does with the
message. Especially it can (a) pass the message, which was perhaps
modified in the <EM>pre-part</EM>, to other filters and finally to
the object. It can (b) redirect it to another destination. Or it can
(c) decide to handle the message on its own. The forward passing of
messages is implemented through the <TT>next</TT> primitive of XOTcl.
After the filter has passed its pre-part, the actual called method is
invoked through <TT>next</TT>.
</P>
<P>Afterwards, similar to <TT>next</TT> calls in OTcl, the execution
returns to the point in the filter, where the <TT>next</TT> call is
located and resumes execution with the actions of the <EM>post-part</EM>.
These may contain arbitrary statements, but especially may take the
result of the actual called method (which is returned by the
next-call) and modify it. The caller then receives the result of the
filter instead of the result of the actual called method.
</P>
<P>The pre- and post-part may be filled with any ordinary
XOTcl-statements. The distinction between the three parts is just a
naming convention for explanation purposes.
</P>
<P>The filter uses the <TT>args</TT> argument which lets us use a list of
variable length as arguments, since it must filter a lot of different
calls, which may have different argument lists. Furthermore, it may
pass through arguments to other filters and the preceding filters may
change the argument list.
</P>
<P>Since any instproc may be a filter, a registration of the filter
is necessary, in order to tell XOTcl, which instprocs are filters on
which classes. The <TT>filter</TT> instance method is able to handle
this task. Similar to the XOTcl-language introduced so far, the
filter registration is dynamical at run-time. By handing a list of
filters the programmer can change the filters registered on a class
at arbitrary times. The filter instance method has the syntax:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> ClassName filter filterList</em></PRE><P>
Now a simple example should show the filter's usage. In the preceding
examples we have defined several rooms. Every time a room action
occurs it is likely that the graphical sub-system has to change
something on the output of that particular room. Therefore, at first
we need a facility to be informed every time an action on a room
happens. This is quite easily done using filters:
</P>
<PRE> Class Room
Room r1; Room r2; # just two test objects
Room instproc roomObservationFilter args
puts "now a room action begins"
set result [next]
puts "now a room action ends - Result: $result"
return $result
}
Room filter roomObservationFilter</PRE><P>
Now every action performed on room-objects is notified with a pre-
and a post-message to the standard output stream. We return the
result of the actual called method, since we don't want to change the
program behavior at all. E.g. we can set an instance variable on both
of the two room objects:
</P>
<PRE> r1 set name "room 1"
r2 set name "room 2"</PRE><P>
The output would be:
</P>
<PRE> now a room action begins
now a room action ends - Result: room 1
now a room action begins
now a room action ends - Result: room 2</PRE><P STYLE="margin-bottom: 0in">
<BR>
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><A NAME="oneFilter"></A><A NAME="718"></A>
  
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><STRONG>Figure 4:</STRONG>
Cascaded Message Filtering</P>
<CENTER>
<TABLE WIDTH=480 BORDER=0 CELLPADDING=2 CELLSPACING=0>
<COL WIDTH=476>
<TR>
<TD WIDTH=476>
<P><FONT SIZE=1 STYLE="font-size: 2pt"><IMG SRC="cascaded-message-filter.gif" NAME="Graphic14" ALIGN=BOTTOM WIDTH=474 HEIGHT=281 BORDER=0></FONT></P>
</TD>
</TR>
</TABLE>
</CENTER>
<P><BR><BR>
</P>
<P>All classes may have more than one filter. In fact they may have a
whole filter chain, where the filters are cascaded through <TT>next</TT>.
The <TT>next</TT> method is responsible for the forwarding of
messages to the remaining filters in the chain one by one till all
pre-parts are executed. Afterwards the actual method is executed and
then the post-parts come to turn. If one next-call is omitted the
chain ends in this filter method. As an example for an additional
filter we may register a filter, which just counts the calls to
rooms.
</P>
<PRE> Room set callCounter 0 ;# set class variable
Room instproc counterFilter args {
[self class] instvar callCounter
incr callCounter
puts "the call number callCounter to a room object"
next
}
Room filter {roomObservationFilter counterFilter}</PRE><P>
Filters are invoked in registration order. The order may be changed
by removing them and adding them in new order. Filters are inherited
by sub-classes. E.g. in the preceding example for the next path, an
<TT>OvalOffice</TT> was derived from the <TT>Room</TT> class. Without
a change to the program each <TT>OvalOffice</TT> object automatically
produces the same filter output as rooms.
</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><A NAME="filterInheritance"></A><A NAME="734"></A>
  
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><STRONG>Figure 5:</STRONG>
Filter Inheritance</P>
<CENTER>
<TABLE WIDTH=2 BORDER=0 CELLPADDING=2 CELLSPACING=0>
<COL WIDTH=0>
<TR>
<TD>
<P><FONT SIZE=1 STYLE="font-size: 2pt"><IMG SRC="filter-inheritance.gif" NAME="Graphic15" ALIGN=BOTTOM WIDTH=508 HEIGHT=350 BORDER=0></FONT></P>
</TD>
</TR>
</TABLE>
</CENTER>
<P><BR>Filter chains can also be combined through (multiple)
inheritance using the <TT>next</TT> method. When the filter chain of
the object's class is passed, the filter chains of the super-classes
are invoked using the same precedence order as for inheritance. Since
on the sub-class there may also be a another filter chain, without
sophisticated computing in the pre- and post-parts one can produce
easily a powerful tracing facility. E.g. if we want to distinguish an
<TT>OvalOffice</TT> from other rooms we may want to add a filter
solely for rooms of the type <TT>OvalOffice</TT>:
</P>
<PRE> Class OvalOffice -superclass Room
OvalOffice o1; # test object
OvalOffice instproc ovalOfficeObservationFilter args {
puts "actions in an oval office"
next
}
OvalOffice filter ovalOfficeObservationFilter</PRE><P>
A simple call to the <TT>o1</TT> object, like:
</P>
<PRE STYLE="margin-bottom: 0.2in"> o1 set location "Washington"</PRE><P>
produces the following output:
</P>
<PRE> actions in an oval office
now a room action begins
the call number 3 to a room object
now a room action ends - Result: Washington</PRE><P>
As seen already filter registrations can be added dynamically at
runtime. But they may also be removed. Perhaps the counting on rooms
should stop after a while, then a simple call of the <TT>filter</TT>
method is sufficient:
</P>
<PRE STYLE="margin-bottom: 0.2in"> Room filter roomObservationFilter</PRE>
<P><BR><BR>
</P>
<H3><A NAME="filter_info"></A>Introspection on Filters
</H3>
In order to gain information about the currently registered filters on
a certain class, the class info option <TT>filters </TT>may be
queried. It returns a list of the currently registered filters:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> ClassName info filter</em></PRE>
<p>
A second special info option for filters is <TT>self regclass</TT>. It
returns the name of the class on which the filter is registered. Since
the filter may be registered on other classes then the class on which
it is defined, this may vary from <TT>self class</TT>.
</P>
<P><BR><BR>
</P>
<H3><A NAME="filter_trace"></A>A simple Trace Program Examples
</H3>
<P>The trace example primarily demonstrates the inheritance of filter
chains. Since all classes inherit from <TT>Object</TT>, a filter on
this class is applied on all messages to objects. The <TT>Trace</TT>
object encapsulates methods for managing the tracing:
</P>
<PRE> Object Trace
Trace set traceStream stdout
Trace proc openTraceFile name {
set [self]::traceStream [open $name w]
}
Trace proc closeTraceFile {} {
close $Trace::traceStream
set [self]::traceStream stdout
}
Trace proc puts line {
::puts $Trace::traceStream $line
}
Trace proc add classname {
$classname filter [concat [$classname info filter] traceFilter]
}</PRE><P>
First we define the object and set a variable for the stream to which
we send the trace outputs (here: stdout). With a method for opening
and a method for closing a file we can redirect the trace stream to a
file. <TT>puts</TT> is helper method for the filter to print an
output to the selected output stream. In <TT>add</TT> the <TT>traceFilter</TT>
is appended to the existing filters of a specified class. The actual
filter method (see below) displays the calls and exits of methods
with an according message. The calls are supplied with the arguments,
the exit traces contain the result values. We have to avoid the
tracing of the trace methods explicitly.
</P>
<PRE> Object instproc traceFilter args {
# don't trace the Trace object
if {[self] == "::Trace"} {return [next]}
::set context "[self class]->[self callingproc]"
::set method [self calledproc]
switch -- $method {
proc -
instproc {::set dargs [list [lindex $args 0] [lindex $args 1] ...] }
default {::set dargs $args }
}
Trace::puts "CALL $context> [self]->$method $dargs"
::set result [next]
Trace::puts "EXIT $context> [self]->$method ($result)"
return $result
}</PRE><P>
As trace message we write the callee´s context (class and
proc), the invoked method (using <TT>calledproc</TT>), and the given
arguments. In the switch statement we avoid to print whole method
bodies.
</P>
<P>With
</P>
<PRE STYLE="margin-bottom: 0.2in"> Trace add Room</PRE><P>
messages to all rooms, including all instances of <TT>Room</TT>´s
sub-classes, are surrounded with a CALL and an EXIT output. With
</P>
<PRE STYLE="margin-bottom: 0.2in"> Trace add Object</PRE><P>
messages to all objects in an XOTcl environment are surrounded with a
CALL and an EXIT output. In general, it is possible to restrict the
trace to instances of certain classes, or to produce trace output for
only certain methods. This requires registration methods and a more
sophisticated implementation of the filter method.
</P>
<H2><A NAME="per-obj-mixins"></A>Per-Object Mixins
</H2>
<P>Per-object mixins (see <a href="#xotcl-mixin">[Neumann and Zdun
1999c]</a> for more details) are a novel approach of XOTcl to handle
complex data-structures dynamically. It resembles the filter presented
in the preceding section. While the filters work on classes and whole
hierarchies, the per-object mixin is applied on a single object.
Therefore, it is a language construct mainly working on the
object-level, while filters work on the level of classes and
hierarchies of classes. The <I>per-object mixin</I> is a class which
is mixed into the precedence order of an object directly before the
object's class itself is searched. All methods which are mixed into
the execution of the current method, by method chaining or through a
per-object mixin, are called <I>mixin methods</I>.
</P>
<H3><A NAME="mixin_supplemental"></A>Supplemental Classes
</H3>
<P>Per-object mix-ins cover a problem which is not solvable elegantly
just by the method chaining, introduced so far. To bring in an
addition to a class, the normal XOTcl way is to define a mixin and
chain the methods through <TT>next</TT>, e.g.:
</P>
<PRE> Class Basic
Basic instproc someProc {
# do the basic computations
}
Class Addition
Addition instproc someProc {
# do the additional computations
[self] next
}</PRE><P>
In order to mix-in the additional functionality of the <EM>supplemental</EM>
class <TT>Addition</TT> a new helper class (sometimes called
intersection class) has to be defined, like:
</P>
<PRE STYLE="margin-bottom: 0.2in">Basic+Addition -superclass Addition Basic</PRE><P>
This is even applicable in a dynamical manner, every object of the
class <TT>Basic</TT> may be changed to class <TT>Basic+Addition</TT>
at arbitrary times, e.g.:
</P>
<PRE> Basic+Addition basicObj
...
basicObj class Basic+Addition</PRE><P>
Now consider a situation with two addition classes. Then following
set of classes has to be defined to cover all possible combinations:
</P>
<PRE> Class Basic
Class Addition1
Class Addition2
Class Basic+Addition1 -superclass Addition1 Basic
Class Basic+Addition2 -superclass Addition2 Basic
Class Basic+Addition1+Addition2 -superclass Addition2 Addition1 Basic</PRE><P>
The number of necessary helper classes rises exponential. For <I>n</I>
additions, 2<I><SUP>n</SUP></I>-1(or their permutations if order
matters) artificially constructed helper-classes are needed to
provide all combinations of additional mix-in functionality.
Furthermore it is possible that the number of additions is unlimited,
since the additions may produce other additions as side-effects. This
demonstrates clearly that the sub-class mechanism provides only a
poor mechanism for mix-in of orthogonal functionality. Therefore we
provide an extension in the form of object mixin classes, which are
added in front of the search precedence of classes.
</P>
<H3><A NAME="mixin-usage"></A>Usage of Per-Object Mixins
</H3>
<P>The mix-ins methods extend the next-path of shadowed methods.
Therefore, per-object mix-in methods use the <TT>next</TT> primitive
to access the next shadowed method. Consider the following example:
</P>
<PRE> Class Agent
Agent instproc move {x y} {
# do the movement
}
Class InteractiveAgent -superclass Agent
# Addition-Classes
Class MovementLog
MovementLog instproc move {x y} {
# movement logging
next
}
Class MovementTest
MovementTest instproc move {x y} {
# movement testing
next
}</PRE><P>
An agent class is defined, which allows agents to move around. Some
of the agents may need logging of the movements, some need a testing
of the movements, and some both (perhaps only for a while). These
functionalities are achieved through the additional classes, which we
will apply through per-object mixins.
</P>
<P>Before we can use the per-object mix-ins on a particular object,
we must register the mixins on it with the <TT>mixin</TT> instance
method. It has the syntax:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> objName mixin mixinList</em></PRE><P>
For example we may create two interactive agents, where one is logged
and one is tested:
</P>
<PRE> InteractiveAgent i1; InteractiveAgent i2
i1 mixin MovementLog
i2 mixin MovementTest</PRE><P>
At arbitrary times the mixins can be changed dynamically. For example
<TT>i2</TT>'s movements can also be logged:
</P>
<PRE STYLE="margin-bottom: 0.2in"> i2 mixin MovementTest MovementLog</PRE><P STYLE="margin-bottom: 0in">
<BR>
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><A NAME="per-obj-mixin"></A><A NAME="662"></A>
  
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><STRONG>Figure 6:</STRONG>
Per-Object Mix-ins: Next-Path for the Example</P>
<CENTER>
<TABLE WIDTH=315 BORDER=0 CELLPADDING=2 CELLSPACING=0>
<COL WIDTH=311>
<TR>
<TD WIDTH=311>
<P><FONT SIZE=1 STYLE="font-size: 2pt"><IMG SRC="next-path-mixin-movement.gif" NAME="Graphic16" ALIGN=BOTTOM WIDTH=307 HEIGHT=187 BORDER=0></FONT></P>
</TD>
</TR>
</TABLE>
</CENTER>
<P><BR><BR>
</P>
<P>The <TT>mixin</TT> option of the <TT>info</TT> instance method
allows us to introspect the per-object mix-ins. It has the syntax:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> objName info mixins ?class?</em></PRE><P STYLE="margin-bottom: 0in">
It returns the list of all mix-ins of the object, if <TT>class</TT>
is not specified, otherwise it returns <TT>1</TT>, if <TT>class</TT>
is a mixin of the object, or <TT>0</TT> if not.
</P>
<P> Note, that the constructors (init methods) of per-object mixins (and per-class mixins)
are only called, if the mixin is registered already during object
initialization (when init is called). For per-object mixins, one can
achieve the initialization of a mixin via an idiom like <PRE>
<TT>Object o -mixin M -init</TT>
</PRE> that
registers the mixin before init is called. When a mixin is registered after object
creation and it needs initializations, it is neccessary to define special methods
for this.
Note, that the behavior described here is introdoced in version 0.84 to ensure
consistent behavior of intrinsic classes, per-object and per-class mixins, and
to achieve predictable behavior for dynamic registration for all kind of mixins,
and as well during recreations of objects having
mixins registered. Older versions used heuristics for the initialisation of per-object mixins.
</P>
<H2><A NAME="per-class-mixins"></A>Per-Class Mixins
</H2>
<P>Per-class mixins are exactly identical in their behavior to
per-object mixins, but they operate on classes. Thus they are the
class-specific variant of the per-object mixins, like instprocs are a
class-specific variant of procs. Therefore, in the language the
per-class mixins are called instmixins.
</p>
<P>
In general a per-class mixin is a class which is mixed into the
precedence order of all instances of the class and all its subclasses
it is registered for. It is also searched before the object's class
itself is searched, but after per-object mixins.
</p>
<P>
Per-class mixins are linearized into the precedence order in the same
as all other classes. I.e. from the full list of per-object mixins,
per-class mixins, and intrinsic classes (and all the superclasses of
all these classes) always the last occurrence is used.
</p>
<P>
From the point of view of language expressibility instmixins are
not required, because they cannot express anything that per-object
mixins cannot express already (like procs can express any instproc
feature). We can simply register the per-object mixin in the
constructor of the class.
</p>
<P>
But there at least the following reasons for instmixins as an additional language
construct:
<OL>
<LI> we can at runtime determine with <tt>info mixin</tt>
and <tt>info instmixin</tt> whether it is a class- or object-specific
mixin. Thus we get a better structuring at runtime.
<LI> We have not to 'pollute' the constructors with per-class mixin
registrations. Therefore, the constructors get more understandable.
<LI>If it is required to add (and remove) dynamically interceptors
to a set of objects, which are instances of a certain type, per-class
mixins are much easier to handle (e.g. add an instmixin to Object
to intercept e.g. all calls to certain predefined methods).
<LI>The language is more 'symmetrical', since any object-specific
feature in XOTcl has a class-specific variant.
</OL>
<P>
<H3><A NAME="pcmixin-usage"></A>Usage of Per-Class Mixins
</H3>
<P>The mix-ins methods of per-class mixins extend the next-path of
shadowed methods in the same way as per-object mixin methods. Before
we can use a per-class mix-in on a particular class, we must
register the mixin on it with the <TT>instmixin</TT> instance method. It
has the syntax:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em>ClassName instmixin mixinList</em></PRE><P>
</P>
<P>
Now consider that in the given per-object mixin example all
interactive agents should be tested. We could either build a subclass
<tt>TestedInteractiveAgent</tt> or register the per-object mixin in
the constructor of the interactive agent class. The subclass solution
leads to the same combinatorial explosion of intersection classes as
discussed in the previous section, if more supplemental classes are
added. The per-object mixin solution pollutes the constructor and does
not prevail the structural semantics that the 'tested' property
belongs to the interactive agent class at runtime
</P>
<P>
Here, we can use a per-class mixin:
</P>
<PRE> Class Agent
Agent instproc move {x y} {# do the movement}
Class InteractiveAgent -superclass Agent
Class MovementTest
MovementTest instproc move {x y} {
# movement testing
next
}
# now register the instmixin
InteractiveAgent instmixin MovementTest
</PRE>
<P> The per-class mixin now operates on all interactive agent
including the instances of subclasses. E.g. for interactive agents
<TT>i1</TT> and <TT>i2</TT> we automatically have movement
testing. <TT>i2 </TT> is also logged, since it has the logging class
as object-specific mixin:
</P>
<PRE> InteractiveAgent i1
InteractiveAgent i2 -mixin MovementLog
i1 move 3 4
i2 move 1 2
</PRE>
<P>
At arbitrary times the instmixins can be changed dynamically.
</P>
<P>The <TT>instmixin</TT> option of the class <TT>info</TT> instance
method allows us to introspect the per-class mixins. It has the
syntax:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> ClassName info instmixins ?class?</em>
</PRE>
<P STYLE="margin-bottom: 0in">
It returns the list of all instmixins of the the class, if <TT>class</TT>
is not specified, otherwise it returns <TT>1</TT>, if <TT>class</TT>
is a mixin of the object, or <TT>0</TT> if not.
</P>
<H3>
<A NAME="callstack_info"></A>Callstack Information
</H3>
<P STYLE="margin-bottom: 0in">Since the presented interceptors are
normal XOTcl instprocs they can access all XOTcl introspection
abilities introduced so far. In instprocs all recent information is
accessible within their scope. But the interceptors are mechanisms,
which cover more then their sole scope. The meaningful usage of the
meta-programming abilities often requires to go further and to get
information from the caller's and the callee's scope (e.g for
delegation decisions). Therefore, we introduced rich callstack
informations for the interceptors. Note, that these are also available
for ordinary methods, but the "called..." info options return empty
strings.
</P>
<P> All callstack information are packed compactly into the
<tt>self</tt> primitive as additional options. Note, before XOTcl
version 0.84 these were implememted as a part of the <tt>info</tt>
method. They are part of the <tt>self</tt> command for conceptual
integrity: introspection options in <tt>info</tt> can be expected to
produce the same result, when they are not explicitly changed. In
contrast, all information provided by <tt>self</tt> are
callstack-dependent.
</P>
<P ALIGN=CENTER STYLE="margin-bottom: 0in"><STRONG>The additional
Callstack Information Options in <tt>self</tt> </STRONG></P>
<CENTER>
<TABLE BORDER=1 CELLPADDING=2 CELLSPACING=3 FRAME=VOID RULES=COLS>
<TBODY>
<TR>
<TD COLSPAN=4></TD>
</TR>
<TR>
<TD ROWSPAN=7 VALIGN=TOP></TD>
<TD>
<P ALIGN=LEFT><TT>self calledproc</TT></P>
</TD>
<TD VALIGN=TOP>
<P ALIGN=LEFT>Returns the name of the proc which was invoked in
the original call.</P>
</TD>
<TD ROWSPAN=5 VALIGN=TOP></TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD >
<P ALIGN=LEFT><TT>self calledclass</TT></P>
</TD>
<TD VALIGN=TOP>
<P ALIGN=LEFT>Returns the name
of the class which presumably (if no dynamic class change occurs
afterwards) is invoked in
the original call.</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD >
<P ALIGN=LEFT><TT>self callingclass</TT></P>
</TD>
<TD VALIGN=TOP>
<P ALIGN=LEFT>Returns the name of the class from which the
call was invoked (if one exists, otherwise an empty
string).</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD >
<P ALIGN=LEFT><TT>self callingproc</TT></P>
</TD>
<TD VALIGN=TOP>
<P ALIGN=LEFT>Returns the name of the proc from which the
call was invoked (if one exists, otherwise an empty
string).</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD >
<P ALIGN=LEFT><TT>self callingobject</TT></P>
</TD>
<TD VALIGN=TOP>
<P ALIGN=LEFT>Returns the name of the object from which the
call was invoked (if one exists, otherwise an empty
string).</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD >
<P ALIGN=LEFT><TT>self regclass</TT></P>
</TD>
<TD VALIGN=TOP>
<P ALIGN=LEFT>In a filter: returns the name
of the class on which the filter is registered.</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD >
<P ALIGN=LEFT><TT>self next</TT></P>
</TD>
<TD VALIGN=TOP>
<P ALIGN=LEFT>Return the
"next" method on the path as a string, i.e. the method which will be
called by [next].</P>
</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD COLSPAN=4></TD>
</TR>
</TBODY>
</TABLE>
</CENTER>
<P><BR><BR>
</P>
<P>Note, that three options with the prefix <TT>calling</TT>
represent the values of <TT>self</TT>, <TT>self proc</TT>, and <TT>self
class</TT> in the scope where the original call was invoked. In the
following section we will show a simple program in which all of the
<TT>info</TT> options have different values.
<H3><A NAME="filter_info_example"></A><BR>Filter Callstack Information Example
</H3>
<P>Now we discuss a simple example that shows that all filter
introspection options may have different values:
</P>
<PRE> Class InfoTrace
InfoTrace instproc infoTraceFilter args {
puts "SELF: [self]"
puts "SELF PROC: [self proc]"
puts "SELF CLASS: [self class]"
puts "INFO CLASS: [[self] info class]"
puts "CALLED PROC: [self calledproc]"
puts "CALLING PROC: [self callingproc]"
puts "CALLING OBJECT: [self callingobject]"
puts "CALLING CLASS: [self callingclass]"
puts "REGISTRATION CLASS: [self regclass]"
[self] next
}
Class CallingObjectsClass
CallingObjectsClass callingObject
Class FilterRegClass -superclass InfoTrace
Class FilteredObjectsClass -superclass FilterRegClass
FilteredObjectsClass filteredObject
CallingObjectsClass instproc callingProc
filteredObject set someVar 0
FilterRegClass filter add infoTraceFilter</PRE><P>
The invocation of <TT>callingObject callingProc</TT> produces the
following output:
</P>
<PRE> SELF: ::filteredObject
SELF PROC: infoTraceFilter
SELF CLASS: ::InfoTrace
INFO CLASS: ::FilteredObjectsClass
CALLED PROC: set
CALLING PROC: callingProc
CALLING OBJECT: ::callingObject
CALLING CLASS: ::CallingObjectsClass
REGISTRATION CLASS: ::FilterRegClass </PRE><P>
The filter reports for <TT>self</TT> the value <TT>filteredObject</TT>,
since this is the object on which the <TT>set</TT> call is invoked;
<TT>infoTraceFilter</TT> is the method of the filter, and therefore
the actual proc, while the actual class is <TT>InfoTrace</TT>, the
filter's class. The class of the actual object is
<TT>FilteredObjectsClass</TT>.
</P>
<P>The called procedure is <TT>set</TT>. While the program stays in a
XOTcl-instproc all calling-info-options are set, the calling
procedure is <TT>callingProc</TT>, the calling class is the class,
where the method is defined (namely <TT>CallingObjectsClass</TT>),
and the object from which the call invoked is <TT>callingObject</TT>.
</P>
<P>Since the filter's registration class differs from the class,
where it is defined, the corresponding information is still missing
(in this example <TT>FilterRegClass</TT>).
</P>
<TABLE COLS=2 WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#000055">
<TR>
<TD WIDTH=75%>
<P><A NAME="nesting"></A><FONT COLOR="#ffffff"><FONT FACE="Arial, Helvetica"><FONT SIZE=6>Class
Nesting and Dynamic Object Aggregations </FONT></FONT></FONT>
</P>
</TD>
<TD>
<IMG SRC="logo-100.jpg" NAME="Graphic9" ALIGN=RIGHT WIDTH=102 HEIGHT=42 BORDER=0></TD>
</TR>
</TABLE>
<P>Most object-oriented analysis and design methods are based on the
concepts of generalization and aggregation. Generalization is
achieved through class hierarchies and inheritance, while static
aggregation is provided through embedding. Since version 8.0 Tcl
offers a namespace concept which can be used as a mechanism to
provide dynamic aggregations.
</P>
<P>A <EM>namespace</EM> provides an encapsulation of variable and
procedure names in order to prevent unwanted name collisions with
other system components. Each namespace has a unique identifier which
becomes part of the fully qualified variable and procedure names.
Namespaces are therefore already object-based in the terminology of
Wegner. Otcl is object-oriented since it offers classes and class
inheritance. Its objects are also namespaces, but an object is more
than only a namespace. Therefore, two incompatible namespace concepts
have existed in OTcl in parallel.
</P>
<P>Extended OTcl combines the namespace concept of Tcl with the
object concept of OTcl. Every object and every class in XOTcl is
implemented as a separate Tcl namespace. The biggest benefit of this
design decision aside from performance advantages is the ability to
aggregate objects and nest classes. Contrary in OTcl every object has
a global identifier. Through the introspection abilities of
namespaces nested classes are also traceable at runtime and can be
changed dynamically. In XOTcl objects are allowed to contain nested
objects, which are dynamically changeable aggregates of the
containing object.
</P>
<H2><A NAME="nested_classes"></A>Nested Classes
</H2>
<P>The notation for nested classes follows the syntax of Tcl
namespaces by using ``::'' as a delimiter. For example the
description of a oval carpet and a desk can nest inside of the
<TT>OvalOffice</TT> class:
</P>
<PRE> Class OvalOffice
# general carpet
Class Carpet
Class OvalOffice::Desk
# special oval carpet - no name collision
Class OvalOffice::Carpet -superclass ::Carpet</PRE><P>
Nested classes can be used exactly like ordinary classes, a user can
sub-class it, derive instances, etc. The information about the
nesting structure of classes is available through the <TT>info</TT>
instance method:
</P>
<PRE><EM> ClassName info classchildren
ClassName info classparent
</em></PRE><P>
The <TT>classchildren</TT> option returns a list of children, if one
or more exist, otherwise it returns an empty string. <TT>classparent</TT>
results in the name of the parent class, if the class is nested.
Since nested classes are realized through namespaces, all
functionality offered by Tcl's <TT>namespace</TT> command is usable
from XOTcl as well.
</P>
<H2><A NAME="obj-agg"></A>Dynamic Object Aggregations
</H2>
<P>The nested classes only provide an aggregation of the descriptive
not of the runtime properties of an object. We have pointed out the
difference of object and class in XOTcl. Because of the splitting of a
class into class and class-object it is possible to give each object
its own namespace. The internal implementation of objects enable them
to contain nested objects, which are aggregates of the containing
object. In XOTcl these can be changed dynamically and introspected
through the language support of dynamic object aggregations <a
href="#xotcl-aggregation">[Neumann and Zdun 2000b]</a>. Suppose an
object of the class <TT>Agent</TT> should aggregate some property
objects of an agent, such as head and body:
</P>
<PRE> ClassAgent
Agent myAgent
Class Agent::Head
Class Agent::Body
Agent::Head ::myAgent::myHead
Agent::Body ::myAgent::myBody</PRE><P>
Now the objects <TT>myHead</TT> and <TT>myBody</TT> are part of the
<TT>myAgent</TT> object and they are accessible through a
qualification using ``::'' (or through Tcl's namespace command). But
in the common case they will be accessed, as introduced so far: the
explicit full qualification is not necessary when such variables are
being accessed from within XOTcl methods, since the object changes to
its namespace.
</P>
<P>The information about the part-of relationship of objects can be
obtained exactly the same way as for classes through the info method
interface:
</P>
<PRE><EM> objName info children
objName info parent
</em></PRE>
<H2><A NAME="nest_agg"></A>
Relationship between Class Nesting and Object Aggregation
</H2>
<P>The classes <TT>Head</TT> and <TT>Body</TT> are children of the
<TT>Agent</TT> class. It is likely that all agents, interactive or
not, have properties for head and body. This implies a static or
predetermined relationship between class nesting and object
aggregation. Such predetermination do not exist in XOTcl, but are
simply build, when specifying the relationship in the constructor,
e.g.:
</P>
<PRE> Agent instproc init args {
[self] ::Agent::Head myHead
[self] ::Agent::Body myBody
}</PRE><P>
Now all agents derived from the class have the two property objects
aggregated after creation. But still they are changeable in a
dynamical manner, e.g. with:
</P>
<PRE> Agent myAgent
myAgent::myHead destroy</PRE><P>
The agent turns into a headless agent. In companion of the
introspection mechanisms such constructions could be very useful.
Suppose, that in the virtual world the agents heads may be slashed
from their bodies. The graphical system simply needs to ask with <TT>info
children</TT> on the agent's object, whether it has a head or not and
can choose the appropriate graphical representation.
</P>
<P STYLE="margin-bottom: 0in">Note, that the not existing
relationship means a great deal of freedom and dynamics, which goes
together with the ideas behind OTcl, e.g. like the renunciation of
protection mechanisms. This policy in programming language design
means, on the one hand, ease of programming and more expressiveness,
but, on the other hand, it contains no protection against bad
software architectures or programming style. We believe that no such
mechanisms could hinder the programmer to do silly things, so our
policy was, to give the programmer rather more powerful constructs
then to make decisions in his place.
</P>
<H2><A NAME="copy_move"></A>Copy/Move
</H2>
Often an object has to be copied/moved. This is a very useful
functionality when XOTcl should be used as a prototyping language.
The XOTcl method <TT>move</TT> provides this functionality. Another
common behavior is implemented by the <tt>copy</tt> method which
clones the actual object to a destination object. The two methods have
the syntax:
<PRE STYLE="margin-bottom: 0.2in"><em> objName move destination </em></PRE><P>
<PRE STYLE="margin-bottom: 0.2in"><em> objName copy destination</em></PRE><P>
Copy and move operations work with all object/class information, i.e.,
information on filters, per-object mixins, parameters, etc. are
automatically copied. Copy and move are integrated with class nesting
and object aggregations. All copy/move operations are deep copy
operations: all nested objects/classes are automatically copied/moved,
too.
E.g.\ if we want to reuse an imperial march object of star wars for
star wars 2, we can just copy the object:
<PRE>
starWars::imperialMarch copy starWars2::imperialMarch
</PRE>
<TABLE COLS=2 WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#000055">
<TR>
<TD WIDTH=75%>
<P><A NAME="assertions"></A><FONT COLOR="#ffffff"><FONT FACE="Arial, Helvetica"><FONT SIZE=6>Assertions
</FONT></FONT></FONT>
</P>
</TD>
<TD>
<IMG SRC="logo-100.jpg" NAME="Graphic10" ALIGN=RIGHT WIDTH=102 HEIGHT=42 BORDER=0></TD>
</TR>
</TABLE>
<P>In order to improve reliability and self documentation we added
assertions to XOTcl. The implemented assertions are modeled after the
``design by contract'' concept of Bertrand Meyer. In XOTcl assertions
can be specified in form of formal and informal pre- and
post-conditions for each method. The conditions are defined as a list
of and-combined constraints. The formal conditions have the form of
normal Tcl conditions, while the informal conditions are defined as
comments (specified with a starting ``<TT>#</TT>''). The lists
containing the pre- and post-conditions are appended to the method
definition (see example below).
</P>
<P>Since XOTcl offers per-object specialization it is desirable to
specify conditions within objects as well (this is different to the
concept of Meyer). Furthermore there may be conditions which must be
valid for the whole class or object at any visible state (that means
in every pre- and post-condition). These are called invariants and
may be defined with following syntax for class invariants:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> ClassName instinvar invariantList</em></PRE><P>
or for objects invariants:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> objName invar invariantList</em></PRE><P>
Logically all invariants are appended to the pre- and post-conditions
with a logical ``and''. All assertions can be introspected.
</P>
<P>Since assertions are contracts they need not to be tested if one
can be sure that the contracts are fulfilled by the partners. But for
example when a component has changed or a new one is developed the
assertions could be checked on demand. For this purpose the <TT>check</TT>
method can be used either to test the pre- or the post-conditions.
The syntax is:
</P>
<PRE STYLE="margin-bottom: 0.2in"><em> objName check ?all? ?instinvar? ?invar? ?pre? ?post?</em></PRE><P>
Per default all options are turned off. <TT>check all</TT> turns all
assertion options for an object on, an arbitrary list (maybe empty)
can be used for the selection of certain options. Assertion options
are introspected by the <TT>info check</TT> option. The following
class is equipped with assertions:
</P>
<PRE> Class Sensor -parameter {{value 1}}
Sensor instinvar {
{[regexp {^[0-9]$} [[self] value]] == 1}
}
Sensor instproc incrValue {} {
incr [self]::value
} {
{# pre-condition:}
{[[self] value] > 0}
} {
{# post-condition:}
{[[self] value] > 1}
}</PRE><P>
The <TT>parameter</TT> instance method defines an instance variable
<TT>value</TT> with value <TT>1</TT>. The invariant expresses the
condition (using the Tcl command <TT>regexp</TT>), that the value
must be a single decimal digit. The method definition expresses the
formal contract between the class and its clients that the method
<TT>incrValue</TT> only gets input-states in which the value of the
variable <TT>value</TT> is positive. If this contract is fulfilled by
the client, the class commits itself to supply a post-condition where
the variable's value is larger than 1. The formal conditions are
ordinary Tcl conditions. If checking is turned on for sensor <TT>s</TT>:
</P>
<PRE STYLE="margin-bottom: 0.2in">s check all</PRE><P>
the pre-conditions and invariants are tested at the beginning and the
post-condition and invariants are tested at the end of the method
execution automatically. A broken assertion, like calling <TT>incrValue</TT>
9 times (would break the invariant of being a single digit) results
in an error message.
</P>
<p>
In assertions we do not check methods that modify or introspect
assertions. These are
<tt>check</tt>,<tt>info</tt>,<tt>proc</tt>,<tt>instproc</tt>,<tt>invar</tt>,
and <tt>instinvar</tt>. The reason for this is that we want to be able
to recover a malicious action in a <tt>catch</tt> error handler, like:
</P>
<pre>
...
if {[catch {[self] assertionBreakingAction} errMsg]} {
puts "CATCHED ERROR: $errMsg"
# remeber checking options, for turning them on later again
set check [[self] info check]
[self] check {}
# recover from broken assertion
...
# turning checking on again
$fb check $check
}
</pre>
<TABLE COLS=2 WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#000055">
<TR>
<TD WIDTH=75%>
<P><A NAME="meta-data"></A><FONT
COLOR="#ffffff"><FONT FACE="Arial, Helvetica"><FONT SIZE=6>Meta-Data
and Automatic Documentation
</FONT></FONT></FONT>
</P>
</TD>
<TD>
<IMG SRC="logo-100.jpg" NAME="Graphic11" ALIGN=RIGHT WIDTH=102 HEIGHT=42 BORDER=0></TD>
</TR>
</TABLE>
<P>To enhance the understandability and the consistency between
documentation and program it is useful to have a facility to make the
documentation a part of the program. There are several kinds of
meta-data which are interesting for a class, e.g. the author, a
description, the version, etc.
</P>
<P>
Older versions of XOTcl have contained a special metadata command
<tt>metadata</tt>. This command is now (from version 0.83) deprecated
and replaced by an integrated solution with XOTcl's API documentation
functionality. The object <tt>@</tt> is used for documentation and
metadata issues. Per default it is not evaluated at all. Everything
that is send to <tt>@</tt> is simply ignored. That way we do not waste
memory/performance at runtime, if we do not require to parse the
metadata/documentation.
</P>
<P>
If we have to know the metadata/documentation, as for instance in the
<tt>xoDoc</tt> component and the <tt>makeDoc</tt> tool, that handle
XOTcl's internal documentation, we have to re-define the documentation
object. Alternatively, we can partially parse the source code for
<tt>@</tt> commands.
</P>
<P>
With <tt>@</tt> the metadata/documentation is handled by first class
XOTcl objects. By defining alternate @ implementations - as in
<tt>xoDoc</tt>/<tt>makeDoc</tt> - we can evaluate the
metadata/documentation arbitrarily. <tt>xoDoc</tt>/<tt>makeDoc</tt>
are only an HTML backend, but the basic idea is to provide support for
several other usages as well (e.g. XML, RDF, online help,
documentation of dynamic structures, etc).
</P>
<P>
The object<tt>@</tt> handles comments via its <tt>unknown</tt>
method. <tt>xoDoc</tt> adds the appropriate instprocs to t<tt>@</tt> to produce HTML
output. The appropriate command is:
</P>
<PRE STYLE="margin-bottom: 0.2in">
xotclsh src/lib/makeDoc.xotcl <DOCDIR> <DOCFILES>
</PRE><P>
The source of a documentation is structurally very similar to the
XOTcl constructs being commented. E.g. one can copy an instproc and
add comments at the right places, like:
</P>
<PRE STYLE="margin-bottom: 0.2in">
Class C
C instproc m {a1 a2} {
return [expr {$a1+$a2}]
}
</PRE><P>
can be commented as follows
</P>
<PRE STYLE="margin-bottom: 0.2in">
@ Class C { description { "my sample class"} }
@ C instproc m {a1 "first number" a2 "second number"} {
description "add two numbers"
return "sum of a1 and a2"
}
</PRE></P>
<P>
One can do essentially a copy+paste of the source and add the
comments via attribute value pairs. Every basic language construct
can have a "description". If you want to include other properties to
the description, you can add them like:
</P>
<PRE STYLE="margin-bottom: 0.2in">
@ C instproc m {a1 "first number" a2 "second number"} {
author "GN+UZ"
date "Feb 31"
description "add two numbers"
return "sum of a1 and a2"
}
</PRE><P>
This way, author and date are added automatically to the generated
HTML file.
In addition, there is a <tt>@File</tt> hook for a per file
description, like:
</P>
<PRE STYLE="margin-bottom: 0.2in">
@ @File {
description {
This is a file which provides a regression test
for the features of the XOTcl - Language.
}
}
</PRE><P>
<TABLE COLS=2 WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#000055">
<TR>
<TD WIDTH=75%>
<P><A NAME="additional-functionalities"></A><FONT COLOR="#ffffff"><FONT FACE="Arial, Helvetica"><FONT SIZE=6>Additional
Functionalities </FONT></FONT></FONT>
</P>
</TD>
<TD>
<IMG SRC="logo-100.jpg" NAME="Graphic12" ALIGN=RIGHT WIDTH=102 HEIGHT=42 BORDER=0></TD>
</TR>
</TABLE>
<H2><A NAME="abstract-classes"></A>Abstract Classes
</H2>
<P>In XOTcl a class is defined abstract if at least one method of
this class is abstract. The instance method <TT>abstract</TT> defines
an abstract method and specifies its interface. Direct calls to
abstract methods produce an error message. E.g. a <TT>Storage</TT> class
provides an abstract interface for access to different storage forms:
</P>
<PRE> Class Storage
Storage abstract instproc open {name}
Storage abstract instproc store {key value}
Storage abstract instproc list {}
Storage abstract instproc fetch key
Storage abstract instproc close {}
Storage abstract instproc delete {k} </PRE><P>
All kinds of storage have to implement every method from the
interface. E.g. a GNU Database Access, a relational database access,
and several other storage forms may be derived by sub-classing
(therefore, all conform to the same storage access interface).
</P>
<H2><A NAME="parameter"></A>Parameter
</H2>
<P>Classes may be equipped with <TT>parameter</TT> definitions which
are automatically created for the convenient setting and querying of
instance variables. Parameters may have a default value, e.g.:
</P>
<PRE> Class Car -parameter {
owner
{doors 4}
}</PRE><P>
Each instance of class <TT>Car</TT> gets two instance variables
defined. <TT>owner</TT> has no default value, and <TT>doors</TT>
defaults to 4. E.g. the following defines a new person object with
the two paramters set:
</P>
<PRE STYLE="margin-bottom: 0.2in"> Car mercedes</PRE><P>
Additionally the <TT>parameter</TT> method automatically creates a
new getter/setter instance method for each parameter -- same named to
the parameter, which queries the parameter if it no argument is given
or sets the parameter if with an given argument. E.g. a car with only
two doors can be created by:
</P>
<PRE STYLE="margin-bottom: 0.2in"> Car porsche -doors 2</PRE><P>
The owner of thefirst car is set by:
</P>
<PRE STYLE="margin-bottom: 0.2in"> mercedes owner Marion</PRE><P>
and the doors of the first car can be queried (and printed to the
screen) by:
</P>
<PRE STYLE="margin-bottom: 0.2in"> puts "The mercedes got [mercedes doors] doors and is owned by [mercedes owner]"</PRE><P>
<TT>parameter</TT> are inherited by subclasses. The class
<TT>Class::Parameters</TT> contains the behavior how default values
are stored.
</p>
<p>
The basic idea is to make the parameter mechanism extensible in a
similar way as the extension mechanisms work for normal object-oriented
methods. One can extend the predefined <tt>Class::Parameters</TT> class with
<tt> someInstproc</TT> and use later
</p>
<PRE STYLE="margin-bottom: 0.2in">
C c1 {{a -default 1 -someInstproc x} ...}
</pre>
<p>
or subclcass it like:
</p>
<PRE>
Class MyParameters -superclass Class::Parameters
Class X -parameterclass MyParameters -parameters ...
</pre>
<p>
Upon object initialization, the parameters are firstly evaluated for all
mixins and then for the class hierarchy. E.g. in the following
example:
</p>
<PRE STYLE="margin-bottom: 0.2in">
Class A -parameter {
{pcm 1}
}
Class B -instmixin A -parameter {
{cl 4}
}
B b
</pre>
<p>
at first the mixin parameter 'pcm' is set, then the class parameter 'cl'.
However, since parameters are applied before the '-' methods, the
per-object mixin parameter defaults in the following example are not
set by the standard initialization routine:
</p>
<PRE STYLE="margin-bottom: 0.2in">
Class C -parameter {
{pom 1}
}
B b -mixin C
</pre>
<p>
If this parameter or any other parameter default, which is introduced
later than the standard initialization routine, is required, then we
can evaluate the parameter defaults manually, like:
</p>
<PRE STYLE="margin-bottom: 0.2in">
[B info parameterclass] searchDefaults b
</pre>
<p>
This searches all default values for the object b which are defined on
mixins or on the class hierarchy.
</P>
<H2><A NAME="cmdCheck"></A>Checking Commands for being Objects,
Classes, or Meta-Classes
</H2>
<P>Since XOTcl is a hybrid language containing several Tcl commands,
sometimes its necessary for applications to distinguish between Tcl
commands and object commands for XOTcl. </TT>method of the
<TT>Object</TT> class looks up an <TT>objName</TT> and returns 1 if it
is an object and 0 if not:
<PRE STYLE="margin-bottom: 0.2in"><em> anyXOTclObject isobject objName</em></PRE><P>
If one can be sure that a command represents an
object, it might be unsure if the command is only an object or also
class or even meta-class. The two instance methods <TT>isClass</TT>
and <TT>isMetaClass</TT> check in the same manner, whether a class or
meta-class is given (since ever XOTcl class is an object, they also
return 0, when objName is not an XOTcl object).
<PRE><em>
anyXOTclObject isclass objName
anyXOTclObject ismetaclass objName
</em></PRE>
<H2>
<A NAME="Exit Handler"></A>Exit Handler
</H2>
<P>A task for a programming language, sometimes of similar importance
as object creation, is the object destruction. XOTcl ensures that all
objects are destroyed and their destructors are invoked when XOTcl
applications terminate. For that reason objects and classes are
destroyed in the order objects, classes, meta-classes. Sometimes
further destruction order is of importance. For these cases, the
XOTcl language provides an exit handler, which is a user-defined
proc, which invokes user-defined exit handling just before the
destruction of objects, classes, meta-classes is invoked. E.g. the
exit handler lets the user specify objects which have to be destroyed
before all other objects. There are three object-specific proc of the
<TT>Object</TT> class pre-defined, which let us specify an exit
handler conviniently.
</P>
<PRE> Object setExitHandler body
Object getExitHandler
Object unsetExitHandler
</PRE><P STYLE="margin-bottom: 0in">
<FONT FACE="courier, monospace">setExitHandler </FONT>lets us specify
a proc body that actually contains the user-defined exit handling.
E.g.
</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<PRE> Object setExitHandler {
aObj destroy
puts "existing"
}
</PRE><P STYLE="margin-bottom: 0in">
destroys the object <FONT FACE="courier, monospace">aObj </FONT>before
all other objects and prints the message existing to the screen. With
<FONT FACE="courier, monospace">getExitHandler</FONT> the exit
handler can be introspected. E.g. if we just want to append the
destruction of object <FONT FACE="courier, monospace">bObj </FONT>to
an exisiting exit handler, we use <FONT FACE="courier, monospace">getExitHandler</FONT>:
</P>
<P STYLE="margin-bottom: 0in"><BR>
</P>
<PRE> Object setExitHandler "[Object getExitHandler]; bObj destroy"
</PRE><P STYLE="margin-bottom: 0in">
<FONT FACE="courier, monospace">unsetExitHandler</FONT> deletetes the
exit handler (and resets it to "").
</P>
<PRE>
Object proc getExitHandler {} {
if {[info exists [self]::__exitHandler]} {
return [[self] set __exitHandler]
} else {
return ""
}
}
Object proc setExitHandler body {
return [[self] set __exitHandler $body]
}
Object proc unsetExitHandler {} {
[self] unset __exitHandler
}</PRE>
<PRE STYLE="margin-top: 0.17in; margin-bottom: 0.2in; page-break-after: avoid"><H2><A NAME="autonames">Automatic Name Creation</A>
</H2></PRE>
The <FONT SIZE=2>XOT<FONT SIZE=1>CL</FONT></FONT> <FONT SIZE=2>autoname</FONT>
instance method provides an simple way to take the task of
automatically creating names out of the responsibility of the
programmer. The example below show how to create on each invocation
of method <FONT SIZE=2>new</FONT> an agent with a fresh name
(prefixed with <FONT SIZE=2>agent</FONT>):
</P>
<PRE>
Agent proc new args {
eval [self] [[self] autoname agent] $args
}
</PRE>
<p>
Autonames may have format strings as in the Tcl 'format' command.
E.g.:
</P>
<pre>[self] autoname a%06d</pre>
<p>
produces a000000, a000001, a000002, ...
</P>
<TABLE COLS=2 WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR="#000055">
<TR>
<TD WIDTH=75%>
<P><A NAME="references"></A><FONT COLOR="#ffffff"><FONT FACE="Arial, Helvetica"><FONT SIZE=6>References
</FONT></FONT></FONT>
</P>
</TD>
<TD>
<IMG SRC="logo-100.jpg" NAME="Graphic2" ALIGN=RIGHT WIDTH=102 HEIGHT=42 BORDER=0></TD>
</TR>
</TABLE>
<P></P><A NAME="xotcl-filter"><STRONG>[Neumann and Zdun 1999a]</STRONG></A>
G. Neumann and U. Zdun.
Filters as a language support for design patterns in object-oriented
scripting languages.
In <EM>Proceedings of COOTS'99, 5th Conference on Object-Oriented
Technologies and Systems</EM>, San Diego, May 1999.
<P></P><A NAME="xotcl-objpattern"><STRONG>[Neumann and Zdun 1999b]</STRONG></A>
G. Neumann and U. Zdun.
Implementing object-specific design patterns using per-object mixins.
In <EM>Proc. of NOSA`99, Second Nordic Workshop on Software
Architecture</EM>, Ronneby, Sweden, August 1999.
<P></P><A NAME="xotcl-mixin"><STRONG>[Neumann and Zdun 1999c]</STRONG></A>
G. Neumann and U. Zdun.
Enhancing object-based system composition through per-object mixins.
In <EM>Proceedings of Asia-Pacific Software Engineering Conference
(APSEC)</EM>, Takamatsu, Japan, December 1999.
<P></P><A NAME="xotcl"><STRONG>[Neumann and Zdun 2000a]</STRONG></A>
G. Neumann and U. Zdun.
XOT<SMALL>CL</SMALL>, an object-oriented scripting language.
In <EM>Proceedings of Tcl2k: The 7th USENIX Tcl/Tk Conference</EM>,
Austin, Texas, February 2000.
<P></P><A NAME="xotcl-aggregation"><STRONG>[Neumann and Zdun 2000b]</STRONG></A>
G. Neumann and U. Zdun. Towards the Usage of Dynamic Object
Aggregations as a Form of Composition
In: <EM>Proceedings of Symposium of Applied Computing (SAC'00)</EM>, Como, Italy, Mar 19-21, 2000.
<P></P><A NAME="tcl"><STRONG>[Ousterhout 1990]</STRONG></A>
J. K. Ousterhout.
Tcl: An embeddable command language.
In <EM>Proc. of the 1990 Winter USENIX Conference</EM>, January 1990.
<P></P><A NAME="ousterhout"><STRONG>[Ousterhout 1998]</STRONG></A>
J. K. Ousterhout.
Scripting: Higher Level Programming for the 21st Century, IEEE Computer 31(3), March 1998.
<P></P><A NAME="otcl"><STRONG>[Wetherall and Lindblad 1995]</STRONG></A>
D. Wetherall and C. J. Lindblad. Extending Tcl for Dynamic
Object-Oriented Programming. Proc. of the Tcl/Tk Workshop '95, July 1995.
</BODY>
</HTML>
|