Visual Basic Module

This documents the Amzi! Logic Server Visual Basic VB Module before .NET. VB.NET users are advised to use the .NET Class instead. This module provides a wrapper on the Logic Server DLL that presents the Logic Server in a manner more consistent with VB use. It includes:

The documentation is organized as follows:

  • Hello Prolog - Getting the Hello Prolog up and running from Visual Basic.
  • Overview - Overview of the Visual Basic cover functions for the Logic Server.
  • Using the Module - How to call the Logic Server from Visual Basic.
  • Implementing Extended Predicates - How to add Prolog extended predicates written in VB 5.0 or later.
  • Reference - Complete listing of all of the Visual Basic Logic Server functions.
  • Example - A more complete Visual Basic example illustrating a variety of Logic Server functions.
  • Hello Prolog

    The hello.vbp Visual Basic Hello Prolog project is in the directory samples\vb\hello. It consists of a form, hello.frm (Hello VB Source Code), and a Logic Server Definitions file, amzi.bas.

    The form has a single button to which all the code is attached. The code initializes the Prolog environment and calls the hello/2 predicate. It checks for error conditions, and if all is well prints in message boxes two status messages then the returned greeting from hello.pro.

    To run, open the project in Visual Basic and select Run/Start. Press the 'Greet Me!' button, then press [Enter] or click the mouse on each of the three OK message boxes.

    The Logic Server VB Module is implemented in the file amzi.bas in the top-level INCLUDE subdirectory. You use it by simply adding that file to to your project.

    Overview

    The Amzi! Logic Server VB Module is designed to give the VB programmer easy access to the services provided by the Logic Server DLL.

    The Logic Server is an engine, similar to a database engine. When you include the Logic Server in your application, you are really including a Logic Server Engine. This engine knows how to access compiled and dynamic Prolog predicates in the Prolog logicbase. The API to the Logic Server provides functions that let you:

    The Logic Server VB Module is distributed in source form in the file amzi.bas. You can build your own VB functions on top of it, or modify the cover functions themselves. (If you have ideas for improving the interface, please let us know and we'll include them in the VB Module in a later release.)

    When you compare the functions in the VB Module, you'll find they extend, and in some cases, modify, the API functions as described in the manual. The differences are:

    Philosophically, the VB Module preserves as much of the flavor of the pure API as possible, and augments it to provide a more VB-like interface.

    Using the Module

    To include the Logic Server in an application, simply add the Logic Server VB Module, amzi.bas, to your project.

    To call the Logic Server, simply embed the API calls in the functions of your application. You will need to open and close the Logic Server as well. The following code opens the Logic Server, loads a compiled Prolog file, executes a simple query, and closes the Logic Server.

    This code assumes the Prolog program, hello.pro, has a fact in it of the form hello(vb, prolog). It also assumes hello.pro has been compiled and linked to create hello.xpl, and that file is in the current directory or your PATH. See the Amzi! Prolog + Logic Server manual for more information on compiling and linking Prolog code.

    Implementing Extended Predicates

    Visual Basic 5.0 (and later), like C/C++, Java and Delphi, can be used to implement custom extended predicates to the Prolog language. These custom extensions give the Prolog code the ability to directly access anything Visual Basic can access.

    The VB functions that implement extended predicates must be in their own module, declared as returning type long, and public. For example:

    The extended predicate function returns 1 for success (true) and 0 for failure (false).

    The predicates are added one at a time by using the VB module function AddPredLS.

    Note: Extended predicate definitions must always be added after calling InitLS and before calling LoadLS.

    The sample in directory samples\vb\extpred includes a simple example of adding a msgbox predicate to Prolog.

    Reference

    This section contains the interface portion of the unit amzi.bas, which defines the Logic Server VB Module. It generally conforms to the ls procedures and functions in the LSAPI Reference. You can browse the source code for more details.

  • Logic Server Constants
  • Functions to Set Up Logic Server
  • Functions for Extended Predicates
  • Functions for Calling Prolog
  • Functions for Dynamic Clauses in Logicbase
  • Functions for String/Term Conversions
  • Functions for Making/Getting Prolog Types
  • Functions for Manipulating Structures
  • Functions for Manipulating Lists
  • Miscellaneous Functions
  • Logic Server Constants

    These are the simple constants used by the Logic Server. They are used to map values to the API data types.

    Logic Server Set-Up

    These functions provide the basic API services. They are used to initialize and close down the Prolog environment. The function, Main, runs the main/0 predicate of a load Prolog file.

    They are based on the API functions lsInit, lsInitLSX, lsAddLSX, lsLoad, lsMain, lsReset, and lsClose.

    The following functions control parameters within the VB Module. SetMaxStrLenLS sets the maximum length for all strings returned from Prolog to Basic .

    SetErrorHandlerLS chooses whether error messages are displayed in message boxes ("MessageBox") or signalled via error #31300 ("ErrorCode"). When an error is signalled, ErrorLS is set to the error message and ErrLS is set to the Amzi! Logic Server error code. The routine that performs these operations is called ErrorHandle.

    Extended Predicates

    When you write an extended predicate, you can directly manipulate the Prolog parameters to that function. These predicates provide that service. The Get family retrieves the nth parameter that was used in the call, and the Unify family unifies some computed result with the nth parameter.

    The VB module contains a number of type-specific additions to the basic API. Functions such as GetLongParmLS and UnifyStrParmLS.

    These functions are based on the API functions lsGetParm, lsGetParmType, lsUnifyParm, and lsStrParmLen.

    Calling Prolog

    These are the functions that actually call predicates in a Prolog logicbase/program. They query term can be represented as a string or a Prolog term. The first argument, a term, is always unified with the result of the call. The Redo function initiates backtracking, which reunifies the term with the next result.

    ClearCall is only useful if you start a Redo loop and don't finish. It clears the stack.

    These functions are based on the API functions lsExec, lsExecStr, lsCall, lsCallStr, lsRedo, and lsClearCall.

    Get and SetCurrentEngineLS let you switch between multiple invocations of the Logic Server. To use them, call InitLS then use GetCurrentEngineLS to get the ID. Save that value. Call InitLS again and use GetCurrentEngineLS to get the new ID. Save that value as well. Then use SetCurrentEngineLS to switch between the active Prolog engines.

    Dynamic Clauses in the Logicbase

    These functions make it easy to assert and retract dynamic terms to and from Prolog's logicbase.

    These functions are based on the API functions lsAsserta, lsAssertaStr, lsAssertz, lsAssertzStr, and lsRetract.

    Converting Strings/Terms

    These functions convert strings to terms and terms to strings. The 'Q' versions create quoted strings, when necessary for atoms and strings that require delimiter symbols. The 'Q' versions are necessary for those cases where you want to use the resulting string in another query.

    These functions are based on the API functions lsTermToStr, lsTermToStrQ, and lsStrToTerm.

    Making/Getting Prolog Types

    These functions map Prolog types to and from VB types, and retrieve VB types from Prolog terms. The type-specific Get functions are VB additions.

    These functions are based on the API functions lsMakeAtom, lsMakeStr, lsMakeInt, lsMakeFloat, lsMakeAddr, lsGetTermType, and lsGetTerm.

    Manipulating Structures

    These Prolog structure-manipulating functions let you create structures, and take apart terms that represent structures. This is especially useful for getting at various arguments in a query. For example, in the query 'sibling(mary, X)', the GetStrArgLS function can be used to get the second argument of this structure, after a CallStrLS has caused the variable to be bound.

    These functions are based on the API functions lsGetFA, lsMakeFA, lsUnifyArg , lsGetArg, lsGetArgType, lsStrArgLen, and lsUnify.

    Manipulating Lists

    These list manipulation functions let you create Prolog lists, add items to lists, and retrieve items from lists. The GetHead family of functions can be used in loops to get all the items in a list. Note that, because GetHead doesn't invoke the Prolog engine or unification, it does not return a Boolean. It returns a normal function return code, in which 0 means success and anything else indicates the end of the list has been reached.

    These functions are based on the API functions lsMakeList, lsPushList, lsGetHead and lsGetTail.

    Miscellaneous Functions

    These miscellaneous functions are used to change the Prolog streams, get the version number, and work with Prolog error codes.

    These functions are based on the API functions lsSetStream, lsGetStream, lsGetVersion, lsGetExceptMsg, and lsGetExceptReadBuffer.

    Examples

    A number of examples can be found in the samples\vb subdirectory.

    Copyright ©1987-2011 Amzi! inc. All Rights Reserved. Amzi! is a registered trademark and Logic Server is a trademark of Amzi! inc.