org.jgrapht.traverse.DepthFirstIterator Java Examples

The following examples show how to use org.jgrapht.traverse.DepthFirstIterator. 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: ResourceManager.java    From hawkular-agent with Apache License 2.0 6 votes vote down vote up
/**
 * Remove the resources from {@link #resourcesGraph} matching the given {@code query} including all direct and
 * indirect descendants.
 *
 * @param query a location eventually containing wildcards
 * @param locationResolver the {@link LocationResolver} to perform the matching of graph nodes against the given
 *            {@code query}
 * @return an unmodifiable list of {@link Resources} that were removed by this method
 */
public List<Resource<L>> removeResources(L query, LocationResolver<L> locationResolver) {
    graphLockWrite.lock();
    try {
        List<Resource<L>> doomedResources = new ArrayList<Resource<L>>();
        GraphIterator<Resource<L>, DefaultEdge> it = new DepthFirstIterator<>(this.resourcesGraph);
        while (it.hasNext()) {
            Resource<L> resource = it.next();
            if (locationResolver.matches(query, resource.getLocation())) {
                getAllDescendants(resource, doomedResources);
                doomedResources.add(resource);
            }
        }

        // we couldn't do this while iterating (a ConcurrentModificationException would have resulted)
        // but now that we have the doomed resources, we can remove them from the graph now
        for (Resource<L> doomedResource : doomedResources) {
            this.resourcesGraph.removeVertex(doomedResource);
        }

        return Collections.unmodifiableList(doomedResources);
    } finally {
        graphLockWrite.unlock();
    }
}
 
Example #2
Source File: MasterGraphChangeHandler.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
private void loadAddons()
{
   DepthFirstIterator<AddonVertex, AddonDependencyEdge> iterator = new DepthFirstIterator<AddonVertex, AddonDependencyEdge>(
            graph.getGraph());

   iterator.addTraversalListener(new TraversalListenerAdapter<AddonVertex, AddonDependencyEdge>()
   {
      @Override
      public void vertexFinished(VertexTraversalEvent<AddonVertex> event)
      {
         Addon addon = event.getVertex().getAddon();
         if (!addon.getStatus().isLoaded())
            lifecycleManager.loadAddon(addon);
      };
   });

   while (iterator.hasNext())
      iterator.next();
}
 
Example #3
Source File: MasterGraphChangeHandler.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
private void startupIncremental()
{
   DepthFirstIterator<AddonVertex, AddonDependencyEdge> iterator = new DepthFirstIterator<AddonVertex, AddonDependencyEdge>(
            graph.getGraph());

   iterator.addTraversalListener(new TraversalListenerAdapter<AddonVertex, AddonDependencyEdge>()
   {
      @Override
      public void vertexFinished(VertexTraversalEvent<AddonVertex> event)
      {
         Addon addon = event.getVertex().getAddon();
         if (addon.getStatus().isLoaded())
            lifecycleManager.startAddon(addon);
      };
   });

   while (iterator.hasNext())
      iterator.next();
}
 
Example #4
Source File: MasterGraphChangeHandler.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
private void clearDirtyStatus()
{
   DepthFirstIterator<AddonVertex, AddonDependencyEdge> iterator = new DepthFirstIterator<AddonVertex, AddonDependencyEdge>(
            graph.getGraph());

   iterator.addTraversalListener(new TraversalListenerAdapter<AddonVertex, AddonDependencyEdge>()
   {
      @Override
      public void vertexFinished(VertexTraversalEvent<AddonVertex> event)
      {
         event.getVertex().setDirty(false);
      };
   });

   while (iterator.hasNext())
      iterator.next();
}
 
Example #5
Source File: AddonGraph.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public String toString()
{
   DepthFirstIterator<AddonVertex, AddonDependencyEdge> iterator = new DepthFirstIterator<AddonVertex, AddonDependencyEdge>(
            getGraph());

   final StringBuilder builder = new StringBuilder();
   iterator.addTraversalListener(new PrintGraphTraversalListener(getGraph(), builder));

   while (iterator.hasNext())
      iterator.next();

   return builder.toString();
}
 
Example #6
Source File: MasterGraph.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
public void merge(final OptimizedAddonGraph other)
{
   if (other.getGraph().vertexSet().isEmpty())
      return;

   if (graph.vertexSet().isEmpty())
   {
      for (AddonVertex vertex : other.getGraph().vertexSet())
      {
         mergeVertex(other, vertex);
      }
   }
   else
   {
      DepthFirstIterator<AddonVertex, AddonDependencyEdge> iterator = new DepthFirstIterator<AddonVertex, AddonDependencyEdge>(
               other.getGraph());

      iterator.addTraversalListener(new TraversalListenerAdapter<AddonVertex, AddonDependencyEdge>()
      {
         @Override
         public void vertexTraversed(VertexTraversalEvent<AddonVertex> event)
         {
            mergeVertex(other, event.getVertex());
         };
      });

      while (iterator.hasNext())
         iterator.next();
   }
}
 
Example #7
Source File: MasterGraph.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public String toString()
{
   DepthFirstIterator<AddonVertex, AddonDependencyEdge> iterator = new DepthFirstIterator<AddonVertex, AddonDependencyEdge>(
            getGraph());

   final StringBuilder builder = new StringBuilder();
   iterator.addTraversalListener(new PrintGraphTraversalListener(getGraph(), builder));

   while (iterator.hasNext())
      iterator.next();

   return builder.toString();
}
 
Example #8
Source File: MasterGraphChangeHandler.java    From furnace with Eclipse Public License 1.0 4 votes vote down vote up
private void markDirty()
{
   DepthFirstIterator<AddonVertex, AddonDependencyEdge> iterator = new DepthFirstIterator<AddonVertex, AddonDependencyEdge>(
            graph.getGraph());

   iterator.addTraversalListener(new TraversalListenerAdapter<AddonVertex, AddonDependencyEdge>()
   {
      @Override
      public void vertexFinished(VertexTraversalEvent<AddonVertex> event)
      {
         // If this vertex is missing or any dependency was missing (is dirty), then this is dirty also
         AddonVertex vertex = event.getVertex();
         Addon addon = vertex.getAddon();
         if (!addon.getStatus().isLoaded() || addon.getStatus().isFailed())
         {
            vertex.setDirty(true);
         }

         for (AddonDependencyEdge edge : graph.getGraph().outgoingEdgesOf(vertex))
         {
            AddonVertex target = graph.getGraph().getEdgeTarget(edge);
            if (target.isDirty())
               vertex.setDirty(true);
         }

         if (lastMasterGraph != null)
         {
            boolean equivalent = false;
            Set<AddonVertex> lastVertices = lastMasterGraph.getVertices(vertex.getName(), vertex.getVersion());
            for (AddonVertex lastVertex : lastVertices)
            {
               if (graph.isSubtreeEquivalent(vertex, lastMasterGraph.getGraph(), lastVertex))
               {
                  equivalent = true;
                  break;
               }
            }

            if (!equivalent)
               vertex.setDirty(true);
         }
      };
   });

   while (iterator.hasNext())
      iterator.next();
}
 
Example #9
Source File: DirectedGraphUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenDirectedGraph_whenCreateInstanceDepthFirstIterator_thenGetIterator() {
    DepthFirstIterator depthFirstIterator = new DepthFirstIterator<>(directedGraph);
    assertNotNull(depthFirstIterator);
}