Interfacing CLIPS with C++

Now that we have learned the basics of CLIPS, we need to interface CLIPS source code with C++. Doing so will allow us to easily control our CLIPS program and let us provide easier input and output for CLIPS.

Configuration

The user must first install the packages mentioned in the previous post, as the are necessary to support certain features of the clipsmm package. Once this is done, we can download the clipsmm package from the sourceforge link here: http://sourceforge.net/projects/clipsmm/files/latest/download.

Change your directory to where the file was downloaded and extract the files. Once this is done, we must cd into the folder and perform the following commands:

autogen.sh

Now, the ./configure command will not work unless if the version of CLIPS used is 6.30 or higher. If you happen to be using an older version, download version 6.30 here.

With the proper version of CLIPS installed, we can change directories back to our clipsmm folder, and again type the following commands:

./configure
make
sudo make install

Once this has successfully been done, we have installed the clipsmm C++ interface. To test if it works, change directories to the clipsmm-0.3.4/examples/facts path and type the following commands:

make clean
make
cd facts
./facts_simple

If you do not see an error, congrats! clipsmm now works on your machine. If you encounter something like “libclips.so.2: cannot open shared object file: No such file or directory“, the system cannot find the dynamically linked library. To fix this, add the following commands to .bashrc in your home directory:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH":/usr/local/lib

and then re-run the shell script by:

source .bashrc

The program should now execute.

Writing Your Own Program

We can load CLIPS source code into a C++ program using enviroment.load("filename.clp"). From this, we can access various information from the CLIPS program using the clipsmm built-in commands, such as environment.assert_fact("(fact information)"), environment.get_facts(), and environment.run(-1). environment.get_facts() returns the initial fact in the fact-list, and using the .next() method we can access the next fact, etc. in the form of a linked-list. We can access the data in each fact by using the slot_names() method, which returns a vector of strings with each argument in the fact.

The example code given uses “If <statement A>, then <statement B>” sentences as input, and outputs all possible if/then statements upon a ‘print’ command, is available here.  The CLIPS source code is contained in the ifthen.clp file, and the C++ interface is contained in the facts_simple.cpp code.

Here is an example use of the program:

$ make
$ sudo make install
$ ./facts_simple
Please enter "If <cause>, then <effect>" statements.Type "exit" when finished.
Type "print" to display all statements.
>If A, then B
>If B, then C
>If C, then D
>print
If A, then B
If B, then C
If C, then D
If B, then D
If A, then D
If A, then C
>exit
$

The source code can be found here: https://www.dropbox.com/sh/ds5f9ns4kj9jynz/AABUi_R5d6SIJ_owT4sE4sI9a?dl=0.