Skip to content

ISpace Interface

sequenze edited this page Jun 16, 2017 · 14 revisions

Defines the minimal required operations that a tuple space datastructure should support.

Inheritance Hierarchy

Object

Syntax

public abstract interface ISpace

Methods

Name                             Description
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 a clone of the first tuple from the space matching the specified pattern. The operation will block if no elements match.
Query(Object[]) Retrieves a clone of the first tuple from the space matching the specified pattern. The operation will block if no elements match.
QueryP(IPattern) Retrieves a clone of 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 a clone of 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 clones of 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 clones of 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.

Remarks

ISpace is the base interface of all spaces. ISpace implementations are either adhering to FIFO, LIFO or Random ordering of the tuples. Regardless of the implementation, locking mechanisms must be placed to ensure threadsafety.

Examples

The following example demonstrates a concretization of the ISpace interface to create a simple Space.

    public sealed class Space : ISpace
    {
        private readonly List<ITuple> elements;

        public Space()
        {
            this.elements = new List<ITuple>();
        }

        public ITuple Get(IPattern pattern)
        {
            return this.Get(pattern.Fields);
        }
        public ITuple Get(params object[] values)
        {
            ITuple t = null;
            lock (this.elements)
            {
                t = this.WaitUntilMatch(values);
                this.elements.Remove(t);
            }
            return t;
        }
        public ITuple GetP(IPattern pattern)
        {
            return this.GetP(pattern.Fields);
        }
        public ITuple GetP(params object[] pattern)
        {
            ITuple t = null;
            lock (this.elements)
            {
                t = this.Find(pattern);
                this.elements.Remove(t);
            }
            return t;
        }
        public IEnumerable<ITuple> GetAll(IPattern pattern)
        {
            return this.GetAll(pattern.Fields);
        }
        public IEnumerable<ITuple> GetAll(params object[] values)
        {
            IEnumerable<ITuple> t = null;
            lock (this.elements)
            {
                t = this.FindAll(values);
                t.Apply(x => this.elements.Remove(x));
            }
            return t;
        }
        public ITuple Query(IPattern pattern)
        {
            return this.Query(pattern.Fields);
        }
        public ITuple Query(params object[] pattern)
        {
            ITuple t = null;
            lock (this.elements)
            {
                t = this.WaitUntilMatch(pattern);
            }
            return t;
        }
        public ITuple QueryP(IPattern pattern)
        {
            return this.QueryP(pattern.Fields);
        }
        public ITuple QueryP(params object[] pattern)
        {
            ITuple t = null;
            lock (this.elements)
            {
                t = this.Find(pattern);
            }
            return t;
        }
        public IEnumerable<ITuple> QueryAll(IPattern pattern)
        {
            return this.QueryAll(pattern.Fields);
        }
        public IEnumerable<ITuple> QueryAll(params object[] values)
        {
            IEnumerable<ITuple> t = null;
            lock (this.elements)
            {
                t = this.FindAll(values);
            }
            return t;
        }
        public void Put(ITuple t)
        {
            this.Put(t.Fields);
        }
        public void Put(params object[] tuple)
        {
            lock (this.elements)
            {
                this.elements.Add(new Tuple(tuple));
                Monitor.PulseAll(this.elements);
            }
        }

        private ITuple WaitUntilMatch(object[] pattern)
        {
            ITuple t;
            while (((t = this.Find(pattern)) == null))
            {
                Monitor.Wait(this.elements);
            }
            return t;
        }
        private ITuple Find(object[] pattern)
        {
            return this.elements.Where(x => this.Match(pattern, x.Fields)).FirstOrDefault();
        }
        private IEnumerable<ITuple> FindAll(object[] pattern)
        {
            return this.elements.Where(x => this.Match(pattern, x.Fields)).ToList();
        }
        private bool Match(object[] pattern, object[] tuple)
        {
            if (tuple.Length != pattern.Length)
            {
                return false;
            }
            bool result = true;
            for (int idx = 0; idx < tuple.Length; idx++)
            {
                if (pattern[idx] is Type)
                {
                    result &= tuple[idx] is Type ? tuple[idx].GetType() == (Type)pattern[idx] : tuple[idx].GetType() == (Type)pattern[idx];
                }
                else
                {
                    result &= tuple[idx].Equals(pattern[idx]);
                }
                if (!result) return false;
            }

            return result;
        }
    }

See Also

IPattern, ITuple

Clone this wiki locally