简体   繁体   English

Java 中是否有有向无环图 (DAG) 数据类型,我应该使用它吗?

[英]Is there a Directed Acyclic Graph (DAG) data type in Java, and should I use it?

I am modeling a power subsystem in Java.我正在为 Java 中的电源子系统建模。 A simple SQLite database contains a set of Line Replaceable Units (LRUs) and the connections between them.一个简单的 SQLite 数据库包含一组线路可更换单元 (LRU) 和它们之间的连接。 I am writing a Power Model API to simplify queries of the data store, using DDD patterns and repositories.我正在编写 Power Model API 来简化对数据存储的查询,使用 DDD 模式和存储库。

I am seeking an appropriate Java collection to model the query results.我正在寻找一个合适的 Java 集合到 model 查询结果。 There are some special cases in a LRU connection stream that have to be modeled:在 LRU 连接 stream 中存在一些必须建模的特殊情况:

  1. Initially, there's a Power Distribution Unit (PDU) with multiple ports (<=16) that feeds power to downstream LRUs.最初,有一个带有多个端口 (<=16) 的配电单元 (PDU),可为下游 LRU 供电。
  2. Typical connections in a power stream involve a single source LRU where power originates and a single Sink LRU where power is drained.电源 stream 中的典型连接包括一个单源 LRU (其中电源产生)和一个单漏极 LRU (其中电源耗尽)。
  3. However, downstream there could be a single source LRU connected to multiple sink LRUs.但是,下游可能有一个源 LRU 连接到多个接收器 LRU。
  4. There are no cycles in a power stream.电源 stream 中没有周期。

The inclusion of #3 above has led me to think about returning query results from the API as a tree.上面包含的#3 让我想到了将 API 的查询结果作为树返回。 But the only tree I've found in java.util is a TreeMap key-value paired red-black tree, which doesn't seem appropriate (or I can't think of an appropriate abstraction for modeling power streams with it.) I've also been considering a LinkedHashSet , but I'm not convinced it is appropriate either.但是我在 java.util 中找到的唯一一棵树是TreeMap键值配对红黑树,这似乎不合适(或者我想不出用它来建模电力流的适当抽象。)我' 也一直在考虑LinkedHashSet ,但我也不相信它是合适的。 It's not clear to me how a node in this structure would point to downstream nodes.我不清楚这个结构中的节点如何指向下游节点。

I'm not concerned about efficiency in time or space at this point.在这一点上,我不关心时间或空间的效率。 My API just has to work by supplying power connection information to external clients (ie, the Presentation Tier of a Java-based Power Monitoring & Control app.) There are also no restrictions on the use of open source data types/libraries.我的 API 只需向外部客户端(即基于 Java 的电力监控应用程序的表示层)提供电源连接信息即可工作。对开源数据类型/库的使用也没有限制。

In general computer science parlance, what I'm really seeking is a Directed-Acyclic-Graph (DAG).用一般的计算机科学术语来说,我真正想要的是有向无环图(DAG)。

Is there an implementation of that for Java?是否有针对 Java 的实现? Am I correct that a DAG is appropriate for my scenario?我是否正确认为 DAG 适合我的场景?

I don't know if it can help but take a look at JGraphT .我不知道它是否有帮助,但看看JGraphT

As you can see if regarding the "related" questions, similar questions were already asked.正如您所看到的,关于“相关”问题,是否已经提出了类似的问题。 And no, Java does not have a general-purpose graph/tree/DAG data type (if we don't count Swing's TreeModel).不, Java 没有通用的图/树/DAG 数据类型(如果我们不计算 Swing 的 TreeModel)。

Make your own one.做你自己的。


Here is an example (readonly) interface:这是一个示例(只读)接口:

interface Node<N extends Node<N,E>, E extends Edge<N,E>> {
    public Set<E> outgoingEdges();
    public Set<E> ingoingEdges();
}

interface Edge<N extends Node<N,E>, E extends Edge<N,E>> {
    public E source();
    public E sink();
}

You would then have然后你会有

interface LRU implements Node<LRU, Line> { ... }
interface Line implements Edge<LRU, Line> { ... }

(or classes instead). (或代替类)。

For this particular problem.对于这个特殊的问题。 I've decided to use a LinkedListMultimap from Guava .我决定使用来自 Guava 的 LinkedListMultimap

FWIW if someone wants a standard-libraries only solution, A map of sets or a map of other collections might do the job too, although not as well. FWIW,如果有人想要一个仅标准库的解决方案,一组 map 或其他 collections 的 map 也可以完成这项工作,尽管效果不佳。

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

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