Skip to content
Example of Caché code generation using ObjectGenerators which implements a very simple RuleEngine.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Code Generation: Rule Engine

The purpose of this repository is to provide an example of how a Rule Engine can be implemented in InterSystems Caché using Object Generators.


  • Code generation is an excellent way of increasing performance moving run-time calculations to compile-time.
  • The intention of this example is to explain how something as complex as a Rule Engine can be modelled in InterSystems Caché.
  • InterSytems Ensemble provides a very powerful Rule Engine that is way much better than this homemade example.


  1. Describe a rule using a human-readable XML.
  2. Compile the rule and generate the code to evaluate that rule in run-time.



set path="C:\Temp\cache-iat-ruleengine-master\cache"
do $system.OBJ.ImportDir(path,"*.inc","ck",.error,1)
do $system.OBJ.ImportDir(path,"*.xml","ck",.error,1)

Run the code

  • The example provides a patient alert rule.
  • After compiling the classes of the project, we can run the example:
    • Create a context to evaluate the rule: this context contains all needed data to evaluate the rule, in this case a patient object.
    • Evaluate the rule.
  • To run the example, simply execute ##class(IAT.RuleEngine.Test.Examples).Run() method
Class IAT.S01.Rules.Test.Example Extends %RegisteredObject

ClassMethod Run() As %Status
  set ret = $$$OK
  try {
    // create a patient
    set p = ##class(Patient).%New()
    set p.MRN="1234", p.Name="John", p.Surname="Snow", p.DOB=$zdh("1975-05-07",3)

    // create a rule context, set data
    set context = ##class(PatientContext).%New()
    set context.Patient = p

    // evaluate Patient Alerts Rule
    set ruleEngine = ##class(IAT.S01.Rules.Engine).%New()
    $$$TOE(sc, ruleEngine.Evaluate("IAT.S01.Rules.Test.PatientAlertsRule", context, .log))

    // print log
    write !,"Rule log:",!
    zwrite log
  } catch ex {
    set ret = ex.AsStatus()
    do $system.Status.DisplayError(ret)
  quit ret


Execute the method from a Terminal session:

do ##class(IAT.RuleEngine.Test.Example).Run()
Patient is so old!
+----------------- general information ---------------
|      oref value: 1
|      class name: IAT.RuleEngine.Test.Patient
| reference count: 3
+----------------- attribute values ------------------
|                DOB = 49069
|                MRN = 1234
|               Name = "John"
|            Surname = "Snow"
Rule log:
log(1)="[2016-02-22 16:29:44] Rule: Not young anymore!"
log(2)="[2016-02-22 16:29:44] Action: 1"
log(3)="[2016-02-22 16:29:44] Action: 2"
log(4)="[2016-02-22 16:29:44] Action: 3"

Developer Community

Have a look at InterSystems Developer Community to learn about InterSystems technology, sharing solutions and staying up-to-date on the latest developments.

This example was published in


%XGEN.AbstractDocument implementation

Dmitry Maslennikov ( has uploaded a very interesting alternative approach which uses %XGEN.AbstractDocument class to process the XData.

The code is available in /pr/xgen branch.

Additional information is available here:

You can’t perform that action at this time.