maruku 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,410 @@
1
+ require 'set'
2
+
1
3
  module Diagrams
2
4
 
5
+ class SSimple
6
+ attr_accessor :begin, :end # begin and end variables
7
+ attr_accessor :spring
8
+ def get_spring() @spring end
9
+ def internal_variables() [] end
10
+ end
11
+
12
+ class SParallel
13
+ attr_accessor :begin, :end # begin and end variables
14
+ attr_accessor :children
15
+ def internal_variables()
16
+ @children.map{|x|x.internal_variables}.flatten
17
+ end
18
+ end
19
+
20
+
21
+ class VarGraph
22
+ class Node
23
+ attr_accessor :name
24
+ attr_accessor :children
25
+ attr_accessor :parents
26
+
27
+ attr_accessor :next # closure of children
28
+ attr_accessor :prev # closure of parents
29
+
30
+ def initialize(name_)
31
+ @name = name_
32
+ @children = []
33
+ @parents = []
34
+ @next = []
35
+ @prev = []
36
+ end
37
+
38
+ def inspect() "N[#{@name}]" end
39
+ alias to_s inspect
40
+
41
+ # return parents
42
+ def compute_parents_closure
43
+ @parents.each do |parent| # xxx rifare
44
+ parent.compute_parents_closure
45
+ end
46
+
47
+ @prev = @parents.map do |p| p.prev end.flatten
48
+ @prev += @parents
49
+ @prev.uniq!
50
+ end
51
+
52
+ def compute_children_closure
53
+ @children.each do |child| # xxx rifare
54
+ child.compute_children_closure
55
+ end
56
+
57
+ @next = @children.map do |c| c.next end.flatten
58
+ @next += @children
59
+ @next.uniq!
60
+ end
61
+
62
+ def dist_up() @parents.empty? ? 0 : @parents.map{|x| x.dist_up}.max+1 end
63
+ def dist_down() @children.empty? ? 0 : @children.map{|x| x.dist_down}.max+1 end
64
+
65
+ end
66
+
67
+ def ==(other)
68
+ self.name == other.name
69
+ end
70
+
71
+ attr_accessor :name2node
72
+
73
+ def initialize
74
+ self.name2node = {}
75
+ end
76
+
77
+ def add_edge(var1, var2)
78
+ raise "var1 = var2 = #{var1.inspect}" if var1 == var2
79
+
80
+ name2node[var1] = Node.new(var1) if not name2node.has_key? var1
81
+ name2node[var2] = Node.new(var2) if not name2node.has_key? var2
82
+
83
+ n1,n2 = name2node[var1],name2node[var2]
84
+
85
+ n1.children.push n2
86
+ n2.parents.push n1
87
+ end
88
+
89
+ def compute_closure
90
+ @name2node.each do |var, node|
91
+ node.compute_children_closure
92
+ node.compute_parents_closure
93
+ end
94
+
95
+ @name2node.each do |var, node|
96
+ # puts "Node #{var} -> #{node.next.inspect}"
97
+ # puts "Node #{var} <- #{node.prev.inspect}"
98
+ end
99
+ end
100
+
101
+ def go_recursive(node_head, id)
102
+ sub = []
103
+ node_head.children.each do |child|
104
+ if child.parents.size == 1
105
+ sub.push child
106
+ end
107
+ end
108
+
109
+ puts "#{id} Found #{sub.size} subs: #{sub.inspect}"
110
+
111
+ node_head.children.each do |child|
112
+ if (not sub.include?(child)) and child.parents.size != 1
113
+ adottare = []
114
+ sub.each do |su|
115
+ if child.prev.include? su
116
+ adottare.push su
117
+ end
118
+ end
119
+
120
+ puts "#{id} child #{child} might be associated with #{adottare.inspect} - #{child.parents.inspect}"
121
+
122
+ end
123
+ end
124
+
125
+ sub.each_with_index do |sub, i|
126
+ puts "#{id}/#{i}: #{sub}"
127
+ go_recursive(sub, "#{id}/#{i}")
128
+ end
129
+ end
130
+
131
+ def go!
132
+
133
+ def do_it(name2node, function)
134
+ dist2nodes = []
135
+ name2node.each do |var, node|
136
+ dist = node.send function
137
+ dist2nodes[dist] =[] if dist2nodes[dist] == nil
138
+ dist2nodes[dist].push node
139
+ end
140
+ dist2nodes
141
+ end
142
+
143
+ up = do_it(@name2node, :dist_up)
144
+ down = do_it(@name2node, :dist_down)
145
+
146
+ up.each_with_index do |nodes, i|
147
+ puts "U Generation #{i}: #{nodes.inspect}"
148
+ end
149
+ down.each_with_index do |nodes, i|
150
+ puts "D Generation #{i}: #{nodes.inspect}"
151
+ end
152
+
153
+ # go_recursive(@name2node[DIAG_TL], "root")
154
+ end
155
+ end
156
+
157
+ class Layout
158
+ def initialize
159
+ @hsystem = VarGraph.new
160
+ @vsystem = VarGraph.new
161
+
162
+ @hlink,@vlink = {},{}
163
+ @hsprings,@vsprings={},{}
164
+ end
165
+
166
+ def to_dot()
167
+ return to_dot_links(@hlink,@hsprings), to_dot_links(@vlink,@vsprings)
168
+ end
169
+
170
+ def to_dot_links(links, springs)
171
+ s = "digraph diagram {\n"
172
+ links.each do |var, to_other|
173
+ if not to_other.empty?
174
+ var_ = var.gsub(/^[^_]+_/,'')
175
+ s += "\t#{var} [label = \"#{var_}\" ];\n"
176
+ to_other.each do |v2, spring|
177
+ s += "\t #{var} -> #{v2} [ label = \"#{spring.inspect}\"];\n"
178
+ end
179
+ end
180
+ end
181
+ s += "}\n"
182
+ end
183
+
184
+ def add_var(s)
185
+ @hlink[s]=[] if not @hlink[s]
186
+ @vlink[s]=[] if not @vlink[s]
187
+ end
188
+
189
+ # var2-var1 = spring
190
+ def add_con_h(var1,var2,hspring)
191
+ add_var var1; add_var var2;
192
+ @hlink[var1].push [var2, hspring]
193
+ @hsprings[[var1,var2]] = hspring
194
+
195
+ @hsystem.add_edge(var1, var2)
196
+ end
197
+
198
+ def add_con_v(var1,var2,vspring)
199
+ add_var var1; add_var var2;
200
+ @vlink[var1].push [var2, vspring]
201
+ @vsprings[[var1,var2]] = vspring
202
+
203
+ @vsystem.add_edge(var1, var2)
204
+ end
205
+
206
+ def get_preferred(var1, var2)
207
+ hpaths = rec_paths(@hlink, var1, var2)
208
+ vpaths = rec_paths(@vlink, var1, var2)
209
+ puts inspect
210
+ hpaths.each do |h|
211
+ puts "H: #{h.inspect}"
212
+ end
213
+ vpaths.each do |v|
214
+ puts "V: #{v.inspect}"
215
+ end
216
+
217
+ hsprings =
218
+ hpaths.map do |h|
219
+ ss = []
220
+ for i in 0..(h.size-2)
221
+ v1,v2 = h[i],h[i+1]
222
+ ss.push @hsprings[[v1,v2]]
223
+ end
224
+ ss
225
+ end
226
+ vsprings =
227
+ vpaths.map do |h|
228
+ ss = []
229
+ for i in 0..(h.size-2)
230
+ v1,v2 = h[i],h[i+1]
231
+ ss.push @vsprings[[v1,v2]]
232
+ end
233
+ ss
234
+ end
235
+
236
+
237
+ def go_up_parallel(par)
238
+ final = par[0]
239
+ for i in 1..(par.size-1)
240
+ final = Spring.parallel(final, par[i])
241
+ end
242
+ final
243
+ end
244
+
245
+ def go_up_series(series)
246
+ tot = series[0]
247
+ for i in 1..(series.size-1)
248
+ tot = Spring.series(tot, series[i])
249
+ end
250
+ tot
251
+ end
252
+
253
+ def compress(x)
254
+ t = x.map do |s| go_up_series(s) end
255
+ final = go_up_parallel(t)
256
+ end
257
+
258
+ def sum_of_shrink(series)
259
+ sum = 0
260
+ series.each do |s| sum += s.shrink end
261
+ sum
262
+ end
263
+
264
+ def sum_of_stretch(series)
265
+ sum = 0
266
+ series.each do |s| sum += s.stretch end
267
+ sum
268
+ end
269
+
270
+ def go_back(imposed, series)
271
+ pref = go_up_series(series)
272
+ if pref.pref > imposed
273
+ # must shrink
274
+ shrink = pref.pref - imposed
275
+ sum = sum_of_shrink(series)
276
+ series.each do |s|
277
+ its_shrink = (s.shrink / sum) * shrink
278
+ its_imposed = s.pref - its_shrink
279
+ if s.imposed
280
+ s.imposed = (its_imposed+ s.imposed)*0.5
281
+ else
282
+ s.imposed = its_imposed
283
+ end
284
+ s.pref = s.pref*0.5 + s.imposed*0.5
285
+ end
286
+ else
287
+ # must stretch
288
+ stretch = imposed - pref.pref
289
+ sum = sum_of_stretch(series)
290
+ series.each do |s|
291
+ its_stretch = (s.stretch / sum) * stretch
292
+ its_imposed = s.pref + its_stretch
293
+ if s.imposed
294
+ s.imposed = (its_imposed+ s.imposed)*0.5
295
+ else
296
+ s.imposed = its_imposed
297
+ end
298
+ s.pref = s.pref*0.5 + s.imposed*0.5
299
+ end
300
+ end
301
+ end
302
+
303
+ for i in 0..100
304
+ hpref = compress(hsprings)
305
+ vpref = compress(vsprings)
306
+
307
+ width = hpref.pref
308
+ height = vpref.pref
309
+
310
+ hsprings.each do |series| go_back(height, series) end
311
+ vsprings.each do |series| go_back(width, series) end
312
+ end
313
+
314
+ hpaths.each do |h|
315
+ ss = []
316
+ len = 0
317
+ for i in 0..(h.size-2)
318
+ v1,v2 = h[i],h[i+1]
319
+ this = @hsprings[[v1,v2]].imposed
320
+ puts "#{v1} - #{v2} | "+ ("%0.2f" % this)
321
+ len += this
322
+ end
323
+ puts "LEN: #{len}"
324
+ ss
325
+ end
326
+
327
+ return hpref,vpref
328
+ end
329
+
330
+ # returns [] if there isn't one
331
+ def rec_paths(links, var1, var2)
332
+ all_paths = []
333
+ (links[var1] || []).each do |v, spring|
334
+ if v == var2
335
+ all_paths.push [var2]
336
+ else
337
+ all_paths = all_paths + rec_paths(links, v, var2)
338
+ end
339
+ end
340
+ all_paths.map{|x| [var1]+x}
341
+ end
342
+
343
+
344
+ def algo2
345
+ @hsystem.compute_closure
346
+ @hsystem.go!
347
+ =begin
348
+ def reduce(links, varA, varZ)
349
+ paths = rec_paths(links, varA, varZ)
350
+
351
+ puts "Found #{paths.size} paths:"
352
+ paths.each_with_index do |path, i|
353
+ puts "path #{i}: #{path.inspect}"
354
+ end
355
+
356
+ var2path = {}
357
+
358
+ paths.each_with_index do |path, p|
359
+ for i in 1..path.size-2
360
+ var = path[i]
361
+ var2path[var] ||= []
362
+ var2path[var].push p
363
+ end
364
+ end
365
+
366
+ var2path.each do |v, paths|
367
+ puts "var #{v} -> #{paths.inspect}"
368
+ end
369
+ end
370
+
371
+ reduce(@hlink, DIAG_TL, DIAG_BR)
372
+ =end
373
+
374
+ end
375
+
376
+ def inspect
377
+ s = ""
378
+ @hlink.each do |v1,a|
379
+ a.each do |v2, spring|
380
+ s += "#{v1.inspect} -> #{v2.inspect} (#{spring.inspect})\n"
381
+ end
382
+ end
383
+ s
384
+ end
385
+ end
386
+
387
+ DIAG_TL = "tl"
388
+ DIAG_BR = "br"
389
+
3
390
  class Diagram
4
- DIAG_TL = "tl"
5
- DIAG_BR = "br"
391
+
392
+
393
+ def top_right(i); "#{@diag_id}_b#{i}_tr"; end
394
+
395
+ def assign_variables(diag_id)
396
+ @boxes.each_with_index do |b, i|
397
+ b.v_tl = "#{diag_id}_b#{i}_tl"
398
+ b.v_br = "#{diag_id}_b#{i}_br"
399
+ end
400
+ @arrows.each_with_index do |a, i|
401
+ a.v_start = "#{diag_id}_a#{i}_start"
402
+ a.v_end = "#{diag_id}_a#{i}_end"
403
+ end
404
+ @points.each_with_index do |p, i|
405
+ p.v_p = "#{diag_id}_p#{i}_p"
406
+ end
407
+ end
6
408
 
7
409
  def do_layout(diag_id)
8
410
  def margin_con() Spring.new(30,0,30,1,1000) end
@@ -93,6 +495,7 @@ module Diagrams
93
495
  File.open("#{diag_id}_h.dot",'w') do |f| f.write doth end
94
496
  File.open("#{diag_id}_v.dot",'w') do |f| f.write dotv end
95
497
 
498
+ l.algo2
96
499
  # hs, vs = l.get_preferred(DIAG_TL, DIAG_BR)
97
500
 
98
501
  # puts "Preferred hs = #{hs.inspect}"
@@ -88,211 +88,6 @@ module Diagrams
88
88
  s
89
89
  end
90
90
 
91
- class Layout
92
- def initialize
93
- @hlink,@vlink = {},{}
94
- @hsprings,@vsprings={},{}
95
- end
96
-
97
- def to_dot()
98
- return to_dot_links(@hlink,@hsprings), to_dot_links(@vlink,@vsprings)
99
- end
100
-
101
- def to_dot_links(links, springs)
102
- s = "digraph diagram {\n"
103
- links.each do |var, to_other|
104
- if not to_other.empty?
105
- var_ = var.gsub(/^[^_]+_/,'')
106
- s += "\t#{var} [label = \"#{var_}\" ];\n"
107
- to_other.each do |v2, spring|
108
- s += "\t #{var} -> #{v2} [ label = \"#{spring.inspect}\"];\n"
109
- end
110
- end
111
- end
112
- s += "}\n"
113
- end
114
-
115
- def add_var(s)
116
- @hlink[s]=[] if not @hlink[s]
117
- @vlink[s]=[] if not @vlink[s]
118
- end
119
-
120
- # var2-var1 = spring
121
- def add_con_h(var1,var2,hspring)
122
- add_var var1; add_var var2;
123
- @hlink[var1].push [var2, hspring]
124
- @hsprings[[var1,var2]] = hspring
125
- end
126
-
127
- def add_con_v(var1,var2,vspring)
128
- add_var var1; add_var var2;
129
- @vlink[var1].push [var2, vspring]
130
- @vsprings[[var1,var2]] = vspring
131
- end
132
-
133
- def get_preferred(var1, var2)
134
- hpaths = rec_paths(@hlink, var1, var2)
135
- vpaths = rec_paths(@vlink, var1, var2)
136
- puts inspect
137
- hpaths.each do |h|
138
- puts "H: #{h.inspect}"
139
- end
140
- vpaths.each do |v|
141
- puts "V: #{v.inspect}"
142
- end
143
-
144
- hsprings =
145
- hpaths.map do |h|
146
- ss = []
147
- for i in 0..(h.size-2)
148
- v1,v2 = h[i],h[i+1]
149
- ss.push @hsprings[[v1,v2]]
150
- end
151
- ss
152
- end
153
- vsprings =
154
- vpaths.map do |h|
155
- ss = []
156
- for i in 0..(h.size-2)
157
- v1,v2 = h[i],h[i+1]
158
- ss.push @vsprings[[v1,v2]]
159
- end
160
- ss
161
- end
162
-
163
-
164
- def go_up_parallel(par)
165
- final = par[0]
166
- for i in 1..(par.size-1)
167
- final = Spring.parallel(final, par[i])
168
- end
169
- final
170
- end
171
-
172
- def go_up_series(series)
173
- tot = series[0]
174
- for i in 1..(series.size-1)
175
- tot = Spring.series(tot, series[i])
176
- end
177
- tot
178
- end
179
-
180
- def compress(x)
181
- t = x.map do |s| go_up_series(s) end
182
- final = go_up_parallel(t)
183
- end
184
-
185
- def sum_of_shrink(series)
186
- sum = 0
187
- series.each do |s| sum += s.shrink end
188
- sum
189
- end
190
-
191
- def sum_of_stretch(series)
192
- sum = 0
193
- series.each do |s| sum += s.stretch end
194
- sum
195
- end
196
-
197
- def go_back(imposed, series)
198
- pref = go_up_series(series)
199
- if pref.pref > imposed
200
- # must shrink
201
- shrink = pref.pref - imposed
202
- sum = sum_of_shrink(series)
203
- series.each do |s|
204
- its_shrink = (s.shrink / sum) * shrink
205
- its_imposed = s.pref - its_shrink
206
- if s.imposed
207
- s.imposed = (its_imposed+ s.imposed)*0.5
208
- else
209
- s.imposed = its_imposed
210
- end
211
- s.pref = s.pref*0.5 + s.imposed*0.5
212
- end
213
- else
214
- # must stretch
215
- stretch = imposed - pref.pref
216
- sum = sum_of_stretch(series)
217
- series.each do |s|
218
- its_stretch = (s.stretch / sum) * stretch
219
- its_imposed = s.pref + its_stretch
220
- if s.imposed
221
- s.imposed = (its_imposed+ s.imposed)*0.5
222
- else
223
- s.imposed = its_imposed
224
- end
225
- s.pref = s.pref*0.5 + s.imposed*0.5
226
- end
227
- end
228
- end
229
-
230
- for i in 0..100
231
- hpref = compress(hsprings)
232
- vpref = compress(vsprings)
233
-
234
- width = hpref.pref
235
- height = vpref.pref
236
-
237
- hsprings.each do |series| go_back(height, series) end
238
- vsprings.each do |series| go_back(width, series) end
239
- end
240
-
241
- hpaths.each do |h|
242
- ss = []
243
- len = 0
244
- for i in 0..(h.size-2)
245
- v1,v2 = h[i],h[i+1]
246
- this = @hsprings[[v1,v2]].imposed
247
- puts "#{v1} - #{v2} | "+ ("%0.2f" % this)
248
- len += this
249
- end
250
- puts "LEN: #{len}"
251
- ss
252
- end
253
-
254
- return hpref,vpref
255
- end
256
-
257
- # returns [] if there isn't one
258
- def rec_paths(links, var1, var2)
259
- all_paths = []
260
- (links[var1] || []).each do |v, spring|
261
- if v == var2
262
- all_paths.push [var2]
263
- else
264
- all_paths = all_paths + rec_paths(links, v, var2)
265
- end
266
- end
267
- all_paths.map{|x| [var1]+x}
268
- end
269
-
270
- def inspect
271
- s = ""
272
- @hlink.each do |v1,a|
273
- a.each do |v2, spring|
274
- s += "#{v1.inspect} -> #{v2.inspect} (#{spring.inspect})\n"
275
- end
276
- end
277
- s
278
- end
279
- end
280
-
281
- def top_right(i); "#{@diag_id}_b#{i}_tr"; end
282
-
283
- def assign_variables(diag_id)
284
- @boxes.each_with_index do |b, i|
285
- b.v_tl = "#{diag_id}_b#{i}_tl"
286
- b.v_br = "#{diag_id}_b#{i}_br"
287
- end
288
- @arrows.each_with_index do |a, i|
289
- a.v_start = "#{diag_id}_a#{i}_start"
290
- a.v_end = "#{diag_id}_a#{i}_end"
291
- end
292
- @points.each_with_index do |p, i|
293
- p.v_p = "#{diag_id}_p#{i}_p"
294
- end
295
- end
296
91
 
297
92
  # in points
298
93
  def to_dev(x)