mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
20 KiB
20 KiB
In [ ]:
!pip install semantica
In [ ]:
from semantica.graph_store import GraphStore
# Neo4j AuraDB Connection Details
# Replace these values with your actual AuraDB credentials
store = GraphStore(
backend="neo4j",
uri="Your URI", # Your AuraDB Instance URI
user="neo4j",
password="Your Password" # Please enter your password here
)
# Connect to the database
store.connect()
print("Connected to graph database successfully!")In [ ]:
# Create individual nodes with labels and properties
apple = store.create_node(
labels=["Company"],
properties={"name": "Apple Inc.", "founded": 1976, "industry": "Technology"}
)
print(f"Created company node: {apple.get('properties', {}).get('name')} (ID: {apple.get('id')})")
tim_cook = store.create_node(
labels=["Person"],
properties={"name": "Tim Cook", "title": "CEO", "age": 63}
)
print(f"Created person node: {tim_cook.get('properties', {}).get('name')} (ID: {tim_cook.get('id')})")
cupertino = store.create_node(
labels=["Location"],
properties={"name": "Cupertino", "state": "California", "country": "USA"}
)
print(f"Created location node: {cupertino.get('properties', {}).get('name')} (ID: {cupertino.get('id')})")
In [ ]:
# Create multiple nodes in batch (more efficient for large datasets)
other_companies = store.create_nodes([
{"labels": ["Company"], "properties": {"name": "Microsoft", "founded": 1975}},
{"labels": ["Company"], "properties": {"name": "Google", "founded": 1998}},
{"labels": ["Company"], "properties": {"name": "Amazon", "founded": 1994}},
])
print(f"Created {len(other_companies)} company nodes in batch")
In [ ]:
# Create relationships between nodes
ceo_rel = store.create_relationship(
start_node_id=tim_cook["id"],
end_node_id=apple["id"],
rel_type="CEO_OF",
properties={"since": 2011}
)
print(f"Created relationship: {ceo_rel.get('type')} (ID: {ceo_rel.get('id')})")
location_rel = store.create_relationship(
start_node_id=apple["id"],
end_node_id=cupertino["id"],
rel_type="HEADQUARTERED_IN",
properties={"since": 1977}
)
print(f"Created relationship: {location_rel.get('type')} (ID: {location_rel.get('id')})")
In [ ]:
# Get nodes by label
companies = store.get_nodes(labels=["Company"], limit=10)
print(f"Found {len(companies)} companies:")
for company in companies:
name = company.get('properties', {}).get('name', 'Unknown')
founded = company.get('properties', {}).get('founded', 'N/A')
print(f" - {name} (founded: {founded})")
# Get a specific node by ID
if apple.get('id'):
node = store.get_node(node_id=apple["id"])
print(f"\nRetrieved node by ID: {node.get('properties', {}).get('name')}")
In [ ]:
# Get relationships for a node
relationships = store.get_relationships(node_id=apple["id"], direction="both")
print(f"Found {len(relationships)} relationships for Apple:")
for rel in relationships:
rel_type = rel.get('type', 'Unknown')
props = rel.get('properties', {})
print(f" - {rel_type}: {props}")
# Get relationships by type and direction
if tim_cook.get('id'):
outgoing = store.get_relationships(
node_id=tim_cook["id"],
rel_type="CEO_OF",
direction="out"
)
print(f"\nOutgoing CEO_OF relationships: {len(outgoing)}")
In [ ]:
# Execute a Cypher query to find CEO relationships
results = store.execute_query("""
MATCH (p:Person)-[r:CEO_OF]->(c:Company)
RETURN p.name as person, c.name as company, r.since as since
""")
print("CEO Relationships:")
for record in results.get("records", []):
person = record.get('person', 'Unknown')
company = record.get('company', 'Unknown')
since = record.get('since', 'N/A')
print(f" - {person} is CEO of {company} since {since}")
In [ ]:
# Parameterized query (safer and more efficient)
results = store.execute_query(
"MATCH (c:Company) WHERE c.founded > $year RETURN c.name, c.founded ORDER BY c.founded",
parameters={"year": 1990}
)
print("Companies founded after 1990:")
for record in results.get("records", []):
name = record.get('c.name', 'Unknown')
founded = record.get('c.founded', 'N/A')
print(f" - {name} (founded: {founded})")
In [ ]:
# Get neighbors of a node (traverse the graph)
if apple.get('id'):
neighbors = store.get_neighbors(
node_id=apple["id"],
direction="both",
depth=2
)
print(f"Found {len(neighbors)} neighbors (up to depth 2) for Apple:")
for neighbor in neighbors:
name = neighbor.get('properties', {}).get('name', 'Unknown')
labels = neighbor.get('labels', [])
print(f" - {name} ({', '.join(labels)})")
In [ ]:
# Find shortest path between two nodes
if tim_cook.get('id') and cupertino.get('id'):
path = store.shortest_path(
start_node_id=tim_cook["id"],
end_node_id=cupertino["id"],
max_depth=5
)
if path:
print(f"Shortest path found:")
print(f" - Path length: {path.get('length')}")
print(f" - Nodes in path: {len(path.get('nodes', []))}")
print(f" - Relationships: {len(path.get('relationships', []))}")
else:
print("No path found between the nodes")
In [ ]:
# Update node properties (merge mode - adds/updates properties)
if tim_cook.get('id'):
updated = store.update_node(
node_id=tim_cook["id"],
properties={"age": 64, "title": "CEO & President"},
merge=True # Merge with existing properties
)
print(f"Updated node: {updated.get('properties', {}).get('name')}")
print(f" New age: {updated.get('properties', {}).get('age')}")
print(f" New title: {updated.get('properties', {}).get('title')}")
# Example: Replace all properties (merge=False)
# updated = store.update_node(
# node_id=node_id,
# properties={"name": "New Name"},
# merge=False # Replace all properties
# )
In [ ]:
# Delete a relationship
if location_rel.get('id'):
deleted = store.delete_relationship(rel_id=location_rel["id"])
if deleted:
print(f"Deleted relationship (ID: {location_rel['id']})")
# Delete a node (with detach=True to also delete its relationships)
# WARNING: This will delete the node and all its relationships
# Uncomment to test:
# if cupertino.get('id'):
# deleted = store.delete_node(node_id=cupertino["id"], detach=True)
# if deleted:
# print(f"Deleted node: {cupertino.get('properties', {}).get('name')}")
print("\nTip: Use detach=True to delete a node and all its relationships")
print(" Use detach=False to only delete the node (fails if relationships exist)")
In [ ]:
# Get comprehensive graph statistics
stats = store.get_stats()
print("Graph Statistics:")
print(f" Total nodes: {stats.get('node_count', 'N/A')}")
print(f" Total relationships: {stats.get('relationship_count', 'N/A')}")
print(f"\nNode labels:")
for label, count in stats.get('label_counts', {}).items():
print(f" - {label}: {count} nodes")
print(f"\nRelationship types:")
for rel_type, count in stats.get('relationship_type_counts', {}).items():
print(f" - {rel_type}: {count} relationships")
In [ ]:
# Using convenience functions (alternative to class methods)
from semantica.graph_store import (
create_node,
create_relationship,
get_nodes,
execute_query,
shortest_path
)
# These functions work with a default store instance
# For this example, we'll continue using the store instance we created
# Example: Using convenience functions
# node = create_node(
# labels=["Person"],
# properties={"name": "Alice", "age": 30}
# )
print("Convenience functions available:")
print(" - create_node, create_nodes")
print(" - create_relationship, create_relationships")
print(" - get_nodes, get_relationships")
print(" - update_node, delete_node")
print(" - execute_query, shortest_path, get_neighbors")
print(" - run_analytics")
In [ ]:
# Create an index on a node property for faster lookups
# This is especially useful for frequently queried properties
index_created = store.create_index(
label="Company",
property_name="name",
index_type="btree" # Default index type
)
if index_created:
print("Created index on Company.name for faster queries")
else:
print("Index may already exist or not be supported by this backend")
# Note: Index creation support varies by backend
# Neo4j: Full support for various index types
# FalkorDB: Limited index support
In [ ]:
# Close the connection
store.close()
print("Connection closed successfully")