Class: TheoryResult

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

Overview

This instances of this class describe the consequence of a theory, e.g. if the the theories dependencies are met and the action complete then this will be true.

Instance Attribute Summary collapse

Attributes included from TheoryComponent

#theory_component_id

Instance Method Summary collapse

Methods included from TheoryComponent

#accessors, #generate_theory_component_id, #statements_with_variable, #theory_variables, #tokens

Methods included from ActsAsCode

#write_structure

Constructor Details

#initialize(statement, result = nil, theory_component_id = nil) ⇒ TheoryResult

Returns a new instance of TheoryResult.

Parameters:

  • statement

    A theory statement explaining what will be tested e.g. <runtime_method>.all_pass?(<test_cases>)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/theory/TheoryResult.rb', line 14

def initialize(statement,result=nil,theory_component_id=nil)
  
  # TEMPORARY - Deprecaction Change:
  unless result.nil? then StandardLogger.instance.warning('Result is being phased out - just use if statements') end
  unless statement.kind_of?(OpenStatement) 
    StandardLogger.instance.warning('Please use open statements for theory results') 
  end 
  unless statement.length > 0 
    StandardLogger.instance.warning('Please include a return true statement in the theory result')
  end      
  
  @statement = statement
  @result = result
  @theory_component_id = theory_component_id unless theory_component_id.nil?
  generate_theory_component_id
end

Instance Attribute Details

#statementObject (readonly)

Returns the value of attribute statement.



8
9
10
# File 'lib/theory/TheoryResult.rb', line 8

def statement
  @statement
end

Instance Method Details

#copyObject



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

def copy
  #return TheoryResult.new(@statement.copy,@result)
  return Marshal.load(Marshal.dump(self))
end

#describe(tab = 0) ⇒ Object



31
32
33
# File 'lib/theory/TheoryResult.rb', line 31

def describe(tab=0)
  return @statement.describe(tab)
end

#map_to(mapping) ⇒ Object

Returns a new theory result with the theory variables replaced with the values in the mapping hash.

}

Parameters:

  • mapping (1=>[], 2=>#<IntrinsicTestCases:0xb712d13c)

    , 3=>#<IntrinsicRuntimeMethod:0xb712c980>



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/theory/TheoryResult.rb', line 52

def map_to(mapping)
  
  # Duplicate the current statement before it is rewritten
  rewritten_statement = @statement.copy
  
  # Find all the containers that contain TheoryVariables
  # NOTE  The statement is put in an array because select all doesn't include the array itself
  containers = [rewritten_statement].select_all {|x| x.respond_to?(:has?)}
  theory_variable_containers = containers.select {|x| x.has? {|y| y.kind_of?(TheoryVariable)}}
  
  # Rewrite the statement replacing the values
  theory_variable_containers.each do |z|
    z.replace_theory_variables!(mapping)
  end     
  
  return TheoryResult.new(rewritten_statement,nil,theory_component_id)
end

#mapping_for(statement) ⇒ Object

Returns a mapping hash that will convert this result to the statement provided.



73
74
75
76
77
78
79
# File 'lib/theory/TheoryResult.rb', line 73

def mapping_for(statement)
  mapping = Mapping.new()
  @statement.tokens.zip(statement.tokens) do |x,y|
    mapping[x.theory_variable_id] = y
  end
  return mapping
end

#same_structure?(structure) ⇒ Boolean

Returns true if supplied statement has the same structure as this theory result’s, it has the same structure if everything about the statement is the same except for the variables. This means that the map_to method could be used to make it write identically.

Returns:



86
87
88
89
90
# File 'lib/theory/TheoryResult.rb', line 86

def same_structure?(structure)
    return false unless structure.tokens.length == @statement.tokens.length
    return false unless structure.write_structure == @statement.write_structure
    return true
end

#validates?(runtime_method, test_cases) ⇒ Boolean

Returns true if theory result is true with the supplied runtime method and test cases. The paramter are ‘rutime_method’ and ‘test_cases’ so the eval can use them.

TODO Write tests for this

Returns:



97
98
99
100
101
102
# File 'lib/theory/TheoryResult.rb', line 97

def validates?(runtime_method,test_cases)
  catch :theory_result_failed do 
    return true if use_statement(runtime_method,test_cases) == true
  end
  return false
end

#write(tab = 0) ⇒ Object



35
36
37
# File 'lib/theory/TheoryResult.rb', line 35

def write(tab=0)
  return @statement.write(tab)
end