|
@@ -49,3 +49,61 @@ def read_attribute(a, b, c, **remainder):
|
|
|
raise PrimitiveFinished(result)
|
|
|
|
|
|
raise Exception("Error in reading edge!")
|
|
|
+
|
|
|
+def precompute_cardinalities(a, **remainder):
|
|
|
+ result = yield [("CN", [])]
|
|
|
+
|
|
|
+ # Read out all edges from the metamodel
|
|
|
+ model_dict = yield [("RD", [a, "model"])]
|
|
|
+ type_mapping = yield [("RD", [a, "type_mapping"])]
|
|
|
+ elems = yield [("RO", [model_dict])]
|
|
|
+ elems = yield [("RE", [i]) for i in elems]
|
|
|
+ elems = [i[1] for i in elems]
|
|
|
+ edges = yield [("RE", [i]) for i in elems]
|
|
|
+ elems = [elems[i] for i, edge_val in enumerate(edges) if edge_val is not None]
|
|
|
+ # Now we have all edges in the metamodel
|
|
|
+
|
|
|
+ # Read out the type of the Association defining all cardinalities
|
|
|
+ metamodel = yield [("RD", [a, "metamodel"])]
|
|
|
+ metamodel_dict= yield [("RD", [metamodel, "model"])]
|
|
|
+ assoc = yield [("RD", [metamodel_dict, "Association"])]
|
|
|
+ slc, suc, tlc, tuc = \
|
|
|
+ yield [("RDE", [assoc, "source_lower_cardinality"]),
|
|
|
+ ("RDE", [assoc, "source_upper_cardinality"]),
|
|
|
+ ("RDE", [assoc, "target_lower_cardinality"]),
|
|
|
+ ("RDE", [assoc, "target_upper_cardinality"]),
|
|
|
+ ]
|
|
|
+
|
|
|
+ # All that we now have to do is find, for each edge, whether or not it has an edge typed by any of these links!
|
|
|
+ # Just find all links typed by these links!
|
|
|
+ types = yield [("RDN", [type_mapping, i]) for i in elems]
|
|
|
+
|
|
|
+ cardinalities = {}
|
|
|
+ for i, edge_type in enumerate(types):
|
|
|
+ if edge_type == slc:
|
|
|
+ t = "slc"
|
|
|
+ elif edge_type == suc:
|
|
|
+ t = "suc"
|
|
|
+ elif edge_type == tlc:
|
|
|
+ t = "tlc"
|
|
|
+ elif edge_type == tuc:
|
|
|
+ t = "tuc"
|
|
|
+ else:
|
|
|
+ continue
|
|
|
+
|
|
|
+ # Found a link, so add it
|
|
|
+ source, destination = yield [("RE", [elems[i]])]
|
|
|
+ # The edge gives the "source" the cardinality found in "destination"
|
|
|
+ cardinalities.setdefault(source, {})[t] = destination
|
|
|
+
|
|
|
+ # Now we have to translate the "cardinalities" Python dictionary to a Modelverse dictionary
|
|
|
+ nodes = yield [("CN", []) for i in cardinalities]
|
|
|
+ yield [("CD", [result, i, node]) for i, node in zip(cardinalities, nodes)]
|
|
|
+ l = list(cardinalities)
|
|
|
+ values = yield [("RD", [result, i]) for i in l]
|
|
|
+
|
|
|
+ for i, value in enumerate(values):
|
|
|
+ cards = cardinalities[l[i]]
|
|
|
+ yield [("CD", [value, card_type, cards[card_type]]) for card_type in cards]
|
|
|
+
|
|
|
+ raise PrimitiveFinished(result)
|