Class: Ecu::LabelList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ecu/labels/label_list.rb,
lib/ecu/interfaces/a2l/label_list.rb,
lib/ecu/interfaces/dcm/label_list.rb,
lib/ecu/interfaces/lab/label_list.rb,
lib/ecu/interfaces/mfile/label_list.rb

Constant Summary collapse

A2LREGEXP =
%r{/begin CHARACTERISTIC\s+([\S]+)\s+"([^"]*)"}
DCM_HEADER =
"KONSERVIERUNG_FORMAT 2.0"
BLANKLINE_REGEX =
/^\s*$/
COMMENT_REGEX =
/^\*.*/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(labels = [], headers = [], subheaders = [], safe = false) ⇒ LabelList

Returns a new instance of LabelList.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ecu/labels/label_list.rb', line 39

def initialize(labels=[], headers=[], subheaders=[], safe=false)
  unless safe
    names = labels.map(&:name)
    if names.uniq.size != names.size
      duplicates = names.select { |name| names.count(name) > 1 }.uniq
      fail ArgumentError,
        "Label list must not contain duplicates: #{duplicates.join(", ")}"
    end
  end
  @labels     = labels
  @headers    = headers
  @subheaders = subheaders
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



53
54
55
# File 'lib/ecu/labels/label_list.rb', line 53

def headers
  @headers
end

#subheadersObject

Returns the value of attribute subheaders.



53
54
55
# File 'lib/ecu/labels/label_list.rb', line 53

def subheaders
  @subheaders
end

Class Method Details

.from_a2l(str) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/ecu/interfaces/a2l/label_list.rb', line 7

def self.from_a2l(str)
  str.gsub!(%r{/\*.*?\*/}, "")
  labels = str.scan(A2LREGEXP).map do |name, description|
    # Ugly hack: Create dummy Festwert until better parsing
    # is implemented
    Ecu::Festwert.new(name: name, description: description, value: 0)
  end
  new(labels)
end

.from_dcm(str) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ecu/interfaces/dcm/label_list.rb', line 21

def self.from_dcm(str)
  buffer     = DcmBuffer.new
  headers    = []
  subheaders = []
  functions  = []
  labels     = {}

  str.each_line.lazy.with_index(1).each do |line, n|
    line = normalize_whitespace(line)
    case line
    when BLANKLINE_REGEX then next
    when COMMENT_REGEX
      case buffer.header
      when :pre   then headers << line[1..].strip
      when :after then subheaders << line[1..].strip
      when :done  then # Header time over, do nothing
      end
    when DCM_HEADER                         then buffer.header_seen!
    when Functions.dcm_header               then buffer.start!(Functions, [line])
    when Festwert.dcm_header                then buffer.start!(Festwert, [line])
    when Festwerteblock.dcm_header          then buffer.start!(Festwerteblock, [line])
    when Kennlinie.dcm_header               then buffer.start!(Kennlinie, [line])
    when Gruppenkennlinie.dcm_header        then buffer.start!(Gruppenkennlinie, [line])
    when Festkennlinie.dcm_header           then buffer.start!(Festkennlinie, [line])
    when Kennfeld.dcm_header                then buffer.start!(Kennfeld, [line])
    when Gruppenkennfeld.dcm_header         then buffer.start!(Gruppenkennfeld, [line])
    when Festkennfeld.dcm_header            then buffer.start!(Festkennfeld, [line])
    when Stuetzstellenverteilung.dcm_header then buffer.start!(Stuetzstellenverteilung, [line])
    when "END"                              then
      case obj = buffer.finish!(line)
      when Label
        fail "Duplicate label #{obj.name}" unless labels[obj.name].nil?

        labels[obj.name] = obj
      when Functions
        fail "Duplicate functions definition" unless functions.empty?

        functions = obj
      else
        fail "Unknown object #{obj}"
      end
    else
      buffer.append!(line)
    end
  rescue RuntimeError => e
    raise MalformedDcmError.new(e.message, n, str), e.message
  end

  new(labels.values, headers, subheaders, true)
end

.from_file(file_path) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/ecu/labels/label_list.rb', line 21

def self.from_file(file_path)
  case File.extname(file_path)
  when ".lab" then self.from_lab(File.read_encoded(file_path))
  when ".a2l" then self.from_a2l(File.read_encoded(file_path))
  else fail "Unknown file extension: #{file_path}!"
  end
end

.from_lab(str) ⇒ Object



6
7
8
9
# File 'lib/ecu/interfaces/lab/label_list.rb', line 6

def self.from_lab(str)
  _, labels, headers, subheaders = LabParser.call(str)
  new(labels, headers, subheaders)
end

.labelclassesObject



29
30
31
32
33
34
35
36
37
# File 'lib/ecu/labels/label_list.rb', line 29

def self.labelclasses
  [
    Festwert,
    Festwerteblock,
    Kennlinie,
    Kennfeld,
    Stuetzstellenverteilung
  ]
end

.normalize_whitespace(line) ⇒ Object



72
73
74
# File 'lib/ecu/interfaces/dcm/label_list.rb', line 72

def self.normalize_whitespace(line)
  line.chomp.gsub(/[[:space:]]/, " ").rstrip
end

Instance Method Details

#+(other) ⇒ Object



114
# File 'lib/ecu/labels/label_list.rb', line 114

def +(other); merge(other) end

#<<(label) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/ecu/labels/label_list.rb', line 55

def <<(label)
  unless self.class.labelclasses.any? { |klass| label.is_a?(klass) }
    fail ArgumentError, "Can only add recognised labels"
  end
  if self.map(&:name).include? label.name
    fail ArgumentError, "Cannot add duplicate labels"
  end
  @labels << label
end

#==(other) ⇒ Object



65
66
67
68
69
# File 'lib/ecu/labels/label_list.rb', line 65

def ==(other)
  @labels.all? do |label|
    other.find { |l| l == label }
  end
end

#contains?(name) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/ecu/labels/label_list.rb', line 124

def contains?(name)
  map(&:name).include?(name)
end

#delete_if(&blk) ⇒ Object



91
92
93
# File 'lib/ecu/labels/label_list.rb', line 91

def delete_if(&blk)
  @labels.delete_if(&blk)
end

#each(&blk) ⇒ Object



71
72
73
# File 'lib/ecu/labels/label_list.rb', line 71

def each(&blk)
  @labels.each(&blk)
end

#empty?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/ecu/labels/label_list.rb', line 120

def empty?
  @labels.empty?
end

#group_by(&blk) ⇒ Object



95
96
97
# File 'lib/ecu/labels/label_list.rb', line 95

def group_by(&blk)
  @labels.group_by(&blk).map { |k, v| [k, self.class.new(v)] }.to_h
end

#inspectObject



134
# File 'lib/ecu/labels/label_list.rb', line 134

def inspect; to_s end

#map_int(&blk) ⇒ Object



75
76
77
# File 'lib/ecu/labels/label_list.rb', line 75

def map_int(&blk)
  self.class.new(@labels.map(&blk), @headers, @subheaders)
end

#merge(other) ⇒ Object



103
104
105
# File 'lib/ecu/labels/label_list.rb', line 103

def merge(other)
  LabelListComparison.new(self, other).merge(priority: :right)
end

#merge!(other) ⇒ Object



107
108
109
110
111
112
# File 'lib/ecu/labels/label_list.rb', line 107

def merge!(other)
  merged      = merge(other)
  @labels     = merged.to_a
  @headers    = merged.headers
  @subheaders = merged.subheaders
end

#reject(&blk) ⇒ Object



87
88
89
# File 'lib/ecu/labels/label_list.rb', line 87

def reject(&blk)
  self.class.new(@labels.reject(&blk), @headers, @subheaders)
end

#reject!(&blk) ⇒ Object



83
84
85
# File 'lib/ecu/labels/label_list.rb', line 83

def reject!(&blk)
  @labels.reject!(&blk)
end

#select(&blk) ⇒ Object



79
80
81
# File 'lib/ecu/labels/label_list.rb', line 79

def select(&blk)
  self.class.new(@labels.select(&blk), @headers, @subheaders)
end

#sizeObject



132
# File 'lib/ecu/labels/label_list.rb', line 132

def size; count end

#sort_by(&blk) ⇒ Object



99
100
101
# File 'lib/ecu/labels/label_list.rb', line 99

def sort_by(&blk)
  self.class.new(@labels.sort_by(&blk), @headers, @subheaders)
end

#to_aObject



116
117
118
# File 'lib/ecu/labels/label_list.rb', line 116

def to_a
  @labels
end

#to_dcmObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ecu/interfaces/dcm/label_list.rb', line 76

def to_dcm
  out = []

  unless headers.empty?
    out += headers.map { "* " + _1 }.push("")
  end

  out << DCM_HEADER << ""

  unless subheaders.empty?
    out += subheaders.map { "* " + _1 }.push("")
  end

  out += map(&:to_dcm)

  out.join("\n")
end

#to_labObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ecu/interfaces/lab/label_list.rb', line 11

def to_lab
  out = []

  out.append(*headers.map { |l| "; #{l}" }, "") unless headers.empty?

  out.append("[SETTINGS]")
  out.append("Version;V1.1")
  out.append("MultiRasterSeparator;&")
  out.append("")

  out.append(*subheaders.map { |l| "; #{l}" }, "") unless subheaders.empty?

  out.append("[Label]")
  out.append(*map(&:to_lab))

  out.join("\n")
end

#to_mfileObject



9
10
11
# File 'lib/ecu/interfaces/mfile/label_list.rb', line 9

def to_mfile
  self.map(&:to_mfile).join("\n")
end

#to_sObject



128
129
130
# File 'lib/ecu/labels/label_list.rb', line 128

def to_s
  "[#{@labels.map(&:to_s).join(", ")}]"
end