|
| 1 | +# ActivityFlow |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +The `ActivityFlow` class manages the workflow of clinical recommendations according to the [FHIR Clinical Practice Guidelines (CPG) specification](https://build.fhir.org/ig/HL7/cqf-recommendations/activityflow.html#activity-lifecycle---request-phases-proposal-plan-order). It implements an activity flow as defined in the FHIR CPG IG, allowing you to guide clinical recommendations through various phases (proposal, plan, order, perform). |
| 6 | + |
| 7 | +You can start new workflows with an appropriate request resource from the generated [CarePlan](Generate-A-Care-Plan.md) or resume existing ones from any phase. |
| 8 | + |
| 9 | +**Important Considerations:** |
| 10 | + |
| 11 | +* **Thread Safety:** The `ActivityFlow` is not thread-safe. Concurrent changes from multiple threads may lead to unexpected behavior. |
| 12 | +* **Blocking Operations:** Some methods of `ActivityFlow` and its associated `Phase` interface may block the caller thread. Ensure these are called from a worker thread to avoid UI freezes. |
| 13 | + |
| 14 | +## Creating an ActivityFlow |
| 15 | + |
| 16 | +Use the appropriate `ActivityFlow.of()` factory function to create an instance. You can start anew flow with a `CPGRequestResource` or resume an existing flow from a `CPGRequestResource` or `CPGEventResource` based on the current state. |
| 17 | + |
| 18 | +**Example:** |
| 19 | +```kotlin |
| 20 | +val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine) |
| 21 | +val request = CPGMedicationRequest( medicationRequestGeneratedByCarePlan) |
| 22 | +val flow = ActivityFlow.of(repository, request) |
| 23 | +``` |
| 24 | + |
| 25 | +## Navigating phases |
| 26 | + |
| 27 | +An `ActivityFlow` progresses through a series of phases, represented by the `Phase` class. Access the current phase using `getCurrentPhase()`. |
| 28 | + |
| 29 | +**Example:** |
| 30 | +```kotlin |
| 31 | + when (val phase = flow.getCurrentPhase( )) { |
| 32 | + is Phase.ProposalPhase -> { /* Handle proposal phase */ } |
| 33 | + is Phase.PlanPhase -> { /* Handle plan phase */ } |
| 34 | + // ... other phases |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## Transitioning between the phases |
| 39 | + |
| 40 | +`ActivityFlow` provides functions to prepare and initiate the next phase: |
| 41 | + |
| 42 | +* **`prepare...()`:** Creates a new request or event for the next phase without persisting changes. |
| 43 | +* **`initiate...()`:** Creates a new phase based on the provided request/event and persists changes to the repository. |
| 44 | + |
| 45 | +**Example:** |
| 46 | +```kotlin |
| 47 | +val preparedPlan = flow.preparePlan().getOrThrow( ) |
| 48 | +// ... modify preparedPlan |
| 49 | +val planPhase = flow.initiatePlan(preparedPlan).getOrThrow( ) |
| 50 | +``` |
| 51 | + |
| 52 | +## Transitioning to Perform Phase |
| 53 | + |
| 54 | +The `preparePerform()` function requires the event type as a parameter since the perform phase can create different event resources. |
| 55 | + |
| 56 | +**Example:** |
| 57 | +```kotlin |
| 58 | +val preparedPerformEvent = flow.preparePerform( CPGMedicationDispenseEvent::class.java).getOrThrow() |
| 59 | + // ... update preparedPerformEvent |
| 60 | + val performPhase = flow.initiatePerform( preparedPerformEvent). getOrThrow( ) |
| 61 | +``` |
| 62 | + |
| 63 | +## Updating states in a phase |
| 64 | + |
| 65 | +* **`RequestPhase`:** (`ProposalPhase`, `PlanPhase`, `OrderPhase`) allows updating the request state using `update()`. |
| 66 | +```kotlin |
| 67 | +proposalPhase.update( |
| 68 | + proposalPhase.getRequestResource().apply { setStatus(Status.ACTIVE) } |
| 69 | +) |
| 70 | +``` |
| 71 | +* **`EventPhase`:** (`PerformPhase`) allows updating the event state using `update()` and completing the phase using `complete()`. |
| 72 | +```kotlin |
| 73 | +performPhase.update( |
| 74 | + performPhase.getEventResource().apply { setStatus(EventStatus.COMPLETED) } |
| 75 | +) |
| 76 | +``` |
| 77 | +## API List |
| 78 | +### Factory functions |
| 79 | + |
| 80 | +* `ActivityFlow.of(...)`: Various overloads for creating `ActivityFlow` instances with different resource types. Refer to the code for specific usage. |
| 81 | + |
| 82 | +### Phase transition API |
| 83 | + |
| 84 | +* `preparePlan()`: Prepares a plan resource. |
| 85 | +* `initiatePlan(...)`: Initiates the plan phase. |
| 86 | +* `prepareOrder()`: Prepares an order resource. |
| 87 | +* `initiateOrder(...)`: Initiates the order phase. |
| 88 | +* `preparePerform(...)`: Prepares an event resource for the perform phase. |
| 89 | +* `initiatePerform(...)`: Initiates the perform phase. |
| 90 | + |
| 91 | +### Other API |
| 92 | +* `getCurrentPhase()`: Returns the current `Phase` of the workflow. |
| 93 | + |
| 94 | +### Request phase API |
| 95 | + |
| 96 | +* `getRequestResource()`: Returns a copy of resource. |
| 97 | +* `update(..)`: Updates the resource. |
| 98 | +* `suspend(..)`: Suspends the phase. |
| 99 | +* `resume(..)`: Resumes the phase. |
| 100 | +* `enteredInError(..)`: Marks the request entered-in-error. |
| 101 | +* `reject(..)`: Rejects the phase. |
| 102 | + |
| 103 | +### Event phase API |
| 104 | + |
| 105 | +* `getEventResource()`: Returns a copy of resource. |
| 106 | +* `update(..)`: Updates the resource. |
| 107 | +* `suspend(..)`: Suspends the phase. |
| 108 | +* `resume(..)`: Resumes the phase. |
| 109 | +* `enteredInError(..)`: Marks the event entered-in-error. |
| 110 | +* `start(..)`: Starts the event. |
| 111 | +* `notDone(..)`: Marks the event not-done. |
| 112 | +* `stop(..)`: Stop the event. |
| 113 | +* `complete(..)`: Marks the event as complete. |
| 114 | + |
| 115 | + |
| 116 | +## Supported activities |
| 117 | +The library currently doesn't implement all of the activities outlined in the [activity profiles](https://build.fhir.org/ig/HL7/cqf-recommendations/profiles.html#activity-profiles). New activities may be added as per the requirement from the application developers. |
| 118 | + |
| 119 | +| Activity | Request | Event | |
| 120 | +|--------------------|-------------------------|-----------------------| |
| 121 | +| Send a message | CPGCommunicationRequest | CPGCommunication | |
| 122 | +| Order a medication | CPGMedicationRequest | CPGMedicationDispense | |
| 123 | + |
| 124 | +## Additional resources |
| 125 | + |
| 126 | +* [FHIR Clinical Practice Guidelines IG](https://build.fhir.org/ig/HL7/cqf-recommendations/) |
| 127 | +* [Activity Flow](https://build.fhir.org/ig/HL7/cqf-recommendations/activityflow.html#activity-flow) |
| 128 | +* [Activity Profiles](https://build.fhir.org/ig/HL7/cqf-recommendations/profiles.html#activity-profiles) |
0 commit comments