Class: RubyProlog::Predicate

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-prolog/ruby-prolog.rb

Constant Summary collapse

@@id_counter =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#clausesObject

Returns the value of attribute clauses.



15
16
17
# File 'lib/ruby-prolog/ruby-prolog.rb', line 15

def clauses
  @clauses
end

#dbObject

Returns the value of attribute db.



15
16
17
# File 'lib/ruby-prolog/ruby-prolog.rb', line 15

def db
  @db
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/ruby-prolog/ruby-prolog.rb', line 14

def id
  @id
end

#nameObject (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

#factObject



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

#inspectObject



24
25
26
# File 'lib/ruby-prolog/ruby-prolog.rb', line 24

def inspect
  @name.to_s
end

#to_goalObject



49
# File 'lib/ruby-prolog/ruby-prolog.rb', line 49

def to_goal; TempClause.new(@db, self, []).to_goal; end

#to_prologObject



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