计算插件的依赖插件

由网友(笑弄清风)分享简介:我有必要创建一个插件系统,将有依赖支持,我不知道要解释的依赖性的最佳途径。该插件将全部从基类的子类,每个都有自己的执行()方法。在每一个插件类,我计划创建一个依赖属性,因为所有其他插件的列表,这取决于当加载的插件,我会导入所有的人,并把他们在一个列表并且基于相关性排序。一旦他们都以正确的顺序(因此与依赖任何事情都是在说...

我有必要创建一个插件系统,将有依赖支持,我不知道要解释的依赖性的最佳途径。该插件将全部从基类的子类,每个都有自己的执行()方法。在每一个插件类,我计划创建一个依赖属性,因为所有其他插件的列表,这取决于

当加载的插件,我会导入所有的人,并把他们在一个列表并且基于相关性排序。一旦他们都以正确的顺序(因此与依赖任何事情都是在说依赖后面的列表)我通过执行每个方法的列表执行()方法倒是循环

在哪里我一直得到模糊的分类背后的逻辑。我刚开始把它们按字母顺序排列,直到我遇到一个具有dependency-如果它的依赖是不在列表中呢,把它变成一个tmp目录列表。在第一轮的进口月底,开始在临时表(什么也没有,但有依赖插件的列表)的结束,并再次检查跑单。如果我发现它在'跑单'的依赖确保它具有比最高的依赖性更高的索引号。如果它的依赖不在列表中,暂缓并移动到临时列表中的下一个插件。一旦我得到的TMP列表的末尾,开始向后上方,然后再试一次。一旦要么所有的插件进行排序,或者遍历它 - 后tmp目录列表不会改变大小,开始执行插件。

你会留在临时表是要么没有发现他们的依赖关系,或者有一个循环依赖插件。 (2插件在tmp目录是互相依赖)

如果你还在读,你能够按照我的逻辑;这是一个合理的计划?有没有更简单的方法吗?如果我想实现的序列号执行令,是否有易的方式有两种顺序和依赖关系? (具有相关性排序跳动,如果有冲突),还是应该插件使用序列或依赖呢? (运行带有序列号的插件第一,比那些具有相关性?)

TL; DR

你会怎么写的逻辑来计算依赖于一个插件系统?

编辑:

Alright-我想我实现从维基百科页面的拓扑排序;继其为DFS逻辑的例子。它似乎工作,但我从来没有做过这样的事了。

http://en.wikipedia.org/wiki/Topological_sorting#Examples

 数据= {
    '10':['11','3'],
    '3':[],
    '5':[],
    '7':[],
    '11':['7','5'],
    '9':['8','11'],
    '2':['11'],
    '8':['7','3']
}

L = []
走访= []

高清nodeps(数据):
    S = []
    对于每个在data.keys():
        如果不是len个(数据[每]):
            S.append(每个)
    返回小号

高清dependson(节点):
    R = []
    对于每个在data.keys():
        n个数据[每]:
            如果n ==节点:
                r.append(每个)
    回报 -  [R

DEF访问(节点):
    如果不能在节点访问:
        visited.append(节点)
        为米在dependson(节点):
            访问(M)
        L.append(节点)

在nodeps(数据)节点:
    访问(节点)

打印L [::  -  1]
 
弹幕姬礼物统计插件 GiftStatistics.dll 1.0.1

解决方案

你所描述听起来像它会工作,但将有一个很难检测到循环依赖算法。

您正在寻找的是你依赖的拓扑排序。如果您创建依赖关系的图,其中从A到B的边表示A依赖于B,那么你可以做的深度优先搜索,以确定正确的顺序。你想做一个普通的DFS的变化,可以检测周期,所以你可以在这种情况下打印错误。

如果你使用一个有序列表中的每个顶点存储图中的连接,您可以添加使用它们的序列号的依赖。使用DFS的优点是所产生的排序列表应该尊重你的顺序排序,当它不与依赖顺序发生冲突。 (我相信,我需要测试这一点。)

I have the need to create a plugin system that will have dependency support and I'm not sure the best way to account for dependencies. The plugins will all be subclassed from a base class, each with its own execute() method. In each plugin class, I'd planned to create a dependencies attribute as a list of all the other plugins it depends on.

When loading the plugins, I would import all of them and put them in a list and sort them based on the dependencies. Once they are all in the correct order (so anything with a dependency is in the list after its said dependencies) I'd loop through the list executing each methods execute() method.

Where I'm keep getting fuzzy is the logic behind the sorting. I can just start putting them in alphabetical order until i come across one that has a dependency- if its dependencies is not in the list yet, put it into a tmp list. at the end of the first round of imports, start at the end of the temp list ( a list with nothing but plugins that have dependencies) and check the 'run list' again. if i find its dependencies in the 'run list' make sure it has a higher index number than its highest dependency. If its dependencies are not in the list, hold off on it and move to the next plugin in the temp list. Once i get to the end of the tmp list, start back at the top and try again. Once either all the plugins are sorted, or the tmp list doesn't change size after looping over it- start executing plugins.

What would be left in the temp list are plugins that either don't have they're dependencies found, or have a circular dependency. (2 plugins in the tmp list that depend on one another)

If you're still reading AND you were able to follow my logic; is this a sound plan? Is there a simpler way? If i wanted to implement sequence numbers for execute order, is there an 'easy' way to have both sequence and dependencies? (with dependencies beating sequencing if there was a conflict) Or should a plugin use a sequence OR dependencies? (Run the plugins with sequence numbers first, than the ones with dependencies?)

TL;DR

How would you write the logic for calculating dependencies in a plugin system?

Edit:

Alright- I think I implemented the Topological sort from the wikipedia page; following its logic example for the DFS. It seems to work, but i've never done anything like this before.

http://en.wikipedia.org/wiki/Topological_sorting#Examples

data = {
    '10' :  ['11', '3'],
    '3'  :  [],
    '5'  :  [],
    '7'  :  [],
    '11' :  ['7', '5'],
    '9'  :  ['8', '11'],
    '2'  :  ['11'],
    '8'  :  ['7', '3']
}

L = []
visited = []

def nodeps(data):
    S = []
    for each in data.keys():
        if not len(data[each]):
            S.append(each)
    return S

def dependson(node):
    r = []
    for each in data.keys():
        for n in data[each]:
            if n == node:
                r.append(each)
    return r

def visit(node):
    if not node in visited:
        visited.append(node)
        for m in dependson(node):
            visit(m)
        L.append(node)

for node in nodeps(data):
    visit(node)

print L[::-1]

解决方案

The algorithm you described sounds like it would work but would have a hard time detecting circular dependencies.

What you're looking for is a Topological sort of your dependencies. If you create a graph of your dependencies where an edge from A to B means A depends on B then you can do a Depth-first search to determine the correct order. You'll want to do a variation of a regular DFS that can detect cycles so you can print an error in that case.

If you use an ordered list on each vertex to store connections in the graph, you can add dependencies using their sequence numbers. An advantage of using a DFS is that the resulting sorted list should respect your sequence ordering when it isn't conflicting with dependency order. (I believe; I need to test this.)

阅读全文

相关推荐

最新文章