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: object

Class 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:

Model

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:

Element|View

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:

Profile

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:

Relationship

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_conn()[source]

Method to check the validity of a list of connections

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:

Element

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:

Relationship

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:

View

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_profile(name: str) Profile | None[source]

Get a profile by name.

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

remove_prop(key)[source]

Method to remove an element property

Parameters:

key (str)

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