Class: Dither::Graph

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dither/graph.rb

Defined Under Namespace

Classes: Edge, Vertex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



11
12
13
14
15
16
17
18
19
# File 'lib/dither/graph.rb', line 11

def initialize
  @vertices = {}
  @edges = []
  @stop_conditions = []
  @skip_conditions = []
  @rand = Random.new
  @max_depth = 10
      @origin_name = :origin
end

Instance Attribute Details

#edgesObject (readonly)

Returns the value of attribute edges.



7
8
9
# File 'lib/dither/graph.rb', line 7

def edges
  @edges
end

#max_depthObject

Returns the value of attribute max_depth.



6
7
8
# File 'lib/dither/graph.rb', line 6

def max_depth
  @max_depth
end

#origin_nameObject

Returns the value of attribute origin_name.



6
7
8
# File 'lib/dither/graph.rb', line 6

def origin_name
  @origin_name
end

#randObject

Returns the value of attribute rand.



6
7
8
# File 'lib/dither/graph.rb', line 6

def rand
  @rand
end

#skip_conditionsObject (readonly)

Returns the value of attribute skip_conditions.



7
8
9
# File 'lib/dither/graph.rb', line 7

def skip_conditions
  @skip_conditions
end

#stop_conditionsObject (readonly)

Returns the value of attribute stop_conditions.



7
8
9
# File 'lib/dither/graph.rb', line 7

def stop_conditions
  @stop_conditions
end

#verticesObject (readonly)

Returns the value of attribute vertices.



7
8
9
# File 'lib/dither/graph.rb', line 7

def vertices
  @vertices
end

Class Method Details

.create(hash) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/dither/graph.rb', line 21

def self.create(hash)
  edges = hash[:edges]
  graph = Graph.new
      graph.origin_name = hash[:origin] if hash.has_key?(:origin)

  edges.each do |edge|
    graph.add_edge(edge[:name], edge[:src_vertex], edge[:dst_vertex])
  end
  graph
end

Instance Method Details

#add_edge(name, src_vertex_name, dst_vertex_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dither/graph.rb', line 32

def add_edge(name, src_vertex_name, dst_vertex_name)
  # check & create vertexes if they do not exist
  vertices[src_vertex_name] ||= Vertex.new(src_vertex_name, [], 0)
  vertices[dst_vertex_name] ||= Vertex.new(dst_vertex_name, [], 0)

  # add edge to src_vertex
  src_vertex = vertices[src_vertex_name]
  dst_vertex = vertices[dst_vertex_name]
  edge = Edge.new(name, src_vertex, dst_vertex, 0)
  src_vertex.edges << edge
  edges << edge

  self
end

#add_skip_condition(cond) ⇒ Object

Raises:



47
48
49
50
# File 'lib/dither/graph.rb', line 47

def add_skip_condition(cond)
  raise Dither::Error, "#{cond} does not respond to skip?" unless cond.respond_to? :skip?
  skip_conditions << cond
end

#add_stop_condition(cond) ⇒ Object

Raises:



52
53
54
55
# File 'lib/dither/graph.rb', line 52

def add_stop_condition(cond)
  raise Dither::Error, "#{cond} does not respond to stop?" unless cond.respond_to? :stop?
  stop_conditions << cond
end

#eachObject



74
75
76
77
78
79
80
81
82
# File 'lib/dither/graph.rb', line 74

def each
  loop do
    break if stop?
    path = []
    random_walk { |a| path << a }
    next if skip?
    yield path
  end
end

#edges_coveredObject



97
98
99
100
# File 'lib/dither/graph.rb', line 97

def edges_covered
  b = edges.count { |a| a.count > 0 }
  b/edges.length.to_f
end

#originObject



57
58
59
# File 'lib/dither/graph.rb', line 57

def origin
  vertices[origin_name]
end

#random_walkObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dither/graph.rb', line 61

def random_walk
  current_edge = origin.edges.sample(:random => rand)
  current_max_depth = max_depth - 1
  loop do
    current_edge.src_vertex.count += 1
    current_edge.count += 1
    yield current_edge
    current_edge = current_edge.dst_vertex.edges.sample(:random => rand)
    current_max_depth -= 1
    break if current_max_depth < 0 || current_edge.src_vertex.name == origin_name
  end
end

#skip?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/dither/graph.rb', line 88

def skip?
  skip_conditions.any? { |cond| cond.skip?(self) }
end

#stop?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/dither/graph.rb', line 84

def stop?
  stop_conditions.any? { |cond| cond.stop?(self) }
end

#vertices_coveredObject



92
93
94
95
# File 'lib/dither/graph.rb', line 92

def vertices_covered
  b = vertices.count { |a| a.count > 0 }
  b/vertices.length.to_f
end