View Single Post
  #7  
Old November 2nd, 2011, 12:11 PM
Stack's Avatar
Stack Stack is offline
Regular Contributor
 
Join Date: Oct 2008
Location: Chesapeake Beach, MD, USA
Posts: 107
Default Re: How to compose a range of records?

Quote:
Originally Posted by Dan Korn View Post
Well, we'll certainly be glad to consider any new feature or enhancement requests, but I can't say what the priority of any given request will be. I'm not sure how many customers would make use of such a feature.



That will certainly work, but there are definitely more succinct ways to accomplish this kind of thing in JavaScript. First off, you can certainly replace all those multiple "if/else" blocks with a few uses of the || ("or") operator, like so:
Code:
FusionPro.Composition.composeThisRecord = (CurrentRecordNumber() == 2) ||
 ((CurrentRecordNumber() >= 345) && (CurrentRecordNumber() <= 351)) ||
 ((CurrentRecordNumber() >= 987) && (CurrentRecordNumber() <= 998));
return FusionPro.Composition.composeThisRecord;
But we can simplify this further by using a local variable, like so:
Code:
var val = CurrentRecordNumber();
FusionPro.Composition.composeThisRecord = (val == 2) ||
 ((val >= 345) && (val <= 351)) ||
 ((val >= 987) && (val <= 998));
return FusionPro.Composition.composeThisRecord;
HINT: Hitting Ctrl+F in the Rule Editor will bring up a Find/Replace dialog. Or, in FusionPro 6.0, there's a handy "Find..." button you can click.

Also, you can use the Building Blocks dialog to iterate through the records and test to make sure that the result is what you want for each record. That's why we're returning the value at the end. You could also use the Print statement to write a message to the log file for each record saying what the result is.

Still, this logic can be generalized even further. Consider this:
Code:
 
function CheckRanges(val)
{
  switch (val)
  {
    case 2:
    case 657:
    // etc.
      return true;
  }
 
  switch (true)
  {
    case (val >= 345 && val <= 351):
    case (val >= 987 && val <= 998):
    // etc.
      return true;
  }
 
  return false;
}
FusionPro.Composition.composeThisRecord = CheckRanges(CurrentRecordNumber());
return FusionPro.Composition.composeThisRecord;
You can see that adding more ranges is just a matter of adding "case" lines to the appropriate "switch" statement. (I didn't invent the "switch (true)" trick, but it's cool!)

But wait, there's more! If you order now, you also get this:
Code:
 
var ranges = [ 2, [345,351], 657, [987,998] ];
function CheckRanges(val)
{
  for (var i in ranges)
  {
    if (ranges[i] instanceof Array)
    {
      if (val >= ranges[i][0] && val <= ranges[i][1])
        return true;
    }
    else if (val == ranges[i])
      return true;
  }
 
  return false;
}
FusionPro.Composition.composeThisRecord = CheckRanges(CurrentRecordNumber());
return FusionPro.Composition.composeThisRecord;
It can cut open this tin can and still slice through a tomato!

Even more succinctly:
Code:
 
var ranges = [ 2, [345,351], 657, [987,998] ];
function CheckRanges(val)
{
  for (var i in ranges)
    if (val == ranges[i] || ((ranges[i] instanceof Array) && val >= ranges[i][0] && val <= ranges[i][1]))
      return true;
  return false;
}
FusionPro.Composition.composeThisRecord = CheckRanges(CurrentRecordNumber());
return FusionPro.Composition.composeThisRecord;
So, all you need to do to modify the ranges you want to recompose is to change this single line:
Code:
var ranges = [ 7, [15,34], [75,83] ]; //etc.
I can't imagine any enhancement to the GUI that would be any simpler than that.

This kind of thing is exactly why we provide a JavaScript-based rules subsystem with hooks into the composition engine, so that you can do whatever job-specific thing you want to do with a few lines of code, instead of us having to implement dozens of custom dialogs to do a small subset of what some customers might want.

Is it possible to use the above JavaScript to also compose those record ranges as individual files? Or would that be similar to "arbitrary chunking", as described in Post #2 in this thread I previously started?
Reply With Quote