-
Notifications
You must be signed in to change notification settings - Fork 11
Models
etscrivner edited this page Jan 25, 2013
·
6 revisions
thunderdome defines two base classes for getting started with your modeling Vertex and Edge. You can use these to define entities, their properties, and the relationships between them.
class Person(thunderdome.Vertex):
# All gremlin paths are relative to the path of the file where the class
# is defined.
gremlin_path = 'enrollments.groovy'
# vid is added automatically
name = thunderdome.Text()
age = thunderdome.Integer()
date_of_birth = thunderdome.DateTime()
# Gremlin methods
all_students = thunderdome.GremlinMethod()
class Class(thunderdome.Vertex):
name = thunderdome.Text()
credits = thunderdome.Decimal()
# edges
class EnrolledIn(thunderdome.Edge):
date_enrolled = thunderdome.DateTime()
class Teaches(thunderdome.Edge):
overall_mood = thunderdome.Text(default='Grumpy')Creating new Vertices
prof = Person.create(name='Professor Professorson',
age=300,
date_of_birth=datetime.datetime(1, 1, 1982))
student = Person.create(name='Johnny Boy',
age=56,
date_of_birth=datetime.datetime(1, 1, 1990))Examine properties of the Vertex
# Print UUID for object
print prof.vid
# Print Titan-specific id for object
print prof.eidLet's create a few more Vertices
physics = Class.create(name='Physics 264', credits=6426.3)
beekeeping = Class.create(name='Beekeeping', credits=23.3)Now we can create Edges between our vertices
# Enroll student in both classes
EnrolledIn.create(student, physics, date_enrolled=datetime.datetime.now())
EnrolledIn.create(student, beekeeping, date_enrolled=datetime.datetime.now())
# Set professor as teacher of both classes
Teaches.create(prof, physics, overall_mood='Pedantic')
Teaches.create(prof, beekeeping, overall_mood='Itchy')We can perform graph traversals like so:
# Get all teachers of a given class
physics.inV(Teaches)
# Get all students for a given class
physics.inV(EnrolledIn)
# Get all classes for a given student
student.outV(EnrolledIn)
# Get all moods for a list of teachers
class_moods = [x.overall_mood for x in prof.outE(Teaches)]
# Execute Gremlin method
# The eid is passed in automatically by thunderdome
all_students = prof.all_students()
for x in all_students:
print x.name