Esempio n. 1
0
        public RPN(ArrayList query, Fleet fleet)
        {
            // Define valid tokens
            operators = new ArrayList();
            operands  = new ArrayList();
            // Here we hardcode operators. Although operands takes from vehicleattribute list
            string[] op1 = { "AND", "OR", "(", ")" };         // operators and parentheses
            operators.AddRange(op1);
            string[] op2 = fleet.VehicleAttributes.ToArray(); // operands
            operands.AddRange(op2);
            // Create and instantiate a new empty Stack.
            Stack rpnStack = new Stack();

            // Apply dijkstra algorithm using a stack to convert infix to postfix notation (=rpn)
            InfixTokens.AddRange(query);
            foreach (string token in InfixTokens)
            {
                if (operands.Contains(token))
                {   // Move operands across to output
                    PostfixTokens.Add(token);
                }
                else if (token.Equals("("))
                {   // Push open parenthesis onto stack
                    rpnStack.Push(token);
                }
                else if (token.Equals(")"))
                {   // Pop all operators off the stack until the mathcing open parenthesis is found
                    while ((rpnStack.Count > 0) && !((string)rpnStack.Peek()).Equals("("))
                    {
                        PostfixTokens.Add(rpnStack.Pop());  // transfer operator to output
                        if (rpnStack.Count == 0)
                        {
                            throw new Exception("Unbalanced parenthesis");
                        }
                    }
                    if (rpnStack.Count == 0)
                    {
                        throw new Exception("Unbalanced parenthesis");
                    }
                    rpnStack.Pop(); // discard open parenthesis
                }
                else if (operators.Contains(token))
                {   // Push operand to the rpn stack after moving to output all higher or equal priority operators
                    while (rpnStack.Count > 0 && ((string)rpnStack.Peek()).Equals("AND"))
                    {
                        PostfixTokens.Add(rpnStack.Pop()); // Pop and add to output
                    }
                    rpnStack.Push(token);                  // Now put the operator onto the stack
                }
                else
                {
                    throw new Exception("Unrecognised token " + token);
                }
            }
            // Copy what's left on the rpnStack
            while (rpnStack.Count > 0)
            {   // move to the output all remaining operators
                if (((string)rpnStack.Peek()).Equals("("))
                {
                    throw new Exception("Unbalanced parenthesis");
                }
                PostfixTokens.Add(rpnStack.Pop());
            }
        }