Singularity / scripts /dev /2.3_test_graph_operation.py
SlappAI's picture
dev scripts
64ed965
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}/traverse_node?node_id={node_id}&direction=down")
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("descendants", [])
}
print(f"\nInspect Relationships for {node_id} (Full Hierarchy):")
print_tree(traversal_hierarchy)
# Test API Endpoints
base_url = "http://localhost:5000"
# Step 1: Load Graph
print("\n--- Testing Graph Loading ---")
response = requests.post(f"{base_url}/load_graph")
print("Load Graph Response:", response.json())
# Step 2: Create 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")