diff --git a/mesa/experimental/signals/__init__.py b/mesa/experimental/signals/__init__.py index c199683b3d8..375795ddf77 100644 --- a/mesa/experimental/signals/__init__.py +++ b/mesa/experimental/signals/__init__.py @@ -1,7 +1,7 @@ """Functionality for Observables.""" from .observable_collections import ObservableList -from .signal import All, Computable, Computed, HasObservables, Observable, Signal +from .mesa_signal import All, Computable, Computed, HasObservables, Observable, Signal __all__ = [ "Observable", diff --git a/mesa/experimental/signals/signal.py b/mesa/experimental/signals/mesa_signal.py similarity index 97% rename from mesa/experimental/signals/signal.py rename to mesa/experimental/signals/mesa_signal.py index dc9acae870b..296c727b87d 100644 --- a/mesa/experimental/signals/signal.py +++ b/mesa/experimental/signals/mesa_signal.py @@ -10,7 +10,7 @@ from collections.abc import Callable from typing import Any -from .signals_util import create_weakref +from mesa.experimental.signals.signals_util import create_weakref __all__ = ["Observable", "HasObservables", "All", "Computable"] @@ -127,8 +127,12 @@ def __init__(self): def __get__(self, instance, owner): computed = getattr(instance, self.private_name) - old_value = computed._value + + # fixme, we are not detecting if one computable is dependent on another + if CURRENT_COMPUTED is not None: + CURRENT_COMPUTED._add_parent(instance, self.public_name, old_value) + new_value = computed() if new_value != old_value: @@ -151,6 +155,7 @@ def __set__(self, instance: HasObservables, value): # noqa D103 # no on change event? setattr(instance, self.private_name, value) value.name = self.public_name + value.owner = instance class Computed: @@ -162,6 +167,7 @@ def __init__(self, func: Callable, *args, **kwargs): self._first = True self._value = None self.name: str = "" # set by Computable + self.owner: HasObservables # set by Computable self.parents: weakref.WeakKeyDictionary[HasObservables, dict[str, Any]] = ( weakref.WeakKeyDictionary() diff --git a/mesa/experimental/signals/observable_collections.py b/mesa/experimental/signals/observable_collections.py index 3c4a3a21341..4c52e3f98e3 100644 --- a/mesa/experimental/signals/observable_collections.py +++ b/mesa/experimental/signals/observable_collections.py @@ -8,7 +8,7 @@ from collections.abc import Iterable, MutableSequence from typing import Any -from .signal import BaseObservable, HasObservables +from .mesa_signal import BaseObservable, HasObservables __all__ = [ "ObservableList",