The Element Class#

The class Element is the core of Gaphor’s data model.

class gaphor.core.modeling.Element(id: str | None = None, model: gaphor.core.modeling.element.RepositoryProtocol | None = None)[source]#

Base class for all model data classes.

property id: str#

An id (read-only), unique within the model.

property model: RepositoryProtocol#

The owning model, raises TypeError when model is not set.

Unlink the element.

All the elements references are destroyed. For composite associations, the associated elements are also unlinked.

The unlink lock is acquired while unlinking this element’s properties to avoid recursion problems.

Event handling#

handle(event) None[source]#

Propagate incoming events.

This only works if the element has been created by an ElementFactory

watcher(default_handler: Optional[Callable[[ElementUpdated], None]] = None) EventWatcherProtocol[source]#

Create a new watcher for this element.

Watchers provide a convenient way to get signalled when a property relative to self has been changed.

To use a watcher, the element should be created by a properly wired up ElementFactory`.

This example is purely illustrative:

>>> element = Element()
>>> watcher = element.watcher(default_handler=print)
>>> watcher.watch("note")  # Watch for changed on element.note

Loading and saving#

load(name, value) None[source]#

Loads value in name.

Make sure that after all elements are loaded, postload() should be called.

postload() None[source]#

Fix up the odds and ends.

This is run after all elements are loaded.

save(save_func) None[source]#

Save the state by calling save_func(name, value).

OCL-style methods#

isKindOf(class_: type[gaphor.core.modeling.element.Element]) bool[source]#

Returns True if the object is an instance of class_.

isTypeOf(other: Element) bool[source]#

Returns True if the object is of the same type as the other.

Protocols#

class gaphor.core.modeling.element.RepositoryProtocol(*args, **kwargs)[source]#
create(type: type[T]) T[source]#

Create a new element in the repository.

select(self, expression: Callable[[Element], bool]) Iterator[Element]#

Select elements from the repository that fulfill expression.

select(self, type_: type[T]) Iterator[T]

Select all elements from the repository of type type_.

select(self, expression: None) Iterator[Element]

Select all elements from the repository.

lookup(id: str) gaphor.core.modeling.element.Element | None[source]#

Get an element by id from the repository.

Returns None if no such element exists.

class gaphor.core.modeling.element.EventWatcherProtocol(*args, **kwargs)[source]#
watch(path: str, handler: Optional[Callable[[ElementUpdated], None]] = None) EventWatcherProtocol[source]#

Add a watch for a specific path. The path is relative to the element that created the watcher object.

Returns self, so watch operations can be chained.

unsubscribe_all() None[source]#

Should be called before the watcher is disposed, to release all watched paths.