diff --git a/brain/README.txt b/brain/README.txt index f47f391..88013df 100644 --- a/brain/README.txt +++ b/brain/README.txt @@ -34,13 +34,21 @@ get triggered so that the receiver neurons' states are updated: >>> n02.getState() -To allow for concurrent (thread-safe) access to the brain all changes to -the neurons' states is under the control of a transaction. If we end the -current transaction all state changes will be forgotton: +To allow for concurrent access to the brain by different clients +simultaneously, the changes to the neurons' states may be under the control of +a session: - >>> from cybertools.brain.transaction import endTransaction - >>> endTransaction() + >>> n01.setState(State()) + >>> n02.setState(State()) + >>> from cybertools.brain.session import Session + >>> session = Session() + >>> n01.setState(State(1.0), session) >>> n01.getState() + >>> n01.getState(session) + + >>> n01.notify(session) >>> n02.getState() + >>> n02.getState(session) + diff --git a/brain/interfaces.py b/brain/interfaces.py index 59371ca..b46c976 100644 --- a/brain/interfaces.py +++ b/brain/interfaces.py @@ -34,7 +34,7 @@ class ISynapsis(Interface): transition = Attribute("A transition changes the sender neuron's state.") - def trigger(transaction=None): + def trigger(session=None): """ Recalculate the receiver neuron's state by executing the synapse's transition using the state of the sender neuron. """ @@ -45,15 +45,15 @@ class INeuron(Interface): senders = Attribute("The sender synapses") receivers = Attribute("The receiver synapses") - def setState(state, transaction=None): + def setState(state, session=None): """ Set the neuron's state. """ - def getState(transaction=None): + def getState(session=None): """ Return the neuron's state. """ - def notify(transaction=None): + def notify(session=None): """ Notifies the neuron that something has happened. This method calls the trigger() method on all downstream (receiver) synapses. In addition it may perform side effects like changing @@ -70,12 +70,13 @@ class IState(Interface): class ITransition(Interface): - def execute(transaction=None): + def execute(session=None): """ Transform the receiver's state to a new state value and return it. """ -class ITransaction(Interface): - """ A transaction that keeps track of the neurons' states. +class ISession(Interface): + """ A session keeps all actions for a certain sequence of user interactions + together by keeping track of the neurons' current and cumulated states. """ diff --git a/brain/neuron.py b/brain/neuron.py index 6b076d4..d23084b 100644 --- a/brain/neuron.py +++ b/brain/neuron.py @@ -25,7 +25,6 @@ $Id$ from zope.interface import implements from cybertools.brain.interfaces import INeuron, ISynapsis from cybertools.brain.state import State, Transition -from cybertools.brain.transaction import getTransaction class Synapsis(object): @@ -41,10 +40,10 @@ class Synapsis(object): receiver.senders.append(self) self.transition = Transition(self) - def trigger(self, transaction=None): + def trigger(self, session=None): receiver = self.receiver - receiver.setState(self.transition.execute(transaction), transaction) - receiver.notify(transaction) + receiver.setState(self.transition.execute(session), session) + receiver.notify(session) class Neuron(object): @@ -56,18 +55,18 @@ class Neuron(object): self.receivers = [] self.state = State() - def setState(self, state, transaction=None): - transaction = getTransaction(transaction) - transaction.setState(self, state) + def setState(self, state, session=None): + if session is None: + self.state = state + else: + session.setState(self, state) - def getState(self, transaction=None): - if transaction is None: - transaction = getTransaction(create=False) - if transaction is None: + def getState(self, session=None): + if session is None: return self.state - return transaction.getState(self) + return session.getState(self) - def notify(self, transaction=None): + def notify(self, session=None): for r in self.receivers: - r.trigger(transaction) + r.trigger(session) diff --git a/brain/transaction.py b/brain/session.py similarity index 60% rename from brain/transaction.py rename to brain/session.py index 5962c6a..84225d1 100644 --- a/brain/transaction.py +++ b/brain/session.py @@ -23,12 +23,12 @@ $Id$ """ from zope.interface import implements -from cybertools.brain.interfaces import ITransaction +from cybertools.brain.interfaces import ISession -class Transaction(object): +class Session(object): - implements(ITransaction) + implements(ISession) def __init__(self): self.states = {} @@ -39,25 +39,3 @@ class Transaction(object): def getState(self, neuron): return self.states.get(neuron, neuron.state) - -transactions = [] - -def getTransaction(transaction=None, create=True): - if transaction is None: - if transactions: - transaction = transactions[0] - elif create: - transaction = Transaction() - transactions.append(transaction) - else: - return None - return transaction - -def endTransaction(transaction=None): - if transaction is None: - if transactions: - del transactions[0] - else: - if transaction in transactions: - del transactions[transactions.index(transaction)] - diff --git a/brain/state.py b/brain/state.py index 065714b..6caba5c 100644 --- a/brain/state.py +++ b/brain/state.py @@ -47,9 +47,9 @@ class Transition(object): self.synapsis = synapsis self.factor = factor - def execute(self, transaction=None): - oldState = self.synapsis.receiver.getState(transaction) - senderState = self.synapsis.sender.getState(transaction) + def execute(self, session=None): + oldState = self.synapsis.receiver.getState(session) + senderState = self.synapsis.sender.getState(session) return State(oldState.value + senderState.value * self.factor)