Need Info for RuleUnitProviderImpl custom implementation

56 views
Skip to first unread message

Vivek Agarwal

unread,
Apr 12, 2024, 6:38:14 AMApr 12
to Drools Usage
Hi, 

I am trying to create n numbers of RuleUnit instance on startup using RuleProvider createInstance method. When instance is created for first time, MyRuleUnit class instance which passing in createInstance method calls defineRules method but next time it is not calling. 

When investigated, identified that RuleUnitProviderImpl uses ruleUnitMap which store the key as canonicalName and as I am sending same class name everytime. it is not working.

Please suggest.

My requirement is creating multiple instances of RuleUnit and defineRules on startup. 

Thanks,
Vivek

Toshiya Kobayashi

unread,
Apr 14, 2024, 11:42:50 PMApr 14
to Drools Usage
Hi Vivek,

I assume you are talking about a RuleUnit DSL use case because you mentioned 'defineRules'.

We expect that one RuleUnit class (implements RuleUnitDefinition) has the same rules. You can create multiple RuleUnitInstances from the same RuleUnit class, but they have the same rules, so there is no need of calling 'defineRules' multiple times. Does it make sense?


> My requirement is creating multiple instances of RuleUnit and defineRules on startup.

Why do you need to call defineRules multiple times? If you have different rules, you should have different RuleUnit classes.

Regards,
Toshiya
2024年4月12日金曜日 19:38:14 UTC+9 viveka...@gmail.com:

Vivek Agarwal

unread,
Apr 15, 2024, 1:23:01 AMApr 15
to drools...@googlegroups.com
Hi Toshiya,

I have different rules (e.g. PromotionRule, DiscountRule, etc.) for which I need to create multiple ruleUnit. I am trying to create these Ruleunit on the fly using yaml configuration which can be controlled business specific. 

Currently RuleProvider.get().createInstance() method creates a rule mapping using the className in internalMap which is not allowing me to create an instance of ruleUnit by passing the name. e.g. In yaml If I define RuleUnit name, dataStore and attributes then using a GenericRuleUnit in code, we want to instantiate the n number of ruleUnit by calling createInstance method and passing the name so that same instance can be fetched later.

Is there any way we can implement the above logic? 

Thanks,
Vivek 

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/e5a1a994-b111-4783-907d-80f1d7bbdaf4n%40googlegroups.com.

Toshiya Kobayashi

unread,
Apr 15, 2024, 11:07:11 PMApr 15
to Drools Usage
Hi Vivek,

Have you tried `NamedRuleUnitData`? By implementing `NamedRuleUnitData.getUnitName()`, the unitName can be under your control, so separatedly stored in the internal map.

Here is the example.

https://github.com/apache/incubator-kie-drools/blob/main/drools-ruleunits/drools-ruleunits-dsl/src/test/java/org/drools/ruleunits/dsl/NamedHelloWorldUnit.java
https://github.com/apache/incubator-kie-drools/blob/main/drools-ruleunits/drools-ruleunits-dsl/src/test/java/org/drools/ruleunits/dsl/RuleUnitRebuildTest.java#L56-L83
("invalidate" is not important here. Just see how the test calls `defineRules` per `createRuleUnitInstance`)

Does it meet your requirement?

Regards,
Toshiya

2024年4月15日月曜日 14:23:01 UTC+9 viveka...@gmail.com:

Vivek Agarwal

unread,
Apr 16, 2024, 2:20:30 AMApr 16
to drools...@googlegroups.com
Hi Toshiya,

Thank you for your email.

Yes, it looks like NamedRuleUnit should work, which I will try to update you but still createRuleUnitInstance takes an object for the RuleUnit class, how getUnitName will help here to create instance of specific ruleUnit based on name. 

Can you please provide a sample example. That will be helpful.

Thanks,
Vivek

Toshiya Kobayashi

unread,
Apr 17, 2024, 1:49:37 AMApr 17
to Drools Usage
> how getUnitName will help here to create instance of specific ruleUnit based on name.

The return value of `getUnitName()` is used as a key of `RuleUnitProviderImpl.ruleUnitMap`, so if you make it (= The return value of `getUnitName()`) unique per RuleUnit object, they are treated as different RuleUnit.

https://github.com/apache/incubator-kie-drools/blob/main/drools-ruleunits/drools-ruleunits-impl/src/main/java/org/drools/ruleunits/impl/RuleUnitProviderImpl.java#L76-L77
https://github.com/apache/incubator-kie-drools/blob/main/drools-ruleunits/drools-ruleunits-impl/src/main/java/org/drools/ruleunits/impl/RuleUnitProviderImpl.java#L102


> Can you please provide a sample example

The `NamedHelloWorldUnit.java` is actually an example. You can create different RuleUnitInstance with different names.

```
        NamedHelloWorldUnit unit1 = new NamedHelloWorldUnit("Name-1");
        RuleUnitInstance<NamedHelloWorldUnit> unitInstance1 = RuleUnitProvider.get().createRuleUnitInstance(unit1);

        NamedHelloWorldUnit unit2 = new NamedHelloWorldUnit("Name-2");
        RuleUnitInstance<NamedHelloWorldUnit> unitInstance2 = RuleUnitProvider.get().createRuleUnitInstance(unit2);

        NamedHelloWorldUnit unit3 = new NamedHelloWorldUnit("Name-3");
        RuleUnitInstance<NamedHelloWorldUnit> unitInstance3 = RuleUnitProvider.get().createRuleUnitInstance(unit3);
```

Regards,
Toshiya

2024年4月16日火曜日 15:20:30 UTC+9 viveka...@gmail.com:

Vivek Agarwal

unread,
Apr 24, 2024, 12:52:02 PMApr 24
to drools...@googlegroups.com
Hi Toshiya,

I tried named ruleunit and it is working fine but invalidRuleUnits is invalidating the rules for each type of instance. 

In my requirement, I am loading the defineRules condition and action from DB call based on specific ruleunit instance for specific tenant or LOB or ruleType but if any specific tenant is updating the rule and I am trying to invalidate ruleInstance for that specific rule type , I am unable to do that as invalidateRuleUnits is taking params as classType which invalidates rules for others as well. 

Then in the next instance creation it is loading for other rules or LOB or tenant which is impacting others. Can we, anyways, invalidate the rule for a specific instance or LOB or tenant for which I will instantiate the class. 

Thanks,
Vivek

Toshiya Kobayashi

unread,
Apr 25, 2024, 10:16:37 PMApr 25
to Drools Usage
Hmm, indeed NamedRuleUnit instances are invalidated altogether by the class name. In other words, NamedRuleUnit is not designed to be such a separated-unit use case.

As much as I have come up with so far,

A) Define different RuleUnit class per tenant

B) Run different process (or pod) per tenant

Sorry that it may not be what you expected.

Regards,
Toshiya

2024年4月25日木曜日 1:52:02 UTC+9 viveka...@gmail.com:

Vivek Agarwal

unread,
Apr 26, 2024, 10:12:59 PMApr 26
to drools...@googlegroups.com
Hi Toshiya,

Thank you so much for the response. 

Can you please help me how I can get the list of rules which got fired when executing the fact for dsl Ruleunit.

Thanks,
Vivek

Toshiya Kobayashi

unread,
Apr 30, 2024, 10:43:04 PMApr 30
to Drools Usage
You can use `AgendaEventListener.beforeMatchFired()` to capture the fired rule.

https://github.com/apache/incubator-kie-drools/blob/main/drools-ruleunits/drools-ruleunits-dsl/src/test/java/org/drools/ruleunits/dsl/RuleUnitsTest.java#L301

Regards,
Toshiya

2024年4月27日土曜日 11:12:59 UTC+9 viveka...@gmail.com:
Reply all
Reply to author
Forward
0 new messages