1#!/usr/bin/env python
2##
3## @file    print_sedml.py
4## @brief   Prints an overview of the elments in the given SED-ML document
5## @author  Frank T. Bergmann
6##
7## <!--------------------------------------------------------------------------
8## This file is part of libSEDML.  Please visit http://sed-ml.org for more
9## information about SEDML, and the latest version of libSEDML.
10##
11## Copyright (c) 2013, Frank T. Bergmann
12## All rights reserved.
13##
14## Redistribution and use in source and binary forms, with or without
15## modification, are permitted provided that the following conditions are met:
16##
17## 1. Redistributions of source code must retain the above copyright notice, this
18##    list of conditions and the following disclaimer.
19## 2. Redistributions in binary form must reproduce the above copyright notice,
20##    this list of conditions and the following disclaimer in the documentation
21##    and/or other materials provided with the distribution.
22##
23## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24## ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28## (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32## SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33## ------------------------------------------------------------------------ -.
34##
35
36from __future__ import print_function
37import sys
38import os.path
39import libsedml
40
41
42def print_change(change):
43  # type: (libsedml.SedSetValue) -> None
44  print("\t\tChange model=", change.getModelReference(), " target=", change.getTarget(),
45        " range=", change.getRange(),
46        " math=", libsedml.formulaToString(change.getMath()), "\n")
47
48def print_repeated_task(current):
49  # type: (libsedml.SedRepeatedTask) -> None
50  print ("\tRepeatedTask id=" , current.getId() , " range=" , current.getRangeId() , " resetModel=" , current.getResetModel() , "\n")
51  for i in range(current.getNumRanges()):
52    print("\t\tRange: id=",current.getRange(i).getId())
53
54  for i in range(current.getNumTaskChanges()):
55    print_change(current.getTaskChange(i))
56
57  for i in range(current.getNumSubTasks()):
58    print("\t\tSubtask order=",current.getSubTask(i).getOrder(),
59          " task=", current.getSubTask(i).getTask(), "\n")
60
61def main (args):
62  """Usage: print_sedml input-filename
63  """
64  if len(args) != 2:
65    print(main.__doc__)
66    sys.exit(1)
67
68  doc = libsedml.readSedML(args[1])
69  if doc.getErrorLog().getNumFailsWithSeverity(libsedml.LIBSEDML_SEV_ERROR) > 0:
70    print (doc.getErrorLog().toString())
71    sys.exit(2)
72
73  print ('The document has {0}" simulation(s).'.format(doc.getNumSimulations()))
74  for i  in range(0, doc.getNumSimulations()):
75    current = doc.getSimulation(i)
76    if current.getTypeCode() == libsedml.SEDML_SIMULATION_UNIFORMTIMECOURSE:
77      tc = current
78      kisaoid="none"
79      if tc.isSetAlgorithm():
80        kisaoid=tc.getAlgorithm().getKisaoID()
81      print ("\tTimecourse id=", tc.getId()," start=",tc.getOutputStartTime()," end=",tc.getOutputEndTime()," numPoints=",tc.getNumberOfPoints()," kisao=",kisaoid,"\n")
82    elif current.getTypeCode() == libsedml.SEDML_SIMULATION_STEADYSTATE:
83      print ("\tSteadyState id=", current.getId(),"\n")
84    elif current.getTypeCode() == libsedml.SEDML_SIMULATION_ONESTEP:
85      print ("\tOneStep id=", current.getId(),"\n")
86    elif current.getTypeCode() == libsedml.SEDML_SIMULATION_ANALYSIS:
87      print ("\tAnalysis id=", current.getId(),"\n")
88    else:
89      print ("\tUncountered unknown simulation. ",current.getId(),"\n")
90
91  print ("\n")
92  print ("The document has ", doc.getNumModels() , " model(s)." , "\n")
93  for i in range(0,doc.getNumModels()):
94    current = doc.getModel(i)
95    print ("\tModel id=" , current.getId() , " language=" , current.getLanguage() , " source=" , current.getSource() , " numChanges=" , current.getNumChanges() , "\n")
96
97  print ("\n")
98  print ("The document has %d task(s)." % doc.getNumTasks())
99  for i in range(0,doc.getNumTasks()):
100    current = doc.getTask(i)
101    if current.getTypeCode() == libsedml.SEDML_TASK_REPEATEDTASK:
102      print_repeated_task(current)
103    else:
104      print ("\tTask id=" , current.getId() , " model=" , current.getModelReference() , " sim=" , current.getSimulationReference() , "\n")
105
106  print ("\n")
107  print ("The document has " , doc.getNumDataGenerators() , " datagenerators(s)." , "\n")
108  for i in range( 0,  doc.getNumDataGenerators()):
109    current = doc.getDataGenerator(i)
110    print ("\tDG id=" , current.getId() , " math=" , libsedml.formulaToString(current.getMath()) , "\n")
111
112  print ("\n")
113  print ("The document has " , doc.getNumOutputs() , " output(s)." , "\n")
114  for i in range (0, doc.getNumOutputs()):
115    current = doc.getOutput(i)
116    tc = current.getTypeCode()
117    if tc == libsedml.SEDML_OUTPUT_REPORT:
118      r = current
119      print ("\tReport id=" , current.getId() , " numDataSets=" , r.getNumDataSets() , "\n")
120    elif tc == libsedml.SEDML_OUTPUT_PLOT2D:
121      p = current
122      print ("\tPlot2d id=" , current.getId() , " numCurves=" , p.getNumCurves() , "\n")
123    elif tc == libsedml.SEDML_OUTPUT_PLOT3D:
124      p = current
125      print ("\tPlot3d id=" , current.getId() , " numSurfaces=" , p.getNumSurfaces() , "\n")
126    else:
127      print ("\tEncountered unknown output " , current.getId() , "\n")
128
129if __name__ == '__main__':
130  main(sys.argv)
131