配置继承机制

发布于 2024-09-07 06:54:09 字数 625 浏览 2 评论 0原文

我有以下结构:

config
  |-- groups
    |-- rootgroup
    |-- group1 (includes rootgroup)
    |-- group2 (includes group1)
    |-- group3 (includes rootgroup)
  |-- users
    |-- Fred (includes group3 and group2)

因此 Fred 的继承树将如下所示:

    _Fred_
   v      v
group2  group3
   v      v
group1    v
   v     /
rootgroup

我需要一个算法来打印从树的左下角开始的线性配置读取顺序(对于给定的示例,它将是 rootgroup - group1 - group2 - group3; group1覆盖 rootgroup,group2 覆盖 group1 等...)并查找递归链接(例如,如果 rootgroup 包含组 2),而且它必须找到递归循环(... -> group2 -> group1 -> rootgroup - >组2->...)。

首选语言是 python,但任何语言都可以。

谢谢。

I have the following structure:

config
  |-- groups
    |-- rootgroup
    |-- group1 (includes rootgroup)
    |-- group2 (includes group1)
    |-- group3 (includes rootgroup)
  |-- users
    |-- Fred (includes group3 and group2)

So inheritance tree for Fred will look like:

    _Fred_
   v      v
group2  group3
   v      v
group1    v
   v     /
rootgroup

I need an algorithm to print the linear config read order beginning at the bottom left of the tree (for given example it would be rootgroup - group1 - group2 - group3; group1 overwrites rootgroup, group2 overwrites group1, etc...) and find recursive links (for example if rootgroup includes group 2), moreover it must find the recursion loop (... -> group2 -> group1 -> rootgroup -> group2 -> ...).

Preferable language is python, but any will do.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

折戟 2024-09-14 06:54:09

在阅读了有向无环图(DAG)之后,我想出了以下解决方案:

def getNodeDepsTree(self, node, back_path=None):
    """Return whole dependency tree for given node"""
    # List of current node dependencies
    node_deps = []

    if not back_path:
        back_path = []

    # Push current node into return path list
    back_path.append(node)

    # Get current node dependencies list
    deps = getNodeDeps(node)

    for dep in deps:
        # If dependency persist in list of visited nodes - it's a recursion
        if dep in back_path:
            pos = back_path.index(dep)
            self.log.error("Recursive link detected. '" + node
                    + "', contains a recursive link to '" + dep
                    + "'. Removing dependency. Recursive loop is:\n\t"
                    + '\n\t'.join(back_path[pos:]))
            continue
        # Recursively call self for child nodes
        node_deps.extend(self.getNodeDepsTree(dep, back_path))

    # Finally add current node as dependency
    node_deps.append(node)

    # Remove current node from list of visited nodes and return
    back_path.pop()

    return node_deps

Well after reading about directed acyclic graphs (DAG) I came up with following solution:

def getNodeDepsTree(self, node, back_path=None):
    """Return whole dependency tree for given node"""
    # List of current node dependencies
    node_deps = []

    if not back_path:
        back_path = []

    # Push current node into return path list
    back_path.append(node)

    # Get current node dependencies list
    deps = getNodeDeps(node)

    for dep in deps:
        # If dependency persist in list of visited nodes - it's a recursion
        if dep in back_path:
            pos = back_path.index(dep)
            self.log.error("Recursive link detected. '" + node
                    + "', contains a recursive link to '" + dep
                    + "'. Removing dependency. Recursive loop is:\n\t"
                    + '\n\t'.join(back_path[pos:]))
            continue
        # Recursively call self for child nodes
        node_deps.extend(self.getNodeDepsTree(dep, back_path))

    # Finally add current node as dependency
    node_deps.append(node)

    # Remove current node from list of visited nodes and return
    back_path.pop()

    return node_deps
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文