Defining a Validation Rule
A validation rule in a template determines whether a possibility is valid (possible, although possibly a bad choice). After the validation step, invalid possibilities are discarded.
Validation rules are associated with specific transformation rules. For example, after changing stock levels, you need to validate that the new stock levels are acceptable. Other kinds of data tranformations would have different validation rules.
Validation rules do not assess impact; that is done in the next phase, impact analysis.
How a Validation Rule Is Used
For each call to the scenario analysis API, the system does the following:
-
Runs all validation rules that are applicable to each possibility.
Notice that the system performs the iteration over the rules and possibilities. Your validation rule simply describes how to validate an individual possibility.
-
At the end of the validation process, the system discards all invalid possibilities.
Template Syntax
The following template fragment shows where to include a validation rule:
"validation": {
"name": "Replenishment Strategies Validations",
"rules": [
{
"description": "Check if it has enough inventory to supply and still maintain its own safety stock.",
"name": "Warehouse safety stock check",
"type": "Python",
"expression": "WarehouseValidation.py.CheckWarehouseSafetyStock",
"parameters": [
"warehouseId"
]
},
{
"description": "Check if supplier is approved to buy from",
"name": "Supplier Check",
"type": "SQL",
"expression": "SupplierValidation.sql",
"parameters": [
"supplierId"
]
}
]
}
This example contains two validation rules.
Formally:
-
The validation object is a top-level property of the template. For this object:
-
name—Specifies a generic name for all the validation rules within this object.
-
rules—Specifies an array of objects, each of which describes one validation rule.
-
-
Within the rules array, each object has the following properties:
-
description—Describes the rule.
-
name—Provides a name for the rule.
-
type—Specifies how this rule is implemented. Use one of the following values for this property: "Python", "ObjectScript", "SQL", "BPL"
-
expression—Specifies the code to execute when using this rule. The syntax used depends on the type property:
-
For "Python", use syntax of the form "PythonFilename.py.FunctionName" or "PythonFilename.py.ClassName.MethodName"
-
For "ObjectScript", use syntax of the form "##class(Package.Class).MethodName()"
-
For "SQL", use syntax of the form "SQLFilename.sql"
-
For "BPL", use the logical name of the BPL: "MyBPLName"
-
-
parameters—Specifies an array of parameters to pass to the given code, in the same order expected by that code. The context object provides the values for these parameters.
-
Implementing in Python
To implement a validation rule in Python, create a Python function as follows:
-
The function must accept arguments as follows:
-
All the values specified in parameters array of the template, in that order.
-
Two additional arguments at the end (samRunContext and snapshot).
-
-
To examine the snapshot, the function must use the interface provided by the snapshot object; see Using the Snapshot to Run Queries.
-
The function must return 1 or 0. The value 1 indicates that the snapshot is valid, and the value 0 indicates that the snapshot is not valid and should not be used in further processing.
For example (with artificial line breaks for readability):
import iris
def CheckWarehouseSafetyStock(warehouseId, samRunContext, snapshot):
iris.cls("SC.Core.Analytics.SAM.Util.SAMLogger").LogInfo(samRunContext,
snapshot.uid,
"Running warehouse safety stock check for warehouse: " + warehouseId)
query = "SELECT quantity from SC_Data.ProductInventory WHERE siteLocationId =?"
params = [warehouseId]
rs = snapshot.Query(query, params)
currentQuantity = 0
if rs._Next():
currentQuantity = rs._Get('quantity')
query = "SELECT minSafetyStock from SC_Data.InventoryThreshold WHERE siteLocationId =?"
params = [warehouseId]
rs = snapshot.Query(query, params)
safetyStock = 0
if rs._Next():
iris.cls("SC.Core.Analytics.SAM.Util.SAMLogger").LogInfo(samRunContext,
snapshot.uid,
"Something went wrong while fetching min safety stock for warehouse: " + warehouseId)
safetyStock = rs._Get('minSafetyStock')
iris.cls("SC.Core.Analytics.SAM.Util.SAMLogger").LogInfo(samRunContext,
snapshot.uid,
"Validation result: " + str(currentQuantity = safetyStock) + " for warehouse: " + warehouseId)
return currentQuantity = safetyStock
Implementing in ObjectScript
To implement a validation rule in ObjectScript, create a class method as follows:
-
The method must be defined as a class method, not an instance method.
-
The method must accept arguments as follows:
-
All the values specified in parameters array of the template, in that order.
-
Two additional arguments at the end (samRunContext and snapshot).
-
-
To examine the snapshot, the method must use the interface provided by the snapshot object; see Using the Snapshot to Run Queries.
-
The method must return 1 or 0. The value 1 indicates that the snapshot is valid, and the value 0 indicates that the snapshot is not valid and should not be used in further processing.
Implementing in SQL
To implement a validation rule in SQL, create a .sql file containing one or more SQL SELECT statements as follows:
-
The statement must accept all the parameters provided by the parameters array of the template, in that order.
-
If the file contains multiple SELECT statements, they must each end with a semicolon.
-
Each statement must select a single column containing either 1 or 0. If any statement returns 0 in this column, the snapshot is not valid and is not used in further processing. If all statements return 1 in this column, the snapshot is valid.
Also see SQL Limitations.
The following simple example checks whether the supplier is active:
SELECT
CASE
WHEN status = 'Active' THEN 1
ELSE 0
END AS result
FROM SC_Data.Supplier
WHERE uid = ?;
Implementing in BPL
To implement a validation rule in BPL, create a BPL business process as follows:
-
The business process request class must be SC.Core.BP.Message.ValidationRequest. This class has three properties, automatically set by the scenario analysis module:
-
snapshotId, which is the ID of the snapshot to which this data belongs.
-
parameters, which is a list of parameters, provided by the parameters array of the template, in that order.
The parameters property provides the %Collection.ListOfDTOpens in a new tab methods; use those methods to access individual parameters.
-
samRunContext, which is the samRunContext as a %DynamicObjectOpens in a new tab.
-
-
The response class must be SC.Core.BP.Message.ValidationResponse.
-
The BPL must set the isValid property of the response equal to 1 or 0. If this property is 1, the snapshot is valid. If this property is 0, the snapshot is not valid and will not be used in further processing.
Checklist
-
Create the Python function, method, SQL, or BPL.
-
If you use BPL, add it to the Supply Chain production and enable it.
-
Within this code, log any errors as well as any intermediate steps that are useful for debugging.
-
Make sure that this code is located appropriately so that it can be found when you use the template.
-
Update the template as shown above.
-
Associate this validation rule with each transformation rule that it is suitable to use with. For example, if the validation rule checks to see if a supplier is valid, associate this validation rule with the transformation rules that related to suppliers.