[Mono-list] Serializing a Graph ..
A Rafael D Teixeira
rafael.teixeirabr@terra.com.br
Tue, 22 Feb 2005 16:20:19 -0300
To have it work make nodes and arcs be kept on arraylists
and inside them use references as indexes (integers). Them serialize
those two arraylists, or a wrapper class containing both.
public class ArcsAndNodes {
public Arraylist Nodes;
public Arraylist Arcs;
private void Init() {
if (Nodes == null) Nodes = new Arraylist();
if (Arcs == null) Arcs = new Arraylist();
}
private static ArcsAndNodes singleton;
public static ArcsAndNodes AN {
get {
if (singleton == null)
singleton = new ArcsAndNodes();
singleton.Init();
return singleton;
}
set {
singleton = value;
}
}
public class Arc {
public int Weight;
public int StartNode;
public int EndNode;
public Arc(){}
[XmlIgnore]
public Node StartAt { get { return (Node)AN.Nodes[StartNode]; } }
[XmlIgnore]
public Node EndAt { get { return (Node)AN.Nodes[EndNode]; } }
public Arc(int n1, int n2) {
StartNode = n1;
EndNode = n2;
StartAt.OutGoingArcs.Add(AN.Arcs.Count);
AN.Arcs.Add(this);
}
}
and so on...
That is the kind of mapping the xmlserializer would have to 'magically'
discover to be able to serialize your modeled classes...
On Mon, 2005-02-21 at 00:56, David Waite wrote:
> the normal serialization is a bag of unique objects with references
> between them; formatting with Xml implies a parent/child relationship.
> The xml serializer as it exists does not make allowances to directly
> create or use ID/IDREF relationships, which is what you really need
> for your cyclic data structure.
>
> -David Waite
>
> On Mon, 21 Feb 2005 00:05:00 +0000, Phillip Neumann <bob@sofsis.cl> wrote:
> >
> >
> > Ive just made the same with BinaryFormatetr, instead of XmlSerialize
> >
> > Why does it work? Why does XmlSerilize not work?
> >
> >
> > Phillip Neumann wrote:
> >
> > > Hello all...
> > >
> > > Im doing some work with Graph, so create some clases:
> > >
> > > Node
> > > Arc
> > > Graph.
> > >
> > > (A Graph is a list of Nodes)
> > >
> > > I like the idea about been able to save/load a graph to/from disk.
> > > Im trying to XmlSerialize my graph.
> > >
> > > I have define my graph so, that this cannot be made, becouse:
> > > 1.- Node = { X, Y, ListOfOutgoingArcs }
> > > 2.- Arc = { StartNode, StopNode, Weight}
> > >
> > > This definition is circular....
> > >
> > > What do u think is the best to modify, for let the graph been saved to
> > > disk?
> > >
> > >
> > > When i run this, i get this:
> > > <mono1.0.5>
> > > System.InvalidOperationException: A cirtular reference was detected
> > > while serializing an object of type Node
> > > </mono>
> > >
> > >
> > > thanks in advance,
> > >
> > >
> > >------------------------------------------------------------------------
> > >
> > >using System;
> > >using System.Xml;
> > >using System.Xml.Serialization;
> > >using System.Collections;
> > >
> > >[Serializable, XmlInclude(typeof(Arc))]
> > >public class Node{
> > >
> > > public ArrayList OutGoingArcs = new ArrayList();
> > > public int X;
> > > public int Y;
> > > public Node(){}
> > > public Node(int x,int y){
> > > X=x;
> > > Y=y;
> > >
> > > }
> > >
> > >}
> > >
> > >public class Arc{
> > > public int Weight;
> > > public Node StartAt;
> > > public Node EndAt;
> > > public Arc(){}
> > > public Arc(Node n1, Node n2){
> > > StartAt=n1;
> > > EndAt = n2;
> > > n1.OutGoingArcs.Add(this);
> > > }
> > >
> > >}
> > >
> > >[Serializable, XmlInclude(typeof(Node))]
> > >public class Graf{
> > > public ArrayList Nodes;
> > >
> > > public Graf (){
> > > Nodes = new ArrayList();
> > > }
> > >
> > > public void Add(Node n){
> > > Nodes.Add(n);
> > > }
> > >}
> > >
> > >public class M{
> > >
> > > public static void Main(){
> > >
> > > Graf g = new Graf();
> > >
> > > Node n1 = new Node(1,1);
> > > Node n2 = new Node(2,2);
> > >
> > > Arc a1=new Arc(n1,n2);
> > >
> > >
> > > g.Add(n1);
> > > g.Add(n2);
> > >
> > > XmlSerializer seria = new XmlSerializer(typeof(Graf));
> > > seria.Serialize(Console.Out,g);
> > >
> > >
> > > }
> > >
> > >
> > >}
> > >
> > >
> >
> > --
> >
> > _________________________
> > Phillip Neumann
> > phillip@sofsis.cl
> > www.sofsis.cl
> >
> >
> >
> _______________________________________________
> Mono-list maillist - Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
> E-mail classificado pelo Identificador de Spam Inteligente Terra.
> Para alterar a categoria classificada, visite
> http://www.terra.com.br/centralunificada/emailprotegido/imail/imail.cgi?+_u=rafael.teixeirabr&_l=1,1108958430.71341.7577.mongu.terra.com.br,6403,Des15,Des15
>
> Esta mensagem foi verificada pelo E-mail Protegido Terra.
> Scan engine: McAfee VirusScan / Atualizado em 18/02/2005 / Verso: 4.4.00 - Dat 4430
> Proteja o seu e-mail Terra: http://www.emailprotegido.terra.com.br/
--
Rafael "Monoman" Teixeira
Mono Hacker since 16 Jul 2001 - http://www.go-mono.org/
Mono Brasil Founding Member - http://monobrasil.redesolbrasil.org/
English Blog: http://monoblog.blogspot.com/
Brazilian Portuguese Blog: http://monoblog.weblogger.terra.com.br/