Search code examples
apachegremlintinkerpop3

Is it possible to exclude vertex on repeat().until()?


I am trying to come up with a gremlin query where I can infer who knows Jack but excluding information who directly comes from Employees.

Who can call for a reference on Jack? A reference cannot be obtained from another employee or from a family relative of the subject.

The query should return:

  • Jim knows Mary knows Jack

  • Jim Knows Mary Employed IBM Employed John Employed Coca Cola Employed Jack

  • Jerry Knows John Employed Coca Cola Employedd Jack

But it shouldn’t include:

  • Jerry Knows Jacks’ Borther Family Jack

  • Jerry Employed Coca Cola Employeed Jack

  • Jeane Degree_from Princeton Degree_From Jack

See the following graph: enter image description here

I came up with the following query but I can't get the part where I exclude Jerry or jane from the path.

g.V().has('isSSEmployee',true).repeat(bothE('knows','employeed').otherV().simplePath()).until(has('name','Jack')).has('isSSEmployee',false).path().by('name').by(label) 

Script to load the data

//only for tinkerpop
graph=TinkerGraph.open()
g=graph.traversal()
//from here on common between neptune and tinkerpop

//Setup started
//create vertexes

//create a person John
g.addV('person').property(id,'p1')
g.V('p1').property('questId','123456')
g.V('p1').property('name','John')


//create a person Jack
g.addV('person').property(id,'p2')
g.V('p2').property('questId','123457')
g.V('p2').property('name','Jack')

//create a person Mary
g.addV('person').property(id,'p3')
g.V('p3').property('questId','123458')
g.V('p3').property('name','Mary')

//create a person Jim and mark him as ssemployee
g.addV('person').property(id,'p4')
g.V('p4').property('name','Jim')
g.V('p4').property('isSSEmployee',true)
g.V('p4').property('questId','1234569')

//create a person Jerry and mark him as ssemployee

g.addV('person').property(id,'p5')
g.V('p5').property('name','Jerry')
g.V('p5').property('questId','12345700')
g.V('p5').property('isSSEmployee',true)

//create a person Jack's Brother 
g.addV('person').property(id,'p6')
g.V('p6').property('name',"Jack's Brother")
g.V('p6').property('questId','1234')

//create Jeane our employee
g.addV('person').property(id,'p7')
g.V('p7').property('name', 'Jeane')
g.V('p7').property('questId', '1234580')
g.V('p7').property('isSSEmployee', true)

//create a company Coca Cola
g.addV('company').property(id,'c1')
g.V('c1').property('name','Coca Cola')
g.V('c1').property('questId','123456')

//create a company IBM

g.addV('company').property(id,'c2')
g.V('c2').property('name','IBM')
g.V('c2').property('questId','123457')

//create an university Princeton
g.addV('univeristy').property(id,'u1')
g.V('u1').property('name','Princeton')


//create edges
//Coca Cola employes John
g.addE('employeed').from(g.V('c1')).to(g.V('p1')).property(id,'c1p1')
g.E('c1p1').property('fromDate','2009-12-13').next()
g.E('c1p1').property('toDate','2019-12-13').next()

//Coca Cola employes Jack
g.addE('employeed').from(g.V('c1')).to(g.V('p2')).property(id,'c1p2')
g.E('c1p2').property('fromDate','2006-12-13').next()
g.E('c1p2').property('toDate','2009-12-12').next()

//Coca Cola employed Jerry our employee
g.addE('employeed').from(g.V('c1')).to(g.V('p5')).property(id,'c1p5')
g.E('c1p5').property('fromDate','2007-12-13').next()
g.E('c1p5').property('toDate','2008-12-13').next()

//IBM employees John

g.addE('employeed').from(g.V('c2')).to(g.V('p1')).property(id,'c2p1')
g.E('c2p1').property('fromDate','2006-12-13').next()
g.E('c2p1').property('toDate','2009-12-13').next()

//IBM employes Mary

g.addE('employeed').from(g.V('c2')).to(g.V('p3')).property(id,'c2p3')
g.E('c2p3').property('fromDate','2006-10-11').next()
g.E('c2p3').property('toDate','2009-10-11').next()


//Jim our employee knows Mary

g.addE('knows').from(g.V('p4')).to(g.V('p3')).property(id,'p4p3')

//Jerry our employee knows John

g.addE('knows').from(g.V('p5')).to(g.V('p1')).property(id,'p5p1').next()

//Mary knows Jack
g.addE('knows').from(g.V('p3')).to(g.V('p2')).property(id,'p3p2')

//Jerry our employee knows Jack's Brother
g.addE('knows').from(g.V('p5')).to(g.V('p6')).property(id,'p5p6')

//Jack's Brother is Jack's Brother (family relation)
g.addE('family').from(g.V('p6')).to(g.V('p2')).property(id,'p6p2')

//Jeane our employee got a degree from Princeton
g.addE('degree_from').from(g.V('p7')).to(g.V('u1')).property(id,'p7u1')
g.E('p7u1').property('class_of',1989)

//Jack got a degree from Princeton
g.addE('degree_from').from(g.V('p2')).to(g.V('u1')).property(id,'p2u1')
g.E('p2u1').property('class_of',1989)


g.V().has('isSSEmployee',true).
  repeat(bothE().otherV().simplePath()).
    until(has('name','Jack')).
  path().
    by('name'). 
    by(label)


Solution

  • Here is what I come up with as right answer

    g.V().has('person','name','Jack').
                  repeat(bothE().has(label,neq('family')).otherV().hasNot('isSSEmployee').simplePath()).
                   until(has(label,'person')).
                   bothE().otherV().has('isSSEmployee',true).path().
                   by('name').
                   by(label)