Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Edge id added to support input ordering.

  • Loading branch information...
commit 56c8d8a3d3ce3b0767000d777c5dd48b10386d88 1 parent 753a962
@franapoli authored
View
4 AdjMatrix.cpp
@@ -39,7 +39,7 @@ AdjMatrix::AdjMatrix(int n, int m) {
(*_mat)[i][j]=0;
}
}
-
+/* THE FOLLOWING WORKS BUT NOW THE SAME IS DONE IN Graph.cpp
void AdjMatrix::toDot(Nodeset &Ns, string fname) {
FILE *fid;
Nodeset::iterator i;
@@ -97,7 +97,7 @@ void AdjMatrix::toDot(Nodeset &Ns, string fname) {
}
-
+*/
void AdjMatrix::Set(unsigned i, unsigned j)
{
(*_mat)[i][j]=1;
View
2  AdjMatrix.h
@@ -17,7 +17,7 @@ class AdjMatrix {
virtual ~AdjMatrix();
AdjMatrix(int n, int m);
int *&operator[](unsigned i) {return *_mat[i];}
- void toDot(Nodeset &ns, string fname);
+ //void toDot(Nodeset &ns, string fname);
void Set(unsigned, unsigned);
ostream& operator>>(std::ostream &);
View
21 Edge.cpp
@@ -7,10 +7,13 @@
#include "Edge.h"
#include "Graph.h"
+#include <assert.h>
+
+unsigned Edge::_lastsocketid=0;
Edge::Edge() {
// TODO Auto-generated constructor stub
-
+ _socketid = ++_lastsocketid;
}
/*Edge::Edge(Node & n1, Node & n2)
@@ -25,6 +28,17 @@ Edge::Edge(unsigned id1, unsigned id2)
{
_srcnodeid=id1;
_dstnodeid=id2;
+ _socketid = ++_lastsocketid;
+ //n2._issource = false;
+ //n1._issink = false;
+}
+
+Edge::Edge(unsigned id1, unsigned id2, unsigned eid)
+{
+ _srcnodeid=id1;
+ _dstnodeid=id2;
+ _socketid = eid;
+ //assert(eid>=_lastsocketid);
//n2._issource = false;
//n1._issink = false;
}
@@ -34,8 +48,9 @@ Edge::~Edge() {
// TODO Auto-generated destructor stub
}
ostream & Edge::operator>>(std::ostream &str){
- str << "E[";
-
+ str << "E";
+ str << _socketid;
+ str << "[";
str << _srcnodeid;
str << ", ";
str << _dstnodeid;
View
5 Edge.h
@@ -18,6 +18,7 @@ class Edge {
Edge();
//Edge(Node &, Node &);
Edge(unsigned, unsigned);
+ Edge(unsigned id1, unsigned id2, unsigned eid);
virtual ~Edge();
std::ostream & operator>>(std::ostream &);
//Node & getSrcNode(void);
@@ -27,10 +28,14 @@ class Edge {
unsigned int getDstNodeId(void){return _dstnodeid;}
void setSrcNodeId(unsigned id){_srcnodeid=id;}
void setDstNodeId(unsigned id){_dstnodeid=id;}
+ unsigned int getId(void){return _socketid;}
+ void shiftId(int s){_socketid+=s; if (_socketid>_lastsocketid) _lastsocketid=_socketid;}
private:
unsigned int _srcnodeid;
unsigned int _dstnodeid;
+ unsigned int _socketid;
+ static unsigned int _lastsocketid;
bool _directed;
};
View
14 Edgeset.cpp
@@ -46,10 +46,22 @@ Edge &Edgeset::AddEdge(unsigned int id1, unsigned int id2)
return *e;
}
-void Edgeset::AddEdges(Edgeset & es)
+Edge &Edgeset::AddEdge(unsigned int id1, unsigned int id2, unsigned id)
+{
+ Edge *e = new Edge(id1, id2, id);
+ if(!has(id1, id2) )
+ push_back(*e);
+ return *e;
+}
+
+
+void Edgeset::AddEdges(Edgeset & es, bool copyid)
{
Edgeset::iterator i;
for(i=es.begin(); i!=es.end(); i++)
+ if(copyid)
+ AddEdge(i->getSrcNodeId(), i->getDstNodeId(), i->getId());
+ else
AddEdge(i->getSrcNodeId(), i->getDstNodeId());
}
View
3  Edgeset.h
@@ -23,7 +23,8 @@ class Edgeset : public vector<Edge>{
void AddEdge(Edge &);
// Edge & AddEdge(Node &, Node &);
Edge & AddEdge(unsigned int id1, unsigned int id2);
- void AddEdges(Edgeset &);
+ Edge & AddEdge(unsigned int id1, unsigned int id2, unsigned id);
+ void AddEdges(Edgeset &, bool copyid=false);
//Nodeset &GetInNodes(Node &);
//Nodeset &GetOutNodes(Node &);
std::ostream & operator>>(std::ostream &str);
View
20 Frame.cpp
@@ -145,7 +145,7 @@ Linkable* Frame::doAction(ACTION_TYPE type, Linkable * p1, Linkable * p2, Linkab
l2.UpdateIds();
symboltable.addSymbol(name, ss.str(), &l2);
gtemp->AddNodes(l2.GetNodes());
- gtemp->AddEdges(l2.GetEdges());
+ gtemp->AddEdges(l2.GetEdges(), true);
if(FRAME_DEBUG) cout << "Result:" << endl;
if(FRAME_DEBUG) l2 >> cout << endl;
l=&l2;
@@ -218,10 +218,10 @@ Linkable* Frame::doAction(ACTION_TYPE type, Linkable * p1, Linkable * p2, Linkab
arrowgraph->AddNodes(p1->GetNodes());
arrowgraph->AddNodes(p2->GetNodes());
- arrowgraph->AddEdges(p1->GetEdges());
- arrowgraph->AddEdges(p2->GetEdges());
+ arrowgraph->AddEdges(p1->GetEdges(), true);
+ arrowgraph->AddEdges(p2->GetEdges(), true);
- arrowgraph->AddEdges(leaf::link(*p1, *p2));
+ arrowgraph->AddEdges(leaf::link(*p1, *p2), true);
if(!p4) {
arrowgraph->SetOutputs(p2->GetOutputs());
@@ -233,7 +233,7 @@ Linkable* Frame::doAction(ACTION_TYPE type, Linkable * p1, Linkable * p2, Linkab
arrowgraph->SetOutputs(p1->GetInputs());
}
gtemp->AddNodes(arrowgraph->GetNodes());
- gtemp->AddEdges(arrowgraph->GetEdges());
+ gtemp->AddEdges(arrowgraph->GetEdges(), true);
if ((int) p3 != 2){ // hashing
Nodeset &temp = p2->GetOutputs();
@@ -341,9 +341,11 @@ Linkable* Frame::doAction(ACTION_TYPE type, Linkable * p1, Linkable * p2, Linkab
else {
gtemp->AddNodes(p2->GetNodes());
- gtemp->AddEdges(p2->GetEdges());
- gtemp->AddEdges(leaf::link(*p2, *p1));
- gtemp->AddEdges(leaf::link(*p2, *p3));
+ gtemp->AddEdges(p2->GetEdges(), true);
+ gtemp->AddEdges(leaf::link(*p2, *p1), true);
+ Edgeset &es1 = leaf::link(*p2, *p3);
+ gtemp->AddEdges(es1, true);
+ gtemp->SwitchEdgeIdOrder(gtemp->getEdgesRooted(*p3), es1);
gtemp->SetOutputs(p2->GetOutputs());
gtemp->SetInputs(p2->GetInputs());
}
@@ -410,7 +412,7 @@ Linkable* Frame::doAction(ACTION_TYPE type, Linkable * p1, Linkable * p2, Linkab
if(FRAME_DEBUG) *gtemp >> cout;
g->AddNodes(gtemp->GetNodes());
- g->AddEdges(gtemp->GetEdges());
+ g->AddEdges(gtemp->GetEdges(), true);
gtemp = new Graph;
return gtemp;
View
136 Graph.cpp
@@ -7,7 +7,8 @@
#include "Graph.h"
#include "AdjMatrix.h"
-
+#include "Linkable.h"
+#include <limits>
Graph::Graph() {
// TODO Auto-generated constructor stub
@@ -28,9 +29,6 @@ void Graph::MergeWith(Graph & graph)
}
-void Graph::FindNode(string string)
-{
-}
@@ -38,6 +36,76 @@ void Graph::Destroy(void)
{
}
+void Graph::ToDot(std::string fname)
+{
+ FILE *fid;
+ fid=fopen((string(fname)).c_str(), "w");
+ string attr_string="";
+ std::map<std::string, std::string>::iterator mapit;
+ std::map<std::string, std::string> attribs;
+
+ fprintf(fid, "digraph G {\n");
+ fprintf(fid, "node [shape=box, style=rounded];\n");
+ fprintf(fid, "rankdir=LR;\n");
+ Nodeset::iterator i,j;
+
+
+ for(i=_V.begin(); i!=_V.end(); ++i){
+ attr_string = "[ label = ";
+ attr_string += i->getName();
+
+ attribs = i->GetAttributes();
+
+ for(mapit = attribs.begin(); mapit!=attribs.end(); mapit++){
+ attr_string += ", ";
+ attr_string += (mapit -> first).c_str();
+ attr_string += " = ";
+ attr_string += (mapit -> second).c_str();
+ }
+ attr_string += "] ";
+
+ fprintf(fid, "%d %s;\n", i->getId(), attr_string.c_str());
+ //fprintf(fid, "%d [ label = \"%s\", color = \"%s\" ];\n", i->getId(), i->getName().c_str(), i->GetAttribute("color").c_str());
+
+
+ }
+
+
+
+ // Sorting edges by id in O(n^2). This is good enough
+ // for small graphs.
+ Edgeset::iterator e;
+ unsigned minid, lastminid;
+ lastminid=0;
+ Edgeset::iterator minedge;
+ while(true){
+ minid = std::numeric_limits<unsigned>::max();
+ for(e=_E.begin(); e!=_E.end(); e++)
+ if(e->getId() < minid && e->getId() > lastminid){
+ minid = e->getId();
+ minedge = e;
+ }
+ if (minid==std::numeric_limits<unsigned>::max())
+ break;
+ lastminid=minid;
+ fprintf(fid, "%d->%d [id=%u];\n", minedge->getSrcNodeId(), minedge->getDstNodeId(), minedge->getId());
+ }
+
+ fprintf(fid, "}");
+ fclose(fid);
+}
+
+
+Edge *Graph::findEdge(unsigned id1, unsigned id2)
+{
+ Edgeset::iterator i;
+
+ for(i=_E.begin(); i!=_E.end(); i++)
+ if(i->getSrcNodeId() == id1 && i->getDstNodeId() == id2)
+ return &*i;
+
+ return 0;
+}
AdjMatrix &Graph::Visit(void)
{
@@ -187,7 +255,8 @@ Nodeset& Graph::GetSources(void)
Graph::operator Nodeset(){
-cerr << "unimplemented";
+cerr << "this was never tested! (Graph::operator Nodeset)";
+return _V;
}
void Graph::AddNode(Node &n){
@@ -195,19 +264,25 @@ void Graph::AddNode(Node &n){
}
-void Graph::AddEdges(Edgeset &e)
+Edgeset &Graph::AddEdges(Edgeset &e, bool copyid)
{
Node *n1, *n2;
Edgeset::iterator i;
- Edge *ne;
+ Edgeset &newedges = *new Edgeset();
for(i = e.begin(); i<e.end(); i++) {
n1 = &_V.findNode(i->getSrcNodeId());
n2 = &_V.findNode(i->getDstNodeId());
- ne=&_E.AddEdge(n1->getId(), n2->getId());
+ if(copyid)
+ newedges.push_back(_E.AddEdge(n1->getId(), n2->getId(), i->getId()));
+ else
+ newedges.push_back(_E.AddEdge(n1->getId(), n2->getId()));
+
n1->_issink=false;
n2->_issource=false;
}
+
+return newedges;
}
@@ -237,3 +312,48 @@ void Graph::SetAttribute(std::string s1, std::string s2)
for(i=_V.begin(); i!=_V.end(); i++)
i->SetAttribute(s1, s2);
}
+
+
+void Graph::resetVisited(void){
+ Nodeset::iterator i;
+ for(i=_V.begin(); i!=_V.end(); i++)
+ i->_visited=false;
+}
+
+/* Gets all edges in the subtree rooted in Node n. This is required
+ * to update edge ids while building the graph in Frame (ACT_FORK).
+ * This is partly copy-pasted from Visit (which is now unused).
+ */
+Edgeset& Graph::getEdgesRooted(Linkable& n)
+{
+ Node i;
+ Nodeset::iterator j;
+ Nodeset stack;
+ Edgeset &es=*new Edgeset;
+ unsigned edgeid;
+ Nodeset *ns;
+
+ resetVisited();
+
+ stack.AddNodes(n.GetNodes());
+ unsigned ij=0;
+
+ while(!stack.empty()){
+ i=stack.back();
+ stack.pop_back();
+
+ i.Visit();
+ ns=&GetOutNodes(i);
+
+ for(j=ns->begin(); j!=ns->end(); j++, ij++) {
+ if(!j->isVisited())
+ stack.AddNode(*j);
+ edgeid = findEdge(i.getId(), j->getId())->getId();
+ es.AddEdge(i.getId(),j->getId(),edgeid);
+ }
+
+ }
+
+ resetVisited();
+ return es;
+}
View
45 Graph.h
@@ -18,29 +18,32 @@ class Graph : public Linkable {
Graph();
Graph(Nodeset &, Edgeset&);
virtual ~Graph();
- void MergeWith(Graph &);
- void FindNode(string);
- void Destroy(void);
- Nodeset& GetOutputs(void);
- Nodeset& GetInputs(void);
- AdjMatrix &Visit(void);
- ostream& operator>>(std::ostream &);
- Edgeset& GetEdges(void) {return _E;}
- void AddEdges(Edgeset &e);
- void AddNodes(Nodeset &e) {_V.AddNodes(e);}
- Nodeset& GetNodes(void) {return _V;}
- void SetInputs(Nodeset &);
- void SetOutputs(Nodeset &);
- Nodeset& GetSources(void);
- Nodeset& GetSinks(void);
+ void MergeWith(Graph &);
+ void Destroy(void);
+ Nodeset& GetOutputs(void);
+ Nodeset& GetInputs(void);
+ AdjMatrix& Visit(void);
+ ostream& operator>>(std::ostream &);
+ Edgeset& GetEdges(void) {return _E;}
+ Edgeset& AddEdges(Edgeset &e, bool copyid=false);
+ void AddNodes(Nodeset &e) {_V.AddNodes(e);}
+ Nodeset& GetNodes(void) {return _V;}
+ void SetInputs(Nodeset &);
+ void SetOutputs(Nodeset &);
+ Nodeset& GetSources(void);
+ Nodeset& GetSinks(void);
virtual operator Nodeset();
- void AddNode(Node &);
- Node &findNode(unsigned id){return _V.findNode(id);}
- Nodeset &GetOutNodes(Node &);
+ void AddNode(Node &);
+ Node& findNode(unsigned id){return _V.findNode(id);}
+ Nodeset& GetOutNodes(Node &);
virtual Linkable &Clone(){Graph &g=*new Graph; g = *this; return g;}
- void UpdateIds();
- void SetAttribute(std::string s1, std::string s2);
- void SetAttribute(unsigned n, std::string s1, std::string s2){_V.SetAttribute(n,s1,s2);}
+ void UpdateIds();
+ void SetAttribute(std::string s1, std::string s2);
+ void SetAttribute(unsigned n, std::string s1, std::string s2){_V.SetAttribute(n,s1,s2);}
+ void ToDot(std::string fname);
+ Edge* findEdge(unsigned id1, unsigned id2);
+ Edgeset& getEdgesRooted(Linkable&);
+ void resetVisited(void);
private:
Edgeset _E;
View
37 Linkable.cpp
@@ -30,6 +30,43 @@ void Linkable::setName(std::string _name)
}
+void Linkable::SwitchEdgeIdOrder(Edgeset& e1, Edgeset& e2) {
+/*Make so that all ids in l1 will come after ids in l2. This is need
+ * to allow subtrees containing "->" to have edges with higher ids than
+ * the tree root.
+ * Reusing ids would be a good idea, but at the moment it is not necessary, so
+ * ids from e1 will just be shifted after e2.
+ * I'll just take care of preserving order in e2.
+ */
+
+ Edgeset::iterator i;
+ unsigned maxid=0;
+
+ for(i=e2.begin(); i!=e2.end(); i++)
+ if(i->getId()>maxid)
+ maxid=i->getId();
+ ;
+
+ unsigned minid=-1;
+ for(i=e1.begin(); i!=e1.end(); i++)
+ if(i->getId()<minid)
+ minid=i->getId();
+ ;
+
+ if(minid>maxid)
+ //nothing to be done
+ return;
+
+ unsigned diff;
+ diff = maxid-minid+1; //the +1 is probably not needed, but quite unharmful
+
+ Edgeset &mye = GetEdges();
+ for(i=mye.begin(); i!=mye.end(); i++)
+ if(e1.has(i->getSrcNodeId(), i->getDstNodeId()))
+ i->shiftId(diff);
+ ;
+}
+
Linkable *Linkable::_has(std::string s) {
std::vector<Linkable *>::iterator i=_linkables.begin();
View
1  Linkable.h
@@ -47,6 +47,7 @@ class Linkable {
void setName (std::string _name);
std::string GetAttribute (std::string s) {return _attributes[s];}
std::map<std::string, std::string> GetAttributes() {return _attributes;}
+ void SwitchEdgeIdOrder(Edgeset &, Edgeset &);
protected:
std::string _name;
View
9 Node.cpp
@@ -9,6 +9,7 @@
#include <iostream> // JUST FOR DEBUG
#include "Nodeset.h"
+class Graph;
unsigned Node::_lastid=0;
Node::Node() {
@@ -98,4 +99,10 @@ ostream& Node::operator>>(std::ostream &str)
}
-Edgeset &Node::GetEdges(void){return *new Edgeset();}
+
+/* Design conflict?
+ * Node has no edge, but we need a GetEdges on a generic Linkable
+ */
+Edgeset &Node::GetEdges(void)
+{return *new Edgeset();}
+
View
1  globals.h
@@ -10,6 +10,7 @@
#include <stdio.h>
#include <vector>
+#include <assert.h>
extern const char *protname;
extern FILE *yyin;
View
5 main.cc
@@ -49,8 +49,9 @@ int main(int ARGC, char *ARGV[])
}
- adjmat = &res->Visit();
- adjmat->toDot(res->GetNodes(), "out.dot");
+ res->ToDot("out.dot");
+ //adjmat = &res->Visit();
+ //adjmat->toDot(res->GetNodes(), "out.dot");
//fprintf(stdout, "Protocol correctly parsed.\n");
return 0;
}

0 comments on commit 56c8d8a

Please sign in to comment.
Something went wrong with that request. Please try again.