Python Shell


It's possible to write Python scripts and run them within ModelSphere. Because Python is not a proprietary language, anybody can use it and its documentation is available online at http://www.python.org/. Python scripts can be written to implement a feature not present in ModelSphere. For instance, Python scripts can produce HTML reports, write SQL code of an unsupported DBMS (database management system), export model elements to an external tool, compute and display statistics on ModelSphere models, and so on.


Python shell as a built-in calculator

Select the Python shell item in the Tools menu. After the python> prompt, type the following commands: You should get 10, 7 and 0.8333 in the output panel. The Python shell can be used as a built-in calculator!  You can also invoke Python statements. After the python> prompt, type the following commands:


A file named test.txt should have been created in the root directory of the application. Now create a new file named demo.py in a text editor like Notepad, and type the five lines you has entered in the Python shell.  Save and close the file. In ModelSphere Python shell, press the Execute file... button and select the demo.py file. The test.txt file is generated again.

  A file containing Python statements in called a Python script.  To know more about Python statements, consult its documentation at http://www.python.org/.
 

Python scripts as a model query language

 Of course, all the interest of Python scripts is in its ability to query a ModelSphere model.  Open the Python script called demo1.py (it is provided as an example with the application), you should have :

#
# import clauses
#
from org.modelsphere.jack.srtool import ApplicationContext
#
# script's entry point
#
name = ApplicationContext.APPLICATION_NAME
vers = ApplicationContext.APPLICATION_BUILD_ID
out_file = open("test.txt","w")
out_file.write("You are working on " + name + " build", vers)
out_file.close()

  The script writes the name and the build ID of the application in the test.txt file. The next example (demo2.py) outputs the name of the project in the test.txt file.

#
# import classes
#
from org.modelsphere.jack.srtool import ApplicationContext
from org.modelsphere.jack.baseDb.db import Db
from org.modelsphere.sms.templates import GenericMapping

#
# script's entry point
#
def main() :
 try:
  project = ApplicationContext.getFocusManager().getCurrentProject()
  db      = project.getDb()
  db.beginTrans(Db.READ_TRANS)
  metafield = GenericMapping.getMetaField("ProjectName")
  name = project.get(metafield)
  out_file = open("test.txt","w")
  out_file.write("Project name:")
  out_file.write(name)
  out_file.close()
 finally:
  db.commitTrans()

main()

   The API documentation (javadoc) of GenericMapping is provided with the application (see index.html under the doc\template-api\ directory).