Web expert system for Visual Prolog (4)

Source: Internet
Author: User
Tags knowledge base

Knowledge Base Operations

Let's talk about a little trick.
Open Geni.prj in VIP, menu options, open Application Expert, click Other options,
Click Project directories, click Output, and fill in the final text box D:\Apache2.2\htdocs\GENI
This directory is where the Geni virtual host resides.

The purpose of this is to compile Geni.exe automatically into the above directory, eliminating the hassle of manual handling.

Start today's topic and conduct a knowledge base operation.

The next step is to execute the clause consult_kb (PARMLIST,PARMLIST1) of the goal segment.

The flow pattern for this clause is (I,O), that is, Parmlist has been constrained,
values are [Parm ("Knowledgebase", "Animal")],
While ParmList1 is a free variable, it is not constrained and the value is empty.

Clause consult_kb (PARMLIST,PARMLIST1) is simpler and more intuitive.

  CONSULT_KB ([Parm ("Knowledgebase", KB) | Rest],rest):-assert (KB), Filenameext (NAME,KB, ". GNI"), Syspath (Exestartuppath,_progname), Filenamepath ( Fullname,exestartuppath,name), consult (Fullname,geni),!.  CONSULT_KB (_,[]):-Errorexit.

For a little explanation.

CONSULT_KB ([Parm ("Knowledgebase", KB) | Rest],rest)

There are 4 points to note on the head of the clause:
1th, head match condition [Parm ("Knowledgebase", KB) | Rest] is:
The first parameter is a list, where the first member is a composite structure parm,parm the first parameter is "Knowledgebase"
2nd, the head of the KB and rest, just the output variable, not the clause header match condition
KB and rest appear here for use in the clause body
3rd, the matching of variables, unity.
The value of the second parameter variable, constrained to rest.
4th, if the head match fails, the statement in the body of the clause is not executed.

Assert (KB)

Deposit Knowledge KB (KB) into VIP Internal Database (fact base)
ASSERT () is a VIP built-in predicate.
The Declaration of KB () is:

Database-tmp    determ KB (STRING)
Here, the keyword Determ determines that only one KB () fact is allowed in the fact base.
The value of KB is "animal".
The KB cannot be a free variable (null value), otherwise an ASSERT () error is run. Filenameext (NAME,KB, ". GNI")

It is a VIP built-in predicate.
It has 2 flow patterns (i, O, O), (o, I, I), and this is the 2nd type.
As a result, name= "Animal.gni"
If the 1th flow mode, such as: Filenameext ("Animal.gni", Name,ext)
As a result, name= "animal", ext= ". GNI"

The different flow modes make the same clause of Prolog, which has many functions.

Syspath (Exestartuppath,_progname)

It is the VIP built-in predicate, and the flow pattern is (o, O).
It returns the name of the program and the directory in which it resides.
In this example, the second parameter is _progname, and the underscore indicates that it is an "anonymous variable" and is not used.

Filenamepath (Fullname,exestartuppath,name)

It is the VIP built-in predicate, the flow pattern is (i, O, O), (o, I, i). For example:

(i, O, o) mode      FullName = "D:\\apache2.2\\htdocs\\geni\\animal.gni"      the clause filenamepath return:      Path =  "d:\\ Apache2.2\\htdocs\\geni\\ "      Name ="  Animal.gni "(o,i,i) mode      Path =  " D:\\apache2.2\\htdocs\\geni\\ "      Name =  "Animal.gni"      the clause Filenamepath returns:      FullName = "D:\\apache2.2\\htdocs\\geni\\animal.gni"
Consult (Fullname,geni)

It is the VPI built-in predicate. The flow pattern is (i, i).
From the text file, read the "facts" and deposit the fact library of the program.
The second parameter, Geni, is the name of the fact base.

Database-geni      rule (rno,category,category,conditions,string)      cond (cno,string,string OPTIONS)       topic ( String maingoal,string Purpose)      head (string headline, String helptopic)       default_startpage (String FILENAME)

The fact name in the Knowledge Base file Animal.gni conforms to the requirements of the fact segment Geni.

Rule (1, "Carnivore", "Cheetah", [up], "animals\\cheetah.htm") ... cond (1, "It has tawny color", "animals\\animals.htm") ... Topic ("Animal", "I'll try to guess which animal you is thinking of") Default_startpage ("animal.htm")
Errorexit ()

Generates a run-time error, and an internal error message.
Error message number 1000
Content is "program error! Check source code at reported position "

Operation mechanism of consult_kb: backtracking and truncation

Let's look at its formal structure:

  CONSULT_KB (...):-......,        ....  consult_kb (...):---...............

This simplification helps to focus and understand the key.

Watch out! This predicate is composed of 2 clauses.
The first clause, I deliberately omitted the operator "!"

The prerequisite for backtracking is that the predicate consists of more than 2 clauses.

There are 2 situations in which backtracking occurs.

1, the first clause in vivo statement execution failed (fail), causing the second clause to execute

2, the first clause in vivo statement execution succeeds, but does not set "truncation",
Failure of other predicate clauses, resulting in a second clause that is traced back to this predicate retry

The "truncation" situation is this:

  CONSULT_KB (...):-......,        ...  ........... CONSULT_KB (...):--.....

With operator "! "Set truncation.

The purpose of this example is to set the truncation to the last line:

1, the statement in the body of the first clause, as long as the failure occurs, which causes backtracking, executes the second clause;

2, the first clause in the body of the statement, all successful, eliminate backtracking points,
This predicate clause is not involved in backtracking caused by other predicate clauses.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.