Click here to Skip to main content
15,896,359 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.7K   800   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 TokenGroup
    {
        #region Local Variables

        private Tokens tokens = null;
        private Variables variables = null;

        #endregion

        #region Constructor

        public TokenGroup()
        {
            tokens = new Tokens(this);
            variables = new Variables();
        }

        #endregion

        #region Public Properties

        public Tokens Tokens
        {
            get
            {
                return tokens;
            }
        }

        public Variables Variables
        {
            get
            {
                return variables;
            }
        }

        #endregion

        #region Private/Internal Methods


        /// <summary>
        /// This method is called when we add a token to Group.Tokens.Add(tk).  This methods allows us to collect the 
        /// unique variables.
        /// </summary>
        /// <param name="tk"></param>
        internal void UpdateVariables(Token tk)
        {

            // loop through the variables in the current token
            foreach (Parser.Variable var in tk.Variables)
            {

                // check if the variable exists it the Group.Variables collection
                if (this.variables.VariableExists(var.VariableName) == false)
                {

                    // we found a new variable to add to our Group.Variables collection
                    this.variables.Add(var.VariableName);
                }

            }


        }

        #endregion

        #region Public Methods

        public bool EvaluateGroup()
        {

            string value = "";
            string ErrorMsg = "";
            bool anyFailures = false;

            // first, populate the variable values in each of the tokens
            foreach (Parser.Token tk in this.tokens)
            {
                // loop through each tokens variables
                foreach (Parser.Variable var in tk.Variables)
                {
                    if (this.variables.VariableExists(var.VariableName) == true)
                    {
                        var.VariableValue = this.variables[var.VariableName].VariableValue;
                    }
                }

                // see if we have tokens and an RPN queue
                if ((tk.TokenItems.Count > 0) && (tk.RPNQueue.Count > 0)) tk.LastErrorMessage = ""; // reset the last error message
                
                // the variables for the token have been populated; evaluate the token
                Evaluate.Evaluator eval = new EvaluationEngine.Evaluate.Evaluator(tk);

                if (eval.Evaluate(out value, out ErrorMsg) == false)
                {
                    tk.LastErrorMessage = ErrorMsg;
                    anyFailures = true;
                }

            }

            return anyFailures;
            
        }

        #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