DBIx::Class::Graph
version 1.05
package MySchema::Graph; use base 'DBIx::Class'; __PACKAGE__->load_components("Graph", "Core"); __PACKAGE__->table("tree"); __PACKAGE__->add_columns("id", "name", "parent_id"); __PACKAGE__->connect_graph(predecessor => "parent_id"); my @children = $rs->get_vertex($id)->successors; my @vertices = $rs->vertices; # do other cool stuff like calculating distances etc.
This module allows to create and interact with a directed graph. It will take care of storing the information in a relational database. It uses Graph for calculations. This module extends the DBIx::Class::ResultSet. Some methods are added to the resultset, some to the row objects.
DBIx::Class::Graph - Represent a graph in a relational database using DBIC
__PACKAGE__->load_components(qw(Graph Core));
To use this module it has to loaded via load_components in the result class.
load_components
XXX
__PACKAGE__->connect_graph( predecessor => 'parent_id' ); __PACKAGE__->connect_graph( successor => 'child_id' ); __PACKAGE__->connect_graph( predecessor => { parents => 'parent_id' } ); __PACKAGE__->connect_graph( successor => { childs => 'child_id' } );
The first argument defines how the tree is build. You can either specify predecessor or successor.
predecessor
successor
The name of the relation to the next vertex is defined by the second argument.
finds a vertex by searching the underlying resultset for $id in the primary key column (only single primary keys are supported). It's not as smart as the original "find" in DBIx::Class::ResultSet because it looks on the primary key(s) for $id only.
$id
The following methods are imported from Graph:
delete_vertex connected_component_by_vertex biconnected_component_by_vertex weakly_connected_component_by_vertex strongly_connected_component_by_vertex is_sink_vertex is_source_vertex is_successorless_vertex is_successorful_vertex is_predecessorless_vertex is_predecessorful_vertex is_isolated_vertex is_interior is_exterior is_self_loop_vertex successors neighbours predecessors degree in_degree out_degree edges_at edges_from edges_to get_vertex_count random_successor random_predecessor vertices_at
Simply sort the resultset
$rs->search(undef, {order_by => "title ASC"})->graph;
Multipgraphs are not supported. This means you there can only be one edge per vertex pair and direction.
you should consider caching the Graph object if you are working with large number of vertices.
DBIx::Class::Tree, DBIx::Class::NestedSet
See "CAVEATS"
Moritz Onken, <onken@houseofdesign.de>
I am also avaiable on the DBIx::Class mailinglist
Copyright (C) 2008 by Moritz Onken
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
Moritz Onken <onken@netcubed.de>
This software is Copyright (c) 2011 by Moritz Onken.
This is free software, licensed under:
The (three-clause) BSD License
To install DBIx::Class::Graph, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Class::Graph
CPAN shell
perl -MCPAN -e shell install DBIx::Class::Graph
For more information on module installation, please visit the detailed CPAN module installation guide.