Handling terms in text files

The readterm predicate makes it possible to access facts in a file. readterm can read any object written by the write predicate and takes the form

    readterm(<name>,TermParam).

where <name> is the name of a domain. The following code excerpt shows how readterm might be used.

PREDICATES

    person(name, addr)

    moredata(file)

 

CLAUSES

    person(Name,Addr) :-

        openread(file_of_data_records, "dd.dat"),

        readdevice(file_of_data_records),

        moredata(file_of_data_records),

        readterm(one_data_record, p(Name, Addr)).

        moredata(_).

        moredata(File) :-

        not(eof(File)),

        moredata(File).

If the file DD.DAT contains facts belonging to the one_data_record domain, such as

    p("Peter","28th Street")
    p("Curt","Wall Street")

the following is an example of a dialog that retrieves information from that file:

    Goal person("Peter",Address).

    Address="28th Street"
    1 Solution

    Goal person("Peter","Not an address").

    no

1. Manipulating Facts Like Terms

Facts that describe database predicates can also be manipulated as though they were terms. This is made possible by the dbasedom domain (which is automatically declared by the Visual Prolog system) or any user-defined database domain name.

Visual Prolog generates one alternative for each predicate in the database. It describes each database predicate by a functor and by the domains of the arguments in that predicate. For example, given this database declaration:

    DATABASE
        person(name, telno)
        city(cno, cname)

Visual Prolog generates the corresponding dbasedom domain:

    DOMAINS
        dbasedom = person(name, telno) ; city(cno, cname)

A named database similarly generates a domain corresponding to the database name, as in the following example:

    DATABASE - test
        person(name, telno)
        city(cno, cname)

generates the domain

    DOMAINS
        test = person(name, telno) ; city(cno, cname)

These domains can be used like any other predefined domain.

(1) Example

The following example shows how you could construct a predicate my_consult, similar to the standard predicate consult.

 

DOMAINS

    file = dbase

 

DATABASE - dba1

    /*  ... Declare database predicates to be read from file */

 

PREDICATES

    my_consult(string)

    repfile(file)

 

CLAUSES

    my_consult(FileName) :-

openread(dbase, FileName),

readdevice(dbase),

repfile(dbase),

readterm(dba1, Term),

assertz(Term),

fail.

    my_consult(_) :- eof(dbase).

    repfile(_).

    repfile(F):-not(eof(F)),repfile(F).

If, for example, the database program section contains the declaration

    p(string, string)

and a file called DD.DAT exists (with contents as described on page 329), you could obtain the following dialog:

Summary

These are the important points covered in this chapter:

1. Visual Prolog Provides three standard predicates for basic writing:

    a. write (for plain output)

    b. writef (for output formatted according to format specifiers)

    c. nl (for generating new lines)

2. Visual Prolog basic standard predicates for reading are

    a. readln (for reading whole lines of characters)

    b. readint, readreal, and readchar (for reading integers, reals, and characters, respectively)

    c. readterm (for reading compound objects)

    d. file_str (for reading a whole text file into a string)

3. additionally, binary blocks may be transferred using

    a. readblock (reads a binary block from a file)

    b. writeblock (writes a binary block to a file)

    c. file_bin transfers between whole files and binary blocks

4. Visual Prolog uses a current_read_device (normally the keyboard) for reading input, and a current_write_device (normally the screen) for sending output. You can specify other devices, and can reassign the current input and output devices at run time. (This reassigning is known as redirecting I/O.)

5. Visual Prolog's basic file-access predicates are:

    a. openread (open a file for reading)

    b. openwrite (open a file for writing)

    c. openappend (open a file for appending)

    d. openmodify (open a file for modification)

    e. openfile (general purpose file opener)

    f. filemode (set a file to text mode or binary mode)

    g. closefile (close a file)

    h. readdevice (ressign the current_read_device or get its name)

    i. writedevice (ressign the current_write_device or get its name)

6. To access files you use the FILE domain, which has five predefined alternatives:

    a. keyboard     (for reading from the keyboard)

    b. screen        (for writing to the screen)

    c. stdin          (for reading from standard input)

    d. stdout        (for writing to standard output)

    e. stderr         (for writing to standard error)

7. To work within and manipulate files, you use the following standard predicates:

    a. filepos (controls the position where reading or writing takes place)

    b. eof (checks whether the file position during a read operation is at the end of the file)

    c. flush (forces the contents of the internal buffer to be written to a file)

    d. existfile (verifies that a file exists)

    e. searchfile (locates a file among several directories)

    f. deletefile (deletes a file)

    g. renamefile (renames a file)

    h. copyfile (copies a file)

    i. fileattrib (gets or sets file attributes)

    j. disk (changes the current disk and directory/subdirectory)

8. To search directories, the following standard predicates are available:

    a. diropen (opens a directory for searching)

    b. dirmatch (finds matching files in a directory)

    c. dirclose (closes a directory)

    d. dirfiles (non-deterministically matches files in a directory)

9. To manipulate file names, the following are available:

    a. filenamepath (joins or splits qualified file names)

    b. filenameext (joins or splits file names and extensions)

10. The standard predicate readterm allows your program to access facts in a file at run time. readterm can read any object written by write, plus facts describing database predicates.