Handling Task Constraints
Task constraints are used to define a Microsoft Project project’s schedule and define when tasks should be started or finished. Constraints can be flexible - start or end as soon as possible or as late as possible - or inflexible. Inflexible constraints are tied to specific dates.
Working with Constraints
The ConstraintDate and ConstraintType properties are exposed by the Task class to handle constraints:
- ConstraintDate accepts and returns the Date data type.
- ConstraintType accepts and returns one of the values of the ConstraintType enumeration.
Setting Constraints in Microsoft Project
To set constraints in Microsoft Project:
- On the View menu, select More Views and then Task Entry Form.
- Double click a task on the Task Entry Form.
- Select the Advance tab.
- Set a constraint by selecting an option from the Constraint type list and a date for the Constraint date list.
Setting constraints in Microsoft Project
Setting Constraints with Aspose.Tasks
The constraint date is NA when the constraint type is As Soon As Possible or As Late As Possible. For date values equal to NA, Aspose.Tasks uses the value “1/1/2000” in the evaluation version, and DateTime.MinValue for the licensed product. In the below case, we take a source project file as an input and apply different types of constraints on various tasks in each case. The following code samples show the application of different Constraint types and the accompanied snapshot of the result for each case.
The input file
The code samples below set the constraint type set to Start No Earlier Than.
1// Create project instance
2Project project = new Project(dataDir + "ConstraintStartNoEarlierThan.mpp");
3
4// Set constraint Start No Earlier Than on task with Id 1
5Task summary = project.getRootTask().getChildren().getById(1);
6summary.set(Tsk.CONSTRAINT_TYPE, ConstraintType.StartNoEarlierThan);
7
8java.util.Calendar cal = java.util.Calendar.getInstance();
9cal.set(2013, 6, 3, 9, 0, 0);
10summary.set(Tsk.CONSTRAINT_DATE, cal.getTime());
11
12// Save project as pdf
13SaveOptions o = new PdfSaveOptions();
14o.setStartDate(project.get(Prj.START_DATE));
15o.setTimescale(Timescale.ThirdsOfMonths);
16project.save(dataDir + "project_StartNoEarlierThan_out.pdf", o);
Output file with Start No Earlier Than constraint
The code samples below set the constraint type set to Finish No Earlier Than.
1// Create project instance
2Project project = new Project(dataDir + "ConstraintStartNoEarlierThan.mpp");
3
4// Set constraint Start No Earlier Than on task with Id 1
5Task summary = project.getRootTask().getChildren().getById(1);
6summary.set(Tsk.CONSTRAINT_TYPE, ConstraintType.FinishNoEarlierThan);
7
8java.util.Calendar cal = java.util.Calendar.getInstance();
9cal.set(2013, 6, 1, 18, 0, 0);
10summary.set(Tsk.CONSTRAINT_DATE, cal.getTime());
11
12// Save project as pdf
13SaveOptions o = new PdfSaveOptions();
14o.setStartDate(project.get(Prj.START_DATE));
15o.setTimescale(Timescale.ThirdsOfMonths);
16project.save(dataDir + "project_StartNoEarlierThan_out.pdf", o);
Output file showing Finish No Earlier Than constraint
The code samples below set the constraint type set to Must Start On.
1// Create project instance
2Project project = new Project(dataDir + "ConstraintStartNoEarlierThan.mpp");
3
4// Set constraint Must Start On for task with Id 2
5Task roof = project.getRootTask().getChildren().getById(2);
6roof.set(Tsk.CONSTRAINT_TYPE, ConstraintType.MustFinishOn);
7
8java.util.Calendar cal = java.util.Calendar.getInstance();
9cal.set(2013, 6, 1, 18, 0, 0);
10roof.set(Tsk.CONSTRAINT_DATE, cal.getTime());
11
12// Save project as pdf
13SaveOptions options = new PdfSaveOptions();
14options.setStartDate(project.get(Prj.START_DATE));
15options.setTimescale(Timescale.ThirdsOfMonths);
16project.save(dataDir + "project_MustStartOn_out.pdf", options);
Output file showing Must Start On constraint
The code samples below set the constraint type set to As Late As Possible.
1// Create project instance
2Project project = new Project(dataDir + "ConstraintStartNoEarlierThan.mpp");
3
4// Set constraint As Late As Possible for task with Id 11
5Task wallBoard = project.getRootTask().getChildren().getById(11);
6wallBoard.set(Tsk.CONSTRAINT_TYPE, ConstraintType.AsLateAsPossible);
7
8// Save project as pdf
9SaveOptions options = new PdfSaveOptions();
10options.setStartDate(project.get(Prj.START_DATE));
11options.setTimescale(Timescale.ThirdsOfMonths);
12project.save(dataDir + "project_AsLateAsPossible_out.pdf", options);
Output file showing As Late As Possible constraint
The code sample below shows the constraint type set to Must Finish On.
1// Create project instance
2Project project = new Project(dataDir + "ConstraintStartNoEarlierThan.mpp");
3
4// Set constraint Must Finish On for task with Id 15
5Task interiorFixtures = project.getRootTask().getChildren().getById(15);
6interiorFixtures.set(Tsk.CONSTRAINT_TYPE, ConstraintType.MustFinishOn);
7
8java.util.Calendar cal = java.util.Calendar.getInstance();
9cal.set(2013, 9, 21, 18, 0, 0);
10interiorFixtures.set(Tsk.CONSTRAINT_DATE, cal.getTime());
11
12// Save project as pdf
13SaveOptions options = new PdfSaveOptions();
14options.setStartDate(project.get(Prj.START_DATE));
15options.setTimescale(Timescale.ThirdsOfMonths);
16project.save(dataDir + "project_MustFinishOn_out.pdf", options);
Output file showing Must Finish On constraint
Getting Constraints
This code sample displays any constraints found when traversing the tasks in the project to a command window.
1Project project = new Project(dataDir + "Project2.mpp");
2
3// Create a ChildTasksCollector instance
4ChildTasksCollector collector = new ChildTasksCollector();
5
6// Collect all the tasks from RootTask using TaskUtils
7TaskUtils.apply(project.getRootTask(), collector, 0);
8
9// Parse through all the collected tasks
10for (Task tsk1 : collector.getTasks())
11{
12 if (tsk1.get(Tsk.CONSTRAINT_DATE).toString() == "1/1/2000")
13 System.out.println("NA");
14 else
15 System.out.println(tsk1.get(Tsk.CONSTRAINT_DATE).toString());
16
17 System.out.println(tsk1.get(Tsk.CONSTRAINT_TYPE).toString());
18}