There are two primary interfaces to the LSAPI:
The C interface is a set of functions (all the names start with 'ls').
These methods provide the basic API services. They are used to initialize and close down the Prolog environment.
These are the methods that actually call predicate in a Prolog module. The query term can be represented as a string or a Prolog term. The methods return the term representing the result, or the value 0 if the query fails.
These methods make it easy to assert and retract dynamic clauses to and from Prolog's logicbase.
These methods convert strings to terms and terms to strings. The 'Q' version create quoted strings when necessary for atoms and strings that require delimiter symbols. They are necessary for those cases when you want to use the resulting string in another query. TermToStr is especially useful during development to display the results of a Prolog query.
These methods create Prolog types from the host environments types.
These methods get host environment types from Prolog terms.
These methods let you create and take apart terms that represent structures. This is especially useful for retrieving arguments in a query. For example, for the query 'sibling(mary, X)' GetStrArg can be used to retrieve the second argument of the strucutre.
These methods let you create Prolog lists, add items to lists and retrieve items from lists. The Get__Head family of functions can be used in loops to get all the items in a list (in conjunction with GetTail).
These methods let you get and set (unify) the parameters that are passed with a Prolog extended predicate. The term parameter is used to distinguish them from functor arguments, although in pure Prolog terminology the parameters are simply the arguments of the functor of the extended predicate. For example, if you implemented an extended predicate called get_url/1, these predicates would let you unify a URL with the first parameter of that predicate.
These methods let you redirect the input and output streams.
These methods are all implemented as part of the LSException class, and let you get details about the exception.
* Not available in all implementations
API functions return a variety of types. Many return the requested value: string, integer, float, etc. Some that initiate Prolog execution or unification return TF, a Prolog true/false, or a term with a 0 value signifying failure. Implementations that support exceptions, indicate errors by throwing an exception. Other implementations return RC, an error checkable function return.
Many API functions require you to specify the type of the Prolog term, pTYPE. pTYPE is an enumerated constant with these values.
pATOM an atom pINT an integer pSTR a string (delimited by ``) pFLOAT a floating point number pSTRUCT a structure pLIST a list pTERM a term pADDR the address of a Prolog value pVAR a Prolog variable pWSTR a Unicode string pWATOM a Unicode atom pREAL an infinite precision real number
Other API functions require you to specify the type of the host language variable, cTYPE. cTYPE is an enumerated constant with these values.
cAATOM an atom (used to indicate a host string will become an atom) (see cATOM below) cASTR an ANSI character string (see cSTR below) cINT an integer cLONG a long cSHORT a short cFLOAT a single precision floating point number cDOUBLE a double precision floating point number cADDR the address of a value cTERM a term cWSTR a Unicode string cWATOM a Unicode atom cSTR a cover type mapped to cASTR or cWSTR depending on whether _UNICODE is specified or not cATOM a cover type mapped to cAATOM or cWATOM depending on whether _UNICODE is specified or not cMOD Special type, indicating the first argument of a :/2 structure, often used in module:goal pattern. Result is stored as cTERM cGOAL Special type, indicating the second argument of a :/2 structure, often used in module:goal pattern. Result is stored as cTERM
The exectipn types, ExType, are:
A few API functions manipulate Prolog I/O streams. They use the enumerated constant STREAM to identify the streams. Values of STREAM are
Other defined types are:
See logicserver.h or each implementation's source or include files for the exact implementations of all data types.
Description:
void AddLSX(STRING lsxname, VOIDptr
vp);
RC lsAddLSX(ENGid cureng, STRING
lsxname, VOIDptr vp);
Remarks:
Causes the Logic Server to load the specified LSX file. Like other API functions that initialize extended predicates, AddLSX() must come after a call to lsInit() and before a call to lsLoad().
LSXs can also be loaded dynamically from Prolog with the loadlsx predicate.
See loadlsx, Extended Predicate Libraries (LSXs) and Writing Extended Predicates
Return Value:
OK if everything worked
Example:
try { Init("dbgene"); AddLSX("aodbc", NULL); // Load the LSX file Load("dbgene"); } catch(CLSException &E) { PrologError(E); }
Description:
void AddPred(STRING name, ARITY
arity, PrologPredicate fptr); (.NET)
void AddPred(STRING PredName, ARITY
Arity, STRING Class, STRING Method,
Object Obj); (Java)
void AddPred(STRING functor, ARITY
arity, ExtPred fptr, VOIDptr vp);
RC lsAddPred(ENGid cureng, STRING
functor, ARITY arity, ExtPred fptr, VOIDptr
vp)
Remarks:
Adds the predicate functor/arity to the extended predicate table, mapping it to the function pointed to by fptr or designated by named class and method in the specified object (for Java). Like InitPreds(), AddPred() must be called between the calls to Init() and Load().
The VOIDptr argument is used in the callback. This argument is passed as the first argument to the extended predicate. This feature allows you to implement call backs to member functions of class. The extended predicate becomes a dispatch function, that uses the argument as a pointer to an object that is used to implement the extended predicate. See the C++ PetsCB and Rubik's cube samples for an example of this technique.
NOTE that when InitPreds is called, this fourth argument is automatically generated to be the engine ID, so call back functions get the engine ID passed as a parameter.
See Extended Predicate Libraries (LSXs) and Writing Extended Predicates
Return Value:
Returns OK if it worked.
Example:
Init("rubik"); // Add 4 extended predicates that are methods of this class AddPred("bugmsg", 1, &::p_bugmsg, this); AddPred("cube_print", 1, &::p_cube_print, this); AddPred("wrfield", 2, &::p_wrfield, this); AddPred("disp_hist", 1, &::p_disp_hist, this); Load("rubik"); ...
Description:
void Asserta(TERM term);
RC lsAsserta(ENGid cureng, TERM
term);
Remarks:
Asserts the term to the logicbase as the first clause under the key which is the term's functor. Same as Prolog asserta/1 and assert/1.
Return Value:
Returns OK if it worked, NOTOK if some indeterminate error happened, and an atom (small positive integer) if the term redefined a protected predicate.
Description:
void AssertaStr(STRING str);
RC lsAssertaStr(ENGid cureng, STRING
str);
Remarks:
A convenient form of Asserta() that uses a string instead of a term. Asserts the term to the logicbase as the first clause under the key which is the term's functor. Same as Prolog asserta/1 and assert/1.
See Asserting and Retracting to and from the Prolog Logicbase
Return Value:
Returns OK if it worked, NOTOK if some indeterminate error happened, and an atom (small positive integer) if the term redefined a protected predicate.
Example:
/* Assert the name of the .xpl file (used to load the logic-base) */ strcpy(strbuf, "system('XPL File', '"); strcat(strbuf, xplname); strcat(strbuf, "')"); ls.AssertaStr(strbuf);
Description:
void Assertz(TERM term);
RC lsAssertz(ENGid cureng, TERM
term);
Remarks:
Asserts the term to the logicbase as the last clause under the key which is the term's functor. Same as Prolog assertz/1.
See Asserting and Retracting to and from the Prolog Logicbase
Return Value:
Returns OK if it worked, NOTOK if some indeterminate error happened, and an atom (small positive integer) if the term redefined a protected predicate.
Example:
/* Assert facts to the Prolog logicbase */ /* Read them from the test file for now */ fp = fopen(sTestFile, "r"); for (i=0; i<16; i++) { fgets(buf, 80, fp); ls.StrToTerm(&t, buf); ls.Assertz(t); }
Description:
void AssertzStr(STRING str);
RC lsAssertzStr(ENGid cureng, STRING
str);
Remarks:
A convenient form of Assertz() that uses a string instead of a term. Asserts the term to the logicbase as the last clause under the key which is the term's functor. Same as Prolog assertz/1.
See Asserting and Retracting to and from the Prolog Logicbase
Return Value:
Returns OK if it worked, NOTOK if some indeterminate error happened, and an atom (small positive integer) if the term redefined a protected predicate.
Example:
/* Assert dynamic facts to the Prolog logicbase */ /* Read them from the test file for now */ fp = fopen(sTestFile, "r"); for (i=0; i<16; i++) { fgets(buf, 80, fp); ls.AssertzStr(buf); }
Description:
TERM Call(TERM term);
TF lsCall(ENGid cureng, TERMptr
termp);
Remarks:
Once a term has been created it can be passed to the Amzi! interpreter with Call(). This is equivalent to the Prolog statement call(X) where X is a Prolog term. Call() is intended for use with Redo() to backtrack through multiple solutions. If backtracking won't be used, then use Exec() instead to avoid unnecessary growth of the Prolog execution control stack.
See Calling Terms
Return Value:
Returns the term, or 0 on failure.
Returns TRUE/FALSE
Example:
TERM t; TF tf; ls.StrToTerm(&t, "parent(elaine,mary)"); tf = ls.Call(&t); if (tf) printf("Elaine is Mary's parent"); else printf("Elaine is not Mary's parent");
Description:
TERM CallStr(STRING str);
TF lsCallStr(ENGid cureng, TERMptr
termp, STRING str);
Remarks:
CallStr() is a more convenient form of Call() that lets you use the string representation of a Prolog term or query. It is intended for use with Redo() to backtrack through multiple solutions. If backtracking won't be used, then use ExecStr() instead to avoid unnecessary growth of the Prolog execution control stack.
Return Value:
Returns the term, or 0 on failure.
Returns TRUE/FALSE
Example:
TERM t; t = ls.CallStr("parent(elaine,mary)"); if (t != 0) printf("Elaine is Mary's parent"); else printf("Elaine is not Mary's parent");
Description:
Remarks:
A call to Call() or CallStr() causes the Prolog engine to build a backtracking choice point on the Prolog control stack. Successive calls to lsRedo() will cause the choice point to be removed when there are no more choices.
If you do not exhaust all the choices with Redo() calls, then a call to ClearCall() can be made to remove the choice point from the stack. It is not in general necessary, but is good form and might be required if the Call() is in a tight loop, thus using up the control stack.
Call() and CallStr() should not be used when the call is intended to be executed only once. For those situations Exec() and ExecStr() should be used, which do not add a choice point to the Prolog control stack.
Return Value:
OK
Description:
Remarks:
Closes down the Prolog environment, closing Prolog streams and freeing all memory allocated by Prolog.
Return Value:
OK
Example:
ls.Close();
Description:
void ErrRaise(STRING msg);
RC lsErrRaise(ENGid cureng, STRING
msg)
Remarks:
Causes an Exec type Prolog error to be raised with the specified error message.
See Error Handling
Description:
TERM Exec(TERM term);
TF lsExec(ENGid cureng, TERMptr
termp);
Remarks:
Exec() and ExecStr() are similar to Call() and CallStr(). The only difference is the Exec functions are intended to be called only once and not backtracked into. That is, you cannot call Redo() after Exec(), but you can after Call().
The advantage of Exec() calls over Call() calls is they do not add anything to the Prolog execution stack, so they can be called with no overhead. The disadvantage, of course, is you can't backtrack through them.
See Calling Terms
Return Value:
Returns the term, or 0 on failure.
Returns TRUE/FALSE
Description:
TERM ExecStr(STRING str);
TF ExecStr(TERMptr termp, STRING
str);
TF lsExecStr(ENGid cureng, TERMptr
termp, STRING str);
Remarks:
ExecStr() is a more convenient form of Exec() that lets you use the string representation of a Prolog term or query.
Return Value:
Returns the term, or 0 on failure.
Returns TRUE/FALSE
Example:
if ( TRUE == (tf = ls.ExecStr(&t, "pet(X)")) ) { ls.GetArg(t, 1, cSTR, buf); printf("Engine two's pet is a %s\n", buf); }
Description:
TERM GetArg(TERM term, int iarg);
RC lsGetArg(ENGid cureng, TERM
term, int iarg, cTYPE ctype, VOIDptr
valp);
Remarks:
Used to extract arguments from complex structures.
If GetArg() is used with the :/2 operator, it assumes it is a module specification, and uses that operator's second argument instead. See example below. In other words, it does the right thing when a module qualifier is used in an lsExec or Call(). If you really want to get the arguments of a :/2 structure, use the special ctypes cMOD and/or cGOAL to get the first and second arguments, respectively, as cTERMs.
Visual Basic Note: When retrieving strings from VB, you must specify 'ByVal' for the VOIDptr val argument.
Return Value:
Returns the term, or 0 on failure.
OK if it worked, NOTOK if it didn't.
Example:
if ( TRUE == (tf = lsExecStr(e2, &t, "pets:pet_id(X)")) ) { lsGetArg(e2, t, 1, cSTR, buf); printf("Engine two's pet is a %s\n", buf); }
Example:
tf = CallStr(&t,"fullname(Pid, Surname, Midname, Name)"); if (tf == FALSE) { AfxMessageBox("No family database loaded"); return FALSE; } while(tf) { GetArg(t, 1, cINT, &PID); GetArg(t, 2, cSTR, Surname); GetArg(t, 3, cSTR, Midname); GetArg(t, 4, cSTR, Name); wsprintf(buf, "%3d: %s %s %s", PID, Name, Midname, Surname); lb->AddString(buf); tf = Redo(); } return TRUE;
Description:
pTYPE GetArgType(TERM term, int
iarg);
pTYPE lsGetArgType(ENGid cureng,
TERM term, int iarg);
Remarks:
Gets the type of the iargth argument of term, assuming term is a structure.
If lsGetArgType is used with the :/2 operator, it assumes it is a module specification, and uses that operator's second argument instead. See discussion in GetArg().
Return Value:
pTYPE, see the Data Types section for possible values.
Description:
int GetArity(TERM term);
Remarks:
Returns the arity of a term. See GetFunctor() to get the functor.
Return Value:
The arity
Description:
STRING GetCallStack(int len);
RC lsGetExceptCallStack(ENGid cureng,
STRING str, int len)
Remarks:
Gets the call stack when the current exception occurred.
See Error Handling
Return Value:
The string representing the call stack
OK
Example:
message = ex.getMessage(); rc = ex.GetRC(); message += "\nerror code: " + Integer.toString(rc); location = ex.GetCallStack(); message += "\ncall stack: " + location;
Description:
STRING GetMsg(int len);
STRING GetMessage(int len);
RC lsGetExceptMsg(ENGid cureng, STRING
str, int len);
Remarks:
Gets the message for the current error.
See Error Handling
Return Value:
The message string
OK
Example:
message = ex.getMessage(); rc = ex.GetRC(); message += "\nerror code: " + Integer.toString(rc);
Example:
rc = lsLoad(CurEng, xplname); if (rc != 0) { lsGetExceptMsg(CurEng, errmsg, 1024); printf("Fatal Error #%d loading Amzi! Logic Server .xpl file:\n%s", rc, errmsg); }
Description:
STRING GetReadBuffer(int len);
RC lsGetExceptReadBuffer(ENGid cureng,
STRING str, int len)
Remarks:
Gets the current read buffer which includes the words "<-Near Here->" to indicate the location of the read error, or at least where the Prolog reader realized it was in trouble.
See Error Handling
Return Value:
The read buffer.
OK
Example:
if (ex.GetType() == LSException.READ) { message = ex.getMessage(); message += "\nline: " + ex.GetLineno(); message += "\nfile: " + ex.GetReadFileName(); location = ex.GetReadBuffer(); message += "\nread buffer: " + location; }
Description:
STRING GetReadFileName(int len);
RC lsGetExceptReadBuffer(ENGid cureng,
STRING str, int len)
Remarks:
Gets the name of the file, if any, that was open when a read error occurred. The name is an empty string if the read error occurred during string I/O or console I/O.
See Error Handling
Return Value:
The name of the file that caused the read error.
OK
Example:
if (ex.GetType() == LSException.READ) { message = ex.getMessage(); message += "\nline: " + ex.GetLineno(); message += "\nfile: " + ex.GetReadFileName(); location = ex.GetReadBuffer(); message += "\nread buffer: " + location; }
Description:
int GetReadLineno();
int CLSException::GetReadLineno();
int lsGetExceptReadLineno(ENGid cureng)
Remarks:
If the read error occurred during file I/O, this function returns a line number in the file close to where the error occurred.
See Error Handling
Return Value:
The line number where the read error occurred.
OK
Example:
if (ex.GetType() == LSException.READ) { message = ex.getMessage(); message += "\nline: " + ex.GetLineno(); message += "\nfile: " + ex.GetReadFileName(); location = ex.GetReadBuffer(); message += "\nread buffer: " + location; }
Description:
ExType GetType();
ExType CLSException::GetType();
ExType lsGetExceptType(ENGid cureng)
Remarks:
Returns the type of the current exception. See the section on Data Types for the values of ExType.
See Error Handling
Return Value:
The exception type
Example:
if (ex.GetType() == LSException.READ) { message = ex.getMessage(); message += "\nline: " + ex.GetLineno(); message += "\nfile: " + ex.GetReadFileName(); location = ex.GetReadBuffer(); message += "\nread buffer: " + location; }
Description:
RC GetRC();
RC CLSException::GetRC();
RC lsGetExceptRC(ENGid cureng)
Remarks:
Returns the return code of the current exception.
See Error Handling
Return Value:
The return code
Example:
message = ex.getMessage(); rc = ex.GetRC(); message += "\nerror code: " + Integer.toString(rc);
Description:
void GetFA(TERM term, STRING functor,
ARITYptr, arityp);
RC lsGetFA(ENGid cureng, TERM
term, STRING functor, ARITYptr arityp);
Remarks:
This function lets you determine the functor and arity of a term. If the term is an atom, then the arity will be 0. If the term is a list then the functor will be "." and the arity 2.
Return Value:
Returns OK for the three types of terms described above, returns NOTOK if the term is not a legal atom, structure or list.
Example:
ENGid cureng; TERM t; char sBuf[80]; ARITY ar; ... lsMakeTerm(cureng, &t, "parent(elaine,mary)"); ... lsGetFA(cureng, t, sBuf, ar); printf("Term was %s/%i", sBuf, ar); ... /* code fragment prints: Term was parent/2 */
Description:
double GetFloatArg(TERM term, int iarg);
Remarks:
Gets the double value of the iarg'th argument of the term.
Return Value:
Returns the double argument.
Description:
double GetFloatHead(TERM term);
Remarks:
Returns the floating point value of the head of the list represented by term.
Return Value:
The floating point value.
Description:
double GetFloatParm(int iparm);
Remarks:
When implementing extended predicates, gets the floating point value of the iparm'th paramete.
Return Value:
The floating point parameter.
Description:
double GetFloatTerm(TERM term);
Remarks:
Returns the floating point value of the term.
Return Value:
The double value.
Description:
Remarks:
Returns the functor of a term. See GetArity() to get the arity.
Return Value:
The functor
Example:
while (stack != 0 && ls.GetTermType(stack) != LogicServer.pATOM) { frame = ls.GetHead(stack); type = ls.GetFunctor(frame); stack = ls.GetTail(stack); ...
Description:
TERM GetHead(TERM term);
RC lsGetHead(ENGid cureng, TERM
term, cTYPE ctype, VOIDptr valp)
Remarks:
The term representing the head of the list is returned or stored in valp. See the Data Types section for legal values of cTYPE.
Return Value:
A term representing the head of the list, or 0 if failure.
OK if everything worked, NOTOK if it failed. Note that it will fail if term does not represent a list or if term represents the empty list.
Example:
while (list != 0) { array.add(ls.TermToStr(ls.GetHead(list), size)); list = ls.GetTail(list); }
Description:
int GetIntArg(TERM term, int iarg);
Remarks:
Gets the integer value of the iarg'th argument of the term.
Return Value:
Returns the integer argument.
Example:
// Get the data // item(Line, Functor, Arity, Description) line = ls.GetIntArg(term, 1) - 1; String functor = ls.GetStrArg(term, 2); int arity = ls.GetIntArg(term, 3); String desc = ls.GetStrArg(term, 4);
Description:
int GetIntHead(TERM term);
Remarks:
Returns the integer value of the head of the list represented by term.
Return Value:
The integer value.
Description:
int GetIntParm(int iparm);
Remarks:
When implementing extended predicates,gets the integer value of the iparmth parameter.
Return Value:
The integer parameter.
Description:
int GetIntTerm(TERM term);
Remarks:
Returns the integer value of the term.
Return Value:
The integer value.
Description:
TERM GetParm(int iparm);
RC lsGetParm(ENGid cureng, int iparm,
cTYPE ctype, VOIDptr valp);
Remarks:
When implementing extended predicates, gets the C/C++ value of the iparm'th parameter, where 1 is the first parameter. See the Data Types section for legal values of cTYPE.
Return Value:
A term representing the parameter, or 0 if failure.
OK if successful.
Example:
char field[40]; GetParm(1, cSTR, field); if (0 == strcmp("stage", field)) { ...
Description:
pTYPE GetParmType(int iparm);
pTYPE lsGetParmType(ENGid cureng,
int iparm);
Remarks:
When implementing extended predicates GetParmType() gets the parameter type of the iparm'th parameter.
Return Value:
pTYPE, see the Data Types section for legal values of pTYPE.
Examples:
pt = lsGetParmType(eid, 3); /* figure out type of third parameter */ if (pt == pINT) /* third parameter was instantiated */ { lsGetParm(eid, 3, cINT, &iElem); /* get its value */ iArray[i] = iElem; /* put it in the array */ } else if (pt == pVAR) /* third parameter was a variable */ { lsMakeInt(eid, &t, iArray[i]); /* fill its value from the array */ lsUnifyParm(eid, 3, cTERM, &t); } else return FALSE; /* third parameter wasn't right */
Description:
STRING GetStrArg(TERM term, int
iarg);
Remarks:
Gets the string value of the iarg'th argument of the term.
Return Value:
Returns the string argument.
Description:
Remarks:
Returns the string value of the head of the list represented by term.
Return Value:
The string value.
Description:
STRING GetStrParm(int iparm);
Remarks:
When implementing extended predicates, gets the string value of the iparm'th parameter.
Return Value:
The string parameter.
Description:
Remarks:
Returns the string value of the term.
Return Value:
The string value.
Description:
int GetStream(STREAM stream);
int lsGetStream(ENGid cureng, STREAM
stream);
Remarks:
Gets the current integer handle of the stream specified by stream. See the section on Data Types for the legal values of STREAM.
Return Value:
The integer handle of the stream.
Description:
TERM GetTail(TERM term);
TERM lsGetTail(ENGid cureng, TERM
term)
Remarks:
The term representing the tail of the list is returned.
Return Value:
The term representing the tail. Returns 0 if term does not represent a list or if term represents the empty list.
Example:
while (list != 0) { element = ls.GetHead(list); name = ls.GetArg(element, 1); value = ls.GetArg(element, 2); if (ls.GetTermType(value) == LogicServer.pLIST) p.setProperty(ls.TermToStr(name, size), ls.TermToStrQ(value, size)); else { p.setProperty(ls.TermToStr(name, size), ls.TermToStr(value, size)); } list = ls.GetTail(list); }
Description:
Remarks:
Copies the value of the term into the variable of type ctype pointed to by valp. See the Data Types section for legal values of cTYPE.
Return Value:
OK if it worked.
Description:
pTYPE GetTermType(TERM term);
pTYPE lsGetTermType(ENGid cureng,
TERM term);
Remarks:
Get the type of the term.
Return Value:
Returns the Prolog type of the term. See the Data Types section for legal values of pTYPE.
Example:
lsGetParm(CurEng, 1, cTERM, &t); ptype = lsGetTermType(CurEng, t); if (ptype == pLIST) ...
Example:
while (list != 0) { element = ls.GetHead(list); name = ls.GetArg(element, 1); value = ls.GetArg(element, 2); if (ls.GetTermType(value) == LogicServer.pLIST) p.setProperty(ls.TermToStr(name, size), ls.TermToStrQ(value, size)); else { p.setProperty(ls.TermToStr(name, size), ls.TermToStr(value, size)); } list = ls.GetTail(list); }
Description:
STRING GetVersion();
RC lsGetVersion(ENGid cureng, STRING
str);
Remarks:
Copies/returns the current version information into the string.
Return Value:
A string representing the version name and number.
OK
Description:
void Init(STRING ininame);
RC lsInit(ENGid *cureng, STRING
ininame);
Remarks:
Initializes the Prolog environment, using <filename>.cfg if present. If it's not present, then it uses amzi.cfg and system defaults. It can be called with an empty string, "", if you don't care to look for an application specific .cfg file. Must be called once, and before Load() is called.
Return Value:
OK if everything worked.
Example:
Init("dbgene"); AddLSX("aodbc", NULL); Load("dbgene"); ...
Description:
void Init2(STRING iniparams);
RC lsInit2(ENGid *cureng, STRING
iniparams);
Remarks:
Initializes the Prolog environment, using the .cfg parameters specified in the iniparams string. The string is of the format "parm1=val1, parm2=val2..". The .cfg parameters can be specified using their full name or with their abbreviation. See the section on INI parameters for a list.
Either Init() or Init2() must be called once, and before Load() is called.
Return Value:
OK if everything worked.
Example:
// This try/catch is designed to catch Logic Server exceptions // and deal with them in the member function error. Note it // catches a reference to the exception object that is thrown // by the Logic Server. try { // Use Init2 rather than Init in order to set the // .cfg parameters from an argument instead of a // .cfg file. In this case, the heap, local, control // and trail are set to small numbers because we have // a small program. Init2("h=100, l=100, c=100, t=100"); // Tell the engine prompt/2 is implemented by // calling the global dispatch function, p_prompt, // with the argument 'this', used to get control // back in this instance of petID. AddPred("prompt", 2, &::p_prompt, this); Load("pets"); } catch(CLSException &e) { error(e); }
Description
void InitLSX(VOIDptr p);
RC lsInitLSX(ENGid cureng, VOIDptr
p);
Remarks:
Causes the Logic Server to look for the 'loadlsx' .cfg file parameter, and load any .lsx files listed there. The pointer can be used for anything, but often is used to identify the calling object or application.
This entry point is only needed if the application is setting the pointer. If lsInitLSX is not called, then the LSXs listed in the .cfg file are automatically loaded with the pointer set to NULL.
See Extended Predicate Libraries (LSXs) and Writing Extended Predicates
Return Value:
OK if everything worked
Description:
void InitPreds(PRED_INITptr predtabp);
RC lsInitPreds(ENGid cureng, PRED_INITptr
predtabp);
Remarks:
Initializes the predicate table pointed to by predtabp. An application can initialize any number of predicate tables. This should be called after the call to lsInit() and before calling any Prolog functions.
See Extended Predicate Libraries (LSXs) and Writing Extended Predicates
Return Value:
OK if everything worked.
Example:
... /* function prototypes */ TF pMakeArray(ENGid); TF pArrayElem(ENGid); /* extended predicate table definitions */ PRED_INIT arrayPreds[] = { {"make_array", 2, pMakeArray}, {"array_elem", 3, pArrayElem}, {NULL, 0, NULL} }; /* extended predicates definitions */ ... void main() { ENGid cureng; lsInit(&cureng, "xarray"); lsInitPreds(cureng, arrayPreds); lsLoad(cureng, "xarray"); lsMain(cureng); lsClose(cureng); }
Description:
void Load(STRING xplname);
RC lsLoad(ENGid cureng, STRING
xplname);
Remarks:
Loads the compiled and linked Prolog program, xplname. You must call Load() or LoadFromMemory() once after calling Init(). If you want to load multiple compiled Prolog files, the rest need to be loaded as .plm files using ExecStr() to call the load predicate with the name of the .plm file to load.
Return Value:
OK if it worked.
Example:
try { // Initialize the Logic Server engine Init(""); // Load the compiled Prolog program, hello.xpl Load("hello"); return TRUE; } catch(CLSException &E) { error(E); return FALSE; }
Description:
void LoadFromMemory(STRING xplname, int
codelength, BYTEptr code);
RC lsLoad(ENGid cureng, STRING
xplname, int codelength, BYTEptr
code);
public void LoadFromURL(String XPLName, URL url) throws LSException, IOException
(Java-only)
Remarks:
Loads the compiled and linked Prolog program, xplname from the specified memory address. You must call Load() or LoadFromMemory() once after calling Init(). If you want to load multiple compiled Prolog files, the rest need to be loaded as .plm files using ExecStr() to call the load predicate with the name of the .plm file to load.
For Java, there is an additional method, LoadFromURL that will read an .xpl file from a Java URL which can be in a JAR file or on the Internet.
Return Value:
OK if it worked.
Example:
LogicServer ls; aBYTEptr p; try { ls.Init(""); char code[1000000]; // Plenty big, best to allocate dynamically std::ifstream in; in.open(argv[1], std::ios::in | std::ios::binary); int i = 0; while (in) in.get(code[i++]); int code_length = i; in.close(); p = (aBYTEptr)&code[0]; ls.LoadFromMemory(argv[1], code_length, p); ...
Description:
Remarks:
Calls main/0 of the loaded Prolog .xpl file.
Return Value:
Prolog TRUE/FALSE depending on success or failure.
Example:
tf = lsMain(CurEng); if (tf == FALSE) ...
Description:
void MakeAddr(TERMptr termp, VOIDptr
valp);
RC lsMakeAddr(ENGid cureng, TERMptr
termp, VOIDptr valp);
Remarks:
Creates a term of type pADDR from the pointer valp.
Return Value:
OK if it worked.
Description:
TERM MakeAtom(STRING str);
RC lsMakeAtom(ENGid cureng, TERMptr
termp, STRING str);
Remarks:
Creates a term of type pATOM from the string str.
Return Value:
A term representing the atom.
OK if it worked.
Description:
TERM MakeFA(STRING functor, ARITY
arity);
RC lsMakeFA(ENGid cureng, TERMptr
termp, STRING functor, ARITY arity);
Remarks:
Creates a term with the specified functor and arity. The arguments are all unbound variables which may be left as is or set to specific values using UnifyArg().
Return Value:
A term representing the structure.
OK if it worked.
Example:
/* create structure bar(one,two) */ lsMakeFA(CurEng, &tInner, "bar", 2); lsUnifyArg(CurEng, &tInner, 1, cATOM, "one"); lsMakeAtom(CurEng, &tArg, "two"); /* another way */ lsUnifyArg(CurEng, &tInner, 2, cTERM, &tArg);
Description:
TERM MakeFloat(double f);
RC lsMakeFloat(ENGid cureng, TERMptr
termp, double f);
Remarks:
Creates a term of type pFLOAT from the double float f.
Return Value:
A term representing the floating point value.
OK if it worked.
Description:
TERM MakeInt(intC i);
RC lsMakeInt(ENGid cureng, TERMptr
termp, intC i);
Remarks:
Creates a term of type pINT from the integer i.
Return Value:
A term representing the integer.
OK if it worked.
Description:
TERM MakeList();
RC lsMakeList(ENGid cureng, TERMptr
termp);
Remarks:
Creates an empty list pointed to by tp.
Return Value:
A term representing the list.
OK if it worked.
Description:
TERM MakeStr(STRING str);
RC lsMakeStr(ENGid cureng, TERMptr
termp, STRING str);
Remarks:
Creates a term of type pSTR from the string str.
Return Value:
A term representing the string.
OK if it worked.
Description:
void PopList(TERMptr listp, cTYPE
ctype, VOIDptr valp);
RC lsPopList(ENGid cureng, TERMptr
listp, cTYPE ctype, VOIDptr valp);
Remarks:
Pops the term of type ctype from the head of the list and save it in valp. See the Data Types section for possible values of cTYPE.
Return Value:
OK if everything worked, NOTOK if it failed. Failure will occur if tp does not point to a list.
Example:
BOOL CProGene::Relationships(CListBox *lb) { ... tf = CallStr(&t, "relations(X)"); if (tf) GetArg(t, 1, cTERM, &tList); while (OK == PopList(&tList, cSTR, buf)) lb->AddString(buf);
Description:
TERM PushList(TERM list, TERM
term);
RC lsPushList(ENGid cureng, TERMptr
listp, TERM term)
Remarks:
The term is pushed on the head of the list and listp points to this new head of list.
Return Value:
A term representing the new head of the list.
OK if everything worked, NOTOK if it failed. Failure will occur if tp does not point to a list.
Description:
Remarks:
Causes Prolog to backtrack and retry the last Call(), CallStr(), or Redo(). The term used in the initiating Call() or CallStr() is unified with a new value if the redo is successful. Redo() can be used in a while loop to get all the solutions to a Prolog query.
See String Passing Interface and Scope of Logic Server Terms
Return Value:
TRUE/FALSE. A loop that has the TF return code as its condition will loop until there are no more solutions.
Example:
tf = CallStr(&t,"fullname(Pid, Surname, Midname, Name)"); if (tf == FALSE) { AfxMessageBox("No family database loaded"); return FALSE; } while(tf) { GetArg(t, 1, cINT, &PID); GetArg(t, 2, cSTR, Surname); GetArg(t, 3, cSTR, Midname); GetArg(t, 4, cSTR, Name); wsprintf(buf, "%3d: %s %s %s", PID, Name, Midname, Surname); lb->AddString(buf); tf = Redo(); } return TRUE;
Description:
Remarks:
Resets the Prolog stacks and runtime environment. Leaves the logicbase unchanged, so all load programs and assert terms are still present.
See Error Handling
Return Value:
OK if successful.
Description:
TF Retract(TERM term);
TF lsRetract(ENGid cureng, TERM
term);
Remarks:
Retracts the first term in the logicbase that unifies with the specified term; term has the unified value, so Retract() can be used to retrieve and process terms as they're being retracted.
See Asserting and Retracting to and from the Prolog Logicbase
Return Value:
TRUE/FALSE
Example:
... TERM t; lsvMakeTerm(cureng, &t, "foo(one)"); lsAsserta(cureng, t); lsvMakeTerm(cureng, &t, "foo(X)"); lsRetract(cureng, t); lsTermToStr(cureng, &t, buf, 40); printf("Removed %s", buf); ... /* This fragment prints: Removed foo(one) */
Description:
TF RetractStr(STRING str);
TF lsRetractStr(ENGid cureng, STRING
str);
Remarks:
Retracts the first term in the logicbase that unifies with the term represented by the string str.
See Asserting and Retracting to and from the Prolog Logicbase
Return Value:
TRUE/FALSE
Example:
ls.AssertaStr("foo(one)"); ls.RetractStr("foo(X)");
Description:
void SetCommandArgs(int argc, char** argv);
RC lsSetCommandArgs(ENGid cureng, int
argc, char** argv);
Remarks:
Passes the values of argc and argv to the Prolog engine so it can process command line arguments if desired. It is necessary to do this if the Prolog code uses the predicate 'command_line/1'.
Return Value:
OK
Example:
void main(int argc, char** argv) { ... lsSetCommandArgs(cureng, argc, argv), ... }
Description:
void SetInput(int (* mygetc)(VOIDptr vp), void(*myungetc)(VOIDptr
vp, int c));
RC lsSetInput(ENGid cureng, int (*
mygetc)(VOIDptr vp), void(*myungetc)(VOIDptr
vp, int c));
Remarks:
When any input stream is set to 3, Amzi! Prolog uses these functions for reading user input from that stream.
Return Value:
OK if it worked.
Description:
void SetIOArg(VOIDptr vp);
RC lsSetIOArg(ENGid cureng, VOIDptr
vp);
Remarks:
When any input stream is set to 3, the vp argument is included in the calls to the getc, ungetc, putc and puts functions defined by SetInput() and SetOutput().
Return Value:
OK if it worked.
Description:
void SetOutput(void (*myputc)(VOIDptr vp, int c),
void (*myputs)(VOIDptr vp, STRING
s));
RC lsSetOutput(ENGid cureng, void (*myputc)(VOIDptr
vp, int c), void (*myputs)(VOIDptr vp, STRING
s));
Remarks:
When any output stream is set to 3, Amzi! Prolog uses these functions for writing to the stream.
Return Value:
OK if it worked.
Description:
SetStream(STREAM stream, int handle);
RC lsSetStream(ENGid cureng, STREAM
stream, int handle);
Remarks:
Sets the stream to the integer handle. Typically this will be used to set the stream to 3, reserved for user-defined function I/O. It might also be used to reset the stream to its old value, obtained from a prior call to GetStream(). See the section on Data Types for the legal values of STREAM.
Return Value:
OK if it worked.
Description:
int StrArgLen(TERM term, int iarg);
int lsStrArgLen(ENGid cureng, TERM
term, int iarg);
Remarks:
Used to determine the length of string to be returned from a lsGetArg() where the argument is either a string or an atom.
Return Value:
The length of the string/atom at the iargth parameter, or NOTOK (-1) if term is not a string.
Description:
int StrParmLen(int iparm);
int lsStrParmLen(ENGid cureng, int iparm);
Remarks:
Used to determine the length of string to be returned from a lsGetParm() where the parameter is either a string or an atom.
Return Value:
The length of the string/atom at the iparmth parameter, or NOTOK (-1) if term is not a string.
Description:
int StrTermLen(TERM term);
int lsStrTermLen(ENGid cureng, TERM
term);
Remarks:
Used to determine the maximum length of string to be returned from a GetTerm() or GetHead() where the parameter is either a string or an atom. This length includes all possible quotes in case TermToStrQ() is used.
Return Value:
The length of the string/atom at the iparmth parameter, or NOTOK (-1) if term is not a string.
Description:
TERM StrToTerm(STRING str);
RC lsStrToTerm(ENGid cureng, TERMptr
termp, STRING str);
Remarks:
Reads the string and creates a term from it.
See Calling Terms
Return Value:
A term representing the string.
OK if it worked.
Example:
sprintf(buf, "%s(%s, Y)", sRelation, sIndividual); lsStrToTerm(CurEng, &t, buf); tf = lsCall(CurEng, &t);
Description:
STRING TermToStr(TERM term);
RC lsTermToStr(ENGid cureng, TERM
term, STRING str, int len);
Remarks:
Writes the term to the string which is len bytes long.
Return Value:
The string representing the term.
OK if it worked.
Example:
lsGetParm(CurEng, 1, cTERM, &t); ptype = lsGetTermType(CurEng, t); if (ptype == pLIST) { while (OK == lsPopList(CurEng, &t, cTERM, &tm)) { lsTermToStr(CurEng, tm, buf, 80); printf("%s", buf); } } else { lsTermToStr(CurEng, t, buf, 80); printf("%s", buf); }
Description:
STRING TermToStrQ(TERM term);
RC lsTermToStrQ(ENGid cureng, TERM
term, STRING str, int len);
Remarks:
This function is the same as TermToStr(), except the string is created with quoted atoms where necessary. This makes it possible to feed the same string back to Prolog.
Return Value:
The string representing the term with atoms quoted.
OK if it worked.
Example:
while (list != 0) { element = ls.GetHead(list); name = ls.GetArg(element, 1); value = ls.GetArg(element, 2); p.setProperty(ls.TermToStr(name, size), ls.TermToStrQ(value, size)); list = ls.GetTail(list); }
Description:
TF Unify(TERM term1, TERM
term2);
TF lsUnify(ENGid cureng, TERM
term1, TERM term2);
Remarks:
Unifies the two terms. Unify() does a full Prolog unification between the two terms. Unlike other functions which return TF, Unify() does not set and reset Prolog break and error handling.
Return Value:
TRUE/FALSE depending on whether the unification succeeded or failed.
Description:
TF UnifyArg(TERMptr termp, int iarg,
cTYPE ctype, VOIDptr valp);
TF lsUnifyArg(ENGid cureng, TERMptr
termp, int iarg, cTYPE ctype, VOIDptr
valp);
Remarks:
Used to build complex terms, UnifyArg() lets you individually set the arguments of term. Used in conjunction with MakeFA(), as well as Prolog terms picked up from calls to Prolog.
Return Value:
TRUE/FALSE depending on whether the unification succeeded or failed.
Example:
/* create structure bar(one,two) */ lsMakeFA(CurEng, &tInner, "bar", 2); lsUnifyArg(CurEng, &tInner, 1, cATOM, "one"); lsMakeAtom(CurEng, &tArg, "two"); /* another way */ lsUnifyArg(CurEng, &tInner, 2, cTERM, &tArg);
Description:
TERM UnifyAtomArg(TERM term, int
iarg, STRING str);
Remarks:
Used to build complex terms, UnifyAtomArg() lets you individually set the argument of a structure to the specified atom value. Used in conjunction with MakeFA(), as well as Prolog terms picked up from calls to Prolog.
Return Value:
The new term with the unified argument, or 0 if failure.
Description:
TF UnifyAtomParm(int iparm, STRING
str);
Remarks:
Unify the iparm'th parameter of an extended predicate with the atom in the passed string.
Return Value:
TRUE/FALSE depending on whether the unification succeeded or failed.
Example:
TF petID::p_prompt() { string answer, prompt; prompt = ls.GetStrParm(1); cout << prompt << "> "; cin >> answer; return ls.UnifyAtomParm(2, answer); }
Description:
TERM UnifyFloatArg(TERM term, int
iarg, double value);
Remarks:
Used to build complex terms, UnifyFloatArg() lets you individually set the argument of a structure to the specified floating point value. Used in conjunction with MakeFA(), as well as Prolog terms picked up from calls to Prolog.
Return Value:
The new term with the unified argument, or 0 if failure.
Description:
TF UnifyFloatParm(int iparm, double f);
Remarks:
Unify the iparm'th parameter of an extended predicate with the double.
Return Value:
TRUE/FALSE depending on whether the unification succeeded or failed.
Description:
TERM UnifyIntArg(TERM term, int iarg,
int value);
Remarks:
Used to build complex terms, UnifyIntArg() lets you individually set the argument of a structure to the specified integer value. Used in conjunction with MakeFA(), as well as Prolog terms picked up from calls to Prolog.
Return Value:
The new term with the unified argument, or 0 if failure.
Description:
TF UnifyIntParm(int iparm, int value);
Remarks:
Unify the iparm'th parameter of an extended predicate with the integer.
Return Value:
TRUE/FALSE depending on whether the unification succeeded or failed.
Example:
public boolean p_keyb() { int c = key.get(); boolean tf = false; try { tf = ls.UnifyIntParm(1, c); } catch (LSException ex) { displayError(ex); } return tf; }
Description:
TF UnifyParm(int iparm, TERM term);
TF UnifyParm(int iparm, cTYPE ctype,
VOIDptr valp);
TF lsUnifyParm(ENGid cureng, int iparm,
cTYPE ctype, VOIDptr valp);
Remarks:
Unify the iparm'th parameter of an extended predicate with the 'term' or the 'variable'. Note that there are two types for representing strings, cATOM and cSTR. While they are both strings, they unify as either a Prolog atom or a Prolog string depending on which is used.
Return Value:
TRUE/FALSE depending on whether the unification succeeded or failed.
Example:
int i; lsUnifyParm(cureng, 1, cATOM, "atomparam"); lsUnifyParm(cureng, 2, cINT, &i);
Description:
TERM UnifyStrArg(TERM term, int iarg,
STRING str);
Remarks:
Used to build complex terms, UnifyStrArg() lets you individually set the argument of a structure to the specified string. Used in conjunction with MakeFA(), as well as Prolog terms picked up from calls to Prolog.
Return Value:
The new term with the unified argument, or 0 if failure.
Description:
TF UnifyStrParm(int iparm, STRING
str);
Remarks:
Unify the iparm'th parameter of an extended predicate with the string.
Return Value:
TRUE/FALSE depending on whether the unification succeeded or failed.
Example:
TF petID::p_prompt() { string answer, prompt; prompt = ls.GetStrParm(1); cout << prompt << "> "; cin >> answer; return ls.UnifyStrParm(2, answer); }
Copyright ©1987-2011 Amzi! inc. All Rights Reserved. Amzi! is a registered trademark and Logic Server is a trademark of Amzi! inc.