AI-Prolog
view release on metacpan or search on metacpan
lib/AI/Prolog.pm view on Meta::CPAN
while (my $result = $prolog->results) {
# $result = [ 'steals', 'badguy', $x ]
print "badguy steals $result->[2]\n";
}
=head1 BUILTINS
See L<AI::Prolog::Builtins|AI::Prolog::Builtins> for the built in predicates.
=head1 CLASS METHODS
=head2 C<new($program)>
This is the constructor. It takes a string representing a Prolog program:
my $prolog = AI::Prolog->new($program_text);
See L<AI::Prolog::Builtins|AI::Prolog::Builtins> and the C<examples/> directory
included with this distribution for more details on the program text.
Returns an C<AI::Prolog> object.
=head2 C<trace([$boolean])>
One can "trace" the program execution by setting this property to a true value
before fetching engine results:
AI::Prolog->trace(1);
while (my $result = $engine->results) {
# do something with results
}
This sends trace information to C<STDOUT> and allows you to see how the engine
is trying to satify your goals. Naturally, this slows things down quite a bit.
Calling C<trace> without an argument returns the current C<trace> value.
=head2 C<raw_results([$boolean])>
You can get access to the full, raw results by setting C<raw_results> to true.
In this mode, the results are returned as an array reference with the functor
as the first element and an additional element for each term. Lists are
represented as array references.
AI::Prolog->raw_results(1);
$prolog->query('steals(badguy, STUFF, VICTIM)');
while (my $r = $prolog->results) {
# do stuff with $r in the form:
# ['steals', 'badguy', $STUFF, $VICTIM]
}
Calling C<raw_results> without an argument returns the current C<raw_results>
value.
This is the default behavior.
=head2 C<quote($string)>.
This method quotes a Perl string to allow C<AI::Prolog> to treat it as a proper
Prolog term (and not worry about it accidentally being treated as a variable if
it begins with an upper-case letter).
my $perl6 = AI::Prolog->quote('Perl 6'); # returns 'Perl 6' (with quotes)
$prolog->query(qq'can_program("ovid",$perl6).');
At the present time, quoted strings may use single or double quotes as strings.
This is somewhat different from standard Prolog which treats a double-quoted
string as a list of characters.
Maybe called on an instance (the behavior is unchanged).
=head2 C<list(@list)>.
Turns a Perl list into a Prolog list and makes it suitable for embedding into
a program. This will quote individual variables, unless it thinks they are
a number. If you wish numbers to be quoted with this method, you will need to
quote them manually.
This method does not add the list brackets.
my $list = AI::Prolog->list(qw/foo Bar 7 baz/);
# returns: 'foo', 'Bar', 7, 'baz'
$prolog->query(qq/append(X,Y,[$list])./);
May be called on an instance (the behavior is unchanged).
=head1 INSTANCE METHODS
=head2 C<do($query_string)>
This method is useful when you wish to combine the C<query()> and C<results()>
methods but don't care about the results returned. Most often used with the
C<assert(X)> and C<retract(X)> predicates.
$prolog->do('assert(loves(ovid,perl)).');
This is a shorthand for:
$prolog->query('assert(loves(ovid,perl)).');
1 while $prolog->results;
This is important because the C<query()> method merely builds the query. Not
until the C<results()> method is called is the command actually executed.
=head2 C<query($query_string)>
After instantiating an C<AI::Prolog> object, use this method to query it.
Queries currently take the form of a valid prolog query but the final period
is optional:
$prolog->query('grandfather(Ancestor, julie).');
This method returns C<$self>.
=head2 C<results>
After a query has been issued, this method will return results satisfying the
query. When no more results are available, this method returns C<undef>.
while (my $result = $prolog->results) {
( run in 2.580 seconds using v1.01-cache-2.11-cpan-2398b32b56e )