/// <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>
        /// 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);
            }
        }
Example #3
0
        /// <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;
        }
Example #4
0
        /// <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>
        /// 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);
        }