3

I'm building an app for android which allows objects to be merged together based on certain conditions. At the moment I'm looking to do this using if statements (nested or not) and am just wondering if there is a better way to do this so it is more flexible and easily expanded.

For example, I have 6 objects, 3 of them are red, 2 are blue and 1 is green.

The rules that can be applied to that are:

  • 3 red can be merged
  • 2 blue can be merged
  • 1 red and 1 blue and 1 green can be merged
  • 1 red and 1 blue can be merged
  • 1 red and 1 green can be merged
  • 1 green and 1 blue can be merged

That is a very basic example of it. There will be a lot more than that.

Nalum
  • 4,143
  • 5
  • 38
  • 55
  • Did you have a look at Jess (http://www.jessrules.com/doc/70/rete.html) already? – Dyonisos Sep 20 '11 at 11:10
  • Nope, didn't know about it. Thanks for the link will have a look – Nalum Sep 20 '11 at 11:11
  • Note that Jess is only free for academic use. A similar, open source rules engine written in Java is [Algernon-J](http://algernon-j.sourceforge.net/). – Daniel Trebbien Sep 20 '11 at 11:18
  • Thanks for that @DanielTrebbien. Non free is bad for me at the moment. Will take a look at both though to get a better understanding of rule systems. – Nalum Sep 20 '11 at 13:23

1 Answers1

2

I would probably do something in region of:

Create a Rule-class, that holds the logic for a given rule (your if-statements). A Rule should have a priority, so that it can be matched up against other rules to determine what rule should be considered first. It also needs a method to which an undetermined number of objects can be passed to, which checks if these objects fulfill the rule.

Create a RuleEngine, which holds a sorted list (by priority, highest first) of all rules. You then pass the objects you want to match towards the rules to the engine and the engine runs through them, breaking the loop if a rule is fulfilled:

for( Rule rule : rules ) {
  if( rule.check( objects ) ) {
    //Do what needs to be done
    break;
  }
}

I don't know if this makes sense to you - I hope it does.

Also if we're talking thousands of rules this solution is not optimal. In that case you would want to determine which rules the object might have a chance to fulfill before checking them (e.g. if you have two red and one green, there's no need to check the 'one-of-each'-rule).

kaspermoerch
  • 16,127
  • 4
  • 44
  • 67
  • Thanks for the answer, it does make sense. There is the possibility of there being hundreds of rules... maybe thousands (though, at the moment, I think unlikely). I'm going to leave the question open for a while to see if I get any more answers. +1 – Nalum Sep 20 '11 at 13:20
  • @KasperMoerch. Really nice and compact. But what if one or more rules get changed? You will have to recompile, retest and redeploy the entire app. I think, storing the logic that each rule holds in XML or even plain text would be a bit slower but more manageable. – Kizz Sep 24 '11 at 19:15
  • @Kizz That is very true and it shouldn't be that hard implementing it into my posted suggestion. – kaspermoerch Sep 26 '11 at 06:56