Return a unicode object with the Gremlin representation of this block.

def to_gremlin(self):
        """Return a unicode object with the Gremlin representation of this block."""
        self.validate()
        if self.optional:
            # Optional edges have to be handled differently than non-optionals, since the compiler
            # provides the guarantee that properties read from an optional, non-existing location
            # always resolve to a "null" value. This guarantee is not upheld by default by Gremlin;
            # in fact, Gremlin .as('foo').out().as('bar').optional('foo') does not provide
            # ANY guarantees as to what the value at any "bar.*" is -- it could be "null",
            # it could be a previous pipeline element's location at "bar.*" or anything else.
            # The .ifThenElse block ensures that the edge traversal happens only if the edge exists,
            # and that otherwise the result in the pipeline is replaced with "null".
            #
            # The code below makes the assumption that links to outward/inward edges are stored
            # as vertex properties named "<direction>_<edge_name>" where direction is "in" or "out".
            # For example, the links to outward edges named "Person_SpeechBy" from Person
            # are assumed to be stored as "out_Person_SpeechBy" on the Person node.
            return (u'ifThenElse{{it.{direction}_{edge_name} == null}}'
                    u'{{null}}{{it.{direction}({edge_quoted})}}'.format(
                        direction=self.direction,
                        edge_name=self.edge_name,
                        edge_quoted=safe_quoted_string(self.edge_name)))
        elif self.within_optional_scope:
            # During a traversal, the pipeline element may be null.
            # The following code returns null when the current pipeline entity is null
            # (an optional edge did not exist at some earlier traverse).
            # Otherwise it performs a normal traversal (previous optional edge did exist).
            return (u'ifThenElse{{it == null}}'
                    u'{{null}}{{it.{direction}({edge_quoted})}}'.format(
                        direction=self.direction,
                        edge_quoted=safe_quoted_string(self.edge_name)))
        else:
            return u'{direction}({edge})'.format(
                direction=self.direction,
                edge=safe_quoted_string(self.edge_name))