public void CopyElement() { StringTransducer copy = StringTransducer.CopyElement(ImmutableDiscreteChar.OneOf('a', 'b')); StringInferenceTestUtilities.TestTransducerValue(copy, "a", "a", 1.0); StringInferenceTestUtilities.TestTransducerValue(copy, "b", "b", 1.0); StringInferenceTestUtilities.TestTransducerValue(copy, "a", "b", 0.0); StringInferenceTestUtilities.TestTransducerValue(copy, "b", "a", 0.0); StringInferenceTestUtilities.TestTransducerValue(copy, string.Empty, string.Empty, 0.0); StringInferenceTestUtilities.TestTransducerValue(copy, "bb", "bb", 0.0); StringInferenceTestUtilities.TestTransducerValue(copy, "bab", "bab", 0.0); StringInferenceTestUtilities.TestTransducerValue(copy, "bab", "ba", 0.0); //// Tests that projection on CopyElement(elements) shrinks the support StringAutomaton automaton = StringAutomaton.ConstantOn(2.0, "a", "ab", "ac"); automaton = automaton.Sum(StringAutomaton.ConstantOn(1.0, "a")); automaton = automaton.Sum(StringAutomaton.Constant(2.0)); automaton = automaton.Product(StringAutomaton.Constant(3.0)); for (int i = 0; i < 2; ++i) { StringInferenceTestUtilities.TestValue(automaton, 15, "a"); StringInferenceTestUtilities.TestValue(automaton, 6.0, "b"); StringInferenceTestUtilities.TestValue(automaton, i == 0 ? 6.0 : 0.0, string.Empty); StringInferenceTestUtilities.TestValue(automaton, i == 0 ? 12.0 : 0.0, "ac", "ab"); automaton = copy.ProjectSource(automaton); } }
/// <summary> /// Initializes static members of the <see cref="StringFormatOpBase{TThis}"/> class. /// </summary> static StringFormatOpBase() { // More general behavior by default RequirePlaceholderForEveryArgument = false; var noBraces = ImmutableDiscreteChar.OneOf('{', '}').Complement(); DisallowBracesAutomaton = StringAutomaton.Constant(1.0, noBraces); DisallowBracesTransducer = StringTransducer.Copy(noBraces); // Make sure that the static constructor of TThis has been invoked so that TThis sets everything up new TThis(); }
[Trait("Category", "OpenBug")] // Test failing with AutomatonTooLarge due to determinization added to SetToProduct in change 47614. Increasing max states to 1M does not fix the issue public void PropertyInferencePerformanceTest() { Rand.Restart(777); var namesData = new[] { "Alice", "Bob", "Charlie", "Eve", "Boris", "John" }; var valueData = new[] { "sender", "receiver", "attacker", "eavesdropper", "developer", "researcher" }; var templatesData = new[] { "{0} is {1}", "{0} is known as {1}", "{1} is a role of {0}", "{0} -- {1}", "{0} aka {1}" }; var textsData = new string[10]; for (int i = 0; i < textsData.Length; ++i) { int entityIndex = Rand.Int(namesData.Length); int templateIndex = Rand.Int(templatesData.Length); textsData[i] = string.Format(templatesData[templateIndex], namesData[entityIndex], valueData[entityIndex]); } var entity = new Range(namesData.Length).Named("entity"); var template = new Range(templatesData.Length).Named("template"); var text = new Range(textsData.Length).Named("text"); var entityNames = Variable.Array <string>(entity).Named("entityNames"); entityNames[entity] = Variable.Random(StringDistribution.Capitalized()).ForEach(entity); var entityValues = Variable.Array <string>(entity).Named("entityValues"); entityValues[entity] = Variable.Random(StringDistribution.Lower()).ForEach(entity); StringDistribution templatePriorMiddle = StringDistribution.ZeroOrMore(ImmutableDiscreteChar.OneOf('{', '}').Complement()); StringDistribution templatePrior = StringDistribution.OneOf( StringDistribution.String("{0} ") + templatePriorMiddle + StringDistribution.String(" {1}"), StringDistribution.String("{1} ") + templatePriorMiddle + StringDistribution.String(" {0}")); var templates = Variable.Array <string>(template).Named("templates"); templates[template] = Variable.Random(templatePrior).ForEach(template); var texts = Variable.Array <string>(text).Named("texts"); using (Variable.ForEach(text)) { var entityIndex = Variable.DiscreteUniform(entity).Named("entityIndex"); var templateIndex = Variable.DiscreteUniform(template).Named("templateIndex"); using (Variable.Switch(entityIndex)) using (Variable.Switch(templateIndex)) { texts[text] = Variable.StringFormat(templates[templateIndex], entityNames[entityIndex], entityValues[entityIndex]); } } texts.ObservedValue = textsData; var engine = new InferenceEngine(); engine.ShowProgress = false; engine.OptimiseForVariables = new[] { entityNames, entityValues }; engine.Compiler.RecommendedQuality = QualityBand.Experimental; // TODO: get this test to work with parallel for loops. engine.Compiler.UseParallelForLoops = false; engine.NumberOfIterations = 1; ProfileAction( () => { Console.WriteLine(engine.Infer <StringDistribution[]>(entityNames)[0]); Console.WriteLine(engine.Infer <StringDistribution[]>(entityValues)[0]); }, 1); }