Jump to content
David Schwartz

awk-like processor using Delphi code?

Recommended Posts

I've got a situation where I have a bunch of combo-boxes where the user can define conditions for rules that are applied to set attributes on a big grid of "buttons" that are used to display the state of different things.

 

(Sort of like if you had a grid and each row has combo-boxes to select what goes in a cell. But in this case, there's a row of them at the top and data is read / saved from / to a selected row in the grid.)

 

There are a half-dozen combo-boxes, a few edits (for literals), and a couple of check-boxes for options.

 

The user selects the factors he wants for a rule, then saves it to the grid. (Actually, I'm using a ListView in this case.)

 

This is a "rule editor". At "run-time", we get some data in and it gets processed by going through each row in the list and applying all of the rules to set how each button looks. There's one record per button.

 

I created an interpreter that loads up groups of rules and processes them for each record.

 

Given that Delphi doesn't support the use of strings as discrimanents in Case statements, what do folks do to deal with this? I've seen a dozen or so approaches over time.

 

But in this case, I'm looking for a solution that's best suited for rapidly going through a list of rules for each incoming record. It's sort of like a table-driven parser in that respect.

 

You need to have a map to get from the list of strings to associated integers, then use the integer values in the Case statement.

 

In my first approach, I made the mistake of tying everything to the UI components. They're used to create and edit the rules. But when processing the data, loading each rule into the UI elements is completely unnecessary since the user isn't interested, and there's really nothing to see. I want them to simply click a button and a form pops up with a grid and all of the buttons have what they want displayed on them.

 

The nice thing about using the combo-boxes is that you can use the ItemIndex property instead of the string in the Text property. That is, the mapping is built-in.

 

Ideally, what I'd like is something like the syntax that the awk language offers, which sort of like a Case statement where you get a string and it falls-through a list of regular expressions. Each one that matches runs a block of code next to it.

 

Maybe there's an interesting way of compensating for the inability to use strings with Case statements that I'm not aware of.

 

Right now I've just got a bunch of Case statements that use a stringlist to map strings to integers. But if I change the list, I have to re-number the lines in the Case statement. Grrrrr

 

(Yes, I know it's best to add things at the end, but I'd prefer to have things either sorted alphabetically or organized in semi-related sub-groups. I don't like having the damn programming language impose restrictions on how things appear to users!)

 

As I think about it, what might be best is something that lets me add items to a list with a string (key) and an anonymous method that does something when that key is found, corresponding to what a string-based Case statement might do, or even what awk lines do (just more verbose).

  • Like 1

Share this post


Link to post

Dictionary and anonymous methods.  Link the method to the keyword. Look up via the keyword and execute the code.

  • Like 2

Share this post


Link to post
Guest

You could hash some strings. But i would go for a parallel TDictionary of sorts, @Lars Fosdal expressed it concisely enough.

Share this post


Link to post
1 hour ago, Lars Fosdal said:

Dictionary and anonymous methods.  Link the method to the keyword. Look up via the keyword and execute the code.

Thanks, I'll give that a try.

Share this post


Link to post
1 hour ago, Alexander Sviridenkov said:

Isn't that similar to CSS?

sort of, but Delphi doesn't support CSS in forms.

 

I know there are some components that support HTML, but ... CSS?

Share this post


Link to post
6 minutes ago, Alexander Sviridenkov said:

HCL has very fast and universal CSS processor (separated from UI/rendering), maybe it can be used there.

What is HCL? Is there a button or graphic that I can display things on and format the BG color, Font attributes, display text, some smileys, etc?

Share this post


Link to post
20 minutes ago, David Schwartz said:

What is HCL? Is there a button or graphic that I can display things on and format the BG color, Font attributes, display text, some smileys, etc?

HTML Component Library. Yes, it can display almost the same as browser (HTML4 and CSS3, except JS) but I was thinking about using its CSS part for calculating and applying rules.

Share this post


Link to post
1 hour ago, David Schwartz said:

I know there are some components that support HTML, but ... CSS?

HtmlViewer supports CSS but of course only that is linked with HTML

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×