Ejemplo n.º 1
0
        /// <summary>
        /// Registers the open data to the list of data for this consumer
        /// </summary>
        protected void AddCurrentRecord()
        {
            currentValues.Add(KeyName, results.Count);
            if (VirtualColumns.Count != 0)
            {
                Scope scope =
                    new Scope(new Dictionary <string, object>
                {
                    { "current", currentValues },
                    { "context", ImportContext.Current }
                });
                using (var session = ExpressionParser.BeginRepl(scope, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }))
                {
                    foreach (ConstConfiguration constConfiguration in VirtualColumns)
                    {
                        currentValues.Add(constConfiguration.Name,
                                          constConfiguration.ConstType == ConstType.SingleExpression
                                ? ExpressionParser.Parse(constConfiguration.ValueExpression, session)
                                : ExpressionParser.ParseBlock(constConfiguration.ValueExpression, session));
                    }
                }
            }

            DynamicResult record = new DynamicResult(currentValues);

            newData.Value = record;
            results.Add(record);
            currentValues.Clear();
        }
Ejemplo n.º 2
0
        private static IDisposable CreateScriptingSession(object target)
        {
            Scope s = new Scope(new Dictionary <string, object> {
                { "$data", target }
            });

            s.ImplicitContext = "$data";
            var context = ExpressionParser.BeginRepl(s,
                                                     (i) => DefaultCallbacks.PrepareDefaultCallbacks(i.Scope, i.ReplSession));

            return(context);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the Evaluationcontext for the current evaluation session
        /// </summary>
        /// <param name="varScope">The variablescope for this decider</param>
        /// <returns>a repl-session that can be used for processing this constraint</returns>
        protected IDisposable GetEvaluationContext(out Scope varScope)
        {
            IDisposable retVal;

            if (!parent.IsContextDriven || parent.Context.GetValueFor(this, "Variables") == null)
            {
                varScope = variables == null ? new Scope() : new Scope(variables);
                retVal   = ExpressionParser.BeginRepl(varScope, a => DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession));
                if (parent.IsContextDriven)
                {
                    parent.Context.SetValueFor(this, "Variables", varScope);
                    parent.Context.SetValueFor(this, "EVContext", retVal);
                }
            }
            else
            {
                varScope = (Scope)parent.Context.GetValueFor(this, "Variables");
                retVal   = (IDisposable)parent.Context.GetValueFor(this, "EVContext");
            }

            return(retVal);
        }
Ejemplo n.º 4
0
        private void DumpSection(IWorkbook wb, DynamicResult[] data, DumpConfiguration configuration, string sheetName)
        {
            if (configuration.Files.Count != 0)
            {
                throw new ArgumentException("No child-sections allowed in a sheet");
            }

            var scope = new Scope(true);

            scope["$sheetName"] = sheetName;
            var first = true;

            string[] keys  = null;
            int      rowId = 0;
            ISheet   shiit = wb.CreateSheet(sheetName.Replace("'", "").Replace("\"", "").Replace("/", "").Replace("\\", ""));

            using (var context = ExpressionParser.BeginRepl(scope, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }))
            {
                var badKeys = scope.Keys.ToList();
                badKeys.Add("$sheetTitle");
                foreach (var item in data)
                {
                    foreach (var key in item.Keys)
                    {
                        if (!(item[key] is DynamicResult[]))
                        {
                            scope[key] = item[key];
                        }
                    }

                    foreach (ConstConfiguration constant in configuration.Constants)
                    {
                        scope[constant.Name] = constant.ConstType == ConstType.SingleExpression
                            ? ExpressionParser.Parse(constant.ValueExpression, context)
                            : ExpressionParser.ParseBlock(constant.ValueExpression, context);
                    }

                    IRow row;
                    if (first)
                    {
                        keys = (from k in scope.Keys join b in badKeys on k equals b into bk
                                from g in bk.DefaultIfEmpty()
                                where g == null
                                select k).ToArray();
                        if (scope["$sheetTitle"] is string s && !string.IsNullOrEmpty(s))
                        {
                            row = shiit.CreateRow(rowId++);
                            SetTitleRow(row, s, keys.Length);
                        }
                        row = shiit.CreateRow(rowId++);
                        DumpRow(row, keys, (IDictionary <string, object>)null);
                        first = false;
                    }

                    row = shiit.CreateRow(rowId++);
                    DumpRow(row, keys, scope);
                }

                if (keys != null)
                {
                    for (int i = 0; i < keys.Length; i++)
                    {
                        shiit.AutoSizeColumn(i);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Dumps collected data into the given stream
        /// </summary>
        /// <param name="outputStream">the output-stream that will receive the dumped data</param>
        /// <param name="data">the data that must be dumped</param>
        /// <param name="configuration">the dumper-configuiration</param>
        /// <returns>a value indicating whether there was any data available for dumping</returns>
        public bool DumpData(Stream outputStream, DynamicResult[] data, DumpConfiguration configuration)
        {
            bool retVal = true;

            if (configuration.Files.Count > 1)
            {
                LogEnvironment.LogDebugEvent(null, "The Dump-Configuration contains more than one file. All settings but the first one are being ignored.", (int)LogSeverity.Warning, null);
            }

            CsvSettings settings = null;

            if (configuration.Files.Count != 0)
            {
                settings = new DumpRuleFile(configuration.Files[0]).ParseJsonFile <CsvSettings>();
            }

            if (settings == null)
            {
                settings = new CsvSettings();
            }

            bool first = true;

            try
            {
                using (StreamWriter writer = new StreamWriter(outputStream, Encoding.GetEncoding(settings.Encoding)))
                {
                    List <string> fields = new List <string>();
                    string        format = "";
                    var           scope  = new Scope();
                    scope["$$Format"] = new Func <string, string>(s =>
                    {
                        var rv = s;
                        foreach (var es in settings.EscapeSettings)
                        {
                            rv = Regex.Replace(rv, es.MatchRegex, es.RegexReplaceExpression, RegexOptions.CultureInvariant | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
                        }

                        return(rv);
                    });
                    using (var scriptingContext = ExpressionParser.BeginRepl(scope, i => DefaultCallbacks.PrepareDefaultCallbacks(i.Scope, i.ReplSession)))
                    {
                        foreach (DynamicResult result in data)
                        {
                            if (first)
                            {
                                first = false;
                                fields.AddRange(result.Keys);
                                fields.AddRange(from t in configuration.Constants select t.Name);
                                Dictionary <string, object> firstRow = new Dictionary <string, object>();
                                fields.ForEach(n => firstRow.Add(n, n));
                                var tmpFields = (from t in fields
                                                 join c in settings.Formattings on t equals c.ColumnName into cj
                                                 from cn in cj.DefaultIfEmpty()
                                                 join v in configuration.Constants on t equals v.Name into vj
                                                 from vn in vj.DefaultIfEmpty()
                                                 select new { ColumnName = t, CustomFormat = cn, ConstDefinition = vn }).ToArray();
                                format = string.Join(settings.CsvSeparator, from t in tmpFields select $"{settings.ValueStartCharacter}{GetColumnDefinition(t.ColumnName, t.CustomFormat, t.ConstDefinition, false)}{settings.ValueEndCharacter}");
                                var headFormat = string.Join(settings.CsvSeparator, from t in tmpFields select $"{settings.ValueStartCharacter}{GetColumnDefinition(t.ColumnName, t.CustomFormat, t.ConstDefinition, true)}{settings.ValueEndCharacter}");
                                scope["$$fmt"]    = format;
                                scope["$$hdr"]    = headFormat;
                                scope["$data"]    = firstRow;
                                scope["$$RowNum"] = -1;
                                foreach (var cst in configuration.Constants.Where(n => n.ConstType == ConstType.ExpressionBlock))
                                {
                                    ExpressionParser.ParseBlock($"fx_{cst.Name}={cst.ValueExpression}", scriptingContext);
                                    ExpressionParser.ParseBlock($"fx_{cst.Name}.ParentScope=Scope()", scriptingContext);
                                }

                                if (settings.TableHeader)
                                {
                                    writer.WriteLine(ExpressionParser.Parse("$$($$hdr)", scriptingContext));
                                }
                            }

                            ExpressionParser.Parse("$$RowNum++", scriptingContext);
                            scope["$data"] = result;
                            foreach (var cst in configuration.Constants)
                            {
                                if (cst.ConstType == ConstType.SingleExpression)
                                {
                                    ExpressionParser.Parse($"{cst.Name}={cst.ValueExpression}", scriptingContext);
                                }
                                else
                                {
                                    ExpressionParser.Parse($"{cst.Name}=fx_{cst.Name}()", scriptingContext);
                                }
                            }

                            writer.WriteLine(ExpressionParser.Parse("$$($$fmt)", scriptingContext));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogEnvironment.LogEvent($"Error: {ex.OutlineException()}", LogSeverity.Error);
                retVal = false;
            }

            return(retVal);
        }