Checking from attribute constraint if another attribute is set?

Hi all,

I'm playing around with MPS, and I have created my own ISO 8601 concept. I'm writing property constraints for every part, and I've got a bit of code to validate that the day is a valid one for the chosen year and month:

int daysInMonth = YearMonth.of(node.year, node.month).lengthOfMonth(); 
propertyValue >= 1 && propertyValue <= daysInMonth;

That said, I've noticed that if I unset the year/month, the old values are still "there" for the purposes of validation. I'd like to make the code just use the generic 1-31 day range if the year or the month are not set. However, I haven't found any way to check if node.year and/or node.month are set from the code.

Am I missing something? How can you check if a certain attribute is set or not?


Comment actions Permalink

Hello Antonio,

Integer properties can only hold numeric value. There is no special value for empty integer properties. When you delete the number for year, for example, the new "empty" propertyValue for "year" is invalid and so the previous value is preserved in the model to avoid having a broken model. The editor will, by default, show a "<no year>" message in red.

You can change the behavior of the editor cell for the "year" property and set its "allow empty" property to true. This will change the behavior of the editor so that instead of rejecting deletion of the number in the "year" property will set it to "0".

Your validator for "day" may then check for "year" (and month, respectively) being "0".

If having "0" as the visible value for "year" or "month" is not desired, you can either hide it with an "alternation" cell or use a string or a custom type for the "year" and "month" properties, in which case you'll need to do the parsing of integers yourself in their respective validators.




Comment actions Permalink

Hello Vaclav,

Ah, I see - it's pretty much like a Java "int" variable in that sense. Thanks, that clarifies things: from looking at the docs, I wasn't sure which primitive attributes could be left in an unset state or not.



Please sign in to leave a comment.