Skip to content

feat: add d2:log and d2:exponent to program rule grammar#101

Merged
enricocolasante merged 7 commits into
mainfrom
d2-pow
Jul 13, 2026
Merged

feat: add d2:log and d2:exponent to program rule grammar#101
enricocolasante merged 7 commits into
mainfrom
d2-pow

Conversation

@superskip

@superskip superskip commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Things to look at:

  • There already was a log function in Calculator.kt, and I just copy-pasted the implementation. A more clever solution might exist.
  • Kotlin uses the name pow for "raising a number to a power", and I think C++ uses the same name. Personally I associate this operation more with the name exp. The advocates for pow probably argue that exp is another function (e^x). Regardless, I tend to look up exp rather than pow if I want to "exponentiate" something, and chances are this is true for other people as well.

Update:

  • Combined the implementation of log and d2_log.
  • Changed name from pow to exponent (as suggested in the roadmap).

@superskip
superskip requested review from enricocolasante and jbee and removed request for jbee May 29, 2026 13:07
@superskip superskip changed the title feat: add d2:log and d2:pow to program rule grammar feat: add d2:log and d2:exponent to program rule grammar Jun 1, 2026
Comment thread src/commonTest/kotlin/org/hisp/dhis/lib/expression/function/ExponentTest.kt Outdated
NamedFunction.isNull -> functions.isNull(evalToMixed(fn.child(0)))
NamedFunction.least -> functions.least(evalToNumbers(fn.children()))
NamedFunction.log -> if (fn.size() == 1) functions.log(evalToNumber(fn.child(0)))
NamedFunction.log, NamedFunction.d2_log -> if (fn.size() == 1) functions.log(evalToNumber(fn.child(0)))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to add d2:log when log already exists?

@superskip superskip Jun 8, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is that all other program rule functions use the d2:-prefix.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this discussed to keep it consistent? Maybe @enricocolasante knows?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say so.
At the moment rule-engine is only allowing CommonD2Functions and RuleEngineD2Functions and all of them have the d2 prefix.
There wasn't a real discussion about it. We already have some generic functions about numeric operations like modulus or floor using the d2 prefix and as always this argument is the one used to continue doing this way.

I think at some point we should separate generic functions and custom DHIS2 functions, the question is: should we do it now?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not the best time to do another refactor of the language(s) on a more fundamental level so if enrico is happy with this PR I think we try to move it forward

superskip added 2 commits June 8, 2026 16:01
- Edit author
- Correction of the test title
- Adds a test case for exponent > 1 and exponent < 0
Also add missing case for negative exponent
@sonarqubecloud

sonarqubecloud Bot commented Jun 9, 2026

Copy link
Copy Markdown

@enricocolasante
enricocolasante merged commit f4c950c into main Jul 13, 2026
3 checks passed
@karolinelien

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants