Class: KnowledgeBase::Traverser

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/knowledge_base/traverse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kb, rules = []) ⇒ Traverser

Returns a new instance of Traverser.



9
10
11
12
13
14
# File 'lib/rbbt/knowledge_base/traverse.rb', line 9

def initialize(kb, rules = [])
  @kb = kb
  @rules = rules
  @assignments = {}
  @matches = {}
end

Instance Attribute Details

#assignmentsObject

Returns the value of attribute assignments.



7
8
9
# File 'lib/rbbt/knowledge_base/traverse.rb', line 7

def assignments
  @assignments
end

#kbObject

Returns the value of attribute kb.



7
8
9
# File 'lib/rbbt/knowledge_base/traverse.rb', line 7

def kb
  @kb
end

#matchesObject

Returns the value of attribute matches.



7
8
9
# File 'lib/rbbt/knowledge_base/traverse.rb', line 7

def matches
  @matches
end

#rulesObject

Returns the value of attribute rules.



7
8
9
# File 'lib/rbbt/knowledge_base/traverse.rb', line 7

def rules
  @rules
end

Instance Method Details

#_ep(paths) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rbbt/knowledge_base/traverse.rb', line 109

def _ep(paths)
  found = []
  paths.each do |match,_next|
    case _next
    when TrueClass
      found << [match]
    when FalseClass
      next
    else
      _ep(_next).each do |_n|
        found << [match] + _n
      end
    end
  end
  found
end

#_fp(rules, clean_matches, assignments) ⇒ Object



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/rbbt/knowledge_base/traverse.rb', line 72

def _fp(rules, clean_matches, assignments)
  return true if rules.empty?

  rule, *rest = rules
  source, db, target = rule.split /\s+/

  wildcard_source = is_wildcard? source
  wildcard_target = is_wildcard? target

  paths = {}
  matches = clean_matches[rule]
  Annotated.purge(matches).each do |match|
    new_assignments = nil
    match_source, _sep, match_target = match.partition "~"

    if wildcard_source
      next if assignments[source] and assignments[source]  != match_source
      new_assignments ||= assignments.dup
      new_assignments[source] = match_source
    end

    if wildcard_target
      next if assignments[target] and assignments[target]  != match_target
      new_assignments ||= assignments.dup
      new_assignments[target] = match_target
    end

    new_paths = _fp(rest, clean_matches, new_assignments)
    next unless new_paths
    paths[match] = new_paths
  end

  return false if paths.empty?

  paths 
end

#clean_matches(rules, all_matches, assignments) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rbbt/knowledge_base/traverse.rb', line 49

def clean_matches(rules, all_matches, assignments)
  paths = {}

  rules.zip(all_matches).each do |rule, matches|
    source, db, target = rule.split /\s+/
    next if matches.nil?

    if is_wildcard? source
      assigned = assignments[source] || []
      matches = matches.select{|m| assigned.include? m.partition("~").first }
    end
    
    if is_wildcard? target
      assigned = assignments[target] || []
      matches = matches.select{|m| assigned.include? m.partition("~").last }
    end

    paths[rule] = matches
  end

  paths
end

#find_paths(rules, all_matches, assignments) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rbbt/knowledge_base/traverse.rb', line 126

def find_paths(rules, all_matches, assignments)
  clean_matches = clean_matches(rules, all_matches, assignments)

  path_hash = _fp(rules, clean_matches, {})

  return [] unless path_hash
  _ep(path_hash).collect do |path|
    path.zip(clean_matches.values_at(*rules)).collect do |item, matches|
      matches.select{|m| m == item}.first
    end
  end
end

#id_dbs(db) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rbbt/knowledge_base/traverse.rb', line 160

def id_dbs(db)
  # ToDo: Revise this, I'm not sure what id does anymore
  # I think it deals with syndication
  if db.include? '?'
    all_dbs = kb.registry.keys.collect{|k| k.to_s }
    _name, _sep, _kb = db.partition("@")
    case
    when _name[0] == '?'
      dbs = all_dbs.select{|_db| 
        n,_s,d=_db.partition("@"); 
        d.nil? or d.empty? or (d == _kb and assignments[_name].include?(n))
      }
    when _kb[0] == '?'
      dbs = all_dbs.select{|_db| n,_s,d=_db.partition("@"); n == _name and assignments[_kb].include?(d) }
    end
  else
    dbs = [db]
  end

  dbs
end

#identify(db, source, target) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rbbt/knowledge_base/traverse.rb', line 25

def identify(db, source, target)
  source_entities = if is_wildcard? source
                      assignments[source] || :all
                    else
                      kb.identify_source db, source
                    end

  target_entities = if is_wildcard? target
                      assignments[target] || :all
                    else
                      kb.identify_target db, target
                    end

  source_entities = [source_entities] unless Array === source_entities or source_entities == :all
  target_entities = [target_entities] unless Array === target_entities or target_entities == :all

  [source_entities, target_entities]
end

#is_wildcard?(name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rbbt/knowledge_base/traverse.rb', line 21

def is_wildcard?(name)
  name[0] == '?'
end

#reassign(matches, source, target) ⇒ Object



44
45
46
47
# File 'lib/rbbt/knowledge_base/traverse.rb', line 44

def reassign(matches, source, target)
  assignments[source] = (matches.any? ? matches.collect{|m|m.partition("~").first}.uniq : nil) if is_wildcard? source
  assignments[target] = (matches.any? ? matches.collect{|m|m.partition("~").last}.uniq : nil) if is_wildcard? target
end

#traverseObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/rbbt/knowledge_base/traverse.rb', line 182

def traverse
  all_matches = []
  path_rules = []
  acc_var = nil
  pre_acc_var_assignments = nil
  rules.each do |rule|
    rule = rule.strip
    next if rule.empty?

    if m = rule.match(/([^\s]+)\s+([^\s=]+)\s+([^\s]+)(?:\s+-\s+(.*))?/)
      Log.debug "Traverse rule: #{rule}"
      path_rules << rule

      source, db, target, conditions = m.captures

      dbs = id_dbs(db)

      rule_matches = []
      dbs.each do |_db|
        matches = traverse_db(_db, source, target, conditions)

        next if matches.nil? or matches.empty?

        # ToDo: Revise this, I'm not sure what id does anymore
        #
        #if db.include? '?'
        #  _name, _sep, _kb = db.partition("@")
        #  case
        #  when _kb[0] == '?'
        #    assignments[_kb] ||= []
        #    assignments[_kb] << _db.partition("@").reject{|p| p.empty?}.last
        #  when _name[0] == '?'
        #    assignments[_name] ||= []
        #    assignments[_name] << _db.partition("@").first
        #  end
        #end

        matches.each do |m|
          rule_matches << m
        end

        assignments.each{|k,v| v.uniq! if v}
      end

      reassign rule_matches, source, target

      all_matches << rule_matches

    elsif m = rule.match(/([^\s=]+)\s*=([^\s]*)\s*(.*)/)
      Log.debug "Assign rule: #{rule}"
      var, db, value_str = m.captures
      names = value_str.split(",").collect{|v| v.strip}
      if db.empty?
        ids = names
      else
        dbs = id_dbs(db)
        ids = names.collect{|name| 
          id = nil
          dbs.each do |db|
            sid, tid = identify db, name, name
            id = (sid + tid).compact.first
            break if id
          end
          id
        }
      end
      assignments[var] = ids

    elsif m = rule.match(/(\?[^\s{]+)\s*{/)
      acc_var = m.captures.first
      pre_acc_var_assignments = assignments.dup
      Log.debug "Start assign block: #{acc_var}"
    elsif m = rule.match(/^\s*}\s*$/)
      Log.debug "Close assign block: #{acc_var}"
      saved_assign = assignments[acc_var]
      assignments.clear
      assignments.merge!(pre_acc_var_assignments)
      pre_acc_var_assignments = nil
      assignments[acc_var] = saved_assign
      all_matches = []
      path_rules = []
    else
      raise "Rule not understood: #{rule}"
    end
  end

  Log.debug "Finding paths: #{all_matches.length}"
  paths = find_paths path_rules, all_matches, assignments
  Log.debug "Found paths: #{paths.length}"

  [assignments, paths]
end

#traverse_db(db, source, target, conditions) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rbbt/knowledge_base/traverse.rb', line 139

def traverse_db(db, source, target, conditions)
  source_entities, target_entities = identify db, source, target

  options = {:source => source_entities, :target => target_entities}
  Log.debug "Traversing #{ db }: #{Misc.fingerprint options}"
  matches = kb.subset(db, options)

  if conditions
    Misc.tokenize(conditions).each do |condition|
      if condition.index "="
        key, value = condition.split("=")
        matches = matches.select{|m| Misc.match_value(m.info[key.strip], value)}
      else
        matches = matches.select{|m| m.info[condition.strip].to_s =~ /\btrue\b/}
      end
    end
  end

  matches
end

#wildcard(name) ⇒ Object



16
17
18
19
# File 'lib/rbbt/knowledge_base/traverse.rb', line 16

def wildcard(name)
  return name unless is_wildcard?(name)
  assignments[name] || name
end