简体   繁体   English

Java中的有向图处理

[英]Directed Graph Processing in Java

I am looking to implement a Java application that will compute a set of tasks to execute.我希望实现一个 Java 应用程序,它将计算一组要执行的任务。 The tasks will have dependencies on each other, forming a directed graph.这些任务将相互依赖,形成一个有向图。 Is there an existing SDK or algorithm (preferably in Java) out there that will help me:是否有现有的 SDK 或算法(最好是 Java)可以帮助我:

  1. Define the graph of tasks定义任务图
  2. Ensure there are no cyclic dependencies in the graph确保图中没有循环依赖
  3. Execute the tasks in the graph using a thread pool使用线程池执行图中的任务

Step 3 is the most important part.第三步是最重要的部分。 I need to execute the tasks in a parallel fashion for maximum performance yet ensure that a task isn't executed before its dependencies.我需要以并行方式执行任务以获得最佳性能,同时确保任务在其依赖项之前不被执行。

Have a look at this previous question , which essentially suggests using JGraphT .看看这个先前的问题,它基本上建议使用JGraphT

It will obviously make 1) easy and has cycle detectors for part 3).它显然会使 1) 变得容易,并为第 3 部分提供循环检测器。 Don't think it will do part 3 for you but all you need to do is get all vertexes with an out degree (or in degree depending on your representation) of 0 and start those tasks.不要认为它会为您完成第 3 部分,但您需要做的就是获取所有出度(或度数取决于您的表示)为 0 的顶点并开始这些任务。 When a task finishes then delete the vertex from the graph and start again.当任务完成时,从图中删除顶点并重新开始。

Dexecutor to the rescue, Dexecutor is designed to execute dependent independent tasks in a reliable way. Dexecutor的救援,Dexecutor 旨在以可靠的方式执行依赖的独立任务。

DexecutorConfig<Integer, Integer> config = new DexecutorConfig<>(executorService, new SleepyTaskProvider());
DefaultDexecutor<Integer, Integer> executor = new DefaultDexecutor<Integer, Integer>(config);
// Graph building
executor.addDependency(1, 2);
executor.addDependency(1, 2);
executor.addDependency(1, 3);
executor.addDependency(3, 4);
executor.addDependency(3, 5);
executor.addDependency(3, 6);
executor.addDependency(2, 7);
executor.addDependency(2, 9);
executor.addDependency(2, 8);
executor.addDependency(9, 10);
executor.addDependency(12, 13);
executor.addDependency(13, 4);
executor.addDependency(13, 14);
executor.addIndependent(11);
//Execution
executor.execute(ExecutionConfig.NON_TERMINATING);

Refer How Do I?请参阅 我该怎么做? for more detail了解更多详情

Why Dexecutor为什么选择执行者

  • Ultra light weight超轻量
  • Ultra Fast超快
  • Immediate/Scheduled Retry Logic supported支持立即/计划重试逻辑
  • Non terminating behaviour supported支持非终止行为
  • Conditionally skip the task execution有条件地跳过任务执行
  • Good test coverage to keep you safe from harm良好的测试覆盖率,让您免受伤害
  • Available in maven central可用于 maven 中央
  • Good amount of documentation大量的文档
  • Distribute Execution Supported (Ignite, Hazelcast, Infinispan)支持分布式执行(Ignite、Hazelcast、Infinispan)

Usefull links有用的链接

Disclaimer: I am the owner免责声明:我是所有者

I have created a Java library that does exactly what you are requesting.我创建了一个 Java 库,它完全符合您的要求。 You can construct a directed graph consisting of Runnable objects and their dependencies.您可以构建由 Runnable 对象及其依赖项组成的有向图。 You then pass this graph to an executor that runs the objects as their dependencies are fulfilled, in an executor.然后,您将此图传递给执行程序,该执行程序在执行程序中运行对象,因为它们的依赖关系得到满足。

Download the library's jar file here: https://github.com/idooley/DAGExecutor/downloads在此处下载库的 jar 文件: https://github.com/idooley/DAGExecutor/downloads

Or clone the whole repository and compile it yourself: https://github.com/idooley/DAGExecutor或者克隆整个存储库并自己编译: https://github.com/idooley/DAGExecutor

Hopefully this will be of use to others.希望这对其他人有用。 Feel free to contribute any patches, new unit tests, or other changes to make it work the way you want for your projects.随意贡献任何补丁、新的单元测试或其他更改,以使其按照您的项目所需的方式工作。

I've use JUNG for creating dependency-graphs among files.我使用JUNG在文件之间创建依赖关系图。 There are a variety of classes for computing distances, clustering, etc.有多种类用于计算距离、聚类等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM