This directory contains examples of Extended Operators in Corticon.js. Extended Operators add custom JavaScript logic to a Decision Service and are useful for complex calculations, session-data handling, and other logic beyond the built-in operators.
- Custom logic: Specialized calculations or transformations.
- Data handling: Manage session data or interact with external systems.
- Reusable functions: Share logic across multiple Decision Services.
- Advanced rule modeling: Extend Corticon.js beyond its built-in operators.
Demonstrates custom get and set operators for managing session data.
- Retrieve and store session data using
getSessionDataandsetSessionData. - Handle multiple data types, including
Date,DateTime, andDecimal. - Use helper functions for data conversion.
Files:
getSetData.js— implements the customgetandsetoperators.getSetData.ers— Rulesheet demonstrating the operators.getSetDataTest.ert— test cases for validating the operators.getSetDataVocab.ecore— Rule Vocabulary for the rules.
Demonstrates financial calculations such as Present Value and Future Value using custom operators.
- Perform calculations across multiple data types (
Decimal,Date,DateTime). - Handle fractional years using
DateorDateTimeinputs. - Validate input parameters.
Files:
FinanceCustomFunctions.js— implements custom functions such aspresentValueandfutureValue.finance.ers— Rulesheet demonstrating financial calculations.financeInteger.ers,financeString.ers— variants for integer and string inputs.test.ert— test cases for validating the rules.vocab.ecore— Rule Vocabulary for the financial rules.
- Custom operators for session data:
getSessionDataretrieves and converts session data to the appropriate type;setSessionDatastores it in a JSON-compatible format. - Financial calculations: Present Value and Future Value based on an interest rate and time period, including fractional years.
- Input validation: Both examples validate parameter types and return descriptive error messages for invalid inputs.
- Reusable logic: Custom functions are implemented in JavaScript and exported for consistent reuse across Decision Services.
- Import the rule projects: In Corticon.js Studio, import the contents of one of the subdirectories into a Rule Project.
- Run the tests: Use the
.ertfiles to validate the rules and custom operators. See setting a mock implementation for the Studio tester. - Customize the operators: Modify the JavaScript files (for example,
getSetData.js,FinanceCustomFunctions.js) to adapt them to your use case. - Deploy the Decision Services: Package and deploy to your target environment, such as a browser, Node.js, or a serverless function.
BasicSampleGetSetOperatorsuses an in-memoryMapto store session data. For production, use a persistent storage mechanism.- The
Financeexample includes helper functions for fractional years and input validation that can be extended to additional use cases.