reflekt 1.0.4 → 1.0.9

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 (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/{Accessor.rb → accessor.rb} +2 -0
  3. data/lib/{Execution.rb → action.rb} +14 -12
  4. data/lib/action_stack.rb +46 -0
  5. data/lib/{Clone.rb → clone.rb} +6 -4
  6. data/lib/{Config.rb → config.rb} +3 -0
  7. data/lib/{Control.rb → control.rb} +16 -12
  8. data/lib/{Reflection.rb → experiment.rb} +16 -90
  9. data/lib/{Meta.rb → meta.rb} +7 -5
  10. data/lib/meta/{ArrayMeta.rb → array_meta.rb} +3 -1
  11. data/lib/meta/{BooleanMeta.rb → boolean_meta.rb} +3 -1
  12. data/lib/meta/{FloatMeta.rb → float_meta.rb} +3 -1
  13. data/lib/meta/{IntegerMeta.rb → integer_meta.rb} +3 -1
  14. data/lib/meta/{NullMeta.rb → null_meta.rb} +4 -2
  15. data/lib/meta/object_meta.rb +35 -0
  16. data/lib/meta/{StringMeta.rb → string_meta.rb} +3 -1
  17. data/lib/{MetaBuilder.rb → meta_builder.rb} +21 -5
  18. data/lib/reflection.rb +123 -0
  19. data/lib/{Reflekt.rb → reflekt.rb} +42 -42
  20. data/lib/{Renderer.rb → renderer.rb} +2 -0
  21. data/lib/{Rule.rb → rule.rb} +3 -1
  22. data/lib/{RuleSet.rb → rule_set.rb} +5 -3
  23. data/lib/{Aggregator.rb → rule_set_aggregator.rb} +29 -18
  24. data/lib/rules/{ArrayRule.rb → array_rule.rb} +3 -1
  25. data/lib/rules/{BooleanRule.rb → boolean_rule.rb} +3 -1
  26. data/lib/rules/{FloatRule.rb → float_rule.rb} +3 -1
  27. data/lib/rules/{IntegerRule.rb → integer_rule.rb} +3 -1
  28. data/lib/rules/{NullRule.rb → null_rule.rb} +5 -6
  29. data/lib/rules/object_rule.rb +42 -0
  30. data/lib/rules/{StringRule.rb → string_rule.rb} +3 -1
  31. data/lib/web/bundle.js +2 -2
  32. data/lib/web/package-lock.json +3 -3
  33. data/lib/web/package.json +1 -1
  34. data/lib/web/server.js +5 -5
  35. metadata +32 -29
  36. data/lib/ShadowStack.rb +0 -44
@@ -1,5 +1,6 @@
1
- require 'Meta'
1
+ require_relative '../meta'
2
2
 
3
+ module Reflekt
3
4
  class ArrayMeta < Meta
4
5
 
5
6
  def initialize()
@@ -32,3 +33,4 @@ class ArrayMeta < Meta
32
33
  end
33
34
 
34
35
  end
36
+ end
@@ -1,5 +1,6 @@
1
- require 'Meta'
1
+ require_relative '../meta'
2
2
 
3
+ module Reflekt
3
4
  class BooleanMeta < Meta
4
5
 
5
6
  def initialize()
@@ -24,3 +25,4 @@ class BooleanMeta < Meta
24
25
  end
25
26
 
26
27
  end
28
+ end
@@ -1,5 +1,6 @@
1
- require 'Meta'
1
+ require_relative '../meta'
2
2
 
3
+ module Reflekt
3
4
  class FloatMeta < Meta
4
5
 
5
6
  def initialize()
@@ -24,3 +25,4 @@ class FloatMeta < Meta
24
25
  end
25
26
 
26
27
  end
28
+ end
@@ -1,5 +1,6 @@
1
- require 'Meta'
1
+ require_relative '../meta'
2
2
 
3
+ module Reflekt
3
4
  class IntegerMeta < Meta
4
5
 
5
6
  def initialize()
@@ -24,3 +25,4 @@ class IntegerMeta < Meta
24
25
  end
25
26
 
26
27
  end
28
+ end
@@ -5,13 +5,14 @@
5
5
  # A "null" value on serialized "inputs" and "output" also becomes a NullMeta.
6
6
  #
7
7
  # @hierachy
8
- # 1. Execution
8
+ # 1. Action
9
9
  # 2. Reflection
10
10
  # 3. Meta <- YOU ARE HERE
11
11
  ################################################################################
12
12
 
13
- require 'Meta'
13
+ require_relative '../meta'
14
14
 
15
+ module Reflekt
15
16
  class NullMeta < Meta
16
17
 
17
18
  def initialize()
@@ -32,3 +33,4 @@ class NullMeta < Meta
32
33
  end
33
34
 
34
35
  end
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
@@ -1,5 +1,6 @@
1
- require 'Meta'
1
+ require_relative '../meta'
2
2
 
3
+ module Reflekt
3
4
  class StringMeta < Meta
4
5
 
5
6
  def initialize()
@@ -24,3 +25,4 @@ class StringMeta < Meta
24
25
  end
25
26
 
26
27
  end
28
+ end
@@ -5,14 +5,20 @@
5
5
  # @see lib/meta for each meta.
6
6
  ################################################################################
7
7
 
8
- require 'Meta'
8
+ require_relative 'meta'
9
9
  # Require all meta.
10
- Dir[File.join(__dir__, 'meta', '*.rb')].each { |file| require file }
10
+ Dir[File.join(__dir__, 'meta', '*.rb')].each { |file| require_relative file }
11
11
 
12
+ module Reflekt
12
13
  class MetaBuilder
13
14
 
14
15
  ##
15
- # Create meta.
16
+ # Create meta type for matching data type.
17
+ #
18
+ # @flow
19
+ # 1. First return basic type
20
+ # 2. Then return custom type
21
+ # 3. Then return "nil" type
16
22
  #
17
23
  # @param value
18
24
  ##
@@ -21,7 +27,6 @@ class MetaBuilder
21
27
  meta = nil
22
28
  data_type = value.class.to_s
23
29
 
24
- # Create meta type for matching data type.
25
30
  case data_type
26
31
  when "Array"
27
32
  meta = ArrayMeta.new()
@@ -33,6 +38,10 @@ class MetaBuilder
33
38
  meta = IntegerMeta.new()
34
39
  when "String"
35
40
  meta = StringMeta.new()
41
+ else
42
+ unless value.nil?
43
+ meta = ObjectMeta.new()
44
+ end
36
45
  end
37
46
 
38
47
  unless meta.nil?
@@ -77,8 +86,15 @@ class MetaBuilder
77
86
  String => :string
78
87
  }
79
88
 
80
- return meta_types[data_type]
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
81
96
 
82
97
  end
83
98
 
84
99
  end
100
+ end
@@ -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
+
26
+ attr_reader :status
27
+
28
+ ##
29
+ # Create a reflection.
30
+ #
31
+ # @param action [Action] The Action that created this Reflection.
32
+ # @param number [Integer] Multiple Reflections can be created per Action.
33
+ # @param aggregator [RuleSetAggregator] The aggregated RuleSet for this class/method.
34
+ ##
35
+ def initialize(action, number, aggregator)
36
+
37
+ @action = action
38
+ @unique_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
+
61
+ end
62
+
63
+ ##
64
+ # Reflect on a method.
65
+ #
66
+ # Create a shadow action.
67
+ # @param *args [Dynamic] The method's arguments.
68
+ ##
69
+ def reflect(*args)
70
+ # Implemented by Control and Experiment.
71
+ end
72
+
73
+ ##
74
+ # Get the results of the reflection.
75
+ #
76
+ # @keys
77
+ # - eid [Integer] Execution ID
78
+ # - aid [Integer] Action ID
79
+ # - rid [Integer] Reflection ID
80
+ # - num [Integer] Reflection number
81
+ #
82
+ # @return [Hash] Reflection metadata.
83
+ ##
84
+ def serialize()
85
+
86
+ # Create execution ID from the ID of the first action in the ActionStack.
87
+ execution_id = @action.unique_id
88
+ unless @action.base.nil?
89
+ execution_id = @action.base.unique_id
90
+ end
91
+
92
+ # Build reflection.
93
+ reflection = {
94
+ :eid => execution_id,
95
+ :aid => @action.unique_id,
96
+ :rid => @unique_id,
97
+ :num => @number,
98
+ :time => @time,
99
+ :class => @klass,
100
+ :method => @method,
101
+ :status => @status,
102
+ :message => @message,
103
+ :inputs => nil,
104
+ :output => nil,
105
+ }
106
+
107
+ unless @inputs.nil?
108
+ reflection[:inputs] = []
109
+ @inputs.each do |meta|
110
+ reflection[:inputs] << meta.serialize()
111
+ end
112
+ end
113
+
114
+ unless @output.nil?
115
+ reflection[:output] = @output.serialize()
116
+ end
117
+
118
+ return reflection
119
+
120
+ end
121
+
122
+ end
123
+ end
@@ -6,8 +6,8 @@
6
6
  # @flow
7
7
  # 1. Reflekt is prepended to a class and setup.
8
8
  # 2. When a class insantiates so does Reflekt.
9
- # 3. An Execution is created on method call.
10
- # 4. Many Refections are created per Execution.
9
+ # 3. An Action is created on method call.
10
+ # 4. Many Refections are created per Action.
11
11
  # 5. Each Reflection executes on cloned data.
12
12
  # 6. Flow is returned to the original method.
13
13
  #
@@ -19,16 +19,16 @@
19
19
  require 'set'
20
20
  require 'erb'
21
21
  require 'rowdb'
22
- require 'Accessor'
23
- require 'Aggregator'
24
- require 'Config'
25
- require 'Control'
26
- require 'Execution'
27
- require 'Reflection'
28
- require 'Renderer'
29
- require 'ShadowStack'
22
+ require_relative 'accessor'
23
+ require_relative 'action'
24
+ require_relative 'action_stack'
25
+ require_relative 'config'
26
+ require_relative 'control'
27
+ require_relative 'experiment'
28
+ require_relative 'renderer'
29
+ require_relative 'rule_set_aggregator'
30
30
  # Require all rules.
31
- Dir[File.join(__dir__, 'rules', '*.rb')].each { |file| require file }
31
+ Dir[File.join(__dir__, 'rules', '*.rb')].each { |file| require_relative file }
32
32
 
33
33
  module Reflekt
34
34
 
@@ -53,34 +53,34 @@ module Reflekt
53
53
  # When Reflekt enabled and control reflection has executed without error.
54
54
  if @@reflekt.config.enabled && !@@reflekt.error
55
55
 
56
- # Get current execution.
57
- execution = @@reflekt.stack.peek()
56
+ # Get current action.
57
+ action = @@reflekt.stack.peek()
58
58
 
59
59
  # Don't reflect when reflect limit reached or method skipped.
60
60
  unless (@reflekt_counts[method] >= @@reflekt.config.reflect_limit) || self.class.reflekt_skipped?(method)
61
61
 
62
- # When stack empty or past execution done reflecting.
63
- if execution.nil? || execution.has_finished_reflecting?
62
+ # When stack empty or past action done reflecting.
63
+ if action.nil? || action.has_finished_reflecting?
64
64
 
65
- # Create execution.
66
- execution = Execution.new(self, method, @@reflekt.config.reflect_amount, @@reflekt.stack)
65
+ # Create action.
66
+ action = Action.new(self, method, @@reflekt.config.reflect_amount, @@reflekt.stack)
67
67
 
68
- @@reflekt.stack.push(execution)
68
+ @@reflekt.stack.push(action)
69
69
 
70
70
  end
71
71
 
72
72
  ##
73
- # Reflect the execution.
73
+ # Reflect the action.
74
74
  #
75
- # The first method call in the execution creates a reflection.
76
- # Then method calls are shadow executions which return to the reflection.
75
+ # The first method call in the action creates a reflection.
76
+ # Then method calls are shadow actions which return to the reflection.
77
77
  ##
78
- if execution.has_empty_reflections? && !execution.is_reflecting?
79
- execution.is_reflecting = true
78
+ if action.has_empty_experiments? && !action.is_reflecting?
79
+ action.is_reflecting = true
80
80
 
81
81
  # Create control.
82
- control = Control.new(execution, 0, @@reflekt.aggregator)
83
- execution.control = control
82
+ control = Control.new(action, 0, @@reflekt.aggregator)
83
+ action.control = control
84
84
 
85
85
  # Execute control.
86
86
  control.reflect(*args)
@@ -91,22 +91,22 @@ module Reflekt
91
91
  # Continue reflecting when control executes succesfully.
92
92
  else
93
93
 
94
- # Save control as reflection.
94
+ # Save control as a reflection.
95
95
  @@reflekt.db.get("reflections").push(control.serialize())
96
96
 
97
- # Multiple reflections per execution.
98
- execution.reflections.each_with_index do |value, index|
97
+ # Multiple experiments per action.
98
+ action.experiments.each_with_index do |value, index|
99
99
 
100
- # Create reflection.
101
- reflection = Reflection.new(execution, index + 1, @@reflekt.aggregator)
102
- execution.reflections[index] = reflection
100
+ # Create experiment.
101
+ experiment = Experiment.new(action, index + 1, @@reflekt.aggregator)
102
+ action.experiments[index] = experiment
103
103
 
104
- # Execute reflection.
105
- reflection.reflect(*args)
104
+ # Execute experiment.
105
+ experiment.reflect(*args)
106
106
  @reflekt_counts[method] = @reflekt_counts[method] + 1
107
107
 
108
- # Save reflection.
109
- @@reflekt.db.get("reflections").push(reflection.serialize())
108
+ # Save experiment.
109
+ @@reflekt.db.get("reflections").push(experiment.serialize())
110
110
 
111
111
  end
112
112
 
@@ -121,15 +121,15 @@ module Reflekt
121
121
 
122
122
  end
123
123
 
124
- execution.is_reflecting = false
124
+ action.is_reflecting = false
125
125
  end
126
126
 
127
127
  end
128
128
 
129
129
  # Don't execute skipped methods when reflecting.
130
- unless execution.is_reflecting? && self.class.reflekt_skipped?(method)
130
+ unless action.is_reflecting? && self.class.reflekt_skipped?(method)
131
131
 
132
- # Continue execution / shadow execution.
132
+ # Continue action / shadow action.
133
133
  super *args
134
134
 
135
135
  end
@@ -137,7 +137,7 @@ module Reflekt
137
137
  # When Reflekt disabled or control reflection failed.
138
138
  else
139
139
 
140
- # Continue execution.
140
+ # Continue action.
141
141
  super *args
142
142
 
143
143
  end
@@ -180,7 +180,7 @@ module Reflekt
180
180
  # Set configuration.
181
181
  @@reflekt.path = File.dirname(File.realpath(__FILE__))
182
182
 
183
- # Get reflections directory path from config or current execution path.
183
+ # Get reflections directory path from config or current action path.
184
184
  if @@reflekt.config.output_path
185
185
  @@reflekt.output_path = File.join(@@reflekt.config.output_path, @@reflekt.config.output_directory)
186
186
  else
@@ -199,10 +199,10 @@ module Reflekt
199
199
  db = @@reflekt.db.value()
200
200
 
201
201
  # Create shadow stack.
202
- @@reflekt.stack = ShadowStack.new()
202
+ @@reflekt.stack = ActionStack.new()
203
203
 
204
204
  # Create aggregated rule sets.
205
- @@reflekt.aggregator = Aggregator.new(@@reflekt.config.meta_map)
205
+ @@reflekt.aggregator = RuleSetAggregator.new(@@reflekt.config.meta_map)
206
206
  @@reflekt.aggregator.train(db[:controls])
207
207
 
208
208
  # Create renderer.