Class: Factbase

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeFactbase

Constructor.



29
30
31
32
# File 'lib/factbase.rb', line 29

def initialize
  @maps = []
  @mutex = Mutex.new
end

Instance Method Details

#exportObject

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

#insertFactbase::Fact

Insert a new fact.

Returns:



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 bar 200)
    (absent zzz)))

Parameters:

  • query (String)

    The query to use for selections



68
69
70
71
# File 'lib/factbase.rb', line 68

def query(query)
  require_relative 'factbase/query'
  Factbase::Query.new(@maps, @mutex, query)
end

#sizeInteger

Size.

Returns:

  • (Integer)

    How many facts are in there



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