Class: RGen::Util::PatternMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rgen/util/pattern_matcher.rb

Overview

A PatternMatcher can be used to find, insert and remove patterns on a given model.

A pattern is specified by means of a block passed to the add_pattern method. The block must take an Environment as first parameter and at least one model element as connection point as further parameter. The pattern matches if it can be found in a given environment and connected to the given connection point elements.

Defined Under Namespace

Classes: Bindable, CheckLater, Lazy, Match, Proxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePatternMatcher

Returns a new instance of PatternMatcher.



17
18
19
20
21
# File 'lib/rgen/util/pattern_matcher.rb', line 17

def initialize
  @patterns = {} 
  @insert_mode = false
  @debug = false
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



15
16
17
# File 'lib/rgen/util/pattern_matcher.rb', line 15

def debug
  @debug
end

Instance Method Details

#add_pattern(name, &block) ⇒ Object



23
24
25
26
27
28
# File 'lib/rgen/util/pattern_matcher.rb', line 23

def add_pattern(name, &block)
  raise "a pattern needs at least 2 block parameters: " + 
    "an RGen environment and a model element as connection point" \
    unless block.arity >= 2
  @patterns[name] = block
end

#find_pattern(env, name, *connection_points) ⇒ Object



30
31
32
# File 'lib/rgen/util/pattern_matcher.rb', line 30

def find_pattern(env, name, *connection_points)
  match = find_pattern_internal(env, name, *connection_points)
end

#insert_pattern(env, name, *connection_points) ⇒ Object



34
35
36
37
38
39
# File 'lib/rgen/util/pattern_matcher.rb', line 34

def insert_pattern(env, name, *connection_points)
  @insert_mode = true
  root = evaluate_pattern(name, env, connection_points)
  @insert_mode = false
  root
end

#lazy(&block) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/rgen/util/pattern_matcher.rb', line 54

def lazy(&block)
  if @insert_mode
    block.call
  else
    Lazy.new(&block)
  end
end

#remove_pattern(env, name, *connection_points) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rgen/util/pattern_matcher.rb', line 41

def remove_pattern(env, name, *connection_points)
  match = find_pattern_internal(env, name, *connection_points)
  if match
    match.elements.each do |e|
      disconnect_element(e)
      env.delete(e)
    end
    match
  else
    nil
  end
end