Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions api/rule-engine.api
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public abstract interface class org/hisp/dhis/rules/api/RuleEngine {
public abstract fun evaluate (Lorg/hisp/dhis/rules/models/RuleEvent;Lorg/hisp/dhis/rules/models/RuleEnrollment;Ljava/util/List;Lorg/hisp/dhis/rules/api/RuleEngineContext;)Ljava/util/List;
public abstract fun evaluateAll (Lorg/hisp/dhis/rules/models/RuleEnrollment;Ljava/util/List;Lorg/hisp/dhis/rules/api/RuleEngineContext;)Ljava/util/List;
public static fun getInstance ()Lorg/hisp/dhis/rules/api/RuleEngine;
public abstract fun order (Ljava/util/List;)Ljava/util/List;
public abstract fun validate (Ljava/lang/String;Ljava/util/Map;)Lorg/hisp/dhis/rules/models/RuleValidationResult;
public abstract fun validateDataFieldExpression (Ljava/lang/String;Ljava/util/Map;)Lorg/hisp/dhis/rules/models/RuleValidationResult;
}
Expand Down Expand Up @@ -227,16 +228,16 @@ public final class org/hisp/dhis/rules/models/RuleDataValue {
}

public final class org/hisp/dhis/rules/models/RuleDataValueHistory {
public fun <init> (Ljava/lang/String;Lkotlin/time/Instant;Lkotlin/time/Instant;Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Lorg/hisp/dhis/rules/models/RuleLocalDate;Lkotlin/time/Instant;Ljava/lang/String;)V
public final fun component1 ()Ljava/lang/String;
public final fun component2 ()Lkotlin/time/Instant;
public final fun component2 ()Lorg/hisp/dhis/rules/models/RuleLocalDate;
public final fun component3 ()Lkotlin/time/Instant;
public final fun component4 ()Ljava/lang/String;
public final fun copy (Ljava/lang/String;Lkotlin/time/Instant;Lkotlin/time/Instant;Ljava/lang/String;)Lorg/hisp/dhis/rules/models/RuleDataValueHistory;
public static synthetic fun copy$default (Lorg/hisp/dhis/rules/models/RuleDataValueHistory;Ljava/lang/String;Lkotlin/time/Instant;Lkotlin/time/Instant;Ljava/lang/String;ILjava/lang/Object;)Lorg/hisp/dhis/rules/models/RuleDataValueHistory;
public final fun copy (Ljava/lang/String;Lorg/hisp/dhis/rules/models/RuleLocalDate;Lkotlin/time/Instant;Ljava/lang/String;)Lorg/hisp/dhis/rules/models/RuleDataValueHistory;
public static synthetic fun copy$default (Lorg/hisp/dhis/rules/models/RuleDataValueHistory;Ljava/lang/String;Lorg/hisp/dhis/rules/models/RuleLocalDate;Lkotlin/time/Instant;Ljava/lang/String;ILjava/lang/Object;)Lorg/hisp/dhis/rules/models/RuleDataValueHistory;
public fun equals (Ljava/lang/Object;)Z
public final fun getCreatedDate ()Lkotlin/time/Instant;
public final fun getEventDate ()Lkotlin/time/Instant;
public final fun getEventDate ()Lorg/hisp/dhis/rules/models/RuleLocalDate;
public final fun getProgramStage ()Ljava/lang/String;
public final fun getValue ()Ljava/lang/String;
public fun hashCode ()I
Expand Down Expand Up @@ -314,28 +315,32 @@ public final class org/hisp/dhis/rules/models/RuleEnrollmentStatus : java/lang/E
public static fun values ()[Lorg/hisp/dhis/rules/models/RuleEnrollmentStatus;
}

public final class org/hisp/dhis/rules/models/RuleEvent {
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/hisp/dhis/rules/models/RuleEventStatus;Lorg/hisp/dhis/rules/models/RuleInstant;Lorg/hisp/dhis/rules/models/RuleInstant;Lorg/hisp/dhis/rules/models/RuleLocalDate;Lorg/hisp/dhis/rules/models/RuleLocalDate;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;)V
public final class org/hisp/dhis/rules/models/RuleEvent : java/lang/Comparable {
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/hisp/dhis/rules/models/RuleEventStatus;Lorg/hisp/dhis/rules/models/RuleLocalDate;Lorg/hisp/dhis/rules/models/RuleInstant;Lorg/hisp/dhis/rules/models/RuleInstant;Lorg/hisp/dhis/rules/models/RuleLocalDate;Lorg/hisp/dhis/rules/models/RuleLocalDate;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;)V
public synthetic fun compareTo (Ljava/lang/Object;)I
public fun compareTo (Lorg/hisp/dhis/rules/models/RuleEvent;)I
public final fun component1 ()Ljava/lang/String;
public final fun component10 ()Ljava/lang/String;
public final fun component11 ()Ljava/util/List;
public final fun component11 ()Ljava/lang/String;
public final fun component12 ()Ljava/util/List;
public final fun component2 ()Ljava/lang/String;
public final fun component3 ()Ljava/lang/String;
public final fun component4 ()Lorg/hisp/dhis/rules/models/RuleEventStatus;
public final fun component5 ()Lorg/hisp/dhis/rules/models/RuleInstant;
public final fun component5 ()Lorg/hisp/dhis/rules/models/RuleLocalDate;
public final fun component6 ()Lorg/hisp/dhis/rules/models/RuleInstant;
public final fun component7 ()Lorg/hisp/dhis/rules/models/RuleLocalDate;
public final fun component7 ()Lorg/hisp/dhis/rules/models/RuleInstant;
public final fun component8 ()Lorg/hisp/dhis/rules/models/RuleLocalDate;
public final fun component9 ()Ljava/lang/String;
public final fun copy (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/hisp/dhis/rules/models/RuleEventStatus;Lorg/hisp/dhis/rules/models/RuleInstant;Lorg/hisp/dhis/rules/models/RuleInstant;Lorg/hisp/dhis/rules/models/RuleLocalDate;Lorg/hisp/dhis/rules/models/RuleLocalDate;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;)Lorg/hisp/dhis/rules/models/RuleEvent;
public static synthetic fun copy$default (Lorg/hisp/dhis/rules/models/RuleEvent;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/hisp/dhis/rules/models/RuleEventStatus;Lorg/hisp/dhis/rules/models/RuleInstant;Lorg/hisp/dhis/rules/models/RuleInstant;Lorg/hisp/dhis/rules/models/RuleLocalDate;Lorg/hisp/dhis/rules/models/RuleLocalDate;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;ILjava/lang/Object;)Lorg/hisp/dhis/rules/models/RuleEvent;
public final fun component9 ()Lorg/hisp/dhis/rules/models/RuleLocalDate;
public final fun copy (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/hisp/dhis/rules/models/RuleEventStatus;Lorg/hisp/dhis/rules/models/RuleLocalDate;Lorg/hisp/dhis/rules/models/RuleInstant;Lorg/hisp/dhis/rules/models/RuleInstant;Lorg/hisp/dhis/rules/models/RuleLocalDate;Lorg/hisp/dhis/rules/models/RuleLocalDate;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;)Lorg/hisp/dhis/rules/models/RuleEvent;
public static synthetic fun copy$default (Lorg/hisp/dhis/rules/models/RuleEvent;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/hisp/dhis/rules/models/RuleEventStatus;Lorg/hisp/dhis/rules/models/RuleLocalDate;Lorg/hisp/dhis/rules/models/RuleInstant;Lorg/hisp/dhis/rules/models/RuleInstant;Lorg/hisp/dhis/rules/models/RuleLocalDate;Lorg/hisp/dhis/rules/models/RuleLocalDate;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;ILjava/lang/Object;)Lorg/hisp/dhis/rules/models/RuleEvent;
public fun equals (Ljava/lang/Object;)Z
public final fun getCompletedDate ()Lorg/hisp/dhis/rules/models/RuleLocalDate;
public final fun getCreatedAtClientDate ()Lorg/hisp/dhis/rules/models/RuleInstant;
public final fun getCreatedDate ()Lorg/hisp/dhis/rules/models/RuleInstant;
public final fun getDataValues ()Ljava/util/List;
public final fun getDueDate ()Lorg/hisp/dhis/rules/models/RuleLocalDate;
public final fun getEvent ()Ljava/lang/String;
public final fun getEventDate ()Lorg/hisp/dhis/rules/models/RuleInstant;
public final fun getEventDate ()Lorg/hisp/dhis/rules/models/RuleLocalDate;
public final fun getOrganisationUnit ()Ljava/lang/String;
public final fun getOrganisationUnitCode ()Ljava/lang/String;
public final fun getProgramStage ()Ljava/lang/String;
Expand Down Expand Up @@ -375,9 +380,11 @@ public final class org/hisp/dhis/rules/models/RuleInstant$Companion {
public final fun parse (Ljava/lang/String;)Lorg/hisp/dhis/rules/models/RuleInstant;
}

public final class org/hisp/dhis/rules/models/RuleLocalDate {
public final class org/hisp/dhis/rules/models/RuleLocalDate : java/lang/Comparable {
public static final field Companion Lorg/hisp/dhis/rules/models/RuleLocalDate$Companion;
public fun <init> (III)V
public synthetic fun compareTo (Ljava/lang/Object;)I
public fun compareTo (Lorg/hisp/dhis/rules/models/RuleLocalDate;)I
public final fun component1 ()I
public final fun component2 ()I
public final fun component3 ()I
Expand Down Expand Up @@ -542,6 +549,11 @@ public final class org/hisp/dhis/rules/models/TriggerEnvironment : java/lang/Enu
public static fun values ()[Lorg/hisp/dhis/rules/models/TriggerEnvironment;
}

public final class org/hisp/dhis/rules/utils/RuleEventUtilsKt {
public static final fun getPreviousDataValue (Ljava/util/List;Lorg/hisp/dhis/rules/models/RuleEvent;)Lorg/hisp/dhis/rules/models/RuleDataValueHistory;
public static final fun orderEvents (Ljava/util/Collection;)Ljava/util/List;
}

public final class org/hisp/dhis/rules/utils/UtilsKt {
public static final fun currentDate ()Lkotlinx/datetime/LocalDate;
public static final fun unwrapVariableName (Ljava/lang/String;)Ljava/lang/String;
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repositories {
maven { url = uri("https://central.sonatype.com/repository/maven-snapshots/") }
}

version = "3.5.0-SNAPSHOT"
version = "3.6.0-SNAPSHOT"
group = "org.hisp.dhis.rules"

if (project.hasProperty("removeSnapshotSuffix")) {
Expand Down
4 changes: 4 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 @@ -34,6 +34,10 @@ interface RuleEngine {
executionContext: RuleEngineContext,
): List<RuleEffect>

fun order(
ruleEvents: List<RuleEvent>,
): List<RuleEvent>

companion object {
@JvmStatic
fun getInstance(): RuleEngine = DefaultRuleEngine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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.models.*
import org.hisp.dhis.rules.utils.orderEvents

internal class DefaultRuleEngine : RuleEngine {
override fun evaluate(
Expand Down Expand Up @@ -110,4 +111,10 @@ internal class DefaultRuleEngine : RuleEngine {
errorMessage = ex.message,
)
}

override fun order(
ruleEvents: List<RuleEvent>,
): List<RuleEvent> {
return orderEvents(ruleEvents)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package org.hisp.dhis.rules.engine

import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import kotlinx.datetime.atStartOfDayIn
import org.hisp.dhis.rules.models.*
import org.hisp.dhis.rules.utils.RuleEngineUtils
import org.hisp.dhis.rules.utils.currentDate
import org.hisp.dhis.rules.utils.orderEvents
import kotlin.time.Instant


Expand Down Expand Up @@ -54,10 +55,7 @@ internal class RuleVariableValueMapBuilder {

private fun buildAllEventValues(ruleEvents: Set<RuleEvent>): Map<String, List<RuleDataValueHistory>> {
val allEventsValues: MutableMap<String, MutableList<RuleDataValueHistory>> = HashMap()
val events: MutableList<RuleEvent> = ArrayList(ruleEvents)

// sort list of events by eventDate and createdDate:
events.sortWith(compareBy<RuleEvent>({ it.eventDate.instant }, { it.createdDate.instant }).reversed())
val events: List<RuleEvent> = orderEvents(ruleEvents).reversed()

// aggregating values by data element uid
for (i in events.indices) {
Expand All @@ -72,7 +70,7 @@ internal class RuleVariableValueMapBuilder {

// append data value to the list
allEventsValues[ruleDataValue.dataElement]?.add(
RuleDataValueHistory(ruleDataValue.value, events[i].eventDate.instant, events[i].createdDate.instant, events[i].programStage),
RuleDataValueHistory(ruleDataValue.value, events[i].eventDate, events[i].resolvedCreatedDate, events[i].programStage),
)
}
}
Expand Down Expand Up @@ -136,11 +134,8 @@ internal class RuleVariableValueMapBuilder {
): Map<String, RuleVariableValue> {
val valueMap: MutableMap<String, RuleVariableValue> = HashMap()
val eventDate =
if (ruleEvent.eventDate.instant < Instant.DISTANT_FUTURE )
ruleEvent.eventDate.instant
.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.toString()
if (ruleEvent.eventDate.localDate.atStartOfDayIn(TimeZone.currentSystemDefault()) < Instant.DISTANT_FUTURE )
ruleEvent.eventDate.toString()
else null
valueMap[RuleEngineUtils.ENV_VAR_EVENT_DATE] =
RuleVariableValue(
Expand Down Expand Up @@ -177,10 +172,7 @@ internal class RuleVariableValueMapBuilder {
RuleValueType.TEXT,
ruleEvent.event,
listOf(ruleEvent.event),
ruleEvent.eventDate.instant
.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.toString(),
ruleEvent.eventDate.toString(),
)
val status = ruleEvent.status.toString()
valueMap[RuleEngineUtils.ENV_VAR_EVENT_STATUS] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlin.time.Instant

data class RuleDataValueHistory(
val value: String,
val eventDate: Instant,
val eventDate: RuleLocalDate,
val createdDate: Instant,
val programStage: String,
)
13 changes: 11 additions & 2 deletions src/commonMain/kotlin/org/hisp/dhis/rules/models/RuleEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ data class RuleEvent(
val programStage: String,
val programStageName: String,
val status: RuleEventStatus,
val eventDate: RuleInstant,
val eventDate: RuleLocalDate,
val createdDate: RuleInstant,
val createdAtClientDate: RuleInstant?,
val dueDate: RuleLocalDate?,
val completedDate: RuleLocalDate?,
val organisationUnit: String,
val organisationUnitCode: String?,
val dataValues: List<RuleDataValue>,
)
): Comparable<RuleEvent> {
internal val resolvedCreatedDate get() = (createdAtClientDate ?: createdDate).instant

override fun compareTo(other: RuleEvent): Int {
val dateComparison = this.eventDate.compareTo(other.eventDate)
if (dateComparison != 0) return dateComparison
return this.resolvedCreatedDate.compareTo(other.resolvedCreatedDate)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import kotlin.js.JsStatic
import kotlin.jvm.JvmStatic

@JsExport
data class RuleLocalDate(val year: Int, val month: Int, val day: Int) {
data class RuleLocalDate(val year: Int, val month: Int, val day: Int): Comparable<RuleLocalDate> {
internal val localDate: LocalDate = LocalDate(year, month, day);

override fun toString() = localDate.toString()

override fun compareTo(other: RuleLocalDate): Int {
return localDate.compareTo(other.localDate)
}

@OptIn(ExperimentalJsStatic::class)
companion object{
@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.hisp.dhis.rules.models

import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import org.hisp.dhis.rules.engine.RuleVariableValue


Expand All @@ -26,10 +24,7 @@ class RuleVariableCurrentEvent(
fieldType,
optionValue,
listOf(optionValue),
ruleEvent.eventDate.instant
.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.toString(),
ruleEvent.eventDate.toString(),
)
}?.firstOrNull() ?: RuleVariableValue(fieldType)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.hisp.dhis.rules.models

import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import org.hisp.dhis.rules.engine.RuleVariableValue

class RuleVariableNewestEvent(
Expand All @@ -26,10 +24,7 @@ class RuleVariableNewestEvent(
fieldType,
optionValue,
ruleDataValues.map { it.value },
value.eventDate
.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.toString(),
value.eventDate.toString(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.hisp.dhis.rules.models

import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import org.hisp.dhis.rules.engine.RuleVariableValue


Expand Down Expand Up @@ -29,10 +27,7 @@ class RuleVariableNewestStageEvent(
fieldType,
optionValue,
stageRuleDataValues.map { it.value },
value.eventDate
.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.toString(),
value.eventDate.toString(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.hisp.dhis.rules.models

import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import org.hisp.dhis.rules.engine.RuleVariableValue
import org.hisp.dhis.rules.utils.getPreviousDataValue


class RuleVariablePreviousEvent(
Expand All @@ -19,25 +18,18 @@ class RuleVariablePreviousEvent(
): RuleVariableValue {
val dataValues = allEventValues[field]
if (ruleEvent != null && !dataValues.isNullOrEmpty()) {
for (ruleDataValue in dataValues) {
if (ruleEvent.eventDate.instant > ruleDataValue.eventDate ||
(ruleEvent.eventDate.instant == ruleDataValue.eventDate && ruleEvent.createdDate.instant > ruleDataValue.createdDate)
) {
val optionValue =
if (useCodeForOptionSet) ruleDataValue.value else getOptionName(ruleDataValue.value)
return RuleVariableValue(
fieldType,
optionValue,
dataValues.map { it.value },
ruleDataValue.eventDate
.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.toString(),
)
}
val ruleDataValue = getPreviousDataValue(dataValues, ruleEvent)
if (ruleDataValue != null) {
val optionValue =
if (useCodeForOptionSet) ruleDataValue.value else getOptionName(ruleDataValue.value)
return RuleVariableValue(
fieldType,
optionValue,
dataValues.map { it.value },
ruleDataValue.eventDate.toString(),
)
}
}

return RuleVariableValue(fieldType)
}
}
Loading