Exemplo n.º 1
0
def _get_all_entity_trees_of_cls_helper(
        tree: EntityTree,
        cls: Type[DatabaseEntity],
        seen_ids: Set[int],
        seen_trees: List[EntityTree],
        direction_checker: SchemaEdgeDirectionChecker):
    """
    Finds all objects in the provided |tree| graph which have the type |cls|.
    When an object of type |cls| is found, updates the provided |seen_ids| and
    |seen_trees| with the object's id and EntityTree respectively.
    """
    entity = tree.entity
    entity_cls = entity.__class__

    # If |cls| is higher ranked than |entity_cls|, it is impossible to reach
    # an object of type |cls| from the current entity.
    if direction_checker.is_higher_ranked(cls, entity_cls):
        return
    if entity_cls == cls and id(entity) not in seen_ids:
        seen_ids.add(id(entity))
        seen_trees.append(tree)
        return
    for child_field_name in get_set_entity_field_names(
            entity, EntityFieldType.FORWARD_EDGE):
        child_trees = tree.generate_child_trees(
            entity.get_field_as_list(child_field_name))
        for child_tree in child_trees:
            _get_all_entity_trees_of_cls_helper(
                child_tree, cls, seen_ids, seen_trees, direction_checker)
Exemplo n.º 2
0
def _get_match_results_for_all_children(
        ingested_entity_tree: EntityTree, db_entity_trees: List[EntityTree],
        root_entity_cls) \
        -> List[Tuple[str, MatchResults]]:
    """Attempts to match all children of the |ingested_entity_tree| to children
    of the |db_entity_trees|. Matching for each child is independent and can
    match to different DB parents.

    Returns a list of tuples with the following values:
    - str: the string name of the child field
    - MatchResult: the result of matching this child field to children of the
        provided |db_entity_trees|
    """
    results = []
    ingested_entity = ingested_entity_tree.entity
    set_child_fields = get_set_entity_field_names(ingested_entity,
                                                  EntityFieldType.FORWARD_EDGE)

    for child_field_name in set_child_fields:
        ingested_child_field = get_field(ingested_entity, child_field_name)
        db_child_trees = generate_child_entity_trees(child_field_name,
                                                     db_entity_trees)
        if isinstance(ingested_child_field, list):
            ingested_child_list = ingested_child_field
        else:
            ingested_child_list = [ingested_child_field]

        ingested_child_trees = \
            ingested_entity_tree.generate_child_trees(ingested_child_list)
        match_results = _match_entity_trees(
            ingested_entity_trees=ingested_child_trees,
            db_entity_trees=db_child_trees,
            root_entity_cls=root_entity_cls)
        results.append((child_field_name, match_results))
    return results