Exemple #1
0
        /// <summary>
        /// Store an exclusive child inside this node.
        /// </summary>
        /// <param name="key">Key for the child</param>
        /// <param name="overwrite">If true, this will overwrite any existing child with a different value.</param>
        /// <returns>The child node</returns>
        /// <exception cref="KBExclusionException">If a non-exclusive child has already been written.</exception>
        public KnowledgeBaseEntry StoreExclusive(object key, bool overwrite)
        {
            switch (mode)
            {
            case ExclusionMode.Empty:
            {
                mode = ExclusionMode.Exclusive;
                var result = new KnowledgeBaseEntry(this, key);
                if (this.Children == EmptyChildren)
                {
                    this.Children = new List <KnowledgeBaseEntry> {
                        result
                    }
                }
                ;
                else
                {
                    this.Children.Add(result);
                }

                return(result);
            }

            case ExclusionMode.NonExclusive:
                throw new KBExclusionException("Exclusive store on non-exclusive node.");


            case ExclusionMode.Exclusive:
                if (overwrite)
                {
                    this.Children[0].OverwriteExclusive(key);
                }
                else if (key != this.Children[0].Key)
                {
                    throw new KBExclusionException("Exclusive store doesn't match previous store.");
                }
                return(this.Children[0]);

            default:
                throw new InvalidOperationException("Invalid exclusion mode");
            }
        }
Exemple #2
0
        /// <summary>
        /// Store a non-exclusive child inside this node.
        /// </summary>
        /// <param name="key">Key for the new child</param>
        /// <returns>The child node</returns>
        /// <exception cref="KBExclusionException">If an exclusive child has already been written.</exception>
        public KnowledgeBaseEntry StoreNonExclusive(object key)
        {
            KnowledgeBaseEntry result = null;

            switch (mode)
            {
            case ExclusionMode.Empty:
            {
                mode   = ExclusionMode.NonExclusive;
                result = new KnowledgeBaseEntry(this, key);
                if (this.Children == EmptyChildren)
                {
                    this.Children = new List <KnowledgeBaseEntry> {
                        result
                    }
                }
                ;
                else
                {
                    this.Children.Add(result);
                }
            }
            break;

            case ExclusionMode.Exclusive:
                throw new KBExclusionException("Non-exclusive store on exclusive node.");

            case ExclusionMode.NonExclusive:
                foreach (var c in this.Children)
                {
                    if (c.Key == key)
                    {
                        return(c);
                    }
                }
                this.Children.Add(result = new KnowledgeBaseEntry(this, key));
                break;
            }
            return(result);
        }
 internal KnowledgeBaseEntry(KnowledgeBaseEntry parent, object key)
 {
     Key = key;
     Parent = parent;
     Children = EmptyChildren;
 }
        /// <summary>
        /// Store a non-exclusive child inside this node.
        /// </summary>
        /// <param name="key">Key for the new child</param>
        /// <returns>The child node</returns>
        /// <exception cref="KBExclusionException">If an exclusive child has already been written.</exception>
        public KnowledgeBaseEntry StoreNonExclusive(object key)
        {
            KnowledgeBaseEntry result =null;
            switch (mode)
            {
                case ExclusionMode.Empty:
                    {
                        mode = ExclusionMode.NonExclusive;
                        result = new KnowledgeBaseEntry(this, key);
                        if (this.Children == EmptyChildren)
                            this.Children = new List<KnowledgeBaseEntry> { result };
                        else
                            this.Children.Add(result);
                    }
                    break;

                case ExclusionMode.Exclusive:
                    throw new KBExclusionException("Non-exclusive store on exclusive node.");

                case ExclusionMode.NonExclusive:
                    foreach (var c in this.Children)
                    {
                        if (c.Key == key)
                            return c;
                    }
                    this.Children.Add(result = new KnowledgeBaseEntry(this, key));
                    break;
            }
            return result;
        }
        /// <summary>
        /// Store an exclusive child inside this node.
        /// </summary>
        /// <param name="key">Key for the child</param>
        /// <param name="overwrite">If true, this will overwrite any existing child with a different value.</param>
        /// <returns>The child node</returns>
        /// <exception cref="KBExclusionException">If a non-exclusive child has already been written.</exception>
        public KnowledgeBaseEntry StoreExclusive(object key, bool overwrite)
        {
            switch (mode)
            {
                case ExclusionMode.Empty:
                    {
                        mode = ExclusionMode.Exclusive;
                        var result = new KnowledgeBaseEntry(this, key);
                        if (this.Children == EmptyChildren)
                            this.Children = new List<KnowledgeBaseEntry> { result };
                        else
                            this.Children.Add(result);

                        return result;
                    }

                case ExclusionMode.NonExclusive:
                    throw new KBExclusionException("Exclusive store on non-exclusive node.");

                case ExclusionMode.Exclusive:
                    if (overwrite)
                        this.Children[0].OverwriteExclusive(key);
                    else if (key != this.Children[0].Key)
                        throw new KBExclusionException("Exclusive store doesn't match previous store.");
                    return this.Children[0];

                default:
                    throw new InvalidOperationException("Invalid exclusion mode");
            }
        }
Exemple #6
0
 internal KnowledgeBaseEntry(KnowledgeBaseEntry parent, object key)
 {
     Key      = key;
     Parent   = parent;
     Children = EmptyChildren;
 }
Exemple #7
0
 /// <summary>
 /// Creates a QueryEnumerator that always returns exactly one KnowledgeBaseEntry
 /// </summary>
 /// <param name="e">The KnowledgeBaseEntry to return</param>
 public SingletonQuery(KnowledgeBaseEntry e)
 {
     Current = e;
 }
Exemple #8
0
 /// <summary>
 /// Creates a QueryEnumerator that always returns exactly one KnowledgeBaseEntry
 /// </summary>
 /// <param name="e">The KnowledgeBaseEntry to return</param>
 public SingletonQuery(KnowledgeBaseEntry e)
 {
     Current = e;
 }