Ejemplo n.º 1
0
        /// <param name="id">what incident to execute the given procedure on</param>
        /// <param name="procedure">what to do for each of the count iterations</param>
        /// <param name="count">how many times to execute the given procedure</param>
        /// <param name="onLast">special logic to do in addition to the standard procedure on the last iteration</param>
        /// <exception cref="Exception"></exception>
        public void OnIncident(Proc.Id id, Proc.edure procedure, int count = -1, Proc.edure onLast = null)
        {
            if (count == 0)
            {
                return;
            }
            List <Proc.edure> responses = incidentResponseTable[id.Value];

            if (count < 0)
            {
                responses.Add(procedure);
                return;
            }
            Proc.edure countLimitedProcedure = incident => {
                if (count <= 0)
                {
                    throw new Exception("how was count decremented outside of this function?");
                }
                Proc.Result result = procedure.Invoke(incident);
                --count;
                if (count > 0)
                {
                    return(result);
                }
                if (onLast != null && result == Proc.Result.Success)
                {
                    result = onLast.Invoke(incident);
                }
                RemoveIncident(id.Value, procedure);
                return(result);
            };
            responseAilias[procedure] = countLimitedProcedure;
            responses.Add(countLimitedProcedure);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// execute the procedure of this strategy, then continue to the next logical strategy
 /// </summary>
 public Proc.Result InvokeChain(Incident incident)
 {
     Proc.Result result = Invoke(incident);
     if (Next == null || result != Proc.Result.Success)
     {
         return(result);
     }
     return(ContinueChain(incident));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// execute the procedure of this strategy (without continuing to the next logical strategy)
        /// </summary>
        public Proc.Result Invoke(Incident incident)
        {
            Proc.Result result       = Proc.Result.Success;
            bool        allowedToRun = !IsExceptionHandler &&
                                       (!OnlyExecuteOnPositiveMerit || (MeritHeuristic == null || MeritHeuristic.Invoke() > 0));

            //Debug.Log("Invoking " + Identifier+ " "+allowedToRunOnMerit+" "+Procedure);
            if (allowedToRun && Procedure != null)
            {
                result = Invoke_Internal(incident);
            }
            return(result);
        }
Ejemplo n.º 4
0
 internal Proc.Result Invoke_Internal(Incident incident)
 {
     Proc.Result result = Proc.Result.Success;
     if (Procedure == null)
     {
         return(result);
     }
     try {
         result = Procedure.Invoke(incident);
     } catch (Exception e) {
         Show.Error(e);
         HandleException(e, incident);
         return(Proc.Result.Halt);
     }
     return(result);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// keep executing procedure until it returns Proc.Result.Success.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="procedure">incident listener is removed if result is <see cref="Proc.Result.Success"/>. should return <see cref="Proc.Result.Halt"/> until it should stop</param>
        /// <param name="onFinish"></param>
        public void WhileIncident(Proc.Id id, Proc.edure procedure, Proc.edure onFinish)
        {
            List <Proc.edure> responses = incidentResponseTable[id.Value];

            Proc.edure resultLimitedProcedure = incident => {
                Proc.Result result = procedure.Invoke(incident);
                if (result == Proc.Result.Success || result == Proc.Result.Failure)
                {
                    RemoveIncident(id, procedure);
                    onFinish.Invoke(incident);
                    return(Proc.Result.Success);
                }
                return(result);
            };
            responseAilias[procedure] = resultLimitedProcedure;
            responses.Add(resultLimitedProcedure);
        }