class Porolog::Arguments
A Porolog::Arguments
specifies arguments of a Predicate
. A predicate is not like a subroutine, although there are many similarities. An Arguments
represents an instance of a predicate with a specific set of arguments. This forms the basis for implementing a goal to solve the predicate with those specific arguments.
@author Luis Esteban
@!attribute [r] predicate
The Predicate for which these are the arguments.
@!attribute [r] arguments
The actual arguments.
Attributes
Public Class Methods
Convenience method for testing/debugging @return [Array<Porolog::Arguments>] all registered Arguments
# File lib/porolog/arguments.rb, line 46 def self.arguments @@arguments end
Creates a new Arguments
for a Predicate
@param predicate [Porolog::Predicate] the Predicate
for which these are the arguments @param arguments [Array<Object>] the actual arguments
# File lib/porolog/arguments.rb, line 37 def initialize(predicate, arguments, &block) @predicate = predicate @arguments = arguments @block = block @@arguments << self end
Unregisters all Arguments
@return [true]
# File lib/porolog/arguments.rb, line 27 def self.reset @@arguments = [] true end
Public Instance Methods
@param other [Porolog::Arguments] arguments for comparison @return [Boolean] whether the Arguments
match
# File lib/porolog/arguments.rb, line 170 def ==(other) @predicate == other.predicate && @arguments == other.arguments end
@return [String] pretty representation
# File lib/porolog/arguments.rb, line 57 def inspect block_inspect = block.nil? ? '' : "{#{block.inspect}}" "#{@predicate&.name}(#{@arguments&.map(&:inspect).join(',')})#{block_inspect}" end
Convenience method for testing/debugging @return [String] pretty identification
# File lib/porolog/arguments.rb, line 52 def myid "Arguments#{(@@arguments.index(self) || -1000) + 1}" end
Returns memoized solutions @param max_solutions [Integer] the maximum number of solutions to find (nil means find all) @return [Array<Hash{Symbol => Object}>] the solutions found (memoized)
# File lib/porolog/arguments.rb, line 121 def solutions(max_solutions = nil) @solutions ||= solve(max_solutions) max_solutions && @solutions[0...max_solutions] || @solutions end
Solves the Arguments
@param max_solutions [Integer] the maximum number of solutions to find (nil means find all) @return [Array<Hash{Symbol => Object}>] the solutions found
# File lib/porolog/arguments.rb, line 129 def solve(max_solutions = nil) @solutions = goal.solve(max_solutions) end
Extracts solution values. @param variables [Symbol, Array
<Symbol>] variable or variables @param max_solutions [Integer] the maximum number of solutions to find (nil means find all) @return [Array<Object>] all the values for the variables given @example
predicate :treasure_at treasure_at(:X,:Y) << [...] arguments = treasure_at(:X,:Y) xs = arguments.solve_for(:X) ys = arguments.solve_for(:Y) coords = xs.zip(ys)
# File lib/porolog/arguments.rb, line 144 def solve_for(*variables, max_solutions: nil) variables = [*variables] solutions(max_solutions).map{|solution| values = variables.map{|variable| solution[variable] } if values.size == 1 values.first else values end } end
@return [Boolean] whether any solutions were found
# File lib/porolog/arguments.rb, line 157 def valid? !solutions(1).empty? end
@return [Array<Symbol>] the variables contained in the arguments
# File lib/porolog/arguments.rb, line 107 def variables @arguments.map(&:variables).flatten.uniq end