Class: Construqt::Flavour::Ciscian::PatternBasedVerb

Inherits:
Object
  • Object
show all
Defined in:
lib/construqt/flavour/ciscian/ciscian.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(section) ⇒ PatternBasedVerb

Returns a new instance of PatternBasedVerb.



373
374
375
376
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 373

def initialize(section)
  self.section=section
  self.changes=[]
end

Instance Attribute Details

#changesObject

Returns the value of attribute changes.



371
372
373
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 371

def changes
  @changes
end

#sectionObject

Returns the value of attribute section.



371
372
373
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 371

def section
  @section
end

Class Method Details

.compare(nu, old) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 451

def self.compare(nu, old)
  nu_ports=nu.nil? ? {} : nu.integrate
  old_ports=old.nil? ? {} : old.integrate

  result = self.new(self.section)

  key_var = (old||nu).find_key_var
  set_keys = (old||nu).keys_of_set + (nu||old).keys_of_set

  set_keys.each do |key_val|
    variables(self.patterns).each do |v|
      if is_key_value?(v)
        result.add({key_var => key_val})
      elsif is_value?(v)
        result.add({key_var => key_val, v => (nu_ports[key_val] && nu_ports[key_val][v]) ? nu_ports[key_val][v] : nil})
      else
        set = []
        set += nu_ports[key_val][v] if nu_ports[key_val]
        set += old_ports[key_val][invert(v)] if old_ports[key_val] && old_ports[key_val][invert(v)]
        set -= nu_ports[key_val][invert(v)] if nu_ports[key_val] && nu_ports[key_val][invert(v)]
        set -= old_ports[key_val][v] if old_ports[key_val]
        result.add({key_var => key_val, v => set}) if set.length > 0
      end
    end
  end

  return [result]
end

.extract_varname(variable) ⇒ Object



416
417
418
419
420
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 416

def self.extract_varname(variable)
  matchdata=variable.match(/{(\+|\-|\=|\*)([^}]+)}/)
  throw "could not extract varname from #{variable}" unless matchdata
  return matchdata[2]
end

.find_regex(varname) ⇒ Object



412
413
414
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 412

def self.find_regex(varname)
  return nil
end

.find_variables(pattern) ⇒ Object



401
402
403
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 401

def self.find_variables(pattern)
  return pattern.scan(/{[^}]+}/)
end

.invert(a) ⇒ Object



386
387
388
389
390
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 386

def self.invert(a)
  return a.gsub(/\+/,"-") if a.match(/\+/)
  return a.gsub(/\-/,"+") if a.match(/\-/)
  throw "cannot invert #{a}"
end

.is_key_value?(v) ⇒ Boolean

Returns:

  • (Boolean)


480
481
482
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 480

def self.is_key_value?(v)
  return !v.nil? && v.start_with?("{*")
end

.is_value?(v) ⇒ Boolean

Returns:

  • (Boolean)


484
485
486
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 484

def self.is_value?(v)
  return !v.nil? && v.start_with?("{=")
end

.matches(patterns, line) ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 422

def self.matches(patterns, line)
  patterns.each do |pattern|
    variables = find_variables(pattern)
    regex=pattern
    variables.each do |var|
      var_regex = find_regex(extract_varname(var))
      var_regex = "#{Construqt::Util::PORTS_DEF_REGEXP}" unless var_regex
      regex=regex.gsub(var, var_regex)
    end

    regex=regex.gsub(" ", "\\s+")
    regex="^"+regex+"$"
    if (matchdata=line.match(regex))
      values={"pattern" => pattern}
      (1..variables.length).each do |i|
        if find_regex(extract_varname(variables[i-1])).nil?
          values[variables[i-1]]=Construqt::Util.expandRangeDefinition(matchdata[i])
        else
          values[variables[i-1]]=[matchdata[i]]
        end
      end

      return values
    end
  end

  return false
end

.parse_line(line, lines, section, result) ⇒ Object



405
406
407
408
409
410
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 405

def self.parse_line(line, lines, section, result)
  entry=matches(self.patterns, line.to_s)
  return false unless entry
  section.add(self.section, self).add(entry)
  return true
end

.variables(patterns) ⇒ Object



392
393
394
395
396
397
398
399
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 392

def self.variables(patterns)
  variables=[]
  patterns.each do |pattern|
    variables+=find_variables(pattern)
  end

  return variables
end

Instance Method Details

#add(entry) ⇒ Object



381
382
383
384
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 381

def add(entry)
  changes << entry
  self
end

#determine_output_patterns(value_sets) ⇒ Object



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 564

def determine_output_patterns(value_sets)
  output_patterns=[]
  empty_pattern = nil
  self.class.patterns.each do |pattern|
    pvs = self.class.find_variables(pattern)
    if (pvs.empty?)
      empty_pattern=pattern
    else
      pvs.each do |pv|
        if (!value_sets[pv].nil? && !value_sets[pv].empty?)
          output_patterns << pattern
          break
        end
      end
    end
  end

  output_patterns << empty_pattern if output_patterns.empty? && !empty_pattern.nil?
  return output_patterns
end

#find_key_varObject



488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 488

def find_key_var
  #find variable for key
  key_var=nil
  changes.each do |entry|
    entry.keys.each do |v|
      if self.class.is_key_value?(v)
        throw "can only have one key value variable in pattern {*var}" if key_var !=nil && key_var !=v
        key_var = v
      end
    end
  end

  return key_var
end

#integrateObject



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 518

def integrate
  set_keys = keys_of_set

  #initialize sets
  sets={}
  set_keys.each do |key_val|
    sets[key_val] = {}
  end

  #initialize start values of values inside sets
  set_keys.each do |key_val|
    self.class.variables(self.class.patterns).each do |v|
      if self.class.is_key_value?(v)
        sets[key_val][v]=key_val
      elsif self.class.is_value?(v)
        sets[key_val][v]=nil
      else
        sets[key_val][v]=[]
      end
    end
  end

  key_var=find_key_var
  changes.each do |entry|
    sets.each do|key_val,value_sets|
      value_sets.each do |v,set|
        if (entry[key_var]==key_val)
          if self.class.is_key_value?(v)
            value_sets[v] = entry[v]
          elsif self.class.is_value?(v)
            value_sets[v] = entry[v]
          else
            value_sets[v] += entry[v] if entry[v]
            #remove duplicates without changing insertion order:
            value_sets[v] = value_sets[v].reverse.uniq.reverse

            value_sets[self.class.invert(v)] -= entry[v] if entry[v] && value_sets[self.class.invert(v)]
          end
        end
      end
    end
  end

  return sets
end

#keys_of_setObject



503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 503

def keys_of_set
  keys=[]

  key_var=find_key_var

  if key_var
    changes.each do |entry|
      keys << entry[key_var] if entry[key_var]
    end
  end

  return keys unless keys.empty?
  return [nil]
end

#serializeObject



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 585

def serialize
  buffer = []
  sets = integrate

  # deactivate grouping, if one variable has a custom regex (i.e. is not a range definition)
  group=true
  self.class.variables(self.class.patterns).each do |v|
    group=false unless self.class.find_regex(self.class.extract_varname(v)).nil?
  end

  sets.each do |key_val,value_sets|
    determine_output_patterns(value_sets).each do |pattern|
      index = 0
      i = 0
      begin
        substitution=false
        result = pattern
        i += 1
        self.class.find_variables(pattern).each do |v|
          if (group)
            if self.class.is_value?(v) || self.class.is_key_value?(v)
              result = result.gsub(v, value_sets[v].to_s) unless value_sets[v].nil? || value_sets[v].to_s.empty?
            else
              result = result.gsub(v, Construqt::Util.createRangeDefinition(value_sets[v])) unless value_sets[v].nil? || value_sets[v].empty?
            end
          else
            if (index < value_sets[v].length)
              result = result.gsub(v, value_sets[v][index])
              substitution=true
            end
          end
        end

        buffer << result if self.class.find_variables(result).empty?
        index+=1
      end while substitution
    end
  end

  return buffer
end

#yesObject



378
379
# File 'lib/construqt/flavour/ciscian/ciscian.rb', line 378

def yes
end