Module: Rbprolog
- Defined in:
- lib/rbprolog.rb,
lib/rbprolog/var.rb,
lib/rbprolog/rule.rb,
lib/rbprolog/context.rb,
lib/rbprolog/version.rb,
lib/rbprolog/deduction.rb,
lib/rbprolog/evaluation.rb
Overview
Simulate the prolog logic processing partially by using ruby based DSL
Representations (Conventions)
- rule - class level
-
name arg0, arg1, …, => [deduction0, deduction1, …]
-
name: defined in the keywords
-
arg: use const to present the variable, and non-const for value
-
deduction: see below
-
- fact - class level
-
name: arg0, arg1, …
-
- deduction - class level
-
name? arg0, arg1, …
-
return: true or false
-
- question - instance level
-
name? arg0, arg1, …
-
- enumerator - instance level
-
name! arg0, arg1, …
-
Deduce all possible answers
-
Example
class FriendLogic
include Rbprolog
keywords :likes, :friends
likes 'p1', 's1'
likes 'p1', 's2'
likes 'p2', 's2'
likes 'p3', 's1'
likes 'p4', X
friends 'p1', W, :if => likes?(W, 's2')
friends X, Y, :if => [likes?(X, Z), likes?(Y, Z)]
end
l = FriendLogic.new
l.likes?('p1', 's1') #=> true
l.friends?('p1', 'p4') #=> true
Defined Under Namespace
Modules: ClassMethods Classes: Context, Deduction, Evaluation, Rule, Var
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(string = nil, &block) ⇒ Object
Initialize the rbprolog instance, each instance can have its own fact and rules.
- #rules ⇒ Object
Class Method Details
.included(mod) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/rbprolog.rb', line 54 def self.included(mod) class << mod include ClassMethods attr_accessor :rules, :syms end end |
Instance Method Details
#initialize(string = nil, &block) ⇒ Object
Initialize the rbprolog instance, each instance can have its own fact and rules. The definition can be passed in as string or block. string is required when variable such as X is used.
l = FriendLogic.new do
likes 'p5', 's1'
end
or
l = FriendLogic.new %q{
friends 'p2', X, :if => likes?(X, 's1')
}
73 74 75 76 77 78 79 80 81 |
# File 'lib/rbprolog.rb', line 73 def initialize(string = nil, &block) if string || block self.extend(Rbprolog) self.singleton_class.keywords(*self.class.syms) self.singleton_class.class_eval(string) if string self.singleton_class.class_eval(&block) if block end end |
#rules ⇒ Object
83 84 85 |
# File 'lib/rbprolog.rb', line 83 def rules self.class.rules + (self.singleton_class.rules || []) end |