views:

200

answers:

7

I'm working on a project which will do some complicated analyzing on some user-supplied input. There will be 3 parts of the code:

1) Input supplied by user, such as keywords

2) Rules, such as if keyword 1 is repeated 3 times in keyword 5, do this, etc.

3) And the analyzing itself which executes the rules and processes the user input, and generates the output necessary based on the processing.

Naturally this will lead to a lot of spaghetti code and many, many if statements in the processing code. I want to avoid that, and keep the rules (i.e. the if statements) separately from the code which loops through the user input and generates the output.

How can I do that, i.e. what is the best way?

+1  A: 

Store it in XML. Easy to parse and update.

I had designed a code generator, which can be controllable from a xml file. For each command I had a entry in the xml. I was processing the node to generate the opcode for that command. Node itself contains the actions I need to do for getting the opcode. For some commands I had to look into database, all those things I had put in this xml file.

Vinay
A: 

You always can implement factory that will create certain strategies according to passed parameters. And then you will use those strategies in your code without any if.

Mykola Golubyev
A: 

Adding an embedded scripting language to your application might help. The rules would then be expressed in scripts, executed by the applications on processing.

The idea is that scripts are easy to change and contain high level logic that will be executed by your application in details.

There are a lot of scripting languages available to do this : lua, Python, Falcon, squirrel, angelscript, etc.

Klaim
+3  A: 

If you have enough rules that you want to externalize, you could try using a business rules engines, like Drools in Java.

A business rules engine is a software system that executes one or more business rules in a runtime production environment. The rules might come from legal regulation ("An employee can be fired for any reason or no reason but not for an illegal reason"), company policy ("All customers that spend more than $100 at one time will receive a 10% discount"), or other sources. (Wikipedia)

It could be a little bit overhead depending of what you're trying to do. In my company we're using such kind of tools for our quality analysis tool.

madgnome
A: 

If it's just detecting keywords, a finite state machine or similar. If it's doing more, then other pattern matching systems, such as rules engines.

Pete Kirkham
+1  A: 

Well, i doubt that it is necessary to have hughe if statements if polymorphism is applied correctly.

Actually, you need a proper domain model for your rules. This goes somehow into the direction of the command pattern, depending on the complexitiy of your code maybe in combination with the state machine pattern.

Once you have your model, defining rules is instantiate them correctly.

This could be done by having an xml definition, which is parsed and transformed into your model. But the new modern and even more fancy way would be using DSLs. If you program in Java and have a certain freedom about your libraries, this would be a proper use case for Embedded DSLs with Groovy. Basically you would need a Builder which constructs your model, that's all.

Lars
A: 

Have a look at rule engines!

The approach from Lars may also be arguable.

Martin K.