[tinkerpop 3.0.1incubating] simple mathematical operations ( + - / * ) after a .select('valA', 'valB',...)

448 views
Skip to first unread message

Florent B.

unread,
May 4, 2016, 7:42:35 AM5/4/16
to Gremlin-users
Hi everyone,

I wondered how could I perform simple operations using gremlin groovy on selected elements.

Query example :
 

v1Id = 123456;
v2Id
= 654321;

g
.V(v1Id).as('v1').match(
  __
.as('v1').both('someEdgeLabel').count().as('v1LinkedVertices').filter(
    both
('someEdgeLabel').id().is(eq(v2Id))
 
).count().as('v1DirectCommonVerticesWithV2'),
).select('v1LinkedVertices', 'v1DirectCommonVerticesWithV2')


Then, I get this kind of result : 

[
 
{
   
v1LinkedVertices: x, // x = number
   
v1DirectCommonVerticesWithV2: y // y = number
 
}
]


How can I perform some basic mathematical operations so I can get this kind of result

[
 
{
   
v1LinkedVertices_MinusOne: x-1, // x = number
   
v1DirectCommonVerticesWithV2_MultipliedByTwo: 2*y, // x = number
   
stuffDoneHere: z // z = x/y
  }
]

 
Regards,

Flo.

Daniel Kuppitz

unread,
May 4, 2016, 8:09:29 AM5/4/16
to gremli...@googlegroups.com
Your query doesn't look valid, hence let's play with ages in the modern graph.

gremlin> g.V().values("age")
==>29
==>27
==>32
==>35

Sum all ages:

gremlin> g.V().values("age").sum()
==>123

Multiply all ages:

gremlin> g.V().values("age").fold(1, mult)
==>876960

Subtract 1:

gremlin> g.V().values("age").map(union(identity(), constant(-1)).sum())
==>28
==>26
==>31
==>34

Multiply by 2 (simple):

gremlin> g.V().values("age").map(union(identity(), identity()).sum())
==>58
==>54
==>64
==>70

Multiply by 2 (generally useful for multiplications by n):

gremlin> g.V().values("age").map(union(identity(), constant(2)).fold(1, mult))
==>58
==>54
==>64
==>70

I don't have a solution for divisions that doesn't use lambdas.

Cheers,
Daniel


--
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/855b0fca-dae7-4a17-a6cf-634a078d54cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Florent B.

unread,
May 4, 2016, 8:19:19 AM5/4/16
to Gremlin-users
Thanks (a lot) for your answer. I'll try to do some stuff with this.
I just hope it'll work with the select step too. :)

FYI, The query I presented was this query 'simplified' :

userId = 40968224
jobId
= 12320
g
.V().hasLabel('joboffer').as('job').match(
  __
.as('job').out('tagged').count().as('jobTags'),
  __
.as('job').both('tagged').hasLabel('tag').filter(
    both
('knows').hasLabel('user').id().is(eq(userId))
 
).count().as('commonTags'),
).select('jobTags', 'commonTags')

I can assure you that this query is working, here is the result :

[ { jobTags: 5, commonTags: 3 } ]

:)

Daniel Kuppitz

unread,
May 4, 2016, 8:26:28 AM5/4/16
to gremli...@googlegroups.com
userId = 40968224
jobId = 12320
g.V().hasLabel('joboffer').as('job').match(
  __.as('job').out('tagged').count().as('jobTags'),
  __.as('job').both('tagged').hasLabel('tag').filter(
    both('knows').hasLabel('user').id().is(eq(userId))
  ).count().as('commonTags'),
).select('jobTags', 'commonTags') 

I can assure you that this query is working

I have no complaints when I look at this query, but the first one was not simplified, it was just busted and invalid.

Cheers,
Daniel


Florent B.

unread,
May 4, 2016, 8:46:45 AM5/4/16
to Gremlin-users
My bad ! :)

Florent B.

unread,
May 4, 2016, 9:22:36 AM5/4/16
to Gremlin-users

I don't have a solution for divisions that doesn't use lambdas.


 .fold(1){a,b -> b/a}

;)

Thanks a lot !

Daniel Kuppitz

unread,
May 4, 2016, 9:45:03 AM5/4/16
to gremli...@googlegroups.com
FYI: That's a lambda. You can solve any use-case using lambdas, but lambda-less traversals are usually preferred as they perform a lot better.

Cheers,
Daniel


--
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.

Florent B.

unread,
May 4, 2016, 9:55:19 AM5/4/16
to Gremlin-users
Good to know :)

Thanks again a lot for your help ! :)

Manjunath Sindagi

unread,
Apr 27, 2017, 6:33:18 AM4/27/17
to Gremlin-users
What is identity here? Instead of identity or constant values, is there a way to pass another query and run the mathematical operations?

Daniel Kuppitz

unread,
Apr 27, 2017, 6:44:06 AM4/27/17
to gremli...@googlegroups.com
identity() is the current element / value, in this case the age.

Cheers,
Daniel

To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/183231ab-e69f-42e6-830a-4bc2e069d023%40googlegroups.com.

Manjunath Sindagi

unread,
Apr 27, 2017, 6:55:59 AM4/27/17
to Gremlin-users
Thanks Daniel.

Is there a way to fire two gremlin queries in one and do mathematical operation. ?

Daniel Kuppitz

unread,
Apr 27, 2017, 7:06:10 AM4/27/17
to gremli...@googlegroups.com
Not sure what you're after. Can you provide a tiny sample graph and briefly describe what you want to do?

Cheers,
Daniel

To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/f7293ffd-d9fa-46bd-aef0-6e151e7350ff%40googlegroups.com.

Manjunath Sindagi

unread,
Apr 28, 2017, 1:11:15 AM4/28/17
to Gremlin-users
Daniel,

It's little difficult to explain over mail, i shall try though

Attaching the graph,

1. An input is given to like Say p1, we need to find the total count of has_dependencies. (here total Cs)

2. after findling total Cs, find the total has_dependencies Cs has.

The ratio of 1/2 needs to be found.

Independently 2 queries are fired. However, now figuring out if this whole 1 & 2 step can be merged in to 1.

Thanks,
Manjunath
IMG_20170428_102747522.jpg

Daniel Kuppitz

unread,
May 8, 2017, 2:42:34 PM5/8/17
to gremli...@googlegroups.com
Hi Manunath,

I still don't get it. This is your sample graph:

g = TinkerGraph.open().traversal()
g.addV().property(id, 'A').as('a').
  addV().property(id, 'B1').as('b1').
  addV().property(id, 'B2').as('b2').
  addV().property(id, 'C').as('c').
  addV().property(id, 'D').as('d').
  addV().property(id, 'P1').as('p1').
  addV().property(id, 'P2').as('p2').
  addV().property(id, 'P3').as('p3').
  addE('has_info').from('a').to('b1').
  addE('has_info').from('a').to('b2').
  addE('has_dependency').from('c').to('b1').
  addE('has_dependency').from('d').to('b2').
  addE('xxx').from('b1').to('p1').
  addE('xxx').from('b1').to('p2').
  addE('xxx').from('b1').to('p3').iterate()

It's pretty clear how to get from P1 to C, but where do you get the 1:2 ratio from?

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/dfcfe21f-ec3d-4d51-9ae6-478b5587715b%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages