Ejemplo n.º 1
0
        /// <summary>
        /// Same as Rule just with observation
        /// </summary>
        /// <param name="list"> list of atomic expressions </param>
        /// <returns>
        /// Packed observation object, and a list containing all the
        /// ConcreteObservations in the packed object
        ///  </returns>
        public static Tuple <Observation, List <ConcreteObservation> > ObservationConversion(List <AtomicExpression> list)
        {
            var stack = new Stack <object>();
            var concreteObservations = new List <ConcreteObservation>();
            var lastExpressionString = string.Empty;

            try
            {
                foreach (var expression in list)
                {
                    // variable
                    if (!string.IsNullOrEmpty(expression.Value))
                    {
                        ConcreteObservation concreteObservation = MainWindow.GetCachedObservationByName(expression.Value);
                        concreteObservations.Add(concreteObservation);

                        stack.Push(concreteObservation);
                        continue;
                    }
                    // unary operator
                    object result;
                    if (expression.Operator == "-")
                    {
                        Observation tmpEval = stack.Pop() as Observation;
                        if (tmpEval.GetType() != typeof(ConcreteObservation))
                        {
                            tmpEval = new Parenthesis(tmpEval);
                        }
                        result = expression.Evaluate(tmpEval);
                    }
                    // binary operator
                    else
                    {
                        var         p2      = stack.Pop();
                        var         p1      = stack.Pop();
                        Observation tmpEval = expression.Evaluate(p1, p2);

                        if (expression.Operator == "ILI" && (lastExpressionString == "I" || lastExpressionString == "-"))
                        {
                            tmpEval = new Parenthesis(tmpEval);
                        }

                        result = tmpEval;
                    }
                    stack.Push(result);
                    lastExpressionString = expression.Operator;
                }
                var observation = stack.Pop() as Observation;
                return(new Tuple <Observation, List <ConcreteObservation> >(observation, concreteObservations));
            }
            catch (InvalidOperationException e)
            {
                throw new ParsingException("parsing failed, reenter your expression!");
            }
        }