Class: Porolog::Goal
Overview
A Porolog::Goal finds solutions for specific Arguments of a Predicate.
Defined Under Namespace
Classes: DefinitionError, Error, NotVariableError
Instance Attribute Summary collapse
-
#arguments ⇒ Porolog::Arguments
The specific Arguments this goal is trying to solve.
-
#calling_goal ⇒ Porolog::Goal?
The parent goal if this goal is a subgoal, otherwise nil.
-
#description ⇒ String
Description of the Arguments the Goal is attempting to solve.
-
#index ⇒ Integer?
Solution index for builtin predicates.
-
#log ⇒ Array<String>
Log of events, namely unification failures.
-
#result ⇒ Boolean
Whether any result has been found.
Class Method Summary collapse
-
.goal_count ⇒ Integer
The number of goals created.
-
.goals ⇒ Array<Porolog::Goal>
A convenience method for testing/debugging.
-
.reset ⇒ Boolean
Clears all goals and ensures they are deleted.
Instance Method Summary collapse
-
#ancestors ⇒ Array<Porolog::Goal>
The calling chain for this goal.
-
#ancestry ⇒ String
A convenience method for testing/debugging.
-
#check_deleted ⇒ Boolean
Determines whether the Goal has been deleted and removes its Variables and Values if it has.
-
#delete! ⇒ Boolean
Delete the Goal.
-
#deleted? ⇒ Boolean
Whether the Goal has been deleted (memoized).
-
#inherit_variables(other_goal = @calling_goal) ⇒ Boolean
Inherits variables and their instantiations from another goal.
-
#initialize(arguments, calling_goal = nil) ⇒ Goal
constructor
Initializes and registers the Goal.
-
#inspect ⇒ String
Pretty representation.
-
#inspect_variables ⇒ String
A convenience method for testing/debugging.
-
#instantiate(name, other, other_goal = self) ⇒ Porolog::Instantiation
Instantiates a Variable to another Variable or Value, for this Goal.
-
#myid ⇒ String
A convenience method for testing/debugging.
-
#satisfy(&block) ⇒ Boolean
Solves the goal but not as the root goal for a query.
-
#solve(max_solutions = nil) ⇒ Array<Hash{Symbol => Object}>
Finds all possible solutions to the Predicate for the specific Arguments.
-
#terminate! ⇒ Boolean
Sets that the goal should no longer search any further rules for the current predicate once the current rule has finished being evaluated.
-
#terminated? ⇒ Boolean
Whether the goal has been cut.
-
#value(value) ⇒ Porolog::Value
A Value, ensuring it has been associated with this Goal so that it can be uninstantiated.
-
#value_of(name, index = nil, visited = []) ⇒ Object
Returns the value of the indicated variable or value.
-
#values ⇒ Array<Object>
A convenience method for testing/debugging.
-
#values_of(object) ⇒ Object+
Returns the values of an Object.
-
#variable(name) ⇒ Porolog::Variable, Object
Finds or tries to create a variable in the goal (as much as possible) otherwise passes the parameter back.
-
#variables ⇒ Hash{Symbol => Object}
The Variables and their current values of this Goal.
-
#variablise(object) ⇒ Object
(also: #[])
Converts all embedded Symbols to Variables within this Goal.
Constructor Details
#initialize(arguments, calling_goal = nil) ⇒ Goal
Initializes and registers the Goal.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/porolog/goal.rb', line 59 def initialize(arguments, calling_goal = nil) @@goals << self @@goal_count += 1 @arguments = arguments @terminate = false @calling_goal = calling_goal @variables = {} @values = {} @result = nil @description = "Solve #{arguments.inspect}" @log = [] @arguments = @arguments.dup(self) if @arguments.is_a?(Arguments) @solutions = [] end |
Instance Attribute Details
#arguments ⇒ Porolog::Arguments
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 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/porolog/goal.rb', line 27 class Goal # Error class for rescuing or detecting any Goal error. class Error < PorologError ; end # Error class indicating an unexpected type of Rule definition. class DefinitionError < Error ; end # Error class indicating a non-Variable was attempted to be instantiated. class NotVariableError < Error ; end # Clears all goals and ensures they are deleted. # @return [Boolean] success def self.reset @@goals ||= [] goals = @@goals @@goals = [] @@goal_count = 0 goals.map(&:deleted?) true end # @return [Integer] the number of goals created def self.goal_count @@goal_count end reset attr_accessor :calling_goal, :arguments, :index, :result, :description, :log # Initializes and registers the Goal. # @param arguments [Porolog::Arguments] the Predicate Arguments that this Goal is to solve. # @param calling_goal [Porolog::Goal] the parent Goal if this Goal is a subgoal. def initialize(arguments, calling_goal = nil) @@goals << self @@goal_count += 1 @arguments = arguments @terminate = false @calling_goal = calling_goal @variables = {} @values = {} @result = nil @description = "Solve #{arguments.inspect}" @log = [] @arguments = @arguments.dup(self) if @arguments.is_a?(Arguments) @solutions = [] end # @return [Array<Porolog::Goal>] the calling chain for this goal. def ancestors ancestors = [] goal = self while goal ancestors << goal goal = goal.calling_goal end ancestors.reverse end # A convenience method for testing/debugging. # @return [String] the calling chain for this goal as a String. def ancestry indent = -1 ancestors.map do |goal| indent += 1 "#{' ' * indent}#{goal.myid} -- #{goal.description} #{goal.variables.inspect}" end.join("\n") end # A convenience method for testing/debugging. # @return [String] the id of the goal. def myid "Goal#{(@@goals.index(self) || -1000) + 1}" end # @return [String] pretty representation. def inspect "#{myid} -- #{description}" end # A convenience method for testing/debugging. # @return [Array<Porolog::Goal>] def self.goals @@goals end # Delete the Goal # @return [Boolean] whether the Goal has been deleted (memoized) def delete! @@goals.delete(self) deleted? end # @return [Boolean] whether the Goal has been deleted (memoized) def deleted? @deleted ||= check_deleted @deleted end # Determines whether the Goal has been deleted and removes its Variables and Values if it has. # @return [Boolean] whether the Goal has been deleted def check_deleted return false if @@goals.include?(self) @variables.delete_if do |_name,variable| variable.remove true end @values.delete_if do |_name,value| value.remove true end true end # Sets that the goal should no longer search any further rules for the current predicate # once the current rule has finished being evaluated. # This method implements the :CUT rule. # That is, backtracking does not go back past the point of the cut. # @return [Boolean] true. def terminate! @log << 'terminating' @terminate = true end # @return [Boolean] whether the goal has been cut. def terminated? @terminate end # Converts all embedded Symbols to Variables within this Goal. # @param object [Object] the object to variablise. # @return [Object] the variablised object. def variablise(object) case object when Symbol,Variable variable(object) when Array object.map{|element| variablise(element) } when Arguments object.dup(self) when Tail Tail.new variablise(object.value) when Value object when NilClass nil else if [UNKNOWN_TAIL, UNKNOWN_ARRAY].include?(object) object else value(object) end end end alias [] variablise # @return [Hash{Symbol => Object}] the Variables and their current values of this Goal def variables @variables.keys.each_with_object({}){|variable,variable_list| value = value_of(variable).value.value if value.is_a?(Variable) variable_list[variable] = nil elsif value.is_a?(Array) variable_list[variable] = value.clean else variable_list[variable] = value end } end # A convenience method for testing/debugging. # @return [String] a tree representation of all the instantiations of this goal's variables. def inspect_variables @variables.values.map(&:inspect_with_instantiations).join("\n") end # A convenience method for testing/debugging. # @return [Array<Object>] the values instantiated for this goal. def values @values.values.map(&:value) end # Finds or tries to create a variable in the goal (as much as possible) otherwise passes the parameter back. # @param name [Object] name of variable or variable # @return [Porolog::Variable, Object] a variable of the goal or the original parameter def variable(name) return name unless name.is_a?(Symbol) || name.is_a?(Variable) if name.is_a?(Variable) variable = name @variables[variable.name] = variable if variable.goal == self return variable end @variables[name] ||= (name.is_a?(Variable) && name || Variable.new(name, self)) @variables[name] end # @param value [Object] the value that needs to be associated with this Goal. # @return [Porolog::Value] a Value, ensuring it has been associated with this Goal so that it can be uninstantiated. def value(value) @values[value] ||= Value.new(value, self) @values[value] end # Returns the value of the indicated variable or value # @param name [Symbol, Object] name of the variable or value # @param index [Object] index is not yet used # @param visited [Array] used to avoid infinite recursion # @return [Object] the value def value_of(name, index = nil, visited = []) variable(name).value(visited) end # Returns the values of an Object. For most objects, this will basically just be the object. # For Arrays, an Array will be returned where the elements are the value of the elements. # @param object [Object] the Object to find the values of. # @return [Object,Array<Object>] the value(s) of the object. def values_of(object) case object when Array object.map{|element| values_of(element) } when Variable,Symbol,Value value_of(object) when Tail # TODO: This needs to splat outwards; in the meantime, it splats just the first. value_of(object.value).first else object end end # Finds all possible solutions to the Predicate for the specific Arguments. # @param max_solutions [Integer] if provided, the search is stopped once the number of solutions has been found # @return [Array<Hash{Symbol => Object}>] def solve(max_solutions = nil) return @solutions unless @arguments predicate = @arguments.predicate predicate&.satisfy(self) do |goal| # TODO: Refactor to overrideable method (or another solution, say a lambda) @solutions << variables @log << "SOLUTION: #{variables}" @log << goal.ancestry @log << goal.inspect_variables return @solutions if max_solutions && @solutions.size >= max_solutions end @solutions end # Solves the goal but not as the root goal for a query. # That is, solves an intermediate goal. # @param block [Proc] code to perform when the Goal is satisfied. # @return [Boolean] whether the definition was satisfied. def satisfy(&block) return false unless @arguments predicate = @arguments.predicate satisfied = false predicate&.satisfy(self) do |subgoal| subgoal_satisfied = block.call(subgoal) satisfied ||= subgoal_satisfied end satisfied end # Instantiates a Variable to another Variable or Value, for this Goal. # @param name [Symbol,Porolog::Variable,Object] the name that references the variable to be instantiated. # @param other [Object] the value that the variable is to be instantiated to. # @param other_goal [Porolog::Goal,nil] the Goal of the other value. # @return [Porolog::Instantiation] the instantiation. def instantiate(name, other, other_goal = self) variable = self.variable(name) other = if other.type == :variable other_goal.variable(other) else other_goal.value(other) end variable.instantiate(other) end # Inherits variables and their instantiations from another goal. # @param other_goal [Porolog::Goal,nil] the Goal to inherit variables from. # @return [Boolean] whether the variables could be inherited (unified). def inherit_variables(other_goal = @calling_goal) return true unless other_goal unified = true unifications = [] variables = ( other_goal.arguments.variables + other_goal.variables.keys + self.arguments.variables ).map(&:to_sym).uniq variables.each do |variable| name = variable unification = Porolog::unify(name, name, other_goal, self) unified &&= !!unification if unified unifications += unification else #:nocov: self.log << "Couldn't unify: #{name.inspect} WITH #{other_goal.myid} AND #{self.myid}" break #:nocov: end end unified &&= Porolog::instantiate_unifications(unifications) if unified unified end end |
#calling_goal ⇒ Porolog::Goal?
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 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/porolog/goal.rb', line 27 class Goal # Error class for rescuing or detecting any Goal error. class Error < PorologError ; end # Error class indicating an unexpected type of Rule definition. class DefinitionError < Error ; end # Error class indicating a non-Variable was attempted to be instantiated. class NotVariableError < Error ; end # Clears all goals and ensures they are deleted. # @return [Boolean] success def self.reset @@goals ||= [] goals = @@goals @@goals = [] @@goal_count = 0 goals.map(&:deleted?) true end # @return [Integer] the number of goals created def self.goal_count @@goal_count end reset attr_accessor :calling_goal, :arguments, :index, :result, :description, :log # Initializes and registers the Goal. # @param arguments [Porolog::Arguments] the Predicate Arguments that this Goal is to solve. # @param calling_goal [Porolog::Goal] the parent Goal if this Goal is a subgoal. def initialize(arguments, calling_goal = nil) @@goals << self @@goal_count += 1 @arguments = arguments @terminate = false @calling_goal = calling_goal @variables = {} @values = {} @result = nil @description = "Solve #{arguments.inspect}" @log = [] @arguments = @arguments.dup(self) if @arguments.is_a?(Arguments) @solutions = [] end # @return [Array<Porolog::Goal>] the calling chain for this goal. def ancestors ancestors = [] goal = self while goal ancestors << goal goal = goal.calling_goal end ancestors.reverse end # A convenience method for testing/debugging. # @return [String] the calling chain for this goal as a String. def ancestry indent = -1 ancestors.map do |goal| indent += 1 "#{' ' * indent}#{goal.myid} -- #{goal.description} #{goal.variables.inspect}" end.join("\n") end # A convenience method for testing/debugging. # @return [String] the id of the goal. def myid "Goal#{(@@goals.index(self) || -1000) + 1}" end # @return [String] pretty representation. def inspect "#{myid} -- #{description}" end # A convenience method for testing/debugging. # @return [Array<Porolog::Goal>] def self.goals @@goals end # Delete the Goal # @return [Boolean] whether the Goal has been deleted (memoized) def delete! @@goals.delete(self) deleted? end # @return [Boolean] whether the Goal has been deleted (memoized) def deleted? @deleted ||= check_deleted @deleted end # Determines whether the Goal has been deleted and removes its Variables and Values if it has. # @return [Boolean] whether the Goal has been deleted def check_deleted return false if @@goals.include?(self) @variables.delete_if do |_name,variable| variable.remove true end @values.delete_if do |_name,value| value.remove true end true end # Sets that the goal should no longer search any further rules for the current predicate # once the current rule has finished being evaluated. # This method implements the :CUT rule. # That is, backtracking does not go back past the point of the cut. # @return [Boolean] true. def terminate! @log << 'terminating' @terminate = true end # @return [Boolean] whether the goal has been cut. def terminated? @terminate end # Converts all embedded Symbols to Variables within this Goal. # @param object [Object] the object to variablise. # @return [Object] the variablised object. def variablise(object) case object when Symbol,Variable variable(object) when Array object.map{|element| variablise(element) } when Arguments object.dup(self) when Tail Tail.new variablise(object.value) when Value object when NilClass nil else if [UNKNOWN_TAIL, UNKNOWN_ARRAY].include?(object) object else value(object) end end end alias [] variablise # @return [Hash{Symbol => Object}] the Variables and their current values of this Goal def variables @variables.keys.each_with_object({}){|variable,variable_list| value = value_of(variable).value.value if value.is_a?(Variable) variable_list[variable] = nil elsif value.is_a?(Array) variable_list[variable] = value.clean else variable_list[variable] = value end } end # A convenience method for testing/debugging. # @return [String] a tree representation of all the instantiations of this goal's variables. def inspect_variables @variables.values.map(&:inspect_with_instantiations).join("\n") end # A convenience method for testing/debugging. # @return [Array<Object>] the values instantiated for this goal. def values @values.values.map(&:value) end # Finds or tries to create a variable in the goal (as much as possible) otherwise passes the parameter back. # @param name [Object] name of variable or variable # @return [Porolog::Variable, Object] a variable of the goal or the original parameter def variable(name) return name unless name.is_a?(Symbol) || name.is_a?(Variable) if name.is_a?(Variable) variable = name @variables[variable.name] = variable if variable.goal == self return variable end @variables[name] ||= (name.is_a?(Variable) && name || Variable.new(name, self)) @variables[name] end # @param value [Object] the value that needs to be associated with this Goal. # @return [Porolog::Value] a Value, ensuring it has been associated with this Goal so that it can be uninstantiated. def value(value) @values[value] ||= Value.new(value, self) @values[value] end # Returns the value of the indicated variable or value # @param name [Symbol, Object] name of the variable or value # @param index [Object] index is not yet used # @param visited [Array] used to avoid infinite recursion # @return [Object] the value def value_of(name, index = nil, visited = []) variable(name).value(visited) end # Returns the values of an Object. For most objects, this will basically just be the object. # For Arrays, an Array will be returned where the elements are the value of the elements. # @param object [Object] the Object to find the values of. # @return [Object,Array<Object>] the value(s) of the object. def values_of(object) case object when Array object.map{|element| values_of(element) } when Variable,Symbol,Value value_of(object) when Tail # TODO: This needs to splat outwards; in the meantime, it splats just the first. value_of(object.value).first else object end end # Finds all possible solutions to the Predicate for the specific Arguments. # @param max_solutions [Integer] if provided, the search is stopped once the number of solutions has been found # @return [Array<Hash{Symbol => Object}>] def solve(max_solutions = nil) return @solutions unless @arguments predicate = @arguments.predicate predicate&.satisfy(self) do |goal| # TODO: Refactor to overrideable method (or another solution, say a lambda) @solutions << variables @log << "SOLUTION: #{variables}" @log << goal.ancestry @log << goal.inspect_variables return @solutions if max_solutions && @solutions.size >= max_solutions end @solutions end # Solves the goal but not as the root goal for a query. # That is, solves an intermediate goal. # @param block [Proc] code to perform when the Goal is satisfied. # @return [Boolean] whether the definition was satisfied. def satisfy(&block) return false unless @arguments predicate = @arguments.predicate satisfied = false predicate&.satisfy(self) do |subgoal| subgoal_satisfied = block.call(subgoal) satisfied ||= subgoal_satisfied end satisfied end # Instantiates a Variable to another Variable or Value, for this Goal. # @param name [Symbol,Porolog::Variable,Object] the name that references the variable to be instantiated. # @param other [Object] the value that the variable is to be instantiated to. # @param other_goal [Porolog::Goal,nil] the Goal of the other value. # @return [Porolog::Instantiation] the instantiation. def instantiate(name, other, other_goal = self) variable = self.variable(name) other = if other.type == :variable other_goal.variable(other) else other_goal.value(other) end variable.instantiate(other) end # Inherits variables and their instantiations from another goal. # @param other_goal [Porolog::Goal,nil] the Goal to inherit variables from. # @return [Boolean] whether the variables could be inherited (unified). def inherit_variables(other_goal = @calling_goal) return true unless other_goal unified = true unifications = [] variables = ( other_goal.arguments.variables + other_goal.variables.keys + self.arguments.variables ).map(&:to_sym).uniq variables.each do |variable| name = variable unification = Porolog::unify(name, name, other_goal, self) unified &&= !!unification if unified unifications += unification else #:nocov: self.log << "Couldn't unify: #{name.inspect} WITH #{other_goal.myid} AND #{self.myid}" break #:nocov: end end unified &&= Porolog::instantiate_unifications(unifications) if unified unified end end |
#description ⇒ String
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 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/porolog/goal.rb', line 27 class Goal # Error class for rescuing or detecting any Goal error. class Error < PorologError ; end # Error class indicating an unexpected type of Rule definition. class DefinitionError < Error ; end # Error class indicating a non-Variable was attempted to be instantiated. class NotVariableError < Error ; end # Clears all goals and ensures they are deleted. # @return [Boolean] success def self.reset @@goals ||= [] goals = @@goals @@goals = [] @@goal_count = 0 goals.map(&:deleted?) true end # @return [Integer] the number of goals created def self.goal_count @@goal_count end reset attr_accessor :calling_goal, :arguments, :index, :result, :description, :log # Initializes and registers the Goal. # @param arguments [Porolog::Arguments] the Predicate Arguments that this Goal is to solve. # @param calling_goal [Porolog::Goal] the parent Goal if this Goal is a subgoal. def initialize(arguments, calling_goal = nil) @@goals << self @@goal_count += 1 @arguments = arguments @terminate = false @calling_goal = calling_goal @variables = {} @values = {} @result = nil @description = "Solve #{arguments.inspect}" @log = [] @arguments = @arguments.dup(self) if @arguments.is_a?(Arguments) @solutions = [] end # @return [Array<Porolog::Goal>] the calling chain for this goal. def ancestors ancestors = [] goal = self while goal ancestors << goal goal = goal.calling_goal end ancestors.reverse end # A convenience method for testing/debugging. # @return [String] the calling chain for this goal as a String. def ancestry indent = -1 ancestors.map do |goal| indent += 1 "#{' ' * indent}#{goal.myid} -- #{goal.description} #{goal.variables.inspect}" end.join("\n") end # A convenience method for testing/debugging. # @return [String] the id of the goal. def myid "Goal#{(@@goals.index(self) || -1000) + 1}" end # @return [String] pretty representation. def inspect "#{myid} -- #{description}" end # A convenience method for testing/debugging. # @return [Array<Porolog::Goal>] def self.goals @@goals end # Delete the Goal # @return [Boolean] whether the Goal has been deleted (memoized) def delete! @@goals.delete(self) deleted? end # @return [Boolean] whether the Goal has been deleted (memoized) def deleted? @deleted ||= check_deleted @deleted end # Determines whether the Goal has been deleted and removes its Variables and Values if it has. # @return [Boolean] whether the Goal has been deleted def check_deleted return false if @@goals.include?(self) @variables.delete_if do |_name,variable| variable.remove true end @values.delete_if do |_name,value| value.remove true end true end # Sets that the goal should no longer search any further rules for the current predicate # once the current rule has finished being evaluated. # This method implements the :CUT rule. # That is, backtracking does not go back past the point of the cut. # @return [Boolean] true. def terminate! @log << 'terminating' @terminate = true end # @return [Boolean] whether the goal has been cut. def terminated? @terminate end # Converts all embedded Symbols to Variables within this Goal. # @param object [Object] the object to variablise. # @return [Object] the variablised object. def variablise(object) case object when Symbol,Variable variable(object) when Array object.map{|element| variablise(element) } when Arguments object.dup(self) when Tail Tail.new variablise(object.value) when Value object when NilClass nil else if [UNKNOWN_TAIL, UNKNOWN_ARRAY].include?(object) object else value(object) end end end alias [] variablise # @return [Hash{Symbol => Object}] the Variables and their current values of this Goal def variables @variables.keys.each_with_object({}){|variable,variable_list| value = value_of(variable).value.value if value.is_a?(Variable) variable_list[variable] = nil elsif value.is_a?(Array) variable_list[variable] = value.clean else variable_list[variable] = value end } end # A convenience method for testing/debugging. # @return [String] a tree representation of all the instantiations of this goal's variables. def inspect_variables @variables.values.map(&:inspect_with_instantiations).join("\n") end # A convenience method for testing/debugging. # @return [Array<Object>] the values instantiated for this goal. def values @values.values.map(&:value) end # Finds or tries to create a variable in the goal (as much as possible) otherwise passes the parameter back. # @param name [Object] name of variable or variable # @return [Porolog::Variable, Object] a variable of the goal or the original parameter def variable(name) return name unless name.is_a?(Symbol) || name.is_a?(Variable) if name.is_a?(Variable) variable = name @variables[variable.name] = variable if variable.goal == self return variable end @variables[name] ||= (name.is_a?(Variable) && name || Variable.new(name, self)) @variables[name] end # @param value [Object] the value that needs to be associated with this Goal. # @return [Porolog::Value] a Value, ensuring it has been associated with this Goal so that it can be uninstantiated. def value(value) @values[value] ||= Value.new(value, self) @values[value] end # Returns the value of the indicated variable or value # @param name [Symbol, Object] name of the variable or value # @param index [Object] index is not yet used # @param visited [Array] used to avoid infinite recursion # @return [Object] the value def value_of(name, index = nil, visited = []) variable(name).value(visited) end # Returns the values of an Object. For most objects, this will basically just be the object. # For Arrays, an Array will be returned where the elements are the value of the elements. # @param object [Object] the Object to find the values of. # @return [Object,Array<Object>] the value(s) of the object. def values_of(object) case object when Array object.map{|element| values_of(element) } when Variable,Symbol,Value value_of(object) when Tail # TODO: This needs to splat outwards; in the meantime, it splats just the first. value_of(object.value).first else object end end # Finds all possible solutions to the Predicate for the specific Arguments. # @param max_solutions [Integer] if provided, the search is stopped once the number of solutions has been found # @return [Array<Hash{Symbol => Object}>] def solve(max_solutions = nil) return @solutions unless @arguments predicate = @arguments.predicate predicate&.satisfy(self) do |goal| # TODO: Refactor to overrideable method (or another solution, say a lambda) @solutions << variables @log << "SOLUTION: #{variables}" @log << goal.ancestry @log << goal.inspect_variables return @solutions if max_solutions && @solutions.size >= max_solutions end @solutions end # Solves the goal but not as the root goal for a query. # That is, solves an intermediate goal. # @param block [Proc] code to perform when the Goal is satisfied. # @return [Boolean] whether the definition was satisfied. def satisfy(&block) return false unless @arguments predicate = @arguments.predicate satisfied = false predicate&.satisfy(self) do |subgoal| subgoal_satisfied = block.call(subgoal) satisfied ||= subgoal_satisfied end satisfied end # Instantiates a Variable to another Variable or Value, for this Goal. # @param name [Symbol,Porolog::Variable,Object] the name that references the variable to be instantiated. # @param other [Object] the value that the variable is to be instantiated to. # @param other_goal [Porolog::Goal,nil] the Goal of the other value. # @return [Porolog::Instantiation] the instantiation. def instantiate(name, other, other_goal = self) variable = self.variable(name) other = if other.type == :variable other_goal.variable(other) else other_goal.value(other) end variable.instantiate(other) end # Inherits variables and their instantiations from another goal. # @param other_goal [Porolog::Goal,nil] the Goal to inherit variables from. # @return [Boolean] whether the variables could be inherited (unified). def inherit_variables(other_goal = @calling_goal) return true unless other_goal unified = true unifications = [] variables = ( other_goal.arguments.variables + other_goal.variables.keys + self.arguments.variables ).map(&:to_sym).uniq variables.each do |variable| name = variable unification = Porolog::unify(name, name, other_goal, self) unified &&= !!unification if unified unifications += unification else #:nocov: self.log << "Couldn't unify: #{name.inspect} WITH #{other_goal.myid} AND #{self.myid}" break #:nocov: end end unified &&= Porolog::instantiate_unifications(unifications) if unified unified end end |
#index ⇒ Integer?
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 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/porolog/goal.rb', line 27 class Goal # Error class for rescuing or detecting any Goal error. class Error < PorologError ; end # Error class indicating an unexpected type of Rule definition. class DefinitionError < Error ; end # Error class indicating a non-Variable was attempted to be instantiated. class NotVariableError < Error ; end # Clears all goals and ensures they are deleted. # @return [Boolean] success def self.reset @@goals ||= [] goals = @@goals @@goals = [] @@goal_count = 0 goals.map(&:deleted?) true end # @return [Integer] the number of goals created def self.goal_count @@goal_count end reset attr_accessor :calling_goal, :arguments, :index, :result, :description, :log # Initializes and registers the Goal. # @param arguments [Porolog::Arguments] the Predicate Arguments that this Goal is to solve. # @param calling_goal [Porolog::Goal] the parent Goal if this Goal is a subgoal. def initialize(arguments, calling_goal = nil) @@goals << self @@goal_count += 1 @arguments = arguments @terminate = false @calling_goal = calling_goal @variables = {} @values = {} @result = nil @description = "Solve #{arguments.inspect}" @log = [] @arguments = @arguments.dup(self) if @arguments.is_a?(Arguments) @solutions = [] end # @return [Array<Porolog::Goal>] the calling chain for this goal. def ancestors ancestors = [] goal = self while goal ancestors << goal goal = goal.calling_goal end ancestors.reverse end # A convenience method for testing/debugging. # @return [String] the calling chain for this goal as a String. def ancestry indent = -1 ancestors.map do |goal| indent += 1 "#{' ' * indent}#{goal.myid} -- #{goal.description} #{goal.variables.inspect}" end.join("\n") end # A convenience method for testing/debugging. # @return [String] the id of the goal. def myid "Goal#{(@@goals.index(self) || -1000) + 1}" end # @return [String] pretty representation. def inspect "#{myid} -- #{description}" end # A convenience method for testing/debugging. # @return [Array<Porolog::Goal>] def self.goals @@goals end # Delete the Goal # @return [Boolean] whether the Goal has been deleted (memoized) def delete! @@goals.delete(self) deleted? end # @return [Boolean] whether the Goal has been deleted (memoized) def deleted? @deleted ||= check_deleted @deleted end # Determines whether the Goal has been deleted and removes its Variables and Values if it has. # @return [Boolean] whether the Goal has been deleted def check_deleted return false if @@goals.include?(self) @variables.delete_if do |_name,variable| variable.remove true end @values.delete_if do |_name,value| value.remove true end true end # Sets that the goal should no longer search any further rules for the current predicate # once the current rule has finished being evaluated. # This method implements the :CUT rule. # That is, backtracking does not go back past the point of the cut. # @return [Boolean] true. def terminate! @log << 'terminating' @terminate = true end # @return [Boolean] whether the goal has been cut. def terminated? @terminate end # Converts all embedded Symbols to Variables within this Goal. # @param object [Object] the object to variablise. # @return [Object] the variablised object. def variablise(object) case object when Symbol,Variable variable(object) when Array object.map{|element| variablise(element) } when Arguments object.dup(self) when Tail Tail.new variablise(object.value) when Value object when NilClass nil else if [UNKNOWN_TAIL, UNKNOWN_ARRAY].include?(object) object else value(object) end end end alias [] variablise # @return [Hash{Symbol => Object}] the Variables and their current values of this Goal def variables @variables.keys.each_with_object({}){|variable,variable_list| value = value_of(variable).value.value if value.is_a?(Variable) variable_list[variable] = nil elsif value.is_a?(Array) variable_list[variable] = value.clean else variable_list[variable] = value end } end # A convenience method for testing/debugging. # @return [String] a tree representation of all the instantiations of this goal's variables. def inspect_variables @variables.values.map(&:inspect_with_instantiations).join("\n") end # A convenience method for testing/debugging. # @return [Array<Object>] the values instantiated for this goal. def values @values.values.map(&:value) end # Finds or tries to create a variable in the goal (as much as possible) otherwise passes the parameter back. # @param name [Object] name of variable or variable # @return [Porolog::Variable, Object] a variable of the goal or the original parameter def variable(name) return name unless name.is_a?(Symbol) || name.is_a?(Variable) if name.is_a?(Variable) variable = name @variables[variable.name] = variable if variable.goal == self return variable end @variables[name] ||= (name.is_a?(Variable) && name || Variable.new(name, self)) @variables[name] end # @param value [Object] the value that needs to be associated with this Goal. # @return [Porolog::Value] a Value, ensuring it has been associated with this Goal so that it can be uninstantiated. def value(value) @values[value] ||= Value.new(value, self) @values[value] end # Returns the value of the indicated variable or value # @param name [Symbol, Object] name of the variable or value # @param index [Object] index is not yet used # @param visited [Array] used to avoid infinite recursion # @return [Object] the value def value_of(name, index = nil, visited = []) variable(name).value(visited) end # Returns the values of an Object. For most objects, this will basically just be the object. # For Arrays, an Array will be returned where the elements are the value of the elements. # @param object [Object] the Object to find the values of. # @return [Object,Array<Object>] the value(s) of the object. def values_of(object) case object when Array object.map{|element| values_of(element) } when Variable,Symbol,Value value_of(object) when Tail # TODO: This needs to splat outwards; in the meantime, it splats just the first. value_of(object.value).first else object end end # Finds all possible solutions to the Predicate for the specific Arguments. # @param max_solutions [Integer] if provided, the search is stopped once the number of solutions has been found # @return [Array<Hash{Symbol => Object}>] def solve(max_solutions = nil) return @solutions unless @arguments predicate = @arguments.predicate predicate&.satisfy(self) do |goal| # TODO: Refactor to overrideable method (or another solution, say a lambda) @solutions << variables @log << "SOLUTION: #{variables}" @log << goal.ancestry @log << goal.inspect_variables return @solutions if max_solutions && @solutions.size >= max_solutions end @solutions end # Solves the goal but not as the root goal for a query. # That is, solves an intermediate goal. # @param block [Proc] code to perform when the Goal is satisfied. # @return [Boolean] whether the definition was satisfied. def satisfy(&block) return false unless @arguments predicate = @arguments.predicate satisfied = false predicate&.satisfy(self) do |subgoal| subgoal_satisfied = block.call(subgoal) satisfied ||= subgoal_satisfied end satisfied end # Instantiates a Variable to another Variable or Value, for this Goal. # @param name [Symbol,Porolog::Variable,Object] the name that references the variable to be instantiated. # @param other [Object] the value that the variable is to be instantiated to. # @param other_goal [Porolog::Goal,nil] the Goal of the other value. # @return [Porolog::Instantiation] the instantiation. def instantiate(name, other, other_goal = self) variable = self.variable(name) other = if other.type == :variable other_goal.variable(other) else other_goal.value(other) end variable.instantiate(other) end # Inherits variables and their instantiations from another goal. # @param other_goal [Porolog::Goal,nil] the Goal to inherit variables from. # @return [Boolean] whether the variables could be inherited (unified). def inherit_variables(other_goal = @calling_goal) return true unless other_goal unified = true unifications = [] variables = ( other_goal.arguments.variables + other_goal.variables.keys + self.arguments.variables ).map(&:to_sym).uniq variables.each do |variable| name = variable unification = Porolog::unify(name, name, other_goal, self) unified &&= !!unification if unified unifications += unification else #:nocov: self.log << "Couldn't unify: #{name.inspect} WITH #{other_goal.myid} AND #{self.myid}" break #:nocov: end end unified &&= Porolog::instantiate_unifications(unifications) if unified unified end end |
#log ⇒ Array<String>
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 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/porolog/goal.rb', line 27 class Goal # Error class for rescuing or detecting any Goal error. class Error < PorologError ; end # Error class indicating an unexpected type of Rule definition. class DefinitionError < Error ; end # Error class indicating a non-Variable was attempted to be instantiated. class NotVariableError < Error ; end # Clears all goals and ensures they are deleted. # @return [Boolean] success def self.reset @@goals ||= [] goals = @@goals @@goals = [] @@goal_count = 0 goals.map(&:deleted?) true end # @return [Integer] the number of goals created def self.goal_count @@goal_count end reset attr_accessor :calling_goal, :arguments, :index, :result, :description, :log # Initializes and registers the Goal. # @param arguments [Porolog::Arguments] the Predicate Arguments that this Goal is to solve. # @param calling_goal [Porolog::Goal] the parent Goal if this Goal is a subgoal. def initialize(arguments, calling_goal = nil) @@goals << self @@goal_count += 1 @arguments = arguments @terminate = false @calling_goal = calling_goal @variables = {} @values = {} @result = nil @description = "Solve #{arguments.inspect}" @log = [] @arguments = @arguments.dup(self) if @arguments.is_a?(Arguments) @solutions = [] end # @return [Array<Porolog::Goal>] the calling chain for this goal. def ancestors ancestors = [] goal = self while goal ancestors << goal goal = goal.calling_goal end ancestors.reverse end # A convenience method for testing/debugging. # @return [String] the calling chain for this goal as a String. def ancestry indent = -1 ancestors.map do |goal| indent += 1 "#{' ' * indent}#{goal.myid} -- #{goal.description} #{goal.variables.inspect}" end.join("\n") end # A convenience method for testing/debugging. # @return [String] the id of the goal. def myid "Goal#{(@@goals.index(self) || -1000) + 1}" end # @return [String] pretty representation. def inspect "#{myid} -- #{description}" end # A convenience method for testing/debugging. # @return [Array<Porolog::Goal>] def self.goals @@goals end # Delete the Goal # @return [Boolean] whether the Goal has been deleted (memoized) def delete! @@goals.delete(self) deleted? end # @return [Boolean] whether the Goal has been deleted (memoized) def deleted? @deleted ||= check_deleted @deleted end # Determines whether the Goal has been deleted and removes its Variables and Values if it has. # @return [Boolean] whether the Goal has been deleted def check_deleted return false if @@goals.include?(self) @variables.delete_if do |_name,variable| variable.remove true end @values.delete_if do |_name,value| value.remove true end true end # Sets that the goal should no longer search any further rules for the current predicate # once the current rule has finished being evaluated. # This method implements the :CUT rule. # That is, backtracking does not go back past the point of the cut. # @return [Boolean] true. def terminate! @log << 'terminating' @terminate = true end # @return [Boolean] whether the goal has been cut. def terminated? @terminate end # Converts all embedded Symbols to Variables within this Goal. # @param object [Object] the object to variablise. # @return [Object] the variablised object. def variablise(object) case object when Symbol,Variable variable(object) when Array object.map{|element| variablise(element) } when Arguments object.dup(self) when Tail Tail.new variablise(object.value) when Value object when NilClass nil else if [UNKNOWN_TAIL, UNKNOWN_ARRAY].include?(object) object else value(object) end end end alias [] variablise # @return [Hash{Symbol => Object}] the Variables and their current values of this Goal def variables @variables.keys.each_with_object({}){|variable,variable_list| value = value_of(variable).value.value if value.is_a?(Variable) variable_list[variable] = nil elsif value.is_a?(Array) variable_list[variable] = value.clean else variable_list[variable] = value end } end # A convenience method for testing/debugging. # @return [String] a tree representation of all the instantiations of this goal's variables. def inspect_variables @variables.values.map(&:inspect_with_instantiations).join("\n") end # A convenience method for testing/debugging. # @return [Array<Object>] the values instantiated for this goal. def values @values.values.map(&:value) end # Finds or tries to create a variable in the goal (as much as possible) otherwise passes the parameter back. # @param name [Object] name of variable or variable # @return [Porolog::Variable, Object] a variable of the goal or the original parameter def variable(name) return name unless name.is_a?(Symbol) || name.is_a?(Variable) if name.is_a?(Variable) variable = name @variables[variable.name] = variable if variable.goal == self return variable end @variables[name] ||= (name.is_a?(Variable) && name || Variable.new(name, self)) @variables[name] end # @param value [Object] the value that needs to be associated with this Goal. # @return [Porolog::Value] a Value, ensuring it has been associated with this Goal so that it can be uninstantiated. def value(value) @values[value] ||= Value.new(value, self) @values[value] end # Returns the value of the indicated variable or value # @param name [Symbol, Object] name of the variable or value # @param index [Object] index is not yet used # @param visited [Array] used to avoid infinite recursion # @return [Object] the value def value_of(name, index = nil, visited = []) variable(name).value(visited) end # Returns the values of an Object. For most objects, this will basically just be the object. # For Arrays, an Array will be returned where the elements are the value of the elements. # @param object [Object] the Object to find the values of. # @return [Object,Array<Object>] the value(s) of the object. def values_of(object) case object when Array object.map{|element| values_of(element) } when Variable,Symbol,Value value_of(object) when Tail # TODO: This needs to splat outwards; in the meantime, it splats just the first. value_of(object.value).first else object end end # Finds all possible solutions to the Predicate for the specific Arguments. # @param max_solutions [Integer] if provided, the search is stopped once the number of solutions has been found # @return [Array<Hash{Symbol => Object}>] def solve(max_solutions = nil) return @solutions unless @arguments predicate = @arguments.predicate predicate&.satisfy(self) do |goal| # TODO: Refactor to overrideable method (or another solution, say a lambda) @solutions << variables @log << "SOLUTION: #{variables}" @log << goal.ancestry @log << goal.inspect_variables return @solutions if max_solutions && @solutions.size >= max_solutions end @solutions end # Solves the goal but not as the root goal for a query. # That is, solves an intermediate goal. # @param block [Proc] code to perform when the Goal is satisfied. # @return [Boolean] whether the definition was satisfied. def satisfy(&block) return false unless @arguments predicate = @arguments.predicate satisfied = false predicate&.satisfy(self) do |subgoal| subgoal_satisfied = block.call(subgoal) satisfied ||= subgoal_satisfied end satisfied end # Instantiates a Variable to another Variable or Value, for this Goal. # @param name [Symbol,Porolog::Variable,Object] the name that references the variable to be instantiated. # @param other [Object] the value that the variable is to be instantiated to. # @param other_goal [Porolog::Goal,nil] the Goal of the other value. # @return [Porolog::Instantiation] the instantiation. def instantiate(name, other, other_goal = self) variable = self.variable(name) other = if other.type == :variable other_goal.variable(other) else other_goal.value(other) end variable.instantiate(other) end # Inherits variables and their instantiations from another goal. # @param other_goal [Porolog::Goal,nil] the Goal to inherit variables from. # @return [Boolean] whether the variables could be inherited (unified). def inherit_variables(other_goal = @calling_goal) return true unless other_goal unified = true unifications = [] variables = ( other_goal.arguments.variables + other_goal.variables.keys + self.arguments.variables ).map(&:to_sym).uniq variables.each do |variable| name = variable unification = Porolog::unify(name, name, other_goal, self) unified &&= !!unification if unified unifications += unification else #:nocov: self.log << "Couldn't unify: #{name.inspect} WITH #{other_goal.myid} AND #{self.myid}" break #:nocov: end end unified &&= Porolog::instantiate_unifications(unifications) if unified unified end end |
#result ⇒ Boolean
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 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/porolog/goal.rb', line 27 class Goal # Error class for rescuing or detecting any Goal error. class Error < PorologError ; end # Error class indicating an unexpected type of Rule definition. class DefinitionError < Error ; end # Error class indicating a non-Variable was attempted to be instantiated. class NotVariableError < Error ; end # Clears all goals and ensures they are deleted. # @return [Boolean] success def self.reset @@goals ||= [] goals = @@goals @@goals = [] @@goal_count = 0 goals.map(&:deleted?) true end # @return [Integer] the number of goals created def self.goal_count @@goal_count end reset attr_accessor :calling_goal, :arguments, :index, :result, :description, :log # Initializes and registers the Goal. # @param arguments [Porolog::Arguments] the Predicate Arguments that this Goal is to solve. # @param calling_goal [Porolog::Goal] the parent Goal if this Goal is a subgoal. def initialize(arguments, calling_goal = nil) @@goals << self @@goal_count += 1 @arguments = arguments @terminate = false @calling_goal = calling_goal @variables = {} @values = {} @result = nil @description = "Solve #{arguments.inspect}" @log = [] @arguments = @arguments.dup(self) if @arguments.is_a?(Arguments) @solutions = [] end # @return [Array<Porolog::Goal>] the calling chain for this goal. def ancestors ancestors = [] goal = self while goal ancestors << goal goal = goal.calling_goal end ancestors.reverse end # A convenience method for testing/debugging. # @return [String] the calling chain for this goal as a String. def ancestry indent = -1 ancestors.map do |goal| indent += 1 "#{' ' * indent}#{goal.myid} -- #{goal.description} #{goal.variables.inspect}" end.join("\n") end # A convenience method for testing/debugging. # @return [String] the id of the goal. def myid "Goal#{(@@goals.index(self) || -1000) + 1}" end # @return [String] pretty representation. def inspect "#{myid} -- #{description}" end # A convenience method for testing/debugging. # @return [Array<Porolog::Goal>] def self.goals @@goals end # Delete the Goal # @return [Boolean] whether the Goal has been deleted (memoized) def delete! @@goals.delete(self) deleted? end # @return [Boolean] whether the Goal has been deleted (memoized) def deleted? @deleted ||= check_deleted @deleted end # Determines whether the Goal has been deleted and removes its Variables and Values if it has. # @return [Boolean] whether the Goal has been deleted def check_deleted return false if @@goals.include?(self) @variables.delete_if do |_name,variable| variable.remove true end @values.delete_if do |_name,value| value.remove true end true end # Sets that the goal should no longer search any further rules for the current predicate # once the current rule has finished being evaluated. # This method implements the :CUT rule. # That is, backtracking does not go back past the point of the cut. # @return [Boolean] true. def terminate! @log << 'terminating' @terminate = true end # @return [Boolean] whether the goal has been cut. def terminated? @terminate end # Converts all embedded Symbols to Variables within this Goal. # @param object [Object] the object to variablise. # @return [Object] the variablised object. def variablise(object) case object when Symbol,Variable variable(object) when Array object.map{|element| variablise(element) } when Arguments object.dup(self) when Tail Tail.new variablise(object.value) when Value object when NilClass nil else if [UNKNOWN_TAIL, UNKNOWN_ARRAY].include?(object) object else value(object) end end end alias [] variablise # @return [Hash{Symbol => Object}] the Variables and their current values of this Goal def variables @variables.keys.each_with_object({}){|variable,variable_list| value = value_of(variable).value.value if value.is_a?(Variable) variable_list[variable] = nil elsif value.is_a?(Array) variable_list[variable] = value.clean else variable_list[variable] = value end } end # A convenience method for testing/debugging. # @return [String] a tree representation of all the instantiations of this goal's variables. def inspect_variables @variables.values.map(&:inspect_with_instantiations).join("\n") end # A convenience method for testing/debugging. # @return [Array<Object>] the values instantiated for this goal. def values @values.values.map(&:value) end # Finds or tries to create a variable in the goal (as much as possible) otherwise passes the parameter back. # @param name [Object] name of variable or variable # @return [Porolog::Variable, Object] a variable of the goal or the original parameter def variable(name) return name unless name.is_a?(Symbol) || name.is_a?(Variable) if name.is_a?(Variable) variable = name @variables[variable.name] = variable if variable.goal == self return variable end @variables[name] ||= (name.is_a?(Variable) && name || Variable.new(name, self)) @variables[name] end # @param value [Object] the value that needs to be associated with this Goal. # @return [Porolog::Value] a Value, ensuring it has been associated with this Goal so that it can be uninstantiated. def value(value) @values[value] ||= Value.new(value, self) @values[value] end # Returns the value of the indicated variable or value # @param name [Symbol, Object] name of the variable or value # @param index [Object] index is not yet used # @param visited [Array] used to avoid infinite recursion # @return [Object] the value def value_of(name, index = nil, visited = []) variable(name).value(visited) end # Returns the values of an Object. For most objects, this will basically just be the object. # For Arrays, an Array will be returned where the elements are the value of the elements. # @param object [Object] the Object to find the values of. # @return [Object,Array<Object>] the value(s) of the object. def values_of(object) case object when Array object.map{|element| values_of(element) } when Variable,Symbol,Value value_of(object) when Tail # TODO: This needs to splat outwards; in the meantime, it splats just the first. value_of(object.value).first else object end end # Finds all possible solutions to the Predicate for the specific Arguments. # @param max_solutions [Integer] if provided, the search is stopped once the number of solutions has been found # @return [Array<Hash{Symbol => Object}>] def solve(max_solutions = nil) return @solutions unless @arguments predicate = @arguments.predicate predicate&.satisfy(self) do |goal| # TODO: Refactor to overrideable method (or another solution, say a lambda) @solutions << variables @log << "SOLUTION: #{variables}" @log << goal.ancestry @log << goal.inspect_variables return @solutions if max_solutions && @solutions.size >= max_solutions end @solutions end # Solves the goal but not as the root goal for a query. # That is, solves an intermediate goal. # @param block [Proc] code to perform when the Goal is satisfied. # @return [Boolean] whether the definition was satisfied. def satisfy(&block) return false unless @arguments predicate = @arguments.predicate satisfied = false predicate&.satisfy(self) do |subgoal| subgoal_satisfied = block.call(subgoal) satisfied ||= subgoal_satisfied end satisfied end # Instantiates a Variable to another Variable or Value, for this Goal. # @param name [Symbol,Porolog::Variable,Object] the name that references the variable to be instantiated. # @param other [Object] the value that the variable is to be instantiated to. # @param other_goal [Porolog::Goal,nil] the Goal of the other value. # @return [Porolog::Instantiation] the instantiation. def instantiate(name, other, other_goal = self) variable = self.variable(name) other = if other.type == :variable other_goal.variable(other) else other_goal.value(other) end variable.instantiate(other) end # Inherits variables and their instantiations from another goal. # @param other_goal [Porolog::Goal,nil] the Goal to inherit variables from. # @return [Boolean] whether the variables could be inherited (unified). def inherit_variables(other_goal = @calling_goal) return true unless other_goal unified = true unifications = [] variables = ( other_goal.arguments.variables + other_goal.variables.keys + self.arguments.variables ).map(&:to_sym).uniq variables.each do |variable| name = variable unification = Porolog::unify(name, name, other_goal, self) unified &&= !!unification if unified unifications += unification else #:nocov: self.log << "Couldn't unify: #{name.inspect} WITH #{other_goal.myid} AND #{self.myid}" break #:nocov: end end unified &&= Porolog::instantiate_unifications(unifications) if unified unified end end |
Class Method Details
.goal_count ⇒ Integer
48 49 50 |
# File 'lib/porolog/goal.rb', line 48 def self.goal_count @@goal_count end |
.goals ⇒ Array<Porolog::Goal>
A convenience method for testing/debugging.
112 113 114 |
# File 'lib/porolog/goal.rb', line 112 def self.goals @@goals end |
.reset ⇒ Boolean
Clears all goals and ensures they are deleted.
38 39 40 41 42 43 44 45 |
# File 'lib/porolog/goal.rb', line 38 def self.reset @@goals ||= [] goals = @@goals @@goals = [] @@goal_count = 0 goals.map(&:deleted?) true end |
Instance Method Details
#ancestors ⇒ Array<Porolog::Goal>
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/porolog/goal.rb', line 77 def ancestors ancestors = [] goal = self while goal ancestors << goal goal = goal.calling_goal end ancestors.reverse end |
#ancestry ⇒ String
A convenience method for testing/debugging.
91 92 93 94 95 96 97 |
# File 'lib/porolog/goal.rb', line 91 def ancestry indent = -1 ancestors.map do |goal| indent += 1 "#{' ' * indent}#{goal.myid} -- #{goal.description} #{goal.variables.inspect}" end.join("\n") end |
#check_deleted ⇒ Boolean
Determines whether the Goal has been deleted and removes its Variables and Values if it has.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/porolog/goal.rb', line 131 def check_deleted return false if @@goals.include?(self) @variables.delete_if do |_name,variable| variable.remove true end @values.delete_if do |_name,value| value.remove true end true end |
#delete! ⇒ Boolean
Delete the Goal
118 119 120 121 |
# File 'lib/porolog/goal.rb', line 118 def delete! @@goals.delete(self) deleted? end |
#deleted? ⇒ Boolean
124 125 126 127 |
# File 'lib/porolog/goal.rb', line 124 def deleted? @deleted ||= check_deleted @deleted end |
#inherit_variables(other_goal = @calling_goal) ⇒ Boolean
Inherits variables and their instantiations from another goal.
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/porolog/goal.rb', line 325 def inherit_variables(other_goal = @calling_goal) return true unless other_goal unified = true unifications = [] variables = ( other_goal.arguments.variables + other_goal.variables.keys + self.arguments.variables ).map(&:to_sym).uniq variables.each do |variable| name = variable unification = Porolog::unify(name, name, other_goal, self) unified &&= !!unification if unified unifications += unification else #:nocov: self.log << "Couldn't unify: #{name.inspect} WITH #{other_goal.myid} AND #{self.myid}" break #:nocov: end end unified &&= Porolog::instantiate_unifications(unifications) if unified unified end |
#inspect ⇒ String
106 107 108 |
# File 'lib/porolog/goal.rb', line 106 def inspect "#{myid} -- #{description}" end |
#inspect_variables ⇒ String
A convenience method for testing/debugging.
206 207 208 |
# File 'lib/porolog/goal.rb', line 206 def inspect_variables @variables.values.map(&:inspect_with_instantiations).join("\n") end |
#instantiate(name, other, other_goal = self) ⇒ Porolog::Instantiation
Instantiates a Variable to another Variable or Value, for this Goal.
310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/porolog/goal.rb', line 310 def instantiate(name, other, other_goal = self) variable = self.variable(name) other = if other.type == :variable other_goal.variable(other) else other_goal.value(other) end variable.instantiate(other) end |
#myid ⇒ String
A convenience method for testing/debugging.
101 102 103 |
# File 'lib/porolog/goal.rb', line 101 def myid "Goal#{(@@goals.index(self) || -1000) + 1}" end |
#satisfy(&block) ⇒ Boolean
Solves the goal but not as the root goal for a query. That is, solves an intermediate goal.
292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/porolog/goal.rb', line 292 def satisfy(&block) return false unless @arguments predicate = @arguments.predicate satisfied = false predicate&.satisfy(self) do |subgoal| subgoal_satisfied = block.call(subgoal) satisfied ||= subgoal_satisfied end satisfied end |
#solve(max_solutions = nil) ⇒ Array<Hash{Symbol => Object}>
Finds all possible solutions to the Predicate for the specific Arguments.
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/porolog/goal.rb', line 269 def solve(max_solutions = nil) return @solutions unless @arguments predicate = @arguments.predicate predicate&.satisfy(self) do |goal| # TODO: Refactor to overrideable method (or another solution, say a lambda) @solutions << variables @log << "SOLUTION: #{variables}" @log << goal.ancestry @log << goal.inspect_variables return @solutions if max_solutions && @solutions.size >= max_solutions end @solutions end |
#terminate! ⇒ Boolean
Sets that the goal should no longer search any further rules for the current predicate once the current rule has finished being evaluated. This method implements the :CUT rule. That is, backtracking does not go back past the point of the cut.
152 153 154 155 |
# File 'lib/porolog/goal.rb', line 152 def terminate! @log << 'terminating' @terminate = true end |
#terminated? ⇒ Boolean
158 159 160 |
# File 'lib/porolog/goal.rb', line 158 def terminated? @terminate end |
#value(value) ⇒ Porolog::Value
234 235 236 237 |
# File 'lib/porolog/goal.rb', line 234 def value(value) @values[value] ||= Value.new(value, self) @values[value] end |
#value_of(name, index = nil, visited = []) ⇒ Object
Returns the value of the indicated variable or value
244 245 246 |
# File 'lib/porolog/goal.rb', line 244 def value_of(name, index = nil, visited = []) variable(name).value(visited) end |
#values ⇒ Array<Object>
A convenience method for testing/debugging.
212 213 214 |
# File 'lib/porolog/goal.rb', line 212 def values @values.values.map(&:value) end |
#values_of(object) ⇒ Object+
Returns the values of an Object. For most objects, this will basically just be the object. For Arrays, an Array will be returned where the elements are the value of the elements.
252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/porolog/goal.rb', line 252 def values_of(object) case object when Array object.map{|element| values_of(element) } when Variable,Symbol,Value value_of(object) when Tail # TODO: This needs to splat outwards; in the meantime, it splats just the first. value_of(object.value).first else object end end |
#variable(name) ⇒ Porolog::Variable, Object
Finds or tries to create a variable in the goal (as much as possible) otherwise passes the parameter back.
219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/porolog/goal.rb', line 219 def variable(name) return name unless name.is_a?(Symbol) || name.is_a?(Variable) if name.is_a?(Variable) variable = name @variables[variable.name] = variable if variable.goal == self return variable end @variables[name] ||= (name.is_a?(Variable) && name || Variable.new(name, self)) @variables[name] end |
#variables ⇒ Hash{Symbol => Object}
191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/porolog/goal.rb', line 191 def variables @variables.keys.each_with_object({}){|variable,variable_list| value = value_of(variable).value.value if value.is_a?(Variable) variable_list[variable] = nil elsif value.is_a?(Array) variable_list[variable] = value.clean else variable_list[variable] = value end } end |
#variablise(object) ⇒ Object Also known as: []
Converts all embedded Symbols to Variables within this Goal.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/porolog/goal.rb', line 165 def variablise(object) case object when Symbol,Variable variable(object) when Array object.map{|element| variablise(element) } when Arguments object.dup(self) when Tail Tail.new variablise(object.value) when Value object when NilClass nil else if [UNKNOWN_TAIL, UNKNOWN_ARRAY].include?(object) object else value(object) end end end |