Re: [Neo4j] Graph traversal doubt

2011-03-21 Thread Marko Rodriguez
Hi,

In Gremlin do,

g.v(torontoId).inE('traveled_to').outV.outE('traveled_to').inV 

If you want it ranked by frequency, do:

m = [:];

g.v(torontoId).inE('traveled_to').outV.outE('traveled_to').inV.groupCount(m)

Take care,
Marko.

http://markorodriguez.com



On Mar 20, 2011, at 11:52 PM, Peter Neubauer wrote:

> 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
>  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 allNodes = new ArrayList();
>> 
>> Node toronto = db.getNodeById(torontoId); // First I get Toronto node and
>> its relationships to know who traveled there
>> 
>> Iterable 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 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
>> User@lists.neo4j.org
>> https://lists.neo4j.org/mailman/listinfo/user
>> 
>> 
> ___
> Neo4j mailing list
> User@lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user

___
Neo4j mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user


Re: [Neo4j] Graph traversal doubt

2011-03-20 Thread Peter Neubauer
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
 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 allNodes = new ArrayList();
>
> Node toronto = db.getNodeById(torontoId); // First I get Toronto node and
> its relationships to know who traveled there
>
> Iterable 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 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
> User@lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user
>
>
___
Neo4j mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user


[Neo4j] Graph traversal doubt

2011-03-20 Thread Adriano Henrique de Almeida
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 allNodes = new ArrayList();

Node toronto = db.getNodeById(torontoId); // First I get Toronto node and
its relationships to know who traveled there

Iterable 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 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
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user