How to use PyCLIPS for interactive sessions?

446 views Asked by At

My clip file contains (printout t"text") and bind ?var (read) statements. If I run the .clp file from below C code, the program prints to console and reads my input from console as expected, thus making it an interactive session between program and the user.

#include "clipscpp.h"
#include <iostream>
#include<stdlib.h>
using namespace std;
int main()
  {
   CLIPS::CLIPSCPPEnv theEnv;

   theEnv.Load("KB.clp");
   theEnv.Reset();
   theEnv.Run(-1);
   return 0;
  }

But, If I try to load .clp in python using PyCLIPS, neither (printout) nor (read) works and the program terminates without doing anything.

import clips
clips.Load("KB.clp")
clips.Clear()
clips.Reset()
clips.Run(-1)

How can I achieve the same result as running from C ?

2

There are 2 answers

0
kombinatorix On

I would suggest something like that:

def parse_trace_stream(trace_stream):
    ...
    """Print trace_stream"""
def parse_stdout_stream(stdout_stream):
    ...
    """Print stdout_stream"""
def parse_error_stream(error_stream):
    ...
    """Print error_stream"""

def evaluate(str):
    if str.count("(") == str.count(")"):
        try:
            clips.Eval(str)
        except:
            parse_error_stream(clips.ErrorStream.Read())

        parse_stdout_stream(clips.StdoutStream.Read())
        parse_trace_stream(clips.TraceStream.Read())

After that, you can write more CLIPS-like code:

evaluate("(load KB.clp)")
evaluate("(clear)")
evaluate("(reset)")
evaluate("(run -1)")
0
Gary Riley On

The FAQ for PyCLIPS (http://pyclips.sourceforge.net/web/?q=view/faq) suggests that you use Python functions for handling I/O.