How can i run this in python???

Help
ohnjay
2009-02-19
2013-04-25
  • ohnjay

    ohnjay - 2009-02-19

    hello guys

    please help me...how could i run this three files?? In the clips invironment this would run. This is an expert system. It diagnosis diseases... This are the files..

    *********************    cocomed.clp    *********************
                        

    (defmodule MAIN (export ?ALL))
    (deffacts MAIN::init
    (start)
    )

    (defrule MAIN::initialize
    (declare (salience 1))
    ?a<-(start)
       =>
        (retract ?a)
        // activate only when testing this file
        (load-facts "queryKB.dat")
        (load-facts "pathKB.dat")
        ;(load-facts "query.dat")
        (focus MAIN RESOLVER CLEANUP)
    )
    (deftemplate MAIN::query
    (slot node)
    (slot question)
    (multislot ans-list)
    )

    (deftemplate MAIN::path
    (slot node)
    (slot answer)
    (multislot next)
    )

    (defglobal ?*question* = (create$))
    (defglobal ?*ans-list* = "")
    (defglobal ?*needInput* = 0)
    (defglobal ?*answer* = "")
    (defglobal ?*anomaly* ="")
    (defglobal ?*next* = "")
    (defglobal ?*symptom_set* =(create$))
    (defglobal ?*anomaly_set* =(create$))
    (defglobal ?*anomaly_match* =(create$))
    (defglobal ?*thenode* = 1)

    (defmodule DIAGNOSE (import MAIN ?ALL)(export ?ALL))

    ;; ****** Ask the appropriate question based on the response asserted
    (deffacts MAIN::start-data
    (current-node 1)
    )

    (defrule MAIN::ask-node-question
    (declare (salience 100))
    ?a <-(current-node ?node)
    ?d <-(query (node ?node)(question ?question)(ans-list $?ans-list))
    =>

    (bind ?*question* (create$ ?*question* ?question))
    (bind ?*ans-list* ?ans-list)
    ;;***** just for testing the back-end
    ;; implode means it reads as string
    (printout t (implode$ ?*anomaly_set*))
    (printout t "QUESTION: " ?question crlf)
    (printout t "ANSWERS " ?ans-list crlf)
    (bind ?ans (read))
    (assert  (answer ?ans ))
    (assert  (key ?ans ))

    ;;; **** end

    ;(printout wdialog "NODE " ?node crlf )
    (printout wdialog "QUESTION " ?question crlf)
    (printout wdialog "ANSWERS " ?ans-list crlf)
    (assert (position ?node))
    (if (eq ?question "DIAGNOSIS") then
    (bind ?*anomaly* $?ans-list)
      (printout wdialog "The following are the possible problem identified: " crlf)
      (printout wdialog $?ans-list crlf)

      ;(halt)
    else
       (bind ?*needInput* 1)
    )
    )
    ;; ********************** finding the path of the current node ******************
    ;;  if there exist path such that the node is the current node and the answer is the given answer
    ;; assert that the next node will be this node(path)

    (defrule MAIN::Rule1
    (declare (salience 100))
    (key Asia)
    =>
    (bind ?*anomaly_set* (create$  28 38 29 82 11 12)
    (printout t ?*anomaly_set*)
    ))

    (defrule MAIN::find-path
    (declare (salience 100))
    ?a<-(current-node ?node)
    ?b<-(answer ?answer)
    (path (node ?node)(next $?next)(answer ?answer))
    =>
    (bind ?*answer* ?answer)
    (bind ?*thenode* ?node)
    (bind ?*next*  ?next)
    (assert (current-node ?next))
    (assert (symptom ?answer))
    )

    (defrule MAIN::symptoms-list
    (declare (salience 100))
    ?s<-(symptom ?p)
    =>
    (bind ?*symptom_set* (create$ ?*symptom_set* ?p))
    (retract ?s)

    )

    (defrule MAIN::diagnosis-found
    (declare (salience 100))
    (current-node ?node)
    (query (node ?node)(question "DIAGNOSIS")(ans-list  ANOMALY ?a))
    =>
    (bind ?*needInput* 0)
    (bind ?*anomaly* ?a)
    )

    (defmodule RESOLVER (import MAIN  ?ALL)(export ?ALL))
    (deftemplate RESOLVER::tracer
    (slot current)
    (slot prev)
    )

    (defrule RESOLVER::search
    (declare(salience 90))
    ?a<-(position ?n)
    (query(node ?n)(question ?q))
    (path(node ?n)(next ?next))
    =>
    (assert(tracer(current ?next)(prev ?n)))
    (assert (position ?next))
    //(retract ?a)
    )

    (defrule RESOLVER::match-diag
    (declare (salience 100))
    (tracer(current ?id))
    (query (node ?id)(question "DIAGNOSIS")(ans-list ?a $?b))
    =>
    (bind ?*anomaly_set* (create$ ?*anomaly_set*  $?b))
    (printout t ?*anomaly_set*)
    )

    (defmodule CLEANUP(import MAIN ?ALL)(import RESOLVER ?ALL))
    (defrule CLEANUP::clean
    ?a<-(position ?p)
    =>
    (retract ?a)
    )
    (defrule CLEANUP::clean1
    ?a<-(tracer(current ?c)(prev ?p))
    =>
    (retract ?a)
    )

    ;#####################
    ; everything above the is a static
    ; everything below is generated dynamically
    ; using a scripted text from the database
    ; ####################

    (defrule  MAIN::Rule9
    (declare (salience 100))
    (key Low_or_declining_yields_without_obvious_symptoms)
    =>
    (bind ?*anomaly_set* (create$  29)))

    (defrule  MAIN::Rule10
    (declare(salience 100))
    (key Obvious_symptoms_or_presence_of_pests_[eggs,_insects,_mites_and_rodents])
    =>
    (bind ?*anomaly_set* (create$  28 38 82 11 12)))

    (defrule  MAIN::Rule11
    (declare(salience 100))
    (key Damage/dying)
    =>
    (bind ?*anomaly_set* (create$  28 29 82 11 12)))

    (defrule  MAIN::Rule12
    (declare(salience 100))
    (key stunted_growth)
    =>
    (bind ?*anomaly_set* (create$  38)))

    (defrule  MAIN::Rule13
    (declare(salience 100))
    (key Crown)
    =>
    (bind ?*anomaly_set* (create$  28 38 29 82 11 12)))

    (defrule  MAIN::Rule14
    (declare(salience 100))
    (key Leaves)
    =>
    (bind ?*anomaly_set* (create$  28 29 82 11)))

    (defrule  MAIN::Rule15
    (declare(salience 100))
    (key drying)
    =>
    (bind ?*anomaly_set* (create$  28 12)))

    (defrule  MAIN::Rule16
    (declare(salience 100))
    (key presence_of_dark_colored_lesions)
    =>
    (bind ?*anomaly_set* (create$  29)))

    (defrule  MAIN::Rule20
    (declare(salience 100))
    (key nuts)
    =>
    (bind ?*anomaly_set* (create$  12)))

    (defrule  MAIN::Rule24
    (declare(salience 100))
    (key Whorling_of_outer_leaves)
    =>
    (bind ?*anomaly_set* (create$  28)))

    (defrule  MAIN::Rule25
    (declare(salience 100))
    (key Shrivelling)
    =>
    (bind ?*anomaly_set* (create$  28)))

    (defrule  MAIN::Rule26
    (declare(salience 100))
    (key presence_of_grayish-white_spot)
    =>
    (bind ?*anomaly_set* (create$  28)))

    (defrule  MAIN::Rule27
    (declare(salience 100))
    (key completely_dried)
    =>
    (bind ?*anomaly_set* (create$  28)))

    (defrule  MAIN::Rule28
    (declare(salience 100))
    (key along_the_leaf_blades)
    =>
    (bind ?*anomaly_set* (create$  28)))

    (defrule  MAIN::Rule29
    (declare(salience 100))
    (key Basal_part)
    =>
    (bind ?*anomaly_set* (create$  11 12)))

    (defrule  MAIN::Rule30
    (declare(salience 100))
    (key discoloration)
    =>
    (bind ?*anomaly_set* (create$  29 11)))

    (defrule  MAIN::Rule31
    (declare(salience 200))
    (key yellow)
    =>
    (bind ?*anomaly_set* (create$  11)))

    (defrule  MAIN::Rule32
    (declare(salience 100))
    (key tissues_rot)
    =>
    (bind ?*anomaly_set* (create$  11)))

    (defrule  MAIN::Rule33
    (declare(salience 100))
    (key spindle_emits_foul_smell)
    =>
    (bind ?*anomaly_set* (create$  11)))

    (defrule  MAIN::Rule34
    (declare(salience 100))
    (key yes)
    =>
    (bind ?*anomaly_set* (create$  11)))

    (defrule  MAIN::Rule36
    (declare(salience 100))
    (key trunk)
    =>
    (bind ?*anomaly_set* (create$  38)))

    (defrule  MAIN::Rule37
    (declare(salience 100))
    (key presence_of_cracks_or_patches_on_the_outer_tissue)
    =>
    (bind ?*anomaly_set* (create$  38)))

    (defrule  MAIN::Rule38
    (declare(salience 100))
    (key exudation_of_dark_reddish_brown_fluid)
    =>
    (bind ?*anomaly_set* (create$  38)))

    (defrule  MAIN::Rule39
    (declare(salience 100))
    (key black-colored)
    =>
    (bind ?*anomaly_set* (create$  38)))

    (defrule  MAIN::Rule40
    (declare(salience 100))
    (key Decayed_and_yellowish)
    =>
    (bind ?*anomaly_set* (create$  38)))

    (defrule  MAIN::Rule41
    (declare(salience 100))
    (key yellowish_brown)
    =>
    (bind ?*anomaly_set* (create$  29)))

    (defrule  MAIN::Rule42
    (declare(salience 100))
    (key dark_brown)
    =>
    (bind ?*anomaly_set* (create$  29)))

    (defrule  MAIN::Rule43
    (declare(salience 100))
    (key circular_to_oblong)
    =>
    (bind ?*anomaly_set* (create$  29)))

    (defrule  MAIN::Rule44
    (declare(salience 100))
    (key ashen_gray_at_the_center_of_the_spots)
    =>
    (bind ?*anomaly_set* (create$  29)))

    (defrule  MAIN::Rule45
    (declare(salience 100))
    (key ovoid_fruiting_bodies_of_the_fungi)
    =>
    (bind ?*anomaly_set* (create$  29)))

    (defrule  MAIN::Rule49
    (declare(salience 100))
    (key small_sunken_necrotic_dots)
    =>
    (bind ?*anomaly_set* (create$  29)))

    (defrule  MAIN::Rule50
    (declare(salience 100))
    (key Presence_of_eggs/pupa/larva)
    =>
    (bind ?*anomaly_set* (create$  82)))

    (defrule  MAIN::Rule52
    (declare(salience 100))
    (key larva)
    =>
    (bind ?*anomaly_set* (create$  82)))

    (defrule  MAIN::Rule53
    (declare(salience 100))
    (key eggs)
    =>
    (bind ?*anomaly_set* (create$  82)))

    (defrule  MAIN::Rule54
    (declare(salience 100))
    (key adult_insect)
    =>
    (bind ?*anomaly_set* (create$  82)))

    (defrule  MAIN::Rule55
    (declare(salience 100))
    (key leaf_skin__is_creamy_white_to_yellowish_with_brown_head)
    =>
    (bind ?*anomaly_set* (create$  82)))

    (defrule  MAIN::Rule56
    (declare(salience 100))
    (key eggs_on_small_holes)
    =>
    (bind ?*anomaly_set* (create$  82)))

    (defrule  MAIN::Rule57
    (declare(salience 100))
    (key leaf_skin_is_reddish_brown)
    =>
    (bind ?*anomaly_set* (create$  82)))

    (defrule  MAIN::Rule58
    (declare(salience 100))
    (key crescentic_slit_on_upper_surface_of_leaflet)
    =>
    (bind ?*anomaly_set* (create$  82)))

    (defrule  MAIN::RULE-ANOMALY12
    (declare(salience 200))
    (key Crown)
    (key nuts)
    (key Basal_part)
    (key drying)
    (key Asia)
    (key Obvious_symptoms_or_presence_of_pests_[eggs,_insects,_mites_and_rodents])
    (key Damage/dying)

    =>
    ;(bind ?*anomaly_match* 12))
    (printout t "Bod rot" crlf)(halt))

    (defrule  MAIN::RULE-ANOMALY11
    (declare(salience 200))
    (key Crown)
    (key Leaves)
    (key Basal_part)
    (key discoloration)
    (key yellow)
    (key tissues_rot)
    (key spindle_emits_foul_smell)
    (key yes)
    (key Asia)
    (key Obvious_symptoms_or_presence_of_pests_[eggs,_insects,_mites_and_rodents])
    (key Damage/dying)

    =>
    ;(bind ?*anomaly_match* 11))
      (printout t "Bod rot" crlf)(halt))

    (defrule  MAIN::RULE-ANOMALY28
    (declare(salience 200))
    (key Crown)
    (key Leaves)
    (key drying)
    ;(key Whorling_of_outer_leaves)
    ;(key Shrivelling)
    ;(key presence_of_grayish-white_spot)
    (key completely_dried)
    ;(key along_the_leaf_blades)
    (key Asia)
    (key Obvious_symptoms_or_presence_of_pests_[eggs,_insects,_mites_and_rodents])
    (key Damage/dying)

    =>
    ;(bind ?*anomaly_match* 28))
    (printout t "LeafBlight" crlf)(halt))     

    (defrule  MAIN::RULE-ANOMALY82
    (declare(salience 200))
    (key Crown)
    (key Leaves)
    (key Presence_of_eggs/pupa/larva)
    (key larva)
    (key eggs)
    (key adult_insect)
    (key leaf_skin__is_creamy_white_to_yellowish_with_brown_head)
    (key eggs_on_small_holes)
    (key leaf_skin_is_reddish_brown)
    (key crescentic_slit_on_upper_surface_of_leaflet)
    (key Asia)
    (key Obvious_symptoms_or_presence_of_pests_[eggs,_insects,_mites_and_rodents])
    (key Damage/dying)

    =>
    ;(bind ?*anomaly_match* 82))
    (printout t "Leaf Miner" crlf)(halt))    

    (defrule  MAIN::RULE-ANOMALY29
    (declare(salience 200))
    (key Crown)
    (key Leaves)
    (key presence_of_dark_colored_lesions)
    (key discoloration)
    (key ovoid_fruiting_bodies_of_the_fungi)
    (key small_sunken_necrotic_dots)
    (key yellowish_brown)
    (key dark_brown)
    (key circular_to_oblong)
    (key ashen_gray_at_the_center_of_the_spots)
    (key Asia)
    (key Low_or_declining_yields_without_obvious_symptoms)
    (key Damage/dying)

    =>
    ;(bind ?*anomaly_match* 29))
      (printout t "Leaf spots" crlf)(halt))

    (defrule  MAIN::RULE-ANOMALY38
    (declare(salience 200))
    (key Crown)
    (key trunk)
    (key presence_of_cracks_or_patches_on_the_outer_tissue)
    (key exudation_of_dark_reddish_brown_fluid)
    (key black-colored)
    (key Decayed_and_yellowish)
    (key Asia)
    (key Obvious_symptoms_or_presence_of_pests_[eggs,_insects,_mites_and_rodents])
    (key stunted_growth)

    =>
    ;(bind ?*anomaly_match* 38))
    (printout t "Stem bleeding" crlf)(halt))

    ********************************************************

    ****************** queryKB.dat  **************************

    (query (node 1)(question "Where is the palm plantation located")(ans-list  Asia North_America South_America Central_America Oceanea Carribean Africa Europe))
    (query (node 2)(question "What indication of the problem is observable")(ans-list  Low_or_declining_yields_without_obvious_symptoms Obvious_symptoms_or_presence_of_pests_[eggs,_insects,_mites_and_rodents]))
    (query (node 3)(question "What is the general appearance of the affected plants")(ans-list  Damage/dying stunted_growth))
    (query (node 4)(question "Which part of the palm is affected")(ans-list  Crown trunk))
    (query (node 5)(question "What part/s of the crown is affected")(ans-list  Leaves nuts Basal_part))
    (query (node 7)(question "Describe the appearance of the affected leaves")(ans-list  drying presence_of_dark_colored_lesions presence_of_white_bacteria Whorling_of_outer_leaves Shrivelling presence_of_grayish-white_spot discoloration ovoid_fruiting_bodies_of_the_fungi))
    (query (node 8)(question "How is the dried part distributed")(ans-list  from_tip_to_leaf_base))
    (query (node 9)(question "Describe the location of lesions")(ans-list  at_the_back_of_midribs small_dark_brown sunken_necrotic_dots small_sunker_necrotic_dots small_sunken_necrotic_dots))
    (query (node 10)(question "Which nuts are affected")(ans-list  immature_nuts))
    (query (node 11)(question "Describe the appearance of the immature nuts")(ans-list  rotting nuts_fall_down))
    (query (node 12)(question "Describe the dried part of the leaf affected")(ans-list  completely_dried))
    (query (node 13)(question "Which of the leaf is shrivelled")(ans-list  along_the_leaf_blades))
    (query (node 14)(question "What is the color of the affected leaves")(ans-list  yellow yellowish_brown dark_brown))
    (query (node 15)(question "Describe the appearance of the bud")(ans-list  tissues_rot spindle_emits_foul_smell))
    (query (node 16)(question "Does the leaves easily separates from the crown")(ans-list  yes no))
    (query (node 17)(question "Decribe the appearance of the trunk")(ans-list  presence_of_cracks_or_patches_on_the_outer_tissue))
    (query (node 18)(question "Describe the cracks on the trunk")(ans-list  exudation_of_dark_reddish_brown_fluid black-colored))
    (query (node 19)(question "Describe the tissues beneath the bleeding patches")(ans-list  Decayed_and_yellowish))
    (query (node 20)(question "Describe the shape of the discoloration")(ans-list  circular_to_oblong ashen_gray_at_the_center_of_the_spots))

    *********************************************************

    ***************    pathKB.dat     ****************************

    (path (node 1)(answer Asia)(next 2))
    (path (node 1)(answer North_America)(next 2))
    (path (node 1)(answer South_America)(next 2))
    (path (node 1)(answer Central_America)(next 2))
    (path (node 1)(answer Oceanea)(next 2))
    (path (node 1)(answer Carribean)(next 2))
    (path (node 1)(answer Africa)(next 2))
    (path (node 1)(answer Europe)(next 2))
    (path (node 2)(answer Low_or_declining_yields_without_obvious_symptoms)(next 3))
    (path (node 2)(answer Obvious_symptoms_or_presence_of_pests_[eggs,_insects,_mites_and_rodents])(next 3))
    (path (node 3)(answer Damage/dying)(next 4))
    (path (node 3)(answer stunted_growth)(next 4))
    (path (node 4)(answer Crown)(next 5))
    (path (node 4)(answer trunk)(next 17))
    (path (node 5)(answer Leaves)(next 7))
    (path (node 5)(answer nuts)(next 10))
    (path (node 5)(answer Basal_part)(next 15))
    (path (node 7)(answer drying)(next 12))
    (path (node 7)(answer presence_of_dark_colored_lesions)(next 9))
    (path (node 7)(answer presence_of_white_bacteria)(next 0))
    (path (node 7)(answer Whorling_of_outer_leaves)(next 0))
    (path (node 7)(answer Shrivelling)(next 13))
    (path (node 7)(answer presence_of_grayish-white_spot)(next 0))
    (path (node 7)(answer discoloration)(next 14))
    (path (node 7)(answer ovoid_fruiting_bodies_of_the_fungi)(next 0))
    (path (node 8)(answer from_tip_to_leaf_base)(next 0))
    (path (node 9)(answer at_the_back_of_midribs)(next 0))
    (path (node 9)(answer small_dark_brown)(next 0))
    (path (node 9)(answer sunken_necrotic_dots)(next 0))
    (path (node 9)(answer small_sunker_necrotic_dots)(next 0))
    (path (node 9)(answer small_sunken_necrotic_dots)(next 0))
    (path (node 10)(answer immature_nuts)(next 11))
    (path (node 11)(answer rotting)(next 0))
    (path (node 11)(answer nuts_fall_down)(next 0))
    (path (node 12)(answer completely_dried)(next 0))
    (path (node 13)(answer along_the_leaf_blades)(next 0))
    (path (node 14)(answer yellow)(next 16))
    (path (node 14)(answer yellowish_brown)(next 0))
    (path (node 14)(answer dark_brown)(next 20))
    (path (node 15)(answer tissues_rot)(next 0))
    (path (node 15)(answer spindle_emits_foul_smell)(next 0))
    (path (node 16)(answer yes)(next 0))
    (path (node 16)(answer no)(next 0))
    (path (node 17)(answer presence_of_cracks_or_patches_on_the_outer_tissue)(next 18))
    (path (node 18)(answer exudation_of_dark_reddish_brown_fluid)(next 19))
    (path (node 18)(answer black-colored)(next 0))
    (path (node 19)(answer Decayed_and_yellowish)(next 0))
    (path (node 20)(answer circular_to_oblong)(next 0))
    (path (node 20)(answer ashen_gray_at_the_center_of_the_spots)(next 0))

    *********************************************************

    Hoping for your reply guys......

     
    • Francesco Garosi

      Hi...

      If you only had "static" constructs in your CLIPS file, you could use it with PyCLIPS in quite an easy way: simply Load(), Reset() and Run(). If it was saved with the name "rules.clp", the following could be a possible session:

      >>> import clips
      >>> clips.Load("rules.clp")
      >>> clips.Reset()
      >>> clips.Run()
      15
      >>> print clips.StdoutStream.Read()
      QUESTION: Where is the palm plantation located
      ANSWERS (Asia North_America South_America Central_America Oceanea Carribean Africa Europe)

      However, it only works when you don't have to interact with the script. This is not your case. Since the (read) function doesn't easily work in PyCLIPS, you might want to use an interactive Python function. See this post for an example:

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

      Please remember that CLIPS is a shell, PyCLIPS is a library. The two tools work differently, and both have pros and cons.

      F.

       

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks