Friday, 19 December 2014

Content Processing Framework



Content Processing Framework (CPF) in Mark Logic   

Content processing framework is a very important concept of Mark Logic which most commonly used to process content for some tailoring and editing on the basis of certain rules. It actually provides the way and options to process content data/document through a sequence of steps to convert data in desired and usable format. 

Today, we are going to discuss key points of CPF which is important to know while implementing CPF based application.  

Overview to Content Processing Framework

Content processing application is often consist of multiple steps and each step of process performs specific task or set of tasks. Each step updates the state of document towards the final with individual commit to database for each step.

Component of CPF

Content processing framework is consist and based on following mandatory components to process data  through CPF to make it more usable and effective.

1. Domains
2. Pipelines
3. Modules and XQuery functions for content processing

1. Domains
Domain is the scope to define set of documents for content processing through CPF. Domains are used to define a group of document/data for specific type of processing however same document can be considered in another domain as well for some other processing. 

2. Pipelines
In simple words we can say that pipeline is a tunnel that consist of various steps and every step does some modification/updates in document content and states and moves towards the final stage where document can be directly used for efficient results. 

Pipeline is a xml document created in a specific format with specific element items which specifies the steps to process a document through CPF. The pipeline xml contains following key points to define processing through CPF. Snippet for pipeline xml is also given below.

<?xml-stylesheet href="/cpf/pipelines.css" type="text/css"?>
<pipeline xmlns="http://marklogic.com/cpf/pipelines" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://marklogic.com/cpf/pipelines pipelines.xsd">
  <pipeline-name>Add-last-date</pipeline-name>
  <pipeline-description>
This pipeline add a new property in document with last-modified
  </pipeline-description>
  <success-action>
    <module>/MarkLogic/cpf/actions/success-action.xqy</module>
  </success-action>
  <failure-action>
    <module>/MarkLogic/cpf/actions/failure-action.xqy</module>
  </failure-action>
  <state-transition>
    <annotation>
      when a document inserted or updated add a new property in document with last-modified
    </annotation>
    <state>http://marklogic.com/states/initial</state>
    <on-success>http://marklogic.com/states/converted</on-success>
    <on-failure>http://marklogic.com/states/error</on-failure>
    <priority>9200</priority>
    <execute>
      <condition>
        <module>/MarkLogic/cpf/actions/mimetype-condition.xqy</module>
        <options xmlns="/MarkLogic/cpf/actions/mimetype-condition.xqy">
          <mime-type>xml</mime-type>
        </options>
      </condition>
      <action>
        <module>/MarkLogic/conversion/actions/convert-html-action.xqy</module>
        <options
         xmlns="/MarkLogic/conversion/actions/convert-html-action.xqy">
          <destination-root/>
          <destination-collection/>
        </options>
      </action>
    </execute>
  </state-transition>
  <!-- States converted and error not handled here -->
</pipeline>


1. Steps : - A Pipeline contains all steps (indicated by state-transition’ tag in xml) that need to be processed while document of specific domain is being processed through CPF.
2. States :- A Pipeline contains information of document state (indicated by state tag in xml) that need to be processed in specific step of CPF. Single document can be processed differently on the basis of state. For ex. If document in inset/initial state we can perform different process and if document in update state then we can perform different process.
3. Success Action: - Pipeline xml provides information of success action (indicated by ‘success-action’ tag in xml) that need to be performed when pipeline process is completed. It is shown by On Success” in already configured pipeline in CPF.
4. Failure Action :- Pipeline xml provides information of failure action (indicated by ‘failure-action’ tag in xml) that need to be performed when pipeline process is completed. It is shown by On Success” in already configured pipeline in CPF.
5. State of document after success of particular step process:- Pipeline xml provides information about state of document, after successful completion of process of specific step in pipeline. As already discussed earlier that each step is executed separately and results committed after each step. So this configuration section informs about state of document committed after process of a pipeline step.
6. Execute :- This section of pipeline xml contains information of conditions that need to be satisfied to process specific documentation and action that need to be performed if condition satisfied.
7. Condition :- This section contains location of module which results in boolean result and executes specific condition which should be satisfied by document being processed than only it is going to be processed by action module. For ex. A file must be XML then only perform some action on that in that case a condition module that verifies mime type of document is used as condition.
8. Action :- This section contains location of module which is written to process specific document if associated condition matched.



Below is the sample of code in action module which adds last-updated” element in all xml documents.

xquery version "1.0-ml";
import module namespace cpf="http://marklogic.com/cpf" 
at "/MarkLogic/cpf/cpf.xqy";
declare variable $cpf:document-uri as xs:string external;
declare variable $cpf:transition as node() external;
if (cpf:check-transition($cpf:document-uri,$cpf:transition)) then try 
{
let $doc := fn:doc($cpf:document-uri)
return
xdmp:node-insert-child(
$doc/book,
<last-updated>{fn:current-dateTime()}</last-updated>),
),
xdmp:log( "add last-updated ran OK" ),
cpf:success($cpf:document-uri, $cpf:transition, ())
} catch ($e) {
cpf:failure($cpf:document-uri, $cpf:transition, $e, ())
}
else ()

So this is enough for start of CPF basic concepts that give you a quick idea abut CPF  framework and very important points in it to start. After that CPF has lots of other advance functionality and implementations which you can explore further. I will also try to share some simple examples with you guys.   

Till than keep exploring.


No comments:

Post a Comment