Skip to content

Commit

Permalink
feat: Add Js API (#121)
Browse files Browse the repository at this point in the history
* feat: Add Js API

* Organize imports

* Fix code review comments
  • Loading branch information
enricocolasante authored Jan 18, 2024
1 parent 52bb9c6 commit 958ba19
Show file tree
Hide file tree
Showing 44 changed files with 433 additions and 174 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.hisp.dhis.rules.api

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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import org.hisp.dhis.lib.expression.Expression
import org.hisp.dhis.lib.expression.spi.ExpressionData
import org.hisp.dhis.lib.expression.spi.IllegalExpressionException
import org.hisp.dhis.rules.createLogger
import org.hisp.dhis.rules.models.Rule
import org.hisp.dhis.rules.models.*
import org.hisp.dhis.rules.engine.RuleEvaluationResult.Companion.errorRule
import org.hisp.dhis.rules.engine.RuleEvaluationResult.Companion.evaluatedResult
import org.hisp.dhis.rules.engine.RuleEvaluationResult.Companion.notEvaluatedResult
import org.hisp.dhis.rules.models.*
import org.hisp.dhis.rules.utils.unwrapVariableName

internal class RuleConditionEvaluator {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.hisp.dhis.rules.engine

import org.hisp.dhis.rules.models.Rule
import org.hisp.dhis.rules.models.*

internal class RuleEngineMultipleExecution {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import kotlinx.datetime.toLocalDateTime
import org.hisp.dhis.rules.models.*
import org.hisp.dhis.rules.utils.RuleEngineUtils
import org.hisp.dhis.rules.utils.currentDate
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

internal class RuleVariableValueMapBuilder private constructor() {
val allConstantValues: MutableMap<String, String> = HashMap()
Expand Down
8 changes: 5 additions & 3 deletions src/commonMain/kotlin/org/hisp/dhis/rules/models/Option.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.hisp.dhis.rules.models

/**
* @author rajazubair
*/
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport

@JsExport
@OptIn(ExperimentalJsExport::class)
data class Option(val name: String, val code: String)
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.hisp.dhis.rules.models

import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport

@JsExport
@OptIn(ExperimentalJsExport::class)
data class RuleAttributeValue(
val trackedEntityAttribute: String,
val value: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.hisp.dhis.rules.models

import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport
import kotlin.jvm.JvmOverloads

/*
Expand Down Expand Up @@ -30,6 +32,8 @@ import kotlin.jvm.JvmOverloads
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

@JsExport
@OptIn(ExperimentalJsExport::class)
data class RuleValidationResult @JvmOverloads constructor(
val valid: Boolean,
val errorMessage: String? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.hisp.dhis.rules.models

import org.hisp.dhis.rules.engine.RuleVariableValue
import org.hisp.dhis.rules.engine.RuleVariableValueMapBuilder

interface RuleVariable {
val name: String
val useCodeForOptionSet: Boolean
val options: List<Option>
val field: String
val fieldType: RuleValueType
fun createValues(
ruleEvent: RuleEvent?,
allEventValues: Map<String, List<RuleDataValue>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package org.hisp.dhis.rules.models

import kotlinx.datetime.LocalDate
import org.hisp.dhis.rules.engine.RuleVariableValue
import org.hisp.dhis.rules.engine.RuleVariableValueMapBuilder
import org.hisp.dhis.rules.utils.currentDate

data class RuleVariableAttribute(
val name: String,
val useCodeForOptionSet: Boolean,
override val name: String,
override val useCodeForOptionSet: Boolean,
override val options: List<Option>,
val trackedEntityAttribute: String,
val trackedEntityAttributeType: RuleValueType
override val field: String,
override val fieldType: RuleValueType
) : RuleVariable {
override fun createValues(
ruleEvent: RuleEvent?,
Expand All @@ -20,15 +19,15 @@ data class RuleVariableAttribute(
): Map<String, RuleVariableValue> {
val valueMap: MutableMap<String, RuleVariableValue> = HashMap()
val currentDate = LocalDate.Companion.currentDate()
val variableValue = if (currentEnrollmentValues.containsKey(trackedEntityAttribute)) {
val value = currentEnrollmentValues[trackedEntityAttribute]
val variableValue = if (currentEnrollmentValues.containsKey(field)) {
val value = currentEnrollmentValues[field]
val optionValue = if (useCodeForOptionSet) value!!.value else getOptionName(value!!.value)!!
RuleVariableValue(
trackedEntityAttributeType, optionValue,
fieldType, optionValue,
listOf(optionValue), currentDate.toString()
)
} else {
RuleVariableValue(trackedEntityAttributeType)
RuleVariableValue(fieldType)
}
valueMap[name] = variableValue
return valueMap
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.hisp.dhis.rules.models

import org.hisp.dhis.rules.engine.RuleVariableValue
import org.hisp.dhis.rules.engine.RuleVariableValueMapBuilder

/*
* Copyright (c) 2004-2018, University of Oslo
Expand Down Expand Up @@ -33,11 +32,11 @@ import org.hisp.dhis.rules.engine.RuleVariableValueMapBuilder
* @author Zubair Asghar.
*/
class RuleVariableCalculatedValue(
val name: String,
val useCodeForOptionSet: Boolean,
override val name: String,
override val useCodeForOptionSet: Boolean,
override val options: List<Option>,
val calculatedValueVariable: String,
val calculatedValueType: RuleValueType
override val field: String,
override val fieldType: RuleValueType
) : RuleVariable {
override fun createValues(
ruleEvent: RuleEvent?,
Expand All @@ -46,7 +45,7 @@ class RuleVariableCalculatedValue(
currentEventValues: Map<String, RuleDataValue>
): Map<String, RuleVariableValue> {
val valueMap: MutableMap<String, RuleVariableValue> = HashMap()
valueMap[name] = RuleVariableValue(calculatedValueType)
valueMap[name] = RuleVariableValue(fieldType)
return valueMap
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.hisp.dhis.rules.models

import org.hisp.dhis.rules.engine.RuleVariableValue
import org.hisp.dhis.rules.engine.RuleVariableValueMapBuilder
import org.hisp.dhis.rules.utils.getLastUpdateDate

class RuleVariableCurrentEvent(
val name: String,
val useCodeForOptionSet: Boolean,
override val name: String,
override val useCodeForOptionSet: Boolean,
override val options: List<Option>,
override val dataElement: String,
override val dataElementType: RuleValueType,
) : RuleVariableDataElement {
override val field: String,
override val fieldType: RuleValueType,
) : RuleVariable {
override fun createValues(
ruleEvent: RuleEvent?,
allEventValues: Map<String, List<RuleDataValue>>,
Expand All @@ -19,15 +18,15 @@ class RuleVariableCurrentEvent(
): Map<String, RuleVariableValue> {
val valueMap: MutableMap<String, RuleVariableValue> = HashMap()
val variableValue: RuleVariableValue
variableValue = if (currentEventValues.containsKey(dataElement)) {
val value = currentEventValues[dataElement]
variableValue = if (currentEventValues.containsKey(field)) {
val value = currentEventValues[field]
val optionValue = if (useCodeForOptionSet) value!!.value else getOptionName(value!!.value)!!
RuleVariableValue(
dataElementType, optionValue,
fieldType, optionValue,
listOf(optionValue), getLastUpdateDate(listOf(value))
)
} else {
RuleVariableValue(dataElementType)
RuleVariableValue(fieldType)
}
valueMap[name] = variableValue
return valueMap
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
package org.hisp.dhis.rules.models

import org.hisp.dhis.rules.engine.RuleVariableValue
import org.hisp.dhis.rules.engine.RuleVariableValueMapBuilder
import org.hisp.dhis.rules.utils.getLastUpdateDate
import org.hisp.dhis.rules.utils.values

class RuleVariableNewestEvent(
val name: String,
val useCodeForOptionSet: Boolean,
override val name: String,
override val useCodeForOptionSet: Boolean,
override val options: List<Option>,
override val dataElement: String,
override val dataElementType: RuleValueType,
) : RuleVariableDataElement {
override val field: String,
override val fieldType: RuleValueType,
) : RuleVariable {
override fun createValues(
ruleEvent: RuleEvent?,
allEventValues: Map<String, List<RuleDataValue>>,
currentEnrollmentValues: Map<String, RuleAttributeValue>,
currentEventValues: Map<String, RuleDataValue>
): Map<String, RuleVariableValue> {
val valueMap: MutableMap<String, RuleVariableValue> = HashMap()
val ruleDataValues = allEventValues[dataElement]
val ruleDataValues = allEventValues[field]
if (ruleDataValues.isNullOrEmpty()) {
valueMap[name] = RuleVariableValue(dataElementType)
valueMap[name] = RuleVariableValue(fieldType)
} else {
val variableValue: RuleVariableValue
val value = ruleDataValues[0]
val optionValue = if (useCodeForOptionSet) value.value else getOptionName(value.value)!!
variableValue = RuleVariableValue(
dataElementType, optionValue,
fieldType, optionValue,
values(ruleDataValues), getLastUpdateDate(ruleDataValues)
)
valueMap[name] = variableValue
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package org.hisp.dhis.rules.models

import org.hisp.dhis.rules.engine.RuleVariableValue
import org.hisp.dhis.rules.engine.RuleVariableValueMapBuilder
import org.hisp.dhis.rules.utils.getLastUpdateDate
import org.hisp.dhis.rules.utils.values

class RuleVariableNewestStageEvent(
val name: String,
val useCodeForOptionSet: Boolean,
override val name: String,
override val useCodeForOptionSet: Boolean,
override val options: List<Option>,
override val dataElement: String,
override val dataElementType: RuleValueType,
override val field: String,
override val fieldType: RuleValueType,
val programStage: String
) : RuleVariableDataElement {
) : RuleVariable {

override fun createValues(
ruleEvent: RuleEvent?,
Expand All @@ -22,7 +21,7 @@ class RuleVariableNewestStageEvent(
): Map<String, RuleVariableValue> {
val valueMap: MutableMap<String, RuleVariableValue> = HashMap()
val stageRuleDataValues: MutableList<RuleDataValue> = ArrayList()
val sourceRuleDataValues = allEventValues[dataElement]
val sourceRuleDataValues = allEventValues[field]
if (!sourceRuleDataValues.isNullOrEmpty()) {

// filter data values based on program stage
Expand All @@ -33,13 +32,13 @@ class RuleVariableNewestStageEvent(
}
}
if (stageRuleDataValues.isEmpty()) {
valueMap[name] = RuleVariableValue(dataElementType)
valueMap[name] = RuleVariableValue(fieldType)
} else {
val variableValue: RuleVariableValue
val value = stageRuleDataValues[0]
val optionValue = if (useCodeForOptionSet) value.value else getOptionName(value.value)!!
variableValue = RuleVariableValue(
dataElementType, optionValue,
fieldType, optionValue,
values(stageRuleDataValues),
getLastUpdateDate(stageRuleDataValues)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package org.hisp.dhis.rules.models

import org.hisp.dhis.rules.engine.RuleVariableValue
import org.hisp.dhis.rules.engine.RuleVariableValueMapBuilder
import org.hisp.dhis.rules.utils.getLastUpdateDateForPrevious
import org.hisp.dhis.rules.utils.values

class RuleVariablePreviousEvent(
val name: String,
val useCodeForOptionSet: Boolean,
override val name: String,
override val useCodeForOptionSet: Boolean,
override val options: List<Option>,
override val dataElement: String,
override val dataElementType: RuleValueType
) : RuleVariableDataElement {
override val field: String,
override val fieldType: RuleValueType
) : RuleVariable {
override fun createValues(
ruleEvent: RuleEvent?,
allEventValues: Map<String, List<RuleDataValue>>,
Expand All @@ -20,7 +19,7 @@ class RuleVariablePreviousEvent(
): Map<String, RuleVariableValue> {
val valueMap: MutableMap<String, RuleVariableValue> = HashMap()
var variableValue: RuleVariableValue? = null
val ruleDataValues = allEventValues[dataElement]
val ruleDataValues = allEventValues[field]
if (ruleEvent != null && !ruleDataValues.isNullOrEmpty()) {
for (ruleDataValue in ruleDataValues) {
// We found preceding value to the current currentEventValues,
Expand All @@ -29,7 +28,7 @@ class RuleVariablePreviousEvent(
val optionValue =
if (useCodeForOptionSet) ruleDataValue.value else getOptionName(ruleDataValue.value)!!
variableValue = RuleVariableValue(
dataElementType, optionValue,
fieldType, optionValue,
values(ruleDataValues),
getLastUpdateDateForPrevious(ruleDataValues, ruleEvent)
)
Expand All @@ -38,7 +37,7 @@ class RuleVariablePreviousEvent(
}
}
if (variableValue == null) {
variableValue = RuleVariableValue(dataElementType)
variableValue = RuleVariableValue(fieldType)
}
valueMap[name] = variableValue
return valueMap
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.hisp.dhis.rules.utils

import org.hisp.dhis.rules.api.ItemValueType

/*
* Copyright (c) 2004-2020, University of Oslo
* All rights reserved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package org.hisp.dhis.rules
import kotlinx.datetime.Clock
import kotlinx.datetime.LocalDate
import org.hisp.dhis.rules.api.RuleEngine
import org.hisp.dhis.rules.models.Rule
import org.hisp.dhis.rules.api.RuleEngineContext
import org.hisp.dhis.rules.engine.DefaultRuleEngine
import org.hisp.dhis.rules.models.*
import org.hisp.dhis.rules.utils.currentDate
import kotlin.test.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package org.hisp.dhis.rules
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate
import org.hisp.dhis.rules.api.RuleEngine
import org.hisp.dhis.rules.models.Rule
import org.hisp.dhis.rules.api.RuleEngineContext
import org.hisp.dhis.rules.engine.DefaultRuleEngine
import org.hisp.dhis.rules.models.*
import org.hisp.dhis.rules.utils.currentDate
import kotlin.test.Test
Expand Down
Loading

0 comments on commit 958ba19

Please sign in to comment.