This is a math problem with software limitations.
I am forced to work with a very inflexible tool and must check 5 fields, each of which can be one of 4 values (or left unset), to ensure that 1 and only 1 field is set with any of the 4 values. I cannot directly compare two fields, nor can I compare a field to null/0/unset. The interface is a horrible web based page, that only allows extremely simple logic to be input, via drop down menus, line by line.
The only pseudo logic I can use is as follows:
if <field> <equals/not equals> <val> [<and/or>]
if <field> <equals/not equals> <val> [<and/or>]
...
Where:
<field> is limited to field1, field2, ..., field5
<val> is limited to val1, val2, val3, val4
[<and/or>] is optional and would lead in to the following line
Each single line of "if" statement is one entry. I know very little about the backend, but what I have gathered from testing is that "and" seems to be a higher precedence than "or". There is also an arbitrary limit (that I have no idea what it is) on the number of lines, so the fewer lines the better.
This "ruleset" is basically one giant "if" statement that will be run through, and eventually return true or false. The actions of true or false are determined outside of this ruleset, so the only thing I need to focus on here is the minimum number of steps to ensure that 1 and only 1 field has a value set, and a way to calculate how many entries are needed for that single ruleset given X fields with Y values. My goal is to calculate all of this out, so I can mathematically prove that doing this is a waste of time and the backend needs to be fixed. I have to make dozens of rulesets like this, all with different numbers of fields and values.
It seems to me that the only way to use this logic would be to check:
if field1 = val1 and
if field2 = val1 or
if field1 = val1 and
if field2 = val2 or
...
if field1 = val1 and
if field2 = val4 or
if field1 = val2 and
if field2 = val1 or
...
This is walking across all of the fields and values to to check if any two pairs of fields both have some value set. When this whole ruleset returns true, it means that more than one field had a value set. I believe the equation that models this is as follows:
((4*4*4)+(3*4*4)+(2*4*4)+(1*4*4))*2 = 320
This is walking each pair of fields without repeating since order does not matter, (1&2, 1&3, 1&4, 1&5, 2&3, 2&4, 2&5, 3&4, 3&5, 4&5), and comparing one value against the other 4, 4 times per pair of fields (one for each possible value). The *2 at the end is because this math is assuming that comparing one field value to another is a single step, while in reality that requires two steps to do.
I do not believe there is any faster way to walk through all of the values, and I believe my math is correct. If either of these or wrong, or can be optimized, please let me know.
As brought up in the comments, this could be scripted, yes. However, there are a vast number of things wrong with this setup as a whole and I would rather objectively show that it needs to be fixed, rather than working around it.
if
test, but nothing specifying what you are allowed to do after the test passes. Do you need to do this with a single bigif
test, or can you nest them?