Skip to content

Commit

Permalink
refactor: Clean up evaluation call to rule engine
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Dec 21, 2023
1 parent 578ed58 commit cfaf295
Show file tree
Hide file tree
Showing 21 changed files with 308 additions and 422 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class RuleConditionEvaluator {
}

private fun addRuleErrorResult(
rule: Rule, ruleAction: org.hisp.dhis.rules.models.RuleAction?, e: Exception, targetType: TrackerObjectType,
rule: Rule, ruleAction: RuleAction?, e: Exception, targetType: TrackerObjectType,
targetUid: String, ruleEvaluationResults: MutableList<RuleEvaluationResult>
) {
val errorMessage: String
Expand Down Expand Up @@ -168,7 +168,7 @@ class RuleConditionEvaluator {
} else result
}

private fun isAssignToCalculatedValue(ruleAction: org.hisp.dhis.rules.models.RuleAction): Boolean {
private fun isAssignToCalculatedValue(ruleAction: RuleAction): Boolean {
return ruleAction is RuleActionAssign && ruleAction.field.isEmpty()
}

Expand All @@ -182,7 +182,7 @@ class RuleConditionEvaluator {

private fun create(
rule: Rule,
ruleAction: org.hisp.dhis.rules.models.RuleAction,
ruleAction: RuleAction,
valueMap: MutableMap<String, RuleVariableValue>,
supplementaryData: Map<String, List<String>>
): RuleEffect {
Expand Down
72 changes: 17 additions & 55 deletions src/commonMain/kotlin/org/hisp/dhis/rules/RuleEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,38 @@ import org.hisp.dhis.lib.expression.spi.ParseException
import org.hisp.dhis.lib.expression.spi.ValueType
import org.hisp.dhis.rules.models.*

data class RuleEngine(
val executionContext: RuleEngineContext,
val events: List<RuleEvent> = emptyList(),
val enrollment: RuleEnrollment? = null,
val triggerEnvironment: TriggerEnvironment? = TriggerEnvironment.SERVER) {
class RuleEngine {

fun evaluate(ruleEvent: RuleEvent): List<RuleEffect> {
return evaluate(ruleEvent, executionContext.rules)
}

fun evaluate(ruleEvent: RuleEvent, rulesToEvaluate: List<Rule>): List<RuleEffect> {
fun evaluate(ruleEvent: RuleEvent, executionContext: RuleEngineContext): List<RuleEffect> {
val valueMap = RuleVariableValueMapBuilder.target(ruleEvent)
.ruleVariables(executionContext.ruleVariables)
.ruleEnrollment(enrollment)
.triggerEnvironment(triggerEnvironment)
.ruleEvents(events)
.constantValueMap(executionContext.constantsValues)
.build()
return RuleEngineExecution(ruleEvent, null, rulesToEvaluate, valueMap, executionContext.supplementaryData).execute()
.ruleVariables(executionContext.ruleVariables)
.ruleEnrollment(executionContext.enrollment)
.triggerEnvironment(TriggerEnvironment.SERVER)
.ruleEvents(executionContext.events)
.constantValueMap(executionContext.constantsValues)
.build()
return RuleEngineExecution(ruleEvent, null, executionContext.rules, valueMap, executionContext.supplementaryData).execute()
}

fun evaluate(ruleEnrollment: RuleEnrollment,
rulesToEvaluate: List<Rule>): List<RuleEffect> {
fun evaluate(ruleEnrollment: RuleEnrollment, executionContext: RuleEngineContext): List<RuleEffect> {
val valueMap = RuleVariableValueMapBuilder.target(ruleEnrollment)
.ruleVariables(executionContext.ruleVariables)
.triggerEnvironment(triggerEnvironment)
.ruleEvents(events)
.triggerEnvironment(TriggerEnvironment.SERVER)
.ruleEvents(executionContext.events)
.constantValueMap(executionContext.constantsValues)
.build()
return RuleEngineExecution(null, ruleEnrollment, rulesToEvaluate, valueMap, executionContext.supplementaryData).execute()
return RuleEngineExecution(null, ruleEnrollment, executionContext.rules, valueMap, executionContext.supplementaryData).execute()
}

fun evaluate(): List<RuleEffects> {
fun evaluate(executionContext: RuleEngineContext): List<RuleEffects> {
val valueMap = RuleVariableValueMapBuilder.target()
.ruleVariables(executionContext.ruleVariables)
.ruleEnrollment(enrollment)
.triggerEnvironment(triggerEnvironment)
.ruleEvents(events)
.ruleEnrollment(executionContext.enrollment)
.triggerEnvironment(TriggerEnvironment.SERVER)
.ruleEvents(executionContext.events)
.constantValueMap(executionContext.constantsValues)
.multipleBuild()
return RuleEngineMultipleExecution(executionContext.rules, valueMap,
executionContext.supplementaryData).execute()
}

fun evaluate( ruleEnrollment: RuleEnrollment): List<RuleEffect> {
return evaluate(ruleEnrollment, executionContext.rules)
}

@Deprecated("use data copy")
class Builder internal constructor(private val ruleEngineContext: RuleEngineContext) {
private var ruleEvents: List<RuleEvent> = emptyList()
private var ruleEnrollment: org.hisp.dhis.rules.models.RuleEnrollment? = null
private var triggerEnvironment: TriggerEnvironment? = null
fun events(ruleEvents: List<RuleEvent>): Builder {
this.ruleEvents = ruleEvents
return this
}

fun enrollment(ruleEnrollment: org.hisp.dhis.rules.models.RuleEnrollment): Builder {
this.ruleEnrollment = ruleEnrollment
return this
}

fun triggerEnvironment(triggerEnvironment: TriggerEnvironment): Builder {
this.triggerEnvironment = triggerEnvironment
return this
}

fun build(): RuleEngine {
return RuleEngine(ruleEngineContext, ruleEvents, ruleEnrollment, triggerEnvironment)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.hisp.dhis.rules

import org.hisp.dhis.rules.models.Rule
import org.hisp.dhis.rules.models.RuleEnrollment
import org.hisp.dhis.rules.models.RuleEvent
import org.hisp.dhis.rules.models.RuleVariable

data class RuleEngineContext(
val rules: List<Rule>,
val ruleVariables: List<RuleVariable>,
val ruleVariables: List<RuleVariable> = emptyList(),
val events: List<RuleEvent> = emptyList(),
val enrollment: RuleEnrollment? = null,
val supplementaryData: Map<String, List<String>> = emptyMap(),
val constantsValues: Map<String, String> = emptyMap(),
val ruleEngineIntent: RuleEngineIntent = RuleEngineIntent.EVALUATION
val constantsValues: Map<String, String> = emptyMap()
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.hisp.dhis.rules.models.*

internal data class RuleEngineExecution(
val event: RuleEvent?,
val enrollment: org.hisp.dhis.rules.models.RuleEnrollment?,
val enrollment: RuleEnrollment?,
val rules: List<Rule>,
val valueMap: MutableMap<String, RuleVariableValue>,
val supplementaryData: Map<String, List<String>>
Expand Down
6 changes: 3 additions & 3 deletions src/commonMain/kotlin/org/hisp/dhis/rules/RuleEngineFilter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import org.hisp.dhis.rules.models.*
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
fun filterRules(rules: List<Rule>, ruleEnrollment: org.hisp.dhis.rules.models.RuleEnrollment?): List<Rule> {
fun filterRules(rules: List<Rule>, ruleEnrollment: RuleEnrollment?): List<Rule> {
val filteredRules: MutableList<Rule> = mutableListOf()
for (rule in rules) {
val programStage: String? = rule.programStage
Expand All @@ -44,7 +44,7 @@ import org.hisp.dhis.rules.models.*
return filteredRules
}

fun filterRules(rules: List<Rule>, ruleEvent: org.hisp.dhis.rules.models.RuleEvent): List<Rule> {
fun filterRules(rules: List<Rule>, ruleEvent: RuleEvent): List<Rule> {
val filteredRules: MutableList<Rule> = mutableListOf()
for (rule in rules) {
val programStage: String? = rule.programStage
Expand All @@ -59,7 +59,7 @@ import org.hisp.dhis.rules.models.*
return filteredRules
}

private fun filterActionRules(ruleActions: List<org.hisp.dhis.rules.models.RuleAction>, attributeType: org.hisp.dhis.rules.models.AttributeType): List<org.hisp.dhis.rules.models.RuleAction> {
private fun filterActionRules(ruleActions: List<org.hisp.dhis.rules.models.RuleAction>, attributeType: AttributeType): List<org.hisp.dhis.rules.models.RuleAction> {
val filteredRuleActions: MutableList<org.hisp.dhis.rules.models.RuleAction> = mutableListOf()
for (ruleAction in ruleActions) {
if (ruleAction !is RuleActionAttribute || ruleAction.attributeType() == attributeType || ruleAction.attributeType() == org.hisp.dhis.rules.models.AttributeType.UNKNOWN) {
Expand Down
36 changes: 0 additions & 36 deletions src/commonMain/kotlin/org/hisp/dhis/rules/RuleEngineIntent.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.hisp.dhis.lib.expression.spi.VariableValue
import org.hisp.dhis.rules.models.RuleValueType

data class RuleVariableValue(
val type: org.hisp.dhis.rules.models.RuleValueType,
val type: RuleValueType,
val value: String? = null,
val candidates: List<String> = listOf(),
val eventDate: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RuleVariableValueMapBuilder private constructor() {
private val ruleVariables: MutableList<RuleVariable>

private val ruleEvents: MutableList<RuleEvent>
var ruleEnrollment: org.hisp.dhis.rules.models.RuleEnrollment? = null
var ruleEnrollment: RuleEnrollment? = null
var ruleEvent: RuleEvent? = null
private var triggerEnvironment: TriggerEnvironment? = null

Expand All @@ -23,7 +23,7 @@ class RuleVariableValueMapBuilder private constructor() {
allConstantValues = HashMap()
}

private constructor(ruleEnrollment: org.hisp.dhis.rules.models.RuleEnrollment) : this() {
private constructor(ruleEnrollment: RuleEnrollment) : this() {

// enrollment is the target
this.ruleEnrollment = ruleEnrollment
Expand All @@ -40,7 +40,7 @@ class RuleVariableValueMapBuilder private constructor() {
return this
}

fun ruleEnrollment(ruleEnrollment: org.hisp.dhis.rules.models.RuleEnrollment?): RuleVariableValueMapBuilder {
fun ruleEnrollment(ruleEnrollment: RuleEnrollment?): RuleVariableValueMapBuilder {
check(this.ruleEnrollment == null) {
"It seems that enrollment has been set as target " +
"already. It can't be used as a part of execution context."
Expand Down Expand Up @@ -111,8 +111,8 @@ class RuleVariableValueMapBuilder private constructor() {
return false
}

private fun buildCurrentEventValues(): Map<String, org.hisp.dhis.rules.models.RuleDataValue> {
val currentEventValues: MutableMap<String, org.hisp.dhis.rules.models.RuleDataValue> = HashMap()
private fun buildCurrentEventValues(): Map<String, RuleDataValue> {
val currentEventValues: MutableMap<String, RuleDataValue> = HashMap()
if (ruleEvent != null) {
for (index in ruleEvent!!.dataValues.indices) {
val ruleDataValue = ruleEvent!!.dataValues[index]
Expand All @@ -122,8 +122,8 @@ class RuleVariableValueMapBuilder private constructor() {
return currentEventValues
}

private fun buildCurrentEnrollmentValues(): Map<String, org.hisp.dhis.rules.models.RuleAttributeValue> {
val currentEnrollmentValues: MutableMap<String, org.hisp.dhis.rules.models.RuleAttributeValue> = HashMap()
private fun buildCurrentEnrollmentValues(): Map<String, RuleAttributeValue> {
val currentEnrollmentValues: MutableMap<String, RuleAttributeValue> = HashMap()
if (ruleEnrollment != null) {
val ruleAttributeValues = ruleEnrollment!!.attributeValues
for (attributeValue in ruleAttributeValues) {
Expand All @@ -133,8 +133,8 @@ class RuleVariableValueMapBuilder private constructor() {
return currentEnrollmentValues
}

private fun buildAllEventValues(): Map<String, MutableList<org.hisp.dhis.rules.models.RuleDataValue>> {
val allEventsValues: MutableMap<String, MutableList<org.hisp.dhis.rules.models.RuleDataValue>> = HashMap()
private fun buildAllEventValues(): Map<String, MutableList<RuleDataValue>> {
val allEventsValues: MutableMap<String, MutableList<RuleDataValue>> = HashMap()
val events: MutableList<RuleEvent> = ArrayList(ruleEvents)
if (ruleEvent != null) {
// target event should be among the list of all
Expand Down Expand Up @@ -302,7 +302,7 @@ class RuleVariableValueMapBuilder private constructor() {
}

companion object {
fun target(ruleEnrollment: org.hisp.dhis.rules.models.RuleEnrollment): RuleVariableValueMapBuilder {
fun target(ruleEnrollment: RuleEnrollment): RuleVariableValueMapBuilder {
return RuleVariableValueMapBuilder(ruleEnrollment)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ data class RuleEnrollment(
val programName: String,
val incidentDate: LocalDate,
val enrollmentDate: LocalDate,
val status: org.hisp.dhis.rules.models.RuleEnrollment.Status,
val status: RuleEnrollment.Status,
val organisationUnit: String,
val organisationUnitCode: String,
val attributeValues: List<org.hisp.dhis.rules.models.RuleAttributeValue>
Expand Down
Loading

0 comments on commit cfaf295

Please sign in to comment.