using System; using System.Collections.Generic; using System.Text; using DNAModule.Sunweaver.Commands.Abstracts; using System.Reflection; using DNAModule.Sunweaver.MetaTags; using DNAModule.Sunweaver.Commands; using Sunweaver; namespace DNAModule.Sunweaver { public static class DNASystem { public static SystemVariables Sysvar = new SystemVariables(); public class CommandListClass { /// /// A raw list that associates strings and base pair types. /// public static Dictionary Commands = new Dictionary(); public static List basePairs = new List(); public BasePair this[string name] { get { try { return (BasePair)System.Activator.CreateInstance(Commands[name]); } catch(System.Collections.Generic.KeyNotFoundException) { throw new Exception("The DNA command \"" + name + "\" wasn\'t recognized."); } } set { Commands[name] = value.GetType(); basePairs.Add(value); } } public static BasePair randomBasePair(float commands, float constants, float references, float stores) { float total = commands + constants + references + stores; float choice = (float)rand.NextDouble() * total; choice -= commands; if (choice <= 0) { return basePairs[rand.Next(basePairs.Count)]; } choice -= constants; if (choice <= 0) { return new Constant(rand.Next(DataPrototypes.IntStack.Min, DataPrototypes.IntStack.Max)); } choice -= references; if (choice <= 0) { return new ExplicitRef("", rand.Next(DataPrototypes.IntStack.Min, DataPrototypes.IntStack.Max)); } choice -= stores; if (choice <= 0) { return new Commands.UnaryMath.ExplicitStore("", rand.Next(DataPrototypes.IntStack.Min, DataPrototypes.IntStack.Max)); } return null; } } public static CommandListClass CommandList = new CommandListClass(); public class MetaTagListClass { /// /// A raw list that associates strings and base pair types. /// public static Dictionary Tags = new Dictionary(); public MetaTag this[string[] line] { get { try { Object[] Params = new Object[1]; Params[0] = line; return (MetaTag)System.Activator.CreateInstance(Tags[line[0]], Params); } catch (System.Collections.Generic.KeyNotFoundException) { throw new Exception("The Meta tag \"" + line[0] + "\" wasn\'t recognized."); } } set { Tags[line[0]] = value.GetType(); } } } public static MetaTagListClass MetaTagList = new MetaTagListClass(); public class ExplicitPunctuationClass { public static Dictionary Puncts = new Dictionary(); public BasePair this[string word, DNAModule.Sunweaver.DataPrototypes.DNA dna] { get { if(word.Length == 1) throw new Exception("The command being parsed, \"" + word + "\" isn't a valid identifier"); string punctless = word.Substring(1, word.Length - 1); int number = 0; if (char.IsLetter(punctless[0])) { //check for if returning false Parser.ParseLabel(punctless, dna, ref number); } else if (char.IsNumber(punctless[0])) { number = Int16.Parse(punctless); punctless = ""; } else throw new Exception("The command being parsed, \"" + word + "\" isn't a valid identifier"); Object[] Params = new object[2]; Params[0] = punctless; Params[1] = number; return (BasePair)System.Activator.CreateInstance(Puncts[word[0]], Params); } set { Puncts[word[0]] = value.GetType(); } } } public static ExplicitPunctuationClass PunctuationsList = new ExplicitPunctuationClass(); /// /// Override this reference with a program-wide global random to link the /// DNA's random operator with the rest of your program /// public static Random rand = new Random(); /// /// Initializes the DNA subsystem. /// static DNASystem() { //Find all types in this assembly String assemblyName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; Assembly ThisModule = Assembly.GetExecutingAssembly(); foreach (Type type in ThisModule.GetExportedTypes()) { foreach(Attribute att in type.GetCustomAttributes(false)) { Commands.Attributes.BasePairAttribute BPA = att as Commands.Attributes.BasePairAttribute; if (BPA != null) { try { BasePair bp = (BasePair)System.Activator.CreateInstance(type); CommandList[bp.ToString()] = bp; } catch (InvalidCastException) { throw new Exception(type.ToString() + " has the BasePair attribute, " + "yet it doesn't inherit from the BasePair class"); } } MetaTags.Attributes.MetaTagAttribute MTA = att as MetaTags.Attributes.MetaTagAttribute; if (MTA != null) { MetaTag tag = (MetaTag)System.Activator.CreateInstance(type); string[] Name = new string[1]; Name[0] = tag.Name(); MetaTagList[Name] = tag; } Commands.Attributes.PunctuationAttribute PA = att as Commands.Attributes.PunctuationAttribute; if (PA != null) { Object[] Params = new object[2]; Params[0] = "default"; Params[1] = 0; BasePair bp = (BasePair)System.Activator.CreateInstance(type, Params); PunctuationsList[bp.ToString(), null] = bp; } } } } /// /// A dummy function so that the static ctor gets called /// public static void Initialize() { } } }