You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
entity A:
number n
end
index A(n)
entity AContainer:
end
AContainer.a [0:] -- A
and assume you want to generate a unique id for each A in an AContainer instance (for example the rule number from the bics bootcamp day 3). It would be nice to support generation of such an id.
# entity responsible for id assignment to it's instances based on the instances' field with name instance_index_field. In practice, this should be a list of strings to support multi-field indices.
entity IdCollector:
string instance_index_field
end
implement IdCollector using collect_ids
# will receive an id from its IdCollector
entity Identifiable:
number id
end
index Identifiable(instance, collector)
IdCollector.instances [0:] -- Identifiable.collector [1]
# TODO: use inheritance over composition? Instances could inherit from Identifiable instead of binding them together.
Identifiable.instance [1] -- std::Entity
implement Identifiable using std::none
implementation collect_ids for IdCollector:
# plugin returns a dict from index value to assigned id. TODO: this method only works for single-field string indices
id_map = map_unique_ids(self.instances, self.instance_index_field, 11)
for i in self.instances:
instance_index_value = i.instance.unique
i.id = id_map[instance_index_value]
std::print("unique: {{i.instance.unique}}, id: {{i.id}}")
end
end
libs/auto_id/plugins/__init__.py
from operator import attrgetter
from inmanta.plugins import plugin
@plugin
def map_unique_ids(collection: "list", index_field: "string", offset: "number") -> "dict":
"""
"""
sorted_collection = sorted(
collection,
key=lambda i: attrgetter(index_field)(i.instance)
)
return {attrgetter(index_field)(instance.instance): offset + i for i, instance in enumerate(sorted_collection)}
# vim: set tabstop=4 shiftwidth=4 expandtab:
The text was updated successfully, but these errors were encountered:
An important consideration here is whether std is really the best place to achieve this. Above example is not very intuitive to use. If that can not be improved, built-in compiler support might be more relevant.
Consider below model:
and assume you want to generate a unique id for each
A
in anAContainer
instance (for example the rule number from the bics bootcamp day 3). It would be nice to support generation of such an id.A proof of concept that still needs some work:
`main.cf:
libs/auto_id/model/_init.cf
libs/auto_id/plugins/__init__.py
The text was updated successfully, but these errors were encountered: