示例#1
0
        public TreeNode <string> Decide_AST(TreeNode <string> root, ref List <TreeNode <string> > unSATCorePrograms,
                                            Context context, Grammar grammar, List <Z3ComponentSpecs> z3ComponentsSpecs, ProgramSpec programSpec,
                                            ref Lemmas lemmas, ref int lemmaCounter, ref int extensionCounter, ref List <long> pruningTimes, Params param, ref List <long> avg_b, ref List <long> avg_e)
        {
            var searchStack = DFS(root, (x) => x.IsHole);
            var hole        = searchStack.Pop();

            string currentLeftHandSide;

            var condition = (hole.holes == null || hole.holes.Count == 0) && hole.IsRoot;

            if (condition)
            {
                currentLeftHandSide = grammar.startSymbol;
            }
            else
            {
                currentLeftHandSide = hole.Parent.holes.Pop();
                hole.Parent.holesBackTrack.Push(currentLeftHandSide);
            }

            //var possibleProductionRules1 = productions.Where(x => x.leftHandSide == currentLeftHandSide &&
            //    !hole.deadends.Any(y => y == x.rightHandSide.First())).ToList();
            var possibleProductionRules = productions.Where(x => x.leftHandSide == currentLeftHandSide).ToList();

            var holeToFill = new TreeNode <string>();

            holeToFill = hole.IsHole ? hole : hole.Children.FirstOrDefault(x => x.IsHole);
            while (possibleProductionRules.Count > 0)
            {
                int index;
                if (param.random)
                {
                    index = rand.Next(0, (possibleProductionRules.Count()));
                }
                else
                {
                    index = 0;
                }

                var choosenProductionRule = possibleProductionRules.ElementAt(index);

                var terminal = choosenProductionRule.rightHandSide.First();

                holeToFill.FillHole(terminal, choosenProductionRule, context, grammar);


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


                //if (RuleResultsInLeaf(grammar, choosenProductionRule))
                //{
                var stopWatch             = new Stopwatch();
                var elapsedTime_Base      = default(long);
                var elapsedTime_Extension = default(long);

                #region reject with base-lemmas
                if (param.use_base_lemmas || (!param.use_base_lemmas && param.use_extended_lemmas))
                {
                    stopWatch.Start();
                    //Reject current partial program using Lemmas
                    var satEncodedProgram = SATEncoder <string> .SATEncode(root, context);

                    var lemmasAsExp  = lemmas.Select(x => x.AsExpression(context)).ToList();
                    var lemmasAsConj = context.MkAnd(lemmasAsExp);

                    //foreach (var lemma in lemmas)
                    //{
                    //checking consistency with the knoweldge base (Lemmas)
                    //var lemmaAsExpersion = lemma.AsExpression(context);

                    var check        = context.MkAnd(lemmasAsConj, satEncodedProgram);
                    var checkIfUnSAT = SMTSolver.CheckIfUnSAT(context, check);

                    if (checkIfUnSAT)
                    {
                        holeToFill.MakeHole();
                        possibleProductionRules.Remove(choosenProductionRule);
                        lemmaCounter++;
                        extensionCounter++;
                        //break;
                    }
                    //}

                    stopWatch.Stop();
                    elapsedTime_Base = stopWatch.ElapsedMilliseconds;
                    avg_b.Add(elapsedTime_Base);
                    stopWatch.Reset();
                }
                #endregion

                #region reject with extended-lemmas
                if (param.use_extended_lemmas && unSATCorePrograms.Count > 0)
                {
                    stopWatch.Start();
                    //Reject current partial program using unSATPrograms
                    //foreach (var unSATCoreProgram in unSATCorePrograms)
                    //{
                    //checking consistency with the knoweldge base (UnSAT Programs)
                    var program = new Program(rand);

                    //var unSATCores = program.CheckConflict(z3ComponentsSpecs, context, programSpec, root, grammar);
                    //var unSATCore = program.CheckConflict(z3ComponentsSpecs, context, programSpec, unSATCoreProgram, grammar);

                    var unSATPorgram = unSATCorePrograms
                                       .Select(y => test(y, grammar, z3ComponentsSpecs)
                                               .SplitBy(LogicalOperators.operators[ELogicalOperators.AND])
                                               .Select(x => ComponentSpecsBuilder.GetComponentSpec(new Z3ComponentSpecs()
                    {
                        key   = x,
                        value = x
                    }))
                                               .SelectMany(x => x).ToList())
                                       .ToList();

                    var unsatSMT = context.MkOr(unSATPorgram.Select(x => context.MkAnd(x)));

                    //var unSATPorgram = test(unSATCoreProgram, grammar, z3ComponentsSpecs)
                    //        .SplitBy(LogicalOperators.operators[ELogicalOperators.AND])
                    //        .Select(x => ComponentSpecsBuilder.GetComponentSpec(new Z3ComponentSpecs()
                    //        {
                    //            key = x,
                    //            value = x
                    //        }))
                    //        .SelectMany(x => x).ToList();
                    var candidateProgram = test(root, grammar, z3ComponentsSpecs)
                                           .SplitBy(LogicalOperators.operators[ELogicalOperators.AND])
                                           .Select(x => ComponentSpecsBuilder.GetComponentSpec(new Z3ComponentSpecs()
                    {
                        key   = x,
                        value = x
                    }))
                                           .SelectMany(x => x).ToList();

                    //var ss = test(root.Children[0], grammar, z3ComponentsSpecs);

                    //var satEncodedArtifactsAsSMTModel_1 = SATEncoder<string>.SMTEncode(z3ComponentsSpecs, context, programSpec, root, grammar, Symbols.ivs);
                    //var satEncodedArtifactsAsSMTModel_2 = SATEncoder<string>.SMTEncode(z3ComponentsSpecs, context, programSpec, unSATCoreProgram, grammar, "r");

                    //var candidateProgram = satEncodedArtifactsAsSMTModel_1.satEncodedProgram.SelectMany(x => x.clauses.First).ToArray();
                    //var unSATPorgram = satEncodedArtifactsAsSMTModel_2.satEncodedProgram.SelectMany(x => x.clauses.First).ToArray();



                    //var check = context.MkNot(context.MkImplies(context.MkAnd(candidateProgram), context.MkAnd(unSATPorgram)));
                    var check = context.MkNot(context.MkImplies(context.MkAnd(candidateProgram), unsatSMT));

                    var checkIfUnSAT = SMTSolver.CheckIfUnSAT(context, check);

                    if (checkIfUnSAT)
                    {
                        holeToFill.MakeHole();
                        possibleProductionRules.Remove(choosenProductionRule);
                        extensionCounter++;
                        //break;
                    }
                    //}
                    stopWatch.Stop();
                    elapsedTime_Extension = stopWatch.ElapsedMilliseconds;
                    avg_e.Add(elapsedTime_Extension);
                    stopWatch.Reset();
                }
                #endregion

                var ratio = (extensionCounter == 0 || lemmaCounter == 0) ? 1 : extensionCounter / lemmaCounter;
                //Console.WriteLine($"Extension/Lemma ratio:{ratio}");

                pruningTimes.Add(elapsedTime_Base - elapsedTime_Extension);
                //Console.WriteLine($"{lemmas.Count == 0} {unSATCorePrograms.Count == 0} Elapsed time base - extension: {elapsedTime_Base - elapsedTime_Extension}");
                //}
                if (!holeToFill.IsHole)
                {
                    if (!RuleResultsInLeaf(grammar, holeToFill.rule))
                    {
                        productions.Remove(holeToFill.rule);
                    }
                    return(holeToFill);
                }
            }

            if (param.printConsole)
            {
                root.Visualize();
            }
            //File.AppendAllText(Resources.path_results, root.ToString());


            //holeToFill.deadends.Clear();
            holeToFill.Parent.holes.Push(holeToFill.Parent.holesBackTrack.Pop());

            holeToFill = searchStack.Pop();

            //holeToFill.deadends.Add(holeToFill.Data);

            if (param.use_base_lemmas || (!param.use_base_lemmas && param.use_extended_lemmas))
            {
                var lemma = Lemma.NewLemma(root, context);

                var lemmasAsExpression = lemma.AsExpression(context);


                var lemmaAsString = CheckLemma_ByString(lemma);
                lemmas.RemoveAll(x => CheckLemma_ByString(x).Contains(lemmaAsString));

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

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

            if (!RuleResultsInLeaf(grammar, holeToFill.rule))
            {
                grammar.productions.Add(holeToFill.rule);
            }
            holeToFill.MakeHole();

            //currentLeftHandSide = holeToFill.Parent.holesBackTrack.Peek();

            holeToFill.Parent.holes.Push(holeToFill.Parent.holesBackTrack.Pop());
            return(Decide_AST(root, ref unSATCorePrograms, context, grammar, z3ComponentsSpecs,
                              programSpec, ref lemmas, ref lemmaCounter, ref extensionCounter, ref pruningTimes, param, ref avg_b, ref avg_e));
        }
示例#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);
                }
            }
        }