File size: 2,533 Bytes
64ed965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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")