Message and Tuple

by ondrej on April 1st, 2007

In the last posts a term message was used. In the Rinda documentation you would not probably find this term; it is called more accurately: tuple.


What is a tuple?

  • [1, 2] is a tuple
  • [:message, 'Hi there!'] is a tuple
  • ['Hi there!', :message] is also a tuple
  • [:result, 42, 'John', 'Susanne', 'London'] is a tuple
  • [:clock, Time.new] is a tuple too
  • [:service, CalculatorService.new(1, true) is a tuple

Tuple is an ordered list of elements [wikipedia].


A blackboard is a place to share tuples; agents are writing, reading and taking tuples.

If you have a simple system, where agents are using only one kind of messages, it is enough to use a single, e.g. [1], ['Hello']. But what to do, if your system requires to differentiate more types of messages? Well, there is no problem with writing different types of messages, but there is a problem how to find the correct one to read or take it.

Basically there are two possibilities:

  • Every type of a message has different number of elements. The number of elements is the distinguishing sign.

    Example: [1] is similar to [2] or ['Hello'], but it is different then [3, 'Hello'].

  • One of the parameters is an identifier. The number of elements and an identifier are the distinguishing signs.

    Example: [:request, 1, 2] is similar to [:request, 24, 'John'], but it is different to [:result, 42, 'London'].


How are the blackboard operations using tuples?

  • The write operation writes a tuple (specified as a parameter).
    ts.write [:message, 'Hi there!']
    
  • Reading operations read and take use tuples in a slightly different way. It is possible to use the nil value as a wild card for the position, where it is used.
    • To read any single:
      ts.take [nil], 0
      
    • To read any pair:
      ts.take [nil, nil], 0
      
    • To read any :message type:
      ts.take [:message, nil], 0
      
    • To read any pair, where the second element has value 4 (not very practical example :):
      ts.take [nil, 4], 0
      
    • To read exactly the tuple [:result, 4]:
      ts.take [:result, 4], 0
      

    The second parameter says, how long (in seconds) should a read operation wait until a tuple appears on the blackboard. Zero means to not to wait at all.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS