A kind of rule matching method and device realizing rule-based engine
Technical field
The present invention relates to Data Matching technical field, be specifically related to a kind of rule matching method and the device that realize rule-based engine.
Background technology
Regulation engine is a kind of assembly be embedded in application program, and most of regulation engine all supports that the rule of simple scripting language realizes, and supports that the embedding of general development language is developed.Regulation engine achieves to be separated operational decision making from application code, and use predefined semantic modules to write operational decision making, application program is by submitting to data or object to carry out triggering rule engine to regulation engine, call corresponding rule by regulation engine, and then trigger corresponding operation.The operation that a usual rule comprises one group of rule condition and performs under this rule condition, the strictly all rules condition in this rule that and if only if is when all the match is successful, and regulation engine will trigger the lower all operations execution of this rule.
Therefore, when regular quantity is more, use prior art to carry out rule match, the strictly all rules condition for each rule needs that the match is successful, just can match desire rule.
At present, in a fairly large number of situation of rule, the method of the rule of needs coupling is found out by the strictly all rules condition of mating each rule, make to need to judge whether that the rule of mating is more in the process of searching the rule needing coupling, reduce the performance of regulation engine to a certain extent, make the efficiency of rule match lower.
Summary of the invention
In order to the inefficient problem of solution rule coupling, the invention provides a kind of rule matching method and the device that realize rule-based engine.
The invention provides a kind of rule matching method realizing rule-based engine, described method comprises,
Receive the event needing coupling, described event comprises at least one property value;
Judge whether to exist the index entry that arbitrary rule to be matched presets to mate with the property value in described event;
If so, then the rule to be matched of the target corresponding to index entry of mating is determined;
The rule condition of described target rule to be matched is mated with the property value in described event.
Preferably, described in judge whether to exist before index entry that arbitrary rule to be matched presets mates with the property value in described event, also comprise:
According to predetermined order rule, the index entry set that arbitrary described rule is preset is sorted;
According to the order of each index entry after sequence, the priority of each index entry in index entry set is set.
Preferably, the described index entry set default to arbitrary described rule according to predetermined order rule is sorted, and is specially;
The index entry of arbitrary relation of equality is set to the first priority;
By in described index entry set except being set to other index entries of the index entry of the first priority, sort according to preset order rule;
Accordingly, the described order according to each index entry after sequence, arranges the priority of each index entry in index entry set, is specially:
According to the order of each index entry after sequence, arrange in index entry set except being set to the priority of other index entries of the index entry of the first priority.
Preferably, described in judge whether to exist the index entry that arbitrary rule to be matched presets and mate with the property value in described event, be specially:
Suppose that the quantity of described rule to be matched is N, then the index entry quantity of the n-th rule to be matched is Mn, and described n is the natural number being not more than N;
If the index entry quantity Mn of the n-th rule equals 1, then judge that whether there is a property value in described event mates with an index entry of described n-th rule.
Preferably, described in judge whether to exist the index entry that arbitrary rule to be matched presets and mate with the property value in described event, be specially:
Suppose that described regular quantity to be matched is N, then the index entry quantity of the n-th rule to be matched is Mn, and described n is the natural number being not more than N;
If the index entry quantity Mn of the n-th rule is greater than 1, then judge that whether there is a property value in described event mates with the first priority index item of described n-th rule;
If, then according to the corresponding relation of the first priority index item and the second priority index item, determine the second priority index item of described n-th rule, and judge that whether there is a property value in described event mates with described second priority index item, the rest may be inferred, until judge whether that the property value existed in described event mates with the Mn priority index item of described n-th rule.
The present invention also provides a kind of rule match device realizing rule-based engine, and described device comprises:
Receiver module, for receiving the event needing coupling, described event comprises at least one property value;
Judge module, the index entry preset for judging whether to exist arbitrary rule to be matched mates with the property value in described event;
First determination module, for when the result of described judge module is for being, determines the rule to be matched of the target corresponding to index entry of mating;
Matching module, for mating with the property value in described event the rule condition of described target rule to be matched.
Preferably, described device also comprises:
Order module, for sorting to the index entry set that arbitrary described rule is preset according to predetermined order rule;
First arranges module, for the order according to each index entry after sequence, arranges the priority of each index entry in index entry set.
Preferably, described order module comprises:
Second arranges module, for the index entry of arbitrary relation of equality is set to the first priority;
Sorting sub-module, for by described index entry set except being set to other index entries of the index entry of the first priority, sort according to preset order rule;
Accordingly, described first arranges module, specifically for:
According to the order of each index entry after sequence in described sorting sub-module, arrange in index entry set except being set to the priority of other index entries of the index entry of the first priority.
Preferably, described judge module comprises:
Obtain submodule, be N for obtaining the quantity of described rule to be matched, and the index entry quantity of the n-th rule to be matched is Mn, described n is the natural number being not more than N;
First judges submodule, for when the index entry quantity Mn of the n-th rule equals 1, judges that whether there is a property value in described event mates with an index entry of described n-th rule.
Preferably, described judge module comprises:
Obtain submodule, be N for obtaining the quantity of described rule to be matched, and the index entry quantity of the n-th rule to be matched is Mn, described n is the natural number being not more than N;
Second judge module, for when the index entry quantity Mn of the n-th rule is greater than 1, judges that whether there is a property value in described event mates with the first priority index item of described n-th rule;
3rd judges submodule, when judging the result of submodule as being for described second, according to the corresponding relation of the first priority index item and the second priority index item, determine the second priority index item of described n-th rule, and judge that whether there is a property value in described event mates with described second priority index item, the rest may be inferred, until judge whether that the property value existed in described event mates with the Mn priority index item of described n-th rule.
Compared with prior art, first the present invention receives the event needing coupling, and described event comprises at least one property value; Judge whether to exist the index entry that arbitrary rule to be matched presets to mate with the property value in described event; If so, then the rule to be matched of the target corresponding to index entry of mating is determined; The rule condition of described target rule to be matched is mated with the property value in described event.The present invention utilizes index to carry out the screening of the rule needing coupling, makes when needs rule match, decreases the quantity of the rule needing coupling, improves the performance of regulation engine and the efficiency of rule match.
Accompanying drawing explanation
In order to be illustrated more clearly in the technical scheme in the embodiment of the present application, below the accompanying drawing used required in describing embodiment is briefly described, apparently, accompanying drawing in the following describes is only some embodiments of the application, for those of ordinary skill in the art, under the prerequisite not paying creative work, other accompanying drawing can also be obtained according to these accompanying drawings.
Fig. 1 is the process flow diagram realizing the rule matching method of rule-based engine of the embodiment of the present invention one;
Fig. 2 is the process flow diagram realizing the rule matching method of rule-based engine of the embodiment of the present invention two;
Fig. 3 is the structural drawing realizing the rule match device of rule-based engine of the embodiment of the present invention three;
Fig. 4 is the structure drawing of device of the order module 302 of the embodiment of the present invention three;
Fig. 5 is judge module 304 structural drawing when the index entry quantity of rule equals 1 of the embodiment of the present invention three;
Fig. 6 is judge module 304 structural drawing when the index entry quantity of rule is greater than 1 of the embodiment of the present invention three.
Embodiment
Below in conjunction with the accompanying drawing in the embodiment of the present application, be clearly and completely described the technical scheme in the embodiment of the present application, obviously, described embodiment is only some embodiments of the present application, instead of whole embodiments.Based on the embodiment in the application, those of ordinary skill in the art are not making the every other embodiment obtained under creative work prerequisite, all belong to the scope of the application's protection.
Embodiment one,
Be a kind of process flow diagram realizing the rule matching method embodiment one of rule-based engine provided by the invention with reference to figure 1, Fig. 1, the present embodiment specifically can comprise:
Step 101, reception need the event of coupling, and described event comprises at least one property value.
In this area, regulation engine is triggered by reception event, and then calls corresponding rule according to the event received and mate with event, if the match is successful, then triggers all operations that this rule that the match is successful can trigger.The present embodiment mainly solves the event how basis receives, in the list of rules storing strictly all rules, match the rule corresponding with this event.
In the present embodiment, first receive the event needing coupling, event can be understood as " operation of controlled member identification ", and said event can comprise a property value herein, also can comprise multiple property value, any one property value of described event can match with arbitrary rule condition of rule.Such as, any one event can be " red eye is bright ", and now " red eye the is bright " property value that is this event, can match with the rule condition of arbitrary rule; Any one event also can be " red eye is bright; greensignal light is also bright simultaneously ", wherein, " red eye is bright " " greensignal light is also bright " is respectively the property value of this event, and the property value of above-mentioned two events also can match with two of an arbitrary rule rule condition.
In practical operation, receive the mode needing match event a lot, belong to prior art, so do not repeat them here, receive the enforcement needing the mode of the event of coupling not affect the present embodiment simultaneously.
Step 102, judge whether to exist the index entry that arbitrary rule to be matched presets and mate with the property value in described event, if so, then enter step 103.
In the present embodiment, each rule to be matched comprises some rule conditions, such as, if " red eye is bright; greensignal light is also bright, then can trigger A program and automatically run " is a rule, wherein simultaneously, " red eye is bright " and " greensignal light is also bright " is respectively rule condition wherein, the operation be triggered when the rule condition that " triggering A program automatically to run " is this rule is all matched to merit.First in advance the rule condition meeting index entry setting model of each rule to be matched is set to index entry, such as, " red eye is bright " can be set to index entry.Secondly, the property value of the event received and the index entry of setting are matched, such as, if the arbitrary property value in the event received is " red eye is bright ", so this property value can the match is successful with " red eye is bright " this rule condition being set to index entry, in a word, if the property value in event can mate with all index entries set by any one rule, then step 103 is entered.
Concrete, index entry setting model can be the rule condition of " relation of equality ", such as, if certain rule rule condition is " age equals 20 ", then " age equals 20 " this rule condition can be set to the index entry of this rule.
In practical operation, each rule to be matched can arrange an index entry, also can arrange multiple index entry.
Concrete, when the index entry arranged according to the setting model of " relation of equality " is for " age equals 20 ", if a property value in the event received also is " age equals 20 ", so, " age equals 20 " as event attribute value can match with " age equals 20 " as index entry.
It should be noted that, in practical operation, first priority index item of usual rule index item is set to have " relation of equality " rule condition, and other priority index items except the first priority can be set to have the rule condition of " relation of equality " or " not etc. relation ".
If there is no the index entry that arbitrary rule to be matched is preset mates with the property value in described event, then the rule do not matched with the event received is described.
Step 103, determine mate the rule to be matched of the target corresponding to index entry.
In the present embodiment, all index entries are all treated a rule all by as target rule to be matched by what mate, such as, if a regular index entry is " age is greater than 18 ", the index entry of another rule is " age equals 20 " simultaneously, and a property value of the event received is " age is greater than 18 ", " age is greater than 18 " so as a property value of this event all can mate with " age is greater than 18 " and " age equals 20 " respectively as two regular index entries, illustrate by have matched all index entries above-mentioned two rules all by as target rule to be matched, for the further coupling of the rule condition of subsequent step.
Step 104, the rule condition of described target rule to be matched to be mated with the property value in described event.
In the present embodiment, the rule condition of rule to be matched for the target determined is mated with the property value in the event received.
In practical operation, the strictly all rules condition of rule to be matched for the target determined and all properties value in the event received can match by the present embodiment, if the match is successful completely, this target rule to be matched and the event matches success received are described, otherwise it fails to match.
In practical operation, other rule conditions except the index entry part that the match is successful of rule to be matched for target and other property values except the property value that the match is successful in the event received also can match by the present embodiment, if the match is successful, this target rule to be matched and the event matches success received then are described, otherwise it fails to match.Such as, suppose that a regular rule condition is for " red eye is bright " and " greensignal light is also bright ", if using above-mentioned " red eye is bright " this rule condition as index entry, when a property value of the event received also is " red eye is bright ", can determine above-mentioned rule by a property value as " red eye is bright " this rule condition of index entry and the event of reception, the match is successful, now want to reach the object that above-mentioned rule is matched to merit, only need other property values of the rule of " greensignal light the is also bright " index that the match is successful except " red eye is bright " and the event received that the match is successful.
First the present embodiment receives the event needing coupling, and described event comprises at least one property value, and next judges whether to exist the index entry that arbitrary rule to be matched presets and mates with the property value in described event; If so, then determine the rule to be matched of the target corresponding to index entry of mating, finally the rule condition of described target rule to be matched is mated with the property value in described event.Compared with prior art, the present embodiment utilizes index to carry out the screening of the rule needing coupling, makes when needs rule match, decreases the quantity of the rule needing coupling, improves the performance of regulation engine and the efficiency of rule match.
Embodiment two,
With reference to figure 2, Fig. 2 is a kind of process flow diagram realizing the rule matching method embodiment two of rule-based engine provided by the invention, because the step 201 in embodiment two and step 205 ~ 206 and the step 101 in embodiment one, step 103 ~ 104 are identical, so no longer repeat it in the present embodiment.
The present embodiment specifically can comprise:
Step 201, reception need the event of coupling, and described event comprises at least one property value.
Step 202, according to predetermined order rule to arbitrary described rule preset index entry set sort.
In the present embodiment, first the index entry of each rule is set, the index entry set using the index entry of each rule as its correspondence, next arranges the ordering rule sorted by index entry, finally, according to the ordering rule pre-set, index entry set corresponding for each rule is sorted.
Concrete, the clooating sequence pre-set can be that the rule index item of " equaling relation " is set to high priority.Such as, suppose that a regular rule condition is for " age equals 20 " and " height is greater than 170 ", the index entry setting model that can be then high priority according to " equaling relation ", is set to index entry by " age equals 20 " this rule condition " equaling relation ".
Step 203, according to the order of each index entry after sequence, the priority of each index entry in index entry set is set.
In the present embodiment, according to sorting to index entry set for the ordering rule sorted of pre-setting, according to the index entry order after sequence, priority setting is carried out to index entry.
Concrete, the index entry being arranged in the anterior locations of index entry order can be set to higher priority.Such as, a regular index entry set comprises A, B, C and D tetra-index entries, it is according to the order after the ordering rule sequence preset being B, C, A, D, and so priority orders is B > C > A > D.
Concrete, the B index entry in the index entry set of above-mentioned rule is the first priority index item, and the first priority index item can be the rule condition of " relation of equality ", introduces in detail, do not repeat them here in the step 102 in embodiment one.C, A and D index entry simultaneously in this index entry set be respectively second and third, level Four index entry, they can be the rule condition of " relation of equality ", also can be the rule condition of " unequal " relation, such as, if certain rule rule condition is " age is greater than 20 ", then also " age is greater than 20 " can be set to second and third or level Four index entry of this rule.
Step 204, judge whether to exist the index entry that arbitrary rule to be matched presets and mate with the property value in described event, if so, then enter step 205.
In the present embodiment, suppose that the quantity of rule to be matched is N, then the index entry quantity of the n-th rule to be matched is Mn, and this n is the natural number being not more than N;
If the index entry quantity Mn of the n-th rule equals 1, then judge that whether there is a property value in the event received mates with an index entry of this n-th rule, if existed, then enters step 205.
If the index entry quantity Mn of the n-th rule is greater than 1, then judge that whether there is a property value in the event received mates with the first priority index item of described n-th rule, if existed, then according to the corresponding relation of the first priority index item and the second priority index item, determine the second priority index item of this n-th rule, and judge that whether there is a property value in the event received mates with described second priority index item, the rest may be inferred, until judge whether that the property value existed in the event of reception mates with the Mn priority index item of described n-th rule.
It should be noted that N, n and Mn in the present embodiment all get natural number.
Below for the rule of n=5, Mn=2:
Reference table 1, table 2, table 1 is the 5th the regular index entry of the first priority and the corresponding table of the index entry of the second priority, and table 2 is the corresponding table that the index entry of the 5th regular the second priority and rule identify.
Suppose that the property value of the event that regulation engine receives is for " age equals 20 " " height is greater than 170 " and " undergraduate course educational background ".First, by the process of regulation engine, " age equals 20 " as the first priority can match with " age equals 20 " of a property value as the event received.Secondly, reference table 1, by the corresponding relation of the first priority index item and the second priority index item, determines the second priority index item corresponding with the first priority index item that the match is successful.Again, judge in the event received except with the first priority index item property value that the match is successful except other property values whether have and match mutually with the second priority index, " height the is greater than 170 " property value in the event that now regulation engine receives just in time is matched with the second priority index item " height is greater than 170 " of this rule.Finally, reference table 2, by the second priority index item and the corresponding relation that identifies of rule, determines that the rule corresponding with the second priority index item that the match is successful identifies, and then it is complete in the determination of rule of mating to be convenient in subsequent step.
Table 1
First priority index item |
Second priority index item |
Age equals 20 |
Height is greater than 170 |
Table 2
Second priority index item |
Rule mark |
Height is greater than 170 |
5th rule |
Wherein, only list and can embody thought of the present invention and can realize minimum data of the present invention, in actual applications, those skilled in the art can expand data according to the version of table 1 and table 2 in table 1 and table 2 for convenience's sake.
Also it should be noted that, relate to the example of concrete data in the embodiment of the present invention, all conveniently those skilled in the art understand the present invention and arrange, and these data can not be considered to being restriction of the present invention.
Step 205, determine mate the rule to be matched of the target corresponding to index entry.
Step 206, the rule condition of described target rule to be matched to be mated with the property value in described event.
Index entry sorts by the present embodiment, utilizes the index after sequence to carry out the screening of the rule needing coupling, makes when needs rule match, decrease the quantity of the rule needing coupling, improve the performance of regulation engine and the efficiency of rule match.
Embodiment three,
Be a kind of structure drawing of device realizing the rule match device embodiment three of rule-based engine provided by the invention with reference to figure 3, Fig. 3, the present embodiment specifically can comprise:
Receiver module 301, for receiving the event needing coupling, described event comprises at least one property value;
Order module 302, for sorting to the index entry set that arbitrary described rule is preset according to predetermined order rule;
With reference to the structural drawing that figure 4, Fig. 4 is order module 302, order module 302 specifically comprises:
Second arranges module 401, for the index entry of arbitrary relation of equality is set to the first priority;
Sorting sub-module 402, for by described index entry set except being set to other index entries of the index entry of the first priority, sort according to preset order rule;
First arranges module 303, for the order according to each index entry after sequence, arranges the priority of each index entry in index entry set.
When described order module 302 adopts the structure of Fig. 4 to realize, described first arranges module 303 for the order according to each index entry after sequence in described sorting sub-module 402, arranges in index entry set except being set to the priority of other index entries of the index entry of the first priority.
Judge module 304, the index entry preset for judging whether to exist arbitrary rule to be matched mates with the property value in described event;
Judge module 304 structural drawing when equaling 1 with reference to figure 5, Fig. 5 index entry quantity that is rule, described judge module 304 specifically comprises:
Obtaining submodule 501, be N, and the index entry quantity of the n-th rule to be matched is Mn for obtaining the quantity of described rule to be matched;
First judges submodule 502, for when the index entry quantity Mn of the n-th rule equals 1, judges that whether there is a property value in described event mates with an index entry of described n-th rule.
Judge module 304 structural drawing when being greater than 1 with reference to figure 6, Fig. 6 index entry quantity that is rule, described judge module 304 specifically comprises:
Obtaining submodule 501, be N, and the index entry quantity of the n-th rule to be matched is Mn for obtaining the quantity of described rule to be matched;
Second judge module 601, for when the index entry quantity Mn of the n-th rule is greater than 1, judges that whether there is a property value in described event mates with the first priority index item of described n-th rule;
3rd judges submodule 602, when judging the result of submodule as being for described second, according to the corresponding relation of the first priority index item and the second priority index item, determine the second priority index item of described n-th rule, and judge that whether there is a property value in described event mates with described second priority index item, the rest may be inferred, until judge whether that the property value existed in described event mates with the Mn priority index item of described n-th rule.
First determination module 305, for when the result of described judge module is for being, determines the rule to be matched of the target corresponding to index entry of mating;
Matching module 306, for mating with the property value in described event the rule condition of described target rule to be matched.
Index after the present embodiment utilizes sequence carries out the screening of the rule needing coupling, makes when needs rule match, decreases the quantity of the rule needing coupling, improves the performance of regulation engine and the efficiency of rule match.
For device embodiment, because it corresponds essentially to embodiment of the method, so relevant part illustrates see the part of embodiment of the method.Device embodiment described above is only schematic, the wherein said unit illustrated as separating component or can may not be and physically separates, parts as unit display can be or may not be physical location, namely can be positioned at a place, or also can be distributed in multiple network element.Some or all of module wherein can be selected according to the actual needs to realize the object of the present embodiment scheme.Those of ordinary skill in the art, when not paying creative work, are namely appreciated that and implement.
It should be noted that, in this article, the such as relational terms of first and second grades and so on is only used for an entity or operation to separate with another entity or operational zone, and not necessarily requires or imply the relation that there is any this reality between these entities or operation or sequentially.And, term " comprises ", " comprising " or its any other variant are intended to contain comprising of nonexcludability, thus make to comprise the process of a series of key element, method, article or equipment and not only comprise those key elements, but also comprise other key elements clearly do not listed, or also comprise by the intrinsic key element of this process, method, article or equipment.When not more restrictions, the key element limited by statement " comprising ... ", and be not precluded within process, method, article or the equipment comprising described key element and also there is other identical element.
The rule matching method realizing rule-based engine provided the embodiment of the present invention above and device are described in detail, apply specific case herein to set forth principle of the present invention and embodiment, the explanation of above embodiment just understands method of the present invention and core concept thereof for helping; Meanwhile, for one of ordinary skill in the art, according to thought of the present invention, all will change in specific embodiments and applications, in sum, this description should not be construed as limitation of the present invention.