-
Notifications
You must be signed in to change notification settings - Fork 10
/
ri3.cpp
303 lines (244 loc) · 7.63 KB
/
ri3.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
Copyright (c) 2014 by Rosalba Giugno
This library contains portions of other open source products covered by separate
licenses. Please see the corresponding source files for specific terms.
RI is provided under the terms of The MIT License (MIT):
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include "c_textdb_driver.h"
#include "timer.h"
#include "AttributeComparator.h"
#include "AttributeDeconstructor.h"
#include "Graph.h"
#include "MatchingMachine.h"
#include "MaMaConstrFirst.h"
#include "Match.h"
//#define FIRST_MATCH_ONLY //if setted, the searching process stops at the first found match
#include "Solver.h"
#include "IsoGISolver.h"
#include "SubGISolver.h"
#include "InducedSubGISolver.h"
#define PRINT_MATCHES
//#define CSV_FORMAT
using namespace rilib;
void usage(char* args0);
int match(MATCH_TYPE matchtype, GRAPH_FILE_TYPE filetype, std::string& referencefile, std::string& queryfile);
int main(int argc, char* argv[]){
if(argc!=5){
usage(argv[0]);
return -1;
}
MATCH_TYPE matchtype;
GRAPH_FILE_TYPE filetype;
std::string reference;
std::string query;
std::string par = argv[1];
if(par=="iso"){
matchtype = MT_ISO;
}
else if(par=="ind"){
matchtype = MT_INDSUB;
}
else if(par=="mono"){
matchtype = MT_MONO;
}
else{
usage(argv[0]);
return -1;
}
par = argv[2];
if(par=="gfu"){
filetype = GFT_GFU;
}
else if(par=="gfd"){
filetype = GFT_GFD;
}
else if(par=="gfda"){
filetype = GFT_GFDA;
}
else if(par=="geu"){
filetype = GFT_EGFU;
}
else if(par=="ged"){
filetype = GFT_EGFD;
}
else if(par=="vfu"){
filetype = GFT_VFU;
}
else{
usage(argv[0]);
return -1;
}
reference = argv[3];
query = argv[4];
return match(matchtype, filetype, reference, query);
};
void usage(char* args0){
std::cout<<"usage "<<args0<<" [iso ind mono] [gfu gfd gfda geu ged vfu] reference query\n";
std::cout<<"\tmatch type:\n";
std::cout<<"\t\tiso = isomorphism\n";
std::cout<<"\t\tind = induced subisomorphism\n";
std::cout<<"\t\tmono = monomorphism\n";
std::cout<<"\tgraph input format:\n";
std::cout<<"\t\tgfu = undirect graphs with labels on nodes\n";
std::cout<<"\t\tgfd = direct graphs with labels on nodes\n";
std::cout<<"\t\tgfd = direct graphs with one single label on nodes\n";
std::cout<<"\t\tgeu = undirect graphs with labels both on nodes and edges\n";
std::cout<<"\t\tged = direct graphs with labels both on nodes and edges\n";
std::cout<<"\t\tvfu = VF2Lib undirect unlabeled format\n";
std::cout<<"\treference file contains one or more reference graphs\n";
std::cout<<"\tquery contains the query graph (just one)\n";
};
int match(
MATCH_TYPE matchtype,
GRAPH_FILE_TYPE filetype,
std::string& referencefile,
std::string& queryfile){
TIMEHANDLE load_s, load_s_q, make_mama_s, match_s, total_s;
double load_t=0;double load_t_q=0; double make_mama_t=0; double match_t=0; double total_t=0;
total_s=start_time();
bool takeNodeLabels = false;
bool takeEdgesLabels = false;
int rret;
AttributeComparator* nodeComparator; //to compare node labels
AttributeComparator* edgeComparator; //to compare edge labels
switch(filetype){
case GFT_GFU:
case GFT_GFD:
// only nodes have labels and they are strings
nodeComparator = new StringAttrComparator();
edgeComparator = new DefaultAttrComparator();
takeNodeLabels = true;
break;
case GFT_GFDA:
nodeComparator = new DefaultAttrComparator();
edgeComparator = new DefaultAttrComparator();
takeNodeLabels = true;
break;
case GFT_EGFU:
case GFT_EGFD:
//labels on nodes and edges, both of them are strings
nodeComparator = new StringAttrComparator();
edgeComparator = new StringAttrComparator();
takeNodeLabels = true;
takeEdgesLabels = true;
break;
case GFT_VFU:
//no labels
nodeComparator = new DefaultAttrComparator();
edgeComparator = new DefaultAttrComparator();
break;
default:
return -1;
}
TIMEHANDLE tt_start;
double tt_end;
//read the query graph
load_s_q=start_time();
Graph *query = new Graph();
rret = read_graph(queryfile.c_str(), query, filetype);
load_t_q+=end_time(load_s_q);
if(rret !=0){
std::cout<<"error on reading query graph\n";
}
make_mama_s=start_time();
MaMaConstrFirst* mama = new MaMaConstrFirst(*query);
mama->build(*query);
make_mama_t+=end_time(make_mama_s);
//mama->print();
long steps = 0, //total number of steps of the backtracking phase
triedcouples = 0, //nof tried pair (query node, reference node)
matchcount = 0, //nof found matches
matchedcouples = 0; //nof mathed pair (during partial solutions)
long tsteps = 0, ttriedcouples = 0, tmatchedcouples = 0;
FILE *fd = open_file(referencefile.c_str(), filetype);
if(fd != NULL){
#ifdef PRINT_MATCHES
//to print found matches on screen
MatchListener* matchListener=new ConsoleMatchListener();
#else
//do not print matches, just count them
MatchListener* matchListener=new EmptyMatchListener();
#endif
int i=0;
bool rreaded = true;
do{//for each reference graph inside the input file
#ifdef PRINT_MATCHES
std::cout<<"#"<<i<<"\n";
#endif
//read the next reference graph
load_s=start_time();
Graph * rrg = new Graph();
int rret = read_dbgraph(referencefile.c_str(), fd, rrg, filetype);
rreaded = (rret == 0);
load_t+=end_time(load_s);
if(rreaded){
//run the matching
match_s=start_time();
match( *rrg,
*query,
*mama,
*matchListener,
matchtype,
*nodeComparator,
*edgeComparator,
&tsteps,
&ttriedcouples,
&tmatchedcouples);
match_t+=end_time(match_s);
//see rilib/Solver.h
// steps += tsteps;
// triedcouples += ttriedcouples;
matchedcouples += tmatchedcouples;
}
delete rrg;
i++;
}while(rreaded);
matchcount = matchListener->matchcount;
delete matchListener;
fclose(fd);
}
else{
std::cout<<"unable to open reference file\n";
return -1;
}
total_t=end_time(total_s);
#ifdef CSV_FORMAT
std::cout<<referencefile<<"\t"<<queryfile<<"\t";
std:cout<<load_t_q<<"\t"<<make_mama_t<<"\t"<<load_t<<"\t"<<match_t<<"\t"<<total_t<<"\t"<<steps<<"\t"<<triedcouples<<"\t"<<matchedcouples<<"\t"<<matchcount;
#else
std::cout<<"reference file: "<<referencefile<<"\n";
std::cout<<"query file: "<<queryfile<<"\n";
std::cout<<"total time: "<<total_t<<"\n";
std::cout<<"matching time: "<<match_t<<"\n";
std::cout<<"number of found matches: "<<matchcount<<"\n";
std::cout<<"search space size: "<<matchedcouples<<"\n";
#endif
// delete mama;
// delete query;
delete mama;
delete query;
delete nodeComparator;
delete edgeComparator;
return 0;
};