📜 ⬆️ ⬇️

Rule Engine, or how to make the system easier

Good day to all!

In this article I will not touch on technical issues and will not give examples of code. This article is intended to give an idea of ​​what the Rule Engine is, what this thing is for and what it can do. If you are interested in this approach to building systems, then you can easily find the Rule Engine to your taste and color.

So, why this thing is needed. Take some kind of enterprise that lives in a very fast rhythm. For example, one of the largest airports, where every few minutes there is a landing or takeoff.
')

Questions and answers



Ask yourself questions:



The second question is simpler, so we will answer it first. The minimum price is a few tens of thousands of euros, but the maximum several hundred human lives. And now to the first question.

So who? Answer: in most cases, people. Not without the help of computers, of course, but still people. Now the question is: how? There is a list of possible options, the person chooses from them. There are not many options, so in principle there is no particular torment of choice. And the last question: on what basis? There are rules, they should be followed and, if possible, implemented. For example, the delay of departure for more than half an hour is highly undesirable. Well, the main question: and where does Rule Engine?

To answer this question, you need to answer another question, namely: why a person is offered only a few options if:

  1. You need to plan at least 3-4 hours ahead, and preferably at 8
  2. If the queue consists of about 200 aircraft (takeoff / landing)
  3. If the rules that need to be considered are several dozen
  4. If these rules change almost every day, and sometimes every hour
  5. If there are always emergency situations
  6. And much more...


The answer is: but because everything you just read is some kind of ideal situation that everyone would like to see, but which is greatly simplified, because in that case a person would simply not cope with the flow of information. Therefore, the number of rules that really pay attention to reduce at best to a dozen, and sometimes less. And plan for an hour or two ahead. So there are all sorts of unpleasant situations, and some of them become just chronic, such as the departure delay for half an hour. As a result, quite a lot of money is lost, because the planes do not fly as they should, but stand on the lanes and wait for what is not known. Passengers are nervous, the company's reputation suffers, it swears at the airport, because pays a very small amount of money for services and is entitled to rely on the appropriate quality of these. Of course, everyone has long been accustomed to and are well aware that the work of the airport is not easy, but you want the best, as always. Just at this moment Rule Engine comes to the rescue.

What kind of beast is this?



Suppose you have an object A, it has a numerical value and a boolean priority. There is a rule: if value> 5, then we set an object a priority, i.e. write it to the priority value true. Thus, if we have a bunch of objects, then by driving them through this rule, we determine which of them are important according to this rule. This will be the first stage of sorting. If at this stage we could not unambiguously determine that object A is more important than object B, then we need another rule, or we decide that objects are equivalent. It is the implementation of the rules of the Rule Engine that does. You give her rules, objects and ask which of the objects to the rules fit, which do not. The rule often and thickly looks like this:

when A.value>5 then A.priority=true 


This is how simple it is. As you understand, the rule can be as complex as you want / as it should.

What's the point?



And the point is that these rules are stored in a separate file, or in a database or somewhere else / like. If you need to change the rule, you do not need to touch your program, it is enough to change the rule itself and tell the Rule Engine that now the rules have changed. If a certain Rule Engine suddenly dislikes you (slows down for example), you can replace it without any problems with another.

What's the catch?



There are many moments that are sometimes not trivial. For example: we have two rules:

  when A.value>5 then A.value=A.value+5 


and

  when A.value<7 then A.value=A.value-3 


And there is an object whose value is 6. We give the Rule Engine this object and these rules, what do we get? Wrong! Why? Because you have to say in what order the rules apply, because the result depends on the order of execution. In order to tell the Rule Engine in which order it should apply the rules, it is necessary to prioritize them. If the two rules have the same priorities, then their order does not affect the result.

Or another question. How to handle the rules? All while there is at least one that runs? Then you can easily hang the system, because one rule will be to reduce something and the other is to increase. And can apply the rule exactly once? Or check the rule exactly once? Feel the difference?

I want blackjack and depraved women!



There is such a thing. Even a lot of things you enjoy.

Rules can be tested. Yes, yes, you do not want to bring down your system due to the fact that you wrote the wrong number in the rule?

Rules can be debagit. Cool, right? Here you have a Rule Engine gives an interesting result. Very interesting. But not the one you expect. What to do? Debug, what else! Put a breakpoint in the rule and go ahead.

Rules have versions. But what about the version system? What if it turns out that our new wonderful rule, which we successfully tested, brings us some losses, because we used to send gifts to childless women, and now we send their faces to an interesting sexual orientation? How to fix? We go to the system of versions, get out the old rule, look, who changed it, dismiss it nafig / deprive of the bonus / look carefully (but what about a peasant, right?

Check the rules for correctness / inconsistency. For example:

 when A.value>5 and A.value<3 then A.priority=true 


Guess what makes this rule? That's right, nothing! We remove this rule, because too much.
Rules can be clicked. In the sense of not as a disaster to click, but with a mouse to click. There is a visual rule editor. Now your secretary Masha can do interesting things with your system. The degree of your surprise depends only on the creativity of Masha.

But manager Fedya doesn’t like to click the rules, he’s a manager, he wants to steer the rules to write. Could be so. To do this, there is such a thing as DSL (Domain Specific Language). Now Fedya can write:

"If a client is a student, then he will not be given a loan."

Yes, Fedya is angry.

Well, at least one name then call it!



What, you want to know how such a miracle is called, where to look? Well, the number of Rule Engine is quite large. All that I have described is for example at JBoss Drools. Written in Java, free, famous. Recommend.

Did you use it yourself?



And then. I wrote a prototype system (described at the beginning of the article) for one very well-known airport as part of writing my bachelor’s work. It was interesting, I learned a lot of new things. To protect the work of the bachelor, in order not to speak abstractly, what I was doing there (because I did!), I wrote a simple program that several times a second generated an integer, wrote it down into a list, and sorted this list. I output the sorting stupidly to the console. At home I wrote a few rules, for example:



Etc. On defense, I taught these rules, then we quickly clicked another one in the editor, then I started the system and we played for several minutes sorting the list with numbers in any way, just changing the priorities of the rules (of course, the rules changed on the fly without stopping the system). It was fun.

And now, by tradition, I will voice to you the true purpose of this article. After this article there was a small movement, the results of which I will present to you in the form of a separate article, if you like this one.

Hopefully we'll see you again (still today).

Source: https://habr.com/ru/post/142622/


All Articles