class RoadForest::PathMatcher

Attributes

completed_child[R]
logging[RW]
pattern[RW]
pattern_root[W]

Public Class Methods

new() click to toggle source
# File lib/roadforest/path-matcher.rb, line 373
def initialize()
  @logging = false
  reset
end

Public Instance Methods

add_matching_nodes(list) click to toggle source
# File lib/roadforest/path-matcher.rb, line 448
def add_matching_nodes(list)
  list.each do |node|
    log "  Adding step:", node
  end
  @search_queue += list
end
check_complete(matching) click to toggle source
# File lib/roadforest/path-matcher.rb, line 463
def check_complete(matching)
  while matching.resolved?
    log "Checking:", matching
    matching.parent.notify_resolved(matching)
    matching = matching.parent
  end
end
complete?() click to toggle source
# File lib/roadforest/path-matcher.rb, line 420
def complete?
  !@completed_child.nil? or @search_queue.empty?
end
log(*args) click to toggle source
# File lib/roadforest/path-matcher.rb, line 455
def log(*args)
  puts args.join(" ") if @logging
end
match(root, graph) click to toggle source
# File lib/roadforest/path-matcher.rb, line 382
def match(root, graph)
  reset
  setup(root, graph)
  search_iteration until complete?
  return Match.new(self)
end
next_matching_node() click to toggle source
# File lib/roadforest/path-matcher.rb, line 444
def next_matching_node
  @search_queue.pop #simple depth first
end
notify_resolved(matching) click to toggle source
# File lib/roadforest/path-matcher.rb, line 424
def notify_resolved(matching)
  log "Resolved:", matching
  @completed_child = matching
end
pattern_root() click to toggle source
# File lib/roadforest/path-matcher.rb, line 409
def pattern_root
  @pattern_root ||=
    begin
      roots = pattern.query(:predicate => ::RDF.type, :object => Graph::Path.Root).to_a
      if roots.length != 1
        raise "A pattern should have exactly one root, has: #{roots.length}\n#{roots.map(&:inspect).join("\n")}"
      end
      roots.first.subject
    end
end
reset() click to toggle source
# File lib/roadforest/path-matcher.rb, line 389
def reset
  @search_queue = []
  @completed_child = nil
end
resolved?() click to toggle source
# File lib/roadforest/path-matcher.rb, line 459
def resolved?
  false
end
search_iteration() click to toggle source
# File lib/roadforest/path-matcher.rb, line 429
def search_iteration
  matching = next_matching_node
  unless matching.nil?
    matching.open
    if matching.children.empty?
      log "No match:", matching
    else
      log "Matches for:", matching
    end
    add_matching_nodes(matching.children)

    check_complete(matching)
  end
end
setup(root, graph) click to toggle source
# File lib/roadforest/path-matcher.rb, line 394
def setup(root, graph)
  add_matching_nodes([Node.new do |node|
    node.parent = self
    node.stem = {}
    node.repeats = {}
    node.pattern = pattern
    node.graph = graph

    node.statement = nil
    node.graph_term = root
    node.pattern_step = pattern_root
  end
  ])
end