B - Advanced 4 - Conditional rules

Purpose Purpose of this section is to introduce conditional rules, that is to have a rule where certain condition has to be met before a restriction is required. Usually, this is handled with operation "implies".


In the previous task (section 3, phase 1), we made Structured mandatory and nothing else. This rule could be thought to be a conditional rule as well, as the definition of this rule in normal language would have been: "When Remittance is given, Structured is mandatory".

However, in this section we shall introduce condition in a more clear format

  1. Creating a rule with implies (When Tx/Id = "id1", Dbtr has to be given mandatory)
  2. Compiling and testing


1: Creating a rule with implies(When Tx/Id = "id1", Dbtr has to be given mandatory)

In phase 1 of advanced, we made a rule mandating Tx/Id to either be "Id1" or "Id2". We can use this rule to our advantage here. 

We can start by making Dbtr to always be mandatory. In fact, we may already have this rule present. Next, we should look at the rule which limits the value of Id. We can copy the relevant section of this rule, which is: 


Next, we should open the rule which makes Dbtr to mandatory, it contains the code 

self.Debtor->size() >= 1

Now, we shall combine these two snippets of code:

self.Debtor->size() >= 1

The above is not yet valid. But by adding the operation "implies" in between of these two expressions we have done what we wanted the rule to do. 

self.Id.matches("Id1") implies
self.Debtor->size() >= 1


2: Compiling and testing

Testing this implementation without making any other changes than described in phase 1 will be giving a confusing error message. Therefore we should adjust the error message to match what the rule actually does. 

We should test at least two cases here: Id1 given without Dbtr and Id2 given with Dbtr. We can also change value of Id to be "Id2", which lets us see that in this case the rule is not triggering at all. 


ConclusionWhenever implies is used in the definition, we need to have at least two expressions. natural language version of expression could be sentence. This could be though as of Expression1 leads to Expression2, or whenever Expression1 happens, Expression2 must happen as well.

If the content before implies does not happen in the XML file, then content after implies does not need to happen. 

If both expressions should happen, we have to use operator "and". However, usually it is a better idea to make two separate validation rules in this case. It allows us to have a more accurate error message, as we can tailor the error message appropriately for each of the two expressions.