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

  1.            Chapter 11   Filing Around
  2.  
  3.  
  4.  
  5.  
  6. Up to this point, you've seen how CLIPS can work with facts that have been 
  7. entered from the keyboard or rules.  However, in some cases you may 
  8.  wish to read or write facts to disk.  In this chapter, you will learn how to 
  9. use CLIPS for file I/O of facts.
  10.  
  11.  
  12. Opening Things Up
  13.  
  14.  
  15. Before a file can be accessed for reading or writing, it must be opened using 
  16. the open function.  The number of files that can be opened at once 
  17.  is dependent on your operating system and hardware.
  18.    The general form of the open function is
  19.  
  20. (call (open "<file-name>" <file-ID> ["<file-access>"]))
  21.  
  22. As an example,
  23.  
  24. (call (open "animals.dat" animals "r"))
  25.  
  26.    The open function is called with a call function because you really want the 
  27. side-effect of opening a file rather than the value returned 
  28.  by the open function.
  29.    The first argument of open, "<file-name>", is the name of the file on your 
  30. computer.  Generally, this will be the physical file name unless 
  31.  you've re-assigned the name using an operating system command.  For the 
  32. example shown, the file-name is animals.dat.
  33.    The second argument, <file-ID>, is the logical name under which CLIPS knows 
  34. the file.  Although the logical could be the same as the file-name, 
  35.  you may want to rename it.  For example, if the file-name is animals.dat, you 
  36. may want to rename it without the period so that you don't have 
  37.  to keep typing double quotes around the name everywhere you use it in CLIPS.  
  38. So you might rename animals.dat as animals or by a different name 
  39.  such as data, my-data, input, or any other valid name.
  40.    Another advantage of a logical name is that you can easily substitute a 
  41. different file-name without making major changes in the program.  
  42.  For example, you may want to read data from animals.dat for awhile and then 
  43. read data from a file called birds.txt.  Only the open statement 
  44.  need be changed to accomplish this.
  45.    The third argument, "<file-access>", is optional as indicated by the square 
  46. brackets around it.  The four possible modes of file-access are
  47.  
  48.       Mode          Action
  49.        r       read access only
  50.        w       write access only
  51.        r+      read and write access
  52.        a       append access only
  53.  
  54. The default value of file-access is "r".          
  55.    As an example of the open function,
  56.  
  57. (call (open "animals.dat" data "r"))
  58. will open a file called "animals.dat" and assign it the logical name data.  
  59. Since the file-mode is "r", an alternative statement is
  60.  
  61. (call (open "animals.dat" data))
  62.  
  63. Either version of the open statement can be used.
  64.  
  65.  
  66. $$$And Closing Them Down
  67.  
  68.  
  69. Once you no longer need to access a file, you should close it down.  Unless a 
  70. file is closed, there is no guarantee that the information written 
  71.  to it will be saved.
  72.    Although you can open a file when the first rule fires and close it when the 
  73. last rule fires, you're taking an unneccesary risk if you don't 
  74.  need to access the data all the time.  There may be a power outage or hardware 
  75. failure that prevents the file from closing and so the data you've 
  76.  written to it may be lost.  The disadvantage of opening and closing files in a 
  77. rule is that execution is slower.
  78.    The general form of the close function is
  79.  
  80. (call (close [<file-ID>]))
  81.  
  82. where the optional argument specifies the file to be closed.  So 
  83.  
  84. (call (close data))
  85.  
  86. will close the file known to CLIPS by the logical file-name animals.  The 
  87. statements
  88.  
  89. (call (close input))
  90. (call (close output))
  91.  
  92. will close the logical files named input and output.  Note that separate 
  93. statements are necessary to close specific files.
  94.    The default case in which no file-ID is specified
  95.  
  96. (call (close))
  97.  
  98. will close all open files.
  99.    Let's try an example now of opening and closing files.  First, we need to 
  100. create the files to be accessed.  Create one file called "animals.dat" 
  101.  with the data shown below.
  102.  
  103. duck goose   ganders
  104. geese
  105. ducks
  106.  
  107. Now make a file called "vegtable.dat".  You don't have to put any data in this 
  108. file.  Notice that the filename is limited to eight characters 
  109.  because of limitations on MSDOS filenames.  Of course, you can also use any 
  110. filenames that are valid on the system you are using.  Just modify 
  111.  the names in the following program to open and close the appropriate files.
  112.  
  113. (defrule read-file
  114.    (initial-fact)
  115. =>
  116.    (printout "Call 1" crlf)
  117.    (call (open "animals.dat" data1 ))
  118.    (printout "Call 2" crlf)
  119.    (call (open "vegtable.dat" data2))
  120.    (printout "Call close" crlf)
  121.    (call (close))
  122.    (printout "Files closed" crlf))
  123.  
  124. When you run this program, CLIPS will print the appropriate messages as it 
  125. opens and closes files.
  126.  
  127.  
  128. A Graceful Exit
  129.  
  130.  
  131. Although the previous program did nothing but open and close files, it was 
  132. important to remind you of the necessity to have both an open and 
  133.  close statement.  Before you write practical programs that read and write to 
  134. files, you should be very aware of these requirements.  In particular, 
  135.  if you do not issue a command to close a file, the data you have written to it 
  136. may be lost.
  137.    CLIPS will not prompt you to close an open file.  The only safeguard built 
  138. into CLIPS for closing files that have been inadvertantly left 
  139.  open is when you issue an (exit) command.  CLIPS will close all open files 
  140. when an (exit) is issued.
  141.    However, a problem arises if you terminate CLIPS with an interrupt control 
  142. character such as Control C or the appropriate interrupt for your 
  143.  computer.  Depending on your operating system, the data you have written to 
  144. open files may not be saved.  That's why it's always a good idea 
  145.  to exit CLIPS with an (exit) rather than an interrupt.  A graceful exit will 
  146. save any data in files inadvertantly left open.
  147.  
  148.  
  149. Well, Read My File
  150.  
  151.  
  152. Let's take a look now at reading data from a file.  The function to read data 
  153. from a file is the familiar (read).  The only new thing that you 
  154.  have to do is specify the opened file to read from as the argument of (read).
  155.    The following program shows how you can read data from a file.
  156.  
  157. (defrule read-file
  158.    (initial-fact)
  159. =>
  160.    (printout "Name of file to read ? " crlf)
  161.    (bind ?name (read))
  162.    (call (open ?name data))
  163.    (bind ?input (read data))
  164.    (printout "Data read from file " ?name " is " ?input crlf)
  165.    (assert (data ?input))
  166.    (call (close))
  167.    (printout "File " ?name " is closed" crlf))
  168.  
  169.    This program shows a general way to read from a file.  Instead of 
  170. hard-coding the file name into the program, you can bind the name of the 
  171.  file to be opened.  Also, notice that the default form of open is used because 
  172. the file is opened for reading only.
  173.    Run the program to read data from the "animals.dat" file.  Be sure to input 
  174. the double quotes around "animals.dat" since CLIPS will not accept 
  175.  the period as part of a valid name.
  176.    The output from the program will be the first atom stored in the file, duck. 
  177.  Just as in inputting data from a keyboard, CLIPS will read one 
  178.  atom at a time.  Since a space or carriage return acts as the terminator of an 
  179. atom, the (read data) will stop reading after the first atom, 
  180.  duck, is read.
  181.  
  182.  
  183. Give Me More
  184.  
  185.  
  186. In order to read more than one atom, you need to set up a loop.  This loop can 
  187. be written by having one rule trigger another.  Another way is 
  188.  to use a while-loop.
  189.    However, before multiple values can be read in by a loop, a very important 
  190. question needs to be addressed.  The important question is how 
  191.  do you stop the loop?  The loop should not try to read past the end of file or 
  192. the operating system will issue an error message.  In order to 
  193.  prevent this, CLIPS returns a EOF symbolic atom if you try to read past the 
  194. end of file.
  195.    The following program illustrates one way of reading in multiple values from 
  196. a file.
  197.  
  198. (defrule read-file
  199.    (initial-fact)
  200. =>
  201.    (printout "Name of file to read ? " crlf)
  202.    (bind ?name (read))
  203.    (call (open ?name data))
  204.    (bind ?input (read data))
  205.    (while (!(eq ?input EOF))
  206.       (printout ?input crlf)
  207.       (bind ?input (read data)))
  208.    (call (close))
  209.    (printout "*** File " ?name " is closed ***" crlf))
  210.  
  211. Enter and run this program on the file "animals.dat".  You'll see that it does 
  212. read and printout all the data in the file.
  213.  
  214.  
  215. Printing To Files
  216.  
  217.  
  218. Besides reading from files, CLIPS allows printing to files with the fprintout 
  219. function.  The first argument of (fprintout) is the logical filename. 
  220.   Following this is the data to be printed to the file.  The following program 
  221. shows an example of printing to a file.
  222.  
  223. (defrule print-to-file
  224.    (initial-fact)
  225. =>
  226.    (printout "Name of file to print to ? " crlf)
  227.    (bind ?name (read))
  228.    (call (open ?name data "w"))
  229.    (printout crlf "*** Enter data to file " ?name
  230.              crlf "    Use EOF to stop input ***" crlf)
  231.    (bind ?input (read))
  232.    (while (!(eq ?input EOF))
  233.       (fprintout data ?input crlf)
  234.       (bind ?input (read)))
  235.    (call (close))
  236.    (printout "*** File " ?name " is closed ***" crlf))
  237.  
  238.   Unlike (read), which reads only one atom at a time, you can use (fprintout) 
  239. with as many atoms as you want.  For example,
  240.  
  241. (fprintout data "This is being printed to a file" ?input crlf)
  242.  
  243. will print all the atoms after data to the file.
  244.  
  245.  
  246. A Standard Change
  247.  
  248.  
  249. If the first argument of (fprintout) is the special logical filename "t", then 
  250. output will be sent to the standard output device.  For example, 
  251.  in the previous program, change the (fprintout) action in the while-loop to 
  252.  
  253. (fprintout t ?input crlf) 
  254.  
  255. and run again.
  256.    Instead of printing to a file, the output will go to the standard output 
  257. device.  Usually, the standard output device will be the terminal 
  258.  screen and so you'll see the output of the (fprintout) on your screen.  
  259. However, depending on your operating system, you may be able to issue 
  260.  an operating system command to change the standard output to another device 
  261. such as a printer or modem port.  In that case, the output of (fprintout) 
  262.  will go to your printer or modem.
  263.    The (read) function also has a "t" option so that you can read from the 
  264. standard input device.  Normally, the standard input device is the 
  265.  keyboard, but your operating system may allow the standard input to be 
  266. redefined just like the standard output.  For example, you may be able 
  267.  to redefine the standard input as a modem port or lightpen port.
  268.  
  269.  
  270.