-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcreateSyntacticTests.cpp
374 lines (352 loc) · 15.9 KB
/
createSyntacticTests.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* \file createSyntacticTests.cpp
* \brief Create syntactic tests from the libsbml source validation files.
* \author Lucian Smith
*
* <!--------------------------------------------------------------------------
* This file is part of libSBML. Please visit http://sbml.org for more
* information about SBML, and the latest version of libSBML.
*
* Copyright (C) 2013-2014 jointly by the following organizations:
* 1. California Institute of Technology, Pasadena, CA, USA
* 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
* 3. University of Heidelberg, Heidelberg, Germany
*
* Copyright (C) 2009-2013 jointly by the following organizations:
* 1. California Institute of Technology, Pasadena, CA, USA
* 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
*
* Copyright (C) 2006-2008 by the California Institute of Technology,
* Pasadena, CA, USA
*
* Copyright (C) 2002-2005 jointly by the following organizations:
* 1. California Institute of Technology, Pasadena, CA, USA
* 2. Japan Science and Technology Agency, Japan
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation. A copy of the license agreement is provided
* in the file named "LICENSE.txt" included with this software distribution
* and also available online as http://sbml.org/software/libsbml/license.html
* ---------------------------------------------------------------------- -->*/
#include <iostream>
#include <fstream>
#include <set>
#include <algorithm>
#include "TestFile.h"
#include "TestValidator.h"
#include <direct.h>
#include <sbml/SBMLTypes.h>
#ifdef LIBSBML_USE_VLD
#include <vld.h>
#endif
/** @cond doxygenIgnored */
using namespace std;
LIBSBML_CPP_NAMESPACE_USE
/** @endcond */
void copyFile(const string& infilename, const string& outfilename)
{
ifstream infile(infilename.c_str());
ofstream outfile(outfilename.c_str());
if (!infile.good()) {
cout << "Unable to open file " << infilename << " for reading." << endl;
return;
}
if (!outfile.good()) {
cout << "Unable to open file " << outfilename << " for writing. Check that the directory exists, and if it does not, create it first." << endl;
return;
}
string str;
while(getline(infile,str)){
outfile << str << endl;
}
infile.close();
outfile.close();
}
void createConstraintsFile(const string& sbmlfilename, const SBMLErrorLog* errlog, vector<SBMLError*>& uniqueErrors, set<unsigned int>& uniqueErrorIDs)
{
string constraintfilename = sbmlfilename;
size_t xml = constraintfilename.find(".xml");
constraintfilename.replace(xml, 4, ".txt");
ofstream cfile(constraintfilename.c_str());
if (!cfile.good()) {
cout << "Unable to open file " << constraintfilename << " for writing. Check that the directory exists, and if it does not, create it first." << endl;
return;
}
for (unsigned int err=0; err<errlog->getNumErrors(); err++) {
const SBMLError* error = errlog->getError(err);
cfile << "------------------" << endl;
cfile << "Validation id :\t" << TestFile::getConstraintIdString(error->getErrorId()) << endl;
cfile << "Validation number:\t" << error->getErrorId() << endl;
cfile << "Severity :\t" << error->getSeverityAsString() << endl;
cfile << "Line number :\t" << error->getLine() << endl;
//cfile << "Column number :\t" << error->getColumn() << endl;
cfile << "Package :\t" << error->getPackage() << endl;
cfile << "Short message :\t" << error->getShortMessage() << endl;
cfile << "Full message :\t" << error->getMessage() << endl;
//Now store the unique error IDs
if (uniqueErrorIDs.insert(error->getErrorId()).second) {
uniqueErrors.push_back(error->clone());
}
}
if (errlog->getNumErrors()>0) {
cfile << "------------------" << endl;
}
}
/**
* Parse the files in the given set of directories, copy the files out (renamed) if they only contain the errors they claim they do, and report what we're doing.
*/
bool
parseDirectories ( const vector<string>& directories, const string& outdir, const string& package, ofstream& report, vector<SBMLError*>& uniqueErrors, set<unsigned int>& uniqueErrorIDs, bool fullreport)
{
for (size_t d=0; d<directories.size(); d++) {
set<TestFile> files = TestFile::getFilesIn(directories[d]);
if (files.size() > 0) {
cout << files.size() << " files found in directory " << directories[d] << ":" << endl;
for (set<TestFile>::iterator file=files.begin(); file != files.end(); file++) {
stringstream reportline;
if (file->getConstraintId() >= 90500 && file->getConstraintId() < 90600) {
//The 905xx constraints are duplicates, and don't need to go into a test suite.
continue;
}
if (fullreport) {
reportline << file->getDirectory()
<< "," << file->getFilename() << ",";
}
reportline
<< file->getConstraintId()
<< "," << file->getNumFailures()
<< "," << file->getSequenceId()
<< "," << file->getAdditionalFailId();
string fullname = file->getFullname();
string outfilename = file->getNewFilename();
if (fullname.find("pass")) {
reportline << "," << "pass";
}
else {
reportline << "," << "fail";
}
reportline << "," << package;
SBMLDocument* document = readSBMLFromFile(fullname.c_str());
document->checkConsistency();
const SBMLErrorLog* errlog = document->getErrorLog();
bool copy = true;
if (errlog->getNumFailsWithSeverity(LIBSBML_SEV_ERROR) > file->getNumFailures() + (file->getAdditionalFailId() > 0)) {
cout << "Not copying document " << file->getFilename() << ", since it has more errors than the expected " << file->getNumFailures() << "." << endl;
if (file->getNumFailures() == 0) {
file = file;
}
copy = false;
}
stringstream sevlv;
if (errlog->getNumFailsWithSeverity(LIBSBML_SEV_FATAL) > 0) {
sevlv << "-sev" << LIBSBML_SEV_FATAL;
reportline << ",Fatal";
}
else if (errlog->getNumFailsWithSeverity(LIBSBML_SEV_ERROR) > 0) {
sevlv << "-sev" << LIBSBML_SEV_ERROR;
reportline << ",Error";
}
else if (errlog->getNumFailsWithSeverity(LIBSBML_SEV_WARNING) > 0) {
sevlv << "-sev" << LIBSBML_SEV_WARNING;
reportline << ",Warning";
}
else if (errlog->getNumFailsWithSeverity(LIBSBML_SEV_INFO) > 0) {
sevlv << "-sev" << LIBSBML_SEV_INFO;
reportline << ",Info";
}
else if (fullname.find("pass") == string::npos) {
cout << "Not copying document " << file->getFilename() << ", since it has no errors at all, but should." << endl;
reportline << ",None" ;
copy = false;
}
else {
reportline << ",None" ;
}
if (fullname.find("fail") != string::npos && !errlog->contains(file->getConstraintId())) {
cout << "Not copying document " << file->getFilename() << ", since the error it is supposed to have did not appear." << endl;
copy = false;
}
sevlv << "-l" << document->getLevel() << "v" << document->getVersion();
outfilename.insert(outfilename.size()-4, sevlv.str());
if (copy) {
if (fullreport) {
reportline << ",copy";
}
mkdir((outdir + "/" + file->getConstraintIdString()).c_str());
string fulloutfilename = outdir + "/" + file->getConstraintIdString() + "/" + outfilename;
copyFile(fullname, fulloutfilename);
createConstraintsFile(fulloutfilename, errlog, uniqueErrors, uniqueErrorIDs);
if (file->getConstraintId()==1020310) {
//These are models that refer to external model definitions, and need to be copied verbatim.
fulloutfilename = outdir + "/" + file->getConstraintIdString() + "/" + file->getFilename();
copyFile(fullname, fulloutfilename);
}
}
else if (fullreport) {
reportline << ",no_copy";
}
for (unsigned int e=0; e<errlog->getNumErrors(); e++)
{
reportline << "," << errlog->getError(e)->getErrorId();
}
reportline << endl;
if (fullreport || copy) {
report << outfilename << "," << reportline.str();
}
delete document;
}
}
else {
cout << "No test files found in " << directories[d] << endl;
}
}
return false;
}
int
main (int argc, char* argv[])
{
#ifndef LIBSBML_HAS_PACKAGE_COMP
cout << "Please compile libsbml with comp enabled to run this program." << endl;
return 1;
#endif
#ifndef LIBSBML_HAS_PACKAGE_FBC
cout << "Please compile libsbml with fbc enabled to run this program." << endl;
return 1;
#endif
#ifndef LIBSBML_HAS_PACKAGE_LAYOUT
cout << "Please compile libsbml with layout enabled to run this program." << endl;
return 1;
#endif
#ifndef LIBSBML_HAS_PACKAGE_QUAL
cout << "Please compile libsbml with qual enabled to run this program." << endl;
return 1;
#endif
#ifndef LIBSBML_HAS_PACKAGE_GROUPS
cout << "Please compile libsbml with groups enabled to run this program." << endl;
return 1;
#endif
#ifndef LIBSBML_HAS_PACKAGE_MULTI
cout << "Please compile libsbml with multi enabled to run this program." << endl;
return 1;
#endif
string prefix(".");
char *srcdir = getenv("srcdir");
if (srcdir != NULL)
{
prefix = srcdir;
}
if (argc >= 2)
{
prefix = argv[1];
}
string outdir(".");
free(srcdir);
srcdir = getenv("outdir");
if (srcdir != NULL)
{
outdir = srcdir;
}
if (argc >= 3)
{
outdir = argv[2];
}
bool fullreport = false;
if (argc >= 4)
{
if ((string)argv[3] == "-full") fullreport = true;
}
prefix += "/src/sbml";
outdir += "/syntactic";
vector<string> validationDirectories;
vector<string> compValidationDirectories;
vector<string> fbcValidationDirectories;
vector<string> layoutValidationDirectories;
vector<string> qualValidationDirectories;
vector<string> groupsValidationDirectories;
vector<string> multiValidationDirectories;
validationDirectories.push_back(prefix + "/validator/test/test-data/libsbml-constraints/");
validationDirectories.push_back(prefix + "/validator/test/test-data/sbml-annotation-constraints/");
validationDirectories.push_back(prefix + "/validator/test/test-data/sbml-general-consistency-constraints/");
validationDirectories.push_back(prefix + "/validator/test/test-data/sbml-identifier-constraints/");
validationDirectories.push_back(prefix + "/validator/test/test-data/sbml-mathml-constraints/");
validationDirectories.push_back(prefix + "/validator/test/test-data/sbml-modeldefinition-constraints/");
validationDirectories.push_back(prefix + "/validator/test/test-data/sbml-modeling-practice-constraints/");
validationDirectories.push_back(prefix + "/validator/test/test-data/sbml-notes-constraints/");
validationDirectories.push_back(prefix + "/validator/test/test-data/sbml-sbo-constraints/");
validationDirectories.push_back(prefix + "/validator/test/test-data/sbml-unit-constraints/");
validationDirectories.push_back(prefix + "/validator/test/test-data/sbml-xml-constraints/");
validationDirectories.push_back(prefix + "/validator/test/test-data/xml-parser-constraints/");
compValidationDirectories.push_back(prefix + "/packages/comp/validator/test/test-data/general-constraints/");
compValidationDirectories.push_back(prefix + "/packages/comp/validator/test/test-data/identifier-constraints/");
compValidationDirectories.push_back(prefix + "/packages/comp/validator/test/test-data/units-constraints/");
fbcValidationDirectories.push_back(prefix + "/packages/fbc/validator/test/test-data/general-constraints/");
fbcValidationDirectories.push_back(prefix + "/packages/fbc/validator/test/test-data/identifier-constraints/");
layoutValidationDirectories.push_back(prefix + "/packages/layout/validator/test/test-data/general-constraints/");
layoutValidationDirectories.push_back(prefix + "/packages/layout/validator/test/test-data/identifier-constraints/");
qualValidationDirectories.push_back(prefix + "/packages/qual/validator/test/test-data/general-constraints/");
qualValidationDirectories.push_back(prefix + "/packages/qual/validator/test/test-data/identifier-constraints/");
qualValidationDirectories.push_back(prefix + "/packages/qual/validator/test/test-data/math-constraints/");
groupsValidationDirectories.push_back(prefix + "/packages/groups/validator/test/test-data/general-constraints/");
groupsValidationDirectories.push_back(prefix + "/packages/groups/validator/test/test-data/identifier-constraints/");
multiValidationDirectories.push_back(prefix + "/packages/multi/validator/test/test-data/general-constraints/");
multiValidationDirectories.push_back(prefix + "/packages/multi/validator/test/test-data/identifier-constraints/");
multiValidationDirectories.push_back(prefix + "/packages/multi/validator/test/test-data/mathml-constraints/");
cout << endl;
cout << "Syntactic Test Suite Creation" << endl;
cout << "=============================" << endl;
cout << endl;
string reportfile = outdir + "/summary.csv";
if (fullreport) {
reportfile = outdir + "/summary-full.csv";
}
ofstream report(reportfile.c_str());
report << "Filename,";
if (fullreport) {
report << "Original directory,";
report << "Original filename,";
}
report << "Validation number,";
report << "Num Failures,";
report << "ID,";
report << "Other error,";
report << "Pass/fail,";
report << "Package,";
report << "Error/Warning,";
if (fullreport) {
report << "Copy/no copy,";
}
report << "Validation error/warning list" << endl;
vector<SBMLError*> uniqueErrors;
set<unsigned int> uniqueErrorIDs;
if (parseDirectories(validationDirectories, outdir, "", report, uniqueErrors, uniqueErrorIDs, fullreport)) return 1;
if (parseDirectories(compValidationDirectories, outdir, "comp", report, uniqueErrors, uniqueErrorIDs, fullreport)) return 1;
if (parseDirectories(fbcValidationDirectories, outdir, "fbc", report, uniqueErrors, uniqueErrorIDs, fullreport)) return 1;
if (parseDirectories(layoutValidationDirectories, outdir, "layout", report, uniqueErrors, uniqueErrorIDs, fullreport)) return 1;
if (parseDirectories(qualValidationDirectories, outdir, "qual", report, uniqueErrors, uniqueErrorIDs, fullreport)) return 1;
if (parseDirectories(groupsValidationDirectories, outdir, "groups", report, uniqueErrors, uniqueErrorIDs, fullreport)) return 1;
if (parseDirectories(multiValidationDirectories, outdir, "multi", report, uniqueErrors, uniqueErrorIDs, fullreport)) return 1;
if (fullreport) {
//Output the unique error messages, so we can read them:
ofstream cfile((outdir + "/uniqueErrors.txt").c_str());
if (!cfile.good()) {
cout << "Unable to open file " << outdir << "/uniqueErrors.txt for writing. Check that the directory exists, and if it does not, create it first." << endl;
return false;
}
for (size_t err=0; err<uniqueErrors.size(); err++) {
SBMLError* error = uniqueErrors[err];
cfile << "------------------" << endl;
cfile << "Validation id :\t" << TestFile::getConstraintIdString(error->getErrorId()) << endl;
cfile << "Validation number:\t" << error->getErrorId() << endl;
cfile << "Severity :\t" << error->getSeverityAsString() << endl;
cfile << "Line number :\t" << error->getLine() << endl;
//cfile << "Column number :\t" << error->getColumn() << endl;
cfile << "Package :\t" << error->getPackage() << endl;
cfile << "Short message :\t" << error->getShortMessage() << endl;
cfile << "Full message :\t" << error->getMessage() << endl;
delete error;
}
cfile << "------------------" << endl;
}
return 0;
}