| 1 | package Langertha::Virtual::Rules; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Helpers that build the rules of Langertha::Engine::Virtual | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 10 | our @EXPORT_OK = qw( answer tool tools after error ); | |||||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | our %EXPORT_TAGS = ( all => \@EXPORT_OK ); | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # The optional arguments of a reaction helper: only usage, a HashRef. | ||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 168 | ▶ | sub _reaction_options { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reaction_options: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 168 | my ( $helper, @options ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 168 | ▶ | croak __PACKAGE__."::$helper takes usage => \\%usage after its arguments" if @options % 2; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 168 | my %options = @options; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 168 | my @unknown = grep { $_ ne 'usage' } sort keys %options; | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | 168 | ▶ | croak __PACKAGE__."::$helper has unknown options: @unknown (known: usage)" if @unknown; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 167 | ▶ | return () unless exists $options{usage}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 19 | ▶ | croak __PACKAGE__."::$helper usage must be a HashRef" unless ref $options{usage} eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 18 | return ( usage => $options{usage} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 83 | ▶ | sub answer { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
answer: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 83 | my ( $text, @options ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 83 | ▶ | croak __PACKAGE__.'::answer needs a text' unless defined $text && !ref $text; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $text and not ref $text
MC/DC50%1/2
defined $text and not ref $text
defined $text
not ref $text
Truth table66%2/3
A and not(B)
A= defined $textB= not ref $text
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 82 | return { answer => $text, _reaction_options( answer => @options ) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 69 | ▶ | sub tool { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tool: called CC=6 SCAR=19.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 69 | my ( $name, $arguments, @options ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 69 | ▶ | croak __PACKAGE__.'::tool needs a tool name' unless defined $name && !ref $name && length $name; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $name and not ref $name and length $name
defined $name and not ref $name
MC/DC0%0/3
defined $name and not ref $name and length $name
defined $name
not ref $name
length $name
Truth table25%1/4
A and not(B) and C
A= defined $nameB= not ref $nameC= length $name
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 69 | ▶ | $arguments //= {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$arguments //= {}
MC/DC100%1/1
$arguments //= {}
$arguments
Truth table100%2/2
A
A= $arguments
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 69 | ▶ | croak __PACKAGE__.'::tool arguments must be a HashRef' unless ref $arguments eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 68 | return { tool_calls => [ [ $name, $arguments ] ], _reaction_options( tool => @options ) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 21 | ▶ | sub tools { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tools: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 21 | my ( $calls, @options ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | 21 | ▶ | croak __PACKAGE__.'::tools needs an ArrayRef of calls' unless ref $calls eq 'ARRAY' && @$calls; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $calls eq "ARRAY" and @$calls
MC/DC0%0/2
ref $calls eq "ARRAY" and @$calls
ref $calls eq "ARRAY"
@$calls
Truth table33%1/3
A and B
A= ref $calls eq "ARRAY"B= @$calls
| |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 21 | my @tool_calls; | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 21 | for my $call (@$calls) { | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | 38 | ▶ | if ( ref $call eq 'ARRAY' && @$call <= 2 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $call eq "ARRAY" and @$call <= 2
MC/DC100%2/2
ref $call eq "ARRAY" and @$call <= 2
ref $call eq "ARRAY"
@$call <= 2
Truth table100%3/3
A and B
A= ref $call eq "ARRAY"B= @$call <= 2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 34 | push @tool_calls, @{ tool(@$call)->{tool_calls} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | ▶ | elsif ( ref $call eq 'HASH' && ref $call->{tool_calls} eq 'ARRAY' ) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $call eq "HASH" and ref $$call{"tool_calls"} eq "ARRAY"
MC/DC50%1/2
ref $call eq "HASH" and ref $$call{"tool_calls"} eq "ARRAY"
ref $call eq "HASH"
ref $$call{"tool_calls"} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $call eq "HASH"B= ref $$call{"tool_calls"} eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | croak __PACKAGE__.'::tools takes the usage of the whole turn: give it to tools(...), not to a tool(...) in it' | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | ▶ | if exists $call->{usage}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | push @tool_calls, @{ $call->{tool_calls} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | croak __PACKAGE__.'::tools takes [ $name, \%arguments ] pairs or tool(...) reactions'; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 18 | return { tool_calls => \@tool_calls, _reaction_options( tools => @options ) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 27 | ▶ | sub error { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 27 | my ( $status, $message ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | 27 | ▶ | croak __PACKAGE__.'::error needs an HTTP status from 400 to 599' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $status and not ref $status and $status =~ /\A[45]\d\d\z/
defined $status and not ref $status
MC/DC33%1/3
defined $status and not ref $status and $status =~ /\A[45]\d\d\z/
defined $status
not ref $status
$status =~ /\A[45]\d\d\z/
Truth table50%2/4
A and not(B) and C
A= defined $statusB= not ref $statusC= $status =~ /\A[45]\d\d\z/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | unless defined $status && !ref $status && $status =~ /\A[45]\d\d\z/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 25 | ▶ | croak __PACKAGE__.'::error needs a message' unless defined $message && !ref $message; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $message and not ref $message
MC/DC100%2/2
defined $message and not ref $message
defined $message
not ref $message
Truth table100%3/3
A and not(B)
A= defined $messageB= not ref $message
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 23 | return { error => { status => 0 + $status, message => $message } }; | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 10 | my %AFTER_KEYS = map { $_ => 1 } qw( tool result ); | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 21 | ▶ | sub after { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
after: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 21 | my ( @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | 21 | ▶ | my %match = @args == 1 ? ( tool => $args[0] ) : @args % 2 ? () : @args; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | croak __PACKAGE__.'::after takes a tool name, or tool => ... and result => ...; ' | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | .'it is a trigger: write after(...) => $reaction' | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 21 | ▶ | if ( @args > 1 && @args % 2 ) || grep { !$AFTER_KEYS{$_} } keys %match; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
@args > 1 and @args % 2 or grep {not $AFTER_KEYS{$_};} keys %match
@args > 1 and @args % 2
MC/DC100%3/3
@args > 1 and @args % 2 or grep {not $AFTER_KEYS{$_};} keys %match
@args > 1
@args % 2
grep {not $AFTER_KEYS{$_};} keys %match
Truth table80%4/5
A and B or C
A= @args > 1B= @args % 2C= grep {not $AFTER_KEYS{$_};} keys %match
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 19 | for my $key ( keys %match ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | 17 | my $value = $match{$key}; | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 17 | ▶ | croak __PACKAGE__."::after $key must be a string or a Regexp" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $value and ref $value and ref $value ne "Regexp"
defined $value and ref $value
MC/DC0%0/3
defined $value and ref $value and ref $value ne "Regexp"
defined $value
ref $value
ref $value ne "Regexp"
Truth table50%2/4
A and B and C
A= defined $valueB= ref $valueC= ref $value ne "Regexp"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | if defined $value && ref $value && ref $value ne 'Regexp'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 19 | return { after => \%match }; | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||