Class: Archimate::DataModel::Model

Inherits:
Object
  • Object
show all
Includes:
Comparison
Defined in:
lib/archimate/data_model/model.rb

Overview

This is the root model type.

It is a container for the elements, relationships, diagrams and organizations of the model.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Comparison

#==, #[], #dig, #each, #hash, included, #inspect, #pretty_print, #to_h

Constructor Details

#initialize(opts = {}) ⇒ Model

Constructor



77
78
79
80
# File 'lib/archimate/data_model/model.rb', line 77

def initialize(opts = {})
  super
  rebuild_index
end

Instance Attribute Details

#archimate_versionSymbol (readonly)

Returns one of [Archimate::ARCHIMATE_VERSIONS], default :archimate_3_0.

Returns:

  • (Symbol)

    one of [Archimate::ARCHIMATE_VERSIONS], default :archimate_3_0



62
# File 'lib/archimate/data_model/model.rb', line 62

model_attr :archimate_version, default: :archimate_3_0

#diagramsArray<Diagram> (readonly)

Returns:



47
# File 'lib/archimate/data_model/model.rb', line 47

model_attr :diagrams, default: [], referenceable_list: true

#documentationPreservedLangString, NilClass (readonly)

Returns model documentation.

Returns:



23
# File 'lib/archimate/data_model/model.rb', line 23

model_attr :documentation, default: nil

#elementsArray<Element> (readonly)

Returns:



32
# File 'lib/archimate/data_model/model.rb', line 32

model_attr :elements, default: [], referenceable_list: true

#file_formatSymbol, NilClass (readonly)

Returns supported Archimate format [Archimate::SUPPORTED_FORMATS] or nil.

Returns:

  • (Symbol, NilClass)

    supported Archimate format [Archimate::SUPPORTED_FORMATS] or nil

See Also:



59
# File 'lib/archimate/data_model/model.rb', line 59

model_attr :file_format, default: nil

#filenameString (readonly)

Following attributes are to hold info on where the model came from

Returns:

  • (String)


55
# File 'lib/archimate/data_model/model.rb', line 55

model_attr :filename, default: nil

#idString (readonly)

Returns unique identifier of this model.

Returns:

  • (String)

    unique identifier of this model



16
# File 'lib/archimate/data_model/model.rb', line 16

model_attr :id

#metadataMetadata, NilClass (readonly)

Returns model metadata.

Returns:

  • (Metadata, NilClass)

    model metadata



29
# File 'lib/archimate/data_model/model.rb', line 29

model_attr :metadata, default: nil

#nameLangString (readonly)

Returns name of the model.

Returns:



19
# File 'lib/archimate/data_model/model.rb', line 19

model_attr :name

#namespacesHash (readonly)

Returns:

  • (Hash)


66
# File 'lib/archimate/data_model/model.rb', line 66

model_attr :namespaces, default: {}

#organizationsArray<Organization> (readonly)

Returns:



38
# File 'lib/archimate/data_model/model.rb', line 38

model_attr :organizations, default: [], referenceable_list: true

#propertiesArray<Property> (readonly)

Returns model properties.

Returns:

  • (Array<Property>)

    model properties



26
# File 'lib/archimate/data_model/model.rb', line 26

model_attr :properties, default: []

#property_definitionsArray<PropertyDefinition> (readonly)

Returns:



41
# File 'lib/archimate/data_model/model.rb', line 41

model_attr :property_definitions, default: [], referenceable_list: true

#relationshipsArray<Relationship> (readonly)

Returns:



35
# File 'lib/archimate/data_model/model.rb', line 35

model_attr :relationships, default: [], referenceable_list: true

#schema_locationsArray<String> (readonly)

Returns:

  • (Array<String>)


69
# File 'lib/archimate/data_model/model.rb', line 69

model_attr :schema_locations, default: []

#versionString, NilClass (readonly)

Returns:

  • (String, NilClass)


44
# File 'lib/archimate/data_model/model.rb', line 44

model_attr :version, default: nil

#viewpointsArray<Viewpoint> (readonly)

Returns:



50
# File 'lib/archimate/data_model/model.rb', line 50

model_attr :viewpoints, default: [], referenceable_list: true

Instance Method Details

#entitiesObject

Called only by [Lint::DuplicateEntities]



88
89
90
# File 'lib/archimate/data_model/model.rb', line 88

def entities
  @index_hash.values
end

#find_by_class(klass) ⇒ Object



180
181
182
# File 'lib/archimate/data_model/model.rb', line 180

def find_by_class(klass)
  @index_hash.values.select { |item| item.is_a?(klass) }
end

#lookup(id) ⇒ Object



82
83
84
85
# File 'lib/archimate/data_model/model.rb', line 82

def lookup(id)
  rebuild_index(id) unless @index_hash.include?(id)
  @index_hash[id]
end

#make_unique_idObject



152
153
154
155
156
157
158
# File 'lib/archimate/data_model/model.rb', line 152

def make_unique_id
  @random ||= Random.new
  begin
    unique_id = format("%08x", @random.rand(0xffffffff))
  end until !@index_hash.key?(unique_id)
  unique_id
end

#merge_entities(master_entity, copies) ⇒ Object

TODO:

this should move into either Comparison or a Mergeable class

Steps to merge merge attributes of each copy into master_entity update references of each copy to reference master_entity instead (where it makes sense) remove reference of each copy from its references



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/archimate/data_model/model.rb', line 126

def merge_entities(master_entity, copies)
  copies.delete(master_entity)
  copies.each do |copy|
    master_entity.merge(copy)
    copy.replace_with(master_entity)
    # if !copy.references.empty?
    #   puts "#{copy.class} still referenced by #{copy.references.map { |ref| ref.class.name }.join(", ")}"
    # end
    deregister(copy)
  end
end

#organizeObject

Note:

this is only called by [Diff::Merge]

Iterate through the model and ensure that elements, relationships, and diagrams are all present in the model’s organizations. If an item is missing then move it into the default top-level element for the item type.



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/archimate/data_model/model.rb', line 108

def organize
  []
    .concat(elements)
    .concat(relationships)
    .concat(diagrams).each do |item|
      if find_in_organizations(item).nil?
        default_organization = default_organization_for(item)
        default_organization.items.push(item) unless default_organization.items.include?(item)
      end
    end
  self
end

#other_attributesArray<AnyAttribute>

Returns:



74
# File 'lib/archimate/data_model/model.rb', line 74

model_attr :other_attributes, default: []

#other_elementsArray<AnyElement>

Returns:



72
# File 'lib/archimate/data_model/model.rb', line 72

model_attr :other_elements, default: []

#rebuild_index(missing_id = :model_creation_event) ⇒ Object

Called only by [Diff::Merge]



93
94
95
96
97
# File 'lib/archimate/data_model/model.rb', line 93

def rebuild_index(missing_id = :model_creation_event)
  return self unless missing_id
  @index_hash = build_index
  self
end

#remove_reference(item) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/archimate/data_model/model.rb', line 160

def remove_reference(item)
  case item
  when Element
    elements.delete(item)
  when Relationship
    relationships.delete(item)
  when Diagram
    diagrams.delete(item)
  else
    raise "Unhandled remove reference for type #{item.class}"
  end
  organizations.each { |org| org.remove_reference(item) }
end

#replace_item_with(item, replacement) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/archimate/data_model/model.rb', line 138

def replace_item_with(item, replacement)
  case item
  when Organization
    organizations.delete(item)
    organizations << replacement
  when Element
    elements.delete(item)
    elements << replacement
  when Relationship
    relationships.delete(item)
    relationships << replacement
  end
end

#to_sObject



99
100
101
# File 'lib/archimate/data_model/model.rb', line 99

def to_s
  "#{Archimate::Color.data_model('Model')}<#{id}>[#{Archimate::Color.color(name, %i[white underline])}]"
end