reflekt 1.0.9 → 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.
data/lib/control.rb CHANGED
@@ -22,64 +22,60 @@ require_relative 'reflection'
22
22
  require_relative 'meta_builder'
23
23
 
24
24
  module Reflekt
25
- class Control < Reflection
26
-
27
- ##
28
- # Reflect on a method.
29
- #
30
- # Create a shadow action.
31
- # @param *args [Dynamic] The method's arguments.
32
- ##
33
- def reflect(*args)
34
-
35
- # Get trained rule sets.
36
- input_rule_sets = @aggregator.get_input_rule_sets(@klass, @method)
37
- output_rule_set = @aggregator.get_output_rule_set(@klass, @method)
38
-
39
- # Fail when no trained rule sets.
40
- if input_rule_sets.nil?
41
- @status = :fail
42
- end
43
-
44
- # When arguments exist.
45
- unless args.size == 0
46
-
47
- # Validate arguments against trained rule sets.
48
- unless input_rule_sets.nil?
49
- unless @aggregator.test_inputs(args, input_rule_sets)
50
- @status = :fail
51
- end
25
+ class Control < Reflection
26
+
27
+ ##
28
+ # Reflect on a method.
29
+ #
30
+ # Create a shadow action.
31
+ # @param *args [Dynamic] The method's arguments.
32
+ ##
33
+ def reflect(*args)
34
+ # Get trained rule sets.
35
+ input_rule_sets = @aggregator.get_input_rule_sets(@klass, @method)
36
+ output_rule_set = @aggregator.get_output_rule_set(@klass, @method)
37
+
38
+ # Fail when no trained rule sets.
39
+ if input_rule_sets.nil?
40
+ @status = :fail
41
+ 🔥"> No trained rule sets", :fail, :reflect
52
42
  end
53
43
 
54
- # Create metadata for each argument.
55
- # TODO: Create metadata for other inputs such as instance variables.
56
- @inputs = MetaBuilder.create_many(args)
57
-
58
- end
44
+ # When arguments exist.
45
+ unless args.size == 0
46
+ # Validate arguments against trained rule sets.
47
+ unless input_rule_sets.nil?
48
+ unless @aggregator.test_inputs(args, input_rule_sets)
49
+ @status = :fail
50
+ 🔥"> Invalid inputs", @status, :reflect
51
+ end
52
+ end
59
53
 
60
- # Action method with real arguments.
61
- begin
54
+ 🔥"> Create meta for #{@method}(): #{args}", :info, :meta, @klass
55
+ # TODO: Create metadata for other inputs such as instance variables.
56
+ @inputs = MetaBuilder.create_many(args)
57
+ end
62
58
 
63
- # Run reflection.
64
- output = @clone.send(@method, *args)
65
- @output = MetaBuilder.create(output)
59
+ # Action method with real arguments.
60
+ begin
61
+ # Run reflection.
62
+ output = @clone.send(@method, *args)
63
+ @output = MetaBuilder.create(output)
66
64
 
67
- # Validate output with aggregated control rule sets.
68
- unless output_rule_set.nil?
65
+ # Validate output with aggregated control rule sets.
69
66
  unless @aggregator.test_output(output, output_rule_set)
70
67
  @status = :fail
68
+ 🔥"> Invalid output", @status, :reflect
71
69
  end
72
- end
73
-
74
- # When a system error occurs.
75
- rescue StandardError => message
76
70
 
77
- @status = :error
78
- @message = message
71
+ # When a system error occurs.
72
+ rescue StandardError => message
73
+ @status = :error
74
+ @message = message
79
75
 
76
+ # TODO: Write log entry to /reflections/errors.txt
77
+ 🔥"#{@method}() not reflected", :error, :control, @klass.class
78
+ end
80
79
  end
81
-
82
80
  end
83
-
84
- end
85
81
  end
data/lib/experiment.rb CHANGED
@@ -22,89 +22,78 @@ require_relative 'reflection'
22
22
  require_relative 'meta_builder'
23
23
 
24
24
  module Reflekt
25
- class Experiment < Reflection
26
-
27
- ##
28
- # Reflect on a method.
29
- #
30
- # Create a shadow action.
31
- # @param *args [Dynamic] The method's arguments.
32
- ##
33
- def reflect(*args)
34
-
35
- # Get aggregated rule sets.
36
- input_rule_sets = @aggregator.get_input_rule_sets(@klass, @method)
37
- output_rule_set = @aggregator.get_output_rule_set(@klass, @method)
38
-
39
- # Fail when no trained rule sets.
40
- if input_rule_sets.nil?
41
- @status = :fail
42
- end
43
-
44
- # When arguments exist.
45
- unless args.size == 0
46
-
47
- # Create random arguments from aggregated rule sets.
48
- unless input_rule_sets.nil?
49
- args = randomize(args, input_rule_sets)
25
+ class Experiment < Reflection
26
+
27
+ ##
28
+ # Reflect on a method.
29
+ #
30
+ # Create a shadow action.
31
+ # @param *args [Dynamic] The method's arguments.
32
+ ##
33
+ def reflect(*args)
34
+ # Get aggregated rule sets.
35
+ input_rule_sets = @aggregator.get_input_rule_sets(@klass, @method)
36
+ output_rule_set = @aggregator.get_output_rule_set(@klass, @method)
37
+
38
+ # Fail when no trained rule sets.
39
+ if input_rule_sets.nil?
40
+ @status = :fail
50
41
  end
51
42
 
52
- # Create metadata for each argument.
53
- # TODO: Create metadata for other inputs such as instance variables.
54
- @inputs = MetaBuilder.create_many(args)
55
-
56
- end
57
-
58
- # Action method with random arguments.
59
- begin
60
-
61
- # Run reflection.
62
- output = @clone.send(@method, *args)
63
- @output = MetaBuilder.create(output)
64
-
65
- # Validate output against aggregated control rule sets.
66
- unless output_rule_set.nil?
67
- unless @aggregator.test_output(output, output_rule_set)
68
- @status = :fail
43
+ # When arguments exist.
44
+ unless args.size == 0
45
+ # Create random arguments from aggregated rule sets.
46
+ unless input_rule_sets.nil?
47
+ args = randomize(args, input_rule_sets)
69
48
  end
70
- end
71
49
 
72
- # When a system error occurs.
73
- rescue StandardError => message
50
+ # Create metadata for each argument.
51
+ # TODO: Create metadata for other inputs such as instance variables.
52
+ 🔥"> Create meta for #{@method}(): #{args}", :info, :meta, @klass
53
+ @inputs = MetaBuilder.create_many(args)
54
+ end
74
55
 
75
- @status = :fail
76
- @message = message
56
+ # Action method with random arguments.
57
+ begin
58
+ # Run reflection.
59
+ output = @clone.send(@method, *args)
60
+ @output = MetaBuilder.create(output)
61
+
62
+ # Validate output against aggregated control rule sets.
63
+ unless output_rule_set.nil?
64
+ unless @aggregator.test_output(output, output_rule_set)
65
+ @status = :fail
66
+ end
67
+ end
77
68
 
69
+ # When a system error occurs.
70
+ rescue StandardError => message
71
+ @status = :fail
72
+ @message = message
73
+ end
78
74
  end
79
75
 
80
- end
81
-
82
- ##
83
- # Create random values for each argument from control reflections.
84
- #
85
- # @param args [Dynamic] The arguments to mirror random values for.
86
- # @param input_rule_sets [Array] Aggregated rule sets for each argument.
87
- #
88
- # @return [Dynamic] Random arguments.
89
- ##
90
- def randomize(args, input_rule_sets)
91
-
92
- random_args = []
93
-
94
- args.each_with_index do |arg, arg_num|
95
-
96
- # Get a random rule in the rule set.
97
- rules = input_rule_sets[arg_num].rules
98
- agg_rule = rules[rules.keys.sample]
99
-
100
- # Create a random value that follows that rule.
101
- random_args << agg_rule.random()
76
+ ##
77
+ # Create random values for each argument from control reflections.
78
+ #
79
+ # @param args [Dynamic] The arguments to mirror random values for.
80
+ # @param input_rule_sets [Array] Aggregated rule sets for each argument.
81
+ #
82
+ # @return [Dynamic] Random arguments.
83
+ ##
84
+ def randomize(args, input_rule_sets)
85
+ random_args = []
86
+
87
+ args.each_with_index do |arg, arg_num|
88
+ # Get a random rule in the rule set.
89
+ rules = input_rule_sets[arg_num].rules
90
+ agg_rule = rules[rules.keys.sample]
91
+
92
+ # Create a random value that follows that rule.
93
+ random_args << agg_rule.random()
94
+ end
102
95
 
96
+ return random_args
103
97
  end
104
-
105
- return random_args
106
-
107
98
  end
108
-
109
- end
110
99
  end
data/lib/meta.rb CHANGED
@@ -11,63 +11,65 @@
11
11
  ################################################################################
12
12
 
13
13
  module Reflekt
14
- class Meta
14
+ class Meta
15
15
 
16
- ##
17
- # Each meta defines its type.
18
- ##
19
- def initialize()
20
- @type = :null
21
- end
16
+ ##
17
+ # Each meta defines its type.
18
+ ##
19
+ def initialize()
20
+ @type = :null
21
+ end
22
22
 
23
- ##
24
- # Each meta loads values.
25
- #
26
- # @param value [Dynamic]
27
- ##
28
- def load(value)
29
- end
23
+ ##
24
+ # Each meta loads values.
25
+ #
26
+ # @param value [Dynamic]
27
+ ##
28
+ def load(value)
29
+ end
30
30
 
31
- ##
32
- # @return [Hash]
33
- ##
34
- def serialize()
35
- {
36
- :type => @type
37
- }
38
- end
31
+ ##
32
+ # @return [Hash]
33
+ ##
34
+ def serialize()
35
+ {
36
+ :type => @type
37
+ }
38
+ end
39
39
 
40
- ##############################################################################
41
- # CLASS
42
- ##############################################################################
40
+ ############################################################################
41
+ # CLASS
42
+ ############################################################################
43
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)
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
54
59
 
55
- # Convert nil meta into NullMeta.
56
- # Meta is nil when there are no @inputs or @output on the method.
57
- if meta.nil?
58
- return NullMeta.new().serialize()
59
- end
60
+ # Symbolize keys.
61
+ # TODO: Remove once "Fix Rowdb.get(path)" bug fixed.
62
+ meta = meta.transform_keys(&:to_sym)
60
63
 
61
- # Symbolize keys.
62
- # TODO: Remove once "Fix Rowdb.get(path)" bug fixed.
63
- meta = meta.transform_keys(&:to_sym)
64
+ # Symbolize type value.
65
+ meta[:type] = meta[:type].to_sym
64
66
 
65
- # Symbolize type value.
66
- meta[:type] = meta[:type].to_sym
67
+ return meta
68
+ end
67
69
 
68
- return meta
70
+ def self.numeric? value
71
+ Float(value) != nil rescue false
72
+ end
69
73
 
70
74
  end
71
-
72
- end
73
75
  end
@@ -1,36 +1,32 @@
1
1
  require_relative '../meta'
2
2
 
3
3
  module Reflekt
4
- class ArrayMeta < Meta
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
5
30
 
6
- def initialize()
7
-
8
- @type = :array
9
- @min = nil
10
- @max = nil
11
- @length = nil
12
-
13
- end
14
-
15
- ##
16
- # @param value [Array]
17
- ##
18
- def load(value)
19
-
20
- @min = value.min()
21
- @max = value.max()
22
- @length = value.length()
23
-
24
- end
25
-
26
- def serialize()
27
- {
28
- :type => @type,
29
- :max => @max,
30
- :min => @min,
31
- :length => @length
32
- }
33
31
  end
34
-
35
- end
36
32
  end
@@ -1,28 +1,26 @@
1
1
  require_relative '../meta'
2
2
 
3
3
  module Reflekt
4
- class BooleanMeta < Meta
4
+ class BooleanMeta < Meta
5
5
 
6
- def initialize()
6
+ def initialize()
7
+ @type = :bool
8
+ @value = nil
9
+ end
7
10
 
8
- @type = :bool
9
- @value = nil
11
+ ##
12
+ # @param value [Boolean]
13
+ ##
14
+ def load(value)
15
+ @value = value.to_s
16
+ end
10
17
 
11
- end
12
-
13
- ##
14
- # @param value [Boolean]
15
- ##
16
- def load(value)
17
- @value = value.to_s
18
- end
18
+ def serialize()
19
+ {
20
+ :type => @type,
21
+ :value => @value
22
+ }
23
+ end
19
24
 
20
- def serialize()
21
- {
22
- :type => @type,
23
- :value => @value
24
- }
25
25
  end
26
-
27
- end
28
26
  end