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



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

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.



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

def headers
  @headers
end

#subheadersObject

Returns the value of attribute subheaders.



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

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
    in BLANKLINE_REGEX then next
    in COMMENT_REGEX
      case buffer.header
      in :pre   then headers << line[1..].strip
      in :after then subheaders << line[1..].strip
      in :done  then # Header time over, do nothing
      end
    in DCM_HEADER                            then buffer.header_seen!
    in ^(Functions.dcm_header)               then buffer.start!(Functions, [line])
    in ^(Festwert.dcm_header)                then buffer.start!(Festwert, [line])
    in ^(Festwerteblock.dcm_header)          then buffer.start!(Festwerteblock, [line])
    in ^(Kennlinie.dcm_header)               then buffer.start!(Kennlinie, [line])
    in ^(Gruppenkennlinie.dcm_header)        then buffer.start!(Gruppenkennlinie, [line])
    in ^(Festkennlinie.dcm_header)           then buffer.start!(Festkennlinie, [line])
    in ^(Kennfeld.dcm_header)                then buffer.start!(Kennfeld, [line])
    in ^(Gruppenkennfeld.dcm_header)         then buffer.start!(Gruppenkennfeld, [line])
    in ^(Festkennfeld.dcm_header)            then buffer.start!(Festkennfeld, [line])
    in ^(Stuetzstellenverteilung.dcm_header) then buffer.start!(Stuetzstellenverteilung, [line])
    in "END"
      case obj = buffer.finish!(line)
      in Label
        fail "Duplicate label #{obj.name}" unless labels[obj.name].nil?

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

        functions = obj
      else
        fail "Unknown object #{obj}"
      end
    else
      buffer.append!(line)
    end
  rescue StandardError => 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
28
# 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))
  when ".dcm" then self.from_dcm(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



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

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



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

def +(other) = merge(other)

#<<(label) ⇒ Object



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

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



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

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

#contains?(name) ⇒ Boolean



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

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

#delete_if(&blk) ⇒ Object



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

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

#each(&blk) ⇒ Object



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

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

#empty?Boolean



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

def empty? = @labels.empty?

#group_by(&blk) ⇒ Object



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

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

#inspectObject



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

def inspect = to_s

#map_int(&blk) ⇒ Object



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

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

#merge(other) ⇒ Object



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

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

#merge!(other) ⇒ Object



112
113
114
115
116
117
# File 'lib/ecu/labels/label_list.rb', line 112

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

#reject(&blk) ⇒ Object



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

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

#reject!(&blk) ⇒ Object



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

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

#select(&blk) ⇒ Object



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

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

#sizeObject



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

def size = count

#sort_by(&blk) ⇒ Object



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

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

#to_aObject



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

def to_a = @labels

#to_dcm(indented = false) ⇒ Object



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(indented=false)
  out = []

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

  out << DCM_HEADER << ""

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

  out += map { _1.to_dcm(indented) }

  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



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

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

#with(headers: [], subheaders: []) ⇒ Object



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

def with(headers: [], subheaders: [])
  self.class.new(@labels, headers, subheaders)
end