Embedding KnowledgebasesIn order to run a knowledgebase within another programming environment, you need the following:
A KnowledgeWright knowledgebase can be embedded in almost any language or tool, including C/C++, Java, Delphi, Visual Basic and web servers. To do this, you use the LogicBase Interface (KWI). The KWI provides functions for managing the reasoning process, asking the user questions and displaying results.
KnowledgeWright is written in Amzi! Prolog + Logic Server. If you are familiar with that product, embedding KnowledgeWright will be straightforward. Before running KnowledgeWright , you have to load and initialize the Amzi! Logic Server with the following calls: lsInit("");
lsInitLSX();
lsLoad
("basic.xpl");Note InitLSX will load the LSX files specified in amzi.cfg. Put the following line in amzi.cfg (no space after the semi-colon):
lsxload = aodbc;aosutils
When you are done interacting with the KWI you need to call:
lsClose();
The basic structure of a program that calls the KWI is as follows:
kwi( ... initialize ... )
kwi( ... open ... )
kwi( ... new_session ... )while ( kwi( ... solve ... ) and MORE = more)
{
while kwi( ... get_action ... ) and ACTION != none)
{ if ( ACTION == ask(user, ... ) ) { ... display prompt, menu, etc. and get value from user kwi( ... assert ... ) } if ( ACTION == tell(user, ...) ) ...
}
} kwi( ... close ... )
The above program structure starts the reasoning process with 'solve'. This usually results in the reasoning engine needing to ask a question or display some intermediate output. These are 'actions' which are processed individually. The answers are asserted to the reasoning engine, and it is asked to 'solve' again.
There are a number of ways to directly extend the reasoning engine with code written in Amzi! Prolog or your host language (Java, C/C++, Delphi or VB). These techniques require advanced programming techniques and are normally needed only with large knowledgebases or for high-performance web servers. See Advanced KWI Programming for details.
You will need two functions to support your interaction with the KWI:
These functions are provided in the runtime directory for each example.
The reasoning engines for KnowledgeWright are written in Amzi! Prolog. So the KWI uses the Amzi! Logic Server to make calls to the Prolog code. The primary Logic Server function that is used is ExecStr. This executes the predicate passed as the string argument. The term is the result of the execution. ExecStr returns false (0) if it fails (which should not happen in the KWI) and a term pointer (non-zero) if it succeeds.
Variables in Prolog start with capital letters. When you see a string passed to ExecStr with a variable, the resulting value needs to be retrieved. It is retrieved from the Term returned by ExecStr. The calls GetStrArg and GetArg get the values of these variables. GetStrArg gets a string argument. GetArg is used when a Prolog list is returned, that list is converted into a suitable data structure for the host language (a Vector for Java, a Collection for Basic). You need not concern yourself with the details of the list conversion, just use the host language structure to retrieve the values.
One KWI call, get_action, returns a Prolog structure that describes the action. The format of the structure is:
functor(atom, [list_item1 = value1, list_item2 = value2...])
The functor is retrieved by the Logic Server call GetFunctor. For get_action, the functor is 'ask' or 'tell'. The atom is the target of the action and is 'user' (additional targets will be supported for performing external actions).
The CGI web server interface is described here.
The Delphi interface is described here.
The Java interface is described here.
The Visual Basic interface is described here.