Neo4j Store API¶
The neo4j_store module handles Neo4j database operations including connection management and batch data insertion.
Classes¶
Neo4jConnection¶
Manages Neo4j database connections with retry logic and proper cleanup.
from ifc_graph import Neo4jConnection
# Using context manager (recommended)
with Neo4jConnection(
uri="bolt://localhost:7687",
username="neo4j",
password="password",
max_retries=3,
retry_delay=1.0
) as conn:
with conn.session() as session:
result = session.run("MATCH (n) RETURN count(n)")
print(result.single()[0])
Manages Neo4j database connections with retry logic and proper cleanup.
Source code in src/ifc_graph/neo4j_store.py
__init__(uri, username, password, max_retries=3, retry_delay=1.0)
¶
Source code in src/ifc_graph/neo4j_store.py
connect()
¶
Establish connection to Neo4j database with retry logic.
Raises:
| Type | Description |
|---|---|
DatabaseConnectionError
|
If connection cannot be established |
Source code in src/ifc_graph/neo4j_store.py
close()
¶
session()
¶
Context manager for database sessions.
Source code in src/ifc_graph/neo4j_store.py
Constructor Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
uri |
str | - | Neo4j connection URI (e.g., "bolt://localhost:7687") |
username |
str | - | Neo4j username |
password |
str | - | Neo4j password |
max_retries |
int | 3 | Maximum connection retry attempts |
retry_delay |
float | 1.0 | Delay between retries in seconds |
Methods¶
connect()¶
Establish connection to Neo4j with retry logic.
conn = Neo4jConnection(uri, username, password)
conn.connect() # Raises DatabaseConnectionError on failure
close()¶
Close the database connection.
session()¶
Context manager for database sessions.
Context Manager Usage¶
# Automatically connects and disconnects
with Neo4jConnection(uri, user, password) as conn:
with conn.session() as session:
# Use session
pass
# Connection closed automatically
Exceptions¶
DatabaseConnectionError¶
Raised when there's an error connecting to the database.
from ifc_graph import DatabaseConnectionError
try:
with Neo4jConnection(uri, user, password) as conn:
pass
except DatabaseConnectionError as e:
print(f"Connection failed: {e}")
Common causes: - Neo4j not running - Wrong URI - Invalid credentials - Network issues
DatabaseOperationError¶
Raised when a database operation fails.
from ifc_graph import DatabaseOperationError
try:
stats = save_to_neo4j(elements, ifc_file, uri, user, password)
except DatabaseOperationError as e:
print(f"Operation failed: {e}")
Common causes: - Query syntax error - Constraint violation - Transaction timeout
Functions¶
save_to_neo4j¶
The main function for saving IFC elements to Neo4j.
from ifc_graph import filter_physical_elements, save_to_neo4j
elements, ifc_file = filter_physical_elements("model.ifc")
stats = save_to_neo4j(
filtered_elements=elements,
ifc_file=ifc_file,
uri="bolt://localhost:7687",
username="neo4j",
password="password",
clear_db=False,
config={
'include_materials': True,
'include_property_sets': True,
'max_properties_per_element': 50,
}
)
print(f"Elements: {stats['elements']}")
print(f"Structures: {stats['structures']}")
print(f"Materials: {stats['materials']}")
Saves filtered IFC elements to Neo4j database using batch operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filtered_elements
|
dict
|
Dictionary of element type -> elements list |
required |
ifc_file
|
Any
|
Loaded IFC file object |
required |
uri
|
str
|
Neo4j connection URI |
required |
username
|
str
|
Neo4j username |
required |
password
|
str
|
Neo4j password |
required |
clear_db
|
bool
|
Whether to clear the database before import (default: False) |
False
|
config
|
Optional[dict]
|
Extraction configuration |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with import statistics |
Raises:
| Type | Description |
|---|---|
DatabaseConnectionError
|
If connection fails |
DatabaseOperationError
|
If database operations fail |
Source code in src/ifc_graph/neo4j_store.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
filtered_elements |
dict | - | Dictionary from filter_physical_elements() |
ifc_file |
ifcopenshell.file | - | Loaded IFC file object |
uri |
str | - | Neo4j connection URI |
username |
str | - | Neo4j username |
password |
str | - | Neo4j password |
clear_db |
bool | False |
Clear database before import |
config |
dict | None | Extraction configuration |
Return Value¶
Returns a dictionary with import statistics:
{
'elements': 150, # Element nodes created
'structures': 12, # Structure nodes created
'materials': 45, # Material relationships created
'property_sets': 0, # Reserved for future use
}
Internal Functions¶
These functions are used internally by save_to_neo4j() but can be used directly for custom workflows.
clear_database¶
Clear all nodes and relationships from the database.
from ifc_graph.neo4j_store import clear_database
with Neo4jConnection(uri, user, password) as conn:
with conn.session() as session:
clear_database(session)
Warning
This will delete all data in the database.
create_project_node¶
Create the IFC project node.
from ifc_graph.neo4j_store import create_project_node
with conn.session() as session:
project = ifc_file.by_type("IfcProject")[0]
project_id = create_project_node(session, project)
batch_create_elements¶
Create element nodes in batches for performance.
from ifc_graph.neo4j_store import batch_create_elements
with conn.session() as session:
count, spatial, materials, psets = batch_create_elements(
session,
elements=wall_elements,
config={'include_materials': True},
batch_size=500
)
batch_create_structures¶
Create structure nodes (storeys, buildings, etc.).
from ifc_graph.neo4j_store import batch_create_structures
with conn.session() as session:
count = batch_create_structures(session, spatial_info, batch_size=500)
batch_create_materials¶
Create material nodes and HAS_MATERIAL relationships.
from ifc_graph.neo4j_store import batch_create_materials
with conn.session() as session:
count = batch_create_materials(session, materials, batch_size=500)
create_metadata¶
Create import metadata node.
from ifc_graph.neo4j_store import create_metadata
with conn.session() as session:
create_metadata(session, {
'timestamp': '2024-01-15 10:30:00',
'element_count': 150,
'types': 'IfcWall, IfcDoor, IfcWindow',
'ifc_file': 'building.ifc',
'duration': 2.5,
})
Configuration Options¶
The config parameter accepts:
| Option | Type | Default | Description |
|---|---|---|---|
include_materials |
bool | True |
Create material nodes and relationships |
include_property_sets |
bool | True |
Extract and store property sets |
max_properties_per_element |
int | 50 |
Maximum properties per element |
Example: Custom Database Operations¶
from ifc_graph import (
Neo4jConnection,
filter_physical_elements,
)
from ifc_graph.neo4j_store import (
clear_database,
create_project_node,
batch_create_elements,
batch_create_structures,
batch_create_materials,
)
# Load IFC
elements, ifc_file = filter_physical_elements("model.ifc")
# Custom database operations
with Neo4jConnection(uri, user, password) as conn:
with conn.session() as session:
# Clear existing data
clear_database(session)
# Create project
project = ifc_file.by_type("IfcProject")[0]
project_id = create_project_node(session, project)
# Process elements type by type
for elem_type, elems in elements.items():
print(f"Processing {len(elems)} {elem_type}...")
count, spatial, materials, psets = batch_create_elements(
session, elems, config={}, batch_size=100
)
batch_create_structures(session, spatial)
batch_create_materials(session, materials)
print("Done!")
See Also¶
- Element Filter API - IFC extraction
- Query Loader API - Cypher query management
- Graph Model - Database schema