コード例 #1
0
ファイル: LogicTest.cs プロジェクト: ruscal/myMoodServer
        public bool Evaluate(Dictionary <string, string> paramVals)
        {
            if (LogicStatement == "")
            {
                return(false);
            }
            LogicParser lp = new LogicParser(LogicStatement, paramVals);

            if (lp.Parse())
            {
                //logic passed
                _pass   = true;
                _result = Success;
                return(true);
            }
            else
            {
                if (_hasFailResult)
                {
                    // logic failed but statement passed
                    _pass   = true;
                    _result = Fail;
                    return(true);
                }
                else
                {
                    //logic failed and statement failed (had no fail result)
                    _pass   = false;
                    _result = "";
                    return(false);
                }
            }
        }
コード例 #2
0
ファイル: LogicParser.cs プロジェクト: ruscal/myMoodServer
        public bool Parse(ref int endIndex)
        {
            PrepLogic();
            // param1='3123'&param2='eerewer'&(Q3[2]==''||Q11!='123')
            if (Logic == "")
            {
                throw new ArgumentException("Cannot parse logic - no logic provided.");
            }
            // get rid of white space
            string      logic  = Logic;
            int         eindex = 0;
            LogicParser lp;
            bool        r;

            if (logic.StartsWith("("))
            {
                //need to calculate nested logic or find end bracket
                char c     = logic[1];
                int  index = 1;
                while (c != ')')
                {
                    if (c == '(')
                    {
                        //nested logic
                        lp = new LogicParser(logic.Substring(index), Params, true);
                        r  = lp.Parse(ref eindex);
                        string result = (Convert.ToInt32(r)).ToString();
                        eindex = index + eindex;
                        string nlogic = logic.Substring(0, index) + result + ((logic.Length > eindex + 1) ? logic.Substring(eindex + 1) : "");
                        logic = nlogic;
                        index = index + (result.Length - 1);
                    }
                    index++;
                    c = logic[index];
                    if (index >= logic.Length)
                    {
                        throw new ArgumentException("Could not find closing bracket.");
                    }
                }
                eindex   = index;
                lp       = new LogicParser(logic.Substring(1, (eindex - 1)), Params);
                r        = lp.Parse();
                endIndex = Logic.Length - (logic.Length - eindex);
                if (logic.Length > (endIndex + 1) && !_ignoreTrailingText)
                {
                    //has trailing text
                    string result = (Convert.ToInt32(r)).ToString();
                    string nlogic = result + logic.Substring(eindex + 1);
                    logic = nlogic;
                    lp    = new LogicParser(logic, Params);
                    return(lp.Parse());
                }
                else
                {
                    return(r);
                }
            }
            else
            {
                int index = logic.IndexOf("(");
                while (index > 0)
                {
                    lp = new LogicParser(logic.Substring(index), Params, true);
                    r  = lp.Parse(ref eindex);
                    string result = (Convert.ToInt32(r)).ToString();
                    eindex = eindex + index;
                    string nlogic = logic.Substring(0, index) + result + ((logic.Length > eindex + 1) ? logic.Substring(eindex + 1) : "");
                    logic = nlogic;
                    index = logic.IndexOf("(");
                }


                //evaluate logic
                //this should be bracket free!
                //e.g. param1=='1234'||param2!=param3||param5<=9&&param6>5

                Regex          regex = new Regex(REGEX_PREDICATE);
                MatchEvaluator me    = new MatchEvaluator(ReplacePredicate);
                logic = regex.Replace(logic, me);
                //e.g. now should be   1||0||1&&1
                regex = new Regex(REGEX_ANDCLAUSE);
                me    = new MatchEvaluator(ReplaceAndClause);
                while (logic.IndexOf('&') > 0)
                {
                    logic = regex.Replace(logic, me);
                }
                if (logic.IndexOf("||") > 0)
                {
                    logic = logic.Replace("||", "|");
                    string[] orArr = logic.Split('|');
                    foreach (string s in orArr)
                    {
                        if (s == "1")
                        {
                            return(true);
                        }
                    }
                    return(false);
                }
                else
                {
                    return(logic == "1");
                }
            }
        }