Adriano,
how about something like this?
import org.junit.Test;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.traversal.Evaluation;
import org.neo4j.graphdb.traversal.Evaluator;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.kernel.Traversal;
import common.Neo4jAlgoTestCase;
public class TraversalTest extends Neo4jAlgoTestCase
{
@Test public void test2Steps() {
graph.makeEdge("John", "Paris");
graph.makeEdge("Peter", "Paris");
graph.makeEdge("John", "Rome");
graph.makeEdge("Peter", "Toronto");
graph.makeEdge("Adriano", "Toronto");
graph.makeEdge("Adriano", "Tokyo");
Node node = graph.getNode( "Toronto" );
TraversalDescription td = Traversal.description().evaluator(
new Evaluator()
{
@Override
public Evaluation evaluate( Path path )
{
if (path.length() == 2) {
return Evaluation.INCLUDE_AND_PRUNE;
}
return Evaluation.EXCLUDE_AND_CONTINUE;
}
});
for (Node res : td.traverse( node ).nodes()){
System.out.println(res);
}
}
}
Cheers,
/peter neubauer
GTalk: neubauer.peter
Skype peter.neubauer
Phone +46 704 106975
LinkedIn http://www.linkedin.com/in/neubauer
Twitter http://twitter.com/peterneubauer
http://www.neo4j.org - Your high performance graph database.
http://startupbootcamp.org/ - Öresund - Innovation happens HERE.
http://www.thoughtmade.com - Scandinavia's coolest Bring-a-Thing party.
On Sun, Mar 20, 2011 at 10:32 PM, Adriano Henrique de Almeida
<[email protected]> wrote:
> Hi,
>
> I have the following attached graph where I have persons who traveled to
> some cities.
>
> What I want to find out is, for a given city, for instance Toronto, "the
> ones who traveled there, also traveled to these other cities" (in the
> attached graph are Tokyo (by Adriano) and Paris (by Peter)).
>
> To retrieve this information, I did the following code:
>
> Collection<Node> allNodes = new ArrayList<Node>();
>
> Node toronto = db.getNodeById(torontoId); // First I get Toronto node and
> its relationships to know who traveled there
>
> Iterable<Relationship> relationships =
> toronto.getRelationships(Relationships.TRAVELED_TO, Direction.INCOMING);
>
>
> for (Relationship relationship : relationships) {
>
> Node[] nodes = relationship.getNodes(); // For each relationship found,
> I all nodes that somehow is related to this relationship
>
> for (Node node : nodes) {
>
> Collection<Node> citiesNode = node.traverse(Order.DEPTH_FIRST,
> StopEvaluator.DEPTH_ONE, ReturnableEvaluator.ALL_BUT_START_NODE,
> Relationships.TRAVELED_TO, Direction.OUTGOING).getAllNodes(); // And finally
> I traverse the graph to find to find from theses nodes where the other
> people traveled to
>
> allNodes.addAll(citiesNode);
>
> }
>
> }
>
> Well, with this I can get the results I wanted, however, it seemed to me
> that what I did was too complicated :) . So, my question is: "is there any
> way to do this traversal in a more straightforward manner?".
>
> Thanks in advance.
>
> --
> Adriano Almeida
>
> _______________________________________________
> Neo4j mailing list
> [email protected]
> https://lists.neo4j.org/mailman/listinfo/user
>
>
_______________________________________________
Neo4j mailing list
[email protected]
https://lists.neo4j.org/mailman/listinfo/user