The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

AI::Prolog::Engine - Run queries against a Prolog database.

SYNOPSIS

 my $engine = AI::Prolog::Engine->new($query, $database).
 while (my $results = $engine->results) {
     print "$result\n";
 }

DESCRIPTION

AI::Prolog::Engine is a Warren Abstract Machine (WAM) based Prolog engine.

The new() function actually bootstraps some Prolog code onto your program to give you access to the built in predicates listed in the AI::Prolog documentation.

See AI::Prolog for more information.

CLASS METHODS

new($query, $database)

This creates a new Prolog engine. The first argument must be of type AI::Prolog::Term and the second my be a database created by AI::Prolog::Parser::consult.

 my $database = Parser->consult($some_prolog_program);
 my $query    = Term->new('steals(badguy, X).');
 my $engine   = Engine->new($query, $database);
 while (my $results = $engine->results) {
    print $results, $/;
 }

trace($boolean)

Set this to a true value to turn on tracing. This will trace through the engine's goal satisfaction process while it's running. This is very slow.

 Engine->trace(1); # turn on tracing
 Engine->trace(0); # turn off tracing

INSTANCE METHODS

results()

This method will return the results from the last run query, one result at a time. It will return false when there are no more results.

 while (my $results = $engine->results) {
    print $results, $/;
 }

query($query)

If you already have an engine object instantiated, call the query() method for subsequent queries. Internally, when calling new(), the engine bootstraps a set of Prolog predicates to provide the built ins. However, this process is slow. Subsequent queries to the same engine with the query() method can double the speed of your program.

 my $engine   = Engine->new($query, $database);
 while (my $results = $engine->results) {
    print $results, $/;
 }
 $query = Term->new("steals(ovid, X).");
 $engine->query($query);
 while (my $results = $engine->results) {
    print $results, $/;
 }