Dynamic Facts in Rule Unit

13 views
Skip to first unread message

Mani Kandan

unread,
Oct 27, 2025, 4:51:35 PM (13 days ago) Oct 27
to Drools Usage
Hi all,
I’ve been working with Drools DRL 8.44 and recently started experimenting with Rule Units. In my current implementation, the facts are dynamic — their classes are only known at runtime. To handle this, I’ve created a RuleFact class that wraps the actual fact as an Object.
In the traditional KieSession approach, I insert these wrapped facts using a BatchCommand before executing the session. Now, I’m trying to replicate a similar approach using Rule Units. 

But I am unable to parse dataSources value in DRL. I am not able to find any example for such implementation, I took some inspiration from https://github.com/kiegroup/drools/blob/main/drools-ruleunits/drools-ruleunits-dsl/src/main/java/org/drools/ruleunits/dsl/SyntheticRuleUnit.java

Any help on how I can do this.

Note:
I have many DRL that I load from DB. each DRL has its own facts. And use same RuleUnitData class for all using NamedRuleUnitData



##################


public class VirtualRuleUnit implements NamedRuleUnitData {

private final String unitName;

private final Map<String, DataSourceDefinition> dataSources;

VirtualRuleUnit(String unitName, Map<String, DataSourceDefinition> dataSources) {
this.unitName = unitName;
this.dataSources = dataSources;
}

@Override
public String getUnitName() {
return unitName;
}

public Map<String, DataSourceDefinition> getDataSources() {
return dataSources;
}



rule "Print SimpleFact per fact"
salience 0
when
$d : /dataSources
$sf : SimpleFact() from $d.get("SimpleFact")
then
System.out.println("Print SimpleFact per fact triggered." + $sf);
end

Toshiya Kobayashi

unread,
Nov 5, 2025, 9:17:53 PM (4 days ago) Nov 5
to Drools Usage
Hello,

Firstly, I strongly recommend to upgrade to the latest 10.x version (10.1.0).

I created an example with RuleUnit and a wrapper fact.

https://github.com/tkobayas/kiegroup-examples/tree/master/Ex-ruleunit-wrapperFact-10.1

I hope it helps.

Cheers,
Toshiya

2025年10月28日火曜日 5:51:35 UTC+9 iamm...@gmail.com:
Reply all
Reply to author
Forward
0 new messages