gem/openquake — pull request #679 by al-maisan (2012-02-10 07:16:35+00:00)
Title: Db exposure model changes

Hello there!

the branch at hand addresses https://bugs.launchpad.net/openquake/+bug/901964 and https://bugs.launchpad.net/openquake/+bug/928035

It needs to be reviewed since it has grown to 1200 lines of diff. However, once approved it will *not* be merged to master because the work is not complete yet.

This branch does not pass the test suite but the exposure model consistency tests added by it should pass:

(export DJANGO_SETTINGS_MODULE="openquake.settings"; nosetests -s tests/risk_exposure_model_unittest.py)

please see
https://github.com/gem/openquake/wiki/images/db/oqmif.png
for an overview of the schema changes.

Finishing this took quite a bit of time because I could not use DDL
constraints but needed to write functions that check the consistency of
the database rows upon insert/update (so called triggers).

As agreed with Vitor we now use the following abbreviations in the
exposure model:

coco = contents cost
reco = retrofiting cost
stco = structural cost

The rules encoded in the exposure model at present are as follows:

** On exposure model level **

1 - if either of coco_type, reco_type, stco_type is set to
"per_area" then
a - area_type must be set
b - area_unit must be set

2 - coco_type and coco_unit must both either be set or undefined
3 - reco_type and reco_unit must both either be set or undefined
4 - stco_type and stco_unit must both either be set or undefined

5 - stco_type must be set unless category is set to "population"

** On exposure datum level **

6 - stco must be set unless the exposure model category is set to
"population"
7 - number_of_units must be set if
a - the model category is set to "population"
b - if either of coco_type, reco_type, stco_type is set to
"per_asset"
c - if either of coco_type, reco_type, stco_type is set to
"per_area" and the area_type is set to "per_asset"
8 - area must be set if either of coco_type, reco_type, stco_type is
set to "per_area"
9 - coco must be set if the coco_type was defined in the model
10 - reco must be set if the reco_type was defined in the model
11 - stco must be set if the stco_type was defined in the model

Please let me know what you think!