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.
- data/CHANGELOG.md +77 -0
- data/examples/operators/page.alf +4 -0
- data/lib/alf-adapter-fs/alf/adapter/folder.rb +4 -0
- data/lib/alf-adapter-fs/alf/adapter/folder/connection.rb +3 -2
- data/lib/alf-adapter/alf/adapter/connection.rb +1 -1
- data/lib/alf-algebra/alf/algebra/operand.rb +2 -2
- data/lib/alf-algebra/alf/algebra/operand/fake.rb +3 -3
- data/lib/alf-algebra/alf/algebra/operand/named.rb +1 -1
- data/lib/alf-algebra/alf/algebra/operand/proxy.rb +14 -0
- data/lib/alf-algebra/alf/algebra/operator.rb +4 -1
- data/lib/alf-algebra/alf/algebra/operator/clip.rb +7 -4
- data/lib/alf-algebra/alf/algebra/operator/frame.rb +22 -0
- data/lib/alf-algebra/alf/algebra/operator/hierarchize.rb +22 -0
- data/lib/alf-algebra/alf/algebra/operator/page.rb +22 -0
- data/lib/alf-algebra/alf/algebra/operator/project.rb +4 -9
- data/lib/alf-algebra/alf/algebra/operator/rank.rb +2 -2
- data/lib/alf-algebra/alf/algebra/support.rb +2 -0
- data/lib/alf-algebra/alf/algebra/support/signature.rb +2 -1
- data/lib/alf-algebra/alf/algebra/support/with_ordering.rb +11 -0
- data/lib/alf-core/ext/domain/heading_based.rb +16 -4
- data/lib/alf-core/facade.rb +5 -1
- data/lib/alf-core/version.rb +2 -2
- data/lib/alf-database/alf/database/connection.rb +2 -2
- data/lib/alf-database/alf/database/options.rb +0 -3
- data/lib/alf-engine/alf/engine.rb +4 -1
- data/lib/alf-engine/alf/engine/aggregate.rb +2 -1
- data/lib/alf-engine/alf/engine/autonum.rb +2 -1
- data/lib/alf-engine/alf/engine/clip.rb +2 -1
- data/lib/alf-engine/alf/engine/coerce.rb +2 -1
- data/lib/alf-engine/alf/engine/cog.rb +23 -0
- data/lib/alf-engine/alf/engine/compact.rb +3 -2
- data/lib/alf-engine/alf/engine/compact/set.rb +2 -1
- data/lib/alf-engine/alf/engine/compact/uniq.rb +2 -1
- data/lib/alf-engine/alf/engine/compiler.rb +60 -33
- data/lib/alf-engine/alf/engine/concat.rb +2 -1
- data/lib/alf-engine/alf/engine/defaults.rb +2 -1
- data/lib/alf-engine/alf/engine/filter.rb +4 -2
- data/lib/alf-engine/alf/engine/generator.rb +2 -1
- data/lib/alf-engine/alf/engine/group/hash.rb +4 -3
- data/lib/alf-engine/alf/engine/hierarchize.rb +87 -0
- data/lib/alf-engine/alf/engine/infer_heading.rb +2 -1
- data/lib/alf-engine/alf/engine/join.rb +5 -1
- data/lib/alf-engine/alf/engine/join/hash.rb +9 -2
- data/lib/alf-engine/alf/engine/leaf.rb +6 -1
- data/lib/alf-engine/alf/engine/materialize/array.rb +2 -1
- data/lib/alf-engine/alf/engine/materialize/hash.rb +2 -1
- data/lib/alf-engine/alf/engine/quota/cesure.rb +2 -1
- data/lib/alf-engine/alf/engine/rank/cesure.rb +2 -1
- data/lib/alf-engine/alf/engine/rename.rb +2 -1
- data/lib/alf-engine/alf/engine/semi/hash.rb +7 -1
- data/lib/alf-engine/alf/engine/set_attr.rb +4 -3
- data/lib/alf-engine/alf/engine/sort.rb +3 -2
- data/lib/alf-engine/alf/engine/sort/in_memory.rb +2 -1
- data/lib/alf-engine/alf/engine/summarize/cesure.rb +2 -1
- data/lib/alf-engine/alf/engine/summarize/hash.rb +3 -2
- data/lib/alf-engine/alf/engine/{cesure.rb → support/cesure.rb} +0 -0
- data/lib/alf-engine/alf/engine/take.rb +53 -0
- data/lib/alf-engine/alf/engine/to_array.rb +6 -5
- data/lib/alf-engine/alf/engine/type_safe.rb +2 -1
- data/lib/alf-engine/alf/engine/ungroup.rb +2 -1
- data/lib/alf-engine/alf/engine/unwrap.rb +2 -1
- data/lib/alf-engine/alf/engine/wrap.rb +2 -1
- data/lib/alf-io/alf/reader.rb +1 -1
- data/lib/alf-io/alf/reader/ruby.rb +23 -0
- data/lib/alf-io/alf/renderer/text.rb +22 -9
- data/lib/alf-lang/alf/lang/lispy.rb +41 -29
- data/lib/alf-lang/alf/lang/oo/algebra_methods.rb +17 -1
- data/lib/alf-optimizer/alf/optimizer/restrict.rb +7 -1
- data/lib/alf-predicate/alf/predicate.rb +11 -2
- data/lib/alf-relation/alf/relation.rb +21 -1
- data/lib/alf-relvar/alf/relvar.rb +21 -1
- data/lib/alf-relvar/alf/relvar/base.rb +11 -6
- data/lib/alf-relvar/alf/relvar/fake.rb +3 -2
- data/lib/alf-relvar/alf/relvar/virtual.rb +1 -2
- data/lib/alf-support/alf/support/bindable.rb +1 -1
- data/lib/alf-support/alf/support/config.rb +91 -18
- data/lib/alf-support/alf/support/tuple_scope.rb +8 -4
- data/lib/alf-types/alf/types.rb +2 -0
- data/lib/alf-types/alf/types/attr_list.rb +3 -1
- data/lib/alf-types/alf/types/heading.rb +4 -1
- data/lib/alf-types/alf/types/keys.rb +0 -4
- data/lib/alf-types/alf/types/ordering.rb +104 -18
- data/lib/alf-types/alf/types/selection.rb +18 -0
- data/lib/alf-types/alf/types/selector.rb +59 -0
- data/lib/alf-types/alf/types/tuple_computation.rb +2 -2
- data/lib/alf-types/alf/types/tuple_expression.rb +18 -2
- data/lib/alf-viewpoint/alf/viewpoint.rb +23 -8
- data/lib/alf-viewpoint/alf/viewpoint/metadata.rb +73 -0
- data/spec/integration/relation/test_extend.rb +20 -0
- data/spec/integration/relation/test_to_array.rb +1 -1
- data/spec/integration/test_examples.rb +18 -2
- data/spec/shared/a_cog.rb +36 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/unit/alf-algebra/operand/test_proxy.rb +54 -0
- data/spec/unit/alf-algebra/operator/hierarchize/test_heading.rb +23 -0
- data/spec/unit/alf-algebra/operator/project/test_key_preserving.rb +3 -0
- data/spec/unit/alf-algebra/operator/project/test_keys.rb +54 -17
- data/spec/unit/alf-algebra/operator/rank/test_keys.rb +2 -2
- data/spec/unit/alf-algebra/operator/restrict/test_keys.rb +2 -2
- data/spec/unit/alf-algebra/operator/test_page.rb +30 -0
- data/spec/unit/alf-algebra/support/test_relational.rb +3 -0
- data/spec/unit/alf-algebra/support/with_ordering/test_total_ordering.rb +61 -0
- data/spec/unit/alf-database/connection/test_reconnect.rb +1 -1
- data/spec/unit/alf-database/options/test_new.rb +4 -4
- data/spec/unit/alf-database/shared_examples/a_facade_on_database_options.rb +1 -2
- data/spec/unit/alf-engine/cog/test_to_cog.rb +14 -0
- data/spec/unit/alf-engine/compiler/test_on_frame.rb +70 -0
- data/spec/unit/alf-engine/compiler/test_on_hierarchize.rb +33 -0
- data/spec/unit/alf-engine/compiler/test_on_page.rb +128 -0
- data/spec/unit/alf-engine/compiler/test_unsupported.rb +48 -0
- data/spec/unit/alf-engine/hierarchize/test_renamer.rb +18 -0
- data/spec/unit/alf-engine/test_filter.rb +43 -11
- data/spec/unit/alf-engine/test_hierarchize.rb +71 -0
- data/spec/unit/alf-engine/test_set_attr.rb +13 -0
- data/spec/unit/alf-engine/test_take.rb +46 -0
- data/spec/unit/alf-engine/test_to_array.rb +45 -28
- data/spec/unit/alf-io/reader/{input.rb → input.rash} +0 -0
- data/spec/unit/alf-io/reader/input.ruby +4 -0
- data/spec/unit/alf-io/reader/test_rash.rb +1 -1
- data/spec/unit/alf-io/reader/test_ruby.rb +25 -0
- data/spec/unit/alf-predicate/predicate/test_evaluate.rb +51 -13
- data/spec/unit/alf-relation/relation/class/test_recursive_type.rb +42 -0
- data/spec/unit/alf-relation/relation/test_equality.rb +19 -1
- data/spec/unit/alf-relation/relation/test_relops.rb +14 -2
- data/spec/unit/alf-relation/relation/test_to_hash.rb +31 -0
- data/spec/unit/alf-relvar/base/test_connection.rb +8 -2
- data/spec/unit/alf-relvar/base/test_delete.rb +2 -2
- data/spec/unit/alf-relvar/base/test_heading.rb +2 -2
- data/spec/unit/alf-relvar/base/test_insert.rb +3 -3
- data/spec/unit/alf-relvar/base/test_keys.rb +2 -2
- data/spec/unit/alf-relvar/base/test_lock.rb +2 -2
- data/spec/unit/alf-relvar/base/test_name.rb +3 -2
- data/spec/unit/alf-relvar/base/test_oo_lang.rb +2 -1
- data/spec/unit/alf-relvar/base/test_to_cog.rb +3 -3
- data/spec/unit/alf-relvar/base/test_to_lispy.rb +3 -2
- data/spec/unit/alf-relvar/base/test_to_relvar.rb +2 -1
- data/spec/unit/alf-relvar/base/test_to_s.rb +3 -2
- data/spec/unit/alf-relvar/base/test_update.rb +5 -4
- data/spec/unit/alf-relvar/virtual/test_connection.rb +3 -2
- data/spec/unit/alf-relvar/virtual/test_delete.rb +3 -4
- data/spec/unit/alf-relvar/virtual/test_expr.rb +3 -2
- data/spec/unit/alf-relvar/virtual/test_heading.rb +5 -4
- data/spec/unit/alf-relvar/virtual/test_insert.rb +2 -2
- data/spec/unit/alf-relvar/virtual/test_keys.rb +5 -4
- data/spec/unit/alf-relvar/virtual/test_oo_lang.rb +7 -2
- data/spec/unit/alf-relvar/virtual/test_to_cog.rb +4 -3
- data/spec/unit/alf-relvar/virtual/test_to_lispy.rb +3 -6
- data/spec/unit/alf-relvar/virtual/test_to_relvar.rb +2 -1
- data/spec/unit/alf-relvar/virtual/test_to_s.rb +3 -3
- data/spec/unit/alf-relvar/virtual/test_update.rb +4 -5
- data/spec/unit/alf-support/config/test_dup.rb +58 -0
- data/spec/unit/alf-support/config/test_freeze.rb +37 -0
- data/spec/unit/alf-support/config/test_hash_get.rb +41 -0
- data/spec/unit/alf-support/config/test_hash_set.rb +61 -0
- data/spec/unit/alf-support/config/test_new.rb +10 -0
- data/spec/unit/alf-support/config/test_option.rb +15 -0
- data/spec/unit/alf-support/config/test_option_setter.rb +72 -0
- data/spec/unit/alf-support/config/test_options.rb +22 -0
- data/spec/unit/alf-support/tuple_scope/test_hash_get.rb +20 -0
- data/spec/unit/alf-support/tuple_scope/test_respond_to.rb +6 -0
- data/spec/unit/alf-support/tuple_scope/test_to_s.rb +22 -0
- data/spec/unit/alf-types/keys/test_select.rb +57 -8
- data/spec/unit/alf-types/ordering/test_coerce.rb +46 -35
- data/spec/unit/alf-types/ordering/test_dive.rb +41 -0
- data/spec/unit/alf-types/ordering/test_hash_get.rb +20 -0
- data/spec/unit/alf-types/ordering/test_merge.rb +61 -0
- data/spec/unit/alf-types/ordering/test_reverse.rb +40 -0
- data/spec/unit/alf-types/ordering/test_selectors.rb +24 -0
- data/spec/unit/alf-types/ordering/test_sorter.rb +62 -8
- data/spec/unit/alf-types/ordering/test_to_attr_list.rb +16 -4
- data/spec/unit/alf-types/ordering/test_total.rb +56 -0
- data/spec/unit/alf-types/selection/test_coerce.rb +38 -0
- data/spec/unit/alf-types/selection/test_select.rb +34 -0
- data/spec/unit/alf-types/selector/test_coerce.rb +36 -0
- data/spec/unit/alf-types/selector/test_composite_q.rb +20 -0
- data/spec/unit/alf-types/selector/test_dive.rb +32 -0
- data/spec/unit/alf-types/selector/test_select.rb +40 -0
- data/spec/unit/alf-types/selector/test_simple_q.rb +20 -0
- data/spec/unit/alf-types/selector/test_to_a.rb +20 -0
- data/spec/unit/alf-types/selector/test_to_lispy.rb +20 -0
- data/spec/unit/alf-types/selector/test_to_ruby_literal.rb +20 -0
- data/spec/unit/alf-types/tuple_expression/test_evaluate.rb +38 -0
- data/spec/unit/alf-viewpoint/metadata/test_add_members.rb +38 -0
- data/spec/unit/alf-viewpoint/metadata/test_all_members.rb +33 -0
- data/spec/unit/alf-viewpoint/metadata/test_depends.rb +50 -0
- data/spec/unit/alf-viewpoint/metadata/test_dup.rb +32 -0
- data/spec/unit/alf-viewpoint/metadata/test_expand.rb +31 -0
- data/spec/unit/alf-viewpoint/metadata/test_expects.rb +38 -0
- data/spec/unit/alf-viewpoint/metadata/test_initialize.rb +40 -0
- data/spec/unit/alf-viewpoint/metadata/test_to_module.rb +37 -0
- data/spec/unit/alf-viewpoint/test_depends.rb +18 -0
- data/spec/unit/alf-viewpoint/test_expects.rb +18 -0
- data/spec/unit/alf-viewpoint/test_members.rb +33 -0
- data/spec/unit/alf-viewpoint/test_metadata.rb +27 -0
- data/spec/unit/alf-viewpoint/test_native_const.rb +6 -0
- metadata +125 -14
- data/spec/unit/alf-database/options/test_default_viewpoint.rb +0 -25
- data/spec/unit/alf-types/keys/test_compact.rb +0 -21
- data/spec/unit/alf-types/ordering/test_plus.rb +0 -27
- 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
|
-
|
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
|
-
|
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
|
-
|
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
|
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(
|
7
|
-
raise unless
|
8
|
-
@
|
9
|
-
|
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
|
@@ -2,36 +2,88 @@ module Alf
|
|
2
2
|
module Support
|
3
3
|
class Config
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
27
|
-
|
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
|
55
|
-
@
|
54
|
+
def [](what)
|
55
|
+
@tuple[what]
|
56
56
|
end
|
57
57
|
|
58
|
-
def
|
59
|
-
@
|
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
|
data/lib/alf-types/alf/types.rb
CHANGED
@@ -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.
|
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
|
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
|
@@ -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
|
-
|
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
|
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
|
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
|
67
|
-
|
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(
|
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
|