Skip to content

Instantly share code, notes, and snippets.

@dkuppitz
Created July 20, 2020 15:26
Show Gist options
  • Save dkuppitz/1a7a5195a7c124746453cf0a61b41d87 to your computer and use it in GitHub Desktop.
Save dkuppitz/1a7a5195a7c124746453cf0a61b41d87 to your computer and use it in GitHub Desktop.
g = TinkerGraph.open().traversal()
g.addV().property('name','united states').as('united states').
addV().property('name', 'california').as('california').
addV().property('name', 'los angeles').as('los angeles').
addV().property('name', 'san francisco').property('marked', 'true').as('san francisco').
addE('contains').from('united states').to('california').
addE('contains').from('california').to('los angeles').
addE('contains').from('california').to('san francisco').
addV().property('name', 'texas').as('texas').
addV().property('name', 'dallas').as('dallas').
addV().property('name', 'houston').as('houston').
addE('contains').from('united states').to('texas').
addE('contains').from('texas').to('dallas').
addE('contains').from('texas').to('houston').
addV().property('name', 'arizona').as('arizona').
addV().property('name', 'pima').as('pima').
addV().property('name', 'maricopa').as('maricopa').
addV().property('name', 'tucson').property('marked', 'true').as('tucson').
addV().property('name', 'phoenix').as('phoenix').
addE('contains').from('united states').to('arizona').
addE('contains').from('arizona').to('pima').
addE('contains').from('arizona').to('maricopa').
addE('contains').from('pima').to('tucson').
addE('contains').from('maricopa').to('phoenix')
solution1 = {
g.V().has('name', 'united states').
until(select('result').or().not(outE('contains'))).
repeat(sack(assign).
map(out('contains').
group().
by(coalesce(values('marked'), constant('false')))).
choose(select('true'),
coalesce(select('false'), sack()).project('result'),
select('false').unfold())).
coalesce(select('result').unfold(),
sack()).dedup().
values('name')
}
solution2 = {
g.V().has('name', 'united states').
until(select('result').or().not(outE('contains'))).
repeat(sack(assign).
map(out('contains').
group().
by(coalesce(values('marked'), constant('false')))).
choose(select('true'),
union(select('false').unfold(), sack().project('result')),
select('false').unfold())).
coalesce(select('result').unfold(),
sack()).dedup().
values('name')
}
solution1()
solution2()
g.V().has('name', 'tucson').properties('marked').drop()
g.V().has('name', 'pima').property('marked', 'true').iterate()
solution1()
solution2()
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV().property('name','united states').as('united states').
......1> addV().property('name', 'california').as('california').
......2> addV().property('name', 'los angeles').as('los angeles').
......3> addV().property('name', 'san francisco').property('marked', 'true').as('san francisco').
......4> addE('contains').from('united states').to('california').
......5> addE('contains').from('california').to('los angeles').
......6> addE('contains').from('california').to('san francisco').
......7>
......7> addV().property('name', 'texas').as('texas').
......8> addV().property('name', 'dallas').as('dallas').
......9> addV().property('name', 'houston').as('houston').
.....10> addE('contains').from('united states').to('texas').
.....11> addE('contains').from('texas').to('dallas').
.....12> addE('contains').from('texas').to('houston').
.....13>
.....13> addV().property('name', 'arizona').as('arizona').
.....14> addV().property('name', 'pima').as('pima').
.....15> addV().property('name', 'maricopa').as('maricopa').
.....16> addV().property('name', 'tucson').property('marked', 'true').as('tucson').
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV().property('name','united states').as('united states').
......1> addV().property('name', 'california').as('california').
......2> addV().property('name', 'los angeles').as('los angeles').
......3> addV().property('name', 'san francisco').property('marked', 'true').as('san francisco').
......4> addE('contains').from('united states').to('california').
......5> addE('contains').from('california').to('los angeles').
......6> addE('contains').from('california').to('san francisco').
......7>
......7> addV().property('name', 'texas').as('texas').
......8> addV().property('name', 'dallas').as('dallas').
......9> addV().property('name', 'houston').as('houston').
.....10> addE('contains').from('united states').to('texas').
.....11> addE('contains').from('texas').to('dallas').
.....12> addE('contains').from('texas').to('houston').
.....13>
.....13> addV().property('name', 'arizona').as('arizona').
.....14> addV().property('name', 'pima').as('pima').
.....15> addV().property('name', 'maricopa').as('maricopa').
.....16> addV().property('name', 'tucson').property('marked', 'true').as('tucson').
.....17> addV().property('name', 'phoenix').as('phoenix').
.....18> addE('contains').from('united states').to('arizona').
.....19> addE('contains').from('arizona').to('pima').
.....20> addE('contains').from('arizona').to('maricopa').
.....21> addE('contains').from('pima').to('tucson').
.....22> addE('contains').from('maricopa').to('phoenix')
==>e[36][25-contains->30]
gremlin>
gremlin> solution1 = {
......1> g.V().has('name', 'united states').
......2> until(select('result').or().not(outE('contains'))).
......3> repeat(sack(assign).
......4> map(out('contains').
......5> group().
......6> by(coalesce(values('marked'), constant('false')))).
......7> choose(select('true'),
......8> coalesce(select('false'), sack()).project('result'),
......9> select('false').unfold())).
.....10> coalesce(select('result').unfold(),
.....11> sack()).dedup().
.....12> values('name')
.....13> }
==>groovysh_evaluate$_run_closure1@3f63a513
gremlin>
gremlin> solution2 = {
......1> g.V().has('name', 'united states').
......2> until(select('result').or().not(outE('contains'))).
......3> repeat(sack(assign).
......4> map(out('contains').
......5> group().
......6> by(coalesce(values('marked'), constant('false')))).
......7> choose(select('true'),
......8> union(select('false').unfold(), sack().project('result')),
......9> select('false').unfold())).
.....10> coalesce(select('result').unfold(),
.....11> sack()).dedup().
.....12> values('name')
.....13> }
==>groovysh_evaluate$_run_closure1@53f0d09c
gremlin>
gremlin> solution1()
==>texas
==>los angeles
==>pima
==>maricopa
gremlin> solution2()
==>texas
==>california
==>pima
==>maricopa
gremlin>
gremlin> g.V().has('name', 'tucson').properties('marked').drop()
gremlin> g.V().has('name', 'pima').property('marked', 'true').iterate()
gremlin>
gremlin>
gremlin> solution1()
==>maricopa
==>texas
==>los angeles
gremlin> solution2()
==>arizona
==>texas
==>california
==>maricopa
gremlin>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment