reflekt 1.0.0 → 1.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e94e86e6d558012df59e1aebfdbbb7c2bf70a2e849e9a186795a3e4d5cb648f0
4
- data.tar.gz: 9db428f6c518c06fbddb83e0abd9920b9dc30344846827b5c9505c3f2bc111c9
3
+ metadata.gz: 457006f1c0a4fbe9a1ce47684aeeec980852cd966f29d07ac57751161f314d52
4
+ data.tar.gz: ca01fd9306ece9e1698aab0dcb4945a848a5a910625f8b9394b4f26aa7819514
5
5
  SHA512:
6
- metadata.gz: e2a7ecf3adb9e06f7f3a09d507265c035bf3c641193711c64fea512b03973696c8ef9278d59eedf43ee83dd03a0f5f552026f0b2f6ba6182c6361bb35a211bf7
7
- data.tar.gz: f002c69e490b63dd065e5f0ba0b40e9dcbbe980f71e7df66da01b1bc37b2dfe1081c9e58a3024b388b96628cc98422875a5b90f1e9227327f3241bc5d61450b0
6
+ metadata.gz: 1de336f083b2e90230453d3ce07cfb36981998587de2e126c88948da75a618ee57a68b32b9869865fb9353b45e85fadc293d98afc2c4669afe8fadc9761fda93
7
+ data.tar.gz: 0b00aa43ef7763bc11e93697446ca0e86114818f3e6238d28711d1038eef0c4bc58dc43b1f1f901ec58a4cdf6c9138493e63572a7d33a0363a591e908b38e751
@@ -1,13 +1,13 @@
1
1
  ################################################################################
2
- # A shadow execution.
2
+ # A shadow action.
3
3
  #
4
4
  # @hierachy
5
- # 1. Execution <- YOU ARE HERE
5
+ # 1. Action <- YOU ARE HERE
6
6
  # 2. Reflection
7
7
  # 3. Meta
8
8
  ################################################################################
9
9
 
10
- class Execution
10
+ class Action
11
11
 
12
12
  attr_accessor :unique_id
13
13
  attr_accessor :caller_object
@@ -24,12 +24,12 @@ class Execution
24
24
  attr_accessor :is_base
25
25
 
26
26
  ##
27
- # Create Execution.
27
+ # Create Action.
28
28
  #
29
29
  # @param object [Object] The calling object.
30
30
  # @param method [Symbol] The calling method.
31
- # @param reflect_amount [Integer] The number of reflections to create per execution.
32
- # @param stack [ShadowStack] The shadow execution call stack.
31
+ # @param reflect_amount [Integer] The number of reflections to create per action.
32
+ # @param stack [ActionStack] The shadow action call stack.
33
33
  ##
34
34
  def initialize(caller_object, method, reflect_amount, stack)
35
35
 
@@ -69,7 +69,7 @@ class Execution
69
69
  end
70
70
 
71
71
  ##
72
- # Is the Execution currently reflecting methods?
72
+ # Is the Action currently reflecting methods?
73
73
  ##
74
74
  def is_reflecting?
75
75
  @is_reflecting
@@ -0,0 +1,44 @@
1
+ ################################################################################
2
+ # Track the actions in a shadow call stack.
3
+ #
4
+ # @pattern Stack
5
+ ################################################################################
6
+
7
+ class ActionStack
8
+
9
+ def initialize()
10
+ @bottom = nil
11
+ @top = nil
12
+ end
13
+
14
+ def peek()
15
+ @top
16
+ end
17
+
18
+ def base()
19
+ @bottom
20
+ end
21
+
22
+ ##
23
+ # Place Action at the top of stack.
24
+ #
25
+ # @param action [Action] The action to place.
26
+ # @return [Action] The placed action.
27
+ ##
28
+ def push(action)
29
+
30
+ # Place first action at bottom of stack.
31
+ if @bottom.nil?
32
+ @bottom = action
33
+ # Connect subsequent actions to each other.
34
+ else
35
+ @top.parent = action
36
+ action.child = @top
37
+ end
38
+
39
+ # Place action at top of stack.
40
+ @top = action
41
+
42
+ end
43
+
44
+ end
@@ -46,24 +46,13 @@ class Aggregator
46
46
  # INPUT
47
47
  ##
48
48
 
49
- unless control["inputs"].nil?
49
+ # Singular null input.
50
+ if control["inputs"].nil?
51
+ train_input(klass, method, nil, 0)
52
+ # Multiple inputs.
53
+ else
50
54
  control["inputs"].each_with_index do |meta, arg_num|
51
-
52
- # TODO: Remove once "Fix Rowdb.get(path)" bug fixed.
53
- meta = meta.transform_keys(&:to_sym)
54
- # Deserialize meta type to symbol.
55
- meta[:type] = meta[:type].to_sym
56
-
57
- # Get rule set.
58
- rule_set = get_input_rule_set(klass, method, arg_num)
59
- if rule_set.nil?
60
- rule_set = RuleSet.new(@meta_map)
61
- set_input_rule_set(klass, method, arg_num, rule_set)
62
- end
63
-
64
- # Train on metadata.
65
- rule_set.train(meta)
66
-
55
+ train_input(klass, method, meta, arg_num)
67
56
  end
68
57
  end
69
58
 
@@ -79,16 +68,33 @@ class Aggregator
79
68
  end
80
69
 
81
70
  # Train on metadata.
82
- output_rule_set.train(control["output"])
71
+ output_rule_set.train(Meta.deserialize(control["output"]))
83
72
 
84
73
  end
85
74
 
86
75
  end
87
76
 
77
+ def train_input(klass, method, meta, arg_num)
78
+
79
+ # Get deserialized meta.
80
+ meta = Meta.deserialize(meta)
81
+
82
+ # Get rule set.
83
+ rule_set = get_input_rule_set(klass, method, arg_num)
84
+ if rule_set.nil?
85
+ rule_set = RuleSet.new(@meta_map)
86
+ set_input_rule_set(klass, method, arg_num, rule_set)
87
+ end
88
+
89
+ # Train on metadata.
90
+ rule_set.train(meta)
91
+
92
+ end
93
+
88
94
  ##
89
95
  # Validate inputs.
90
96
  #
91
- # @stage Called when validating a reflection.
97
+ # @stage Called when validating a control reflection.
92
98
  # @param inputs [Array] The method's arguments.
93
99
  # @param input_rule_sets [Array] The RuleSets to validate each input with.
94
100
  ##
@@ -177,6 +183,7 @@ class Aggregator
177
183
  FalseClass => BooleanRule,
178
184
  Float => FloatRule,
179
185
  Integer => IntegerRule,
186
+ NilClass => NullRule,
180
187
  String => StringRule
181
188
  }
182
189
 
@@ -184,6 +191,21 @@ class Aggregator
184
191
 
185
192
  end
186
193
 
194
+ def self.testable?(args, input_rule_sets)
195
+
196
+ args.each_with_index do |arg, arg_num|
197
+
198
+ rule_type = value_to_rule_type(arg)
199
+ if input_rule_sets[arg_num].rules[rule_type].nil?
200
+ return false
201
+ end
202
+
203
+ end
204
+
205
+ return true
206
+
207
+ end
208
+
187
209
  ##############################################################################
188
210
  # HELPERS
189
211
  ##############################################################################
@@ -7,17 +7,17 @@
7
7
  # on object, not indirectly through clone which results in "undefined method".
8
8
  #
9
9
  # @hierachy
10
- # 1. Execution
10
+ # 1. Action
11
11
  # 2. Reflection
12
12
  # 3. Clone <- YOU ARE HERE
13
13
  ################################################################################
14
14
 
15
15
  class Clone
16
16
 
17
- def initialize(execution)
17
+ def initialize(action)
18
18
 
19
- # Clone the execution's calling object.
20
- @caller_object_clone = execution.caller_object.clone
19
+ # Clone the action's calling object.
20
+ @caller_object_clone = action.caller_object.clone
21
21
 
22
22
  # TODO: Clone any other instances that this clone references.
23
23
  # TODO: Replace clone's references to these new instances.
@@ -26,11 +26,12 @@ class Config
26
26
  :bool => [BooleanRule],
27
27
  :int => [IntegerRule],
28
28
  :float => [FloatRule],
29
+ :null => [NullRule],
29
30
  :string => [StringRule]
30
31
  }
31
32
 
32
33
  # An absolute path to the directory that contains the output directory.
33
- # Defaults to current execution path.
34
+ # Defaults to current action path.
34
35
  @output_path = nil
35
36
 
36
37
  # Name of output directory.
@@ -1,12 +1,21 @@
1
1
  ################################################################################
2
2
  # A shapshot of real data.
3
3
  #
4
- # A control's @number property will always be zero.
4
+ # @note
5
+ # A control's @number will always be 0.
6
+ #
7
+ # @nomenclature
8
+ # args, inputs/output and meta represent different stages of a value.
5
9
  #
6
10
  # @hierachy
7
- # 1. Execution
11
+ # 1. Action
8
12
  # 2. Control <- YOU ARE HERE
9
13
  # 3. Meta
14
+ #
15
+ # @status
16
+ # - :pass [Symbol] The reflection passes the rules.
17
+ # - :fail [Symbol] The reflection fails the rules or produces a system error.
18
+ # - :error [Symbol] The control reflection produces a system error.
10
19
  ################################################################################
11
20
 
12
21
  require 'Reflection'
@@ -17,35 +26,37 @@ class Control < Reflection
17
26
  ##
18
27
  # Reflect on a method.
19
28
  #
20
- # Creates a shadow execution.
29
+ # Creates a shadow action.
21
30
  # @param *args [Dynamic] The method's arguments.
22
31
  ##
23
32
  def reflect(*args)
24
33
 
25
- # Get aggregated rule sets.
34
+ # Get trained rule sets.
26
35
  input_rule_sets = @aggregator.get_input_rule_sets(@klass, @method)
27
36
  output_rule_set = @aggregator.get_output_rule_set(@klass, @method)
28
37
 
38
+ # Fail when no trained rule sets.
39
+ if input_rule_sets.nil?
40
+ @status = :fail
41
+ end
42
+
29
43
  # When arguments exist.
30
44
  unless args.size == 0
31
45
 
32
- # When aggregated rule sets exist.
46
+ # Validate arguments against trained rule sets.
33
47
  unless input_rule_sets.nil?
34
-
35
- # Validate arguments against aggregated rule sets.
36
48
  unless @aggregator.test_inputs(args, input_rule_sets)
37
49
  @status = :fail
38
50
  end
39
-
40
51
  end
41
52
 
42
53
  # Create metadata for each argument.
43
- # TODO: Create metadata for other inputs such as properties on the instance.
54
+ # TODO: Create metadata for other inputs such as instance variables.
44
55
  @inputs = MetaBuilder.create_many(args)
45
56
 
46
57
  end
47
58
 
48
- # Action method with new/old arguments.
59
+ # Action method with real arguments.
49
60
  begin
50
61
 
51
62
  # Run reflection.
@@ -5,7 +5,7 @@
5
5
  # @see lib/meta for each meta.
6
6
  #
7
7
  # @hierachy
8
- # 1. Execution
8
+ # 1. Action
9
9
  # 2. Reflection
10
10
  # 3. Meta <- YOU ARE HERE
11
11
  ################################################################################
@@ -16,7 +16,7 @@ class Meta
16
16
  # Each meta defines its type.
17
17
  ##
18
18
  def initialize()
19
- @type = nil
19
+ @type = :null
20
20
  end
21
21
 
22
22
  ##
@@ -28,12 +28,44 @@ class Meta
28
28
  end
29
29
 
30
30
  ##
31
- # Each meta provides metadata.
32
- #
33
31
  # @return [Hash]
34
32
  ##
35
- def result()
36
- {}
33
+ def serialize()
34
+ {
35
+ :type => @type
36
+ }
37
+ end
38
+
39
+ ##############################################################################
40
+ # CLASS
41
+ ##############################################################################
42
+
43
+ ##
44
+ # Deserialize metadata.
45
+ #
46
+ # @todo Deserialize should create a Meta object.
47
+ # @todo Require each Meta type to handle its own deserialization.
48
+ #
49
+ # @param meta [Hash] The metadata to deserialize.
50
+ # @param meta [Hash]
51
+ ##
52
+ def self.deserialize(meta)
53
+
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
+
37
69
  end
38
70
 
39
71
  end
@@ -73,6 +73,7 @@ class MetaBuilder
73
73
  FalseClass => :bool,
74
74
  Float => :float,
75
75
  Integer => :int,
76
+ NilClass => :null,
76
77
  String => :string
77
78
  }
78
79
 
@@ -1,13 +1,22 @@
1
1
  ################################################################################
2
- # A snapshot of simulated data.
2
+ # A snapshot of random data.
3
+ #
4
+ # @note
5
+ # A reflection's random value is within the bounds of aggregated control rule sets
6
+ # as well as as the arg type being inputted into the current control reflection.
3
7
  #
4
8
  # @nomenclature
5
9
  # args, inputs/output and meta represent different stages of a value.
6
10
  #
7
11
  # @hierachy
8
- # 1. Execution
12
+ # 1. Action
9
13
  # 2. Reflection <- YOU ARE HERE
10
14
  # 3. Meta
15
+ #
16
+ # @status
17
+ # - :pass [Symbol] The reflection passes the rules.
18
+ # - :fail [Symbol] The reflection fails the rules or produces a system error.
19
+ # - :error [Symbol] The control reflection produces a system error.
11
20
  ################################################################################
12
21
 
13
22
  require 'Clone'
@@ -20,35 +29,30 @@ class Reflection
20
29
  ##
21
30
  # Create a Reflection.
22
31
  #
23
- # @status
24
- # - :pass The reflection passes the rules.
25
- # - :fail The reflection fails the rules or produces a system error.
26
- # - :error The control reflection produces a system error.
27
- #
28
- # @param execution [Execution] The Execution that created this Reflection.
29
- # @param number [Integer] Multiple Reflections can be created per Execution.
32
+ # @param action [Action] The Action that created this Reflection.
33
+ # @param number [Integer] Multiple Reflections can be created per Action.
30
34
  # @param aggregator [Aggregator] The aggregated RuleSet for this class/method.
31
35
  ##
32
- def initialize(execution, number, aggregator)
36
+ def initialize(action, number, aggregator)
33
37
 
34
- @execution = execution
35
- @unique_id = execution.unique_id + number
38
+ @action = action
39
+ @unique_id = action.unique_id + number
36
40
  @number = number
37
41
 
38
42
  # Dependency.
39
43
  @aggregator = aggregator
40
44
 
41
45
  # Caller.
42
- @klass = execution.klass
43
- @method = execution.method
46
+ @klass = action.klass
47
+ @method = action.method
44
48
 
45
49
  # Metadata.
46
50
  @inputs = nil
47
51
  @output = nil
48
52
 
49
- # Clone the execution's calling object.
53
+ # Clone the action's calling object.
50
54
  # TODO: Abstract away into Clone class.
51
- @clone = execution.caller_object.clone
55
+ @clone = action.caller_object.clone
52
56
 
53
57
  # Result.
54
58
  @status = :pass
@@ -60,7 +64,7 @@ class Reflection
60
64
  ##
61
65
  # Reflect on a method.
62
66
  #
63
- # Creates a shadow execution.
67
+ # Creates a shadow action.
64
68
  # @param *args [Dynamic] The method's arguments.
65
69
  ##
66
70
  def reflect(*args)
@@ -69,36 +73,33 @@ class Reflection
69
73
  input_rule_sets = @aggregator.get_input_rule_sets(@klass, @method)
70
74
  output_rule_set = @aggregator.get_output_rule_set(@klass, @method)
71
75
 
76
+ # Fail when no trained rule sets.
77
+ if input_rule_sets.nil?
78
+ @status = :fail
79
+ end
80
+
72
81
  # When arguments exist.
73
82
  unless args.size == 0
74
83
 
75
- # When aggregated rule sets exist.
84
+ # Create random arguments from aggregated rule sets.
76
85
  unless input_rule_sets.nil?
77
-
78
- # Randomize arguments from rule sets.
79
86
  args = randomize(args, input_rule_sets)
80
-
81
- # Validate arguments against aggregated rule sets.
82
- unless @aggregator.test_inputs(args, input_rule_sets)
83
- @status = :fail
84
- end
85
-
86
87
  end
87
88
 
88
89
  # Create metadata for each argument.
89
- # TODO: Create metadata for other inputs such as properties on the instance.
90
+ # TODO: Create metadata for other inputs such as instance variables.
90
91
  @inputs = MetaBuilder.create_many(args)
91
92
 
92
93
  end
93
94
 
94
- # Action method with new/old arguments.
95
+ # Action method with random arguments.
95
96
  begin
96
97
 
97
98
  # Run reflection.
98
99
  output = @clone.send(@method, *args)
99
100
  @output = MetaBuilder.create(output)
100
101
 
101
- # Validate output with aggregated control rule sets.
102
+ # Validate output against aggregated control rule sets.
102
103
  unless output_rule_set.nil?
103
104
  unless @aggregator.test_output(output, output_rule_set)
104
105
  @status = :fail
@@ -118,7 +119,7 @@ class Reflection
118
119
  ##
119
120
  # Create random values for each argument from control reflections.
120
121
  #
121
- # @param args [Dynamic] The arguments to create random values for.
122
+ # @param args [Dynamic] The arguments to mirror random values for.
122
123
  # @param input_rule_sets [Array] Aggregated rule sets for each argument.
123
124
  #
124
125
  # @return [Dynamic] Random arguments.
@@ -129,9 +130,11 @@ class Reflection
129
130
 
130
131
  args.each_with_index do |arg, arg_num|
131
132
 
132
- rule_type = Aggregator.value_to_rule_type(arg)
133
- agg_rule = input_rule_sets[arg_num].rules[rule_type]
133
+ # Get a random rule in the rule set.
134
+ rules = input_rule_sets[arg_num].rules
135
+ agg_rule = rules[rules.keys.sample]
134
136
 
137
+ # Create a random value that follows that rule.
135
138
  random_args << agg_rule.random()
136
139
 
137
140
  end
@@ -143,22 +146,28 @@ class Reflection
143
146
  ##
144
147
  # Get the results of the reflection.
145
148
  #
149
+ # @keys
150
+ # - eid [Integer] Execution ID
151
+ # - aid [Integer] Action ID
152
+ # - rid [Integer] Reflection ID
153
+ # - num [Integer] Reflection number
154
+ #
146
155
  # @return [Hash] Reflection metadata.
147
156
  ##
148
- def result()
157
+ def serialize()
149
158
 
150
- # The ID of the first execution in the ShadowStack.
151
- base_id = nil
152
- unless @execution.base == nil
153
- base_id = @execution.base.unique_id
159
+ # Create execution ID from the ID of the first action in the ActionStack.
160
+ execution_id = @action.unique_id
161
+ unless @action.base.nil?
162
+ execution_id = @action.base.unique_id
154
163
  end
155
164
 
156
165
  # Build reflection.
157
166
  reflection = {
158
- :base_id => base_id,
159
- :exe_id => @execution.unique_id,
160
- :ref_id => @unique_id,
161
- :ref_num => @number,
167
+ :eid => execution_id,
168
+ :aid => @action.unique_id,
169
+ :rid => @unique_id,
170
+ :num => @number,
162
171
  :time => @time,
163
172
  :class => @klass,
164
173
  :method => @method,
@@ -171,12 +180,12 @@ class Reflection
171
180
  unless @inputs.nil?
172
181
  reflection[:inputs] = []
173
182
  @inputs.each do |meta|
174
- reflection[:inputs] << meta.result()
183
+ reflection[:inputs] << meta.serialize()
175
184
  end
176
185
  end
177
186
 
178
187
  unless @output.nil?
179
- reflection[:output] = @output.result()
188
+ reflection[:output] = @output.serialize()
180
189
  end
181
190
 
182
191
  return reflection