import requests def print_tree(node, prefix="", visited=None, root_id="Healthcare"): """Recursive function to print all relationships in a hierarchical tree format.""" if visited is None: visited = set() node_id = node.get("node_id") if not node_id: print(f"{prefix}(unknown node)") return if node_id in visited or (node_id == root_id and prefix != ""): print(f"{prefix}(already listed) {node_id}") return visited.add(node_id) relationship_label = f"({node.get('relationship', '')})" if node.get("relationship") else "" print(f"{prefix}{node_id} {relationship_label}") # Check if there are descendants to traverse descendants = node.get("descendants", []) if not descendants: print(f"{prefix}(no further descendants)") else: for i, child in enumerate(descendants): new_prefix = f"{prefix}├── " if i < len(descendants) - 1 else f"{prefix}└── " print_tree(child, new_prefix, visited, root_id) def inspect_and_print_relationships(node_id): """Inspect and display relationships for a specified node in a tree format.""" response = requests.get(f"{base_url}/inspect_relationships?node_id={node_id}") traversal_hierarchy = response.json() print(f"\nTraversal Response for {node_id}:", traversal_hierarchy) # Debugging line # Ensure the root node structure matches expectations if "node_id" not in traversal_hierarchy: traversal_hierarchy = { "node_id": node_id, "descendants": traversal_hierarchy.get("child_relations", []) } print(f"\nInspect Relationships for {node_id} (Full Hierarchy):") print_tree(traversal_hierarchy) # Base URL for API base_url = "http://localhost:5000" # Step 1: Load Graph (Specify the graph to load, e.g., PHSA/340B section) print("\n--- Testing Graph Loading ---") graph_data = {"graph_file": "graphs/PHSA/phsa_sec_340b.json"} response = requests.post(f"{base_url}/load_graph", json=graph_data) print("Load Graph Response:", response.json()) # Step 2: Create a Test Node print("\n--- Testing Node Creation ---") create_data = { "node_id": "patient_123", "data": {"name": "John Doe", "age": 45, "medical_conditions": ["hypertension", "diabetes"]}, "domain": "Healthcare", "type": "Patient" } response = requests.post(f"{base_url}/create_node", json=create_data) print("Create Node Response:", response.json()) # Step 3: Inspect Relationships for Node (Healthcare) - Full hierarchical tree print("\n--- Testing Inspect Relationships for Node (Healthcare) ---") inspect_and_print_relationships("Healthcare") # Step 4: Inspect Relationships for Specific Node (Section 340B) print("\n--- Testing Inspect Relationships for Node (Section 340B) ---") inspect_and_print_relationships("Section 340B")