/// <summary>
 /// Adds the predecessor event to the given event stub.
 /// </summary>
 public void AddPredecessor(EventStub eventStub, EventStub predecessor)
 {
     eventStub.AddPredecessor(predecessor);
     CalculateLevelRecursively(eventStub);
 }
 /// <summary>
 /// Sets the level of the given event to the given level, also applying changes necessary to other events
 /// that might have an overlap in participants.
 /// </summary>
 public void SetLevel(EventStub evnt, int level, bool cascade = false)
 {
     int oldLevel = GetLevelForEvent(evnt);
     //if level does not change, don't need to do anyrhing
     if (oldLevel == level)
     {
         return;
     }
     //if we add it to a higher level, must adapt predecessors and make sure any event in the
     //same level with overlapping participants is recursively set to a higher level
     if (oldLevel > level)
     {
         for (int i = level; i <= oldLevel; i++)
         {
             foreach (EventStub other in GetEventsInLevel(i))
             {
                 evnt.RemovePredecessor(other);
                 if (i == level + 1 && other != evnt)
                 {
                     other.AddPredecessor(evnt);
                 }
             }
         }
         foreach(EventStub other in GetEventsInLevel(level - 1))
         {
             evnt.AddPredecessor(other);
         }
         levelsForEvents[evnt] = level;
         foreach (EventStub other in new List<EventStub>(
             GetEventsInLevel(level).Except(new EventStub[] { evnt })))
         {
             if (other.ParticipantsOverlap(evnt))
             {
                 SetLevel(other, level + 1);
             }
         }
     }
     //if we add it to a lower level, must essentially do the same as wehn adding to a higher level but
     //adapting the predecessors is different
     if (oldLevel < level)
     {
         for (int i = oldLevel; i < level; i++)
         {
             foreach (EventStub other in GetEventsInLevel(i))
             {
                 other.RemovePredecessor(evnt);
                 if (i == level - 1 && other != evnt)
                 {
                     evnt.AddPredecessor(other);
                 }
             }
         }
         levelsForEvents[evnt] = level;
         foreach (EventStub other in new List<EventStub>(
             GetEventsInLevel(level).Except(new EventStub[] { evnt })))
         {
             if (other.ParticipantsOverlap(evnt) || (other.Predecessors.Contains(evnt) && cascade))
             {
                 SetLevel(other, level + 1, true);
             }
             else
             {
                 other.RemovePredecessor(evnt);
             }
         }
     }
     //Clean up any level that may be empty.
     CleanupEmptyLevels(1);
     //Calculate the level for any object in the event, as it might change
     foreach (SmartObject obj in evnt.InvolvedObjects)
     {
         CalculateLevel(obj);
     }
 }