コード例 #1
0
        /// <summary>
        /// Creates a transducer that replaces escaped argument placeholders with the corresponding arguments.
        /// </summary>
        /// <param name="escapedArgs">The list of arguments.</param>
        /// <param name="transpose">Specifies whether the created transducer should be transposed (useful for backward message computation).</param>
        /// <returns>The created transducer.</returns>
        private static StringTransducer GetPlaceholderReplacingTransducer(IList <StringAutomaton> escapedArgs, bool transpose)
        {
            var alternatives = new List <StringTransducer>();

            for (int i = 0; i < escapedArgs.Count; ++i)
            {
                StringTransducer alternative = StringTransducer.ConsumeElement((char)('0' + i));
                alternative.AppendInPlace(StringTransducer.Produce(escapedArgs[i]), (byte)(i + 1));
                alternatives.Add(alternative);
            }

            StringTransducer result = DisallowBraceReplacersTransducer.Clone();

            result.AppendInPlace(StringTransducer.ConsumeElement(LeftBraceReplacer));
            result.AppendInPlace(StringTransducer.Sum(alternatives));
            result.AppendInPlace(StringTransducer.ConsumeElement(RightBraceReplacer));
            result = StringTransducer.Repeat(result, minTimes: 0);
            result.AppendInPlace(DisallowBraceReplacersTransducer);

            if (transpose)
            {
                result.TransposeInPlace();
            }

            return(result);
        }
コード例 #2
0
        public void Sum()
        {
            StringTransducer replace = StringTransducer.Sum(
                StringTransducer.Replace(DiscreteChar.Lower(), DiscreteChar.Digit()),
                StringTransducer.Replace(DiscreteChar.Lower(), DiscreteChar.LetterOrDigit()));

            StringInferenceTestUtilities.TestTransducerValue(replace, "hello", "123", 2.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, "w", "1337", 2.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, "w", string.Empty, 2.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, string.Empty, "17", 2.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, string.Empty, string.Empty, 2.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, "hello", "worlds", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, "hello", "WORLDS111", 1.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, "123", "worlds", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, "123", "123", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(replace, "1", string.Empty, 0.0);
        }
コード例 #3
0
        /// <summary>
        /// Creates a transducer that replaces argument placeholders with the corresponding arguments.
        /// </summary>
        /// <param name="args">The list of arguments.</param>
        /// <param name="argNames">The list of argument names.</param>
        /// <param name="forBackwardMessage">Specifies whether the created transducer should be transposed so that the backward message can be computed.</param>
        /// <param name="withGroups">Specifies whether filled in arguments should be labeled with different groups.</param>
        /// <returns>The created transducer.</returns>
        private static StringTransducer GetPlaceholderReplacingTransducer(
            IList <StringAutomaton> args, IReadOnlyList <string> argNames, bool forBackwardMessage, bool withGroups)
        {
            var alternatives = new List <StringTransducer>();

            for (int argumentIndex = 0; argumentIndex < args.Count; ++argumentIndex)
            {
                StringTransducer alternative;
                if (!forBackwardMessage)
                {
                    alternative = StringTransducer.Consume(argNames[argumentIndex]);
                    alternative.AppendInPlace(StringTransducer.Produce(args[argumentIndex]), withGroups ? argumentIndex + 1 : 0);
                }
                else
                {
                    // After transposing 'Produce' will become 'Consume',
                    // and starting from 'Consume' makes projection computations more efficeint because it allows to detect dead braches earlier
                    Debug.Assert(!withGroups, "Groups are never needed for backward message.");
                    alternative = StringTransducer.Produce(args[argumentIndex]);
                    alternative.AppendInPlace(StringTransducer.Consume(argNames[argumentIndex]));
                }

                alternatives.Add(alternative);
            }

            StringTransducer result = DisallowBracesTransducer.Clone();

            result.AppendInPlace(StringTransducer.ConsumeElement('{'));
            result.AppendInPlace(StringTransducer.Sum(alternatives));
            result.AppendInPlace(StringTransducer.ConsumeElement('}'));
            result = StringTransducer.Repeat(result, minTimes: 0);
            result.AppendInPlace(DisallowBracesTransducer);

            if (forBackwardMessage)
            {
                result = StringTransducer.Transpose(result);
            }

            return(result);
        }