示例#1
0
        public static string SAT_Encode_NoOrder(TreeNode <string> root, Context context)
        {
            var sat_encoded_program = SATEncoder <string> .SATEncode(root, context);

            var sat_encoded_pogram_as_string = sat_encoded_program.Args.ToList().Select(x => x.ToString()).ToList();

            return(String.Join(" ", sat_encoded_pogram_as_string));
        }
示例#2
0
        public TreeNode <string> Decide_AST(TreeNode <string> root, Lemmas lemmas, Context context, Grammar grammar, Params param)
        {
            var hole = DFS(root, (x) => x.IsHole).Peek();

            var condition           = root.holes == null;
            var currentLeftHandSide = condition ? grammar.startSymbol : hole.Parent.holes.Pop();

            if (!condition)
            {
                hole.Parent.holesBackTrack.Push(currentLeftHandSide);
            }

            var possibleProductionRules = productions.Where(x => x.leftHandSide == currentLeftHandSide).ToList();

            while (possibleProductionRules.Count > 0)
            {
                var index = rand.Next(0, (possibleProductionRules.Count()));
                //var index = 0;
                //var index = 1;
                var choosenProductionRule = possibleProductionRules.ElementAt(index);

                var terminal = choosenProductionRule.rightHandSide.First();

                var holeToFill = hole.IsHole ? hole : hole.Children.FirstOrDefault(x => x.IsHole);

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

                if (RuleResultsInLeaf(grammar, choosenProductionRule))
                {
                    var satEncodedProgram = SATEncoder <string> .SATEncode(root, context);

                    foreach (var lemma in lemmas)
                    {
                        //checking consistency with the knoweldge base
                        var lemmaAsExpersion = lemma.AsExpression(context);
                        var check            = context.MkAnd(lemmaAsExpersion, satEncodedProgram);
                        var checkIfUnSAT     = SMTSolver.CheckIfUnSAT(context, check);
                        if (checkIfUnSAT)
                        {
                            holeToFill.MakeHole();
                            possibleProductionRules.Remove(choosenProductionRule);
                            break;
                        }
                    }
                }
                if (!holeToFill.IsHole)
                {
                    if (!RuleResultsInLeaf(grammar, holeToFill.rule))
                    {
                        productions.Remove(holeToFill.rule);
                    }

                    return(hole);
                }
            }
            return(null);
        }
示例#3
0
        public static string SAT_Encode(TreeNode <string> root, Context context)
        {
            var sat_encoded_program = SATEncoder <string> .SATEncode(root, context);

            var sat_encoded_pogram_as_string  = sat_encoded_program.Args.ToList().Select(x => x.ToString()).OrderBy(x => Int32.Parse(x.SplitBy("_").ElementAt(1))).ToList();
            var sat_encoded_pogram_as_string1 = sat_encoded_program.Args.ToList().Select(x => x.ToString()).ToList();

            return(String.Join(" ", sat_encoded_pogram_as_string));
        }
示例#4
0
        public static Lemma NewLemma(TreeNode <string> root, Context context)
        {
            var lemmaClauses = SATEncoder <string> .satEncode(root, context)
                               .Select(x => new LemmaClause(new List <BoolExpr>()
            {
                context.MkNot(x)
            }));

            return(new Lemma(lemmaClauses));
        }
示例#5
0
        public void Propogate(TreeNode <string> root, Lemmas lemmas, Context context, Grammar grammar)
        {
            var satEncodedProgram = SATEncoder <string> .SATEncode(root, context);

            var lemmasIConjunction = lemmas.LemmasInConjunction(context);

            var rule1 = grammar.productions.First();

            var holes = root.Holes();

            foreach (var hole in holes)
            {
                var index = hole.Parent.Children.IndexOf(hole);
                var nonTerminalToExpand = hole.Parent.rule.rightHandSide[index + 1];
                var rules = grammar.productions.Where(x => x.leftHandSide == nonTerminalToExpand).ToList();

                var componentsFromRules_SATEncoded = rules.Select(x => context.MkBoolConst($"C_{hole.index}_{x.component}"));

                var or_componentsFromRules_SATEncoded = componentsFromRules_SATEncoded.Count() == 1 ? componentsFromRules_SATEncoded.First() : context.MkOr(componentsFromRules_SATEncoded);

                foreach (var rule in rules)
                {
                    var component_SATEncoded = context.MkBoolConst($"C_{hole.index}_{rule.component}");

                    var leftHandSide = context.MkAnd(new BoolExpr[] { satEncodedProgram, lemmasIConjunction, or_componentsFromRules_SATEncoded });
                    var check        = context.MkNot(context.MkImplies(leftHandSide, component_SATEncoded));
                    if (SMTSolver.CheckIfUnSAT(context, check))
                    {
                        hole.FillHole(rule.component, rule, context, grammar);
                        Console.WriteLine("Propogate:");
                        //root.Visualize();
                        Propogate(root, lemmas, context, grammar);
                        return;
                    }
                }
            }
        }
示例#6
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));
        }
示例#7
0
        public UnSatCore CheckConflict(List <Z3ComponentSpecs> componentSpecs, Context context, ProgramSpec programSpec, TreeNode <string> root, Grammar grammar)
        {
            var satEncodedArtifactsAsSMTModel = SATEncoder <string> .SMTEncode(componentSpecs, context, programSpec, root, grammar, Symbols.ivs);

            return(SMTSolver.SMTSolve(context, satEncodedArtifactsAsSMTModel));
        }