Modelο
The Model class is the root container for an Archimate architecture. It
holds all Element, Relationship, and View, Node & Connection objects and
provides the read / write entry points.
Module contentsο
Model module - extracted from the legacy monolith.
- class pyArchimate.model.Model(name=None, uuid=None, desc=None)[source]ο
Bases:
objectClass to create ArchiMate v3.x compliant models with full hierarchy and styling support.
Supports element grouping (parent-child relationships), visual styling (colors, transparency), junction types (AND/OR/XOR), and advanced hierarchy queries with round-trip fidelity.
Element Hierarchy (P3): - Use add_child(parent_uuid, child_uuid) to create parent-child relationships - Supports unlimited nesting (default max depth 5, configurable) - Automatic cycle detection prevents invalid hierarchies - When parent is deleted, children are orphaned (not deleted) - Query methods: get_parent(), get_children(), get_ancestors(), get_descendants()
Visual Styling (P3): - Elements support custom fill colors, line colors, line width, and transparency - Colors can be hex (#RRGGBB) or named colors; all normalized to hex for export - All visual properties preserved during XML round-trip export/import cycles - Use element.set_fill_color(), set_line_color(), etc. for styling
Advanced Queries (P3): - get_siblings(elem_uuid): Find all elements with same parent - find_by_hierarchy_path(path): Query by path like β/Parent/Childβ with wildcard support - Path examples: β/Rootβ, β/Parent/Child/β, β/A//B/Leafβ - Performance: cycle detection <1ms, queries <10ms on 1000+ element models
Junction Semantics (P3): - Junction elements support type semantics: βandβ, βorβ, βxorβ - Types are validated and preserved across XML export/import cycles - Use element.set_junction_type(type_str) to set semantics
Note: Perspectives are not handled in the current version of this library
This class defines methods and properties to create Elements, Relationships, Diagrams (Views) with Nodes and Connections with visual layout.
It also reads, writes or merges XML files using the ArchiMate Open Exchange File format.
- Parameters:
name (str) β Model name
uuid (str) β Model Identifier
desc (str) β Model documentation
- Returns:
Model object
- Return type:
Example:
from pyArchimate import ArchiType from pyArchimate.model import Model m = Model('Enterprise Architecture') # Create elements process = m.add(ArchiType.BusinessProcess, 'Order Management') func1 = m.add(ArchiType.BusinessFunction, 'Order Entry') func2 = m.add(ArchiType.BusinessFunction, 'Order Fulfillment') # Build hierarchy m.add_child(process.uuid, func1.uuid) m.add_child(process.uuid, func2.uuid) # Apply visual styling process.set_fill_color('#e8f4f8') func1.set_fill_color('#b3e5fc') # Query hierarchy children = m.get_children(process.uuid) siblings = m.get_siblings(func1.uuid) ancestors = m.get_ancestors(func1.uuid) # Find by path results = m.find_by_hierarchy_path('/Order Management/Order Entry') # Export (preserves all hierarchy and visual properties) m.write('model.archimate')
- add(concept_type=None, name=None, uuid=None, desc=None, folder=None, profile=None)[source]ο
Method to add a new Element in this model
- Parameters:
concept_type (str) β Archimate Element type
name (str) β Elementβs name
uuid (str) β Elementβs Identifier
desc (str) β Elementβs documentation
folder (str) β Elementβs organization path
profile β str Archimate Element profile name
- Returns:
Element or View class object
- Return type:
- add_child(parent_uuid: str, child_uuid: str) None[source]ο
Add a parent-child relationship between two elements.
- Parameters:
parent_uuid β UUID of parent element
child_uuid β UUID of child element
- Raises:
KeyError β If parent or child UUID not in model
ValueError β If child already has parent, cycle would be created, or max depth exceeded
- add_profile(name=None, uuid=None, concept=None)[source]ο
Adds a new profile to the profiles dictionary and associates it with this model.
This method creates an instance of the Profile class using the provided arguments and stores it in the internal dictionary, keyed by its unique identifier (UUID). If no UUID or name is provided, the default values will be used. This method also returns the newly created Profile instance.
- Parameters:
name (str, optional) β The name of the profile being added.
uuid (str, optional) β The unique identifier for the profile.
concept (Any, optional) β A concept object associated with the profile.
- Returns:
The newly created Profile object.
- Return type:
- add_relationship(rel_type: str = '', source: Any = None, target: Any = None, uuid: str | None = None, name: str | None = None, access_type: str | None = None, influence_strength: str | None = None, desc: str | None = None, is_directed: bool | None = None, profile: str | None = None) Relationship[source]ο
Method to add a new Relationship between two Element objects
- Parameters:
rel_type (str) β Archimate relationship type
source ([str|Element]) β Source Element by Identifier or by object
target ([str|Element]) β Target Element by Identifier or by object
uuid (str) β Relationship Identifier
name (str) β Relationship name
access_type (str) β if type is Access, type of Access (Read, Writeβ¦)
influence_strength (str) β if type is Influence, strength of the influence (1, 10, +,++β¦)
desc (str) β Relationship documentation
is_directed (bool) β if type is Association, flag to indicated if the relationhsip is directed
- Returns:
Relationship class object
- Return type:
- check_connection(c: Any) bool[source]ο
Method to check the validity of a single connection
- Parameters:
c (Connection) β Connection object
- Returns:
True if the connection is valid
- Return type:
boolean
- check_invalid_nodes()[source]ο
Check and get the list of nodes that are orphans (without known related Element) :return: list of orphan nodes :rtype: list(Node)
- property connsο
Get the list of all connections from the model :return: Connections :rtype: list(Connection)
- default_theme(theme='archi')[source]ο
Set the default color theme for the model :param theme: default theme reference :return: nothing
- property elementsο
Get the list of Elements in this model
- Returns:
[Element]
- Return type:
list
- embed_props(remove_props=False)[source]ο
Method to embed properties of each view, element, relationship into their description attribute as a stringified json tag
Some tools like Aris are not configured to managed conceptβs properties, so we embed the properties before exporting the model there
- expand_props(clean_doc=True)[source]ο
Method to expand modelβs concepts desc attribute properties tag into conceptβs properties
- filter_elements(fct)[source]ο
Method to filter Elements
- Parameters:
fct (function) β callback (lambda) function with filtering criteria
- Returns:
list of Elements
- Return type:
list
- filter_relationships(fct)[source]ο
Method to find relationhips by providing a callback function with criteria
- Parameters:
fct (function) β callback function
- Returns:
list(Relationships)
- Return type:
list
- filter_views(fct)[source]ο
Method to find views by providing a callback function with criteria
- Parameters:
fct (function) β callback function
- Returns:
list(Views)
- Return type:
list
- find_by_hierarchy_path(path: str) list[Element][source]ο
Find elements by hierarchy path (e.g., β/parent/child/elementβ).
Supports wildcard matching at end:
'/parent/child/*'matches all children of child.- Parameters:
path β Hierarchy path string starting with β/β, levels separated by β/β
- Returns:
List of matching Elements
- find_elements(name=None, elem_type=None)[source]ο
Method to find elements by name or type or both
- Parameters:
name (str) β name criteria
elem_type (str) β elem_type criteria
- Returns:
list(Element)
- Return type:
list
- find_relationships(rel_type, elem, direction='both')[source]ο
Find all relationships of a list of elements
- Parameters:
rel_type (str) β type of relationship tp search for
elem (Element) β an element with relationships
direction (str) β data direction [βin_relsβ, βout_relsβ, βbothβ | None]
- Returns:
[Relationship]
- Return type:
list
- find_views(name)[source]ο
Method to find views by name
- Parameters:
name (str)
- Returns:
list(View)
- Return type:
list
- get_ancestors(elem_uuid: str) list[Element][source]ο
Get all ancestors of an element (parent, grandparent, β¦, root).
- Parameters:
elem_uuid β Element UUID
- Returns:
List of ancestor Elements [parent, grandparent, β¦, root] (excludes the element itself)
- get_children(elem_uuid: str) list[Element][source]ο
Get all direct children of a given element.
- Parameters:
elem_uuid β Element UUID
- Returns:
List of child Elements (empty if no children)
- get_depth(elem_uuid: str) int[source]ο
Get the nesting depth of an element (0 = root).
- Parameters:
elem_uuid β Element UUID
- Returns:
Depth level
- get_descendants(elem_uuid: str) list[Element][source]ο
Get all descendants of an element in breadth-first order.
- Parameters:
elem_uuid β Element UUID
- Returns:
List of descendant Elements (excludes the element itself)
- get_elements_by_viewpoint(viewpoint_id: str) list[Any][source]ο
Return elements assigned to the given viewpoint slug.
- Parameters:
viewpoint_id (str) β canonical viewpoint slug
- Returns:
list of Element objects
- Return type:
list[Element]
- Raises:
ValueError β if viewpoint_id is not a recognised slug
- get_leaf_elements() list[Element][source]ο
Get all leaf elements (elements with no children).
- Returns:
List of leaf Elements
- get_or_create_element(elem_type: str, elem: str, create_elem: bool = False) Any | None[source]ο
Method to get an Element by name or create one if not existing
- Parameters:
elem_type (str) β Archimate type of the element
elem (str) β name of the Element
create_elem (bool) β if True, create a new Element if not found
- Returns:
Element object
- Return type:
- get_or_create_relationship(rel_type: str, name: str | None, source: Any, target: Any, create_rel: bool = False, access_type: str | None = None, influence_strength: str | None = None, desc: str | None = None, is_directed: bool | None = None) Any | None[source]ο
Method to get a Relationship by source/target/type and/or by name or create one if not found
- Parameters:
rel_type (str) β Archimate type of the relationship
name (str) β name of the relationships
source ([str|Element]) β Source Element or Identifier of the Element
target ([str|Element]) β Target Element or Identifier of the Element
create_rel (bool) β if True, create a new relationship if not found
access_type (str) β if type is Access, and creat_rel is true, set the access type
influence_strength (str) β if type is Influence, and creat_rel is true, set the strenght
desc (str) β relationship description
is_directed (bool) β if type is Association, and creat_rel is true, set the direction flag
- Returns:
Relationship object
- Return type:
- get_or_create_view(view, create_view=False)[source]ο
Method to get or create a view by name
- Parameters:
view (str) β View name
create_view (bool) β if True, create a view if not found
- Returns:
View object
- Return type:
- get_parent(elem_uuid: str) Element | None[source]ο
Get the parent element of a given element.
- Parameters:
elem_uuid β Element UUID
- Returns:
Parent Element or None if root
- get_root_elements() list[Element][source]ο
Get all root elements (elements with no parent).
- Returns:
List of root Elements
- get_siblings(elem_uuid: str) list[Element][source]ο
Get all sibling elements (elements with same parent).
- Parameters:
elem_uuid β UUID of element to find siblings for
- Returns:
List of sibling Elements (excludes elem_uuid itself)
- Raises:
KeyError β If element UUID not in model
- get_viewpoints()[source]ο
Return all 13 standard ArchiMate 3.x viewpoints.
- Returns:
list of Viewpoint objects
- Return type:
list[Viewpoint]
- get_views_by_viewpoint(viewpoint_id: str) list[Any][source]ο
Return views whose primary viewpoint matches the given slug.
- Parameters:
viewpoint_id (str) β canonical viewpoint slug
- Returns:
list of View objects
- Return type:
list[View]
- Raises:
ValueError β if viewpoint_id is not a recognised slug
- merge(file_path)[source]ο
Method to merge an Archimate file into this model
- Parameters:
file_path (str)
- property nodesο
Get the list of all nodes from the model :return: Nodes :rtype: list(Node)
- property profiles: list[Profile]ο
Property to access the profiles.
This property provides access to the _profiles attribute of the class instance. It allows getting the internal profiles data which is encapsulated within the class.
- Returns:
The value of the _profiles attribute.
- prop(key, value=None)[source]ο
Method to get or set an elementβs property
- Parameters:
key (str) β Property key
value (str) β Property value
- Returns:
an existing element property value if βvalueβ argument is None
- Return type:
str
- property propsο
Dictionary of model properties (read only)
- Returns:
properties dictionary
- Return type:
dict
- read(file_path, *args, **kwargs)[source]ο
Method to read an Archimate file
The method detects automatically and reads the following formats: - Open Group Open Exchange File (.xml) - plain XML text - Archi Tool (.archimate) - ZIP archive containing model.xml
ZIP archives (.archimate) are transparently extracted before parsing. File format is detected using magic bytes (PK signature) for ZIP, falling back to plain text reading for .xml files.
- Parameters:
file_path (str) β Path to the file to read
- Raises:
SystemExit β If file cannot be read or is invalid
- property relationshipsο
Get the list of Relationships in this Model
- Returns:
[Relationship]
- Return type:
list
- remove_child(parent_uuid: str, child_uuid: str) None[source]ο
Remove a parent-child relationship (orphan the child).
- Parameters:
parent_uuid β UUID of parent element
child_uuid β UUID of child element
- Raises:
KeyError β If parent or child UUID not in model
ValueError β If child is not actually a child of parent
- property typeο
Get the type of Model :return: type :rtype: str
- property uuidο
Get the Model Identifier
- Returns:
Identifer
- Return type:
str
- property viewsο
Get the list of views in this model
- Returns:
[View]
- Return type:
list
- write(file_path=None, writer=None)[source]ο
Method to write the file_path to an Archimate file
Auto-selects writer based on file extension if not explicitly specified: - .archimate files use Archi native format (archiWriter) - .xml files use OpenGroup Exchange format (archimateWriter) - Default to OpenGroup Exchange format
- Parameters:
file_path (str) β Output file path
writer (Writers|str|callable|None) β writer selection (enum value, registry key, or callable) used to format the output. If None, auto-detects based on file_path extension.
- Returns:
XML data structure as string
- Return type:
str