A straight-forward class of applications for the CGI-Amzi! interface is intelligent form editors. Prolog code can be used to analyze user input from forms and then, based on that analysis, generate either responses or other HTML user input elements to gather or correct missing or wrong information.
Expert systems can be implemented that dynamically generate HTML forms to query the user, and, based on user answers, either generate more questions or provide answers in HTML to the user.
Expert systems are just one form of intelligent interaction enabled by the CGI-Amzi! interface. Others include intelligent tutorials, natural language processing applications, database or other information retrieval, and, of course, interactive games.
WebLS is another Amzi! product that is an example of the type of application you can implement using the CGI-Amzi! interface. WebLS is a custom rule engine that uses a simple rule language to let webmasters add expertise to their Web pages. The rule engine is written in Prolog and uses HTML specified in WebLS question and answer fields to communicate with the user.
The executable file that the server calls is written in C, and is provided with full source code. That program starts the Amzi! Logic Server and loads the Prolog CGI script. Before calling the main entry point of the Prolog script, the CGI executable program first gathers information from the HTML form and asserts that information to the Prolog dynamic database. This step is what allows the Prolog code to easily reason over the information entered by the user in the incoming HTML form.
The CGI executable than calls the Prolog portion of the CGI-Amzi! interface. This is a Prolog wrapper that provides many of the bookkeeping functions necessary for CGI. That Prolog program then calls predicates that are defined in the user-written Prolog script. From this point, the user Prolog program is processing the CGI data.
The Prolog program communicates back with the CGI interface through a number of extended predicates that are implemented in both the Prolog wrapper and C executable program.
Hello CGI is the simplist CGI program. It simply displays a greeting. The source code is in samples/internet/cgi/hello.
Installing CGI programs can be complex, and depends on the web server you are using. Regardless of the web server, the following tasks must be completed:
To help in debugging CGI scripts, you will want to consult the error log files for your server. Following are two sample set-ups.
Copy the .xpl file, the .exe (copied from acgi.exe), amzi.dll and amzi.cfg in the cgi-bin directory.
Copy the .xpl file and acgi (renamed) to the /apache/cgi-bin directory. Use Apache directives to tell it where to find libamzi.so or copy libamzi.so to /lib. Use the Apache setEnv or passEnv directives to set the value of AMZI_DIR and put your amzi.cfg file in /amzi/config.
The main entry point looks at the CGI variable request_method, which was asserted by the CGI executable shell, and calls processMethod with its value. Usually a 'get' request is used when the user wants to receive a form for the first time, and a 'post' request is used to submit information. You can control which type of request is used by setting 'method=' in your <FORM ...> definition.
cgiMain :- cgi(request_method, RM), processMethod(RM). cgiMain :- throw($cgiMain failed\n$).The predicate processMethod/1 calls various helper predicates to check the information on the form and either thanks the user or gives them the opportunity to edit and fix the inputs. It generates HTML for the output form using the extended predicate cgiSend/1.
% % For the initial get, simply return our HTML form % processMethod('GET') :- cgiSend($Content-type: text/html$), cgiSend($$), cgiSendLocalHTMLFile('infoform.htm').
% % After the user has filled in the form, we need to check it, then % send a response back. % processMethod('POST') :- sendHeader, checkFacts, writeRequestLog, cgiSend($Thank you! Your information request has been successfully submitted!$), cgiSend($<P>Return to <A HREF="/index.html">Amzi!'s home page</A>.$), sendFooter. processMethod('POST') :- cgiSend($<P>Press the 'Back' or '<-' button on your browser to change your form and resubmit it.$), sendFooter.Here is one of the checking predicates. It looks at the various fact/2 clauses that were asserted in the dynamic database by the CGI executable shell. Each fact represents an input field that was filled in on the original HTML form. In this case it sees if the user has requested a catalog or newsletters. If not,then there is no potential problem. If so, then make sure there is information entered for the three address fields.
checkAddress :- fact(request, RL), not(member(catalog, RL)), not(member(newsletters, RL)), !. checkAddress :- fact(address1, A1), fact(city, C1), fact(country, C2), !. checkAddress :- cgiSend($Please fill in your mailing address including street, city, region and country.$), fail, !.
The CGI interface consists of two parts. First, there is a Prolog library which provides the basic framework for the script and many supporting functions. Second, there is a C program that invokes the script framework in the Prolog library and also provides some additional supporting functions. The files are:
As currently written, the C program loads an .xpl file with the same name. For example, acgi.exe will load acgi.xpl.
The field names and values are Prolog atoms.fact(field_name, value)
You script can also access the values of many CGI variables. The Amzi! implementation uses lowercase versions of the standard CGI names (replacing spaces with underscores) and create predicates of arity 2 representing each value. For example:
All cgi, extraheader and accept variables have string values. All system variables have atom values.cgi(content_length, $1462$)
The following table lists the possible CGI variables that may be set.
Web servers differ in the variables they set and the format of the values
they use.
Standard CGI Name | Predicate Name |
auth_name | cgi |
auth_user | cgi |
auth_type | cgi |
cgi_version | cgi |
content_file | cgi |
content_length | cgi |
content_type | cgi |
cookie | extraheaders |
gateway_interface | cgi |
http_cookie | extraheaders |
http_from | cgi |
http_referer | cgi |
http_user_agent | cgi |
logname | cgi |
path_info | cgi |
path_translated | cgi |
query_string | cgi |
referer_url | cgi |
remote_addr | cgi |
remote_host | cgi |
remote_user | cgi |
request_method | cgi |
request_range | cgi |
script_name | cgi |
server_admin | cgi |
server_name | cgi |
server_port | cgi |
server_protocol | cgi |
server_software | cgi |
user_agent | cgi |
debug_mode | system |
gmt_offset | system |
There are some additional system variables that affect the operation of the functions provided by acgi.pro. These are:
Copyright ©1987-2000 Amzi! inc. All Rights Reserved.