forked from pact-foundation/pact_broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bethtest.rb
103 lines (76 loc) · 2.23 KB
/
bethtest.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'set'
class Link
def initialize from, to
@from = from
@to = to
end
def include? endpoint
@from == endpoint || @to == endpoint
end
def connected? other
(self.to_a & other.to_a).any?
end
def to_s
"#{@from} - #{@to}"
end
def to_a
[@from, @to]
end
end
def unique_nodes links
links.collect(&:to_a).flatten.uniq.sort
end
def nodes_connected_to_node node, links
unique_nodes links.select{|l|l.include?(node)}
end
def connected_links link, link_pool
link_pool.select{|l| l.connected?(link)}
end
def nodes_connected_to_nodes_within_pool nodes, links, node_pool
nodes.collect{ | node | nodes_connected_to_node(node, links) }.flatten & node_pool
end
def connected_links_still_within_pool links, link_pool
links.collect{ | link | connected_links(link, link_pool) }.flatten.uniq
end
def split_into_clusters_of_nodes links
node_pool = unique_nodes links
groups = []
while node_pool.any?
group = []
groups << group
connected_nodes = [node_pool.first]
while connected_nodes.any?
group.concat(connected_nodes)
node_pool = node_pool - connected_nodes
connected_nodes = nodes_connected_to_nodes_within_pool connected_nodes, links, node_pool
end
end
groups
end
def recurse link, link_pool
connected_links = link_pool.select{ | candidate| candidate.connected?(link) }
if connected_links.empty?
[link]
else
([link] + connected_links.map{| connected_link| recurse(connected_link, link_pool - connected_links)}.flatten).uniq
end
end
def recurse_groups groups, link_pool
if link_pool.empty?
groups
else
first, *rest = *link_pool
group = recurse first, rest
recurse_groups(groups + [group], link_pool - group)
end
end
def split_into_clusters_of_links links
recurse_groups [], links.dup
end
links = [Link.new('A', 'B'), Link.new('A', 'C'), Link.new('C', 'D'), Link.new('D', 'E'), Link.new('E','A'),
Link.new('Y', 'Z'), Link.new('X', 'Y'),
Link.new('M', 'N'), Link.new('N', 'O'), Link.new('O', 'P'), Link.new('P','Q')]
groups = split_into_clusters_of_nodes links
puts groups.collect{ | group| "group = #{group.join(" ")}"}
groups = split_into_clusters_of_links links
puts groups.collect{ | group| "group = #{group.join(", ")}"}