1#!/usr/bin/env perl 2# -*-Perl-*- 3## 4## @file addCustomValidator.pl 5## @brief Example creating a custom validator to be called during validation 6## @author Frank T. Bergmann 7## 8## <!-------------------------------------------------------------------------- 9## This sample program is distributed under a different license than the rest 10## of libSBML. This program uses the open-source MIT license, as follows: 11## 12## Copyright (c) 2013-2018 by the California Institute of Technology 13## (California, USA), the European Bioinformatics Institute (EMBL-EBI, UK) 14## and the University of Heidelberg (Germany), with support from the National 15## Institutes of Health (USA) under grant R01GM070923. All rights reserved. 16## 17## Permission is hereby granted, free of charge, to any person obtaining a 18## copy of this software and associated documentation files (the "Software"), 19## to deal in the Software without restriction, including without limitation 20## the rights to use, copy, modify, merge, publish, distribute, sublicense, 21## and/or sell copies of the Software, and to permit persons to whom the 22## Software is furnished to do so, subject to the following conditions: 23## 24## The above copyright notice and this permission notice shall be included in 25## all copies or substantial portions of the Software. 26## 27## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 30## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 31## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 32## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 33## DEALINGS IN THE SOFTWARE. 34## 35## Neither the name of the California Institute of Technology (Caltech), nor 36## of the European Bioinformatics Institute (EMBL-EBI), nor of the University 37## of Heidelberg, nor the names of any contributors, may be used to endorse 38## or promote products derived from this software without specific prior 39## written permission. 40## ------------------------------------------------------------------------ --> 41## 42## NOTE: This example is currently not working as SWIG does not allow for perl 43## directos yet ... otherwise it would be working as described below. 44## 45 46use LibSBML; 47no strict; 48## 49## Declares a custom validator to be called. This allows you to validate 50## any aspect of an SBML Model that you want to be notified about. You could 51## use this to notify your application that a model contains an unsupported 52## feature of SBML (either as warning). 53## 54## In this example the validator will go through the model and test for the 55## presence of 'fast' reactions and algebraic rules. If either is used a 56## warning will be added to the error log. 57## 58{ 59package MyCustomValidator; 60 @ISA = (LibSBML::SBMLValidator); 61 sub new { 62 my $class = shift; 63 bless \$class => $class 64 } 65 sub clone { 66 return new MyCustomValidator() 67 } 68 sub validate{ 69 my $self = shift; 70 # if we don't have a model we don't apply this validator. 71 if ($self->getDocument() == undef or $self->getModel() == undef) { 72 return 0; 73 } 74 75 # if we have no rules and reactions we don't apply this validator either 76 if ($self->getModel()->getNumReactions() == 0 and $self->getModel()->getNumRules() == 0) { 77 return 0; 78 } 79 80 $numErrors = 0; 81 # test for algebraic rules 82 for ($i=0; $i< $self->getModel()->getNumRules();$i++) { 83 if ($self->getModel()->getRule($i)->getTypeCode() == $LibSBML::SBML_ALGEBRAIC_RULE) { 84 $self->getErrorLog()->add(new LibSBML::SBMLError(99999, 3, 1, 85 "This model uses algebraic rules, however this application does not support them.", 86 0, 0, 87 $LibSBML::LIBSBML_SEV_WARNING, # or LIBSBML_SEV_ERROR if you want to stop 88 $LibSBML::LIBSBML_CAT_SBML # or whatever category you prefer 89 )); 90 $numErrors = $numErrors + 1; 91 } 92 } 93 94 # test for fast reactions 95 for ($i=0; $i < $self->getModel()->getNumReactions(); $i++) { 96 # test whether value is set, and true 97 if ($self->getModel()->getReaction($i)->isSetFast() and $self->getModel()->getReaction($i)->getFast()) { 98 self.getErrorLog().add(new LibSBML::SBMLError(99999, 3, 1, 99 "This model uses fast reactions, however this application does not support them.", 100 0, 0, 101 $LibSBML::LIBSBML_SEV_WARNING, # or LIBSBML_SEV_ERROR if you want to stop 102 $LibSBML::LIBSBML_CAT_SBML # or whatever category you prefer 103 )); 104 $numErrors = $numErrors + 1; 105 } 106 } 107 return numErrors; 108 } 109} 110 111if ($#ARGV != 0) { 112 print "Usage: addCustomValidator filename\n"; 113 exit 1; 114} 115 116 117# read the file name 118$document = LibSBML::readSBML($ARGV[0]); 119 120# add a custom validator 121$document->addValidator(new MyCustomValidator()); 122 123# check consistency like before 124$numErrors = $document->checkConsistency(); 125 126# print errors and warnings 127$document->printErrors(); 128 129# return number of errors 130exit $numErrors; 131 132