Class: Factbase
- Inherits:
-
Object
- Object
- Factbase
- Defined in:
- lib/factbase.rb
Overview
Factbase.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024 Yegor Bugayenko
- License
-
MIT
Defined Under Namespace
Classes: Fact, Query, Spy, Syntax, Term, WhiteList
Instance Method Summary collapse
-
#export ⇒ Object
Export it into a chain of bytes.
-
#import(bytes) ⇒ Object
Import from a chain of bytes.
-
#initialize ⇒ Factbase
constructor
Constructor.
-
#insert ⇒ Factbase::Fact
Insert a new fact.
-
#query(query) ⇒ Object
Create a query capable of iterating.
-
#size ⇒ Integer
Size.
- #to_json(_ = nil) ⇒ Object
Constructor Details
#initialize ⇒ Factbase
Constructor.
29 30 31 32 |
# File 'lib/factbase.rb', line 29 def initialize @maps = [] @mutex = Mutex.new end |
Instance Method Details
#export ⇒ Object
Export it into a chain of bytes.
74 75 76 |
# File 'lib/factbase.rb', line 74 def export Marshal.dump(@maps) end |
#import(bytes) ⇒ Object
Import from a chain of bytes.
79 80 81 82 83 |
# File 'lib/factbase.rb', line 79 def import(bytes) # rubocop:disable Security/MarshalLoad @maps += Marshal.load(bytes) # rubocop:enable Security/MarshalLoad end |
#insert ⇒ Factbase::Fact
Insert a new fact.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/factbase.rb', line 42 def insert require_relative 'factbase/fact' map = {} @mutex.synchronize do f = Factbase::Fact.new(Mutex.new, map) f.id = @maps.size + 1 @maps << map end Factbase::Fact.new(@mutex, map) end |
#query(query) ⇒ Object
Create a query capable of iterating.
There is a Lisp-like syntax, for example:
(eq title 'Object Thinking')
(gt time '2024-03-23T03:21:43')
(gt cost 42)
(exists seenBy)
(and
(eq foo 42)
(or
(gt 200)
(absent zzz)))
68 69 70 71 |
# File 'lib/factbase.rb', line 68 def query(query) require_relative 'factbase/query' Factbase::Query.new(@maps, @mutex, query) end |
#size ⇒ Integer
Size.
36 37 38 |
# File 'lib/factbase.rb', line 36 def size @maps.size end |
#to_json(_ = nil) ⇒ Object
85 86 87 |
# File 'lib/factbase.rb', line 85 def to_json(_ = nil) @maps.to_json end |