You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
The main instrument of Drools is a rule.
A rule has:
- The Rule name
- The Condition, which starts with the when keyword
- The Consequence, which starts with the then keyword
Rules are collected into Rules Flow and Rules Packages.
Rules Packages are collected into Knowledge Base.
Knowledge Base Session is an interface called to "fire rules" and to interact with Rules Engine.
There are two types of Knowledge Sessions:
- Stateless Knowledge Session - Stateful Knowledge Session
The difference is in keeping or not keeping track of data changes during the execution of rules.
Stateless Knowledge Session does not keep the state. It can be called as a method with input facts/data to provide a conclusion. This is the simplest session type.
More complex is Sateful Knowledge Session, which keeps track of changing data while executing the rules. A good example of Stateful Knowledge Session is monitoring automatic manufacturing, keeping track of all changes during the process.
What is the difference between rules and Java methods?
- Methods are called directly with specific instances passed and result in a single execution.
- Rules execute by matching against any data as long it is inserted into the engine.
- Rules can never be called directly and specific instances cannot be passed to a rule.
- Depending on the matches, a rule may fire once or several times, or not at all.
From Java to Rules Example
Here we provide an example of a rule in the Drools language written in Java dialect.
Yes, Drools has more than one dialect, but we will focus on the Java dialect.
Business Requirements:
When a user searches for a product, check for a previous search attempt, stored in cookies, and offer a related product.
Java code:
// starting a user web session
HttpSession session = request.getSession(true);
// reading cookies to get product name in a previous search
String productName = null;
Cookies[] cookies = request.getCookies();
if(cookies != null) {
for(Cookie ck : cookies) {
if(ck.getName().equals(“ProductName”)) {
productName = ck.getValue();
}
}
}
// add this product, if not null, to the offer
// this part of code was identified as often changeable
// business might add more conditions here
if( productName != null && listOfProductLinks.get(productName) != null) {
offer.add(listOfProductLinks.get(productName));
}
Was it clear so far?
The last part of Java code was identified as often changeable to be replaced by rules.
What is the syntax of the rule file?
The rule file must have a condition starting with when keyword and a conclusion starting with then keyword. The condition and conclusion will use specific objects of specific classes. There must be import statements in the beginning of the file.
2016-07-14_23:36 by Irl Hirsch
It is done frequently, but as noted can be dangerous, particularly in cardiac patients. I tend to discourage its use-it just takes one law suit to change one's mind, and many years ago I was an "expert witness" for someone given IV insulin prior to surgery and had a subsequent cardiac arrest. The patient was most likely hypokalemic and on digoxin (this was many years ago) but reading or seeing that one time is all it takes to change one's practice.
<br/>// starting a user web session
<br/>HttpSession session = request.getSession(true);
<br/>// reading cookies to get product name in a previous search
<br/>String productName = null;
<br/>Cookies[] cookies = request.getCookies();
<br/>if(cookies != null) {
<br/> for(Cookie ck : cookies) {
<br/> if(ck.getName().equals(“ProductName”)) {
<br/> productName = ck.getValue();
<br/> }
<br/> }
<br/>}
<br/>// add this product, if not null, to the offer
<br/>// this part of code was identified as often changeable
<br/>// business might add more conditions here
<br/>if( productName != null && listOfProductLinks.get(productName) != null) {
<br/> offer.add(listOfProductLinks.get(productName));
<br/>}
<br/>
Was it clear so far?
onclick="window.location.href='/BASE/jsp/demo.jsp?checkFlavor=itsp&issueID=306&intro=general&group=aitu&ur=f'">
The last part of Java code was identified as often changeable to be replaced by rules.
What is the syntax of the rule file?
The rule file must have a condition starting with when keyword and a conclusion starting with then keyword. The condition and conclusion will use specific objects of specific classes. There must be import statements in the beginning of the file.
It is done frequently, but as noted can be dangerous, particularly in cardiac patients. I tend to discourage its use-it just takes one law suit to change one's mind, and many years ago I was an "expert witness" for someone given IV insulin prior to surgery and had a subsequent cardiac arrest. The patient was most likely hypokalemic and on digoxin (this was many years ago) but reading or seeing that one time is all it takes to change one's practice.