Hopen/G/Runnable.pm

filter: Statementstmt 96.3% 51 / 54 (1 uncoverable) Branchbran 100% 10 / 10 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 5 / 5 Subroutinesub 82.3% 14 / 17 Podpod 100% 2 / 2 totaltotal 94.7% 90 / 96 (1 uncoverable) CC 10 SCAR 11.0
CC
10
Cov
97%
passthrough
6.0
run
5.0
__ANON__
2.0
CRAP 3.0
« Hopen/G/Node.pm ↑ Summary Hopen/Scope/Hash.pm »
1 # Data::Hopen::G::Runnable - parent class for anything runnable in a hopen graph
2 package Data::Hopen::G::Runnable;
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use Data::Hopen::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 our $VERSION = '0.000021';
7  
8 15 ▶use Data::Hopen;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Data::Hopen::Scope::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Data::Hopen::Util::Data qw(forward_opts);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Data::Hopen::Util::NameSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Docs {{{1
15  
16 =head1 NAME
17  
18 Data::Hopen::G::Runnable - parent class for runnable things in a hopen graph
19  
20 =head1 SYNOPSIS
21  
22 Anything with L</run> inherits from this. TODO should this be a role?
23  
24 =head1 ATTRIBUTES
25  
26 =head2 need
27  
28 (B<Not currently used>)
29 Inputs this Runnable requires.
30 A L<Data::Hopen::Util::NameSet>, with the restriction that C<need> may not
31 contain regexes. ("Sorry, I can't run unless you give me every variable
32 in the world that starts with Q." I don't think so!)
33 Or maybe later an arrayref? TODO.
34  
35 =head2 scope
36  
37 If defined, a L<Data::Hopen::Scope> that will have the final say on the
38 data used by L</run>. This is the basis of the fine-grained override
39 mechanism in hopen.
40  
41 =head2 want
42  
43 (B<Not currently used>)
44 Inputs this Runnable accepts but does not require.
45 A L<Data::Hopen::Util::NameSet>, which may include regexes.
46 Or maybe later an arrayref? TODO.
47  
48 =cut
49  
50 # }}}1
51  
52 15 ▶use parent 'Data::Hopen::G::Entity';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 15 ▶use Class::Tiny {
Subroutine BEGIN: called CC=1 SCAR=0.4
54 # NOTE: want and need are not currently used.
55 0 ▶ want => sub { Data::Hopen::Util::NameSet->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 ▶ need => sub { Data::Hopen::Util::NameSet->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
57  
58 58 ▶ scope => sub { Data::Hopen::Scope::Hash->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 15 };
60  
61 =head1 FUNCTIONS
62  
63 =head2 run
64  
65 Run the operation, whatever that means. Returns a new hashref.
66 Usage:
67  
68 my $hrOutputs = $op->run([options])
69  
70 Options are:
71  
72 =over
73  
74 =item -context
75  
76 A L<Data::Hopen::Scope> or subclass including the inputs the caller wants to
77 pass to the Runnable. The L</scope> of the Runnable itself may override
78 values in the C<context>.
79  
80 =item -visitor
81  
82 If given, an instance that supports C<visit_goal()> and C<visit_node()> calls.
83 A L<Data::Hopen::G::DAG> instance invokes those calls after processing each
84 goal or other node, respectively. They are invoked I<after> the goal or
85 node has run. They are, however, given access to the L<Data::Hopen::Scope>
86 that the node used for its inputs, in the C<$node_inputs> parameter. Example:
87  
88 $visitor->visit_goal($goal, $node_inputs);
89  
90 The return value from C<visit_goal()> or C<visit_node()> is ignored.
91  
92 =item -nocontext
93  
94 If C<< -nocontext=>1 >> is specified, don't link a context scope into
95 this one. May not be specified together with C<-context>.
96  
97 =back
98  
99 See the source for this function, which contains as an example of setting the
100 scope.
101  
102 =cut
103  
104 114 ▶sub run {
Subroutine run: called CC=5 SCAR=16.1
Pod documented
105 114 my ($self, %args) = getparameters('self', [qw(; context visitor nocontext)], @_);
106 114 my $context_scope = $args{context}; # which may be undef - that's OK
107 114 ▶ croak "Can't combine -context and -nocontext" if $args{context} && $args{nocontext};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"context"} and $args{"nocontext"} 1 113 114
Condition100%3/3
$args{"context"} and $args{"nocontext"}
!ll&&!rl&&r
111021
MC/DC100%2/2
$args{"context"} and $args{"nocontext"}
$args{"context"} $args{"nocontext"}
Truth table100%3/3
A and B
A= $args{"context"}B= $args{"nocontext"}
ABresult
0X0
100
111
108  
109 # Link the outer scope to our scope
110 113 ▶ my $saver = $args{nocontext} ? undef : $self->scope->outerize($context_scope);
Branch: 1 branch
BranchTrueFalseTotal
$args{'nocontext'} ? : 1 112 113
111  
112 113 ▶ hlog { '->', ref($self), $self->name, 'input', Dumper($self->scope->as_hashref) } 3;
Subroutine __ANON__: called CC=1 SCAR=0.0
113  
114 113 my $retval = $self->_run(forward_opts(\%args, {'-'=>1}, qw[visitor]));
115  
116 105 ▶ die "$self\->_run() did not return a hashref" unless ref $retval eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $retval eq "HASH" 1 104 105
117 # Prevent errors about `non-hashref 1` or `invalid key`.
118  
119 104 ▶ hlog { '<-', ref $self, $self->name, 'output', Dumper($retval) } 3;
Subroutine __ANON__: called CC=1 SCAR=0.0
120  
121 104 return $retval;
122 } #run()
123  
124 =head2 _run
125  
126 The internal method that implements L</run>. Must be implemented by
127 subclasses. When C<_run> is called, C<< $self->scope >> has been hooked
128 to the context scope, if any.
129  
130 The only parameter is C<-visitor>, which is always passed by name
131 (C<< -visitor=>$v >>). C<_run> is always called in scalar context,
132 and B<must> return a new hashref.
133  
134 I recommend starting your C<_run> function with:
135  
136 my ($self, %args) = getparameters('self', [qw(; visitor)], @_);
137  
138 and working from there.
139  
140 =cut
141  
142 0 ▶sub _run {
Subroutine _run: not called CC=1 SCAR=0.0
143 # uncoverable subroutine
144 0 die('Unimplemented'); # uncoverable statement
145 }
146  
147 =head2 passthrough
148  
149 Returns a new hashref of this Runnable's local values, as defined
150 by L<Data::Hopen::Scope/local>. Usage:
151  
152 my $hashref = $runnable->passthrough([-context => $outer_scope]);
153 # To use $outer_scope as the context
154 my $hashref = $runnable->passthrough(-nocontext => 1);
155 # To ignore the context
156  
157 Other valid options include L<-levels|Data::Hopen::Scope/$levels>.
158  
159 =cut
160  
161 57 ▶sub passthrough {
Subroutine passthrough: called CC=6 SCAR=17.9
Pod documented
162 57 my ($self, %args) = getparameters('self', ['*'], @_);
163 57 my $outer_scope = $args{context}; # which may be undef - that's OK
164 57 ▶ croak "Can't combine -context and -nocontext" if $args{context} && $args{nocontext};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"context"} and $args{"nocontext"} 1 56 57
Condition100%3/3
$args{"context"} and $args{"nocontext"}
!ll&&!rl&&r
5511
MC/DC100%2/2
$args{"context"} and $args{"nocontext"}
$args{"context"} $args{"nocontext"}
Truth table100%3/3
A and B
A= $args{"context"}B= $args{"nocontext"}
ABresult
0X0
100
111
165  
166 # Link the outer scope to our scope
167 56 ▶ my $saver = $args{nocontext} ? undef : $self->scope->outerize($outer_scope);
Branch: 1 branch
BranchTrueFalseTotal
$args{'nocontext'} ? : 55 1 56
168  
169 # Copy the names
170 56 ▶ my $levels = $args{levels} // 'local';
Condition100%2/2
$args{"levels"} // "local"
l!l
533
MC/DC100%1/1
$args{"levels"} // "local"
$args{"levels"}
Truth table100%2/2
A
A= $args{"levels"}
Aresult
11
00
171 56 my @names = @{$self->scope->names(-levels=>$levels)};
172 56 my $retval = {};
173 56 $retval->{$_} = $self->scope->find($_, -levels=>$levels) foreach @names;
174  
175 56 return $retval;
176 } #passthrough()
177  
178 15 1;
179 __END__
« Hopen/G/Node.pm ↑ Summary Hopen/Scope/Hash.pm »