Table Of Contents

Previous topic

geotable.py

Next topic

model.py

This Page

managementmodel.py

>>> from simo.builder.modelbase.modelbase import ModelbaseDef
>>> tdf = open('../../simulator/xml/schemas/Typedefs_SIMO.xsd')
>>> typedef = tdf.read()
>>> tdf.close()
>>> sf = open('../../simulator/xml/schemas/management_modelbase.xsd')
>>> schema = sf.read()
>>> sf.close()
>>> xml = u'''<management_base
...   xmlns="http://www.simo-project.org/simo"
...   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...   <model>
...      <name>group_objects_by_variable</name>
...      <description>desc</description>
...      <implemented_at>simomanagement.py</implemented_at>
...      <parameters>
...         <parameter>
...            <type>level</type>
...            <description>target level</description>
...         </parameter>
...         <parameter>
...            <type>level</type>
...            <description>child level</description>
...         </parameter>
...         <parameter>
...            <type>variable</type>
...            <description>grouping variable</description>
...         </parameter>
...         <parameter>
...            <type>value</type>
...            <description>0 or 1</description>
...          </parameter>
...      </parameters>
...   </model>
... </management_base>'''
>>> class Lexicon(object):
...     def __init__(self):
...         self.models = {}
...     def get_variable_ind(self, level, variable, active=False):
...         if variable == 'GEO_X':
...             return (1,1)
...         elif variable == 'GEO_Y':
...             return (1,2)
...         elif variable == 'ALT':
...             return (1,3)
...         elif variable == 'TS':
...             return (1,4)
...     def get_level_ind(self, level):
...         return 1
...     def add_model(self, mtype, mname):
...         if mtype not in self.models:
...             self.models[mtype] = set()
...         self.models[mtype].add(mname)
>>> mb = ModelbaseDef(typedef)
>>> mb.schema = ('management', schema)
>>> mb.xml = ('testxml', xml, Lexicon(), '', 'management')
>>> mb.xml['management']['testxml'][:16]
u'<management_base'

class ManagementModel(object):

Management model

Attributes:

-name -at: the model library name in which the model implementation is found -paramenters: a list of parameter definitions, list items are dictionaries with keys ‘type’ and ‘desc’. Type is either level, variable or value.

def __init__(self, ns, elem):

Parses the XML data into model instance attributes:

>>> mm = mb.obj['management']['testxml']['group_objects_by_variable']
>>> mm.name
'group_objects_by_variable'
>>> mm.language
'python'
>>> mm.parameters
[{'type': 'level', 'desc': 'target level'}, {'type': 'level', 'desc': 'child level'}, {'type': 'variable', 'desc': 'grouping variable'}, {'type': 'value', 'desc': '0 or 1'}]

class ModelParamManagementModel(object):

Parameter values for management model, defined in modelchain.

def __init__(self, ns, elem, lexicon, model):

Too few parameter values defined in model chain:

>>> execfile('builder/modelbase/test/mocktask.py')
>>> from simo.builder.modelbase.managementmodel import ManagementModelParam
>>> from lxml import etree
>>> pelem = etree.XML('''<management>
...                        <parameters>
...                           <parameter>
...                              <level>stratum</level>
...                           </parameter>
...                        </parameters>
...                      </management>''')
>>> param = ManagementModelParam('', pelem , task, mm)
>>> task.validator.errors 
set(["wrong number of parameters defined for management model
     'group_objects_by_variable' in task 'test task'"])

Wrong type of parameters defined in model chain:

>>> pelem = etree.XML('''<management>
...                        <parameters>
...                           <parameter>
...                              <level>stratum</level>
...                           </parameter>
...                           <parameter>
...                              <variable>
...                                 <name>SP</name>
...                                 <level>stratum</level>
...                              </variable>
...                           </parameter>
...                           <parameter>
...                              <level>stratum</level>
...                           </parameter>
...                           <parameter>
...                              <value>1</value>
...                           </parameter>
...                        </parameters>
...                     </management>''')
>>> param = ManagementModelParam('', pelem , task, mm)
>>> task.validator.errors 
set(["wrong number of parameters defined for management model
         'group_objects_by_variable' in task 'test task'",
     "parameter at position 1 should be of type 'level' instead of
          'variable' for management model 'group_objects_by_variable'
          in task 'test task'",
     "parameter at position 2 should be of type 'variable' instead of
         'level' for management model 'group_objects_by_variable' in
         task 'test task'"])