All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.org.hisp.dhis.rules.engine.RuleEngineMultipleExecution.kt Maven / Gradle / Ivy

package org.hisp.dhis.rules.engine

import org.hisp.dhis.rules.models.*

internal class RuleEngineMultipleExecution {
    fun execute(
        rules: List,
        ruleVariableValueMap: RuleVariableValueMap,
        supplementaryData: Map>,
    ): List {
        val ruleEffects: MutableList = ArrayList()
        for ((enrollment, valueMap) in ruleVariableValueMap.enrollmentMap) {
            val enrollmentRuleEffects =
                RuleConditionEvaluator()
                    .getEvaluatedAndErrorRuleEffects(
                        TrackerObjectType.ENROLLMENT,
                        enrollment.enrollment,
                        valueMap,
                        supplementaryData,
                        filterRules(rules),
                    )
            ruleEffects.add(
                RuleEffects(
                    TrackerObjectType.ENROLLMENT,
                    enrollment.enrollment,
                    enrollmentRuleEffects,
                ),
            )
        }
        for ((event, valueMap) in ruleVariableValueMap.eventMap) {
            ruleEffects.add(
                RuleEffects(
                    TrackerObjectType.EVENT,
                    event.event,
                    RuleConditionEvaluator().getEvaluatedAndErrorRuleEffects(
                        TrackerObjectType.EVENT,
                        event.event,
                        valueMap,
                        supplementaryData,
                        filterRules(rules, event),
                    ),
                ),
            )
        }
        return ruleEffects
    }

    private fun filterRules(rules: List): List {
        val filteredRules: MutableList = mutableListOf()
        for (rule in rules) {
            val programStage: String? = rule.programStage
            if (programStage.isNullOrEmpty()) {
                val ruleActions =
                    filterActionRules(
                        rule.actions,
                        AttributeType.TRACKED_ENTITY_ATTRIBUTE,
                    )
                filteredRules.add(rule.copy(actions = ruleActions))
            }
        }
        return filteredRules
    }

    private fun filterRules(
        rules: List,
        ruleEvent: RuleEvent,
    ): List {
        val filteredRules: MutableList = mutableListOf()
        for (rule in rules) {
            val programStage: String? = rule.programStage
            if (programStage.isNullOrEmpty() || programStage == ruleEvent.programStage) {
                val ruleActions =
                    filterActionRules(
                        rule.actions,
                        AttributeType.DATA_ELEMENT,
                    )
                filteredRules.add(rule.copy(actions = ruleActions))
            }
        }
        return filteredRules
    }

    private fun filterActionRules(
        ruleActions: List,
        attributeType: AttributeType,
    ): List {
        val filteredRuleActions: MutableList = mutableListOf()
        for (ruleAction in ruleActions) {
            if (ruleAction.attributeType() == null ||
                ruleAction.attributeType() == attributeType.name ||
                ruleAction.attributeType() == AttributeType.UNKNOWN.name
            ) {
                filteredRuleActions.add(ruleAction)
            }
        }
        return filteredRuleActions
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy