in Search
 
Home Forums Marketplace Files
 
 
 

determine if a task is placed between two other task in a workflow definition

Last post 06-23-2009, 1:23 AM by koenmatthijssen. 2 replies.
Sort Posts: Previous Next
  •  06-16-2009, 3:25 AM 14715

    determine if a task is placed between two other task in a workflow definition

    Hello,

    i would like to know of a specific task is modelled between two other task. Is their a function that returns this?
  •  06-19-2009, 6:50 AM 14721 in reply to 14715

    Re: determine if a task is placed between two other task in a workflow definition

    Hello.

    I don't know such an existing function, but you could write your own one.

    You can use the Teamplate.Bll.BModel class, which implements the functionality to get the model structure.

    BSession AdminSession = new BSession();
    if (!AdminSession.Connect("", "Admin", ""))
      throw new Exception("Could not login admin.");
    try
    {
      BModel fBModel = new BModel();
      fBModel.SetSessionToken(AdminSession.getToken());
      if (!BModel.Load(ModelId))
        throw new Exception(String.Format("There was no model with the id {0} found!", ModelId));

      if (isTaskBetween(TaskId, BetweenTaskId1, BetweenTaskId2))
        Response.Write(String.Format("Task {0} is between task {1} and task {2}.", TaskId, BetweenTaskId1, BetweenTaskId2));
      else
        Response.Write(String.Format("Task {0} is not between task {1} and task {2}.", TaskId, BetweenTaskId1, BetweenTaskId2));
    }
    finally
    {
      AdminSession.Dispose();
    }

    This code uses the following functions:

    public bool isTaskBetween(int TaskId, int BetweenTaskId1, int BetweentaskId2)
    {
        //Loading the three tasks:
        ModelTask Task = findTask(TaskId);
        ModelTask BetweenTask1 = findTask(BetweenTaskId1);
        ModelTask BetweenTask2 = findTask(BetweenTaskId2);

       //checking, whether Task is located between BetweenTask1 and BetweenTask2
        return isTaskBetween(Task, BetweenTask1, BetweenTask2);
    }
    protected bool isTaskBetween(ModelTask Task, ModelTask BetweenTask1, ModelTask BetweenTask2)
    {
        //Task is located between BetweenTask1 and BetweenTask2 if Task comes after BetweenTask1 and is executed before BetweenTask2 or converse.
        return (comesTaskAfter(Task, BetweenTask1) && comesTaskBefore(Task, BetweenTask2))
                    || (comesTaskAfter(Task, BetweenTask2) && comesTaskBefore(Task, BetweenTask1));
    }

    public ModelTask findTask(int TaskId)
    {
        //This function loads a ModelTask object for the given TaskId.
        if (TaskId < 0)
            throw new Exception(String.Format("Invalid task id \"{0}\"!", TaskId));
        foreach (ModelObject obj in BModel.GetObjects())
        {
            if (!(obj is ModelTask))
                continue;
            ModelTask task = obj as ModelTask;
            if (task.Id != TaskId)
                continue;
            return task;
        }
        return null;
    }

    private List<ModelTask> getAllChildTasks(ModelTask Task)
    {
        //This function gets all tasks which directly follow after the given Task.
        List<ModelTask> result = new List<ModelTask>();
        ModelObjectCollection AllLinks = BModel.GetObjects(ModelObject.ModelType.ModelLink);
        foreach (ModelObject Obj in AllLinks)
        {
            ModelLink Link = (ModelLink)Obj;
            if (Link.Rework)
               continue;
            if (Link.ParentObject != Task.Id)
                continue;
            result.Add(findTask(Link.ChildObject));
        }
        return result;
    }

    protected bool comesTaskAfter(ModelTask Task, ModelTask PreviousTask)
    {
        //This method checks whether Task will be executed after PreviousTask.
        List<ModelTask> Children = getAllChildTasks(PreviousTask);
        foreach (ModelTask Child in Children)
            if (Child.Id == Task.Id)
                return true;
        foreach (ModelTask Child in Children)
            if (comesTaskAfter(Task, Child))
                return true;
        return false;
    }

    protected bool comesTaskBefore(ModelTask Task, ModelTask LaterTask)
    {
        return comesTaskAfter(LaterTask, Task);
    }

    Im know, that this code is not finished. It surely can be optimized and made less error-prone. And probably there is a better way.

    I hope it is understandable and helpful.

    Best regards.
    Martin
  •  06-23-2009, 1:23 AM 14740 in reply to 14721

    Re: determine if a task is placed between two other task in a workflow definition

    Thanks,

    that's what I needed!
View as RSS news feed in XML
  Privacy    Site Terms   Contact Administrator