Search

What the Quote?

"Please don't feed the redhead."

Monica Ferrante

"Where's the 'existing server'? Hold on, I don't know if 'redneck' has an I.P. address."

Steven Rodgers

"You can always spot Java written by C-coders because it's littered with public void (blah, blah, blah). They're proceduretarded."

Nathan T Freeman

« happy Friday... here's yer debugger | Main| we do because we must because we do »

unsung hero: @CheckFormulaSyntax

Category fgoto
It's time for another FGOTO post. Earlier today I had occasion to use an @Function that has been around since version 6 but some of you might not even know exists: @CheckFormulaSyntax. Here's what it does:

  • You pass it any string that is supposed to be valid Notes Formula but might not be syntactically correct.
  • If it is, in fact, valid Formula, the function simply returns "1".
  • Otherwise, it returns a list comprised of the following:
    1. The error message returned by the compiler
    2. Line where the error occurred
    3. The error column (number of character spaces from the first character in the line where the error occurred, beginning with 1)
    4. The error offset (number of character spaces from the first character in the block where the error occurred, beginning with 1)
    5. The length of the text that caused the error
    6. The actual text that caused the error
Where would such a thing be useful? Some of you may actually remember the first time I can recall using this function: it was back when I was playing with XIDED, and added Formula validation to various portions of the IDE. However, it's also useful in at least two "real world" scenarios:

  1. Evaluating dynamically generated Formula (for example, constructing Formula based on variable data)
  2. Storing Formula within configuration data to allow runtime evaluation of user/admin-maintained code
For example, imagine an application where workflow rules change frequently. One way to minimize the frequency with which the actual codebase must be updated to account for these rule changes is to construct a complex rules engine that accounts for all possible scenarios by allowing a process owner to create documents that define the entire flow. One way to simplify such an engine is to allow concepts such as the actor for each state, the destination state for each action, the description of the action, and so on, to be expressed as Formula that will be evaluated whenever an action is taken. This allows you (or, if you're lucky, a sufficiently savvy "power user") to manage a complex workflow with a comparatively simple rules engine, all without having to touch the underlying code every time the business decides that the application should route documents in drastically different ways.

One challenge this approach presents, however, is the possibility that Formula entered as data will be syntactically incorrect with no warning of its syntactical errors until some "life or death" document fails to route to the correct destination state. You can mitigate this risk, to some extent, simply by adding the following Input Validation to any field intended to store Formula:

syntaxResult := @CheckFormulaSyntax(@ThisValue);

@If(
@Elements(syntaxResult) > 1;

@Failure(
@Implode(("Field " + @ThisName + " contains an invalid formula") : syntaxResult[1] : syntaxResult[6]; ": ")
);

@Success
)


Note the use of @ThisValue and @ThisName to allow the Input Validation to evaluate the value and name, respectively, of the current field, allowing you to use the above Formula without modification in any field.

With this validation in place, if syntactical errors are found (sadly, there is still no reliable way to detect logical errors in advance... but we're working on it), the user populating the Formula field will see something akin to the following:
image



Comments

Gravatar Image1 - @CheckFormulaSyntax is a great function and I implemented it in our CMS system several years ago. Basically what we did was to allow the designer of a website (or intranet) to make several parts of a page dynamic based on @Formula code. A page can be created using static code (fx. HTML) and the customer can then insert <% .... %> tags that enclose the @Formula code to be evaluated. If for some reason the code doesn't work we can replace the <% .... %> part with some HTML code that outputs the @Formula code and highlights the specific part of the code that it fails on. This is really nice when you use @Formula code this way because you have no editor like the normal @Formula-editor.

Next step - with XPages - is to be able to evaluate custom @Formula code within a 'running' XPage. The eval() JavaScript function within XPages currently has a lot of bugs, but we're working on getting around some of it, and hoping that IBM/Lotus will do the rest Emoticon

Gravatar Image2 - Good post! This function is often overlooked: we used it when Mark was putting together his "faux views" application for UKLUG 2009:
{ Link }

Gravatar Image3 - "evaluate custom @Formula code within a 'running' XPage."

{ Link }

Post A Comment

:-D:-o:-p:-x:-(:-):-\:angry::cool::cry::emb::grin::huh::laugh::lips::rolleyes:;-)