Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an appropriate data structure and database schema to store logic rules?

Preface: I don't have experience with rules engines, building rules, modeling rules, implementing data structures for rules, or whatnot. Therefore, I don't know what I'm doing or if what I attempted below is way off base.

I'm trying to figure out how to store and process the following hypothetical scenario. To simplify my problem, say that I have a type of game where a user purchases an object, where there could be 1000's of possible objects, and the objects must be purchased in a specified sequence and only in certain groups. For example, say I'm the user and I want to purchase object F. Before I can purchase object F, I must have previously purchased object A OR (B AND C). I cannot buy F and A at the same time, nor F and B,C. They must be in the sequence the rule specifies. A first, then F later. Or, B,C first, then F later. I'm not concerned right now with the span of time between purchases, or any other characteristics of the user, just that they are the correct sequence for now.

What is the best way to store this information for potentially thousands of objects that allows me to read in the rules for the object being purchased, and then check it against the user's previous purchase history?

I've attempted this, but I'm stuck at trying to implement the groupings such as A OR (B AND C). I would like to store the rules in a database where I have these tables:

 Objects
    (ID(int),Description(char)) 

ObjectPurchRules
    (ObjectID(int),ReqirementObjectID(int),OperatorRule(char),Sequence(int)) 

But obviously as you process through the results, without the grouping, you get the wrong answer. I would like to avoid excessive string parsing if possible :). One object could have an unknown number of previous required purchases. SQL or psuedocode snippets for processing the rules would be appreciated. :)

like image 539
Jayson Avatar asked Dec 14 '09 19:12

Jayson


People also ask

What is a database logical schema?

A logical schema can be defined as the design of the database at its logical level. In this level, the programmers, as well as the database administrator (DBA), work. At this level, data can be described as certain types of data records that can be stored in the form of data structures.

What are the 3 types of database schema?

Schema is of three types: Logical Schema, Physical Schema and view Schema.

What is an example of a database schema?

There are mainly two tools you'd use to create a database schema. The first is an RDBMS, or a relational database management system. Examples of RDBMSs include SQL Server, MySQL, and PostgreSQL. The second and most important tool is SQL (structured query language).

What is schema data structure?

A data schema is a diagrammatic representation of the data structure. A data schema is simply a type of data structure. A data structure is a representation of the arrangement, relationships, and contents of data in an organization's data resource.


1 Answers

It seems like your problem breaks down to testing whether a particular condition has been satisfied.

You will have compound conditions. So given a table of items:

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 construct a table of conditions:

ID_Condition  VerbA  ObjectA_ID  Boolean  VerbB            ObjectB_ID
---------------------------------------------------------------------
1             OWNS   1           OR       MEETS_CONDITION  2
2             OWNS   2           AND      OWNS             3

So OWNS means the id is a key to the Items table, and MEETS_CONDITION means that the id is a key to the Conditions table.

This isn't meant to restrict you. You can add other tables with quests or whatever, and add extra verbs to tell you where to look. Or, just put quests into your Items table when you complete them, and then interpret a completed quest as owning a particular badge. Then you can handle both items and quests with the same code.

like image 158
John Avatar answered Oct 19 '22 15:10

John