Beyond the terminal, NanoCERN provides a clean Python API for integrating deterministic safety checks into your own applications, simulations, or AI pipelines.
1. Importing the Reactor
“`python
from nanocern import MinimalReactor, State
reactor = MinimalReactor()
“`
2. Loading Knowledge Units
You can load KUs from the local library or your own custom JSON definitions.
“`python
reactor.load_ku(“drugs/IBUPROFEN.ku”)
“`
3. Executing a Reaction
The `execute_reaction` method is the primary entry point. It returns a `NewState` on success or an `NKU` on failure.
“`python
state = State(variables={
“egfr_ml_min”: 25, # Low kidney function
“dose_mg”: 400
})
new_state, nku = reactor.execute_reaction(“IBUPROFEN-RENAL”, state)
if nku:
print(f”Safety Rejection: {nku.violated_envelope}”)
# Handle safety block
else:
print(“State accepted. Proceeding with simulation/prescription.”)
“`
4. Custom Envelopes
Developers can define their own envelopes for proprietary domains (e.g., Financial constraints, Engineering safety).
“`json
{
“id”: “BRIDGE-LOAD-SAFETY”,
“applies_if”: {
“load_tons <": 50,
"wind_speed_mph <": 80
}
}
```
5. Performance Tips
- Pre-loading: Load all KUs into memory at startup for <1ms checks.
- Parallel Cycles: Use `execute_batch` (coming in v2.1) for high-throughput simulation auditing.
[Back to Documentation Hub →](/nanocern-docs-hub/)