Exemplo n.º 1
0
        public static List <ProgramNode> SATEncodeTemp(TreeNode <T> node, ProgramSpec programSpec, List <Z3ComponentSpecs> componentSpecs, Context context, Grammar grammar, string interVar, List <ProgramNode> specList = null)
        {
            if (specList == null)
            {
                specList = new List <ProgramNode>();
            }

            var specAsString   = componentSpecs.Where(x => x.key.Equals(node.Data)).FirstOrDefault();
            var nodeSpecAsList = grammar.typeConstants.Where(x => x.Item1 == node.Data.ToString()).FirstOrDefault();

            var nodeSpec         = new List <BoolExpr>();
            var nodeOriginalSpec = new List <BoolExpr>();

            if (specAsString == null && nodeSpecAsList == null)
            {
                nodeSpec = new List <BoolExpr> {
                    context.MkBool(true)
                };
            }
            else
            {
                var spec = String.Empty;
                spec = GetSpecAsString(node, grammar, componentSpecs, interVar);

                nodeSpec = ComponentSpecsBuilder.GetComponentSpec(new Z3ComponentSpecs()
                {
                    key   = node.Data.ToString(),
                    value = spec
                });

                nodeOriginalSpec = (specAsString != null) ?
                                   ComponentSpecsBuilder.GetComponentSpec(new Z3ComponentSpecs()
                {
                    key   = node.Data.ToString(),
                    value = specAsString.value
                }) : null;
            }


            var storeSpec = new Pair <List <BoolExpr>, List <BoolExpr> >(nodeSpec, nodeOriginalSpec);

            specList.Add(new ProgramNode(node.Data.ToString(), node.index, storeSpec));

            foreach (var child in node.Children.Where(x => !x.IsHole))
            {
                SATEncodeTemp(child, programSpec, componentSpecs, context, grammar, interVar, specList);
            }

            return(specList);
        }
Exemplo n.º 2
0
        public static List <ExampleNode> GetProgramSpecZ3Expression(List <List <string> > programSpecAsString, Context context)
        {
            var programSpecAsBoolExprList = new List <ExampleNode>();

            foreach (var example in programSpecAsString)
            {
                var exampleSpecsAsBoolExprArray = example.Select(x => ComponentSpecsBuilder.GetComponentSpec(
                                                                     new Z3ComponentSpecs()
                {
                    key   = $"parameter_{x.SplitBy(".").FirstOrDefault()}",
                    value = x
                }).First()).ToList();
                programSpecAsBoolExprList.Add(new ExampleNode(exampleSpecsAsBoolExprArray));
            }

            return(programSpecAsBoolExprList);
        }
Exemplo n.º 3
0
        public Lemma AnalyzeConflict(UnSatCore unSATCore, List <Z3ComponentSpecs> z3ComponentsSpecs, Context context, TreeNode <string> root, Grammar grammar)
        {
            var lemma = new Lemma();

            foreach (var clause in unSATCore)
            {
                //var rule = programRoot.GetAtIndex(Int32.Parse(clause.index)).rule;
                var rule = grammar.DFS(root, (x) => x.index == Int32.Parse(clause.index)).Pop().rule;
                var componentsToCheck = grammar.productions.Where(x => x.leftHandSide == rule.leftHandSide)
                                        .Select(x => x.rightHandSide.First()).ToList();

                var lemmaClause = new LemmaClause();

                lemmaClause.Add(context.MkNot(clause.expression));

                if (clause.spec != null && z3ComponentsSpecs.Any(x => x.key == clause.name && x.type != ComponentType.Parameter))
                {
                    foreach (var component in componentsToCheck)
                    {
                        var componentSpec = z3ComponentsSpecs.Where(x => x.key == component).FirstOrDefault();
                        if (componentSpec != null)
                        {
                            var z3ComponentSpec = context.MkAnd(ComponentSpecsBuilder.GetComponentSpec(componentSpec));

                            var check         = context.MkNot(context.MkImplies(z3ComponentSpec, clause.spec));
                            var lightEncoding = unSATCore.Where(x => x != clause);
                            if (SMTSolver.CheckIfUnSAT(context, check))
                            {
                                lemmaClause.Add(context.MkNot(context.MkBoolConst($"C_{clause.index}_{component}")));
                            }
                        }
                    }
                }
                //lemma.lemmaLength = unSATCores.SelectMany(x => x).Max(x => x.index).ToInt();
                lemma.Add(lemmaClause);
            }
            return(lemma);
        }
Exemplo n.º 4
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));
        }
Exemplo n.º 5
0
        public static string GetSpecAsString(TreeNode <T> node, Grammar grammar, List <Z3ComponentSpecs> componentSpecs, string interVar)
        {
            var spec = String.Empty;

            var specAsString = componentSpecs.Where(x => x.key.Equals(node.Data)).FirstOrDefault();

            if (specAsString == null)
            {
                var nodeSpecAsList = grammar.typeConstants.Where(x => x.Item1 == node.Data.ToString()).FirstOrDefault();
                if (nodeSpecAsList != null)
                {
                    var specList = (nodeSpecAsList.Item2.argType == ArgType.List) ? ((List <string>)nodeSpecAsList.Item2.obj) : new List <string>()
                    {
                        nodeSpecAsList.Item2.obj.ToString()
                    };

                    var originalSpec = String.Join($" {LogicalOperators.operators[ELogicalOperators.AND]} ", specList);
                    spec = originalSpec.Replace(y, $"{interVar}{node.index}");

                    var nodeSpec1 = ComponentSpecsBuilder.GetComponentSpec(new Z3ComponentSpecs()
                    {
                        key   = node.Data.ToString(),
                        value = spec
                    });
                }
            }
            else
            {
                if (node.IsLeaf && node.IsRoot)
                {
                    spec = specAsString.value.Replace($"{Symbols.inputArg}{Symbols.dot}", $"{node.Data.ToString()}{Symbols.dot}");
                }
                else if (node.IsLeaf)
                {
                    //spec = GetLeafSpec(programSpec, node);
                    //spec = ReplaceInputSymbolsWithIntermediateVariables(node, specAsString.Item2);


                    spec = GetLeafSpec(specAsString.value, node, interVar);

                    //if (specAsString.value.Contains(Symbols.dot))
                    //{
                    //    spec = specAsString.value.Replace($"{Symbols.outputArg}{Symbols.dot}", $"{interVar}{node.index}{Symbols.dot}");
                    //    spec = spec.Replace($"{Symbols.inputArg}{Symbols.dot}", $"{node.Data.ToString()}{Symbols.dot}");
                    //}
                    //else
                    //{
                    //    spec = specAsString.value.Replace($"{Symbols.outputArg}", $"{interVar}{node.index}");
                    //    spec = spec.Replace($"{Symbols.inputArg}", $"{node.Data.ToString()}");
                    //}
                }
                else if (node.IsRoot)
                {
                    spec = ReplaceInputSymbolsWithIntermediateVariables(node, specAsString.value, grammar, interVar);
                }
                else
                {
                    spec = ReplaceInputSymbolsWithIntermediateVariables(node, specAsString.value, grammar, interVar);
                    spec = spec.Replace(y, interVar + node.index);
                }
            }
            return(spec);
        }