Ejemplo n.º 1
0
  /** Construct the ExertionSorter */
  public ExertionSorter(Exertion topLevelJob) throws ContextException, SortingException {

    dag = new DAG();
    projectMap = new HashMap();
    contextIdsMap = new HashMap<String, String>();
    revContextIdsMap = new HashMap<String, String>();
    this.topLevelJob = topLevelJob;

    addVertex(this.topLevelJob);

    try {
      getMapping(this.topLevelJob);
      checkParentCycle(this.topLevelJob);

      List sortedProjects = new ArrayList();
      for (Iterator i = TopologicalSorter.sort(dag).iterator(); i.hasNext(); ) {
        String id = (String) i.next();
        sortedProjects.add(projectMap.get(id));
      }

      this.sortedProjects = Collections.unmodifiableList(sortedProjects);
      reorderJob(this.topLevelJob, this.sortedProjects);
    } catch (CycleDetectedException ce) {
      throw new SortingException(ce.getMessage());
    }
  }
  public static List<IndexCreator> sort(List<? extends IndexCreator> creators)
      throws IllegalArgumentException {
    try {
      final HashMap<String, IndexCreator> creatorsById =
          new HashMap<String, IndexCreator>(creators.size());

      DAG dag = new DAG();

      for (IndexCreator creator : creators) {
        creatorsById.put(creator.getId(), creator);

        dag.addVertex(creator.getId());

        for (String depId : creator.getCreatorDependencies()) {
          dag.addEdge(creator.getId(), depId);
        }
      }

      List<String> sortedIds = TopologicalSorter.sort(dag);

      final ArrayList<IndexCreator> sortedCreators = new ArrayList<IndexCreator>(creators.size());

      for (String id : sortedIds) {
        final IndexCreator creator = creatorsById.get(id);

        if (creator != null) {
          sortedCreators.add(creator);
        } else {
          throw new IllegalArgumentException(
              String.format(
                  "IndexCreator with ID=\"%s\" does not exists, the present creator ID=\"%s\" depends on it!",
                  id, dag.getParentLabels(id)));
        }
      }

      return sortedCreators;
    } catch (CycleDetectedException e) {
      throw new IllegalArgumentException("Supplied IndexCreator inter-dependencies", e);
    }
  }