| 1 | package FalkorDB::Path; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | sub new { | ||
| 7 | my ( $class, %args ) = @_; | ||
| 8 | return bless \%args, $class; | ||
| 9 | } | ||
| 10 | |||
| 11 | sub nodes { shift->{nodes} } | ||
| 12 | sub edges { shift->{edges} } | ||
| 13 | sub elements { shift->{elements} } | ||
| 14 | |||
| 15 | sub new_from_string { | ||
| 16 | my ( $class, $path_str ) = @_; | ||
| 17 | my @nodes; | ||
| 18 | my @edges; | ||
| 19 | my @elements; | ||
| 20 | |||
| 21 | if ( $path_str =~ /^\[(.*)\]$/ ) { | ||
| 22 | my $content = $1; | ||
| 23 | while ( $content =~ /(\(\d+\)|\[\d+\])/g ) { | ||
| 24 | my $token = $1; | ||
| 25 | if ( $token =~ /^\((\d+)\)$/ ) { | ||
| 26 | my $node_id = 0 + $1; | ||
| 27 | push @nodes, $node_id; | ||
| 28 | push @elements, { type => 'node', id => $node_id }; | ||
| 29 | } | ||
| 30 | elsif ( $token =~ /^\[(\d+)\]$/ ) { | ||
| 31 | my $edge_id = 0 + $1; | ||
| 32 | push @edges, $edge_id; | ||
| 33 | push @elements, { type => 'edge', id => $edge_id }; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | return $class->new( | ||
| 39 | nodes => \@nodes, | ||
| 40 | edges => \@edges, | ||
| 41 | elements => \@elements, | ||
| 42 | ); | ||
| 43 | } | ||
| 44 | |||
| 45 | 1; | ||
| 46 | __END__ |