Tutorial 6: Diabetes Risk Score Calculator
Prerequisites: Getting the Project Files
Before you begin, this tutorial requires you to download two key assets from our GitHub repository:
- The "Form Template" Rule Project: The foundational project for creating dynamic forms.
- The Front-End Renderer: The HTML and JavaScript files needed to display the forms.
Follow these two steps to get everything you need.
Step 1: Import the Rule Projects into Studio
We use a PowerShell script to automatically find and install all the sample rule projects, including the essential "Form Template," into your Corticon.js Studio.
-
Navigate to the
sample-projects
Directory: -
Download the Import Script:
- In the file list, find and click on
Import-CorticonSamples.ps1
. - On the script's page, click the Download raw file button (the icon with a downward arrow).
- Save the script to a convenient location, like your Desktop.
- In the file list, find and click on
-
Run the Script:
- Open a PowerShell window, navigate to where you saved the file, and run it:
.\Import-CorticonSamples.ps1
- This script will temporarily clone the
dynamic-forms
repository in the background, find all the samples, and install them into your Corticon.js Studio.
- Open a PowerShell window, navigate to where you saved the file, and run it:
-
Restart Corticon.js Studio:
- After the script finishes, restart the studio. Go to Help -> Samples to find the "Form Template" project.
Step 2: Get the Front-End Files
The front-end rendering application is in the front-end-files
directory. We will use the downgit
tool to download just this specific folder.
-
Download the Directory:
- Click this direct link to download the
front-end-files
directory as a ZIP file: - Download
front-end-files
using downgit - This will download a file named
front-end-files.zip
.
- Click this direct link to download the
-
Unzip the Files:
- Create a main project folder on your computer for this work (e.g.,
C:\corticon-tutorial
). - Unzip the
front-end-files.zip
directly into that folder. Your folder structure should now look like this:C:\corticon-tutorial\
└── front-end-files\
├── clientSideComponent\
├── decisionServices\
├── trace\
└── index.html
└── ... (and other files)
- Create a main project folder on your computer for this work (e.g.,
-
Important Note for Later:
- As you proceed through the tutorials, you will generate new Decision Services from Corticon.js Studio. You must save these into the
decisionServices
subfolder. For example:C:\corticon-tutorial\front-end-files\decisionServices\
. This ensures the front-end application can find and load them.
- As you proceed through the tutorials, you will generate new Decision Services from Corticon.js Studio. You must save these into the
Tutorial: Building the Diabetes Risk Score Calculator
In this tutorial, you will build a Type 2 Diabetes risk assessment form. This project demonstrates how to create a "scorecard," where a user's answers to a series of questions each contribute points towards a final score. The final score is then used to determine a risk level.
What You Will Learn:
- How to implement a scorecard using conditional rules.
- How to increment a score based on multiple user inputs.
- How to use a final calculated score to present a summary or recommendation to the user.
- How to structure a multi-stage questionnaire.
Step 1: Building the Vocabulary
First, we'll define the data model. We need an entity to hold the user's answers and their calculated risk score.
-
In Corticon.js Studio, open the Form Template project you imported earlier.
-
In the Project Explorer, open the
Rule Vocabulary.ecore
file. -
You will see the standard
UI
,Container
,UIControl
, andOption
entities. We need to add one more to store our registrant's information. -
Expand the 'Data' folder in the rule vocabulary. Double click on the entity 'renameToYourPathToData', and enter
T2DB
. -
Right click on the entity called 'Data' and click 'Add Association'.
-
For the source entity name, keep 'Data.Data' for the the target entity name, select 'Data.T2DB', and click the 'One' button beneath it. Then, change 'Navigability' to 'Data.Data->Data.T2DB'.
-
Add the following attributes to the
T2DB
entity. These will store the user's answers and the results of the calculation.
Attribute Name | Data Type |
---|---|
age | Integer |
bmi | Decimal |
familyHistory | Boolean |
highBloodPressure | Boolean |
physicallyActive | Boolean |
gender | String |
gestationalDiabetes | Boolean |
score | Integer |
risk | String |
- Save your vocabulary file.
Step 2: Creating the Form Stages
We will build the questionnaire one stage at a time. Each stage will ask a question and, in some cases, add points to the score
attribute immediately.
Stage 0: Age
- Create a New > Rulesheet named
Stage0_Age.ers
. - Set the Precondition to
UI.currentStageNumber = 0
. - Add the following rules in the Actions section:
* Set Data Path:
UI.pathToData
='T2DB'
* Create Container:UI.containers
=Container.new
with atitle
of'Diabetes Risk Assessment'
. * Add Age Input: Create aUIControl.new
oftype
'Number' with thefieldName
'age'
and the label 'What is your age?'. * Set Next Stage:UI.nextStageNumber
=1
Stage 1: BMI (Body Mass Index)
- Create a New > Rulesheet named
Stage1_BMI.ers
. - Set the Precondition to
UI.currentStageNumber = 1
. - Add the following rules:
* Create Container: Create a
Container.new
with thetitle
of'Diabetes Risk Assessment'
. * Add BMI Input: Create aUIControl.new
oftype
'Number' with thefieldName
'bmi'
and an appropriate label. * Set Next Stage:UI.nextStageNumber
=2
Stage 2: Calculate Score (Non-Visual)
This is a background stage where we add points to the score based on the first two answers.
- Create a New > Rulesheet named
Stage2_Calculate1.ers
. - Set the Precondition to
UI.currentStageNumber = 2
. - Add the following rules:
* Enable Background Processing:
UI.noUiToRenderContinue
=true
. * Age-Based Scoring: Create several Conditional Rules: * Condition:T2DB.age >= 40 AND T2DB.age <= 49
* Action:T2DB.score
=1
* Condition:T2DB.age >= 50 AND T2DB.age <= 59
* Action:T2DB.score
=2
* (Continue for other age brackets as defined in the report) * BMI-Based Scoring: Create conditional rules to add points based on the BMI value. * Condition:T2DB.bmi >= 25 AND T2DB.bmi <= 30
* Action:T2DB.score += 1
* (Continue for other BMI ranges) * Set Next Stage:UI.nextStageNumber
=3
Subsequent Stages
Continue this pattern for the remaining questions, creating rulesheets for each stage.
- Stage 3 (Gender): Asks for gender. This is needed for the conditional gestational diabetes question later.
- Stage 4 (Family History): A Yes/No question (
YesNoBoolean
) that adds 1 point toT2DB.score
if the conditionT2DB.familyHistory = true
is met. - Stage 5 (High Blood Pressure): A Yes/No question that adds 1 point if true.
- Stage 6 (Physical Activity): A Yes/No question that adds 1 point if false (i.e., not active).
- Stage 7 (Gestational Diabetes): This is a conditional stage.
* Routing (Stage 7): A non-visual routing rulesheet. If
T2DB.gender = 'Female'
, setnextStageNumber = 8
. IfT2DB.gender = 'Male'
, setnextStageNumber = 9
(skipping the question). * Question (Stage 8): The Yes/No question about gestational diabetes, which adds 1 point if true. Then setsnextStageNumber = 9
. - Stage 9 (Final Calculation - Non-Visual): This final background stage determines the risk level.
* Condition:
T2DB.score >= 5
* Action:T2DB.risk
='You are at increased risk of having pre-diabetes or type 2 diabetes.'
* Condition:T2DB.score < 5
* Action:T2DB.risk
='You are likely at low risk for pre-diabetes.'
* Set Next Stage:UI.nextStageNumber
=10
- Stage 10 (Display Results): The final stage. Displays the calculated
T2DB.risk
string in aReadOnlyText
control and setsUI.done = true
.
Step 3: Assembling the Ruleflow
- Create a New > Ruleflow named
T2DB.erf
. - Drag all your rulesheets onto the canvas.
- Do not connect them. The stage progression is controlled entirely by your rules.
Step 4: Testing Your Logic
- Test Case 1 (High Risk Scenario):
* Input: Create a
T2DB
object with data that should result in a high score (e.g.,age = 55
,bmi = 31
,familyHistory = true
,highBloodPressure = true
,physicallyActive = false
). * Execution: Run the test, providing the necessarycurrentStageNumber
for each step. * Expected Output: After the final calculation stage, theT2DB.score
should be correctly summed up, and theT2DB.risk
attribute should contain the "increased risk" message.
Conclusion
You have successfully built a clinical risk scorecard! This powerful pattern can be adapted for all sorts of use cases, from financial assessments to lead qualification and beyond. You have demonstrated mastery of conditional logic, incremental calculations, and dynamic user paths.