reflekt 1.0.5 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/lib/accessor.rb +45 -0
  3. data/lib/action.rb +127 -0
  4. data/lib/action_stack.rb +44 -0
  5. data/lib/{Clone.rb → clone.rb} +11 -11
  6. data/lib/config.rb +48 -0
  7. data/lib/control.rb +81 -0
  8. data/lib/experiment.rb +99 -0
  9. data/lib/meta.rb +75 -0
  10. data/lib/meta/array_meta.rb +32 -0
  11. data/lib/meta/boolean_meta.rb +26 -0
  12. data/lib/meta/float_meta.rb +26 -0
  13. data/lib/meta/integer_meta.rb +26 -0
  14. data/lib/meta/{NullMeta.rb → null_meta.rb} +21 -19
  15. data/lib/meta/object_meta.rb +35 -0
  16. data/lib/meta/string_meta.rb +26 -0
  17. data/lib/meta_builder.rb +100 -0
  18. data/lib/reflection.rb +123 -0
  19. data/lib/reflekt.rb +277 -0
  20. data/lib/renderer.rb +38 -0
  21. data/lib/rule.rb +54 -0
  22. data/lib/rule_set.rb +110 -0
  23. data/lib/rule_set_aggregator.rb +260 -0
  24. data/lib/rules/array_rule.rb +94 -0
  25. data/lib/rules/boolean_rule.rb +43 -0
  26. data/lib/rules/float_rule.rb +55 -0
  27. data/lib/rules/integer_rule.rb +55 -0
  28. data/lib/rules/null_rule.rb +35 -0
  29. data/lib/rules/object_rule.rb +42 -0
  30. data/lib/rules/string_rule.rb +75 -0
  31. data/lib/web/index.html +3 -4
  32. metadata +46 -29
  33. data/lib/Accessor.rb +0 -37
  34. data/lib/Action.rb +0 -88
  35. data/lib/ActionStack.rb +0 -44
  36. data/lib/Aggregator.rb +0 -260
  37. data/lib/Config.rb +0 -42
  38. data/lib/Control.rb +0 -83
  39. data/lib/Meta.rb +0 -71
  40. data/lib/MetaBuilder.rb +0 -84
  41. data/lib/Reflection.rb +0 -195
  42. data/lib/Reflekt.rb +0 -243
  43. data/lib/Renderer.rb +0 -39
  44. data/lib/Rule.rb +0 -52
  45. data/lib/RuleSet.rb +0 -109
  46. data/lib/meta/ArrayMeta.rb +0 -34
  47. data/lib/meta/BooleanMeta.rb +0 -26
  48. data/lib/meta/FloatMeta.rb +0 -26
  49. data/lib/meta/IntegerMeta.rb +0 -26
  50. data/lib/meta/StringMeta.rb +0 -26
  51. data/lib/rules/ArrayRule.rb +0 -88
  52. data/lib/rules/BooleanRule.rb +0 -47
  53. data/lib/rules/FloatRule.rb +0 -57
  54. data/lib/rules/IntegerRule.rb +0 -57
  55. data/lib/rules/NullRule.rb +0 -33
  56. data/lib/rules/StringRule.rb +0 -81
data/lib/meta.rb ADDED
@@ -0,0 +1,75 @@
1
+ ################################################################################
2
+ # Metadata for input and output.
3
+ #
4
+ # @pattern Abstract class
5
+ # @see lib/meta for each meta.
6
+ #
7
+ # @hierachy
8
+ # 1. Action
9
+ # 2. Reflection
10
+ # 3. Meta <- YOU ARE HERE
11
+ ################################################################################
12
+
13
+ module Reflekt
14
+ class Meta
15
+
16
+ ##
17
+ # Each meta defines its type.
18
+ ##
19
+ def initialize()
20
+ @type = :null
21
+ end
22
+
23
+ ##
24
+ # Each meta loads values.
25
+ #
26
+ # @param value [Dynamic]
27
+ ##
28
+ def load(value)
29
+ end
30
+
31
+ ##
32
+ # @return [Hash]
33
+ ##
34
+ def serialize()
35
+ {
36
+ :type => @type
37
+ }
38
+ end
39
+
40
+ ############################################################################
41
+ # CLASS
42
+ ############################################################################
43
+
44
+ ##
45
+ # Deserialize metadata.
46
+ #
47
+ # TODO: Deserialize should create a Meta object.
48
+ # TODO: Require each Meta type to handle its own deserialization.
49
+ #
50
+ # @param meta [Hash] The metadata to deserialize.
51
+ # @param meta [Hash]
52
+ ##
53
+ def self.deserialize(meta)
54
+ # Convert nil meta into NullMeta.
55
+ # Meta is nil when there are no @inputs or @output on the method.
56
+ if meta.nil?
57
+ return NullMeta.new().serialize()
58
+ end
59
+
60
+ # Symbolize keys.
61
+ # TODO: Remove once "Fix Rowdb.get(path)" bug fixed.
62
+ meta = meta.transform_keys(&:to_sym)
63
+
64
+ # Symbolize type value.
65
+ meta[:type] = meta[:type].to_sym
66
+
67
+ return meta
68
+ end
69
+
70
+ def self.numeric? value
71
+ Float(value) != nil rescue false
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,32 @@
1
+ require_relative '../meta'
2
+
3
+ module Reflekt
4
+ class ArrayMeta < Meta
5
+
6
+ def initialize()
7
+ @type = :array
8
+ @min = nil
9
+ @max = nil
10
+ @length = nil
11
+ end
12
+
13
+ ##
14
+ # @param value [Array]
15
+ ##
16
+ def load(value)
17
+ @min = value.min()
18
+ @max = value.max()
19
+ @length = value.length()
20
+ end
21
+
22
+ def serialize()
23
+ {
24
+ :type => @type,
25
+ :max => @max,
26
+ :min => @min,
27
+ :length => @length
28
+ }
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../meta'
2
+
3
+ module Reflekt
4
+ class BooleanMeta < Meta
5
+
6
+ def initialize()
7
+ @type = :bool
8
+ @value = nil
9
+ end
10
+
11
+ ##
12
+ # @param value [Boolean]
13
+ ##
14
+ def load(value)
15
+ @value = value.to_s
16
+ end
17
+
18
+ def serialize()
19
+ {
20
+ :type => @type,
21
+ :value => @value
22
+ }
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../meta'
2
+
3
+ module Reflekt
4
+ class FloatMeta < Meta
5
+
6
+ def initialize()
7
+ @type = :float
8
+ @value = nil
9
+ end
10
+
11
+ ##
12
+ # @param value [Float]
13
+ ##
14
+ def load(value)
15
+ @value = value
16
+ end
17
+
18
+ def serialize()
19
+ {
20
+ :type => @type,
21
+ :value => @value
22
+ }
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../meta'
2
+
3
+ module Reflekt
4
+ class IntegerMeta < Meta
5
+
6
+ def initialize()
7
+ @type = :int
8
+ @value = nil
9
+ end
10
+
11
+ ##
12
+ # @param value [Integer]
13
+ ##
14
+ def load(value)
15
+ @value = value
16
+ end
17
+
18
+ def serialize()
19
+ {
20
+ :type => @type,
21
+ :value => @value
22
+ }
23
+ end
24
+
25
+ end
26
+ end
@@ -10,25 +10,27 @@
10
10
  # 3. Meta <- YOU ARE HERE
11
11
  ################################################################################
12
12
 
13
- require 'Meta'
13
+ require_relative '../meta'
14
14
 
15
- class NullMeta < Meta
16
-
17
- def initialize()
18
- @type = :null
19
- end
20
-
21
- ##
22
- # @param value [NilClass]
23
- ##
24
- def load(value)
25
- # No need to load a value for null meta.
15
+ module Reflekt
16
+ class NullMeta < Meta
17
+
18
+ def initialize()
19
+ @type = :null
20
+ end
21
+
22
+ ##
23
+ # @param value [NilClass]
24
+ ##
25
+ def load(value)
26
+ # No need to load a value for null meta.
27
+ end
28
+
29
+ def serialize()
30
+ {
31
+ :type => @type,
32
+ }
33
+ end
34
+
26
35
  end
27
-
28
- def serialize()
29
- {
30
- :type => @type,
31
- }
32
- end
33
-
34
36
  end
@@ -0,0 +1,35 @@
1
+ ################################################################################
2
+ # A reprsentation of a null value.
3
+ #
4
+ # @hierachy
5
+ # 1. Action
6
+ # 2. Reflection
7
+ # 3. Meta <- YOU ARE HERE
8
+ ################################################################################
9
+
10
+ require_relative '../meta'
11
+
12
+ module Reflekt
13
+ class ObjectMeta < Meta
14
+
15
+ def initialize()
16
+ @type = :object
17
+ @class_type = nil
18
+ end
19
+
20
+ ##
21
+ # @param value [Dynamic] Any custom class.
22
+ ##
23
+ def load(value)
24
+ @class_type = value.class
25
+ end
26
+
27
+ def serialize()
28
+ {
29
+ :type => @type,
30
+ :class_type => @class_type
31
+ }
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ require_relative '../meta'
2
+
3
+ module Reflekt
4
+ class StringMeta < Meta
5
+
6
+ def initialize()
7
+ @type = :string
8
+ @length = nil
9
+ end
10
+
11
+ ##
12
+ # @param value [String]
13
+ ##
14
+ def load(value)
15
+ @length = value.length
16
+ end
17
+
18
+ def serialize()
19
+ {
20
+ :type => @type,
21
+ :length => @length
22
+ }
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,100 @@
1
+ ################################################################################
2
+ # Create metadata.
3
+ #
4
+ # @pattern Builder
5
+ # @see lib/meta for each meta.
6
+ ################################################################################
7
+
8
+ require_relative 'meta'
9
+ # Require all meta from the meta directory.
10
+ Dir[File.join(__dir__, 'meta', '*.rb')].each { |file| require_relative file }
11
+
12
+ module Reflekt
13
+ class MetaBuilder
14
+
15
+ ##
16
+ # Create meta type for matching data type.
17
+ #
18
+ # @logic
19
+ # 1. First return basic type
20
+ # 2. Then return custom type
21
+ # 3. Then return "nil" type
22
+ #
23
+ # @param value [Dynamic] Any input or output.
24
+ ##
25
+ def self.create(value)
26
+
27
+ meta = nil
28
+ data_type = value.class.to_s
29
+
30
+ case data_type
31
+ when "Array"
32
+ meta = ArrayMeta.new()
33
+ when "TrueClass", "FalseClass"
34
+ meta = BooleanMeta.new()
35
+ when "Float"
36
+ meta = FloatMeta.new()
37
+ when "Integer"
38
+ meta = IntegerMeta.new()
39
+ when "String"
40
+ meta = StringMeta.new()
41
+ else
42
+ unless value.nil?
43
+ meta = ObjectMeta.new()
44
+ end
45
+ end
46
+
47
+ unless meta.nil?
48
+ meta.load(value)
49
+ end
50
+
51
+ return meta
52
+
53
+ end
54
+
55
+ ##
56
+ # Create meta for multiple values.
57
+ #
58
+ # @param values
59
+ ##
60
+ def self.create_many(values)
61
+
62
+ meta = []
63
+
64
+ values.each do |value|
65
+ meta << self.create(value)
66
+ end
67
+
68
+ return meta
69
+
70
+ end
71
+
72
+ ##
73
+ # @param data_type [Type]
74
+ ##
75
+ def self.data_type_to_meta_type(value)
76
+
77
+ data_type = value.class
78
+
79
+ meta_types = {
80
+ Array => :array,
81
+ TrueClass => :bool,
82
+ FalseClass => :bool,
83
+ Float => :float,
84
+ Integer => :int,
85
+ NilClass => :null,
86
+ String => :string
87
+ }
88
+
89
+ if meta_types.key? data_type
90
+ return meta_types[data_type]
91
+ elsif value.nil?
92
+ return nil
93
+ else
94
+ return :object
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+ end
data/lib/reflection.rb ADDED
@@ -0,0 +1,123 @@
1
+ ################################################################################
2
+ # A snapshot of real or random data.
3
+ #
4
+ # @pattern Abstract class
5
+ #
6
+ # @nomenclature
7
+ # args, inputs/output and meta represent different stages of a value.
8
+ #
9
+ # @hierachy
10
+ # 1. Action
11
+ # 2. Reflection <- YOU ARE HERE
12
+ # 3. Meta
13
+ #
14
+ # @status
15
+ # - :pass [Symbol] The reflection passes the rules.
16
+ # - :fail [Symbol] The reflection fails the rules or produces a system error.
17
+ # - :error [Symbol] The control reflection produces a system error.
18
+ ################################################################################
19
+
20
+ require_relative 'clone'
21
+ require_relative 'meta_builder'
22
+
23
+ module Reflekt
24
+ class Reflection
25
+ include LitCLI
26
+
27
+ attr_reader :status
28
+
29
+ ##
30
+ # Create a reflection.
31
+ #
32
+ # @param action [Action] The Action that created this Reflection.
33
+ # @param number [Integer] Multiple Reflections can be created per Action.
34
+ # @param aggregator [RuleSetAggregator] The aggregated RuleSet for this class/method.
35
+ ##
36
+ def initialize(action, number, aggregator)
37
+ @action = action
38
+ @reflection_id = action.unique_id + number
39
+ @number = number
40
+
41
+ # Dependency.
42
+ @aggregator = aggregator
43
+
44
+ # Caller.
45
+ @klass = action.klass
46
+ @method = action.method
47
+
48
+ # Metadata.
49
+ @inputs = nil
50
+ @output = nil
51
+
52
+ # Clone the action's calling object.
53
+ # TODO: Abstract away into Clone class.
54
+ @clone = action.caller_object.clone
55
+
56
+ # Result.
57
+ @status = :pass
58
+ @time = Time.now.to_i
59
+ @message = nil
60
+ end
61
+
62
+ ##
63
+ # Reflect on a method.
64
+ #
65
+ # Create a shadow action.
66
+ # @param *args [Dynamic] The method's arguments.
67
+ ##
68
+ def reflect(*args)
69
+ # Implemented by Control and Experiment.
70
+ end
71
+
72
+ ##
73
+ # Get the results of the reflection.
74
+ #
75
+ # @keys
76
+ # - eid [Integer] Execution ID
77
+ # - aid [Integer] Action ID
78
+ # - rid [Integer] Reflection ID
79
+ # - num [Integer] Reflection number
80
+ #
81
+ # @return [Hash] Reflection metadata.
82
+ ##
83
+ def serialize()
84
+ # Create execution ID from the ID of the first action in the ActionStack.
85
+ execution_id = @action.unique_id
86
+ unless @action.base.nil?
87
+ execution_id = @action.base.unique_id
88
+ end
89
+
90
+ # Build reflection.
91
+ reflection = {
92
+ :eid => execution_id,
93
+ :aid => @action.unique_id,
94
+ :rid => @reflection_id,
95
+ :num => @number,
96
+ :time => @time,
97
+ :class => @klass,
98
+ :method => @method,
99
+ :status => @status,
100
+ :message => @message,
101
+ :inputs => nil,
102
+ :output => nil,
103
+ }
104
+
105
+ # TODO: After the last experiment for an action is completed, serialize()
106
+ # appears to be called twice. Possibly due to inheritance.
107
+ 🔥"> Save meta for #{@method}()", :save, :meta, @klass
108
+
109
+ unless @inputs.nil?
110
+ reflection[:inputs] = []
111
+ @inputs.each do |meta|
112
+ meta.nil? ? nil : reflection[:inputs] << meta.serialize()
113
+ end
114
+ end
115
+
116
+ unless @output.nil?
117
+ reflection[:output] = @output.serialize()
118
+ end
119
+
120
+ return reflection
121
+ end
122
+ end
123
+ end