Пример #1
0
        /// <summary>
        /// Compiles a collection of transformation tables into a state machine.
        /// </summary>
        /// <param name="transformations">The transformations.</param>
        /// <returns>
        /// Compiled state machine.
        /// </returns>
        public StateMachine Compile(IEnumerable <IGlyphTransformationTable> transformations)
        {
            var builder = new StateMachineBuilder();

            foreach (var transformation in transformations)
            {
                this.CompileTransformation(transformation, builder);
            }

            return(builder.GetStateMachine());
        }
        /// <summary>
        /// Tests that multiple calls to AddPath yield correct state machine.
        /// </summary>
        /// <param name="paths">The paths.</param>
        /// <param name="expectedEntryState">Expected state of the entry.</param>
        public void TestAddPaths(ITransition[][] paths, State expectedEntryState)
        {
            var builder = new StateMachineBuilder();

            foreach (var path in paths)
            {
                builder.AddPath(path);
            }

            var result = builder.GetStateMachine();

            var expectedMachine = new StateMachine(expectedEntryState);

            Assert.IsTrue(new StateMachineEqualityComparer().Equals(expectedMachine, result));
        }
        public void UngroupPath_SimplePathWithBackTransitions_UngroupsThePath()
        {
            State state1 = new State(), state2;

            state1.Transitions.Add(new AlwaysTransition {
                TargetState = state1
            });
            var path = new ITransition[]
            {
                new SimpleTransition
                {
                    HeadShift = 1,
                    GlyphId   = 1,
                    Action    = new SubstitutionAction {
                        ReplacedGlyphCount = 1
                    },
                    LookupFlags = LookupFlags.IgnoreBaseGlyphs,
                    TargetState = state1
                },
                new SetTransition
                {
                    HeadShift  = 2,
                    GlyphIdSet = new HashSet <ushort> {
                        21, 22, 23
                    },
                    Action = new SubstitutionAction {
                        ReplacedGlyphCount = 2
                    },
                    LookupFlags = LookupFlags.IgnoreBaseGlyphs,
                    TargetState = state2 = new State(),
                },
                new AlwaysTransition
                {
                    HeadShift = 3,
                    Action    = new SubstitutionAction {
                        ReplacedGlyphCount = 3
                    },
                    LookupFlags = LookupFlags.IgnoreBaseGlyphs,
                    TargetState = new State {
                        Transitions = new ITransition[] { new AlwaysTransition {
                                                              TargetState = state2
                                                          } }
                    }
                }
            };

            var builder = new StateMachineBuilder();
            var result  = builder.UngroupPath(path).ToList();

            var expectedPaths = new IEnumerable <ITransition>[]
            {
                new ITransition[]
                {
                    new SimpleTransition
                    {
                        HeadShift = 1,
                        GlyphId   = 1,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 1
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    },
                    new SimpleTransition
                    {
                        HeadShift = 2,
                        GlyphId   = 21,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 2
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    },
                    new AlwaysTransition
                    {
                        HeadShift = 3,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 3
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    }
                },
                new ITransition[]
                {
                    new SimpleTransition
                    {
                        HeadShift = 1,
                        GlyphId   = 1,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 1
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    },
                    new SimpleTransition
                    {
                        HeadShift = 2,
                        GlyphId   = 22,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 2
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    },
                    new AlwaysTransition
                    {
                        HeadShift = 3,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 3
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    }
                },
                new ITransition[]
                {
                    new SimpleTransition
                    {
                        HeadShift = 1,
                        GlyphId   = 1,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 1
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    },
                    new SimpleTransition
                    {
                        HeadShift = 2,
                        GlyphId   = 23,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 2
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    },
                    new AlwaysTransition
                    {
                        HeadShift = 3,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 3
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    }
                }
            };

            var comparer = new PathEqualityComparer();

            Assert.IsTrue(comparer.Equals(expectedPaths[0], result[0]));
            Assert.IsTrue(comparer.Equals(expectedPaths[1], result[1]));
            Assert.IsTrue(comparer.Equals(expectedPaths[2], result[2]));
            Assert.AreEqual(expectedPaths.Length, result.Count);

            Assert.AreSame(result[0].First().TargetState.Transitions.First().TargetState, result[0].First().TargetState);
            Assert.AreSame(result[2].ElementAt(2).TargetState.Transitions.First().TargetState, result[2].ElementAt(1).TargetState);
        }
        public void UngroupPath_SimplePath_UngroupsThePath()
        {
            var path = new ITransition[]
            {
                new SimpleTransition
                {
                    HeadShift = 1,
                    GlyphId   = 1,
                    Action    = new SubstitutionAction {
                        ReplacedGlyphCount = 1
                    },
                    LookupFlags = LookupFlags.IgnoreBaseGlyphs
                },
                new SetTransition
                {
                    HeadShift  = 2,
                    GlyphIdSet = new HashSet <ushort> {
                        21, 22, 23
                    },
                    Action = new SubstitutionAction {
                        ReplacedGlyphCount = 2
                    },
                    LookupFlags = LookupFlags.IgnoreBaseGlyphs
                },
                new AlwaysTransition
                {
                    HeadShift = 3,
                    Action    = new SubstitutionAction {
                        ReplacedGlyphCount = 3
                    },
                    LookupFlags = LookupFlags.IgnoreBaseGlyphs
                }
            };

            var builder = new StateMachineBuilder();
            var result  = builder.UngroupPath(path).ToList();

            var expectedPaths = new IEnumerable <ITransition>[]
            {
                new ITransition[]
                {
                    new SimpleTransition
                    {
                        HeadShift = 1,
                        GlyphId   = 1,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 1
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    },
                    new SimpleTransition
                    {
                        HeadShift = 2,
                        GlyphId   = 21,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 2
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    },
                    new AlwaysTransition
                    {
                        HeadShift = 3,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 3
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    }
                },
                new ITransition[]
                {
                    new SimpleTransition
                    {
                        HeadShift = 1,
                        GlyphId   = 1,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 1
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    },
                    new SimpleTransition
                    {
                        HeadShift = 2,
                        GlyphId   = 22,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 2
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    },
                    new AlwaysTransition
                    {
                        HeadShift = 3,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 3
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    }
                },
                new ITransition[]
                {
                    new SimpleTransition
                    {
                        HeadShift = 1,
                        GlyphId   = 1,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 1
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    },
                    new SimpleTransition
                    {
                        HeadShift = 2,
                        GlyphId   = 23,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 2
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    },
                    new AlwaysTransition
                    {
                        HeadShift = 3,
                        Action    = new SubstitutionAction {
                            ReplacedGlyphCount = 3
                        },
                        LookupFlags = LookupFlags.IgnoreBaseGlyphs
                    }
                }
            };

            var comparer = new PathEqualityComparer();

            Assert.IsTrue(comparer.Equals(expectedPaths[0], result[0]));
            Assert.IsTrue(comparer.Equals(expectedPaths[1], result[1]));
            Assert.IsTrue(comparer.Equals(expectedPaths[2], result[2]));
            Assert.AreEqual(expectedPaths.Length, result.Count);
        }
        public void GetStateMachine_ForkingPath_CollectsAllStatesAndTransitions()
        {
            SimpleTransition commonTransition, forkedTransition1, forkedTransition2;

            var paths = new[]
            {
                new[]
                {
                    commonTransition = new SimpleTransition
                    {
                        GlyphId   = 1,
                        HeadShift = 1,
                        Action    = new SubstitutionAction
                        {
                            ReplacedGlyphCount  = 1,
                            ReplacementGlyphIds = new ushort[] { 1 }
                        }
                    },
                    forkedTransition1 = new SimpleTransition
                    {
                        GlyphId   = 2,
                        HeadShift = 2,
                        Action    = new SubstitutionAction
                        {
                            ReplacedGlyphCount  = 2,
                            ReplacementGlyphIds = new ushort[] { 2 }
                        }
                    }
                },
                new[]
                {
                    new SimpleTransition
                    {
                        GlyphId   = 1,
                        HeadShift = 1,
                        Action    = new SubstitutionAction
                        {
                            ReplacedGlyphCount  = 1,
                            ReplacementGlyphIds = new ushort[] { 1 }
                        }
                    },
                    forkedTransition2 = new SimpleTransition {
                        GlyphId   = 3,
                        HeadShift = 3,
                        Action    = new SubstitutionAction
                        {
                            ReplacedGlyphCount  = 3,
                            ReplacementGlyphIds = new ushort[] { 3 }
                        }
                    }
                }
            };

            var builder = new StateMachineBuilder();

            foreach (var path in paths)
            {
                builder.AddPath(path);
            }

            var result = builder.GetStateMachine();

            Assert.AreEqual(result.States.Count, 4);
            Assert.IsTrue(
                new[] { commonTransition, forkedTransition1, forkedTransition2 }.ValuesEqual(result.Transitions, new TransitionNonrecursiveEqualityComparer()));
        }