简体   繁体   English

将用户定义的逻辑存储在数据库中

[英]Storing user-defined logic in a database

I'm designing a database to store information about events that are dynamic in nature. 我正在设计一个数据库来存储有关动态事件的信息。 What I mean by this is that, each type of event will have some variables attached to them that changes on each occurrence based on some rules defined by the user. 我的意思是,每种事件类型都将附加一些变量,这些变量会根据用户定义的某些规则在每次发生时更改。

Let's say we have Event Type A with variable X and Y. In this event type, the user can define some rules that determines the value of X and Y on each occurrence of the event. 假设我们的事件类型A带有变量X和Y。在这种事件类型中,用户可以定义一些规则,这些规则确定每次事件发生时X和Y的值。

An example of a set of rules a user might define: 用户可能定义的一组规则的示例:

  • On first occurrence, X = 0; 第一次出现时,X = 0; Y = 0; Y = 0;
  • On each occurrence, X = X + 1; 每次出现时,X = X +1;
  • On each occurrence, if X == 100 then { X = 0; 每次出现时,如果X == 100,则{X = 0; Y = Y + 1 } Y = Y +1}

By defining these rules, the value of X and Y changes dynamically on all occurrences of the event as follow: 通过定义这些规则,X和Y的值将在事件的所有发生时动态更改,如下所示:

  • 1st occurrence: X = 1, Y = 0 第一次出现:X = 1,Y = 0
  • 2nd occurrence: X = 2, Y = 0 第二次出现:X = 2,Y = 0
  • ... ...
  • 100th occurrence: X = 0, Y = 1 第100次出现:X = 0,Y = 1

Now, I'm not sure how to store the "user-defined rules" in a database and later query them in my code. 现在,我不确定如何在数据库中存储“用户定义的规则”,然后在我的代码中查询它们。 Can anyone point me in the right direction? 谁能指出我正确的方向? Here's a start: 这是一个开始:

EVENTS
id;
name;
description;
event_type;

EVENT_TYPE_A_OCCURRENCES
id;
event_id;
X;
Y;

EVENT_RULES
id;
event_id;
frequency; // the frequency in which this rule applies
at_occurrence; // apply this rule at a specific occurrence
condition; // stores the code for the condition
statements; // stores the code for the statements

I'm no expert, please help me solve this problem. 我不是专家,请帮助我解决此问题。 Thank you. 谢谢。

Assume following user defined rules stored in table: 假设以下用户定义的规则存储在表中:

-----------------------------------------------------------------
|eventid|occurance|keep-old-x|keep-old-y|x-frequency|y-frequency|
-----------------------------------------------------------------
| A     | 1       | T        | F        | 1         | 100       |
-----------------------------------------------------------------
| B     | 2       | F        | T        | -2        | 0         |
-----------------------------------------------------------------
| C     | 5       | T        | T        | 100       | -3        |
-----------------------------------------------------------------

Lets say before event X = 10, Y = 12. Event = A, ocuuurance = 1, keep-old-x = T, keep-old-y = F, x-frequency = 1, y-frequency = 100 假设在事件X = 10,Y = 12之前。事件= A,阈值= 1,keep-old-x = T,keep-old-y = F,x频率= 1,y-frequency = 100

if keep-old-x is T then
        X = X + x-frequency
else
        X = x-frequency
endif

if keep-old-y is T then
        Y = Y + y-frequency
else
        Y = y-frequency
endif

Now, X = 11, Y = 100 现在,X = 11,Y = 100

You may need to add two more columns to change value of X variable on specific value; 您可能需要再添加两列,以将X变量的值更改为特定值; as: 如:

--------------------------
|if-x-value| x-new-value |
--------------------------
| 100      | 0           |
--------------------------
| 125      | 5           |
--------------------------
| 150      | 10          |
--------------------------

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在关系数据库中存储具有用户定义组件的实体 - Storing Entities with User-defined Components in Relational Database 数据库中的用户定义函数(PostgreSQL) - User-defined functions in Database (PostgreSQL) 数据库系统中用户定义的完整性规则示例? - Example of user-defined integrity rule in database systems? 支持用户定义的数据库字段的动态类 - Dynamic Class To Support User-Defined Database Fields 在数据库中存储业务逻辑 - Storing Business Logic in Database 在数据库中存储逻辑 - Storing logic inside database 用户管理:以用户定义的“组”,数据库架构和物流管理用户 - User Management: Managing users in user-defined “groups”, database schema and logistics 您将如何设计数据库以允许用户定义的架构 - How would you design your database to allow user-defined schema 将用户定义的表类型与生成的数据库部署脚本一起使用时出现问题 - Problem using user-defined table type with generated database deployment script 您将如何在SQL数据库中创建和存储用户定义的自定义字段? - How would you create and store user-defined custom fields in a SQL database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM