Wednesday, 7 January 2009

Dividing and conquering with CAM

It can sometimes be difficult to  know how to manage some of the processing of complex structures.  Where do you put rules?  What is a context used for?  When should I use a condition and when should I use a context node?

  1. Default Rules - these are rules that are applied to the template first and can be over-ridden either by Context Nodes or conditional rules.
  2. Context Nodes - These are used to divide rules up into groups.  Typically they are groups of rules that will apply when a particular condition is true.  These conditions might simply be the existence of a node in the tree, or a node having a particular value.  
  3. Conditional Rules - This is used when a condition is applied to a single node.  Typically these are rules that check values of the node in question and apply, for example, a mask based on its length.
Rule processing is done by first applying the Default Rules to the template.  These rules are unaffected by the content of the XML file being checked.  They can therefore be associated with nodes in a pre-processing stage when a template is read in.  Context and conditional rules are applied only when an XML file is being processed.  The processor looks through each node in the template structure in question and checks it against the XML nodes it finds in the input document.  If a node has conditional rules applying they are applied and then the checks are done based on the content of the XML file.  If a node does not exist in the XML file any associated conditions are still checked but in the context of the parent node.  The reason for this is that a condition might be designed to mark a node as excluded and that might explain why the XML does not contain the node.

CAMV in particular does not distinguish between Context and Conditions.  It combines the conditions defined for the Context with any conditions found on the rule.  This combination is done during the compiler stage.  It is possible to see this by using the CAMed> Tools menu> Compile Template option.  This will output a .cxx file which is a simple compiled template that is consumed by the CAMV processor.

JCAM (in the process of being withdrawn) took a different approach.  Every time an XML file was submitted for processing the template was read in and a stage called trimming was run.  This first applied the default rules and then using the XML file applied the context and conditional rules.  If nodes were repeated JCAM duplicated that part of the tree and then applied any rules.  This trimming process had some problems, for example marking a node as excluded meant it was removed from the tree altogether, making it impossible to recreate it should a conditional rule mark the node as in use.  CAMV gets round this by holding both a default set of states and an applied set that is used each time a node is encountered.  Should an excluded node be marked as in use the flag in the applied settings is changed from excluded = true to false.

Rule of Thumb for Use of Context
If a condition is based on the structure of the document it will probably be best to create a context node.  Secondly it is possible to nest context nodes to handle complex compound conditions.  This sometimes makes the creation of the template easier - hence dividing to conquer!

No comments:

Post a Comment