Class: Lutaml::UmlRepository::Queries::SearchQuery

Inherits:
BaseQuery
  • Object
show all
Includes:
Lutaml::Uml::ModelHelpers
Defined in:
lib/lutaml/uml_repository/queries/search_query.rb

Constant Summary collapse

SEARCHERS =
{
  class: :search_classes,
  attribute: :search_attributes,
  association: :search_associations,
}.freeze

Instance Method Summary collapse

Methods included from Lutaml::Uml::ModelHelpers

#class_type_for, #extract_package_path, #format_cardinality, #format_definition, #normalize_stereotypes, #package_path_for, #parse_cardinality, #qualified_name_for

Methods inherited from BaseQuery

#initialize

Constructor Details

This class inherits a constructor from Lutaml::UmlRepository::Queries::BaseQuery

Instance Method Details

#full_text_search(query_string, fields: [:name], case_sensitive: false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lutaml/uml_repository/queries/search_query.rb', line 35

def full_text_search(query_string, fields: [:name], case_sensitive: false)
  results = { classes: [], packages: [], total: 0 }
  return results if query_string.nil? || query_string.empty?

  results[:classes] = search_classes(
    query_string, fields: fields, case_sensitive: case_sensitive
  )
  results[:packages] = search_packages(
    query_string, case_sensitive: case_sensitive
  )
  results[:total] = results[:classes].size + results[:packages].size
  results
end

#get_all_associationsObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lutaml/uml_repository/queries/search_query.rb', line 127

def get_all_associations
  all_associations = []

  if document.is_a?(Lutaml::Uml::Document) && document.associations
    all_associations << document.associations
  end

  indexes[:qualified_names].each_value do |entity|
    next unless classifiable_with_associations?(entity)

    all_associations << entity.associations
  end

  all_associations.flatten.uniq
end

#search(query_string, types: %i[class attribute association],, fields: [:name], case_sensitive: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lutaml/uml_repository/queries/search_query.rb', line 19

def search(query_string, types: %i[class attribute association],
                  fields: [:name], case_sensitive: false)
  return empty_result if query_string.nil? || query_string.empty?

  results = { classes: [], attributes: [], associations: [] }
  types.each do |type|
    key = :"#{type}s"
    results[key] = public_send(SEARCHERS[type],
                               query_string,
                               fields: fields,
                               case_sensitive: case_sensitive)
  end
  results[:total] = results.values_at(:classes, :attributes, :associations).sum(&:size)
  results
end

#search_associations(query, fields: %i[ name owner_end member_end owner_end_attribute_name member_end_attribute_name documentation ],, case_sensitive: false) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/lutaml/uml_repository/queries/search_query.rb', line 83

def search_associations(query,
                        fields: %i[
                          name owner_end member_end
                          owner_end_attribute_name
                          member_end_attribute_name documentation
                        ],
                        case_sensitive: false)
  pattern = pattern_from(query, case_sensitive)

  get_all_associations.filter_map do |assoc|
    match_field = first_matching_field(assoc, fields, pattern)
    next unless match_field

    build_search_result(
      assoc, :association,
      assoc.name || "(unnamed)", match_field,
      { "source" => assoc.owner_end, "target" => assoc.member_end }
    )
  end.uniq
end

#search_attributes(query, fields: [:name], case_sensitive: false) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lutaml/uml_repository/queries/search_query.rb', line 63

def search_attributes(query, fields: [:name],
                             case_sensitive: false)
  pattern = pattern_from(query, case_sensitive)

  indexes[:qualified_names].filter_map do |class_qname, entity|
    next unless entity.is_a?(Lutaml::Uml::Classifier) && entity.attributes

    attr_match = find_matching_attribute(entity, fields, pattern)
    next unless attr_match

    attr, match_field = attr_match
    build_search_result(
      attr, :attribute,
      "#{class_qname}::#{attr.name}", match_field,
      { "class_name" => entity.name, "class_qname" => class_qname },
      package_path: extract_package_path(class_qname)
    )
  end.uniq
end

#search_by_stereotype(query, case_sensitive: false) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/lutaml/uml_repository/queries/search_query.rb', line 104

def search_by_stereotype(query, case_sensitive: false)
  pattern = pattern_from(query, case_sensitive)

  find_entities_by_stereotype_pattern(pattern).map do |entity|
    build_search_result(
      entity,
      entity.class.name.split("::").last.downcase.to_sym,
      "", :stereotype
    )
  end
end

#search_classes(query, fields: %i[name documentation],, case_sensitive: false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lutaml/uml_repository/queries/search_query.rb', line 49

def search_classes(query, fields: %i[name documentation],
                        case_sensitive: false)
  pattern = pattern_from(query, case_sensitive)

  indexes[:qualified_names].filter_map do |qname, entity|
    next unless entity.is_a?(Lutaml::Uml::Class)

    match_field = first_matching_field(entity, fields, pattern)
    next unless match_field

    build_search_result(entity, :class, qname, match_field)
  end.uniq
end

#search_packages(query, case_sensitive: false) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/lutaml/uml_repository/queries/search_query.rb', line 116

def search_packages(query, case_sensitive: false)
  pattern = pattern_from(query, case_sensitive)

  indexes[:package_paths].filter_map do |path_string, package|
    next unless path_string.to_s.match?(pattern)

    build_search_result(package, :package, path_string, :package_path,
                        package_path: path_string)
  end
end