Class: RubyProlog::Database

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Returns a new instance of Database.



261
262
263
264
265
266
# File 'lib/ruby-prolog/ruby-prolog.rb', line 261

def initialize
  @by_name = {}
  @by_id = {}
  @listing_enabled = false
  @listing = {}
end

Instance Attribute Details

#by_idObject (readonly)

Returns the value of attribute by_id.



259
260
261
# File 'lib/ruby-prolog/ruby-prolog.rb', line 259

def by_id
  @by_id
end

#by_nameObject (readonly)

Returns the value of attribute by_name.



259
260
261
# File 'lib/ruby-prolog/ruby-prolog.rb', line 259

def by_name
  @by_name
end

Instance Method Details

#append(head, body) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
# File 'lib/ruby-prolog/ruby-prolog.rb', line 279

def append(head, body)
  pred = @by_id[head.pred_id]
  if pred.nil?
    raise "No such predicate for head: #{head.inspect}"
  end
  pred.clauses << [head, body]
  if @listing_enabled && @listing[pred.id] != false
    # Ruby hashes maintain insertion order
    @listing[pred.id] = true
  end
end

#enable_listing(flag = true) ⇒ Object



275
276
277
# File 'lib/ruby-prolog/ruby-prolog.rb', line 275

def enable_listing(flag=true)
  @listing_enabled = true
end

#initialize_copy(orig) ⇒ Object



291
292
293
294
295
296
297
# File 'lib/ruby-prolog/ruby-prolog.rb', line 291

def initialize_copy(orig)
  super
  @by_id = @by_id.transform_values do |pred|
    pred.fork(self)
  end
  @by_name = @by_name.transform_values {|pred| @by_id[pred.id]}
end

#listingObject



299
300
301
# File 'lib/ruby-prolog/ruby-prolog.rb', line 299

def listing
  @listing.select{|_,v| v}.map{|k,v| @by_id[k]}
end

#register(pred_name, skip_listing: false) ⇒ Object



268
269
270
271
272
273
# File 'lib/ruby-prolog/ruby-prolog.rb', line 268

def register(pred_name, skip_listing: false)
  pred = @by_name[pred_name] = Predicate.new(self, pred_name)
  @by_id[pred.id] = pred
  @listing[pred.id] = false if skip_listing
  pred
end