How to write a gremlin code as function?

Hi @luigidellaquila
I have the betweenness centrality code verified in gremlin, I need to add that as a function and pass the value.

Here is my code
g.V(g.V().has(‘PT’,‘TrxID’,‘T7’).next()).repeat(__.bothE().subgraph(‘subGraph’).bothV()).times(3).cap(‘subGraph’).next().traversal().V().as(“v”).repeat(both().simplePath().as(“v”)).emit().filter(project(“x”,“y”,“z”).by(select(first, “v”)).by(select(last, “v”)).by(select(all, “v”).count(local)).as(“triple”).coalesce(select(“x”, “y”).as(“a”).select(“triples”).unfold().as(“t”).select(“x”,“y”).where(eq(“a”)).select(“t”),store(“triples”)).select(“z”).as(“length”).select(“triple”).select(“z”).where(eq(“length”))).select(all, “v”).unfold().groupCount().next()

The ‘T7’ is my id for the record to pass.

If I pass that as parameter, it not working, May I know how to do that please

Also while calling from browse can I do that, like “select Betweeness_on_trans(T7)”

Thanks

Hi @hanzgs

by not working you mean that you get an error or no results?

Thanks

Hi @wolf4ood
It’s giving 500 error, but when I run the code in browse it works

Thanks

@hanzgs

do you have the error message?

Thanks

I have tried :id as well

Error is

{
“errors”: [
{
“reason”: “Bad request”,
“code”: 400,
“content”: "Error on evaluation of the script library. Error: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:\r\nScript10.groovy: 6: unexpected token: : @ line 6, column 29.\r\n g.V(g.V().has(‘PT’, ‘TrxID’,:id).next()).both().both().both().cyclicPath().path()\r\n ^\r\n\r\n1 error\r\n\nScript library was:\ndef AccLabels() {\ng.V().hasLabel(‘Account’) \n}\n\ndef CyclePaths(id) {\ng.V(g.V().has(‘PT’, ‘TrxID’,:id).next()).both().both().both().cyclicPath().path()\n}\n\ndef Labels() {\ng.V().label()\n}\n\ndef Betweeness_on_Trans() {\ng.V(g.V().has(‘PT’,‘TrxID’,‘T7’).next()).repeat(.bothE().subgraph(‘subGraph’).bothV()).times(3).cap(‘subGraph’).next().traversal().V().as(“v”).repeat(both().simplePath().as(“v”)).emit().filter(project(“x”,“y”,“z”).by(select(first, “v”)).by(select(last, “v”)).by(select(all, “v”).count(local)).as(“triple”).coalesce(select(“x”, “y”).as(“a”).select(“triples”).unfold().as(“t”).select(“x”,“y”).where(eq(“a”)).select(“t”),store(“triples”)).select(“z”).as(“length”).select(“triple”).select(“z”).where(eq(“length”))).select(all, “v”).unfold().groupCount().next()\n}\n\ndef Closeness_on_Trans() {\ng.V(g.V().has(‘PT’,‘TrxID’,‘T7’).next()).repeat(.bothE().subgraph(‘subGraph’).bothV()).times(3).cap(‘subGraph’).next().traversal().withSack(1f).V().as(“v”).repeat(both().simplePath().as(“v”)).emit().filter(project(“x”,“y”, “z”).by(select(first, “v”)).by(select(last, “v”)).by(select(all, “v”).count(local)).as(“triple”).coalesce(select(“x”, “y”).as(“a”).select(“triples”).unfold().as(“t”).select(“x”,“y”).where(eq(“a”)).select(“t”),store(“triples”)).select(“z”).as(“length”).select(“triple”).select(“z”).where(eq(“length”))).group().by(select(first,“v”)).by(select(all, “v”).count(local).sack(div).sack().sum()).next()\n}\n\n\r\n\tDB name=“Test1"”
}
]
}

Hi @hanzgs

g.V(g.V().has('PT', 'TrxID',id).next()).both().both().both().cyclicPath().path()

works for me.
Be aware that if you have a function that do not compile you will se also the exception when running a different function. OrientDB wrap all functions in 1 script that will be compiled by the script engine.
So you could see an exception in syntax of a different function

Thanks