reflekt 1.0.1 → 1.0.2

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: cfaf70b43c12867f93934b6b1f2e0d672486b3331369c9c85c0646e54bfc18d2
4
- data.tar.gz: 437bbcdd66ae77ec202360255386edc10d69bcfb2da5b41816b0aed3c3151285
3
+ metadata.gz: 7f9ff9a7d13d77e0e468cf961d6ef1dd6dd2c381b6d3d4fb1a1799b59aa1fdc6
4
+ data.tar.gz: 8bb82ca18b00bd0a1e329bd94557a164eff90fb607fbbcbfa6ef8b026f4619c9
5
5
  SHA512:
6
- metadata.gz: 789f134df80a4cb9455274cd36d968f83903f6c474b6974d451ecfea1910799121f815eeecb9162a3b0ea2417f52983f1bb4557a5dd9b729fbdfa3e7325a6b99
7
- data.tar.gz: 3bfb838fb97dda4989fba353db654b9498ce2ff8112f880d0bb5c926ae58865e05916701ec81cf393486b0b279af308a349245c970242879774c172bf12e2e7c
6
+ metadata.gz: 9d22878d22a93be41323d89b6a718808f78fa61c8cc186ddde480a5298ea1a1ae1fc49576141186d3dee7c02a29295c67a59e41aa732ec1e9dc3b4fd878e7add
7
+ data.tar.gz: 2d759df0674e8dc496ae92b585588b2a927c8aa7bda0368be8265705e1340af60ebe393c84b059dd2516d23cb8f7577b7baa4fe016f1a17ed3934a9fbcc084a2
@@ -49,10 +49,7 @@ class Aggregator
49
49
  unless control["inputs"].nil?
50
50
  control["inputs"].each_with_index do |meta, arg_num|
51
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
52
+ meta = Meta.deserialize(meta)
56
53
 
57
54
  # Get rule set.
58
55
  rule_set = get_input_rule_set(klass, method, arg_num)
@@ -79,7 +76,7 @@ class Aggregator
79
76
  end
80
77
 
81
78
  # Train on metadata.
82
- output_rule_set.train(control["output"])
79
+ output_rule_set.train(Meta.deserialize(control["output"]))
83
80
 
84
81
  end
85
82
 
@@ -177,6 +174,7 @@ class Aggregator
177
174
  FalseClass => BooleanRule,
178
175
  Float => FloatRule,
179
176
  Integer => IntegerRule,
177
+ NilClass => NullRule,
180
178
  String => StringRule
181
179
  }
182
180
 
@@ -184,6 +182,21 @@ class Aggregator
184
182
 
185
183
  end
186
184
 
185
+ def self.testable?(args, input_rule_sets)
186
+
187
+ args.each_with_index do |arg, arg_num|
188
+
189
+ rule_type = value_to_rule_type(arg)
190
+ if input_rule_sets[arg_num].rules[rule_type].nil?
191
+ return false
192
+ end
193
+
194
+ end
195
+
196
+ return true
197
+
198
+ end
199
+
187
200
  ##############################################################################
188
201
  # HELPERS
189
202
  ##############################################################################
@@ -26,6 +26,7 @@ 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
 
@@ -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 property will always be zero.
6
+ #
7
+ # @nomenclature
8
+ # args, inputs/output and meta represent different stages of a value.
5
9
  #
6
10
  # @hierachy
7
11
  # 1. Execution
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'
@@ -28,12 +28,44 @@ class Meta
28
28
  end
29
29
 
30
30
  ##
31
- # Each meta provides metadata.
31
+ # Each meta serializes metadata.
32
32
  #
33
33
  # @return [Hash]
34
34
  ##
35
- def result()
35
+ def serialize()
36
36
  {}
37
37
  end
38
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
+
69
+ end
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,6 +1,10 @@
1
1
  ################################################################################
2
2
  # A snapshot of simulated data.
3
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.
7
+ #
4
8
  # @nomenclature
5
9
  # args, inputs/output and meta represent different stages of a value.
6
10
  #
@@ -8,6 +12,11 @@
8
12
  # 1. Execution
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,11 +29,6 @@ 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
32
  # @param execution [Execution] The Execution that created this Reflection.
29
33
  # @param number [Integer] Multiple Reflections can be created per Execution.
30
34
  # @param aggregator [Aggregator] The aggregated RuleSet for this class/method.
@@ -72,14 +76,17 @@ class Reflection
72
76
  # When arguments exist.
73
77
  unless args.size == 0
74
78
 
75
- # When aggregated rule sets exist.
79
+ # Create random arguments from aggregated rule sets.
76
80
  unless input_rule_sets.nil?
77
81
 
78
- # Randomize arguments from rule sets.
79
- args = randomize(args, input_rule_sets)
82
+ # Base random arguments on the types of the current arguments.
83
+ if Aggregator.testable?(args, input_rule_sets)
84
+
85
+ args = randomize(args, input_rule_sets)
80
86
 
81
- # Validate arguments against aggregated rule sets.
82
- unless @aggregator.test_inputs(args, input_rule_sets)
87
+ # TODO: Fallback to argument types from aggregated control rule sets
88
+ # when arg types not testable or reflect_amount above 3.
89
+ else
83
90
  @status = :fail
84
91
  end
85
92
 
@@ -98,7 +105,7 @@ class Reflection
98
105
  output = @clone.send(@method, *args)
99
106
  @output = MetaBuilder.create(output)
100
107
 
101
- # Validate output with aggregated control rule sets.
108
+ # Validate output against aggregated control rule sets.
102
109
  unless output_rule_set.nil?
103
110
  unless @aggregator.test_output(output, output_rule_set)
104
111
  @status = :fail
@@ -145,7 +152,7 @@ class Reflection
145
152
  #
146
153
  # @return [Hash] Reflection metadata.
147
154
  ##
148
- def result()
155
+ def serialize()
149
156
 
150
157
  # The ID of the first execution in the ShadowStack.
151
158
  base_id = nil
@@ -171,12 +178,12 @@ class Reflection
171
178
  unless @inputs.nil?
172
179
  reflection[:inputs] = []
173
180
  @inputs.each do |meta|
174
- reflection[:inputs] << meta.result()
181
+ reflection[:inputs] << meta.serialize()
175
182
  end
176
183
  end
177
184
 
178
185
  unless @output.nil?
179
- reflection[:output] = @output.result()
186
+ reflection[:output] = @output.serialize()
180
187
  end
181
188
 
182
189
  return reflection
@@ -92,7 +92,7 @@ module Reflekt
92
92
  else
93
93
 
94
94
  # Save control as reflection.
95
- @@reflekt.db.get("reflections").push(control.result())
95
+ @@reflekt.db.get("reflections").push(control.serialize())
96
96
 
97
97
  # Multiple reflections per execution.
98
98
  execution.reflections.each_with_index do |value, index|
@@ -106,12 +106,12 @@ module Reflekt
106
106
  @reflekt_counts[method] = @reflekt_counts[method] + 1
107
107
 
108
108
  # Save reflection.
109
- @@reflekt.db.get("reflections").push(reflection.result())
109
+ @@reflekt.db.get("reflections").push(reflection.serialize())
110
110
 
111
111
  end
112
112
 
113
113
  # Save control.
114
- @@reflekt.db.get("controls").push(control.result())
114
+ @@reflekt.db.get("controls").push(control.serialize())
115
115
 
116
116
  # Save results.
117
117
  @@reflekt.db.write()
@@ -13,6 +13,7 @@
13
13
 
14
14
  require 'set'
15
15
  require 'MetaBuilder'
16
+ require_relative './meta/NullMeta.rb'
16
17
 
17
18
  class RuleSet
18
19
 
@@ -37,40 +38,44 @@ class RuleSet
37
38
  ##
38
39
  # Train rule set on metadata.
39
40
  #
40
- # @param meta [Meta] The metadata to train on.
41
+ # @param meta [Hash] The metadata to train on.
41
42
  ##
42
43
  def train(meta)
43
44
 
44
- unless meta.nil? || meta[:type].nil?
45
+ # Track supported meta types.
46
+ meta_type = meta[:type]
47
+ @meta_types << meta_type
45
48
 
46
- meta_type = meta[:type]
47
- @meta_types << meta_type
49
+ # Get rule types for this meta type.
50
+ if @meta_map.key? meta_type
51
+ @meta_map[meta_type].each do |rule_type|
48
52
 
49
- # Get rule types for this meta type.
50
- if @meta_map.key? meta_type
51
- @meta_map[meta_type].each do |rule_type|
52
-
53
- # Ensure rule exists.
54
- if @rules[rule_type].nil?
55
- @rules[rule_type] = rule_type.new()
56
- end
53
+ # Ensure rule exists.
54
+ if @rules[rule_type].nil?
55
+ @rules[rule_type] = rule_type.new()
56
+ end
57
57
 
58
- # Train rule.
59
- @rules[rule_type].train(meta)
58
+ # Train rule.
59
+ @rules[rule_type].train(meta)
60
60
 
61
- end
62
61
  end
63
-
64
62
  end
65
63
 
66
64
  end
67
65
 
66
+ ##
67
+ # @param value [Dynamic]
68
+ ##
68
69
  def test(value)
69
- result = true
70
70
 
71
- # Only test data type on rule of matching meta type.
71
+ result = true
72
72
  meta_type = MetaBuilder.data_type_to_meta_type(value)
73
73
 
74
+ # Fail if value's meta type not testable by rule set.
75
+ unless @meta_types.include? meta_type
76
+ return false
77
+ end
78
+
74
79
  @rules.each do |klass, rule|
75
80
  if (rule.type == meta_type)
76
81
  unless rule.test(value)
@@ -80,6 +85,7 @@ class RuleSet
80
85
  end
81
86
 
82
87
  return result
88
+
83
89
  end
84
90
 
85
91
  ##
@@ -22,7 +22,7 @@ class ArrayMeta < Meta
22
22
 
23
23
  end
24
24
 
25
- def result()
25
+ def serialize()
26
26
  {
27
27
  :type => @type,
28
28
  :max => @max,
@@ -16,7 +16,7 @@ class BooleanMeta < Meta
16
16
  @value = value.to_s
17
17
  end
18
18
 
19
- def result()
19
+ def serialize()
20
20
  {
21
21
  :type => @type,
22
22
  :value => @value
@@ -16,7 +16,7 @@ class FloatMeta < Meta
16
16
  @value = value
17
17
  end
18
18
 
19
- def result()
19
+ def serialize()
20
20
  {
21
21
  :type => @type,
22
22
  :value => @value
@@ -16,7 +16,7 @@ class IntegerMeta < Meta
16
16
  @value = value
17
17
  end
18
18
 
19
- def result()
19
+ def serialize()
20
20
  {
21
21
  :type => @type,
22
22
  :value => @value
@@ -0,0 +1,34 @@
1
+ ################################################################################
2
+ # A reprsentation of a null value.
3
+ #
4
+ # @note
5
+ # A "null" value on serialized "inputs" and "output" also becomes a NullMeta.
6
+ #
7
+ # @hierachy
8
+ # 1. Execution
9
+ # 2. Reflection
10
+ # 3. Meta <- YOU ARE HERE
11
+ ################################################################################
12
+
13
+ require 'Meta'
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.
26
+ end
27
+
28
+ def serialize()
29
+ {
30
+ :type => @type,
31
+ }
32
+ end
33
+
34
+ end
@@ -16,7 +16,7 @@ class StringMeta < Meta
16
16
  @length = value.length
17
17
  end
18
18
 
19
- def result()
19
+ def serialize()
20
20
  {
21
21
  :type => @type,
22
22
  :length => @length
@@ -27,14 +27,16 @@ class BooleanRule < Rule
27
27
  # @param value [Boolean]
28
28
  ##
29
29
  def test(value)
30
- @booleans.include? value
30
+
31
+ # Booleans are stored as strings.
32
+ @booleans.include? value.to_s
33
+
31
34
  end
32
35
 
33
36
  def result()
34
37
  {
35
38
  :type => @type,
36
- :is_true => @booleans.include?,
37
- :is_false => @booleans.include?
39
+ :booleans => @booleans
38
40
  }
39
41
  end
40
42
 
@@ -0,0 +1,36 @@
1
+ require 'Rule'
2
+
3
+ class NullRule < Rule
4
+
5
+ def initialize()
6
+ @type = :null
7
+ end
8
+
9
+ ##
10
+ # @param meta [NullMeta]
11
+ ##
12
+ def train(meta)
13
+ # No need to train. NullMeta is always null.
14
+ end
15
+
16
+ ##
17
+ # @param value [NilClass]
18
+ ##
19
+ def test(value)
20
+
21
+ return false unless value.nil?
22
+ return true
23
+
24
+ end
25
+
26
+ def result()
27
+ {
28
+ :type => @type
29
+ }
30
+ end
31
+
32
+ def random()
33
+ nil
34
+ end
35
+
36
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reflekt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maedi Prichard
@@ -48,11 +48,13 @@ files:
48
48
  - lib/meta/BooleanMeta.rb
49
49
  - lib/meta/FloatMeta.rb
50
50
  - lib/meta/IntegerMeta.rb
51
+ - lib/meta/NullMeta.rb
51
52
  - lib/meta/StringMeta.rb
52
53
  - lib/rules/ArrayRule.rb
53
54
  - lib/rules/BooleanRule.rb
54
55
  - lib/rules/FloatRule.rb
55
56
  - lib/rules/IntegerRule.rb
57
+ - lib/rules/NullRule.rb
56
58
  - lib/rules/StringRule.rb
57
59
  - lib/web/README.md
58
60
  - lib/web/bundle.js