Visitar URL original
add support for consistent unique id generation base on index tuple · Issue #70 · inmanta/std · GitHub
Skip to content

add support for consistent unique id generation base on index tuple #70

Open

Description

@sanderr

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:

Activity

  1. sanderr commented on Mar 17, 2020

    @sanderr
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions