1
// Copyright (C) 2007 Magnus Vikstrom
2
// Licensed under the GNU LGPL Version 2.1.
4
// First added: 2007-02-12
5
// Last changed: 2007-03-21
7
#include <dolfin/dolfin_log.h>
8
#include <dolfin/XMLGraph.h>
10
using namespace dolfin;
12
//-----------------------------------------------------------------------------
13
XMLGraph::XMLGraph(Graph& graph) : XMLObject(), _graph(graph), state(OUTSIDE)
17
//-----------------------------------------------------------------------------
22
//-----------------------------------------------------------------------------
23
void XMLGraph::startElement(const xmlChar *name, const xmlChar **attrs)
29
if ( xmlStrcasecmp(name, (xmlChar *) "graph") == 0 )
31
readGraph(name, attrs);
39
if ( xmlStrcasecmp(name, (xmlChar *) "vertices") == 0 )
41
readVertices(name, attrs);
42
state = INSIDE_VERTICES;
44
else if ( xmlStrcasecmp(name, (xmlChar *) "edges") == 0 )
46
readEdges(name, attrs);
54
if ( xmlStrcasecmp(name, (xmlChar *) "vertex") == 0 )
55
readVertex(name, attrs);
61
if ( xmlStrcasecmp(name, (xmlChar *) "edge") == 0 )
62
readEdge(name, attrs);
70
//-----------------------------------------------------------------------------
71
void XMLGraph::endElement(const xmlChar *name)
76
if ( xmlStrcasecmp(name, (xmlChar *) "graph") == 0 )
86
if ( xmlStrcasecmp(name, (xmlChar *) "vertices") == 0)
95
if ( xmlStrcasecmp(name, (xmlChar *) "edges") == 0)
106
//-----------------------------------------------------------------------------
107
void XMLGraph::open(std::string filename)
111
//-----------------------------------------------------------------------------
112
bool XMLGraph::close()
114
return state == DONE;
116
//-----------------------------------------------------------------------------
117
void XMLGraph::readGraph(const xmlChar *name, const xmlChar **attrs)
120
std::string type = parseString(name, attrs, "type");
122
// Open graph for editing
123
editor.open(_graph, type);
125
//-----------------------------------------------------------------------------
126
void XMLGraph::readVertices(const xmlChar *name, const xmlChar **attrs)
128
dolfin_debug("readVertices()");
129
uint num_vertices = parseUnsignedInt(name, attrs, "size");
130
editor.initVertices(num_vertices);
132
//-----------------------------------------------------------------------------
133
void XMLGraph::readEdges(const xmlChar *name, const xmlChar **attrs)
135
dolfin_debug("readEdges()");
136
uint num_edges = parseUnsignedInt(name, attrs, "size");
137
editor.initEdges(num_edges);
139
//-----------------------------------------------------------------------------
140
void XMLGraph::readVertex(const xmlChar *name, const xmlChar **attrs)
143
currentVertex = parseUnsignedInt(name, attrs, "index");
145
// Read number of incident edges
146
uint num_edges = parseUnsignedInt(name, attrs, "num_edges");
148
// Vertex weights not yet implemented
149
//uint w = parseUnsignedInt(name, attrs, "weight");
151
editor.addVertex(currentVertex, num_edges);
153
//-----------------------------------------------------------------------------
154
void XMLGraph::readEdge(const xmlChar *name, const xmlChar **attrs)
157
uint v1 = parseUnsignedInt(name, attrs, "v1");
158
uint v2 = parseUnsignedInt(name, attrs, "v2");
160
//dolfin_debug2("readEdge, v1 = %d, v2 = %d", v1, v2);
162
// Edge weights not yet implemented
163
//uint w = parseUnsignedInt(name, attrs, "weight");
164
editor.addEdge(v1, v2);
166
//-----------------------------------------------------------------------------
167
void XMLGraph::closeGraph()
171
//-----------------------------------------------------------------------------