Example #1
0
        private ExpressionResultPair GetTail(string tailName)
        {
            Utility.AssertNotNull(tailName, "name");
            ExpressionResultPair pair = null;

            _myNameNodeMap.TryGetValue(tailName, out pair);
            return(pair);
        }
Example #2
0
        /// <summary>
        /// Called by an expression when it references another expression in the engine
        /// </summary>
        /// <param name="tailName"></param>
        /// <param name="context"></param>
        internal void AddDependency(string tailName, ExpressionContext context)
        {
            ExpressionResultPair actualTail = this.GetTail(tailName);
            string headName = context.CalcEngineExpressionName;
            ExpressionResultPair actualHead = this.GetTail(headName);

            // An expression could depend on the same reference more than once (ie: "a + a * a")
            _myDependencies.AddDepedency(actualTail, actualHead);
        }
Example #3
0
        public string[] GetPrecedents(string name)
        {
            ExpressionResultPair        pair       = this.GetTail(name);
            List <ExpressionResultPair> dependents = new List <ExpressionResultPair>();

            if ((pair != null))
            {
                _myDependencies.GetDirectPrecedents(pair, dependents);
            }

            return(this.GetNames(dependents));
        }
Example #4
0
        private ExpressionResultPair GetTailWithValidate(string tailName)
        {
            Utility.AssertNotNull(tailName, "name");
            ExpressionResultPair pair = this.GetTail(tailName);

            if (pair == null)
            {
                throw new ArgumentException($"No expression is associated with the name '{tailName}'");
            }
            else
            {
                return(pair);
            }
        }
Example #5
0
        public T GetResult <T>(string name)
        {
            ExpressionResultPair tail = this.GetTailWithValidate(name);

            if ((!object.ReferenceEquals(typeof(T), tail.ResultType)))
            {
                string msg = $"The result type of '{name}' ('{tail.ResultType.Name}') does not match the supplied type argument ('{typeof(T).Name}')";
                throw new ArgumentException(msg);
            }

            GenericExpressionResultPair <T> actualTail = (GenericExpressionResultPair <T>)tail;

            return(actualTail.Result);
        }
Example #6
0
        private ExpressionResultPair[] GetRootTails(string[] roots)
        {
            // No roots supplied so get everything
            if (roots.Length == 0)
            {
                return(_myDependencies.GetTails());
            }

            // Get the tail for each name
            ExpressionResultPair[] arr = new ExpressionResultPair[roots.Length];

            for (int i = 0; i <= arr.Length - 1; i++)
            {
                arr[i] = this.GetTailWithValidate(roots[i]);
            }

            return(arr);
        }
Example #7
0
        public bool Remove(string name)
        {
            ExpressionResultPair tail = this.GetTail(name);

            if (tail == null)
            {
                return(false);
            }

            ExpressionResultPair[] dependents = _myDependencies.GetDependents(tail);
            _myDependencies.Remove(dependents);

            foreach (ExpressionResultPair pair in dependents)
            {
                _myNameNodeMap.Remove(pair.Name);
            }

            return(true);
        }
Example #8
0
        internal void FixTemporaryHead(IDynamicExpression expression, ExpressionContext context, Type resultType)
        {
            Type pairType = typeof(GenericExpressionResultPair <>);

            pairType = pairType.MakeGenericType(resultType);

            ExpressionResultPair pair = (ExpressionResultPair)Activator.CreateInstance(pairType);
            string headName           = context.CalcEngineExpressionName;

            pair.SetName(headName);
            pair.SetExpression(expression);

            ExpressionResultPair oldPair = _myNameNodeMap[headName];

            _myDependencies.ReplaceDependency(oldPair, pair);
            _myNameNodeMap[headName] = pair;

            // Let the pair store the result of its expression
            pair.Recalculate();
        }
Example #9
0
        public bool HasPrecedents(string name)
        {
            ExpressionResultPair pair = this.GetTail(name);

            return((pair != null) && _myDependencies.HasPrecedents(pair));
        }
Example #10
0
        public IExpression GetExpression(string name)
        {
            ExpressionResultPair tail = this.GetTailWithValidate(name);

            return(tail.Expression);
        }
Example #11
0
        public object GetResult(string name)
        {
            ExpressionResultPair tail = this.GetTailWithValidate(name);

            return(tail.ResultAsObject);
        }
Example #12
0
        internal Type ResolveTailType(string tailName)
        {
            ExpressionResultPair actualTail = this.GetTail(tailName);

            return(actualTail.ResultType);
        }