line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package AI::Prolog; |
2
|
|
|
|
|
|
|
$VERSION = '0.741'; ## no critic |
3
|
5
|
|
|
5
|
|
141204
|
use strict; |
|
5
|
|
|
|
|
17
|
|
|
5
|
|
|
|
|
224
|
|
4
|
5
|
|
|
5
|
|
32
|
use Carp qw( confess carp croak ); |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
514
|
|
5
|
|
|
|
|
|
|
|
6
|
5
|
|
|
5
|
|
5517
|
use Hash::Util 'lock_keys'; |
|
5
|
|
|
|
|
14579
|
|
|
5
|
|
|
|
|
29
|
|
7
|
5
|
|
|
5
|
|
8978
|
use Exporter::Tidy shortcuts => [qw/Parser Term Engine/]; |
|
5
|
|
|
|
|
54
|
|
|
5
|
|
|
|
|
40
|
|
8
|
|
|
|
|
|
|
|
9
|
5
|
|
|
5
|
|
2009
|
use aliased 'AI::Prolog::Parser'; |
|
5
|
|
|
|
|
1801
|
|
|
5
|
|
|
|
|
41
|
|
10
|
5
|
|
|
5
|
|
605
|
use aliased 'AI::Prolog::Term'; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
30
|
|
11
|
5
|
|
|
5
|
|
688
|
use aliased 'AI::Prolog::Engine'; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
22
|
|
12
|
|
|
|
|
|
|
|
13
|
5
|
|
|
5
|
|
7184
|
use Text::Quote; |
|
5
|
|
|
|
|
3006967
|
|
|
5
|
|
|
|
|
179
|
|
14
|
5
|
|
|
5
|
|
55
|
use Regexp::Common; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
54
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# they don't want pretty printed strings if they're using this interface |
17
|
|
|
|
|
|
|
Engine->formatted(0); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# Until (and unless) we figure out the weird bug that prevents some values |
20
|
|
|
|
|
|
|
# binding in the external interface, we need to stick with this as the default |
21
|
|
|
|
|
|
|
Engine->raw_results(1); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub new { |
24
|
7
|
|
|
7
|
1
|
1280
|
my ( $class, $program ) = @_; |
25
|
7
|
|
|
|
|
69
|
my $self = bless { |
26
|
|
|
|
|
|
|
_prog => Parser->consult($program), |
27
|
|
|
|
|
|
|
_query => undef, |
28
|
|
|
|
|
|
|
_engine => undef, |
29
|
|
|
|
|
|
|
} => $class; |
30
|
7
|
|
|
|
|
65
|
lock_keys %$self; |
31
|
7
|
|
|
|
|
106
|
return $self; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub do { |
35
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $query ) = @_; |
36
|
0
|
|
|
|
|
0
|
$self->query($query); |
37
|
0
|
|
|
|
|
0
|
1 while $self->results; |
38
|
0
|
|
|
|
|
0
|
$self; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub query { |
42
|
37
|
|
|
37
|
1
|
2352
|
my ( $self, $query ) = @_; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# make that final period optional |
45
|
37
|
100
|
|
|
|
144
|
$query .= '.' unless $query =~ /\.$/; |
46
|
37
|
|
|
|
|
147
|
$self->{_query} = Term->new($query); |
47
|
37
|
100
|
|
|
|
220
|
unless ( defined $self->{_engine} ) { |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# prime the pump |
50
|
7
|
|
|
|
|
16
|
$self->{_engine} = Engine->new( @{$self}{qw/_query _prog/} ); |
|
7
|
|
|
|
|
44
|
|
51
|
|
|
|
|
|
|
} |
52
|
37
|
|
|
|
|
180
|
$self->{_engine}->query( $self->{_query} ); |
53
|
37
|
|
|
|
|
90
|
return $self; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub results { |
57
|
44
|
|
|
44
|
1
|
1528
|
my $self = shift; |
58
|
44
|
50
|
|
|
|
125
|
unless ( defined $self->{_query} ) { |
59
|
0
|
|
|
|
|
0
|
croak "You can't fetch results because you have not set a query"; |
60
|
|
|
|
|
|
|
} |
61
|
44
|
|
|
|
|
154
|
$self->{_engine}->results; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub trace { |
65
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
66
|
0
|
0
|
|
|
|
0
|
if (@_) { |
67
|
0
|
|
|
|
|
0
|
$self->{_engine}->trace(shift); |
68
|
0
|
|
|
|
|
0
|
return $self; |
69
|
|
|
|
|
|
|
} |
70
|
0
|
|
|
|
|
0
|
return $self->{_engine}->trace; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub raw_results { |
74
|
1
|
|
|
1
|
1
|
7
|
my $class = shift; |
75
|
1
|
50
|
|
|
|
5
|
if (@_) { |
76
|
1
|
|
|
|
|
6
|
Engine->raw_results(shift); |
77
|
1
|
|
|
|
|
2
|
return $class; |
78
|
|
|
|
|
|
|
} |
79
|
0
|
|
|
|
|
|
return Engine->raw_results; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
my $QUOTER; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub quote { |
85
|
0
|
|
|
0
|
1
|
|
my ( $proto, $string ) = @_; |
86
|
0
|
0
|
|
|
|
|
$QUOTER = Text::Quote->new unless $QUOTER; |
87
|
0
|
|
|
|
|
|
return $QUOTER->quote_simple($string); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub list { |
91
|
0
|
|
|
0
|
1
|
|
my $proto = shift; |
92
|
|
|
|
|
|
|
return |
93
|
0
|
0
|
|
|
|
|
join ", " => map { /^$RE{num}{real}$/ ? $_ : $proto->quote($_) } @_; |
|
0
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub continue { |
97
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
98
|
0
|
0
|
|
|
|
|
return 1 unless $self->{_engine}; # we haven't started yet! |
99
|
0
|
|
|
|
|
|
!$self->{_engine}->halt; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
__END__ |