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
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
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
<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.
<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?
onclick="window.location.href='/BASE/jsp/demo.jsp?checkFlavor=itsp&issueID=315&intro=general&group=aitu&ur=f'">
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
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
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