Fix some bugs in conformance checker

This commit is contained in:
Joeri Exelmans 2024-10-04 10:50:29 +02:00
parent 03cc568516
commit 0785b9218e
2 changed files with 70 additions and 63 deletions

View file

@ -48,39 +48,35 @@ class Conformance:
Returns: Returns:
Boolean indicating whether the check has passed Boolean indicating whether the check has passed
""" """
try: errors = []
self.check_typing() errors += self.check_typing()
self.check_link_typing() errors += self.check_link_typing()
self.check_multiplicities() errors += self.check_multiplicities()
self.check_constraints() errors += self.check_constraints()
return True return errors
except RuntimeError as e:
if log:
print(e)
return False
def check_structural(self, *, build_morphisms=True, log=False): # def check_structural(self, *, build_morphisms=True, log=False):
""" # """
Perform a structural conformance check # Perform a structural conformance check
Args: # Args:
build_morphisms: boolean indicating whether to create morpishm links # build_morphisms: boolean indicating whether to create morpishm links
log: boolean indicating whether to log errors # log: boolean indicating whether to log errors
Returns: # Returns:
Boolean indicating whether the check has passed # Boolean indicating whether the check has passed
""" # """
try: # try:
self.precompute_structures() # self.precompute_structures()
self.match_structures() # self.match_structures()
if build_morphisms: # if build_morphisms:
self.build_morphisms() # self.build_morphisms()
self.check_nominal(log=log) # self.check_nominal(log=log)
return True # return True
except RuntimeError as e: # except RuntimeError as e:
if log: # if log:
print(e) # print(e)
return False # return False
def read_attribute(self, element: UUID, attr_name: str): def read_attribute(self, element: UUID, attr_name: str):
""" """
@ -218,8 +214,8 @@ class Conformance:
# optional for attribute links # optional for attribute links
opt = self.read_attribute(tm_element, "optional") opt = self.read_attribute(tm_element, "optional")
if opt != None: if opt != None:
self.source_multiplicities[tm_name] = (0 if opt else 1, 1) self.source_multiplicities[tm_name] = (0, float('inf'))
self.target_multiplicities[tm_name] = (0, 1) self.target_multiplicities[tm_name] = (0 if opt else 1, 1)
def get_type(self, element: UUID): def get_type(self, element: UUID):
""" """
@ -234,6 +230,7 @@ class Conformance:
for each element of model check whether a morphism for each element of model check whether a morphism
link exists to some element of type_model link exists to some element of type_model
""" """
errors = []
ref_element, = self.bottom.read_outgoing_elements(self.scd_model, "ModelRef") ref_element, = self.bottom.read_outgoing_elements(self.scd_model, "ModelRef")
model_names = self.bottom.read_keys(self.model) model_names = self.bottom.read_keys(self.model)
for m_name in model_names: for m_name in model_names:
@ -245,19 +242,20 @@ class Conformance:
if ref_element in self.bottom.read_outgoing_elements(tm_element, "Morphism"): if ref_element in self.bottom.read_outgoing_elements(tm_element, "Morphism"):
sub_m = UUID(self.bottom.read_value(m_element)) sub_m = UUID(self.bottom.read_value(m_element))
sub_tm = UUID(self.bottom.read_value(tm_element)) sub_tm = UUID(self.bottom.read_value(tm_element))
if not Conformance(self.state, sub_m, sub_tm).check_nominal(): nested_errors = Conformance(self.state, sub_m, sub_tm).check_nominal()
raise RuntimeError(f"Incorrectly model reference: {m_name}") errors += [f"In ModelRef ({m_name}):" + err for err in nested_errors]
except ValueError as e: except ValueError as e:
import traceback import traceback
traceback.format_exc(e) traceback.format_exc(e)
# no or too many morphism links found # no or too many morphism links found
raise RuntimeError(f"Incorrectly typed element: {m_name}") errors.append(f"Incorrectly typed element: {m_name}")
return True return errors
def check_link_typing(self): def check_link_typing(self):
""" """
for each link, check whether its source and target are of a valid type for each link, check whether its source and target are of a valid type
""" """
errors = []
self.precompute_sub_types() self.precompute_sub_types()
for m_name, tm_name in self.type_mapping.items(): for m_name, tm_name in self.type_mapping.items():
m_element, = self.bottom.read_outgoing_elements(self.model, m_name) m_element, = self.bottom.read_outgoing_elements(self.model, m_name)
@ -275,15 +273,15 @@ class Conformance:
source_type_expected = self.type_model_names[tm_source] source_type_expected = self.type_model_names[tm_source]
if source_type_actual != source_type_expected: if source_type_actual != source_type_expected:
if source_type_actual not in self.sub_types[source_type_expected]: if source_type_actual not in self.sub_types[source_type_expected]:
raise RuntimeError(f"Invalid source type {source_type_actual} for element {m_name}") errors.append(f"Invalid source type {source_type_actual} for element {m_name}")
# check if target is typed correctly # check if target is typed correctly
target_name = self.model_names[m_target] target_name = self.model_names[m_target]
target_type_actual = self.type_mapping[target_name] target_type_actual = self.type_mapping[target_name]
target_type_expected = self.type_model_names[tm_target] target_type_expected = self.type_model_names[tm_target]
if target_type_actual != target_type_expected: if target_type_actual != target_type_expected:
if target_type_actual not in self.sub_types[target_type_expected]: if target_type_actual not in self.sub_types[target_type_expected]:
raise RuntimeError(f"Invalid target type {target_type_actual} for element {m_name}") errors.append(f"Invalid target type {target_type_actual} for element {m_name}")
return True return errors
def check_multiplicities(self): def check_multiplicities(self):
""" """
@ -291,12 +289,13 @@ class Conformance:
""" """
self.deref_primitive_values() self.deref_primitive_values()
self.precompute_multiplicities() self.precompute_multiplicities()
errors = []
for tm_name in self.type_model_names.values(): for tm_name in self.type_model_names.values():
# abstract classes # abstract classes
if tm_name in self.abstract_types: if tm_name in self.abstract_types:
type_count = list(self.type_mapping.values()).count(tm_name) type_count = list(self.type_mapping.values()).count(tm_name)
if type_count > 0: if type_count > 0:
raise RuntimeError(f"Invalid instantiation of abstract class: {tm_name}") errors.append(f"Invalid instantiation of abstract class: {tm_name}")
# class multiplicities # class multiplicities
if tm_name in self.multiplicities: if tm_name in self.multiplicities:
lc, uc = self.multiplicities[tm_name] lc, uc = self.multiplicities[tm_name]
@ -304,48 +303,55 @@ class Conformance:
for sub_type in self.sub_types[tm_name]: for sub_type in self.sub_types[tm_name]:
type_count += list(self.type_mapping.values()).count(sub_type) type_count += list(self.type_mapping.values()).count(sub_type)
if type_count < lc or type_count > uc: if type_count < lc or type_count > uc:
raise RuntimeError(f"Cardinality of type exceeds valid multiplicity range: {tm_name} ({type_count})") errors.append(f"Cardinality of type exceeds valid multiplicity range: {tm_name} ({type_count})")
# association source multiplicities # association source multiplicities
if tm_name in self.source_multiplicities: if tm_name in self.source_multiplicities:
tm_element, = self.bottom.read_outgoing_elements(self.type_model, tm_name) tm_element, = self.bottom.read_outgoing_elements(self.type_model, tm_name)
tm_source_element = self.bottom.read_edge_source(tm_element) tm_tgt_element = self.bottom.read_edge_target(tm_element)
tm_source_name = self.type_model_names[tm_source_element] tm_tgt_name = self.type_model_names[tm_tgt_element]
lc, uc = self.source_multiplicities[tm_name] lc, uc = self.source_multiplicities[tm_name]
for i, t in self.type_mapping.items(): for tgt_obj_name, t in self.type_mapping.items():
if t == tm_source_name or t in self.sub_types[tm_source_name]: if t == tm_tgt_name or t in self.sub_types[tm_tgt_name]:
count = 0 count = 0
i_element, = self.bottom.read_outgoing_elements(self.model, i) tgt_obj_node, = self.bottom.read_outgoing_elements(self.model, tgt_obj_name)
outgoing = self.bottom.read_outgoing_edges(i_element) incoming = self.bottom.read_incoming_edges(tgt_obj_node)
for o in outgoing: for i in incoming:
try: try:
if self.type_mapping[self.model_names[o]] == tm_name: if self.type_mapping[self.model_names[i]] == tm_name:
count += 1 count += 1
except KeyError: except KeyError:
pass # for elements not part of model, e.g. morphism links pass # for elements not part of model, e.g. morphism links
if count < lc or count > uc: if count < lc or count > uc:
raise RuntimeError(f"Source cardinality of type {tm_name} exceeds valid multiplicity range in {i}.") errors.append(f"Source cardinality of type {tm_name} ({count}) out of bounds ({lc}..{uc}) in {tgt_obj_name}.")
# association target multiplicities # association target multiplicities
if tm_name in self.target_multiplicities: if tm_name in self.target_multiplicities:
tm_element, = self.bottom.read_outgoing_elements(self.type_model, tm_name) tm_element, = self.bottom.read_outgoing_elements(self.type_model, tm_name)
tm_target_element = self.bottom.read_edge_target(tm_element) # tm_target_element = self.bottom.read_edge_target(tm_element)
tm_target_name = self.type_model_names[tm_target_element] tm_src_element = self.bottom.read_edge_source(tm_element)
tm_src_name = self.type_model_names[tm_src_element]
lc, uc = self.target_multiplicities[tm_name] lc, uc = self.target_multiplicities[tm_name]
for i, t in self.type_mapping.items(): # print("checking assoc", tm_name, "source", tm_src_name)
if t == tm_target_name or t in self.sub_types[tm_target_name]: # print("subtypes of", tm_src_name, self.sub_types[tm_src_name])
for src_obj_name, t in self.type_mapping.items():
if t == tm_src_name or t in self.sub_types[tm_src_name]:
# print("got obj", src_obj_name, "of type", t)
count = 0 count = 0
i_element, = self.bottom.read_outgoing_elements(self.model, i) src_obj_node, = self.bottom.read_outgoing_elements(self.model, src_obj_name)
outgoing = self.bottom.read_incoming_edges(i_element) # outgoing = self.bottom.read_incoming_edges(src_obj_node)
outgoing = self.bottom.read_outgoing_edges(src_obj_node)
for o in outgoing: for o in outgoing:
try: try:
if self.type_mapping[self.model_names[o]] == tm_name: if self.type_mapping[self.model_names[o]] == tm_name:
# print("have an outgoing edge", self.model_names[o], self.type_mapping[self.model_names[o]], "---> increase counter")
count += 1 count += 1
except KeyError: except KeyError:
pass # for elements not part of model, e.g. morphism links pass # for elements not part of model, e.g. morphism links
if count < lc or count > uc: if count < lc or count > uc:
print(f"Target cardinality of type {tm_name} exceeds valid multiplicity range in {i}.") errors.append(f"Target cardinality of type {tm_name} ({count}) out of bounds ({lc}..{uc}) in {src_obj_name}.")
return False # else:
return True # print(f"OK: Target cardinality of type {tm_name} ({count}) within bounds ({lc}..{uc}) in {src_obj_name}.")
return errors
def evaluate_constraint(self, code, **kwargs): def evaluate_constraint(self, code, **kwargs):
""" """
@ -367,6 +373,7 @@ class Conformance:
Check whether all constraints defined for a model are respected Check whether all constraints defined for a model are respected
""" """
# local constraints # local constraints
errors = []
for m_name, tm_name in self.type_mapping.items(): for m_name, tm_name in self.type_mapping.items():
if tm_name != "GlobalConstraint": if tm_name != "GlobalConstraint":
tm_element, = self.bottom.read_outgoing_elements(self.type_model, tm_name) tm_element, = self.bottom.read_outgoing_elements(self.type_model, tm_name)
@ -376,7 +383,7 @@ class Conformance:
morphisms = [m for m in morphisms if m in self.model_names] morphisms = [m for m in morphisms if m in self.model_names]
for m_element in morphisms: for m_element in morphisms:
if not self.evaluate_constraint(code, element=m_element): if not self.evaluate_constraint(code, element=m_element):
raise RuntimeError(f"Local constraint of {tm_name} not satisfied in {m_name}.") errors.append(f"Local constraint of {tm_name} not satisfied in {m_name}.")
# global constraints # global constraints
for m_name, tm_name in self.type_mapping.items(): for m_name, tm_name in self.type_mapping.items():
@ -385,8 +392,8 @@ class Conformance:
code = self.read_attribute(tm_element, "constraint") code = self.read_attribute(tm_element, "constraint")
if code != None: if code != None:
if not self.evaluate_constraint(code, model=self.model): if not self.evaluate_constraint(code, model=self.model):
raise RuntimeError(f"Global constraint {tm_name} not satisfied.") errors.append(f"Global constraint {tm_name} not satisfied.")
return True return errors
def precompute_structures(self): def precompute_structures(self):
""" """

View file

@ -75,7 +75,7 @@ def ramify(state: State, model: UUID, prefix = "RAM_") -> UUID:
# Double-check: The RAMified meta-model should also conform to 'SCD': # Double-check: The RAMified meta-model should also conform to 'SCD':
conf = Conformance(state, ramified, scd_metamodel) conf = Conformance(state, ramified, scd_metamodel)
if not conf.check_nominal(log=True): if len(conf.check_nominal(log=True)) > 0:
raise Exception("Unexpected error: RAMified MM does not conform to SCD MM") raise Exception("Unexpected error: RAMified MM does not conform to SCD MM")
return ramified return ramified