gpt4 book ai didi

java - GORM/Grails 中的家谱模型?

转载 作者:搜寻专家 更新时间:2023-11-01 01:43:36 24 4
gpt4 key购买 nike

我在 GORM/Grails 中为家谱建模时遇到问题。我知道建议使用有向无环图来对这样的结构进行建模。

我从 Person 类开始:

class Persion {
String name
}

一个可以有以下关系:

  • 0 到 n 个 child
  • 0 到 n 个 sibling (可以是姐妹或兄弟)
  • 0 到 n 个伴侣(可以是妻子或丈夫)
  • 0 到 n 个 parent (可能是母亲或父亲)

<强>1。我必须如何为这种关系结构建模?

<强>2。如何从这些结构中插入或删除人员?

<强>3。如何保证生成的图没有环?

编辑:

假设我们有一个人 A:

  • 子女关系:

    • 如果将 child B 添加到 A,则 child B 必须将 A 作为 parent 。
  • parent 关系:

    • 如果将父 C 添加到 A,则 C 有子 A
  • 合作伙伴关系:

    • 如果你把搭档D加到A,那么D就有搭档A

最佳答案

你可以像下面的类(class)那样做。相当于 child 、 parent 等。

class TreeNode  

{


String name





/**
* This method deletes a node and all the relations that are bound to this node.
* @return
*/
def deleteNode() {

// delete all child relations
def myChildren = getChildren()
println "myChildren: "+myChildren*.name

myChildren.each { child ->
println "child: "+child.name
removeFromChildren(child)
}

// delete all parent relations
def myParents = getParents()
println "myParents: "+myParents*.name
myParents.each { parent ->
println "parent: "+parent.name
removeFromParents(parent)
}


delete(flush:true)
}


TreeSet<TreeNode> getChildren() {
TreeNodeChild.executeQuery("select tnc.child from TreeNodeChild tnc where tnc.node = :node", [node: this])
}


TreeNode removeFromChildren(TreeNode child) {
TreeNodeChild.findByNodeAndChild(this, child).delete(flush: true)
this
}

/**
* Add a node as type (i.e. child) to another node.
* @param child
* @return
*/
TreeNode addToChildren(TreeNode child) {
TreeNodeChild tnc = new TreeNodeChild(node: this, child: child)
if (tnc.validate()) {

if (!isCyclic(child, "type")) {
println ">>>>>>>> no cycle"
tnc.save(flush: true)
}
else {
println ">>>>>>>> !!!!!!! cycle found"
}
}
this
}


TreeSet<TreeNode> getParents() {
TreeNodeChild.executeQuery("select tnc.node from TreeNodeChild tnc where tnc.child = :child", [child: this])
}

TreeNode removeFromParents(TreeNode parent) {
TreeNodeChild.findByNodeAndChild(parent, this).delete(flush: true)
this
}

TreeNode addToParents(TreeNode parent) {
TreeNodeChild tnc = new TreeNodeChild(node: parent, child: this)
if (tnc.validate()) {

if (!parent.isCyclic(this, "type")) {
println ">>>>>>>> no cycle"
tnc.save(flush: true)
}
else {
println ">>>>>>>> !!!!!!! cycle found"
}
}
this
}







private boolean isCyclic(node) {
boolean cyclic = false
def myParents = this.getParents()

// if there are parents of this node
if (myParents.size() != 0) {

// if the new node is in the parents set of this node
if (myParents.contains(node)) {
cyclic = true
return cyclic
}
else {
// go into each parent of this node and test if new node is contained in their parents
myParents.each { parent ->
if (cyclic) {
return cyclic
}
cyclic = parent.isCyclic(node)
}
}
}

return cyclic
}





}

关于java - GORM/Grails 中的家谱模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19528687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com