Class: Lutaml::UmlRepository::Queries::AssociationQuery

Inherits:
BaseQuery
  • Object
show all
Defined in:
lib/lutaml/uml_repository/queries/association_query.rb

Overview

Query service for association operations.

Provides methods to find associations related to classes. Associations can be owned by classes or defined at the document level.

Examples:

Finding all associations for a class

query = AssociationQuery.new(document, indexes)
associations = query.find_for_class("ModelRoot::Building")

Finding owned associations only

associations = query.find_for_class(klass, owned_only: true)

Finding associations in a specific direction

associations = query.find_for_class(klass, direction: :source)

Instance Method Summary collapse

Methods inherited from BaseQuery

#initialize

Constructor Details

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

Instance Method Details

#allArray<Lutaml::Uml::Association>

Retrieve all associations in the document

Returns:



115
116
117
# File 'lib/lutaml/uml_repository/queries/association_query.rb', line 115

def all
  resolve_all_associations
end

#find_aggregationsObject



92
93
94
# File 'lib/lutaml/uml_repository/queries/association_query.rb', line 92

def find_aggregations
  find_by_type("aggregation")
end

#find_between_classes(owner_end_xmi_id, member_end_xmi_id) ⇒ Array<Lutaml::Uml::Association>

Find associations between two classes

Parameters:

  • owner_end_xmi_id (String)

    XMI ID of the owner end class

  • member_end_xmi_id (String)

    XMI ID of the member end class

Returns:



105
106
107
108
109
110
# File 'lib/lutaml/uml_repository/queries/association_query.rb', line 105

def find_between_classes(owner_end_xmi_id, member_end_xmi_id)
  resolve_all_associations.select do |assoc|
    assoc.owner_end_xmi_id == owner_end_xmi_id &&
      assoc.member_end_xmi_id == member_end_xmi_id
  end
end

#find_by_type(association_type) ⇒ Array<Lutaml::Uml::Association>

Find associations by their type.

Parameters:

  • association_type (String)

    The type of association (e.g., “aggregation”, “composition”, “association”)

Returns:



86
87
88
89
90
# File 'lib/lutaml/uml_repository/queries/association_query.rb', line 86

def find_by_type(association_type)
  resolve_all_associations.select do |assoc|
    assoc.member_end_type == association_type
  end
end

#find_compositionsObject



96
97
98
# File 'lib/lutaml/uml_repository/queries/association_query.rb', line 96

def find_compositions
  find_by_type("composition")
end

#find_for_class(class_or_qname, options = {}) ⇒ Array<Lutaml::Uml::Association>

Find associations for a specific class.

Examples:

# Get all associations
all = query.find_for_class("ModelRoot::Building")

# Get only owned associations
owned = query.find_for_class(
"ModelRoot::Building", owned_only: true)

# Get associations where class is the source
sources = query.find_for_class(
"ModelRoot::Building", direction: :source)

Parameters:

  • class_or_qname (Lutaml::Uml::Class, String)

    The class object or qualified name string

  • options (Hash) (defaults to: {})

    Query options

Options Hash (options):

  • :owned_only (Boolean)

    Return only associations owned by the class (default: false)

  • :navigable_only (Boolean)

    Return only navigable associations (default: false)

  • :direction (Symbol)

    Filter by direction - :source (class is owner_end), :target (class is member_end), or :both (default: :both)

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lutaml/uml_repository/queries/association_query.rb', line 48

def find_for_class(class_or_qname, options = {}) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  owned_only = options[:owned_only] || false
  navigable_only = options[:navigable_only] || false
  direction = options[:direction] || :both

  klass = resolve_class(class_or_qname)
  return [] unless klass

  class_name = klass.name
  results = []

  # Get owned associations from the class itself
  if klass.respond_to?(:associations) && klass.associations
    results.concat(klass.associations)
  end

  # Get associations from document level unless owned_only
  if !owned_only &&
      document.respond_to?(:associations) && document.associations
    document_associations = document.associations.select do |assoc|
      match_association?(assoc, class_name, direction)
    end
    results.concat(document_associations)
  end

  # Filter navigable if requested
  if navigable_only
    results.select! { |assoc| navigable?(assoc) }
  end

  results.uniq
end