At least, with this chapter in our ongoing learning journey (see the earlier chapters in the links below this article) there is no real risk of someone misunderstanding the concept we are going to discuss today. It is a case of creating clones or a Mirror-Image Instances of an existing entity instance in another entity. And the relative latecomer (August 2016 to be exact) InferInstanceFor() function is how to do it. To complete the process, if you use InferInstanceFor() in a rule then it is pretty much a certainty that you will use For() just after that. Why? Because although you create the mirror-image instances with the first function, the second one is how you populate the new instance – otherwise there will be no information in it, you will just have an empty shell.
As ever, this is going to be way more interesting and dare we say it easy to understand, if there is an example to look at. We’re still working with our supermarket example from the previous chapters, here is a reminder of the current entity model with our two entities:
We can imagine for the purposes of this example, that we add another attribute to identify products by their brand. And we will create another entity, the shop’s own branded products to use for this demonstration. Consider the business requirement – we want to extract the list of products that were the shop’s own brand, because these products qualify for loyalty points. Other branded products do not. So we are going to need to create mirror-image instances, but also to use a filter to only create certain products based on the brand.
Step One : Create the new entity
Step Two : Create a relationship between the new entity and the source entity, in this case the purchased items. The direction is important – it must be from the new, target entity to the source entity, and the relationship type needs to be set as a many to one. This is part of the nature of a relationship which is part of an inferred associative entity , to give it the proper title. You will see an error if you try another form of relationship.
Step Three: Infer the products without a filter using InferInstanceFor()
InferInstanceFor(the shop’s own branded product, the relation between the own brand products and the purchased items)
That is the magic, right there – the relationship allows us to set a source and create the target instance.
Step Four: Populate the attributes of the shop’s own branded products to make the entity useful
Of course, creating the entity instance is one step, but since it is devoid of useful attributes, we need to get some values from the source entity so we will use the For() function to do so. Watch the video below for examples.
Step Five: Add the filter to only create instances of the shop’s own branded products
For this step, as discussed in the video below, what happens if you do not want to create mirror-image instances for every entity instance? How do you filter the output. Well. you add a condition to your InferInstanceFor() to specify your conditions.
Step Six : Sit back and enjoy the result.
Tip: If you enjoy a video walk-through, we’ve got you covered. The video below walks through the steps and provides extra background commentary about mirror-image instances. You can also find an example in the online documentation and an earlier article about InferInstanceFor() with some more examples right here.
Previous Articles in this learning journey
Containment Relationships were discussed here.
Inferring Containment Relationships was discussed here.
Reference Relationships were introduced in this article.
Inferred Reference Relationships were discussed here.