Class: Reflekt::Experiment

Inherits:
Reflection show all
Defined in:
lib/experiment.rb

Instance Attribute Summary

Attributes inherited from Reflection

#status

Instance Method Summary collapse

Methods inherited from Reflection

#initialize, #serialize

Constructor Details

This class inherits a constructor from Reflekt::Reflection

Instance Method Details

#randomize(args, input_rule_sets) ⇒ Dynamic

Create random values for each argument from control reflections.

Parameters:

  • args (Dynamic)

    The arguments to mirror random values for.

  • input_rule_sets (Array)

    Aggregated rule sets for each argument.

Returns:

  • (Dynamic)

    Random arguments.


84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/experiment.rb', line 84

def randomize(args, input_rule_sets)
  random_args = []

  args.each_with_index do |arg, arg_num|
    # Get a random rule in the rule set.
    rules = input_rule_sets[arg_num].rules
    agg_rule = rules[rules.keys.sample]

    # Create a random value that follows that rule.
    random_args << agg_rule.random()
  end

  return random_args
end

#reflect(*args) ⇒ Object

Reflect on a method.

Create a shadow action.

Parameters:

  • *args (Dynamic)

    The method's arguments.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/experiment.rb', line 33

def reflect(*args)
  # Get aggregated rule sets.
  input_rule_sets = @aggregator.get_input_rule_sets(@klass, @method)
  output_rule_set = @aggregator.get_output_rule_set(@klass, @method)

  # Fail when no trained rule sets.
  if input_rule_sets.nil?
    @status = :fail
  end

  # When arguments exist.
  unless args.size == 0
    # Create random arguments from aggregated rule sets.
    unless input_rule_sets.nil?
      args = randomize(args, input_rule_sets)
    end

    # Create metadata for each argument.
    # TODO: Create metadata for other inputs such as instance variables.
    🔥"> Create meta for #{@method}(): #{args}", :info, :meta, @klass
    @inputs = MetaBuilder.create_many(args)
  end

  # Action method with random arguments.
  begin
    # Run reflection.
    output = @clone.send(@method, *args)
    @output = MetaBuilder.create(output)

    # Validate output against aggregated control rule sets.
    unless output_rule_set.nil?
      unless @aggregator.test_output(output, output_rule_set)
        @status = :fail
      end
    end

  # When a system error occurs.
  rescue StandardError => message
    @status = :fail
    @message = message
  end
end