Custom Gremlin Example

Here we illustrate creating models and utilizing the Gremlin system for custom methods

Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from mogwai.connection import setup
from mogwai.models import Vertex, Edge
from mogwai import properties
from mogwai import gremlin
from mogwai._compat import print_
import datetime
from pytz import utc
from functools import partial
import os

setup('127.0.0.1')


class IsFriendsWith(Edge):
    label = 'is_friends_with'

    since = properties.DateTime(required=True,
                                default=partial(datetime.datetime.now, tz=utc),
                                description='Owned object since')


class Person(Vertex):

    element_type = 'person'  # this is optional, will default to the class name
    gremlin_path = os.path.join(os.getcwd(), 'custom_gremlin.groovy')

    name = properties.String(required=True, max_length=512)
    email = properties.Email(required=True)

    friends_and_friends_of_friends = gremlin.GremlinMethod(method_name='friends_and_friends_of_friends',
                                                           property=True,
                                                           defaults={'friend_edge_label': IsFriendsWith.get_label()})
    friends_and_friends_of_friends_table = gremlin.GremlinTable(method_name='friends_and_friends_of_friends_table',
                                                                property=True,
                                                                defaults={'friend_edge_label': IsFriendsWith.get_label()})

    @property
    def friends(self):
        return self.bothV(IsFriendsWith)


## Creation
# Create the People
bob = Person.create(name='Bob', email='bob@bob.net')
alice = Person.create(name='Alice', email='alice@alice.net')
john = Person.create(name='John', email='john@john.net')
tim = Person.create(name='Tim', email='tim@tim.net')
kyle = Person.create(name='Kyle', email='kyle@kyle.net')

# Create Friendships
IsFriendsWith.create(outV=bob, inV=alice)
IsFriendsWith.create(outV=alice, inV=john)
IsFriendsWith.create(outV=alice, inV=tim)
IsFriendsWith.create(outV=tim, inV=kyle)

## Traverse
# All known direct friends with Bob
bobs_friends = bob.friends
print_("Bobs direct friends: {}".format(bobs_friends))
# Output:
# [ Person(element_type=person, id=8132, values={'name': Alice, 'email': alice@alice.net}) ]


# Friends and Friends of Friends using Custom Gremlin Method
bobs_friends_and_friends_of_friends = bob.friends_and_friends_of_friends
print_("Bobs Friends and Friends of Friends: {}".format(bobs_friends_and_friends_of_friends))

# Output:
# [
#   [ Person(element_type=person, id=8128, values={'name': Bob, 'email': bob@bob.net}),
#     Person(element_type=person, id=8132, values={'name': Alice, 'email': alice@alice.net}),
#     Person(element_type=person, id=8136, values={'name': John, 'email': john@john.net})
#    ], [
#     Person(element_type=person, id=8128, values={'name': Bob, 'email': bob@bob.net}),
#     Person(element_type=person, id=8132, values={'name': Alice, 'email': alice@alice.net}),
#     Person(element_type=person, id=8140, values={'name': Tim, 'email': tim@tim.net})
#    ]
# ]

for friend_set in bobs_friends_and_friends_of_friends:
    assert len(friend_set) <= 3
    assert friend_set[0] == bob
    assert friend_set[1] == alice
    assert kyle not in friend_set


# GremlinTable
print_("Using GremlinTable:")
friend_table = bob.friends_and_friends_of_friends_table
for row in friend_table:
    print_("{} is friends with {}, who is also friends with {}".format(row.person.name, row.friend.name, row.friend_of_friend.name))

# Output:
# Using GremlinTable:
# Bob is friends with Alice, who is also friends with John
# Bob is friends with Alice, who is also friends with Tim

Gremlin

custom_gremlin.groovy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def friends_and_friends_of_friends(id, friend_edge_label) {
    /**
     * Traverse a Person and find any Friends of Friends
     *
     * :param id: Vertex id of the Person, if null abort
     * :param friend_edge_label: Edge label of the IsFriendsWith class
     * :returns: list[Person]
     */

     return g.v(id).out(friend_edge_label).loop(1){it.loops < 3}.path

}

def friends_and_friends_of_friends_table(id, friend_edge_label) {
    /**
     * Traverse a Person and find any Friends of Friends
     *
     * :param id: Vertex id of the Person, if null abort
     * :param friend_edge_label: Edge label of the IsFriendsWith class
     * :returns: list[Person]
     */
     return g.v(id).as('person').out(friend_edge_label).as('friend').out(friend_edge_label).simplePath.as('friend_of_friend').table{it}{it}{it}.cap
}