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

Help in getting interactive session to work

Help
2007-10-16
2013-04-25
  • speleolinux
    speleolinux
    2007-10-16

    Hi all

    I have a CLIPS system that works OK when done via CLIPS interface but I can't get the hang of moving it Python. What happens below is that the user is asked "Is the sample Cellular (C), etc.. " but even if one enters C the next question "Is the sample Mammalian cells ... " is not asked. The output indicates that
    After Run
    f-1     (sample-composition "C")

    but the next rule is not invoked. The python script just ends.

    import clips

    def clips_raw_input(prompt):
        return clips.String(raw_input(prompt))
    clips.RegisterPythonFunction(clips_raw_input, "raw_input")

    print "\n--- CLIPS ---"
    clips.Eval('(printout t "Hello, World!" crlf)')
    print clips.StdoutStream.Read()

    clips.Reset()

    clips.Build("""
    ; Sample Composition for Protein Extraction
    (defrule get-sample-composition ""
        (not (sample-composition ?))
        =>
        (bind ?sample-composition (python-call raw_input "Is the sample Cellular (C), tissue sample (T) or body fluid (B) ? "))
        (assert (sample-composition (upcase ?sample-composition)))
    )
    """)

    clips.Build("""
    ; Protein Extraction for Cellular Samples
    (defrule get-cell-type""
        (sample-composition C)
        =>
        (bind ?cell-type (python-call raw_input "Is the sample Mammalian cells (M) or other (O) i.e. Fungal, plant or bacterial cells ? "))
        (assert (cell-type (upcase ?cell-type)))
    )
    """)

    print "Before Run"
    clips.PrintFacts()

    clips.Run()
    print "\n"

    print "After Run"
    clips.PrintFacts()
    print "\n"

     
    • Hi speleolinux!

      I think I've found the reason why it doesn't work in Python... from your output, you might notice that a STRING (clips.String) object is returned to the CLIPS engine, as shown by the double quotes in the fact representation:

      f-1 (sample-composition "C")

      whereas your rules test for SYMBOLs (clips.Symbol). This is because the definition of clips_raw_input "casts" the result to a string (this happens also if the cast is not explicit, because Python's raw_input() normally returns a string). A way to handle this without modifying the CLIPS code, can be to redefine the input routine as follows:

      def clips_raw_syminput(prompt):
          return clips.Symbol(raw_input(prompt))
      clips.RegisterPythonFunction(clips_raw_syminput, "raw_input")

      (sorry for missing indentation) leading to the following result:

      >>> ================================ RESTART ================================
      >>>

      --- CLIPS ---
      Hello, World!

      Before Run
      f-0     (initial-fact)
      For a total of 1 fact.
      Is the sample Cellular
      (C), tissue sample (T) or body fluid (B) ? C
      Is the sample Mammalian cells (M)
      or other (O) i.e. Fungal, plant or bacterial cells ? O

      After Run
      f-0     (initial-fact)
      f-1     (sample-composition C)
      f-2     (cell-type O)
      For a total of 3 facts.

      >>> ================================ RESTART ================================
      >>>

      --- CLIPS ---
      Hello, World!

      Before Run
      f-0     (initial-fact)
      For a total of 1 fact.
      Is the sample Cellular
      (C), tissue sample (T) or body fluid (B) ? T

      After Run
      f-0     (initial-fact)
      f-1     (sample-composition T)
      For a total of 2 facts.

      >>>

      ...which is, I think, what you expected. Maybe a more elaborate input routine (eg. one that checks the input to be at least compatible with SYMBOL values) can also be useful. From a Python point of view, in my opinion, it would be more correct to handle input values as strings in the CLIPS code too.

      I hope this helps!

      Cheers,

      F.

       
    • speleolinux
      speleolinux
      2007-10-25

      Hi Francesco

      Thanks so much. Now I know what the problem was. I was effectively comparing a C and a "C" and so the pattern didn't match and the rule didn't fire. Once knowing that I was also able to fix it by changing in the rule the pattern match from (sample-composition C) to (sample-composition "C").

      So thanks for getting back to me. I'll prob have more questions later as I'm trying to get CLIPS to work from within a Python Django application.

      Mike