Tutorial 3: Data-Driven Vehicle Selection
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 Data-Driven Vehicle Selection Formβ
In this tutorial, you will build a dynamic vehicle selection form. What makes this project special is that the dropdown menus for vehicle makes and models will be populated dynamically by calling an external REST API. This is a powerful technique for forms that rely on data from other systems.
What You Will Learn:
- How to use the
dataSource
attribute to populate a dropdown from a URL. - How to use
DataSourceOptions
to map data from a JSON response to your dropdown's display text and value. - How to create cascading dropdowns, where the selection in one dropdown filters the options in another.
Step 1: Building the Vocabularyβ
First, we'll define the data model. This form only needs to store the user's final selections.
-
In Corticon.js Studio, open the Form Template project you imported earlier.
-
In the Project Explorer, open the
Rule Vocabulary.ecore
file. -
Expand the 'Data' folder in the rule vocabulary. Double click on the entity 'renameToYourPathToData', and enter
AutoQuote
. -
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.AutoQuote', and click the 'One' button beneath it. Then, change 'Navigability' to 'Data.Data->Data.AutoQuote'.
-
Add the following attributes to the
AutoQuote
entity:
Attribute Name | Data Type |
---|---|
vehicle_make | String |
vehicle_model | String |
vehicle_year | Integer |
- Save your vocabulary file.
Step 2: Creating the Form Stagesβ
This form has three primary stages: selecting the make, the model, and the year.
Stage 0: Select Vehicle Makeβ
This first stage will call an external API to get a list of vehicle makes.
- Create a New > Rulesheet named
Stage0_Make.ers
. - Set the Precondition to
UI.currentStageNumber = 0
. - In the Actions section, add the following rules:
- Set Data Path:
UI.pathToData
='AutoQuote'
- Create Container:
UI.containers
=Container.new
with atitle
of'Select Vehicle'
. - Create Make Dropdown: Add a
UIControl.new
toUI.containers.uiControls
.type
='MultipleChoices'
fieldName
='vehicle_make'
label
='Please select the make of the vehicle'
dataSource
='https://api.npoint.io/d487567c8a34a506350e'
(This is our mock API endpoint)
- Configure DataSource Mapping: Because our API uses different key names, we need to map them. Add a
DataSourceOptions.new
to the dropdown control you just created.dataValueField
='brand'
dataTextField
='brand'
- Set Next Stage:
UI.nextStageNumber
=1
- Set Data Path:
Stage 1: Select Vehicle Modelβ
This stage is similar, but the URL it calls will be dynamic, including the make selected in the previous stage.
- Create a New > Rulesheet named
Stage1_Model.ers
. - Set the Precondition to
UI.currentStageNumber = 1
. - Add the following rules:
- Create Container:
UI.containers
=Container.new
with atitle
of'Select Vehicle'
. - Create Model Dropdown: Add a
UIControl.new
toUI.containers.uiControls
.type
='MultipleChoices'
fieldName
='vehicle_model'
label
='Please select the model'
dataSource
='https://api.npoint.io/d487567c8a34a506350e'
- Configure DataSource Mapping and Filtering: Add a
DataSourceOptions.new
to the model dropdown.dataValueField
='name'
dataTextField
='name'
pathToOptionsArray
=$.[?(@.brand == '
+ AutoQuote.vehicle_make +')].models[*]
- Note: This JSONPath expression is the key. It filters the results from the API, only showing the models where the
brand
matches thevehicle_make
the user already selected.
- Set Next Stage:
UI.nextStageNumber
=2
- Create Container:
Stage 2: Select Vehicle Year and Finishβ
This is the final stage where the user selects the year.
- Create a New > Rulesheet named
Stage2_Year.ers
. - Set the Precondition to
UI.currentStageNumber = 2
. - Add the following rules:
- Create Container:
UI.containers
=Container.new
with atitle
of'Select Vehicle'
. - Create Year Input: Add a
UIControl.new
oftype
'Number' with thefieldName
'vehicle_year'
and an appropriate label. - End the Form:
UI.done
=true
.
- Create Container:
Step 3: Assembling the Ruleflowβ
- Create a New > Ruleflow named
Select-Vehicle.erf
. - Drag all three of your rulesheets onto the canvas.
- Do not connect them. The stage progression is handled entirely by the
nextStageNumber
attributes set in your rules.
Step 4: Testing Your Logicβ
Testing this ruleflow involves checking that the dataSource
and pathToOptionsArray
are being set correctly.
- Test Case 1 (Initial Load):
- Input:
UI
object withcurrentStageNumber = 0
. - Expected Output: The
UI
object should have aUIControl
for the vehicle make, and itsdataSource
anddataSourceOptions
attributes should be set correctly.nextStageNumber
should be1
.
- Input:
- Test Case 2 (Model Selection):
- Input:
UI
object withcurrentStageNumber = 1
. Create anAutoQuote
object withvehicle_make = 'Ford'
. - Expected Output: The
UI
object should have aUIControl
for the vehicle model. ItspathToOptionsArray
should now contain the string$.[?(@.brand == 'Ford')].models[*]
, demonstrating that the path is being dynamically constructed.
- Input:
Conclusionβ
Congratulations! You've now built a sophisticated form that fetches its options from an external API and uses cascading logic to filter choices. This is a very common pattern in real-world applications and a powerful skill to have.