Class: Lutaml::UmlRepository::LazyRepository

Inherits:
Repository
  • Object
show all
Defined in:
lib/lutaml/uml_repository/lazy_repository.rb

Overview

LazyRepository provides lazy loading optimization for very large UML models

For extremely large models (1000+ classes), LazyRepository optimizes memory usage and initial load time by building indexes on-demand rather than upfront. Indexes are built only when first accessed, reducing the initial load time significantly.

Examples:

Using lazy loading from XMI

repo = Lutaml::Xmi::Repository.from_xmi_lazy('large-model.xmi')
# Only metadata loaded at this point

klass = repo.find_class("ModelRoot::MyClass")
# Now qualified_names index is built

Building all indexes manually

repo = Lutaml::Xmi::Repository.from_xmi_lazy('model.xmi')
repo.build_all_indexes
# All indexes are now available

Instance Attribute Summary

Attributes inherited from Repository

#document, #indexes, #metadata

Instance Method Summary collapse

Methods inherited from Repository

#all_attributes, #all_classes, #all_diagrams, #associations_index, #associations_of, #classes_in_package, #classes_index, #diagrams_index, #export_to_package, #find_attribute, #find_diagram, from_file, from_file_cached, from_file_lazy, from_package, from_package_lazy, from_xmi, from_xmi_lazy, #list_packages, #marshal_dump, #marshal_load, #package_tree, #packages_index, #qualified_name_for, #query, #query!, #search, #statistics, #validate

Methods included from Repository::Deprecated

#export, #find_associations, #find_children, #find_diagrams, #search_classes

Constructor Details

#initialize(document:, indexes: nil, lazy: true) ⇒ LazyRepository

Initialize a new LazyRepository with lazy index building.

Parameters:

  • document (Lutaml::Uml::Document)

    The UML document to wrap

  • indexes (Hash, nil) (defaults to: nil)

    Pre-built indexes, or nil to build lazily

  • lazy (Boolean) (defaults to: true)

    Whether to enable lazy loading (default: true)



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
# File 'lib/lutaml/uml_repository/lazy_repository.rb', line 31

def initialize(document:, indexes: nil, lazy: true) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  @document = document
  @indexes = indexes || {}
  @lazy_mode = lazy
  @index_builders_pending = Set.new(%i[
                                      package_paths
                                      qualified_names
                                      stereotypes
                                      inheritance_graph
                                      diagram_index
                                    ])

  # Initialize runtime query services (not serialized to LUR)
  # These are lightweight wrappers that operate on @document and @indexes
  @package_query = Queries::PackageQuery.new(@document, @indexes)
  @class_query = Queries::ClassQuery.new(@document, @indexes)
  @inheritance_query = Queries::InheritanceQuery.new(@document, @indexes)
  @association_query = Queries::AssociationQuery.new(@document, @indexes)
  @diagram_query = Queries::DiagramQuery.new(@document, @indexes)
  @search_query = Queries::SearchQuery.new(@document, @indexes)

  # Initialize statistics calculator (lazily computed)
  @statistics_calculator = StatisticsCalculator.new(@document, @indexes)

  # Initialize error handler for helpful error messages
  @error_handler = ErrorHandler.new(self)

  # Don't freeze - we build indexes on demand
end

Instance Method Details

#ancestors_of(class_or_qname) ⇒ Array

Get all ancestor classes up to the root.

Ensures the qualified_names and inheritance_graph indexes are built.

Parameters:

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

    The class object or qualified name string

Returns:

  • (Array)

    Array of ancestor class objects



130
131
132
133
134
# File 'lib/lutaml/uml_repository/lazy_repository.rb', line 130

def ancestors_of(class_or_qname)
  ensure_index(:qualified_names)
  ensure_index(:inheritance_graph)
  super
end

#build_all_indexesLazyRepository

Manually build all remaining indexes.

This method can be called to force building all indexes at once, rather than waiting for them to be built on-demand.

Examples:

repo = Repository.from_xmi_lazy('model.xmi')
repo.build_all_indexes
# All indexes are now built

Returns:



170
171
172
173
# File 'lib/lutaml/uml_repository/lazy_repository.rb', line 170

def build_all_indexes
  @index_builders_pending.each { |index_name| ensure_index(index_name) }
  self
end

#descendants_of(class_or_qname, max_depth: nil) ⇒ Array

Get all descendant classes.

Ensures the inheritance_graph index is built before querying.

Parameters:

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

    The class object or qualified name string

  • max_depth (Integer, nil) (defaults to: nil)

    Maximum depth to traverse

Returns:

  • (Array)

    Array of descendant class objects



144
145
146
147
# File 'lib/lutaml/uml_repository/lazy_repository.rb', line 144

def descendants_of(class_or_qname, max_depth: nil)
  ensure_index(:inheritance_graph)
  super
end

#diagrams_in_package(package_path) ⇒ Array<Lutaml::Uml::Diagram>

Get diagrams in a specific package.

Ensures the diagram_index is built before querying.

Parameters:

  • package_path (String)

    The package path or package ID

Returns:



155
156
157
158
# File 'lib/lutaml/uml_repository/lazy_repository.rb', line 155

def diagrams_in_package(package_path)
  ensure_index(:diagram_index)
  super
end

#find_class(qualified_name, raise_on_error: false) ⇒ Lutaml::Uml::Class, ...

Find a class by its qualified name.

Ensures the qualified_names index is built before querying.

Lutaml::Uml::Enum, nil]

Parameters:

  • qualified_name (String)

    The qualified name

  • raise_on_error (Boolean) (defaults to: false)

    Whether to raise an error if not found

Returns:



69
70
71
72
# File 'lib/lutaml/uml_repository/lazy_repository.rb', line 69

def find_class(qualified_name, raise_on_error: false)
  ensure_index(:qualified_names)
  super
end

#find_classes_by_stereotype(stereotype) ⇒ Array

Find all classes with a specific stereotype.

Ensures the stereotypes index is built before querying.

Parameters:

  • stereotype (String)

    The stereotype to search for

Returns:

  • (Array)

    Array of class objects with the stereotype



92
93
94
95
# File 'lib/lutaml/uml_repository/lazy_repository.rb', line 92

def find_classes_by_stereotype(stereotype)
  ensure_index(:stereotypes)
  super
end

#find_package(path, raise_on_error: false) ⇒ Lutaml::Uml::Package, ...

Find a package by its path.

Ensures the package_paths index is built before querying.

Parameters:

  • path (String)

    The package path

  • raise_on_error (Boolean) (defaults to: false)

    Whether to raise an error if not found

Returns:



81
82
83
84
# File 'lib/lutaml/uml_repository/lazy_repository.rb', line 81

def find_package(path, raise_on_error: false)
  ensure_index(:package_paths)
  super
end

#index_built?(index_name) ⇒ Boolean

Check if an index is built.

Examples:

repo.index_built?(:qualified_names)  # => false
repo.find_class("ModelRoot::MyClass")
repo.index_built?(:qualified_names)  # => true

Parameters:

  • index_name (Symbol)

    The name of the index to check

Returns:

  • (Boolean)

    True if the index is built, false otherwise



183
184
185
# File 'lib/lutaml/uml_repository/lazy_repository.rb', line 183

def index_built?(index_name)
  @indexes.key?(index_name) && !@indexes[index_name].nil?
end

#pending_indexesArray<Symbol>

Get list of pending indexes.

Returns the names of indexes that have not yet been built.

Examples:

repo.pending_indexes  # => [:package_paths, :qualified_names, ...]

Returns:

  • (Array<Symbol>)

    Array of pending index names



194
195
196
# File 'lib/lutaml/uml_repository/lazy_repository.rb', line 194

def pending_indexes
  @index_builders_pending.to_a
end

#subtypes_of(class_or_qname, recursive: false) ⇒ Array

Get direct child classes (subtypes).

Ensures the inheritance_graph index is built before querying.

Parameters:

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

    The class object or qualified name string

  • recursive (Boolean) (defaults to: false)

    Whether to include all descendants

Returns:

  • (Array)

    Array of child class objects



118
119
120
121
# File 'lib/lutaml/uml_repository/lazy_repository.rb', line 118

def subtypes_of(class_or_qname, recursive: false)
  ensure_index(:inheritance_graph)
  super
end

#supertype_of(class_or_qname) ⇒ Lutaml::Uml::Class?

Get the direct parent class (supertype).

Ensures the qualified_names and inheritance_graph indexes are built.

Parameters:

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

    The class object or qualified name string

Returns:



104
105
106
107
108
# File 'lib/lutaml/uml_repository/lazy_repository.rb', line 104

def supertype_of(class_or_qname)
  ensure_index(:qualified_names)
  ensure_index(:inheritance_graph)
  super
end