Hopen/G/GraphBuilder.pm

filter: Statementstmt 40.0% 28 / 70 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 60.0% 9 / 15 Podpod 100% 5 / 5 totaltotal 32.8% 42 / 128 CC 17 SCAR 32.4
CC
17
Cov
27%
_wrapper
72.0
default_goal
20.0
goal
20.0
CRAP 25.6
↑ Summary Hopen/G/DAG.pm »
1 # Data::Hopen::G::GraphBuilder - fluent interface for building graphs
2 package Data::Hopen::G::GraphBuilder;
3 1 ▶use Data::Hopen;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Data::Hopen::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶our @EXPORT; BEGIN { @EXPORT=qw(make_GraphBuilder); }
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.000021';
11  
12 1 ▶use Class::Tiny {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 name => 'ANON', # Name is optional; it's here so the
14 # constructor won't croak if you use one.
15 dag => undef, # The current G::DAG instance
16 node => undef, # The last node added
17 1 };
18  
19 1 ▶use Class::Method::Modifiers qw(install_modifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Getargs::Mixed;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # Docs {{{1
24  
25 =head1 NAME
26  
27 Data::Hopen::G::GraphBuilder - fluent interface for building graphs
28  
29 =head1 SYNOPSIS
30  
31 A GraphBuilder wraps a L<Data::Hopen::G::DAG> and a current
32 L<Data::Hopen::G::Node>. It permits building chains of nodes in a
33 fluent way. For example, in an L<App::hopen> hopen file:
34  
35 # $Build is a Data::Hopen::G::DAG created by App::hopen
36 use language 'C';
37  
38 my $builder = $Build->C::compile(file => 'foo.c');
39 # Now $builder holds $Build (the DAG) and a node created by
40 # C::compile().
41  
42 =head1 ATTRIBUTES
43  
44 =head2 name
45  
46 An optional name, in case you want to identify your Builder instances.
47  
48 =head2 dag
49  
50 The current L<Data::Hopen::G::DAG> instance, if any.
51  
52 =head2 node
53  
54 The current L<Data::Hopen::G::Node> instance, if any.
55  
56 =head1 INSTANCE FUNCTIONS
57  
58 =cut
59  
60 # }}}1
61  
62 =head2 add
63  
64 Adds a node to the graph. Returns the node. Note that this B<does not>
65 change the builder's current node (L</node>).
66  
67 =cut
68  
69 0 ▶sub add {
Subroutine add: not called CC=1 SCAR=6.9
Pod documented
70 0 my ($self, %args) = getparameters('self', ['node'], @_);
71 0 $self->dag->add($args{node});
72 0 return $args{node};
73 } #add()
74  
75 =head2 default_goal
76  
77 Links the most recent node in the chain to the default goal in the DAG.
78 If the DAG does not have a default goal, adds one called "all".
79  
80 As a side effect, calling this function clears the builder's record of the
81 current node and returns C<undef>. The idea is that this function
82 will be used at the end of a chain of calls. Clearing state in this way
83 reduces the chance of unintentionally connecting nodes.
84  
85 =cut
86  
87 0 ▶sub default_goal {
Subroutine default_goal: not called CC=4 SCAR=30.0
Pod documented
88 0 ▶ my $self = shift or croak 'Need an instance';
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Need an instance") 0 0 0
89 0 ▶ croak "Need a node to link to the goal" unless $self->node;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->node 0 0 0
90  
91 0 ▶ my $goal = $self->dag->default_goal // $self->dag->goal('all');
Condition0%0/3
$self->dag->default_goal // $self->dag->goal("all")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->dag->default_goal // $self->dag->goal("all")
$self->dag->default_goal $self->dag->goal("all")
Truth table0%0/3
A // B
A= $self->dag->default_goalB= $self->dag->goal("all")
ABresult
1X1
011
000
92 0 $self->dag->add($self->node); # no harm in it - DAG::add() is idempotent
93 0 $self->dag->connect($self->node, $goal);
94  
95 0 $self->node(undef); # Less likely to leak state between goals.
96  
97 0 return undef;
98 # Also, if this is the last thing in an App::hopen hopen file,
99 # whatever it returns gets recorded in MY.hopen.pl. Therefore,
100 # return $self would cause a copy of the whole graph to be dropped into
101 # MY.hopen.pl, which would be a Bad Thing.
102 } #default_goal()
103  
104 =head2 goal
105  
106 Links the most recent node in the chain to the given goal in the DAG.
107 Clears the builder's record of the current node and returns undef.
108  
109 =cut
110  
111 0 ▶sub goal {
Subroutine goal: not called CC=4 SCAR=30.0
Pod documented
112 0 ▶ my $self = shift or croak 'Need an instance';
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Need an instance") 0 0 0
113 0 ▶ my $goal_name = shift or croak 'Need a goal name';
Branch: 1 branch
BranchTrueFalseTotal
my $goal_name = shift() or croak("Need a goal name") 0 0 0
114 0 ▶ croak "Need a node to link to the goal" unless $self->node;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->node 0 0 0
115  
116 0 my $goal = $self->dag->goal($goal_name);
117 0 $self->dag->add($self->node); # no harm in it - DAG::add() is idempotent
118 0 $self->dag->connect($self->node, $goal);
119  
120 0 $self->node(undef); # Less likely to leak state between goals.
121  
122 0 return undef; # undef: See comment in goal()
123 } #goal()
124  
125 =head2 to
126  
127 Connect one node to another, where both are wrapped in C<GraphBuilder>s.
128 Usage:
129  
130 $builder_1->to($builder_2);
131 # No $builder_1->node has an edge to $builder_2->node
132  
133 Returns C<undef>, because chaining would be ambiguous. For example,
134 in the snippet above, would the chain continue from C<$builder_1> or
135 C<$builder_2>?
136  
137 Does not change the state of either GraphBuilder.
138  
139 =cut
140  
141 0 ▶sub to {
Subroutine to: not called CC=3 SCAR=24.8
Pod documented
142 0 my ($self, %args) = parameters('self', [qw(dest)], @_);
143 croak 'Destination is not a ' . __PACKAGE__
144 0 ▶ unless $args{dest}->DOES(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"dest"}->DOES(__PACKAGE__) 0 0 0
145 croak 'Cannot connect nodes from different graphs'
146 0 ▶ if refaddr($self->dag) != refaddr($args{dest}->dag);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($self->dag) != builtin::refaddr($args{"dest"}->dag) 0 0 0
147  
148 0 $self->dag->connect($self->node, $args{dest}->node);
149 0 return undef;
150 } #to()
151  
152 =head1 STATIC FUNCTIONS
153  
154 =head2 make_GraphBuilder
155  
156 Given the name of a subroutine, wrap the given subroutine for use in a
157 GraphBuilder chain such as that shown in the L</SYNOPSIS>. Usage:
158  
159 sub worker {
160 my $graphbuilder = shift;
161 ...
162 return $node; # Will automatically be linked into the chain
163 }
164  
165 make_GraphBuilder 'worker';
166 # now worker can take a DAG or GraphBuilder, and the
167 # return value will be the GraphBuilder.
168  
169 The C<worker> subroutine is called in scalar context.
170  
171 =cut
172  
173 sub _wrapper;
174  
175 0 ▶sub make_GraphBuilder {
Subroutine make_GraphBuilder: not called CC=2 SCAR=17.9
Pod documented
176 0 my $target = caller;
177 0 ▶ my $funcname = shift or croak 'Need the name of the sub to wrap'; # yum
Branch: 1 branch
BranchTrueFalseTotal
my $funcname = shift() or croak("Need the name of the sub to wrap") 0 0 0
178  
179 0 install_modifier $target, 'around', $funcname, \&_wrapper;
180 } #make_GraphBuilder()
181  
182 # The "around" modifier
183 0 ▶sub _wrapper {
Subroutine _wrapper: not called CC=8 SCAR=42.8
184 0 ▶ my $orig = shift or die 'Need a function to wrap';
Branch: 1 branch
BranchTrueFalseTotal
my $orig = shift() or die "Need a function to wrap" 0 0 0
185 0 ▶ croak "Need a parameter" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
186  
187 # Create the GraphBuilder if we don't have one already.
188 0 my $self = shift;
189 $self = __PACKAGE__->new(dag=>$self)
190 0 ▶ unless eval { $self->DOES(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->DOES(__PACKAGE__) } } 0 0 0
191 croak "Parameter must be a DAG or Builder"
192 0 ▶ unless eval { $self->dag->DOES('Data::Hopen::G::DAG') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->dag->DOES("Data::Hopen::G::DAG") } } 0 0 0
193  
194 0 unshift @_, $self; # Put the builder on the arg list
195  
196 # Call the worker
197 0 my $worker_retval = &{$orig}; # @_ passed to code
198  
199 # If we got a node, remember it.
200 0 ▶ if(ref $worker_retval && eval { $worker_retval->DOES('Data::Hopen::G::Node') } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $worker_retval and eval { do { $worker_retval->DOES("Data::Hopen::G::Node") } }) 0 0 0
Condition0%0/3
ref $worker_retval and eval { do { $worker_retval->DOES("Data::Hopen::G::Node") } }
!ll&&!rl&&r
000
MC/DC0%0/2
ref $worker_retval and eval { do { $worker_retval->DOES("Data::Hopen::G::Node") } }
ref $worker_retval eval { do { $worker_retval->DOES("Data::Hopen::G::Node") } }
Truth table0%0/3
A and B
A= ref $worker_retvalB= eval { do { $worker_retval->DOES("Data::Hopen::G::Node") } }
ABresult
0X0
100
111
201 0 $self->dag->add($worker_retval); # Link it into the graph
202 0 ▶ $self->dag->connect($self->node, $worker_retval) if $self->node;
Branch: 1 branch
BranchTrueFalseTotal
if $self->node 0 0 0
203  
204 0 $self->node($worker_retval); # It's now our current node
205 }
206  
207 0 return $self;
208 }; #_wrapper()
209  
210 1 1;
211 __END__
↑ Summary Hopen/G/DAG.pm »