LuaCLIPS

LuaCLIPS is the CLIPS expert system tool in a Lua accessible DLL.

CLIPS stands for "C Language Integrated Production System", and it provides a complete environment for the construction of rule and/or object based expert systems. Lua is an open source scripting language designed for extending applications.

Project status: Senescent

LuaCLIPS was written as part of BonSAI, an open source wargame AI. As Lua has been replaced by C# as the scripting language in BonSAI the LuaCLIPS project is now officially over. LuaCLIPS is stable, and within the limits of Lua quite useful, so feel free to experiment with the final version.

Download

You can download a binary file (277k zipped) of the final version.

The source code can also be assembled as follows:

  1. The base CLIPS source code is available from the CLIPS site.
  2. To that you need to add this Visual C 6 project and additional code (48k zipped), or the equivalent.
Download

In choosing Lua & CLIPS interpreters, make sure that you end up with a compatible set of compiler options, language versions and C/C++ compilations. Version History information is available below. Here is a binary of a Lua DLL & lib file (65k zipped) compatible with the final version of LuaCLIPS. The source code is available from the Lua web site.

LuaCLIPS Functions

LuaCLIPS exposes the following functions & data structures to Lua. The functions argument names used here refer to what they do. You can rename the functions & their arguments to whatever you like or add additional functions, by altering the above C code. Be aware however that in choosing the length of function names in Lua, there is an unfortunate trade-off between inscrutability and inefficiency.

All CLIPS printout returns are sent to the Lua stack. Values are stored in the Lua table created by CLIPSEngineInit().

CLIPS Environment
void* CLIPSEngineInit(bool verboseOutput, string returnTableName) -- return value is a pointer to the CLIPS environment created
CLIPSEngineClose(void* clipsEnvironment) -- close all CLIPS environments

Commands
load(void* clipsEnvironment, string fileName)
save(void* clipsEnvironment, string fileName)
clear(void* clipsEnvironment)
reset(void* clipsEnvironment)
run(void* clipsEnvironment)
execCommand(void* clipsEnvironment, string clipsCode) -- send this string to RouteCommand() in CLIPS

Storage in Lua
returnTableName = {} -- a global Lua table that stores return values, named by CLIPSEngineInit()

There is some redundancy here. For example, clips.execCommand(m_env, "(reset)") and clips.reset(m_env) would have the same outcome. NB clips.execCommand(m_env, "(exit)"), and all CLIPS calls to (exit) are ignored.

Sample Lua Code

To load the LuaCLIPS DLL and use CLIPS in Lua, you will need something like the following Lua code. Writing the equivalent of the (hopefully self explanatory) Lua function writeToLog() is left as an exercise for the reader. Variable names are chosen for clarity, rather than efficiency.

-- A function to load a DLL as a Lua library

function doLibrary(libName, funct)
-- function to load a DLL
-- calls writeToLog() and exeDirectory, a global recording where the DLL is
local path = exeDirectory .. "\\" .. libName

a, b, c = package.loadlib(path, funct)
-- Lua 5.02 for the above would be
-- a, b, c = loadlib(path, funct)

if a == nil then
    --error messages
    if c == "init" then
        writeToLog(b .. "- " .. funct) -- couldn't find main function
    else
        writeToLog(b .. "- " .. path) -- couldn't find file
    end
    else
        --load lib
        a()
        writeToLog(libName .. " - Library loaded") end
    end
    --nb Lua will exit with an error dialogue on the first unprotected function call of an unloaded library
end

-- Load the LuaCLIPS library and do something simple but pointless in CLIPS

doLibrary("LuaCLIPS.dll", "clipsLib")
clipsEnvironment = clipsLib.CLIPSEngineInit(false, "ClipsR")

clipsLib.execCommand(clipsEnvironment, "(defrule r2(light green) (name Michael) => (printout t \"Run Michael! Run!\" crlf))")
clipsLib.execCommand(clipsEnvironment, "(assert (light green))")
clipsLib.execCommand(clipsEnvironment, "(assert (name Michael))")

clipsLib.run(clipsEnvironment)
clipsLib.CLIPSEngineClose(clipsEnvironment)

-- ClipsR = {} is the table set up by CLIPSEngineInit() to store replies. ClipsR[0] stores the number of elements in ClipsR[]

for a = 1, ClipsR[0], 1 do
    writeToLog(ClipsR[a])
end

package.unload

The Lua DLL available on this site has been modified to include the non-standard function, package.unload. In the above example it would be called as:

package.unload("LuaCLIPS.dll")

Version History

Version 1.3 - Final, Stable
Released: 18 June 2006
Lua version: 5.1.1, with the non-standard function package.unload
CLIPS version: 6.24

  • CLIPS version updated
  • Lua version updated
  • Lua stack overflow bug in printTrace() fixed, there can now be more than LUA_MINSTACK lines of CLIPS output
  • The load() function rewritten, to pass on error messages in verbose mode.
  • The CLIPSEngineClose() function rewritten to call DeallocateEnvironmentData() instead of DestroyEnvironment(). Fixing a memory leak.
  • The genexit() function in sysdep.c rewritten, to ignore CLIPS calls to (exit).
  • msvcrt.dll dynamically linked to, using the /MD compiler option in place of /ML. For compatibility with other libraries in the project.

Version 1.2
Released: 16 March 2006
Lua version: 5.1
CLIPS version: 6.23

  • Lua version updated
  • Uses Lua libraries with C++ name decoration, but exports to Lua scripts using (the prettier) C decoration
  • Version information added to DLL
  • Visual C++ project replaced, as Xiangjun Xu's site with the original Win DLL project files seems to have gone.

Version 1.1
Released: 11 March 2006
Lua version: 5.0.2
CLIPS version: 6.23

  • CLIPS printout function made standard (previously the 't' argument was omitted).

Version 1.0
Released: 27 January 2006
Lua version: 5.0.2
CLIPS version: 6.23

Links

BonSAI
CLIPS
Lua
LuaCLIPS at LuaForge

MIT/Expat License for LuaCLIPS

CLIPS as a Lua DLL, Copyright (C) 2006 Mike Kreuzer.
CLIPS as a Win DLL, Copyright (C) 2002 Xiangjun Xu.
Based on CLIPS which is public domain software.

The full license is avaliable here.