private void Load(Storage.Storage storage)
        {
            Storage.Records.Job job = storage.ReadJob();

            d_name     = job.Name;
            d_priority = job.Priority;
            d_token    = job.Token;
            d_timeout  = job.Timeout;
            d_filename = job.Filename;

            d_optimizer = Registry.Create(job.Optimizer.Name);

            if (d_optimizer == null)
            {
                throw new Exception("Could not find optimizer");
            }

            d_storage = storage;
            d_optimizer.FromStorage(storage, job.Optimizer);
            d_dispatcher.Name = job.Dispatcher.Name;

            foreach (KeyValuePair <string, string> pair in job.Dispatcher.Settings)
            {
                d_dispatcher.Settings[pair.Key] = ExpandJobPath(pair.Value);
            }

            foreach (string ext in job.Extensions)
            {
                Extension e = Extension.Create(this, ext);

                if (e == null)
                {
                    throw new Exception(String.Format("XML: Could not find extension `{0}'", ext));
                }

                e.FromStorage(storage, job.Optimizer);
            }
        }
Exemple #2
0
        public Records.Job ReadJob()
        {
            Records.Job job     = new Records.Job();
            object[]    jobspec = QueryFirst("SELECT `name`, `priority`, `timeout`, `token`, `optimizer`, `dispatcher`, `filename` FROM `job`");

            job.Name            = As <string>(jobspec[0]);
            job.Priority        = As <double>(jobspec[1], 1);
            job.Timeout         = As <double>(jobspec[2]);
            job.Token           = As <string>(jobspec[3]);
            job.Optimizer.Name  = As <string>(jobspec[4]);
            job.Dispatcher.Name = As <string>(jobspec[5]);
            job.Filename        = As <string>(jobspec[6]);

            /* Optimizer stuff */
            Query("SELECT `name`, `value` FROM `settings`", delegate(IDataReader reader) {
                job.Optimizer.Settings[As <string>(reader[0])] = As <string>(reader[1]);
                return(true);
            });

            Dictionary <string, Records.Boundary> boundaries = new Dictionary <string, Records.Boundary>();

            Query(@"SELECT `name`, `min_repr`, `max_repr`, `min_initial_repr`, `max_initial_repr`
			        FROM `boundaries`"            , delegate(IDataReader reader) {
                Records.Boundary boundary = new Records.Boundary();

                boundary.Name       = As <string>(reader[0]);
                boundary.Min        = As <string>(reader[1]);
                boundary.Max        = As <string>(reader[2]);
                boundary.MinInitial = As <string>(reader[3]);
                boundary.MaxInitial = As <string>(reader[4]);

                job.Optimizer.Boundaries.Add(boundary);
                boundaries[boundary.Name] = boundary;

                return(true);
            });

            Query("SELECT `name`, `boundary` FROM `parameters`", delegate(IDataReader reader) {
                Records.Parameter parameter = new Records.Parameter();
                parameter.Name     = As <string>(reader[0]);
                parameter.Boundary = boundaries[As <string>(reader[1])];

                job.Optimizer.Parameters.Add(parameter);
                return(true);
            });

            /* Dispatcher stuff */
            Query("SELECT `name`, `value` FROM `dispatcher`", delegate(IDataReader reader) {
                job.Dispatcher.Settings[As <string>(reader[0])] = As <string>(reader[1]);
                return(true);
            });

            /* Fitness stuff */
            Query("SELECT `name`, `value`, `mode` FROM `fitness_settings`", delegate(IDataReader reader) {
                string name = As <string>(reader[0]);
                string val  = As <string>(reader[1]);
                string mode = As <string>(reader[2]);

                if (name == "__expression__")
                {
                    job.Optimizer.Fitness.Expression = val;
                }
                else if (name == "__mode__")
                {
                    job.Optimizer.Fitness.Mode = val;
                }
                else
                {
                    job.Optimizer.Fitness.Variables.Add(name, new Records.Fitness.Variable(val, mode));
                }

                return(true);
            });

            /* State stuff */
            Int64 iteration = As <Int64>(QueryValue("SELECT COUNT(iteration) FROM iteration"));

            if (iteration > 0)
            {
                Query("SELECT * FROM `state` ORDER BY iteration DESC LIMIT 1", delegate(IDataReader reader) {
                    byte[] random = (byte[])reader["random"];

                    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter;
                    formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                    MemoryStream stream = new MemoryStream();
                    stream.Write(random, 0, random.Length);
                    stream.Seek(0, SeekOrigin.Begin);

                    job.Optimizer.State.Random = (Optimization.Random)formatter.Deserialize(stream);
                    stream.Close();

                    for (int i = 0; i < reader.FieldCount; ++i)
                    {
                        string name = reader.GetName(i);

                        if (name.StartsWith("_s_"))
                        {
                            job.Optimizer.State.Settings.Add(name.Substring(3), As <string>(reader[i]));
                        }
                    }

                    return(false);
                });
            }

            /* Extensions stuff */
            Query("SELECT `name` FROM `extensions`", delegate(IDataReader reader) {
                string name = As <string>(reader[0]);

                job.Extensions.Add(name);
                return(true);
            });

            return(job);
        }