Recalculate

We've seen how the rebuild phase recreates all of the dependencies for a particular model; once this list of dependencies is available, the recalculate phase can make use of the list to carry out its calculations.

Recall that we had this combination of instance and bind statements:

<xf:instance>
  <instanceData xmlns="">
    <a>10</a>
    <b>10</b>
    <c />
    <d />
  </instanceData>
</xf:instance>

<xf:bind nodeset="c" calculate="../a * ../b" constraint=". &lt;= 100" />
<xf:bind nodeset="d" calculate="../a + ../b" constraint=". &lt;= 20" />

and that after the rebuild phase, we'd have the following MDDG:

Target Type Expression Dependents
a[1] node c[1], d[1]
b[1] node c[1], d[1]
c[1] calculate a[1] * b[1] c[1]'s constraint
c[1] constraint c[1] <= 100
d[1] calculate a[1] + b[1] d[1]'s constraint
d[1] constraint d[1] <= 20

We've already said that the whole purpose of having this 'master' graph is to ensure that the XForms processor doesn't carry out unnecessary calculations, so the next question is how is it used.

The Pertinent Dependency Subgraph

If you got the hang of the master graph that contains details of all calculations and dependencies in a model, then the idea of a smaller graph that contains only the relevant parts of the master, will be quite easy. For example, if a changes, we can see that it has dependents of c and d, and so create a smaller graph:

Target Type Expression Dependents
c[1] calculate a[1] * b[1] c[1]'s constraint
d[1] calculate a[1] + b[1] d[1]'s constraint

To create this list the dependency-engine needs to know what data has changed; if a node is changed by the user (via a form control) or by xf:setvalue, then a reference to the node is recorded in a change list. This list of nodes is then used during the recalculate phase to create a pertinent dependency subgraph, which shows only those calculations from the 'master' list, that need to be performed.

To continue our example, note that the two calculations that were just added to the subgraph also have dependents:

Target Type Expression Dependents
c[1] calculate a[1] * b[1] c[1]'s constraint
d[1] calculate a[1] + b[1] d[1]'s constraint

Having a dependent means that after c and d are recalculated there are further calculations that need to take place, and they too must be added to the subgraph:

Target Type Expression Dependents
c[1] calculate a[1] * b[1] c[1]'s constraint
d[1] calculate a[1] + b[1] d[1]'s constraint
c[1] constraint c[1] <= 100
d[1] constraint d[1] <= 20

Once this list has been created, all recalculations in the list can be performed. The PDS is then discarded, since it will be recreated the next time recalculate occurs.