A general model for Relationships
In software design, we are frequently called upon to map relationships between different objects and concepts. This is also true in knowledge architecture and relational database design.
However, due to the constraints of current object-oriented languages that I have worked with, I have found the task of implementing a general and abstract model for relationships difficult and perhaps even impossible. Barring bytecode engineering, I'm not certain that I will be able to implement the following Relationship Archetype Pattern in Java:
Relationships can be understood along two different axes. These are symmetry and uniformity.
Example:
| symmetric | asymmetric | |
| uniform | Person A and Person B are sisters |
Person A is Person B's older sister |
| non-uniform | Person Timmy and Dog Lassie are passengers |
Person Timmy is Dog Lassie's owner |
A symmetric relationship is one between two relators that share the same role, while an Assymetric Relationship is one between two relators of different roles. A uniform relationship is one between two relators of the same type, while a non-uniform relationship is one between two relators of different types.
In Java, we can very nearly abstract the concept of type using generics, but we run into problems because unlike types in the strict sense, generic markers are not polymorphic. It is also not the case that we can shadow class signatures, to allow for something along the lines of the following:
public class Relationship <T,U>{
...
}
where T could be used for symmetric relationships in the following manner:
public Relationship<String> rel = new Relationship("a", "b");
and also where both T and U could be used in the case of asymmetric relationships, as follows:
public Relationship<String, Integer> rel2 = new Relationship("c", new Integer(1));
such that we could call the following method and get different types depending on how we constructed the class:
String resultA = rel.getSecondaryEndpoint(); Integer resultB = rel2.getSecondaryEndpoint();
This is because the two methods would have the same erasure and thus be indistinguishable from each other after compilation.
So, Java generics are not sufficient to permit us to implement in an elegant sense the abstract concept of relationship without resorting to completely separate class trees for symmetric and asymmetric relational patterns. The task of implementing uniform and non-uniform relationships suffers a similar breakdown due to a derivative of this same ambiguity regarding erasure.
Thus, at present, I have not been able to implement a general model for relationships in Java, and consider it a major conceptual hurdle in some of my work regarding commercial relational modeling. I'll investigate bytecode engineering routes, though I do not have high hopes for simplifying the API through this method.
