0

1- Is it possible to fire only selected rules for a particular customer in Drool. Suppose I have 100 rules these are rules from different customers. So when a particular customer opens up the application I want to fire only those rules which belong to him. It can be possible that a rule can belong to more than one customer.

2 - I want to get a list of rules which needed to be fired for a particular customer and a list of facts to fire those rules so that I can fetch only that particular facts from my db. And after fetching them, fire those rules which are specific to that customer

pratik2392
  • 53
  • 2
  • 9

2 Answers2

2

You'll have to identify the owning customers along with the rules, or maintain data associating a customer with rules. I think that the latter is to be preferred, but I'll outline both solutions. In any case, the Customer has to be identified by a fact.

rule "some rule"
when
    Customer( id in ("Smith&Co", "Brown&Sons", "Jones Inc." ) )
    ...
then ... end

To associcate a Customer with rules you need:

class Customer {
    private String name;
    private List<String> rules;
    //...
}

and write an AgendaFilter

class RuleFilter implements AgendaFilter {
    static List<Customer> customers = new ArrayList<>();
    static {
        customers.add(...);
        ...
    }
    static RuleFilter getFilterFor( String custname ){
         for( Customer cust: customers ){
             if( cust.getName().equals( custname ) ){
                 return cust;
             }
         }
         throw IllegalArgumentException( "no customer: " + custname );
    }
    Customer current;
    RuleFilter( Customer current ){
        this.current = current;
    }
    // ... getters & setters
    boolean accept( Match match ){
        return current.getRules().contains( match.getRule().getName() );
    }
}

And run the session for a customer by providing a filter instance:

 AgendaFilter currFilter = RuleFilter( getFilterFor( customername ) ); 
 fireAllRules( currFilter );
laune
  • 31,114
  • 3
  • 29
  • 42
  • So you are saying to create agenda for each customer and fire the rules based on agenda... – pratik2392 Jul 22 '16 at 10:14
  • No, I'm not saying that. I'm saying that you should create an instance of an agenda filter depending on the customer. – laune Jul 24 '16 at 21:50
  • ok...I think this will work for me can you share some link where I can see the full implementation of the same and understand it further..... – pratik2392 Jul 25 '16 at 05:40
  • Most of the "full implementation" requires information I don't have - it depends on the customers and their rules. I've added some trivial code you should have been able to write yourself. – laune Jul 25 '16 at 09:55
  • I want to get a list of rules which needed to be fired for a particular customer and a list of fact/facts to fire those rules so that I can fetch only that particular facts from my db. And after fetching them, fire those rules which are specific to that customer. – pratik2392 Jul 27 '16 at 08:59
  • "I want to... customer:" This isn't a comment on this question or my answer. - Also, this sounds quite unclear. - Perhaps you ask a new question and provide a more detailed example. – laune Jul 27 '16 at 11:34
  • here's the link to question... http://stackoverflow.com/questions/38626569/get-list-of-facts-from-rule – pratik2392 Jul 28 '16 at 05:25
-1
  static RuleFilter getFilterFor( String custname ){
         for( Customer cust: customers ){
             if( cust.getName().equals( custname ) ){
                 return cust;
             }
         } 

in method signature you are returning RuleFilter, but inside for loop you are returning cust, how this is going to work?

sunil
  • 1
  • 3
  • This is a good point but does no answer the question. Should be a comment. Best to contribute on other answers until you can comment. – Lex Mar 27 '18 at 23:23