Search code examples
clipspyclips

PyClips clips.BatchStar() and Clips (batch ...) do not work together


I have three files:

1.) A python filetest.py:

import clips
PATH_TO_CLP_FILE = r'd:\temp\batch_bug.clp'
clips.BatchStar(PATH_TO_CLP_FILE)
clips.PrintFacts()

2.) A file batch_bug.clp:

(assert (asdf0))
(batch "D:\\temp\\batchbug2.clp")
(assert (asdf1))
(printout t (facts))

And finally a file batchbug2.clp:

(assert (fdsa))


Running python test.py results in the following output:

f-0     (initial-fact)
f-1     (asdf0)
f-2     (asdf1)
For a total of 3 facts.

[ENVRNMNT8] Environment data not fully deallocated.

[ENVRNMNT8] MemoryAmount = 22.

[ENVRNMNT8] MemoryCalls = 1.

This is not what I expected because there is no (fdsa) fact, i.e. calling (batch "D:\\temp\\batchbug2.clp") did not work. In addition, there are the [ENVRNMNT8] messages. What is going on here?

EDIT:

I found out about the CLIPS batch*(...) command. And using this instead of the batch(...) works as expected. Why is that?


Solution

  • The batch command opens a file and uses its contents when requests are made for characters from standard input (the keyboard). When you're using CLIPS interactively, character requests are made from standard input by the read/evaluate/print loop (the CLIPS> command prompt) as well as any read or readline function calls in your code.

    When you embed CLIPS as in done in this PyCLIPS example, there is no read/evaluate/print loop so the only requests for characters from standard input are going to come from the read and readline functions. Since this example doesn't make any character requests from standard input, the contents of the batch file is never processed.

    The batch* command opens a file, directly parses its contents for commands, and then immediately executes them--No requests for characters from standard input need to be made for the commands to execute.

    The batch command is useful for running test cases from the command prompt because you can simulate all keyboard input. The batch* command is useful for running command scripts when you don't have immediate access to the command prompt (either because you're embedding CLIPS or CLIPS is currently executing) or you don't want the executed commands and their return values echoed to standard output.

    The ENVRNMNT8 error message occurs when CLIPS exits and it determines that all allocated memory has not been properly freed. In this case, there's a bug in the deallocation code for an unprocessed batch file that doesn't free a string containing the batch file name (for CLIPS 6.30). A fix has been checked into the CLIPS SVN repository at SourceForge.