예제 #1
0
        public static void InitializePdaProducingPdvmSimulation <TPDVM>(
            out TestGraph graph,
            out Func <int, int, IEnumerable <string> > simulate)
            where TPDVM : TestPDVM, new()
        {
            graph = new TestGraph();

            var transitionProvider = new TestTransitionProvider(graph);
            var cacheProvider      =
                new TestCacheProvider <int, int, int, TestPdaConstructingContext, TestPdaConstructingGssNode>();

            var contextProcessor = new PdaExtractingContextProcessor <int, int, int, TestEdge>();

            var pdvm       = new TPDVM();
            var simulation = new PdaConstructingPdvmSimulation(
                contextProcessor, cacheProvider, transitionProvider,
                () => pdvm);

            simulate = (startVertex, maxCyclesExpansion) =>
            {
                var start = simulation.Load(startVertex, 1, -1);
                simulation.Run();

                var finals = pdvm.Finals;
                var paths  = new List <string>();
                PdaContextDecoder.ExtractWords(
                    start,
                    context => finals.Contains(context),
                    maxCyclesExpansion,
                    rawPath =>
                {
                    var path = string.Join(" ", rawPath.Select(edge => edge.Tag));
                    paths.Add(path);

                    return(true);
                },
                    _ => { });

                foreach (var path in paths)
                {
                    Console.WriteLine(path);
                }

                using (var writer = new StreamWriter(Path.GetTempPath() + "contexts.dot"))
                {
                    DumpPda(start, writer);
                }

                return(paths);
            };
        }
예제 #2
0
        public static void InitializePdaProducingSimulation(
            Func <int, int> startStatesProvider,
            Func <int> startStackSymbolsProvider,
            out TestDPDA dpda,
            out TestGraph graph,
            out Func <int, int, IEnumerable <string> > simulate)
        {
            dpda  = new TestDPDA(startStatesProvider, startStackSymbolsProvider);
            graph = new TestGraph();

            var transitionProvider = new TestTransitionProvider(graph);
            var cacheProvider      =
                new TestCacheProvider <int, int, int, TestPdaConstructingContext, TestPdaConstructingGssNode>();

            var contextProcessor = new PdaExtractingContextProcessor <int, int, int, TestEdge>();

            var simulation = new PdaConstructingSimulation(
                dpda, contextProcessor, cacheProvider, transitionProvider);

            var finalsInternal = new HashSet <TestPdaConstructingContext>();

            simulation.OnFinalState += head => finalsInternal.Add(head.CurrentContext);

            simulate = (startVertex, maxCyclesExpansion) =>
            {
                var start = simulation.Load(startVertex);
                simulation.Run();

                var paths = new List <string>();
                PdaContextDecoder.ExtractWords(
                    start,
                    context => finalsInternal.Contains(context),
                    maxCyclesExpansion,
                    rawPath =>
                {
                    var path = string.Join(" ", rawPath.Select(edge => edge.Tag));
                    paths.Add(path);

                    return(true);
                },
                    _ => { });

                using (var writer = new StreamWriter(Path.GetTempPath() + "contexts.dot"))
                {
                    DumpPda(start, writer);
                }

                return(paths);
            };
        }