Ejemplo n.º 1
0
        public TreeNode <string> Synthesize(int demand, Params param, Context context, SynthesisParams synthesisParams)
        {
            lemmaCreationTimes = new List <long>();
            pruningTimes       = new List <long>();
            var root = new TreeNode <string>();

            lemmas            = new Lemmas();
            unSATCorePrograms = new List <TreeNode <string> >();
            var currentNode = root;


            while (true)
            {
                //currentNode = grammar.Decide(root, lemmas, context, grammar);
                currentNode = synthesisParams.grammar.Decide_AST(root, ref unSATCorePrograms, context, synthesisParams.grammar,
                                                                 synthesisParams.z3ComponentSpecs, synthesisParams.programSpec, ref lemmas, ref lemmaCounter, ref extensionCounter,
                                                                 ref pruningTimes, param, ref avg_b, ref avg_e);

                if (param.printConsole)
                {
                    root.Visualize();
                }

                //File.AppendAllText(Resources.path_results, root.ToString());
                //synthesisParams.grammar.Propogate(root, lemmas, context, synthesisParams.grammar);

                var unSATCore = CheckConflict(synthesisParams.z3ComponentSpecs, context, synthesisParams.programSpec, root, synthesisParams.grammar);

                if (unSATCore?.Count == 1)
                {
                    ;
                }
                if (unSATCore?.Count != 0)
                {
                    var stopWatch             = new Stopwatch();
                    var elapsedTime_Base      = default(long);
                    var elapsedTime_Extension = default(long);

                    if (unSATCore.First().name != root.Data)
                    {
                        var lemma         = Lemma.NewLemma(root, context);
                        var lemmaAsString = synthesisParams.grammar.CheckLemma_ByString(lemma);

                        lemmas.RemoveAll(x => synthesisParams.grammar.CheckLemma_ByString(x).Contains(lemmaAsString));
                        lemmas.Add(lemma);
                    }
                    else
                    {
                        if (param.use_base_lemmas && !param.use_extended_lemmas)
                        {
                            stopWatch.Start();

                            //creating lemma from UnSATCore
                            var lemma = AnalyzeConflict(unSATCore, synthesisParams.z3ComponentSpecs, context, root, synthesisParams.grammar);



                            //lemmas.RemoveAll(x => synthesisParams.grammar.CheckLemma(lemma, x, context));
                            lemmas.Add(lemma);

                            stopWatch.Stop();
                            elapsedTime_Base = stopWatch.ElapsedMilliseconds;
                            stopWatch.Reset();
                        }

                        if (param.use_extended_lemmas)
                        {
                            stopWatch.Start();

                            //creating unSAT Programs from UnSATCore
                            var rootOfUnSATCoreProgram = ExtractUnSATProgram(unSATCore, synthesisParams.grammarGround, context);
                            if (rootOfUnSATCoreProgram == null)
                            {
                                var lemma = Lemma.NewLemma(root, context);

                                var count = lemmas.Where(x => x.AsExpression(context) == lemma.AsExpression(context)).Count();
                                if (count == 0)
                                {
                                    lemmas.Add(lemma);
                                }
                            }
                            else
                            {
                                unSATCorePrograms.Add(rootOfUnSATCoreProgram);
                            }

                            stopWatch.Stop();
                            elapsedTime_Extension = stopWatch.ElapsedMilliseconds;
                        }
                    }
                    if (elapsedTime_Base != 0 && elapsedTime_Extension != 0)
                    {
                        lemmaCreationTimes.Add(elapsedTime_Base - elapsedTime_Extension);
                    }

                    //Console.WriteLine($"{lemmas.Count == 0} {unSATCorePrograms.Count == 0} Elapsed time base - extension: {elapsedTime_Base - elapsedTime_Extension}");

                    if (param.find_groundTruth)
                    {
                        root = BackTrack_Chronological(unSATCore, synthesisParams.grammar, currentNode, root);
                    }
                    else
                    {
                        root = BackTrack_Non_Chronological(unSATCore, synthesisParams.grammar, currentNode, root);
                    }
                }

                //if (lemmas.IsUnSAT(context))
                //    return null;

                else if (root.IsConcrete)
                {
                    if (param.find_groundTruth)
                    {
                        var program_as_string = SAT_Encode(root, context);
                        //var check = AreEqual_Concrete(program_as_string, synthesisParams.programSpec.program);
                        var check = AreEqual_Examples(synthesisParams.programSpec, root);
                        if (!check)
                        {
                            if (param.use_base_lemmas || (!param.use_base_lemmas && param.use_extended_lemmas))
                            {
                                var lemma = Lemma.NewLemma(root, context);

                                var count = lemmas.Where(x => x.AsExpression(context) == lemma.AsExpression(context)).Count();
                                if (count == 0)
                                {
                                    lemmas.Add(lemma);
                                }
                            }

                            root = BackTrack_Chronological(unSATCore, synthesisParams.grammar, currentNode, root);
                            continue;
                        }
                    }

                    var benchmark_Id = Resources.path_programSpec.Replace(".xml", $"{synthesisParams.benchmarkId}.xml");
                    Console.WriteLine($"\nConcrete progam found for benchmark {benchmark_Id}:");
                    root.Visualize();
                    Console.WriteLine($"####################################### ");

                    return(root);
                }
            }
        }