alf-core 0.13.1 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. data/CHANGELOG.md +77 -0
  2. data/examples/operators/page.alf +4 -0
  3. data/lib/alf-adapter-fs/alf/adapter/folder.rb +4 -0
  4. data/lib/alf-adapter-fs/alf/adapter/folder/connection.rb +3 -2
  5. data/lib/alf-adapter/alf/adapter/connection.rb +1 -1
  6. data/lib/alf-algebra/alf/algebra/operand.rb +2 -2
  7. data/lib/alf-algebra/alf/algebra/operand/fake.rb +3 -3
  8. data/lib/alf-algebra/alf/algebra/operand/named.rb +1 -1
  9. data/lib/alf-algebra/alf/algebra/operand/proxy.rb +14 -0
  10. data/lib/alf-algebra/alf/algebra/operator.rb +4 -1
  11. data/lib/alf-algebra/alf/algebra/operator/clip.rb +7 -4
  12. data/lib/alf-algebra/alf/algebra/operator/frame.rb +22 -0
  13. data/lib/alf-algebra/alf/algebra/operator/hierarchize.rb +22 -0
  14. data/lib/alf-algebra/alf/algebra/operator/page.rb +22 -0
  15. data/lib/alf-algebra/alf/algebra/operator/project.rb +4 -9
  16. data/lib/alf-algebra/alf/algebra/operator/rank.rb +2 -2
  17. data/lib/alf-algebra/alf/algebra/support.rb +2 -0
  18. data/lib/alf-algebra/alf/algebra/support/signature.rb +2 -1
  19. data/lib/alf-algebra/alf/algebra/support/with_ordering.rb +11 -0
  20. data/lib/alf-core/ext/domain/heading_based.rb +16 -4
  21. data/lib/alf-core/facade.rb +5 -1
  22. data/lib/alf-core/version.rb +2 -2
  23. data/lib/alf-database/alf/database/connection.rb +2 -2
  24. data/lib/alf-database/alf/database/options.rb +0 -3
  25. data/lib/alf-engine/alf/engine.rb +4 -1
  26. data/lib/alf-engine/alf/engine/aggregate.rb +2 -1
  27. data/lib/alf-engine/alf/engine/autonum.rb +2 -1
  28. data/lib/alf-engine/alf/engine/clip.rb +2 -1
  29. data/lib/alf-engine/alf/engine/coerce.rb +2 -1
  30. data/lib/alf-engine/alf/engine/cog.rb +23 -0
  31. data/lib/alf-engine/alf/engine/compact.rb +3 -2
  32. data/lib/alf-engine/alf/engine/compact/set.rb +2 -1
  33. data/lib/alf-engine/alf/engine/compact/uniq.rb +2 -1
  34. data/lib/alf-engine/alf/engine/compiler.rb +60 -33
  35. data/lib/alf-engine/alf/engine/concat.rb +2 -1
  36. data/lib/alf-engine/alf/engine/defaults.rb +2 -1
  37. data/lib/alf-engine/alf/engine/filter.rb +4 -2
  38. data/lib/alf-engine/alf/engine/generator.rb +2 -1
  39. data/lib/alf-engine/alf/engine/group/hash.rb +4 -3
  40. data/lib/alf-engine/alf/engine/hierarchize.rb +87 -0
  41. data/lib/alf-engine/alf/engine/infer_heading.rb +2 -1
  42. data/lib/alf-engine/alf/engine/join.rb +5 -1
  43. data/lib/alf-engine/alf/engine/join/hash.rb +9 -2
  44. data/lib/alf-engine/alf/engine/leaf.rb +6 -1
  45. data/lib/alf-engine/alf/engine/materialize/array.rb +2 -1
  46. data/lib/alf-engine/alf/engine/materialize/hash.rb +2 -1
  47. data/lib/alf-engine/alf/engine/quota/cesure.rb +2 -1
  48. data/lib/alf-engine/alf/engine/rank/cesure.rb +2 -1
  49. data/lib/alf-engine/alf/engine/rename.rb +2 -1
  50. data/lib/alf-engine/alf/engine/semi/hash.rb +7 -1
  51. data/lib/alf-engine/alf/engine/set_attr.rb +4 -3
  52. data/lib/alf-engine/alf/engine/sort.rb +3 -2
  53. data/lib/alf-engine/alf/engine/sort/in_memory.rb +2 -1
  54. data/lib/alf-engine/alf/engine/summarize/cesure.rb +2 -1
  55. data/lib/alf-engine/alf/engine/summarize/hash.rb +3 -2
  56. data/lib/alf-engine/alf/engine/{cesure.rb → support/cesure.rb} +0 -0
  57. data/lib/alf-engine/alf/engine/take.rb +53 -0
  58. data/lib/alf-engine/alf/engine/to_array.rb +6 -5
  59. data/lib/alf-engine/alf/engine/type_safe.rb +2 -1
  60. data/lib/alf-engine/alf/engine/ungroup.rb +2 -1
  61. data/lib/alf-engine/alf/engine/unwrap.rb +2 -1
  62. data/lib/alf-engine/alf/engine/wrap.rb +2 -1
  63. data/lib/alf-io/alf/reader.rb +1 -1
  64. data/lib/alf-io/alf/reader/ruby.rb +23 -0
  65. data/lib/alf-io/alf/renderer/text.rb +22 -9
  66. data/lib/alf-lang/alf/lang/lispy.rb +41 -29
  67. data/lib/alf-lang/alf/lang/oo/algebra_methods.rb +17 -1
  68. data/lib/alf-optimizer/alf/optimizer/restrict.rb +7 -1
  69. data/lib/alf-predicate/alf/predicate.rb +11 -2
  70. data/lib/alf-relation/alf/relation.rb +21 -1
  71. data/lib/alf-relvar/alf/relvar.rb +21 -1
  72. data/lib/alf-relvar/alf/relvar/base.rb +11 -6
  73. data/lib/alf-relvar/alf/relvar/fake.rb +3 -2
  74. data/lib/alf-relvar/alf/relvar/virtual.rb +1 -2
  75. data/lib/alf-support/alf/support/bindable.rb +1 -1
  76. data/lib/alf-support/alf/support/config.rb +91 -18
  77. data/lib/alf-support/alf/support/tuple_scope.rb +8 -4
  78. data/lib/alf-types/alf/types.rb +2 -0
  79. data/lib/alf-types/alf/types/attr_list.rb +3 -1
  80. data/lib/alf-types/alf/types/heading.rb +4 -1
  81. data/lib/alf-types/alf/types/keys.rb +0 -4
  82. data/lib/alf-types/alf/types/ordering.rb +104 -18
  83. data/lib/alf-types/alf/types/selection.rb +18 -0
  84. data/lib/alf-types/alf/types/selector.rb +59 -0
  85. data/lib/alf-types/alf/types/tuple_computation.rb +2 -2
  86. data/lib/alf-types/alf/types/tuple_expression.rb +18 -2
  87. data/lib/alf-viewpoint/alf/viewpoint.rb +23 -8
  88. data/lib/alf-viewpoint/alf/viewpoint/metadata.rb +73 -0
  89. data/spec/integration/relation/test_extend.rb +20 -0
  90. data/spec/integration/relation/test_to_array.rb +1 -1
  91. data/spec/integration/test_examples.rb +18 -2
  92. data/spec/shared/a_cog.rb +36 -0
  93. data/spec/spec_helper.rb +8 -0
  94. data/spec/unit/alf-algebra/operand/test_proxy.rb +54 -0
  95. data/spec/unit/alf-algebra/operator/hierarchize/test_heading.rb +23 -0
  96. data/spec/unit/alf-algebra/operator/project/test_key_preserving.rb +3 -0
  97. data/spec/unit/alf-algebra/operator/project/test_keys.rb +54 -17
  98. data/spec/unit/alf-algebra/operator/rank/test_keys.rb +2 -2
  99. data/spec/unit/alf-algebra/operator/restrict/test_keys.rb +2 -2
  100. data/spec/unit/alf-algebra/operator/test_page.rb +30 -0
  101. data/spec/unit/alf-algebra/support/test_relational.rb +3 -0
  102. data/spec/unit/alf-algebra/support/with_ordering/test_total_ordering.rb +61 -0
  103. data/spec/unit/alf-database/connection/test_reconnect.rb +1 -1
  104. data/spec/unit/alf-database/options/test_new.rb +4 -4
  105. data/spec/unit/alf-database/shared_examples/a_facade_on_database_options.rb +1 -2
  106. data/spec/unit/alf-engine/cog/test_to_cog.rb +14 -0
  107. data/spec/unit/alf-engine/compiler/test_on_frame.rb +70 -0
  108. data/spec/unit/alf-engine/compiler/test_on_hierarchize.rb +33 -0
  109. data/spec/unit/alf-engine/compiler/test_on_page.rb +128 -0
  110. data/spec/unit/alf-engine/compiler/test_unsupported.rb +48 -0
  111. data/spec/unit/alf-engine/hierarchize/test_renamer.rb +18 -0
  112. data/spec/unit/alf-engine/test_filter.rb +43 -11
  113. data/spec/unit/alf-engine/test_hierarchize.rb +71 -0
  114. data/spec/unit/alf-engine/test_set_attr.rb +13 -0
  115. data/spec/unit/alf-engine/test_take.rb +46 -0
  116. data/spec/unit/alf-engine/test_to_array.rb +45 -28
  117. data/spec/unit/alf-io/reader/{input.rb → input.rash} +0 -0
  118. data/spec/unit/alf-io/reader/input.ruby +4 -0
  119. data/spec/unit/alf-io/reader/test_rash.rb +1 -1
  120. data/spec/unit/alf-io/reader/test_ruby.rb +25 -0
  121. data/spec/unit/alf-predicate/predicate/test_evaluate.rb +51 -13
  122. data/spec/unit/alf-relation/relation/class/test_recursive_type.rb +42 -0
  123. data/spec/unit/alf-relation/relation/test_equality.rb +19 -1
  124. data/spec/unit/alf-relation/relation/test_relops.rb +14 -2
  125. data/spec/unit/alf-relation/relation/test_to_hash.rb +31 -0
  126. data/spec/unit/alf-relvar/base/test_connection.rb +8 -2
  127. data/spec/unit/alf-relvar/base/test_delete.rb +2 -2
  128. data/spec/unit/alf-relvar/base/test_heading.rb +2 -2
  129. data/spec/unit/alf-relvar/base/test_insert.rb +3 -3
  130. data/spec/unit/alf-relvar/base/test_keys.rb +2 -2
  131. data/spec/unit/alf-relvar/base/test_lock.rb +2 -2
  132. data/spec/unit/alf-relvar/base/test_name.rb +3 -2
  133. data/spec/unit/alf-relvar/base/test_oo_lang.rb +2 -1
  134. data/spec/unit/alf-relvar/base/test_to_cog.rb +3 -3
  135. data/spec/unit/alf-relvar/base/test_to_lispy.rb +3 -2
  136. data/spec/unit/alf-relvar/base/test_to_relvar.rb +2 -1
  137. data/spec/unit/alf-relvar/base/test_to_s.rb +3 -2
  138. data/spec/unit/alf-relvar/base/test_update.rb +5 -4
  139. data/spec/unit/alf-relvar/virtual/test_connection.rb +3 -2
  140. data/spec/unit/alf-relvar/virtual/test_delete.rb +3 -4
  141. data/spec/unit/alf-relvar/virtual/test_expr.rb +3 -2
  142. data/spec/unit/alf-relvar/virtual/test_heading.rb +5 -4
  143. data/spec/unit/alf-relvar/virtual/test_insert.rb +2 -2
  144. data/spec/unit/alf-relvar/virtual/test_keys.rb +5 -4
  145. data/spec/unit/alf-relvar/virtual/test_oo_lang.rb +7 -2
  146. data/spec/unit/alf-relvar/virtual/test_to_cog.rb +4 -3
  147. data/spec/unit/alf-relvar/virtual/test_to_lispy.rb +3 -6
  148. data/spec/unit/alf-relvar/virtual/test_to_relvar.rb +2 -1
  149. data/spec/unit/alf-relvar/virtual/test_to_s.rb +3 -3
  150. data/spec/unit/alf-relvar/virtual/test_update.rb +4 -5
  151. data/spec/unit/alf-support/config/test_dup.rb +58 -0
  152. data/spec/unit/alf-support/config/test_freeze.rb +37 -0
  153. data/spec/unit/alf-support/config/test_hash_get.rb +41 -0
  154. data/spec/unit/alf-support/config/test_hash_set.rb +61 -0
  155. data/spec/unit/alf-support/config/test_new.rb +10 -0
  156. data/spec/unit/alf-support/config/test_option.rb +15 -0
  157. data/spec/unit/alf-support/config/test_option_setter.rb +72 -0
  158. data/spec/unit/alf-support/config/test_options.rb +22 -0
  159. data/spec/unit/alf-support/tuple_scope/test_hash_get.rb +20 -0
  160. data/spec/unit/alf-support/tuple_scope/test_respond_to.rb +6 -0
  161. data/spec/unit/alf-support/tuple_scope/test_to_s.rb +22 -0
  162. data/spec/unit/alf-types/keys/test_select.rb +57 -8
  163. data/spec/unit/alf-types/ordering/test_coerce.rb +46 -35
  164. data/spec/unit/alf-types/ordering/test_dive.rb +41 -0
  165. data/spec/unit/alf-types/ordering/test_hash_get.rb +20 -0
  166. data/spec/unit/alf-types/ordering/test_merge.rb +61 -0
  167. data/spec/unit/alf-types/ordering/test_reverse.rb +40 -0
  168. data/spec/unit/alf-types/ordering/test_selectors.rb +24 -0
  169. data/spec/unit/alf-types/ordering/test_sorter.rb +62 -8
  170. data/spec/unit/alf-types/ordering/test_to_attr_list.rb +16 -4
  171. data/spec/unit/alf-types/ordering/test_total.rb +56 -0
  172. data/spec/unit/alf-types/selection/test_coerce.rb +38 -0
  173. data/spec/unit/alf-types/selection/test_select.rb +34 -0
  174. data/spec/unit/alf-types/selector/test_coerce.rb +36 -0
  175. data/spec/unit/alf-types/selector/test_composite_q.rb +20 -0
  176. data/spec/unit/alf-types/selector/test_dive.rb +32 -0
  177. data/spec/unit/alf-types/selector/test_select.rb +40 -0
  178. data/spec/unit/alf-types/selector/test_simple_q.rb +20 -0
  179. data/spec/unit/alf-types/selector/test_to_a.rb +20 -0
  180. data/spec/unit/alf-types/selector/test_to_lispy.rb +20 -0
  181. data/spec/unit/alf-types/selector/test_to_ruby_literal.rb +20 -0
  182. data/spec/unit/alf-types/tuple_expression/test_evaluate.rb +38 -0
  183. data/spec/unit/alf-viewpoint/metadata/test_add_members.rb +38 -0
  184. data/spec/unit/alf-viewpoint/metadata/test_all_members.rb +33 -0
  185. data/spec/unit/alf-viewpoint/metadata/test_depends.rb +50 -0
  186. data/spec/unit/alf-viewpoint/metadata/test_dup.rb +32 -0
  187. data/spec/unit/alf-viewpoint/metadata/test_expand.rb +31 -0
  188. data/spec/unit/alf-viewpoint/metadata/test_expects.rb +38 -0
  189. data/spec/unit/alf-viewpoint/metadata/test_initialize.rb +40 -0
  190. data/spec/unit/alf-viewpoint/metadata/test_to_module.rb +37 -0
  191. data/spec/unit/alf-viewpoint/test_depends.rb +18 -0
  192. data/spec/unit/alf-viewpoint/test_expects.rb +18 -0
  193. data/spec/unit/alf-viewpoint/test_members.rb +33 -0
  194. data/spec/unit/alf-viewpoint/test_metadata.rb +27 -0
  195. data/spec/unit/alf-viewpoint/test_native_const.rb +6 -0
  196. metadata +125 -14
  197. data/spec/unit/alf-database/options/test_default_viewpoint.rb +0 -25
  198. data/spec/unit/alf-types/keys/test_compact.rb +0 -21
  199. data/spec/unit/alf-types/ordering/test_plus.rb +0 -27
  200. data/spec/unit/alf-viewpoint/test_namespace.rb +0 -23
@@ -5,9 +5,25 @@ module Alf
5
5
 
6
6
  def self.def_operator_method(name, clazz)
7
7
  define_method(name) do |*args|
8
+ # add self operands at begining of args
8
9
  args.unshift(_self_operand)
10
+
11
+ # split operands vs. arguments
9
12
  operands, arguments = args[0...clazz.arity], args[clazz.arity..-1]
10
- _operator_output clazz.new(operands, *arguments)
13
+
14
+ # build the new expression
15
+ expr = clazz.new(operands, *arguments)
16
+
17
+ # bind it if operands were bound
18
+ conns = operands.map(&:connection).uniq
19
+ if conns.size == 1
20
+ expr.connection = conns.first
21
+ elsif conns.size > 1
22
+ raise NotSupportedError, "Multiple connections unsupported"
23
+ end
24
+
25
+ # let the abstraction have a chance to of decorating it
26
+ _operator_output(expr)
11
27
  end
12
28
  end
13
29
 
@@ -45,6 +45,8 @@ module Alf
45
45
  end
46
46
  alias :on_generator :on_unoptimizable
47
47
  alias :on_infer_heading :on_unoptimizable
48
+ alias :on_page :on_unoptimizable
49
+ alias :on_frame :on_unoptimizable
48
50
 
49
51
  def on_binary_optimizable(expr, predicate)
50
52
  binary_split(expr, predicate)
@@ -95,7 +97,11 @@ module Alf
95
97
  end
96
98
 
97
99
  def on_restrict(expr, predicate)
98
- apply(expr.operand, predicate & expr.predicate)
100
+ if predicate.native? or expr.predicate.native?
101
+ restrict(expr, predicate)
102
+ else
103
+ apply(expr.operand, predicate & expr.predicate)
104
+ end
99
105
  end
100
106
 
101
107
  def on_summarize(expr, predicate)
@@ -37,6 +37,10 @@ module Alf
37
37
 
38
38
  end
39
39
 
40
+ def native?
41
+ Native===expr
42
+ end
43
+
40
44
  def tautology?
41
45
  expr.tautology?
42
46
  end
@@ -73,8 +77,13 @@ module Alf
73
77
  Predicate.new(expr.rename(renaming))
74
78
  end
75
79
 
76
- def evaluate(scope)
77
- scope.instance_exec(&to_proc)
80
+ def evaluate(scope, connection = nil)
81
+ proc = to_proc
82
+ if proc.arity == 1
83
+ proc.call(scope)
84
+ else
85
+ scope.instance_exec(&to_proc)
86
+ end
78
87
  end
79
88
 
80
89
  def and_split(attr_list)
@@ -47,6 +47,10 @@ module Alf
47
47
  end
48
48
  end
49
49
 
50
+ def self.empty
51
+ @empty ||= new([].to_set)
52
+ end
53
+
50
54
  def check_internal_representation!
51
55
  error = lambda{|msg| raise TypeError, msg }
52
56
  error["Set expected"] unless reused_instance.is_a?(Set)
@@ -91,7 +95,23 @@ module Alf
91
95
 
92
96
  # Returns an engine Cog
93
97
  def to_cog
94
- Engine::Leaf.new(self)
98
+ Engine::Leaf.new(self, self)
99
+ end
100
+
101
+ def to_hash(from, to=nil)
102
+ if from.is_a?(Hash) and to.nil?
103
+ raise ArgumentError "Hash of size 1 expected. " unless from.size==1
104
+ to_hash(from.keys.first, from.values.first)
105
+ else
106
+ each.each_with_object({}) do |tuple, hash|
107
+ key, value = tuple[from], tuple[to]
108
+ if hash.has_key?(key) and hash[key] != value
109
+ raise "Key expected for `#{from}`, divergence found on `#{key}`"
110
+ else
111
+ hash[key] = value
112
+ end
113
+ end
114
+ end
95
115
  end
96
116
 
97
117
  # Returns a textual representation of this relation
@@ -3,6 +3,26 @@ module Alf
3
3
  include Algebra::Operand
4
4
  include Lang::ObjectOriented
5
5
 
6
+ def connection
7
+ expr.connection
8
+ end
9
+
10
+ def connection=(conn)
11
+ expr.connection = conn
12
+ end
13
+
14
+ def connection!
15
+ expr.connection!
16
+ end
17
+
18
+ def bind(connection)
19
+ expr.bind(connection)
20
+ end
21
+
22
+ def bound?
23
+ expr.bound?
24
+ end
25
+
6
26
  def type
7
27
  @type ||= Relation[heading]
8
28
  end
@@ -76,7 +96,7 @@ module Alf
76
96
  private
77
97
 
78
98
  def _operator_output(expr)
79
- Relvar::Virtual.new(expr, connection)
99
+ Relvar::Virtual.new(expr)
80
100
  end
81
101
 
82
102
  end # module Relvar
@@ -3,12 +3,17 @@ module Alf
3
3
  class Base
4
4
  include Relvar
5
5
 
6
- def initialize(name, connection = nil)
7
- raise unless name.is_a?(Symbol)
8
- @name = name
9
- @connection = connection
6
+ def initialize(expr)
7
+ raise "Named operand expected, got `#{expr}`" unless expr.is_a?(Algebra::Operand::Named)
8
+ @expr = expr
9
+ end
10
+ attr_reader :expr
11
+
12
+ ### Relvar contract
13
+
14
+ def name
15
+ expr.name
10
16
  end
11
- attr_reader :name
12
17
 
13
18
  ### Static analysis & inference
14
19
 
@@ -42,7 +47,7 @@ module Alf
42
47
  ### to_xxx
43
48
 
44
49
  def to_cog
45
- connection!.cog(name)
50
+ connection!.cog(name, self)
46
51
  end
47
52
 
48
53
  def to_lispy
@@ -3,10 +3,11 @@ module Alf
3
3
  class Fake
4
4
  include Relvar
5
5
 
6
- def initialize(heading)
6
+ def initialize(expr, heading)
7
+ @expr = expr
7
8
  @heading = Heading.coerce(heading)
8
9
  end
9
- attr_reader :heading
10
+ attr_reader :expr, :heading
10
11
 
11
12
  def insert(tuples)
12
13
  @inserted_tuples = tuples.to_a
@@ -3,9 +3,8 @@ module Alf
3
3
  class Virtual
4
4
  include Relvar
5
5
 
6
- def initialize(expr, connection = nil)
6
+ def initialize(expr)
7
7
  @expr = expr
8
- @connection = connection
9
8
  end
10
9
  attr_reader :expr
11
10
 
@@ -17,7 +17,7 @@ module Alf
17
17
  end
18
18
 
19
19
  def connection!
20
- raise UnboundError unless bound?
20
+ raise UnboundError, "Not bound `#{self}`" unless bound?
21
21
  @connection
22
22
  end
23
23
 
@@ -2,36 +2,88 @@ module Alf
2
2
  module Support
3
3
  class Config
4
4
 
5
- def self.delegation_methods
6
- public_instance_methods(false).reject{|m| m.to_s =~ /=$/ }
7
- end
5
+ module ClassMethods
6
+
7
+ def options
8
+ @options ||= []
9
+ end
10
+
11
+ def each_option(&bl)
12
+ options.each(&bl)
13
+ end
14
+
15
+ def delegation_methods
16
+ public_instance_methods(false).reject{|m| m.to_s =~ /=$/ }
17
+ end
8
18
 
9
- def self.helpers(to = :config)
10
- meths = delegation_methods
11
- Module.new do
12
- meths.each do |m|
13
- define_method(m){|*args, &bl| self.send(to).send(m, *args, &bl) }
19
+ def helpers(to = :config)
20
+ meths = delegation_methods
21
+ Module.new do
22
+ meths.each do |m|
23
+ define_method(m){|*args, &bl| self.send(to).send(m, *args, &bl) }
24
+ end
14
25
  end
15
26
  end
16
- end
17
27
 
18
- def self.option(name, domain, default_value)
19
- getter_name = domain == Boolean ? :"#{name}?" : :"#{name}"
20
- setter_name = :"#{name}="
21
- ivar_name = :"@#{name}"
22
- define_method(getter_name) do
23
- return instance_variable_get(ivar_name) if instance_variable_defined?(ivar_name)
24
- default_value
28
+ def option(name, domain, default_value)
29
+ options << [name, domain, default_value]
30
+ getter_name = self.getter_name(name, domain)
31
+ setter_name = self.setter_name(name)
32
+ ivar_name = self.ivar_name(name)
33
+ define_method(getter_name) do
34
+ value = instance_variable_defined?(ivar_name) \
35
+ ? instance_variable_get(ivar_name)
36
+ : default_value
37
+ value.is_a?(Proc) && domain != Proc ? instance_exec(&value) : value
38
+ end
39
+ define_method(setter_name) do |val|
40
+ val = val.is_a?(Proc) ? val : Support.coerce(val, domain)
41
+ instance_variable_set(ivar_name, val)
42
+ end
43
+ end
44
+
45
+ def getter_name(option, domain)
46
+ domain == Boolean ? :"#{option}?" : :"#{option}"
47
+ end
48
+
49
+ def setter_name(option)
50
+ :"#{option}="
51
+ end
52
+
53
+ def ivar_name(option)
54
+ :"@#{option}"
25
55
  end
26
- define_method(setter_name) do |val|
27
- instance_variable_set(ivar_name, Support.coerce(val, domain))
56
+
57
+ def option_get(conf, name)
58
+ option = options.find{|(n,_,_)| n == name }
59
+ raise ConfigError, "No such option `#{name}`" unless option
60
+ conf.send(getter_name(option[0], option[1]))
61
+ end
62
+
63
+ def option_set(conf, name, value)
64
+ option = options.find{|(n,_,_)| n == name }
65
+ raise ConfigError, "No such option `#{name}`" unless option
66
+ conf.send(setter_name(option[0]), value)
28
67
  end
68
+
29
69
  end
70
+ extend(ClassMethods)
30
71
  private_class_method :option
31
72
 
32
73
  # Creates a default options instance
33
74
  def initialize(h = {})
34
75
  install_options_from_hash(h)
76
+ yield(self) if block_given?
77
+ end
78
+
79
+ # Returns the value of an option
80
+ def [](option)
81
+ self.class.option_get(self, option)
82
+ end
83
+
84
+ # Sets the value of an option
85
+ def []=(option, value)
86
+ self.class.option_set(self, option, value)
35
87
  end
36
88
 
37
89
  # Merge with another options from a hash
@@ -39,6 +91,27 @@ module Alf
39
91
  dup.install_options_from_hash(h)
40
92
  end
41
93
 
94
+ # Duplicates this configuration as well as all option values
95
+ def dup
96
+ super.tap do |c|
97
+ self.class.each_option do |name,_,default|
98
+ ivar_name = self.class.ivar_name(name)
99
+ if instance_variable_defined?(ivar_name) || !default.is_a?(Proc)
100
+ c[name] = (self[name].dup rescue self[name])
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ # Freeze this configuration as well as all option values
107
+ def freeze
108
+ super
109
+ self.class.each_option do |name,_,_|
110
+ self[name].freeze
111
+ end
112
+ self
113
+ end
114
+
42
115
  protected
43
116
 
44
117
  def install_options_from_hash(h)
@@ -51,12 +51,16 @@ module Alf
51
51
  self
52
52
  end
53
53
 
54
- def query
55
- @parent.query(yield)
54
+ def [](what)
55
+ @tuple[what]
56
56
  end
57
57
 
58
- def tuple_extract
59
- @parent.tuple_extract(yield)
58
+ def to_s
59
+ @tuple.to_s
60
+ end
61
+
62
+ def inspect
63
+ @tuple.inspect
60
64
  end
61
65
 
62
66
  end # module OwnMethods
@@ -32,6 +32,8 @@ module Alf
32
32
  require_relative 'types/boolean'
33
33
  require_relative 'types/size'
34
34
  require_relative 'types/attr_name'
35
+ require_relative 'types/selector'
36
+ require_relative 'types/selection'
35
37
  require_relative 'types/attr_list'
36
38
  require_relative 'types/heading'
37
39
  require_relative 'types/ordering'
@@ -83,7 +83,7 @@ module Alf
83
83
  # @return [Integer] 0 if same set of attribute names, -1 if self is a subset of
84
84
  # other, 1 if a superset, nil otherwise.
85
85
  def <=>(other)
86
- return nil unless other.is_a?(AttrList)
86
+ return nil unless other.respond_to?(:to_set)
87
87
  s1, s2 = to_set, other.to_set
88
88
  if s1==s2 then 0
89
89
  elsif s1.subset?(s2) then -1
@@ -105,12 +105,14 @@ module Alf
105
105
  c = set_compare(other)
106
106
  c and (c >= (proper ? 1 : 0))
107
107
  end
108
+ alias :supersetOf? :superset?
108
109
 
109
110
  # Returns true if self is a (proper) subset of `other`
110
111
  def subset?(other, proper = false)
111
112
  c = set_compare(other)
112
113
  c and (c <= (proper ? -1 : 0))
113
114
  end
115
+ alias :subsetOf? :subset?
114
116
 
115
117
  # Returns true if self intersects another attribute list
116
118
  def intersect?(other)
@@ -136,7 +136,10 @@ module Alf
136
136
  # nil if not comparable.
137
137
  def <=>(other)
138
138
  return nil unless other.is_a?(Heading) && to_attr_list==other.to_attr_list
139
- comparisons = attributes.keys.map{|k| self[k] <=> other[k] }.reject{|x| x == 0}.uniq
139
+ comparisons = attributes.keys
140
+ .map{|k| self[k] <=> other[k] }
141
+ .reject{|x| x == 0}
142
+ .uniq
140
143
  case comparisons.size
141
144
  when 0 then 0
142
145
  when 1 then comparisons.first
@@ -23,10 +23,6 @@ module Alf
23
23
  map{|k| renaming.rename_attr_list(k) }
24
24
  end
25
25
 
26
- def compact
27
- reject{|k| k.empty? }
28
- end
29
-
30
26
  def to_ruby_literal
31
27
  "Alf::Keys[" << reused_instance.map{|k| Support.to_ruby_literal(k.to_a) }.join(',') << "]"
32
28
  end
@@ -6,20 +6,27 @@ module Alf
6
6
  class Ordering
7
7
  extend Domain::Reuse.new(Array)
8
8
 
9
+ def self.new(array)
10
+ super(array.map{|(x,y)| [Selector.coerce(x), y.to_sym] })
11
+ end
12
+
13
+ ArrayOfArray = ->(x){
14
+ x.is_a?(Array) && x.all?{|y| y.is_a?(Array)}
15
+ }
16
+ ArrayWithDirections = ->(x){
17
+ x.each_with_index.all?{|elm,i| (i%2==0) || elm.to_s =~ /^asc|desc$/ }
18
+ }
19
+
9
20
  coercions do |c|
10
21
  c.delegate :to_ordering
22
+ c.coercion(ArrayOfArray){|arg,_|
23
+ Ordering.new(arg)
24
+ }
25
+ c.coercion(ArrayWithDirections){|arg,_|
26
+ Ordering.new(arg.each_slice(2).to_a)
27
+ }
11
28
  c.coercion(Array){|arg,_|
12
- if arg.all?{|a| a.is_a?(Array)}
13
- Ordering.new(arg)
14
- else
15
- symbolized = arg.map{|s| AttrName.coerce(s) }
16
- sliced = symbolized.each_slice(2)
17
- if sliced.all?{|a,o| [:asc,:desc].include?(o)}
18
- Ordering.new sliced.to_a
19
- else
20
- Ordering.new symbolized.map{|a| [a, :asc]}
21
- end
22
- end
29
+ Ordering.new(arg.map{|x| [x, :asc] })
23
30
  }
24
31
  end
25
32
 
@@ -37,6 +44,17 @@ module Alf
37
44
 
38
45
  reuse :to_a
39
46
 
47
+ # Returns the directions associated with an attribute name, nil
48
+ # if no such attribute.
49
+ #
50
+ # @param [Symbol] an attribute name.
51
+ # @return [Symbol] the associated direction or nil
52
+ def [](attribute)
53
+ attribute = Selector.coerce(attribute)
54
+ pair = reused_instance.find{|p| p.first == attribute }
55
+ pair && pair.last
56
+ end
57
+
40
58
  # Compares two tuples according to this ordering.
41
59
  #
42
60
  # Both t1 and t2 should have all attributes used by this ordering.
@@ -47,8 +65,9 @@ module Alf
47
65
  # @return [-1, 0 or 1] according to the classical ruby semantics of
48
66
  # `(t1 <=> t2)`
49
67
  def compare(t1, t2)
68
+ extract = proc{|t,x| Array(x).inject(t){|m,a| m[a]} }
50
69
  reused_instance.each do |atr, dir|
51
- x, y = t1[atr], t2[atr]
70
+ x, y = extract[t1,atr], extract[t2,atr]
52
71
  comp = x.respond_to?(:<=>) ? (x <=> y) : (x.to_s <=> y.to_s)
53
72
  comp *= -1 if dir == :desc
54
73
  return comp unless comp == 0
@@ -58,13 +77,77 @@ module Alf
58
77
 
59
78
  # Computes the union of this ordering with another one.
60
79
  #
61
- # The union is simply defined by extension of self with other's attributes
62
- # and directions.
80
+ # The union is simply defined by extension of self with other's
81
+ # attributes and directions. Duplicates are automatically removed.
82
+ #
83
+ # When a conflict arises (same attribute but not same direction),
84
+ # the block is yield with the attribute name, then `self`'s and `other`'s
85
+ # directions as arguments. The block is expected to return the direction
86
+ # to use to the attribute. A default block is provided that always favors
87
+ # the direction found in `other`.
63
88
  #
64
89
  # @param [Ordering] other another Ordering (coercions will apply)
65
90
  # @return [Ordering] the union ordering
66
- def +(other)
67
- Ordering.new(reused_instance + Ordering.coerce(other).reused_instance)
91
+ def merge(other, &bl)
92
+ bl ||= lambda{|attr,d1,d2| d2 }
93
+ other = Ordering.coerce(other)
94
+ attributes = self.selectors | other.selectors
95
+ directions = attributes.to_a.map{|a|
96
+ left, right = self[a], other[a]
97
+ direction = if left.nil? or right.nil?
98
+ left || right
99
+ elsif left == right
100
+ left
101
+ else
102
+ bl.call(a, left, right)
103
+ end
104
+ [a, direction]
105
+ }
106
+ Ordering.new(directions)
107
+ end
108
+ alias :+ :merge
109
+
110
+ # Reverse this ordering.
111
+ #
112
+ # @return [Ordering] another ordering where the direction of every
113
+ # attribute has been flipped (asc <-> desc)
114
+ def reverse
115
+ Ordering.new(reused_instance.map{|attr,dir|
116
+ [ attr, dir == :asc ? :desc : :asc ]
117
+ })
118
+ end
119
+
120
+ # Returns a total ordering given some key definitions.
121
+ #
122
+ # @return [Ordering]
123
+ def total(keys, &bl)
124
+ list = to_attr_list
125
+ if k = keys.to_a.find{|k| k.to_attr_list.subsetOf?(list) }
126
+ self
127
+ elsif k = keys.first
128
+ merge(k.to_ordering){|k,d1,d2| d1 }
129
+ elsif bl && (key = bl.call.to_attr_list)
130
+ merge(key.to_ordering){|k,d1,d2| d1 }
131
+ else
132
+ raise NotSupportedError, "Unable to find a total order (no key available)"
133
+ end
134
+ end
135
+
136
+ # Dives into a relation/tuple valued attribute `attr`.
137
+ #
138
+ # @return [Ordering] the sub-ordering to use for `attr`
139
+ def dive(attr)
140
+ attrs = reused_instance
141
+ .map {|(s,d)| [s.dive(attr), d] }
142
+ .reject{|(s,d)| s.nil? }
143
+ Ordering.new(attrs)
144
+ end
145
+
146
+ # Returns the list of selectors
147
+ #
148
+ # @return [Array[Selector]] the list of selectors
149
+ def selectors
150
+ reused_instance.map(&:first)
68
151
  end
69
152
 
70
153
  # Converts to an attribute list.
@@ -72,14 +155,14 @@ module Alf
72
155
  # @return [AttrList] a list of attribute names that participate to the
73
156
  # ordering
74
157
  def to_attr_list
75
- AttrList.new(reused_instance.map(&:first))
158
+ AttrList.new(selectors.map{|x| Array(x).first })
76
159
  end
77
160
 
78
161
  # Returns a lispy expression.
79
162
  #
80
163
  # @return [String] a lispy expression for this ordering
81
164
  def to_lispy
82
- Support.to_ruby_literal(to_a)
165
+ Support.to_ruby_literal(to_a.map{|(x,d)| [x.outcoerce, d]})
83
166
  end
84
167
 
85
168
  # Returns a ruby literal for this ordering.
@@ -88,8 +171,11 @@ module Alf
88
171
  def to_ruby_literal
89
172
  "Alf::Ordering[#{Support.to_ruby_literal(reused_instance)}]"
90
173
  end
174
+
175
+ alias :to_s :to_ruby_literal
91
176
  alias :inspect :to_ruby_literal
92
177
 
178
+ EMPTY = Ordering.new([])
93
179
  end # class Ordering
94
180
  end # module Types
95
181
  end # module Alf