开发者

How can i structure a program (proces) with a very high number of IF statements

i have to create a program for a kind of complex proces. Well, the process is not complex, but there are a lot of variables which control the process. I can't in detail tell about the process, so i've made up one, which has the same load of IF's:

the process is: should we stop the iron melt oven or not. We've got these parameters: if the temp goes above 800 degrees celcius, stop it Except, when we expect cool water to be available from oven 2 in the next 20 minutes, we can continue

Except, when the temp rises 10 degrees in the next 10 minutes, we can't wait another 10 minutes for the cold water, so we have to stop.

Except, when the temp goes down for some reason to 790-800 degrees for 5 minutes, we add an extra 5 minutes to the time we need the extra cool water.

Except, when the temp goes down for some reason to 780-790 degrees for 5 minutes, we add an extra 5 minutes to the time we need the extra cool water.

etc. etc.

You can all think of another 20 except / if / then's

i开发者_运维问答n our process we have > 50 situations, all for one goal: should the machine stop or not.

I must say i normally don't have that many situations for one goal/issue (namely: stop the machine or not), and it is also timebound: if this is happening for 10 minutes then...., and we have to calculate the situation every minute again.

Is there a way to program this in a smart way?

(and also unit test, because we have loads and loads of combinations which are all seperate unit tests i guess?)


Instead of manually working out all the situations in which you must stop the oven based on your knowledge of how an oven works, you could get the computer to do this for you.

Try to program a model for how an oven works and at each time point you can run a simulation of what will happen in the next 10 minutes or so. If the simulation shows that a serious problem will occur soon then you can stop the oven now, otherwise let it run for a minute, read the inputs and run the simulation again.

Coding an accurate model will require some effort, but as your requirement change slightly (for example your over was upgraded or repaired) it will be relatively easy to update the model - in the best case you just modify the model parameters or update a single formula.

With your current approach if you modified the oven slightly you would have to first calculate manually what effects that will have, and then go through all the code line-by-line updating the different conditions.


Regarding unit testing - ideally you should test your model on data from a real oven in a variety of situations and check that your simulation predicts the actual measured results (with some tolerance allowed for reading inaccuracies). If this is not possible then you can hardcode some example scenarios and the expected result (e.g. overheating) and test that the simulation correctly predicts the result.


Some problem spaces dont fit into the OOP principle very elegantly. It sounds your situation can be managed much better in a rule based paradigm (especially when rules change). Here is an exampe of a rule based framework for .NET: http://www.codeproject.com/KB/cs/Drools_NETPrimer.aspx


imho the way you are reasoning about the problem closely resembles a state machine.

So its more about modeling states and what you can do in each of those. WF was mentioned for a different use (actually its ruleset), but I'd look more at the state based workflows, as there you can focus on each of those states and how you can transition between then.


+1 bump MrDosu's answer on rule engines. A simple rule engine like the WF rule engine may be enough for what you need. As long as you use .NET 3.0+ you can use this programmatically without using workflows.

EDIT: unit testing this should be easy, but laborious to create all the scenarios to test. The data (a class) that you send as input to the rule engine is the current state of the scenario. Create a number of states or scenarios and send these to the rule engine in a unit test. Or you could combine multiple states one after another in a single unit test to test a logical progression

EDIT2: some source code for using WF rulesets

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.ServiceModel.Description;  
using System.Xml.Schema;  
using System.Workflow.Activities.Rules;  
using System.Workflow.ComponentModel.Serialization;  

namespace ServiceMediation.Behaviors  
{  
    public class TransformingMessageServiceBehavior : IServiceBehavior  
    {  
        private string _ruleSetPath = null;  
        private RuleSet _ruleSet = null;  

        public TransformingMessageServiceBehavior(string ruleSet)
        {
            if (!string.IsNullOrWhiteSpace(ruleSet))
            {
                _ruleSetPath = ruleSet;
                WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
                XmlTextReader reader = new XmlTextReader(_ruleSetPath);
                RuleDefinitions rules = serializer.Deserialize(reader) 
                    as RuleDefinitions;
                _ruleSet = rules.RuleSets[0];
            }
        }
...

and then something like

RuleValidation validation = new RuleValidation(typeof(EvaluationData), null);
RuleExecution exec = new RuleExecution(validation, data);  
_ruleSet.Execute(exec);  

with EvaluationData = the data you give as input. The rules are typed to this data. It also serves as output result, check the result of executing the ruleset on a property of the data.


Find a library that provide implementation for finite state machine and model your process as a fsm. There is a number of questions about fsms on stackoverflow: http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=fsm+site:stackoverflow.com.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜