-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunittestfornetdrawer.cpp
283 lines (235 loc) · 7.45 KB
/
unittestfornetdrawer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "network.h"
#include "nnode.h"
#include "link.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include "math.h"
#include <string>
using namespace NEAT;
//void setPosition(double &x, double &y, double Min, double Max){
void setPosition(double &x, double &y){
//randomly generate position for a node;
double Min = 0.0, Max = 1000.0;
double p = (double)rand() / RAND_MAX;
x = Min + p * (Max - Min);
x = (floor(x * 10))/10; //only keep one digit after the decimal point
double q = (double)rand() / RAND_MAX;
y = Min + q * (Max - Min);
y = (floor(y * 10))/10;
return;
}
void setPositionX(double &x, int side){
//side can only be 0, 1, 2 (representing left, right, middle)
switch(side){
case 0:
x = 100.0;
break;
case 1:
x = 500.0;
break;
case 2:
x = 300.0;
}
return;
}
void printNode(NNode* node, double x, double y, std::ofstream &myfile){
int type = node->type;
//nodetype is 1, which is transition
if(type){
myfile << "<transition id=\"" << node->node_id << "\">\n";
myfile << "<graphics>\n";
myfile << "<position x=\"" << x << "\" y=\"" << y << "\"/>\n";
myfile << "</graphics>\n";
myfile << "<name>\n";
myfile << "<value>" << node->node_id << "</value>\n";
myfile << "<graphics/>\n";
myfile << "</name>\n";
myfile << "<orientation>\n";
myfile << "<value>90</value>\n";
myfile << "</orientation>\n";
myfile << "<rate>\n";
myfile << "<value>1.0</value>\n";
myfile << "</rate>\n";
myfile << "<timed>\n";
myfile << "<value>false</value>\n";
myfile << "</timed>\n";
myfile << "</transition>\n";
}
else{ ////nodetype is 0, which is place
myfile << "<place id=\"" << node->node_id << "\">\n";
myfile << "<graphics>\n";
myfile << "<position x=\"" << x << "\" y=\"" << y << "\"/>\n";
myfile << "</graphics>\n";
myfile << "<name>\n";
myfile << "<value>" << node->node_id << "</value>\n";
myfile << "<graphics/>\n";
myfile << "</name>\n";
myfile << "<initialMarking>\n";
myfile << "<value>" << node->tok_count << "</value>\n";
myfile << "<graphics/>\n";
myfile << "</initialMarking>\n";
myfile << "</place>\n";
}
}
void printLink(Link* nlink, std::map<NNode*, int> npos, std::vector<double> xpos, std::vector<double> ypos, std::ofstream &myfile){
NNode* in = nlink->in_node;
NNode* out = nlink->out_node;
int in_ptr = npos[in];
int out_ptr = npos[out];
myfile << "<arc id=\"" << in->node_id << " to " << out->node_id << "\" ";
myfile << "source=\"" << in->node_id << "\" target=\"" << out->node_id <<"\">\n";
myfile << "<graphics/>\n";
myfile << "<inscription>\n";
myfile << "<value>" << (int)nlink->weight << "</value>\n";
myfile << "<graphics/>\n";
myfile << "</inscription>\n";
myfile << "<arcpath id=\"000\" x=\"" << ((int)xpos[in_ptr]) + 11;
myfile << "\" y=\"" << ((int)ypos[in_ptr]) + 5 << "\" curvePoint=\"false\"/>\n";
myfile << "<arcpath id=\"001\" x=\"" << ((int)xpos[out_ptr]) + 11;
myfile << "\" y=\"" << ((int)ypos[out_ptr]) + 5 << "\" curvePoint=\"false\"/>\n";
myfile << "</arc>\n";
}
int netdrawer(const Network* network){
//build an xml file for the network
//initialize the xml file
std::ofstream myfile;
char buff[100];
sprintf(buff, "PetriNet_%d.xml", network->net_id);
myfile.open(buff, std::ofstream::out);
myfile <<"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<pnml>\n<net id=\"Net-One\" type=\"P/T net\">";
//a hashmap to record posiitons of nodes using node pointer (NNode*) and position number(int)
std::map<NNode*, int> nodepos;
//two vectors to record positions of the nodes
std::vector<double> xpos;
std::vector<double> ypos;
//Write all the places
std::vector<NNode*>::const_iterator curnode;
int count = 0;
double x, y = 50.0;
setPositionX(x, 2); //setting positions for transitions
//Write all the transitions
for(curnode = network->transitions.begin(); curnode != network->transitions.end(); ++curnode){
//generate an position
nodepos[*curnode] = count;
xpos.push_back(x);
ypos.push_back(y);
printNode(*curnode, x, y, myfile);
y += 50;
count++;
}
y = 50.0;
//Write all the places
for(curnode = network->places.begin(); curnode != network->places.end(); ++curnode) {
//generate an random position for curnode;
int side = randint(0, 1);
setPositionX(x, side);
nodepos[*curnode] = count;
xpos.push_back(x);
ypos.push_back(y);
y += 50;
printNode(*curnode, x, y, myfile);
count++;
}
//Write all the links
std::vector<Link*>::const_iterator curlink;
for(curnode = network->places.begin(); curnode != network->places.end(); ++curnode) {
for(curlink = (*curnode)->incoming.begin(); curlink != (*curnode)->incoming.end(); ++curlink){
printLink(*curlink, nodepos, xpos, ypos, myfile);
}
}
for(curnode = network->transitions.begin(); curnode != network->transitions.end(); ++curnode){
for(curlink = (*curnode)->incoming.begin(); curlink != (*curnode)->incoming.end(); ++curlink){
printLink(*curlink, nodepos, xpos, ypos, myfile);
}
}
//closing xml file
myfile <<"</net>\n</pnml>";
myfile.close();
return 0;
}
int main(){
/*
//test setPosition
double x = 0.0, y = 0.0;
for(int i = 0; i < 10; i++){
setPosition(x, y);
std::cout << " x = " << x << " y = " << y << std::endl;
}
*/
/* //testing printNode
std::ofstream myfile;
myfile.open( "net.xml");
myfile <<"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<pnml>\n<net id=\"Net-One\" type=\"P/T net\">";
NNode* nodep = new NNode(TRANSITION, 1111);
NNode* nodet = new NNode(PLACE, 2222);
double x = 0.0, y = 0.0;
setPosition(x, y);
printNode(nodep, x, y, myfile);
setPosition(x, y);
printNode(nodet, x, y, myfile);
//closing xml file
myfile <<"</net>\n</pnml>";
myfile.close();
*/
/*
//testing printLink
std::ofstream myfile;
myfile.open( "net.xml");
myfile <<"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<pnml>\n<net id=\"Net-One\" type=\"P/T net\">";
//a hashmap to record posiitons of nodes using node pointer (NNode*) and position number(int)
std::map<NNode*, int> nodepos;
//two vectors to record positions of the nodes
std::vector<double> xpos;
std::vector<double> ypos;
NNode* nodep = new NNode(TRANSITION, 1111);
NNode* nodet = new NNode(PLACE, 2222);
double x = 0.0, y = 0.0;
int count = 0;
setPosition(x, y);
nodepos[nodep] = count;
xpos.push_back(x);
ypos.push_back(y);
printNode(nodep, x, y, myfile);
count++;
setPosition(x, y);
nodepos[nodet] = count;
xpos.push_back(x);
ypos.push_back(y);
printNode(nodet, x, y, myfile);
Link* nlink = new Link(2, nodep, nodet);
printLink(nlink, nodepos, xpos, ypos, myfile);
//closing xml file
myfile <<"</net>\n</pnml>";
myfile.close();
*/
std::vector<NNode*> ps;
std::vector<NNode*> ts;
std::vector<NNode*> all;
NNode* np1 = new NNode(PLACE, 1111);
NNode* np2 = new NNode(PLACE, 1112);
NNode* np3 = new NNode(PLACE, 1113);
NNode* nt1 = new NNode(TRANSITION, 2221);
NNode* nt2 = new NNode(TRANSITION, 2222);
NNode* nt3 = new NNode(TRANSITION, 2223);
NNode* nodegroup[] = {np1, np2, np3, nt1, nt2, nt3};
for(int i = 0; i < 3; i++){
std::cout << i << " : " << nodegroup[i]->tok_count << std::endl;
}
for(int i = 0; i < 6; i++){
if(nodegroup[i]->type == 0)
ps.push_back(nodegroup[i]);
else
ts.push_back(nodegroup[i]);
all.push_back(nodegroup[i]);
}
Link* linkgroup[4];
linkgroup[1] = new Link(2, np1, nt1);
linkgroup[2] = new Link(1, np1, nt2);
linkgroup[3] = new Link(3, np2, nt3);
linkgroup[0] = new Link(2, np3, nt1);
Network* network = new Network(ps, ts, all, 1);
netdrawer(network);
return 0;
}