Skip to main content

Library untuk mengubah BPMN JSON menjadi Cypher queries untuk Neo4j

Project description

BPMN → Neo4j Graph Transformation Library

A Python library for converting BPMN JSON into a Neo4j graph database.
It provides parsing, schema validation, semantic validation, transformation to Cypher, and execution in Neo4j.


📂 Project Structure

bpmn-neo4j-lib/
 ├── parsers/            # JSON parsing
 ├── validators/         # Schema + semantic validation
 ├── transformers/       # GraphTransformer, node/edge builders
 ├── neo4j/              # Neo4j executor
 ├── utils/              # Logger
 └── exceptions/         # Custom error handling

📦 Installation

pip install bpmn-neo4j-lib

Requirements:

  • Python 3.10+
  • Neo4j 5.x (local or remote)

🚀 Usage

You can use the library step by step, or orchestrate the whole process with your own wrapper.


🔹 Step-by-step Example

1. Load a JSON file

from bpmn_neo4j.parsers.json_parser import load_json

data = load_json("examples/sample_bpmn.json")

✅ Reads a BPMN JSON file.
If the file is broken (invalid JSON), the parser attempts auto-repair and saves a _fixed_by_<method>.json file.


2. Validate schema

from bpmn_neo4j.validators.schema_validator import validate_schema

validated = validate_schema(data, auto_fix=True)

✅ Ensures JSON follows the official BPMN schema.
auto_fix=True automatically assigns missing IDs, removes duplicates, and fills required fields.


3. Validate semantics

from bpmn_neo4j.validators.bpmn_semantic_validator import validate_semantics

validate_semantics(validated)

✅ Checks BPMN Method & Style rules:

  • All flows have valid source/target.
  • Start/End events are consistent.
  • Activities and gateways follow BPMN rules.
  • Detects orphan nodes or invalid boundary/message flows.

4. Transform JSON into Cypher

from bpmn_neo4j.transformers.graph_transformer import GraphTransformer

transformer = GraphTransformer(json_data=validated)
cypher_lines = transformer.transform()

✅ Converts BPMN nodes & flows into Cypher queries:

  • Creates nodes: Activities, Events, Pools, Lanes.
  • Creates edges: Sequence Flows.
  • Keeps track of process_id, node_count, and edge_count.

5. Connect to Neo4j

from bpmn_neo4j.neo4j.neo4j_repositories import Neo4jExecutor

executor = Neo4jExecutor(
    uri="bolt://localhost:7687",
    user="neo4j",
    password="12345678"
)

✅ Manages Neo4j connection.
Supports health checks, index creation, and batch execution of queries.


6. Setup indexes

executor.setup_indexes()

✅ Ensures Neo4j has indexes for efficient querying:

  • (Activity {id})
  • (Event {id})
  • (Pool {id})
  • (Lane {id})

7. Run batch Cypher queries

executor.run_batch(
    cypher_lines,
    reset=True,          # clears database first
    batch_size=20,       # queries per transaction
    process_id=transformer.process_id
)

✅ Executes generated Cypher queries in Neo4j:

  • Resets DB if reset=True.
  • Uses batching for efficiency.
  • Associates nodes with process_id.

8. Get graph metrics

print(executor.get_metrics(transformer.process_id))
executor.close()

✅ Retrieves graph statistics:

  • Node counts (activities, events, start/end).
  • Path count and average path length.
  • Useful for analysis and validation.

🔹 Output Example

{
  "total_nodes": 7,
  "activities": 5,
  "events": 2,
  "start_events": 1,
  "end_events": 1,
  "paths": 1,
  "avg_path_length": 7.0
}

🔹 Full Example

from bpmn_neo4j.parsers.json_parser import load_json
from bpmn_neo4j.validators.schema_validator import validate_schema
from bpmn_neo4j.validators.bpmn_semantic_validator import validate_semantics
from bpmn_neo4j.transformers.graph_transformer import GraphTransformer
from bpmn_neo4j.neo4j.neo4j_repositories import Neo4jExecutor

# 1. Load BPMN JSON
data = load_json("examples/sample_bpmn.json")

# 2. Schema validation
validated = validate_schema(data, auto_fix=True)

# 3. Semantic validation
validate_semantics(validated)

# 4. Transform to Cypher
transformer = GraphTransformer(json_data=validated)
cypher_lines = transformer.transform()

# 5. Execute in Neo4j
executor = Neo4jExecutor("bolt://localhost:7687", "neo4j", "12345678")
executor.setup_indexes()
executor.run_batch(cypher_lines, reset=True, batch_size=20, process_id=transformer.process_id)

# 6. Metrics
print(executor.get_metrics(transformer.process_id))

executor.close()

🧪 Running Tests

pytest tests/

📜 License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bpmn_neo4j_lib_test-0.0.6a0.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bpmn_neo4j_lib_test-0.0.6a0-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file bpmn_neo4j_lib_test-0.0.6a0.tar.gz.

File metadata

  • Download URL: bpmn_neo4j_lib_test-0.0.6a0.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for bpmn_neo4j_lib_test-0.0.6a0.tar.gz
Algorithm Hash digest
SHA256 ddbbe68c078f3afa72989f69e305694e1b80e6443388983ca68ac2a618c0c328
MD5 f5c04126fce73988f94deedc4746f438
BLAKE2b-256 ce30daec1879e97f893f88f30279d788cff436579c820fe582ae9ba60c64c10f

See more details on using hashes here.

File details

Details for the file bpmn_neo4j_lib_test-0.0.6a0-py3-none-any.whl.

File metadata

File hashes

Hashes for bpmn_neo4j_lib_test-0.0.6a0-py3-none-any.whl
Algorithm Hash digest
SHA256 ddca6e1b78f9dbeae41bf4f04e63269d1751db30f6c027287fd1f6972384c39d
MD5 c852e5ccc2fdd94c4516fb600781958f
BLAKE2b-256 1351d8ca1f9ea90123d9de31a71966ff433350edf3b666a412afa36bade3e058

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page