diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/Logger.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/Logger.kt index 852ad103..c4fff9bf 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/Logger.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/Logger.kt @@ -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 \ No newline at end of file +expect fun createLogger(className: String): Logger diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/api/DataItem.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/api/DataItem.kt index 3c54ded0..2f7ed1f8 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/api/DataItem.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/api/DataItem.kt @@ -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) \ No newline at end of file +data class DataItem( + val displayName: String, + val valueType: ItemValueType, +) diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/api/EnvironmentVariables.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/api/EnvironmentVariables.kt index 165dac95..dfed951b 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/api/EnvironmentVariables.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/api/EnvironmentVariables.kt @@ -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) - ) -} \ No newline at end of file + 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), + ) +} diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/api/RuleEngine.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/api/RuleEngine.kt index d2e5852a..90f60bd2 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/api/RuleEngine.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/api/RuleEngine.kt @@ -5,16 +5,37 @@ import org.hisp.dhis.rules.models.* import kotlin.jvm.JvmStatic interface RuleEngine { - fun validate(expression: String, dataItemStore: Map): RuleValidationResult - fun validateDataFieldExpression(expression: String, dataItemStore: Map): RuleValidationResult - fun evaluateAll(enrollmentTarget: RuleEnrollment?, eventsTarget: List, executionContext: RuleEngineContext): List - fun evaluate(target: RuleEnrollment, ruleEvents: List, executionContext: RuleEngineContext): List - fun evaluate(target: RuleEvent, ruleEnrollment: RuleEnrollment?, ruleEvents: List, executionContext: RuleEngineContext): List + fun validate( + expression: String, + dataItemStore: Map, + ): RuleValidationResult + + fun validateDataFieldExpression( + expression: String, + dataItemStore: Map, + ): RuleValidationResult + + fun evaluateAll( + enrollmentTarget: RuleEnrollment?, + eventsTarget: List, + executionContext: RuleEngineContext, + ): List + + fun evaluate( + target: RuleEnrollment, + ruleEvents: List, + executionContext: RuleEngineContext, + ): List + + fun evaluate( + target: RuleEvent, + ruleEnrollment: RuleEnrollment?, + ruleEvents: List, + executionContext: RuleEngineContext, + ): List companion object { @JvmStatic - fun getInstance(): RuleEngine { - return DefaultRuleEngine() - } + fun getInstance(): RuleEngine = DefaultRuleEngine() } -} \ No newline at end of file +} diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/api/RuleEngineContext.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/api/RuleEngineContext.kt index eb2e973b..71d45e40 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/api/RuleEngineContext.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/api/RuleEngineContext.kt @@ -7,5 +7,5 @@ data class RuleEngineContext( val rules: List, val ruleVariables: List = emptyList(), val supplementaryData: Map> = emptyMap(), - val constantsValues: Map = emptyMap() + val constantsValues: Map = emptyMap(), ) diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/DefaultRuleEngine.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/DefaultRuleEngine.kt index c3a7d7b9..edd37edf 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/DefaultRuleEngine.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/DefaultRuleEngine.kt @@ -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, executionContext: RuleEngineContext): List { - 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, + executionContext: RuleEngineContext, + ): List { + 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, executionContext: RuleEngineContext): List { - val valueMap = RuleVariableValueMapBuilder().build(executionContext.constantsValues, executionContext.ruleVariables, ruleEvents.toSet(), target) + override fun evaluate( + target: RuleEnrollment, + ruleEvents: List, + executionContext: RuleEngineContext, + ): List { + 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, executionContext: RuleEngineContext): List { - val valueMap = RuleVariableValueMapBuilder() + override fun evaluateAll( + enrollmentTarget: RuleEnrollment?, + eventsTarget: List, + executionContext: RuleEngineContext, + ): List { + 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): RuleValidationResult { + override fun validate( + expression: String, + dataItemStore: Map, + ): RuleValidationResult { // Rule condition expression should be evaluated against Boolean return getExpressionDescription(expression, ExpressionMode.RULE_ENGINE_CONDITION, dataItemStore) } - override fun validateDataFieldExpression(expression: String, dataItemStore: Map): RuleValidationResult { + override fun validateDataFieldExpression( + expression: String, + dataItemStore: Map, + ): 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): RuleValidationResult { - return try { + private fun getExpressionDescription( + expression: String, + mode: ExpressionMode, + dataItemStore: Map, + ): RuleValidationResult = + try { val validationMap: Map = dataItemStore.mapValues { e -> e.value.valueType.toValueType() } Expression(expression, mode, false).validate(validationMap) val displayNames: Map = dataItemStore.mapValues { e -> e.value.displayName } @@ -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, ) } - } } diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleConditionEvaluator.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleConditionEvaluator.kt index 4f744b57..0ef0743f 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleConditionEvaluator.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleConditionEvaluator.kt @@ -17,7 +17,7 @@ internal class RuleConditionEvaluator { targetUid: String, valueMap: Map, supplementaryData: Map>, - rules: List + rules: List, ): List { val ruleEvaluationResults = getRuleEvaluationResults(targetType, targetUid, valueMap, supplementaryData, rules) return ruleEvaluationResults @@ -29,25 +29,27 @@ internal class RuleConditionEvaluator { targetUid: String, valueMap: Map, supplementaryData: Map>, - rules: List + rules: List, ): List { - val ruleEvaluationResults = getRuleEvaluationResults( - targetType, - targetUid, - valueMap, - supplementaryData, - rules - ) + val ruleEvaluationResults = + getRuleEvaluationResults( + targetType, + targetUid, + valueMap, + supplementaryData, + rules, + ) return ruleEvaluationResults .filter { result -> !result.error } .flatMap { result -> result.ruleEffects } } fun getRuleEvaluationResults( - targetType: TrackerObjectType, targetUid: String, + targetType: TrackerObjectType, + targetUid: String, valueMap: Map, supplementaryData: Map>, - rules: List + rules: List, ): List { val mutableValueMap = valueMap.toMutableMap() val ruleEvaluationResults: MutableList = ArrayList() @@ -59,12 +61,12 @@ internal class RuleConditionEvaluator { rule.condition, mutableValueMap, supplementaryData, - ExpressionMode.RULE_ENGINE_CONDITION + ExpressionMode.RULE_ENGINE_CONDITION, ).toBoolean() ) { for (action in rule.actions) { try { - //Check if action is assigning value to calculated variable + // Check if action is assigning value to calculated variable if (isAssignToCalculatedValue(action)) { updateValueMap( unwrapVariableName(action.content()!!), @@ -72,9 +74,9 @@ internal class RuleConditionEvaluator { RuleValueType.TEXT, process(action.data, mutableValueMap, supplementaryData, ExpressionMode.RULE_ENGINE_ACTION), listOf(), - null + null, ), - mutableValueMap + mutableValueMap, ) } else { ruleEffects.add(create(rule, action, mutableValueMap, supplementaryData)) @@ -94,83 +96,92 @@ internal class RuleConditionEvaluator { for ((rule, _, evaluatedAs) in ruleEvaluationResults) { log.fine( "Rule " + rule.name + " with id " + rule.uid + - " executed for " + targetType.name + "(" + targetUid + ")" + - " with condition (" + rule.condition + ")" + - " was evaluated " + evaluatedAs + " executed for " + targetType.name + "(" + targetUid + ")" + + " with condition (" + rule.condition + ")" + + " was evaluated " + evaluatedAs, ) } return ruleEvaluationResults } private fun addRuleErrorResult( - rule: Rule, ruleAction: RuleAction?, e: Exception, targetType: TrackerObjectType, - targetUid: String, ruleEvaluationResults: MutableList + rule: Rule, + ruleAction: RuleAction?, + e: Exception, + targetType: TrackerObjectType, + targetUid: String, + ruleEvaluationResults: MutableList, ) { val errorMessage: String - errorMessage = if (ruleAction != null && e is IllegalExpressionException) { - "Action " + ruleAction::class.simpleName + + errorMessage = + if (ruleAction != null && e is IllegalExpressionException) { + "Action " + ruleAction::class.simpleName + " from rule " + rule.name + " with id " + rule.uid + " executed for " + targetType.name + "(" + targetUid + ")" + " with condition (" + rule.condition + ")" + " raised an error: " + e.message - } else if (ruleAction != null) { - "Action " + ruleAction::class.simpleName + + } else if (ruleAction != null) { + "Action " + ruleAction::class.simpleName + " from rule " + rule.name + " with id " + rule.uid + " executed for " + targetType.name + "(" + targetUid + ")" + " with condition (" + rule.condition + ")" + " raised an unexpected exception: " + e.message - } else if (e is IllegalExpressionException) { - "Rule " + rule.name + " with id " + rule.uid + + } else if (e is IllegalExpressionException) { + "Rule " + rule.name + " with id " + rule.uid + " executed for " + targetType.name + "(" + targetUid + ")" + " with condition (" + rule.condition + ")" + " raised an error: " + e.message - } else { - "Rule " + rule.name + " with id " + rule.uid + + } else { + "Rule " + rule.name + " with id " + rule.uid + " executed for " + targetType.name + "(" + targetUid + ")" + " with condition (" + rule.condition + ")" + " raised an unexpected exception: " + e.message - } + } log.severe(errorMessage) ruleEvaluationResults.add(errorRule(rule, errorMessage)) } private fun process( - condition: String?, valueMap: Map, - supplementaryData: Map>, mode: ExpressionMode + condition: String?, + valueMap: Map, + supplementaryData: Map>, + mode: ExpressionMode, ): String { if (condition.isNullOrEmpty()) { return "" } val expression = Expression(condition, mode, false) - val build = ExpressionData( - valueMap.mapValues { v -> v.value.toVariableValue() }, - emptyMap(), - supplementaryData, - emptyMap(), - emptyMap() - ) - return convertInteger(expression.evaluate({ name: String -> - throw UnsupportedOperationException( - "function not supported: $name" + val build = + ExpressionData( + valueMap.mapValues { v -> v.value.toVariableValue() }, + emptyMap(), + supplementaryData, + emptyMap(), + emptyMap(), ) - }, build)).toString() + return convertInteger( + expression.evaluate({ name: String -> + throw UnsupportedOperationException( + "function not supported: $name", + ) + }, build), + ).toString() } - private fun convertInteger(result: Any?): Any? { - return if (result is Double && result % 1 == 0.0) { + private fun convertInteger(result: Any?): Any? = + if (result is Double && result % 1 == 0.0) { result.toInt() - } else result - } + } else { + result + } - private fun isAssignToCalculatedValue(ruleAction: RuleAction): Boolean { - return ruleAction.type == "ASSIGN" && ruleAction.field() == null - } + private fun isAssignToCalculatedValue(ruleAction: RuleAction): Boolean = ruleAction.type == "ASSIGN" && ruleAction.field() == null private fun updateValueMap( variable: String, value: RuleVariableValue, - valueMap: MutableMap + valueMap: MutableMap, ) { valueMap[variable] = value } @@ -179,7 +190,7 @@ internal class RuleConditionEvaluator { rule: Rule, ruleAction: RuleAction, valueMap: MutableMap, - supplementaryData: Map> + supplementaryData: Map>, ): RuleEffect { if (ruleAction.type == "ASSIGN") { val data = process(ruleAction.data, valueMap, supplementaryData, ExpressionMode.RULE_ENGINE_ACTION) @@ -190,11 +201,21 @@ internal class RuleConditionEvaluator { RuleEffect(rule.uid, ruleAction, data) } } - val data = if (!ruleAction.data.isNullOrEmpty()) process(ruleAction.data, valueMap, supplementaryData, ExpressionMode.RULE_ENGINE_ACTION) else "" + val data = + if (!ruleAction.data.isNullOrEmpty()) { + process( + ruleAction.data, + valueMap, + supplementaryData, + ExpressionMode.RULE_ENGINE_ACTION, + ) + } else { + "" + } return RuleEffect( rule.uid, ruleAction, - data + data, ) } diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleEngineMultipleExecution.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleEngineMultipleExecution.kt index b2cd9227..0aa1f343 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleEngineMultipleExecution.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleEngineMultipleExecution.kt @@ -3,31 +3,43 @@ package org.hisp.dhis.rules.engine import org.hisp.dhis.rules.models.* internal class RuleEngineMultipleExecution { - fun execute(rules: List, ruleVariableValueMap: RuleVariableValueMap, - supplementaryData: Map>): List { + 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) - ) + val enrollmentRuleEffects = + RuleConditionEvaluator() + .getEvaluatedAndErrorRuleEffects( + TrackerObjectType.ENROLLMENT, + enrollment.enrollment, + valueMap, + supplementaryData, + filterRules(rules), + ) ruleEffects.add( RuleEffects( - TrackerObjectType.ENROLLMENT, enrollment.enrollment, - enrollmentRuleEffects - ) + TrackerObjectType.ENROLLMENT, + enrollment.enrollment, + enrollmentRuleEffects, + ), ) } for ((event, valueMap) in ruleVariableValueMap.eventMap) { ruleEffects.add( RuleEffects( - TrackerObjectType.EVENT, event.event, + TrackerObjectType.EVENT, + event.event, RuleConditionEvaluator().getEvaluatedAndErrorRuleEffects( - TrackerObjectType.EVENT, event.event, valueMap, - supplementaryData, filterRules(rules, event) - ) - ) + TrackerObjectType.EVENT, + event.event, + valueMap, + supplementaryData, + filterRules(rules, event), + ), + ), ) } return ruleEffects @@ -38,35 +50,46 @@ internal class RuleEngineMultipleExecution { for (rule in rules) { val programStage: String? = rule.programStage if (programStage.isNullOrEmpty()) { - val ruleActions = filterActionRules( - rule.actions, - AttributeType.TRACKED_ENTITY_ATTRIBUTE - ) + 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 { + 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 - ) + val ruleActions = + filterActionRules( + rule.actions, + AttributeType.DATA_ELEMENT, + ) filteredRules.add(rule.copy(actions = ruleActions)) } } return filteredRules } - private fun filterActionRules(ruleActions: List, attributeType: AttributeType): List { + 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) { + if (ruleAction.attributeType() == null || + ruleAction.attributeType() == attributeType.name || + ruleAction.attributeType() == AttributeType.UNKNOWN.name + ) { filteredRuleActions.add(ruleAction) } } diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleEvaluationResult.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleEvaluationResult.kt index b20385a0..1e5a91ac 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleEvaluationResult.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleEvaluationResult.kt @@ -8,25 +8,28 @@ internal data class RuleEvaluationResult( val rule: Rule, val ruleEffects: List, val evaluatedAs: Boolean, - val error: Boolean + val error: Boolean, ) { companion object { - fun evaluatedResult(rule: Rule, ruleEffects: List): RuleEvaluationResult { - return RuleEvaluationResult(rule, ruleEffects, true, false) - } + fun evaluatedResult( + rule: Rule, + ruleEffects: List, + ): RuleEvaluationResult = RuleEvaluationResult(rule, ruleEffects, true, false) - fun notEvaluatedResult(rule: Rule): RuleEvaluationResult { - return RuleEvaluationResult(rule, emptyList(), false, false) - } + fun notEvaluatedResult(rule: Rule): RuleEvaluationResult = RuleEvaluationResult(rule, emptyList(), false, false) - fun errorRule(rule: Rule, errorMessage: String): RuleEvaluationResult { - val effects = listOf( - RuleEffect( - rule.uid, - RuleAction(errorMessage, "ERROR"), - errorMessage + fun errorRule( + rule: Rule, + errorMessage: String, + ): RuleEvaluationResult { + val effects = + listOf( + RuleEffect( + rule.uid, + RuleAction(errorMessage, "ERROR"), + errorMessage, + ), ) - ) return RuleEvaluationResult(rule, effects, false, true) } } diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValue.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValue.kt index ee6c7c2e..f30e7db1 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValue.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValue.kt @@ -8,18 +8,15 @@ data class RuleVariableValue( val type: RuleValueType, val value: String? = null, val candidates: List = listOf(), - val eventDate: String? = null + val eventDate: String? = null, ) { - fun toVariableValue(): VariableValue { - return VariableValue(valueType(), value, candidates, eventDate) - } + fun toVariableValue(): VariableValue = VariableValue(valueType(), value, candidates, eventDate) - private fun valueType(): ValueType { - return when (type) { + private fun valueType(): ValueType = + when (type) { RuleValueType.DATE -> ValueType.DATE RuleValueType.NUMERIC -> ValueType.NUMBER RuleValueType.BOOLEAN -> ValueType.BOOLEAN else -> ValueType.STRING } - } } diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValueMap.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValueMap.kt index 9546726b..1a53d3b3 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValueMap.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValueMap.kt @@ -5,5 +5,5 @@ import org.hisp.dhis.rules.models.RuleEvent internal data class RuleVariableValueMap( val enrollmentMap: Map>, - val eventMap: Map> -) \ No newline at end of file + val eventMap: Map>, +) diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValueMapBuilder.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValueMapBuilder.kt index ba36333c..b9756264 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValueMapBuilder.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/RuleVariableValueMapBuilder.kt @@ -8,27 +8,54 @@ import org.hisp.dhis.rules.utils.RuleEngineUtils import org.hisp.dhis.rules.utils.currentDate internal class RuleVariableValueMapBuilder { - - fun build(allConstantValues: Map, ruleVariables: List, ruleEvents: Set, ruleEnrollment: RuleEnrollment? = null, ruleEvent: RuleEvent? = null): Map { - val allEvents = ruleEvent?.let { val set = HashSet(ruleEvents); set.add(it); set.toSet() } ?: ruleEvents - return buildEnvironmentVariables(allEvents, ruleEnrollment, ruleEvent) + buildRuleVariableValues(ruleVariables, allEvents, ruleEnrollment, ruleEvent) + buildConstantsValues(allConstantValues) + fun build( + allConstantValues: Map, + ruleVariables: List, + ruleEvents: Set, + ruleEnrollment: RuleEnrollment? = null, + ruleEvent: RuleEvent? = null, + ): Map { + val allEvents = + ruleEvent?.let { + val set = HashSet(ruleEvents) + set.add(it) + set.toSet() + } ?: ruleEvents + return buildEnvironmentVariables(allEvents, ruleEnrollment, ruleEvent) + + buildRuleVariableValues(ruleVariables, allEvents, ruleEnrollment, ruleEvent) + + buildConstantsValues(allConstantValues) } - fun multipleBuild(allConstantValues: Map, ruleVariables: List, ruleEvents: Set, ruleEnrollment: RuleEnrollment? = null): RuleVariableValueMap { - val enrollmentMap = ruleEnrollment?.let { enrollment -> mapOf(Pair(enrollment, build(allConstantValues, ruleVariables, ruleEvents, ruleEnrollment))) }.orEmpty() - return RuleVariableValueMap(enrollmentMap, ruleEvents.associateBy( {it}, {build(allConstantValues, ruleVariables, ruleEvents, ruleEnrollment, it)} )) + fun multipleBuild( + allConstantValues: Map, + ruleVariables: List, + ruleEvents: Set, + ruleEnrollment: RuleEnrollment? = null, + ): RuleVariableValueMap { + val enrollmentMap = + ruleEnrollment + ?.let { enrollment -> + mapOf(Pair(enrollment, build(allConstantValues, ruleVariables, ruleEvents, ruleEnrollment))) + }.orEmpty() + return RuleVariableValueMap( + enrollmentMap, + ruleEvents.associateBy({ + it + }, { build(allConstantValues, ruleVariables, ruleEvents, ruleEnrollment, it) }), + ) } - private fun buildCurrentEnrollmentValues(ruleEnrollment: RuleEnrollment): Map { - return ruleEnrollment.attributeValues.associateBy { it.trackedEntityAttribute } - } + private fun buildCurrentEnrollmentValues(ruleEnrollment: RuleEnrollment): Map = + ruleEnrollment.attributeValues.associateBy { + it.trackedEntityAttribute + } private fun buildAllEventValues(ruleEvents: Set): Map> { val allEventsValues: MutableMap> = HashMap() val events: MutableList = ArrayList(ruleEvents) // sort list of events by eventDate and createdDate: - events.sortWith(compareBy({ it.eventDate}, {it.createdDate }).reversed()) + events.sortWith(compareBy({ it.eventDate }, { it.createdDate }).reversed()) // aggregating values by data element uid for (i in events.indices) { @@ -42,17 +69,24 @@ internal class RuleVariableValueMapBuilder { } // append data value to the list - allEventsValues[ruleDataValue.dataElement]?.add(RuleDataValueHistory(ruleDataValue.value, events[i].eventDate, events[i].createdDate, events[i].programStage)) + allEventsValues[ruleDataValue.dataElement]?.add( + RuleDataValueHistory(ruleDataValue.value, events[i].eventDate, events[i].createdDate, events[i].programStage), + ) } } return allEventsValues.toMap() } - private fun buildConstantsValues(allConstantValues: Map): Map { - return allConstantValues.mapValues { RuleVariableValue(RuleValueType.NUMERIC, it.value) } - } + private fun buildConstantsValues(allConstantValues: Map): Map = + allConstantValues.mapValues { + RuleVariableValue(RuleValueType.NUMERIC, it.value) + } - private fun buildEnvironmentVariables(ruleEvents: Set, ruleEnrollment: RuleEnrollment?, ruleEvent: RuleEvent?): Map { + private fun buildEnvironmentVariables( + ruleEvents: Set, + ruleEnrollment: RuleEnrollment?, + ruleEvent: RuleEvent?, + ): Map { val currentDate = LocalDate.Companion.currentDate() val environmentVariablesValuesMap = buildEnvironmentVariables(ruleEvents, currentDate) @@ -62,67 +96,103 @@ internal class RuleVariableValueMapBuilder { return environmentVariablesValuesMap + enrollmentVariableValueMap + eventVariableValueMap } - private fun buildEnvironmentVariables(ruleEvents: Set, currentDate: LocalDate): Map { + private fun buildEnvironmentVariables( + ruleEvents: Set, + currentDate: LocalDate, + ): Map { val valueMap: MutableMap = HashMap() - valueMap[RuleEngineUtils.ENV_VAR_CURRENT_DATE] = RuleVariableValue( - RuleValueType.TEXT, - currentDate.toString(), - listOf(currentDate.toString()), - currentDate.toString() - ) - valueMap[RuleEngineUtils.ENV_VAR_EVENT_COUNT] = RuleVariableValue( - RuleValueType.NUMERIC, ruleEvents.size.toString(), - listOf(ruleEvents.size.toString()), currentDate.toString() - ) + valueMap[RuleEngineUtils.ENV_VAR_CURRENT_DATE] = + RuleVariableValue( + RuleValueType.TEXT, + currentDate.toString(), + listOf(currentDate.toString()), + currentDate.toString(), + ) + valueMap[RuleEngineUtils.ENV_VAR_EVENT_COUNT] = + RuleVariableValue( + RuleValueType.NUMERIC, + ruleEvents.size.toString(), + listOf(ruleEvents.size.toString()), + currentDate.toString(), + ) val environment = TriggerEnvironment.SERVER.clientName - valueMap[RuleEngineUtils.ENV_VAR_ENVIRONMENT] = RuleVariableValue( - RuleValueType.TEXT, - environment, - listOf(environment), - currentDate.toString() - ) + valueMap[RuleEngineUtils.ENV_VAR_ENVIRONMENT] = + RuleVariableValue( + RuleValueType.TEXT, + environment, + listOf(environment), + currentDate.toString(), + ) return valueMap.toMap() } - private fun buildEventEnvironmentVariables(ruleEvents: Set, ruleEvent: RuleEvent, currentDate: LocalDate): Map { + private fun buildEventEnvironmentVariables( + ruleEvents: Set, + ruleEvent: RuleEvent, + currentDate: LocalDate, + ): Map { val valueMap: MutableMap = HashMap() - val eventDate = ruleEvent.eventDate.toLocalDateTime(TimeZone.currentSystemDefault()).date.toString() - valueMap[RuleEngineUtils.ENV_VAR_EVENT_DATE] = RuleVariableValue( - RuleValueType.TEXT, eventDate, listOf(eventDate), currentDate.toString() - ) + val eventDate = + ruleEvent.eventDate + .toLocalDateTime(TimeZone.currentSystemDefault()) + .date + .toString() + valueMap[RuleEngineUtils.ENV_VAR_EVENT_DATE] = + RuleVariableValue( + RuleValueType.TEXT, + eventDate, + listOf(eventDate), + currentDate.toString(), + ) if (ruleEvent.dueDate != null) { val dueDate = ruleEvent.dueDate - valueMap[RuleEngineUtils.ENV_VAR_DUE_DATE] = RuleVariableValue( - RuleValueType.TEXT, dueDate.toString(), - listOf(dueDate.toString()), currentDate.toString() - ) + valueMap[RuleEngineUtils.ENV_VAR_DUE_DATE] = + RuleVariableValue( + RuleValueType.TEXT, + dueDate.toString(), + listOf(dueDate.toString()), + currentDate.toString(), + ) } if (ruleEvent.completedDate != null) { val completedDate = ruleEvent.completedDate - valueMap[RuleEngineUtils.ENV_VAR_COMPLETED_DATE] = RuleVariableValue( - RuleValueType.TEXT, completedDate.toString(), - listOf(completedDate.toString()), currentDate.toString() - ) + valueMap[RuleEngineUtils.ENV_VAR_COMPLETED_DATE] = + RuleVariableValue( + RuleValueType.TEXT, + completedDate.toString(), + listOf(completedDate.toString()), + currentDate.toString(), + ) } val eventCount = ruleEvents.size.toString() - valueMap[RuleEngineUtils.ENV_VAR_EVENT_COUNT] = RuleVariableValue( - RuleValueType.NUMERIC, eventCount, - listOf(eventCount), currentDate.toString() - ) - valueMap[RuleEngineUtils.ENV_VAR_EVENT_ID] = RuleVariableValue( - RuleValueType.TEXT, ruleEvent.event, - listOf(ruleEvent.event), currentDate.toString() - ) + valueMap[RuleEngineUtils.ENV_VAR_EVENT_COUNT] = + RuleVariableValue( + RuleValueType.NUMERIC, + eventCount, + listOf(eventCount), + currentDate.toString(), + ) + valueMap[RuleEngineUtils.ENV_VAR_EVENT_ID] = + RuleVariableValue( + RuleValueType.TEXT, + ruleEvent.event, + listOf(ruleEvent.event), + currentDate.toString(), + ) val status = ruleEvent.status.toString() - valueMap[RuleEngineUtils.ENV_VAR_EVENT_STATUS] = RuleVariableValue( - RuleValueType.TEXT, status, listOf(status), currentDate.toString() - ) + valueMap[RuleEngineUtils.ENV_VAR_EVENT_STATUS] = + RuleVariableValue( + RuleValueType.TEXT, + status, + listOf(status), + currentDate.toString(), + ) val organisationUnit = ruleEvent.organisationUnit valueMap[RuleEngineUtils.ENV_VAR_OU] = RuleVariableValue(RuleValueType.TEXT, organisationUnit) val programStageId = ruleEvent.programStage - valueMap[RuleEngineUtils.ENV_VAR_PROGRAM_STAGE_ID] = RuleVariableValue(RuleValueType.TEXT, programStageId ) + valueMap[RuleEngineUtils.ENV_VAR_PROGRAM_STAGE_ID] = RuleVariableValue(RuleValueType.TEXT, programStageId) val programStageName = ruleEvent.programStageName valueMap[RuleEngineUtils.ENV_VAR_PROGRAM_STAGE_NAME] = RuleVariableValue(RuleValueType.TEXT, programStageName) val organisationUnitCode = ruleEvent.organisationUnitCode @@ -132,35 +202,56 @@ internal class RuleVariableValueMapBuilder { return valueMap.toMap() } - private fun buildEnrollmentEnvironmentVariables(ruleEnrollment: RuleEnrollment, currentDate: LocalDate): Map { + private fun buildEnrollmentEnvironmentVariables( + ruleEnrollment: RuleEnrollment, + currentDate: LocalDate, + ): Map { val valueMap: MutableMap = HashMap() - valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_ID] = RuleVariableValue( - RuleValueType.TEXT, ruleEnrollment.enrollment, - listOf(ruleEnrollment.enrollment), currentDate.toString() - ) - valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_COUNT] = RuleVariableValue( - RuleValueType.NUMERIC, "1", - listOf("1"), currentDate.toString() - ) - valueMap[RuleEngineUtils.ENV_VAR_TEI_COUNT] = RuleVariableValue( - RuleValueType.NUMERIC, "1", - listOf("1"), currentDate.toString() - ) + valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_ID] = + RuleVariableValue( + RuleValueType.TEXT, + ruleEnrollment.enrollment, + listOf(ruleEnrollment.enrollment), + currentDate.toString(), + ) + valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_COUNT] = + RuleVariableValue( + RuleValueType.NUMERIC, + "1", + listOf("1"), + currentDate.toString(), + ) + valueMap[RuleEngineUtils.ENV_VAR_TEI_COUNT] = + RuleVariableValue( + RuleValueType.NUMERIC, + "1", + listOf("1"), + currentDate.toString(), + ) val enrollmentDate = ruleEnrollment.enrollmentDate - valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_DATE] = RuleVariableValue( - RuleValueType.TEXT, enrollmentDate.toString(), - listOf(enrollmentDate.toString()), currentDate.toString() - ) + valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_DATE] = + RuleVariableValue( + RuleValueType.TEXT, + enrollmentDate.toString(), + listOf(enrollmentDate.toString()), + currentDate.toString(), + ) val incidentDate = ruleEnrollment.incidentDate - valueMap[RuleEngineUtils.ENV_VAR_INCIDENT_DATE] = RuleVariableValue( - RuleValueType.TEXT, incidentDate.toString(), - listOf(incidentDate.toString()), currentDate.toString() - ) + valueMap[RuleEngineUtils.ENV_VAR_INCIDENT_DATE] = + RuleVariableValue( + RuleValueType.TEXT, + incidentDate.toString(), + listOf(incidentDate.toString()), + currentDate.toString(), + ) val status = ruleEnrollment.status.toString() - valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_STATUS] = RuleVariableValue( - RuleValueType.TEXT, status, - listOf(status), currentDate.toString() - ) + valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_STATUS] = + RuleVariableValue( + RuleValueType.TEXT, + status, + listOf(status), + currentDate.toString(), + ) val organisationUnit = ruleEnrollment.organisationUnit valueMap[RuleEngineUtils.ENV_VAR_OU] = RuleVariableValue(RuleValueType.TEXT, organisationUnit) val programName = ruleEnrollment.programName @@ -172,20 +263,27 @@ internal class RuleVariableValueMapBuilder { return valueMap.toMap() } - private fun buildRuleVariableValues(ruleVariables: List, ruleEvents: Set, ruleEnrollment: RuleEnrollment?, ruleEvent: RuleEvent?): Map { + private fun buildRuleVariableValues( + ruleVariables: List, + ruleEvents: Set, + ruleEnrollment: RuleEnrollment?, + ruleEvent: RuleEvent?, + ): Map { // map data values within all events to data elements val allEventValues = buildAllEventValues(ruleEvents) // map tracked entity attributes to values from enrollment - val currentEnrollmentValues = ruleEnrollment?.let {buildCurrentEnrollmentValues(it)}.orEmpty() + val currentEnrollmentValues = ruleEnrollment?.let { buildCurrentEnrollmentValues(it) }.orEmpty() return ruleVariables.associateBy( - {it.name}, - {it.createValues( - ruleEvent, - allEventValues, - currentEnrollmentValues - )} + { it.name }, + { + it.createValues( + ruleEvent, + allEventValues, + currentEnrollmentValues, + ) + }, ) } } diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/AttributeType.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/AttributeType.kt index e5c47be2..caaff902 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/AttributeType.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/AttributeType.kt @@ -6,5 +6,5 @@ package org.hisp.dhis.rules.models enum class AttributeType { DATA_ELEMENT, TRACKED_ENTITY_ATTRIBUTE, - UNKNOWN + UNKNOWN, } diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/Option.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/Option.kt index 91f26b2e..f35f850d 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/Option.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/Option.kt @@ -3,4 +3,7 @@ package org.hisp.dhis.rules.models import kotlin.js.JsExport @JsExport -data class Option(val name: String, val code: String) +data class Option( + val name: String, + val code: String, +) diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/Rule.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/Rule.kt index e55042c2..f6dbcd03 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/Rule.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/Rule.kt @@ -6,10 +6,10 @@ data class Rule( val uid: String = "", val name: String? = null, val programStage: String? = null, - val priority: Int? = 0 -):Comparable{ - override fun compareTo(other: Rule): Int { - return if (this.priority != null && other.priority != null) { + val priority: Int? = 0, +) : Comparable { + override fun compareTo(other: Rule): Int = + if (this.priority != null && other.priority != null) { this.priority.compareTo(other.priority) } else if (this.priority != null) { -1 @@ -18,5 +18,4 @@ data class Rule( } else { 0 } - } } diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleAction.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleAction.kt index f85a01f4..df748b85 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleAction.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleAction.kt @@ -3,17 +3,11 @@ package org.hisp.dhis.rules.models data class RuleAction( val data: String?, val type: String, - val values: Map = emptyMap() -){ - fun content():String? { - return values["content"] - } + val values: Map = emptyMap(), +) { + fun content(): String? = values["content"] - fun field():String? { - return values["field"] - } + fun field(): String? = values["field"] - fun attributeType():String? { - return values["attributeType"] - } -} \ No newline at end of file + fun attributeType(): String? = values["attributeType"] +} diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleAttributeValue.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleAttributeValue.kt index 3c9e1570..a42f0235 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleAttributeValue.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleAttributeValue.kt @@ -5,5 +5,5 @@ import kotlin.js.JsExport @JsExport data class RuleAttributeValue( val trackedEntityAttribute: String, - val value: String + val value: String, ) diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleDataValue.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleDataValue.kt index f47342a3..7500ae8c 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleDataValue.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleDataValue.kt @@ -5,5 +5,5 @@ import kotlin.js.JsExport @JsExport data class RuleDataValue( val dataElement: String, - val value: String + val value: String, ) diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleDataValueHistory.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleDataValueHistory.kt index c9a8b5bd..b8ba40fa 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleDataValueHistory.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleDataValueHistory.kt @@ -6,5 +6,5 @@ data class RuleDataValueHistory( val value: String, val eventDate: Instant, val createdDate: Instant, - val programStage: String + val programStage: String, ) diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEffect.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEffect.kt index 66f3ca22..c8747ad5 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEffect.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEffect.kt @@ -3,5 +3,5 @@ package org.hisp.dhis.rules.models data class RuleEffect( val ruleId: String, val ruleAction: RuleAction, - val data: String? = "" -) \ No newline at end of file + val data: String? = "", +) diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEffects.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEffects.kt index f7676281..fdd8d442 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEffects.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEffects.kt @@ -1,8 +1,9 @@ package org.hisp.dhis.rules.models + data class RuleEffects( val trackerObjectType: TrackerObjectType, val trackerObjectUid: String, - val ruleEffects: List + val ruleEffects: List, ) { val isEnrollment: Boolean get() = trackerObjectType == TrackerObjectType.ENROLLMENT diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEngineValidationException.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEngineValidationException.kt index 7a7df64d..889d9f10 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEngineValidationException.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEngineValidationException.kt @@ -1,3 +1,5 @@ package org.hisp.dhis.rules.models -class RuleEngineValidationException(cause: IllegalArgumentException) : IllegalArgumentException(cause.message) \ No newline at end of file +class RuleEngineValidationException( + cause: IllegalArgumentException, +) : IllegalArgumentException(cause.message) diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEnrollment.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEnrollment.kt index 12382f4e..379d9444 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEnrollment.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEnrollment.kt @@ -10,5 +10,5 @@ data class RuleEnrollment( val status: RuleEnrollmentStatus, val organisationUnit: String, val organisationUnitCode: String?, - val attributeValues: List + val attributeValues: List, ) diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEnrollmentStatus.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEnrollmentStatus.kt index 11c96d4d..17a9327b 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEnrollmentStatus.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEnrollmentStatus.kt @@ -6,5 +6,5 @@ import kotlin.js.JsExport enum class RuleEnrollmentStatus { ACTIVE, COMPLETED, - CANCELLED -} \ No newline at end of file + CANCELLED, +} diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEvent.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEvent.kt index 8379c06b..08f73b7b 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEvent.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEvent.kt @@ -14,5 +14,5 @@ data class RuleEvent( val completedDate: LocalDate?, val organisationUnit: String, val organisationUnitCode: String?, - val dataValues: List + val dataValues: List, ) diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEventStatus.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEventStatus.kt index 5476f787..422e16e1 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEventStatus.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEventStatus.kt @@ -9,5 +9,5 @@ enum class RuleEventStatus { SCHEDULE, SKIPPED, VISITED, - OVERDUE -} \ No newline at end of file + OVERDUE, +} diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleValidationResult.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleValidationResult.kt index 150d4327..347375c3 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleValidationResult.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleValidationResult.kt @@ -33,24 +33,24 @@ import kotlin.jvm.JvmStatic @JsExport data class RuleValidationResult( - val valid: Boolean, - val errorMessage: String? = null, - val exception: Throwable? = null, - val description: String? = null -){ - companion object { - @JvmStatic - fun invalid(errorMessage: String): RuleValidationResult { - return RuleValidationResult( - valid = false, errorMessage = errorMessage - ) - } + val valid: Boolean, + val errorMessage: String? = null, + val exception: Throwable? = null, + val description: String? = null, +) { + companion object { + @JvmStatic + fun invalid(errorMessage: String): RuleValidationResult = + RuleValidationResult( + valid = false, + errorMessage = errorMessage, + ) - @JvmStatic - fun valid(description: String): RuleValidationResult { - return RuleValidationResult( - valid = true, description = description - ) - } - } -} \ No newline at end of file + @JvmStatic + fun valid(description: String): RuleValidationResult = + RuleValidationResult( + valid = true, + description = description, + ) + } +} diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleValueType.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleValueType.kt index 1df5f8b3..bfb00c60 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleValueType.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleValueType.kt @@ -3,13 +3,14 @@ package org.hisp.dhis.rules.models import kotlin.js.JsExport @JsExport -enum class RuleValueType(private val defaultValue: Any) { +enum class RuleValueType( + private val defaultValue: Any, +) { TEXT(""), NUMERIC(0.0), BOOLEAN(false), - DATE("2020-01-01"); + DATE("2020-01-01"), + ; - fun defaultValue(): Any { - return defaultValue - } + fun defaultValue(): Any = defaultValue } diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleVariable.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleVariable.kt index f290fc71..651decba 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleVariable.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleVariable.kt @@ -8,17 +8,18 @@ interface RuleVariable { val options: List