Skip to content

Commit

Permalink
Merge pull request #134 from dhis2/env_expected_function
Browse files Browse the repository at this point in the history
fix: Get environment from expected function
  • Loading branch information
enricocolasante authored May 13, 2024
2 parents 84a66c0 + 591ed11 commit 24a80bf
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/commonMain/kotlin/org/hisp/dhis/rules/Environment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.hisp.dhis.rules

expect fun getEnvironment(): String
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import org.hisp.dhis.lib.expression.spi.ValueType
import org.hisp.dhis.rules.api.DataItem
import org.hisp.dhis.rules.api.RuleEngine
import org.hisp.dhis.rules.api.RuleEngineContext
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> {
val valueMap = RuleVariableValueMapBuilder.target(target)
.ruleVariables(executionContext.ruleVariables)
.ruleEnrollment(ruleEnrollment)
.triggerEnvironment(TriggerEnvironment.SERVER)
.triggerEnvironment(TriggerEnvironment.valueOf(getEnvironment()))
.ruleEvents(ruleEvents)
.constantValueMap(executionContext.constantsValues)
.build()
Expand All @@ -28,7 +29,7 @@ internal class DefaultRuleEngine: RuleEngine {
override fun evaluate(target: RuleEnrollment, ruleEvents: List<RuleEvent>, executionContext: RuleEngineContext): List<RuleEffect> {
val valueMap = RuleVariableValueMapBuilder.target(target)
.ruleVariables(executionContext.ruleVariables)
.triggerEnvironment(TriggerEnvironment.SERVER)
.triggerEnvironment(TriggerEnvironment.valueOf(getEnvironment()))
.ruleEvents(ruleEvents)
.constantValueMap(executionContext.constantsValues)
.build()
Expand All @@ -42,7 +43,7 @@ internal class DefaultRuleEngine: RuleEngine {
val valueMap = RuleVariableValueMapBuilder.target()
.ruleVariables(executionContext.ruleVariables)
.ruleEnrollment(enrollmentTarget)
.triggerEnvironment(TriggerEnvironment.SERVER)
.triggerEnvironment(TriggerEnvironment.valueOf(getEnvironment()))
.ruleEvents(eventsTarget)
.constantValueMap(executionContext.constantsValues)
.multipleBuild()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package org.hisp.dhis.rules.models

import kotlin.js.JsExport

/*
* Copyright (c) 2004-2018, University of Oslo
* All rights reserved.
Expand Down Expand Up @@ -30,8 +33,10 @@ package org.hisp.dhis.rules.models
/**
* @author Zubair Asghar.
*/
@JsExport
enum class TriggerEnvironment(val clientName: String) {
ANDROIDCLIENT("AndroidClient"),
SERVER("Server")
SERVER("Server"),
WEBCLIENT("WebClient")

}
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, "Server")
assertProgramRuleVariableAssignment(ruleEffects, rule, TriggerEnvironment.valueOf(getEnvironment()).clientName)
}

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

actual fun getEnvironment(): String {
return "WEBCLIENT"
}
5 changes: 5 additions & 0 deletions src/jvmMain/kotlin/org/hisp/dhis/rules/Environment.jvm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.hisp.dhis.rules

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

actual fun getEnvironment(): String {
return "ANDROIDCLIENT"
}

0 comments on commit 24a80bf

Please sign in to comment.