用pyDatalog解析依赖关系图

2024-06-28 20:25:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力编写更具可读性、声明性的程序。所以我决定实现一个我们目前使用的简单算法。程序实现如下:

  1. 有命令和资源
  2. 每个命令可以提供并需要多个资源
  3. 该算法将循环遍历所有命令,并调度为其提供所有需求的命令。在
  4. 命令提供的所有资源现在都已提供
  5. 如果所有命令都安排好了,我们就完成了
  6. 如果还有命令,我们就不能满足依赖关系,也不能为算法的迭代安排新的命令

因此,我提出的数据日志变体看起来不错,但有两个问题:

  1. 这是错误的
  2. 我需要一个循环来读取结果

您可以找到完整的源文件here。在

它取决于假设,您可以使用pytest轻松运行它。在

下面的测试失败:如果我们需要由以前的“等级”或顺序提供的资源。它找不到它。我试图使follows递归,但是即使在简单的例子中也失败了。在

def test_graph_multirequire():
    """Test if the resolver can handle a graph with multiple requires"""
    tree = [
        [('A'), ('B'), ('C'), ('D'), ('E'), ('F'), ('G'), ()],
        [(), ('A'), ('B'), ('C', 'A'), ('D'), ('E'), ('F'), ('G')]
    ]
    run_graph(tree)


def run_graph(tree):
    """Run an example"""
    try:
        tree_len = len(tree[0])
        index = list(range(tree_len))
        random.shuffle(index)
        for i in index:
            + is_command(i)
            for provide in tree[0][i]:
                + provides(i, provide)
            for require in tree[1][i]:
                + requires(i, require)
        ##############################
        is_root(X) <= is_command(X) & ~requires(X, Y)
        follows(X, Z) <= (
            provides(X, Y) & requires(Z, Y) & (X != Z)
        )
        order(0, X) <= is_root(X)
        order(N, X) <= (N > 0) & order(N - 1, Y) & follows(Y, X)
        ##############################
        ordered = []
        try:
            for x in range(tree_len):
                nodes = order(x, N)
                if not nodes:
                    break
                ordered.extend([x[0] for x in nodes])
        except AttributeError:
            ordered = index
        assert len(ordered) >= tree_len
        print(ordered)
        provided = set()
        for command in ordered:
            assert set(tree[1][command]).issubset(provided)
            provided.update(tree[0][command])
    finally:
        pd.clear()

我的问题:

  1. 我用错工具了吗?在
  2. 有人知道全面的数据日志怎么做吗?在
  3. 你实际是如何解决上述问题的?在

编辑:

  1. 我缺少像all()和exists()这样的量词,如何在pyDatalog中表达这一点?在

Tags: in命令算法treeforindexlenis
1条回答
网友
1楼 · 发布于 2024-06-28 20:25:18

pyDatalog(和prolog)很好地适应了这种问题。挑战是要脱离传统的程序化编程思维。您可能想在网上搜索prolog课程:许多原则也适用于pyDatalog。在

用声明性语言编写循环包括3个步骤:

1)定义一个谓词,其中包含在循环过程中更改的所有变量。在

在这种情况下,您需要跟踪部分计划、已经生成的内容和剩余的计划:

partial_plan(Planned, Produced, Todo)

例如,partial_plan([0,],['A',],[1,2,3,4,5,6,7])为true。要查找计划,您需要查询:

^{pr2}$

2)描述基本(=最简单)情况。在这里,起点是所有的命令仍然需要计划。在

+ partial_plan([], [], [0,1,2,3,4,5,6,7])

3)描述迭代规则。在这里,您需要选择一个要执行的命令,以及其需求已经生成,并将其添加到Planned:

partial_plan(Planned1, Produced1, Todo1) <= (
    # find a smaller plan first
    (Planned0 == Planned1[:-1]) &
    partial_plan(Planned0, Produced0, Todo0) &
    # pick a command to be done, reducing the list of todos, 
    pick(Command, Todo0, Todo1) &
    # verify that it can be done with what has been produced already
    subset(requirement[Command], Produced0) &
    # add it to the plan 
    (Planned1 == Planned0 + [Command, ]) &
    (Produced1 == Produced0 + result[Command])
    )

现在我们必须定义第3步中介绍的谓词。在

# pick
pick(Command, Todo0, Todo1) <= (
    (X._in(enumerate(Todo0))) & # X has the form [index, value]
    (Command == X[1]) &
    (Todo1 == Todo0[:X[0]] + Todo0[X[0]+1:]) # remove the command from Todo0
    )

# result and requirement are defined as logic functions, 
# so that they can be used in expressions
for i in range(len(tree[0])):
    result[i] = tree[0][i]
    requirement[i] = tree[1][i]

subset检查L1的所有元素是否在L2中(注意all量词)。它也被定义为一个循环:

subset([], List2) <= (List2 == List2) # base case
subset(L1, L2) <= (
    (0 < len(L1)) &
    (L1[0]._in(L2)) & # first element in L2
    subset(L1[1:], L2) # remainder is a subset of L2
    )

然后必须声明上面所有的pyDatalog变量和谓词,以及“enumerate”、“result”和“requirement”函数。在

注意:这还没有经过测试;可能需要一些小的更改。在

相关问题 更多 >