Beispiel #1
0
        /// <summary>
        /// Executes the instructions
        /// </summary>
        /// <returns>The instructions.</returns>
        /// <param name="seed">used seed</param>
        public void executeInstructions(System.Random random)
        {
            foreach (Instruction i in instructions)
            {
                String    value = "";
                FieldInfo info  = this.GetType().GetField(i.field, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

                // If the value starts with RANDOM(x, y)
                // We have to generate a new number
                if (i.value.StartsWith("RANDOM") && info.FieldType.Equals(typeof(double)))
                {
                    Match m = randRegex.Match(i.value);
                    if (m.Success)
                    {
                        double f1 = float.Parse(m.Groups[1].Value);
                        double f2 = float.Parse(m.Groups[2].Value);

                        value = "" + (random.NextDouble() * (f2 - f1) + f1);
                    }
                }

                // If the value starts with ADD(fieldName, floating point)
                // we have to get the requested value and add the second parameter
                if (i.value.StartsWith("ADD") && info.FieldType.Equals(typeof(double)))
                {
                    Match m = addRegex.Match(i.value);
                    if (m.Success)
                    {
                        String fname = m.Groups [1].Value;
                        double f2    = double.Parse(m.Groups[2].Value);

                        FieldInfo finfo = this.GetType().GetField(fname);
                        if (finfo == null || !finfo.FieldType.Equals(typeof(double)))
                        {
                            continue;
                        }

                        value = "" + ((double)finfo.GetValue(this) + f2);
                    }
                }

                if (i.value.StartsWith("TIME") && info.FieldType.Equals(typeof(double)))
                {
                    Match m = timeRegex.Match(i.value);
                    if (m.Success)
                    {
                        double ys = m.Groups [1].Success ? double.Parse(m.Groups[1].Value) : 0.0;
                        double ds = m.Groups [2].Success ? double.Parse(m.Groups[2].Value) : 0.0;
                        double hs = m.Groups [3].Success ? double.Parse(m.Groups[3].Value) : 0.0;
                        double ms = m.Groups [4].Success ? double.Parse(m.Groups[4].Value) : 0.0;
                        double ss = m.Groups [5].Success ? double.Parse(m.Groups[5].Value) : 0.0;

                        value = "" + (ys * (365.0 * 24.0 * 60.0 * 60.0) + ds * (24.0 * 60.0 * 60.0) + hs * (60.0 * 60.0) + ms * 60.0 + ss);
                    }
                }

                if (!value.Equals(""))
                {
                    ReflectionTools.setValue(i.field, value, this);
                }
            }

            foreach (FieldInfo info in this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
            {
                object obj = info.GetValue(this);
                if (obj is InstructionSet)
                {
                    ((InstructionSet)obj).executeInstructions(random);
                }

                if (obj != null && obj.GetType().GetInterface("IList") != null)
                {
                    IList ilist = (IList)obj;

                    foreach (object v in ilist)
                    {
                        if (v is InstructionSet)
                        {
                            ((InstructionSet)v).executeInstructions(random);
                        }
                    }
                }
            }
        }