Program rule, assign value by summing boolean data elements

Hi @Natalie_Tibbels.

I have created a quick demo on the https://play.dhis2.org/2.34.0/ but I guess it will be deleted tonight; so I am adding a metadata package (remove the .pdf extension as it is a .zip)metadata.json.zip.pdf (3.3 KB) that you could import in the play server.

To be honest I was a bit more difficult that I initially thought because of the differences between the PR in Android and web.

The result in web|android is (what I think you wanted); note that I have added a “3” by default so it can be appreciated the changes.
vokoscreen-2020-05-09_10-55-40
vokoscreen-2020-05-09_10-59-37

You will need several Program Rules variable, in the form:
2 * (number of boolean fields)
Because you will need two program rules variables to assign either 0 or 1 according to the boolean state:
image

Then you will need the same amount of program rules pluse one, in the form:
2 * (number of boolean fields) + 1
image

Basically you are having two program rules per boolean to assign the number 0 or 1 so calculations can be made later:

PR1 condtion: !d2:hasValue(‘prv_boolean_one’) || !#{prv_boolean_one}
PR1 action: Assign value: “0” to field “#{prv_bool_one_to_number}”

PR2 condition: d2:hasValue(‘prv_boolean_one’) && #{prv_boolean_one}
PR2 action: Assign value: “1” to field “#{prv_bool_one_to_number}”

(repeat these two PR for every boolean)

Program rule to calculate (this is a bit tricky as we want it to be evaluated always so condition is true):

PR1 condtion: true
PR1 action: Assign value: “#{prv_bool_one_to_number} + #{prv_bool_two_to_number} + 3” to field “jb_test_boolean_calculation”

Please note that the program rules require to have priorities. The important thing is that the calculation is executed after the other ones. In the example I used Priority 1 for the assignations and 5 for the calculation.

Hope it helps.

2 Likes

This is amazing, thank you!

Hi again! Thanks so much for your help. I’ve made this whole thing work almost exactly as you said, except that I’ve been asked to include a couple of non-boolean data elements as well. Everything works except that the last program rule is adding integers together, not adding like a sum. So instead of 3+1+1 (where 3, 1, and 1 are calculated program rules based on answers in the form) = 5, it’s coming out to be 311. Any quick thoughts on what I might be doing wrong?

Also, I figured out the other value types, but not how to populate based on an option set. Thanks again!

This is probably because they are being treated like a string instead of a number. Is it happening only in web? Android? Both?

This is somehow known in Android (Home - DHIS2 Documentation).

Is there any chance you could provide access to the program with the program rules?

Just a remark, this is probably going to not be a problem soon when Android and Web share the same rule engine.

Best.

Thanks again! When will android and web share the same rule engine?

The program rules work just fine on the browser, but on android, every single one says “There is a configuration issue causing a loop in the rules.” If I click “accept,” The boolean calculation will work, but the option set or numeric rules will not.

For example, I have data elements for age (numeric) and sex (option set - male/female). I want to assign the value 3 for men 35 and older.

  1. I created the PRV ‘age’ (data element in current event) and PRV ‘sex’.
  2. Created a calculated PRV, ‘male_35plus’,
  3. Created a program rule to assign value, condition #{age} >=35 &&#{sex} ==‘01’ , with action assign value 3 to PRV ‘male_35plus’.
  4. male_35plus is then added to scores from other questions

Another example, I want to assign the value 3 if a person’s occupation is “seasonal worker” or “uniform” (option set code ‘sw’ or ‘un’), and 0 for all other options.

  1. Created PRV ‘occupation’ (DE in current event)
  2. Created PRV ‘occupation_calculated’ (calculated value)
  3. Created PR to assign value to occupation_calculated, expression=#{occupation} ==‘sw’, action= assign value: “3” to field #{occupation_calculated}.
  4. Created second PR for the other option code ‘un’

Those are all priority 1, and then the process of assigning sums of calculated value to an aggregate score is priority 5.

Let me know if you see anything wrong with this process for the way android evaluates! Or it may be part of what you the issues in the doc you posted (thank you).

Hello, am trying to do the same thing with a boolean field such that when i click Yes, it assigns value to another data element, but the assignment never happens. My program rule expression is :

The rule expression works if I display key:value pair but the same rule does not assign to data element

Hi @abdimohammed, just to understand properly the issue. Is this working in web but not in Android?

Hi? It occurs on both android and Web.

@jaime.bosque Can I give you credentials for my test server you try it out?? It would mean a lot if I can fix the problem.
Thanks

Thank you @jaime.bosque - for right now I think it seems to be working, apparently the problem was that I had value type text for the option set instead of number, which caused the score to concatenate. Field testing is ongoing and will keep you posted.

@abdimohammed Do you have both program rules, to assign 0 or 1 based on yes or no? I’m not sure it will help, but I was having trouble if only assigning for yes.

Hello all, the issue was that I was not generating resource table after making my changes.
Solution: Generate resource table

1 Like

Hello @jaime.bosque,
How Does the updates affect this part?? Do we need to change anything here?

Hi @abdimohammed. Not sure what you are referring to.

Hi,
Am refering to this answer here. I had set my program rules according to this answer. But the rules are not working after updating

error … This is an example you had given earlier. Does the update affect the rules above?

All rules configured as above works perfect in dhis2 web but the same rules not working on dhis2 android

Hi @abdimohammed.

Can you review this: Home - DHIS2 Documentation in case you are using the new version 2.2? Some changes were included in the RuleEngine and you might be affected by it.

Thank you @jaime.bosque , I was just looking up this post to figure out some new issues with program rules in the app. Will review that link and see if we can resolve them.

Do you know when the android and web will share the same rule engine?

My main issue is with the assign function, but I’m not assigning text, I’m assigning a numeric value that I then want to sum, not concatenate.

Rule 1 (priority 1)
Condition: #{occupation} ==5
Action: Assign value 3 to #{occupation_calculated}

Rule 2 (priority 1)
Condition: #{marital}==1
Action: Assign value 2 to #{marital_calculated}

Rule 3 (priority 2)
Condition: true
Action: Assign value #{occupation_calculated} + #{marital_calculated} to field “Final score”

In theory, if the user selects the option code 5 for occupation, it will assign a value of 3 to the calculated program variable, and then selects the option code 1 for marital status, it will assign a value of 2 to the calculated PV for marital status, and then the final score should be 5. This works fine on the web, but on the app (I think since 2.33.3), there’s no value. If you have a moment, let me know if you see anything off, thanks!