Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        public void ConsumeElement()
        {
            StringTransducer consume = StringTransducer.ConsumeElement('x');

            StringInferenceTestUtilities.TestTransducerValue(consume, "x", string.Empty, 1.0);
            StringInferenceTestUtilities.TestTransducerValue(consume, "xx", string.Empty, 0.0);
            StringInferenceTestUtilities.TestTransducerValue(consume, string.Empty, string.Empty, 0.0);
            StringInferenceTestUtilities.TestTransducerValue(consume, string.Empty, "x", 0.0);
            StringInferenceTestUtilities.TestTransducerValue(consume, string.Empty, "xx", 0.0);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a transducer that replaces braces surrounding the given argument placeholder
        /// with <see cref="LeftBraceReplacer"/> and <see cref="RightBraceReplacer"/>.
        /// </summary>
        /// <param name="argument">The index of the argument.</param>
        /// <param name="argumentCount">The total number of the 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 GetArgumentEscapingTransducer(int argument, int argumentCount, bool transpose)
        {
            Debug.Assert(argumentCount >= 1 && argumentCount <= 10, "Up to 10 arguments currently supported.");
            Debug.Assert(argument >= 0 && argument < argumentCount, "Argument index must be less than the number of arguments.");

            List <Pair <StringTransducer, StringTransducer> > argumentToEscapingTransducers;

            if (!ArgumentCountToEscapingTransducers.TryGetValue(argumentCount, out argumentToEscapingTransducers))
            {
                argumentToEscapingTransducers = new List <Pair <StringTransducer, StringTransducer> >(argumentCount);
                ArgumentCountToEscapingTransducers[argumentCount] = argumentToEscapingTransducers;

                for (int i = 0; i < argumentCount; ++i)
                {
                    // Escapes braces in {i}
                    StringTransducer replaceBracesForDigit = StringTransducer.ConsumeElement('{');
                    replaceBracesForDigit.AppendInPlace(StringTransducer.ProduceElement(LeftBraceReplacer));
                    replaceBracesForDigit.AppendInPlace(StringTransducer.CopyElement((char)('0' + i)));
                    replaceBracesForDigit.AppendInPlace(StringTransducer.ConsumeElement('}'));
                    replaceBracesForDigit.AppendInPlace(StringTransducer.ProduceElement(RightBraceReplacer));

                    // Skips any number of placeholders which differ from {i}, with arbitrary intermediate text
                    DiscreteChar     noBraces      = DiscreteChar.UniformOver('{', '}').Complement();
                    StringTransducer braceReplacer = StringTransducer.Copy(noBraces);
                    if (argumentCount > 1)
                    {
                        // Skips every placeholder except {i}
                        StringTransducer skipOtherDigits = StringTransducer.CopyElement('{');
                        skipOtherDigits.AppendInPlace(StringTransducer.CopyElement(AllDigitsExcept(i, argumentCount - 1)));
                        skipOtherDigits.AppendInPlace(StringTransducer.CopyElement('}'));

                        braceReplacer.AppendInPlace(skipOtherDigits);
                        braceReplacer = StringTransducer.Repeat(braceReplacer, minTimes: 0);
                        braceReplacer.AppendInPlace(StringTransducer.Copy(noBraces));
                    }

                    // Skips placeholders, then escapes {i} and skips placeholders
                    StringTransducer escapeAndSkip = replaceBracesForDigit.Clone();
                    escapeAndSkip.AppendInPlace(braceReplacer);
                    StringTransducer transducer = braceReplacer.Clone();
                    transducer.AppendInPlace(escapeAndSkip);  // TODO: use Optional() here if {i} can be omitted

                    StringTransducer transducerTranspose = StringTransducer.Transpose(transducer);
                    argumentToEscapingTransducers.Add(Pair.Create(transducer, transducerTranspose));
                }
            }

            return(transpose ? argumentToEscapingTransducers[argument].Second : argumentToEscapingTransducers[argument].First);
        }
Ejemplo n.º 4
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);
        }