home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / compiler / clips / clip_doc / clips12.man < prev    next >
Encoding:
Text File  |  1993-09-01  |  9.2 KB  |  223 lines

  1.            Chapter 12   The Great Expansion
  2.  
  3.  
  4.  
  5.  
  6. In the preceeding chapters of this book, you've learned the syntax of CLIPS.  
  7. However, CLIPS has an additional feature that greatly extends its 
  8.  capabilities by allowing you to define your own functions.  If you are an 
  9. experienced C programmer, you can learn from this chapter how to make 
  10.  your own customized version of CLIPS.
  11.  
  12.  
  13. Random Problems
  14.  
  15.  
  16. In order to make your own user defined functions, you should have knowledge of 
  17. C, since CLIPS is written in C.  The following description of 
  18.  how to add a function assumes you know the C language and how to compile 
  19. programs in C.  The description is for an implementation on an IBM 
  20.  personal computer using the Lattice C compiler, and also for a UNIX based 
  21. system.  However, it should be understandable for other C implementations 
  22.  also.
  23.    If you are using an IBM personal computer, you will probably not be able to 
  24. compile CLIPS on a floppy disk system because the object code 
  25.  files are so large.  Even using RAM disks and a 640 K machine will still give 
  26. trouble in compiling.  A hard disk is really necessary.
  27.    The user defined function to be added to CLIPS is a random number generator. 
  28.  There are many cases in which a random number generator is of 
  29.  use.  Since a random number generator is not provided in the CLIPS syntax or 
  30. in the math library, let's write a user defined function and add 
  31.  it to CLIPS.
  32.    As a first step in adding the random function, create a file called rand.c 
  33. or a name appropriate to your system.  The random number generator 
  34.  used will be the simple random number function provided by the Lattice C 
  35. compiler, rand.
  36.  
  37. #include <math.h>
  38. #include <stdio.h>
  39. #include "clips.h"
  40.  
  41. float my_rand()
  42.    {
  43.       extern int rand();
  44.       return ((float) rand());
  45.    }
  46.  
  47.    The name "my_rand()" can be any name you want.  The rand function must be 
  48. declared extern because it is not defined in the rand.c file.  Normally, 
  49.  the rand function returns integers.  But since CLIPS only uses floating point 
  50. numbers, it is cast as a float in the return statement.
  51.    The second step in adding the random function is to modify the main.c file.  
  52. The statements which have been modified are shown with comments 
  53.  that include an "!" as in "/***!" in the USRFUNCS() section at the end.
  54.  
  55. /*  CLIPS: main.c (V3.01) Last modified 7/21/86   */
  56.  
  57. #include <stdio.h>
  58.  
  59. /***************************************************************/
  60. /* MAIN: Start execution of CLIPS.  This function must be      */
  61. /*   redefined in order to embed CLIPS within another program. */
  62. /*   Example of redefined main:                                */
  63. /*     main()                                                  */
  64. /*       {                                                     */
  65. /*        init_clips();                                        */
  66. /*            .                                                */
  67. /*            .                                                */
  68. /*            .                                                */
  69. /*        process_data();                                      */
  70. /*        run(-1);                                             */
  71. /*        evaluate_data();                                     */
  72. /*            .                                                */
  73. /*            .                                                */
  74. /*            .                                                */
  75. /*        final_results();                                     */
  76. /*       }                                                     */
  77. /***************************************************************/
  78. main()
  79.   {   
  80.    printf("         CLIPS (V3.01 7/21/86)\n");
  81.    init_clips();   
  82.    command_loop();     
  83.   }
  84.  
  85. /*************************************************************/
  86. /* USRFUNCS:  The function which informs CLIPS of any user   */
  87. /*   defined functions.  In the default case, there are no   */
  88. /*   user defined functions.  To define functions, either    */
  89. /*   this function must be replaced by a function with the   */
  90. /*   same name within this file, or this function can be     */
  91. /*   deleted from this file and included in another file.    */
  92. /*   User defined functions may be included in this file or  */
  93. /*   other files.                                            */
  94. /*   Example of redefined usrfctns:                          */
  95. /*     usrfuncs()                                            */
  96. /*       {                                                   */
  97. /*        define_function("fun1",'i',fun1);                  */
  98. /*        define_function("other",'f',other);                */
  99. /*       }                                                   */
  100. /*************************************************************/
  101.      
  102.  /***! THESE STATEMENTS ARE ADDED FOR THE RANDOM FUNCTION !***/
  103.  
  104. usrfuncs()
  105.   {
  106.      extern mathfctns();  /***! includes the math library !***/
  107.      extern float my_rand();    /***! add random function !***/
  108.  
  109.      mathfctns();         /***! includes the math library !***/
  110.  
  111.      define_function("rand",'f',my_rand); /***! add random !***/
  112.   }
  113.  
  114.    The above changes also add the math library functions called mathfctns().  
  115. Strictly speaking, it's not necessary to add the math library in 
  116.  this example for the random function.  However, it's most likely you'll want 
  117. to use both the math library and the random function generator 
  118.  and so both are shown.
  119.  
  120.  
  121. Lotta Links
  122.  
  123.  
  124. Once you have these two files created, it's time to compile them.  On a UNIX 
  125. system, enter
  126.  
  127. cc -c rand.c
  128.  
  129.    To compile the rand.c file on a Lattice C compiler, you will have to enter a 
  130. command specific to the directory storage of your files.  The 
  131.  general form of the command is
  132.  
  133. lcl =4096 rand.c -ml -n
  134.                    -i { Lattice include files }
  135.                    -i { CLIPS include files }
  136.  
  137.    where 
  138.       4096 increases the stack size for the compiler
  139.       -ml is for the large memory model option
  140.       -n allows long variable names, greater than nine characters
  141.  
  142.  
  143.    Depending on your directory structure, the Lattice include files "i option" 
  144. may be
  145.  
  146. -ic:\lattice\includes\
  147.  
  148. if they are on drive c: in the directory \lattice\includes\.
  149.  
  150.    Likewise, if the CLIPS include files are on drive c: in the directory 
  151. \clips\source\, then the "i option" command is
  152.  
  153. -ic:\clips\source\
  154.    
  155. Compile the main.c file in the same manner.   The output of the compiler is an 
  156. object code file which is then linked to the other CLIPS object 
  157.  files.  Note that it is not neccessary to re-compile the other object files 
  158. such as parser.o, clips.o, and so forth since they have not been 
  159.  changed. 
  160.    In a UNIX system, you should first copy all object versions of CLIPS files 
  161. to your directory.  This is a precaustion in case your linker cannot 
  162.  follow the path names to object files in other directories.  The UNIX command 
  163. to link the object modules is
  164.  
  165. ld -o myclips rand,clips,main,math,sysdep,npsr,parser,usrint
  166.  
  167. where myclips is the name of your customized version of CLIPS with the random 
  168. function.
  169.    The command for the linking with the Lattice C compiler will depend on the 
  170. directories your object files are stored in.  The general form 
  171.  of the link command is
  172.  
  173. link { Lattice large memory link originator }
  174.  
  175.    The files to be linked are
  176.  
  177. clips.o,rand.o,main.o,math.o,sysdep.o,npsr.o,
  178. parser.o,usrint.o,myclips,null,
  179.  
  180. where myclips is the name of your customized version of CLIPS, and null is an 
  181. option to the linker which says not to create a memory map.  Note 
  182.  that the comma after the null is required.  If you do not specify a name for 
  183. the executable code, for example myclips, the linker will use the 
  184.  first argument, clips, as the name.
  185.    In addition, you will also need to link the
  186.  
  187. { Lattice large memory math library }
  188.                     +
  189. { Lattice large memory function library }
  190.  
  191. The output should be the new CLIPS with the user defined random function.
  192.  
  193.  
  194. A Good Execution
  195.  
  196.  
  197. To test out the random function, start up CLIPS and enter this test program.
  198.  
  199. (defrule random-test
  200.    (initial-fact)
  201. =>
  202.    (bind ?count 0)
  203.    (while (< ?count 10)
  204.       (bind ?rnd (rand))  ; call the random number function, rand
  205.       (printout ?rnd crlf)
  206.       (bind ?count (+ ?count 1))))
  207.  
  208. When you run this program, it will generate 10 random numbers.
  209.    Notice that a separate file was created for the random number function.  
  210. Although you could add the appropriate statements to the math library 
  211.  file, math.c, it is definitely not a good idea.  As new releases of CLIPS are 
  212. given out, the math.c file will be updated and so you would have 
  213.  to modify it again.  There is also the danger of your having a non-standard 
  214. version of the math library.  It is preferable to keep your functions 
  215.  in a separate personal math library file.
  216.    The math.c file has some routines for testing the number of arguments to a 
  217. function that you may find useful if you add functions with arguments 
  218.  to your personal math library.  Note that the random function does not use 
  219. arguments and so no testing is really neccessary.  However, you may 
  220.  want to include argument testing in other functions.
  221.  
  222.  
  223.