DagLinkCalculator
For a DAG (Direct Acyclic Graph, like the one used with acts-as-dag), it calculates the list of all links (direct and indirect) based on the list of direct links (parent-child).
Example:
We have a DAG like this:
A --- B --- C --- D --- H
| |
-- F --- E --
we receive the direct links info that describe that DAG like this:
[
{ ancestor_id: 'A', descendant_id: 'B' },
{ ancestor_id: 'A', descendant_id: 'F' },
{ ancestor_id: 'B', descendant_id: 'C' },
{ ancestor_id: 'C', descendant_id: 'D' },
{ ancestor_id: 'F', descendant_id: 'E' },
{ ancestor_id: 'E', descendant_id: 'D' },
{ ancestor_id: 'D', descendant_id: 'H' },
]
we'll produce an intermediate structures (routes) like this:
[
<struct DagLinkCalculator::NodeRoute nodes=['H', 'D']>
<struct DagLinkCalculator::NodeRoute nodes=['H', 'D', 'C']>
<struct DagLinkCalculator::NodeRoute nodes=['H', 'D', 'C', 'B']>
<struct DagLinkCalculator::NodeRoute nodes=['H', 'D', 'C', 'B', 'A']>
<struct DagLinkCalculator::NodeRoute nodes=['H', 'D', 'E']>
<struct DagLinkCalculator::NodeRoute nodes=['H', 'D', 'E', 'F']>
<struct DagLinkCalculator::NodeRoute nodes=['H', 'D', 'E', 'F', 'A']>
<struct DagLinkCalculator::NodeRoute nodes=['D', 'C']>
<struct DagLinkCalculator::NodeRoute nodes=['D', 'C', 'B']>
<struct DagLinkCalculator::NodeRoute nodes=['D', 'C', 'B', 'A']>
<struct DagLinkCalculator::NodeRoute nodes=['D', 'E']>
<struct DagLinkCalculator::NodeRoute nodes=['D', 'E', 'F']>
<struct DagLinkCalculator::NodeRoute nodes=['D', 'E', 'F', 'A']>
<struct DagLinkCalculator::NodeRoute nodes=['C', 'B']>
<struct DagLinkCalculator::NodeRoute nodes=['C', 'B', 'A']>
<struct DagLinkCalculator::NodeRoute nodes=['B', 'A']>
<struct DagLinkCalculator::NodeRoute nodes=['E', 'F']>
<struct DagLinkCalculator::NodeRoute nodes=['E', 'F', 'A']>
<struct DagLinkCalculator::NodeRoute nodes=['F', 'A']>
]
and then return a list of links like this:
[
{ ancestor_id: 'A', descendant_id: 'B', direct: true, count: 1 },
{ ancestor_id: 'A', descendant_id: 'C', direct: false, count: 1 },
{ ancestor_id: 'A', descendant_id: 'D', direct: false, count: 2 },
{ ancestor_id: 'A', descendant_id: 'E', direct: false, count: 1 },
{ ancestor_id: 'A', descendant_id: 'F', direct: true, count: 1 },
{ ancestor_id: 'A', descendant_id: 'H', direct: false, count: 2 },
{ ancestor_id: 'B', descendant_id: 'C', direct: true, count: 1 },
{ ancestor_id: 'B', descendant_id: 'D', direct: false, count: 1 },
{ ancestor_id: 'B', descendant_id: 'H', direct: false, count: 1 },
{ ancestor_id: 'C', descendant_id: 'D', direct: true, count: 1 },
{ ancestor_id: 'C', descendant_id: 'H', direct: false, count: 1 },
{ ancestor_id: 'D', descendant_id: 'H', direct: true, count: 1 },
{ ancestor_id: 'E', descendant_id: 'D', direct: true, count: 1 },
{ ancestor_id: 'E', descendant_id: 'H', direct: false, count: 1 },
{ ancestor_id: 'F', descendant_id: 'D', direct: false, count: 1 },
{ ancestor_id: 'F', descendant_id: 'E', direct: true, count: 1 },
{ ancestor_id: 'F', descendant_id: 'H', direct: false, count: 1 },
]
Usage
Given a list of objects defining the direct links:
direct_link_hashes = [
{ ancestor_id: 'A', descendant_id: 'B' },
{ ancestor: 'A', descendant_id: 'F' },
{ ancestor_id: 'B', descendant: 'C' },
{ parent_id: 'C', descendant_id: 'D' },
{ ancestor_id: 'F', child_id: 'E' },
{ parent_id: 'E', child_id: 'D' },
{ parent: 'D', child: 'H' },
]
calculator = DagLinkCalculator.from_direct_links_hashes(direct_link_hashes)
calculator.all_links_hashes
calculator.all_links_structs
calculator.all_links_structs
Cycle Detection
When generating the routes, if a cycle is detected (a parent node that is also a descendant node), a CycleException
will be raised:
e.g.
'nodes "H" and "F" are ancestor and descendant of each other: cycle detected!'
Installation
Add this line to your application's Gemfile:
gem 'dag_link_calculator'
And then execute:
$ bundle
Or install it yourself as:
$ gem install dag_link_calculator
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/artirix/dag_link_calculator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.