Esempio n. 1
0
    def add_attribute_lookup(self, key):
        """Adds a fast-lookup index for the component/attribute key path.

        This also causes the newly-created lookup table to rebuild its index.
        Depending on how many entities already exist, building the index could
        take a couple of seconds.
        """
        # Don't add the same one twice.
        if self.lookup_tables.get(key, None):
            return

        attribute = entity_module.Entity.reflect_attribute(key)
        if not isinstance(attribute, entity_component.Field):
            logging.info(
                ("Can't create a lookup for %s, because it's not a simple "
                 "field."), attribute)
            return

        logging.debug("Creating a lookup table for %s", key)
        component, _ = key.split("/")

        lookup_table = entity_lookup.AttributeLookupTable(attribute=key,
                                                          entity_manager=self)

        # Only use the entities that actually have the component to build the
        # index.
        lookup_table.update_index(
            self.find(expression.ComponentLiteral(component), complete=False))

        self.lookup_tables[key] = lookup_table
Esempio n. 2
0
    def find_by_component(self, component, complete=True):
        """Finds all entities that have the component.

        Arguments:
            complete: If True, will attempt to collect the component.
        """
        query = entity_query.Query(expression.ComponentLiteral(component))
        if complete:
            self.collect_for(query)

        return list(self.lookup_tables["components"].lookup(component))
Esempio n. 3
0
 def testHasComponent(self):
     query = "has component Process"
     expected = expression.ComponentLiteral("Process")
     self.assertQueryMatches(query, expected)
Esempio n. 4
0
def FlattenComponentLiteral(*args, **kwargs):
    if not isinstance(args[0], expression.Binding):
        raise ValueError(
            "'has component' must be followed by a component. Got %s." %
            (args[0]))
    return expression.ComponentLiteral(args[0].value, **kwargs)