Java Code Examples for org.jgrapht.graph.SimpleDirectedGraph#outgoingEdgesOf()

The following examples show how to use org.jgrapht.graph.SimpleDirectedGraph#outgoingEdgesOf() . 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: HierUtil.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Private utility to return the parents (direct ascendants) of a given child node.
 *
 * @param vertex contains node name and acts as cursor for current location.
 * @param graph  contains a reference to simple digraph {@code org.jgrapht.graph.SimpleDirectedGraph}.
 * @return Set of names that are parents of given child.
 */
static Set<String> getParents( String vertex, SimpleDirectedGraph<String, Relationship> graph )
{
    Set<String> parents = new HashSet<>();
    if ( graph == null )
    {
        return null;
    }
    LOG.debug( "getParents [{}]", vertex);
    Set<Relationship> edges;
    try
    {
        edges = graph.outgoingEdgesOf( vertex );
    }
    catch ( java.lang.IllegalArgumentException iae )
    {
        // vertex is leaf.
        return null;
    }
    for ( Relationship edge : edges )
    {
        parents.add( edge.getParent() );
    }
    return parents;
}
 
Example 2
Source File: HierUtil.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * Utility function recursively traverses a given digraph to build a set of all ascendant names.
 *
 * @param vertex     contains the position of the cursor for traversal of graph.
 * @param graph      contains a reference to simple digraph {@code org.jgrapht.graph.SimpleDirectedGraph}.
 * @param ascendants contains the result set of ascendant names.
 * @return value contains the vertex of current position.
 */
private static String getAscendants( Map<String, String> vertex, SimpleDirectedGraph<String, Relationship> graph,
    Set<String> ascendants )
{
    String v = vertex.get( VERTEX );
    if ( v == null )
    {
        return null;
    }
    else if ( graph == null )
    {
        return null;
    }
    LOG.debug( "getAscendants [{}]", v);
    Set<Relationship> edges;
    try
    {
        edges = graph.outgoingEdgesOf( v );

    }
    catch ( java.lang.IllegalArgumentException iae )
    {
        // vertex is leaf.
        return null;
    }
    for ( Relationship edge : edges )
    {
        vertex.put( VERTEX, edge.getParent() );
        ascendants.add( edge.getParent() );
        v = getAscendants( vertex, graph, ascendants );
    }
    return v;
}
 
Example 3
Source File: HierUtil.java    From directory-fortress-core with Apache License 2.0 4 votes vote down vote up
/**
 * Private utility to recursively traverse the hierarchical graph and return all of the ascendants of a given child node.
 *
 * @param vertex      contains node name and acts as cursor for current location.
 * @param graph       contains a reference to simple digraph {@code org.jgrapht.graph.SimpleDirectedGraph}.
 * @param parents     contains the result set of parent nodes.
 * @param stopName    contains the name of node where traversal ends.
 * @param isInclusive if set to true will include the parentName in the result set. False will not return specified parentName.
 * @return Set of names that are parents of given child.
 */
private static String getAscendants( Map<String, String> vertex, SimpleDirectedGraph<String, Relationship> graph,
    Set<String> parents, String stopName, boolean isInclusive )
{
    String v = vertex.get( VERTEX );
    if ( v == null )
    {
        return null;
    }
    else if ( graph == null )
    {
        return null;
    }
    LOG.debug( "getAscendants [{}]", v);
    Set<Relationship> edges;
    try
    {
        edges = graph.outgoingEdgesOf( v );
    }
    catch ( java.lang.IllegalArgumentException iae )
    {
        // vertex is leaf.
        return null;
    }
    for ( Relationship edge : edges )
    {
        if ( edge.getParent().equalsIgnoreCase( stopName ) )
        {
            if ( isInclusive )
            {
                parents.add( edge.getParent() );
            }
            break;
        }
        else
        {
            vertex.put( VERTEX, edge.getParent() );
            parents.add( edge.getParent() );
            v = getAscendants( vertex, graph, parents, stopName, isInclusive );
        }
    }
    return v;
}
 
Example 4
Source File: AdminMgrConsole.java    From directory-fortress-core with Apache License 2.0 4 votes vote down vote up
/**
 * @param g
 */
private static String getParents(Map vertex, SimpleDirectedGraph<String, Relationship> g, List<String> parents)
{
    String v;
    //ReaderUtil.clearScreen();
    //System.out.println("addJGraph");
    //String vertex = "Max";
    v = (String) vertex.get("Vertex");
    if (g == null)
    {
        System.out.println("getAscendants simple directed graph is null");
        return null;
    }
    if (v == null)
    {
        System.out.println("getAscendants simple directed graph is null");
        return null;
    }
    System.out.println("getAscendants V [" + v + "]");

    Set<Relationship> edges = g.outgoingEdgesOf(v);
    for (Relationship edge : edges)
    {
        if (v == null)
            return null;
        else
        {
            System.out.println("Edge:" + edge);
            //parents.add(edge.toString());
            v = edge.toString();
            //Max : Super
            //getAscendants V <Super)>
            int indx = v.indexOf(GlobalIds.PROP_SEP);
            int indx2 = v.indexOf(')');
            if (indx >= 0)
            {
                v = v.substring(indx + 2, indx2);
            }

            //String parent =
            vertex.put("Vertex", v);
            if (!v.equalsIgnoreCase("Root"))
                parents.add(v);

            v = getParents(vertex, g, parents);
        }
    }
    return v;
}