Java Code Examples for net.sf.mpxj.Task#getPredecessors()

The following examples show how to use net.sf.mpxj.Task#getPredecessors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlannerWriter.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * This method writes predecessor data to a Planner file.
 * We have to deal with a slight anomaly in this method that is introduced
 * by the MPX file format. It would be possible for someone to create an
 * MPX file with both the predecessor list and the unique ID predecessor
 * list populated... which means that we must process both and avoid adding
 * duplicate predecessors. Also interesting to note is that MSP98 populates
 * the predecessor list, not the unique ID predecessor list, as you might
 * expect.
 *
 * @param mpxjTask MPXJ task instance
 * @param plannerTask planner task instance
 */
private void writePredecessors(Task mpxjTask, net.sf.mpxj.planner.schema.Task plannerTask)
{
   Predecessors plannerPredecessors = m_factory.createPredecessors();
   plannerTask.setPredecessors(plannerPredecessors);
   List<Predecessor> predecessorList = plannerPredecessors.getPredecessor();
   int id = 0;

   List<Relation> predecessors = mpxjTask.getPredecessors();
   for (Relation rel : predecessors)
   {
      Integer taskUniqueID = rel.getTargetTask().getUniqueID();
      Predecessor plannerPredecessor = m_factory.createPredecessor();
      plannerPredecessor.setId(getIntegerString(++id));
      plannerPredecessor.setPredecessorId(getIntegerString(taskUniqueID));
      plannerPredecessor.setLag(getDurationString(rel.getLag()));
      plannerPredecessor.setType(RELATIONSHIP_TYPES.get(rel.getType()));
      predecessorList.add(plannerPredecessor);
      m_eventManager.fireRelationWrittenEvent(rel);
   }
}
 
Example 2
Source File: PrimaveraPMFileWriter.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Writes task predecessor links to a PM XML file.
 *
 * @param task MPXJ Task instance
 */
private void writePredecessors(Task task)
{
   List<Relation> relations = task.getPredecessors();
   for (Relation mpxj : relations)
   {
      RelationshipType xml = m_factory.createRelationshipType();
      m_project.getRelationship().add(xml);

      xml.setLag(getDuration(mpxj.getLag()));
      xml.setObjectId(Integer.valueOf(++m_relationshipObjectID));
      xml.setPredecessorActivityObjectId(mpxj.getTargetTask().getUniqueID());
      xml.setSuccessorActivityObjectId(mpxj.getSourceTask().getUniqueID());
      xml.setPredecessorProjectObjectId(PROJECT_OBJECT_ID);
      xml.setSuccessorProjectObjectId(PROJECT_OBJECT_ID);
      xml.setType(RELATION_TYPE_MAP.get(mpxj.getType()));
   }
}
 
Example 3
Source File: BasicTest.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Simple test to exercise iterating through the task predecessors.
 *
 * @throws Exception
 */
@Test public void testRelationList() throws Exception
{
   File in = new File(MpxjTestData.filePath("legacy/sample.mpx"));
   ProjectFile mpx = new MPXReader().read(in);

   for (Task task : mpx.getTasks())
   {
      List<Relation> rels = task.getPredecessors();
      if (rels != null)
      {
         for (Relation rel : rels)
         {
            assertNotNull(mpx.getTaskByUniqueID(rel.getTargetTask().getUniqueID()));
         }
      }
   }
}
 
Example 4
Source File: TaskLinksTest.java    From mpxj with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Test a relationship between two tasks.
 *
 * @param project parent project
 * @param taskID1 first task
 * @param taskID2 second task
 * @param name1 expected task name 1
 * @param name2 expected task name 1
 * @param type expected relation type
 * @param lagDuration expected lag duration
 * @param lagUnits expected lag units
 */
private void testTaskLinks(ProjectFile project, int taskID1, int taskID2, String name1, String name2, RelationType type, double lagDuration, TimeUnit lagUnits)
{
   Task task1 = project.getTaskByID(Integer.valueOf(taskID1));
   Task task2 = project.getTaskByID(Integer.valueOf(taskID2));

   assertEquals(name1, task1.getName());
   assertEquals(name2, task2.getName());

   List<Relation> relations = task2.getPredecessors();
   assertEquals(1, relations.size());
   Relation relation = relations.get(0);
   assertEquals(task2, relation.getSourceTask());
   assertEquals(task1, relation.getTargetTask());
   assertEquals(type, relation.getType());
   assertEquals(lagUnits, relation.getLag().getUnits());
   assertTrue(NumberHelper.equals(lagDuration, relation.getLag().getDuration(), 0.0001));
}
 
Example 5
Source File: ProjectFileImporter.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
private void importDependencies(ProjectFile pf, Map<Integer, GanttTask> foreignId2nativeTask) {
  getTaskManager().getAlgorithmCollection().getScheduler().setEnabled(false);
  try {
    for (Task t : pf.getAllTasks()) {
      if (t.getPredecessors() == null) {
        continue;
      }
      for (Relation r : t.getPredecessors()) {
        GanttTask dependant = foreignId2nativeTask.get(foreignId(r.getSourceTask()));
        GanttTask dependee = foreignId2nativeTask.get(foreignId(r.getTargetTask()));
        if (dependant == null) {
          myErrors.add(Pair.create(Level.SEVERE, String.format(
              "Failed to import relation=%s because source task=%s was not found", r, foreignId(r.getSourceTask()))));
          continue;
        }
        if (dependee == null) {
          myErrors.add(Pair.create(Level.SEVERE, String.format(
              "Failed to import relation=%s because target task=%s", t, foreignId(r.getTargetTask()))));
          continue;
        }
        try {
          TaskDependency dependency = getTaskManager().getDependencyCollection().createDependency(dependant, dependee);
          dependency.setConstraint(convertConstraint(r));
          if (r.getLag().getDuration() != 0.0) {
            // TODO(dbarashev): get rid of days
            dependency.setDifference((int) r.getLag().convertUnits(TimeUnit.DAYS, pf.getProjectProperties()).getDuration());
          }
          dependency.setHardness(TaskDependency.Hardness.parse(getTaskManager().getDependencyHardnessOption().getValue()));
        } catch (TaskDependencyException e) {
          GPLogger.getLogger("MSProject").log(Level.SEVERE, "Failed to import relation=" + r, e);
          myErrors.add(Pair.create(Level.SEVERE, String.format("Failed to import relation=%s: %s", r, e.getMessage())));
        }
      }
    }
  } finally {
    getTaskManager().getAlgorithmCollection().getScheduler().setEnabled(true);
  }
}
 
Example 6
Source File: SDEFWriter.java    From mpxj with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Write each predecessor for a task.
 *
 * @param record Task instance
 */
private void writeTaskPredecessors(Task record)
{
   m_buffer.setLength(0);
   //
   // Write the task predecessor
   //
   if (!record.getSummary() && !record.getPredecessors().isEmpty())
   { // I don't use summary tasks for SDEF
      m_buffer.append("PRED ");
      List<Relation> predecessors = record.getPredecessors();

      for (Relation pred : predecessors)
      {
         m_buffer.append(SDEFmethods.rset(pred.getSourceTask().getUniqueID().toString(), 10) + " ");
         m_buffer.append(SDEFmethods.rset(pred.getTargetTask().getUniqueID().toString(), 10) + " ");
         String type = "C"; // default finish-to-start
         if (!pred.getType().toString().equals("FS"))
         {
            type = pred.getType().toString().substring(0, 1);
         }
         m_buffer.append(type + " ");

         Duration dd = pred.getLag();
         double duration = dd.getDuration();
         if (dd.getUnits() != TimeUnit.DAYS)
         {
            dd = Duration.convertUnits(duration, dd.getUnits(), TimeUnit.DAYS, m_minutesPerDay, m_minutesPerWeek, m_daysPerMonth);
         }
         Double days = Double.valueOf(dd.getDuration() + 0.5); // Add 0.5 so half day rounds up upon truncation
         Integer est = Integer.valueOf(days.intValue());
         m_buffer.append(SDEFmethods.rset(est.toString(), 4) + " "); // task duration in days required by USACE
      }
      m_writer.println(m_buffer.toString());
   }
}
 
Example 7
Source File: MppTaskTest.java    From mpxj with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Tests Relations.
 *
 * @param mpp mpp file
 */
private void testRelations(ProjectFile mpp)
{
   List<Task> listAllTasks = mpp.getTasks();
   assertNotNull(listAllTasks);

   Task task1 = mpp.getTaskByID(Integer.valueOf(1));
   Task task2 = mpp.getTaskByID(Integer.valueOf(2));
   Task task3 = mpp.getTaskByID(Integer.valueOf(3));
   Task task4 = mpp.getTaskByID(Integer.valueOf(4));
   Task task5 = mpp.getTaskByID(Integer.valueOf(5));

   List<Relation> listPreds = task2.getPredecessors();
   Relation relation = listPreds.get(0);
   assertEquals(1, relation.getTargetTask().getUniqueID().intValue());
   assertEquals(RelationType.FINISH_START, relation.getType());
   assertEquals(task1, relation.getTargetTask());

   listPreds = task3.getPredecessors();
   relation = listPreds.get(0);
   assertEquals(2, relation.getTargetTask().getUniqueID().intValue());
   assertEquals(RelationType.START_START, relation.getType());
   Duration duration = relation.getLag();
   if (duration.getUnits() == TimeUnit.DAYS)
   {
      assertEquals(1, (int) duration.getDuration());
   }
   else
   {
      if (duration.getUnits() == TimeUnit.HOURS)
      {
         assertEquals(8, (int) duration.getDuration());
      }
   }

   listPreds = task4.getPredecessors();
   relation = listPreds.get(0);
   assertEquals(3, relation.getTargetTask().getUniqueID().intValue());
   assertEquals(RelationType.FINISH_FINISH, relation.getType());

   boolean removed = task4.removePredecessor(relation.getTargetTask(), relation.getType(), relation.getLag());
   assertTrue(removed);
   listPreds = task4.getPredecessors();
   assertTrue(listPreds.isEmpty());

   task4.addPredecessor(relation.getTargetTask(), relation.getType(), relation.getLag());

   task4.addPredecessor(task2, RelationType.FINISH_START, Duration.getInstance(0, TimeUnit.DAYS));

   listPreds = task4.getPredecessors();
   removed = task4.removePredecessor(task2, RelationType.FINISH_FINISH, Duration.getInstance(0, TimeUnit.DAYS));
   assertFalse(removed);

   task4.addPredecessor(task2, RelationType.FINISH_START, Duration.getInstance(0, TimeUnit.DAYS));
   listPreds = task4.getPredecessors();
   removed = task4.removePredecessor(task2, RelationType.FINISH_START, Duration.getInstance(0, TimeUnit.DAYS));
   assertTrue(removed);

   listPreds = task4.getPredecessors();
   relation = listPreds.get(0);
   assertEquals(3, relation.getTargetTask().getUniqueID().intValue());
   assertEquals(RelationType.FINISH_FINISH, relation.getType());

   listPreds = task5.getPredecessors();
   relation = listPreds.get(0);
   assertEquals(4, relation.getTargetTask().getUniqueID().intValue());
   assertEquals(RelationType.START_FINISH, relation.getType());
}
 
Example 8
Source File: MSPDIWriter.java    From mpxj with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * This method writes predecessor data to an MSPDI file.
 * We have to deal with a slight anomaly in this method that is introduced
 * by the MPX file format. It would be possible for someone to create an
 * MPX file with both the predecessor list and the unique ID predecessor
 * list populated... which means that we must process both and avoid adding
 * duplicate predecessors. Also interesting to note is that MSP98 populates
 * the predecessor list, not the unique ID predecessor list, as you might
 * expect.
 *
 * @param xml MSPDI task data
 * @param mpx MPX task data
 */
private void writePredecessors(Project.Tasks.Task xml, Task mpx)
{
   List<Project.Tasks.Task.PredecessorLink> list = xml.getPredecessorLink();

   List<Relation> predecessors = mpx.getPredecessors();
   for (Relation rel : predecessors)
   {
      Integer taskUniqueID = rel.getTargetTask().getUniqueID();
      list.add(writePredecessor(taskUniqueID, rel.getType(), rel.getLag()));
      m_eventManager.fireRelationWrittenEvent(rel);
   }
}