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, Looged, Pre, Query, Spy, Syntax, Term, WhiteList

Instance Method Summary collapse

Constructor Details

#initializeFactbase

Constructor.



33
34
35
36
# File 'lib/factbase.rb', line 33

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

Instance Method Details

#empty?Boolean

Is it empty?

Returns:

  • (Boolean)

    TRUE if there are no facts inside



40
41
42
# File 'lib/factbase.rb', line 40

def empty?
  @maps.empty?
end

#exportObject

Export it into a chain of bytes.



82
83
84
# File 'lib/factbase.rb', line 82

def export
  Marshal.dump(@maps)
end

#import(bytes) ⇒ Object

Import from a chain of bytes.



87
88
89
90
91
# File 'lib/factbase.rb', line 87

def import(bytes)
  # rubocop:disable Security/MarshalLoad
  @maps += Marshal.load(bytes)
  # rubocop:enable Security/MarshalLoad
end

#insertFactbase::Fact

Insert a new fact.

Returns:



52
53
54
55
56
57
58
59
# File 'lib/factbase.rb', line 52

def insert
  require_relative 'factbase/fact'
  map = {}
  @mutex.synchronize do
    @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



76
77
78
79
# File 'lib/factbase.rb', line 76

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

#sizeInteger

Size.

Returns:

  • (Integer)

    How many facts are in there



46
47
48
# File 'lib/factbase.rb', line 46

def size
  @maps.size
end

#to_json(_ = nil) ⇒ String

Convert the entire factbase into JSON.

Returns:

  • (String)

    The factbase in JSON format



95
96
97
# File 'lib/factbase.rb', line 95

def to_json(_ = nil)
  @maps.to_json
end

#to_xmlString

Convert the entire factbase into XML.

Returns:

  • (String)

    The factbase in XML format



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/factbase.rb', line 101

def to_xml
  Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.fb do
      @maps.each do |m|
        xml.f_ do
          m.each do |k, vv|
            if vv.is_a?(Array)
              xml.send(:"#{k}_") do
                vv.each do |v|
                  xml.send(:v, v)
                end
              end
            else
              xml.send(:"#{k}_", vv)
            end
          end
        end
      end
    end
  end.to_xml
end

#to_yamlString

Convert the entire factbase into YAML.

Returns:

  • (String)

    The factbase in YAML format



125
126
127
# File 'lib/factbase.rb', line 125

def to_yaml
  YAML.dump({ 'facts' => @maps })
end