/// <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(); }
/// <summary> /// Runs all configured queries in the sequence they were registered in the configuration /// </summary> /// <param name="queries">the queries to run on the target</param> private void ProcessQueries(QueryConfigurationCollection queries) { Dictionary <string, object> variables = new Dictionary <string, object> { { "current", currentItem.Value }, { "parents", Parents.ToArray() } }; QueryDefinition callConfig = new QueryDefinition(); foreach (QueryConfiguration config in queries) { callConfig.Query = config.Query; callConfig.Source = config.Source; callConfig.Targets = (from t in config.Targets select new TargetDefinition { TargetName = t.TargetName, RegisterAs = t.RegisterName }).ToArray(); callConfig.Parameters = (from t in config.Parameters select new ParameterDefinition { ParameterName = t.ParameterName, ParameterValue = ExpressionParser.Parse(t.ParameterExpression, variables, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }) }).ToArray(); currentCallback.Value(callConfig); } }
/// <summary> /// Resolves an expression-resolved ColumnMapping /// </summary> /// <param name="expression">the expression that was provided for the mapping</param> /// <param name="value">the value of the provided columnname</param> /// <returns>the result of the Expression</returns> private static object ProcessResolveExpression(string expression, object value) { Dictionary <string, object> variables = new Dictionary <string, object> { { "value", value } }; var retVal = ExpressionParser.Parse(expression, variables, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }); return(retVal); }
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); }
public void TestImplicitObject() { Dictionary <string, object> tmp1 = new Dictionary <string, object> { { "TestKey", "TestVal" } }; ExpressionParser.Parse("TestKey = 42", (object)tmp1, s => DefaultCallbacks.PrepareDefaultCallbacks(s.Scope, s.ReplSession)); Assert.AreEqual(tmp1["TestKey"], 42); var test = new TestClass(); test.TestString = "TestVal"; ExpressionParser.Parse("TestString = \"The Answer is 42\"", test, s => DefaultCallbacks.PrepareDefaultCallbacks(s.Scope, s.ReplSession)); Assert.AreEqual(test.TestString, "The Answer is 42"); ExpressionParser.Parse("TestString = \"The Answer is 42\"", (object)tmp1, s => DefaultCallbacks.PrepareDefaultCallbacks(s.Scope, s.ReplSession)); Assert.IsFalse(tmp1.ContainsKey("TestString")); }
/// <summary> /// Indicates whether the current Log-string needs to be processed by this Logger instance /// </summary> /// <param name="contextString">the context string that was provided by the log-sender</param> /// <returns>a value indicating whether the current message is supposed to be processed using this logger</returns> private bool Loggable(string contextString) { bool retVal = false; bool isContextMessage = !string.IsNullOrEmpty(contextString); bool isContextLogger = !string.IsNullOrEmpty(ContextFilter); retVal = isContextMessage == isContextLogger; if (isContextMessage && retVal) { retVal = (bool)ExpressionParser.Parse(ContextFilter, new Dictionary <string, object> { { "context", contextString } }, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }); } return(retVal); }
/// <summary> /// Excecutes a Script /// </summary> /// <param name="variables">the initial variables for the script</param> /// <param name="prepareVariables">a callback that will be used in order to initialize lazy-evaluation variable values</param> /// <returns>the return value that was generated by the script</returns> public TOutput Execute(IDictionary <string, object> variables, InitializeScopeVariables prepareVariables) { ScriptVisitor visitor; InitializeScopeVariables dff = prepareVariables ?? (a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }); InitializeScopeVariables newInitializer = args => { PreparePowerCalls(args.Scope, prepareVariables, args.Visitor); dff(args); }; using (var session = InterpreterBuffer.GetReplInstance(variables, newInitializer, out visitor)) { // todo: reactivate return(Execute(session)); } }
/// <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); }
/// <summary> /// Gets an existing free repl session or creates a new one if required /// </summary> /// <param name="baseValues">the base values used to initialize the repl - session</param> /// <param name="scopeInitializer">a callback that is used to prepare the scope variables for the scripting</param> /// <param name="visitorInstance">the visitor instance that is capable to process expressions and scripts</param> /// <returns>a runner item that can be used to control the repl session</returns> public static IDisposable GetReplInstance(IDictionary <string, object> baseValues, InitializeScopeVariables scopeInitializer, out ScriptVisitor visitorInstance) { bool simpleInit = (baseValues is Scope); lock (runners) { RunnerItem retVal = runners.FirstOrDefault(n => !simpleInit && n.Lock()); if (retVal == null) { ScriptVisitor visitor = simpleInit ? new ScriptVisitor((Scope)baseValues) : new ScriptVisitor(); retVal = new RunnerItem(visitor, simpleInit); retVal.Lock(); if (visitor.Reactivateable) { runners.Add(retVal); } } visitorInstance = (ScriptVisitor)retVal.Visitor; if (!simpleInit) { visitorInstance.ClearScope(baseValues); } InitializeScopeVariables dff = scopeInitializer ?? (a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }); visitorInstance.Prepare(dff); return(retVal); } }
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); } } } }
/// <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); }
/// <summary> /// Dumps a single section of data /// </summary> /// <param name="writer">the target writer</param> /// <param name="data">the dumped data</param> /// <param name="configuration">the dumper configuration</param> /// <param name="scope">the scope that is used for variable exposition</param> private bool DumpSection(StreamWriter writer, DynamicResult[] data, DumpConfiguration configuration, Scope scope) { bool retVal = false; foreach (DynamicResult result in data) { foreach (string key in result.Keys) { if (!(result[key] is DynamicResult[])) { scope[key] = result[key]; } } foreach (ConstConfiguration constant in configuration.Constants) { scope[constant.Name] = constant.ConstType == ConstType.SingleExpression ? ExpressionParser.Parse(constant.ValueExpression, scope, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }) : ExpressionParser.ParseBlock(constant.ValueExpression, scope, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }); } foreach (DumpFormatFile file in configuration.Files) { retVal = true; writer.Write(scope.FormatText(GetFileText(file))); foreach (DumpConfiguration child in file.Children) { scope.OpenInnerScope(); try { DumpSection(writer, result[child.Source] as DynamicResult[], child, scope); } finally { scope.CollapseScope(); } } } } return(retVal); }
/// <summary> /// Parses a value when it matches a specified pattern /// </summary> /// <param name="currentField">the current field that was read from a csv</param> private void TryParseRaw(CsvDataRecord currentField) { try { if (!string.IsNullOrEmpty(currentField.RawText)) { var conversion = importConfiguration.TypeConversions.FirstOrDefault(n => Regex.IsMatch(currentField.RawText, n.RawValuePattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline)); if (conversion != null) { currentField.Converted = ExpressionParser.Parse(conversion.ParseExpression, currentField, i => DefaultCallbacks.PrepareDefaultCallbacks(i.Scope, i.ReplSession)); } } } catch (Exception ex) { LogEnvironment.LogEvent($"Unable to parse the value {currentField.RawText}. {ex.Message}", LogSeverity.Warning); } }
/// <summary> /// Consumes the provided text and returns the initial character position of recognition /// </summary> /// <param name="text">the text that was extracted from a TextSource</param> /// <param name="logCallback">a callback that can be used to log events on the parser-process</param> /// <param name="getNotification">a callback that will provide an acceptance parameter for the current parsing-unit</param> /// <returns>a value indicating whether the data was successfully processed</returns> protected override bool Consume(string text, LogParserEventCallback <string> logCallback, out Func <DynamicResult, TextAcceptanceCallbackParameter> getNotification) { getNotification = null; int acceptedLength = -1; int newLineAddition = 0; if (text.EndsWith(Environment.NewLine)) { newLineAddition = Environment.NewLine.Length; text = text.Substring(0, text.Length - newLineAddition); } Dictionary <string, object> variables = new Dictionary <string, object>(); foreach (RegexWrapper cfg in regexes) { Match m = cfg.Regex.Match(text); if (m.Success) { if (cfg.Start) { if (HasOpenValues) { LogEnvironment.LogDebugEvent(null, "Entity may be incomplete! Please check Configuration (Maybe an inappropriate Starter-Regex is configured)", (int)LogSeverity.Warning, null); if (!ignoreWarnedResults) { AddCurrentRecord(); } else { DismissCurrentRecord(text); } } } acceptedLength = m.Length + newLineAddition; foreach (string groupName in cfg.Regex.GetGroupNames()) { ColumnConfiguration col = config.Columns[groupName]; if (col != null) { string val = m.Groups[groupName].Value; object dbValue = val; if (!string.IsNullOrEmpty(col.ConvertExpression)) { variables.Clear(); variables.Add("value", val); dbValue = ExpressionParser.Parse(col.ConvertExpression, variables, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }); } SetValueOfColumn(col.TableColumn, dbValue); } } bool ok = m.Index == 0 && acceptedLength >= text.Length; if (cfg.End) { if (!HasOpenValues) { LogEnvironment.LogDebugEvent(null, "Preventing empty record from being registered! Please check Configuration.", (int)LogSeverity.Warning, null); //callback(new TextAcceptanceCallbackParameter(false, 0, 0, "", null)); return(false); } if (!ignoreWarnedResults || ok) { AddCurrentRecord(); } else { DismissCurrentRecord(text); } } getNotification = t => new TextAcceptanceCallbackParameter(true, m.Index, acceptedLength, config.Name, t, text); return(true); } } return(false); }
public static void InitializeConstraints(IConstraintFactory constraintFactory, PluginFactory pluginFactory, DbContext entityContext) { var constraintTypes = entityContext.Set <ConstraintDefinition>(); Dictionary <string, object> emptyDict = new Dictionary <string, object> { { "Ask", Ask.Value }, { "AskType", typeof(Ask) } }; foreach (var constraintType in constraintTypes) { Type targetType = (Type)ExpressionParser.Parse(constraintType.ConstraintType, emptyDict, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }); ConstraintConstructor cst = new ConstraintConstructor(targetType, constraintType.ConstraintIdentifier); constraintType.Parameters.OrderBy(n => n.ParameterOrder) .ForEach( n => cst.Parameters.Add( new ConstructorParameter(ExpressionParser.Parse(n.ParameterValue, emptyDict, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }), (Type)ExpressionParser.Parse(n.ParameterType, emptyDict, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); })))); constraintFactory.RegisterType(cst); } var dc = new DeciderContext { PluginFactory = pluginFactory, ConstraintFactory = constraintFactory, DbContext = entityContext }; factory2Decisions.TryAdd(constraintFactory, dc); context2Decisions.TryAdd(entityContext, dc); constraintFactory.Disposed += UnRegisterFactory; }
public void NativeTests() { var test = new TestClass(); NativeScriptHelper.AddReference("DUMMY", "--ROSLYN--"); ExpressionParser.Parse("TestString = `E(\"A\" as hicks -> DUMMY)::\"return Global.GoFuckYourself();\" with {GoFuckYourself:function(){return \"hallo\";}}", test, s => DefaultCallbacks.PrepareDefaultCallbacks(s.Scope, s.ReplSession)); Assert.AreEqual(test.TestString, "hallo"); ExpressionParser.Parse(@"TestString = `E(#DUMMY)::@# int a = -1; for (int i = 0; i< 100000; i++){ a += 10; a = a%71; } return Global.GoFuckYourself(a); # with {GoFuckYourself:function(val){return 'System.String'.Format(""velo{0}"",val);}}", test, s => DefaultCallbacks.PrepareDefaultCallbacks(s.Scope, s.ReplSession)); Assert.AreEqual(test.TestString, "velo35"); }