4 Simple Component - Microprocesses with standard external interfaces Exercise: Write a class called component that subclasses microprocess with the following... Attributes:
{ "inbox": [],"outbox": [] } Clearly this allows for more inboxes and outboxes, but at this stage we'll keep things simple. Behaviour: (methods)
self.send(value, boxname) Then given the suggested implementation of boxes above the following should be true afterwards:
self.boxes[boxname][-1] == value ie the last value in the list associated with the boxname is the value we sent to that outbox. More explicitly, if the value of self.boxes was this beforehand:
self.boxes == { "inbox" : [], And the following call had been made:
self.send("gameover","outbox") The self.boxes would look like this afterwards:
self.boxes == { "inbox" : [], self.boxes == { "inbox" : ["hello", "world", "gameover"], Then I would expect the following behaviour code....
print "GRABBING DATA", self.recv("inbox") ... to display the following sort of behaviour:
GRABBING DATA hello The value of self.boxes should also change as follows after each call:
self.boxes == { "inbox" : ["hello", "world", "gameover"], # At start self.boxes == { "inbox" : ["hello", "world", "gameover"], The following behaviour is expected:
print self.dataReady("inbox") -> displays 3 print self.dataReady("control") -> displays 2 print self.dataReady("feedback") -> displays 0 print self.dataReady("somerandomname") -> causes a KeyError: somerandomname exception to be thrown
Answer:
Discussion: Ok that's a fairly long description, but a fairly simple implementation. So what's this done? It's enabled us to send data to a running generator and receive data back. We're not worried what the generator is doing at any point in time, and so the communications between us and the generator (or between generators) is asynchronous. An extension to the suggested __init__ is to do the following: class component(microprocess): This small extension means that classes subclassing component can have a different set of inboxes and outboxes. For example: class spinnyThing(component): That said, components by themselves are relatively boring. Unless we have some way of moving the data between generators we haven't gained anything (really) beyond the printer example above. So we need someone/something that can move data/messages from outboxes and deliver to inboxes... |
Kamaelia
is an open source project originated from and guided by BBC
Research. For more information browse the site or get in
contact.
This is an ongoing community based development site. As a result the contents of this page is the opinions of the contributors of the pages involved not the organisations involved. Specificially, this page may contain personal views which are not the views of the BBC. (the site is powered by a wiki engine)
(C) Copyright 2008 Kamaelia Contributors, including the British Broadcasting Corporation, All Rights Reserved.