Pattern.pm

filter: Statementstmt 93.0% 120 / 129 Branchbran 93.7% 45 / 48 Conditioncond 72.7% 8 / 11 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 84.0% 21 / 25 Podpod 72.2% 13 / 18 totaltotal 88.6% 210 / 237 CC 29 SCAR 19.4
CC
29
Cov
92%
related_to
16.0
node
7.0
_quote
5.0
CRAP 7.0
« Abstract.pm ↑ Summary
1 package Neo4j::Cypher::Pattern;
2 3 ▶use base Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use overload '""' => 'as_string';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.1002';
9 3 our @EXPORT_OK = qw/pattern ptn/;
10  
11 sub puke(@);
12 sub belch(@);
13  
14 51 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
15 51 my $class = shift;
16 51 my $self = {};
17 51 $self->{stmt}=[];
18 51 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 49 51
19 2 my $nq = join('|',@_);
20 2 $self->{no_quote} = qr/$nq/;
21 }
22 51 bless $self, $class;
23 }
24  
25 0 ▶sub pattern {
Subroutine pattern: not called CC=1 SCAR=6.9
Pod documented
26 0 Neo4j::Cypher::Pattern->new(@_);
27 }
28  
29 50 ▶sub ptn { Neo4j::Cypher::Pattern->new(@_); }
Subroutine ptn: called CC=1 SCAR=0.0
Pod documented
30  
31 2 ▶sub path {
Subroutine path: called CC=3 SCAR=12.5
Pod documented
32 2 my $self = shift;
33 2 ▶ puke("Need arg1 => identifier") if (!defined $_[0] || ref($_[0]));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or ref $_[0] 0 2 2
Condition33%1/3
not defined $_[0] or ref $_[0]
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $_[0] or ref $_[0]
not defined $_[0] ref $_[0]
Truth table33%1/3
not(A) or B
A= not defined $_[0]B= ref $_[0]
ABresult
1X1
011
000
34 2 return "$_[0] = $self";
35 }
36  
37 # alias for path
38 2 ▶sub as { shift->path(@_) }
Subroutine as: called CC=1 SCAR=0.0
Pod documented
39  
40 94 ▶sub node {
Subroutine node: called CC=7 SCAR=19.5
Pod documented
41 # args:
42 # scalar string = varname
43 # array ref - array of labels
44 # hash ref - hash of props/values
45 94 my $self = shift;
46 94 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 6 88 94
47 6 push @{$self->{stmt}}, '()';
48 6 return $self;
49 }
50 88 my ($varname) = grep { !ref } @_;
51 88 my ($lbls) = grep { ref eq 'ARRAY' } @_;
52 88 my ($props) = grep { ref eq 'HASH' } @_;
53 # look for labels
54 88 my @l;
55 88 ($varname, @l) = split /:/, $varname;
56 88 ▶ if (@l) {
Branch: 1 branch
BranchTrueFalseTotal
if (@l) 20 68 88
57 20 ▶ $lbls //= [];
Condition50%1/2
$lbls //= []
l!l
020
MC/DC0%0/1
$lbls //= []
$lbls
Truth table50%1/2
A
A= $lbls
Aresult
11
00
58 20 push @$lbls, @l;
59 }
60 88 ▶ my $str = $lbls ? join(':',$varname, @$lbls) : $varname;
Branch: 1 branch
BranchTrueFalseTotal
$lbls ? : 24 64 88
61 88 ▶ if ($props) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props) 28 60 88
62 28 my $p;
63 28 ▶ while (my($k,$v) = each %$props) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$props) 32 28 60
64 32 push @$p, "$k:".$self->_quote($v);
65 }
66 28 $p = join(',',@$p);
67 28 $str .= " {$p}";
68 }
69 88 push @{$self->{stmt}}, "($str)";
70 88 return $self;
71 }
72  
73 86 ▶sub N {shift->node(@_);}
Subroutine N: called CC=1 SCAR=0.0
Pod documented
74  
75 32 ▶sub related_to {
Subroutine related_to: called CC=16 SCAR=27.7
Pod documented
76 32 my $self = shift;
77 32 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 3 29 32
78 3 push @{$self->{stmt}}, '--';
79 3 return $self;
80 }
81 29 my ($hops) = grep { ref eq 'ARRAY' } @_;
82 29 my ($props) = grep { ref eq 'HASH' } @_;
83 29 my ($varname,$type) = grep { !ref } @_;
84 29 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) { } 3 26 29
85 3 ($varname) = split /:/,$varname;
86 } else {
87 26 ($varname, $type) = $varname =~ /^([^:]*):?(.*)/;
88 }
89 29 my $dir;
90 29 ▶ if ($varname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varname) 27 2 29
91 27 $varname =~ s/^(<|>)//;
92 27 $dir = $1;
93 27 $varname =~ s/(<|>)$//;
94 27 $dir = $1;
95 }
96 29 ▶ unless ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir) 15 14 29
97 15 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 11 4 15
98 11 $type =~ s/^(<|>)//;
99 11 $dir = $1;
100 11 $type =~ s/(<|>)$//;
101 11 $dir = $1;
102 }
103 }
104 29 ▶ my $str = $varname.($type ? ":$type" : "");
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 15 14 29
105  
106 29 ▶ if ($hops) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hops) 9 20 29
107 9 ▶ if (@$hops == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$hops == 0) { } 2 7 9
elsif (@$hops == 1) { } 0 7 7
108 2 $str.="*";
109 }
110 elsif (@$hops==1) {
111 0 $str .= "*$$hops[0]";
112 }
113 else {
114 7 $str .= "*$$hops[0]..$$hops[1]"
115 }
116 }
117 29 ▶ if ($props) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props) 3 26 29
118 3 my $p;
119 3 ▶ while (my($k,$v) = each %$props) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$props) 3 3 6
120 3 push @$p, "$k:".$self->_quote($v);
121 }
122 3 $p = join(',',@$p);
123 3 $str .= " {$p}";
124 }
125 29 ▶ $str = ($str ? "-[$str]-" : '--');
Branch: 1 branch
BranchTrueFalseTotal
$str ? : 25 4 29
126 29 $str =~ s/\[ \{/[{/;
127 29 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 22 7 29
128 22 ▶ if ($dir eq "<") {
Branch: 2 branches
BranchTrueFalseTotal
if ($dir eq '<') { } 7 15 22
elsif ($dir eq '>') { } 15 0 15
129 7 $str = "<$str";
130 }
131 elsif ($dir eq ">") {
132 15 $str = "$str>";
133 }
134 else {
135 0 1; # huh?
136 }
137 }
138 29 push @{$self->{stmt}}, $str;
139 29 return $self;
140 }
141  
142 29 ▶sub R {shift->related_to(@_)}
Subroutine R: called CC=1 SCAR=0.0
Pod documented
143  
144 # N('a')->toN('b') -> (a)-->(b)
145 # N('a')->fromN('b') -> (a)<--(b)
146 1 ▶sub _N {shift->related_to->node(@_)}
Subroutine _N: called CC=1 SCAR=0.0
147 1 ▶sub to_N {shift->related_to('>')->node(@_)}
Subroutine to_N: called CC=1 SCAR=0.0
Pod documented
148 1 ▶sub from_N {shift->related_to('<')->node(@_)}
Subroutine from_N: called CC=1 SCAR=0.0
Pod documented
149  
150 # 'class' method
151 # do pattern->C($pat1, $pat2)
152 5 ▶sub compound {
Subroutine compound: called CC=1 SCAR=0.0
Pod documented
153 5 my $self = shift;
154 5 return join(',',@_);
155 }
156  
157 5 ▶sub C {shift->compound(@_)}
Subroutine C: called CC=1 SCAR=0.0
Pod documented
158  
159 17 ▶sub clear { shift->{stmt}=[],1; }
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
160  
161 163 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod undocumented
162 163 my $self = shift;
163 163 return join('',@{$self->{stmt}});
164 }
165  
166 35 ▶sub _quote {
Subroutine _quote: called CC=5 SCAR=16.1
167 return $_[1] if (
168 35 ▶ ($_[0]->{no_quote} and $_[1] =~ $_[0]->{no_quote}) or
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"no_quote"} and $_[1] =~ /$_[0]{"no_quote"}/ or $_[1] =~ /(?:^|\s)\$/ 4 31 35
Condition100%6/6
$_[0]{"no_quote"} and $_[1] =~ /$_[0]{"no_quote"}/ or $_[1] =~ /(?:^|\s)\$/
l!l&&r!l&&!r
2231
$_[0]{"no_quote"} and $_[1] =~ /$_[0]{"no_quote"}/
!ll&&!rl&&r
3212
MC/DC100%3/3
$_[0]{"no_quote"} and $_[1] =~ /$_[0]{"no_quote"}/ or $_[1] =~ /(?:^|\s)\$/
$_[0]{"no_quote"} $_[1] =~ /$_[0]{"no_quote"}/ $_[1] =~ /(?:^|\s)\$/
Truth table80%4/5
A and B or C
A= $_[0]{"no_quote"}B= $_[1] =~ /$_[0]{"no_quote"}/C= $_[1] =~ /(?:^|\s)\$/
ABCresult
11X1
0X11
1011
0X00
1000
169 $_[1] =~ /(?:^|\s)\$/ # no quote parameters
170 );
171 31 ▶ return ${$_[1]} if (ref $_[1] eq 'SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "SCALAR" 1 30 31
172 # escape single quotes
173 30 my $v = $_[1];
174 30 $v =~ s/'/\\'/g;
175 30 return "'$v'";
176 }
177 0 ▶sub pop { pop @{shift->{stmt}}; }
Subroutine pop: not called CC=1 SCAR=6.9
Pod undocumented
178  
179 0 ▶sub belch (@) {
Subroutine belch: not called CC=1 SCAR=6.9
Pod undocumented
180 0 my($func) = (caller(1))[3];
181 0 Carp::carp "[$func] Warning: ", @_;
182 }
183  
184 0 ▶sub puke (@) {
Subroutine puke: not called CC=1 SCAR=6.9
Pod undocumented
185 0 my($func) = (caller(1))[3];
186 0 Carp::croak "[$func] Fatal: ", @_;
187 }
188  
189 =head1 NAME
190  
191 Neo4j::Cypher::Pattern - Generate Cypher pattern strings
192  
193 =head1 SYNOPSIS
194  
195 # express a cypher pattern
196 use Neo4j::Cypher::Pattern qw/ptn/;
197  
198 ptn->node();
199 ptn->N(); #alias
200 ptn->N("varname");
201 ptn->N("varname",["label"],{prop => "value"});
202 ptn->N("varname:label");
203 ptn->N(["label"],{prop => "value"});
204  
205 ptn->node('a')->related_to()->node('b'); # (a)--(b)
206 ptn->N('a')->R()->N('b'); # alias
207 # additional forms
208 ptn->N('a')->R("varname","typename",[$minhops,$maxhops],{prop => "value"})
209 ->N('b'); # (a)-[varname:typename*minhops..maxhops { prop:"value }]-(b)
210 ptn->N('a')->R("varname:typename")->N('b'); # (a)-[varname:typename]-(b)
211 ptn->N('a')->R(":typename")->N('b'); # (a)-[:typename]-(b)
212 ptn->N('a')->R("", "typename")->N('b'); # (a)-[:typename]-(b)
213 # directed relns
214 ptn->N('a')->R("<:typename")->N('b'); # (a)<-[:typename]-(b)
215 ptn->N('a')->R("varname:typename>")->N('b'); # (a)-[varname:typename]->(b)
216  
217 # these return strings
218 $pattern->path('varname'); # path variable assigned to a pattern
219 $pattern->as('varname'); # alias
220 ptn->compound($pattern1, $pattern2); # comma separated patterns
221 ptn->C($pattern1, $pattern2); # alias
222  
223 =head1 DESCRIPTION
224  
225 The L<Cypher|https://neo4j.com/docs/developer-manual/current/cypher/>
226 query language of the graph database L<Neo4j|https://neo4j.com> uses
227 L<patterns|https://neo4j.com/docs/developer-manual/current/cypher/syntax/patterns>
228 to represent graph nodes and their relationships, for selecting and
229 matching in queries. C<Neo4j::Cypher::Pattern> can be used to create
230 Cypher pattern productions in Perl in an intuitive way. It is part of
231 the L<Neo4j::Cypher::Abstract> distribution.
232  
233 =head2 Basic idea : produce patterns by chaining method calls
234  
235 C<Neo4j::Cypher::Pattern> objects possess methods to represent nodes
236 and relationships. Each method adds its portion of the pattern, with
237 arguments, to the object's internal queue. Every method returns the
238 object itself. When an object is rendered as a string, it concatenates
239 nodes and relationship productions to yield the entire query statement
240 as a string.
241  
242 These features add up to the following idiom. Suppose we want to
243 render the Cypher pattern
244  
245 (b {name:"Slate"})<-[:WORKS_FOR]-(a {name:"Fred"})-[:KNOWS]->(c {name:"Barney"})
246  
247 In C<Neo4j::Cypher::Pattern>, we do
248  
249 $p = Neo4j::Cypher::Pattern->new()->N('b',{name=>'Slate'})
250 ->R('<:WORKS_FOR')->N('a',{name => 'Fred'})
251 ->R(':KNOWS>')->N('c',{name=>'Barney'});
252 print "$p\n"; # "" is overloaded by $p->as_string()
253  
254 Because you may create many patterns in a program, a short
255 alias for the constructor can be imported, and extra variable
256 assignments can be avoided.
257  
258 print ptn->N('b',{name=>'Slate'})
259 ->R('<:WORKS_FOR')->N('a',{name => 'Fred'})
260 ->R(':KNOWS>')->N('c',{name=>'Barney'}), "\n";
261  
262 =head2 Quoting
263  
264 In pattern productions, values for properties will be quoted by
265 default with single quotes (single quotes that are present will be
266 escaped) unless the values are numeric.
267  
268 To prevent quoting Cypher statement list variable names (for example),
269 make the name an argument to the pattern I<constructor>:
270  
271 ptn('event')->N('y')->R("<:IN")->N('e:Event'=> { id => 'event.id' });
272  
273 # renders (y)<-[:IN]-(e:Event {id:event.id})
274 # rather than (y)<-[:IN]-(e:Event {id:"event.id"})
275  
276 =head1 METHODS
277  
278 =over
279  
280 =item Constructor new()
281  
282 =item pattern(), ptn()
283  
284 Exportable aliases for the constructor. Arguments are variable names
285 that should not be quoted in rendering values of properties.
286  
287 =item node(), N()
288  
289 Render a node. Arguments in any order:
290  
291 scalar string: variable name or variable:label
292 array ref: array of node labels
293 hash ref: hash of property => value
294  
295 =item related_to(), R()
296  
297 Render a relationship. Arguments in any order:
298  
299 scalar string: variable name or variable:type
300 array ref: variable-length pattern:
301 [$minhops, $maxhops]
302 [] (empty array)- any number of hops
303 [$hops] - exactly $hops
304 hash ref : hash of property => value
305  
306 =item path(), as()
307  
308 Render the pattern set equal to a path variable:
309  
310 $p = ptn->N('a')->_N('b');
311 print $p->as('pth'); # gives 'pth = (a)--(b)'
312  
313 =item compound(), C()
314  
315 Render multiple patterns separated by commas
316  
317 ptn->compound( ptn->N('a')->to_N('b'), ptn->N('a')->from_N('c'));
318 # (a)-->(b), (a)<--(c)
319  
320 =item Shortcuts _N, to_N, from_N
321  
322 ptn->N('a')->_N('b'); # (a)--(b)
323 ptn->N('a')->to_N('b'); # (a)-->(b)
324 pth->N('a')->from_N('b'); # (a)<--(b)
325  
326 =back
327  
328 =head1 SEE ALSO
329  
330 L<Neo4j::Cypher::Abstract>
331  
332 =head1 AUTHOR
333  
334 Mark A. Jensen
335 CPAN: MAJENSEN
336 majensen -at- cpan -dot- org
337  
338 =head1 LICENSE
339  
340 This software is provided for use under the terms of Perl itself.
341  
342 =head1 COPYRIGHT
343  
344 (c) 2017 Mark A. Jensen
345  
346 =cut
347 3 1;
« Abstract.pm ↑ Summary