C - Expert 2 - Queries within collections
This section introduces the syntax used message locations when collection element is used in a validation rule
Message location supports added logic, which is especially great when dealing with collections; we are able to give the error message only for the cases where invalid XML content was found and correction has to be made.
Usage in short is: when collection is used, query will have to be a negation of the validation rule. In other words, if a validation rule limits the values of elements, we want to give the query for every element which does not fill the validation rule.
We shall be doing the following here:
The rule we made in the previous phase should look like this:
self.Unstructured->forAll(q | q.matches("Ustrd1") or q.matches("Ustrd2"))
The message location where we want to tell about the invalid content given in XML should be everything which does not fill the OCL rule above, i.e. the query should be a negation of the above.
There are three steps involved here:
- Replacing forAll() with select()
- include everything in right-hand-side with operation not()
- Replacing or with and
Reasons for these changes are:
- In a query, we do not want to limit the content anymore; it is already limited with an OCL rule. Instead, we want to find content. Therefore select() is the method we want to use
- not() is an OCL operation which returns the reverse of the content within it. if true is returned otherwise, false will be returned. And vice versa
- in a rule we want to use or as both of the values are valid to be used. In a query, we know that validation rule has been triggered and there is at least one occurrence within XML which does not pass the rule. We want to point to every case which does not fill the rule. Every case which does not return true will contain something other than "Ustrd1" and "Ustrd2" Therefore operand "and" fills the purpose and operand or would lead to incorrect behaviour.
Doing the above will give us a guery:
self.Unstructured->select(q | not(q.matches("Ustrd1") and q.matches("Ustrd2")))