Home
Open Source Works

Developing with PIE

PIE is particularly suited to integrating into other applications (although could easily be used as an end-user program too). Once a knowledge base has been developed, it is easy to integrate into another program.

The anatomy of a Perl program that uses PIE is as follows. Firstly, the Knowledge Base and Inference Engine must be included:

use Pie::KnowledgeBase;
use Pie::Engine;

Then it is a matter of creating a knowledge base, and using the engine on it:
# Create the Knowledge Base object
my $kb = Pie::Knowledgebase->new();

# ...and have it parse the rules in an XML file:
$kb->parse_rules('rules.xml');

# Now create an Engine, and point it at the Knowlege Base
my $engine = Pie::Engine->new($kb);

Forward Chaining

At this point, all of the internal data structures and code is ready to be used. There are various ways to use the engine, starting with the simplest, forward chaining:

my $ret = $engine->forward_chain();

After forward chaining is complete, the Engine will have new knowledge based on whatever facts were available at the time. Any attribute can be obtained and checked as follows:
my $attribute = $engine->get_attribute('my.attribute');
print "Value of my.attribute is " . $attribute->value . "\n";

Backward Chaining

Backward chaining is an interactive process, which causes questions to be asked. By default, these questions are asked interactively via the console. Back chaining is used as follows:

# Optional: Switch to mixed-mode chaining
$engine->mixed_mode(1);

my $ret = $engine->back_chain('my.attribute');
if($ret eq 'solved') {
   print "Value of my.attribute is " . $engine->get_attribute('my.attribute')->value . "\n";
} else {
  print "Back chaining was " . $ret . "\n";
}

Again, the value of any attribute can be checked, as in the forward chaining example.

Mixed mode chaining is generally a preferable way to run rules. It employs a combination of forward and backward chaining, which effectively performs a mini-forward chain every time an attribute is set, whilst basically performing a backward chain.