-2

I'm having problem converting the Strings "[3]" here:

  p = {"results":["[3]","[3]"],"success":true,"version":"2.4.0","queryTime":63.102287}

to Lists.

p[0] gives me:

 {"results":["[3]"],"success":true,"version":"2.4.0","queryTime":68.24303}

So I was thinking I could use Eval.me(p[0]) but thet gives me an error saying:

  {"message":"","error":"javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: static groovy.util.Eval.me() is applicable for argument types: (com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline) values: [[GremlinStartPipe, GraphQueryPipe(has,vertex), IdentityPipe, PropertyPipe(Sannolikhet), RangeFilterPipe(0,0)]]\nPossible solutions: me(java.lang.String), me(java.lang.String, java.lang.Object, java.lang.String), is(java.lang.Object), use([Ljava.lang.Object;), _(groovy.lang.Closure), dump()","api":{"description":"evaluate an ad-hoc Gremlin script for a graph.","parameters":{"rexster.returnKeys":"an array of element property keys to return (default is to return all element properties)","rexster.showTypes":"displays the properties of the elements with their native data type (default is false)","load":"a list of 'stored procedures' to execute prior to the 'script' (if 'script' is not specified then the last script in this argument will return the values","rexster.offset.end":"end index for a paged set of data to be returned","rexster.offset.start":"start index for a paged set of data to be returned","params":"a map of parameters to bind to the script engine","language":"the gremlin language flavor to use (default to groovy)","script":"the Gremlin script to be evaluated"}},"success":false} 

Anyone that knows how this can be done without using Eval.me?

EDIT

To Clarify, the first part of my script:

 p = g.V('containerName','CvsRisk').outE.inV.hasNot('Sannolikhet',null).'Sannolikhet';

I "extract" all the vertices in a container with the containerName CvsRisk that has a property named "Sannolikhet" that is not null. Running this part of the script gives me the following response from the server:

  {"results":["[3]","[3,3]","[3,3]","[3,3]","[3,3]","[3,3]","[3,3]","[3]","[3,3]","[3]","[3,3]","[3,3]"],"success":true,"version":"2.4.0","queryTime":20.429118}

Which is what I want since all my properties named "Sannolikhet" contains an array of values.

It is from here I want to iterate through the result and sum up the values in the Strings, but in order to do so I need to convert the Strings to lists.

agiledevpro
  • 113
  • 1
  • 3
  • 15
  • 1
    so what code are you using right now? – cfrick Dec 15 '14 at 13:57
  • I've tried using Eval.me but that does not work, or what do you mean? – agiledevpro Dec 15 '14 at 14:00
  • 1
    1st of all, you are converting an instance of `GremlinGroovyPipeline` class here, and not the `String`. so, the 1st thing you need to do is `Eval.me(p[0].toString())` – injecteer Dec 15 '14 at 14:08
  • 2
    if the shown code there (which more looks like json) is the toString, then this will not work. i'd rather assume, that there is a trivial api to get the results. like p.results. – cfrick Dec 15 '14 at 14:12
  • I just sat the variable p here to the result I get from the server after running my code to explain my problem. I've tried p.results but that gived an error saying: "message":"","error":"java.lang.String cannot be cast to com.tinkerpop.blueprints.Element" – agiledevpro Dec 15 '14 at 14:18

2 Answers2

0

You can parse the resulting JSON with JsonSlurper and collect the results with Eval applied. If you are sure there is no method ready to get the value in gremlin, then i guess you have to work with something like this:

Update: on a second thought, p.results should probably work fine, forget the first two commented lines:

//result='{"results":["[3]","[3,3]","[3,3]","[3,3]","[3,3]","[3,3]","[3,3]","[3]","[3,3]","[3]","[3,3]","[3,3]"],"success":true,"version":"2.4.0","queryTime":20.429118}'

//json = new groovy.json.JsonSlurper().parseText result

p = ["results":["[3]","[3,3]","[3,3]","[3,3]","[3,3]","[3,3]","[3,3]","[3]","[3,3]","[3]","[3,3]","[3,3]"]]

assert p.results.size() == 12

vertices = p.results.collect { Eval.me(it) }

assert vertices.size() == 12

assert vertices[0] == [3]
assert vertices[1] == [3, 3]
assert vertices[-3] == [3]
assert vertices[-1] == [3, 3]
Will
  • 13,630
  • 1
  • 37
  • 42
  • Thank you for your answer, but p.results does not work. Doing p.results gives me an error saying "message":"","error":"java.lang.String cannot be cast to com.tinkerpop.blueprints.Element" – agiledevpro Dec 16 '14 at 07:53
0

I have found a solution

The whole solution is:

  def p = g.V('containerName','CvsRisk').outE.inV.hasNot('Sannolikhet',null).'Sannolikhet';
  m= [];for(i in p){m.add(Eval.me(i));};k=m.sum();k*.toInteger().sum()
agiledevpro
  • 113
  • 1
  • 3
  • 15