Class: KnowledgeBase::Traverser
- Inherits:
-
Object
- Object
- KnowledgeBase::Traverser
- Defined in:
- lib/scout/knowledge_base/traverse.rb
Instance Attribute Summary collapse
-
#assignments ⇒ Object
Returns the value of attribute assignments.
-
#kb ⇒ Object
Returns the value of attribute kb.
-
#matches ⇒ Object
Returns the value of attribute matches.
-
#rules ⇒ Object
Returns the value of attribute rules.
Instance Method Summary collapse
- #_ep(paths) ⇒ Object
- #_fp(rules, clean_matches, assignments) ⇒ Object
- #clean_matches(rules, all_matches, assignments) ⇒ Object
- #find_paths(rules, all_matches, assignments) ⇒ Object
- #id_dbs(db) ⇒ Object
- #identify(db, source, target) ⇒ Object
-
#initialize(kb, rules = []) ⇒ Traverser
constructor
A new instance of Traverser.
- #is_list?(name) ⇒ Boolean
- #is_wildcard?(name) ⇒ Boolean
- #reassign(matches, source, target) ⇒ Object
- #traverse(nopaths = false) ⇒ Object
- #traverse_db(db, source, target, conditions) ⇒ Object
- #wildcard(name) ⇒ Object
Constructor Details
#initialize(kb, rules = []) ⇒ Traverser
Returns a new instance of Traverser.
6 7 8 9 10 11 |
# File 'lib/scout/knowledge_base/traverse.rb', line 6 def initialize(kb, rules = []) @kb = kb @rules = rules @assignments = {} @matches = {} end |
Instance Attribute Details
#assignments ⇒ Object
Returns the value of attribute assignments.
4 5 6 |
# File 'lib/scout/knowledge_base/traverse.rb', line 4 def assignments @assignments end |
#kb ⇒ Object
Returns the value of attribute kb.
4 5 6 |
# File 'lib/scout/knowledge_base/traverse.rb', line 4 def kb @kb end |
#matches ⇒ Object
Returns the value of attribute matches.
4 5 6 |
# File 'lib/scout/knowledge_base/traverse.rb', line 4 def matches @matches end |
#rules ⇒ Object
Returns the value of attribute rules.
4 5 6 |
# File 'lib/scout/knowledge_base/traverse.rb', line 4 def rules @rules end |
Instance Method Details
#_ep(paths) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/scout/knowledge_base/traverse.rb', line 116 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
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 |
# File 'lib/scout/knowledge_base/traverse.rb', line 79 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] 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 if matches return false if paths.empty? paths end |
#clean_matches(rules, all_matches, assignments) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/scout/knowledge_base/traverse.rb', line 56 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
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/scout/knowledge_base/traverse.rb', line 133 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
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/scout/knowledge_base/traverse.rb', line 167 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
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/scout/knowledge_base/traverse.rb', line 26 def identify(db, source, target) source_entities = if is_wildcard? source assignments[source] || :all elsif is_list? source kb.load_list(source[1..-1]) else kb.identify_source db, source end target_entities = if is_wildcard? target assignments[target] || :all elsif is_list? target kb.load_list(target[1..-1]) 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_list?(name) ⇒ Boolean
22 23 24 |
# File 'lib/scout/knowledge_base/traverse.rb', line 22 def is_list?(name) name[0] == ':' end |
#is_wildcard?(name) ⇒ Boolean
18 19 20 |
# File 'lib/scout/knowledge_base/traverse.rb', line 18 def is_wildcard?(name) name[0] == '?' end |
#reassign(matches, source, target) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/scout/knowledge_base/traverse.rb', line 49 def reassign(matches, source, target) #assignments[source] = (matches.any? ? matches.collect{|m| m.source_entity }.uniq : nil) if is_wildcard? source #assignments[target] = (matches.any? ? matches.collect{|m| m.target_entity }.uniq : nil) if is_wildcard? target assignments[source] = (matches.any? ? matches.source_entity.uniq : nil) if is_wildcard? source assignments[target] = (matches.any? ? matches.target_entity.uniq : nil) if is_wildcard? target end |
#traverse(nopaths = false) ⇒ Object
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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/scout/knowledge_base/traverse.rb', line 189 def traverse(nopaths = false) 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 = nil 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 if rule_matches.nil? rule_matches = matches else matches.each do |m| rule_matches << m end end assignments.each{|k,v| v.uniq! if v} end reassign rule_matches, source, target if rule_matches 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) names = names.collect{|name| assignments.include?(name) ? assignments[name] : name}.flatten 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 return [assignments, nil] if nopaths 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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/scout/knowledge_base/traverse.rb', line 146 def traverse_db(db, source, target, conditions) source_entities, target_entities = identify db, source, target = {:source => source_entities, :target => target_entities} Log.debug "Traversing #{ db }: #{Log.fingerprint options}" matches = kb.subset(db, ) 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
13 14 15 16 |
# File 'lib/scout/knowledge_base/traverse.rb', line 13 def wildcard(name) return name unless is_wildcard?(name) assignments[name] || name end |