.. _db-py_simo: #################### db.py #################### :: >>> from simo.db.simodb.test.test_classes import * >>> from simo.builder import names >>> from minimock import Mock >>> 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 ... ... ... ... ... first_thinning ... ... ... Calculate thinning limits ... Forced first thinning ... ... ... ... ... clearcut ... ... ... Forced clearcut ... ... ... ... ... strip_cutting ... ... ... Forced stripcut ... Calculate productive value ... ... ... ''' >>> sf = open('../../simulator/xml/schemas/aggregation_modelbase.xsd') >>> mbschema = sf.read() >>> sf.close() >>> mbxml = u''' ... ... sum ... desc... ... simoaggr.py ... true ... ... ''' >>> mcsf = open('../../simulator/xml/schemas/model_chain.xsd') >>> mcschema = mcsf.read() >>> mcsf.close() >>> mcxml = u''' ... ... ... ... ... Thinning_limits_JKK ... ... ... ... THINNING_LEVEL ... 0.5 ... ... ... ... ... ... ... ... ''' >>> from simo.builder.modelbase.predictionmodel import PredictionModel >>> from lxml import etree >>> xml2 = u''' ... ... Volume_BAHdomF ... StandModels.dll ... C ... ... Timo Pukkala ... ... ... ... ... ... ... 1 ... ... ... ... ... ... ... ... ... ... TS ... comp_unit ... ... ... ... ... THINNING_LEVEL ... ... ... ... self ... ... ... PVland_Sp ... ... ... ... ... ''' >>> class Validator: ... def elem_name(self, text): ... return text ... def variable_ind(self, level, variable, active=False): ... return (1,1) ... def add_model(self, mname, mtype): ... pass ... def add_error(self, text): ... print 'error:', text ... errors = [] ... warnings = [] >>> elem = etree.fromstring(xml2) >>> pr = PredictionModel('', elem[0], Validator(), ... '../../simulator/models/prediction', ... elem.attrib['name']) >>> pr.param_names ['THINNING_LEVEL'] >>> pr.param_limits [None] >>> pr.n_params 1 >>> mimpl = {names.PREDICTION: {'Thinning_limits_JKK': pr}} ********************* class SimoDB(object): ********************* def __init__(self, file, create_new=False, read_only=False): ============================================================ Initializes the database, creating or opening the necessary files :: >>> from simo.db.simodb.db import SimoDB >>> db = SimoDB('db/simodb/test/test.fs', True) >>> db.dbroot['test'] = 'test' >>> db.commit() >>> print db.dbroot['test'] test >>> db.close() >>> db = SimoDB('db/simodb/test/test.fs', False) >>> print db.dbroot['test'] test >>> db2 = SimoDB('db/simodb/test/test.fs', False, read_only=True) >>> db.close() >>> db2.close() >>> db2 = None >>> db = SimoDB('db/simodb/test/test2.fs', True) def set_schema(self, type, schema): =================================== Sets a new type to the db if not present and sets a schema for that type. Returns True if successful, False if not.:: >>> try: ... db.set_schema('operation2modelchains', schema) ... except ValueError, e: ... print e Typedef not set >>> db.typedef = typedef >>> db.set_schema('operation2modelchains', schema) >>> db.set_schema('modelbase', mbschema, 'aggregation') >>> db.set_schema('modelchain', mcschema) >>> try: ... db.set_schema('test', mcschema) ... except ValueError, e: ... print e unsupported SIMO class type 'test' def get_schema(self, simoclass, atype): ======================================= Returns the schema of the given type or None if the type is not available.:: >>> db.get_schema('operation2modelchains')==schema True >>> db.get_schema('modelbase', 'aggregation')==mbschema True >>> db.get_schema('modelchain')==mcschema True >>> db.get_schema('Something else') def set_xml(self, simoclass, xml, mtype): ========================================= Stores the xml to the database. This triggers object instance creation as well. The xml parameter is in general a tuple of (name, xml text, lexicon object instance). In case of model chain XML the tuple consists of (name, xml text, lexicon object instance, chain type, model impelementation). For modelbase XML the tuple consists of: (name, xml text, lexicon object instance, directory path, model type). The directory path refers to where the implementations of models are located. The method returns a tuple of (top level error, ok, warnings, errors). Raises an ValueError if the schema has not been set. :: >>> db.set_xml('operation2modelchains', ('testxml', xml, XMLLexicon())) (None, True, [], set([])) >>> db.set_xml('modelbase', ('testxml', mbxml, ModelLexicon(), '', ... 'aggregation')) (None, True, [], set([])) >>> db.set_xml('modelchain', ('testxml', mcxml, ChainLexicon(), 'simulation', ... mimpl)) (None, True, [], set([])) >>> db.set_xml('modelchain', ('testxml2', mcxml, ChainLexicon(), 'simulation', ... mimpl)) (None, True, [], set([])) def get_xml(self, simoclass, name): =================================== Returns the xmltext set to the given name in the given type in the database. Returns (None, None) if either the type or the name is not available :: >>> data = db.get_xml('operation2modelchains', 'testxml') >>> data[0][:23] u'>> data[1] True >>> data = db.get_xml('modelbase', 'testxml', 'aggregation') >>> data[0][:17] u'>> data[1] True >>> data = db.get_xml('modelchain', 'testxml', 'simulation') >>> data[0][:13] u'>> data[1] True >>> db.get_xml('something else', 'testxml') (None, None) def get_names(self, simoclass, atype): ====================================== Returns a list of the names of the xmls that have been saved to the database. Modelchain and modelbase need chain/model type definition as well:: >>> db.get_names('operation2modelchains') ['testxml'] >>> db.get_names('modelbase', 'aggregation') ['testxml'] >>> db.get_names('modelchain', 'simulation') ['testxml', 'testxml2'] >>> db.get_names('something else') def get_obj(self, simoclass, name): =================================== Returns an instance of the given type and name:: >>> ob = db.get_obj('operation2modelchains', 'testxml') >>> ob.operation_mapping['first_thinning'] # doctest: +NORMALIZE_WHITESPACE ['Calculate thinning limits', 'Forced first thinning'] >>> ob.forced_op_chains # doctest: +NORMALIZE_WHITESPACE set(['Calculate thinning limits', 'Forced stripcut', 'Forced low thinning', 'Forced clearcut', 'Calculate productive value', 'Forced first thinning']) >>> ob = db.get_obj('modelbase', 'testxml', 'aggregation') >>> print ob.keys() ['sum'] >>> mbo = ob['sum'] >>> mbo.type 'aggregation' >>> mbo.name 'sum' >>> mbo._lib 'simoaggr.py' >>> mbo.language 'python' >>> mbo.dirs '' >>> mc = db.get_obj('modelchain', 'testxml', 'simulation')# doctest: +ELLIPSIS >>> mc.chain_groups['Forced thinnings'] ['Calculate thinning limits'] >>> mc.chain_type 'simulation' >>> len(mc.chains) 1 >>> c = mc.chains[0] >>> c.condition >>> c.evaluate_at 'comp_unit' >>> c.name 'Calculate thinning limits' >>> len(c.tasks) 1 >>> t = c.tasks[0] >>> t.name 'Calculate thinning limits' >>> t.value_fixer False >>> t.model # doctest: +ELLIPSIS >>> t.model._v_func #doctest: +ELLIPSIS >>> t.condition_parser >>> t.validator >>> p = t.model_param >>> p.parameters [0.5] >>> p.rect_factor 1.0 >>> p.risk_level 1 >>> p._validator def get_valid(self, simoclass=None): ==================================== Returns if the given simoclass is valid or if simoclass is None, information on all the simoclasses. >>> db.get_valid('modelchain') == \ ... {'valid': True, 'types': {'simulation': {'testxml': True, ... 'testxml2': True}}} True >>> db.get_valid() == \ ... {'modelchain': {'valid': True, ... 'types': {'simulation': {'testxml': True, ... 'testxml2': True}}}, ... 'problem_definition': {'valid': False, 'names': {}}, ... 'lexicon': {'valid': False, 'names': {}}, ... 'text2data': {'valid': False, 'names': {}}, ... 'operation2modelchains': {'valid': True, 'names': {'testxml': True}}, ... 'aggregation_definition': {'valid': False, 'names': {}}, ... 'expression_definition': {'valid': False, 'names': {}}, ... 'message_translation': {'valid': False, 'names': {}}, ... 'lexicon_translation': {'valid': False, 'names': {}}, ... 'modelbase': {'valid': True, 'types': {'aggregation': {'testxml': True}}}, ... 'text2operation': {'valid': False, 'names': {}}, ... 'output_constraint': {'valid': False, 'names': {}}, ... 'simulation_control': {'valid': False, 'names': {}}} True