Confusion with Gremlin types in .NET

304 views
Skip to first unread message

Stefan A.

unread,
Mar 25, 2019, 10:10:09 AM3/25/19
to Gremlin-users
Hi guys i'm new in Gremlin and Graph DB (sorry in front for any trivial questions).

In my project i post request has URI that looks like this: /NodeName1/NodeName2/NodeName3/.... (Node1 has edge to Node2, Node2 to Node3, etc).
And i need to get to the last one node in that uri, but I never know how long that uri will be so i came up with idea to chain methods has('name', '...').out() after i split that uri and count nodes in that uri.

Query would look like this: 

g.V().has('name', "NodeName1").out().has('name',"NodeName2").out()....

For that i will use _query variable and than chain methods on that variable. But there is problem. _query is type GraphTraversal<S,E>  which is generic type and i dont know what types i need to set, but because g.V() returns GraphTraversal<Vertex,Vertex> i set that, but when i want to execute something like this: 

var result = _query.has('name', "NodeName1").out().id().Next();

I get error that says "Cant convert int64 to Gremlin.Vertex". When i remove id() it will return type Vertex and i can get id with result.id (but that's not what i want).

This is my code:

protected GraphTraversalSource g;
protected GraphTraversal<Vertex,Vertex> _query;

public GremlinTest(string rootId = null)
{
  g = Traversal().WithRemote(_remoteConnection);
  _query = !string.IsNullOrEmpty(rootId) ? g.V(rootId) : g.V();
}

public void AddVertex(string name)
{
  _query.has("name", name).out();
}

public void GetId()
{
 _query.id();
}

public "What return type?" Execute()
{
 return _query.Next() or _query.ToList() or _query.Iterate();
}

I also want to create query that will get me property of all nodes that are connected with last node in URI. I came up with something like this: But again i'm not sure about returning type and if it's good.

g.V().has('name', "NodeName1").out().has('name',"NodeName2").out()....has('name', "LastNode").Repeat(Out().SimplePath()).Until(Label().Is("Something")).ValueMap<string, object>(true);


Hope i explained what i have trouble with. Also I will answer any of your question as soon as i can.

Thank you in front guys Stefan.

Stephen Mallette

unread,
Mar 30, 2019, 9:30:52 AM3/30/19
to gremli...@googlegroups.com
I think you just need to consider the generics as <Start,End> and id() is defined as:

public GraphTraversal<S, object> Id ()

so for:

g.V().out()......id() 

would be of type GraphTraversal<Vertex,object> 



--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/162660f4-d04d-42e9-b413-7750a4d878ab%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stefan A.

unread,
Apr 1, 2019, 3:38:50 AM4/1/19
to Gremlin-users
Yes i understand that. But i want to have _query variable that i will chain, always different, methods. So in start i don't know what i'm gonna return.

Something like this:

_query = g.V();

// In some other method
_query.has('name').out().has('name', ...)

// In other method

// In method to execute this query
_query.next() or toList() or iterate()

And when i set _query to be GraphTraversal<Vertex,object>  type. First method _query=g.V() will set him to GraphTraversal<Vertex,Vertex> and than i can't return for example properties because it cant convert Vertex to VertexProeprty.
To unsubscribe from this group and stop receiving emails from it, send an email to gremli...@googlegroups.com.

Stephen Mallette

unread,
Apr 1, 2019, 3:24:28 PM4/1/19
to gremli...@googlegroups.com
oh...hmm, not sure what you do in that case. no type erasure in c# obviously. perhaps someone with more c# expertise can chime in....

To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/1caea08a-800b-4021-9d64-b5280c7fcd54%40googlegroups.com.

Florian Hockmann

unread,
Apr 2, 2019, 3:38:22 AM4/2/19
to Gremlin-users
That's actually a problem I also already faced but found no good solution for. You can simply add Gremlin Bytecode to an existing traversal without having to use the returned traversal as the steps also modify the existing traversal. This might help you a bit, but you probably have to combine some methods as you for example can't call id() on a traversal that isn't already on an element (like vertex or edge).
Or you concatenate some of the calls of your methods and then let them return the changed traversal with its correct signature.

Stefan A.

unread,
Apr 2, 2019, 3:42:04 AM4/2/19
to Gremlin-users
Yeah i also found no good solution. So i'm starting to execute queries in one method until someone come up idea :) But thank you for response guys.
Reply all
Reply to author
Forward
0 new messages