[英]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: 用户可能定义的一组规则的示例:
By defining these rules, the value of X and Y changes dynamically on all occurrences of the event as follow: 通过定义这些规则,X和Y的值将在事件的所有发生时动态更改,如下所示:
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.