Module: CodeModels::QuerySerialized

Defined in:
lib/codemodels/query_serialized.rb

Class Method Summary collapse

Class Method Details

.attrs(root) ⇒ Object



16
17
18
# File 'lib/codemodels/query_serialized.rb', line 16

def self.attrs(root)
	root.keys.select {|k| k.start_with? 'attr_'}
end

.collect_values(el) ⇒ Object

the set of values appearing in the object and its children



54
55
56
57
58
59
60
61
62
63
# File 'lib/codemodels/query_serialized.rb', line 54

def self.collect_values(el)
	values = Set.new
	rel_conts(el).each do |r|
		values(el,r).each {|c| values.merge(collect_values(c))}
	end
	attrs(el).each do |a|
		values(el,a).each {|v| values.add(v)}
	end
	values
end

.collect_values_with_count(el) ⇒ Object

a counting map values appearing in the object and its children



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/codemodels/query_serialized.rb', line 66

def self.collect_values_with_count(el)
	values = Hash.new {|h,k| h[k]=0}
	rel_conts(el).each do |r|
		values(el,r).each do |ch| 
			collect_values_with_count(ch).each {|v,count| values[v]+=count}
		end
	end
	attrs(el).each do |a|
		values(el,a).each {|v| values[v]+=1 }
	end
	values
end


20
21
22
23
24
25
26
27
28
# File 'lib/codemodels/query_serialized.rb', line 20

def self.print_tree(root,depth=0)
	traverse(root) do |n,d|
		s = ""
		d.times { s = s + "  " }
		s = s + n['type'] if n
		s = s + '<NIL>' unless n
		puts s
	end
end

.rel_conts(root) ⇒ Object



8
9
10
# File 'lib/codemodels/query_serialized.rb', line 8

def self.rel_conts(root)
	root.keys.select {|k| k.start_with? 'relcont_'}
end

.rel_non_conts(root) ⇒ Object



12
13
14
# File 'lib/codemodels/query_serialized.rb', line 12

def self.rel_non_conts(root)
	root.keys.select {|k| k.start_with? 'relnoncont_'}
end

.traverse(root, depth = 0, &op) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/codemodels/query_serialized.rb', line 37

def self.traverse(root,depth=0,&op)
	return traverse(root['root'],depth,&op) if root and (root.key? 'root')
	op.call(root,depth)
	return unless root		
	rel_conts(root).each do |r|
		if root[r].is_a? Array
			root[r].each do |c|
				raise "expected an object but it is a #{c.class} (relation: #{r})" unless c.is_a? Hash
				traverse(c,depth+1,&op)
			end
		else
			traverse(root[r],depth+1,&op)
		end
	end
end

.values(root, feat) ⇒ Object



30
31
32
33
34
35
# File 'lib/codemodels/query_serialized.rb', line 30

def self.values(root,feat)
	raw = root[feat]
	return [] if raw==nil
	return raw if raw.is_a? Array
	return [raw]
end