[rules-users] modifying individual facts in rule drl file

Mehak mehak.kapoor at hewitt.com
Thu Feb 21 01:26:24 EST 2008


Hi 
I am using drool to put my application business logic 
In my java service class, I am adding into an ArrayList objects: task1,task2
and task3. 

 list.add(task1); 
 list.add(task2); 
 list.add(task3); 
 new RuleRunner().runRules(new String[] { "ruleTraverseFacts.drl" }, list); 

Code in the RuleRunner class is : 
public void runRules(String[] rules, ArrayList facts) throws Exception { 
                // RuleBase contains the rules to be applied to facts 
                RuleBase ruleBase = RuleBaseFactory.newRuleBase(); 
                PackageBuilder builder = new PackageBuilder(); 

                for (int i = 0; i < rules.length; i++) { 
                        String ruleFile = rules[i]; 
                        System.out.println("Loading file: " + ruleFile); 
                        builder.addPackageFromDrl(new
InputStreamReader(RuleRunner.class 
                                       
.getResourceAsStream("/BusinessRules/" + ruleFile))); 
                } 

                Package pkg = builder.getPackage(); 
                ruleBase.addPackage(pkg); 
                //Facts are inserted in the working Memory of the ruleBase 
                WorkingMemory workingMemory = ruleBase.newStatefulSession(); 
                Collection factList=new ArrayList(); 
                for (int i = 0; i < facts.size(); i++) { 
                        Object fact = facts.get(i); 
                        System.out.println("Inserting fact: " +
fact.toString()); 
                        factList.add(workingMemory.insert(fact)); 
                } 
In my .drl class I have defined rules: 
package BusinessRules 
import model.* 
import java.util.* 

rule "traverse Collection" 
        when 
        
$o1 : Task($child : childTasks , childTasks != null)   
then 
  
 $o1.setWorkHours((getRollUp($child)) + $o1.getWorkHours()); 
 update($o1); 
 System.out.println("estimated hours" + $o1.getWorkHours()); 
 retract($o1); 
  
end 
 function int getRollUp(ArrayList child) { 
        //some code 
} 

What I would want to do is identify and manipulate individual objects in the
working memory. 
like task1.getId();. 
All the rules in the drl file are applied to all the facts in working
memory. How can I access individual objects without specifying any
condition?
-- 
View this message in context: http://www.nabble.com/modifying-individual-facts-in-rule-drl-file-tp15605550p15605550.html
Sent from the drools - user mailing list archive at Nabble.com.




More information about the rules-users mailing list