/// <summary>
        /// Finds pattern based on <see cref="Strings"/>
        /// </summary>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="ArgumentException"/>
        /// <returns>RegEx</returns>
        public RegularExpression FindPattern(CancellationToken ct = default)
        {
            if (Strings == null)
            {
                throw new ArgumentNullException($"Provided strings cannot be null. Use {nameof(LoadStrings)} to load strings to process.");
            }
            if (Strings.WordCount == 0)
            {
                throw new ArgumentException($"Provided strings cannot be empty. Use {nameof(LoadStrings)} to load strings to process.");
            }

            try
            {
                OnOperationChanged(OperationArgs.OperationTypes.Minimizing);
                var minimized = DfaMinimizer <char> .Minimize(Strings, ct);

                OnOperationChanged(OperationArgs.OperationTypes.ExtractingData);
                var transitions = minimized.GetTransitions(ct).ToList();
                ct.ThrowIfCancellationRequested();

                var info = minimized.GetAutomataInfo(ct);

                OnOperationChanged(OperationArgs.OperationTypes.Generating);
                var result = GenerateRegex(transitions, info.Item3, info.Item1, ct);

                OnOperationChanged(OperationArgs.OperationTypes.Finished);
                return(result);
            }
            catch (OperationCanceledException)
            {
                OnOperationChanged(OperationArgs.OperationTypes.Cancelled);
                return(null);
            }
        }
Example #2
0
        /// <summary>
        /// Minimizes given <paramref name="trie"/>
        /// </summary>
        /// <param name="trie">Trie to minimize</param>
        /// <param name="ct">Cancellation token</param>
        /// <exception cref="OperationCanceledException"></exception>
        public static DfaMinimizer <char> Minimize(Trie trie, CancellationToken ct = default)
        {
            var dfaMinimizer = new DfaMinimizer <char>(trie.StateCount, trie.TransitionCount, 0, trie.WordCount);

            return(dfaMinimizer.LoadTransitions(trie.GetTransitions(), ct)
                   .SetFinalState(trie.FiniteStates, ct)
                   .Process(ct));
        }