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 >> 7.2.4. DRools Basics
Current Topic: 7.2.4.1. Truth Maintenance System
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
Truth Maintenance System

Truth Maintenance System (TMS) supports logical chaining in Drools.

TMS includes conditional elements, such as and/or/not/exists/test, logical concepts or operations, such as Assertion (Insert), Retraction (Retract), Modification (Update and Modify) and more…
Drools supports conditional element and as well as &&.

Drools supports conditional element or as well as ||.

Conditional Element exists checks for the existence of data in the Working Memory

Assertion
• Expert systems typically use the term assert or assertion to refer to facts made available to the system.
• However, due to "assert" being a keyword in most languages, DRools uses the insert keyword;
• These two terms are used interchangeably.
• When an Object is inserted it returns a FactHandle. This FactHandle is the token used to represent your inserted object within the WorkingMemory.
• It is also used for interactions with the WorkingMemory when you wish to retract or modify an object.

Person person = new Person("Bob");
FactHandle personHandle = ksession.insert( person );


Retraction
• Retraction is the removal of a fact from Working Memory.
• Any rules that are activated and dependent on that fact will be cancelled.
• Note that it is possible to have rules that depend on the nonexistence of a fact, in which case retracting a fact may cause a rule to activate. (See the not and exists keywords.)
• Retraction may be done using the FactHandle that was returned by the insert call.
• On the right hand side of a rule the retract statement is used, which works with a simple object reference.

Person person = new Person("Bob");
FactHandle personHandle = ksession.insert( person );
....
ksession.retract( personHandle );


Modification: Update & Modify
• The Rule Engine must be notified of modified facts, so that they can be reprocessed.
• You must use the update() method to notify the WorkingMemory of changed objects.
Update() always takes the modified object as a second parameter, which allows you to specify new instances for immutable objects.
Modify() statement is recommended on the right hand side of a rule the , as it makes the changes and notifies the engine in a single statement.

Example of modify with logical conditions:

when
exists (application.criminalRecord) or application.person.age < 18
then
modify(application.rejected = true);
end


Two rules with MODIFY and LOCK-ON-ACTIVE

rule "Assign people in North Carolina (NC) to sales region 1"
ruleflow-group "test"
lock-on-active true
when
$p : Person( )
$a : Address( state == "NC") from $p.address
then
modify ($p) {} // Assign person to sales region 1 in a modify block
end
----------------------------------------------------------------------------------------
rule "Apply a discount to people in the city of Raleigh"
ruleflow-group "test"
lock-on-active true
when
$p : Person( )
$a : Address( city == "Raleigh") from $p.address
then
modify ($p) {} // Apply discount to person in a modify block
end
Was it clear so far?

In the example, persons in Raleigh, NC should be assigned to sales region 1 and receive a discount;
i.e., you would expect both rules to activate and fire.
Instead you will find that only the second rule fires.
Why? Because of the lock-on-active true attribute.


Drools provide direct access to List & Map objects
• Directly access a List value by index
• Directly access a Map value by key
• Can use the contains keyword similar as in Java

Examples:

listOfProducts[0] = “redHat”;
mapOfProducts.put(“redHat”, aRedHatProductObject);

when
mapOfProducts contains “redHat”
then
modify($offer.add(mapOfProducts[“redHat”]) );
end


Drools function allows developers creating a method (function) in a rule source file
• Starts with the keyword “function” and makes sense when can be reused
• Very similar to a Java method, might be useful when a developer does not want to touch Java code

function void printTime() {
logger.info((new Date()).toString());
}


Drools can declare new Type, which is very similar to introducing a new class in Java

declare Product
productName : String
productPrice : double
productAvailability : java.util.Date
end

Declaration as well as Java classes might include metadata, for example @author Jeff Zhuk
Assignments:
1. Answer QnAs
2. Create at least one QnA related to the content and email to jeff.zhuk@javaschool.com
| Check Your Progress | Propose QnA | Have a question or comments for open discussion?
<br/>Person person = new Person("Bob");
<br/>FactHandle personHandle = ksession.insert( person );
<br/>


Retraction
• Retraction is the removal of a fact from Working Memory.
• Any rules that are activated and dependent on that fact will be cancelled.
• Note that it is possible to have rules that depend on the nonexistence of a fact, in which case retracting a fact may cause a rule to activate. (See the not and exists keywords.)
• Retraction may be done using the FactHandle that was returned by the insert call.
• On the right hand side of a rule the retract statement is used, which works with a simple object reference.
<br/>Person person = new Person("Bob");
<br/>FactHandle personHandle = ksession.insert( person );
<br/>....
<br/>ksession.retract( personHandle );
<br/>


Modification: Update & Modify
• The Rule Engine must be notified of modified facts, so that they can be reprocessed.
• You must use the update() method to notify the WorkingMemory of changed objects.
Update() always takes the modified object as a second parameter, which allows you to specify new instances for immutable objects.
Modify() statement is recommended on the right hand side of a rule the , as it makes the changes and notifies the engine in a single statement.

Example of modify with logical conditions:
<br/>when
<br/>   exists (application.criminalRecord) or application.person.age < 18
<br/>then
<br/>   modify(application.rejected = true);
<br/>end
<br/>


Two rules with MODIFY and LOCK-ON-ACTIVE
<br/>rule "Assign people in North Carolina (NC) to sales region 1"
<br/>ruleflow-group "test"
<br/>lock-on-active true
<br/>when
<br/>    $p : Person( ) 
<br/>    $a : Address( state == "NC") from $p.address 
<br/>then
<br/>    modify ($p) {} // Assign person to sales region 1 in a modify block
<br/>end
<br/>----------------------------------------------------------------------------------------
<br/>rule "Apply a discount to people in the city of Raleigh"
<br/>ruleflow-group "test"
<br/>lock-on-active true
<br/>when
<br/>    $p : Person( ) 
<br/>    $a : Address( city == "Raleigh") from $p.address 
<br/>then
<br/>    modify ($p) {} // Apply discount to person in a modify block
<br/>end
<br/>






Was it clear so far?


In the example, persons in Raleigh, NC should be assigned to sales region 1 and receive a discount;
i.e., you would expect both rules to activate and fire.
Instead you will find that only the second rule fires.
Why? Because of the lock-on-active true attribute.


Drools provide direct access to List & Map objects
• Directly access a List value by index
• Directly access a Map value by key
• Can use the contains keyword similar as in Java

Examples:
<br/>listOfProducts[0] = “redHat”;
<br/>mapOfProducts.put(“redHat”, aRedHatProductObject);
<br/>
<br/>when
<br/>     mapOfProducts contains “redHat”
<br/>then
<br/>    modify($offer.add(mapOfProducts[“redHat”]) );
<br/>end
<br/>


Drools function allows developers creating a method (function) in a rule source file
• Starts with the keyword “function” and makes sense when can be reused
• Very similar to a Java method, might be useful when a developer does not want to touch Java code
<br/>function void printTime() {
<br/>    logger.info((new Date()).toString());
<br/>}
<br/>


Drools can declare new Type, which is very similar to introducing a new class in Java
<br/>declare Product
<br/>  productName : String
<br/>  productPrice : double
<br/>  productAvailability : java.util.Date
<br/>end
<br/>

Declaration as well as Java classes might include metadata, for example @author Jeff Zhuk
Assignments:
1. Answer QnAs
2. Create at least one QnA related to the content and email to jeff.zhuk@javaschool.com

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

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