mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
- Add new Graph_Store.ipynb introduction notebook - Update Advanced_Graph_Analytics.ipynb with graph store persistence - Update Fraud_Detection.ipynb with graph database storage - Update Transaction_Network_Analysis.ipynb with blockchain graph storage - Update Criminal_Network_Analysis.ipynb with criminal network persistence - Update Welcome_to_Semantica.ipynb with Graph Store module documentation - Update docs/cookbook.md, docs/examples.md, docs/CodeExamples.md - Sync all notebooks to docs/cookbook directory
11 KiB
11 KiB
In [ ]:
from semantica.graph_store import GraphStore
# Option 1: Neo4j (requires Neo4j server)
# store = GraphStore(
# backend="neo4j",
# uri="bolt://localhost:7687",
# user="neo4j",
# password="password"
# )
# Option 2: KuzuDB (embedded - no server required)
store = GraphStore(
backend="kuzu",
database_path="./demo_graph_db"
)
# Option 3: FalkorDB (requires Redis/FalkorDB server)
# store = GraphStore(
# backend="falkordb",
# host="localhost",
# port=6379,
# graph_name="demo_graph"
# )
# Connect to the database
store.connect()
print("Connected to graph store!")
In [ ]:
# Create individual nodes
apple = store.create_node(
labels=["Company"],
properties={"name": "Apple Inc.", "founded": 1976, "industry": "Technology"}
)
print(f"Created company node: {apple}")
tim_cook = store.create_node(
labels=["Person"],
properties={"name": "Tim Cook", "title": "CEO", "age": 63}
)
print(f"Created person node: {tim_cook}")
cupertino = store.create_node(
labels=["Location"],
properties={"name": "Cupertino", "state": "California", "country": "USA"}
)
print(f"Created location node: {cupertino}")
In [ ]:
# Create multiple nodes in batch
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
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 CEO relationship: {ceo_rel}")
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 location relationship: {location_rel}")
In [ ]:
# Get all Company nodes
companies = store.get_nodes(labels=["Company"], limit=10)
print(f"Found {len(companies)} companies:")
for company in companies:
print(f" - {company.get('properties', {}).get('name', 'Unknown')}")
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:
print(f" - Type: {rel.get('type')}, Properties: {rel.get('properties')}")
In [ ]:
# Execute a Cypher query
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", []):
print(f" {record}")
In [ ]:
# Parameterized query
results = store.execute_query(
"MATCH (c:Company) WHERE c.founded > $year RETURN c.name, c.founded",
parameters={"year": 1990}
)
print("Companies founded after 1990:")
for record in results.get("records", []):
print(f" {record}")
In [ ]:
# Get neighbors of a node
neighbors = store.get_neighbors(
node_id=apple["id"],
direction="both",
depth=2
)
print(f"Found {len(neighbors)} neighbors (up to depth 2):")
for neighbor in neighbors:
print(f" - {neighbor.get('properties', {}).get('name', 'Unknown')}")
In [ ]:
# Find shortest path (if nodes are connected)
path = store.shortest_path(
start_node_id=tim_cook["id"],
end_node_id=cupertino["id"],
max_depth=5
)
if path:
print(f"Shortest path length: {path.get('length')}")
print(f"Nodes in path: {len(path.get('nodes', []))}")
else:
print("No path found")
In [ ]:
stats = store.get_stats()
print("Graph Statistics:")
print(f" Node count: {stats.get('node_count', 'N/A')}")
print(f" Relationship count: {stats.get('relationship_count', 'N/A')}")
print(f" Label counts: {stats.get('label_counts', {})}")
print(f" Relationship types: {stats.get('relationship_type_counts', {})}")
In [ ]:
# Close the connection
store.close()
print("Connection closed.")