| 1 | package POE::Component::Hailo; | ||||||||||||
| 2 | BEGIN { | ||||||||||||
| 3 | 2 | ▶ | $POE::Component::Hailo::AUTHORITY = 'cpan:HINRIK'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | } | ||||||||||||
| 5 | BEGIN { | ||||||||||||
| 6 | 2 | ▶ | $POE::Component::Hailo::VERSION = '0.10'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | } | ||||||||||||
| 8 | |||||||||||||
| 9 | 2 | ▶ | use 5.010; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 2 | ▶ | use Carp 'croak'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 2 | ▶ | use Hailo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 2 | ▶ | use POE qw(Wheel::Run Filter::Reference); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | sub spawn { | ||||||||||||
| 17 | 1 | ▶ | my ($package, %args) = @_; | ||||||||||
|
Subroutine
spawn: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||
| 18 | |||||||||||||
| 19 | 1 | ▶ | croak "Hailo_args parameter missing" if ref $args{Hailo_args} ne 'HASH'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | 1 | my $options = delete $args{options}; | |||||||||||
| 21 | 1 | my $self = bless \%args, $package; | |||||||||||
| 22 | |||||||||||||
| 23 | $self->{response} = { | ||||||||||||
| 24 | 1 | learn => 'hailo_learned', | |||||||||||
| 25 | train => 'hailo_trained', | ||||||||||||
| 26 | reply => 'hailo_replied', | ||||||||||||
| 27 | learn_reply => 'hailo_learn_replied', | ||||||||||||
| 28 | stats => 'hailo_stats', | ||||||||||||
| 29 | save => 'hailo_saved', | ||||||||||||
| 30 | }; | ||||||||||||
| 31 | |||||||||||||
| 32 | POE::Session->create( | ||||||||||||
| 33 | object_states => [ | ||||||||||||
| 34 | $self => [qw( | ||||||||||||
| 35 | _start | ||||||||||||
| 36 | shutdown | ||||||||||||
| 37 | _sig_DIE | ||||||||||||
| 38 | _sig_chld | ||||||||||||
| 39 | _go_away | ||||||||||||
| 40 | _child_stderr | ||||||||||||
| 41 | _child_stdout | ||||||||||||
| 42 | )], | ||||||||||||
| 43 | $self => { | ||||||||||||
| 44 | 1 | ▶ | map { +$_ => '_hailo_method' } keys %{ $self->{response } }, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | } | ||||||||||||
| 46 | ], | ||||||||||||
| 47 | (ref $options eq 'HASH' ? (options => $options) : ()), | ||||||||||||
| 48 | ); | ||||||||||||
| 49 | |||||||||||||
| 50 | 1 | return $self; | |||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | sub _start { | ||||||||||||
| 54 | 1 | ▶ | my ($kernel, $session, $self) = @_[KERNEL, SESSION, OBJECT]; | ||||||||||
|
Subroutine
_start: called CC=3 SCAR=11.3
| |||||||||||||
| 55 | 1 | $self->{session_id} = $session->ID(); | |||||||||||
| 56 | 1 | $kernel->sig(DIE => '_sig_DIE'); | |||||||||||
| 57 | |||||||||||||
| 58 | 1 | ▶ | if (defined $self->{alias}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | 1 | $kernel->alias_set($self->{alias}); | |||||||||||
| 60 | } | ||||||||||||
| 61 | else { | ||||||||||||
| 62 | 0 | $kernel->refcount_increment($self->{session_id}, __PACKAGE__); | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | $self->{wheel} = POE::Wheel::Run->new( | ||||||||||||
| 66 | Program => \&_main, | ||||||||||||
| 67 | 1 | ▶ | ProgramArgs => [ %{ $self->{Hailo_args} } ], | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | StdoutEvent => '_child_stdout', | ||||||||||||
| 69 | StderrEvent => '_child_stderr', | ||||||||||||
| 70 | StdioFilter => POE::Filter::Reference->new, | ||||||||||||
| 71 | ( $^O eq 'MSWin32' ? ( CloseOnCall => 0 ) : ( CloseOnCall => 1 ) ), | ||||||||||||
| 72 | ); | ||||||||||||
| 73 | |||||||||||||
| 74 | 1 | $kernel->sig_child( $self->{wheel}->PID, '_sig_chld' ); | |||||||||||
| 75 | 1 | return; | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | sub _sig_DIE { | ||||||||||||
| 79 | 0 | ▶ | my ($kernel, $self, $ex) = @_[KERNEL, OBJECT, ARG1]; | ||||||||||
|
Subroutine
_sig_DIE: not called CC=1 SCAR=6.9
| |||||||||||||
| 80 | 0 | chomp $ex->{error_str}; | |||||||||||
| 81 | 0 | warn "Error: Event $ex->{event} in $ex->{dest_session} raised exception:\n"; | |||||||||||
| 82 | 0 | warn " $ex->{error_str}\n"; | |||||||||||
| 83 | 0 | $kernel->sig_handled(); | |||||||||||
| 84 | 0 | return; | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | sub session_id { | ||||||||||||
| 88 | 0 | ▶ | return $_[0]->{session_id}; | ||||||||||
|
Subroutine
session_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | sub _hailo_method { | ||||||||||||
| 92 | 1 | ▶ | my ($kernel, $self, $state, $args, $context) | ||||||||||
|
Subroutine
_hailo_method: called CC=4 SCAR=14.1
| |||||||||||||
| 93 | = @_[KERNEL, OBJECT, STATE, ARG0, ARG1]; | ||||||||||||
| 94 | 1 | my $sender = $_[SENDER]->ID(); | |||||||||||
| 95 | |||||||||||||
| 96 | 1 | ▶ | return if $self->{shutdown}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | |||||||||||||
| 98 | 1 | ▶ | $args //= [ ]; | ||||||||||
|
Condition50%1/2
$args //= []
MC/DC0%0/1
$args //= []
$args
Truth table50%1/2
A
A= $args
| |||||||||||||
| 99 | 1 | ▶ | $context = { %{ $context // { } } }; | ||||||||||
|
Condition50%1/2
$context // {}
MC/DC0%0/1
$context // {}
$context
Truth table50%1/2
A
A= $context
| |||||||||||||
| 100 | my $request = { | ||||||||||||
| 101 | args => $args, | ||||||||||||
| 102 | context => $context, | ||||||||||||
| 103 | method => $state, | ||||||||||||
| 104 | sender => $sender, | ||||||||||||
| 105 | 1 | event => $self->{response}{$state}, | |||||||||||
| 106 | }; | ||||||||||||
| 107 | |||||||||||||
| 108 | 1 | $kernel->refcount_increment($sender, __PACKAGE__); | |||||||||||
| 109 | 1 | $self->{wheel}->put($request); | |||||||||||
| 110 | |||||||||||||
| 111 | 1 | return; | |||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | sub _sig_chld { | ||||||||||||
| 115 | 1 | ▶ | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||||||||||
|
Subroutine
_sig_chld: called CC=2 SCAR=7.0
| |||||||||||||
| 116 | 1 | ▶ | $kernel->yield('shutdown') if !$self->{shutdown}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 117 | 1 | $kernel->yield('_go_away'); | |||||||||||
| 118 | 1 | $kernel->sig_handled(); | |||||||||||
| 119 | 1 | return; | |||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | sub _child_stderr { | ||||||||||||
| 123 | 0 | ▶ | my ($kernel, $self, $input) = @_[KERNEL, OBJECT, ARG0]; | ||||||||||
|
Subroutine
_child_stderr: not called CC=1 SCAR=6.9
| |||||||||||||
| 124 | 0 | warn "$input\n"; | |||||||||||
| 125 | 0 | return; | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | sub _child_stdout { | ||||||||||||
| 129 | 1 | ▶ | my ($kernel, $self, $input) = @_[KERNEL, OBJECT, ARG0]; | ||||||||||
|
Subroutine
_child_stdout: called CC=1 SCAR=0.0
| |||||||||||||
| 130 | 1 | $kernel->post(@$input{qw(sender event result context)}); | |||||||||||
| 131 | 1 | $kernel->refcount_decrement($input->{sender}, __PACKAGE__); | |||||||||||
| 132 | 1 | return; | |||||||||||
| 133 | } | ||||||||||||
| 134 | |||||||||||||
| 135 | sub shutdown { | ||||||||||||
| 136 | 1 | ▶ | my ($self) = $_[OBJECT]; | ||||||||||
|
Subroutine
shutdown: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 137 | 1 | $self->{shutdown} = 1; | |||||||||||
| 138 | 1 | $self->{wheel}->shutdown_stdin; | |||||||||||
| 139 | 1 | return; | |||||||||||
| 140 | } | ||||||||||||
| 141 | |||||||||||||
| 142 | sub _go_away { | ||||||||||||
| 143 | 1 | ▶ | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||||||||||
|
Subroutine
_go_away: called CC=3 SCAR=11.4
| |||||||||||||
| 144 | |||||||||||||
| 145 | 1 | delete $self->{wheel}; | |||||||||||
| 146 | 1 | $kernel->alias_remove($_) for $kernel->alias_list(); | |||||||||||
| 147 | 1 | ▶ | if (!defined $self->{alias}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 0 | $kernel->refcount_decrement($self->{session_id}, __PACKAGE__); | |||||||||||
| 149 | } | ||||||||||||
| 150 | 1 | return; | |||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | sub _main { | ||||||||||||
| 154 | 0 | ▶ | my (%args) = @_; | ||||||||||
|
Subroutine
_main: not called CC=5 SCAR=34.0
| |||||||||||||
| 155 | |||||||||||||
| 156 | 0 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 157 | 0 | binmode STDIN; | |||||||||||
| 158 | 0 | binmode STDOUT; | |||||||||||
| 159 | } | ||||||||||||
| 160 | |||||||||||||
| 161 | 0 | my $hailo; | |||||||||||
| 162 | 0 | eval { $hailo = Hailo->new(%args) }; | |||||||||||
| 163 | 0 | ▶ | if ($@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 164 | 0 | chomp $@; | |||||||||||
| 165 | 0 | warn "$@\n"; | |||||||||||
| 166 | 0 | return; | |||||||||||
| 167 | } | ||||||||||||
| 168 | |||||||||||||
| 169 | 0 | my $raw; | |||||||||||
| 170 | 0 | my $size = 4096; | |||||||||||
| 171 | 0 | my $filter = POE::Filter::Reference->new; | |||||||||||
| 172 | |||||||||||||
| 173 | 0 | ▶ | while (sysread STDIN, $raw, $size) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 174 | 0 | my $requests = $filter->get([$raw]); | |||||||||||
| 175 | 0 | for my $req (@$requests) { | |||||||||||
| 176 | 0 | my $method = $req->{method}; | |||||||||||
| 177 | 0 | $req->{result} = [$hailo->$method(@{ $req->{args} })]; | |||||||||||
| 178 | 0 | my $response = $filter->put([$req]); | |||||||||||
| 179 | 0 | print @$response; | |||||||||||
| 180 | } | ||||||||||||
| 181 | } | ||||||||||||
| 182 | |||||||||||||
| 183 | 0 | return; | |||||||||||
| 184 | } | ||||||||||||
| 185 | |||||||||||||
| 186 | 2 | 1; | |||||||||||
| 187 | |||||||||||||
| 188 | =encoding utf8 | ||||||||||||
| 189 | |||||||||||||
| 190 | =head1 NAME | ||||||||||||
| 191 | |||||||||||||
| 192 | POE::Component::Hailo - A non-blocking wrapper around L<Hailo|Hailo> | ||||||||||||
| 193 | |||||||||||||
| 194 | =head1 SYNOPSIS | ||||||||||||
| 195 | |||||||||||||
| 196 | use strict; | ||||||||||||
| 197 | use warnings; | ||||||||||||
| 198 | use POE qw(Component::Hailo); | ||||||||||||
| 199 | |||||||||||||
| 200 | POE::Session->create( | ||||||||||||
| 201 | package_states => [ | ||||||||||||
| 202 | (__PACKAGE__) => [ qw(_start hailo_learned hailo_replied) ], | ||||||||||||
| 203 | ], | ||||||||||||
| 204 | ); | ||||||||||||
| 205 | |||||||||||||
| 206 | POE::Kernel->run; | ||||||||||||
| 207 | |||||||||||||
| 208 | sub _start { | ||||||||||||
| 209 | POE::Component::Hailo->spawn( | ||||||||||||
| 210 | alias => 'hailo', | ||||||||||||
| 211 | Hailo_args => { | ||||||||||||
| 212 | storage_class => 'SQLite', | ||||||||||||
| 213 | brain_resource => 'hailo.sqlite', | ||||||||||||
| 214 | }, | ||||||||||||
| 215 | ); | ||||||||||||
| 216 | |||||||||||||
| 217 | POE::Kernel->post(hailo => learn => | ||||||||||||
| 218 | ['This is a sentence'], | ||||||||||||
| 219 | ); | ||||||||||||
| 220 | } | ||||||||||||
| 221 | |||||||||||||
| 222 | sub hailo_learned { | ||||||||||||
| 223 | POE::Kernel->post(hailo => reply => ['This']); | ||||||||||||
| 224 | } | ||||||||||||
| 225 | |||||||||||||
| 226 | sub hailo_replied { | ||||||||||||
| 227 | my $reply = $_[ARG0]->[0]; | ||||||||||||
| 228 | die "Didn't get a reply" if !defined $reply; | ||||||||||||
| 229 | print "Got reply: $reply\n"; | ||||||||||||
| 230 | POE::Kernel->post(hailo => 'shutdown'); | ||||||||||||
| 231 | } | ||||||||||||
| 232 | |||||||||||||
| 233 | =head1 DESCRIPTION | ||||||||||||
| 234 | |||||||||||||
| 235 | POE::Component::Hailo is a L<POE|POE> component that provides a | ||||||||||||
| 236 | non-blocking wrapper around L<Hailo|Hailo>. It accepts the events listed | ||||||||||||
| 237 | under L</INPUT> and emits the events listed under L</OUTPUT>. | ||||||||||||
| 238 | |||||||||||||
| 239 | =head1 METHODS | ||||||||||||
| 240 | |||||||||||||
| 241 | =head2 C<spawn> | ||||||||||||
| 242 | |||||||||||||
| 243 | This is the constructor. It takes the following arguments: | ||||||||||||
| 244 | |||||||||||||
| 245 | B<'alias'>, an optional alias for the component's session. | ||||||||||||
| 246 | |||||||||||||
| 247 | B<'Hailo_args'>, a hash reference of arguments to pass to L<Hailo|Hailo>'s | ||||||||||||
| 248 | constructor. | ||||||||||||
| 249 | |||||||||||||
| 250 | B<'options'>, a hash reference of options to pass to | ||||||||||||
| 251 | L<POE::Session|POE::Session>'s constructor. | ||||||||||||
| 252 | |||||||||||||
| 253 | =head2 C<session_id> | ||||||||||||
| 254 | |||||||||||||
| 255 | Takes no arguments. Returns the POE Session ID of the component. | ||||||||||||
| 256 | |||||||||||||
| 257 | =head1 INPUT | ||||||||||||
| 258 | |||||||||||||
| 259 | This component reacts to the following POE events: | ||||||||||||
| 260 | |||||||||||||
| 261 | =head2 C<learn> | ||||||||||||
| 262 | |||||||||||||
| 263 | =head2 C<train> | ||||||||||||
| 264 | |||||||||||||
| 265 | =head2 C<reply> | ||||||||||||
| 266 | |||||||||||||
| 267 | =head2 C<learn_reply> | ||||||||||||
| 268 | |||||||||||||
| 269 | =head2 C<stats> | ||||||||||||
| 270 | |||||||||||||
| 271 | =head2 C<save> | ||||||||||||
| 272 | |||||||||||||
| 273 | All these events take two arguments. The first is an array reference of | ||||||||||||
| 274 | arguments which will be passed to the L<Hailo|Hailo> method of the same | ||||||||||||
| 275 | name. The second (optional) is a hash reference. You'll get this hash | ||||||||||||
| 276 | reference back with the corresponding event listed under L</OUTPUT>. | ||||||||||||
| 277 | |||||||||||||
| 278 | =head2 C<shutdown> | ||||||||||||
| 279 | |||||||||||||
| 280 | Takes no arguments. Terminates the component. | ||||||||||||
| 281 | |||||||||||||
| 282 | =head1 OUTPUT | ||||||||||||
| 283 | |||||||||||||
| 284 | The component will post the following event to your session: | ||||||||||||
| 285 | |||||||||||||
| 286 | =head2 C<hailo_learned> | ||||||||||||
| 287 | |||||||||||||
| 288 | =head2 C<hailo_trained> | ||||||||||||
| 289 | |||||||||||||
| 290 | =head2 C<hailo_replied> | ||||||||||||
| 291 | |||||||||||||
| 292 | =head2 C<hailo_learn_replied> | ||||||||||||
| 293 | |||||||||||||
| 294 | =head2 C<hailo_stats> | ||||||||||||
| 295 | |||||||||||||
| 296 | =head2 C<hailo_saved> | ||||||||||||
| 297 | |||||||||||||
| 298 | C<ARG0> is an array reference of arguments returned by the underlying | ||||||||||||
| 299 | L<Hailo|Hailo> method. C<ARG1> is the context hashref you provided (if any). | ||||||||||||
| 300 | |||||||||||||
| 301 | =head1 AUTHOR | ||||||||||||
| 302 | |||||||||||||
| 303 | Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com | ||||||||||||
| 304 | |||||||||||||
| 305 | =head1 LICENSE AND COPYRIGHT | ||||||||||||
| 306 | |||||||||||||
| 307 | Copyright 2010 Hinrik E<Ouml>rn SigurE<eth>sson | ||||||||||||
| 308 | |||||||||||||
| 309 | This program is free software, you can redistribute it and/or modify | ||||||||||||
| 310 | it under the same terms as Perl itself. | ||||||||||||
| 311 | |||||||||||||
| 312 | =cut | ||||||||||||