blib/lib/POE/Component/AI/MegaHAL.pm

filter: Statementstmt 68.6% 68 / 99 Branchbran 38.2% 13 / 34 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 82.3% 14 / 17 Podpod 100% 3 / 3 totaltotal 58.9% 102 / 173 CC 25 SCAR 25.3
CC
25
Cov
59%
_main
42.0
_megahal_function
18.0
spawn
5.5
CRAP 12.5
↑ Summary
1 package POE::Component::AI::MegaHAL;
2 2 $POE::Component::AI::MegaHAL::VERSION = '1.20';
3 #ABSTRACT: A non-blocking wrapper around AI::MegaHAL.
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use AI::MegaHAL;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use POE qw(Wheel::Run Filter::Line Filter::Reference);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶sub spawn {
Subroutine spawn: called CC=5 SCAR=17.0
Pod documented
12 1 my $package = shift;
13 1 my %params = @_;
14  
15 1 $params{ lc $_ } = delete $params{$_} for keys %params;
16 1 ▶ $params{'autosave'} = 1 unless defined ( $params{'autosave'} ) and $params{'autosave'} eq '0';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"autosave"} and $params{"autosave"} eq 0 0 1 1
Condition33%1/3
defined $params{"autosave"} and $params{"autosave"} eq 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $params{"autosave"} and $params{"autosave"} eq 0
defined $params{"autosave"} $params{"autosave"} eq 0
Truth table33%1/3
A and B
A= defined $params{"autosave"}B= $params{"autosave"} eq 0
ABresult
0X0
100
111
17 1 my $options = delete $params{'options'};
18 1 my $self = bless \%params, $package;
19  
20 1 ▶ POE::Session->create(
Branch: 1 branch
BranchTrueFalseTotal
ref $options eq 'HASH' ? : 1 0 1
21 object_states => [
22 $self => {
23 do_reply => '_megahal_function',
24 initial_greeting => '_megahal_function',
25 learn => '_megahal_function',
26 _cleanup => '_megahal_function',
27 },
28 $self => [ qw(_child_closed _child_error _child_stderr _child_stdout _start shutdown _sig_chld) ],
29 ],
30 ( ref ( $options ) eq 'HASH' ? ( options => $options ) : () ),
31 );
32  
33 1 return $self;
34 }
35  
36 5 ▶sub session_id {
Subroutine session_id: called CC=1 SCAR=0.0
Pod documented
37 5 return $_[0]->{session_id};
38 }
39  
40 4 ▶sub _megahal_function {
Subroutine _megahal_function: called CC=11 SCAR=28.9
41 4 my ($kernel,$self,$state) = @_[KERNEL,OBJECT,STATE];
42 4 my $sender = $_[SENDER]->ID();
43 4 ▶ return if $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown"} 0 4 4
44  
45 4 my $args;
46  
47 4 ▶ if ( ref( $_[ARG0] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[10] eq 'HASH') { } 4 0 4
48 4 $args = { %{ $_[ARG0] } };
49 }
50 else {
51 0 warn "first parameter must be a hashref, trying to adjust. "
52 ."(fix this to get rid of this message)";
53 0 $args = { @_[ARG0..$#_] };
54 }
55  
56 4 ▶ unless ( $args->{event} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"event"}) 0 4 4
57 0 warn "where am i supposed to send the output?";
58 0 return;
59 }
60  
61  
62 4 ▶ return if $state =~ /^(do_reply|learn)$/ and !defined $args->{text};
Branch: 1 branch
BranchTrueFalseTotal
if $state =~ /^(do_reply|learn)$/ and not defined $args->{"text"} 0 4 4
Condition33%1/3
$state =~ /^(do_reply|learn)$/ and not defined $args->{"text"}
!ll&&!rl&&r
040
MC/DC0%0/2
$state =~ /^(do_reply|learn)$/ and not defined $args->{"text"}
$state =~ /^(do_reply|learn)$/ not defined $args->{"text"}
Truth table33%1/3
A and not(B)
A= $state =~ /^(do_reply|learn)$/B= not defined $args->{"text"}
ABresult
0X0
100
111
63  
64 4 ▶ delete $args->{text} if $state eq 'initial_greeting' and defined $args->{text};
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "initial_greeting" and defined $args->{"text"} 0 4 4
Condition33%1/3
$state eq "initial_greeting" and defined $args->{"text"}
!ll&&!rl&&r
400
MC/DC0%0/2
$state eq "initial_greeting" and defined $args->{"text"}
$state eq "initial_greeting" defined $args->{"text"}
Truth table33%1/3
A and B
A= $state eq "initial_greeting"B= defined $args->{"text"}
ABresult
0X0
100
111
65  
66 4 ▶ delete $args->{text} if $state eq '_cleanup' and defined $args->{text};
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "_cleanup" and defined $args->{"text"} 0 4 4
Condition33%1/3
$state eq "_cleanup" and defined $args->{"text"}
!ll&&!rl&&r
400
MC/DC0%0/2
$state eq "_cleanup" and defined $args->{"text"}
$state eq "_cleanup" defined $args->{"text"}
Truth table33%1/3
A and B
A= $state eq "_cleanup"B= defined $args->{"text"}
ABresult
0X0
100
111
67  
68 4 $args->{sender} = $sender;
69 4 $args->{func} = $state;
70 4 $kernel->refcount_increment( $sender => __PACKAGE__ );
71 4 $args->{sender} = $sender;
72  
73 4 ▶ $self->{wheel}->put( $args ) if defined $self->{wheel};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"wheel"} 4 0 4
74 4 return;
75 }
76  
77 1 ▶sub _start {
Subroutine _start: called CC=3 SCAR=11.4
78 1 my ($kernel,$self) = @_[KERNEL,OBJECT];
79 1 $self->{session_id} = $_[SESSION]->ID();
80  
81 1 ▶ if ( $self->{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'alias'}) { } 0 1 1
82 0 $kernel->alias_set( $self->{alias} );
83 } else {
84 1 $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ );
85 }
86  
87 $self->{wheel} = POE::Wheel::Run->new(
88 Program => \&_main,
89 1 ▶ ProgramArgs => [ AutoSave => $self->{autosave}, Path => $self->{path} ],
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 1 1
90 ErrorEvent => '_child_error',
91 CloseEvent => '_child_closed',
92 StdoutEvent => '_child_stdout',
93 StderrEvent => '_child_stderr',
94 StdioFilter => POE::Filter::Reference->new(),
95 StderrFilter => POE::Filter::Line->new(),
96 ( $^O eq 'MSWin32' ? ( CloseOnCall => 0 ) : ( CloseOnCall => 1 ) ),
97 );
98  
99 1 $kernel->sig_child( $self->{wheel}->PID, '_sig_chld' );
100 1 return;
101 }
102  
103 1 ▶sub _sig_chld {
Subroutine _sig_chld: called CC=1 SCAR=0.0
104 1 $_[KERNEL]->sig_handled();
105 }
106  
107 0 ▶sub _child_closed {
Subroutine _child_closed: not called CC=1 SCAR=6.9
108 0 delete $_[OBJECT]->{wheel};
109 0 return;
110 }
111  
112 1 ▶sub _child_error {
Subroutine _child_error: called CC=1 SCAR=0.0
113 1 delete $_[OBJECT]->{wheel};
114 1 return;
115 }
116  
117 1 ▶sub _child_stderr {
Subroutine _child_stderr: called CC=2 SCAR=7.1
118 1 my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0];
119 1 ▶ warn $input . "\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
120 1 return;
121 }
122  
123 4 ▶sub _child_stdout {
Subroutine _child_stdout: called CC=1 SCAR=0.0
124 4 my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0];
125 4 my $sender = delete $input->{sender};
126 4 my $event = delete $input->{event};
127 4 $kernel->post( $sender => $event => $input );
128 4 $kernel->refcount_decrement( $sender => __PACKAGE__ );
129 4 return;
130 }
131  
132 1 ▶sub shutdown {
Subroutine shutdown: called CC=3 SCAR=11.0
Pod documented
133 1 my ($kernel,$self) = @_[KERNEL,OBJECT];
134 1 $kernel->alias_remove( $_ ) for $kernel->alias_list();
135 1 ▶ $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"alias"} 1 0 1
136 1 $self->{shutdown} = 1;
137 1 $self->{wheel}->shutdown_stdin;
138 1 return;
139 }
140  
141 0 ▶sub _main {
Subroutine _main: not called CC=6 SCAR=37.4
142 0 my %params = @_;
143 0 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 0 0
144 0 binmode(STDIN); binmode(STDOUT);
145 }
146 0 my $raw;
147 0 my $size = 4096;
148 0 my $filter = POE::Filter::Reference->new();
149 0 my $megahal;
150 0 eval {
151 0 $megahal = AI::MegaHAL->new( %params );
152 };
153  
154 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
155 0 print STDERR $@ . "\n";
156 0 return;
157 }
158  
159 0 ▶ while ( sysread ( STDIN, $raw, $size ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread STDIN, $raw, $size) 0 0 0
160 0 my $requests = $filter->get( [ $raw ] );
161 0 _process_requests( $megahal, $_, $filter ) for @{ $requests };
162 }
163 0 ▶ $megahal->_cleanup() if $params{'AutoSave'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"AutoSave"} 0 0 0
164 0 $megahal->DESTROY;
165 }
166  
167 0 ▶sub _process_requests {
Subroutine _process_requests: not called CC=1 SCAR=6.9
168 0 my ($megahal,$req,$filter) = @_;
169  
170 0 my $func = $req->{func};
171 0 $req->{reply} = $megahal->$func( $req->{text} );
172 0 my $response = $filter->put( [ $req ] );
173 0 print STDOUT @$response;
174 }
175  
176 2 qq[You talking to me];
177  
178 __END__
↑ Summary