Class: ValueGraphTransformation::Graph::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/value_graph_transformation/graph/graph.rb

Overview

A directed Graph

Direct Known Subclasses

Context

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Graph) initialize

Returns a new instance of Graph



13
14
15
16
# File 'lib/value_graph_transformation/graph/graph.rb', line 13

def initialize
  @vertices = []
  @edges = []
end

Instance Attribute Details

- (Array<Edge>) edges (readonly)

Returns:



11
12
13
# File 'lib/value_graph_transformation/graph/graph.rb', line 11

def edges
  @edges
end

- (Array<Vertex>) vertices (readonly)

Returns:



8
9
10
# File 'lib/value_graph_transformation/graph/graph.rb', line 8

def vertices
  @vertices
end

Instance Method Details

- (Vertex) add(vertex = Vertex.new)

Adds a vertex to the graph.

Parameters:

  • vertex (Vertex) (defaults to: Vertex.new)

    the vertex to add

Returns:

  • (Vertex)

    the newly created Vertex.

Specifications:

should return a vertex

should add the vertex to its list of vertices



21
22
23
24
# File 'lib/value_graph_transformation/graph/graph.rb', line 21

def add(vertex=Vertex.new)
  @vertices << vertex
  vertex
end

- (Edge) connect(source, target)

Creates an edge between two vertices.

Parameters:

  • source (Vertex)

    the source vertex

  • target (Vertex)

    the target vertex

Returns:

  • (Edge)

    the newly created Edge between the two vertices.

Specifications:

should return an edge

should add the edge to its list of edges

should add the edge on the source list of the target vertex

should add the edge on the target list of the source vertex

should fail if source not part of the graph

should fail if target not part of the graph



31
32
33
34
35
36
37
38
39
40
# File 'lib/value_graph_transformation/graph/graph.rb', line 31

def connect(source, target)
  fail "'#{source.to_s}' not part of the graph" unless vertices.include?(source)
  fail "'#{target.to_s}' not part of the graph" unless vertices.include?(target)

  edge = Edge.new(source, target)
  source.targets << edge
  target.sources << edge
  @edges << edge
  edge
end

- (Object) delete(vertex)

Removes the vertex from the graph and disconnects its edges.

Parameters:

  • vertex (Vertex)

    the vertex to remove.

Specifications:

should remove the vertex from the graph



44
45
46
47
# File 'lib/value_graph_transformation/graph/graph.rb', line 44

def delete(vertex)
  fail "'#{vertex.to_s}' not part of the graph" unless @vertices.delete(vertex)
  (vertex.targets + vertex.sources).each{|edge| disconnect(edge)}
end

- (Object) disconnect(edge)

Disconnects and removes the edge from the graph.

Parameters:

  • edge (Edge)

    the edge to remove.

Specifications:

should remove the edge from the graph

should remove the edge from the source list of the target vertex

should remove the edge from the target list of the source vertex



51
52
53
54
55
56
57
# File 'lib/value_graph_transformation/graph/graph.rb', line 51

def disconnect(edge)
  fail "'#{edge.to_s}' not part of the graph" unless @edges.delete(edge)
  edge.source.targets.delete(edge)
  edge.source = nil
  edge.target.sources.delete(edge)
  edge.target = nil
end

- (String) to_dot

Returns the graph in dot code.

Returns:

  • (String)

    the graph in dot code.

Specifications:

should return a String in valid dot format

digraph {
	0;
	1;
	2;
	0->1;
	0->1;
	2->0;
	0->2;
}


60
61
62
63
64
65
66
# File 'lib/value_graph_transformation/graph/graph.rb', line 60

def to_dot
  ids = {}
  @vertices.each_with_index{|vertex, index| ids[vertex] = "#{index}" }
  nodes = ids.values.collect{|id| "\t#{id};"}
  edges = @edges.collect{|edge| "\t#{ids[edge.source]}->#{ids[edge.target]};" }
  "digraph {\n#{(nodes+edges).join("\n")}\n}"
end