Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / Windows Forms

Evaluation Engine 1.3

Rate me:
Please Sign up or sign in to vote.
4.79/5 (13 votes)
15 Apr 2009CPOL2 min read 40.4K   799   53  
The Evaluation Engine is a parser and interpreter that can be used to build a Business Rules Engine. It allows for mathematical and boolean expressions, operand functions, variables, variable assignment, comments, and short-circuit evaluation. A syntax editor is also included.
using System;
using System.Collections.Generic;
using System.Text;

namespace EvaluationEngine.Parser
{
    public class Tokens : IEnumerable<Token>
    {

        #region Local Variables

        // save the token objects in a sorted list
        private System.Collections.Generic.List<Token> items;

        private TokenGroup tokenGroup;  // a reference to the parent object....a group of tokens

        #endregion

        #region Public Constructor

        public Tokens(TokenGroup Group)
        {
            items = new List<Token>();
            tokenGroup = Group;
        }

        #endregion

        #region Public Properties

        public int Count
        {
            get
            {
                return items.Count;
            }
        }

        #endregion

        #region Public Methods

        public bool Add(Token tk)
        {
            items.Add(tk);

            // the token has been added to the collection....set the  token object group
            tk.TokenGroup = tokenGroup;

            // update the token group list of variables
            tokenGroup.UpdateVariables(tk);

            return true;
        }

        public void Clear()
        {
            items.Clear();
        }

        #endregion

        #region Public Indexer

        public Token this[int index]
        {
            get
            {
                return this.items[index];
            }
        }

        #endregion

        #region IEnumerable<Token> Members

        public IEnumerator<Token> GetEnumerator()
        {
            return new TokensEnumerator(items);
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new TokensEnumerator(items);
        }

        #endregion
    }


    public class TokensEnumerator : IEnumerator<Token>
    {

        #region Local Variables

        private System.Collections.Generic.List<Token> items;
        int location;

        #endregion

        #region Constructor

        public TokensEnumerator(System.Collections.Generic.List<Token> Items)
        {
            items = Items;
            location = -1;
        }

        #endregion

        #region IEnumerator<Token> Members

        public Token Current
        {
            get
            {
                if (location > 0 || location < items.Count)
                {
                    return items[location];
                }
                else
                {
                    // we are outside the bounds					
                    throw new InvalidOperationException("The enumerator is out of bounds");
                }

            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            // do nothing
        }

        #endregion

        #region IEnumerator Members

        object System.Collections.IEnumerator.Current
        {
            get
            {
                if (location > 0 || location < items.Count)
                {
                    return (object)items[location];
                }
                else
                {
                    // we are outside the bounds					
                    throw new InvalidOperationException("The enumerator is out of bounds");
                }

            }
        }

        public bool MoveNext()
        {
            location++;
            return (location < items.Count);
        }

        public void Reset()
        {
            location = -1;
        }

        #endregion
    }


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
Iran (Islamic Republic of) Iran (Islamic Republic of)
he studied MCSD (C# based 2003) and MCDBA (2005) CWNA, CWNP at Sematech
IC Programming with 8051, AVR , IC desighn with FPGA and board desigh at Contronic Co

He also worked on Wireless Low level TCP/IP Programmable Module and video motion Detection algorithm
he is student of Industrial engineering in University of Payam e noor Tehran learning about PMBOK and management systems.
He has Certificate in Advanced English (CAE) and also he studied German language in ökf österreichisches Kulturforum

Comments and Discussions