Class: Reflection

Inherits:
Object
  • Object
show all
Defined in:
lib/reflection.rb

Direct Known Subclasses

Control, Experiment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, number, aggregator) ⇒ Reflection

Create a reflection.

Parameters:

  • action (Action)

    The Action that created this Reflection.

  • number (Integer)

    Multiple Reflections can be created per Action.

  • aggregator (RuleSetAggregator)

    The aggregated RuleSet for this class/method.



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
# File 'lib/reflection.rb', line 34

def initialize(action, number, aggregator)

  @action = action
  @unique_id = action.unique_id + number
  @number = number

  # Dependency.
  @aggregator = aggregator

  # Caller.
  @klass = action.klass
  @method = action.method

  # Metadata.
  @inputs = nil
  @output = nil

  # Clone the action's calling object.
  # TODO: Abstract away into Clone class.
  @clone = action.caller_object.clone

  # Result.
  @status = :pass
  @time = Time.now.to_i
  @message = nil

end

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



25
26
27
# File 'lib/reflection.rb', line 25

def status
  @status
end

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.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/reflection.rb', line 80

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.



68
69
70
# File 'lib/reflection.rb', line 68

def reflect(*args)
  # Implemented by Control and Experiment.
end

#serializeHash

Get the results of the reflection.

Returns:

  • (Hash)

    Reflection metadata.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/reflection.rb', line 110

def serialize()

  # Create execution ID from the ID of the first action in   the ActionStack.
  execution_id = @action.unique_id
  unless @action.base.nil?
    execution_id = @action.base.unique_id
  end

  # Build reflection.
  reflection = {
    :eid => execution_id,
    :aid => @action.unique_id,
    :rid => @unique_id,
    :num => @number,
    :time => @time,
    :class => @klass,
    :method => @method,
    :status => @status,
    :message => @message,
    :inputs => nil,
    :output => nil,
  }

  unless @inputs.nil?
    reflection[:inputs] = []
    @inputs.each do |meta|
      reflection[:inputs] << meta.serialize()
    end
  end

  unless @output.nil?
    reflection[:output] = @output.serialize()
  end

  return reflection

end