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