Using gremlin in scala

129 views
Skip to first unread message

hansl...@gmail.com

unread,
Sep 19, 2017, 2:58:03 PM9/19/17
to Gremlin-users
Hi
I am new to gremlin using to detect cycle in a directed graph.
I believe following query in groovy does that, but I am unable to write it in scala
g.V().as("a").repeat(out().simplePath()).emit(loops().is(gt(1))).where(out().as("a")).path()

Appreciate any help.
Thanks.
--Hans

Michael Pollmeier

unread,
Sep 20, 2017, 9:52:59 PM9/20/17
to Gremlin-users
Please post some graph setup and expected results.

HadoopMarc

unread,
Sep 21, 2017, 6:08:34 AM9/21/17
to Gremlin-users
Hi Hans,

Check the class named "__" in the Java API. This class is necessary for anonymous traversals like the out().simplePath() traversal. In the groovy examples the __ is not visible. You could also look into gremlin-scala (authored by the other poster), I do not know how anonymous traversals are handled there.

Cheers,    Marc

Op dinsdag 19 september 2017 20:58:03 UTC+2 schreef hansl...@gmail.com:

hansl...@gmail.com

unread,
Sep 21, 2017, 2:57:37 PM9/21/17
to Gremlin-users
Thank you, Michael.

Here is the code snippet in scala script. I need to implement isDirectedGraphCyclic(TinkerGraph) to detect the cycle.
Thank you for the help.
--Hans

/*********************************************************/
{code}

import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph

def isDirectedGraphCyclic(graph: TinkerGraph): Boolean = {
//Need to write the query similar to
//g.V().as("a").repeat(out().simplePath()).emit(loops().is(gt(1))).where(out().as("a")).path()
return true
}

val graph = TinkerGraph.open
val g = graph.traversal
val vA = g.addV("a").next
val vB = g.addV("b").next
val vC = g.addV("c").next
val vD = g.addV("d").next

vA.addEdge("next", vB)
vA.addEdge("next", vD)
vC.addEdge("next", vA)
vC.addEdge("next", vD)

//Assert graph has no cycles
val resultNo = isDirectedGraphCyclic(graph)

//Add another edge and make sure it becomes cyclic
vB.addEdge("next", vC)
val resultYes = isDirectedGraphCyclic(graph)

{code}
/***********************************************/

Michael Pollmeier

unread,
Sep 22, 2017, 4:55:28 AM9/22/17
to Gremlin-users
Thanks for sharing, I also learned something from this :)
Here's how you can implement it with gremlin-scala

import gremlin.scala._
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph

val graph
= TinkerGraph.open.asScala

val vA
= graph + "a"
val vB
= graph + "b"
val vC
= graph + "c"
val vD
= graph + "d"
vA
--- "next" --> vB
vA
--- "next" --> vD
vC
--- "next" --> vA
vC
--- "next" --> vD

def isDirectedGraphCyclic: Boolean = {
  val paths
= graph.V.as("a")
   
.repeat(_.out.simplePath)
   
.emit
   
.where(_.out.as("a"))
   
.toList

  paths
.size == 0

}

//Assert graph has no cycles
assert(isDirectedGraphCyclic)


//Add another edge and make sure it becomes cyclic

vB
--- "next" --> vC
assert(!isDirectedGraphCyclic)



hansl...@gmail.com

unread,
Sep 22, 2017, 5:50:11 PM9/22/17
to Gremlin-users
Thank you, Michael.
This looks good.

--Hans
Reply all
Reply to author
Forward
0 new messages