Drools rules engine notes

Reference: https://www.cnblogs.com/jpfss/p/10870002.html

https://blog.csdn.net/hc_ttxs/article/details/85248696

 

I. Overview

In general, we use an interface to do things, go first to wear parameters, and secondly to get to the results achieved after the implementation of the interface is complete, and drools is the same, we need to pass data into it, to check the rules, call external interfaces, but also you may need to obtain the results obtained after the rule is finished. In drools, this data is transmitted into the object, the term called Fact objects .

Fact object is an ordinary java bean , read and write operations may be performed in any of the current rule object, the object provides a method call, when inserted into a java bean workingMemory, the rule is used in reference to the original object, the rule by reading and writing fact object to the application to read and write data for these properties, the need to provide getter setter accessor, rules can be dynamically inserted workingMemory to delete the new fact in the current object.

Rules files can be used .drl files can also be a xml file

 

Second, the rules of grammar

Package Penalty for : in terms of a rules file, Package Penalty for is necessary to define the rules file must be placed in the first row . In particular, the package name is arbitrary and does not necessarily correspond to the physical path , the concept of the package with different java, there is only a logical distinction . Query function and the like as defined under the same package can be used directly.

For example: package com.drools.demo.point

Import : Import rules files need to external variables, the same procedure as used herein with java, but is different from the java, Import introduced here may be not only a class, may be accessible to a class of a the static method .

such as:

import com.drools.demo.point.PointDomain;

import com.drools.demo.point.PointDomain.getById;

rule : the definition of a rule. rule "ruleName". A rule may contain three parts:

Properties section : define some properties of the current rule execution, etc., such as whether can be performed repeatedly, expiration date, and time to take effect.

Part conditions : i.e. the LHS, define the current conditions of a rule, such When Message (); determines whether there is a Message object in the current workingMemory.

Results section : the RHS, where you can write an ordinary java code, the operation that is currently performed after the rule conditions are met, you can call direct method Fact subject to operational applications.

Rule examples:

rule "name"
       no-loop true
       when
               $message:Message(status == 0)
       then
               System.out.println("fit");
               $message.setStatus(1);
               update($message);
end

no-loop: define whether the current rules do not allow multiple cycles to perform, the default is false

salience: the priority, the greater the value before execution, the execution order can control rules

when: rule conditions start

Guess you like

Origin www.cnblogs.com/Jasper-changing/p/12082651.html