Preface: I do not have knowledge about rules engines, building rules, modeling rules, applying data structures for rules, or whatnot. Therefore, I'm not sure what I am doing or maybe things i attempted below is much off base.

I am trying to puzzle out how you can store and process the next hypothetical scenario. To simplify my problem, state that I've got a kind of game in which a user purchases an item, where there might be thousands of possible objects, and also the objects should be bought inside a specified sequence and just in a few groups. For instance, say I am the consumer and I wish to purchase object F. Before I'm able to purchase object F, I have to have formerly bought object A OR (B AND C). I am unable to buy F along with a simultaneously, nor F and B,C. They ought to be within the sequence the rule identifies. An initial, then F later. Or, B,C first, then F later. I am not concerned at this time using the length of time between purchases, or other qualities from the user, just that they're the right sequence for the time being.

What's the easiest method to store these details for potentially 1000's of objects that enables me to see within the rules for that object being bought, after which check it from the user's previous purchase history?

I have attempted this, but I am stuck at attempting to implement the types like a OR (B AND C). I must keep rules inside a database where I've these tables:



But clearly while you process with the results, with no grouping, you receive the incorrect answer. I must avoid excessive string parsing if at all possible :). One object might have a mystery quantity of previous needed purchases. SQL or psuedocode clips for processing the guidelines could be appreciated. :)

This can be a very complex problem that I am untrained to reply to, but I have seen plenty of references to. The essential issue is that for games, quests and products and "stats" for a number of objects might have non-relational dependencies. This thread may help you a lot.

You might like to get a few books around the subject, and consider using LUA like a rules processor.

It appears much like your problem stops working to testing whether a specific condition continues to be satisfied.

You'll have compound conditions. So given a table of products:

ID_Item    Description


1          A

2          B

3          C

4          F

and given a table of possible actions:

ID_Action  VerbID  ItemID    ConditionID


1          BUY     4         1

We create a table of conditions:

ID_Condition  VerbA  ObjectA_ID  Boolean  VerbB            ObjectB_ID


1             Is the owner of   1           OR       MEETS_CONDITION  2

2             Is the owner of   2           AND      Is the owner of             3

So Is the owner of means the id is really a answer to the Products table, and MEETS_CONDITION implies that the id is really a answer to the circumstances table.

This is not designed to restrict you. You can include other tables with quests or whatever, and add extra verbs to inform you where you can look. Or, just put quests to your Products table whenever you complete them, after which interpret a completed mission as possessing a specific badge. You'll be able to handle both products and quests with similar code.

Personally I'd do that in code, not in SQL. The items ought to be its very own class applying an interface (i.e. IItem). IItem might have a technique known as OkToPurchase that will determine if it's OK to buy that item. To achieve that, it might use a number of of an accumulation of rules (i.e. HasPreviouslyPurchased(x), CurrentlyOwns(x), etc.) that you could build.

The excellent factor is the fact that you can easily extend this method with new rules having to break all of the existing logic.

Here's some pseudocode:

bool OkToPurchase()
   if( HasPreviouslyPurchased('x') && !CurrentlyOwns('y') )
       return true;
       return false;

bool HasPreviouslyPurchased( item )
    return purchases.contains( item )

bool CurrentlyOwns( item )
    return user.Items.contains( item )