예제 #1
0
        public void Synthesize_WhileTrue(Params param)
        {
            using (Context context = new Context(new Dictionary <string, string>()
            {
                { "proof", "true" }
            }))
            {
                var benchmark_count = Directory.GetFiles(Resources.path_programSpec_base).Length;

                for (int benchmark_id = 1; benchmark_id <= benchmark_count; benchmark_id++)
                {
                    var stopWatch = new Stopwatch();
                    stopWatch.Start();

                    var numberOfPrograms = 1;

                    var typeSpecs         = TypeSpecBuilder.Build(Resources.path_typeSpec);
                    var programSpec       = ProgramSpecBuilder.Build(Resources.path_programSpec_x.Replace(".xml", $"{benchmark_id}.xml"), context, typeSpecs);
                    var grammar           = GrammarBuilder.Build(Resources.path_grammarSpec, typeSpecs, random, programSpec.parameters);
                    var grammarGround     = GrammarBuilder.Build(Resources.path_grammarSpec, typeSpecs, random, programSpec.parameters);
                    var z3ComponentsSpecs = ComponentSpecsBuilder.Build(Resources.path_componentSpec, context, programSpec, grammar);


                    var synthesisParams = new SynthesisParams()
                    {
                        typeSpecs        = typeSpecs,
                        programSpec      = programSpec,
                        grammar          = grammar,
                        grammarGround    = grammarGround,
                        z3ComponentSpecs = z3ComponentsSpecs,
                        benchmarkId      = benchmark_id
                    };

                    var roots = new List <TreeNode <string> >();
                    for (int i = 0; i < numberOfPrograms; i++)
                    {
                        var root = Synthesize(numberOfPrograms, param, context, synthesisParams);
                        roots.Add(root);
                    }

                    stopWatch.Stop();
                    Console.WriteLine($"Time Elapsed: {(double)stopWatch.Elapsed.TotalSeconds}");


                    if (numberOfPrograms == 1)
                    {
                        var    root       = roots.First();
                        string createText = $"{stopWatch.Elapsed.TotalSeconds.ToString()} {benchmark_id} {lemmas.Count} {unSATCorePrograms.Count} {root.Size} {avg_b.Average()} {avg_e.Average()} {SAT_Encode(root, context)}\n";
                        if (benchmark_id == 1)
                        {
                            //if (!Directory.Exists(Resources.path_results))
                            //    Directory.CreateDirectory(Resources.path_results);

                            File.WriteAllText(Resources.path_results, String.Empty);
                            File.AppendAllText(Resources.path_results, "{stopWatch.Elapsed.TotalSeconds.ToString()} {benchmark_id} {lemmas.Count} {unSATCorePrograms.Count} {root.Size} {SAT_Encode(root, context)}\n");
                        }
                        File.AppendAllText(Resources.path_results, createText);
                    }

                    if (param.debug)
                    {
                        Console.WriteLine($"Press Enter to continue");
                        Console.ReadLine();
                    }
                }
            }
        }
예제 #2
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);
                }
            }
        }