Versioning and Archiving of Rules

Rule Engine includes a versioning and archiving of rules mechanism, which ensures all changes made to the DroolsRule Model are being reported. The version numbers are incremental, but not progressive. The basis for calculation of the next version number is a maximum version calculated for all rules associated to the same rule module.

The Rules archiving mechanism keeping track of all the changes happening in the Rule Engine, and makes it possible to have a consistent representation of the applied rules at the moment the promotion chain is triggered. The rules are grouped by the Knowledge base and the Rule Knowledge Base module in the Drools implementation. This implementation keeps track of the module version without archiving the historical version of the module reference. If needed, the module snapshot can be readily reconstructed knowing the rules scope for a requested version.

Technical Implementation

The Rule versioning related classes are located in the de.hybris.platform.ruleengine.versioning package. The data type interceptors, archiving the historical versions and validating these are in the de.hybris.platform.ruleengine.dao.interceptors package.

The following displays the RuleEngineRulePrepareInterceptor code:

@Override
public void onPrepare(final DroolsRuleModel droolsRule, final InterceptorContext context) throws InterceptorException
{
    if (isNull(droolsRule.getVersion()) && nonNull(droolsRule.getKieBase()))
    {
        droolsRule.setVersion(getCurrentRulesVersion(droolsRule.getKieBase().getKieModule()));
    }
    else if (isNull(droolsRule.getVersion()))
    {
        droolsRule.setVersion(DEFAULT_VERSION);
    }
    if (context.isNew(droolsRule))
    {
        if (isNull(droolsRule.getUuid()))
        {
            final UUID uuid = UUID.randomUUID();
            droolsRule.setUuid(uuid.toString());
        }
    }
    else
    {
        getHistoricalContentCreator().createHistoricalVersion(droolsRule, context);
    }
    droolsRule.setChecksum(calculateChecksum(droolsRule));
}

The following displays the RuleEngineRuleValidateInterceptor code:

@Override
public void onValidate(final DroolsRuleModel droolsRule, final InterceptorContext ctx) throws InterceptorException
{
    if (isEmpty(ctx.getElementsRegisteredFor(PersistenceOperation.DELETE)))
    {
        final AbstractValidationResult validationResult = getValidationResult(droolsRule, ctx);
        if (!validationResult.succeeded())
        {
            throw new InterceptorException(
                    "Validation of " + droolsRule + " has failed with message '" + validationResult.getErrorMessage() + "'.", this);
        }
    }
}

The following displays the de.hybris.platform.droolsruleengineservices.interceptors.DroolsKIEBasePrepareInterceptor code:

@Override
public void onPrepare(final DroolsKIEBaseModel base, final InterceptorContext context) throws InterceptorException
{
    final DroolsKIEModuleModel kieModule = base.getKieModule();
    final Set<DroolsRuleModel> rules = base.getRules();
    // if the module is associated with the base, make it sure it has a correct version, derived from the associated rules version
    if (nonNull(kieModule))
    {
        final Long moduleVersion = kieModule.getVersion();
        final Long currentRulesVersion = getCurrentRulesVersion(kieModule);
        if (nonNull(moduleVersion) && nonNull(currentRulesVersion)
                && currentRulesVersion.longValue() > moduleVersion.longValue())
        {
            kieModule.setVersion(currentRulesVersion);
        }
        if (isNotEmpty(rules))
        {
            rules.stream().max(comparing(DroolsRuleModel::getVersion))
                    .ifPresent(r -> setNewVersionIfApplicable(kieModule, currentRulesVersion, r.getVersion()));
        }
    }
}

The Rule Engine implementation of RuleEngineRulePrepareInterceptor is based on checksum calculation (using the md5Hex algorithm on rule payload) and global parameters map, the developers can change this by implementing their own version of this interface and registering it in the spring context as shown below in the ruleModelChecksumCalculator code:

<alias name="defaultRuleModelChecksumCalculator" alias="ruleModelChecksumCalculator" />
<bean id="defaultRuleModelChecksumCalculator" class="de.hybris.platform.ruleengine.versioning.impl.RuleEngineRuleModelChecksumCalculator"/>

RuleModelHistoricalContentCreator

This interface is used in the RuleEngineRulePrepareInterceptor as an encapsulation for all operations related to rule versioning and archiving. It has a simple structure, accepting the AbstractRuleEngineRuleModel and InterceptorContext as input parameters:

@FunctionalInterface
public interface RuleModelHistoricalContentCreator
{
    /**
     * creates the historical version of the model
     *
     * @param rule
     *           - the model to be versioned
     * @param context
     *           - the InterceptorContext instance, where the versioned copy is registered
     */
    void createHistoricalVersion(AbstractRuleEngineRuleModel rule, InterceptorContext context);
}

The default implementation of this interface performs the correct creation of historical rule model, but it may still require further extension of functionalities related to, for example, extension of the AbstractRuleEngineRuleModel. The extension in this case is provided by a configurable list of HistoricalRuleContentProvider:

/**
 * Historical rule content provider interface to be used in order to set the historical copy incrementally
 */
public interface HistoricalRuleContentProvider
{
    void copyOriginalValuesIntoHistoricalVersion(final AbstractRuleEngineRuleModel droolsRule,
            final AbstractRuleEngineRuleModel historicalDroolsRule, final InterceptorContext ctx);
}

To copy additional attributes to a historical version, using this mechanism, provide a custom implementation of this interface through spring context configuration, such as in the following example code snippet:

<bean id="myModuleHistoricalRuleContentProvider" class="de.hybris.platform.mymodule.versioning.impl.MyModuleHistoricalRuleContentProvider"/>
 
<bean depends-on="historicalRuleContentProviders" parent="listMergeDirective">
    <property name="add" ref="myModuleHistoricalRuleContentProvider"/>
</bean>

KIE Module

When the KIE Module is available (for Drools-based implementation), it is also being versioned and keeps the maximum version of all associated rules. This provides the a unique way to reconstruct the KIE Module composition for each rule version available in the platform.