Class: TheoryAction

Inherits:
Object show all
Includes:
TheoryComponent
Defined in:
lib/theory/TheoryAction.rb

Overview

The theory action represents the what theory inserts into the main runtime method.

Direct Known Subclasses

ActionImplementation

Instance Attribute Summary collapse

Attributes included from TheoryComponent

#theory_component_id

Instance Method Summary collapse

Methods included from TheoryComponent

#accessors, #generate_theory_component_id, #tokens

Methods included from ActsAsCode

#write_structure

Constructor Details

#initialize(action, target_id, theory_component_id = nil) ⇒ TheoryAction

Returns a new instance of TheoryAction.

Parameters:

  • action

    A statement to insert into the main runtime method. e.g. action.write => “return var8”

  • target_id

    The id of the nested statement or method to add the generated statement to.

Raises:

  • (StandardError)


13
14
15
16
17
18
19
# File 'lib/theory/TheoryAction.rb', line 13

def initialize(action,target_id,theory_component_id=nil)
  raise StandardError.new('Expecting theory statement but was '+action.class.to_s) unless action.kind_of?(TheoryStatement)
  @action = action
  @target_id = target_id
  @theory_component_id = theory_component_id unless theory_component_id.nil?    
  generate_theory_component_id
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



7
8
9
# File 'lib/theory/TheoryAction.rb', line 7

def action
  @action
end

#target_idObject (readonly)

Returns the value of attribute target_id.



7
8
9
# File 'lib/theory/TheoryAction.rb', line 7

def target_id
  @target_id
end

Instance Method Details

#copyObject



77
78
79
# File 'lib/theory/TheoryAction.rb', line 77

def copy
  return TheoryAction.new(@action.copy,@target_id,@theory_component_id)
end

#describe(tab = 0) ⇒ Object

Provide a general description of the action, explaining the statement it will add to the runtime method and where. This is pseudo code and shouldn’t be interpretted at runtime. Use “write” for runtime actions.

Parameters:

  • tab (defaults to: 0)


28
29
30
31
# File 'lib/theory/TheoryAction.rb', line 28

def describe(tab=0)
  # TODO  That is nicer way to do tabs - I should make the other methods consistent with it
  return ("\t"*tab)+"<runtime_method>.add_statement_at(#{@action.describe},#{@target_id.describe})"
end

#map_to(mapping) ⇒ Object

Returns a action implementation with the theory variable declarations replaced with the values in the mapping hash.



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
75
# File 'lib/theory/TheoryAction.rb', line 46

def map_to(mapping)

  # Duplicate the current statement before it is rewritten
  rewritten_statement = @action.copy
  target_id = @target_id.copy
  
  # Find all containers of VariableDeclarations that declare a TheoryVariable
  containers = [rewritten_statement,target_id].select_all {|x| x.respond_to?(:has?)}
  theory_variable_containers = containers.select {|x| x.has? {|y| y.kind_of?(TheoryVariable)}}
  
  # Do any of the containers contain each other?
  # => TODO Need to prevent this
  all_theory_variables = Set.new
  theory_variable_containers.each do |x|
    results = x.select_all([]) {|y| y.kind_of?(TheoryVariable)}
    all_theory_variables += results
  end
  
  map = {}
  all_theory_variables.each do |x|
    next if mapping[x.theory_variable_id].nil?
    map[x] = mapping[x.theory_variable_id]  
  end
      
  rewritten_statement.replace_variables_alt!(map) 
  target_id.replace_variables_alt!(map)
  
  return ActionImplementation.new(rewritten_statement,target_id,@theory_component_id)
  
end

#statements_with_variable(variable_id) ⇒ Object



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

def statements_with_variable(variable_id)
  
  # Duplicate the current statement before it is rewritten
  rewritten_statement = @action.copy
  target_id = @target_id.copy
  
  # Find all containers of VariableDeclarations that declare a TheoryVariable
  containers = [rewritten_statement,target_id].select_all {|x| x.respond_to?(:has?)}
  theory_variable_containers = containers.select {|x| x.has? {|y| y.kind_of?(TheoryVariable)}}
  
  results = theory_variable_containers.select do |x|
    reg = eval '/var'+variable_id.to_s+'/'
    x.write.match(reg)
  end
  return results
  
end

#theory_variablesObject

Returns all the theory vairables in this theory action



39
40
41
# File 'lib/theory/TheoryAction.rb', line 39

def theory_variables
  return @action.select_all {|x| x.kind_of?(TheoryVariable)}
end

#write(tab = 0) ⇒ Object



33
34
35
# File 'lib/theory/TheoryAction.rb', line 33

def write(tab=0)
  return ("\t"*tab)+"<runtime_method>.add_statement_at(#{@action.write},#{@target_id.write})"
end