private void CollectSpecialSingles(Tree.Collectors.Result ret, IEnumerable <Tree.Node> forest) { // Special case for single instructions. Generate embeddings for // those, but separate them strictly. Dictionary <string, List <Tree.Node> > constnodes = new Dictionary <string, List <Tree.Node> >(); List <string> morethanoneconst = new List <string>(); foreach (Tree.Node node in forest) { if (node.ChildCount == 0) { var slice = node.Slice; var code = Tree.Node.InstructionCode(node.Instruction, true); if (slice != null) { code = String.Format("{0}[{1}]", code, String.Join(",", Array.ConvertAll <int, string>(slice, a => a.ToString()))); } List <Tree.Node> clst; if (!constnodes.TryGetValue(code, out clst)) { clst = new List <Tree.Node>(); constnodes[code] = clst; } if (clst.Count == 1) { morethanoneconst.Add(code); } clst.Add(node); } } foreach (var code in morethanoneconst) { var lst = constnodes[code]; var proto = (Tree.Node)lst[0].Clone(); // Create embedding var embedding = ret.Prototype(proto, new Tree.NodePath[] {}); embedding.Inline = true; foreach (Tree.Node node in lst) { embedding.Embed(node); } } }
private Tree.Embedding[] Filter(Tree.Collectors.Result collection) { Options parser = Options.Instance; Tree.Filters.IFilter filter; if (parser.Filter != null) { Plugins.Plugins plugins = Plugins.Plugins.Instance; Type type = plugins.Find(typeof(Tree.Filters.IFilter), parser.Filter); if (type == null) { throw new Exception(String.Format("The filter `{0}' could not be found...", parser.Filter)); } filter = (Tree.Filters.IFilter)type.GetConstructor(new Type[] {}).Invoke(new object[] {}); } else { filter = new Tree.Filters.Default(); } // Prefilter remove rands List <Tree.Embedding> ret = new List <Tree.Embedding>(); foreach (Tree.Embedding embed in collection.Prototypes) { if (!(embed.Expression.Instruction is InstructionRand)) { ret.Add(embed); } } return(filter.Filter(ret)); }
public void Generate() { Profile.Do("load network", () => { LoadNetwork(); }); if (Options.Instance.Validate) { d_validator = new Validator(d_network); } if (Options.Instance.DelayTimeStep > 0) { Cdn.Expression expr = new Cdn.Expression(Options.Instance.DelayTimeStep.ToString("R")); Cdn.Instruction[] instr = new Cdn.Instruction[1]; instr[0] = new Cdn.InstructionNumber(Options.Instance.DelayTimeStep); expr.Instructions = instr; d_network.Integrator.AddVariable(new Cdn.Variable("delay_dt", expr, Cdn.VariableFlags.Out)); } Profile.Do("initialize knowledge", () => { // Initialize the knowledge Knowledge.Initialize(d_network); }); if (!Options.Instance.NoSparsity) { Profile.Do("sparsity", () => { var sparsity = new Sparsity(); sparsity.Optimize(); }); } var t = Profile.Begin("collect"); // Collect all the equations Tree.Collectors.Result collection = Collect(); t.End(); t = Profile.Begin("filter"); // Filter conflicts and resolve final embeddings Tree.Embedding[] embeddings = Filter(collection); t.End(); t = Profile.Begin("resolve equations"); // Resolve final equations Dictionary <State, Tree.Node> equations = ResolveEquations(embeddings); t.End(); // Create program t = Profile.Begin("create program"); Programmer.Program program = new Programmer.Program(ProgrammerOptions(), embeddings, equations); t.End(); bool outistemp = false; // Write program if (Options.Instance.Validate || Options.Instance.Compile) { // Create a new temporary directory for the output files string path = Path.GetTempFileName(); File.Delete(path); Directory.CreateDirectory(path); program.Options.Output = path; outistemp = true; } else { Directory.CreateDirectory(program.Options.Output); } t = Profile.Begin("write program"); d_writtenFiles = Options.Instance.Formatter.Write(program); t.End(); if (Options.Instance.PrintCompileSource) { foreach (string filename in d_writtenFiles) { Console.WriteLine("File: {0}", filename); Console.WriteLine(File.ReadAllText(filename)); } } if (Options.Instance.Validate && !Options.Instance.PrintCompileSource) { try { d_validator.Validate(program, d_writtenFiles); } catch (Exception e) { Console.Error.WriteLine(e.Message); Directory.Delete(program.Options.Output, true); Environment.Exit(1); } } else if (Options.Instance.Compile) { var files = Options.Instance.Formatter.Compile(Options.Instance.Verbose); if (Options.Instance.Verbose) { Log.WriteLine("Compiled {0}...", String.Join(", ", Array.ConvertAll <string, string>(files, a => Path.GetFileName(a)))); } if (!String.IsNullOrEmpty(Options.Instance.Output)) { try { Directory.CreateDirectory(Options.Instance.Output); } catch { } } foreach (var f in files) { var dest = Path.GetFileName(f); if (!String.IsNullOrEmpty(Options.Instance.Output)) { dest = Path.Combine(Options.Instance.Output, dest); } try { File.Delete(dest); } catch {} File.Move(f, dest); } } if (outistemp) { try { Directory.Delete(program.Options.Output, true); } catch {}; } }