Saturday, March 28, 2009

Using Enums in Jess Patterns

As Wolfgang Laun and Scott Krasnigor have mentioned on the Jess list, you can indeed use enums in your Jess rule patterns. The trick is that if you look at the way enums work in Java 1.5 and beyond, accessing the enumeration type is a function call. To match a slot on the return value of a function call, you have to use Jess's return value constraint (=).

Figure 1 shows a pseudo-code example.



(slot-name =(EnumType.enumConstant))


Figure 1. Using enums in Jess patterns.

In SINFERS, our rules get a bit more sophisticated. Figure 2 shows the LHS of the rule that proposes candidate computations of the amount of exchangeable calcium (Ca++) ions as a function of the the amount of exchangeable bases, soil pH, and the percentage of clay in a soil sample.



(defrule PROPOSE::ptf__15F1_CA_11
"Proposes _15F1_CA_11 as best candidate to compute _15F1_CA"
(not (MAIN::SoilProperty (labCode "_15F1_CA" ) (state =(SoilPropertyStateType.COMPUTED))))
(MAIN::SoilProperty
(labCode "_15F1_CEC")
(state =(SoilPropertyStateType.INITIAL)|=(SoilPropertyStateType.COMPUTED))
(uncertainty ?e1)
(value ?v1)
(OBJECT ?objSp1))

(MAIN::SoilProperty
(labCode "_4A1")
(state =(SoilPropertyStateType.INITIAL)|=(SoilPropertyStateType.COMPUTED))
(uncertainty ?e2)
(value ?v2)
(OBJECT ?objSp2))

(MAIN::SoilProperty
(labCode "_P10_NR_C")
(state =(SoilPropertyStateType.INITIAL)|=(SoilPropertyStateType.COMPUTED))
(uncertainty ?e3)
(value ?v3)
(OBJECT ?objSp3))


Figure 2. A real world example from SINFERS.

The pattern matches if we see sinfers.core.SoilProperty objects (as shadow facts) that are either INITIAL (meaning they were part of the input set) or COMPUTED (meaning they have survived candidacy, and are now authoritative sources of data for this computed property).

Add this powerful pattern-matching technique to your Jess toolbox! -JM