-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.rb
96 lines (75 loc) · 2.05 KB
/
graph.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
COLORS = [:red, :blue, :purple, :green]
class Intersection
def color
""
end
def label(opts={})
"[ CF #{object_id.to_s[-4..-2]} #{roads.size} road#{roads.size == 1 ? '' : 's'} ]"
end
def to_graph
txt = []
@roads.each_with_index do |road, ix|
# make it a grouping
txt << "("
# don't double-count the intersecting road
points = road.road_after[0..-2] + [self] + road.road_before
(0..points.size - 2).each do |i|
edge = [points[i + 1].label(:group => road.group),
points[i ].label(:group => road.group)]
txt << edge.join(" -> { label: #{points[i + 1].color}; }")
end
txt << ")"
end
txt.join "\n"
end
end
class Group
def label(opts={})
"[ #{roads.map {|r| "R(" + r.object_id.to_s[-4..-2] + ")" }.join "|"} " +
"#{roads.map {|r| r.traffic.size.to_s }.join "|"}]"
end
end
class Road
def label(opts={})
color = opts[:color] ? " { color: #{opts[:color]}; }" : ""
"[ R(#{object_id.to_s[-4..-2]}) #{group} #{traffic.size} ]#{color}"
end
def to_graph(opts={})
node = opts[:intersection] || label
opts[:direction] ||= :both
txt = []
if prev and [:back, :both].include? opts[:direction]
p, edges = prev.to_graph :direction => :back
txt += edges
txt << [p, node].join(" -> ")
end
if succ and [:forward, :both].include? opts[:direction]
s, edges = succ.to_graph :direction => :forward
txt += edges
txt << [node, s].join(" -> ")
end
[node, txt]
end
end
class Intersection
def self.graph(cfs, roads)
kadut = {}
txt = []
cfs.each do |cf|
cf.roads.each do |k|
r = kadut[k.road.last] || k.road
kadut[k.road.last] = r.insert(r.index(k) + 1, cf)
end
end
kadut.each do |_, points|
txt << "("
(0..points.size - 2).each do |i|
edge = [points[i + 1].label,
points[i ].label]
txt << edge.join(" -> { label: #{points[i + 1].color}; }")
end
txt << ")"
end
txt.join "\n"
end
end