Hi Luca,
try using the attached code and let me know if it is worthwhile.

Kind regards,
Alessandro

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
package Traverse_Depth_First;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;

import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;

public class Traverse {

	private Set<Vertex> listVertex = new LinkedHashSet<Vertex>(); 
	private OrientGraph g;

	public Traverse(OrientGraph g) {
		this.g = g;
	}

	public Set<Vertex> get(String id) {
		if (getRoot(id)) {
			return getChildren(id);
		} else
			return listVertex;
	}

	protected boolean getRoot(String id) {
		Iterable<Vertex> root = g.command(
				new OSQLSynchQuery<Vertex>("select from " + id)).execute();
		List<Vertex> listRoot = new ArrayList<Vertex>();
		CollectionUtils.addAll(listRoot, root.iterator());
		if (listRoot.size() == 0) {
			System.out.println("Root not found");
			return false;
		} else {
			listVertex.add(listRoot.get(0));
			return true;
		}
	}

	// classe o rid, direzione
	protected Set<Vertex> getChildren(String id) {
		List<Vertex> listaVertex = new ArrayList<Vertex>();
		Iterable<Vertex> result = g.command(
				new OSQLSynchQuery<Vertex>("select expand(out()) from " + id))
				.execute();
		CollectionUtils.addAll(listaVertex, result.iterator());
		for (int i = listaVertex.size() - 1; i >= 0; i--) {
			Vertex v = listaVertex.get(i);
			listVertex.add(listaVertex.get(i));
			getChildren(v.getId().toString());
		}
		return listVertex;
	}

}
package Traverse_Depth_First;

import java.io.IOException;
import java.util.Set;

import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;

public class Main {

	private static String remote="remote:localhost/";

	public static void main(String[] args) {

		String nomeDb="Depth_first";
		String currentPath=remote+nomeDb;

		OServerAdmin serverAdmin;
		try {
			serverAdmin = new OServerAdmin(currentPath).connect("root", "root");
			if(serverAdmin.existsDatabase()){   
				OrientGraph g=new OrientGraph(currentPath); 
				Traverse traverse = new Traverse(g);
				String id="#12:0";
				Set<Vertex> vertex=traverse.get(id);
				for(Vertex v:vertex){
					System.out.println(v.getProperty("id"));
				}
				g.shutdown();
			}
			serverAdmin.close();
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
}

Reply via email to