Class: Ecu::LabelList

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

Defined Under Namespace

Classes: DcmLexer, DcmParser

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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



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

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.



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

def headers
  @headers
end

#subheadersObject

Returns the value of attribute subheaders.



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

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



18
19
20
21
22
# File 'lib/ecu/interfaces/dcm/label_list.rb', line 18

def self.from_dcm(str)
  DcmParser.new(str)
    .then { _1.call }
    .then { new(*_1, true) }
end

.from_dcm_partial(str) ⇒ Object



24
25
26
27
28
29
# File 'lib/ecu/interfaces/dcm/label_list.rb', line 24

def self.from_dcm_partial(str)
  str
    .then { DCM_HEADER + "\n\n" + _1 }
    .then { from_dcm(_1) }
    .first
end

.from_file(file_path) ⇒ Object



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

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.new(str).call
  new(labels, headers, subheaders)
end

.labelclassesObject



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

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

Instance Method Details

#+(other) ⇒ Object



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

def +(other) = merge(other)

#<<(label) ⇒ Object



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

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



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

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

#contains?(name) ⇒ Boolean



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

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

#delete_if(&blk) ⇒ Object



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

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

#each(&blk) ⇒ Object



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

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

#empty?Boolean



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

def empty? = @labels.empty?

#group_by(&blk) ⇒ Object



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

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

#inspectObject



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

def inspect = to_s

#map_int(&blk) ⇒ Object



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

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

#merge(other) ⇒ Object



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

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

#merge!(other) ⇒ Object



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

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

#reject(&blk) ⇒ Object



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

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

#reject!(&blk) ⇒ Object



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

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

#select(&blk) ⇒ Object



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

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

#sizeObject



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

def size = count

#sort_by(&blk) ⇒ Object



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

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

#to_aObject



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

def to_a = @labels

#to_dcm(indented = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ecu/interfaces/dcm/label_list.rb', line 31

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_json(pretty = false) ⇒ Object



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

def to_json(pretty=false)
  self.map(&:to_h)
    .map { |e| e.reject { |_, v| v.nil? }.to_h }
    .then { pretty ? JSON.pretty_generate(_1) : JSON.generate(_1) }
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



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

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

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



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

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