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

Close

pyclips embed in php

Help
ohnjay
2008-08-20
2013-04-25
  • ohnjay
    ohnjay
    2008-08-20

    helo,
    Here i am again.:)..Guys,can pyclips be embedded in php??

     
    • Johan Lindberg
      Johan Lindberg
      2008-08-20

      Hi,

      try http://sourceforge.net/projects/phlips/ instead. It is a PHP extension for CLIPS and allows deployment of CLIPS programs in PHP.

      HTH
      Johan Lindberg
      johan@pulp.se

       
    • ohnjay
      ohnjay
      2008-08-21

      I've tried phlips already. My problem is that the embedded clips won't have any output at all..:(..That's why i tried another one which was the pyclips...

       
      • Hi faithiez,

        as you probably know Python and PHP are different environments, although both can be used to develop web applications. One thing to keep in mind, though, is that web applications are generally "less stateful" than single-task applications, so in case you decide to develop an application for the web that uses CLIPS (I think that this applies to both PHP and Python) you also have to figure out a method to keep the state of CLIPS for subsequent calls from the same user... it's not an easy problem.

        F.

         
    • ohnjay
      ohnjay
      2008-09-02

      hi franz,

      Thanks for the infos.But do you have any idea how pyclips work in a web application?

       
    • Johan Lindberg
      Johan Lindberg
      2008-09-03

      Maybe this would be a good topic for an example, similar to what we've done for wxPython. WineDemo using Django or whatever the most popular Python web framework is nowadays.

       
    • ohnjay
      ohnjay
      2008-09-04

      Oh my...i'm getting confused. I'm in a great trouble. Anyways, i am still a student. That is why i'm a newbie on that kind of stuffs. I need some information from you. I'm having my thesis now about expert systems, and i was searching for a suitable or easy way to embed clips and make it as an web application.

       
    • Johan Lindberg
      Johan Lindberg
      2008-09-04

      Hi again,

      could you please be more specific as to *why* the PHLIPS module didn't work? You mention that it has no output at all. What have you tried and how do you think PHLIPS works (what were you expecting to happen)?

      The wrappers around CLIPS aren't always working in the way you might expect. Did you use (printout t ... ) in the RHS of a rule and expected it to end up in the HTTP Response? Or did you do something else?

      In general. You're going to have to write a program (in Python for PyCLIPS or PHP for PHLIPS) that:

      1) takes the input (the HTTP FORM data) from the HTTP Request,
      2) asserts it into CLIPS (possibly resets the engine as well, it depends),
      3) performs a (run),
      4) queries CLIPS for the results (usually with find-all-facts or something similar),
      5) formats the results into HTML,
      6) and writes it to the HTTP Response.

      there are some variations. For example, you might want to register Python functions that you call from within the RHS to accumulate results in a Python data structure (should make it easier to work with from the web framework) but in essence you're going to have to do something similar to the above regardless of whether you're using PHP or Python.

      HTH
      Johan Lindberg
      johan@pulp.se

       
    • ohnjay
      ohnjay
      2008-09-04

      helo,

      yes i used printout  t..

      this is my clips file:(but this is not my thesis file yet...hehe..this is just an example):

         ;;; ==============================================
         ;;;|           Family Tree System                 |
         ;;;|                                              |
         ;;;|     This program displays the relationship   |
         ;;;|     between the member of the family tree    |
         ;;;|                                              |
         ;;;|     To execute,merely load, reset and run.   |
         ;;; ==============================================

      ;;*****************************************
      ;;* Some initial lists of the family tree *
      ;;*****************************************

      (deffacts initial_facts
        (male John)
        (male Tom)
        (male Jerry)
        (male Danny)
        (male Roy)
        (male Kerr)
        (male Larry)
        (female Mary)
        (female Joy)
        (female May)
        (son Jerry John)
        (son Tom John)
        (daughter May Mary)
        (wife Mary John)
        (wife Joy Tom)
        (wife May Roy)
        (son Larry Roy)
        (son Kerr Roy)
        (son Danny Tom)
      )

      ;;;******************************************************
      ;;;* Rules for adding another facts for the family tree *
      ;;;******************************************************

      (defrule father
        (male ?father-name)
        (wife ?wife-name ?father-name)
        (or(or(son ?name ?father-name)(daughter ?name ?father-name))
           (or(son ?name ?wife-name)(daughter ?name ?wife-name)))
      =>
        (assert (father ?father-name ?name))
      )

      (defrule mother
        (female ?mother-name)
        (husband ?father-name ?mother-name)
        (or(or(son ?name ?mother-name)(daughter ?name ?mother-name))
           (or(son ?name ?father-name)(daughter ?name ?father-name)))
      =>
        (assert (mother ?mother-name ?name))
      )

      (defrule husband
        (male ?husband-name)
        (wife ?wife-name ?husband-name)
      =>
        (assert (husband ?husband-name ?wife-name))
      )

      (defrule married
        (or(wife ?wife-name ?husband-name)
           (husband ?husband-name ?wife-name))
      =>
        (assert (married ?husband-name ?wife-name))
      )

       
      (defrule self
         (or(and(father ?fathername ?childname)(father ?fathername ?childname))
            (and(mother ?mothername ?childname)(mother ?mothername ?childname)))
      =>
         (assert (self ?childname ?childname))
        )

      (defrule child
         (or(male ?name)(female ?name))
         (or(son ?name ?father-name)(daughter ?name ?mother-name))
         (married ?father-name ?mother-name)
        
      =>
         (assert (child ?name ?father-name ?mother-name))
      )

      (defrule grandfather
         (male ?grandfather-name)
         (or(and(father ?grandfather-name ?child)(father ?child ?grandson))
             (and(father ?grandfather-name ?child)(mother ?child ?grandson)))
        =>
         (assert (grandfather ?grandfather-name ?grandson))
        )

      (defrule sibling
         (or(and(and(father ?fname ?c1)(father ?fname ?c2))(not(self ?c1 ?c2)))
            (and(and(mother ?mname ?c1)(mother ?mname ?c2))(not(self ?c1 ?c2))))
         (and(child ?c1 ?fname ?mname)(child ?c2 ?fname ?mname))
      =>
         (assert (sibling ?c1 ?c2))
        )

      (defrule sibling2
         (and(sibling ?c1 ?c2)
             (not(sibling ?c2 ?c1)))
      =>
         (assert (sibling2 ?c1 ?c2))
      )

      ;;;**********************************************************
      ;;;* Rules for printing the informations of the family tree *
      ;;;**********************************************************

      (defrule printfathers
        (father ?father-name ?name)
      =>
        (printout t ?father-name " is the father of " ?name crlf)
      )

      (defrule printmarried
        (married ?husband-name ?wife-name)
      =>
        (printout t ?husband-name " and " ?wife-name " are couple... "crlf)
      )

      (defrule printgrandfather
        (grandfather ?grandf-name ?sibling-name)
      =>
        (printout t ?grandf-name " was the grandfather of " ?sibling-name "...." crlf)
      )

        (defrule printsiblings
         ;; (or(and(male ?firstname)(male ?secondname))(and(male ?firstname)(female ?secondname)))
         ;; (and(child ?firstname ?father-name ?mother-name)(child ?secondname ?father-name ?mother-name))
         (sibling2 ?firstname ?secondname)
      =>
         (printout t ?firstname" and " ?secondname " are sibling.."crlf)
      )

      This is my php file:
      <?php
         clips_init ( );

        clips_clear();
        clips_load("familytreefinal.clp");
        clips_reset();
        clips_run();

      ?>

      --this program should display
        Tom is the father of Danny
        Roy is the father of Kerr
        Roy is the father of Larry
        Kerr andLarry are sibling..
        Roy and May are couple...
        Tom and Joy are couple...
        John is the father of of Tom
        John was the granfather of Larry...  etc...

        but this won't display..the page displays nothing..:(

       
      • Johan Lindberg
        Johan Lindberg
        2008-09-04

        When you say "this program should display ..." is that because you've read that somehwere in the PHLIPS docs (or source) or is that because that's how you think it ought to work.

        I'm not sure that the CLIPS engine wrapped inside of PHLIPS has had it's stdout pipe routed to the "outside". PyCLIPS, for example, hasn't. At least not by default.

        I'm not going to pretend I know the innards of PHLIPS because I really don't. I'm just seeing parallells here with PyCLIPS and I'm trying to figure out what's actually happening. You can easily use PyCLIPS in a web environment but you can't use (printout t ...) as the communications channel from CLIPS to the "outside".

        I'll try to write up a web example (WineDemo or something similar) using PyCLIPS in the coming weeks.

        BR
        Johan Lindberg
        johan@pulp.se

         
        • Thank you Johan...

          I'll also try to write an example, in the form of a tutorial, on how to build a separate server for use in web applications, in order to avoid the problem of having to call one-shot CLIPS instances and keep separate sessions in separate environments. I think that one-shot applications - as in: create a CLIPS instance, load a .clp file, provide some output as a HTML page, destroy the CLIPS instance - is generally not a good approach: it wouldn't work for expert system like applications (think of AutoDemo, for example, where some user interaction is required) or for environments that require long running CLIPS instances.

          Since the matter of using PyCLIPS (and more generally CLIPS) in web applications seems to be of major interest, maybe a simple tutorial would help some people.

          Cheers,

          F.

           
    • ohnjay
      ohnjay
      2008-09-08

      helo,
            Actually its only my idea of what was the output.:) I just refer it when you load the clips file from the clips environment.. but i refered my functions to http://phlips.sourceforge.net/documentation.php.
          Anyways do you know how jclips work? What's the difference of jclips,phlips and pyclips? Which was more easy on implementing a web application?

       
      • Johan Lindberg
        Johan Lindberg
        2008-09-08

        Yes, Ok. I see. That would suggest then that PHLIPS works as PyCLIPS. You have to, *manually*, extract results from the CLIPS instance and print them to he HTTP Response stream.

        I *think* JCLIPS works the same way. But I honestly think you should use CLIPSJNI instead of JCLIPS. See the download page at http://clipsrules.sourceforge.net/ and CLIPS docs for more info.

        About being easiest to develop web apps in. That's a no-brainer. Stick with Python ;-)

         
    • ohnjay
      ohnjay
      2008-09-09

      Thank you very very much johan.. If i let you choose between jclips and pyclips? Which was more easier?..:)

       
      • Well, the question is quite unusual in a forum dedicated to a Python module... Since we are Python developers, we tend to think that Python (and therefore PyCLIPS) should be easier! There are several web sites, forums and other resources that advocate for Python versus Java or vice-versa. You choose what's more suitable to your needs and, why not, taste.

        In my opinion (the same as Johan, as you can see in a previous post) Python saves you from several hassles and issues that Java has, so as a beginner I'd choose Python if I were in your place. If you choose Java, however, try to use CLIPSJNI: the other project, JClips, seems not so active at the moment. Moreover, CLIPSJNI is developed by the same author of CLIPS, so it's surely very tightly integrated.

        In my opinion, you should follow Johan's advice, and try to implement a simple one-shot web application with PyCLIPS: embed Python in a web server and execute a simple program using the PyCLIPS module, then poll the CLIPS engine for a result and display it on the resulting web page. I try to show you a *really* simple example, supposing that you have mod_python configured and that your /var/www/python directory is handled by mod_python via a Python file called httest.py: refer to mod_python documentation (http://www.modpython.org to get the latest) on how to configure such a server.

        The following text has to be saved (obviously without the line numbers, reported here only to preserve formatting) in a file called /var/www/python/modpy_pyclips.clp:

        01  (defrule duck-rule
        02      (duck)
        03      =>
        04      (assert (quack)))
        05
        06  (reset)
        07  (assert (duck))

        And then you should prepare a Python file with the following contents, once again without line numbers, and save it as /var/www/python/httest.py:

        01  # sample web application
        02
        03  import clips
        04  from mod_python import apache
        05
        06  clips.BatchStar("/var/www/python/modpy_pyclips.clp")
        07  clips.Run()
        08
        09  def handler(req):
        10      req.content_type = "text/http"
        11      req.write("<h1>List of facts in PyCLIPS</h1><p></p>")
        12      f = clips.InitialFact()
        13      while f:
        14          req.write("<br>%s" % f.PPForm())
        15          f = f.Next()
        16      return apache.OK

        Then, if you point your browser to the server at the URL http://your-server/python/httest.py you will read a page with the following information:

        List of facts in PyCLIPS

        f-0 (initial-fact)
        f-1 (duck)
        f-2 (quack)

        (there will actually be some more formatting, of course). Well, this is the easy approach, that you can use when you don't need any kind of "wizard style" user input. You can use a form in a calling page to provide input to your program: again, see the mod_python documentation to learn how. As I wrote before, I'll work on a tutorial on how to use a separate server and session information to "statefully" give access to multiple PyCLIPS sessions.

        Of course you'll have to do something more to adapt your program/exercise to the simple example I provided, but this is what exercises are for, isn't it?

        I hope to have explained how easy it can be to develop a simple web application with PyCLIPS when you have some knowledge of how a web server works!

        HTH,

        F.

         
        • Sheesh... too bad, the SourceForge.net forum interface does not even *try* to preserve formatting, even when there's something at the beginning of a line! Fine, anyway Python formatting is quite straightforward, and CLIPS doesn't need much formatting apart from newlines.

          It should still be readable...

          Cheers,

          F.

           
    • ohnjay
      ohnjay
      2008-09-11

      helo franz,
          Thanks for the example.But before i test your sample, i used the information i found on http://www.modpython.org/live/current/doc-html/inst-testing.html. After i install the mod_python ,i brows the mptest.py to test if the mod_pyhton works but then it does not appear on the page. What was the problem?? I used xampp. When i restart the apache after installing the mod_python, it was busy and wouldn't start anymore...huhuhuh...what should i do?? What was my mistakes?? Please help me..
          In your example, does the httest.py works on the python environment/shell when you run it?? Because i try it running then an error occurs. The error was:
           Traceback (most recent call last):
           File "C:/Python25/httest.py", line 4, in <module>
           from mod_python import apache
          File "C:\Python25\Lib\site-packages\mod_python\apache.py", line   30,     in <module>
          import _apache
        ImportError: No module named _apache
         Please help me...I don't know what to do..

       
    • ohnjay
      ohnjay
      2008-09-11

      helo franz,
      Oh my your right SourceForge.net forum does not preserve formatting...lLook what happen with my message..

      Thanks for the example.But before i test your sample, i used the information i found on http://www.modpython.org/live/current/doc-html/inst-testing.html. After i install the mod_python ,i brows the mptest.py to test if the mod_pyhton works but then it does not appear on the page. What was the problem?? I used xampp. When i restart the apache after installing the mod_python, it was busy and wouldn't start anymore...huhuhuh...what should i do?? What was my mistakes?? Please help me..

      In your example, does the httest.py works on the python environment/shell when you run it?? Because i try it running then an error occurs.

      The error was:

      Traceback (most recent call last):

      File "C:/Python25/httest.py", line 4, in <module>

      from mod_python import apache

      File "C:\Python25\Lib\site-packages\mod_python\apache.py",

      line 30,    in <module>    import _apache

      ImportError: No module named _apache

      ================================================

         Please help me...I don't know what to do..

      Is it more readable then the first one??..

       
    • Hi,

      no, it will NOT work in the Python shell, you have to test it in a working Apache/mod_python environment. To do this you have to correctly configure mod_python for your Apache installation. This is not an easy operation, if you are a novice you might need to have someone to help you through the various steps. However Apache and mod_python configuration is outside the scope of this forum: mod_python is really well documented and I suggest you to thoroughly review the related documentation.

      F.

       
    • ohnjay
      ohnjay
      2008-09-11

      Thank you very much franz..I must configure this as soo as possible...:)