cybertools.brain: work in progress

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1272 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2006-07-14 11:50:21 +00:00
parent 02911c189b
commit 9897a60a73
5 changed files with 40 additions and 54 deletions

View file

@ -34,13 +34,21 @@ get triggered so that the receiver neurons' states are updated:
>>> n02.getState()
<State 1.0>
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()
<State 0.0>
>>> n01.getState(session)
<State 1.0>
>>> n01.notify(session)
>>> n02.getState()
<State 0.0>
>>> n02.getState(session)
<State 1.0>

View file

@ -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.
"""

View file

@ -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)

View file

@ -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)]

View file

@ -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)