Please check out Griffin Weber’s lecture on “Rewriting i2b2: CQ2 and Leaf“.