# Ways to implement a decision making process involving complex rules [closed]

I'm investigating different solutions for solving what looks like a decision making problem. Although the domain I'm working on is different, for the problem at hand, it's quite easy explaining with an example of online shopping cart.

Suppose you have a shopping cart like you have in every online retailer but this one checks some criteria and allow you to proceed only if the cart conforms to some criteria. Those criteria can depend on:

• a property of the item you add to your cart (ex: minimum quantity allowed for the item you add)
• combinations of the items you add (ex: if you have in your cart an item from sports category, you need to add an item from nutrition category)
• properties of the cart itself (ex: option to give multiple orders from the same cart but only if certain type of items are in the cart)
• and many other rules with exceptions...

So at a given time, your cart is in one of two states: valid or invalid. It starts with the state invalid: An empty cart. And at each action you take the state changes.

Some of the actions that change the state of the cart are:

• adding an item to cart
• changing a property of the item added, like color, size, quantity, etc
• changing a property of the cart, like applying a discount code.

What I'm trying to achieve is to find a clean way to decide after every action, whether the cart is in a valid state or not.

The current code base for this is a mess. A method of hundreds lines of code, taking 15 arguments and filled with ifs and fors.

I wonder what are the possible solution patterns I can use to refactor this into a cleaner and maintainable implementation.

I've investigated finite state machines but couldn't figure out how to represent this into states and actions. Decision making trees I've seen are for pretty simple flows when you can answer yes or no easily. And they were implemented with nested ifs and elses which is quite similar to the current (messy) solution. My investigation lead me to AI and expert systems but I stopped there. As you can guess, I have no previous experience dealing with these kind of problems. Maybe what I'm trying to solve is a complex one and it's ok to have complex solutions to complex problems. But maybe this is a generic problem where you can give some direction to focus my research.

• Welcome! I don't think this question is really answerable. You've given a very vague description of a problem and you're asking for pretty detailed advice about what to do with it. We can't tell what data structures you need because we've no idea what data you're trying to store in them. – David Richerby Jan 23 '16 at 19:05
• Welcome to CS.SE! I'm afraid I don't understand what the problem is or what the question is. You've defined some concepts (shopping cart, validity), but haven't told us what you want to do with them. Are you looking for techniques to write code to check whether the current state of the cart is valid? to try to find a sequence of actions that will take it to a valid state? something else? It feels like maybe you wrote the background to your question but then forgot to write down what the question itself is. Can you edit the question to clarify what you are asking? – D.W. Jan 23 '16 at 22:45
• @D.W. and David. Sorry about that, and you're absolutely right. I'll try to edit and clarify – Bres Miloe Jan 23 '16 at 23:00
• Thanks for the edit. A small suggestion: rather than using "Stuff Edit: More stuff", we prefer that you edit the question to read coherently to someone who sees it for the first time (i.e., to be what it should have been from the start): we have revision history, so there's no need to mark changes. A more significant comment: What approaches have you considered, and why did you reject them? This doesn't seem a matter of data structures, or if it is, it's not apparent to me how (from the question). – D.W. Jan 23 '16 at 23:24
• Thanks for your helpful directions. I'll change and add more information per your suggestions. For the data structures part, I had initially tagged as decision-problem but David changed it to data-structures. Maybe it was misleading that I mentioned data structures in the question. – Bres Miloe Jan 24 '16 at 9:14