Class: RubyProlog::Predicate
- Inherits:
-
Object
- Object
- RubyProlog::Predicate
- Defined in:
- lib/ruby-prolog/ruby-prolog.rb
Constant Summary collapse
- @@id_counter =
0
Instance Attribute Summary collapse
-
#clauses ⇒ Object
Returns the value of attribute clauses.
-
#db ⇒ Object
Returns the value of attribute db.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#<<(goals) ⇒ Object
DSL for 0-arity predicates.
- #[](*args) ⇒ Object
- #fact ⇒ Object
- #fork(new_db) ⇒ Object
-
#initialize(db, name, explicit_id: nil) ⇒ Predicate
constructor
A new instance of Predicate.
- #inspect ⇒ Object
- #to_goal ⇒ Object
- #to_prolog ⇒ Object
Constructor Details
#initialize(db, name, explicit_id: nil) ⇒ Predicate
Returns a new instance of Predicate.
17 18 19 20 21 22 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 17 def initialize(db, name, explicit_id: nil) @id = explicit_id || (@@id_counter += 1) @db = db @name = name @clauses = [] end |
Instance Attribute Details
#clauses ⇒ Object
Returns the value of attribute clauses.
15 16 17 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 15 def clauses @clauses end |
#db ⇒ Object
Returns the value of attribute db.
15 16 17 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 15 def db @db end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
14 15 16 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 14 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 14 def name @name end |
Instance Method Details
#<<(goals) ⇒ Object
DSL for 0-arity predicates
48 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 48 def <<(goals); TempClause.new(@db, self, []) << goals; end |
#[](*args) ⇒ Object
28 29 30 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 28 def [](*args) TempClause.new(@db, self, args) end |
#fact ⇒ Object
50 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 50 def fact; TempClause.new(@db, self, []).fact; end |
#fork(new_db) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 38 def fork(new_db) dupe = self.clone dupe.db = new_db dupe.clauses = dupe.clauses.dup dupe end |
#inspect ⇒ Object
24 25 26 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 24 def inspect @name.to_s end |
#to_goal ⇒ Object
49 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 49 def to_goal; TempClause.new(@db, self, []).to_goal; end |
#to_prolog ⇒ Object
32 33 34 35 36 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 32 def to_prolog @clauses.map do |head, body| "#{head.to_prolog}#{body ? " :- #{body.to_prolog}" : ''}." end.join("\n") end |