Search code examples
regexipv4oclecore

OCLinEcore and Regex to create valid IPv4 string


I have an Ecore model, where I'm trying to add some OCL constraints.

My problem comes when I try to make a property (a string) that is in valid IPv4 form. I guess I should use a regular expression, but I have no idea at all about how to use regex on OCLinEcore.


Solution

  • As of Eclipse Juno, you can use regular expressions in OCL. The function to call on it is matches(regex), just like in Java.

    context Packet
    inv ValidIPv4 : ip.matches('\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\b')
    

    Source: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.ocl.doc%2Fhelp%2FCompleteOCLTutorial.html

    If you have a previous version of Eclipse, then I suggest adding a Java constraint on your Ecore element, then validate your element through the generated Java template code.