Class: Locomotive::Utilities::RelAlg2XML

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/utils/relalg2xml.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast) ⇒ RelAlg2XML

Returns a new instance of RelAlg2XML.



95
96
97
98
99
100
101
102
103
# File 'lib/locomotive/utils/relalg2xml.rb', line 95

def initialize(ast)
  @ast = ast
  # create an identifier for each node by a preorder-traversal
  # and enhance it by an xmlid-annotation
  id = 0
  @ast.traverse do |ast|
    ast.ann_xmlid = (id += 1)
  end
end

Class Method Details

.define_content_cmp(*mtds) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/locomotive/utils/relalg2xml.rb', line 36

def define_content_cmp(*mtds)
  mtds.each do |mtd|
    name = "content_#{mtd}".to_sym
    define_method(name) do |ast|
      cols = []
      cols << "<kind name=\"#{ast.ann_fun_kind}\"/>"
      cols << column(nil, :name => ast.ann_result, :new => true)
      cols << column(nil, :name => ast.ann_operands.first, :new => false, :position => 1)
      cols << column(nil, :name => ast.ann_operands.last, :new => false, :position => 2)
      cols.join("\n")
    end
  end
end

.define_content_ranks(*mtds) ⇒ Object



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
# File 'lib/locomotive/utils/relalg2xml.rb', line 10

def define_content_ranks(*mtds)
  mtds.each do |mtd|
    name = "content_#{mtd}".to_sym
    define_method(name) do |ast|
      cols = []
      cols << column(nil, :name => ast.ann_res, :new => true)
      pos = -1
      ast.ann_order.keys.each do |ord|
        cols << column(nil, :name => ord, :function => "sort",
                               :pos => pos += 1,
                               :direction => ast.ann_order[ord],
                               :new => false)
      end
  
      pos = -1
      ast.ann_part.each do |part|
        cols << column(nil, :name => part, :function => "part",
                               :pos => pos + 1,
                               :new => false)
      end
  
      cols.join("\n")
    end
  end
end

Instance Method Details

#content_attach(ast) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/locomotive/utils/relalg2xml.rb', line 160

def content_attach(ast)
  cols = []
  col = ast.value.first.first
  cols << column(value(ast.value[col].first, :type => ast.ann_schema[col].first),
                 :name => col, :new => true)
  cols.join("\n")
end

#content_cross(ast) ⇒ Object



131
132
# File 'lib/locomotive/utils/relalg2xml.rb', line 131

def content_cross(ast)
end

#content_eqjoin(ast) ⇒ Object



114
115
116
117
118
119
# File 'lib/locomotive/utils/relalg2xml.rb', line 114

def content_eqjoin(ast)
  cols = []
  cols << column(nil, :name => ast.ann_col1,  :new => false, :position => 1)
  cols << column(nil, :name => ast.ann_col2,  :new => false, :position => 2)
  cols.join("\n")
end

#content_fun(ast) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/locomotive/utils/relalg2xml.rb', line 171

def content_fun(ast)
  cols = []
  cols << "<kind name=\"#{ast.ann_fun_kind}\"/>"
  cols << column(nil, :name => ast.ann_result, :new => true)
  cols << column(nil, :name => ast.ann_operands.first, :new => false, :position => 1)
  cols << column(nil, :name => ast.ann_operands.last, :new => false, :position => 2)
  cols.join("\n")
end

#content_nil(ast) ⇒ Object



168
169
# File 'lib/locomotive/utils/relalg2xml.rb', line 168

def content_nil(ast)
end

#content_project(ast) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/locomotive/utils/relalg2xml.rb', line 121

def content_project(ast)
  cols = [] 
  ast.ann_items.keys.each do |old|
    ast.ann_items[old].each do |new|
      cols << column(nil, :name => new, :old_name => old, :new => (new != old))
    end
  end
  cols.join("\n")
end

#content_ref_tbl(ast) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/locomotive/utils/relalg2xml.rb', line 149

def content_ref_tbl(ast)
  cols = []
  cols << "<table name=\"#{ast.value}\">"
  ast.ann_items.keys.each do |col|
    cols << column(nil, :name => col, :tname => ast.ann_items[col],
                        :type => ast.ann_schema[col].join(","))
  end
  cols << "</table>"
  cols.join("\n")
end

#content_serialize_relation(ast) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/locomotive/utils/relalg2xml.rb', line 137

def content_serialize_relation(ast)
  cols = []
  cols << column(nil, :name => ast.ann_iter, :new => false, :function => :iter)
  cols << column(nil, :name => ast.ann_pos, :new => false, :function => :pos)
  pos = 0
  ast.ann_items.each do |item|
    cols << column(nil, :name => item, :new => false, :function => :item, :position => pos)
    pos += 1
  end
  cols.join("\n")
end

#content_table(ast) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/locomotive/utils/relalg2xml.rb', line 105

def content_table(ast)
  cols = []
  ast.ann_values.keys.each do |col|
    cols << column(value(ast.ann_values[col], :type => ast.ann_schema[col].first),
                   :name => col, :new => true)
  end
  cols.join("\n")
end

#content_union(ast) ⇒ Object



134
135
# File 'lib/locomotive/utils/relalg2xml.rb', line 134

def content_union(ast)
end

#to_xmlObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/locomotive/utils/relalg2xml.rb', line 208

def to_xml

  node_list = []

  node_list << <<PROLOG
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<query_plan_bundle>
<query_plan id=\"0\">
<properties>
<property name=\"overallResultType\" value=\"TUPLE\"/>
</properties>
<logical_query_plan unique_names=\"true\">
PROLOG

  @ast.traverse_strategy = Locomotive::AstHelpers::PostOrderTraverse
  @ast.traverse do |ast|
    node_list << to_xml_wrapper(ast)
  end

node_list << <<EPILOG
</logical_query_plan>
</query_plan>
</query_plan_bundle>
EPILOG

  node_list.join("\n")
end

#to_xml_wrapper(ast) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/locomotive/utils/relalg2xml.rb', line 180

def to_xml_wrapper(ast)
#<schema>
#{schema ast.ann_schema}
#</schema>
<<XMLNODE
<node id=\"#{ast.ann_xmlid.to_s}\" kind=\"#{ast.kind.to_s}\">
<content>
#{ content_method = "content_#{->(kind) {
                             if kind == "serialize relation".to_sym then
                               :serialize_relation
                             else
                               kind
                             end }[ast.kind]}"
if !self.respond_to?(content_method) then
  "<nocontent/>"
else
  self.send(content_method, ast)
end}
</content>
#{edges = []
edges << edge(:to => ast.left_child.ann_xmlid) if ast.has_left_child?
edges << edge(:to => ast.right_child.ann_xmlid) if ast.has_right_child?
edges.join("\n")
 }
</node>
XMLNODE
end