1

I want to be able to draw a UML diagram of my code that utilizes IPv4 addresses. They are normally just a string but they must be created in a very specific way to be valid. Here is the regex for one:

\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\. (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\. (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\. (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

I am not exactly sure how to define the ipaddr attribute and functions that support checking it.

If I had a class that did something with an ipaddr, like create a socket I would have something like this...

--------------------------------------------
SocketCreator
--------------------------------------------
socket: Socket
ipaddr: str  # This is what needs to change
--------------------------------------------
create_connection(socket:Socket)
--------------------------------------------

Maybe having something like this?

------------------------------------------
IpAddrV4: String
------------------------------------------
string: String[0-3]=<0-255>+String[4-7]=<0-255>+String[8-11]=<0-255>+String[12-15]=<0-255>
Interlooper
  • 494
  • 2
  • 5
  • 14

1 Answers1

2

If you’re just interested in UML syntax

If you want to express some conditions, restrictions or invariants in UML, you may use an UML constraint. The constraint is then expressed between curly braces { ... } either in natural language or in OCL.

You could use an attribute constraint, just next to the attribue:

------------------------------------------
IpAddrV4
------------------------------------------
Address: String { matching regex '...' }  

Or you could use a more general class constraint, writing your constraint in a comment-box connected to the relevant class with a dashed line:

{ inv: Address matching regex ... }  

As far as I know, OCL specifications do not provide native support for regex. But some dialects (e.g. Eclipse) do, according to this SO answer.

If you want to get more from your model

What's the hidden problem in your modelling approach ?

Your question focuses on a possible implementation of the IPv4 address. You presume it's a string that needs to comply with a certain constraint, and your model leaks this assumption.

But are you really interested only in a specific representation of an IPv4 address as a string ? Or are you interested in what the address really is and what you can do with it ? And in the latter case, what are the behaviors of this class ?

I fear that with your current approach, you risk to end up with an UML model that just transcribes your current code into a graphical presentation. You then might not enjoy the full benefits that a real model could provide, such as abstraction, encapsulation, and separation of concerns.

What's an IPv4 address

Going back to the roots of RFC 791 (section 2.3) it appears that an IP address:

  • is made of four octets, so 32 bits (and this 32 bit representation is used in the low-level functions) ;
  • can be viewed as a network address followed by a local address;
  • has an "address class" that governs the layout of the network and local address.

It appears in RFC 1180 (section 2) that when a user input provides an IP address, it should use the doted decimal numbering format.

The POSIX standard proposes some standard conversion between doted decimal string and internet address.

The UML class

I'd propose something like, to which you need to add the above-mentionned constraint on the string format:

----------------------------------------------
IpAddrV4
----------------------------------------------
-address: Integer     //UInt32 in fact
----------------------------------------------
+IPAddrV4() 
+IPAddrV4(Address: String)
+GetAddress() : String
+GetBinaryAddress(): Integer //UInt32 in fact
+GetAddressClass(): String   //Char in reality
----------------------------------------------

Final word

Sorry if this might sound rude. But designing in 2019 an IPv4 application sounds weird and incomplete. The advantage with abstraction, is that it could allow you to design a more general address that would be able to cope as well with IPv6 formats and inner structure.

Community
  • 1
  • 1
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • I've seen better answers from you. Asking questions in an answer does not seem to be appropriate. Why not clarify that first via comments? – qwerty_so Apr 13 '19 at 15:18
  • @ThomasKilian First of all, thank you for the compliment ;-) Now reread carefully my question and you’ll find out that their intent is only to suggest an issue in the modelling approach. Go to my conclusion, and you’ll find out why considering IP adresses as some kind of string matching a regex restricts future evolutions of the design. I understand that you disagree and downvote because I do not just provide an UML answer that shows to document a constraint. But I really want to address the design issue to help OP thinking about the bigger picture. – Christophe Apr 13 '19 at 15:53
  • Yes, I've read your answer. It's detailed as always. However, here I just think that the comment of muszeo is the real answer. Anyhow, keep on with you good ones for most of your answers :-) Also I think the OP will likely learn something from it and eventually compensate with an up-vote. – qwerty_so Apr 13 '19 at 17:21
  • @ThomasKilian Ok! Thanks! I've edited the answer to give the OP the choice between an answer limited to the UML topics he/she explicitly asked for, and the more general advice about the modelling issue. – Christophe Apr 13 '19 at 23:21
  • Just a typo in the heading left _your_ -> _you're_ ;-) – qwerty_so Apr 14 '19 at 05:32
  • Wow, I just want to thank you for giving such a detailed and complete answer to my question; I appreciate it greatly! Q: `But are you really interested only in a specific representation of an IPv4 address as a string ? Or are you interested in what the address really is and what you can do with it ? And in the latter case, what are the behaviors of this class ?` A: I was thinking of a simple implementation. The strings should just match the regex. However it might be interesting to implement a full class Q: `But designing in 2019 an IPv4 application sounds weird and incomplete.` – Interlooper Apr 15 '19 at 16:59
  • Q: `But are you really interested only in a specific representation of an IPv4 address as a string ? Or are you interested in what the address really is and what you can do with it ? And in the latter case, what are the behaviors of this class ?` A: I was thinking of a simple implementation. The strings should just match the regex. It might be interesting to implement a full class Q: `But designing in 2019 an IPv4 application sounds weird...` A: It was more as an example however I would use something like the design you put up, I never heard about attribute constraints before! – Interlooper Apr 15 '19 at 17:07
  • 1
    @Interlooper Thanks for this feedback. I am glad that it was useful :-) – Christophe Apr 15 '19 at 17:21