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
1 change: 1 addition & 0 deletions api/expression-parser.api
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ public final class org/hisp/dhis/lib/expression/Expression {
public synthetic fun <init> (Ljava/lang/String;Lorg/hisp/dhis/lib/expression/ExpressionMode;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun collectDataItemForRegenerate ()Ljava/util/Set;
public final fun collectDataItems ()Ljava/util/Set;
public final fun collectInOrgUnitGroups ()Ljava/util/Set;
public final fun collectProgramRuleVariableNames ()Ljava/util/Set;
public final fun collectProgramVariables ()Ljava/util/Set;
public final fun collectProgramVariablesNames ()Ljava/util/Set;
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repositories {
mavenCentral()
}

version = "1.4.0-SNAPSHOT"
version = "1.4.1-SNAPSHOT"
group = "org.hisp.dhis.lib.expression"

if (project.hasProperty("removeSnapshotSuffix")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.hisp.dhis.lib.expression

import org.hisp.dhis.lib.expression.ast.NamedFunction
import org.hisp.dhis.lib.expression.ast.Node
import org.hisp.dhis.lib.expression.ast.VariableType
import org.hisp.dhis.lib.expression.eval.Api.collectDataItems
import org.hisp.dhis.lib.expression.eval.Api.collectFunctionStringArguments
import org.hisp.dhis.lib.expression.eval.Api.collectUIDs
import org.hisp.dhis.lib.expression.eval.Api.collectVariableNames
import org.hisp.dhis.lib.expression.eval.Api.collectVariables
Expand Down Expand Up @@ -43,6 +45,10 @@ class Expression(
return collectVariableNames(root, VariableType.PROGRAM)
}

fun collectInOrgUnitGroups(): Set<String> {
return collectFunctionStringArguments(root, NamedFunction.d2_inOrgUnitGroup)
}

/**
* For testing only.
*
Expand Down
14 changes: 14 additions & 0 deletions src/commonMain/kotlin/org/hisp/dhis/lib/expression/eval/Api.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hisp.dhis.lib.expression.eval

import org.hisp.dhis.lib.expression.ast.NamedFunction
import org.hisp.dhis.lib.expression.ast.Node
import org.hisp.dhis.lib.expression.ast.NodeType
import org.hisp.dhis.lib.expression.ast.Nodes.VariableNode
Expand Down Expand Up @@ -110,4 +111,17 @@ Support functions to collect identifiers to supply values to main functions
}
) { node: Node<*> -> node.getType() === NodeType.DATA_ITEM }
}

internal fun collectFunctionStringArguments(root: Node<*>, function: NamedFunction, argIndex: Int = 0): Set<String> {
return root.aggregate(
LinkedHashSet(),
{ node: Node<*> ->
val argNode = node.childOrNull(argIndex)
if (node.getValue() === function && argNode != null && argNode.getType() === NodeType.ARGUMENT && argNode.size() > 0)
argNode.child(0).getRawValue()
else null
},
{ set: MutableSet<String>, e: String? -> set.add(e!!) }
) { node: Node<*> -> node.getType() === NodeType.FUNCTION }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ internal class HasValueTest : AbstractVariableBasedTest() {
assertProgramVariableNames(setOf("var"), "d2:hasValue(V{var})")
}

@Test
fun collectOrgUnitGroup() {
assertOrgUnitGroups(setOf("groupA"), "d2:inOrgUnitGroup(\"groupA\")")
assertOrgUnitGroups(setOf("CXw2yu5fodb"), "d2:inOrgUnitGroup(\"CXw2yu5fodb\")")
assertOrgUnitGroups(setOf("CXw2yu5fodb","gzcv65VyaGq"), "d2:inOrgUnitGroup(\"CXw2yu5fodb\") && d2:inOrgUnitGroup(\"gzcv65VyaGq\")")
assertOrgUnitGroups(setOf(), "d2:hasValue(A{var})")
}

private fun assertHasValue(expression: String, values: Map<String, VariableValue>, expected: Boolean) {
assertEquals(expected, evaluate(expression, values))
}
Expand All @@ -95,4 +103,9 @@ internal class HasValueTest : AbstractVariableBasedTest() {
assertEquals(expected, expr.collectProgramVariablesNames());
}

private fun assertOrgUnitGroups(expected: Set<String>, expression: String) {
val expr = Expression(expression, ExpressionMode.RULE_ENGINE_CONDITION)
assertEquals(expected, expr.collectInOrgUnitGroups());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class ExpressionJs(expression: String, mode: ExpressionMode) {
return expr.collectUIDs().toTypedArray()
}

fun collectInOrgUnitGroups(): Array<String> {
return expr.collectInOrgUnitGroups().toTypedArray()
}

fun describe(displayNames: JsMap<String, String>): String {
return expr.describe(toMap(displayNames, { it }, { it }))
}
Expand Down
Loading