Numbers and Formulas

Playing with shapes

samples/basic/tutorial/shapes.kb

The values of facts can be mathematical expressions. Those expressions can refer to other facts. And, in keeping with its goal-seeking behavior, when the reasoning engine needs to evaluate an expression that refers to another fact, it will set a sub-goal of finding a value for that fact.

We'll see this behavior with a toy system that computes the area and perimeter of a variety of shapes.

    1. Create a new knowledgebase, named shapes.
    2. Set the goals of the knowledgebase to area and perimeter.
    3. Create a question, named shape, that asks the user to choose between a rectangle and a circle.
    4. Create a rule_set, named area, that has two rules:

      conditions
      value
      shape = "circle"
      pi * radius ** 2
      shape = "rectangle"
      height * width

    5. Create a rule_set, named perimeter, that has two rules:

      conditions
      value
      shape = "circle"
      2 * pi * radius
      shape = "rectangle"
      2 * (height + width)

    6. Create a fact, named pi, that has the value 3.14159.
    7. Create three questions, named radius, height, and width, that ask 'fill_in_the_blank' questions to get the values of each. Make sure each has an answer_type of 'number'. (Otherwise, a 3 will be a "3".)
    8. Run it, noticing that it only asks for radius when dealing with a circle, and height and width when dealing with a rectangle.
    9. Study the trace to understand its behavior.

Mathematical expressions like these can be used pretty much anywhere a straight value can be used.


Copyright ©1996-2002 Amzi! inc. All Rights Reserved. Amzi!, KnowledgeWright and WebLS are registered trademarks and Subscription Plus and Logic Server are trademarks of Amzi! inc.