-
Notifications
You must be signed in to change notification settings - Fork 6
Open
Description
Consider below model:
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.
A proof of concept that still needs some work:
`main.cf:
entity TestEntity:
string unique
end
index TestEntity(unique)
implement TestEntity using std::none
collector = IdCollector(instance_index_field = "unique")
Identifiable(collector = collector, instance = TestEntity(unique = "0"))
Identifiable(collector = collector, instance = TestEntity(unique = "1"))
Identifiable(collector = collector, instance = TestEntity(unique = "2"))
Identifiable(collector = collector, instance = TestEntity(unique = "3"))
Identifiable(collector = collector, instance = TestEntity(unique = "4"))
libs/auto_id/model/_init.cf
# 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:
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels