Welcome to the IDIOM Decision Products Knowledgebase.

alt
 



Recent Comments

    Another External Context Example

    Shawn Marshall  25 February 2010 04:26:12 PM
    What is context?
    Context is the location of a node on the customised schema. Context is expressed as a pathway (xPath) which you would navigate in order to reach the node if you were starting from the schema's root node.

    e.g. In this sample schema
    Image

    The root node is the node.

    The context of the node is /CarSalesLot/CarsForSale/Make

    The xPath of the make node is /CarSalesLot/CarsForSale/Make

    From the above example we can see that the context of a node is synonymous with its xPath.

    What does Idiom use context for?
    Idiom uses context to lookup node values within the schema defined documents, or to write out values from decisions or decision groups. Context defines where the data is or where the data is to be written.

    Image

    What else affects context?
    For lookup operations within collections, Idiom requires the index value of the current collection element (from which the node value is to be retrieved) to also be provided. In these cases Idiom will combine context (xPath) with an index value in order to perform a node lookup.

    Image

    In the above business document if we want to lookup the Year value for the Toyota Corolla we would have to specify the index value for the  collection - in this case [index 2] would be a reference to the car details for the Toyota Corolla. Without this index value the lookup becomes ambiguous and Idiom would not know which element to retrieve the node value from.

    When running rules how does Idiom maintain context?
    Idiom uses a concept of "current context", which is the fully qualified (Xpath plus indexes as required) context of the currently executing decision/decision group. So as we navigate through decision models or execute formulas the current context is continuously changing to match the context of the decision group / decision /formula that we are working with.

    Idiom keeps a history of all the previously visited contexts in the tree for each decision and decision group. This context history is held in a container called the "context stack". So for each decision group and its children (decisions or decision groups) Idiom will maintain a list of previously visited contexts in the parent tree. And when we complete a decision group (or decision), that group's contexts are removed from the "context stack". Note that the context stack is not directly visible to the decision designer. It underpins all Idiom decisions and operations, but can only be accessed implicitly by the decision designer through the PreviousContext and ExternalContext operators. PreviousContext shifts the context to the prior context, whereas ExternalContext references an additional context from within an existing formula context.

    Image

    In this decision model, as the decision group executes, the context values of both the inner and outer loops are cached so that the context stack grow as follows :-

    Begin Decision group:

    CarSalesLot

    First step in iterating across CarsForSale (outer loop)

    CarSalesLot
    CarSalesLot /CarsForSale[1]



    Then begin iterating across SalesContacts to find a match (inner loop)
    CarSalesLot
    CarSalesLot /CarsForSale[1]
    CarSalesLot /SalesContacts[1]



    Then as we iterate across the inner loop (SalesContacts) the index value of the SalesContacts iteration changes
    CarSalesLot
    CarSalesLot /CarsForSale[1]
    CarSalesLot /SalesContacts[2]



    And as we leave the outer loop again to move to the next index value of the outer loop (CarsForSale) iteration we see the inner loop contexts removed and the index of the outer loop incremented
    CarSalesLot
    CarSalesLot /CarsForSale[2]



    When do I need to reference the "Context Stack" / "Context History"?        
    You will typically reference the context stack (by using the ExternalContext operator) to shift between collections when you need to simultaneously refer to two elements that are contained within different collections, for instance, when comparing two lists. When you are using decision groups in the same decision tree that are iterating across collections it is best to think of the decision groups as context references in the decision stack. In order for formula’s to lookup nodes which are resident in a parent decision group's context you will need to use the ExternalContext operator to perform these node lookups.
    Using Idiom decision groups it is possible to perform complex schema traversals to iterate nested and sibling collections in order to drive the flow of decisioning within a decision model.  

    e.g. In this sample business document






    Image:Another External Context Example

    In this example the rules need to check if any of the calls the sales reps have had from their contacts matches with a car that is in inventory. To do this you will probably want to iterate across the nodes and for each car check if there is a car which matches the preferred make in the collection.

    Image

    So the above decision model has a decision group on the node which will iterate across all cars and then a further decision group on the node which for every car node will iterate across all contacts.

    In this example we will need to make a comparison between the car the contact is interested in [CarSalesLot/SalesContacts/PreferredMake] and the make of the car we are currently iterating over in the inventory [CarSalesLot/CarsForSale/make]. In order to do this we will have to make use of the external context operator.

    Image  

    The above formula which performs the comparison has its context set to the decision which the formula is attached to. Since this is [CarSalesLot/SalesContact/RegistrationMatched] we will need to perform a lookup of the [CarSalesLot/carsForSale/Make] from within the formula. The node lookup will have to be prefixed with the external context operator because the [CarSalesLot/carsForSale/Make] node is a member of a collection which we were previously iterating. By using the external context operator here we will be telling Idiom to go and get the CarSalesLot/carsForSale/Make] node from the most recent Contact collection element (i.e. the one currently indexed).