Hi Zhencheng,
Thank you for contacting Aspose support team.
Following is the sample code with demonstrates all the required features mentioned above. Please give it a try and share the feedback with us.
public static void main(String[] args)
{
setLicense();
{
Tasks_758092();
}
catch(ParseException e) { e.printStackTrace(); }
System.out.print(“Execution completed successfully”);
}
// write your code here
static void Tasks_758092() throws ParseException
{
String pattern = "yyyy, MM, dd, HH, mm, ss";
SimpleDateFormat format = new SimpleDateFormat(pattern);
Project proj = new Project(“D:/Aspose_DotNetProjects/Blank2010.mpp”);
proj.setCalculationMode(CalculationMode.Automatic);
proj.set(Prj.START_DATE, format.parse(“2004, 1, 1, 8, 0,0”));
proj.getRootTask().getChildren().add(“Finish to Start #1”);
Task parent = proj.getRootTask().getChildren().add(“Parent”);
parent.set(Tsk.START, format.parse(“2004, 1, 5, 8, 0, 0”));
parent.set(Tsk.DURATION, proj.getDuration(8, TimeUnitType.Hour));
parent.outlineIndent();
Task firstChild = proj.getRootTask().getChildren().add(“First Child”);
firstChild.set(Tsk.START, format.parse(“2004, 1, 6, 8, 0, 0”));
firstChild.set(Tsk.DURATION, proj.getDuration(32, TimeUnitType.Hour));
firstChild.outlineIndent();
TaskLink link1 = proj.getTaskLinks().add(parent, firstChild);
Task secondChild = proj.getRootTask().getChildren().add(“Second Child”);
secondChild.set(Tsk.START, format.parse(“2004, 1, 6, 8, 0, 0”));
secondChild.set(Tsk.DURATION, proj.getDuration(32, TimeUnitType.Hour));
secondChild.outlineIndent();
TaskLink link2 = proj.getTaskLinks().add(parent, secondChild);
Task thirdChild = proj.getRootTask().getChildren().add(“Third Child”);
thirdChild.set(Tsk.START, format.parse(“2004, 1, 6, 8, 0, 0”));
thirdChild.set(Tsk.DURATION, proj.getDuration(32, TimeUnitType.Hour));
thirdChild.outlineIndent();
TaskLink link3 = proj.getTaskLinks().add(parent, thirdChild);
proj.getRootTask().getChildren().add(“Second Parent”);
Task FirstGrandChild = proj.getRootTask().getChildren().add(“First grand child”);
FirstGrandChild.set(Tsk.START, format.parse(“2004, 1, 12, 8, 0, 0”));
FirstGrandChild.set(Tsk.DURATION, proj.getDuration(40, TimeUnitType.Hour));
FirstGrandChild.outlineIndent();
TaskLink link4 = proj.getTaskLinks().add(firstChild, FirstGrandChild);
TaskLink link5 = proj.getTaskLinks().add(secondChild, FirstGrandChild);
TaskLink link6 = proj.getTaskLinks().add(thirdChild, FirstGrandChild);
Task SecondGrandChild = proj.getRootTask().getChildren().add(“Second grand child”);
SecondGrandChild.set(Tsk.START, format.parse(“2004, 1, 12, 8, 0, 0”));
SecondGrandChild.set(Tsk.DURATION, proj.getDuration(40, TimeUnitType.Hour));
SecondGrandChild.outlineIndent();
TaskLink link7 = proj.getTaskLinks().add(thirdChild, SecondGrandChild);
TaskLink link8 = proj.getTaskLinks().add(firstChild, SecondGrandChild);
TaskLink link9 = proj.getTaskLinks().add(secondChild, SecondGrandChild);
Task ThirdGrandChild = proj.getRootTask().getChildren().add(“Third Grand child”);
ThirdGrandChild.set(Tsk.START, format.parse(“2004, 1, 12, 8, 0, 0”));
ThirdGrandChild.set(Tsk.DURATION,
proj.getDuration(40, TimeUnitType.Hour));
ThirdGrandChild.outlineIndent();
TaskLink link10 = proj.getTaskLinks().add(secondChild, ThirdGrandChild);
TaskLink link11 = proj.getTaskLinks().add(thirdChild, ThirdGrandChild);
TaskLink link12 = proj.getTaskLinks().add(firstChild, ThirdGrandChild);
Task FourthGrandChild = proj.getRootTask().getChildren().add(“Fourth grand child”);
FourthGrandChild.set(Tsk.START, format.parse(“2004, 1, 14, 8, 0, 0”));
FourthGrandChild.set(Tsk.DURATION, proj.getDuration(40, TimeUnitType.Hour));
FourthGrandChild.outlineIndent();
TaskLink link13 =
proj.getTaskLinks().add(secondChild, FourthGrandChild);
link13.setLinkLag(4800);
link13.setLagFormat(TimeUnitType.Day);
TaskLink link14 = proj.getTaskLinks().add(thirdChild, FourthGrandChild);
link14.setLinkLag(9600);
link14.setLagFormat(TimeUnitType.Day);
Task FifthGrandChild = proj.getRootTask().getChildren().add(“Fifth grand child”);
FifthGrandChild.set(Tsk.START, format.parse(“2004, 1, 12, 8, 0, 0”));
FifthGrandChild.set(Tsk.DURATION, proj.getDuration(40, TimeUnitType.Hour));
FifthGrandChild.outlineIndent();
TaskLink link15 = proj.getTaskLinks().add(secondChild, FifthGrandChild);
link15.setLinkLag(50);
link15.setLagFormat(TimeUnitType.Percent);
link15.setLinkType(TaskLinkType.FinishToFinish);
TaskLink link16 = proj.getTaskLinks().add(thirdChild, FifthGrandChild);
link16.setLinkLag(60);
link16.setLagFormat(TimeUnitType.Percent);
link16.setLinkType(TaskLinkType.StartToStart);
Task SeventhChild = proj.getRootTask().getChildren().add(“Seventh Child”);
//Set Task Duration
SeventhChild.set(Tsk.DURATION, proj.getDuration(8, TimeUnitType.Hour));
SeventhChild.set(Tsk.START, format.parse(“2004, 1, 9, 8, 0, 0”));
SeventhChild.set(Tsk.ACTUAL_START, format.parse(“2004, 1, 9, 8, 0, 0”));
SeventhChild.set(Tsk.PERCENT_COMPLETE, 100);
Resource rsc1 = proj.getResources().add(“Resource 1”);
rsc1.set(Rsc.TYPE, ResourceType.Work);
ResourceAssignment assgn = proj.getResourceAssignments().add(SeventhChild, rsc1);
assgn.set(Asn.START, format.parse(“2004, 1, 9, 8, 0, 0”));
assgn.set(Asn.ACTUAL_START, format.parse(“2004, 1, 9, 8, 0, 0”));
proj.recalculate();
proj.save(“Project1.mpp”, SaveFileFormat.MPP);