Register   Login   About   Study   Enterprise   Share
AI / Internet Technology University (AITU)
Fast Login - available after registration







|

Top Links: >> 80. Technology >> Internet Technology Summit Program >> 7. Enterprise, Knowledge Architecture, IoT, AI and ML >> 7.2. Rules and Knowledge-Driven Applications
Current Topic: 7.2.2. DRools Introduction: KnowledgeBase, Session and a Rule Sample
Sub-Topics: 7.2.2.1. Distributed Processing with Spring Batch and Drools | 7.2.2.2. Rete Algorithm
-- Scroll to check for more content below...
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.

Here is an abbreviated example of the rule file.

import java.util.List;
import cgi.examples.Offer;
rule 'offer product'
ruleflow-group "user preferences"
dialect "java"

when

$productName : ProductName
$listOfProductLinks : ListOfProductLinks
$offer : Offer()
(
$productName != null && $listOfProductLinks.get($productName)
)

then

modify($offer) {
addProductLink($listOfProductLinks.get($productName))
};

end


Notes:
DRools keywords are when, then and modify.
Of course, there must be some integration between the Java and Rules.

References:
https://docs.jboss.org/drools/release/5.6.0.Final/drools-expert-docs/html/

Assignments:
1. Answer QnAs
2. Create 2 QnAs related to the content
| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
Comments
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?



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.

Here is an abbreviated example of the rule file.
<br/>import java.util.List;
<br/>import cgi.examples.Offer;
<br/>rule 'offer product'
<br/>  ruleflow-group "user preferences"
<br/>  dialect "java"
<br/>
<br/>  when
<br/>
<br/>    $productName :   ProductName
<br/>    $listOfProductLinks : ListOfProductLinks
<br/>    $offer : Offer()
<br/>    (
<br/>     $productName != null && $listOfProductLinks.get($productName) 
<br/>    )
<br/>
<br/>  then
<br/>
<br/>     modify($offer)  {
<br/>         addProductLink($listOfProductLinks.get($productName))
<br/>      };
<br/>
<br/>  end
<br/>


Notes:
DRools keywords are when, then and modify.
Of course, there must be some integration between the Java and Rules.

References:
https://docs.jboss.org/drools/release/5.6.0.Final/drools-expert-docs/html/

Assignments:
1. Answer QnAs
2. Create 2 QnAs related to the content

| Check Your Progress | Propose QnA | Have a question or comments for open discussion?

Comments

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.


Have a suggestion? - shoot an email
Looking for something special? - Talk to me
Read: IT of the future: AI and Semantic Cloud Architecture | Fixing Education
Do you want to move from theory to practice and become a magician? Learn and work with us at Internet Technology University (ITU) - JavaSchool.com.

Technology that we offer and How this works: English | Spanish | Russian | French

Internet Technology University | JavaSchool.com | Copyrights © Since 1997 | All Rights Reserved
Patents: US10956676, US7032006, US7774751, US7966093, US8051026, US8863234
Including conversational semantic decision support systems (CSDS) and bringing us closer to The message from 2040
Privacy Policy