Knowledge Base
The Knowledge Base object uses sub-classes to perform the main work, but also provides a few functions of its own (all are only accessible by the main class). This allows for the core work of rule and question storage to be performed in a variety of ways, the default of which is to use system memory.
Synopsis
use Pie::KnowledgeBase;
$kb = Pie::KnowledgeBase->new();Description
The KnowledgeBase object provides access to the rules and questions in an expert system knowledge base. It provides the means to load rules and questions from external sources (eg. XML files), and provides an API to access known rules and questions.
Constructor
Can be constructed with an associative array of options:
- kb_type
The implementation of the Knowledge Base (storage part), defaults to "Memory".
The following options are provided by the Memory sub-class, and may not be available on all alternatives.
- rule_storage
- question_type
- rule_source
- name
The implementation of the rule storage for the KnowledgeBase object. Defaults to 'Memory'.
The implementation of the Question object used by the KnowledgeBase. Defaults to 'Console'.
The implementation of the RuleParser that will be used to read in rules. Defaults to 'XML'.
An optional name for the KnowledgeBase. Not really used.
parse_rules
Calls the RuleParser object's parse() method. For XML, this requires the name of at least one XML file to be specified. The parse() method calls KnowledgeBase methods to insert rules and questions into storage.
new_rule
Add a rule to the KnowledgeBase. This method creates a Rule object, and requires a name for the rule be supplied as an argument. It does not assign any conditions or actions to the rule.
new_question
Takes an attribute and some text as arguments, and creates a new question object. The attribute supplied is the one that will be set after the question is answered. The text is shown to ask the question. Responses are added elsewhere.
ask_question
This method calls the ask_question() method in the Question object. The method returns the value obtained by asking the question.
check_question
Simply checks if there is a question defined for the supplied attribute.
reset_rule_iterator
The KnowledgeBase object maintains an internal counter that can be used when iterating through rules (which avoids having to collect lists of all rules). This method simply resets the counter ready for a complete iteration through the rule set.
iterate_rules
This method can be called repeatedly, and returns a Rule object from the rule set each time (or undef if no further rules are available). Call reset_rule_iterator() before using this function, or supply a reference to a scalar that can be used as the counter for iteration.
reset_question_iterator
Same as reset_rule_iterator() but for the internal question counter.
iterate_questions
Same as iterate_rules(), except it iterates over all available questions.
