reflekt 1.0.9 → 1.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/accessor.rb +33 -27
- data/lib/action.rb +108 -71
- data/lib/action_stack.rb +29 -31
- data/lib/clone.rb +10 -12
- data/lib/config.rb +44 -41
- data/lib/control.rb +44 -48
- data/lib/experiment.rb +63 -74
- data/lib/meta.rb +50 -48
- data/lib/meta/array_meta.rb +26 -30
- data/lib/meta/boolean_meta.rb +17 -19
- data/lib/meta/float_meta.rb +17 -19
- data/lib/meta/integer_meta.rb +17 -19
- data/lib/meta/null_meta.rb +19 -19
- data/lib/meta/string_meta.rb +17 -19
- data/lib/meta_builder.rb +74 -74
- data/lib/reflection.rb +91 -91
- data/lib/reflekt.rb +160 -126
- data/lib/renderer.rb +27 -30
- data/lib/rule.rb +33 -33
- data/lib/rule_set.rb +64 -65
- data/lib/rule_set_aggregator.rb +191 -193
- data/lib/rules/array_rule.rb +77 -73
- data/lib/rules/boolean_rule.rb +29 -35
- data/lib/rules/float_rule.rb +42 -46
- data/lib/rules/integer_rule.rb +42 -46
- data/lib/rules/null_rule.rb +30 -30
- data/lib/rules/string_rule.rb +54 -62
- data/lib/web/index.html +3 -4
- metadata +17 -3
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
#
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
61
|
-
|
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
|
-
#
|
64
|
-
|
65
|
-
|
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
|
-
|
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
|
-
|
78
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
#
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
|
73
|
-
|
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
|
-
|
76
|
-
|
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
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
##
|
17
|
+
# Each meta defines its type.
|
18
|
+
##
|
19
|
+
def initialize()
|
20
|
+
@type = :null
|
21
|
+
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
23
|
+
##
|
24
|
+
# Each meta loads values.
|
25
|
+
#
|
26
|
+
# @param value [Dynamic]
|
27
|
+
##
|
28
|
+
def load(value)
|
29
|
+
end
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
31
|
+
##
|
32
|
+
# @return [Hash]
|
33
|
+
##
|
34
|
+
def serialize()
|
35
|
+
{
|
36
|
+
:type => @type
|
37
|
+
}
|
38
|
+
end
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
############################################################################
|
41
|
+
# CLASS
|
42
|
+
############################################################################
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
62
|
-
|
63
|
-
meta = meta.transform_keys(&:to_sym)
|
64
|
+
# Symbolize type value.
|
65
|
+
meta[:type] = meta[:type].to_sym
|
64
66
|
|
65
|
-
|
66
|
-
|
67
|
+
return meta
|
68
|
+
end
|
67
69
|
|
68
|
-
|
70
|
+
def self.numeric? value
|
71
|
+
Float(value) != nil rescue false
|
72
|
+
end
|
69
73
|
|
70
74
|
end
|
71
|
-
|
72
|
-
end
|
73
75
|
end
|
data/lib/meta/array_meta.rb
CHANGED
@@ -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
|
data/lib/meta/boolean_meta.rb
CHANGED
@@ -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
|
-
|
6
|
+
def initialize()
|
7
|
+
@type = :bool
|
8
|
+
@value = nil
|
9
|
+
end
|
7
10
|
|
8
|
-
|
9
|
-
@value
|
11
|
+
##
|
12
|
+
# @param value [Boolean]
|
13
|
+
##
|
14
|
+
def load(value)
|
15
|
+
@value = value.to_s
|
16
|
+
end
|
10
17
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|