Saturday, October 12, 2013

A Common Problem
When writing complex CIAC Process Orchestration processes, the developer will invariably need to make use of Windows Scripts and Windows PowerShell. Windows Scripts can be very simple to use except for the slight problem that they can only receive input as a single-line of data. 

This problem will cause any programmer struggles because there are so many activities that return multi-line data. Every CIAC programmer will need to develop a routine that compresses a multi-line string variable into a single line. 

This paper will provide the reader with a simple way to perform that compression and also provide some hints in how they might want to improve upon the solution outlined for their own production grade solutions.


The Solution


To provide a solution that will leverage native process activities we will first start with the rule that our multi-line data might contain blank lines and that we do not need those blank lines. So, the solution here will be to compress any blank lines out of the data.


Step 1; utilize the CIAC Activity ‘Match’ to break the single-line structure into rows of “Row” data. This will result in each returned row containing a hidden new-line character

Step 2; Go through each of the returned rows of data

Step 2.1; Trim the data so any white space is removed.

Step 2.2; As long as the data row we have is not zero in length

Step 3; Set the output variable to the contents of the data to be returned.


The Solution within CIAC


The process to perform this action will be very small. While this solution shown below fits the need of the use-case that drove it’s creation, you may need to modify it:


The input to this process has two input fields, the first is the multi-line data to compress and the second is an indicator of special processing. The indicator of special process is not relevant to this paper and will not be discussed except to say that it is part of a larger project code named “Lightning”. You may search for other papers written by this author about project Lightning.



The output variable is a String variable that is not defined with the Multi-line check-box:


The “Match Regular Express” activity has a regular expression looking for groups of lines:



You may find a help on the internet for writing regular expressions, but, do know that these are not new or at all unique to CIAC. Regular expressions have been around for a long time and have been built into nearly every language through some integration or another.

The output from the Regular Express Match will be rows of data, and you might get double new-line characters - blank rows of data from what appeared to not contain blank rows of data. The next part of the code logic will rectify this problem.

The next step is to use a “For Each” logic activity to process each of the rows of data that came back from the “Regular Express Match”.


In the properties for the “For Each” we will be setting the source collection to be the output from the regular express match.



Selecting the Workflow activity, then “Match Regular Express”, “All Matches Table” and finally “Rows” will give the “For Each” what it needs for processing.

All we will do from this point is construct a Branch condition to test if it does contain more than zero as a length and we will add it to a local variable designated to store the single compressed line of data.


The local variable is “NewTemplateBody” as shown above in the Variable to Update field.

After all the rows of data have been processed we will simple copy our local variable to the output variable:


In this properties block, you can see that the Variable to update is the “CompressedSingleLine” variable and that it will be set to the value of our local “NewTemplateBody” that was computed from within the “For Each” loop.

Summary


Compressing a multi-line to a single line is simple, as you can see above. And, should you follow those steps above you will quickly be on your way toward creating your applications.

Some improvements upon what I have shown above might include better usage of Regular Expressions as well as a possible need to insert pseudo line separators. Consult your Regular Expression guide to tweak the very simple regular express included in this description.

But, you might be wonder why you would need a pseudo line separator. Well, consider the possibility that you might be compressing a multi-line string so that a Windows Script can process it (you cannot send a multi-line string into a Windows Script). If you were going to use Windows Script to write the contents of your multi-line data to a flat file you will be required to insert some type of character or character string to tell Windows Script where the line data should be broken apart at so that newline characters get placed in the proper position.

As a final tip, place your new Compress Multi-line process in a common location so that all of your future applications and workflows can easily access it (rather, so that you don’t have to search for it each and every time)

Please look for other papers written by this author on tips and techniques of writing CIAC Process Orchestrator code. If you are interested in project Lightning, please search for information on that, too.