Rich Dynamic SenseTalk Object Vending

Ok, here was a challenge this past week. I wanted to pass some unprocessed text to a new object and have it crunch on the data, slice and dice it into the properties of said new object on the fly. This helps abstract that functionality from use, and allows multiple (read, any number of) data adapters to be used for various data sources. Ideally I would like to do
repeat until someCondition
 insert a new Question with someData into aList
end repeat

This syntax 'with someData' will work but someData must be a property list. In talking with Mr. SenseTalk, it was discussed how to work around this minor limitation (I have proposed a general solution to this challenge to be discussed at another time). The solution that we both agreed would work for now is to pass in a property like 'rawData' and then let the initialize handler crank on that data (knows it is there) and then delete it when done. The prototype object's initialize handler looks something like this pseudo SenseTalk code.
to initialize  -- Proto's handler
 if my rawData is empty then Throw "Malformed Proto Data Exception", "rawData was unspecified at the time of handling the initialize message.  Data provided during initialization is required via the rawData property...  example 'get a new Proto with ( rawData: someData )'"
end initialize

What this does is enforce a rule, required rawData to be provided. If the data is not provided, the Proto object refuses to vend a new object by throwing an exception. We use this fact to our advantage by trying and catching all that the same time. In effect, the byproduct of the exception is what we want.
-- Attempt to vend a proto without proper data
repeat with each line in data()
 try to insert a new Proto with ( rawData: it ) into aList
end repeat

put aList

Now this may seem a bit obvious of an example, maybe simplistic. However, your rules enforced by script in the initialize handler can do anything at all. Read the time of day, maybe require certain objects to be vended only during periods of configuration management completion? Using sockets, semiphore files or result files you may want to disallow certain types of objects due to rules in your business flow. A more concrete example that is less than trivial yet simple is the creation of Question objects.

I had a list of tests and questions from the American Amateur Relay League, AARL, or ham radio. I wanted to create in Eggplant a simple flash test quiz that I will use to master the 397 or so questions for the basic Technician test. To start with, I wanted to model this test in an interactive fashion, in an OO way of thinking and implementation. Thus, I had in my data model the Question proto object. I wanted to split the data up with a simple statement and then feed the known AARL question chunks to the Question proto to vend valid questions. Included here is that work, too long to show but to point out you can have all kinds of rules and parsing done at start up of an object. The limit is your mind, work, requirements, and needs.
Sign In or Register to comment.