Search code examples
gremlintinkerpop3

How to Generate TinkerPOP ByteCode?


I wonder if anyone can give me some pointers (that is where to start), on how to implement a translation from let say, a query language A to TinkerPop Graph Traversal. Let assume that A semantic intuitively translate to a subset of TinkerPop Traversal. In other words, what i am asking is:

  1. Where Can I find the set instruction set of TinkerPOP Virtual Machine
  2. What is TinkerPOP virtual Machine ByteCode Like.
  3. Is there any documentation API that helps toward that.

The documentation says it is easy to generate the ByteCode but does not get in great details about the bytecode and its shapes and so on.

I am hoping that someone could help with that: the section Graph Language Provider is empty in the current documentation


Solution

  • On the JVM you can get a Bytecode object from any traversal with asAdmin().getBytecode() as follows:

    gremlin> g.V().hasLabel('person').out().in().tree().asAdmin().getBytecode()
    ==>[[], [V(), hasLabel(person), out(), in(), tree()]]
    

    Converted to GraphSON format the Bytecode format looks like this (example from the IO documentation):

    {
      "@type" : "g:Bytecode",
      "@value" : {
        "step" : [ [ "V" ], [ "hasLabel", "person" ], [ "out" ], [ "in" ], [ "tree" ] ]
      }
    }
    

    The full instruction set is basically bound to the JVM at this time and is simply the list of Gremlin steps plus related expressions/tokens (e.g. P, T, etc). We are currently working on getting Gremlin defined more as a specification first rather than having it bound to the JVM as it is today, but that will take some time to complete.

    Note that you are talking about development of a Gremlin Compiler. There already is an example which is just about ready for release as I write this in sparql-gremlin - pre-release documentation can be found here. This module takes the SPARQL query language and converts it to Gremlin Bytecode.

    gremlin> graph = TinkerFactory.createModern()
    ==>tinkergraph[vertices:6 edges:6]
    gremlin> g = graph.traversal(SparqlTraversalSource) //1\
    ==>sparqltraversalsource[tinkergraph[vertices:6 edges:6], standard]
    gremlin> g.sparql("""SELECT ?name ?age
                         WHERE { ?person v:name ?name . ?person v:age ?age }
                         ORDER BY ASC(?age)""") //2\
    ==>[name:vadas,age:27]
    ==>[name:marko,age:29]
    ==>[name:josh,age:32]
    ==>[name:peter,age:35]
    

    The code is not terribly complex - perhaps you could look to it for inspiration. If you have further questions please consider asking them on gremlin-users mailing list. It would be great to see more Gremlin Compilers available. There are currently others (SQL and Cypher), but I believe that the SPARQL compiler is the only one that is Bytecode based at this time.