Module: Babeltrace2Gen::BTMatchMembers

Included in:
BTEnvironmentClass, Babeltrace2Gen::BTFieldClass::Structure
Defined in:
lib/metababel/bt2_matching_utils.rb

Instance Method Summary collapse

Instance Method Details

#match?(match_obj) ⇒ Boolean

Returns:

  • (Boolean)


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/metababel/bt2_matching_utils.rb', line 37

def match?(match_obj)
  # Object here have only one symbol, who cannot be nil
  attr_sym = self.class::BT_MATCH_ATTRS[0]

  self_members = send(attr_sym)
  match_members = match_obj.send(attr_sym)

  # Do the product of menbers to get the matching one
  # We keep only the matching one.
  args_matched = match_members.filter_map do |match_obj|
    # Find all machings members
    matches = self_members.filter { |member| member.match?(match_obj) }
    # Check that one match_obj only match zero or one member.
    unless matches.length < 2
      raise "Match expression '#{match_obj.name}' must match only one member, '#{matches.length}' matched #{matches.map(&:name)}."
    end

    # If not argument matched, then nil; otherwise, return the matched member.
    matches.pop.bt_get_variable unless matches.empty?
  end

  # We didn't match anything
  if args_matched.empty? || args_matched.uniq.length != match_members.length
    false
  # Same arguments in the model have been matched twice
  elsif args_matched.uniq.length != args_matched.length
    raise "Members '#{args_matched.uniq.map(&:name)}' matched multiple times in '#{match_members.map(&:name)}'. "
  # Not all match mernbers found a matchings
  elsif args_matched.uniq.length != match_members.length
    false
  else
    args_matched
  end
end