【问题标题】:Configuration inheritance mechanism配置继承机制
【发布时间】:2011-03-09 13:39:34
【问题描述】:

我有以下结构:

配置 |-- 团体 |-- 根组 |-- group1(包括根组) |-- 组2(包括组1) |-- group3(包括根组) |-- 用户 |-- Fred(包括 group3 和 group2)

所以 Fred 的继承树如下所示:

_弗雷德_ v v 组 2 组 3 v v 组 1 v v / 根组

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

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

谢谢。

【问题讨论】:

  • 你现在如何存储关系?它现在是具有 obj-per-line 和制表深度的文本,表示“子”关系,还是您有一些基于类/对象的关系存储?你现在呈现源结构的方式(|-- group1(包括rootgroup))不利于解析。
  • 对不起,目前是文件结构。每个组都是一个目录,其中包含一个文件,其中每行列出一个当前组依赖项。

标签: python inheritance recursion


【解决方案1】:

在阅读了有向无环图 (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

【讨论】:

    猜你喜欢
    • 2021-11-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 2018-11-04
    • 2011-12-20
    相关资源
    最近更新 更多