Pnick
Pnick

Reputation: 1

CLIPS/clipspy Rule Order

So I'm trying to self-learn CLIPS and clipspy for a class assignment and I'm a bit stuck. The code below compiles and runs just fine but the output is a bit strange. I am trying to expand on examples I found in the manual.

import clips
import logging

env = clips.Environment()

logging.basicConfig(level=10, format='%(message)s')
router = clips.LoggingRouter()
router.add_to_environment(env)

env.build("""
(defrule whodunit
  (shoot ?hunter ?who)
  =>
  (printout t ?hunter " shot " ?who crlf)
)""")

env.build("""
(defrule animalGame
  (animal ?ani)
  (shoot $? ?ani)
  =>
  (assert (game ?ani))
)""")

env.build("""
(defrule gameAnimal
  (game ?ani)
  =>
  (assert (animal ?ani))
)""")

env.build("""
(defrule isIllegal
  (shoot ?a ?b)
  (not(game ?b))
  =>
  (assert (criminal ?a))
)""")

env.assert_string("(animal duck)")
env.assert_string("(animal dog)")
env.assert_string("(shoot Brian duck)")
env.assert_string("(shoot Bob rhino)")
env.assert_string("(game deer)")

env.run()

for fact in env.facts():
    print(fact)

This outputs:

(initial-fact)
(animal duck)
(animal dog)
(shoot Brian duck)
(shoot Bob rhino)
(game deer)
(animal deer)
(criminal Bob)
(criminal Brian)
(game duck)

It seems that Brian is labeled a criminal for shooting an animal that is not game despite the animal he is shooting being declared game in the next step. Is there anyway to re-evaluate rules to fix this contradiction?

Upvotes: 0

Views: 282

Answers (1)

Pnick
Pnick

Reputation: 1

I figured it out. The answer is salience. I also realized there's some logical flaws/inconsistencies declaring all animals hunted as game and charging shooting animals that are not game as criminal. Anyways:

env.build("""
(defrule animalGame
  (declare (salience 100))
  (animal ?ani)
  (shoot $? ?ani)
  =>
  (assert (game ?ani))
)""")

Upvotes: 0

Related Questions