Class: Porolog::Value

Inherits:
Object show all
Defined in:
lib/porolog/value.rb

Overview

A Porolog::Value combines a value with a goal so that when the goal is closed, the value can be uninstantiated at the same time.

Defined Under Namespace

Classes: Error, GoalError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, goal) ⇒ Porolog::Value

Returns the Value.

Parameters:

  • value (Object)

    the value to be associated with a Goal.

  • goal (Porolog::Goal)

    the Goal to be associated.

Raises:



32
33
34
35
36
37
38
39
40
# File 'lib/porolog/value.rb', line 32

def initialize(value, goal)
  raise GoalError, "Not a Goal: #{goal.inspect}" unless goal.is_a?(Goal)
  
  @value = value
  @value = value.value if value.is_a?(Value)
  @goal  = goal
  
  @instantiations = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Passes on methods to the Value’s value.



72
73
74
# File 'lib/porolog/value.rb', line 72

def method_missing(method, *args, &block)
  @value.send(method, *args, &block)
end

Instance Attribute Details

#goalPorolog::Goal

Returns The goal in which the value was instantiated.

Returns:

  • (Porolog::Goal)

    The goal in which the value was instantiated.



20
21
22
23
24
25
26
27
28
29
30
31
32
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/porolog/value.rb', line 20

class Value
  
  # Error class for rescuing or detecting any Value error.
  class Error     < PorologError ; end
  # Error class indicating that the supplied goal is not actually a goal.
  class GoalError < Error        ; end
  
  attr_accessor :goal, :instantiations
  
  # @param value [Object] the value to be associated with a Goal.
  # @param goal [Porolog::Goal] the Goal to be associated.
  # @return [Porolog::Value] the Value.
  def initialize(value, goal)
    raise GoalError, "Not a Goal: #{goal.inspect}" unless goal.is_a?(Goal)
    
    @value = value
    @value = value.value if value.is_a?(Value)
    @goal  = goal
    
    @instantiations = []
  end
  
  # Pretty presentation.
  # @return [String] the inspect of the value prefixed by the goal id.
  def inspect
    "#{@goal.myid}.#{@value.inspect}"
  end
  
  # Pretty presentation with instantiations and indexes.
  # This method is for polymorphic compatibility with Porolog::Variable.
  # It used by Porolog::Goal#inspect_variables.
  # @param visited [Array] the values already visited (to prevent infinite recursion).
  # @param depth [Integer] the level of indentation that shows containment.
  # @param index [Integer,Symbol,Array] the index into this value.
  # @param self_index [Integer,Symbol,Array] the index of which this value belongs.
  # @return [String] the inspect of the value in the context of the variables of a goal.
  def inspect_with_instantiations(visited = [], depth = 0, index = nil, self_index = nil)
    index_str = index && "[#{index.inspect}]" || ''
    prefix    = self_index&.inspect || ''
    
    "#{'  ' * depth}#{prefix}#{inspect}#{index_str}"
  end
  
  # Uninstantiate the Value.
  # @return [Boolean] true
  def remove
    @instantiations.dup.each(&:remove)
    @instantiations[0..-1] = []
    true
  end
  
  # Passes on methods to the Value's value.
  def method_missing(method, *args, &block)
    @value.send(method, *args, &block)
  end
  
  # Responds to all the Value's value methods as well as its own.
  # @return [Boolean] whether the value responds to the method.
  def respond_to?(method, include_all = false)
    @value.respond_to?(method, include_all) || super
  end
  
  # @return [Object] the value of the Value.
  def value(*)
    @value
  end
  
  # @return [Symbol] the type of the value.
  def type
    @value.type
  end
  
  # Compares Values for equality.
  # @return [Boolean] whether the Values' values are equal.
  def ==(other)
    @value == other.value
  end
  
  # @return [Array<Porolog::Variable,Symbol>] variables embedded in the value.
  def variables
    @value.variables
  end
  
end

#instantiationsArray<Porolog::Instantiation>

Returns Instantiations of this value.

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/porolog/value.rb', line 20

class Value
  
  # Error class for rescuing or detecting any Value error.
  class Error     < PorologError ; end
  # Error class indicating that the supplied goal is not actually a goal.
  class GoalError < Error        ; end
  
  attr_accessor :goal, :instantiations
  
  # @param value [Object] the value to be associated with a Goal.
  # @param goal [Porolog::Goal] the Goal to be associated.
  # @return [Porolog::Value] the Value.
  def initialize(value, goal)
    raise GoalError, "Not a Goal: #{goal.inspect}" unless goal.is_a?(Goal)
    
    @value = value
    @value = value.value if value.is_a?(Value)
    @goal  = goal
    
    @instantiations = []
  end
  
  # Pretty presentation.
  # @return [String] the inspect of the value prefixed by the goal id.
  def inspect
    "#{@goal.myid}.#{@value.inspect}"
  end
  
  # Pretty presentation with instantiations and indexes.
  # This method is for polymorphic compatibility with Porolog::Variable.
  # It used by Porolog::Goal#inspect_variables.
  # @param visited [Array] the values already visited (to prevent infinite recursion).
  # @param depth [Integer] the level of indentation that shows containment.
  # @param index [Integer,Symbol,Array] the index into this value.
  # @param self_index [Integer,Symbol,Array] the index of which this value belongs.
  # @return [String] the inspect of the value in the context of the variables of a goal.
  def inspect_with_instantiations(visited = [], depth = 0, index = nil, self_index = nil)
    index_str = index && "[#{index.inspect}]" || ''
    prefix    = self_index&.inspect || ''
    
    "#{'  ' * depth}#{prefix}#{inspect}#{index_str}"
  end
  
  # Uninstantiate the Value.
  # @return [Boolean] true
  def remove
    @instantiations.dup.each(&:remove)
    @instantiations[0..-1] = []
    true
  end
  
  # Passes on methods to the Value's value.
  def method_missing(method, *args, &block)
    @value.send(method, *args, &block)
  end
  
  # Responds to all the Value's value methods as well as its own.
  # @return [Boolean] whether the value responds to the method.
  def respond_to?(method, include_all = false)
    @value.respond_to?(method, include_all) || super
  end
  
  # @return [Object] the value of the Value.
  def value(*)
    @value
  end
  
  # @return [Symbol] the type of the value.
  def type
    @value.type
  end
  
  # Compares Values for equality.
  # @return [Boolean] whether the Values' values are equal.
  def ==(other)
    @value == other.value
  end
  
  # @return [Array<Porolog::Variable,Symbol>] variables embedded in the value.
  def variables
    @value.variables
  end
  
end

Instance Method Details

#==(other) ⇒ Boolean

Compares Values for equality.

Returns:

  • (Boolean)

    whether the Values’ values are equal.



94
95
96
# File 'lib/porolog/value.rb', line 94

def ==(other)
  @value == other.value
end

#inspectString

Pretty presentation.

Returns:

  • (String)

    the inspect of the value prefixed by the goal id.



44
45
46
# File 'lib/porolog/value.rb', line 44

def inspect
  "#{@goal.myid}.#{@value.inspect}"
end

#inspect_with_instantiations(visited = [], depth = 0, index = nil, self_index = nil) ⇒ String

Pretty presentation with instantiations and indexes. This method is for polymorphic compatibility with Porolog::Variable. It used by Porolog::Goal#inspect_variables.

Parameters:

  • visited (Array) (defaults to: [])

    the values already visited (to prevent infinite recursion).

  • depth (Integer) (defaults to: 0)

    the level of indentation that shows containment.

  • index (Integer, Symbol, Array) (defaults to: nil)

    the index into this value.

  • self_index (Integer, Symbol, Array) (defaults to: nil)

    the index of which this value belongs.

Returns:

  • (String)

    the inspect of the value in the context of the variables of a goal.



56
57
58
59
60
61
# File 'lib/porolog/value.rb', line 56

def inspect_with_instantiations(visited = [], depth = 0, index = nil, self_index = nil)
  index_str = index && "[#{index.inspect}]" || ''
  prefix    = self_index&.inspect || ''
  
  "#{'  ' * depth}#{prefix}#{inspect}#{index_str}"
end

#removeBoolean

Uninstantiate the Value.

Returns:

  • (Boolean)

    true



65
66
67
68
69
# File 'lib/porolog/value.rb', line 65

def remove
  @instantiations.dup.each(&:remove)
  @instantiations[0..-1] = []
  true
end

#respond_to?(method, include_all = false) ⇒ Boolean

Responds to all the Value’s value methods as well as its own.

Returns:

  • (Boolean)

    whether the value responds to the method.



78
79
80
# File 'lib/porolog/value.rb', line 78

def respond_to?(method, include_all = false)
  @value.respond_to?(method, include_all) || super
end

#typeSymbol

Returns the type of the value.

Returns:

  • (Symbol)

    the type of the value.



88
89
90
# File 'lib/porolog/value.rb', line 88

def type
  @value.type
end

#valueObject

Returns the value of the Value.

Returns:

  • (Object)

    the value of the Value.



83
84
85
# File 'lib/porolog/value.rb', line 83

def value(*)
  @value
end

#variablesArray<Porolog::Variable,Symbol>

Returns variables embedded in the value.

Returns:



99
100
101
# File 'lib/porolog/value.rb', line 99

def variables
  @value.variables
end