1: /**
2: * Declares a custom validator to be called. This allows you to validate
3: * any aspect of an SBML Model that you want to be notified about. You could
4: * use this to notify your application that a model contains an unsupported
5: * feature of SBML (either as warning).
6: *
7: * In this example the validator will go through the model and test for the
8: * presence of 'fast' reactions and algebraic rules. If either is used a
9: * warning will be added to the error log.
10: */
11: class MyCustomValidator : public SBMLValidator
12: {
13: public:
14: MyCustomValidator() : SBMLValidator() {}
15: MyCustomValidator(const MyCustomValidator& orig) : SBMLValidator(orig) {
16:
17: }
18: virtual ~MyCustomValidator() {}
19:
20: virtual SBMLValidator* clone() const { return new MyCustomValidator(*this); }
21:
22: virtual unsigned int validate() {
23: // if we don't have a model we don't apply this validator.
24: if (getDocument() == NULL || getModel() == NULL)
25: return 0;
26:
27: // if we have no rules and reactions we don't apply this validator either
28: if (getModel()->getNumReactions() == 0 && getModel()->getNumRules() == 0)
29: return 0;
30:
31: unsigned int numErrors = 0;
32: // test for algebraic rules
33: for (unsigned int i = 0; i < getModel()->getNumRules(); i++)
34: {
35: if (getModel()->getRule(i)->getTypeCode() == SBML_ALGEBRAIC_RULE) {
36:
37: getErrorLog()->add(SBMLError(99999, 3, 1,
38: "This model uses algebraic rules, however this application does not support them.",
39: 0, 0,
40: LIBSBML_SEV_WARNING, // or LIBSBML_SEV_ERROR if you want to stop
41: LIBSBML_CAT_SBML // or whatever category you prefer
42: ));
43:
44: numErrors++;
45: }
46: }
47:
48: // test for fast reactions
49: for (unsigned int i = 0; i < getModel()->getNumReactions(); i++)
50: {
51: // test whether value is set, and true
52: if (getModel()->getReaction(i)->isSetFast() &&
53: getModel()->getReaction(i)->getFast()) {
54:
55: getErrorLog()->add(SBMLError(99999, 3, 1,
56: "This model uses fast reactions, however this application does not support them.",
57: 0, 0,
58: LIBSBML_SEV_WARNING, // or LIBSBML_SEV_ERROR if you want to stop
59: LIBSBML_CAT_SBML // or whatever category you prefer
60: ));
61:
62: numErrors++;
63:
64: }
65: }
66:
67: return numErrors;
68: }
69:
70:
71: };
72:
73:
74: int
75: main (int argc, char *argv[])
76: {
77: if (argc != 2)
78: {
79: cout << endl << "Usage: addCustomValidator filename" << endl << endl;
80: return 1;
81: }
82:
83: const char* filename = argv[1];
84:
85: // read the file name
86: SBMLDocument* document = readSBML(filename);
87:
88: // add a custom validator
89: document->addValidator(new MyCustomValidator());
90:
91: // check consistency like before
92: int numErrors = document->checkConsistency();
93:
94: // print errors and warnings
95: document->printErrors();
96:
97: // return number of errors
98: return numErrors;
99:
100: }