Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core is a singleton #76

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion process_bigraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from process_bigraph.processes import register_processes
from process_bigraph.composite import Process, Step, Composite, ProcessTypes, interval_time_precision


pretty = pprint.PrettyPrinter(indent=2)

# make the singleton instance of the ProcessTypes class
core = ProcessTypes()


def pp(x):
"""Print ``x`` in a pretty format."""
Expand Down
25 changes: 17 additions & 8 deletions process_bigraph/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,27 @@ class ProcessTypes(TypeSystem):
It maintains a registry of process types and provides methods to register
new process types, protocols, and emitters.
"""
_instance = None # Class-level private variable for the singleton instance

def __init__(self):
super().__init__()
self.process_registry = Registry()
self.protocol_registry = Registry()
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super(ProcessTypes, cls).__new__(cls)
# Initialize the instance
cls._instance._is_initialized = False
return cls._instance

self.register_types(PROCESS_TYPES)
self.register_protocols(BASE_PROTOCOLS)
self.register_processes(BASE_EMITTERS)
def __init__(self):
if not self._is_initialized: # Prevent re-initialization
super().__init__()
self.process_registry = Registry()
self.protocol_registry = Registry()

self.register_process('composite', Composite)
self.register_types(PROCESS_TYPES)
self.register_protocols(BASE_PROTOCOLS)
self.register_processes(BASE_EMITTERS)

self.register_process('composite', Composite)
self._is_initialized = True

def register_protocols(self, protocols):
"""Register protocols with the core"""
Expand Down
7 changes: 7 additions & 0 deletions process_bigraph/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,13 @@ def test_stochastic_deterministic_composite(core):
pass


def test_singleton_core():
from process_bigraph import core
from process_bigraph import ProcessTypes
core2 = ProcessTypes() # This should point to the singleton instance
assert core is core2, "ProcessTypes is not behaving like a singleton."


if __name__ == '__main__':
core = ProcessTypes()
core = register_types(core)
Expand Down
Loading