Skip to content

Commit

Permalink
Merge pull request #135 from dhis2/env_expected_function
Browse files Browse the repository at this point in the history
fix: Fix native trigger environment
  • Loading branch information
enricocolasante authored May 16, 2024
2 parents 24a80bf + 39c0846 commit cb47f8c
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/commonMain/kotlin/org/hisp/dhis/rules/Environment.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package org.hisp.dhis.rules

expect fun getEnvironment(): String
import org.hisp.dhis.rules.models.TriggerEnvironment

expect fun getEnvironment(): TriggerEnvironment
3 changes: 3 additions & 0 deletions src/commonMain/kotlin/org/hisp/dhis/rules/api/RuleEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ 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, triggerEnvironment: TriggerEnvironment): List<RuleEffects>
fun evaluate(target: RuleEnrollment, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext, triggerEnvironment: TriggerEnvironment): List<RuleEffect>
fun evaluate(target: RuleEvent, ruleEnrollment: RuleEnrollment?, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext, triggerEnvironment: TriggerEnvironment): List<RuleEffect>
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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import org.hisp.dhis.rules.getEnvironment
import org.hisp.dhis.rules.models.*

internal class DefaultRuleEngine: RuleEngine {
override fun evaluate(target: RuleEvent, ruleEnrollment: RuleEnrollment?, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffect> {
override fun evaluate(target: RuleEvent, ruleEnrollment: RuleEnrollment?, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext, triggerEnvironment: TriggerEnvironment): List<RuleEffect> {
val valueMap = RuleVariableValueMapBuilder.target(target)
.ruleVariables(executionContext.ruleVariables)
.ruleEnrollment(ruleEnrollment)
.triggerEnvironment(TriggerEnvironment.valueOf(getEnvironment()))
.triggerEnvironment(triggerEnvironment)
.ruleEvents(ruleEvents)
.constantValueMap(executionContext.constantsValues)
.build()
Expand All @@ -26,10 +26,14 @@ internal class DefaultRuleEngine: RuleEngine {
)
}

override fun evaluate(target: RuleEnrollment, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffect> {
override fun evaluate(target: RuleEvent, ruleEnrollment: RuleEnrollment?, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffect> {
return evaluate(target, ruleEnrollment, ruleEvents, executionContext, getEnvironment())
}

override fun evaluate(target: RuleEnrollment, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext, triggerEnvironment: TriggerEnvironment): List<RuleEffect> {
val valueMap = RuleVariableValueMapBuilder.target(target)
.ruleVariables(executionContext.ruleVariables)
.triggerEnvironment(TriggerEnvironment.valueOf(getEnvironment()))
.triggerEnvironment(triggerEnvironment)
.ruleEvents(ruleEvents)
.constantValueMap(executionContext.constantsValues)
.build()
Expand All @@ -39,18 +43,26 @@ internal class DefaultRuleEngine: RuleEngine {
)
}

override fun evaluateAll(enrollmentTarget: RuleEnrollment?, eventsTarget: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffects> {
override fun evaluate(target: RuleEnrollment, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffect> {
return evaluate(target, ruleEvents, executionContext, getEnvironment())
}

override fun evaluateAll(enrollmentTarget: RuleEnrollment?, eventsTarget: List<RuleEvent>, executionContext: RuleEngineContext, triggerEnvironment: TriggerEnvironment): List<RuleEffects> {
val valueMap = RuleVariableValueMapBuilder.target()
.ruleVariables(executionContext.ruleVariables)
.ruleEnrollment(enrollmentTarget)
.triggerEnvironment(TriggerEnvironment.valueOf(getEnvironment()))
.triggerEnvironment(triggerEnvironment)
.ruleEvents(eventsTarget)
.constantValueMap(executionContext.constantsValues)
.multipleBuild()
return RuleEngineMultipleExecution().execute(executionContext.rules, valueMap,
executionContext.supplementaryData)
}

override fun evaluateAll(enrollmentTarget: RuleEnrollment?, eventsTarget: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffects> {
return evaluateAll(enrollmentTarget, eventsTarget, executionContext, getEnvironment())
}

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import kotlin.js.JsExport
@JsExport
enum class TriggerEnvironment(val clientName: String) {
ANDROIDCLIENT("AndroidClient"),
NATIVE("Native"),
SERVER("Server"),
WEBCLIENT("WebClient")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ProgramRuleVariableTest {
fun testEnvironmentProgramVariableIsAssigned() {
val rule = getRule("V{environment}")
val ruleEffects = callEnrollmentRuleEngine(rule)
assertProgramRuleVariableAssignment(ruleEffects, rule, TriggerEnvironment.valueOf(getEnvironment()).clientName)
assertProgramRuleVariableAssignment(ruleEffects, rule, getEnvironment().clientName)
}

@Test
Expand Down
6 changes: 4 additions & 2 deletions src/jsMain/kotlin/org/hisp/dhis/rules/Environment.js.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.hisp.dhis.rules

actual fun getEnvironment(): String {
return "WEBCLIENT"
import org.hisp.dhis.rules.models.TriggerEnvironment

actual fun getEnvironment(): TriggerEnvironment {
return TriggerEnvironment.WEBCLIENT
}
2 changes: 1 addition & 1 deletion src/jsMain/kotlin/org/hisp/dhis/rules/RuleEnrollmentJs.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.hisp.dhis.rules

import org.hisp.dhis.rules.models.RuleEnrollmentStatus
import org.hisp.dhis.rules.models.RuleAttributeValue
import org.hisp.dhis.rules.models.RuleEnrollmentStatus

@JsExport
data class RuleEnrollmentJs(
Expand Down
6 changes: 4 additions & 2 deletions src/jvmMain/kotlin/org/hisp/dhis/rules/Environment.jvm.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.hisp.dhis.rules

actual fun getEnvironment(): String {
return "SERVER"
import org.hisp.dhis.rules.models.TriggerEnvironment

actual fun getEnvironment(): TriggerEnvironment {
return TriggerEnvironment.SERVER
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.hisp.dhis.rules

actual fun getEnvironment(): String {
return "ANDROIDCLIENT"
import org.hisp.dhis.rules.models.TriggerEnvironment

actual fun getEnvironment(): TriggerEnvironment {
return TriggerEnvironment.NATIVE
}

0 comments on commit cb47f8c

Please sign in to comment.