PPQ Graph Searching Engine. Args: GraphCommandProcessor ([type]): [description]
| 388 | |
| 389 | |
| 390 | class SearchableGraph(GraphCommandProcessor): |
| 391 | """PPQ Graph Searching Engine. |
| 392 | |
| 393 | Args: |
| 394 | GraphCommandProcessor ([type]): [description] |
| 395 | """ |
| 396 | def __init__(self, graph: Union[BaseGraph, Callable]) -> None: |
| 397 | super().__init__(graph) |
| 398 | self._cache = {} |
| 399 | |
| 400 | def process(self, command: GraphCommand) -> Any: |
| 401 | if isinstance(command, TraversalCommand): |
| 402 | if command.command_type == GraphCommandType.TRAVERSAL_PATTERN_MATCHING: |
| 403 | return self.path_matching( |
| 404 | sp_expr=command._sp_expr, |
| 405 | rp_expr=command._rp_expr, |
| 406 | ep_expr=command._ep_expr, |
| 407 | direction=command._direction) |
| 408 | else: |
| 409 | return self.opset_matching( |
| 410 | sp_expr=command._sp_expr, |
| 411 | rp_expr=command._rp_expr, |
| 412 | ep_expr=command._ep_expr, |
| 413 | direction=command._direction) |
| 414 | else: |
| 415 | raise TypeError( |
| 416 | 'To execute a traversal-based pattern matching, a TraversalCommand is required here.\n' |
| 417 | 'Initialize your own TraversalCommand instance for invoking ' |
| 418 | 'this function rather than using plain GraphCommand Please.') |
| 419 | |
| 420 | def _acceptable_command_types(self) -> List[GraphCommandType]: |
| 421 | return [ |
| 422 | GraphCommandType.TRAVERSAL_PATTERN_MATCHING, |
| 423 | GraphCommandType.TRAVERSAL_OPSET_MATCHING, |
| 424 | GraphCommandType.CONCAT_MATCHING, |
| 425 | GraphCommandType.ACTIVATION_MATCHING, |
| 426 | ] |
| 427 | |
| 428 | def _path_matching( |
| 429 | self, |
| 430 | start_point: Operation, |
| 431 | rp_expr: RelayPattern, |
| 432 | ep_expr: PointPattern, |
| 433 | direction: str = 'up' |
| 434 | ) -> List[Path]: |
| 435 | # memoization based searching. |
| 436 | if start_point in self._cache: return self._cache[start_point] |
| 437 | |
| 438 | # find next operations with given direction |
| 439 | if direction == 'up': following_ops = self.graph.get_upstream_operations(start_point) |
| 440 | else: following_ops = self.graph.get_downstream_operations(start_point) |
| 441 | |
| 442 | ret_collection = [] |
| 443 | for op in following_ops: |
| 444 | # if operation is a valid end point, add it to path and stop further searching. |
| 445 | if ep_expr(op): |
| 446 | ret_collection.append(Path(start_point).append(op)) |
| 447 |
no outgoing calls
no test coverage detected