Ejemplo n.º 1
0
        public bool TryMatch <T>(string input, out TokenMatch <T> match) where T : class, new()
        {
            var results = new List <TokenMatch <T> >();

            foreach (var template in templates)
            {
                log.Info("Matching: {0}", template.Name);
                try
                {
                    if (tokenizer.TryParse <T>(template, input, out var count, out var result))
                    {
                        results.Add(new TokenMatch <T>
                        {
                            Matches  = count,
                            Result   = result,
                            Template = template
                        });
                    }
                }
                catch (Exception e)
                {
                    var exception = new TokenMatcherException(e.Message, e)
                    {
                        Template = template
                    };

                    throw exception;
                }
            }

            match = results.OrderByDescending(r => r.Matches).FirstOrDefault();

            return(match != null);
        }
Ejemplo n.º 2
0
        public TokenMatcherResult Match(string input, string[] tags)
        {
            if (tags == null)
            {
                tags = new string[0];
            }

            var results = new TokenMatcherResult();

            foreach (var name in Templates.Names)
            {
                if (!Templates.TryGet(name, out var template))
                {
                    continue;
                }

                log.Verbose("Start: Matching: {0}", template.Name);

                using (new LogIndentation())
                {
                    // Check template has tags
                    if (CheckTemplateTags(template, tags) == false)
                    {
                        continue;
                    }

                    try
                    {
                        TokenizeResult result;

                        using (new LogIndentation())
                        {
                            result = tokenizer.Tokenize(template, input);
                        }

                        results.Results.Add(result);

                        log.Verbose("Match Success: {0}", result.Success);
                        log.Verbose("Total Matches: {0}", result.Tokens.Matches.Count);
                        log.Verbose("Total Errors : {0}", result.Exceptions.Count);
                    }
                    catch (Exception e)
                    {
                        var exception = new TokenMatcherException(e.Message, e)
                        {
                            Template = template
                        };

                        log.ErrorException($"Error processing template: {template.Name}", e);

                        throw exception;
                    }
                }

                log.Verbose("Finish: Matching: {0}", template.Name);
            }

            // Assign best match
            results.BestMatch = results.GetBestMatch();

            return(results);
        }