Explore the Web Geni source program in depth
The purpose of tossing web Geni is to ascertain its structure design, operating mechanism, for the cottage out of their own expert system shell, accumulate experience, inspire ideas.
To do this, the source code must be studied in detail.
The Webgeni consists of 2 modules: Geni.pro and Cgitools.pro. The former is the main program, responsible for reasoning, generating web pages, and the latter is responsible for the relative underlying CGI data processing. This article explores the object, mainly is Geni.pro.
The Visual Prolog ("VIP") program consists of several code snippets:
Predicates: predicate segment. A function declaration equivalent to the C language
Clauses: Clause segment. Equivalent to C language function implementation
DOMAINS: Domain segment. Equivalent to the C language Declaration data type structure
Database or FACTS: An internal db (FACT) segment. It is a unique mechanism for Prolog, which is actually a memory buffer for storing structured data. Erlang Eresye has an imitation of it.
GOAL: Target segment. The starting point of the program run, equivalent to the C language function, main ().
Beginning of program goal segment
goalstartpage,cgi_string = Cgi_getstring (), Str_namelist (cgi_string,parmlist), consult_kb (ParmList,ParmList1), Userdefined_startpage (), Write_startform (), Assert_conditions (ParmList1), infer ().
Following, step-by-step tracing, parsing the program.
1. Clause StartPage
StartPage:-Write ("content-type:text/html\n\n"), Write ("
This program runs on the server side and requests the client browser to make the above response.
2. Clause cgi_getstring ()Its predicate is declared in the file Cgitools.pre. Pre file, which is specifically used to declare predicates.
GLOBAL predicates procedure STRING cgi_getstring ()
Global modified predicates, constants, domains, and so on, allowing multiple program modules to be called.
The procedure keyword defines a predicate as a subroutine. Subroutines never Fail (fail), never backtracking, only one run result (except run-time errors).
In file Cgitools.pro, define clause cgi_getstring (cgi_string)
Attention!The predicate cgi_getstring () declaration has no arguments, while the clause cgi_getstring (cgi_string) is implemented with arguments.
The VIP compiler does not consider it wrong to declare this inconsistency with the definition.
Also, an assignment statement such as Cgi_string = Cgi_getstring () is not prolog.
Why is this, do not know.
VIP also has some similar situation, the following also will encounter. I hope the VIP is right, just I didn't understand.
3, focus on the content of CGI dataIt took me a long time to see Cgi_getstring () in Cgitools.pro to get cgi_string, and str_namelist to get the parmlist process.
I think that the details of these two things are not related to the construction expert system, no need to study.
It is important to clarify the content and structure of cgi_string and parmlist.
The predicate declaration from cgi_getstring is visible, and cgi_string is a string.In Cgitools.dom, there are the following declarations:
GLOBAL DOMAINS PARM = PARM (STRING name,string Val) parmlist = parm*
As can be seen, the structure of parmlist is this form:
[Parm ("AAA", "one"), Parm ("BBB", "one"), Parm ("CCC", "three")]
The following methods can be used to verify.
The goal segments in the Geni.pro are all annotated with annotations and replaced by the following code:
Predicateswrite_vars (parmlist) write_data (STRING) clauses write_data (cgi_string):- Write ("<p>"), Writef ("%", cgi_string), write ("</p>"). Write_vars ([]):-!. Write_vars ([Parm (Key,value) | Rest]):- Writef ("<tr><td>%</td><td>%</td></tr>\n", Key,value), Write_vars (Rest). goalstartpage,cgi_string = Cgi_getstring (), File_str ("C:\\dd.dat", cgi_string), Str_namelist (CGI_String,ParmList), Write_data (cgi_string), write ("<table>\n"), Write_vars (Parmlist), write ("</table>\n"), Write ( " <BR/>\n "), write (" </body>
predicate file_str, very prolog characteristic.
File_str (String osfilename, String stringvariable)
Flow mode (i, O), (I, I)
Read and write files, with the same "function", afraid only Prolog do.
In this example, the string is stored in the file Dd.dat.
The contents of the file are:
Knowledgebase=animal
Two predicates Write_vars (parmlist) and Write_data (STRING), written to the contents of the Web page, can be self-validating.
The source of "Knowledgebase=animal" is this:
Open File D:\Apache2.2\htdocs\GENI\default.htm
<form action= "Geni.exe" method= "POST" >
<select name= "Knowledgebase" size= "1" >
<option>animal</option>
<option>Starting</option>>
<option>Tyre</option>>
</select>
<input type= "Submit" value= "Select Problem" >
</form>
Knowledgebase is the name of the "variable" SELECT, and Animal is a value of the variable
There are 3 possible values for this variable: Animal,starting,tyre
The value of the variable is determined by the user in the browser