It's interesting how questions about Gremlin go through phases of
popularity. For example, there was the period of "how do i search for
string property values that start with 'a'". Before that we had "how do i
do basic mathematical operations". In each case, we've added documentation
or new features that makes those questions die off and then something new
emerges as something everyone is trying to do.

As of the last 6 months, I'd say the new popular thing to ask about has
been "how do I dynamically add properties to graph elements" or some
variation of that. I've written a blog post on the topic recently:

http://stephen.genoprime.com/snippet/2020/03/28/snippet-7.html

so that I can typically just paste that link to folks who need more
details. There are also a variety of SO and gremlin-users posts on the
topic and yet the question persists as there seem to always be new forms of
the question.

In looking at the pattern we offer, it is a "complex" solution given that:

1. It is a sufficiently common task that actually has some neat benefits in
terms of performance - i.e. it is far better to send a shorter traversal
with a long list of parameters than a long traversal with tons of steps
(which we typically see folks do for "bulk loading")
2. It is a task that new users who may not be familiar with Gremlin
immediately want to try and they don't see the "pattern" aspect of it - it
just looks like a blob of Gremlin to cut/paste

I've often felt as though we'd missed something important in Gremlin where
we don't deal with Maps well. As it is a data type users work with
constantly, a typical expectation would be for Gremlin to allow:

g.addV([T.label: "person", name:'marko', age: 29, country: 'usa'])

While we know that doesn't fit our API exactly that's the kind of ease that
I think is expected rather than:

v = g.addV("person").
      property("name", "marko").
      property("age", 29).
      property("country", "usa").next()

or dynamically (not including T.label):

m = [name:'marko',age:29,country:'usa']
g.withSideEffect('properties',m).
  addV('person').as('vertex').
  sideEffect(select('properties').
             unfold().as('kv').
             select('vertex').
             property(select('kv').by(Column.keys),
                      select('kv').by(Column.values)))

An interesting parallel to this is the merging of Map instances which is
another common collection manipulation function. Here's a rough
demonstration of it:

gremlin>
g.inject([name:'marko'],[age:29]).unfold().group().by(keys).by(values)
==>[name:[marko],age:[29]]

which again relies on unfold() to deconstruct the maps into key/value
pairs. This operation is quite analogous to what was done above for
dynamically setting properties (right down to the concept of
multi-properties).

If we think of a Graph Element as a Map then we can unify these patterns
behind a step like merge() where you could push one set of key/values into
another. Roughly speaking merge() might be like:

g.withSideEffect('properties',m).
  addV('person').
  merge('properties')

Perhaps from() and to() modulators could come into play instead:

g.withSideEffect('properties',m).
  addV('person').as('p').
  merge().from('properties).to('p')

Which would let the following be equivalent:

g.inject([name:'marko'],[age:29]).unfold().group().by(keys).by(values)
g.inject([name:'marko'],[age:29])..merge()

and perhaps then:

g.inject([name:'marko'],[age:29])..merge().by(unfold())

which would have the effect of keeping single cardinality.

Well, just something to think about since this issue keeps coming up again
and again. Happy to hear any thoughts on the topic.

Reply via email to