Module: Factbase::IndexedTerm
- Defined in:
- lib/factbase/indexed/indexed_term.rb
Overview
Term with an index.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024-2025 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
-
#predict(maps, params) ⇒ Array<Hash>|nil
Reduces the provided list of facts (maps) to a smaller array, if it’s possible.
Instance Method Details
#predict(maps, params) ⇒ Array<Hash>|nil
Reduces the provided list of facts (maps) to a smaller array, if it’s possible.
NIL must be returned if indexing is prohibited in this case.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/factbase/indexed/indexed_term.rb', line 22 def predict(maps, params) case @op when :one key = [maps.object_id, @operands.first, @op] if @idx[key].nil? @idx[key] = [] prop = @operands.first.to_s maps.to_a.each do |m| @idx[key].append(m) if !m[prop].nil? && m[prop].size == 1 end end (maps & []) | @idx[key] when :exists key = [maps.object_id, @operands.first, @op] if @idx[key].nil? @idx[key] = [] prop = @operands.first.to_s maps.to_a.each do |m| @idx[key].append(m) unless m[prop].nil? end end (maps & []) | @idx[key] when :eq if @operands.first.is_a?(Symbol) && _scalar?(@operands[1]) key = [maps.object_id, @operands.first, @op] if @idx[key].nil? @idx[key] = {} prop = @operands.first.to_s maps.to_a.each do |m| m[prop]&.each do |v| @idx[key][v] = [] if @idx[key][v].nil? @idx[key][v].append(m) end end end vv = if @operands[1].is_a?(Symbol) sym = @operands[1].to_s.gsub(/^\$/, '') params[sym] || [] else [@operands[1]] end if vv.empty? nil else j = vv.map { |v| @idx[key][v] || [] }.reduce(&:|) (maps & []) | j end end when :and r = nil @operands.each do |o| n = o.predict(maps, params) if n.nil? r = nil break end if r.nil? r = n else r &= n end break if r.empty? end r when :or r = nil @operands.each do |o| n = o.predict(maps, params) if n.nil? r = nil break end r = maps & [] if r.nil? r |= n end r when :not r = @operands.first.predict(maps, params) if r.nil? nil else (maps & []) | (maps.to_a - r.to_a) end end end |