locomotive 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. data/lib/locomotive.rb +6 -0
  2. data/lib/locomotive/core_extensions.rb +6 -0
  3. data/lib/locomotive/core_extensions/array.rb +22 -0
  4. data/lib/locomotive/core_extensions/class.rb +47 -0
  5. data/lib/locomotive/core_extensions/hash.rb +7 -0
  6. data/lib/locomotive/core_extensions/inflector.rb +29 -0
  7. data/lib/locomotive/core_extensions/module.rb +77 -0
  8. data/lib/locomotive/core_extensions/symbol.rb +17 -0
  9. data/lib/locomotive/misc.rb +1 -0
  10. data/lib/locomotive/misc/type_check.rb +129 -0
  11. data/lib/locomotive/relational_algebra.rb +8 -0
  12. data/lib/locomotive/relational_algebra/attributes.rb +171 -0
  13. data/lib/locomotive/relational_algebra/operators.rb +15 -0
  14. data/lib/locomotive/relational_algebra/operators/abstraction.rb +2 -0
  15. data/lib/locomotive/relational_algebra/operators/abstraction/lambda.rb +83 -0
  16. data/lib/locomotive/relational_algebra/operators/abstraction/variable.rb +65 -0
  17. data/lib/locomotive/relational_algebra/operators/aggregation.rb +2 -0
  18. data/lib/locomotive/relational_algebra/operators/aggregation/aggr_builtin.rb +26 -0
  19. data/lib/locomotive/relational_algebra/operators/aggregation/aggregation.rb +76 -0
  20. data/lib/locomotive/relational_algebra/operators/basic_operators.rb +144 -0
  21. data/lib/locomotive/relational_algebra/operators/boolean.rb +4 -0
  22. data/lib/locomotive/relational_algebra/operators/boolean/and.rb +9 -0
  23. data/lib/locomotive/relational_algebra/operators/boolean/basic_boolean.rb +66 -0
  24. data/lib/locomotive/relational_algebra/operators/boolean/not.rb +56 -0
  25. data/lib/locomotive/relational_algebra/operators/boolean/or.rb +9 -0
  26. data/lib/locomotive/relational_algebra/operators/builtins.rb +3 -0
  27. data/lib/locomotive/relational_algebra/operators/builtins/arith_builtin.rb +37 -0
  28. data/lib/locomotive/relational_algebra/operators/builtins/basic_builtin.rb +20 -0
  29. data/lib/locomotive/relational_algebra/operators/builtins/function.rb +69 -0
  30. data/lib/locomotive/relational_algebra/operators/comparisons.rb +6 -0
  31. data/lib/locomotive/relational_algebra/operators/comparisons/basic_comparison.rb +65 -0
  32. data/lib/locomotive/relational_algebra/operators/comparisons/equal.rb +13 -0
  33. data/lib/locomotive/relational_algebra/operators/comparisons/greater.rb +13 -0
  34. data/lib/locomotive/relational_algebra/operators/comparisons/greater_equal.rb +14 -0
  35. data/lib/locomotive/relational_algebra/operators/comparisons/less.rb +21 -0
  36. data/lib/locomotive/relational_algebra/operators/comparisons/less_equal.rb +13 -0
  37. data/lib/locomotive/relational_algebra/operators/error.rb +1 -0
  38. data/lib/locomotive/relational_algebra/operators/error/error.rb +52 -0
  39. data/lib/locomotive/relational_algebra/operators/filter.rb +1 -0
  40. data/lib/locomotive/relational_algebra/operators/filter/select.rb +50 -0
  41. data/lib/locomotive/relational_algebra/operators/join.rb +5 -0
  42. data/lib/locomotive/relational_algebra/operators/join/basic_join.rb +9 -0
  43. data/lib/locomotive/relational_algebra/operators/join/cross.rb +28 -0
  44. data/lib/locomotive/relational_algebra/operators/join/equi_join.rb +61 -0
  45. data/lib/locomotive/relational_algebra/operators/join/predicates.rb +95 -0
  46. data/lib/locomotive/relational_algebra/operators/join/theta_join.rb +53 -0
  47. data/lib/locomotive/relational_algebra/operators/projections.rb +2 -0
  48. data/lib/locomotive/relational_algebra/operators/projections/attach.rb +67 -0
  49. data/lib/locomotive/relational_algebra/operators/projections/projection.rb +106 -0
  50. data/lib/locomotive/relational_algebra/operators/ranking.rb +6 -0
  51. data/lib/locomotive/relational_algebra/operators/ranking/basic_ranking.rb +67 -0
  52. data/lib/locomotive/relational_algebra/operators/ranking/rank.rb +9 -0
  53. data/lib/locomotive/relational_algebra/operators/ranking/rank_lists.rb +45 -0
  54. data/lib/locomotive/relational_algebra/operators/ranking/row_id.rb +24 -0
  55. data/lib/locomotive/relational_algebra/operators/ranking/row_number.rb +55 -0
  56. data/lib/locomotive/relational_algebra/operators/ranking/row_rank.rb +9 -0
  57. data/lib/locomotive/relational_algebra/operators/serialization.rb +2 -0
  58. data/lib/locomotive/relational_algebra/operators/serialization/basic_serialize.rb +9 -0
  59. data/lib/locomotive/relational_algebra/operators/serialization/serialize_relation.rb +105 -0
  60. data/lib/locomotive/relational_algebra/operators/set.rb +4 -0
  61. data/lib/locomotive/relational_algebra/operators/set/basic_set.rb +24 -0
  62. data/lib/locomotive/relational_algebra/operators/set/difference.rb +11 -0
  63. data/lib/locomotive/relational_algebra/operators/set/distinct.rb +35 -0
  64. data/lib/locomotive/relational_algebra/operators/set/union.rb +11 -0
  65. data/lib/locomotive/relational_algebra/operators/tables.rb +3 -0
  66. data/lib/locomotive/relational_algebra/operators/tables/literal_table.rb +95 -0
  67. data/lib/locomotive/relational_algebra/operators/tables/nil.rb +13 -0
  68. data/lib/locomotive/relational_algebra/operators/tables/ref_table.rb +75 -0
  69. data/lib/locomotive/relational_algebra/operators/typeing.rb +1 -0
  70. data/lib/locomotive/relational_algebra/operators/typeing/cast.rb +59 -0
  71. data/lib/locomotive/relational_algebra/ordering.rb +30 -0
  72. data/lib/locomotive/relational_algebra/query_information.rb +666 -0
  73. data/lib/locomotive/relational_algebra/rel_alg_ast_node.rb +51 -0
  74. data/lib/locomotive/relational_algebra/rel_alg_exceptions.rb +15 -0
  75. data/lib/locomotive/relational_algebra/schema.rb +87 -0
  76. data/lib/locomotive/relational_algebra/types.rb +86 -0
  77. data/lib/locomotive/tree_helpers.rb +3 -0
  78. data/lib/locomotive/tree_helpers/annotations.rb +58 -0
  79. data/lib/locomotive/tree_helpers/ast.rb +99 -0
  80. data/lib/locomotive/tree_helpers/ast_traversal.rb +43 -0
  81. data/lib/locomotive/utils.rb +2 -0
  82. data/lib/locomotive/utils/relalg2xml.rb +239 -0
  83. data/lib/locomotive/utils/xml.rb +47 -0
  84. metadata +157 -0
@@ -0,0 +1,5 @@
1
+ require 'locomotive/relational_algebra/operators/join/basic_join'
2
+ require 'locomotive/relational_algebra/operators/join/cross'
3
+ require 'locomotive/relational_algebra/operators/join/equi_join'
4
+ require 'locomotive/relational_algebra/operators/join/predicates'
5
+ require 'locomotive/relational_algebra/operators/join/theta_join'
@@ -0,0 +1,9 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class Join < Binary; end
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,28 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class Cross < Join
6
+ def initialize(op1,op2)
7
+ super(op1,op2)
8
+ end
9
+
10
+ def left_and_right(op1,op2)
11
+ self.schema = op1.schema + op2.schema
12
+ super(op1,op2)
13
+ end
14
+
15
+ def clone
16
+ Cross.new(left.clone,right.clone)
17
+ end
18
+
19
+ def set(var,plan)
20
+ Cross.new(
21
+ left.set(var,plan),
22
+ right.set(var,plan))
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,61 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class EquiJoin < Join
6
+ attr_accessor :item1
7
+ def_sig :item1=, ConstAttribute
8
+ attr_accessor :item2
9
+ def_sig :item2=, ConstAttribute
10
+
11
+
12
+ def initialize(op1,op2,it1,it2)
13
+ self.item1,
14
+ self.item2 = it1, it2
15
+ super(op1,op2)
16
+ end
17
+
18
+ def left_and_right(op1,op2)
19
+ unless op1.schema.attributes?([self.item1])
20
+ raise CorruptedSchema,
21
+ "Schema #{op1.schema.attributes} does not " \
22
+ "contain all attributes of #{item1}."
23
+ end
24
+ unless op2.schema.attributes?([self.item2])
25
+ raise CorruptedSchema,
26
+ "Schema #{op2.schema.attributes} does not " \
27
+ "contain all attributes of #{item2}."
28
+ end
29
+ self.schema = op1.schema + op2.schema
30
+ super(op1,op2)
31
+ end
32
+
33
+ def xml_kind
34
+ :eqjoin
35
+ end
36
+
37
+ def xml_content
38
+ content do
39
+ [column(:name => item1.to_xml, :new => false, :position => 1),
40
+ column(:name => item2.to_xml, :new => false, :position => 2)
41
+ ].join
42
+ end
43
+ end
44
+
45
+ def clone
46
+ EquiJoin.new(left.clone,right.clone,
47
+ item1.clone, item2.clone)
48
+ end
49
+
50
+ def set(var,plan)
51
+ EquiJoin.new(
52
+ left.set(var,plan),
53
+ right.set(var,plan),
54
+ item1.clone,
55
+ item2.clone)
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,95 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class Predicate
6
+ include Locomotive::XML
7
+ def_node :column, :comparison
8
+
9
+ private
10
+
11
+ def first=(first)
12
+ @first = first
13
+ end
14
+ def_sig :first=, ConstAttribute
15
+
16
+ def second=(snd)
17
+ @second = snd
18
+ end
19
+ def_sig :second=, ConstAttribute
20
+
21
+ public
22
+
23
+ attr_reader :first,
24
+ :second
25
+
26
+ def initialize(first_, second_)
27
+ self.first,
28
+ self.second = first_, second_
29
+ end
30
+
31
+ def clone
32
+ self.class.new(first.clone,second.clone)
33
+ end
34
+ end
35
+
36
+
37
+ class Equivalence < Predicate
38
+ def to_xml
39
+ comparison :kind => :eq do
40
+ [column(:name => first.to_xml, :new => false, :position => 1),
41
+ column(:name => second.to_xml, :new => false, :position => 2)].join
42
+ end
43
+ end
44
+ end
45
+
46
+ class PredLessThan < Predicate
47
+ def to_xml
48
+ comparison :kind => :lt do
49
+ [column(:name => first.to_xml, :new => false, :position => 1),
50
+ column(:name => second.to_xml, :new => false, :position => 2)].join
51
+ end
52
+ end
53
+ end
54
+
55
+ class PredGreaterThan < Predicate
56
+ def to_xml
57
+ comparison :kind => :gt do
58
+ [column(:name => first.to_xml, :new => false, :position => 1),
59
+ column(:name => second.to_xml, :new => false, :position => 2)].join
60
+ end
61
+ end
62
+ end
63
+
64
+ class PredicateList
65
+ include Locomotive::XML
66
+ def_node :content
67
+
68
+ private
69
+
70
+ attr_accessor :pred_list
71
+ def_sig :pred_list=, [Predicate]
72
+
73
+ public
74
+ delegate :[],
75
+ :to_a,
76
+ :to => :pred_list
77
+
78
+ def initialize(*ary)
79
+ self.pred_list = ary
80
+ end
81
+
82
+ def to_xml
83
+ pred_list.collect do |pred|
84
+ pred.to_xml
85
+ end.join
86
+ end
87
+
88
+ def clone
89
+ PredicateList.new( *pred_list.clone )
90
+ end
91
+ end
92
+
93
+ end
94
+
95
+ end
@@ -0,0 +1,53 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class ThetaJoin < Join
6
+ private
7
+
8
+ def to_predicate_list(predicates)
9
+ case
10
+ when Array === predicates then
11
+ PredicateList.new(*predicates)
12
+ when PredicateList === predicates then
13
+ predicates
14
+ end
15
+ end
16
+
17
+ public
18
+ attr_accessor :predicate_list
19
+ def_sig :predicate_list=, PredicateList
20
+
21
+ def initialize(op1, op2, pred_list)
22
+ self.predicate_list = to_predicate_list pred_list
23
+ super(op1,op2)
24
+ end
25
+
26
+ def left_and_right(op1,op2)
27
+ self.schema = op1.schema + op2.schema
28
+ super(op1,op2)
29
+ end
30
+
31
+ def xml_content
32
+ content do
33
+ predicate_list.to_xml
34
+ end
35
+ end
36
+
37
+ def clone
38
+ ThetaJoin.new(left.clone,
39
+ right.clone,
40
+ predicate_list.clone)
41
+ end
42
+
43
+ def set(var,plan)
44
+ ThetaJoin.new(
45
+ left.set(var,plan),
46
+ right.set(var,plan),
47
+ predicate_list.clone)
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,2 @@
1
+ require 'locomotive/relational_algebra/operators/projections/attach'
2
+ require 'locomotive/relational_algebra/operators/projections/projection'
@@ -0,0 +1,67 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class AttachItem
6
+ include Locomotive::XML
7
+ def_node :column
8
+
9
+ attr_accessor :attribute
10
+ def_sig :attribute=, ConstAttribute
11
+ attr_accessor :atom
12
+ def_sig :atom=, RAtomic
13
+
14
+ def initialize(attr, atom)
15
+ self.attribute,
16
+ self.atom = attr, atom
17
+ end
18
+
19
+ def to_xml
20
+ column :name => attribute.to_xml,
21
+ :new => true do
22
+ atom.to_xml
23
+ end
24
+ end
25
+
26
+ def clone
27
+ AttachItem.new(attribute, atom)
28
+ end
29
+ end
30
+
31
+
32
+
33
+ class Attach < Unary
34
+ attr_accessor :item
35
+ def_sig :item=, AttachItem
36
+
37
+ def initialize(op, item)
38
+ self.item = item
39
+ super(op)
40
+ end
41
+
42
+ def child=(op)
43
+ self.schema = op.schema +
44
+ Schema.new({ item.attribute => [item.atom.type] })
45
+ super(op)
46
+ end
47
+
48
+ def xml_content
49
+ content do
50
+ item.to_xml
51
+ end
52
+ end
53
+
54
+ def clone
55
+ Attach.new(child.clone, item.clone)
56
+ end
57
+
58
+ def set(var, plan)
59
+ Attach.new(
60
+ child.set(var,plan),
61
+ item.clone)
62
+ end
63
+ end
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,106 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class ProjectList
6
+ include Locomotive::XML
7
+ def_node :column
8
+
9
+ private
10
+
11
+ attr_accessor :project_list
12
+ def_sig :project_list=, { GenericAttribute => [ConstAttribute] }
13
+
14
+ public
15
+ delegate :[],
16
+ :to_a,
17
+ :to => :project_list
18
+
19
+ def initialize(hash)
20
+ self.project_list = hash
21
+ end
22
+
23
+ def old_items
24
+ project_list.keys
25
+ end
26
+
27
+ def new_items
28
+ project_list.values.flatten
29
+ end
30
+
31
+ def to_xml
32
+ project_list.collect do |old,news|
33
+ news.collect do |new|
34
+ column :name => new.to_xml,
35
+ :old_name => old.to_xml,
36
+ :new => new != old
37
+ end.join
38
+ end.join
39
+ end
40
+
41
+ def clone
42
+ ProjectList.new( project_list.clone )
43
+ end
44
+ end
45
+
46
+ class Project < Unary
47
+ private
48
+
49
+ attr_reader :proj_list
50
+
51
+ def to_project_list(list)
52
+ case
53
+ when Hash === list then
54
+ ProjectList.new(list)
55
+ when Array === list then
56
+ ProjectList.new(list.collect do |item|
57
+ [item, [item]]
58
+ end.to_hash)
59
+ when ProjectList === list then
60
+ list
61
+ else raise ArgumentError,
62
+ "list does not seem to be a projectlist"
63
+ end
64
+ end
65
+
66
+ public
67
+
68
+ def initialize(op, proj_list)
69
+ @proj_list = to_project_list proj_list
70
+ super(op)
71
+ end
72
+
73
+ def child=(op)
74
+ unless op.schema.attributes?(proj_list.old_items)
75
+ raise CorruptedSchema,
76
+ "Schema #{op.schema.attributes} does not " \
77
+ "contain all attributes of #{proj_list.old_items}."
78
+ end
79
+ proj_list.old_items.each do |old|
80
+ proj_list[old].each do |new|
81
+ schema[new] = op.schema[old]
82
+ end
83
+ end
84
+ super(op)
85
+ end
86
+
87
+ def xml_content
88
+ content do
89
+ proj_list.to_xml
90
+ end
91
+ end
92
+
93
+ def clone
94
+ Project.new(child.clone, proj_list.clone)
95
+ end
96
+
97
+ def set(var,plan)
98
+ Project.new(
99
+ child.set(var,plan),
100
+ proj_list.clone)
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ end
@@ -0,0 +1,6 @@
1
+ require 'locomotive/relational_algebra/operators/ranking/rank_lists'
2
+ require 'locomotive/relational_algebra/operators/ranking/basic_ranking'
3
+ require 'locomotive/relational_algebra/operators/ranking/rank'
4
+ require 'locomotive/relational_algebra/operators/ranking/row_id'
5
+ require 'locomotive/relational_algebra/operators/ranking/row_number'
6
+ require 'locomotive/relational_algebra/operators/ranking/row_rank'
@@ -0,0 +1,67 @@
1
+ module Locomotive
2
+
3
+ module RelationalAlgebra
4
+
5
+ class Numbering < Unary
6
+ private
7
+ def to_sort_by(sortby)
8
+ case
9
+ when SortList === sortby then
10
+ sortby
11
+ when Array === sortby then
12
+ SortList.new(
13
+ sortby.map { |si|
14
+ [si, Ascending.instance]
15
+ }.to_hash )
16
+ when Hash === sortby then
17
+ SortList.new(sortby)
18
+ when sortby.nil? then
19
+ SortList.new({})
20
+ end
21
+ end
22
+
23
+ public
24
+ attr_reader :res,
25
+ :sort_by
26
+
27
+
28
+ def initialize(op, res, sortby)
29
+ @res = res
30
+ @sort_by = to_sort_by sortby
31
+ super(op)
32
+ end
33
+
34
+ def child=(op)
35
+ unless op.schema.attributes?(sort_by.attributes)
36
+ raise CorruptedSchema,
37
+ "Schema #{op.schema.attributes} does not " \
38
+ "contain all attributes of #{sort_by.attributes}."
39
+ end
40
+ self.schema = op.schema + Schema.new({ @res => [RNat.instance] })
41
+ super(op)
42
+ end
43
+
44
+ def xml_content
45
+ content do
46
+ [column( :name => res.to_xml, :new => true),
47
+ sort_by.to_xml].join
48
+ end
49
+ end
50
+
51
+ def clone
52
+ self.class.new(child.clone,
53
+ res.clone,
54
+ sort_by.clone)
55
+ end
56
+
57
+ def set(var, plan)
58
+ self.class.new(
59
+ child.set(var,plan),
60
+ res.clone,
61
+ sort_by.clone)
62
+ end
63
+ end
64
+
65
+ end
66
+
67
+ end