Exemplo n.º 1
0
    def __find_paths(self, graph, entity_items, relation_items, edges, output_paths=Paths(), used_edges=set()):
        new_output_paths = Paths([])

        if len(relation_items) == 0:
            if len(entity_items) > 0:
                return Paths()
            return output_paths

        used_relations = []
        for relation_item in relation_items:
            for relation in relation_item.uris:
                used_relations = used_relations + [relation]
                for edge in self.find_edges(edges, relation, used_edges):
                    entities = MyList()
                    if not (edge.source_node.are_all_uris_generic() or edge.uri.is_type()):
                        entities.extend(edge.source_node.uris)
                    if not (edge.dest_node.are_all_uris_generic() or edge.uri.is_type()):
                        entities.extend(edge.dest_node.uris)
                    new_paths = self.__find_paths(graph,
                                                  entity_items - LinkedItem.list_contains_uris(entity_items, entities),
                                                  relation_items - LinkedItem.list_contains_uris(relation_items,
                                                                                                 used_relations),
                                                  edges - {edge},
                                                  output_paths=output_paths.extend(edge),
                                                  used_edges=used_edges | set([edge]))
                    new_output_paths.add(new_paths, lambda path: len(path) >= len(graph.relation_items))

        return new_output_paths
Exemplo n.º 2
0
    def __find_paths_start_with_entities(self,
                                         graph,
                                         entity_items,
                                         relation_items,
                                         edges,
                                         output_paths=Paths(),
                                         used_edges=set()):
        new_output_paths = Paths([])
        for entity_item in entity_items:
            for entity in entity_item.uris:
                for edge in self.find_edges_by_entity(edges, entity,
                                                      used_edges):
                    if not edge.uri.is_type():
                        used_relations = [edge.uri]
                    else:
                        used_relations = edge.dest_node.uris
                    entities = MyList()
                    if not (edge.source_node.are_all_uris_generic()
                            or edge.uri.is_type()):
                        entities.extend(edge.source_node.uris)
                    if not (edge.dest_node.are_all_uris_generic()
                            or edge.uri.is_type()):
                        entities.extend(edge.dest_node.uris)

                    entity_use = entity_items - LinkedItem.list_contains_uris(
                        entity_items, entities)
                    relation_use = relation_items - LinkedItem.list_contains_uris(
                        relation_items, used_relations)
                    edge_use = edges - {edge}

                    new_paths = self.__find_paths(
                        graph,
                        entity_use,
                        relation_use,
                        edge_use,
                        output_paths=output_paths.extend(edge),
                        used_edges=used_edges | set([edge]))
                    # new_paths = self.__find_paths(graph,
                    #                               entity_items - LinkedItem.list_contains_uris(entity_items, entities),
                    #                               relation_items - LinkedItem.list_contains_uris(relation_items,
                    #                                                                              used_relations),
                    #                               edges - {edge},
                    #                               output_paths=output_paths.extend(edge),
                    #                               used_edges=used_edges | set([edge]))
                    new_output_paths.add(
                        new_paths,
                        lambda path: len(path) >= len(graph.relation_items))
        return new_output_paths
Exemplo n.º 3
0
    def __find_paths_start_with_entities(self, graph, entity_items, relation_items, edges, output_paths=Paths(),
                                         used_edges=set()):
        new_output_paths = Paths([])
        for entity_item in entity_items:
            unavailable_edges = self.find_edges_by_entities(edges, entity_item)
            available_edges = edges - unavailable_edges
            available_entity_items = entity_items - [entity_item]
            for entity in entity_item.uris:
                for edge in self.find_edges_by_entity(edges, entity, used_edges):
                    if not edge.uri.is_type():
                        used_relations = [edge.uri]
                    else:
                        used_relations = edge.dest_node.uris

                    unavailable_relations = LinkedItem.list_contains_uris(relation_items, used_relations)
                    for unavailable_relation in unavailable_relations:
                        available_relations = relation_items-MyList([unavailable_relation])
                        new_paths = self.__find_paths(graph,
                                                      available_entity_items,
                                                      # entity_items - LinkedItem.list_contains_uris(entity_items, entities),
                                                      available_relations,
                                                      edges - {edge},  # available_edges,  # ,
                                                      output_paths=output_paths.extend(edge),
                                                      used_edges=used_edges | set([edge]))
                        new_output_paths.add(new_paths, lambda path: len(path) >= len(graph.relation_items))
        return new_output_paths