def test_XMLOutputStream_startEnd(self):
   oss = libsbml.ostringstream()
   stream = libsbml.XMLOutputStream(oss,"",False)
   self.assert_( stream != None )
   stream.startEndElement( "id")
   str = oss.str()
   self.assert_((  "<id/>" == str ))
   _dummyList = [ stream ]; _dummyList[:] = []; del _dummyList
   pass  
 def test_XMLOutputStream_createStringWithProgramInfo(self):
   expected =  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
   oss = libsbml.ostringstream()
   stream = libsbml.XMLOutputStream(oss,"UTF-8",True, "", "")
   self.assert_( stream != None )
   str = oss.str()
   self.assert_(( expected == str ))
   _dummyList = [ stream ]; _dummyList[:] = []; del _dummyList
   pass  
 def test_XMLOutputStream_PredefinedEntity(self):
   oss = libsbml.ostringstream()
   stream = libsbml.XMLOutputStream(oss,"",False)
   stream.startElement( "testpde")
   stream.writeAttribute( "amp",     "&"     )
   stream.writeAttribute( "apos",    "'"     )
   stream.writeAttribute( "gt",      ">"     )
   stream.writeAttribute( "lt",      "<"     )
   stream.writeAttribute( "quot",    "\""    )
   stream.writeAttribute( "pdeamp",  "&amp;" )
   stream.writeAttribute( "pdeapos", "&apos;")
   stream.writeAttribute( "pdegt",   "&gt;"  )
   stream.writeAttribute( "pdelt",   "&lt;"  )
   stream.writeAttribute( "pdequot", "&quot;")
   stream.endElement( "testpde")
   expected =  "<testpde amp=\"&amp;\" apos=\"&apos;\" gt=\"&gt;\" lt=\"&lt;\" quot=\"&quot;\" pdeamp=\"&amp;\" pdeapos=\"&apos;\" pdegt=\"&gt;\" pdelt=\"&lt;\" pdequot=\"&quot;\"/>";
   s = oss.str()
   self.assert_(( expected == s ))
   _dummyList = [ stream ]; _dummyList[:] = []; del _dummyList
   pass  
 def test_XMLOutputStream_Elements(self):
   d = 2.4
   l = 123456789
   ui = 5
   i = -3
   oss = libsbml.ostringstream()
   stream = libsbml.XMLOutputStream(oss,"",False)
   stream.startElement( "fred")
   stream.writeAttribute( "chars", "two")
   stream.writeAttributeBool( "bool",True)
   stream.writeAttribute( "double",d)
   stream.writeAttribute( "long",l)
   stream.writeAttribute( "uint",ui)
   stream.writeAttribute( "int",i)
   stream.endElement( "fred")
   expected =  "<fred chars=\"two\" bool=\"true\" double=\"2.4\" long=\"123456789\" uint=\"5\" int=\"-3\"/>";
   s = oss.str()
   self.assert_(( expected == s ))
   _dummyList = [ stream ]; _dummyList[:] = []; del _dummyList
   pass  
 def test_XMLOutputStream_CharacterReference(self):
   oss = libsbml.ostringstream()
   stream = libsbml.XMLOutputStream(oss,"",False)
   stream.startElement( "testcr")
   stream.writeAttribute( "chars",    "one"     )
   stream.writeAttribute( "amp",      "&"       )
   stream.writeAttribute( "deccr",    "&#0168;"  )
   stream.writeAttribute( "hexcr",    "&#x00a8;")
   stream.writeAttribute( "lhexcr",   "&#x00A8;")
   stream.writeAttribute( "nodeccr1", "&#01688"  )
   stream.writeAttribute( "nodeccr2", "&#;"     )
   stream.writeAttribute( "nodeccr3", "&#00a8;" )
   stream.writeAttribute( "nodeccr4", "&#00A8;" )
   stream.writeAttribute( "nohexcr1", "&#x;"    )
   stream.writeAttribute( "nohexcr2", "&#xABCD" )
   stream.endElement( "testcr")
   expected =  "<testcr chars=\"one\" amp=\"&amp;\" deccr=\"&#0168;\" hexcr=\"&#x00a8;\" lhexcr=\"&#x00A8;\" nodeccr1=\"&amp;#01688\" nodeccr2=\"&amp;#;\" nodeccr3=\"&amp;#00a8;\" nodeccr4=\"&amp;#00A8;\" nohexcr1=\"&amp;#x;\" nohexcr2=\"&amp;#xABCD\"/>";
   s = oss.str()
   self.assert_(( expected == s ))
   _dummyList = [ stream ]; _dummyList[:] = []; del _dummyList
   pass  
Example #6
0
    def validate(self, file):
        if not os.path.exists(file):
            print("[Error] %s : No such file." % (infile))
            self.numinvalid += 1
            return

        start = time.time()
        sbmlDoc = libsbml.readSBML(file)
        stop = time.time()
        timeRead = (stop - start) * 1000
        errors = sbmlDoc.getNumErrors()

        seriousErrors = False

        numReadErr = 0
        numReadWarn = 0
        errMsgRead = ""

        if errors > 0:

            for i in range(errors):
                severity = sbmlDoc.getError(i).getSeverity()
                if (severity == libsbml.LIBSBML_SEV_ERROR) or (
                        severity == libsbml.LIBSBML_SEV_FATAL):
                    seriousErrors = True
                    numReadErr += 1
                else:
                    numReadWarn += 1

                oss = libsbml.ostringstream()
                sbmlDoc.printErrors(oss)
                errMsgRead = oss.str()

        # If serious errors are encountered while reading an SBML document, it
        # does not make sense to go on and do full consistency checking because
        # the model may be nonsense in the first place.

        numCCErr = 0
        numCCWarn = 0
        errMsgCC = ""
        skipCC = False
        timeCC = 0.0

        if seriousErrors:
            skipCC = True
            errMsgRead += "Further consistency checking and validation aborted."
            self.numinvalid += 1
        else:
            sbmlDoc.setConsistencyChecks(libsbml.LIBSBML_CAT_UNITS_CONSISTENCY,
                                         self.ucheck)
            start = time.time()
            failures = sbmlDoc.checkConsistency()
            stop = time.time()
            timeCC = (stop - start) * 1000

            if failures > 0:

                isinvalid = False
                for i in range(failures):
                    severity = sbmlDoc.getError(i).getSeverity()
                    if (severity == libsbml.LIBSBML_SEV_ERROR) or (
                            severity == libsbml.LIBSBML_SEV_FATAL):
                        numCCErr += 1
                        isinvalid = True
                    else:
                        numCCWarn += 1

                if isinvalid:
                    self.numinvalid += 1

                oss = libsbml.ostringstream()
                sbmlDoc.printErrors(oss)
                errMsgCC = oss.str()

        #
        # print results
        #

        print("                 filename : %s" % (file))
        print("         file size (byte) : %d" % (os.path.getsize(file)))
        print("           read time (ms) : %f" % (timeRead))

        if not skipCC:
            print("        c-check time (ms) : %f" % (timeCC))
        else:
            print("        c-check time (ms) : skipped")

        print("      validation error(s) : %d" % (numReadErr + numCCErr))
        if not skipCC:
            print("    (consistency error(s)): %d" % (numCCErr))
        else:
            print("    (consistency error(s)): skipped")

        print("    validation warning(s) : %d" % (numReadWarn + numCCWarn))
        if not skipCC:
            print("  (consistency warning(s)): %d" % (numCCWarn))
        else:
            print("  (consistency warning(s)): skipped")

        if errMsgRead or errMsgCC:
            print()
            print("===== validation error/warning messages =====\n")
            if errMsgRead:
                print(errMsgRead)
            if errMsgCC:
                print("*** consistency check ***\n")
                print(errMsgCC)
  def validate(self, file):
    if not os.path.exists(file):
      print "[Error] %s : No such file." % (infile)
      self.numinvalid += 1
      return

    start    = time.time()
    sbmlDoc  = libsbml.readSBML(file)
    stop     = time.time()
    timeRead = (stop - start)*1000
    errors   = sbmlDoc.getNumErrors()
    
    seriousErrors = False

    numReadErr  = 0
    numReadWarn = 0
    errMsgRead  = ""

    if errors > 0:
      
      for i in range(errors):
        severity = sbmlDoc.getError(i).getSeverity()
        if (severity == libsbml.LIBSBML_SEV_ERROR) or (severity == libsbml.LIBSBML_SEV_FATAL):
          seriousErrors = True
          numReadErr += 1
        else:
          numReadWarn += 1

        oss = libsbml.ostringstream()
        sbmlDoc.printErrors(oss)
        errMsgRead = oss.str()

    # If serious errors are encountered while reading an SBML document, it
    # does not make sense to go on and do full consistency checking because
    # the model may be nonsense in the first place.

    numCCErr  = 0
    numCCWarn = 0
    errMsgCC  = ""
    skipCC    = False;
    timeCC    = 0.0

    if seriousErrors:
      skipCC = True;
      errMsgRead += "Further consistency checking and validation aborted."
      self.numinvalid += 1;    
    else:
      sbmlDoc.setConsistencyChecks(libsbml.LIBSBML_CAT_UNITS_CONSISTENCY, self.ucheck)
      start    = time.time()
      failures = sbmlDoc.checkConsistency()
      stop     = time.time()
      timeCC   = (stop - start)*1000
  
 
      if failures > 0:

        isinvalid = False;
        for i in range(failures):
          severity = sbmlDoc.getError(i).getSeverity()
          if (severity == libsbml.LIBSBML_SEV_ERROR) or (severity == libsbml.LIBSBML_SEV_FATAL):
            numCCErr += 1
            isinvalid = True;
          else:
            numCCWarn += 1

        if isinvalid:
          self.numinvalid += 1;    

        oss = libsbml.ostringstream()
        sbmlDoc.printErrors(oss)
        errMsgCC = oss.str()

    #
    # print results
    #
        
    print "                 filename : %s" % (file)
    print "         file size (byte) : %d" % (os.path.getsize(file))
    print "           read time (ms) : %f" % (timeRead)

    if not skipCC :
      print "        c-check time (ms) : %f" % (timeCC)
    else:
      print "        c-check time (ms) : skipped"

    print "      validation error(s) : %d" % (numReadErr  + numCCErr)
    if not skipCC :
      print "    (consistency error(s)): %d" % (numCCErr)
    else:
      print "    (consistency error(s)): skipped"

    print "    validation warning(s) : %d" % (numReadWarn + numCCWarn)
    if not skipCC :
      print "  (consistency warning(s)): %d" % (numCCWarn)
    else:
      print "  (consistency warning(s)): skipped"