Class: Tangle::Graph
- Inherits:
-
Object
- Object
- Tangle::Graph
- Includes:
- Mixin::Initialize
- Defined in:
- lib/tangle/graph.rb
Overview
Base class for all kinds of graphs
Direct Known Subclasses
Constant Summary collapse
Instance Attribute Summary collapse
-
#mixins ⇒ Object
readonly
Returns the value of attribute mixins.
Instance Method Summary collapse
-
#add_edge(*vertices, **kvargs) ⇒ Object
Add a new edge to the graph.
-
#add_vertex(**kvargs) ⇒ Object
Add a new vertex to the graph.
-
#edges(&selector) ⇒ Object
Get all edges.
- #get_vertex(name_or_vertex) ⇒ Object
-
#initialize(vertices: nil, edges: nil, mixins: [Tangle::Mixin::Connectedness]) ⇒ Graph
constructor
Initialize a new graph, optionally preloading it with vertices and edges.
-
#subgraph(&selector) ⇒ Object
(also: #dup)
Return a subgraph, optionally filtered by a vertex selector block.
-
#vertices ⇒ Object
Get all vertices.
Constructor Details
#initialize(vertices: nil, edges: nil, mixins: [Tangle::Mixin::Connectedness]) ⇒ Graph
Initialize a new graph, optionally preloading it with vertices and edges
Graph.new() => Graph Graph.new(vertices: array_or_hash) => Graph Graph.new(vertices: array_or_hash, edges: array_or_hash) => Graph Graph.new(mixins: [MixinModule, …], …) => Graph
When array_or_hash is a hash, it contains the objects as values and their names as keys. When array_or_hash is an array the objects will get assigned unique names (within the graph).
vertices can contain anything, and the Vertex object that is created will delegate all missing methods to its content.
edges can contain an array of exactly two, either names of vertices or vertices.
mixins is an array of modules that can be mixed into the various classes that makes up a graph. Initialization of a Graph, Vertex or Edge looks for submodules in each mixin, with the same name and extends any created object. Defaults to [Tangle::Mixin::Connectedness].
Any subclass of Graph should also subclass Edge to manage its unique constraints.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tangle/graph.rb', line 38 def initialize(vertices: nil, edges: nil, mixins: [Tangle::Mixin::Connectedness]) @vertices_by_id = {} @vertices_by_name = {} @edges ||= [] initialize_mixins(mixins) initialize_vertices(vertices) initialize_edges(edges) end |
Instance Attribute Details
#mixins ⇒ Object (readonly)
Returns the value of attribute mixins.
121 122 123 |
# File 'lib/tangle/graph.rb', line 121 def mixins @mixins end |
Instance Method Details
#add_edge(*vertices, **kvargs) ⇒ Object
Add a new edge to the graph
add_edge(vtx1, vtx2, …) => Edge
65 66 67 68 |
# File 'lib/tangle/graph.rb', line 65 def add_edge(*vertices, **kvargs) vertices = vertices.map { |v| get_vertex(v) } insert_edge(self.class::Edge.new(*vertices, graph: self, **kvargs)) end |
#add_vertex(**kvargs) ⇒ Object
Add a new vertex to the graph
add_vertex(…) => Vertex
Optional named arguments:
name: unique name or label for vertex
contents: delegate object for missing methods
90 91 92 |
# File 'lib/tangle/graph.rb', line 90 def add_vertex(**kvargs) insert_vertex(Vertex.new(graph: self, **kvargs)) end |
#edges(&selector) ⇒ Object
Get all edges.
edges => Array
53 54 55 56 57 58 59 |
# File 'lib/tangle/graph.rb', line 53 def edges(&selector) if block_given? @edges.select(&selector) else @edges.to_a end end |
#get_vertex(name_or_vertex) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/tangle/graph.rb', line 94 def get_vertex(name_or_vertex) case name_or_vertex when Vertex name_or_vertex else @vertices_by_name[name_or_vertex] || @vertices_by_id.fetch(name_or_vertex) end end |
#subgraph(&selector) ⇒ Object Also known as: dup
Return a subgraph, optionally filtered by a vertex selector block
subgraph => Graph subgraph { |vertex| … } => Graph
Unless a selector is provided, the subgraph contains the entire graph.
111 112 113 114 115 116 117 118 |
# File 'lib/tangle/graph.rb', line 111 def subgraph(&selector) graph = self.class.new dup_vertices_into(graph, &selector) dup_edges_into(graph) graph end |
#vertices ⇒ Object
Get all vertices.
vertices => Array
74 75 76 77 78 79 80 |
# File 'lib/tangle/graph.rb', line 74 def vertices if block_given? @vertices_by_id.select { |_, vertex| yield(vertex) } else @vertices_by_id end.values end |