public static List <Z3ComponentSpecs> Build(string fileName, Context ctx, ProgramSpec programSpec, Grammar grammar) { context = ctx; var specContent = GetComponentSpecsFile(fileName); return(BuildComponentSpecsFromSpec(specContent, programSpec, grammar)); }
public static string GetLeafSpec(ProgramSpec programSpec, TreeNode <T> node, string interVar) { var argIndex = Int32.Parse(node.Data.ToString().Replace(Symbols.inputArg, "") != node.Data.ToString() ? node.Data.ToString().Replace(Symbols.inputArg, "") : "-1"); var argType = (argIndex != -1) ? programSpec.args[argIndex - 1].type : Symbols.otherType; var retSpecList = new List <string>(); switch (argType) { case (Symbols.listType): foreach (var property in Symbols.properties) { retSpecList.Add(node.Data.ToString() + Symbols.dot + property + RelationalOperators.operators[ERelationalOperators.Eq] + interVar + node.index + Symbols.dot + property); } break; case (Symbols.intType): retSpecList.Add(node.Data.ToString() + RelationalOperators.operators[ERelationalOperators.Eq] + interVar + node.index); break; case (Symbols.otherType): retSpecList.Add(node.Data.ToString() + RelationalOperators.operators[ERelationalOperators.Eq] + interVar + node.index); break; default: break; } var retSpec = String.Join(LogicalOperators.operators[ELogicalOperators.AND], retSpecList); return(retSpec); }
public bool AreEqual_Examples(ProgramSpec programSpec, TreeNode <string> root) { var runner = new ProgramRunner(); var examples = Extensions.DeepClone(programSpec.concreteExamples); foreach (var concreteExample in examples) { var inputArgs = concreteExample.Where(x => x.parameterType == ParameterType.Input). Select(x => x.obj).ToArray(); var outputArg = concreteExample.Single(x => x.parameterType == ParameterType.Output); try { var outputResult = runner.ExecuteProgram(root, inputArgs); var check = AreEqual(outputArg, outputResult); return(check); } catch (Exception ex) { return(false); } } return(false); }
public static List <ExampleNode> SATEncodeProgramSpec(Context context, ProgramSpec programSpec) { var programSpecAsString = GetProgramSpecZ3AsString(programSpec); var programSpecAsZ3Expression = GetProgramSpecZ3Expression(programSpecAsString, context); return(programSpecAsZ3Expression); }
public static List <List <string> > GetProgramSpecZ3AsString(ProgramSpec programSpec) { var programSpecAsString = new List <List <string> >(); foreach (var example in programSpec.examples) { var exampleSpecAsString = new List <string>(); foreach (var parameter in example.parameters) { switch (parameter.argType) { case (ArgType.List): parameter.As <List <string> >().ForEach((x) => { exampleSpecAsString.Add(x); }); break; case (ArgType.Int): exampleSpecAsString.Add(parameter.As <string>()); break; } } programSpecAsString.Add(exampleSpecAsString); } return(programSpecAsString); }
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); }
private static List <Z3ComponentSpecs> BuildComponentSpecsFromSpec(XElement componentSpecsXML, ProgramSpec programSpec, Grammar grammar) { var componentSpecsList = componentSpecsXML.Descendants(key_componentSpec) .Select(x => new Z3ComponentSpecs() { key = x.Descendants(key_name).FirstOrDefault().Value.Trim(), value = x.Descendants(key_spec).FirstOrDefault().Value.Trim(), type = ComponentType.Component } ).ToList(); var ret = programSpec.parameters.Where(x => x.parameterType == ParameterType.Input).Select( x => new Z3ComponentSpecs() { key = x.obj.ToString(), value = GetSpecNonComponents(x), type = ComponentType.Parameter } ).ToList(); //var ret2 = grammar.types.Select( x => Tuple.Create(x.Item1, x.Item2.obj); return(componentSpecsList.Union(ret).ToList()); }
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)); }
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)); }
public static List <ProgramNode> SATEncodeProgram(List <Z3ComponentSpecs> componentSpecs, Context context, ProgramSpec programSpec, TreeNode <T> programRoot, Grammar grammar, string interVar) { var satEncodingList = SATEncodeTemp(programRoot, programSpec, componentSpecs, context, grammar, interVar); return(satEncodingList); }
public static SMTModel SMTEncode(List <Z3ComponentSpecs> componentSpecs, Context context, ProgramSpec programSpec, TreeNode <T> programRoot, Grammar grammar, string interVar) { return(new SMTModel() { satEncodedProgram = SATEncodeProgram(componentSpecs, context, programSpec, programRoot, grammar, interVar), satEncodedProgramSpec = SATEncodeProgramSpec(context, programSpec) }); }