Import .clp and add facts

465 Views Asked by At

I am using python and Clips to solve an issue, here is what I am trying to do:

I am looking to load a .clp file from python and run it. I need to add facts based off a database as well. So the .clp file will have rules in it and I am using

clips.Load("myfile.clp")

to load my file. I am stuck on how to assert facts now into clips. I also have a varable final in clips that will store what it comes out with based on the facts. I need to bring that back to python to run other code.

Thanks

1

There are 1 best solutions below

0
On

I assume you are using PyCLIPS.

import clips

def clips_callable(f):
    def wf(*args, **kwargs):
        if f(*args, **kwargs):
            return clips.Symbol("TRUE")
        else:
            return clips.Symbol("FALSE")
    clips.RegisterPythonFunction(wf, f.__name__)

@clips_callable
def pyprint(s):
    print s
    print "".join(map(str, s))


clips.Load("test.clp")
clips.Reset()
clips.Run()

# assert a fact.
a = clips.Assert("(order (part-id p1) (quantity 20))")
clips.Run()

test.clp looks something like:

(deffunction MAIN::print ($?value) 
    (python-call pyprint ?value)
;   (printout t ?value)
)

(deftemplate MAIN::order
    (slot part-id)
    (slot quantity)
)

I included the @clips_callable decorator as a bonus, which makes it very easy to call a python function from clips.