Example #1
0
        private void initializePatterns()
        {
            if (_patterns == null)
            {
                _patterns = new List <PatternBase>();

                foreach (ConfigurableElement patternConfiguration in AcroniManagerConfigurationSection.Instance.Patterns)
                {
                    Type patternType = ConfigurableBase.GetType(patternConfiguration.Class, typeof(PatternBase), "pattern");

                    Data.Matcher matcher = MatcherFactory.GetMatcher(patternConfiguration);

                    if (patternConfiguration.Configurations == null ||
                        patternConfiguration.Configurations.Count == 0)
                    {
                        CreatePattern(patternType, null, matcher);
                    }
                    else
                    {
                        foreach (ParameterizableConfigurationElement executionConfiguration in patternConfiguration.Configurations)
                        {
                            CreatePattern(patternType, executionConfiguration, matcher);
                        }
                    }
                }
            }
        }
Example #2
0
        public EndpointRoutingMiddleware(
            MatcherFactory matcherFactory,
            EndpointDataSource endpointDataSource,
            ILogger <EndpointRoutingMiddleware> logger,
            RequestDelegate next)
        {
            if (matcherFactory == null)
            {
                throw new ArgumentNullException(nameof(matcherFactory));
            }

            if (endpointDataSource == null)
            {
                throw new ArgumentNullException(nameof(endpointDataSource));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            _matcherFactory     = matcherFactory;
            _endpointDataSource = endpointDataSource;
            _logger             = logger;
            _next = next;
        }
Example #3
0
        public EndpointRoutingMiddleware(
            MatcherFactory matcherFactory,
            ILogger <EndpointRoutingMiddleware> logger,
            IEndpointRouteBuilder endpointRouteBuilder,
            RequestDelegate next)
        {
            if (matcherFactory == null)
            {
                throw new ArgumentNullException(nameof(matcherFactory));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (endpointRouteBuilder == null)
            {
                throw new ArgumentNullException(nameof(endpointRouteBuilder));
            }

            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            _matcherFactory = matcherFactory;
            _logger         = logger;
            _next           = next;

            _endpointDataSource = new CompositeEndpointDataSource(endpointRouteBuilder.DataSources);
        }
Example #4
0
        public DispatcherMiddleware(
            MatcherFactory matcherFactory,
            IOptions <DispatcherOptions> options,
            ILogger <DispatcherMiddleware> logger,
            RequestDelegate next)
        {
            if (matcherFactory == null)
            {
                throw new ArgumentNullException(nameof(matcherFactory));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }

            _matcherFactory = matcherFactory;
            _options        = options;
            _logger         = logger;
            _next           = next;
        }
Example #5
0
        public void MatchesNull()
        {
            var expr = ToExpression <object>(() => It.IsAny <object>()).Body;

            var matcher = MatcherFactory.CreateMatcher(expr);

            Assert.True(matcher.Matches(null));
        }
Example #6
0
        public void MatchesIfAssignableType()
        {
            var expr = ToExpression <object>(() => It.IsAny <object>()).Body;

            var matcher = MatcherFactory.CreateMatcher(expr);

            Assert.True(matcher.Matches("foo"));
        }
Example #7
0
        public void MatchesIfAssignableInterface()
        {
            var expr = ToExpression <IDisposable>(() => It.IsAny <IDisposable>()).Body;

            var matcher = MatcherFactory.CreateMatcher(expr);

            Assert.True(matcher.Matches(new Disposable()));
        }
Example #8
0
        public void Construct_GivenAlgorithm_CreatesTheRightMatcher()
        {
            FileScanner.PatternMatching.MatcherFactory factory = new MatcherFactory();
            var patterns = new List<string>{ "a", "b", "c" };

            Assert.AreEqual(factory.Create(patterns, MatcherFactory.MatchAlgorithm.AhoCorasick).GetType().Name, "AhoMatcher");
            Assert.AreEqual(factory.Create(patterns, MatcherFactory.MatchAlgorithm.Regex).GetType().Name, "RegexMatcher");
        }
Example #9
0
        public void DoesntMatchIfNotAssignableType()
        {
            var expr = ToExpression <IFormatProvider>(() => It.IsAny <IFormatProvider>()).Body;

            var matcher = MatcherFactory.CreateMatcher(expr);

            Assert.False(matcher.Matches("foo"));
        }
Example #10
0
        public void Construct_GivenNoAlgorithm_CreatesDefaultMatcher()
        {
            FileScanner.PatternMatching.MatcherFactory factory = new MatcherFactory();
            var patterns = new List<string>{ "a", "b" };
            Assert.AreEqual(factory.Create(patterns).GetType().Name, "RegexMatcher");

            patterns = new List<string> { "a", "b", "c", "d" };
            Assert.AreEqual(factory.Create(patterns).GetType().Name, "AhoMatcher");
        }
Example #11
0
        public void Is_correctly_handled_by_MatcherFactory()
        {
            var expression      = GetExpression();
            var matchExpression = FindMatchExpression(expression);

            Debug.Assert(matchExpression != null);
            var(matcher, _) = MatcherFactory.CreateMatcher(matchExpression);
            Assert.IsType <Match <int> >(matcher);
        }
        public void Matches_several_matchers_from_params_array(object first, object second, bool shouldMatch)
        {
            var methodCallExpr = (MethodCallExpression)ToExpression <IX>(x => x.Method(It.IsAny <int>(), It.IsAny <string>())).Body;
            var expr           = methodCallExpr.Arguments.Single();
            var parameter      = typeof(IX).GetMethod("Method").GetParameters().Single();

            var(matcher, _) = MatcherFactory.CreateMatcher(expr, parameter);

            Assert.Equal(shouldMatch, matcher.Matches(new object[] { first, second }, typeof(object[])));
        }
Example #13
0
        public void MatchesIfAssignableInterface()
        {
            var expr = ToExpression <IDisposable>(() => It.IsAny <IDisposable>()).ToLambda().Body;

            var matcher = MatcherFactory.CreateMatcher(expr, false);

            matcher.Initialize(expr);

            Assert.IsTrue(matcher.Matches(new Disposable()));
        }
        public void MatchesNull()
        {
            var expr = ToExpression <object>(() => It.IsAny <object>()).ToLambda().Body;

            var matcher = MatcherFactory.CreateMatcher(expr, false);

            matcher.Initialize(expr);

            Assert.True(matcher.Matches(null));
        }
Example #15
0
        public void MatchesIfAssignableType()
        {
            var expr = ToExpression <object>(() => It.IsAny <object>()).ToLambda().Body;

            var matcher = MatcherFactory.CreateMatcher(expr, false);

            matcher.Initialize(expr);

            Assert.IsTrue(matcher.Matches("foo"));
        }
Example #16
0
        public void Construct_GivenAlgorithm_CreatesTheRightMatcher()
        {
            FileScanner.PatternMatching.MatcherFactory factory = new MatcherFactory();
            var patterns = new List <string> {
                "a", "b", "c"
            };

            Assert.AreEqual(factory.Create(patterns, MatcherFactory.MatchAlgorithm.AhoCorasick).GetType().Name, "AhoMatcher");
            Assert.AreEqual(factory.Create(patterns, MatcherFactory.MatchAlgorithm.Regex).GetType().Name, "RegexMatcher");
        }
Example #17
0
        public static Task <MatchResult> Run([OrchestrationTrigger] DurableOrchestrationContext context, TraceWriter log)
        {
            log.Info("Validation Starting");
            var input   = context.GetInput <EarningValidation>();
            var factory = MatcherFactory.CreateMatcher();

            var result = factory.Match(input.Commitments, input.Earning, input.Accounts);

            log.Info("Validation Finishing");
            return(Task.FromResult(result));
        }
Example #18
0
        public void SetupEvaluatedSuccessfully_succeeds_for_matching_values()
        {
            var seconds        = new List <string>();
            var methodCallExpr = (MethodCallExpression)ToExpression <IX>(x => x.Method(It.IsAny <int>(), Capture.In(seconds))).Body;
            var expr           = methodCallExpr.Arguments.Single();
            var parameter      = typeof(IX).GetMethod("Method").GetParameters().Single();

            var(matcher, _) = MatcherFactory.CreateMatcher(expr, parameter);

            matcher.SetupEvaluatedSuccessfully(new object[] { 42, "" }, typeof(object[]));
        }
Example #19
0
 private void Initialize()
 {
     if (this.arrayInitExpression != null)
     {
         this.matchers = this.arrayInitExpression.Expressions
                         .Select(e => MatcherFactory.CreateMatcher(e, false)).ToArray();
     }
     else
     {
         this.matchers = new IMatcher[0];
     }
 }
Example #20
0
 public ParamArrayMatcher(NewArrayExpression expression)
 {
     if (expression != null)
     {
         this.matchers = expression.Expressions
                         .Select(e => MatcherFactory.CreateMatcher(e)).ToArray();
     }
     else
     {
         this.matchers = new IMatcher[0];
     }
 }
Example #21
0
        public void Construct_GivenNoAlgorithm_CreatesDefaultMatcher()
        {
            FileScanner.PatternMatching.MatcherFactory factory = new MatcherFactory();
            var patterns = new List <string> {
                "a", "b"
            };

            Assert.AreEqual(factory.Create(patterns).GetType().Name, "RegexMatcher");

            patterns = new List <string> {
                "a", "b", "c", "d"
            };
            Assert.AreEqual(factory.Create(patterns).GetType().Name, "AhoMatcher");
        }
Example #22
0
        public void IsMatch_GivenMultipleLinesStream_AndTextWithAMatch_ReturnsTrue(MatcherFactory.MatchAlgorithm algorithm)
        {
            var matcher = factory.Create(new List<string> { "p1" }, algorithm);

            using (var stream = new MemoryStream())
            {
                var writer = new StreamWriter(stream);
                writer.WriteLine("12");
                writer.WriteLine("p13456");
                writer.WriteLine("7p189p10");
                writer.Flush();
                stream.Seek(0, SeekOrigin.Begin);

                Assert.IsTrue(matcher.IsMatch(new StreamReader(stream)));
            }
        }
Example #23
0
        private EndpointRoutingMiddleware CreateMiddleware(
            Logger <EndpointRoutingMiddleware> logger = null,
            MatcherFactory matcherFactory             = null,
            RequestDelegate next = null)
        {
            next ??= c => Task.CompletedTask;
            logger ??= new Logger <EndpointRoutingMiddleware>(NullLoggerFactory.Instance);
            matcherFactory ??= new TestMatcherFactory(true);

            var middleware = new EndpointRoutingMiddleware(
                matcherFactory,
                logger,
                new DefaultEndpointRouteBuilder(Mock.Of <IApplicationBuilder>()),
                next);

            return(middleware);
        }
        private IServiceProvider CreateServices(MatcherFactory matcherFactory)
        {
            var services = new ServiceCollection();

            if (matcherFactory != null)
            {
                services.AddSingleton <MatcherFactory>(matcherFactory);
            }

            services.AddLogging();
            services.AddOptions();
            services.AddRouting();

            var serviceProvder = services.BuildServiceProvider();

            return(serviceProvder);
        }
Example #25
0
        private EndpointRoutingMiddleware CreateMiddleware(
            Logger <EndpointRoutingMiddleware> logger = null,
            MatcherFactory matcherFactory             = null)
        {
            RequestDelegate next = (c) => Task.FromResult <object>(null);

            logger         = logger ?? new Logger <EndpointRoutingMiddleware>(NullLoggerFactory.Instance);
            matcherFactory = matcherFactory ?? new TestMatcherFactory(true);

            var middleware = new EndpointRoutingMiddleware(
                matcherFactory,
                logger,
                new DefaultEndpointRouteBuilder(Mock.Of <IApplicationBuilder>()),
                next);

            return(middleware);
        }
        private EndpointRoutingMiddleware CreateMiddleware(
            Logger <EndpointRoutingMiddleware> logger = null,
            MatcherFactory matcherFactory             = null)
        {
            RequestDelegate next = (c) => Task.FromResult <object>(null);

            logger         = logger ?? new Logger <EndpointRoutingMiddleware>(NullLoggerFactory.Instance);
            matcherFactory = matcherFactory ?? new TestMatcherFactory(true);

            var options    = Options.Create(new EndpointOptions());
            var middleware = new EndpointRoutingMiddleware(
                matcherFactory,
                new CompositeEndpointDataSource(Array.Empty <EndpointDataSource>()),
                logger,
                next);

            return(middleware);
        }
Example #27
0
        private IServiceProvider CreateServices(MatcherFactory matcherFactory)
        {
            var services = new ServiceCollection();

            if (matcherFactory != null)
            {
                services.AddSingleton <MatcherFactory>(matcherFactory);
            }

            services.AddLogging();
            services.AddOptions();
            services.AddRouting();
            var listener = new DiagnosticListener("Microsoft.AspNetCore");

            services.AddSingleton(listener);
            services.AddSingleton <DiagnosticSource>(listener);

            var serviceProvder = services.BuildServiceProvider();

            return(serviceProvder);
        }
Example #28
0
    private EndpointRoutingMiddleware CreateMiddleware(
        Logger <EndpointRoutingMiddleware> logger = null,
        MatcherFactory matcherFactory             = null,
        DiagnosticListener listener = null,
        RequestDelegate next        = null)
    {
        next ??= c => Task.CompletedTask;
        logger ??= new Logger <EndpointRoutingMiddleware>(NullLoggerFactory.Instance);
        matcherFactory ??= new TestMatcherFactory(true);
        listener ??= new DiagnosticListener("Microsoft.AspNetCore");

        var middleware = new EndpointRoutingMiddleware(
            matcherFactory,
            logger,
            new DefaultEndpointRouteBuilder(Mock.Of <IApplicationBuilder>()),
            new DefaultEndpointDataSource(),
            listener,
            next);

        return(middleware);
    }
Example #29
0
    public EndpointRoutingMiddleware(
        MatcherFactory matcherFactory,
        ILogger <EndpointRoutingMiddleware> logger,
        IEndpointRouteBuilder endpointRouteBuilder,
        EndpointDataSource rootCompositeEndpointDataSource,
        DiagnosticListener diagnosticListener,
        RequestDelegate next)
    {
        if (endpointRouteBuilder == null)
        {
            throw new ArgumentNullException(nameof(endpointRouteBuilder));
        }

        _matcherFactory     = matcherFactory ?? throw new ArgumentNullException(nameof(matcherFactory));
        _logger             = logger ?? throw new ArgumentNullException(nameof(logger));
        _diagnosticListener = diagnosticListener ?? throw new ArgumentNullException(nameof(diagnosticListener));
        _next = next ?? throw new ArgumentNullException(nameof(next));

        // rootCompositeEndpointDataSource is a constructor parameter only so it always gets disposed by DI. This ensures that any
        // disposable EndpointDataSources also get disposed. _endpointDataSource is a component of rootCompositeEndpointDataSource.
        _ = rootCompositeEndpointDataSource;
        _endpointDataSource = new CompositeEndpointDataSource(endpointRouteBuilder.DataSources);
    }
Example #30
0
        public async Task <Metric> ProcessEarning(Earning earning)
        {
            var sw = Stopwatch.StartNew();

            var cache = GetLocalCache();

            var metric = new Metric {
                Actor = $"#{_actorNumber}({earning.Ukprn})"
            };
            var payableEarnings    = new List <PayableEarning>();
            var nonPayableEarnings = new List <NonPayableEarning>();
            var key         = string.Concat(earning.Ukprn, "-", earning.LearnerReferenceNumber);
            var commitments = (List <Commitment>) await cache.Get(key);

            metric.InsideRead = sw.ElapsedTicks;
            sw.Restart();

            //if (commitments != null && commitments.Count > 0)
            //{
            // compare
            var matcher     = MatcherFactory.CreateMatcher();
            var accounts    = new List <Account>();
            var matchResult = matcher.Match(commitments, earning, accounts);
            var payable     = matchResult.ErrorCodes.Count > 0;

            // create (non)payable earning
            if (payable)
            {
                payableEarnings.Add(new PayableEarning
                {
                    Commitment = commitments[0],
                    Earning    = earning
                });
            }
            else
            {
                nonPayableEarnings.Add(new NonPayableEarning
                {
                    Earning = earning,
                    Errors  = matchResult.ErrorCodes
                });
            }
            //}
            //else
            //{
            //    nonPayableEarnings.Add(new NonPayableEarning
            //    {
            //        Earning = earning,
            //        Errors = new[] { "DLOCK_02" }
            //    });
            //}

            metric.InsideCalc = sw.ElapsedTicks;
            sw.Restart();

            //if (commitments != null)
            //{
            commitments[0].NegotiatedPrice += 100;
            metric.Other = await cache.Update(key, commitments);

            //}

            metric.InsideWrite = sw.ElapsedTicks;
            return(metric);
        }
Example #31
0
 public Searcher(IPreprocessor preprocessor, MatcherFactory matcherFactory)
 {
     _preprocessor   = preprocessor;
     _matcherFactory = matcherFactory;
 }
Example #32
0
 public void Match_GivenTextWithNoMatches_ReturnsNull(MatcherFactory.MatchAlgorithm algorithm,
                                                      List<string> patterns, String testString)
 {
     var matcher = factory.Create(patterns, algorithm);
     Assert.IsNull(matcher.Match(new StringReader(testString)));
 }
Example #33
0
        private void FindMatches(StreamReader streamReader, IEnumerable <string> phrases)
        {
            var matcher = new MatcherFactory().Create(phrases.ToList());

            _matches = matcher.Matches(streamReader);
        }
Example #34
0
 public void Construct_GivenNoPatterns_ThrowsArgumentException()
 {
     FileScanner.PatternMatching.MatcherFactory factory = new MatcherFactory();
     factory.Create(new List<string>());
 }
Example #35
0
 public void Matches_GivenMultipleLinesSring_AndTextWithAMatch_ReturnsListWithTheMatch_CountingTheNewline(MatcherFactory.MatchAlgorithm algorithm)
 {
     var matcher = factory.Create(new List<string> { "p1" }, algorithm);
     var text = "12\r\np13456\r\n7p189p10";
     var expectedMatches = new List<Match> { new Match(4, "p1"), new Match(13, "p1"), new Match(17, "p1") };
     Assert.IsTrue(Enumerable.SequenceEqual(matcher.Matches(new StringReader(text)), expectedMatches));
 }
Example #36
0
        public void Matches_GivenMultipleLinesStream_AndTextWithAMatch_ReturnsListWithTheMatch_CountingTheNewline(MatcherFactory.MatchAlgorithm algorithm)
        {
            var matcher = factory.Create(new List<string> { "p1" }, algorithm);

            using (var stream = new MemoryStream())
            {
                var writer = new StreamWriter(stream);
                writer.WriteLine("12");
                writer.WriteLine("p13456");
                writer.WriteLine("7p189p10");
                writer.Flush();
                stream.Seek(0, SeekOrigin.Begin);

                var expectedMatches = new List<Match> { new Match(4, "p1"), new Match(13, "p1"), new Match(17, "p1") };
                Assert.IsTrue(Enumerable.SequenceEqual(matcher.Matches(new StreamReader(stream)), expectedMatches));
            }
        }
Example #37
0
 public void IsMatch_GivenMultipleLinesString_AndTextWithAMatch_ReturnsTrue(MatcherFactory.MatchAlgorithm algorithm)
 {
     var matcher = factory.Create(new List<string> { "p1" }, algorithm);
     var text = "12\r\np13456\r\n7p189p10";
     Assert.IsTrue(matcher.IsMatch(new StringReader(text)));
 }
Example #38
0
 public void Match_GivenMultipleLinesString_AndTextWithAMatch_ReturnsTheMatch_CountingTheNewline(MatcherFactory.MatchAlgorithm algorithm)
 {
     var matcher = factory.Create(new List<string> { "p1" }, algorithm);
     var text = "12\r\np13456\r\n7p189p10";
     Assert.AreEqual(matcher.Match(new StringReader(text)), new Match(4, "p1"));
 }
Example #39
0
 public ArrayMatcher(IJsonArray value, MatcherFactory matcherFactory)
 {
     _matcherFactory = matcherFactory;
     Value = value;
 }
Example #40
0
 public void Construct_GivenNoPatterns_ThrowsArgumentException()
 {
     FileScanner.PatternMatching.MatcherFactory factory = new MatcherFactory();
     factory.Create(new List <string>());
 }
Example #41
0
 public void Matches_GivenTextWithMatches_ReturnsListWithMatches(MatcherFactory.MatchAlgorithm algorithm,
                                                                 List<string> patterns, String testString,
                                                                 List<Match> expectedMatches)
 {
     var matcher = factory.Create(patterns, algorithm);
     Assert.IsTrue(Enumerable.SequenceEqual(matcher.Matches(new StringReader(testString)), expectedMatches));
 }
Example #42
0
 public void Match_GivenTextWithMatches_ReturnsMatch(MatcherFactory.MatchAlgorithm algorithm,
                                                     List<string> patterns, String testString,
                                                     Match expectedMatch)
 {
     var matcher = factory.Create(patterns, algorithm);
     Assert.AreEqual(matcher.Match(new StringReader(testString)), expectedMatch);
 }