Class: Lutaml::XMI::Parsers::XML

Inherits:
Object
  • Object
show all
Includes:
XMIBase
Defined in:
lib/lutaml/xmi/parsers/xml.rb

Overview

Class for parsing .xmi schema files into ::Lutaml::Uml::Document

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XMIBase

#get_guidance, included, #set_xmi_model

Instance Attribute Details

#all_packaged_elements_cacheObject (readonly)

Returns the value of attribute all_packaged_elements_cache.



17
18
19
# File 'lib/lutaml/xmi/parsers/xml.rb', line 17

def all_packaged_elements_cache
  @all_packaged_elements_cache
end

#xmi_cacheObject (readonly)

Returns the value of attribute xmi_cache.



17
18
19
# File 'lib/lutaml/xmi/parsers/xml.rb', line 17

def xmi_cache
  @xmi_cache
end

#xmi_root_modelObject (readonly)

Returns the value of attribute xmi_root_model.



17
18
19
# File 'lib/lutaml/xmi/parsers/xml.rb', line 17

def xmi_root_model
  @xmi_root_model
end

Class Method Details

.deep_clone(obj) ⇒ Object



72
73
74
75
76
# File 'lib/lutaml/xmi/parsers/xml.rb', line 72

def deep_clone(obj)
  # TODO: we need this if xmi_model is being modified in serialize_generalization_by_name
  #Marshal.load(Marshal.dump(obj)) if obj != nil
  obj
end

.parse(xml, _options = {}) ⇒ Lutaml::Uml::Document

Parameters:

  • xml (String)

    path to xml

  • options (Hash)

    options for parsing

Returns:



25
26
27
28
# File 'lib/lutaml/xmi/parsers/xml.rb', line 25

def parse(xml, _options = {})
  xmi_model = get_xmi_model(xml)
  new.parse(xmi_model)
end

.serialize_generalization_by_name(xml, name, guidance = nil) ⇒ Hash

Parameters:

  • xml (String)

    path to xml

  • name (String)
  • guidance (String) (defaults to: nil)

Returns:

  • (Hash)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lutaml/xmi/parsers/xml.rb', line 49

def serialize_generalization_by_name(xml, name, guidance = nil)
  # puts "#{xml}, #{name}, #{guidance}"

  # Load from cache or file
  xml_cache_key = (Digest::SHA256.file xml).hexdigest
  xmi_model = deep_clone(@xmi_root_model_cache_static[xml_cache_key])
  xmi_model_to_cache = nil
  if xmi_model == nil
    xmi_model = get_xmi_model(xml)
    xmi_model_to_cache = deep_clone(xmi_model)
  end
  xmi_cache = @xmi_cache_static[xml_cache_key]

  instance = new
  ret_val = instance.serialize_generalization_by_name(xmi_model, name, guidance, xmi_cache)

  # Put to cache
  @xmi_cache_static[xml_cache_key] = instance.xmi_cache if guidance == nil
  @xmi_root_model_cache_static[xml_cache_key] = xmi_model_to_cache if xmi_model_to_cache

  ret_val
end

.serialize_xmi(xml, with_gen: false) ⇒ Hash

Parameters:

  • xml (String)

    path to xml

  • with_gen (Boolean) (defaults to: false)

Returns:

  • (Hash)


33
34
35
36
# File 'lib/lutaml/xmi/parsers/xml.rb', line 33

def serialize_xmi(xml, with_gen: false)
  xmi_model = get_xmi_model(xml)
  new.serialize_xmi(xmi_model, with_gen: with_gen)
end

.serialize_xmi_to_liquid(xml, guidance = nil) ⇒ Liquid::Drop

Parameters:

  • xml (String)

    path to xml

Returns:

  • (Liquid::Drop)


40
41
42
43
# File 'lib/lutaml/xmi/parsers/xml.rb', line 40

def serialize_xmi_to_liquid(xml, guidance = nil)
  xmi_model = get_xmi_model(xml)
  new.serialize_xmi_to_liquid(xmi_model, guidance)
end

Instance Method Details

#parse(xmi_model) ⇒ Lutaml::Uml::Document

Parameters:

  • xmi_model (Lutaml::Model::Serializable)

Returns:



81
82
83
84
85
86
# File 'lib/lutaml/xmi/parsers/xml.rb', line 81

def parse(xmi_model)
  set_xmi_model(xmi_model)
  serialized_hash = serialize_xmi(xmi_model)

  ::Lutaml::Uml::Document.new(serialized_hash)
end

#serialize_generalization_by_name(xmi_model, name, guidance_yaml = nil, xmi_cache = nil) ⇒ Hash

Parameters:

  • xmi_model (Lutaml::Model::Serializable)
  • name (String)
  • guidance_yaml (String) (defaults to: nil)

Returns:

  • (Hash)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/lutaml/xmi/parsers/xml.rb', line 121

def serialize_generalization_by_name(xmi_model, name, # rubocop:disable Metrics/MethodLength
                                     guidance_yaml = nil, xmi_cache = nil)
  set_xmi_model(xmi_model, xmi_cache)
  klass = find_klass_packaged_element(name)
  guidance = get_guidance(guidance_yaml)
  options = {
    xmi_root_model: @xmi_root_model,
    xmi_cache: @xmi_cache,
    with_gen: true,
    with_absolute_path: true,
  }
  ::Lutaml::XMI::KlassDrop.new(
    klass,
    guidance,
    options,
  )
end

#serialize_xmi(xmi_model, with_gen: false, with_absolute_path: false) ⇒ Object

return [Hash]

Parameters:

  • xmi_model (Lutaml::Model::Serializable)
  • with_gen: (Boolean) (defaults to: false)
  • with_absolute_path: (Boolean) (defaults to: false)


92
93
94
95
96
97
98
99
# File 'lib/lutaml/xmi/parsers/xml.rb', line 92

def serialize_xmi(xmi_model, with_gen: false, with_absolute_path: false)
  set_xmi_model(xmi_model)
  serialize_to_hash(
    xmi_model,
    with_gen: with_gen,
    with_absolute_path: with_absolute_path,
  )
end

#serialize_xmi_to_liquid(xmi_model, guidance_yaml = nil) ⇒ Object

return [Liquid::Drop]

Parameters:

  • xmi_model (Lutaml::Model::Serializable)
  • guidance_yaml (String) (defaults to: nil)


104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lutaml/xmi/parsers/xml.rb', line 104

def serialize_xmi_to_liquid(xmi_model, guidance_yaml = nil)
  set_xmi_model(xmi_model)
  model = xmi_model.model
  options = {
    xmi_root_model: @xmi_root_model,
    xmi_cache: @xmi_cache,
    with_gen: true,
    with_absolute_path: true,
  }
  guidance = get_guidance(guidance_yaml)
  ::Lutaml::XMI::RootDrop.new(model, guidance, options)
end