Пример #1
0
        public IEnumerable <string> Generate(string before, string after)
        {
            var result = new List <string>();

            //parse code
            var beforeAst = Parse(before);
            var afterAast = Parse(after);

            //run tree edit distance
            var zss          = new PythonZss(beforeAst, afterAast);
            var editDistance = zss.Compute();

            //get primary edits
            var rootAndNonRootEdits = WitnessFunctions.SplitEditsByRootsAndNonRoots(editDistance);
            //replace insert and delete by update
            var unparser = new Unparser();

            foreach (var edit in rootAndNonRootEdits.Item1)
            {
                if (edit is Update)
                {
                    result.Add("Update " + unparser.Unparse(edit.TargetNode) + " to " + unparser.Unparse(edit.ModifiedNode));
                }
                else if (edit is Insert)
                {
                    result.Add("Insert " + unparser.Unparse(edit.ModifiedNode));
                }
            }
            //for each edit, create a hint
            return(result);
        }
Пример #2
0
        public static SynthesisEngine ConfigureSynthesis(Grammar grammar)
        {
            var witnessFunctions     = new WitnessFunctions(grammar);
            var deductiveSynthesis   = new DeductiveSynthesis(witnessFunctions);
            var synthesisExtrategies = new ISynthesisStrategy[] { deductiveSynthesis };
            var synthesisConfig      = new SynthesisEngine.Config {
                Strategies = synthesisExtrategies
            };

            return(new SynthesisEngine(grammar, synthesisConfig));
        }
Пример #3
0
        public IEnumerable <Transformation> LearnTransformations(List <Tuple <string, string> > examples,
                                                                 int numberOfPrograms = 1, string ranking = "specific")
        {
            var spec = CreateExampleSpec(examples);

            //TODO: this is not thread safe. If multiple instances of Refazer are changing
            //the value of the ranking scores, we can have a problem.
            RankingScore.ScoreForContext = ranking.Equals("specific") ? 100 : -100;
            var scoreFeature = new RankingScore(Grammar.Value);
            DomainLearningLogic learningLogic = new WitnessFunctions(Grammar.Value);

            _prose = new SynthesisEngine(Grammar.Value,
                                         new SynthesisEngine.Config
            {
                LogListener = new LogListener(),
                Strategies  = new[] { new DeductiveSynthesis(learningLogic) },
                UseThreads  = false,
                CacheSize   = int.MaxValue
            });

            var learned = _prose.LearnGrammarTopK(spec, scoreFeature, 1);

            var uniqueTransformations = new List <ProgramNode>();

            //filter repetitive transformations
            foreach (var programNode in learned)
            {
                var exists = false;
                foreach (var uniqueTransformation in uniqueTransformations)
                {
                    if (programNode.ToString().Equals(uniqueTransformation.ToString()))
                    {
                        exists = true;
                        break;
                    }
                }
                if (!exists)
                {
                    uniqueTransformations.Add(programNode);
                }
            }
            uniqueTransformations = uniqueTransformations.Count > numberOfPrograms
                ? uniqueTransformations.GetRange(0, numberOfPrograms)
                : uniqueTransformations;

            return(uniqueTransformations.Select(e => new PythonTransformation(e)));
        }
Пример #4
0
        public ActionResult LearnTransformation([FromBody] LearnTransformRequestBody requestBody)
        {
            try
            {
                Console.WriteLine(requestBody.Instance);

                ProgramSet learned;
                using (var ctx = new Context())
                {
                    var inputExamples = Utils.GetInputOutputExamplesModified(ctx, requestBody.InputOutputExamples,
                                                                             SmtPrefix, requestBody.DeclareStatements);
                    var spec = Utils.CreateExampleSpec(_grammar, inputExamples);
                    RankingScore.ScoreForContext = 100;
                    var scoreFeature = new RankingScore(_grammar.Value);
                    DomainLearningLogic learningLogic = new WitnessFunctions(_grammar.Value);
                    _prose = new SynthesisEngine(_grammar.Value,
                                                 new SynthesisEngine.Config
                    {
                        LogListener = new LogListener(),
                        Strategies  = new ISynthesisStrategy[] { new DeductiveSynthesis(learningLogic) },
                        UseThreads  = false,
                        CacheSize   = int.MaxValue
                    });
                    learned = _prose.LearnGrammarTopK(spec, scoreFeature);
                }

                var finalPrograms = learned.RealizedPrograms.Select(program => new FinalProgram(program.ToString(), program.PrintAST())).ToList();
                if (finalPrograms.Count == 0)
                {
                    Console.WriteLine("No Programs Found");
                }

                foreach (var program in finalPrograms)
                {
                    Console.WriteLine(program.HumanReadableAst);
                }
                return(Ok(JsonConvert.SerializeObject(finalPrograms)));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine(ex.TargetSite);
                Console.WriteLine("Error: " + ex.Message);
                return(StatusCode(500, ex.Message));
            }
        }