#!/usr/bin/perl -w use strict; use AI::Perlog; my $pg = AI::Perlog->new; load_data(); my $acts_of_horror = who_steals_from_whom(); unless ( @$acts_of_horror ) { print "Looks like everybody's safe\n"; } else { foreach my $crime ( @$acts_of_horror ) { my ( $thief, $victim ) = @$crime; print "$thief will steal from $victim.\n"; } } exit; sub who_steals_from_whom { my $thieves = $pg->thief( '$person' ); my $victims = $pg->owns( qw/ $person _ / ); my @crimes; foreach my $victim ( @$victims ) { my $curr_victim = $victim->{person}; foreach my $thief ( @$thieves ) { my $curr_thief = $thief->{person}; if (rich($curr_victim) and ! $pg->knows($curr_thief,$curr_victim)){ push @crimes => [$curr_thief, $curr_victim]; } } } return \@crimes; } sub rich { my $person = shift; my $items = $pg->owns( $person, '$item' ); local $_; foreach ( @$items ) { return 1 if $pg->valuable( $_->{ item } ); } return; } sub load_data { while ( ) { chomp; next if ( ! $_ or substr( $_, 0, 1 ) eq '#' ); my @fact = split /\t/; $pg->add_fact( @fact ); } } __DATA__ owns merlyn gold owns Ovid pocket lint owns Ovid cheap whiskey owns kudra domination fund valuable gold valuable domination fund thief badguy thief badgirl # :) knows badgirl merlyn