Totally Objects - Boolean Expression Parser (Version 5.5 [1.0])


This Totally Objects product for IBM Smalltalk enables Strings containing Boolean expressions (using the operators AND, OR and NOT) to be used to see if a condition supplied in a block holds.


This Totally Objects product has been packaged as a configuration map. To import it into your library select 'Browse Configuration Maps' from the 'Tools' menu of the 'System Transcript'. In the 'Configuration Maps Browser' select 'Import...' from the 'Names' menu and select the file tobbep5-5_1-0-*.dat. You should then select the configuration map contained within this file.

To load the configuration map into your image select it in the 'Configuration Maps Browser' and select 'Load With Required Maps' from the 'Editions' menu.


You first need to create an instance of the class TobBepParser.

This object can be configured to allow certain sequences of characters to mean AND, OR and NOT, to specify valid characters that can be treated as brackets, and quote marks. Methods for configuring are:

Next you need to provide a String expression to parse. To do this you use the method #parse:. Expressions must be formatted in a similar manner to the following example:

The order of precedence of the operations are NOT, AND and OR. Notice that any elements in the expression containing white space or any of the reserved words can be put inside quote marks to keep them together as a unit. So the above expression is equivalent to:

The answer to the #parse: method is an instance of TobBepEvaluator, or, if the String contains a syntax error the exception TobBepExceptions::ExTobBepSyntaxError is signalled. This exception has two arguments; the first is a description of the error and the second is the expression.

The final stage is to evaluate the logic in the expression by providing a context to which it applies. You do this by sending the TobBepEvaluator the message #evaluate:logicBlock:. The first argument is some data and the second is a two-argument (or three-argument) block. This block is evaluated repeatedly using the data as the first argument and the element from the expression as the second (and optionally the String representation of the element before conversion - see 'Advanced Operation' below) and must answer a Boolean.

Let us look at an example:

A user wishes to find the documents in a large collection that contain certain text. The code might look something like this:

Advanced Operation


There are times when certain conditions hold for multiple items that we might wish to test. Testing each these against the whole expression may be unnecessary. For example, if we already have our documents grouped by subject and then we wish to find documents containing a word that happens to appear in one of the subject tiles, we do not need to bother looking for the word in each of the documents on that subject. Similarly, if we wish to find documents not containing a particular word, we can save ourselves the effort of searching through documents that are sure to contain that word. To so this we use the method #reduce:logicBlock: to produce a new TobBepEvaluator that contains simplified logic, and the method #canEvaluateWithoutFurtherData to see if the evaluation of a TobBepEvaluator will automatically answer true or false whatever data is supplied (using the method #value). To explain this, the above example is extended; this time the first argument to the method is a Dictionary. The keys contain a subject string, and the values, the documents on that subject.

So for example if the argument docsDict is:

If the expressionString is 'dogs OR horses'. Then all the elements of the 'dogs' collection will be included without testing any of them. All the items of the 'cats' collection will be tested with the expression 'dogs OR horses'.

If the expressionString is 'dogs AND cats'. Then all the elements of the 'dogs' collection will be tested with the expression 'cats'. All the items of the 'cats' collection will be tested with the expression 'dogs'.

If the expressionString is 'dogs AND NOT cats'. Then all the elements of the 'dogs' collection will be tested with the expression 'NOT cats'. All the items of the 'cats' collection will be excluded without testing them.

Validating and Converting Elements

To test that elements contain valid strings the following methods can be sent to the parser:

If a element passes the validation test then it can be converted from a String to another object. This is achieved by sending the following methods to the parser.

Sometimes elements may contain comparison expressions (such as "color = blue" or "time > 13:00"). The class TobBepComparison has been provided to make the management of such expressions easier. Sending the message #asTobBepComparison to an element's String will convert it to a TobBepComparison (or answer 'nil' if it is not a suitable format). The following methods can then be used to interrogate it further:

Also, TobBepComparisons can be made directly using the class method #left:operation:right:. This can be useful if the 'left' or 'right' value need to be converted.

The following example checks the validity of a Date against an expression.

Further Information