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

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/factbase/indexed/indexed_term.rb', line 22

def predict(maps, params)
  key = [maps.object_id, @operands.first, @op]
  case @op
  when :one
    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
    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])
      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)
          params[@operands[1].to_s] || []
        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
    if @operands.all? { |o| o.op == :eq } && @operands.size > 1 \
      && @operands.all? { |o| o.operands.first.is_a?(Symbol) && _scalar?(o.operands[1]) }
      key = [maps.object_id, @operands.map { |o| o.operands.first }, :multi_eq]
      props = @operands.map { |o| o.operands.first }.sort
      if @idx[key].nil?
        @idx[key] = {}
        maps.to_a.each do |m|
          _all_tuples(m, props).each do |t|
            @idx[key][t] = [] if @idx[key][t].nil?
            @idx[key][t].append(m)
          end
        end
      end
      tuples = _as_tuples(
        @operands.sort_by { |o| o.operands.first }.map do |o|
          if o.operands[1].is_a?(Symbol)
            params[o.operands[1].to_s] || []
          else
            [o.operands[1]]
          end
        end
      )
      j = tuples.map { |t| @idx[key][t] || [] }.reduce(&:|)
      r = (maps & []) | j
    else
      @operands.each do |o|
        n = o.predict(maps, params)
        if n.nil?
          r = nil
          break
        end
        if r.nil?
          r = n
        else
          r &= n.to_a
        end
        break if r.empty?
      end
    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.to_a
    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