Пример #1
0
        string GetLogicalExpressionString(LogicalExpression exp)
        {
            string retVal = "";

            if (exp.HasUnary())
            {
                retVal = GetLogicalExpressionString(exp.GetUnary().GetLogicalExpression());
            }
            else if (exp.HasRelational())
            {
                RelationalOperator op       = exp.GetRelational().GetRelationalOperator();
                string             lhs      = GetArithmeticExpressionString(exp.GetRelational().GetLHSArithmeticExpression());
                string             rhs      = GetArithmeticExpressionString(exp.GetRelational().GetRHSArithmeticExpression());
                string             opString = "";

                if (op.HasEquals())
                {
                    opString = "==";
                }
                else if (op.HasNotEquals())
                {
                    opString = "!=";
                }
                else if (op.HasPartOf())
                {
                    opString = "partof";
                }
                else if (op.HasNotPartOf())
                {
                    opString = "notpartof";
                }
                else if (op.HasLessThan())
                {
                    opString = "<";
                }
                else if (op.HasLessThanOrEquals())
                {
                    opString = "<=";
                }
                else if (op.HasGreaterThan())
                {
                    opString = ">";
                }
                else if (op.HasGreaterThanOrEquals())
                {
                    opString = ">=";
                }

                retVal = lhs + " " + opString + " " + rhs;
            }
            else if (exp.HasLogical())
            {
                string lhs = GetLogicalExpressionString(exp.GetLogical().GetLHSLogicalExpression());
                string rhs = GetLogicalExpressionString(exp.GetLogical().GetRHSLogicalExpression());
                string opString;

                if (exp.GetLogical().GetLogicalOperator().HasAnd())
                {
                    opString = "and";
                }
                else //OR
                {
                    opString = "or";
                }

                retVal = lhs + " " + opString + " " + rhs;
            }
            else if (exp.HasValue())
            {
                if (exp.GetValue().HasConstant())
                {
                    if ((bool)exp.GetValue().GetConstant().GetBoolean().Value)
                    {
                        retVal = "true";
                    }
                    else
                    {
                        retVal = "false";
                    }
                }
                else
                {
                    retVal = exp.GetValue().GetVariable().GetName().Value;
                }
            }

            return(retVal);
        }
Пример #2
0
        bool EvaluateLogicalExpression(LogicalExpression exp)
        {
            bool retVal = false;

            if (exp.HasUnary())
            {
                retVal = EvaluateLogicalExpression(exp.GetUnary().GetLogicalExpression());
            }
            else if (exp.HasRelational())
            {
                RelationalOperator op  = exp.GetRelational().GetRelationalOperator();
                ConstantType       lhs = EvaluateArithmeticExpression(exp.GetRelational().GetLHSArithmeticExpression());
                ConstantType       rhs = EvaluateArithmeticExpression(exp.GetRelational().GetRHSArithmeticExpression());

                if (op.HasEquals())
                {
                    if (lhs.HasBoolean() && rhs.HasBoolean())
                    {
                        retVal = ((bool)lhs.GetBoolean().Value == (bool)rhs.GetBoolean().Value);
                    }
                    else if (lhs.HasInteger())
                    {
                        if (rhs.HasInteger())
                        {
                            retVal = ((int)lhs.GetInteger().Value == (int)rhs.GetInteger().Value);
                        }
                        else if (rhs.HasFloat2())
                        {
                            retVal = ((int)lhs.GetInteger().Value == (int)rhs.GetFloat2().Value);
                        }
                    }
                    else if (lhs.HasString2() && rhs.HasString2())
                    {
                        string lhsString = (string)lhs.GetString2().Value;
                        string rhsString = (string)rhs.GetString2().Value;

                        retVal = (lhsString == rhsString);
                    }
                    else if (lhs.HasFloat2())
                    {
                        if (rhs.HasFloat2())
                        {
                            retVal = ((float)lhs.GetFloat2().Value == (float)rhs.GetFloat2().Value);
                        }
                        else if (rhs.HasInteger())
                        {
                            retVal = ((float)lhs.GetFloat2().Value == (float)rhs.GetInteger().Value);
                        }
                    }
                }
                else if (op.HasNotEquals())
                {
                    if (lhs.HasBoolean() && rhs.HasBoolean())
                    {
                        retVal = ((bool)lhs.GetBoolean().Value != (bool)rhs.GetBoolean().Value);
                    }
                    else if (lhs.HasInteger())
                    {
                        if (rhs.HasInteger())
                        {
                            retVal = ((int)lhs.GetInteger().Value != (int)rhs.GetInteger().Value);
                        }
                        else if (rhs.HasFloat2())
                        {
                            retVal = ((int)lhs.GetInteger().Value != (int)rhs.GetFloat2().Value);
                        }
                    }
                    else if (lhs.HasString2() && rhs.HasString2())
                    {
                        retVal = (lhs.GetString2() != rhs.GetString2());
                    }
                    else if (lhs.HasFloat2())
                    {
                        if (rhs.HasFloat2())
                        {
                            retVal = ((float)lhs.GetFloat2().Value != (float)rhs.GetFloat2().Value);
                        }
                        else if (rhs.HasInteger())
                        {
                            retVal = ((float)lhs.GetFloat2().Value != (float)rhs.GetInteger().Value);
                        }
                    }
                }
                else if (op.HasPartOf())
                {
                    if (lhs.HasBoolean() && rhs.HasBoolean())
                    {
                        retVal = ((bool)lhs.GetBoolean().Value == (bool)rhs.GetBoolean().Value);
                    }
                    else if (lhs.HasInteger())
                    {
                        if (rhs.HasInteger())
                        {
                            retVal = ((int)lhs.GetInteger().Value == (int)rhs.GetInteger().Value);
                        }
                        else if (rhs.HasFloat2())
                        {
                            retVal = ((int)lhs.GetInteger().Value == (int)rhs.GetFloat2().Value);
                        }
                    }
                    else if (lhs.HasString2() && rhs.HasString2())
                    {
                        retVal = (lhs.GetString2() == rhs.GetString2());
                    }
                    else if (lhs.HasFloat2())
                    {
                        if (rhs.HasFloat2())
                        {
                            retVal = ((float)lhs.GetFloat2().Value == (float)rhs.GetFloat2().Value);
                        }
                        else if (rhs.HasInteger())
                        {
                            retVal = ((float)lhs.GetFloat2().Value == (float)rhs.GetInteger().Value);
                        }
                    }
                }
                else if (op.HasNotPartOf())
                {
                    if (lhs.HasBoolean() && rhs.HasBoolean())
                    {
                        retVal = ((bool)lhs.GetBoolean().Value != (bool)rhs.GetBoolean().Value);
                    }
                    else if (lhs.HasInteger())
                    {
                        if (rhs.HasInteger())
                        {
                            retVal = ((int)lhs.GetInteger().Value != (int)rhs.GetInteger().Value);
                        }
                        else if (rhs.HasFloat2())
                        {
                            retVal = ((int)lhs.GetInteger().Value != (int)rhs.GetFloat2().Value);
                        }
                    }
                    else if (lhs.HasString2() && rhs.HasString2())
                    {
                        retVal = (lhs.GetString2().Value != rhs.GetString2().Value);
                    }
                    else if (lhs.HasFloat2())
                    {
                        if (rhs.HasFloat2())
                        {
                            retVal = ((float)lhs.GetFloat2().Value != (float)rhs.GetFloat2().Value);
                        }
                        else if (rhs.HasInteger())
                        {
                            retVal = ((float)lhs.GetFloat2().Value != (float)rhs.GetInteger().Value);
                        }
                    }
                }
                else if (op.HasLessThan())
                {
                    if (lhs.HasBoolean() && rhs.HasBoolean())
                    {
                        throw new InvalidRuleException("Incompatible types in expression");
                    }
                    else if (lhs.HasInteger())
                    {
                        if (rhs.HasInteger())
                        {
                            retVal = ((int)lhs.GetInteger().Value < (int)rhs.GetInteger().Value);
                        }
                        else if (rhs.HasFloat2())
                        {
                            retVal = ((int)lhs.GetInteger().Value < (int)rhs.GetFloat2().Value);
                        }
                    }
                    else if (lhs.HasString2() && rhs.HasString2())
                    {
                        retVal = String.Compare(lhs.GetString2().Value, rhs.GetString2().Value, true) == -1;
                    }
                    else if (lhs.HasFloat2())
                    {
                        if (rhs.HasFloat2())
                        {
                            retVal = ((float)lhs.GetFloat2().Value < (float)rhs.GetFloat2().Value);
                        }
                        else if (rhs.HasInteger())
                        {
                            retVal = ((float)lhs.GetFloat2().Value < (float)rhs.GetInteger().Value);
                        }
                    }
                }
                else if (op.HasLessThanOrEquals())
                {
                    if (lhs.HasBoolean() && rhs.HasBoolean())
                    {
                        throw new InvalidRuleException("Incompatible types in expression");
                    }
                    else if (lhs.HasInteger())
                    {
                        if (rhs.HasInteger())
                        {
                            retVal = ((int)lhs.GetInteger().Value <= (int)rhs.GetInteger().Value);
                        }
                        else if (rhs.HasFloat2())
                        {
                            retVal = ((int)lhs.GetInteger().Value <= (int)rhs.GetFloat2().Value);
                        }
                    }
                    else if (lhs.HasString2() && rhs.HasString2())
                    {
                        int result = String.Compare(lhs.GetString2().Value, rhs.GetString2().Value);
                        retVal = (result == -1) || (result == 0);
                    }
                    else if (lhs.HasFloat2())
                    {
                        if (rhs.HasFloat2())
                        {
                            retVal = ((float)lhs.GetFloat2().Value <= (float)rhs.GetFloat2().Value);
                        }
                        else if (rhs.HasInteger())
                        {
                            retVal = ((float)lhs.GetFloat2().Value <= (float)rhs.GetInteger().Value);
                        }
                    }
                }
                else if (op.HasGreaterThan())
                {
                    if (lhs.HasBoolean() && rhs.HasBoolean())
                    {
                        throw new InvalidRuleException("Incompatible types in expression");
                    }
                    else if (lhs.HasInteger())
                    {
                        if (rhs.HasInteger())
                        {
                            retVal = ((int)lhs.GetInteger().Value > (int)rhs.GetInteger().Value);
                        }
                        else if (rhs.HasFloat2())
                        {
                            retVal = ((int)lhs.GetInteger().Value > (int)rhs.GetFloat2().Value);
                        }
                    }
                    else if (lhs.HasString2() && rhs.HasString2())
                    {
                        retVal = String.Compare(lhs.GetString2().Value, rhs.GetString2().Value, true) == 1;
                    }
                    else if (lhs.HasFloat2())
                    {
                        if (rhs.HasFloat2())
                        {
                            retVal = ((float)lhs.GetFloat2().Value > (float)rhs.GetFloat2().Value);
                        }
                        else if (rhs.HasInteger())
                        {
                            retVal = ((float)lhs.GetFloat2().Value > (float)rhs.GetInteger().Value);
                        }
                    }
                }
                else if (op.HasGreaterThanOrEquals())
                {
                    if (lhs.HasBoolean() && rhs.HasBoolean())
                    {
                        throw new InvalidRuleException("Incompatible types in expression");
                    }
                    else if (lhs.HasInteger())
                    {
                        if (rhs.HasInteger())
                        {
                            retVal = ((int)lhs.GetInteger().Value >= (int)rhs.GetInteger().Value);
                        }
                        else if (rhs.HasFloat2())
                        {
                            retVal = ((int)lhs.GetInteger().Value >= (int)rhs.GetFloat2().Value);
                        }
                    }
                    else if (lhs.HasString2() && rhs.HasString2())
                    {
                        int result = String.Compare(lhs.GetString2().Value, rhs.GetString2().Value, true);
                        retVal = (result == 1) || (result == 0);
                    }
                    else if (lhs.HasFloat2())
                    {
                        if (rhs.HasFloat2())
                        {
                            retVal = ((float)lhs.GetFloat2().Value >= (float)rhs.GetFloat2().Value);
                        }
                        else if (rhs.HasInteger())
                        {
                            retVal = ((float)lhs.GetFloat2().Value >= (float)rhs.GetInteger().Value);
                        }
                    }
                }
            }
            else if (exp.HasLogical())
            {
                bool lhs = EvaluateLogicalExpression(exp.GetLogical().GetLHSLogicalExpression());
                bool rhs = EvaluateLogicalExpression(exp.GetLogical().GetRHSLogicalExpression());

                if (exp.GetLogical().GetLogicalOperator().HasAnd())
                {
                    retVal = lhs && rhs;
                }
                else//OR
                {
                    retVal = lhs || rhs;
                }
            }
            else if (exp.HasValue())
            {
                if (exp.GetValue().HasConstant())
                {
                    retVal = exp.GetValue().GetConstant().GetBoolean().Value;
                }
                else
                {
                    string   currName = exp.GetValue().GetVariable().GetName().Value;
                    Variable variable;

                    if (m_Context.GetState().GetVariable(currName, out variable))
                    {
                        retVal = (bool)variable.GetValue();
                    }
                }
            }
            return(retVal);
        }