1. Home
  2. Sample Projects
  3. Student pass-fail state

Student pass-fail state

◷ Reading Time: 5 minutes

Using Natural Language, validate the input and ultimately decide the student pass-fail state. This will create the JavaScript code for the Natural Language code and can be tested in the JavaScript Testbed.

Scenario

In this tutorial, we are going to write a list of rules in Natural Language to validate the input and determine the pass-fail state of a student:

  • If the total score is greater than or equal to 50, state pass, otherwise fail

Process Steps

These are the procedural steps for this project.

1. Create a Fact concept to define the inputs: ‘Concept.xml’ 

There is one object named ‘Person’ and its attributes are Name, Birth year, Test results, Pass state, and Last year results. This will make it easier to manage the variables.

Fact concept showing Person group with five parameters included

2. Add a Business Glossary to define the expressions: ‘BusinessGlossary.xml’ 

Using business glossary, you can define expressions such as ‘(Person.LastYearTestResults) + (Person.TestResults)’ with a term ‘Total’. Therefore, you can use the term ‘Total’ in your code instead of using the long expression.

  • Total: (Person.LastYearTestResults) + (Person.TestResults)
  • Name: Person.Name
  • BirthYear: Person.BirthYear
Business glossary showing three parameters and associated domains

3. Add a Natural Language file and write validation rules in Natural Language: ‘PersonValidation.xml’

Name and BirthYear are defined in the Business Glossary.

This logic file validates the name and BirthYear. If one or both are empty, the notification shows.

when Validate name
  Name is ""
then
	error = "Name cannot be empty"
	and isValid = false
otherwise
	Validate BirthYear
end
 
when Validate BirthYear
  BirthYear is null
then
	error = "Birth Year cannot be empty"
	and isValid = false
end
Natural language example showing the person logic

4. Add a flow file: ‘Flow.xml’ 

Add the created Natural Language file to the flow.

Simple flow of the decision with the Person Natural Language block embedded

5. Add another Natural Language file: ‘TestResultState.xml’ 

Write the rules to state the pass-fail grading of a student. Here we can use the term ‘Total’ which is defined in the Business Glossary.

when 
  isValid 
then
  DefinePassState
end
 
when DefinePassState
	Total > 50
then
	Person.Pass = true
otherwise
	Person.Pass = false
end
Further Natural Language example showing the Check Test result state logic

6. Add the Natural Language file to the flow 

The flow will show the direction and run all of the files by connecting the two Natural Language files.

Simple flow of the decision with the Person and Check Test Result State Natural Language logic embedded

Run in JavaScript Testbed

Once the code is running without errors in FlexRule Designer, you can test the code in JS Testbed.

1. Open the Natural Language file

2. Click on the ‘JS Test Bed’ icon

Detail view of the JS Test Bed menu item

3. JS Testbed will open in the web browser

JS Test Bed view showing the Add input parameters

4. Select ‘Code’ to insert the Input Parameters

JS Test Bed view showing 'Code' selected to insert the Input Parameters.

5. Copy the Input Parameters.

JS Test Bed view showing JSON object with parameter values

6. Click on ‘Run Logic’

Detail of the Run logic button

7. Output is displayed in Result section

JS Test Bed view showing the results of the logic

Download the project

The project can be downloaded from the attachment at the end of the page.

Updated on February 6, 2024

Article Attachments

Was this article helpful?

Related Articles