graph_wrapper.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
32 #include <mtt/graph_wrapper.h>
33 
34 Agnode_t*GVGraph::selectNode(string&name)
35 {
36  map<string, Agnode_t*>::iterator it = _nodes.find(name);
37  if(it!=_nodes.end())
38  return it->second;
39  else
40  return NULL;
41 }
42 
43 Agedge_t*GVGraph::selectEdge(string&source,string&target)
44 {
45  pair<string, string> key; //= std::make_pair<string,string>(source,target);
46  key.first = source;
47  key.second = target;
48 
49  if(_edges.find(key)!=_edges.end())
50  return _edges[key];
51 
52  return NULL;
53 }
54 
55 int GVGraph::setNodeAttribute(string name,string attribute,string value)
56 {
57  return agsafeset(selectNode(name),const_cast<char*>(attribute.c_str()),const_cast<char*>(value.c_str()),const_cast<char*>(""));
58 
59 // return _agset(selectNode(name),attribute,value);
60 }
61 
62 int GVGraph::setEdgeAttribute(string source,string target,string attribute,string value)
63 {
64  return agsafeset(selectEdge(source,target),const_cast<char*>(attribute.c_str()),const_cast<char*>(value.c_str()),const_cast<char*>(""));
65 
66 // return _agset(selectEdge(source,target),attribute,value);
67 }
68 
70 {
71  gvRender(_context,_graph,(char*)"xgtk",NULL);
72 }
73 
75 {
76  return _context;
77 }
78 
79 Agraph_t* GVGraph::_agopen(string name,int kind)
80 {
81  return agopen(const_cast<char*>(name.c_str()),kind);
82 }
83 
84 string GVGraph::_agget(void*object,string attr,string alt)
85 {
86  string str=agget(object, const_cast<char*>(attr.c_str()));
87 
88  if(str==string())
89  return alt;
90  else
91  return str;
92 }
93 
94 int GVGraph::_agset(void*object,string attr,string value)
95 {
96  return agsafeset(object, const_cast<char*>(attr.c_str()),const_cast<char*>(value.c_str()),const_cast<char*>(value.c_str()));
97 }
98 
99 Agsym_t* GVGraph::_agnodeattr(string name,string value)
100 {
101  return agnodeattr(_graph,const_cast<char*>(name.c_str()),const_cast<char*>(value.c_str()));
102 }
103 
104 Agsym_t* GVGraph::_agedgeattr(string name,string value)
105 {
106  return agedgeattr(_graph,const_cast<char*>(name.c_str()),const_cast<char*>(value.c_str()));
107 }
108 Agnode_t* GVGraph::_agnode(string name)
109 {
110  return agnode(_graph,const_cast<char*>(name.c_str()));
111 }
112 
113 int GVGraph::_gvLayout(GVC_t*gvc,graph_t*graph,string engine)
114 {
115  return gvLayout(gvc,graph,engine.c_str());
116 }
117 
118 GVGraph::GVGraph(string name,double node_size):
119  _context(gvContext()),
120  _graph(_agopen(name, AGDIGRAPHSTRICT)) // Strict directed graph, see libgraph doc
121 {
122  //Set graph attributes
123 // _agset(_graph, "overlap", "prism");
124 // _agset(_graph, "splines", "true");
125 // _agset(_graph, "pad", "0,2");
126 // _agset(_graph, "dpi", "96,0");
127  _agset(_graph, "nodesep", "0.1");
128 // _agset(_graph, "ranksep", "2.0");
129 // _agset(_graph, "landscape","true");
130 // _agset(_graph, "center","true");
131 // _agset(_graph, "aspect","1");
132 // _agset(_graph, "forcelabels","true");
133  _agset(_graph, "rankdir","LR");
134 // _agset(_graph, "style","filled");
135 
136 
137  //Set default attributes for the future nodes
138  _agnodeattr("shape", "box");
139  _agnodeattr("height", "0.02");
140 
141 // _agnodeattr(_graph, "label", "");
142 // _agnodeattr(_graph, "regular", "true");
143 
144  //Divide the wanted width by the DPI to get the value in points
145 // double dpi=lexical_cast<double>(_agget(_graph, "dpi", "96,0"));
146 // string nodePtsWidth= lexical_cast<string>(node_size/dpi);
147 
148  //GV uses , instead of . for the separator in floats
149 // replace(nodePtsWidth.begin(), nodePtsWidth.end(), '.', ',');
150 
151 // cout<<"nodePtsWidth:"<<nodePtsWidth<<endl;
152 // _agnodeattr(_graph, "width", nodePtsWidth);
153 }
154 
156 {
157  gvFreeLayout(_context, _graph);
158  agclose(_graph);
159  gvFreeContext(_context);
160 }
161 
162 void GVGraph::addNode(const string& name)
163 {
164  if(_nodes.find(name)!=_nodes.end())
165  removeNode(name);
166 
167  _nodes.insert(pair<string,Agnode_t*>(name, _agnode(name)));
168 }
169 
170 void GVGraph::addNodes(vector<string>& names)
171 {
172  for(uint i=0; i<names.size(); ++i)
173  addNode(names[i]);
174 }
175 
176 void GVGraph::removeNode(const string& name)
177 {
178  string nn=name;
179 
180  if(_nodes.find(name)!=_nodes.end())
181  {
182  agdelete(_graph, _nodes[name]);
183  _nodes.erase(name);
184 
185  map< pair<string,string>, Agedge_t*>::iterator it;
186 
187  for(it=_edges.begin();it!=_edges.end();it++)
188  {
189  if(it->first.first==nn || it->first.second==nn)
190  removeEdge(it->first.first,it->first.second);
191  }
192  }
193 }
194 
196 {
197  map<string, Agnode_t*>::iterator it;
198 
199  vector<string>names;
200 
201  for(it=_nodes.begin();it!=_nodes.end();it++)
202  names.push_back(it->first);
203 
204  for(uint i=0;i<names.size();i++)
205  removeNode(names[i]);
206 }
207 
208 void GVGraph::setRootNode(const string& name)
209 {
210  if(_nodes.find(name)!=_nodes.end())
211  _agset(_graph, "root", name);
212 }
213 
214 void GVGraph::addEdge(const string &source, const string &target)
215 {
216  if(_nodes.find(source)!=_nodes.end() && _nodes.find(target)!=_nodes.end())
217  {
218  pair<string,string> key(source,target);
219 
220  if(_edges.find(key)==_edges.end())
221  _edges.insert( pair< pair<string,string >, Agedge_t* >(key, agedge(_graph, _nodes[source], _nodes[target])));
222  }
223 }
224 
225 void GVGraph::removeEdge(const string &source, const string &target)
226 {
227  removeEdge(pair<string,string>(source, target));
228 }
229 
230 void GVGraph::removeEdge(const pair<string, string>& key)
231 {
232  if(_edges.find(key)!=_edges.end())
233  {
234  agdelete(_graph, _edges[key]);
235  _edges.erase(key);
236  }
237 }
238 
240 {
241  gvFreeLayout(_context, _graph);
242 }
243 
245 {
246  _gvLayout(_context, _graph, "dot");
247 }
GVGraph(string name, double node_size=50)
Construct a Graphviz graph object.
Agedge_t * selectEdge(string &source, string &target)
int _gvLayout(GVC_t *gvc, graph_t *graph, string engine)
Layout wrapper function.
Agraph_t * _graph
Agraph main object.
Agnode_t * selectNode(string &name)
Selects a node.
void addNodes(vector< string > &names)
Add several nodes.
void clearNodes()
Clear the whole graph.
GVC_t * _context
GV context main variable.
void addNode(const string &name)
Add a new node to the graph.
Agraph_t * _agopen(string name, int kind)
Open the graph.
void startRender()
Start render.
int setEdgeAttribute(string source, string target, string attribute, string value)
map< pair< string, string >, Agedge_t * > _edges
Edge map, used for edge tracking.
map< string, Agnode_t * > _nodes
Node map, used for node tracking.
void removeEdge(const string &source, const string &target)
Remove a edge.
string _agget(void *object, string attr, string alt=string())
Get graph attribute.
Agsym_t * _agedgeattr(string name, string value)
Set edge attributes.
GVGraph class declaration.
~GVGraph()
Graphviz graph destructor.
Agsym_t * _agnodeattr(string name, string value)
Set node attributes.
Agnode_t * _agnode(string name)
Low level function to add new nodes.
int setNodeAttribute(string name, string attribute, string value)
Sets node attributes.
void freeLayout()
Free the layout This function should be called before any modifications to the current graph...
void removeNode(const string &name)
Remove a node.
void addEdge(const string &source, const string &target)
Add a new edge.
void applyLayout()
Apply a new layout.
void setRootNode(const string &name)
Set a root node for the graph.
int _agset(void *object, string attr, string value)
Set attribute.
GVC_t * getGVCcontext(void)
Get the current GVC context.


mtt
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:18