private bool Evaluate(string[] splits, List <Clause> Clauses, object?before, object?after)
        {
            bool current = false;

            var invertNextStatement = false;
            var operatorExpected    = false;

            BOOL_OPERATOR Operator = BOOL_OPERATOR.OR;

            var updated_i = 0;

            for (int i = 0; i < splits.Length; i = updated_i)
            {
                if (operatorExpected)
                {
                    Operator         = (BOOL_OPERATOR)Enum.Parse(typeof(BOOL_OPERATOR), splits[i]);
                    operatorExpected = false;
                    updated_i        = i + 1;
                }
                else
                {
                    if (splits[i].StartsWith("("))
                    {
                        //Get the substring closing this paren
                        var matchingParen = FindMatchingParen(splits, i);

                        // First remove the parenthesis from the beginning and end
                        splits[i]             = splits[i][1..];
        private static bool Operate(BOOL_OPERATOR Operator, bool first, bool second)
        {
            switch (Operator)
            {
            case BOOL_OPERATOR.AND:
                return(first && second);

            case BOOL_OPERATOR.OR:
                return(first || second);

            case BOOL_OPERATOR.XOR:
                return(first ^ second);

            case BOOL_OPERATOR.NAND:
                return(!(first && second));

            case BOOL_OPERATOR.NOR:
                return(!(first || second));

            case BOOL_OPERATOR.NOT:
                return(!first);

            default:
                return(false);
            }
        }
Example #3
0
        private static bool Evaluate(string[] splits, Dictionary <Clause, bool> ClauseResults)
        {
            bool current = false;


            var internalIndex  = 0;
            var hasNotOperator = splits[0].Replace("(", "").Replace(")", "").Equals(BOOL_OPERATOR.NOT.ToString());

            if (hasNotOperator)
            {
                internalIndex = 1;
            }

            var res = ClauseResults.Where(x => x.Key.Label == splits[internalIndex].Replace("(", "").Replace(")", ""));

            if (!(res.Count() == 1))
            {
                return(false);
            }
            if (hasNotOperator)
            {
                current = !res.First().Value;
            }
            else
            {
                current = res.First().Value;
            }

            BOOL_OPERATOR Operator = BOOL_OPERATOR.AND;

            var updated_i        = internalIndex + 1;
            var operatorExpected = true;

            for (int i = updated_i; i < splits.Length; i = updated_i)
            {
                if (operatorExpected)
                {
                    Operator         = (BOOL_OPERATOR)Enum.Parse(typeof(BOOL_OPERATOR), splits[i]);
                    operatorExpected = false;
                }
                else
                {
                    if (splits[i].StartsWith("("))
                    {
                        //Get the substring closing this paren
                        var matchingParen = FindMatchingParen(splits, i);
                        current   = Operate(Operator, current, Evaluate(splits[i..(matchingParen + 1)], ClauseResults));
Example #4
0
 /// <summary>
 ///     Try to shortcut a boolean operation
 /// </summary>
 /// <param name="current"> The current boolean state </param>
 /// <param name="operation"> The Operation </param>
 /// <returns> (If you can use a shortcut, the result of the shortcut) </returns>
 public static (bool CanShortcut, bool Value) TryShortcut(bool current, BOOL_OPERATOR operation)
 {
     // If either argument of an AND statement is false, or either argument of a NOR statement is true,
     // the result is always false and we can optimize away evaluation of next
     if ((operation == BOOL_OPERATOR.AND && !current) ||
         (operation == BOOL_OPERATOR.NOR && current))
     {
         return(true, false);
     }
     // If either argument of an NAND statement is false, or either argument of an OR statement is
     // true, the result is always true and we can optimize away evaluation of next
     if ((operation == BOOL_OPERATOR.OR && current) ||
         (operation == BOOL_OPERATOR.NAND && !current))
     {
         return(true, true);
     }
     return(false, false);
 }