예제 #1
0
        private static IPropositionalLogic <T> ParseConditionInternal(IReadOnlyList <string> condition, Func <string, T, bool> conditionChecker)
        {
            var walker = 0;

            // Will collect not fully constructed logic (ex: (e1 & ), (! )
            var incompleteLogic = new Stack <IPropositionalLogic <T> >();

            // Will point the top node of expression
            IPropositionalLogic <T> topExpression = null;

            // Will store just-evaluated complete expression
            IPropositionalLogic <T> justFullExpression = null;

            while (walker < condition.Count)
            {
                var current = condition[walker];
                walker++;

                switch (current)
                {
                case "(":
                    var insideString = GetParenthesisSurrounding(walker, condition, out var endIdx);
                    var inside       = ParseConditionInternal(insideString, conditionChecker);
                    justFullExpression = inside;
                    walker             = endIdx + 1;
                    break;

                case ")":
                    throw new InvalidOperationException("Parenthesis mismatch!");

                case "!":
                    incompleteLogic.Push(new NotLogic <T>());
                    break;

                case "|":
                    incompleteLogic.Push(new OrLogic <T>(topExpression));
                    break;

                case "&":
                    incompleteLogic.Push(new AndLogic <T>(topExpression));
                    break;

                default:
                    var stringValueLogic = new ValueLogic <T>(t => conditionChecker(current, t));
                    justFullExpression = stringValueLogic;
                    break;
                }
                if (justFullExpression == null)
                {
                    continue;
                }

                // If a full expression has appeared
                if (incompleteLogic.Count > 0)
                {
                    topExpression = incompleteLogic.Pop();
                    topExpression.SetRestExpression(justFullExpression);
                }
                else
                {
                    topExpression = justFullExpression;
                }

                // Should connect all incomplete logic
                while (incompleteLogic.Count > 0)
                {
                    var temp = incompleteLogic.Pop();
                    temp.SetRestExpression(topExpression);
                    topExpression = temp;
                }

                justFullExpression = null;
            }

            return(topExpression);
        }
예제 #2
0
 public void SetRestExpression(IPropositionalLogic <T> e2)
 {
     _e2 = e2;
 }
예제 #3
0
 public void SetRestExpression(IPropositionalLogic <T> e1)
 {
     _e1 = e1;
 }
예제 #4
0
 public OrLogic(IPropositionalLogic <T> e1)
 {
     _e1 = e1;
 }
예제 #5
0
 public void SetRestExpression(IPropositionalLogic <T> _) => throw new InvalidOperationException("Operator expected!");