Class: EnumMachineContrib::DecisionGraph

Inherits:
Object
  • Object
show all
Defined in:
lib/enum_machine_contrib/decision_graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph) ⇒ DecisionGraph

Returns a new instance of DecisionGraph.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/enum_machine_contrib/decision_graph.rb', line 8

def initialize(graph)
  @vertexes = Set.new
  @edges    = Set.new

  vertex_by_value = {}
  graph.each do |from_value, to_value_list|
    from_value = array_wrap(from_value)
    vertex_by_value[from_value] ||= Vertex[from_value]
    from_vertex = vertex_by_value[from_value]
    @vertexes << from_vertex

    to_value_list.sort_by(&:to_s).each do |to_value|
      to_value = array_wrap(to_value)
      vertex_by_value[to_value] ||= Vertex[to_value]
      to_vertex = vertex_by_value[to_value]
      @vertexes << to_vertex

      @edges << from_vertex.edge_to(to_vertex)
    end
  end
end

Instance Attribute Details

#edgesObject

Returns the value of attribute edges.



6
7
8
# File 'lib/enum_machine_contrib/decision_graph.rb', line 6

def edges
  @edges
end

#vertexesObject

Returns the value of attribute vertexes.



6
7
8
# File 'lib/enum_machine_contrib/decision_graph.rb', line 6

def vertexes
  @vertexes
end

Instance Method Details

#decision_treeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/enum_machine_contrib/decision_graph.rb', line 30

def decision_tree
  drop_self_cycled_edges!
  combine_equal_vertexes!

  loop do
    complexity_was = complexity

    decision_tree = DecisionTree.wrap(to_h)
    decision_tree.resolve!
    resolved_decision_tree = decision_tree.resolved

    edges.each do |edge|
      next unless resolved_decision_tree.key?(edge.from.value)

      if resolved_decision_tree[edge.from.value].any? { |to_value_list| to_value_list.include?(edge.to.value) }
        edge.resolved!
      end
    end

    strongly_connected_components = decision_tree.values.filter(&:cycled?)
    strongly_connected_components.each do |strongly_connected_component|
      resolve_strong_component!(strongly_connected_component)
    end

    break if complexity == complexity_was
  end

  decision_tree = DecisionTree[vertexes.index_by(&:value)]
  decision_tree.resolve!
  decision_tree
end

#next_achievable_vertexes(vertex, all, visited: Set.new) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/enum_machine_contrib/decision_graph.rb', line 183

def next_achievable_vertexes(vertex, all, visited: Set.new)
  return [] if visited.include?(vertex)

  achievable_vertexes = [vertex]
  visited << vertex

  vertex.outcoming_edges.each do |edge|
    if all.include?(edge.to)
      achievable_vertexes += next_achievable_vertexes(edge.to, all, visited: visited)
    end
  end

  achievable_vertexes
end

#resolve_strong_component!(component_cycled_vertex) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/enum_machine_contrib/decision_graph.rb', line 95

def resolve_strong_component!(component_cycled_vertex) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  input_values  = component_cycled_vertex.incoming_edges.flat_map { |edge| edge.from.value }
  output_values = component_cycled_vertex.outcoming_edges.flat_map { |vertex| vertex.to.value }

  active_vertexes = vertexes.filter(&:active?)
  input_vertexes  = active_vertexes.reject { |vertex| (input_values & vertex.value).empty? }
  output_vertexes = active_vertexes.reject { |vertex| (output_values & vertex.value).empty? }

  component_vertexes = active_vertexes.reject { |vertex| (component_cycled_vertex.value & vertex.value).empty? }

  component_vertexes.each do |vertex|
    vertex.incoming_edges.each do |edge|
      if (input_vertexes + component_vertexes).exclude?(edge.from)
        # drop insignificant incoming edges
        edge.dropped!
      end
    end
  end

  single_incoming_vertexes = (component_vertexes + output_vertexes).filter { |vertex| vertex.incoming_edges.size == 1 }

  single_incoming_chains = []
  single_incoming_vertexes.each do |vertex|
    single_incoming_edge = vertex.incoming_edges.first
    single_incoming_edge.resolved!

    current_chain = single_incoming_chains.detect { |chain| chain.first == single_incoming_edge.to || chain.last == single_incoming_edge.from }
    if current_chain
      current_chain.replace(
        if current_chain.first == single_incoming_edge.to
          current_chain.unshift(single_incoming_edge.from)
        else
          current_chain.push(single_incoming_edge.to)
        end,
      )
    else
      single_incoming_chains << [single_incoming_edge.from, single_incoming_edge.to]
    end
  end

  single_incoming_chains.each do |chain|
    chain_preceding_vertexes  = chain[0].incoming_edges.map(&:from) & (input_vertexes + component_vertexes)
    chain_achievable_vertexes = chain[1..].flat_map { |vertex| vertex.outcoming_edges.map(&:to) }

    pre_chain_vertexes = chain_preceding_vertexes - chain_achievable_vertexes

    if pre_chain_vertexes.size == 1
      single_incoming_edge = chain[0].incoming_edges.detect { |edge| edge.from == pre_chain_vertexes[0] }
      single_incoming_edge.resolved!

      chain.unshift(single_incoming_edge.from)
    end

    chain.flat_map { |vertex| vertex.outcoming_edges.to_a }.group_by(&:to).each_value do |outcoming_edges|
      next if outcoming_edges.size < 2

      outcoming_edges[0..outcoming_edges.size - 2].each(&:dropped!)
    end
  end

  around_vertexes = input_vertexes + component_vertexes + output_vertexes

  component_vertexes.each do |maybe_bottlneck_vertex|
    rest_vertexes = around_vertexes.excluding(maybe_bottlneck_vertex)

    input_achievable_vertexes = next_achievable_vertexes(input_vertexes[0], rest_vertexes, visited: Set.new([maybe_bottlneck_vertex]))
    next if input_achievable_vertexes.size == rest_vertexes.size

    maybe_bottlneck_vertex.outcoming_edges.each do |current_edge|
      if input_achievable_vertexes.include?(current_edge.to) && maybe_bottlneck_vertex.incoming_edges.map(&:from).exclude?((current_edge.to))
        current_edge.to.incoming_edges.each do |edge|
          unless edge.from == maybe_bottlneck_vertex
            edge.dropped!
          end
        end
      end
    end

    rest_vertexes -= input_achievable_vertexes

    maybe_bottlneck_vertex.incoming_edges.each do |edge|
      if rest_vertexes.include?(edge.from)
        edge.dropped!
      end
    end
  end
end

#to_hObject



62
63
64
65
66
67
68
69
# File 'lib/enum_machine_contrib/decision_graph.rb', line 62

def to_h
  vertexes.filter(&:active?).to_h do |vertex|
    [
      vertex.value,
      vertex.outcoming_edges.map { |edge| edge.to.value },
    ]
  end
end