Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

How to interact

Help
edereje
2008-01-31
2013-04-25
  • edereje
    edereje
    2008-01-31

    Hi all,
    I am new to python and clips. In the last couple of months i was trying to play with CLIPS. These days i am looking for user friendly GUI to link with my CLIPS program. I have been doing simple things with wxpython, pyclips and clips.I managed to assert facts from the GUI developed with wxpython and able to run the clips program.
    To implement my idea i took a fictious example here, say we want a program that can find spouse for a person.
    I want my program to include images, and i have no idea to include this in CLIPS,but this is possible in wxpython.
    The deftemplat created is as follows:
      (deftemplate person
       (multislot name)
       (multislot age (default 30))
       (multislot gender))

    (deftemplate spouse
       (multislot name)
       (multislot age)
       (multislot gender)
       (slot picture))
    and the rules:
    (defrule report
      (person (name ?name)(age ?age)(gender ?sex))
    =>
      (printout t "Your personal details are: " crlf)
      (printout t "Name :" ?name " Age: " ?age " Gender: " ?sex crlf))

    (defrule Mate-male
      (person (name ?pname)(age ?page)(gender male))
      (spouse (name ?sname)(age ?sage&:(> ?page ?sage))(gender female))
    =>
      (printout t ?pname " can marry " ?sname  crlf))
    (defrule Mate-female
      (person (name ?pname)(age ?page)(gender female))
      (spouse (name ?sname)(age ?sage&:(< ?page ?sage))(gender male))
    =>
      (printout t ?pname " can marry " ?sname  crlf))
     
    after i include deffacts for spouse, i managed to use the GUI and assert facts for the person and managed to run the program. But my additional intension was to show the pictures of each selected spouse for each fired rule, so that the user can see also the pictures.
    i managed to see the pictures in the GUI, when it is programmed statically, that means, when the user selects one of the choice item, then he/she can see the image but this is static, and has no relation with fired rule.
    here i want to integrate this two, when the rule is fired and say x, and y are candidates to marry z, the pictures of x and y can be shown; this may be on Grid or one at a time.
    can any one help me to figure out this.
    I was also looking for how to obtain facts that may be included in Clips after a rule is fired in python and use that fact for firing other rules or doing other things in python.
    Thank you in advance for your help.
    Dereje

     
    • Johan Lindberg
      Johan Lindberg
      2008-01-31

      Hi Dereje,

      > But my additional intension was to show the pictures of each selected
      > spouse for each fired rule, so that the user can see also the pictures.
      > i managed to see the pictures in the GUI, when it is programmed statically,
      > that means, when the user selects one of the choice item, then he/she can
      > see the image but this is static, and has no relation with fired rule.

      You have to find a way to transport the value from a fired rule to the GUI. Would you consider adding a deftemplate that holds all of those values? Instead of printing to stdout you could assert a "match" fact that either holds the fact address or just copy the slots you're interested in.

      (defrule Mate-male
        (person (name ?pname)(age ?page)(gender male))
        (spouse (name ?sname)(age ?sage&:(> ?page ?sage))(gender female)(picture ?pict))
        =>
        (assert (match (name ?sname) (age ?sage) (picture ?pict))))

      if you change your other rule as well you should end up with several match facts after reset and run.

      Then, in the GUI, you can do (NOTE! This is pseudo-code I haven't tested it so you may have to fiddle with it to get it working):

      for match in clips.Eval("(find-all-facts ((?m match)) TRUE)"):
        name= match.Slots["name"]
        # update name field
        age= match.Slots["age"]
        # update age field
        picture = match.Slots["picture"]
        # construct and display the image here 
        ...

      Using the above transport mechanism means that you *have to* reset and run whenever the user asks for a result. But that shouldn't make it troublesome for you anyway.

      Study the WineDemo GUI example. It uses exactly this technique and look especially at the calculateRecommendation method and it's basic cycle of:
      1) reset
      2) assert data from GUI as facts (input facts)
      3) run
      4) query result data (output facts) using find-all-facts or similar
      5) modify the GUI

      HTH
      Johan Lindberg
      johan@pulp.se

       
      • edereje
        edereje
        2008-01-31

        Thank you Johan for your endlesss support. Before i posted my problem, i have tried to understand the Auto Demo. However, i could not figure out it this way. I will try this approach again and inform you my progress. Thank you once again for your help.
        with regards,
        Dereje

         
        • Johan Lindberg
          Johan Lindberg
          2008-01-31

          > Before i posted my problem, i have tried to understand the Auto Demo. However,
          > i could not figure out it this way.

          Well the Auto Demo is quite different. It uses a decision tree to select the "next" question to the user and may or may not depend on the previous answers when responding. This makes it neccessary *not to* reset between user interactions. If I understood you correctly you ask the user once and then give an answer, right? That's what the Wine Demo does as well and that's why it's much more relevant to study. In fact, I'm guessing you could use the recommendation method from Wine Demo as a template for your code.

          Good luck
          Johan Lindberg
          johan@pulp.se

           
    • edereje
      edereje
      2008-01-31

      Hello Johan again,
      I had one problem in understanding wine demo, the first thing is regarding wx.xrc can i get any reference on it to read and understand how it works? the second one is the file i downloaded from the new pyclips website which includes all the three examples, the wine demo is not working for me, i got the following error:
      Traceback (most recent call last):
        File "F:\Python\PyCLIPS-wxPy-Examples\WineDemo\WineDemo.wx.py", line 228, in <module>
          app= WineDemo(0)
        File "C:\Python25\Lib\site-packages\wx-2.8-msw-ansi\wx\_core.py", line 7836, in __init__
          self._BootstrapApp()
        File "C:\Python25\Lib\site-packages\wx-2.8-msw-ansi\wx\_core.py", line 7433, in _BootstrapApp
          return _core_.PyApp__BootstrapApp(*args, **kwargs)
        File "F:\Python\PyCLIPS-wxPy-Examples\WineDemo\WineDemo.wx.py", line 74, in OnInit
          self.calculateRecommendation(None)
        File "F:\Python\PyCLIPS-wxPy-Examples\WineDemo\WineDemo.wx.py", line 203, in calculateRecommendation
          for fact in clips.Eval("(get-wine-list)"):
        File "C:\Python25\Lib\site-packages\clips\_clips_wrap.py", line 3430, in Eval
          return _cl2py(_c.eval(expr))
        File "C:\Python25\Lib\site-packages\clips\_clips_wrap.py", line 353, in _cl2py
          raise TypeError("list cannot be converted")
      TypeError: list cannot be converted,
      is it because of my system or any other reason? for the time being i have downloaded the same example from your blog site and that one is working properly.
      with regards,
      Dereje

       
    • edereje
      edereje
      2008-01-31

      Hi Johan again,
      I figured out the second problem above, I think the problem is with my installation, because when i try all the examples, they are not working. However, when i test the programs on another pc it is working, which indicates that there is something missing on this pc. I will uninstall the packages and reinstall it. Sorry for bothering you.
      with regards,
      Dereje

       
      • Johan Lindberg
        Johan Lindberg
        2008-01-31

        > I figured out the second problem above, I think the problem is with my
        > installation, because when i try all the examples, they are not working.
        > However, when i test the programs on another pc it is working, which
        > indicates that there is something missing on this pc. I will uninstall
        > the packages and reinstall it.

        Ok, good. You had me worried there at first ;-)

        > I had one problem in understanding wine demo, the first thing is regarding
        > wx.xrc can i get any reference on it to read and understand how it works?

        XRC is the XML format for describing GUI layouts used in wxWidgets (and wxPython). I used the XRCEd tool to design the GUI. You can load the XRC file from the Wine Demo and modify it if you want. If you have downloaded the docs and demos from wxpython.org it should show up in your Start menu as Resource Editor. You can also get it directly from http://xrced.sourceforge.net/

        The only problem with XRC is that it cannot handle ALL widgets so you might be better off doing it by hand. It *does* reduce the lines of code you need to write though, so I try to use it as much as possible.

        You can read more about XRC in the wxWidgets docs or at http://www.wxpython.org/docs/api/wx.xrc-module.html

         
      • Hi Dereje,

        for what I can see, it looks like you're using an older PyCLIPS version: I had to add some functionality (which was really needed) to allow lists of facts to be exported from the "CLIPS world" to Python, and Johan could clean up the examples code removing some steps that were necessary to convert lists of facts. At the time the only PyCLIPS version that works with Johan's examples is 1.0.6: it should be easy to replace an older one.

        I hope this will solve the problem.

        Cheers,

        F.

         
    • edereje
      edereje
      2008-02-02

      Hi all,
      I found xrced to be interesting even though i need to practice it. With the previous recommendations i am trying to learn more. But i got some problem in reading facts slots from clips, they are returnd in multifileld from. For example here is slot value i got
      <Multifield [<Symbol 'kasech'>]>, i want to get the string 'kasech' which i later use for some other event handling purpose in my program. I have tried to use PPForm, but could not able to extract the required slot value. can any one guide me how to get the slot value the way i want it to be?
      Thank you in advance for your help.
      with regards,
      Dereje

       
      • Hi Dereje,

        Multifield, Symbol, String, InstanceName and the like are just "wrapper classes" that are used to distinguish the types in CLIPS from the ones in Python, but they extend the equivalent ones in Python. In the specific case a Multifield is much like a list, but is wrapped in a different type just to let you know that it comes from CLIPS.

        To get the name "kasech" from your example, let's suppose you have the multifield stored in a Python variable (let's call it m):

        >>> m
        <Multifield [<Symbol 'kasech'>]>
        >>> m[0]
        <Symbol 'kasech'>
        >>> str(m[0])
        'kasech'
        >>>

        Using PPForm is not always suitable to extract values for several reasons: one of them is that the PPForm in some cases could be incomplete, due to the use of a limited buffer in the PPForm creation. Moreover, pretty-print forms can be difficult to parse.

        F.