Skip to content

RuleUnit DSL does not apply RuleConfig clock type to session #6911

Description

@tkobayas

Problem

When creating a RuleUnitInstance from a RuleUnitDefinition (Java DSL) with a RuleConfig that sets ClockType.PSEUDO, the pseudo clock is not applied. The session still uses the default JDKTimerService (realtime clock), causing unitInstance.getClock() to fail with a ClassCastException when cast to SessionPseudoClock.

Root Cause

In RuleUnitProviderForDSL.ModelRuleUnit.internalCreateInstance(), the RuleUnitExecutorImpl is created before the RuleConfig clock type can be merged into the session configuration:

public RuleUnitInstance<T> internalCreateInstance(T data, RuleConfig ruleConfig) {
    ReteEvaluator reteEvaluator = new RuleUnitExecutorImpl(ruleBase); // uses default REALTIME clock
    return new DSLRuleUnitInstance<>(this, data, reteEvaluator, unitGlobalsResolver, ruleConfig);
}

The DRL-based RuleUnit path handles this via RuleConfigImpl.mergeSessionConfiguration() which is called before the executor is created. The DSL path should do the same.

Expected Behavior

RuleConfig ruleConfig = RuleUnitProvider.get().newRuleConfig();
ruleConfig.setClockType(ClockType.PSEUDO);

try (RuleUnitInstance<MyUnit> unitInstance = RuleUnitProvider.get().createRuleUnitInstance(unit, ruleConfig)) {
    SessionPseudoClock clock = unitInstance.getClock(); // should work
    clock.advanceTime(40L, TimeUnit.MINUTES);
    unitInstance.fire(); // timer rules should fire after advancing
}

Actual Behavior

java.lang.ClassCastException: class org.drools.core.time.impl.JDKTimerService cannot be cast to class org.kie.api.time.SessionPseudoClock

Context

Discovered while implementing rule attribute support for RuleUnit DSL (incubator-kie#6235). The timer attribute is supported in the DSL, but cannot be fully tested with pseudo clock due to this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions