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:
		
							parent
							
								
									02911c189b
								
							
						
					
					
						commit
						9897a60a73
					
				
					 5 changed files with 40 additions and 54 deletions
				
			
		|  | @ -34,13 +34,21 @@ get triggered so that the receiver neurons' states are updated: | ||||||
|   >>> n02.getState() |   >>> n02.getState() | ||||||
|   <State 1.0> |   <State 1.0> | ||||||
| 
 | 
 | ||||||
| To allow for concurrent (thread-safe) access to the brain all changes to | To allow for concurrent access to the brain by different clients | ||||||
| the neurons' states is under the control of a transaction. If we end the | simultaneously, the changes to the neurons' states may be under the control of | ||||||
| current transaction all state changes will be forgotton: | a session: | ||||||
| 
 | 
 | ||||||
|   >>> from cybertools.brain.transaction import endTransaction |   >>> n01.setState(State()) | ||||||
|   >>> endTransaction() |   >>> n02.setState(State()) | ||||||
|  |   >>> from cybertools.brain.session import Session | ||||||
|  |   >>> session = Session() | ||||||
|  |   >>> n01.setState(State(1.0), session) | ||||||
|   >>> n01.getState() |   >>> n01.getState() | ||||||
|   <State 0.0> |   <State 0.0> | ||||||
|  |   >>> n01.getState(session) | ||||||
|  |   <State 1.0> | ||||||
|  |   >>> n01.notify(session) | ||||||
|   >>> n02.getState() |   >>> n02.getState() | ||||||
|   <State 0.0> |   <State 0.0> | ||||||
|  |   >>> n02.getState(session) | ||||||
|  |   <State 1.0> | ||||||
|  |  | ||||||
|  | @ -34,7 +34,7 @@ class ISynapsis(Interface): | ||||||
| 
 | 
 | ||||||
|     transition = Attribute("A transition changes the sender neuron's state.") |     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 |         """ Recalculate the receiver neuron's state by executing the | ||||||
|             synapse's transition using the state of the sender neuron. |             synapse's transition using the state of the sender neuron. | ||||||
|         """ |         """ | ||||||
|  | @ -45,15 +45,15 @@ class INeuron(Interface): | ||||||
|     senders = Attribute("The sender synapses") |     senders = Attribute("The sender synapses") | ||||||
|     receivers = Attribute("The receiver synapses") |     receivers = Attribute("The receiver synapses") | ||||||
| 
 | 
 | ||||||
|     def setState(state, transaction=None): |     def setState(state, session=None): | ||||||
|         """ Set the neuron's state. |         """ Set the neuron's state. | ||||||
|         """ |         """ | ||||||
| 
 | 
 | ||||||
|     def getState(transaction=None): |     def getState(session=None): | ||||||
|         """ Return the neuron's state. |         """ Return the neuron's state. | ||||||
|         """ |         """ | ||||||
| 
 | 
 | ||||||
|     def notify(transaction=None): |     def notify(session=None): | ||||||
|         """ Notifies the neuron that something has happened. This method |         """ Notifies the neuron that something has happened. This method | ||||||
|             calls the trigger() method on all downstream (receiver) synapses. |             calls the trigger() method on all downstream (receiver) synapses. | ||||||
|             In addition it may perform side effects like changing |             In addition it may perform side effects like changing | ||||||
|  | @ -70,12 +70,13 @@ class IState(Interface): | ||||||
| 
 | 
 | ||||||
| class ITransition(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. |         """ Transform the receiver's state to a new state value and return it. | ||||||
|         """ |         """ | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class ITransaction(Interface): | class ISession(Interface): | ||||||
|     """ A transaction that keeps track of the neurons' states. |     """ A session keeps all actions for a certain sequence of user interactions | ||||||
|  |         together by keeping track of the neurons' current and cumulated states. | ||||||
|     """ |     """ | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -25,7 +25,6 @@ $Id$ | ||||||
| from zope.interface import implements | from zope.interface import implements | ||||||
| from cybertools.brain.interfaces import INeuron, ISynapsis | from cybertools.brain.interfaces import INeuron, ISynapsis | ||||||
| from cybertools.brain.state import State, Transition | from cybertools.brain.state import State, Transition | ||||||
| from cybertools.brain.transaction import getTransaction |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class Synapsis(object): | class Synapsis(object): | ||||||
|  | @ -41,10 +40,10 @@ class Synapsis(object): | ||||||
|         receiver.senders.append(self) |         receiver.senders.append(self) | ||||||
|         self.transition = Transition(self) |         self.transition = Transition(self) | ||||||
| 
 | 
 | ||||||
|     def trigger(self, transaction=None): |     def trigger(self, session=None): | ||||||
|         receiver = self.receiver |         receiver = self.receiver | ||||||
|         receiver.setState(self.transition.execute(transaction), transaction) |         receiver.setState(self.transition.execute(session), session) | ||||||
|         receiver.notify(transaction) |         receiver.notify(session) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class Neuron(object): | class Neuron(object): | ||||||
|  | @ -56,18 +55,18 @@ class Neuron(object): | ||||||
|         self.receivers = [] |         self.receivers = [] | ||||||
|         self.state = State() |         self.state = State() | ||||||
| 
 | 
 | ||||||
|     def setState(self, state, transaction=None): |     def setState(self, state, session=None): | ||||||
|         transaction = getTransaction(transaction) |         if session is None: | ||||||
|         transaction.setState(self, state) |             self.state = state | ||||||
|  |         else: | ||||||
|  |             session.setState(self, state) | ||||||
| 
 | 
 | ||||||
|     def getState(self, transaction=None): |     def getState(self, session=None): | ||||||
|         if transaction is None: |         if session is None: | ||||||
|             transaction = getTransaction(create=False) |  | ||||||
|             if transaction is None: |  | ||||||
|                 return self.state |                 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: |         for r in self.receivers: | ||||||
|             r.trigger(transaction) |             r.trigger(session) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -23,12 +23,12 @@ $Id$ | ||||||
| """ | """ | ||||||
| 
 | 
 | ||||||
| from zope.interface import implements | 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): |     def __init__(self): | ||||||
|         self.states = {} |         self.states = {} | ||||||
|  | @ -39,25 +39,3 @@ class Transaction(object): | ||||||
|     def getState(self, neuron): |     def getState(self, neuron): | ||||||
|         return self.states.get(neuron, neuron.state) |         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)] |  | ||||||
| 
 |  | ||||||
|  | @ -47,9 +47,9 @@ class Transition(object): | ||||||
|         self.synapsis = synapsis |         self.synapsis = synapsis | ||||||
|         self.factor = factor |         self.factor = factor | ||||||
| 
 | 
 | ||||||
|     def execute(self, transaction=None): |     def execute(self, session=None): | ||||||
|         oldState = self.synapsis.receiver.getState(transaction) |         oldState = self.synapsis.receiver.getState(session) | ||||||
|         senderState = self.synapsis.sender.getState(transaction) |         senderState = self.synapsis.sender.getState(session) | ||||||
|         return State(oldState.value + senderState.value * self.factor) |         return State(oldState.value + senderState.value * self.factor) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 helmutm
						helmutm