コード例 #1
0
        public ConcreteExample(List <Parameter> parameters, List <TypeSpec> argSpecList, Context context)
        {
            this.parameters = new List <Parameter>();

            var argSpec = argSpecList.Where(x => x.type == ArgType.List).First();

            foreach (var parameter in parameters)
            {
                var param = ProgramSpecBuilder.GetParamByType(parameter, argSpec);

                this.parameters.Add(param);
            }
        }
コード例 #2
0
        private static Grammar BuildGrammarFromSpec(XElement grammarSpec, List <TypeSpec> typeSpecs, Random rand, List <Parameter> parameters)
        {
            var countArity = 0;

            var typeConstants = grammarSpec.Descendants(key_types).First().Value.Trim().SplitBy(Symbols.seperator)
                                .Select((x, index) =>
            {
                return(new Parameter(ParameterType.Other,
                                     EnumHelper.ToEnum <ArgType>(x.SplitBy("(").First()), x.SplitBy("(").Last().Remove(")").Remove("[").Remove("]"), index + 1));
            })
                                .Select(x =>
            {
                var typeSpec = typeSpecs.Where(y => y.type == x.argType).First();
                var symbol   = (x.argType == ArgType.List) ? $"[{x.obj.ToString()}]" : x.obj.ToString();
                return(Tuple.Create(symbol, ProgramSpecBuilder.GetParamByType(x, typeSpec)));
            }).ToList();

            var startSymbol = grammarSpec.Descendants(key_startSymbol)
                              .Select(x => x.Value.TrimStart().TrimEnd()).FirstOrDefault();

            var terminals = grammarSpec.Descendants(key_terminals)
                            .Select(x => x.Value.TrimStart().TrimEnd()).FirstOrDefault()
                            .SplitBy(_ruleSeparator);

            var nonTerminals = grammarSpec.Descendants(key_nonTerminals)
                               .Select(x => x.Value.TrimStart().TrimEnd()).FirstOrDefault()
                               .SplitBy(_ruleSeparator);

            var productionRuleEntries = grammarSpec.Descendants(key_rules)
                                        .Select(x => x.Value.TrimStart().TrimEnd()).ToList();

            var productions = new List <Production>();

            foreach (var entry in productionRuleEntries)
            {
                var splitedEntry         = entry.SplitBy(_ruleInference);
                var leftHandsideSymbol   = splitedEntry.Select(x => x.Trim()).First();
                var rightHandSideSymbols = splitedEntry.Last().SplitBy(_ruleSeparator);

                foreach (var rhs in rightHandSideSymbols)
                {
                    if (rhs.Contains("("))
                    {
                        var rules = parameters.Where(x => x.argType == EnumHelper.ToEnum <ArgType>(rhs.SplitBy("(").First()) && x.parameterType == ParameterType.Input)
                                    .Select(x => new Production(leftHandsideSymbol, new List <string>()
                        {
                            x.obj.ToString()
                        }, 0)).ToList();
                        productions.AddRange(rules);
                    }
                    else
                    {
                        var splittedRHS1 = rhs.SplitBy(_blankSpace).ToList().Where(x => nonTerminals.Contains(x)).ToList();
                        if (splittedRHS1.Count() > countArity)
                        {
                            countArity = splittedRHS1.Count();
                        }

                        var splittedRHS = rhs.SplitBy(_blankSpace);

                        productions.Add(new Production(leftHandsideSymbol, splittedRHS, splittedRHS1.Count()));
                    }
                }
            }
            return(new Grammar(startSymbol, nonTerminals, terminals, productions, countArity, typeConstants, rand));
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: mrbiglari/automation
        public void Synthesize_WhileTrue(Params param)
        {
            using (Context context = new Context(new Dictionary <string, string>()
            {
                { "proof", "true" }
            }))
            {
                var benchmark_count = Directory.GetFiles(Resources.path_programSpec_base).Length;

                for (int benchmark_id = 1; benchmark_id <= benchmark_count; benchmark_id++)
                {
                    var stopWatch = new Stopwatch();
                    stopWatch.Start();

                    var numberOfPrograms = 1;

                    var typeSpecs         = TypeSpecBuilder.Build(Resources.path_typeSpec);
                    var programSpec       = ProgramSpecBuilder.Build(Resources.path_programSpec_x.Replace(".xml", $"{benchmark_id}.xml"), context, typeSpecs);
                    var grammar           = GrammarBuilder.Build(Resources.path_grammarSpec, typeSpecs, random, programSpec.parameters);
                    var grammarGround     = GrammarBuilder.Build(Resources.path_grammarSpec, typeSpecs, random, programSpec.parameters);
                    var z3ComponentsSpecs = ComponentSpecsBuilder.Build(Resources.path_componentSpec, context, programSpec, grammar);


                    var synthesisParams = new SynthesisParams()
                    {
                        typeSpecs        = typeSpecs,
                        programSpec      = programSpec,
                        grammar          = grammar,
                        grammarGround    = grammarGround,
                        z3ComponentSpecs = z3ComponentsSpecs,
                        benchmarkId      = benchmark_id
                    };

                    var roots = new List <TreeNode <string> >();
                    for (int i = 0; i < numberOfPrograms; i++)
                    {
                        var root = Synthesize(numberOfPrograms, param, context, synthesisParams);
                        roots.Add(root);
                    }

                    stopWatch.Stop();
                    Console.WriteLine($"Time Elapsed: {(double)stopWatch.Elapsed.TotalSeconds}");


                    if (numberOfPrograms == 1)
                    {
                        var    root       = roots.First();
                        string createText = $"{stopWatch.Elapsed.TotalSeconds.ToString()} {benchmark_id} {lemmas.Count} {unSATCorePrograms.Count} {root.Size} {avg_b.Average()} {avg_e.Average()} {SAT_Encode(root, context)}\n";
                        if (benchmark_id == 1)
                        {
                            //if (!Directory.Exists(Resources.path_results))
                            //    Directory.CreateDirectory(Resources.path_results);

                            File.WriteAllText(Resources.path_results, String.Empty);
                            File.AppendAllText(Resources.path_results, "{stopWatch.Elapsed.TotalSeconds.ToString()} {benchmark_id} {lemmas.Count} {unSATCorePrograms.Count} {root.Size} {SAT_Encode(root, context)}\n");
                        }
                        File.AppendAllText(Resources.path_results, createText);
                    }

                    if (param.debug)
                    {
                        Console.WriteLine($"Press Enter to continue");
                        Console.ReadLine();
                    }
                }
            }
        }