Пример #1
0
        public async void Update(string xml)
        {
            try
            {
                //xml = xml.Replace("&lt", "<");
                //await JSExecutor.InvokeAsync<string>("alert", xml);
                //throw new Exception(xml);
                (CDFG cdfg, List <ParseException> exceptions) = XmlParser.Parse(xml);
                if (exceptions.Count == 0)
                {
                    bool optimizedCDFG = false;
                    if (Settings.CreateGraph)
                    {
                        if (ProgramExecutor <string> .CanOptimizeCDFG(cdfg) && Settings.EnableOptimizations)
                        {
                            int boardWidth  = Settings.BoardWidth;
                            int boardHeight = Settings.BoardHeight;


                            cancelSource?.Cancel();

                            cancelSource = new CancellationTokenSource();
                            CDFG newCdfg = new CDFG();
                            newCdfg.StartDFG = ProgramExecutor <string> .OptimizeCDFG <string>(boardWidth, boardHeight, cdfg, cancelSource.Token, Settings.EnableGC);

                            newCdfg.AddNode(null, newCdfg.StartDFG);

                            if (cancelSource.IsCancellationRequested)
                            {
                                return;
                            }

                            cdfg          = newCdfg;
                            optimizedCDFG = true;
                        }
                        (string nodes, string edges) = SimpleGraph.CDFGToSimpleGraph(cdfg);
                        await JSExecutor.InvokeAsync <string>("setGraph", nodes, edges);
                    }
                    await JSExecutor.InvokeAsync <string>("ClearErrors");

                    RunSimulator(cdfg, optimizedCDFG);
                }
                else
                {
                    var errorInfos = exceptions.GroupBy(e => e.ID)
                                     .Select(e => $"{{id: \"{e.Key}\", message: \"{String.Join(@"\n", e.Select(ee => ee.Message))}\"}}");
                    await JSExecutor.InvokeAsync <string>("ShowBlocklyErrors", errorInfos.ToArray());
                }
            }
            catch (ParseException e)
            {
                await JSExecutor.InvokeAsync <string>("ShowUnexpectedError", e.Message);

                Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
            }
            catch (Exception e)
            {
                await JSExecutor.InvokeAsync <string>("ShowUnexpectedError", e.Message);

                Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
            }
        }
Пример #2
0
        public void Update(string xml)
        {
            try
            {
                (CDFG cdfg, List<ParseException> exceptions) = XmlParser.Parse(xml);
                if (exceptions.Count == 0)
                {
                    string js = String.Empty;
                    bool optimizedCDFG = false;
                    if (Settings.CreateGraph)
                    {
                        if (ProgramExecutor<string>.CanOptimizeCDFG(cdfg) && Settings.EnableOptimizations)
                        {
                            int boardWidth = Settings.BoardWidth;
                            int boardHeight = Settings.BoardHeight;

                            
                            cancelSource?.Cancel();

                            cancelSource = new CancellationTokenSource();
                            CDFG newCdfg = new CDFG();
                            newCdfg.StartDFG = ProgramExecutor<string>.OptimizeCDFG<string>(boardWidth, boardHeight, cdfg, cancelSource.Token, Settings.EnableGC);
                            newCdfg.AddNode(null, newCdfg.StartDFG);

                            if (cancelSource.IsCancellationRequested)
                            {
                                return;
                            }

                            cdfg = newCdfg;
                            optimizedCDFG = true;
                        }
                        (string nodes, string edges) = SimpleGraph.CDFGToSimpleGraph(cdfg);
                        js = $"setGraph({nodes}, {edges});";
                    }
                    js += $"ClearErrors();";
                    Browser.ExecuteScriptAsync(js);

                    RunSimulator(cdfg, optimizedCDFG);
                }
                else
                {
                    var errorInfos = exceptions.GroupBy(e => e.ID)
                                               .Select(e => $"{{id: \"{e.Key}\", message: \"{String.Join(@"\n", e.Select(ee => ee.Message))}\"}}");
                    string ids = string.Join(", ", errorInfos);
                    string js = $"ShowBlocklyErrors([{ids}]);";
                    Browser.ExecuteScriptAsync(js);
                }
            }
            catch (ParseException e)
            {
                string message = $"ShowUnexpectedError(\"{e.Message.Replace('\"', ' ').Replace('\'', ' ')}\");";
                Browser.ExecuteScriptAsync(message);
                Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
            }
            catch (Exception e)
            {
                string message = $"ShowUnexpectedError(\"{e.Message.Replace('\"', ' ').Replace('\'', ' ')}\");";
                Browser.ExecuteScriptAsync(message);
                Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
            }
        }