Class: ProgramR::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/programr/graph_master.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Returns a new instance of Node.



37
38
39
40
# File 'lib/programr/graph_master.rb', line 37

def initialize
  @template = nil
  @children = {}
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



36
37
38
# File 'lib/programr/graph_master.rb', line 36

def children
  @children
end

Instance Method Details

#get_template(pattern, starGreedy, isGreedy = false) ⇒ Object



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
# File 'lib/programr/graph_master.rb', line 59

def get_template(pattern,starGreedy,isGreedy=false)  
  currentTemplate = nil
  gotValue        = nil
  curGreedy       = []
  if(@template)
    if(isGreedy)
	starGreedy.push(pattern.shift) until(pattern.empty?||pattern[0]==THAT ||
                                    pattern[0] == TOPIC)
    end
    return @template if(pattern.empty?)
    currentTemplate = @template if(pattern[0] == THAT || pattern[0] == TOPIC)
  end
  branch = pattern.shift
  isGreedy = false if(branch == THAT || branch == TOPIC)
  unless(branch != THAT || @children.key?(THAT))
    branch = pattern.shift until (branch == nil or branch == TOPIC)
  end
  return nil unless(branch)
  if(@children[branch])
    gotValue = @children[branch].get_template(pattern.clone,curGreedy)
  elsif(isGreedy)
    curGreedy.push(branch)
    gotValue = get_template(pattern.clone,curGreedy,true)
  end
  if(gotValue)
    starGreedy.push(branch) if(branch == THAT || branch == TOPIC)
    starGreedy.concat(curGreedy)
    return gotValue
  end
  return currentTemplate if currentTemplate   
  ["_","*"].each do |star|
    next unless(@children.key?(star))
    next unless(gotValue=@children[star].get_template(pattern.clone,
                                                      curGreedy,true))
starGreedy.push(branch) if(branch == THAT || branch == TOPIC)
    starGreedy.concat(['<newMatch>',branch].concat(curGreedy))
    return gotValue 
  end
  return nil
end

#inspectNode(nodeId = nil, ind = 0) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/programr/graph_master.rb', line 100

def inspectNode(nodeId = nil, ind = 0)
  str = ''
  str += '| '*(ind - 1) + "|_#{nodeId}" unless ind == 0
  str += ": [#{@template.inspect}]" if @template
  str += "\n" unless ind == 0
  @children.each_key{|c| str += @children[c].inspectNode(c, ind+1)}
  str
end

#learn(category, path) ⇒ Object



52
53
54
55
56
57
# File 'lib/programr/graph_master.rb', line 52

def learn(category, path)
  branch = path.shift
  return @template = category.template unless branch
  @children[branch] = Node.new unless @children[branch]
  @children[branch].learn(category, path)
end

#merge(aCache) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/programr/graph_master.rb', line 42

def merge(aCache)
  aCache.children.keys.each do |key|
    if(@children.key?(key))
      @children[key].merge(aCache.children[key])
	next
    end
    @children[key] = aCache.children[key]
  end
end