Are you talking to me? Sometimes you need to look through entity instances and relate them to each other in Oracle Intelligent Advisor. Sometimes, when working with relationships, the rule author needs to reference instances of the same entity by using a Self-Referential Relationship.. Let’s take a quick tour of how this kind of relationship works. The good news, is that although it looks a little unusual because it has the same source and target, for the most part you can handle Self-Referential Relationships just like any other relationship.
But to help you understand the kind of context that might need this sort of relationship, let’s work an example. First the scenario. Undoubtedly one of the most asked questions is something like this.Recall from previous articles in this series, that we are interested in a series of promotional messages that will accompany our customer receipt. The project looks like this so far.
Recall that in the previous articles we have inferred various messages using a Word inference table for example.
The new requirement is, that we want to take those promotional messages and create something like a coherent message. We would like the end of the receipt to say something this, and we will use Self-Referential Relationships to do it. Imagine if we just wanted to have a string at the end of the receipt “Thank you for shopping with us today : here is your coupon for next time : 20 % off Cat Food on Thursdays”.
Let’s not get ahead of ourselves though. We first need to experiment with self-referential relationships. Consider the following project and relationships.
So you can see above that the reference relationship is between instances of the child. So now we can add a rule to check the children. Let’s start with something that looks right, but is actually not going to work.
Now get ready for the big surprise…run the project and add ONE child.
It’s at this point that a lot of rule authors scratch their head and think they have done something wrong. They haven’t really – it’s just the way the Oracle Intelligent Advisor engine works. When using Self-Referential Relationships, there will come moments where it is not clear to the engine how to separate one instance from another.
And in this case. it has looked at Pauline, and then looked again at Pauline and said “TWIN!”. So how do we stop that happening? We will use a technique that is very useful and that will come up many times in our future learning journey posts. We can use an alias. An alias is simply a way to clarify to the engine what to do. And we will explicitly help the engine not refer to the person twice in the same breath as well:
Notice the alias in IsMemberOf() is really just a way to make it clear to Oracle Intelligent Advisor that this involves comparing instances of the same entity. Once you have declared the alias, you can use it in the ensuing conditions. If you start a new paragraph however the alias is lost – the alias is said to no longer be in scope.
Enter two children into your debugger with the same date of birth but different names. And this time you get what you expect – Paul is the twin of Pauline, in the case of the screenshot above.
In this learning journey we have covered two really important concepts – firstly, that reference relationships can be self-referential and secondly, that if you intend to compare instances, you will need an alias. Aliases will figure in later steps in this journey as well, so keep them in mind. Plus as a bonus we have seen IsMemberOf() in action again!
The video below walks through all the steps and discusses them as part of our learning journey.
So now that we have laid the groundwork for aliases and self-referential relationships, in the next step of our journey we will create our custom receipt.