Activity Flow
Manages the workflow of clinical recommendations according to the FHIR Clinical Practice Guidelines (CPG) specification. This class implements an activity flow, allowing you to take proposals and guide them through various phases (proposal, plan, order, perform) of a clinical recommendation. You can also resume existing workflows from any phase.
NOTE
The
prepare
andinitiate
apis ofActivityFlow
and all apis ofPhase
interface may block the caller thread and should only be called from a worker thread.The
ActivityFlow
is not thread safe and concurrent changes to the flow/phase with multiple threads may produce undesired results.
Creating an ActivityFlow:
Use appropriate ActivityFlow.of()
factory function to create an instance. You can start a new flow with a CPGRequestResource
or resume an existing flow from a CPGRequestResource
or CPGEventResource
based on the last state of the flow.
val request = CPGMedicationRequest(medicationRequestGeneratedByCarePlan)
val flow = ActivityFlow.of(repository, request)
Navigating Phases:
An ActivityFlow
progresses through a series of phases, represented by the Phase
class. You can access the current phase using getCurrentPhase()
.
when (val phase = flow.getCurrentPhase( ) ) {
is Phase.ProposalPhase -> // Handle proposal phase
is Phase.PlanPhase -> // Handle plan phase
is Phase.OrderPhase -> // Handle order phase
is Phase.PerformPhase -> // Handle perform phase
}
Transitioning Between Phases:
ActivityFlow provides functions to prepare and initiate the next phase.
The prepare api creates a new request or event based on the phase and returns it back to you. It doesn't make any changes to the current phase request and also doesn't persist anything to the repository.
The initiate api creates a new phase based on the current phase and provided request/event. It does make changes to the current phase request and the provided request and persists them to the repository. For example, to move from the proposal phase to the plan phase:
val preparePlanResult = flow.getCurrentPhase( ).preparePlan()
if (preparePlanResult.isFailure) {
// Handle failure
}
val preparedPlan = preparePlanResult.getOrThrow()
// ... modify preparedPlan
val planPhase = flow.getCurrentPhase().initiatePlan(preparedPlan)
Note: The specific prepare
and initiate
functions available depend on the current phase.
Transitioning to Perform Phase:
Since the perform creates a CPGEventResource and the same flow could create different event resources, you need to provide the appropriate event type as a parameter to the preparePerform.
Example:
// Prepare and initiate the perform phase
val preparedPerformEvent = flow.getCurrentPhase().preparePerform(CPGMedicationDispenseEvent::class.java) . getOrThrow( )
// update preparedPerformEvent
val performPhase = flow.getCurrentPhase( ) . initiatePerform(preparedPerformEvent) . getOrThrow( )
Updating states in a phase:
ProposalPhase
, PlanPhase
and OrderPhase
are all a type of Phase.RequestPhase
and allows you to update state of the request.
val planPhase = flow.getCurrentPhase().initiatePlan(preparedPlan)
val medicationRequest = planPhase.getRequestResource()
// update medicationRequest
planPhase.update(updated medicationRequest)
PerformPhase
is a type of Phase.EventPhase
and allows you to update the state of the event.
val performPhase = ...
val medicationDispense = performPhase.getEventResource()
// update medicationDispense
performPhase.update(updated medicationDispense)
performPhase.complete()
Functions
Returns the current phase of the flow. The users may check the type of flow by calling Phase.getPhaseName on the getCurrentPhase and then cast it to appropriate classes.
Initiates an order phase based on the state of the currentPhase and preparePlan. This api will persist the preparedOrder into repository.
Initiate a perform phase based on the state of the currentPhase and preparePlan. This api will persist the preparedEvent into repository.
Initiates a plan phase based on the state of the currentPhase and preparedPlan. This api will persist the preparedPlan into repository.
Prepares an order resource based on the state of the currentPhase and returns it to the caller without persisting any changes into repository.
Prepares an event resource based on the state of the currentPhase and returns it to the caller without persisting any changes into repository.
Prepares a plan resource based on the state of the currentPhase and returns it to the caller without persisting any changes into repository.