Class: RoadForest::PathMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/path-matcher.rb

Defined Under Namespace

Classes: Edge, Failure, ForwardEdge, Match, MatchStep, NoMatch, Node, ReverseEdge

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePathMatcher

Returns a new instance of PathMatcher.



373
374
375
376
# File 'lib/roadforest/path-matcher.rb', line 373

def initialize()
  @logging = false
  reset
end

Instance Attribute Details

#completed_childObject (readonly)

Returns the value of attribute completed_child.



379
380
381
# File 'lib/roadforest/path-matcher.rb', line 379

def completed_child
  @completed_child
end

#loggingObject

Returns the value of attribute logging.



378
379
380
# File 'lib/roadforest/path-matcher.rb', line 378

def logging
  @logging
end

#patternObject

Returns the value of attribute pattern.



378
379
380
# File 'lib/roadforest/path-matcher.rb', line 378

def pattern
  @pattern
end

#pattern_rootObject



409
410
411
412
413
414
415
416
417
418
# 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

Instance Method Details

#add_matching_nodes(list) ⇒ Object



448
449
450
451
452
453
# 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) ⇒ Object



463
464
465
466
467
468
469
# 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?Boolean

Returns:

  • (Boolean)


420
421
422
# File 'lib/roadforest/path-matcher.rb', line 420

def complete?
  !@completed_child.nil? or @search_queue.empty?
end

#log(*args) ⇒ Object



455
456
457
# File 'lib/roadforest/path-matcher.rb', line 455

def log(*args)
  puts args.join(" ") if @logging
end

#match(root, graph) ⇒ Object



382
383
384
385
386
387
# 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_nodeObject



444
445
446
# File 'lib/roadforest/path-matcher.rb', line 444

def next_matching_node
  @search_queue.pop #simple depth first
end

#notify_resolved(matching) ⇒ Object



424
425
426
427
# File 'lib/roadforest/path-matcher.rb', line 424

def notify_resolved(matching)
  log "Resolved:", matching
  @completed_child = matching
end

#resetObject



389
390
391
392
# File 'lib/roadforest/path-matcher.rb', line 389

def reset
  @search_queue = []
  @completed_child = nil
end

#resolved?Boolean

Returns:

  • (Boolean)


459
460
461
# File 'lib/roadforest/path-matcher.rb', line 459

def resolved?
  false
end

#search_iterationObject



429
430
431
432
433
434
435
436
437
438
439
440
441
442
# 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) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
# 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