Class: RubyProlog::Database
- Inherits:
-
Object
- Object
- RubyProlog::Database
- Defined in:
- lib/ruby-prolog/ruby-prolog.rb
Instance Attribute Summary collapse
-
#by_id ⇒ Object
readonly
Returns the value of attribute by_id.
-
#by_name ⇒ Object
readonly
Returns the value of attribute by_name.
Instance Method Summary collapse
- #append(head, body) ⇒ Object
- #enable_listing(flag = true) ⇒ Object
-
#initialize ⇒ Database
constructor
A new instance of Database.
- #initialize_copy(orig) ⇒ Object
- #listing ⇒ Object
- #register(pred_name, skip_listing: false) ⇒ Object
Constructor Details
#initialize ⇒ Database
Returns a new instance of Database.
269 270 271 272 273 274 275 276 277 278 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 269 def initialize @by_name = { 'false' => Predicate.new(self, 'false', explicit_id: 0) } @by_id = { 0 => @by_name['false'] } @listing_enabled = false @listing = {} end |
Instance Attribute Details
#by_id ⇒ Object (readonly)
Returns the value of attribute by_id.
267 268 269 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 267 def by_id @by_id end |
#by_name ⇒ Object (readonly)
Returns the value of attribute by_name.
267 268 269 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 267 def by_name @by_name end |
Instance Method Details
#append(head, body) ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 291 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
287 288 289 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 287 def enable_listing(flag=true) @listing_enabled = true end |
#initialize_copy(orig) ⇒ Object
303 304 305 306 307 308 309 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 303 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 |
#listing ⇒ Object
311 312 313 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 311 def listing @listing.select{|_,v| v}.map{|k,v| @by_id[k]} end |
#register(pred_name, skip_listing: false) ⇒ Object
280 281 282 283 284 285 |
# File 'lib/ruby-prolog/ruby-prolog.rb', line 280 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 |