Beispiel #1
0
        public virtual object Clone()
        {
            Codelet clone = (Codelet)MemberwiseClone();

            clone.Reset();
            return(clone);
        }
        /// <summary>
        /// Request to evaluate the codelet with given time
        /// </summary>
        public bool EvaluateCodelet(Codelet codelet, bool debugMode)
        {
            if (codelet.GetFutureCodelet(nameActive) != null)
            {
                return(false);
            }
            currentEvaluating = codelet;

            codelet.AddFutureCodelet(nameActive, isEvaluating);
            if (debugMode)
            {
                //Profiler.Start(codelet.GetType().FullName);
                //SingleUserLog.Update(codelet.ToString(), Profiler.NumExecuted(codelet.ToString()));
                //SingleUserLog.Replace(codelet.ToString());
            }
            receiver.Receive("EvaluateCodelet", codelet);
            bool done = codelet.Evaluate();

            if (debugMode)
            {
                //Profiler.End(used * 1000);
            }

            codelet.RemoveFutureCodelet(nameActive);

            if (done && codelet.Salience > 0)
            {
                // Complete it!
                CompleteCodelet(codelet);
            }
            currentEvaluating = null;

            return(done);
        }
 /// <summary>
 /// Remove the given codelet
 /// </summary>
 public void DeleteCodelet(Codelet codelet)
 {
     // Console.WriteLine("Delete " + codelet.ToString)
     if (!codelets.Remove(codelet.Salience, codelet))
     {
         receiver.Receive("Could not delete " + codelet.ToString(), codelet);
     }
 }
 /// <summary>
 /// Create a new coderack
 /// </summary>
 public Coderack(IMessageReceiver receiver)
 {
     codelets      = new SalienceTree <Codelet>();
     isEvaluating  = new DescriptorCodelet(this, "evaluating");
     isCompleting  = new DescriptorCodelet(this, "completing");
     watching      = false;
     this.receiver = receiver;
 }
 /// <summary> 
 /// Create a new coderack
 /// </summary> 
 public Coderack(IMessageReceiver receiver)
 {
     codelets = new SalienceTree<Codelet>();
     isEvaluating = new DescriptorCodelet(this, "evaluating");
     isCompleting = new DescriptorCodelet(this, "completing");
     watching = false;
     this.receiver = receiver;
 }
Beispiel #6
0
        /// <summary>
        /// Remove a future codelet with the specified key
        /// </summary>
        public void RemoveFutureCodelet(string key)
        {
            Codelet child = GetFutureCodelet(key);

            if (child != null)
            {
                children.Remove(key);
            }
        }
        /// <summary>
        /// Delete a codelet at random
        /// </summary>
        public virtual void DeleteRandomCodelet()
        {
            Codelet codelet = SelectRandomCodelet();

            if (codelet.GetFutureCodelet(nameActive) == null)
            {
                DeleteCodelet(codelet);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Check if we have a future codelet with the specified key
        /// </summary>
        public Codelet GetFutureCodelet(string key)
        {
            Codelet found = null;

            if (children.TryGetValue(key, out found))
            {
                return(found);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Request to complete a codelet
        /// </summary>
        public virtual bool CompleteCodelet(Codelet codelet)
        {
            if (codelet.GetFutureCodelet(nameActive) != null)
            {
                return(true);
            }

            codelet.AddFutureCodelet(nameActive, isCompleting);
            if (codelet.Complete())
            {
                DeleteCodelet(codelet);
                return(true);
            }
            else
            {
                codelet.RemoveFutureCodelet(nameActive);
                return(false);
            }
        }
        /// <summary>
        /// Add the given codelet
        /// </summary>
        public virtual bool AddCodelet(Codelet codelet, string location)
        {
            //Console.WriteLine("Add " + codelet.ToString() + ": " + location + ": " + codelets.Count);
            if (codelet.coderack != this)
            {
                throw new ArgumentException("codelet does not have this coderack!");
            }

            if (currentEvaluating != null)
            {
                codelet.Trace = currentEvaluating.Trace.AppendFrame(currentEvaluating, location);
            }
            else
            {
                codelet.Trace.AppendFrame(null, location);
            }
            codelets.Add(codelet.Salience, codelet);
            if (watching)
            {
                codelet.watched = true;  // we're watched if our adder was
            }
            return(true);
        }
        /// <summary> 
        /// Request to evaluate the codelet with given time
        /// </summary> 
        public bool EvaluateCodelet(Codelet codelet, bool debugMode)
        {
            if (codelet.GetFutureCodelet(nameActive) != null)
                return false;
            currentEvaluating = codelet;

            codelet.AddFutureCodelet(nameActive, isEvaluating);
            if (debugMode) {
                //Profiler.Start(codelet.GetType().FullName);
                //SingleUserLog.Update(codelet.ToString(), Profiler.NumExecuted(codelet.ToString()));
                //SingleUserLog.Replace(codelet.ToString());
            }
            receiver.Receive("EvaluateCodelet", codelet);
            bool done = codelet.Evaluate();
            if (debugMode) {
                //Profiler.End(used * 1000);
            }

            codelet.RemoveFutureCodelet(nameActive);

            if (done && codelet.Salience > 0) {
                // Complete it!
                CompleteCodelet(codelet);
            }
            currentEvaluating = null;

            return done;
        }
 /// <summary> 
 /// Remove the given codelet
 /// </summary> 
 public void DeleteCodelet(Codelet codelet)
 {
     // Console.WriteLine("Delete " + codelet.ToString)
     if (!codelets.Remove(codelet.Salience, codelet))
         receiver.Receive("Could not delete " + codelet.ToString(), codelet);
 }
        /// <summary> 
        /// Request to complete a codelet
        /// </summary> 
        public virtual bool CompleteCodelet(Codelet codelet)
        {
            if (codelet.GetFutureCodelet(nameActive) != null)
                return true;

            codelet.AddFutureCodelet(nameActive, isCompleting);
            if (codelet.Complete()) {
                DeleteCodelet(codelet);
                return true;
            } else {
                codelet.RemoveFutureCodelet(nameActive);
                return false;
            }
        }
 /// <summary> 
 /// Adjust the total amount of salience, as a result of a codelet changing
 /// </summary> 
 public void ChangeSalience(Codelet codelet, double before, double after)
 {
     codelets.ChangeSalience(codelet, before, after);
 }
        /// <summary> 
        /// Add the given codelet
        /// </summary> 
        public virtual bool AddCodelet(Codelet codelet, string location)
        {
            //Console.WriteLine("Add " + codelet.ToString() + ": " + location + ": " + codelets.Count);
            if (codelet.coderack != this)
                throw new ArgumentException("codelet does not have this coderack!");

            if (currentEvaluating != null)
                codelet.Trace = currentEvaluating.Trace.AppendFrame(currentEvaluating, location);
            else
                codelet.Trace.AppendFrame(null, location);
            codelets.Add(codelet.Salience, codelet);
            if (watching)
                codelet.watched = true;  // we're watched if our adder was

            return true;
        }
        /// <summary>
        /// Execute the coderack for a given amount of time
        /// </summary>
        public override void Execute(int time, bool debugMode)
        {
            //int origtime = time;
            // Profiler profiler = new Profiler();
            SalienceList <Codelet> codelist = (SalienceList <Codelet>)codelets;

            while (time > 0 && CountTop > 0)
            // && (profiler.GetTime() / 1000 < origtime))
            {
                // start from the end and move backwards, since new are added to end
                LinkedListNode <double>  keys   = codelist.LinkedKeys.Last;
                LinkedListNode <Codelet> values = codelist.LinkedValues.Last;
                if (values == null)
                {
                    // This shouldn't happen-- we're out of executable codelets
                    break;
                }

                while (values != null)
                {
                    Codelet codelet = values.Value;
                    if (values.Previous != null)
                    {
                        // Use this one or the last one?
                        if ((keys.Previous.Value + keys.Value) * randgen.NextDouble() > keys.Value)
                        {
                            codelet = values.Previous.Value;
                        }
                    }

                    if (codelet == null)
                    {
                        // This shouldn't happen-- we're out of executable codelets
                        break;
                    }

                    if (!codelet.NeedsEvaluation)
                    {
                        // Skip it!
                        time -= 1;
                        continue;
                    }

                    int codetime = codelet.time;
                    watching = codelet.watched;

                    // Console.WriteLine("=(" + CountPrime.ToString + "/" + codelets.Count.ToString + ")=> " + codelet.ToString())
                    try
                    {
                        EvaluateCodelet(codelet, debugMode);
                    }
                    catch (UserException e) {
                        receiver.Receive(e.Message, codelet);
                        if (!codelet.immune)
                        {
                            // Remove the bad codelet
                            DeleteCodelet(codelet);
                        }
                        else
                        {
                            codelet.RemoveFutureCodelet(nameActive);
                        }
                        if (debugMode)
                        {
                            throw new Exception(e.Message + " - " + e.StackTrace, e);
                        }
                    }
                    catch (Exception e)
                    {
                        // Record exception here!
                        receiver.Receive(codelet.ToString() + " threw exception " + e.Message, codelet);
                        receiver.Receive(e.StackTrace, e);
                        if (!codelet.immune)
                        {
                            // Remove the bad codelet
                            DeleteCodelet(codelet);
                        }
                        else
                        {
                            codelet.RemoveFutureCodelet(nameActive);
                        }
                        if (debugMode)
                        {
                            throw new Exception(e.Message + " - " + e.StackTrace, e);
                        }
                    }

                    watching = false;
                    time    -= codetime;

                    // Move back two
                    values = values.Previous;
                    keys   = keys.Previous;
                    if (values != null)
                    {
                        values = values.Previous;
                        keys   = keys.Previous;
                    }
                }
            }

            // Check if we're taking the time we say we are

            /*long micros = (long)(profiler.GetTime() / 1000);
             * if (Math.Abs((origtime - time) - micros) > Math.Min((origtime - time), micros) / 100)
             * {
             *  receiver.Receive("Time Miscalculation: " + (origtime - time).ToString() + " units took " + micros.ToString(), this);
             *  if (debugMode)
             *  {
             *      receiver.Receive(Profiler.AnnounceWorst(), this);
             *      //receiver.Receive(profiler.AnnounceEach(), this);
             *      //throw new Exception(Profiler.AnnounceEach());
             *      //SingleUserLog.Replace("Complete");
             *  }
             * }*/

            if (time > 0)
            {
                receiver.Receive("Coderack Exhausted.", this);
            }
            else
            {
                receiver.Receive("Time Limit Reached.", this);
            }
        }
        public override int ExecuteOne(bool debugMode)
        {
            SalienceList <Codelet> codelist = (SalienceList <Codelet>)codelets;

            // start from the end and move backwards, since new are added to end
            LinkedListNode <double>  keys   = codelist.LinkedKeys.Last;
            LinkedListNode <Codelet> values = codelist.LinkedValues.Last;

            if (values == null)
            {
                return(1);   // nothing to do!
            }
            Codelet codelet = values.Value;

            if (values.Previous != null)
            {
                // Use this one or the last one?
                if ((keys.Previous.Value + keys.Value) * randgen.NextDouble() > keys.Value)
                {
                    codelet = values.Previous.Value;
                }
            }

            if (codelet == null)
            {
                return(1);   // nothing to do!
            }
            if (!codelet.NeedsEvaluation)
            {
                // Skip it!
                return(1);
            }

            int time = codelet.time;

            watching = codelet.watched;
            bool done = false;

            // Console.WriteLine("=(" + CountPrime.ToString + "/" + codelets.Count.ToString + ")=> " + codelet.ToString())
            try
            {
                done = EvaluateCodelet(codelet, debugMode);
            }
            catch (Exception e)
            {
                // Record exception here!
                receiver.Receive(codelet.ToString() + " threw exception " + e.Message, codelet);
                receiver.Receive(e.StackTrace, e);
                if (!codelet.immune)
                {
                    // Remove the bad codelet
                    DeleteCodelet(codelet);
                }
                else
                {
                    codelet.RemoveFutureCodelet(nameActive);
                }
                if (debugMode)
                {
                    throw new Exception(e.Message + " - " + e.StackTrace, e);
                }
            }

            watching = false;

            // Move back two
            values = values.Previous;
            keys   = keys.Previous;
            if (values != null)
            {
                values = values.Previous;
                keys   = keys.Previous;
            }

            return(time);
        }
        // return codelet's time
        public virtual int ExecuteOne(bool debugMode)
        {
            Codelet codelet = SelectSalientCodelet();

            if (codelet == null)
            {
                return(1); // nothing to do!
            }
            if (!codelet.NeedsEvaluation)
            {
                // Skip it!
                return(1);
            }

            int time = codelet.time;

            watching = codelet.watched;
            bool done = false;

            try
            {
                done = EvaluateCodelet(codelet, debugMode);
            }
            catch (UserException e) {
                receiver.Receive(e.Message, codelet);
                if (!codelet.immune)
                {
                    // Remove the bad codelet
                    DeleteCodelet(codelet);
                }
                else
                {
                    codelet.RemoveFutureCodelet(nameActive);
                }
                if (debugMode)
                {
                    throw new Exception(e.Message + " - " + e.StackTrace, e);
                }
            }
            catch (Exception e)
            {
                // Record exception here!
                receiver.Receive(codelet.ToString() + " threw exception " + e.Message, codelet);
                receiver.Receive(e.StackTrace, e);
                if (!codelet.immune)
                {
                    // Remove the bad codelet
                    DeleteCodelet(codelet);
                }
                else
                {
                    codelet.RemoveFutureCodelet(nameActive);
                }
                if (debugMode)
                {
                    throw new Exception(e.Message + " - " + e.StackTrace, e);
                }
            }

            watching = false;

            return(time);
        }
Beispiel #19
0
 /// <summary>
 /// Add a future codelet with the specified key
 /// </summary>
 public void AddFutureCodelet(string key, Codelet codelet)
 {
     children.Add(key, codelet);
 }
 /// Deserialization constructor
 public Coderack()
 {
     isEvaluating = new DescriptorCodelet(this, "evaluating");
     isCompleting = new DescriptorCodelet(this, "completing");
     watching     = false;
 }
 /// <summary>
 /// Adjust the total amount of salience, as a result of a codelet changing
 /// </summary>
 public void ChangeSalience(Codelet codelet, double before, double after)
 {
     codelets.ChangeSalience(codelet, before, after);
 }
 /// Deserialization constructor
 public Coderack()
 {
     isEvaluating = new DescriptorCodelet(this, "evaluating");
     isCompleting = new DescriptorCodelet(this, "completing");
     watching = false;
 }
Beispiel #23
0
 /// <summary> 
 /// Add a future codelet with the specified key
 /// </summary> 
 public void AddFutureCodelet(string key, Codelet codelet)
 {
     children.Add(key, codelet);
 }