Search code examples
pythonclipsclipspy

Clips empty error in ClipsPy when using make_instance


I am new to CLIPS and to clipsPy. I am trying to create a instance of a CLIPS class

This is the class I have defined and correctly build in my python environment (clipsPy)

ENTITIES_CLASS = """
(defclass ENTITY-CLASS (is-a INITIAL-OBJECT)
    (slot text (type STRING))
    (slot confidence (type FLOAT))
    (slot type (type SYMBOL))
)
"""
env.build(ENTITIES_CLASS)

This works as expected, but when I try to create an instance of this class:

new_instance = "(ent0-0 of ENTITY-CLASS (text 'Bruce Springsteen')(confidence 1.0)(type PER))"
env.make_instance( new_instance )

I get this empty error:

enter image description here

I have tried multiple forms of building the new_instance string but none have work :

new_instance = '(ent0-0 of ENTITY-CLASS (text "Bruce Springsteen")(confidence 1.0)(type PER))'
new_instance = "(ent0-0 of ENTITY-CLASS (text 'Bruce Springsteen') (confidence 1.0) (type PER) )"

Where is my syntax error? I appreciate any help


Solution

  • The empty error issue might be due to how Jupyter re-directs IO.

    On IPython I get:

    In [1]: import clips                                                                                                                                                                                               
    
    In [2]: env = clips.Environment()                                                                                                                                                                                  
    
    In [3]: ENTITIES_CLASS = """ 
       ...: (defclass ENTITY-CLASS (is-a INITIAL-OBJECT) 
       ...:     (slot text (type STRING)) 
       ...:     (slot confidence (type FLOAT)) 
       ...:     (slot type (type SYMBOL)) 
       ...: ) 
       ...: """ 
       ...: env.build(ENTITIES_CLASS)                                                                                                                                                                                  
    
    In [4]: env.make_instance("(ent0-0 of ENTITY-CLASS (text 'Bruce Springsteen')(confidence 1.0)(type PER))")                                                                                                         
    
    ---------------------------------------------------------------------------
    CLIPSError                                Traceback (most recent call last)
    <ipython-input-4-92b62ecc6bed> in <module>
    ----> 1 env.make_instance("(ent0-0 of ENTITY-CLASS (text 'Bruce Springsteen')(confidence 1.0)(type PER))")
    
    /usr/local/lib/python3.6/dist-packages/clips/classes.py in make_instance(self, command)
        215         ist = lib.EnvMakeInstance(self._env, command.encode())
        216         if ist == ffi.NULL:
    --> 217             raise CLIPSError(self._env)
        218 
        219         return Instance(self._env, ist)
    
    CLIPSError: [INSFUN7] ('Bruce Springsteen') illegal for single-field slot text of instance [ent0-0] found in put-text primary in class ENTITY-CLASS. [PRCCODE4] Execution halted during the actions of message-handler put-text primary in class ENTITY-CLASS
    

    The problem lies within the way you represent the string 'Bruce Springstreen'. In CLIPS, STRING types are within doublequotes ".

    In [4]: env.make_instance('(ent0-0 of ENTITY-CLASS (text "Bruce Springsteen")(confidence 1.0)(type PER))')                                                                                                         
    Out[4]: Instance: [ent0-0] of ENTITY-CLASS (text "Bruce Springsteen") (confidence 1.0) (type PER)