12 Replies Latest reply: Sep 3, 2014 12:51 AM by Frank.Hampshire-Oracle RSS

    Comparing grandchild entity instances

    2738155

      I have 3 entities ( including global) say Contact(global), Case, Product. There is 1:m relationship between Contact->Case and between Case->Product. Say there are two case instances (Case1 and Case2) and each case instance has 3 product instances( Case1: Product1, Product2, Product3; Case2: Product4, Product5, Product6). Now I need to compare whether any product is different between Case1 and Case2 and in case any different product is found then I need to update a field with "New Information" (say). I have gone through all functions available in OPM but couldn't find anything suitable for this requirement. Kindly suggest.

      Thanks in advance.

        • 1. Re: Comparing grandchild entity instances
          Frank.Hampshire-Oracle

          How many cases do you need to have? In this example are you always going to have 2 cases?

           

          What makes a product different from another, can it be identified by some form of id, or does it have many attributes that have to be compared?

           

          Have you considered making the product contained by global and referenced by each Case?

           

          What your asking for can certainly be done, but depending on how you setup your data model there are easy and hard ways to do it.

           

          Cheers

          Frank

          • 2. Re: Comparing grandchild entity instances
            2738155

            Yes I am always having 2 cases and have to compare between products of these two cases only.

            Products can be compared on basis of id (alphanumeric) and date created.

            Data model is such that products are tagged to the cases not to Contact directly.

            • 3. Re: Re: Comparing grandchild entity instances
              Frank.Hampshire-Oracle

              Here's an approach you might take.

               

              1. Define the old case and the new case as different entities under global. This will help when talking about them in the same rule. Its possible to compare the two cases without doing this, but you will have to use aliases (an advanced feature) to do this .

               

              Global

              - old case

              - new case

               

              2. define Products as being contained by global and referenced by the old and new case. In this case you can share the same product between cases. This makes comparison very simple.

               

              Global

              - old case

              - new case

              - product

               

              global -- all instances of the product --> product

              product  -- the new case of the product --> new case

              product  -- the old case of the product --> old case

               

               

              the product is in the old and new cases if

                InstanceCount(the new case of the product) = 1, and

                InstanceCount(the old case of the product) = 1

               

              there is a difference between the old and new product if

                Exists(all instances of the product, the product is not in the old and new cases)

               

               

              This is just one way of solving this problem.

               

              Cheers

              Frank

               

               

              • 4. Re: Comparing grandchild entity instances
                2738155

                Thanks Frank the approach you mentioned above is serving the purpose but is it possible to do same without needing to change the data model i.e. the product is linked to case not to contact directly.

                Contact---(1:M)---> Case---(1:M)---> Product

                • 5. Re: Re: Comparing grandchild entity instances
                  Frank.Hampshire-Oracle

                  It definitely is possible, but it the solution is a little more involved.

                   

                  The first thing to know is that when you write a rule for an attribute (or relationship) on an entity, that rule executes for every instance of that entity in the rulebase. This is important because we will need to execute rules on the contact entity.

                   

                   

                  1. Identify the “old” Case and the “new” Case

                   

                  The rule we are trying to write identifies if the old case is different from the new case, so we need to clearly identify which is the old case and which is the new case.

                   

                  Create a new relationship “the case’s update” The case -- (1:1) --> The case. The idea here is to assign the new case as a target of this relationship to the new case.

                   

                  Hint: It usually makes for nicer rules if the entity text is the definite article (ie “the case” instead of “case”).

                   

                  2. Write rules about differences between old and new cases

                   

                  A updated case will be different if it has a different number of products

                   

                  the case’s number of products= InstanceCount(the case’s products)

                   

                  Compare the number of products of the old case to the new case. When we need to talk about different instances of the same entity (in this case the old and new cases) we need to identify each instance. If we don’t we will end up with a rule like “the case’s number of products = the case’s number of products” which makes no sense.

                   

                  We can do this using an alias, see Reason across multiple entities for detailed information on using Aliases and scope.

                   

                  the case has an update with the same number of products if

                    ForScope(the case’s update, the updated case)

                      the case’s number of products = the updated case’s number of products

                   

                  In rule above I have used “ForScope” with an alias “the updated case” this allows me to talk about the source and target. When I reference an attribute of the source entity I substitute “the updated case” where the entity name “the case” occurs, this means that the attribute “The case’s number of products” becomes “the updated case’s number of products”. In order for aliasing to work, the text of the attribute must have the entity text in it.

                   

                  Finally the we have to go through every product in the case and make sure that we can find a matching product in the updated case (matching id and date). This combined with the number of products in each case should give us the answer.

                   

                  The case does not have changed products if

                    The case has an update with the same number of products; and

                    ForScope(the case’s update, the updated case)

                      ForAllScope(the case’s products)

                        ExistsScope(the updated case’s products, the updated product)

                          The product’s date = the updated product’s date; and

                          The product’s id = the updated product’s id

                   

                  In the rule above, you can see I've needed to use an alias to distinguish the updated case (the updated case) and the products of the updated case (the updated products) so I can use them in the rule.

                  • 6. Re: Comparing grandchild entity instances
                    2738155

                    the above solution provided is working fine but the debugger is unable to identify which instance of case is to be treated as the "new case", how can this be done? Do we have to write some rule for this?

                    • 7. Re: Comparing grandchild entity instances
                      2738155

                      ComparisonImage.JPG

                      The above image shows what is happening, debugger is unable to identify that which case is to be treated as updated case and hence not giving "Uncertain" output. But once case instance is manually selected, everything is working fine. Kindly suggest.

                      • 8. Re: Re: Comparing grandchild entity instances
                        Frank.Hampshire-Oracle

                        2738155 wrote:

                        ... debugger is unable to identify that which case is to be treated as updated case and hence not giving "Uncertain" output. But once case instance is manually selected, everything is working fine. Kindly suggest.

                         

                        That is expected. The two instances of case will be treated identically until you define which one is the old case and which one is the new case. There are two ways to do this.

                         

                        1. Manually identify the new and old case by assigning one case as the target of the other case's "the case's update" relationship

                        2. Infer the new and old case by making the relationship "the case's update" an inferred one (you will have to change it to a many-to-many), and then writing a rule to assign one case as the target of the other case's "the case's update".

                         

                        The first option is pretty straight forward, the second option will mean an "is member of rule"

                         

                        the case (the new case) is a member of the case's update if

                          the new case is not the case; and

                          <rules to identify the 'new case'>


                         

                        The conclusion in the rule above is an IsMemberOf statement which will infer membership of "the case's update", using an alias to differentiate between "the case" (source) and "the new case" (possible target).

                        The first premise "the new case is not the case" ensures that a case cannot be its own update.

                         

                        After that, you will have to add the rules that distinguish the updated case from the old case.

                         

                        The complexity with trying to reason about two instances of the same entity is why i originally suggested that you use difference entities for the old and the new case.

                         

                         

                         



                        • 9. Re: Comparing grandchild entity instances
                          2738155

                          Above solution worked fine. Thanks

                          • 10. Re: Comparing grandchild entity instances
                            Frank.Hampshire-Oracle

                            Glad to hear you were able to get this approach to work. There are some fairly advanced concepts needed to get this sort of scenario to work. The techniques allow you to do very powerful things with rules.

                             

                            Cheers

                            Frank

                            • 11. Re: Comparing grandchild entity instances
                              2738155

                              Are these advanced concepts documented somewhere? Could you provide the link to the same...

                              • 12. Re: Comparing grandchild entity instances
                                Frank.Hampshire-Oracle

                                The concepts needed for solving this sort of problem are mostly addressed in the Reason across multiple entities section of the Oracle Policy Modeling User's Guide