Pattern: Agent Life Cycle
Problem: An agent should be able to check the blackboard for an n-tuple and to process the obtained n-tuple. (It is a trivial pattern.)
Solution: The solution is to perform operation read or take in a loop.
loop do read or take n-tuple process n-tuple end
Example:
loop do t = ts.take [:plus, nil, nil] res = t[1] + t[2] ts.write [:result, res] end
Comments:
The method process should execute agent’s duty :), there is only questionable, if it should:
- start a new thread, to process the loaded n-tuple, and immediately listen for a next n-tuple
- or it should process the loaded n-tuple, and when the processing is finished, to start listening for a next n-tuple.
I would prefer the second way - it is more agent-like, it is not mixing threads and agents concepts (including managing agents vs managing threads) - if you would like to process more n-tuples in parallel, simply start another agent instance.