Class: KnowledgeBase::Traverser
- Inherits:
-
Object
- Object
- KnowledgeBase::Traverser
- Defined in:
- lib/rbbt/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
- #identify(db, source, target) ⇒ Object
-
#initialize(kb, rules = []) ⇒ Traverser
constructor
A new instance of Traverser.
- #is_wildcard?(name) ⇒ Boolean
- #reassign(matches, source, target) ⇒ Object
- #traverse ⇒ Object
- #wildcard(name) ⇒ Object
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
#assignments ⇒ Object
Returns the value of attribute assignments.
7 8 9 |
# File 'lib/rbbt/knowledge_base/traverse.rb', line 7 def assignments @assignments end |
#kb ⇒ Object
Returns the value of attribute kb.
7 8 9 |
# File 'lib/rbbt/knowledge_base/traverse.rb', line 7 def kb @kb end |
#matches ⇒ Object
Returns the value of attribute matches.
7 8 9 |
# File 'lib/rbbt/knowledge_base/traverse.rb', line 7 def matches @matches end |
#rules ⇒ Object
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
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rbbt/knowledge_base/traverse.rb', line 105 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
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 |
# File 'lib/rbbt/knowledge_base/traverse.rb', line 71 def _fp(rules, clean_matches, assignments) return true if rules.empty? rule, *rest = rules source, db, target = rule.split /\s+/ paths = {} matches = clean_matches[rule] Annotated.purge(matches).each do |match| new_assignments = nil match_source, _sep, match_target = match.partition "~" if is_wildcard? source next if assignments[source] and assignments[source] != match_source new_assignments ||= assignments.dup new_assignments[source] = match_source end if is_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 |
# 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+/ if is_wildcard? source assigned = assignments[source] matches = matches.select{|m| assigned.include? m.source } end if is_wildcard? target assigned = assignments[target] matches = matches.select{|m| assigned.include? m.target } end paths[rule] = matches end paths end |
#find_paths(rules, all_matches, assignments) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rbbt/knowledge_base/traverse.rb', line 122 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.first.annotate item.dup end end 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
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.source.uniq : []) if is_wildcard? source assignments[target] = (matches.any? ? matches.target.uniq : []) if is_wildcard? target end |
#traverse ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rbbt/knowledge_base/traverse.rb', line 136 def traverse all_matches = [] rules.each do |rule| rule = rule.strip next if rule.empty? source, db, target, conditions = rule.match(/([^\s]+)\s+([^\s]+)\s+([^\s]+)(?:\s+-\s+([^\s]+))?/).captures source_entities, target_entities = identify db, source, target matches = kb.subset(db, :source => source_entities, :target => target_entities) if conditions conditions.split(/\s+/).each do |condition| if condition.index "=" key, value = conditions.split("=") matches = matches.select{|m| m.info[key.strip].to_s =~ /\b#{value.strip}\b/} else matches = matches.select{|m| m.info[condition.strip].to_s =~ /\btrue\b/} end end end reassign matches, source, target all_matches << matches end paths = find_paths rules, all_matches, assignments [assignments, paths] 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 |