Module: ContainsTheories

Included in:
Cauldron::Pot
Defined in:
lib/theories.rb

Overview

TODO Allot of this functionalty should be moved to the rake tasks

Instance Method Summary collapse

Instance Method Details

#create_theoryObject

This method is used on to create a theory fixture for use in the test



99
100
101
102
103
104
105
106
107
108
109
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/theories.rb', line 99

def create_theory
  
  theory_id = "16"
 
  StandardLogger.instance.warning("NOTE: Creating new theory fixture #{theory_id.to_s}")
  # Does a directory for this theory fixture already exist.
  directory_path = $LOC+File.join('test','fixtures','theories',theory_id.to_s)
  if(File.exists?(directory_path))
    StandardLogger.instance.warning("Theory ficture already exists #{theory_id.to_s} -cancelling")
    return
  else
    
    # Create the directory
    FileUtils.mkdir_p(directory_path)
    
  end
  
  # This is just updated here - nothing fancy
  declaration = %q!
  dependent_one = TheoryDependent.new(
    StringToTheory.run(
      "if(var1.kind_of?(RuntimeMethod))\nreturn true\nend"
    )
  )     
  dependent_two = TheoryDependent.new(
    StringToTheory.run(
      "if(var3[var4].kind_of?(CTestCase))\nreturn true\nend"
    )
  )     
  action = TheoryAction.new(
    TheoryStatement.new(
      StringToTheory.run(
        'BlockStatement.new(Statement.new(InstanceCallContainer.new(Container.new(InstanceCallContainer.new(var1.params[var2],StringLength.new),Subtract.new,1),Times.new)))'
      )
    ),
    StringToTheory.run('var1.statement_id')
  )
  result_one = TheoryResult.new(
    StringToTheory.run(
      "if(var1.history2(var3[var4]).select {|x| x['statement_id'] == var3.last.statement_id} == var3[var4][:params][var2].length-1)\nreturn true\nend"
    )
  )
    Theory.new(
      [dependent_one,dependent_two],
      action,
      [result_one]
    )    
  !
  
  # Save the declaration (incase the theory needs modified
  declaration_file = File.open(File.join(directory_path,'declaration.txt'),'w+')
  declaration_file << declaration
  declaration_file.close    
  
  theory = eval declaration
  data = Marshal.dump(theory)

  # TODO  Shouldn't include colours for this description
  desc_file = File.open(File.join(directory_path,'desc'),'w+')
  desc_file << theory.describe
  desc_file.close
  
  dump_file = File.open(File.join(directory_path,'dump'),'w+')
  dump_file << data
  dump_file.close

end

#create_theory_implementationObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/theories.rb', line 46

def create_theory_implementation
  
  theory = Theory.load_theory(6)
  potential_values = [
      Parser.run('test_cases[0][:params][0]'),
      Parser.run('test_cases[0][:params]'),
      Parser.run('test_cases[0][:output]'),
      Parser.run('test_cases[0]'),
      Parser.run('test_cases'),
      Parser.run('runtime_method')
  ]
  results = theory.rewrite_permutations(potential_values)
  # NOTE: Rember use grep -A and -B to match particular cases
  implemented_theory = results[332]
  return implemented_theory
  
end

#load_implementation_result(result_id) ⇒ Object

Raises:

  • (StandardError)


91
92
93
94
95
96
# File 'lib/theories.rb', line 91

def load_implementation_result(result_id)
  directory_path = $LOC+File.join('test','fixtures','implementation_results',result_id.to_s)
  raise StandardError.new("ImplementationResult fixture #{result_id} does not exist") unless(File.exists?(directory_path))
  dump_file = File.open(File.join(directory_path,'dump'),'r')
  return Marshal.load(dump_file)    
end

#load_theory_implementation(implementation_id) ⇒ Object

Raises:

  • (StandardError)


39
40
41
42
43
44
# File 'lib/theories.rb', line 39

def load_theory_implementation(implementation_id)
  directory_path = $LOC+File.join('test','fixtures','theory_implementations',implementation_id.to_s)
  raise StandardError.new("Theory fixture #{implementation_id} does not exist") unless(File.exists?(directory_path))
  dump_file = File.open(File.join(directory_path,'dump'),'r')
  return Marshal.load(dump_file.read)
end

#save_theory_implementation(implementation_id, theory_id, implementation = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/theories.rb', line 64

def save_theory_implementation(implementation_id,theory_id,implementation=nil)
  StandardLogger.instance.warning("Caution: Creating new theory implementation #{implementation_id}")
  directory_path = $LOC+File.join('test','fixtures','theory_implementations',implementation_id.to_s)
  if(File.exists?(directory_path))
    StandardLogger.instance.warning("Theory Implementation ficture already exists #{theory_id} -cancelling")
    return
  else
    FileUtils.mkdir_p(directory_path)
  end 
  
  if implementation.nil? then implementation = create_theory_implementation end
  
  desc_file = File.open(File.join(directory_path,'desc.txt'),'w')
  desc_file << implementation.describe
  desc_file.close    
  
  dump_file = File.open(File.join(directory_path,'dump'),'w')
  data = Marshal.dump(implementation)
  dump_file << data
  dump_file.close
  
  dump_file = File.open(File.join(directory_path,'theory_id'),'w')
  dump_file << theory_id.to_s
  dump_file.close    
  
end

#setupObject



9
10
11
12
13
14
# File 'lib/theories.rb', line 9

def setup
  @theory_1 = Theory.load_theory(1)
  @theory_2 = Theory.load_theory(2)
  #@theory_3 = Theory.load_theory(3)
  @theory_3 = theory_3    
end

#theory_3Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/theories.rb', line 16

def theory_3
  
  dependent_one = TheoryDependent.new(
    StringToTheory.run(
      "if(var1.realise2(var2[var3][:params]).params[0].value.length == var2[var3][:output])\nreturn true\nend"
    )
  )
  action_one = TheoryAction.new(
    TheoryStatement.new(
      StringToTheory.run(
        'Statement.new(Return.new,InstanceCallContainer.new(var1.params[0],StringLength.new))'
      )             
    ),
    StringToTheory.run("var1.statement_id") 
  )
  result_one = TheoryResult.new(
    StringToTheory.run("if(var1.all_pass?(var2))\nreturn true\nend"),true
  )      
  Theory.new([dependent_one],action_one,[result_one]) 
      
  
end