# From: "Dat Nguyen" <thucdat@hotmail.com>
# Subject: Module cheetah
# Date: Wed, 19 May 2004 01:16:37 -0400
#
# Animal Taxonomy is a favorite subject of specialized rule-based programming 
# languages. With some creative construction, C-Kermit can solve that problem 
# in the same manner. The cheetah.kr script exposes the same look and feel of 
# many rule-based programs. Many configuration problems in computer 
# administration and network should be solvable similarly, provided the rule 
# set can be identified.
#
# This demonstration comes in two modules: rule-engine and cheetah.
# To run the demonstration, tell Kermit to TAKE the cheetah module.
#
# Here is cheetah:

take rule-engine

define rule_1 {
    (if (AND
         (isThisFactTrue '(the animal has hair))
         (availableRule '\v(macro))
         )
      (.
       (addThisFact '(the animal is a mammal))
       (1)
       )
     (0)
     )
}

define rule_2 {
    (if (AND
         (isThisFactTrue '(the animal gives milk))
         (availableRule '\v(macro))
         )
      (.
       (addThisFact '(the animal is a mammal))
       (1)
       )
     (0)
     )
}

define rule_3 {
    (if (AND
         (isThisFactTrue '(the animal has feathers))
         (availableRule '\v(macro))
         )
      (.
       (addThisFact '(the animal is a bird))
       (1)
       )
     (0)
     )
}

define rule_4 {
    (if (AND
         (isThisFactTrue '(the animal flies))
         (isThisFactTrue '(the animal lays eggs))
         (availableRule '\v(macro))
         )
      (.
       (addThisFact '(the animal is a bird))
       (1)
       )
     (0)
     )
}

define rule_5 {
    (if (AND
         (isThisFactTrue '(the animal is a mammal))
         (isThisFactTrue '(the animal eats meat))
         (availableRule '\v(macro))
         )
      (.
       (addThisFact '(the animal is a carnivore))
       (1)
       )
     (0)
     )
}

define rule_6 {
    (if (AND
         (isThisFactTrue '(the animal is a mammal))
         (isThisFactTrue '(the animal has pointed teeth))
         (isThisFactTrue '(the animal has claws))
         (isThisFactTrue '(the animal's eyes point forward))
         (availableRule '\v(macro))
         )
      (.
       (addThisFact '(the animal is a carnivore))
       (1)
       )
     (0)
     )
}

define rule_7 {
    (if (AND
         (isThisFactTrue '(the animal is a mammal))
         (isThisFactTrue '(the animal has hooves))
         (availableRule '\v(macro))
         )
      (.
       (addThisFact '(the animal is an ungulate))
       (1)
       )
     (0)
     )
}

define rule_8 {
    (if (AND
         (isThisFactTrue '(the animal is a mammal))
         (isThisFactTrue '(the animal chews its cud))
         (availableRule '\v(macro))
         )
      (.
       (addThisFact '(the animal is an ungulate))
       (addThisFact '(the animal is even-toed))
       (1)
       )
     (0)
     )
}

define rule_9 {
    (if (AND
         (isThisFactTrue '(the animal is a carnivore))
         (isThisFactTrue '(the animal has a tawny colour))
         (isThisFactTrue '(the animal has dark spots))
         (availableRule '\v(macro))
         )
      (.
       (say '(the animal is a cheetah))
       (addThisFact '(the animal is a cheetah))
       (1)
       )
     (0)
     )
}

define rule_10 {
    (if (AND
         (isThisFactTrue '(the animal is a carnivore))
         (isThisFactTrue '(the animal has a tawny colour))
         (isThisFactTrue '(the animal has black stripes))
         (availableRule '\v(macro))
         )
      (.
       (say '(the animal is a tiger))
       (addThisFact '(the animal is a tiger))
       (1)
       )
     (0)
     )
}

define rule_11 {
    (if (AND
         (isThisFactTrue '(the animal is an ungulate))
         (isThisFactTrue '(the animal has long legs))
         (isThisFactTrue '(the animal has a long neck))
         (availableRule '\v(macro))
         )
      (.
       (say '(the animal is a giraffe))
       (addThisFact '(the animal is a giraffe))
       (1)
       )
     (0)
     )
}

define rule_12 {
    (if (AND
         (isThisFactTrue '(the animal is an ungulate))
         (isThisFactTrue '(the animal has a white colour))
         (isThisFactTrue '(the animal has black stripes))
         (availableRule '\v(macro))
         )
      (.
       (addThisFact '(the animal is a zebra))
       (1)
       )
     (0)
     )
}

define rule_13 {
    (if (AND
         (isThisFactTrue '(the animal is a bird))
         (isThisFactTrue '(the animal does not fly))
         (isThisFactTrue '(the animal has long legs))
         (isThisFactTrue '(the animal has a long neck))
         (isThisFactTrue '(the animal is black and white))
         (availableRule '\v(macro))
         )
      (.
       (addThisFact '())
       (1)
       )
     (0)
     )
}

define rule_14 {
    (if (AND
         (isThisFactTrue '(the animal is a bird))
         (isThisFactTrue '(the animal does not fly))
         (isThisFactTrue '(the animal swims))
         (isThisFactTrue '(the animal is black and white))
         (availableRule '\v(macro))
         )
      (.
       (addThisFact '(the animal is a penguin))
       (1)
       )
     (0)
     )
}

define rule_15 {
    (if (AND
         (isThisFactTrue '(the animal is a bird))
         (isThisFactTrue '(the animal is a good flier))
         (availableRule '\v(macro))
         )
      (.
       (addThisFact '(the animal is an albatross))
       (1)
       )
     (0)
     )
}

echo
clearAllFacts
addGoal never
echo

# The following assumption reveals a giraffe
addThisFact {the animal has hair}
addThisFact {the animal has claws}
addThisFact {the animal has pointed teeth}
addThisFact {the animal's eyes point forward}
addThisFact {the animal has a tawny colour}
addThisFact {the animal has dark spots}
echo

firerule {rule_1 rule_2 rule_3 rule_4 rule_5 rule_6 rule_7 rule_8 -
rule_9 rule_10 rule_11 rule_12 rule_13 rule_14 rule_15}
