By Peter Bell

Is a Relationship Just a Special Case of an Attribute?

The interesting thing about coming down from meta-grammars is that you see that any context free grammar can be described fully in terms of just concepts and attributes of those concepts (where the attributes may be of a primitive or custom data type or may relate to another concept). So, as you start to look at the traditional OO modeling trifecta of Object, Attribute and Relationship, the question is whether we need a separate concept called relationships or if we should just model them as a special type of attribute.

Lets look at the properties that define an attribute as opposed to a relationship to see how well they combine:

Attribute ::= Name [ShortDescription] DataType Required
Relationship ::= RelatedObjectName Required Multiple Composed

The RelatedObjectName really maps to the data type of attribute. Relationships typically don’t have a short description (which is fine as it is optional for attributes). They both have “Required”, and attributes could usefully have a Multiple for some use cases to distinguish between single and multiple selects within value lists. Attributes are by definition composed (delete an object and it’ll delete its attribute values) and you could add a Name to a relationship – perhaps driven by descriptive purposes, so a User might have a WorksFor attribute with a data type of Company. In short, there are enough similarities between the initially proposed concept syntaxes that it would be possible to combine them. Interesting . . .

The next question is how you would distinguish attributes from relationships for the operations that might only be performed on one or the other. If you had a list of object names, if there was never a custom data type with a name identical to that of an object, you could figure out fairly easily whether an attribute was pointing to an object or not, but I’m not sure that would be a good limitation, so I’d probably want an Object Boolean to distinguish relationships from attributes.

So, the data is similar enough that we could merge it, but we’re going to use a Boolean to distinguish the two types of attributes (looking at this from an object modeling perspective you might decide that conceptually PrimativeAttribute and ObjectAttribute both subclass an abstract Attribute class). The question then is whether there would be any benefits at all in going down from three concepts to two. How many operations would be perform on both attributes and relationships? Initially I can’t really think of many (the generation of getters and setters for attributes irrespective of data type is the one thing that comes to mind).

Another way of looking at this would be to distinguish between composed objects that from an OO perspective are really just attributes with a complex data type and associated objects that are something “outside” of the object they are associated to. However, while conceptually this is an interesting approach, the code you write to pull related object data is actually pretty much the same whether the object is associated or composed (the only difference is cascading deletes for composed objects) so it isn’t really as useful as other distinctions.

You could square the circle conceptually by imagining composed relationships to have a data type of the composed object(s) and associated relationships to have a data type of a pointer to the associated object(s) (we’re talking conceptual model here – the way most of the code base is implemented it won’t treat composed and associate objects any differently from each other except is a small set of special cases). However, I’m not sure that we get any benefits by doing so.

In the end, while it is interesting to consider the idea of treating relationships as a type of attribute, I don’t yet see any compelling benefits so I’m going to go with the standard convention in ODL of having Objects, Attributes and Relationships and I’ll just keep my mind open to refactoring the grammar if it makes sense down the line.

Anyone else got any thoughts on this?!

Comments
I have been thinking about the exact same thing. But perhaps not so rigourously.

In my current code generator I generate code that allows you to include up to two attributes from a parent object in a child object (hasOne relationship). I was lokking to extend that and was considering a sytax of object.attribute for the attributes of a linked object.

I would like to have some way to include data from child objects too and was considering structures or lists as a way to implement that.

Will look for you tomorrow to discuss it.
# Posted By Kevin Roche | 1/30/07 10:45 AM
Hi Kevin, Sounds like that might tie into some ORM thinking I've been doing about providing access child object data without running into n+1 query problems by allowing child object attributes (or aggregate functions of their attributes if it is a has_many relationship) could be returned by automatically generating subqueries in the select statement. Looking forward to chatting about all of this tomorrow. Have a great flight!
# Posted By Peter Bell | 1/30/07 10:53 AM
"the question is whether we need a separate concept called relationships or if we should just model them as a special type of attribute."

As I recall, the ER model for databases has this concept. But I could be wrong. In effect though, that's how I think of them. Not sure if that's worth much though.
# Posted By Sam | 1/30/07 12:14 PM
Both ER models for DBs and UML class diagrams have the concept of relationships (although they are different as db relationships are single directional whereas db relationships are inherently bidirectional) but that doesn't mean we should use them - it just means *they* use them :->
# Posted By Peter Bell | 1/30/07 12:22 PM
Hey Sam, Between college and your job I'm pretty impressed you're keeping up with all these postings - must be a heavier reading schedule= than you get from most lecturers :->
# Posted By Peter Bell | 1/30/07 12:23 PM
You mentioned "would there be any benefit in modeling an object as having only attributes (where relationship = special type of attribute where data type = object)" in another post... I decided to post this comment here, because you also said you'd explore it in another post.

My immediate thought to that is that I don't see any value in it. I mean, I might think of it like that, but to model it like that, I don't like. I suppose it does hide more information, but at the same time, that information in my eyes, at least, is pretty important. This isn't a strongly held position though, so I could easily be swayed with some evidence.

When I was talking about ER models, I mean that you can model/view a relationship as a special type of attribute. I could be wrong, of course, but I thought there was a way to do so. You wouldn't keep the related object within it, but the relationship itself can have attributes. Ahh, maybe I just cleared it up for myself. Perhaps I didn't mean what I thought I meant the first time =)

RE: the school and work and your site: Yeah, I'm in the middle of writing a paper that's due in a couple of hours, and I needed a break (been at it since 6 this morning + several hours yesterday). Anyway, for my break, I thought, "let's see what Peter Bell has to say today."

It is a pretty heavy reading schedule you provide. I'm sure I'll slack off later in the semester when I've got more than a paper to write.

And work, it is slow today, since I got blocked by a technical problem that needs to be fixed before I can continue. The problem is, I have no idea how to fix it and no diagnostic information to go on, so I'm waiting on another site's tech support to see if they know anything.

Wow, can I make this comment any longer? =)

Yes. Back to the paper. This was the last post to read for today (that I know of). =)
# Posted By Sam | 1/30/07 12:41 PM
Hi Sam, Tend to agree with you but will let you know if I find a good reason to model relationship as type of attribute. Misunderstood you on ER diags. Now understand, didn't know that - will check it out some time thanks!

Good luck with the paper!
# Posted By Peter Bell | 1/30/07 1:30 PM
Thanks for the luck, but just to clear things up, I think I was confused on the ER thing. I meant that relationships could be modeled WITH attributes, but originally said that relationships could be modeled AS attributes. Now, for all I know, they can be modeled AS attributes, but to my current knowledge, that is not the case.
# Posted By Sam | 1/30/07 2:25 PM
Ahh, OK, thanks!
# Posted By Peter Bell | 1/30/07 3:11 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.