|
1 | | -import logging |
2 | | - |
3 | 1 | import networkx as nx |
| 2 | + |
4 | 3 | from controller.metakg import MetaKG |
5 | 4 | from model import ConsolidatedMetaKGDoc |
6 | 5 |
|
7 | | -logger = logging.basicConfig(level=logging.INFO, filename="missing_bte.log") |
8 | | - |
9 | | - |
10 | 6 | class MetaKGPathFinder: |
11 | 7 | def __init__(self, query_data=None, expanded_fields=None): |
12 | 8 | """ |
@@ -104,43 +100,51 @@ def get_paths(self, cutoff=2, api_details=False, predicate_filter=None, bte=Fals |
104 | 100 | If True, includes full details of the 'api' in the result. |
105 | 101 | - predicate_filter: list (default=None) |
106 | 102 | A list of predicates to filter the results by. |
107 | | -
|
| 103 | + - bte: bool (default=False) |
| 104 | + If True, includes BTE information in the result. |
108 | 105 | Returns: |
109 | 106 | - all_paths_with_edges: list of dict |
110 | 107 | A list containing paths and their edge information for all subject-object pairs. |
111 | 108 | """ |
112 | 109 |
|
113 | 110 | all_paths_with_edges = [] |
114 | 111 |
|
115 | | - # Predicate Filter Setup |
116 | 112 | predicate_filter_set = set(predicate_filter) if predicate_filter else None |
| 113 | + |
117 | 114 | if 'predicate' in self.expanded_fields and self.expanded_fields['predicate']: |
118 | 115 | predicate_filter_set.update(self.expanded_fields['predicate']) |
119 | 116 |
|
120 | | - # Graph iteration over subject-object pairs |
121 | | - for subject in self.expanded_fields["subject"]: |
122 | | - for object in self.expanded_fields["object"]: |
123 | | - try: |
124 | | - # Check if a path exists between the subject and object |
125 | | - if nx.has_path(self.G, subject, object): |
126 | | - raw_paths = nx.all_simple_paths(self.G, source=subject, target=object, cutoff=cutoff) |
127 | | - for path in raw_paths: |
128 | | - paths_data = {"path": path, "edges": []} |
129 | | - edge_added = False |
130 | | - for i in range(len(path) - 1): |
131 | | - source_node = path[i] |
132 | | - target_node = path[i + 1] |
133 | | - edge_key = f"{source_node}-{target_node}" |
134 | | - edge_data = self.predicates.get(edge_key, []) |
135 | | - |
136 | | - for data in edge_data: |
137 | | - if predicate_filter_set and data["predicate"] not in predicate_filter_set: |
138 | | - continue |
139 | | - paths_data = self.build_edge_results(paths_data, data, api_details, source_node, target_node, bte) |
140 | | - edge_added = True |
141 | | - if edge_added: |
142 | | - all_paths_with_edges.append(paths_data) |
143 | | - except Exception: |
144 | | - continue |
145 | | - |
146 | | - return all_paths_with_edges |
| 117 | + try: |
| 118 | + # Graph iteration over subject-object pairs |
| 119 | + for subject in self.expanded_fields["subject"]: |
| 120 | + for object in self.expanded_fields["object"]: |
| 121 | + if subject not in self.G: |
| 122 | + return { "error": f"Subject node {subject} is not found in the MetaKG" } |
| 123 | + if object not in self.G: |
| 124 | + return { "error": f"Object node {object} is not found in the MetaKG" } |
| 125 | + try: |
| 126 | + # Check if a path exists between the subject and object |
| 127 | + if nx.has_path(self.G, subject, object): |
| 128 | + raw_paths = nx.all_simple_paths(self.G, source=subject, target=object, cutoff=cutoff) |
| 129 | + for path in raw_paths: |
| 130 | + paths_data = {"path": path, "edges": []} |
| 131 | + edge_added = False |
| 132 | + for i in range(len(path) - 1): |
| 133 | + source_node = path[i] |
| 134 | + target_node = path[i + 1] |
| 135 | + edge_key = f"{source_node}-{target_node}" |
| 136 | + edge_data = self.predicates.get(edge_key, []) |
| 137 | + |
| 138 | + for data in edge_data: |
| 139 | + if predicate_filter_set and data["predicate"] not in predicate_filter_set: |
| 140 | + continue |
| 141 | + paths_data = self.build_edge_results(paths_data, data, api_details, source_node, target_node, bte) |
| 142 | + edge_added = True |
| 143 | + if edge_added: |
| 144 | + all_paths_with_edges.append(paths_data) |
| 145 | + except nx.exception.NodeNotFound as node_err: |
| 146 | + return { "error": node_err } |
| 147 | + return all_paths_with_edges |
| 148 | + |
| 149 | + except Exception as e: |
| 150 | + return { "error": e } |
0 commit comments