Example #1
0
        public string TryFix(Dictionary <string, long> tests, ProgramNode current,
                             State input, Unparser unparser, Tuple <string, List <string> > staticTests = null)
        {
            object output = null;

            try
            {
                output = current.Invoke(input);
            }
            catch (Exception)
            {
                return(null);
            }
            if (output != null)
            {
                var programSet = output as IEnumerable <PythonNode>;
                var range      = programSet.Count() < 100 ? programSet.ToList() : programSet.ToList().GetRange(0, 200);
                foreach (var changedProgram in range)
                {
                    if (staticTests != null && !CheckStaticTests(changedProgram, staticTests))
                    {
                        continue;
                    }
                    var newCode = unparser.Unparse(changedProgram);
                    try
                    {
                        var isFixed = IsFixed(tests, newCode);
                        if (isFixed)
                        {
                            if (UsedPrograms.ContainsKey(current.ToString()))
                            {
                                var count = UsedPrograms[current.ToString()];
                                UsedPrograms.Remove(current.ToString());
                                UsedPrograms.Add(current.ToString(), count + 1);
                            }
                            else
                            {
                                UsedPrograms.Add(current.ToString(), 1);
                            }
                            return(newCode);
                        }
                    }
                    catch (Exception e)
                    {
                        Trace.TraceError("Exception was thrown when trying to apply fix to submission");
                        Trace.TraceError(e.Message);
                    }
                }
            }
            return(null);
        }
Example #2
0
        private bool TryInParallel(Mistake mistake, Dictionary <string, long> tests, ProgramNode program, State input)
        {
            var  unparser  = new Unparser();
            bool isFixed   = false;
            var  fixedCode = TryFix(tests, program, input, unparser);

            if (fixedCode != null)
            {
                mistake.UsedFix          = program.ToString();
                mistake.SynthesizedAfter = fixedCode;
                isFixed = true;
            }
            return(isFixed);
        }
Example #3
0
        public IEnumerable <PythonNode> Run(PythonNode ast)
        {
            var results = new List <PythonNode>();

            //if it does not find a match to some edit, return null
            //one edit may depends of another one.
            var hasEmptySet = EditSets.Any(e => !(e.Any()));

            if (hasEmptySet)
            {
                return(null);
            }

            var combinations = GetAllCombinations();
            var unparser     = new Unparser();

            foreach (var combination in combinations)
            {
                combination.ForEach(e => e.Applied = false);
                var rewriter = new Rewriter2(combination);
                try
                {
                    var newAst = ast.Rewrite(rewriter);

                    //hack to check if there is syntax errors:
                    var code = unparser.Unparse(newAst);
                    ASTHelper.ParseContent(code);
                    results.Add(newAst);
                }
                catch (TransformationNotApplicableExpection e)
                {
                    //does not add program
                }
                catch (SyntaxErrorException)
                {
                    //does not add program
                }
            }
            return(results);
        }