Graph traversal

46 views
Skip to first unread message

tampler

unread,
Apr 15, 2020, 7:50:19 AM4/15/20
to scala-graph
Hello

I wanna traverse an AST-based graph and call some method for each node. I made some impl, but can't call the node method, since I"m getting `NodeT` in return.

```scala
final case class NodeA(id: Int, op: Function1[Int, Int]) extends AST
final case class NodeB(id: Int, op: Function1[Int, Int]) extends AST
sealed trait AST extends Product with Serializable

val g = Graph.from(nodes, edges)
val root = g.get(n0)

// Traverse graph
def procNode(node: g.NodeT): Unit = println(node)

root.innerEdgeTraverser.map(_.foreach(procNode))

```
However, If I try to do something like:
```scala
def procNode(node: g.NodeT):Unit = node.id + 1
```
I'm getting compile error, since node is of type `g.NodeT`. How do I implement `procNode` correctly ?

Thank you!

Peter Empen

unread,
Apr 15, 2020, 8:47:23 AM4/15/20
to scala-graph
Your graph knows about the node type AST but not its subclasses. So you need either

sealed trait AST {
def id: Int
}

or match against node.value. That's all.

Btw., case classes do extend Serializable and also Product.

Peter Empen

unread,
Apr 15, 2020, 8:56:06 AM4/15/20
to scala-graph
You may want to go with

root.innerNodeTraverser foreach procNode

Daniel Sobral

unread,
Apr 15, 2020, 11:58:18 AM4/15/20
to tampler, scala-graph
If you don't want NodeT, why not do an *outer* traversal instead? Or, for that matter, just calling ".outer" on the node before passing it to procNode?

-- 
Daniel C. Sobral



--
You received this message because you are subscribed to the Google Groups "scala-graph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-graph...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-graph/aae77de1-1a7b-4028-92cf-b4fa90773919%40googlegroups.com.

tampler

unread,
Apr 15, 2020, 12:34:08 PM4/15/20
to scala-graph
Thanks for a prompt reply guys. Worked like a charm for me.


sealed trait AST            

final
case class NodeA(id: Int, op: Function1[Int, Int]) extends AST {
 def run(): Unit = println(">>>>> Run A")
}

final
case class NodeB(id: Int, op: Function1[Int, Int]) extends AST {
 def run(): Unit = println(">>>>> Run B")
}

// Traverse graph nodes
def procNode(node: g.NodeT): Unit = node.value match {
 
case n: NodeA => n.run()
 
case n: NodeB => n.run()
 
case _        =>
}
root
.innerNodeTraverser.foreach(procNode)
 


On Wednesday, April 15, 2020 at 6:58:18 PM UTC+3, Daniel Sobral wrote:
If you don't want NodeT, why not do an *outer* traversal instead? Or, for that matter, just calling ".outer" on the node before passing it to procNode?

-- 
Daniel C. Sobral



On Wed, Apr 15, 2020 at 5:50 AM tampler <socne...@gmail.com> wrote:
Hello

I wanna traverse an AST-based graph and call some method for each node. I made some impl, but can't call the node method, since I"m getting `NodeT` in return.

```scala
final case class NodeA(id: Int, op: Function1[Int, Int]) extends AST
final case class NodeB(id: Int, op: Function1[Int, Int]) extends AST
sealed trait AST extends Product with Serializable

val g = Graph.from(nodes, edges)
val root = g.get(n0)

// Traverse graph
def procNode(node: g.NodeT): Unit = println(node)

root.innerEdgeTraverser.map(_.foreach(procNode))

```
However, If I try to do something like:
```scala
def procNode(node: g.NodeT):Unit = node.id + 1
```
I'm getting compile error, since node is of type `g.NodeT`. How do I implement `procNode` correctly ?

Thank you!

--
You received this message because you are subscribed to the Google Groups "scala-graph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages