class Lindenmayer::ContextSensitiveProduction

Context-Sensitive Production, checks if value matches in context Handles left-only, right-only, and left-right context

Attributes

key[R]

Public Class Methods

new(key, transform, options = {}) click to toggle source

key - (string) Full key, e.g. “AB<C>DE” transform - (string) Replacement if matching

# File lib/context_sensitive_production.rb, line 10
def initialize(key, transform, options = {})
  @lookahead = key.include?('>') ? key.rpartition('>').last : nil
  @lookbehind = key.include?('<') ? key.partition('<').first : nil
  @key = key.rpartition('<').last.partition('>').first
  @transform = transform
  post_init(options)
end

Public Instance Methods

transform(idx, context) click to toggle source
# File lib/context_sensitive_production.rb, line 18
def transform(idx, context)
  if (@lookbehind.nil? ||
      includes_full_context?(context[0, idx], @lookbehind)) &&
     (@lookahead.nil? ||
      includes_full_context?(context[(idx + 1)..-1], @lookahead))
    apply_transform
  else
    @key
  end
end

Private Instance Methods

includes_full_context?(context, required_context) click to toggle source
# File lib/context_sensitive_production.rb, line 31
def includes_full_context?(context, required_context)
  required_context = required_context.dup.split('')

  context.split('').each do |char|
    required_context.shift if required_context[0] == char
  end

  required_context.empty?
end