Interface ITreeAlgorithmT

ITreeAlgorithm<T> interface

Represents an algorithm that can be applied to a tree of objects T.

public interface ITreeAlgorithm<in T>
ParameterDescription
TThe type of object to apply method interface to.

Methods

NameDescription
Alg(T, int)Processes a node of a tree.
PostAlg(T, int)Called after processing of a node of a tree.
PreAlg(T, int)Called before processing of a node of a tree.

Examples

Shows how to use <see cref=“Aspose.Tasks.Util.ITreeAlgorithm`1” /> tree based algorithm.

public void WorkWithITreeAlgorithm()
{
    var project = new Project(DataDir + "Project1.mpp");

    var root = project.RootTask.Children.Add("Project Management");
    var summary = root.Children.Add("Manage iteration");

    var task = summary.Children.Add("Acquire staff");
    task.Set(Tsk.Start, new DateTime(1999, 5, 3, 9, 0, 0));
    task.Set(Tsk.Duration, project.GetDuration(8 * 14, TimeUnitType.Hour));
    task.Set(Tsk.Finish, project.Get(Prj.Calendar).GetFinishDateByStartAndWork(task.Get(Tsk.Start), task.Get(Tsk.Duration)));

    var resource = project.Resources.Add("Project Manager");
    resource.Set(Rsc.Type, ResourceType.Work);

    project.ResourceAssignments.Add(task, resource);

    // use tree algorithm to gather common work and update work 
    var acc = new WorkAccumulator();
    TaskUtils.Apply(summary, acc, 0);

    var summaryWork = acc.Work.ToDouble();
    summary.Set(Tsk.Work, project.GetWork(summaryWork));
    summary.Set(Tsk.RemainingWork, project.GetWork(summaryWork));

    // ...
}

private class WorkAccumulator : ITreeAlgorithm<Task>
{
    /// <summary>Initializes a new instance of the <see cref="WorkAccumulator" /> class.</summary>
    public WorkAccumulator()
    {
        this.Work = new Duration();
    }

    public Duration Work { get; private set; }

    public void PreAlg(Task el, int level)
    {
        // there is nothing to do in pre algorithm steps
    }

    public void Alg(Task el, int level)
    {
        if (!el.Get(Tsk.IsSummary))
        {
            this.Work.Add(el.Get(Tsk.Work));
        }
    }

    public void PostAlg(Task el, int level)
    {
        // there is nothing to do in post algorithm steps
    }
}

See Also