Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

PyCLIPS

Help
2009-01-23
2013-04-25
  • Sofia Puerto
    Sofia Puerto
    2009-01-23

    Hi I would like to know how do I use a function defined in a .clp file into PyCLIPS?

    Thanks,
    Sofia

     
    • Hi,

      Let's have a file, "simplefun.clp" containing the sign example of Basic Programming Guide, page 190:

      (deffunction
          sign (?num)
          (if (> ?num 0) then (return 1))
          (if (< ?num 0) then (return -1))
          0)

      Now, we want to call the function from Python. Quite easy, you just Load() the file and use the Call() API:

      >>> import clips
      >>> clips.Load("simplefun.clp")
      >>> clips.Call("sign", 42)
      <Integer 1>

      That said, PyCLIPS provides more "pythonic" ways to use CLIPS functions:

      >>> sign = clips.FindFunction("sign")
      >>> sign.Call(0)
      <Integer 0>

      that is, the Function object itself has an OO Call() member function. PyCLIPS goes further, as Function objects can be directly called on provided arguments:

      >>> sign.Call(-13)
      <Integer -1>

      See paragraph 3.8 (Function) of the PyCLIPS manual here:

      http://pyclips.sourceforge.net/manual/pyclips-objects.html#SECTION005800000000000000000

      for more details on Function objects.

      Cheers,

      F.

       
      • Sofia Puerto
        Sofia Puerto
        2009-01-27

        I have been trying to import this function:

        http://clipsrules.wiki.sourceforge.net/CSV.CLP

        and I get this error:

        >>> clips.call("CVS-import", "BRIGE-GOAL.csv" goal)

        SyntaxError: invalid syntax

        >>> clips.call("CVS-import", "BRIGE-GOAL.csv" 'goal')

        Traceback (most recent call last):
          File "<pyshell#11>", line 1, in <module>
            clips.call("CVS-import", "BRIGE-GOAL.csv" 'goal')
        AttributeError: 'module' object has no attribute 'call'

        I don't know if still have a syntax error. 

        Thank you a lot for your help,

        Sofia

         
        • Hi Sofia,

          actually the form you use is _not_ Python at all. Python and CLIPS "live" in different spaces, and they don't mix easily: I tried to make CLIPS more Pythonic, but the opposite can only be achieved by making Python a CLIPS library - which was not my goal, even though it comes for free as a (pursued) "side effect" in PyCLIPS.

          Look:

          >>> clips.call("CVS-import", "BRIGE-GOAL.csv" goal)
          Here you have a call (I'll return here later) to a Python method that passes actual parameters, but there's no comma (the Python parameter separator) between the string "BRIGE-GOAL.csv" and the identifier <goal>. Python complains: it's a syntax error in Python, not in CLIPS. CLIPS is not even reached. So you tried this:

          >>> clips.call("CVS-import", "BRIGE-GOAL.csv" 'goal')
          This is an old C trick: when you write two string literals one after another, they are automatically concatenated by the C compiler (and in Python by the Python interpreter) before anything else. So, if you write in Python:
          >>> name = "sofia" 'puerto'

          ...then you get:
          >>> name
          'sofiapuerto'

          so your call to clips.call("CVS-import", "BRIGE-GOAL.csv" 'goal') would have been the same as clips.call("CVS-import", "BRIGE-GOAL.csvgoal") (yes, exactly this) which is not what you wanted. However it's perfectly legal in Python, as a matter of syntax. But... there is _no_ "clips.call" function in PyCLIPS, only "clips.Call". In other words, Python is case sensitive, as you can find out in http://www.python.org/doc/2.6/reference/lexical_analysis.html#identifiers-and-keywords (strangely, it's not discussed elsewhere).

          However, probably you wouldn't have succeeded even if you spelled it the right way:

          >>> clips.Call("CVS-import", "BRIGE-GOAL.csv" 'goal') # notice the upper case 'C'

          because, as stated in http://pyclips.sourceforge.net/manual/pyclips-toplevel.html#SECTION004120000000000000000 (look for the syntax of the "Call()" API) you should provide, as a second argument to Call:

          - "a [string enclosing a] list of arguments separated by blank characters using CLIPS syntax" (in fact, I wrote "the argument string" in the manual), in your case it could have been a string whose Python form is something like: '"BRIGE-GOAL.csv" goal', notice the wrapping single quotes and the double quotes around the file name: the file name is interpreted as a string, <goal> as a SYMBOL)

          - a sequence of CLIPS understandable values, such as a tuple: remember that you want <goal> to be a SYMBOL, so your tuple will have to be something like (clips.String("BRIGE-GOAL.csv"), clips.Symbol("goal")) and your call would be: clips.Call("CVS-import", (clips.String("BRIGE-GOAL.csv"), clips.Symbol("goal"))) instead of what you tried to write.

          Please, take your time to review the CLIPS User Guide and the CLIPS Basic Programming guide for more instructions on how CLIPS treats basic types, such as lexemes and various other literals: it will also help you understand how PyCLIPS works.

          HTH,

          F.

           
          • Sofia Puerto
            Sofia Puerto
            2009-01-28

            Thank you a lot for the information I'm a civil engineer and obviously I'm not an expert in programing.

            I would love to have more information related with "the Python DB API to store stuff in a DB" that you mention before, because what I have is a list of facts that I want to store somewhere and be able to modify them.

            So if you can recommend me a web page where I can find some information I'll appreciate it.

            Thanks,
            Sofia

             
        • Also...

          from another point of view, if what you need is to store your KB in a file or some other kind of permanent storage, and your KB is made out of rules which are similar to each other (possibly in clusters, too) you can let Python do the dirty work of writing and reading a file in a format you like (or use the Python DB API to store stuff in a DB) and create the rules from the file/DB with appropriate Python calls to the CLIPS API using PyCLIPS: actually, that is the primary purpose of PyCLIPS itself.

          Cheers,

          F.

           
    • Hi Sofia,

      it took me a little time, but finally I've come up with a little example in the form of a tutorial. You can find it here:

      http://pyclips.sourceforge.net/web/?q=node/25

      I hope it will help you and the many others that ask for something like this.

      F.

       
    • Sofia Puerto
      Sofia Puerto
      2009-02-09

      Hi F., The information that you have published has been really useful, thank you. I am wondering now if it is possible to create a rules DB. Can PyClips construct rules from a DB?.

      Thanks,

      Sofia

       
      • Of course you can. PyCLIPS allows to build whatever construct you like in a programmatic way (for instance, there is the clips.BuildRule() function to create rules) so you can store in a DB the parts you need to build rules too (eg. parts of the LHS and RHS of rules).

        F.

         
        • Sofia Puerto
          Sofia Puerto
          2009-02-12

          Hi Franz, I am sorry to ask you so many questions. I am a Master Civil Engineering student and I'm working on my Thesis, which consist on creating an expert system for bridge emergency situations. I have been working mainly in clips but I am trying to find the best way to interact with the user and at the same time I want that my expert system learn from the experience. Searching on line I found Python, Pyclips and wxpython which I thing are a good option for what I want.

          So now I see that as you said, it is possible to create a rule DB and build rules from pyclips, which is exactly what I need, because I have a bunch of rules in clips, that basically ask questions and depending on your answer lead you to another question or give you and advise.

          I tried to follow the example that you put on pyclips page but I don't have sqlite module:

          >>> import sqlite as db

          Traceback (most recent call last):
            File "<pyshell#2>", line 1, in <module>
              import sqlite as db
          ImportError: No module named sqlite

          Where can I find it? where can I find more information related to this module? manuals, examples, etc. I am really interested.

          Thank you,

          Sofia

           
          • Hi Sofia,

            in the example it reads:

            import sqlite3 as db

            (and not just "import sqlite as db"). In fact the module is part of the standard Python library as of Python 2.5; the documentation about this module is included in the Python documentation. As a matter of storing rules in a DB, you have to either find common patterns to your rules in order to be able to build them from static data, or store the entire rules as strings in database tables. Obviously the latter solution is less elegant and possibly makes the DB too tightly coupled to the CLIPS "subprogram", which IMHO is confusing and may lead to mistakes.

            Such a complex system should be carefully designed, in order to focus on where to store what and in which form: it could be quite a difficult task. Since it's your thesis, try to design the database in the best possible way, and if your rules are not really dynamic maybe you might prefer to store them in .clp files rather than database tables; if you have to build a big number of rules that, for instance, differ in terms of "parameters" (literal values: numbers, strings, symbols), you might want to find a way to build them from a template.

            F.

             
            • Sofia Puerto
              Sofia Puerto
              2009-02-12

              Hi Franz, Thank you. Yes I have a common pattern, I defined the same pattern for all my rules with a template and actually I have them organized in a excel file but as far as I know I cant built rules in clips so that is why I am trying to find a way to built them in PyClips. Is there any example where I can see how clips.BuildRule() function works, calling LHS and RHS from a DB?

              Thanks again,
              Sofia

               
              • Hi,

                if you have a common pattern for rules, then you'll probably have a fixed part and a variable part for each of them. The BuildRule() function takes 3 to 4 parameters, that is:

                - the rule name (a SYMBOL) as a Python string
                - the LHS including parentheses as a Python string
                - the RHS including parentheses as a Python string
                - an optional comment

                as stated in the PyCLIPS documentation. Please read the documentation about string interpolation at http://docs.python.org/library/stdtypes.html#string-formatting-operations for more information on how to build strings from templates in Python. Once you retrieve the variable parts of your rules from the database, use templates and interpolation to build the LHS and RHS of the actual rules, and possibly the rule name too. Then, for each rule, call the BuildRule() function on the obtained name, LHS and RHS as per PyCLIPS documentation and you're done.

                I think it shouldn't be too difficult to use this method to populate the CLIPS engine from Python using PyCLIPS. I don't know of any pre-built examples for this application around in the Internet, at least as publicly available code. However, Python is quite an easy language to learn, and if you grok it at least a little you'll soon be able to build all the rules you need, no matter from where you want to collect the data that constitute their variable parts, be it from a database, a CVS file or whatever binary/XML/flat format you want to implement or use for their persistent storage. I don't know if there is a Python module to read Excel files, but if there is one you could even possibly keep your rules in Excel format!

                However the PyCLIPS forum is not the right place to discuss the use of persistence to store and retrieve strings, or the use of templates to build custom strings. The Python tutorial already has detailed information on subjects such as file I/O and string formatting: please take your time to review it as well as the Python manual, to refine your knowledge about Python. Since you're relatively new to Python, from what I can see, you probably need to focus a little more on this aspect of your project.

                F.