Added a couple of recipes - collections/edge move CTR
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/bef9e9bc Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/bef9e9bc Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/bef9e9bc Branch: refs/heads/TINKERPOP-1489 Commit: bef9e9bce3961fff0a3eaddaa59fe6dcd94e56f1 Parents: 189953a Author: Stephen Mallette <sp...@genoprime.com> Authored: Wed Dec 27 10:00:33 2017 -0500 Committer: Stephen Mallette <sp...@genoprime.com> Committed: Wed Dec 27 10:01:38 2017 -0500 ---------------------------------------------------------------------- docs/src/recipes/collections.asciidoc | 67 ++++++++++++++++++++++++ docs/src/recipes/edge-move.asciidoc | 57 ++++++++++++++++++++ docs/src/recipes/index.asciidoc | 4 ++ docs/static/images/gremlin-collections.png | Bin 0 -> 48305 bytes docs/static/images/gremlin-edge.png | Bin 0 -> 79829 bytes 5 files changed, 128 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bef9e9bc/docs/src/recipes/collections.asciidoc ---------------------------------------------------------------------- diff --git a/docs/src/recipes/collections.asciidoc b/docs/src/recipes/collections.asciidoc new file mode 100644 index 0000000..446ed46 --- /dev/null +++ b/docs/src/recipes/collections.asciidoc @@ -0,0 +1,67 @@ +//// +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +//// +[[collections]] +== Collections + +image:gremlin-collections.png[width=400] + +Lists and maps form the basis for much of the processing in Gremlin traversals. They are core to how side-effects +are typically held and how results are generally produced. Being able to pick them apart and reformat them is sometimes +required. + +One of the most common ways to end up with a `Map` is with `valueMap()`: + +[gremlin-groovy,modern] +---- +g.V().has('name','marko').valueMap('name','age') +---- + +The problem is that unless the graph is making use of multi-properties, there is little need to have the value of each +property stored as a `List`. One way to unwrap this value from the list is to avoid having it there in the first place +by avoiding use of `valueMap()`: + +[gremlin-groovy,modern] +---- +g.V().has('name','marko'). + local(properties('name','age'). + group().by(key()).by(value())) +---- + +Interestingly, it's worth looking at how to process the output of `valueMap()` to attain this output as the approach is +generally applicable to processing any `Map` instances with any sorts of values: + +[gremlin-groovy,modern] +---- +g.V().has('name','marko'). + valueMap('name','age'). + unfold(). + group(). + by(keys). + by(select(values).unfold()) +---- + +The code above, basically deconstruct then reconstructs the `Map`. The key to the pattern is to first `unfold()` the +`Map` into its key and value entries. Then for each key and value produce a new `Map` using `group()` where the key +for that map is the key of the entry (those are obviously unique as you picked them out of the `valueMap()`) and the +value is simply the `unfold()` of the list of values in each entry. Recall that the `select(values).unfold()` only +returns one value (i.e. the first) not only because there is only one, but also because `by()` will only call `next()` +on that sub-traversal (it does not iterate the entire thing). + +Generally speaking, a `Map` constructed as part of `group()` or `project()` will already be in the form required as +the `by()` modulators would be written in such a fashion as to produce that final output. It would be unnecessary to +deconstruct/reconstruct it. Be certain that there isn't a way to re-write the `group()` or `project()` to get the +desired output before taking this approach. \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bef9e9bc/docs/src/recipes/edge-move.asciidoc ---------------------------------------------------------------------- diff --git a/docs/src/recipes/edge-move.asciidoc b/docs/src/recipes/edge-move.asciidoc new file mode 100644 index 0000000..f31b060 --- /dev/null +++ b/docs/src/recipes/edge-move.asciidoc @@ -0,0 +1,57 @@ +//// +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +//// +[[edge-move]] +== Moving an Edge + +image:gremlin-edge.png[width=145] + +Aside from their properties, edges are immutable structures where the label and the related in and out vertices +cannot be modified. To "move" an edge from one vertex to another, it requires that the edge be dropped and a new edge +be created with the same properties and label. It is possible to simulate this "move" in a single traversal as +follows: + +[gremlin-groovy,modern] +---- +g.V().has('name','marko'). + outE().inV(). + path().by('name').by(valueMap(true)) +---- + +The "marko" vertex contains a "knows" edge to the "vadas" vertex. The following code shows how to "move" that edge to +the "peter" vertex in a single traversal: + +[gremlin-groovy,modern] +---- +g.V().has('name','marko').as('a'). + outE('knows').as('e1').filter(inV().has('name','vadas')). <1> + V().has('name','peter'). + addE('knows').from('a').as('e2'). <2> + sideEffect(select('e1').properties(). + unfold().as('p'). + select('e2'). + property(select('p').key(), select('p').value())). <3> + select('e1').drop() <4> +g.V().has('name','marko'). + outE().inV(). + path().by('name').by(valueMap(true)) +---- + +<1> Find the edge to "move" and label that as "e1". It will be necessary to reference this later to get the edge +properties to transfer to the new "moved" edge. +<2> Add the "moved" edge and label it as "e2". +<3> Use a `sideEffect()` to transfer the properties from "e1" to "e2". +<4> Use `drop()` to get rid of the old edge at "e1" now that the new "e2" edge is in place. \ No newline at end of file http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bef9e9bc/docs/src/recipes/index.asciidoc ---------------------------------------------------------------------- diff --git a/docs/src/recipes/index.asciidoc b/docs/src/recipes/index.asciidoc index bb88301..c59cc89 100644 --- a/docs/src/recipes/index.asciidoc +++ b/docs/src/recipes/index.asciidoc @@ -38,6 +38,8 @@ include::between-vertices.asciidoc[] include::centrality.asciidoc[] +include::collections.asciidoc[] + include::connected-components.asciidoc[] include::cycle-detection.asciidoc[] @@ -46,6 +48,8 @@ include::duplicate-edge.asciidoc[] include::duplicate-vertex.asciidoc[] +include::edge-move.asciidoc[] + include::if-then-based-grouping.asciidoc[] include::pagination.asciidoc[] http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bef9e9bc/docs/static/images/gremlin-collections.png ---------------------------------------------------------------------- diff --git a/docs/static/images/gremlin-collections.png b/docs/static/images/gremlin-collections.png new file mode 100755 index 0000000..178c166 Binary files /dev/null and b/docs/static/images/gremlin-collections.png differ http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/bef9e9bc/docs/static/images/gremlin-edge.png ---------------------------------------------------------------------- diff --git a/docs/static/images/gremlin-edge.png b/docs/static/images/gremlin-edge.png new file mode 100755 index 0000000..62b834f Binary files /dev/null and b/docs/static/images/gremlin-edge.png differ