Skip to content

AgentBase Class

sequenze edited this page Jun 19, 2017 · 4 revisions

Provides basic functionality for a threaded interaction with a space. This is an abstract class.

Inheritance Hierarchy

Object

Syntax

public abstract class AgentBase : ISpace

Constructors

Name                                         Description
AgentBase(String,ISpace) Initializes a new instance of the AgentBase class.

Methods

Name                             Description
Start() Starts the underlying thread, executing the 'DoWork' method.
Get(IPattern) Retrieves and removes the first tuple from the Space, matching the specified pattern. The operation will block if no elements match.
Get(Object[]) Retrieves and removes the first tuple from the Space, matching the specified pattern. The operation will block if no elements match.
GetP(IPattern) Retrieves and removes the first tuple from the Space, matching the specified pattern. The operation is non-blocking. The operation will return null if no elements match.
GetP(Object[]) Retrieves and removes the first tuple from the Space, matching the specified pattern. The operation is non-blocking. The operation will return null if no elements match.
GetAll(IPattern) Retrieves and removes all tuples from the Space matching the specified pattern. The operation is non-blocking. The operation will return an empty set if no elements match.
GetAll(Object[]) Retrieves and removes all tuples from the Space matching the specified pattern. The operation is non-blocking. The operation will return an empty set if no elements match.
Query(IPattern) Retrieves the first tuple from the Space, matching the specified pattern. The operation will block if no elements match.
Query(Object[]) Retrieves the first tuple from the Space, matching the specified pattern. The operation will block if no elements match.
QueryP(IPattern) Retrieves the first tuple from the Space, matching the specified pattern. The operation is non-blocking. The operation will return null if no elements match.
QueryP(Object[]) Retrieves the first tuple from the Space, matching the specified pattern.The operation is non-blocking.The operation will return null if no elements match.
QueryAll(IPattern) Retrieves all tuples from the Space matching the specified pattern. The operation is non-blocking. The operation will return an empty set if no elements match.
QueryAll(Object[]) Retrieves all tuples from the Space matching the specified pattern. The operation is non-blocking. The operation will return an empty set if no elements match.
Put(ITuple) Inserts the tuple passed as argument into the Space.
Put(Object[]) Inserts the tuple passed as argument into the Space.
DoWork() Method which is automatically executed when starting the agent.

Remarks

The AgentBase is a first class citizen with respect to dotSpace. This means that any entity derrived from AgentBase is able to access the space operations natively.

Examples

The following example shows how to use a simple Agent combined with a FifoSpace. As the example shows, the AgentBase is inherited and the abstract methods are implemented. The Worker is able to take the role of both a producer and consumer.

    public class Worker : AgentBase
    {
        private bool produce;
        private int nrItems;

        public Worker(string name, bool produce, int nrItems, ISpace ts) : base(name, ts)
        {
            this.produce = produce;
            this.nrItems = nrItems;
        }

        protected override void DoWork()
        {
            try
            {
                while (this.nrItems > 0)
                {
                    if (this.produce)
                    {
                        // Wait until we can start
                        this.Get(true);
                        this.Put("Im am producing...");
                    }
                    else
                    {
                        // Start the producer
                        this.Put(true);
                        ITuple tuple = this.Get(typeof(string));
                        Console.WriteLine(tuple[0]);
                    }
                    this.nrItems--;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }

    static void Main(string[] args)
    {
        FifoSpace space = new FifoSpace();
        Worker producer = new Worker("producer", true, 5, space);
        Worker consumer = new Worker("consumer", false, 5, space);
        producer.Start();
        consumer.Start();
        Console.Read();
    }

    /*
    The following is printed to the console:
    Im am producing...
    Im am producing...
    Im am producing...
    Im am producing...
    Im am producing...
    */

See Also

ISpace, IPattern, ITuple

Clone this wiki locally