예제 #1
0
        public void TestCopyCDFG()
        {
            DFG <Block> dfg1 = new DFG <Block>();

            dfg1.AddNode(new Constant(3, "a", "", false));
            dfg1.AddNode(new SetNumberVariable((VariableBlock)dfg1.Nodes[0].value, "b", ""));
            dfg1.AddNode(new Constant(6, "g", "", false));
            dfg1.AddNode(new Constant(6, "h", "", false));
            dfg1.AddNode(new BoolOP((VariableBlock)dfg1.Nodes[2].value, (VariableBlock)dfg1.Nodes[3].value, "i", BoolOPTypes.EQ, "", true));
            dfg1.FinishDFG();

            DFG <Block> dfg2 = new DFG <Block>();

            dfg2.AddNode(new Constant(6, "c", "", false));
            dfg2.AddNode(new GetNumberVariable("b", "d", "", false));
            dfg2.AddNode(new ArithOP((VariableBlock)dfg2.Nodes[0].value, (VariableBlock)dfg2.Nodes[1].value, "e", ArithOPTypes.ADD, "", false));
            dfg2.AddNode(new SetNumberVariable((VariableBlock)dfg2.Nodes[2].value, "f", ""));
            dfg2.FinishDFG();

            CDFG original = new CDFG();

            original.AddNode(new While(new Conditional((VariableBlock)dfg1.Nodes[4].value, dfg2, null)), dfg1);
            original.AddNode(null, dfg2);
            original.StartDFG = dfg1;

            CDFG copy1 = original.Copy();

            CheckCopyCDFG(original, copy1);

            CDFG copy2 = original.Copy();

            CheckCopyCDFG(original, copy2);
        }
예제 #2
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);
            }
        }
예제 #3
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);
            }
        }
예제 #4
0
        public void Run(int width, int height, CDFG graph, bool alreadyOptimized)
        {
            if (CanOptimizeCDFG(graph) && EnableOptimizations && !alreadyOptimized)
            {
                CDFG optimizedCDFG = new CDFG();
                optimizedCDFG.StartDFG = OptimizeCDFG <T>(width, height, graph, KeepRunning.Token, EnableGarbageCollection);
                optimizedCDFG.AddNode(null, optimizedCDFG.StartDFG);

                graph = optimizedCDFG;
            }

            DFG <Block>            runningGraph    = graph.StartDFG;
            Stack <IControlBlock>  controlStack    = new Stack <IControlBlock>();
            Stack <List <string> > scopedVariables = new Stack <List <string> >();

            Rectangle[] oldRectangles = null;
            bool        firstRun      = true;

            Dictionary <string, List <IDropletSource> > sumOutputtedDropelts = new Dictionary <string, List <IDropletSource> >();

            controlStack.Push(null);
            scopedVariables.Push(new List <string>());

            Schedule scheduler = new Schedule(width, height);

            scheduler.SHOULD_DO_GARBAGE_COLLECTION = EnableGarbageCollection;
            List <StaticDeclarationBlock> staticModuleDeclarations = runningGraph.Nodes.Where(node => node.value is StaticDeclarationBlock)
                                                                     .Select(node => node.value as StaticDeclarationBlock)
                                                                     .ToList();

            if (staticModuleDeclarations.Count > 0)
            {
                scheduler.PlaceStaticModules(staticModuleDeclarations);
                scopedVariables.Peek().AddRange(scheduler.NewVariablesCreatedInThisScope.Distinct());
            }

            while (runningGraph != null)
            {
                int time = scheduler.ListScheduling(runningGraph, Executor);
                scopedVariables.Peek().AddRange(scheduler.NewVariablesCreatedInThisScope.Distinct().Where(x => !x.Contains("#@#Index")));

                foreach (var item in scheduler.OutputtedDroplets)
                {
                    if (sumOutputtedDropelts.ContainsKey(item.Key))
                    {
                        sumOutputtedDropelts[item.Key].AddRange(item.Value);
                    }
                    else
                    {
                        sumOutputtedDropelts.Add(item.Key, item.Value);
                    }
                }

                List <Command>[] commandTimeline = CreateCommandTimeline(scheduler.ScheduledOperations, time);
                if (firstRun)
                {
                    bool[] usedElectrodes = GetusedElectrodes(width, height, commandTimeline, EnableSparseElectrodes);
                    StartExecutor(graph.StartDFG, scheduler.StaticModules.Select(pair => pair.Value).ToList(), usedElectrodes);
                    firstRun = false;
                }
                SendCommands(commandTimeline, ref oldRectangles, scheduler.rectanglesAtDifferentTimes);

                if (KeepRunning.IsCancellationRequested)
                {
                    return;
                }

                runningGraph.Nodes.ForEach(x => x.value.Reset());
                (runningGraph, _) = GetNextGraph(graph, runningGraph, Executor, scheduler.Variables, controlStack, scopedVariables, scheduler.FluidVariableLocations);
            }

            Executor.UpdateDropletData(sumOutputtedDropelts.Values.SelectMany(x => x.Select(y => y.GetFluidConcentrations())).ToList());
        }