如何将自定义双向图从 QuickGraph 应用于 Graph# 的 GraphLayout?

发布于 2024-08-24 09:18:53 字数 1150 浏览 3 评论 0 原文

怎么了?

    using QuickGraph;
    using GraphSharp;

     public class State
        {
            public string Name { get; set; }
            public override string ToString()
            {
                return Name;
            }
        }

     public class Event
        {
            public string Name;
            public override string ToString()
            {
                return Name;
            }
        }

    BidirectionalGraph<State, TaggedEdge<State, Event>> x =
                    new BidirectionalGraph<State, TaggedEdge<State, Event>>();

    GraphLayout graphLayout = new GraphLayout();
    graphLayout.Graph = x;

错误:

无法将类型 QuickGraph.Bi DirectionGraph> 隐式转换为 QuickGraph.IBi DirectionGraph ;>。存在显式转换(您是否缺少强制转换?)

如果我进行强制转换,则应用程序在启动时会出现错误错误,而没有任何信息

出了什么问题?

Whats wrong?

    using QuickGraph;
    using GraphSharp;

     public class State
        {
            public string Name { get; set; }
            public override string ToString()
            {
                return Name;
            }
        }

     public class Event
        {
            public string Name;
            public override string ToString()
            {
                return Name;
            }
        }

    BidirectionalGraph<State, TaggedEdge<State, Event>> x =
                    new BidirectionalGraph<State, TaggedEdge<State, Event>>();

    GraphLayout graphLayout = new GraphLayout();
    graphLayout.Graph = x;

Error:

Cannot implicitly convert type QuickGraph.BidirectionalGraph<ChashaGraphSharp.State,QuickGraph.TaggedEdge<ChashaGraphSharp.State,ChashaGraphSharp.Event>> to QuickGraph.IBidirectionalGraph<object,QuickGraph.IEdge<object>>. An explicit conversion exists (are you missing a cast?)

If I put the cast, then application gets fault error on start without any information

What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佞臣 2024-08-31 09:18:53

您需要使用 IEdge 类型而不是 TaggedEdge 创建 BidirectionGraph 实例:

BidirectionalGraph<State, IEdge<State, Event>> x =
                new BidirectionalGraph<State, IEdge<State, Event>>();

我不能说我完全理解为什么会出现这种情况,但是上面的方法应该可行。

编辑:问了一个问题解释了为什么这个演员阵容不起作用。

You need to create your instance of BidirectionGraph using the type IEdge instead of TaggedEdge:

BidirectionalGraph<State, IEdge<State, Event>> x =
                new BidirectionalGraph<State, IEdge<State, Event>>();

I can't say that I fully understand why this is the case, however the above should work.

EDIT: I asked a question that kind of explains why this cast doesn't work.

給妳壹絲溫柔 2024-08-31 09:18:53

如果您使用自定义图表(IE 不是“Bi DirectionGraph”)
您需要使用继承自“ContextualGraphLayout”的自定义 GraphLayout

此处,不要使用“GraphLayout”,而是使用“ContextualGraphLayout” >,双向图<状态,边缘<状态>>>”。

我强烈建议创建虚拟模型类以获得可读性。
例如:

public MyVertex : 状态 { }
公共 MyEdge : Edge {
   公共 MyEdge(MyVertex 源、MyVertex 目标)
      :基础(源,目标){}

}

public MyGraph : Bi DirectionGraph; { }
公共MyGraphLayout:ContextualGraphLayout {
    公共 MyGraphLayout () : base() { }

    公共MyGraphLayout(布尔allowParallelEdges)
        : 基(allowParallelEdges) { }

    公共MyGraphLayout(布尔allowParallelEdges,int vertexCapacity)
        :基础(allowParallelEdges,vertexCapacity){}

}

If you use a custom graph (IE not a "BidirectionalGraph<Object, IEdge<Object>")
You need to use a custom GraphLayout that inherits from "ContextualGraphLayout"

Here, instead of using "GraphLayout" use "ContextualGraphLayout<State,Edge<<State>>,BidirectionalGraph<State, Edge<State>>>".

I strongly advise to create dummy model classes to gain readability.
For instance:

public MyVertex : State { }
public MyEdge : Edge<MyVertex> {
   public MyEdge (MyVertex source, MyVertex target)
      : base(source, target) { }

}

public MyGraph : BidirectionalGraph<MyVertex, MyEdge> { }
public MyGraphLayout : ContextualGraphLayout<MyVertex, MyEdge, MyGraph> {
    public MyGraphLayout () : base() { }

    public MyGraphLayout (bool allowParallelEdges)
        : base(allowParallelEdges) { }

    public MyGraphLayout (bool allowParallelEdges, int vertexCapacity)
        : base(allowParallelEdges, vertexCapacity) { }

}

滴情不沾 2024-08-31 09:18:53

是的

,但 TaggedEdge 不实现 IEdge 接口
如何使用自定义TaggedEdge?

Yes

But TaggedEdge does not implement IEdge interface
How to use custom TaggedEdge?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文