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

#find_paths(rules, all_matches, assignments) ⇒ Object



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

def find_paths(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

#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.source.uniq : []) if is_wildcard? source
  assignments[target] = (matches.any? ? matches.target.uniq : []) if is_wildcard? target
end

#traverseObject



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
# File 'lib/rbbt/knowledge_base/traverse.rb', line 69

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