Using Python DSL (Domain Specific Language) to Solve Follow Up Complexity

by Asreedh R

In our iConnect patient recruitment platform, patients can email trial investigators. One of the challenges has been tracking the outcome of each patient email. To track this outcome we have developed a “Referral Follow up Module”. The main goal of this module is to track what happens to a patient referral till we have a final outcome on that referral without causing too much burden on either the patient or the trial investigator to do the outcome reporting. We have categorized follow up into three types:

  1. Connection Follow Up
  2. Pre-Contact Status Follow Up
  3. Post Contact Success Status Follow Up

Connection Follow Up:

This kind of follow up is done when there is no connection between investigator and patient after a certain period of time (investigator doesn’t contact patient as per our system).

Pre-Contact Status Follow Up:

This kind of follow up is done when patient referral status is not a recruitment status after a certain period of time.

Post Contact Success Status Follow UP:

This kind of follow up is done when referral status is in recruitment status but has not reached the final status for a certain interval of time.

All theses follow ups are done based on referral created date, referral modified date, referral status, referral follow up level, referral follow up count, last follow up person, last follow response etc. So, if we are going to handle these conditions using code then it will become a spaghetti code.

We can use python DSL for solving this complexity. We specify the conditions and actions in domain specific language which enables us to remove or add new rules without touching the code. For follow up we use python DSL implementation from the repository https://github.com/venmo/business-rules. Here, different situations are listed as rules.

We need to define three things, Set of Rules (with conditions and actions), Variables which can be used in the conditions (Eg.: referral created date, referral status etc.), and Actions to be taken (sending follow up email to Patient/investigator etc). Python DSL provides an engine which iterates referrals over rules one by one and and the rule for which the conditions are met, action specified is executed. A sample rule is given below:

Consider a case when the connection follow up starts. The condition for this referral status should be Pending (Not Attended), referral creation date should be 3 days ago. Rule is written as:

CONNECTION_FUP_START_TIME = 3
FIRST_PATIENT_CONNECTION_FUP_SUBJECT = 'Email Subject'
FIRST_PATIENT_CONNECTION_FUP_TEMPLATE = 'first_pat_conntion.html'

RULES = [ 
	# first patient connection follow up 
	{ 
		"conditions": { 
			"all": [ 
				{ 
					"name": "get_current_status", 
					"operator": "equal_to_case_insensitive", 
					"value": "Pending", 
				}, 
				{ 
					"name": "get_referral_age", 
					"operator": "equal_to", 
					"value": CONNECTION_FUP_START_TIME 
				} 
			]
		},

		"actions": [ 
			{ 
				"name": "follow_up_patient", 
				"params": { 
					"follow_up_level": 'Connection', 
					"email_subject": FIRST_PATIENT_CONNECTION_FUP_SUBJECT, 
					"email_template": FIRST_PATIENT_CONNECTION_FUP_TEMPLATE 
				} 
			} 
		]
 	}
]

A more detailed usage is described in readme. Please check the repository for more details.

Leave a Reply

Your email address will not be published. Required fields are marked *

Healthcare Informatics Solutions

Healthcare IT news, developments, opinions and solutions

Contact us now

Popular Posts