public override void Add(TriggerCategory cat, int id, TriggerHandler handler, string description = null)
        {
            Trigger trigger = new Trigger(cat, id);

            if (description != null)
            {
                descriptions.Add(trigger, description);
            }
            if (!handlers.ContainsKey(trigger))
            {
                switch (trigger.Category)
                {
                case TriggerCategory.Cause:
                    triggerCauseIdCounter++;
                    break;

                case TriggerCategory.Condition:
                    triggerConditionIdCounter++;
                    break;

                case TriggerCategory.Effect:
                    triggerEffectIdCounter++;
                    break;

                case TriggerCategory.Flow:
                    triggerFlowIdCounter++;
                    break;
                }
                handlers.Add(trigger, handler);
            }
            else
            {
                throw new UnauthorizedAccessException($"Override of existing Trigger {trigger}'s handler with handler in {handler.Method}.");
            }
        }
 internal Trigger(TriggerCategory cat, int id, SourcePosition sourcePos)
 {
     category       = cat;
     this.id        = id;
     SourcePosition = sourcePos;
     contents       = new List <IExpression>();
 }
示例#3
0
 /// <summary>
 /// Determines whether the block contains the trigger.
 /// </summary>
 /// <param name="cat">The category.</param>
 /// <param name="id">The identifier.</param>
 /// <param name="startIndex">Index in the block to start from</param>
 /// <returns>
 ///   <c>true</c> if the block contains the trigger; otherwise, <c>false</c>.
 /// </returns>
 public bool ContainsTrigger(TriggerCategory cat, int id = -1, int startIndex = 0)
 {
     if (startIndex <= Count - 1)
     {
         for (int i = startIndex; i <= Count - 1; i++)
         {
             Trigger trigger = base[i];
             if (trigger.Category == cat)
             {
                 if (id == -1 || trigger.Id == id)
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
示例#4
0
 /// <summary>
 /// Operates like IndexOf for Triggers
 /// </summary>
 /// <param name="cat"></param>
 /// <param name="id"></param>
 /// <param name="startIndex"></param>
 /// <returns>Index of trigger or -1 if not found</returns>
 public int IndexOfTrigger(TriggerCategory cat, int id = -1, int startIndex = 0)
 {
     if (startIndex <= Count - 1)
     {
         for (int i = startIndex; i <= Count - 1; i++)
         {
             Trigger trigger = base[i];
             if (trigger.Category == cat)
             {
                 if (id == -1 || trigger.Id == id)
                 {
                     return(i);
                 }
             }
         }
     }
     return(-1);
 }
示例#5
0
        public int LastIndexOfTrigger(TriggerCategory cat, int id = -1, int index = 0)
        {
            int lastIndex = -1;

            if (index <= Count - 1)
            {
                for (int i = index; i <= Count - 1; i++)
                {
                    Trigger trigger = base[i];
                    if (trigger.Category == cat)
                    {
                        if (id == -1 || trigger.Id == id)
                        {
                            lastIndex = i;
                        }
                    }
                }
            }
            return(lastIndex);
        }
        /// <summary>
        /// Registers a Trigger to the TriggerHandler with optional description
        /// </summary>
        /// <param name="cat"></param>
        /// <param name="id"></param>
        /// <param name="handler"></param>
        /// <param name="description"></param>
        public void Add(TriggerCategory cat, TriggerHandler handler, string description = null)
        {
            int id = BaseId;

            switch (cat)
            {
            case TriggerCategory.Cause:
                id += triggerCauseIdCounter++;
                break;

            case TriggerCategory.Condition:
                id += triggerConditionIdCounter++;
                break;

            case TriggerCategory.Effect:
                id += triggerEffectIdCounter++;
                break;

            case TriggerCategory.Flow:
                id += triggerFlowIdCounter++;
                break;
            }

            Trigger trigger = new Trigger(cat, id);

            if (description != null)
            {
                descriptions.Add(trigger, description);
            }
            if (!handlers.ContainsKey(trigger))
            {
                handlers.Add(trigger, handler);
            }
            // the below should never happen with this approach provided the BaseId is a unassigned trigger id
            else
            {
                throw new UnauthorizedAccessException($"Override of existing Trigger {trigger}'s handler with handler in {handler.Method}.");
            }
        }