Using multiple variables for math.

瀏覽次數:28 次
跳到第一則未讀訊息

Sam Rosen

未讀,
2018年6月14日 下午1:35:322018/6/14
收件者:Gremlin-users
Going off this example: http://tinkerpop.apache.org/docs/current/reference/#math-step

I'm trying to do some calculation for every vertex with the following code, but I continually get a null pointer exception. This seems to be my best attempt; where am I going wrong?



graph
= TinkerFactory.createModern()
g
= graph.traversalI()


g
.V()
 
.group()
 
.by(
   both
()
   
.fold()
   
.as("neighbors")
   
.unfold()
   
.outE()
   
.fold()
   
.as("edges")
   
.math("neighbors - edges")
   
.by(
         unfold
().count())
   
.by(
         unfold
().count())
 
)
 
.by()
> java.lang.NullPointerException

Is there another way to accomplish arithmetic on some filtering of a traversal and the original traversals length?


Thanks,
Sam

Robert Dale

未讀,
2018年6月14日 下午2:13:162018/6/14
收件者:gremli...@googlegroups.com
I believe the reason for the NPE is that there is a starting vertex where both().outE() does not exist and count() is applied to it.
Another thing is that you can reference back to variables prior to a reducing barrier. e.g. fold().as('a').unfold().fold().as('b').select('a') 
 
You have to account for cases where there are no results.
One way to do that is filter for where the full traversal exists:
g.V().where(__.both().outE()).groupCount().by(.....

Another option is to use count() which will always give a result:
gremlin> g.V().group().by(project('n','e').by(both().count()).by(both().outE().count()).math('n-e'))
==>[1.0:[v[1],v[6]],-2.0:[v[2]],0.0:[v[4]],-1.0:[v[5]],-3.0:[v[3]]]

There are probably other ways too.

Robert Dale


--
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/df13dffa-f914-44b4-be6a-d9a6bede1d5a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sam Rosen

未讀,
2018年6月14日 下午3:11:452018/6/14
收件者:Gremlin-users
My initial attempts all used counts but I'd still get null pointer exceptions or simply no right answers. The main problem with the given working option is that it requires computing the incident vertices twice.

Robert Dale

未讀,
2018年6月14日 下午3:17:182018/6/14
收件者:gremli...@googlegroups.com
I see what you mean. Try this:

gremlin> g.V().group().by(both().fold().project('n','e').by(unfold().count()).by(unfold().outE().count()).math('n-e')).unfold()
==>[1.0:[v[1],v[6]],-2.0:[v[2]],0.0:[v[4]],-1.0:[v[5]],-3.0:[v[3]]]

Robert Dale


Sam Rosen

未讀,
2018年6月14日 下午3:35:172018/6/14
收件者:Gremlin-users
Another good solution. Here's another one that allows maintaining the incident vertices with the result.

g.V()
.project("v", "neighbors")
.by()
.by(both().fold()) // find incident vertices
.group()
.by(
  project
("count", "edge_count")
 
.by(
   
select("neighbors").unfold().count()
 
)
 
.by(
   
select("neighbors").unfold().outE().count()
 
).math("count-edge_count")    
)
==>[1.0:[[v:v[1],n:[v[3],v[2],v[4]]],[v:v[6],n:[v[3]]]],-2.0:[[v:v[2],n:[v[1]]]],0.0:[[v:v[4],n:[v[5],v[3],v[1]]]],-1.0:[[v:v[5],n:[v[4]]]],-3.0:[[v:v[3],n:[v[1],v[4],v[6]]]]]

回覆所有人
回覆作者
轉寄
0 則新訊息