In Hammurapi Rules a rule (i.e. a unit of inference) is a Java method with Infer annotation. Such method is called an inference method. An inference method shall have one or more parameters through which input facts are fed to the inference method. Inference methods and their parameters can have Condition annotations. Classes with inference methods are called rule classes. Rule classes are organized into rule sets. Such classes are instantiated when a rule session is created. They can be configured with parameters from a rule set definition.
Interfaces and annotations to bind methods to the inference network are defined in com.hammurapi.reasoning.spi package.
@Infer @Condition("son, parent: son.getObject().equals(parent.getObject())") public GrandSon infer(Son son, Parent parent) { return new GrandSon(son.getSubject(), parent.getSubject()); }
The inference method above defines a condition as an annotation. An alternative is to use if block inside the method. Use of annotation, though, is preferred because it allows the engine to optimize the inference network by chaining/sharing conditions. In the case of condition chaining/sharing if two inference methods have identical conditions (e.g. invoiceAmount>1000) then the engine may optimize inference network so the condition is evaluated once per fact instead once per fact per inference method. The method doesn't use InferenceContext.post(), it simply returns conclusions. As such it doesn't have to declare conclusion types in its @Infer annotations.
@Infer(Cousin.class) public void infer(Child child1, Child child2, Sibling sibling) { if (child1.getObject().equals(sibling.getSubject()) && child2.getObject().equals(sibling.getObject())) { InferenceContext.INSTANCE.post(new Cousin(child1.getSubject(), child2.getSubject())); } }
The method above uses InferenceContext.post() method to post conclusions and, therefore, has to declare conclusion types which it posts.
The diagram above shows rule lifecycle 1).
Rule interface then its init() method is invoked (2.4).InferenceContext.INSTANCE.post() (3.2).close() method is invoked, the session invokes release() methods on rule instances which implements Rule interface.