blib/lib/FSM/Tiny.pm

filter: Statementstmt 87.6% 78 / 89 Branchbran 61.5% 16 / 26 Conditioncond 75.0% 6 / 8 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 84.2% 16 / 19 Podpod 80.0% 4 / 5 totaltotal 80.7% 122 / 151 CC 20 SCAR 15.4
CC
20
Cov
81%
new
15.3
new
5.0
run
4.0
CRAP 4.7
↑ Summary
1 package FSM::Tiny;
2  
3 # http://www.crsr.net/Programming_Languages/PerlAutomata.html
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.03';
8 2 our $DEBUG = 0;
9  
10 2 ▶use Class::Accessor::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 my %Defaults = (
13 current => '',
14 4 (map { $_ => {} } qw/rules context/),
15 2 ▶ (map { $_ => sub {} } qw/on_enter on_exit on_transition/)
Subroutine __ANON__: not called CC=1 SCAR=0.0
16 );
17  
18 2 Class::Accessor::Lite->mk_accessors(keys %Defaults);
19  
20 1 ▶sub new {
Subroutine new: called CC=6 SCAR=27.3
Pod documented
21 1 my $package = shift;
22 0 ▶ my %args = $_[1] ? %{ @_ } : %{ $_[0] };
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 1 1
23 1 my $self = bless +{ %Defaults, %args }, $package;
24  
25 1 for my $key (keys %{ $self->rules }) {
26 0 my $s = $self->rules->{$key};
27 0 ▶ if (my $r = ref $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = ref $s) { } 0 0 0
28 0 ▶ if ($r eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 0 0 0
elsif ($r eq 'CODE') { } 0 0 0
29 0 $self->register($key, @$s);
30 }
31 elsif ($r eq 'CODE') {
32 0 $self->register($key, $s);
33 }
34 }
35 else {
36 0 delete $self->rules->{$key};
37 }
38 }
39  
40 1 return $self;
41 }
42  
43 44 ▶sub _log { warn "[FSM::Simele DEBUG] ".join(' ', @_) . "\n" if $DEBUG }
Subroutine _log: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $FSM::Tiny::DEBUG 0 44 44
44  
45 3 ▶sub register {
Subroutine register: called CC=3 SCAR=11.0
Pod documented
46 3 my $self = shift;
47 3 my ($key, $code, $guards) = @_;
48 3 ▶ $self->current($key) unless $self->current;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->current 1 2 3
49 3 ▶ $guards ||= [];
Condition100%2/2
$guards ||= []
l!l
21
MC/DC100%1/1
$guards ||= []
$guards
Truth table100%2/2
A
A= $guards
Aresult
11
00
50 3 _log("register: ${key}");
51 3 $self->rules->{$key} = FSM::Tiny::State->new(
52 code => $code,
53 guards => $guards
54 );
55 }
56  
57 0 ▶sub unregister {
Subroutine unregister: not called CC=1 SCAR=6.9
Pod undocumented
58 0 my ($self, $key) = @_;
59 0 _log("unregister: ${key}");
60 0 delete $self->rules->{$key};
61 }
62  
63 42 ▶sub step {
Subroutine step: called CC=3 SCAR=11.0
Pod documented
64 42 my $self = shift;
65 42 ▶ my $st = $self->rules->{$self->current} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $st = $self->rules->{$self->current} or return 42 0 42
66 42 $st->run($self->context);
67 42 ▶ my $next = $st->next($self->context) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $next = $st->next($self->context) or return 41 1 42
68 41 $self->current($next);
69 41 _log("next -> " . $self->current);
70 41 return 1;
71 }
72  
73 1 ▶sub run {
Subroutine run: called CC=4 SCAR=13.9
Pod documented
74 1 my $self = shift;
75 1 ▶ $self->context(+{ %{ $self->context }, %{ $_[0] || {} } });
Condition50%1/2
$_[0] || {}
l!l
01
MC/DC0%0/1
$_[0] || {}
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
76 1 local $_ = $self->context;
77 1 $self->on_enter->($self->context);
78 1 while (1) {
79 42 ▶ if (!@{$self->rules->{$self->current}{guards}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->rules->{$self->current}{"guards"};}) 1 41 42
80 1 $self->step;
81 1 last;
82 }
83 41 ▶ $self->step or last;
Branch: 1 branch
BranchTrueFalseTotal
$self->step or last 41 0 41
84 41 $self->on_transition->($self->context);
85 }
86 1 $self->on_exit->($self->context);
87 1 $self;
88 }
89  
90 package FSM::Tiny::State;
91  
92 3 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
93 3 my $package = shift;
94 3 my %args = @_;
95 3 ▶ my @guards = @{ $args{guards} || [] };
Condition50%1/2
$args{'guards'} || []
l!l
30
MC/DC0%0/1
$args{'guards'} || []
$args{'guards'}
Truth table50%1/2
A
A= $args{'guards'}
Aresult
11
00
96 3 my @list;
97 3 ▶ while (@guards) {
Branch: 1 branch
BranchTrueFalseTotal
if (@guards) 3 3 6
98 3 my ($key, $code) = splice @guards, 0, 2;
99 push @list, FSM::Tiny::Guard->new(
100 key => $key,
101 3 ▶ code => (ref($code) || '') ne 'CODE' ? sub { $code } : $code
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
(ref $code || '') ne 'CODE' ? : 1 2 3
Condition100%2/2
ref $code || ''
l!l
21
MC/DC100%1/1
ref $code || ''
ref $code
Truth table100%2/2
A
A= ref $code
Aresult
11
00
102 );
103 }
104 3 $args{guards} = \@list;
105 3 bless \%args, $package;
106 }
107  
108 42 ▶sub next {
Subroutine next: called CC=3 SCAR=11.0
109 42 my ($self, $context) = @_;
110 42 for my $guard (@{ $self->{guards} }) {
111 42 ▶ return $guard->key if $guard->check($context);
Branch: 1 branch
BranchTrueFalseTotal
if $guard->check($context) 41 1 42
112 }
113 1 return '';
114 }
115  
116 42 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
117 42 my ($self, $context) = @_;
118 42 local $_ = $context;
119 42 $self->{code}->($context);
120 }
121  
122 package FSM::Tiny::Guard;
123  
124 41 ▶sub key { shift->{key} }
Subroutine key: called CC=1 SCAR=0.0
125  
126 42 ▶sub code { shift->{code} }
Subroutine code: called CC=1 SCAR=0.0
127  
128 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.2
129 3 my $package = shift;
130 3 my %args = @_;
131 0 ▶ bless +{ key => '', code => sub { 1 }, %args }, $package;
Subroutine __ANON__: not called CC=1 SCAR=1.2
132 }
133  
134 42 ▶sub check {
Subroutine check: called CC=1 SCAR=0.0
135 42 my ($self, $context) = @_;
136 42 return $self->code->($context);
137 }
138  
139 2 1;
140 __END__
↑ Summary