Skip to content

Commit

Permalink
Merge pull request #175 from AgenciaImplementacion/configure_domains_…
Browse files Browse the repository at this point in the history
…for_structures

Configure domains for structures
  • Loading branch information
m-kuhn authored Apr 12, 2018
2 parents 39720f1 + 5dd9e43 commit 7d8518f
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
47 changes: 46 additions & 1 deletion projectgenerator/libqgsprojectgen/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,15 +399,18 @@ def parse_model(self, model_content, domains):
re_model = re.compile('\s*MODEL\s*([\w\d_-]+).*')
# TOPIC Catastro_Registro [=]
re_topic = re.compile('\s*TOPIC\s*([\w\d_-]+).*')
re_structure = re.compile('\s*STRUCTURE\s*([\w\d_-]+)\s*\=.*') # STRUCTURE StructureName =
re_class = re.compile(
'\s*CLASS\s*([\w\d_-]+)\s*[EXTENDS]*\s*([\w\d_-]*).*') # CLASS ClassName [EXTENDS] [BaseClassName] [=]
re_class_extends = re.compile(
'\s*EXTENDS\s*([\w\d_\-\.]+)\s*\=.*') # EXTENDS BaseClassName =
re_end_structure = None # END StructureName;
re_end_class = None # END ClassName;
re_end_topic = None # END TopicName;
re_end_model = None # END ModelName;

current_model = ''
current_structure = ''
current_topic = ''
current_class = ''
attributes = dict()
Expand All @@ -423,7 +426,8 @@ def parse_model(self, model_content, domains):
current_model = result.group(1)
re_end_model = re.compile(
'.*END\s*{}\..*'.format(current_model)) # END TopicName;
else: # The is a current_model
else: # There is a current_model

if not current_topic:
result = re_topic.search(line)
if result:
Expand All @@ -441,6 +445,47 @@ def parse_model(self, model_content, domains):
print("domains_with_local:", domains_with_local)
continue
else: # There is a current_topic

if not current_structure:
result = re_structure.search(line)
if result:
current_structure = result.group(1)
if self.debug:
print("Structure encontrada", current_structure)
attributes = dict()
re_end_structure = re.compile(
'\s*END\s*{};.*'.format(current_structure)) # END StructureName;
continue
else:
attribute = {res.group(1): d for d in domains_with_local for res in
[re.search('\s*([\w\d_-]+).*:.*\s{};.*'.format(d), line)] if res}

if attribute:
if self.debug:
print("MATCH (STRUCTURE):", attribute)
old_key = list(attribute.keys())[0] # Not qualified name
new_key = "{}.{}.{}.{}".format(current_model, current_topic, current_structure,
old_key) # Fully qualified name
attr_value = list(attribute.values())[0]
if attr_value not in domains: # Match was vs. local name, find its corresponding qualified name
for k, v in local_names.items():
if attr_value in v:
attribute[old_key] = k
break
attribute[new_key] = attribute.pop(old_key)
attributes.update(attribute)
continue

result = re_end_structure.search(line)
if result:
if attributes:
models_info.update(
{'{}.{}.{}'.format(current_model, current_topic, current_structure): attributes})
if self.debug:
print("END Structure encontrada", current_structure)
current_structure = ''
continue

if not current_class: # Go for classes
result = re_class.search(line)
if result:
Expand Down
87 changes: 87 additions & 0 deletions projectgenerator/tests/test_domain_class_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,93 @@ def test_domain_class_relations_ZG_Abfallsammelstellen_ZEBA_V1_geopackage(self):
for expected_relation in expected_relations:
self.assertIn(expected_relation, relations_dicts)

def test_domain_structure_relations_ZG_Naturschutz_und_Erholung_V1_0_postgis(self):
# Schema Import
importer = iliimporter.Importer()
importer.tool_name = 'ili2pg'
importer.configuration = iliimporter_config(importer.tool_name)
importer.configuration.ilifile = testdata_path(
'ilimodels/repo/ZG_Naturschutz_und_Erholung_V1_0.ili')
importer.configuration.ilimodels = 'ZG_Naturschutz_und_Erholung_V1_0'
importer.configuration.schema = 'any_{:%Y%m%d%H%M%S%f}'.format(
datetime.datetime.now())
importer.configuration.epsg = 21781
importer.configuration.inheritance = 'smart2'
importer.stdout.connect(self.print_info)
importer.stderr.connect(self.print_error)
self.assertEqual(importer.run(), iliimporter.Importer.SUCCESS)

generator = Generator('ili2pg',
'dbname=gis user=docker password=docker host=postgres',
importer.configuration.inheritance,
importer.configuration.schema)

available_layers = generator.layers()
relations = generator.relations(available_layers)

# Check domain class relations in the relations list
relations_dicts = list()
for relation in relations:
relations_dicts.append({"referencing_layer": relation.referencing_layer.name,
"referenced_layer": relation.referenced_layer.name,
"referencing_field": relation.referencing_field,
"referenced_field": relation.referenced_field,
"name": relation.name})

# 7 domain-structure + 1 domain-class relations are expected
expected_relations = list()

expected_relations.append({'name': 'ns_bewirtschaftung_ns_bewirtschaftungen_ns_bewirtschaftungen_ilicode',
'referenced_layer': 'ns_bewirtschaftungen',
'referenced_field': 'ilicode',
'referencing_layer': 'ns_bewirtschaftung',
'referencing_field': 'ns_bewirtschaftungen'})

expected_relations.append({'name': 'datenbestand_zustaendigestelle_fkey',
'referenced_layer': 'zustaendige_stelle',
'referenced_field': 't_id',
'referencing_layer': 'datenbestand',
'referencing_field': 'zustaendigestelle'})

expected_relations.append({'name': 'ni_punkt_typ_ni_punkt_typen_ni_punkt_typen_ilicode',
'referenced_layer': 'ni_punkt_typen',
'referenced_field': 'ilicode',
'referencing_layer': 'ni_punkt_typ',
'referencing_field': 'ni_punkt_typen'})

expected_relations.append({'name': 'ei_linie_typ_ei_linie_typen_ei_linie_typen_ilicode',
'referenced_layer': 'ei_linie_typen',
'referenced_field': 'ilicode',
'referencing_layer': 'ei_linie_typ',
'referencing_field': 'ei_linie_typen'})

expected_relations.append({'name': 'ei_bewirtschaftung_ei_bewirtschaftungen_ei_bewirtschaftungen_ilicode',
'referenced_layer': 'ei_bewirtschaftungen',
'referenced_field': 'ilicode',
'referencing_layer': 'ei_bewirtschaftung',
'referencing_field': 'ei_bewirtschaftungen'})

expected_relations.append({'name': 'ei_punkt_typ_ei_punkt_typen_ei_punkt_typen_ilicode',
'referenced_layer': 'ei_punkt_typen',
'referenced_field': 'ilicode',
'referencing_layer': 'ei_punkt_typ',
'referencing_field': 'ei_punkt_typen'})

expected_relations.append({'name': 'ni_linie_typ_ni_linie_typen_ni_linie_typen_ilicode',
'referenced_layer': 'ni_linie_typen',
'referenced_field': 'ilicode',
'referencing_layer': 'ni_linie_typ',
'referencing_field': 'ni_linie_typen'})

expected_relations.append({'name': 'naturschutzrelevantes_objekt_ohne_schutzstatus_typ_nro_typ_ilicode',
'referenced_layer': 'nro_typ',
'referenced_field': 'ilicode',
'referencing_layer': 'naturschutzrelevantes_objekt_ohne_schutzstatus',
'referencing_field': 'typ'})

for expected_relation in expected_relations:
self.assertIn(expected_relation, relations_dicts)

def print_info(self, text):
print(text)

Expand Down

0 comments on commit 7d8518f

Please sign in to comment.