/// <summary>
        /// Determine the text to be shown.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>The text</returns>
        protected override string GetText(WriterContext context)
        {
            if (context.CurrentEntity == TestSource.TestEntity)
            {
                return("Test");
            }

            // Get current entity
            var evalSettings = new EvaluationSettings
            {
                ContextEntity = context.CurrentEntity,
                TimeZoneName  = context.Settings.TimeZoneName
            };

            try
            {
                ExpressionRunResult runResult = context.ExternalServices.ExpressionRunner.Run(Expression, evalSettings);
                object oResult = runResult.Value;

                string result;
                if (oResult is IEnumerable <string> )
                {
                    result = string.Join(", ", (IEnumerable <string>)oResult);
                }
                else
                {
                    result = (string)oResult;
                }
                return(result);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
示例#2
0
        private static void TestEvaluation(TestData testData, IExpression expression)
        {
            // Prep settings
            EvaluationSettings settings = new EvaluationSettings();

            if (testData.Context != null)
            {
                settings.ContextEntity = testData.Context.Resource;
            }
            settings.TimeZoneName      = TimeZoneHelper.SydneyTimeZoneName;
            settings.ParameterResolver = name => testData.Parameters.ContainsKey(name) ? testData.Parameters[name].Value : null;

            // Do evaluate
            ExpressionRunResult result = Factory.ExpressionRunner.Run(expression, settings);
            object actual = result.Value;

            // Check result
            if (!testData.Expected.NoValue)
            {
                object expected = ReformatResult(testData.Expected.Value, testData);
                object actual2  = ReformatResult(actual, testData);

                if (!expected.Equals(actual2))
                {
                    throw new Exception(string.Format("Expected eval result {0} but actual result was {1}.", expected, actual2));
                }
            }
        }
        /// <summary>
        ///     Gets the hidden controls.
        /// </summary>
        /// <param name="contextEntity">The context entity.</param>
        /// <param name="controlsWithVisibilityCalculations"></param>
        /// <param name="compiledExpressions">The compiled expressions.</param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException">
        ///     TimeZone information must be provided in settings object, or via
        ///     RequestContext.
        /// </exception>
        private ISet <long> GetHiddenControls(IEntity contextEntity, IEnumerable <long> controlsWithVisibilityCalculations, IDictionary <long, IExpression> compiledExpressions)
        {
            var hiddenControls = new HashSet <long>();

            var evaluationSettings = new EvaluationSettings {
                ContextEntity = contextEntity
            };

            var requestContext = RequestContext.GetContext();

            if (requestContext != null)
            {
                evaluationSettings.TimeZoneName = requestContext.TimeZone;
            }

            if (evaluationSettings.TimeZoneName == null)
            {
                throw new InvalidOperationException(
                          "TimeZone information must be provided in settings object, or via RequestContext.");
            }

            // Enumerate through the controls with visibility calcs
            // and mark any without compiled expressions as hidden.
            // A control without a compiled expression indicates that something went wrong during the compile.
            foreach (var controlId in controlsWithVisibilityCalculations)
            {
                if (!compiledExpressions.ContainsKey(controlId))
                {
                    hiddenControls.Add(controlId);
                }
            }

            // Evalute any compiled expressions
            foreach (var kvp in compiledExpressions)
            {
                long        controlId  = kvp.Key;
                IExpression expression = kvp.Value;

                try
                {
                    // The control is shown if the calculation returns true
                    ExpressionRunResult result = Factory.ExpressionRunner.Run(expression, evaluationSettings);

                    if (result.Value.Equals(false))
                    {
                        hiddenControls.Add(controlId);
                    }
                }
                catch (Exception ex)
                {
                    // Something went wrong. Hide in case of error
                    hiddenControls.Add(controlId);
                    EventLog.Application.WriteError(
                        "An error occurred trying to evaluate visibility calculation for control {0}. Error {1}.",
                        controlId, ex);
                }
            }

            return(hiddenControls);
        }
示例#4
0
        /// <summary>
        /// Evaluates an expression tree.
        /// </summary>
        /// <param name="expression">The expression tree.</param>
        /// <param name="settings">Additional settings to be used in evaluation, such as the root context object, timezone info, etc.</param>
        /// <returns>The result of the evaluation.</returns>
        public ExpressionRunResult Run(IExpression expression, EvaluationSettings settings)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            Expression expressionToRun = expression as Expression;

            if (expressionToRun == null)
            {
                throw new ArgumentException("Expected instance of type Expression.", "expression");
            }
            if (expressionToRun.Root == null)
            {
                throw new ArgumentException("expression.Root");
            }

            using (EntryPointContext.AppendEntryPoint("ExprRun"))
                using (Profiler.Measure("ExpressionEngine.Run"))
                {
                    var evalContext = new EvaluationContext
                    {
                        Settings = settings,
                    };

                    object result = expressionToRun.Evaluate(evalContext);

                    var resultList = result as IEnumerable <IEntity>;
                    if (resultList != null)
                    {
                        result = resultList.ToList( );
                    }

                    ExpressionRunResult runResult = new ExpressionRunResult(result);
                    return(runResult);
                }
        }
示例#5
0
        /// <summary>
        /// Get all instances of the requested type.
        /// </summary>
        /// <param name="context">The context, such as the parent entity, from which these entities are being loaded.</param>
        /// <returns>List of entities.</returns>
        public override IEnumerable <DataElement> GetData(WriterContext context)
        {
            EvaluationSettings settings = new EvaluationSettings
            {
                ContextEntity = context.CurrentEntity,
                TimeZoneName  = context.Settings.TimeZoneName
            };
            ExpressionRunResult result = context.ExternalServices.ExpressionRunner.Run(Expression, settings);

            IEnumerable <IEntity> instances;

            if (result.Value == null)
            {
                instances = Enumerable.Empty <IEntity>();
            }
            else
            {
                instances = result.Value as IEnumerable <IEntity>;
                if (instances == null)
                {
                    IEntity instance = result.Value as IEntity;
                    if (instance != null)
                    {
                        instances = new[] { instance }
                    }
                    ;
                    else
                    {
                        throw new Exception("Expected result to be list of entities.");
                    }
                    // assert false, the cast step should ensure this, or have thrown a ParseException
                }
            }

            return(instances.Select((entity, pos) => new DataElement(entity, pos)));
        }