org.jgrapht.traverse.GraphIterator Java Examples

The following examples show how to use org.jgrapht.traverse.GraphIterator. 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
/**
 * Find the resources in {@link #resourcesGraph} matching the given {@code query}.
 *
 * @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 match the given {@code query}
 */
public List<Resource<L>> findResources(L query, LocationResolver<L> locationResolver) {
    graphLockRead.lock();
    try {
        List<Resource<L>> result = new ArrayList<Resource<L>>();
        GraphIterator<Resource<L>, DefaultEdge> it = new BreadthFirstIterator<Resource<L>, DefaultEdge>(
                this.resourcesGraph);
        while (it.hasNext()) {
            Resource<L> resource = it.next();
            if (locationResolver.matches(query, resource.getLocation())) {
                result.add(resource);
            }
        }
        return Collections.unmodifiableList(result);
    } finally {
        graphLockRead.unlock();
    }
}
 
Example #2
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 #3
Source File: JPQLStatementFromClause.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
public String buildClause(QueryGraph queryGraph, Map<String, String> queryEntityAliases, Map entityAliasesMaps) {

		GraphIterator<IModelEntity, Relationship> iterator = new TopologicalOrderIterator<>(queryGraph);
		GraphIteratorListener listener = new GraphIteratorListener(entityAliasesMaps, queryEntityAliases);
		iterator.addTraversalListener(listener);
		IModelEntity firstEntity = null;
		if (iterator.hasNext()) {
			firstEntity = iterator.next();
		}

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

		List<String> fromClauseElements = new ArrayList<>();

		String fistEntityName = firstEntity.getName();
		String firtEntityAlias = getAlias(entityAliasesMaps, queryEntityAliases, firstEntity);
		List<String> joinStatments = listener.getJoinStatments();

		fromClauseElements.add(FROM);
		fromClauseElements.add(fistEntityName);
		fromClauseElements.add(firtEntityAlias);
		fromClauseElements.addAll(joinStatments);

		return StringUtils.join(fromClauseElements, " ");

	}
 
Example #4
Source File: ResourceManager.java    From hawkular-agent with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an immutable {@link List} of all {@link Resource}s contained in {@link #resourcesGraph} in breadth-first
 * order.
 *
 * @return the list of all {@link Resource}s
 */
public List<Resource<L>> getResourcesBreadthFirst() {
    graphLockRead.lock();
    try {
        List<Resource<L>> result = new ArrayList<Resource<L>>();

        Set<Resource<L>> roots = getRootResources();
        if (roots.isEmpty()) {
            return Collections.emptyList();
        }

        // loop over each root resource and traverse their tree hierarchy breadth-first
        // roots.forEach(root -> new BreadthFirstIterator<>(ResourceManager.this.resourcesGraph, root)
        //     .forEachRemaining(it -> result.add(it)));
        for (Resource<L> root : roots) {
            GraphIterator<Resource<L>, DefaultEdge> it = new BreadthFirstIterator<Resource<L>, DefaultEdge>(
                    ResourceManager.this.resourcesGraph, root);
            while (it.hasNext()) {
                result.add(it.next());
            }
        }

        return Collections.unmodifiableList(result);
    } finally {
        graphLockRead.unlock();
    }
}
 
Example #5
Source File: ResourceTypeManager.java    From hawkular-agent with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an immutable {@link List} of all {@link ResourceType}s contained in {@link #resourceTypesGraph} in
 * breadth-first order.
 *
 * @return the list of all {@link ResourceType}s
 */
public List<ResourceType<L>> getResourceTypesBreadthFirst() {
    List<ResourceType<L>> result = new ArrayList<ResourceType<L>>();
    GraphIterator<ResourceType<L>, DefaultEdge> it = new BreadthFirstIterator<ResourceType<L>, DefaultEdge>(
            this.resourceTypesGraph);
    while (it.hasNext()) {
        result.add(it.next());
    }
    return Collections.unmodifiableList(result);
}