Table Of Contents

Previous topic

text2data.py

Next topic

lexicon.py

This Page

operationconversion.py

class OperationConversionDef(XmlObject):

def __init__(self, typedef, schema, xmldata):

Initializes the output constraint by processing the schema and xml documents:

>>> from simo.builder.importers.operation2modelchains import \
...     OperationMappingDef
>>> tdf = open('../../simulator/xml/schemas/Typedefs_SIMO.xsd')
>>> typedef = tdf.read()
>>> tdf.close()
>>> sf = open('../../simulator/xml/schemas/operation2modelchains.xsd')
>>> schema = sf.read()
>>> sf.close()
>>> xml = u'''<SIMO_operation_mapping
... xmlns="http://www.simo-project.org/simo"
... xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
... xsi:schemaLocation="http://www.simo-project.org/simo
... ../schemas/operation2modelchains.xsd">
... <operation>
...     <from>
...         <name>thinning</name>
...     </from>
...     <to>
...         <model_chain>Calculate thinning limits</model_chain>
...         <model_chain>Forced low thinning</model_chain>
...         <model_chain>Update Tree after forced thinning</model_chain>
...     </to>
... </operation>
... <operation>
...     <from>
...         <name>first_thinning</name>
...     </from>
...     <to>
...         <model_chain>Calculate thinning limits</model_chain>
...     </to>
... </operation>
... </SIMO_operation_mapping>'''
>>> class Lexicon(object):
...     def get_level_ind(self, level):
...         return 1
...     def get_variable_ind(self, level, var, active=False):
...         return (None, None)
>>> omd = OperationMappingDef(typedef)
>>> omd.schema = schema
>>> try:
...     omd.xml = ('testxml', xml, Lexicon())
... except ValueError, e:
...     print e
>>> om = omd.obj['testxml']
>>> from simo.builder.importers.text2operation import OperationConversionDef
>>> tdf = open('../../simulator/xml/schemas/Typedefs_SIMO.xsd')
>>> typedef = tdf.read()
>>> tdf.close()
>>> sf = open('../../simulator/xml/schemas/text2operation.xsd')
>>> schema = sf.read()
>>> sf.close()
>>> xml = u'''<operation_conversion
... xmlns="http://www.simo-project.org/simo"
... xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
... xsi:schemaLocation="http://www.simo-project.org/simo
... ../schemas/operation_conversion.xsd">
... <operation_level>comp_unit</operation_level>
... <none_value_indicator>0</none_value_indicator>
... <id_row_position>
...    <pos>0</pos>
... </id_row_position>
... <operation_type>
...     <row_position>2</row_position>
...     <mapping>
...         <type>
...             <from>1</from>
...             <to>harvest</to>
...         </type>
...         <type>
...             <from>2</from>
...             <to>silviculture</to>
...         </type>
...     </mapping>
... </operation_type>
... <operation_name>
...     <row_position>3</row_position>
...     <no_operation_value>99</no_operation_value>
...     <mapping>
...         <!-- Perushakkuut -->
...         <operation>
...             <type>1</type>
...             <from>2</from>
...             <to>first_thinning</to>
...         </operation>
...         <operation>
...             <type>1</type>
...             <from>21</from>
...             <to>first_thinning</to>
...             <parameters>
...                <parameter>
...                   <name>TARGET_N</name>
...                   <level>comp_unit</level>
...                   <value>500</value>
...                </parameter>
...                <parameter>
...                   <name>POINTLESS</name>
...                   <level>comp_unit</level>
...                   <value>-10</value>
...                </parameter>
...             </parameters>
...         </operation>
...         <operation>
...             <type>2</type>
...             <from>970</from>
...             <to>pruning</to>
...         </operation>
...     </mapping>
... </operation_name>
... <operation_timing>
...     <row_position>5</row_position>
...     <time_step>
...         <unit>year</unit>
...         <mapping>
...             <step>
...                 <from>1</from>
...                 <to>2</to>
...             </step>
...             <step>
...                 <from>2</from>
...                 <to>7</to>
...             </step>
...         </mapping>
...     </time_step>
... </operation_timing>
... <variable_mapping>
...    <variable>
...          <name>
...                  <from>Uudistuspuulaji</from>
...                  <to>REGEN_SP</to>
...          </name>
...          <level>comp_unit</level>
...          <row_position>13</row_position>
...          <from_datatype>int</from_datatype>
...          <none_to_value/>
...          <categorical>
...                  <value_mapping>
...                          <value>
...                                  <from>81 82</from>
...                                  <to>1</to>
...                          </value>
...                          <value>
...                                  <from>91</from>
...                                  <to>2</to>
...                          </value>
...                  </value_mapping>
...          </categorical>
...    </variable>
... </variable_mapping>
... </operation_conversion>'''
>>> class Lexicon(object):
...     def get_level_ind(self, level):
...         return 1
...     def get_variable_ind(self, level, var, active=False):
...         if var=='POINTLESS':
...             return (None, None)
...         else:
...             return (1, 1)
>>> ocd = OperationConversionDef(typedef)
>>> ocd.schema = schema
>>> try:
...     ocd.xml = ('testxml', xml, Lexicon(), om)
... except ValueError, e:
...     print e
errors in xml to object conversion
>>> ocd.errors 
set(["Variable 'POINTLESS' not found at level 'comp_unit' in lexicon for
      operation conversion 'testxml'",
     "Operation 'pruning' not defined in operation mapping for operation
      conversion 'testxml'"])
>>> ocd.xml['testxml'][:21]
u'<operation_conversion'

Conversion definition with no operation type definitions and timing type of date:

>>> xml2 = u'''<operation_conversion
... xmlns="http://www.simo-project.org/simo"
... xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
... xsi:schemaLocation="http://www.simo-project.org/simo
... ../schemas/operation_conversion.xsd">
... <operation_level>comp_unit</operation_level>
... <none_value_indicator>0</none_value_indicator>
... <id_row_position>
...   <pos>0 1</pos>
...   <delimiter>-</delimiter>
... </id_row_position>
... <operation_name>
...     <row_position>3</row_position>
...     <no_operation_value>99</no_operation_value>
...     <mapping>
...         <operation>
...             <from>2</from>
...             <to>first_thinning</to>
...         </operation>
...     </mapping>
... </operation_name>
... <operation_timing>
...     <row_position>5</row_position>
...     <date/>
... </operation_timing>
... </operation_conversion>'''
>>> try:
...     ocd.xml = ('testxml2', xml2, Lexicon(), om)
... except ValueError, e:
...     print e

def xml_to_obj(self, root, lexicon):

Note that the timing type of ‘date’ has no implementation yet:

>>> oc = ocd.obj['testxml']
>>> oc.validator
>>> oc.op2mc.operation_mapping['first_thinning']
['Calculate thinning limits']
>>> oc.operation_conv.operation_level
'comp_unit'
>>> oc.operation_conv.no_operation_indicator
['99']
>>> oc.operation_conv.none_value_indicator
['0']
>>> oc.operation_conv.comment_prefix
>>> oc.operation_conv.id_rowpos
[0]
>>> oc.operation_conv.type_rowpos
2
>>> oc.operation_conv.operation_rowpos
3
>>> oc.operation_conv.timing_rowpos
5
>>> t = oc.operation_conv.timing
>>> t.type
'step'
>>> t.step_conversion['2']
6
>>> t.step_unit
'year'
>>> oc.operation_conv.types[None]
>>> oc.operation_conv.types['1']
'harvest'
>>> oc.operation_conv.types['2']
'silviculture'
>>> keys = oc.operation_conv.operations.keys()
>>> for key in keys:
...    print 'Key:', key
...    print oc.operation_conv.operations[key].type
...    print oc.operation_conv.operations[key].from_name
...    print oc.operation_conv.operations[key].to_name
Key: ('1', '2')
harvest
2
first_thinning
Key: ('1', '21')
harvest
21
first_thinning
>>> params = oc.operation_conv.operations[('1', '21')].parameters
>>> len(params)
1
>>> params[0].value
500.0
>>> params[0].variable
'TARGET_N'
>>> params[0].level
'comp_unit'
>>> params[0].variable_ind
(1, 1)
>>> oc.operation_conv.timing.type
'step'
>>> oc.operation_conv.timing.step_conversion
{'1': 1, '2': 6}
>>> oc.operation_conv.timing.step_unit
'year'
>>> mapping = oc.operation_conv.variables[13]
>>> mapping.var_type
'categorical'
>>> vc = mapping.map[81][0]
>>> vc.level
'comp_unit'
>>> vc.to_var
'REGEN_SP'
>>> vc.to_val
1
>>> ocd.warnings
[]

The ‘no operation types’ and ‘date timing’ variant:

>>> oc = ocd.obj['testxml2']
>>> oc.operation_conv.id_rowpos
[0, 1]
>>> oc.operation_conv.id_delim
'-'
>>> t = oc.operation_conv.timing
>>> t.type
'date'
>>> oc.operation_conv.types
{None: None}