module Rbprolog::ClassMethods

Public Instance Methods

const_missing(sym) click to toggle source
# File lib/rbprolog.rb, line 96
def const_missing(sym)
  Var.new(sym)
end
keywords(*syms) click to toggle source

Define the vocabulary of rules and facts

# File lib/rbprolog.rb, line 89
def keywords(*syms)
  raise if syms.any? {|sym| sym.to_s.end_with? '?'}

  self.syms ||= []
  self.syms.concat(syms)
end
method_missing(sym, *args) click to toggle source

Generate rule, fact and deduction based on conventions

Calls superclass method
# File lib/rbprolog.rb, line 101
def method_missing(sym, *args)
  if self.syms.include? sym
    Hash === args.last ? rule(sym, *args) : rule(sym, *args, :if => [])
  elsif self.syms.include? sym.to_s.chomp('?').to_sym
    Deduction.new(sym.to_s.chomp('?').to_sym, *args)
  else
    super
  end
end
rule(sym, *args, options) click to toggle source

Internal class method to install instance methods for question and enumerator

# File lib/rbprolog.rb, line 112
def rule(sym, *args, options)
  self.rules ||= []
  self.rules << Rule.new(sym, *args, options[:if])

  unless method_defined?(sym)
    define_method("#{sym}!") do |*args|
      deduction = Deduction.new(sym, *args)

      deduction.extend(Enumerable)

      rules = self.rules
      deduction.define_singleton_method(:each) do |&block|
        each_deduce(Context.new, rules, []) do |hash|
          block.call hash
        end
      end

      deduction
    end

    define_method("#{sym}?") do |*args|
      self.send("#{sym}!", *args).any? {|hash| true}
    end
  end
end