Skip to content

Commit

Permalink
Formatting code
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Oct 4, 2024
1 parent 21f9d5c commit c7a8251
Show file tree
Hide file tree
Showing 54 changed files with 6,096 additions and 4,185 deletions.
7 changes: 5 additions & 2 deletions src/commonMain/kotlin/org/hisp/dhis/rules/Logger.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.hisp.dhis.rules

data class Logger(val severe: (String) -> Unit, val fine: (String) -> Unit)
data class Logger(
val severe: (String) -> Unit,
val fine: (String) -> Unit,
)

expect fun createLogger(className: String): Logger
expect fun createLogger(className: String): Logger
5 changes: 4 additions & 1 deletion src/commonMain/kotlin/org/hisp/dhis/rules/api/DataItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ package org.hisp.dhis.rules.api
* Class is place holder for program rule variable, Constant and program environment variable display name and type.
* @author Zubair Asghar
*/
data class DataItem(val displayName: String, val valueType: ItemValueType)
data class DataItem(
val displayName: String,
val valueType: ItemValueType,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ package org.hisp.dhis.rules.api
import org.hisp.dhis.rules.utils.RuleEngineUtils

object EnvironmentVariables {
val ENV_VARIABLES = mapOf(
Pair(RuleEngineUtils.ENV_VAR_COMPLETED_DATE, ItemValueType.DATE),
Pair(RuleEngineUtils.ENV_VAR_CURRENT_DATE, ItemValueType.DATE),
Pair(RuleEngineUtils.ENV_VAR_EVENT_DATE, ItemValueType.DATE),
Pair(RuleEngineUtils.ENV_VAR_INCIDENT_DATE, ItemValueType.DATE),
Pair(RuleEngineUtils.ENV_VAR_ENROLLMENT_DATE, ItemValueType.DATE),
Pair(RuleEngineUtils.ENV_VAR_DUE_DATE, ItemValueType.DATE),
Pair(RuleEngineUtils.ENV_VAR_EVENT_COUNT, ItemValueType.NUMBER),
Pair(RuleEngineUtils.ENV_VAR_TEI_COUNT, ItemValueType.NUMBER),
Pair(RuleEngineUtils.ENV_VAR_ENROLLMENT_COUNT, ItemValueType.NUMBER),
Pair(RuleEngineUtils.ENV_VAR_EVENT_ID, ItemValueType.NUMBER),
Pair(RuleEngineUtils.ENV_VAR_PROGRAM_STAGE_ID, ItemValueType.NUMBER),
Pair(RuleEngineUtils.ENV_VAR_ENROLLMENT_ID, ItemValueType.NUMBER),
Pair(RuleEngineUtils.ENV_VAR_ENROLLMENT_STATUS, ItemValueType.TEXT),
Pair(RuleEngineUtils.ENV_VAR_EVENT_STATUS, ItemValueType.TEXT),
Pair(RuleEngineUtils.ENV_VAR_OU, ItemValueType.TEXT),
Pair(RuleEngineUtils.ENV_VAR_OU_CODE, ItemValueType.TEXT),
Pair(RuleEngineUtils.ENV_VAR_ENVIRONMENT, ItemValueType.TEXT),
Pair(RuleEngineUtils.ENV_VAR_PROGRAM_NAME, ItemValueType.TEXT),
Pair(RuleEngineUtils.ENV_VAR_PROGRAM_STAGE_NAME, ItemValueType.TEXT)
)
}
val ENV_VARIABLES =
mapOf(
Pair(RuleEngineUtils.ENV_VAR_COMPLETED_DATE, ItemValueType.DATE),
Pair(RuleEngineUtils.ENV_VAR_CURRENT_DATE, ItemValueType.DATE),
Pair(RuleEngineUtils.ENV_VAR_EVENT_DATE, ItemValueType.DATE),
Pair(RuleEngineUtils.ENV_VAR_INCIDENT_DATE, ItemValueType.DATE),
Pair(RuleEngineUtils.ENV_VAR_ENROLLMENT_DATE, ItemValueType.DATE),
Pair(RuleEngineUtils.ENV_VAR_DUE_DATE, ItemValueType.DATE),
Pair(RuleEngineUtils.ENV_VAR_EVENT_COUNT, ItemValueType.NUMBER),
Pair(RuleEngineUtils.ENV_VAR_TEI_COUNT, ItemValueType.NUMBER),
Pair(RuleEngineUtils.ENV_VAR_ENROLLMENT_COUNT, ItemValueType.NUMBER),
Pair(RuleEngineUtils.ENV_VAR_EVENT_ID, ItemValueType.NUMBER),
Pair(RuleEngineUtils.ENV_VAR_PROGRAM_STAGE_ID, ItemValueType.NUMBER),
Pair(RuleEngineUtils.ENV_VAR_ENROLLMENT_ID, ItemValueType.NUMBER),
Pair(RuleEngineUtils.ENV_VAR_ENROLLMENT_STATUS, ItemValueType.TEXT),
Pair(RuleEngineUtils.ENV_VAR_EVENT_STATUS, ItemValueType.TEXT),
Pair(RuleEngineUtils.ENV_VAR_OU, ItemValueType.TEXT),
Pair(RuleEngineUtils.ENV_VAR_OU_CODE, ItemValueType.TEXT),
Pair(RuleEngineUtils.ENV_VAR_ENVIRONMENT, ItemValueType.TEXT),
Pair(RuleEngineUtils.ENV_VAR_PROGRAM_NAME, ItemValueType.TEXT),
Pair(RuleEngineUtils.ENV_VAR_PROGRAM_STAGE_NAME, ItemValueType.TEXT),
)
}
39 changes: 30 additions & 9 deletions src/commonMain/kotlin/org/hisp/dhis/rules/api/RuleEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,37 @@ import org.hisp.dhis.rules.models.*
import kotlin.jvm.JvmStatic

interface RuleEngine {
fun validate(expression: String, dataItemStore: Map<String, DataItem>): RuleValidationResult
fun validateDataFieldExpression(expression: String, dataItemStore: Map<String, DataItem>): RuleValidationResult
fun evaluateAll(enrollmentTarget: RuleEnrollment?, eventsTarget: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffects>
fun evaluate(target: RuleEnrollment, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffect>
fun evaluate(target: RuleEvent, ruleEnrollment: RuleEnrollment?, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffect>
fun validate(
expression: String,
dataItemStore: Map<String, DataItem>,
): RuleValidationResult

fun validateDataFieldExpression(
expression: String,
dataItemStore: Map<String, DataItem>,
): RuleValidationResult

fun evaluateAll(
enrollmentTarget: RuleEnrollment?,
eventsTarget: List<RuleEvent>,
executionContext: RuleEngineContext,
): List<RuleEffects>

fun evaluate(
target: RuleEnrollment,
ruleEvents: List<RuleEvent>,
executionContext: RuleEngineContext,
): List<RuleEffect>

fun evaluate(
target: RuleEvent,
ruleEnrollment: RuleEnrollment?,
ruleEvents: List<RuleEvent>,
executionContext: RuleEngineContext,
): List<RuleEffect>

companion object {
@JvmStatic
fun getInstance(): RuleEngine {
return DefaultRuleEngine()
}
fun getInstance(): RuleEngine = DefaultRuleEngine()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ data class RuleEngineContext(
val rules: List<Rule>,
val ruleVariables: List<RuleVariable> = emptyList(),
val supplementaryData: Map<String, List<String>> = emptyMap(),
val constantsValues: Map<String, String> = emptyMap()
val constantsValues: Map<String, String> = emptyMap(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,88 @@ import org.hisp.dhis.rules.api.RuleEngine
import org.hisp.dhis.rules.api.RuleEngineContext
import org.hisp.dhis.rules.models.*

internal class DefaultRuleEngine: RuleEngine {
override fun evaluate(target: RuleEvent, ruleEnrollment: RuleEnrollment?, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffect> {
val valueMap = RuleVariableValueMapBuilder().build(executionContext.constantsValues, executionContext.ruleVariables, ruleEvents.toSet(), ruleEnrollment, target)
internal class DefaultRuleEngine : RuleEngine {
override fun evaluate(
target: RuleEvent,
ruleEnrollment: RuleEnrollment?,
ruleEvents: List<RuleEvent>,
executionContext: RuleEngineContext,
): List<RuleEffect> {
val valueMap =
RuleVariableValueMapBuilder().build(
executionContext.constantsValues,
executionContext.ruleVariables,
ruleEvents.toSet(),
ruleEnrollment,
target,
)
return RuleConditionEvaluator().getRuleEffects(
TrackerObjectType.EVENT, target.event, valueMap,
executionContext.supplementaryData, executionContext.rules
TrackerObjectType.EVENT,
target.event,
valueMap,
executionContext.supplementaryData,
executionContext.rules,
)
}

override fun evaluate(target: RuleEnrollment, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffect> {
val valueMap = RuleVariableValueMapBuilder().build(executionContext.constantsValues, executionContext.ruleVariables, ruleEvents.toSet(), target)
override fun evaluate(
target: RuleEnrollment,
ruleEvents: List<RuleEvent>,
executionContext: RuleEngineContext,
): List<RuleEffect> {
val valueMap =
RuleVariableValueMapBuilder().build(
executionContext.constantsValues,
executionContext.ruleVariables,
ruleEvents.toSet(),
target,
)
return RuleConditionEvaluator().getRuleEffects(
TrackerObjectType.ENROLLMENT, target.enrollment, valueMap,
executionContext.supplementaryData, executionContext.rules
TrackerObjectType.ENROLLMENT,
target.enrollment,
valueMap,
executionContext.supplementaryData,
executionContext.rules,
)
}

override fun evaluateAll(enrollmentTarget: RuleEnrollment?, eventsTarget: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffects> {
val valueMap = RuleVariableValueMapBuilder()
override fun evaluateAll(
enrollmentTarget: RuleEnrollment?,
eventsTarget: List<RuleEvent>,
executionContext: RuleEngineContext,
): List<RuleEffects> {
val valueMap =
RuleVariableValueMapBuilder()
.multipleBuild(executionContext.constantsValues, executionContext.ruleVariables, eventsTarget.toSet(), enrollmentTarget)
return RuleEngineMultipleExecution().execute(executionContext.rules, valueMap,
executionContext.supplementaryData)
return RuleEngineMultipleExecution().execute(
executionContext.rules,
valueMap,
executionContext.supplementaryData,
)
}

override fun validate(expression: String, dataItemStore: Map<String, DataItem>): RuleValidationResult {
override fun validate(
expression: String,
dataItemStore: Map<String, DataItem>,
): RuleValidationResult {
// Rule condition expression should be evaluated against Boolean
return getExpressionDescription(expression, ExpressionMode.RULE_ENGINE_CONDITION, dataItemStore)
}

override fun validateDataFieldExpression(expression: String, dataItemStore: Map<String, DataItem>): RuleValidationResult {
override fun validateDataFieldExpression(
expression: String,
dataItemStore: Map<String, DataItem>,
): RuleValidationResult {
// Rule action data field should be evaluated against all i.e Boolean, String, Date and Numerical value
return getExpressionDescription(expression, ExpressionMode.RULE_ENGINE_ACTION, dataItemStore)
}

private fun getExpressionDescription(expression: String, mode: ExpressionMode, dataItemStore: Map<String, DataItem>): RuleValidationResult {
return try {
private fun getExpressionDescription(
expression: String,
mode: ExpressionMode,
dataItemStore: Map<String, DataItem>,
): RuleValidationResult =
try {
val validationMap: Map<String, ValueType> = dataItemStore.mapValues { e -> e.value.valueType.toValueType() }
Expression(expression, mode, false).validate(validationMap)
val displayNames: Map<String, String> = dataItemStore.mapValues { e -> e.value.displayName }
Expand All @@ -55,14 +101,13 @@ internal class DefaultRuleEngine: RuleEngine {
RuleValidationResult(
valid = false,
exception = RuleEngineValidationException(ex),
errorMessage = ex.message
errorMessage = ex.message,
)
} catch (ex: ParseException) {
RuleValidationResult(
valid = false,
exception = RuleEngineValidationException(ex),
errorMessage = ex.message
errorMessage = ex.message,
)
}
}
}
Loading

0 comments on commit c7a8251

Please sign in to comment.