Esempio n. 1
0
        public RuleEngine(IAggregatorLogger logger, string[] ruleCode, SaveMode mode, bool dryRun)
        {
            State = EngineState.Unknown;

            this.logger   = logger;
            this.saveMode = mode;
            this.DryRun   = dryRun;

            var directives = new DirectivesParser(logger, ruleCode);

            if (!directives.Parse())
            {
                State = EngineState.Error;
                return;
            }

            if (directives.Language == DirectivesParser.Languages.Csharp)
            {
                var types = new List <Type>()
                {
                    typeof(object),
                    typeof(System.Linq.Enumerable),
                    typeof(System.Collections.Generic.CollectionExtensions),
                    typeof(Microsoft.VisualStudio.Services.WebApi.IdentityRef)
                };
                var references = types.ConvertAll(t => t.Assembly).Distinct();

                var scriptOptions = ScriptOptions.Default
                                    .WithEmitDebugInformation(true)
                                    .WithReferences(references)
                                    // Add namespaces
                                    .WithImports(
                    "System",
                    "System.Linq",
                    "System.Collections.Generic",
                    "Microsoft.VisualStudio.Services.WebApi"
                    );

                this.roslynScript = CSharpScript.Create <string>(
                    code: directives.GetRuleCode(),
                    options: scriptOptions,
                    globalsType: typeof(Globals));
            }
            else
            {
                logger.WriteError($"Cannot execute rule: language is not supported.");
                State = EngineState.Error;
            }
        }
Esempio n. 2
0
        public RuleEngine(IAggregatorLogger logger, string[] ruleCode, SaveMode mode, bool dryRun)
        {
            State = EngineState.Unknown;

            this.logger   = logger;
            this.saveMode = mode;
            this.DryRun   = dryRun;

            var directives = new DirectivesParser(logger, ruleCode);

            if (!directives.Parse())
            {
                State = EngineState.Error;
                return;
            }

            if (directives.Language == DirectivesParser.Languages.Csharp)
            {
                var references = LoadReferences(directives);
                var imports    = GetImports(directives);

                var scriptOptions = ScriptOptions.Default
                                    .WithEmitDebugInformation(true)
                                    .WithReferences(references)
                                    // Add namespaces
                                    .WithImports(imports)
                ;

                this.roslynScript = CSharpScript.Create <string>(
                    code: directives.GetRuleCode(),
                    options: scriptOptions,
                    globalsType: typeof(Globals));
            }
            else
            {
                logger.WriteError($"Cannot execute rule: language is not supported.");
                State = EngineState.Error;
            }
        }