Commit 7287af55 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

graphml: added support for graph properties


git-svn-id: https://svn.forked.de/graph-tool/trunk@55 d4600afd-f417-0410-95de-beed9576f240
parent 3243afae
......@@ -209,6 +209,7 @@ private:
self->m_canonical_edges = (value == "canonical");
}
}
self->m_active_descriptor = "";
}
else if (name == "data")
{
......@@ -358,7 +359,9 @@ private:
void handle_property(std::string key_id, std::string descriptor,
bool is_vertex, std::string value)
{
if (is_vertex)
if (descriptor == "")
m_g.set_graph_property(m_key_name[key_id], value, m_key_type[key_id]);
else if (is_vertex)
m_g.set_vertex_property(m_key_name[key_id], get_vertex_descriptor(descriptor), value, m_key_type[key_id]);
else
m_g.set_edge_property(m_key_name[key_id], get_edge_descriptor(descriptor), value, m_key_type[key_id]);
......
......@@ -86,6 +86,9 @@ public:
virtual boost::any do_add_vertex() = 0;
virtual std::pair<boost::any,bool> do_add_edge(boost::any source, boost::any target) = 0;
virtual void
set_graph_property(const std::string& name, const std::string& value, const std::string& value_type) = 0;
virtual void
set_vertex_property(const std::string& name, boost::any vertex, const std::string& value, const std::string& value_type) = 0;
......@@ -106,7 +109,7 @@ class mutate_graph_impl : public mutate_graph
bool is_directed() const
{
return is_convertible<typename graph_traits<MutableGraph>::directed_category,
directed_tag>::value;
directed_tag>::value;
}
virtual any do_add_vertex()
......@@ -120,6 +123,26 @@ class mutate_graph_impl : public mutate_graph
any_cast<vertex_descriptor>(target), m_g);
return std::make_pair(any(retval.first), retval.second);
}
virtual void
set_graph_property(const std::string& name, const std::string& value, const std::string& value_type)
{
bool type_found = false;
try
{
mpl::for_each<value_types>(put_property<MutableGraph,value_types>
(name, m_dp, m_g, value, value_type, m_type_names, type_found));
}
catch (bad_lexical_cast)
{
throw parse_error("invalid value \"" + value + "\" for key " +
name + " of type " + value_type);
}
if (!type_found)
throw parse_error("unrecognized type \"" + value_type +
"\" for key " + name);
}
virtual void
set_vertex_property(const std::string& name, any vertex, const std::string& value, const std::string& value_type)
......@@ -249,6 +272,7 @@ write_graphml(std::ostream& out, const Graph& g, VertexIndexMap vertex_index,
typedef mpl::vector<bool, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, float, double, long double, std::string> value_types;
char* type_names[] = {"boolean", "int", "int", "int", "int", "long", "long", "long", "long", "float", "double", "double", "string"};
std::map<std::string, std::string> graph_key_ids;
std::map<std::string, std::string> vertex_key_ids;
std::map<std::string, std::string> edge_key_ids;
int key_count = 0;
......@@ -257,14 +281,18 @@ write_graphml(std::ostream& out, const Graph& g, VertexIndexMap vertex_index,
for (dynamic_properties::const_iterator i = dp.begin(); i != dp.end(); ++i)
{
std::string key_id = "key" + lexical_cast<std::string>(key_count++);
if (i->second->key() == typeid(vertex_descriptor))
if (i->second->key() == typeid(Graph))
vertex_key_ids[i->first] = key_id;
else
else if (i->second->key() == typeid(vertex_descriptor))
vertex_key_ids[i->first] = key_id;
else if (i->second->key() == typeid(edge_descriptor))
edge_key_ids[i->first] = key_id;
else
continue;
std::string type_name = "string";
mpl::for_each<value_types>(get_type_name<value_types>(i->second->value(), type_names, type_name));
out << " <key id=\"" << key_id << "\" for=\""
<< (i->second->key() == typeid(vertex_descriptor) ? "node" : "edge") << "\""
<< (i->second->key() == typeid(Graph) ? "graph" : (i->second->key() == typeid(vertex_descriptor) ? "node" : "edge")) << "\""
<< " attr.name=\"" << i->first << "\""
<< " attr.type=\"" << type_name << "\""
<< " />\n";
......@@ -275,6 +303,16 @@ write_graphml(std::ostream& out, const Graph& g, VertexIndexMap vertex_index,
<< " parse.nodeids=\"" << (ordered_vertices ? "canonical" : "free") << "\""
<< " parse.edgeids=\"canonical\" parse.order=\"nodesfirst\">\n";
// Output graph data
for (dynamic_properties::const_iterator i = dp.begin(); i != dp.end(); ++i)
{
if (i->second->key() == typeid(Graph))
{
out << " <data key=\"" << graph_key_ids[i->first] << "\">"
<< i->second->get_string(g) << "</data>\n";
}
}
typedef typename graph_traits<Graph>::vertex_iterator vertex_iterator;
vertex_iterator v, v_end;
for (tie(v, v_end) = vertices(g); v != v_end; ++v)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment