Ejemplo n.º 1
0
        ///
        /// A method used to precompile rules for a provided type
        ///
        public Func <T, bool> CompileRule <T>(RulesConfig rulesConfig)
        {
            // Loop through the rules and compile them against the properties of the supplied shallow object
            Expression combinedExp = null;
            var        genericType = Expression.Parameter(typeof(T));

            foreach (var rulesGroup in rulesConfig.RulesGroups)
            {
                Expression groupExpression = null;
                foreach (var rule in rulesGroup.Rules)
                {
                    Expression binaryExpression = getRuleExpression <T>(genericType, rule);
                    if (rulesGroup.RulesOperator == InterRuleOperatorType.And)
                    {
                        groupExpression = groupExpression != null?Expression.AndAlso(groupExpression, binaryExpression) : binaryExpression;
                    }
                    else // OR
                    {
                        groupExpression = groupExpression != null?Expression.OrElse(groupExpression, binaryExpression) : binaryExpression;
                    }
                }

                // Stiching the rules into 1 statement
                if (rulesConfig.RulesOperator == InterRuleOperatorType.And)
                {
                    combinedExp = combinedExp != null?Expression.AndAlso(combinedExp, groupExpression) : groupExpression;
                }
                else // OR
                {
                    combinedExp = combinedExp != null?Expression.OrElse(combinedExp, groupExpression) : groupExpression;
                }
            }

            return(Expression.Lambda <Func <T, bool> >(combinedExp ?? Expression.Constant(true), genericType).Compile());
        }
Ejemplo n.º 2
0
        private static void RegisterTypes()
        {
            _builder.RegisterType <MarkOffFileProvider>().As <IMarkOffFileProvider>();
            _builder.RegisterType <CSVMarkOffFileParser>().As <IMarkOffFileParser>();

            _builder.RegisterType <ReconcileEngine>().As <IReconcileEngine>();
            _builder.RegisterInstance(RulesConfig.RegisteredRules()).As <IList <IRuleEvaluator> >();
        }
Ejemplo n.º 3
0
 public RulesEditor(RulesConfig config) : this()
 {
     descriptionTextBox.Text = config.Description;
     foreach (var r in config.Rules)
     {
         _rules.Add(r.Clone());
     }
     _previousRulesConfig = config;
 }
Ejemplo n.º 4
0
        public void GetMatchingRules_MultiGroupAnMatch_RuleReturned()
        {
            // Arrange
            var engine = new RulesService <TestModel>(new RulesCompiler(), new LazyCache.Mocks.MockCachingService());

            // Act
            var ruleConfig =
                new RulesConfig
            {
                Id            = Guid.NewGuid(),
                RulesOperator = Rule.InterRuleOperatorType.And,
                RulesGroups   = new RulesGroup[] {
                    new RulesGroup {
                        RulesOperator = Rule.InterRuleOperatorType.Or,
                        Rules         = new[] {
                            new Rule {
                                ComparisonOperator = Rule.ComparisonOperatorType.StringStartsWith, ComparisonValue = "NOT MATCHING PREFIX", ComparisonPredicate = nameof(TestModel.TextField)
                            },
                            new Rule {
                                ComparisonOperator = Rule.ComparisonOperatorType.GreaterThan, ComparisonValue = 4.ToString(), ComparisonPredicate = nameof(TestModel.NumericField)
                            }
                        }
                    },
                    new RulesGroup {
                        RulesOperator = Rule.InterRuleOperatorType.Or,
                        Rules         = new[] {
                            new Rule {
                                ComparisonOperator = Rule.ComparisonOperatorType.StringStartsWith, ComparisonValue = "SomePrefix", ComparisonPredicate = nameof(TestModel.TextField)
                            },
                            new Rule {
                                ComparisonOperator = Rule.ComparisonOperatorType.GreaterThan, ComparisonValue = 55.ToString(), ComparisonPredicate = nameof(TestModel.NumericField)
                            }
                        }
                    }
                }
            };
            var text = ruleConfig.ToJson();
            var deserializedRules = FromJson(text);

            var matching = engine.GetMatchingRules(
                new TestModel {
                TextField = "SomePrefixBlahBlah", NumericField = 10
            },
                new[] { deserializedRules });

            // Assert
            Assert.Single(matching.Data);
        }
Ejemplo n.º 5
0
 private void RulesConfigListener(RulesConfig rulesConfig, string name)
 {
     lock (_lockObjectRules)
     {
         try
         {
             _rules.Clear();
             _rules.AddRange(rulesConfig.Rules.Select(x => x.GetInternalRule(_ruleFactories.Factories)).ToList());
         }
         catch (Exception e)
         {
             _logger.LogCritical(e, e.Message);
             throw;
         }
     }
 }
Ejemplo n.º 6
0
        private static string generateFilename(RulesConfig config, string location)
        {
            if (config.Filename != null)
            {
                return(config.Filename);
            }
            var startName = config.Description;
            var path      = Path.Combine(location, startName + ".txt");
            int startInt  = 1;

            while (File.Exists(path))
            {
                path = Path.Combine(location, startName + " " + ++startInt + ".txt");
            }
            return(path);
        }
Ejemplo n.º 7
0
        public static void WriteRulesConfig(RulesConfig config, string location = null)
        {
            var filename = generateFilename(config, location ?? rulesConfigFolderFullPath);

            config.Filename = filename;

            Directory.CreateDirectory(location ?? rulesConfigFolderFullPath);

            using (var sw = new StreamWriter(filename, false))
            {
                sw.WriteLine(config.Description);
                foreach (var rule in config.Rules)
                {
                    sw.WriteLine(rule.Serialize());
                }
            }
        }
Ejemplo n.º 8
0
 public ProcessManager(Process p, MemoryOffset memoryOffset, RulesConfig config, int pollIntervall)
 {
     _process = p;
     if (memoryOffset.OffsetType == OffsetType.IntPointer)
     {
         _baseMemoryOffset = p.Read <int>(new IntPtr(memoryOffset.MemoryOffsetAddress));
     }
     else if (memoryOffset.OffsetType == OffsetType.LongPointer)
     {
         _baseMemoryOffset = p.Read <long>(new IntPtr(memoryOffset.MemoryOffsetAddress));
     }
     else
     {
         _baseMemoryOffset = memoryOffset.MemoryOffsetAddress;
     }
     _config        = config;
     _pollIntervall = pollIntervall;
     _nextConfig    = null;
 }
Ejemplo n.º 9
0
        private static RulesConfig readRulesConfig(string filePath)
        {
            try
            {
                RulesConfig rule = null;

                if (File.Exists(filePath))
                {
                    using (var sr = new StreamReader(filePath))
                    {
                        string row;
                        while ((row = sr.ReadLine()) != null)
                        {
                            if (string.IsNullOrWhiteSpace(row))
                            {
                                continue;
                            }
                            if (rule == null)
                            {
                                rule = new RulesConfig(row)
                                {
                                    Filename = filePath
                                }
                            }
                            ;
                            else
                            {
                                rule.Rules.Add(new MemoryRule(row));
                            }
                        }

                        sr.BaseStream.Close();
                        sr.Close();
                    }
                }
                return(rule);
            }
            catch (Exception)
            {
                return(null);
            }
        }
Ejemplo n.º 10
0
 private void run(object o, DoWorkEventArgs args)
 {
     ProcessStarted?.Invoke(this, EventArgs.Empty);
     while (!_stopCalled)
     {
         lock (_config)
         {
             foreach (var rule in _config.Rules)
             {
                 IntPtr address = new IntPtr(_baseMemoryOffset + rule.MemoryOffset64);
                 var    bytes   = _process.ReadArray <byte>(address, rule.NumBytes);
                 if (rule.Bytes == null)
                 {
                     rule.Bytes = bytes;
                     continue;
                 }
                 else
                 {
                     IChangedDataContainer changedBytes;
                     if (isBytesChangedAccordingToRule(bytes, rule, out changedBytes))
                     {
                         rule.Bytes = bytes;
                         OnLog(rule, "You");
                         MemoryChanged?.Invoke(this, new MemoryChangedEventArgs(rule, changedBytes));
                     }
                     else
                     {
                         rule.Bytes = bytes;
                     }
                 }
             }
         }
         Thread.Sleep(_pollIntervall);
         //Update config if request has been made
         if (_nextConfig != null)
         {
             _config     = _nextConfig;
             _nextConfig = null;
         }
     }
 }
Ejemplo n.º 11
0
 public void SetRulesConfig(RulesConfig config)
 {
     _nextConfig = config;
 }