Graph::Grammar(3pm)
π Graph::Grammar π
okay bestie, this is Graph::Grammar from the User Contributed Perl Documentation corner of your local perl install β and it's giving graph rewriting realness. let's get into it π¦
β¨ NAME β¨
Graph::Grammar β it's a graph grammar, aka a rewriting method. yes that's the whole name. iconic behavior honestly π
πΈ SYNOPSIS πΈ
use Graph::Grammar;
use Graph::Undirected;
my $graph = Graph::Undirected->new;
# Create graph here
my @rules = (
[ sub { 1 }, ( sub { 1 } ) x 2, NO_MORE_VERTICES, sub { [ @_[1..3] ] } ],
);
parse_graph( $graph, @rules );
like, that's it. that's the whole vibe. you build a graph, you hand it some rules, you call parse_graph, and the graph gets rewritten. slay π
π DESCRIPTION π
so Graph::Grammar is a Perl implementation of a graph rewriting method (a.k.a. a graph grammar, iykyk). much of the API draws inspiration from Parse::Yapp β but here's the plot twist: instead of acting on text streams, Graph::Grammar is oriented at graphs, as implemented in Perl's Graph module. π§
it implements a single method, parse_graph(), which accepts an instance of Graph and an array of rules. every rule gets evaluated for each vertex in the graph, and if a match is found, an action associated with the rule is executed. a rule generally looks like this:
[ $vertex_condition, @neighbour_conditions, $action ]
where, bestie:
$vertex_conditionis a subroutine reference evaluating the center vertex. the subroutine is called with the graph in$_[0]and the vertex in$_[1]. the subroutine should evaluate to true if the condition is fulfilled. β@neighbour_conditionsis an array of subroutine references for the neighbours of the center vertex. inputs and outputs of each subroutine reference are the same as defined for$vertex_condition. every condition has to match at least one of the neighbours β and no overlaps allowed, we're not sharing. π ββοΈ so the rule automatically fails if the number of neighbours is less than@neighbour_conditions. there can be more neighbours than@neighbour_conditions, but if you need a strict number of neighbours, scroll down and look forNO_MORE_VERTICES(spoiler: it's right there in METHODS). also@neighbour_conditionscan be empty. lowkey chill of it.$actioncan be either a subroutine reference, or literally anything else. if$actionis a subroutine reference, then in the case of a match it is called with the graph in$_[0]and the remaining@_members being graph vertices corresponding to rule conditions. that is,$_[1]is the center vertex,$_[2]is a vertex matching the first neighbour condition, and so on. π«Ά if$actionis not a subroutine reference, then it gets cloned byCloneand inserted instead of the center vertex. boom, new vertex, no cap.
there are two ways to request a particular number of neighbours for the central vertex:
- include an appropriate requirement inside
$vertex_condition, or - put
NO_MORE_VERTICESas the last element of@neighbour_conditions, i.e.:
[ sub { 1 }, ( sub { 1 } ) x 2, NO_MORE_VERTICES, sub { [ @_[1..3] ] } ]
edge conditions are also supported π and they always act on the center vertex and its neighbours matching their individual conditions, i.e.:
[ $vertex_condition,
EDGE { $edge_condition1->( @_ ) }, $vertex_condition1,
EDGE { $edge_condition2->( @_ ) }, $vertex_condition2,
# ...
$action ]
β METHODS β
π₯ parse_graph( $graph, @rules )
perform graph rewriting of $graph. it modifies the supplied graph and returns it upon completion. so yeah, it's not a copy situation, it's an in-place glow-up. main character energy honestly π
π EDGE
when used before a neighbour condition, this places a condition on the edge connecting the center vertex with a neighbour matched by the following rule. it accepts a block or sub {}, i.e.:
EDGE { $_[0]->get_edge_attribute( $_[1], $_[2], 'color' ) eq 'red' }
the subroutine is evaluated with three parameters: the graph, the center vertex, and its neighbour matching the following neighbour condition. the subroutine should evaluate to true if the condition is fulfilled. β¨
π NO_MORE_VERTICES
when used before the rule action in a rule, it restricts the number of center vertex neighbours to the vertex conditions. that's it. that's the whole job. and it does it flawlessly. π
π AUTHORS π
Andrius Merkys, <merkys@cpan.org> β thank you for your service, king π«‘π
okay that's the whole page bestie!! no cap, every option and every detail is right here where you left it. this was Graph::Grammar, documented as perl v5.40.0, dated 2024-09-26 β so go forth and rewrite some graphs, you absolute legend ππ¦π
π¦ where this page comes from
- ubuntu resolute: libgraph-grammar-perl 0.2.0-1 Β· BSD-3-Clause
- ubuntu stonking: libgraph-grammar-perl 0.2.0-1 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms