.. _outputconstraint-py:
###################
outputconstraint.py
###################
*************************************
class OutputConstraintDef(XmlObject):
*************************************
def __init__(self, typedef):
============================
Initializes the output constraint by processing the schema and xml documents::
>>> from simo.builder.output.outputconstraint import \
... OutputConstraintDef
>>> tdf = open('../../simulator/xml/schemas/Typedefs_SIMO.xsd')
>>> typedef = tdf.read()
>>> tdf.close()
>>> sf = open('../../simulator/xml/schemas/output_constraint.xsd')
>>> schema = sf.read()
>>> sf.close()
>>> xml = u'''
...
... comp_unit
... AREA SC
...
...
... stratum
... SP Age BA some_exotic_variable
...
... '''
>>> passingxml = u'''
...
... comp_unit
... AREA SC
...
...
... stratum
... SP Age BA
...
...
... tree
... *
...
... '''
>>> class Lexicon(object):
... def get_level_ind(self, level):
... levels = {'comp_unit':1, 'stratum':2, 'tree':3}
... return levels[level]
... def get_variable_ind(self, level, var, check_active=False):
... #NB! removed while outputconstrains are inactive
... #if check_active:
... if True:
... if var=='some_exotic_variable':
... return (None, None)
... else:
... return (1, 1)
>>> ocd = OutputConstraintDef(typedef)
>>> ocd.schema = schema
>>> try:
... ocd.xml = ('testxml', xml, Lexicon())
... except ValueError, e:
... print e
errors in xml to object conversion
>>> ocd.errors # doctest: +NORMALIZE_WHITESPACE
set(["Variable 'some_exotic_variable' not found at level 'stratum' in
lexicon for output constraint 'testxml'"])
>>> # NB! This is the error which we should get if the checking for
>>> # active variables is used
>>> #set(["Variable 'some_exotic_variable' not found at level 'stratum' in
>>> # lexicon or it's not set to be computed during simulation for output
>>> # constraint 'testxml'"])
>>> ocd.xml = ('passingxml', passingxml, Lexicon())
def xml_to_obj(self, root, lexicon):
====================================
::
>>> oc = ocd.obj['passingxml']
>>> oc.variables[('comp_unit', 1)]
[('AREA', 1), ('SC', 1)]
>>> oc.variables[('stratum', 2)]
[('SP', 1), ('Age', 1), ('BA', 1)]
>>> oc.variables[('tree', 3)] is None
True
>>> ocd.errors
set([])
>>> ocd.warnings
[]