Grammar.pm

filter: Statementstmt 96.5% 83 / 86 Branchbran 68.7% 22 / 32 Conditioncond 66.6% 16 / 24 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 83.3% 145 / 174 CC 28 SCAR 29.2
CC
28
Cov
85%
parse_graph
29.9
__ANON__
4.1
__ANON__
2.1
CRAP 18.5
↑ Summary Grammar/Rule/Edge.pm »
1 package Graph::Grammar;
2  
3 # ABSTRACT: Grammar for graphs
4 2 our $VERSION = '0.2.0'; # VERSION
5  
6 =head1 NAME
7  
8 Graph::Grammar - Graph grammar, i.e. rewriting method
9  
10 =head1 SYNOPSIS
11  
12 use Graph::Grammar;
13 use Graph::Undirected;
14  
15 my $graph = Graph::Undirected->new;
16  
17 # Create graph here
18  
19 my @rules = (
20 [ sub { 1 }, ( sub { 1 } ) x 2, NO_MORE_VERTICES, sub { [ @_[1..3] ] } ],
21 );
22  
23 parse_graph( $graph, @rules );
24  
25 =head1 DESCRIPTION
26  
27 Graph::Grammar is a Perl implementation of a graph rewriting method (a.k.a. graph grammar).
28 Much of the API draws inspiration from L<Parse::Yapp>, but instead of acting on text streams Graph::Grammar is oriented at graphs, as implemented in Perl's L<Graph> module.
29 Graph::Grammar implements a single method C<parse_graph()> which accepts an instance of L<Graph> and an array of rules.
30 Every rule is evaluated for each vertex in a graph and, if a match is found, an action associated with the rule is executed.
31 A rule generally looks like this:
32  
33 [ $vertex_condition, @neighbour_conditions, $action ]
34  
35 Where:
36  
37 C<$vertex_condition> is a subroutine reference evaluating the center vertex.
38 The subroutine is called with the graph in C<$_[0]> and the vertex in <$_[1]>.
39 Subroutine should evaluate to true if condition is fulfilled.
40  
41 C<@neighbour_conditions> is an array of subroutine references for the neighbours of the center vertex.
42 Inputs and outputs of each subroutine reference are the same as defined for C<$vertex_condition>.
43 Every condition has to match at least one of the neighbours (without overlaps).
44 Thus the rule will automatically fail if the number of neighbours is less than C<@neighbour_conditions>.
45 There can be more neighbours than C<@neighbour_conditions>, but if strict number of neighbours is needed, look below for C<NO_MORE_VERTICES>.
46 C<@neighbour_conditions> can be empty.
47  
48 C<$action> can be either a subroutine reference, or anything else.
49 If C<$action> is a subroutine reference, then in the case of a match it is called with the graph in C<$_[0]> and remaining C<@_> members being graph vertices corresponding to rule conditions.
50 That is, C<$_[1]> is the center vertex, C<$_[2]> is a vertex matching the first neighbour condition and so on.
51 If C<$action> is not a subroutine reference, then it is cloned by L<Clone> and inserted instead of the center vertex.
52  
53 There are two ways to request a particular number of neighbours for the central vertex.
54 First of them is to include an appropriate requirement into C<$vertex_condition>.
55 Second is to put C<NO_MORE_VERTICES> as the last element of C<@neighbour_conditions>, i.e.:
56  
57 [ sub { 1 }, ( sub { 1 } ) x 2, NO_MORE_VERTICES, sub { [ @_[1..3] ] } ]
58  
59 Edge conditions are also supported and they always act on the center vertex and its neighbours matching their individual conditions, i.e.:
60  
61 [ $vertex_condition,
62 EDGE { $edge_condition1->( @_ ) }, $vertex_condition1,
63 EDGE { $edge_condition2->( @_ ) }, $vertex_condition2,
64 # ...
65 $action ]
66  
67 =cut
68  
69 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 2 ▶use parent Exporter::;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 2 our @EXPORT = qw( EDGE NO_MORE_VERTICES parse_graph );
74  
75 2 ▶use Clone qw( clone );
Subroutine BEGIN: called CC=1 SCAR=0.0
76 2 ▶use Graph::Grammar::Rule::Edge;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 ▶use Graph::Grammar::Rule::NoMoreVertices;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 2 ▶use Graph::MoreUtils qw( graph_replace );
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
80 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use Set::Object qw( set );
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 2 our $DEBUG = 0;
84  
85 =head1 METHODS
86  
87 =head2 C<parse_graph( $graph, @rules )>
88  
89 Perform graph rewriting of C<$graph>.
90 Modifies the supplied graph and returns it upon completion.
91  
92 =cut
93  
94 sub parse_graph
95 5 ▶{
Subroutine parse_graph: called CC=25 SCAR=34.0
Pod documented
96 5 my( $graph, @rules ) = @_;
97  
98 5 my $changes = 1;
99  
100 MAIN:
101 5 ▶ while( $changes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changes) 8 5 13
102 8 $changes = 0;
103  
104 8 for my $i (0..$#rules) {
105 8 my $rule = $rules[$i];
106 8 my @rule = @$rule;
107 8 my $rule_name;
108 8 my $self_rule = shift @rule;
109  
110 # First element in the rule could be a rule name
111 8 ▶ if( !ref $self_rule ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self_rule) 8 0 8
112 8 $rule_name = $self_rule;
113 8 $self_rule = shift @rule;
114 }
115  
116 8 my $action = pop @rule;
117 8 my $no_more_vertices;
118 8 ▶ if( @rule && blessed $rule[-1] && $rule[-1]->isa( Graph::Grammar::Rule::NoMoreVertices:: ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rule and builtin::blessed($rule[-1]) and $rule[-1]->isa("Graph::Grammar::Rule::NoMoreVertices")) 3 5 8
Condition83%5/6
@rule and builtin::blessed($rule[-1]) and $rule[-1]->isa("Graph::Grammar::Rule::NoMoreVertices")
!ll&&!rl&&r
503
@rule and builtin::blessed($rule[-1])
!ll&&!rl&&r
323
MC/DC66%2/3
@rule and builtin::blessed($rule[-1]) and $rule[-1]->isa("Graph::Grammar::Rule::NoMoreVertices")
@rule builtin::blessed($rule[-1]) $rule[-1]->isa("Graph::Grammar::Rule::NoMoreVertices")
Truth table75%3/4
A and B and C
A= @ruleB= builtin::blessed($rule[-1])C= $rule[-1]->isa("Graph::Grammar::Rule::NoMoreVertices")
ABCresult
0XX0
10X0
1100
1111
119 3 $no_more_vertices = 1;
120 3 pop @rule;
121 }
122  
123 8 my $neighbours = grep { ref $_ eq 'CODE' } @rule;
124  
125 8 my $affected_vertices = set();
126  
127 VERTEX:
128 8 for my $vertex ($graph->vertices) {
129 60 ▶ next unless $self_rule->( $graph, $vertex );
Branch: 1 branch
BranchTrueFalseTotal
unless &$self_rule($graph, $vertex) 18 42 60
130 42 ▶ next unless defined $graph->degree( $vertex );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $graph->degree($vertex) 0 42 42
131 42 ▶ next if $graph->degree( $vertex ) < $neighbours;
Branch: 1 branch
BranchTrueFalseTotal
if $graph->degree($vertex) < $neighbours 0 42 42
132 42 ▶ next if $no_more_vertices && $graph->degree( $vertex ) > $neighbours;
Branch: 1 branch
BranchTrueFalseTotal
if $no_more_vertices and $graph->degree($vertex) > $neighbours 18 24 42
Condition100%3/3
$no_more_vertices and $graph->degree($vertex) > $neighbours
!ll&&!rl&&r
18618
MC/DC100%2/2
$no_more_vertices and $graph->degree($vertex) > $neighbours
$no_more_vertices $graph->degree($vertex) > $neighbours
Truth table100%3/3
A and B
A= $no_more_verticesB= $graph->degree($vertex) > $neighbours
ABresult
0X0
100
111
133  
134 24 my @matching_neighbours;
135 24 my $matching_neighbours = set();
136 24 for my $i (0..$#rule) {
137 30 my $neighbour_rule = $rule[$i];
138 30 ▶ next if blessed $neighbour_rule && $neighbour_rule->isa( Graph::Grammar::Rule::Edge:: ); # Edge rules are evaluated separately
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($neighbour_rule) and $neighbour_rule->isa("Graph::Grammar::Rule::Edge") 12 18 30
Condition66%2/3
builtin::blessed($neighbour_rule) and $neighbour_rule->isa("Graph::Grammar::Rule::Edge")
!ll&&!rl&&r
18012
MC/DC50%1/2
builtin::blessed($neighbour_rule) and $neighbour_rule->isa("Graph::Grammar::Rule::Edge")
builtin::blessed($neighbour_rule) $neighbour_rule->isa("Graph::Grammar::Rule::Edge")
Truth table66%2/3
A and B
A= builtin::blessed($neighbour_rule)B= $neighbour_rule->isa("Graph::Grammar::Rule::Edge")
ABresult
0X0
100
111
139  
140 18 my $match;
141 18 ▶ if( $i && blessed $rule[$i-1] && $rule[$i-1]->isa( Graph::Grammar::Rule::Edge:: ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i and builtin::blessed($rule[$i - 1]) and $rule[$i - 1]->isa('Graph::Grammar::Rule::Edge')) { } 12 6 18
Condition66%4/6
$i and builtin::blessed($rule[$i - 1]) and $rule[$i - 1]->isa("Graph::Grammar::Rule::Edge")
!ll&&!rl&&r
6012
$i and builtin::blessed($rule[$i - 1])
!ll&&!rl&&r
6012
MC/DC33%1/3
$i and builtin::blessed($rule[$i - 1]) and $rule[$i - 1]->isa("Graph::Grammar::Rule::Edge")
$i builtin::blessed($rule[$i - 1]) $rule[$i - 1]->isa("Graph::Grammar::Rule::Edge")
Truth table50%2/4
A and B and C
A= $iB= builtin::blessed($rule[$i - 1])C= $rule[$i - 1]->isa("Graph::Grammar::Rule::Edge")
ABCresult
0XX0
10X0
1100
1111
142 # With edge condition
143 13 ▶ $match = first { !$matching_neighbours->has( $_ ) &&
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
not $matching_neighbours->has($_) and &$neighbour_rule($graph, $_) and $rule[$i - 1]->matches($graph, $vertex, $_) 13 0 13
Condition33%1/3
not $matching_neighbours->has($_) and &$neighbour_rule($graph, $_)
!ll&&!rl&&r
0013
MC/DC0%0/2
not $matching_neighbours->has($_) and &$neighbour_rule($graph, $_)
not $matching_neighbours->has($_) &$neighbour_rule($graph, $_)
Truth table33%1/3
not(A) and B
A= not $matching_neighbours->has($_)B= &$neighbour_rule($graph, $_)
ABresult
0X0
100
111
144 $neighbour_rule->( $graph, $_ ) &&
145 $rule[$i-1]->matches( $graph, $vertex, $_ ) }
146 12 $graph->neighbours( $vertex );
147 } else {
148 # Without edge condition
149 6 ▶ $match = first { !$matching_neighbours->has( $_ ) &&
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
not $matching_neighbours->has($_) and &$neighbour_rule($graph, $_) 6 0 6
150 $neighbour_rule->( $graph, $_ ) }
151 6 $graph->neighbours( $vertex );
152 }
153 18 ▶ next VERTEX unless $match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 9 9 18
154  
155 9 push @matching_neighbours, $match;
156 9 $matching_neighbours->insert( $match );
157 }
158  
159 15 ▶ if( $DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Graph::Grammar::DEBUG) 0 15 15
160 0 ▶ print STDERR defined $rule_name ? "apply rule $i: $rule_name\n" : "apply rule $i\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $rule_name ? : 0 0 0
161 }
162  
163 15 my $overlaps = ($affected_vertices * $matching_neighbours)->size +
164 $affected_vertices->has( $vertex );
165 15 ▶ if( $DEBUG && $overlaps ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Graph::Grammar::DEBUG and $overlaps) 0 15 15
Condition33%1/3
$Graph::Grammar::DEBUG and $overlaps
!ll&&!rl&&r
1500
MC/DC0%0/2
$Graph::Grammar::DEBUG and $overlaps
$Graph::Grammar::DEBUG $overlaps
Truth table33%1/3
A and B
A= $Graph::Grammar::DEBUGB= $overlaps
ABresult
0X0
100
111
166 0 print STDERR "$overlaps overlapping vertices\n";
167 }
168 15 $affected_vertices->insert( $vertex, @matching_neighbours );
169  
170 15 ▶ if( ref $action eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $action eq 'CODE') { } 15 0 15
171 15 $action->( $graph, $vertex, @matching_neighbours );
172 } else {
173 0 graph_replace( $graph, clone( $action ), $vertex );
174 }
175 15 $changes++;
176 }
177 }
178 }
179  
180 5 return $graph;
181 }
182  
183 =head2 C<EDGE>
184  
185 When used before a neighbour condition, places a condition on edge connecting the center vertex with a neighbour matched by the following rule.
186 Accepts a block or sub {}, i.e.:
187  
188 EDGE { $_[0]->get_edge_attribute( $_[1], $_[2], 'color' ) eq 'red' }
189  
190 Subroutine is evaluated with three parameters: graph, center vertex and its neighbour matching the following neighbour condition.
191 Subroutine should evaluate to true if condition is fulfilled.
192  
193 =cut
194  
195 1 ▶sub EDGE(&) { Graph::Grammar::Rule::Edge->new( $_[0] ) }
Subroutine EDGE: called CC=1 SCAR=0.0
Pod documented
196  
197 =head2 C<NO_MORE_VERTICES>
198  
199 When used before the rule action in a rule, restricts the number of center vertex neighbours to vertex conditions.
200  
201 =cut
202  
203 1 ▶sub NO_MORE_VERTICES { Graph::Grammar::Rule::NoMoreVertices->new }
Subroutine NO_MORE_VERTICES: called CC=1 SCAR=0.0
Pod documented
204  
205 =head1 AUTHORS
206  
207 Andrius Merkys, E<lt>merkys@cpan.orgE<gt>
208  
209 =cut
210  
211 2 1;
↑ Summary Grammar/Rule/Edge.pm »