Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I build a tree hierarchy from a list in groovy using recursive closure?

I defined the recursive domain class in grails:

class Work {

  String code
  String title
  String description
  static hasMany = [subWorks:Work]
  static mappedBy = [subWorks: 'parentWork']

  Work getRootWork(){
    if(parentWork) return parentWork.getRootWork()
      else return this
  }

  boolean isLeafWork(){
    return subWorks.isEmpty()
  }

  boolean isRootWork(){
    return !parentWork
  }

I have a list of Works, but the hierarchy structure is not built yet. The structure looks like:

def works = [new Work(code:'A', title:'TitleA'), 
    new Work(code:'B', title:'TitleB'), 
    new Work(code:'A.1', title:'Titile A.1'), 
    new Work(code:'B.1', title:'Title B.1'),
    new Work(code:'B.2', title:'Title B.2'),
    new Work(code:'B.3', title:'Title B.3'), 
    new Work(code:'B.2.2', title:'Title B.2.2'),
    new Work(code:'B.2.3', title:'Title B.2.3'),
    new Work(code:'A.1.1', title:'Title A.1.1'),
    new Work(code:'A.1.2', title:'Title A.1.2'),]

What I need is to build the hierarchical relationship among these works, based on the code hinted. e.g. A.1 is the first child work of A; B.1.1 is the first child of B.1 work, whose parent is B work. I know that Groovy supports recursive closures to build this kind of hierarchical structure. How do I achieve my goal using Groovy recursive closure, such as the JN2515 Fibonacci number example, in Groovy official documentation? Many thanks!

like image 831
Yi. Avatar asked Oct 05 '22 10:10

Yi.


1 Answers

like this...?

def root = new Work(code:'*', title:'ROOT')

def build 

build = { p, list ->
  list.groupBy{it.code.split('\\.').first()}.each{ el, sublist ->
    el = sublist[0]        
    el.parentWork = p
    if(sublist.size()>1){
        build(el, sublist[1..-1] )
    }
  }

}
build(root, works.sort{it.code.length()})

if I'm not in error even in this anonim form may work

def root = new Work(code:'*', title:'ROOT')

{ p, list ->
  list.groupBy{it.code.split('\\.').first()}.each{ el, sublist ->
    el = sublist[0]        
    el.parentWork = p
    if(sublist.size()>1){
      call(el, sublist[1..-1] )
    }
  }

}(root, works.sort{it.code.length()})
like image 77
Fabiano Taioli Avatar answered Oct 13 '22 10:10

Fabiano Taioli