.. _text2operation-py:
######################
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'''
...
...
... thinning
...
...
... Calculate thinning limits
... Forced low thinning
... Update Tree after forced thinning
...
...
...
...
... first_thinning
...
...
... Calculate thinning limits
...
...
... '''
>>> 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'''
... comp_unit
... 0
...
... 0
...
...
... 2
...
...
... 1
... harvest
...
...
... 2
... silviculture
...
...
...
...
... 3
... 99
...
...
...
... 1
... 2
... first_thinning
...
...
... 1
... 21
... first_thinning
...
...
... TARGET_N
... comp_unit
... 500
...
...
... POINTLESS
... comp_unit
... -10
...
...
...
...
... 2
... 970
... pruning
...
...
...
...
... 5
...
... year
...
...
... 1
... 2
...
...
... 2
... 7
...
...
...
...
...
...
...
... Uudistuspuulaji
... REGEN_SP
...
... comp_unit
... 13
... int
...
...
...
...
... 81 82
... 1
...
...
... 91
... 2
...
...
...
...
...
... '''
>>> 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 # doctest: +NORMALIZE_WHITESPACE
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'>> xml2 = u'''
... comp_unit
... 0
...
... 0 1
... -
...
...
... 3
... 99
...
...
... 2
... first_thinning
...
...
...
...
... 5
...
...
... '''
>>> 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}