Example #1
0
 def mask_dnode(self, g: Graph) -> Graph:
     """deprecated"""
     g2 = Graph(True, True, True, g.get_n_nodes(), g.get_n_links())
     for n in g.iter_nodes():
         assert g2.add_new_node(
             n.type, n.label if n.type == GraphNodeType.CLASS_NODE else
             b"DataNode").id == n.id
     for e in g.iter_links():
         assert g2.add_new_link(e.type, e.label, e.source_id,
                                e.target_id).id == e.id
     return g2
Example #2
0
    def build(self, g: Graph) -> GraphExplorer:
        # TODO: can make it more efficient by giving estimation to graph explorer
        g_explorer = GraphExplorer()
        for node in g.iter_nodes():
            g_explorer.real_add_new_node(GraphNodeHop(0), node.type,
                                         node.label)
        for link in g.iter_links():
            g_explorer.add_new_link(link.type, link.label, link.source_id,
                                    link.target_id)

        self.explore(g_explorer)
        return g_explorer
Example #3
0
    def convert_graph(graph: Graph):
        node_index: Dict[int, Node] = {}

        for v in graph.iter_nodes():
            type = Node.DATA_NODE if v.is_data_node() else Node.CLASS_NODE
            node_index[v.id] = Node(v.id, type, v.label)

        for l in graph.iter_links():
            if data_node_mode == 2:
                if node_index[l.target_id].type == Node.DATA_NODE:
                    # ignore data node
                    continue

            link = Link(l.id, l.label, l.source_id, l.target_id)
            Node.add_outgoing_link(node_index[l.source_id], link)
            Node.add_incoming_link(node_index[l.target_id], link)

        if data_node_mode == DataNodeMode.IGNORE_DATA_NODE:
            for v2 in [
                    v for v in node_index.values() if v.type == Node.DATA_NODE
            ]:
                del node_index[v2.id]

        if data_node_mode == DataNodeMode.IGNORE_LABEL_DATA_NODE:
            # we convert label of node to DATA_NODE
            leaf_source_nodes: Set[Node] = set()
            for v in [
                    v for v in node_index.values() if v.type == Node.DATA_NODE
            ]:
                assert len(v.incoming_links) == 1
                link = v.incoming_links[0]
                source = node_index[link.source_id]
                leaf_source_nodes.add(source)

            for node in leaf_source_nodes:
                link_label_count = {}
                for link in node.outgoing_links:
                    target = node_index[link.target_id]
                    if target.type == Node.DATA_NODE:
                        if link.label not in link_label_count:
                            link_label_count[link.label] = 0

                        link_label_count[link.label] += 1
                        target.label = 'DATA_NODE' + str(
                            link_label_count[link.label])

        return node_index