Previous topic

Gremlin

Next topic

<no title>

Groovy

Scripts container for Gremlin-Groovy files.

The GroovyScripts class is used to store and manage an index of Gremlin-Groovy scripts.

By default, a GroovyScripts object is built into the Graph object as g.scripts. You write your Gremlin-Groovy scripts in a text file, which allows you to get full syntax highlighting in an editor.

Example (same example as used for Gremlin):

// gremlin.groovy

// calculate basic collaborative filtering for user_id
def rank_items(user_id) {
    m = [:]
    g.v(user_id).out('likes').in('likes').out('likes').groupCount(m)
    m.sort{a,b -> a.value <=> b.value}
    return m.values()
}

The scripts file can contain more than one function per file.

You add the scripts file to the Scripts object using the g.scripts.update(file_path) method. Then you can get the individual scripts by their function names:

>>> from bulbs.neo4jserver import Graph
>>> g = Graph()                              # create Neo4j Graph object
>>> g.scripts.update('gremlin.groovy')       # add file to scripts index
>>> script = g.scripts.get('rank_items')     # get a function by its name
>>> params = dict(user_id=3)                 # put function params in dict
>>> items = g.gremlin.query(script, params)  # execute the script in DB

Something goes here.

class bulbs.groovy.GroovyScripts(config, file_path=None)[source]

Store and manage an index of Gremlin-Groovy scripts.

Parm config:

Config object.

Parameters:

file_path (str) – Path to the base Groovy scripts file.

Variables:
  • config – Config object.
  • source_files – List containing the absolute paths to the script files, in the order they were added.
  • methods – LastUpdatedOrderedDict mapping Groovy method names to the Python Method object, which is a namedtuple containing the Groovy script’s definition, signature, body, and sha1.

Note

Use the update() method to add subsequent script files. Order matters. Groovy methods are overridden if subsequently added files contain the same method name as a previously added file.

default_file = 'gremlin.groovy'

Relative path to the default script file

get(method_name, namespace=None)[source]

Returns the Groovy script with the method name.

Parameters:method_name (str) – Method name of a Groovy script.
Return type:str
get_method(method_name, namespace=None)[source]

Returns a Python namedtuple for the Groovy script with the method name.

Parameters:method_name (str) – Name of a Groovy method.
Return type:bulbs.groovy.Method
update(file_path, namespace=None)[source]

Updates the script index with the Groovy methods in the script file.

Return type:None
refresh()[source]

Refreshes the script index by re-reading the Groovy source files.

Return type:None