Class: Rbprolog::Rule
- Inherits:
-
Object
- Object
- Rbprolog::Rule
- Defined in:
- lib/rbprolog/rule.rb
Overview
when fail at one predicate, the output values need be reset, and remove from the parent rule/s context
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#sym ⇒ Object
Returns the value of attribute sym.
Instance Method Summary collapse
- #each_deduce(rules, *args, id) ⇒ Object
-
#initialize(sym, *args, deductions) ⇒ Rule
constructor
A new instance of Rule.
- #match!(context, args) ⇒ Object
- #match?(context, args) ⇒ Boolean
Constructor Details
#initialize(sym, *args, deductions) ⇒ Rule
Returns a new instance of Rule.
7 8 9 10 11 |
# File 'lib/rbprolog/rule.rb', line 7 def initialize(sym, *args, deductions) @sym = sym @args = args @deductions = [deductions].flatten end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
5 6 7 |
# File 'lib/rbprolog/rule.rb', line 5 def args @args end |
#sym ⇒ Object
Returns the value of attribute sym.
5 6 7 |
# File 'lib/rbprolog/rule.rb', line 5 def sym @sym end |
Instance Method Details
#each_deduce(rules, *args, id) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rbprolog/rule.rb', line 13 def each_deduce(rules, *args, id) print "#{"\t" * id.size}#{id.join('.')} #{@sym}(#{@args.map(&:to_s).join(', ')}).deduce(#{args.map(&:to_s).join(', ')})" context = Context.new context.scope(self) do if self.match!(context, args) puts " => #{@sym}(#{@args.map {|arg| context.deduce(arg).to_s}.join(', ')})" #{context.to_s} #{context.binds.inspect}" deduce_deductions(context, rules, *@deductions, id) do yield context.binds end else print "\n" end end end |
#match!(context, args) ⇒ Object
33 34 35 36 37 |
# File 'lib/rbprolog/rule.rb', line 33 def match!(context, args) match = match?(context, args) @args.zip(args).each {|v1, v2| context.match!(v1, v2)} if match match end |
#match?(context, args) ⇒ Boolean
29 30 31 |
# File 'lib/rbprolog/rule.rb', line 29 def match?(context, args) @args.size == args.size && @args.zip(args).all? {|v1, v2| context.match?(v1, v2)} end |