CN115034392B - Compiling method and device of Rete network and executing method and device of Rete algorithm - Google Patents

Compiling method and device of Rete network and executing method and device of Rete algorithm Download PDF

Info

Publication number
CN115034392B
CN115034392B CN202210677672.0A CN202210677672A CN115034392B CN 115034392 B CN115034392 B CN 115034392B CN 202210677672 A CN202210677672 A CN 202210677672A CN 115034392 B CN115034392 B CN 115034392B
Authority
CN
China
Prior art keywords
node
input
double
attribute information
input connecting
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202210677672.0A
Other languages
Chinese (zh)
Other versions
CN115034392A (en
Inventor
陈永宏
张超
胡定贵
雷澍
尹华承
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Qingju Technology Co ltd
Original Assignee
Qingju Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Qingju Technology Co ltd filed Critical Qingju Technology Co ltd
Priority to CN202210677672.0A priority Critical patent/CN115034392B/en
Publication of CN115034392A publication Critical patent/CN115034392A/en
Application granted granted Critical
Publication of CN115034392B publication Critical patent/CN115034392B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N5/00Computing arrangements using knowledge-based models
    • G06N5/02Knowledge representation; Symbolic representation
    • G06N5/022Knowledge engineering; Knowledge acquisition
    • G06N5/025Extracting rules from data
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N5/00Computing arrangements using knowledge-based models
    • G06N5/04Inference or reasoning models
    • G06N5/046Forward inferencing; Production systems
    • G06N5/047Pattern matching networks; Rete networks

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • Data Mining & Analysis (AREA)
  • Evolutionary Computation (AREA)
  • Computational Linguistics (AREA)
  • Computing Systems (AREA)
  • General Physics & Mathematics (AREA)
  • Mathematical Physics (AREA)
  • Software Systems (AREA)
  • Artificial Intelligence (AREA)
  • Computer Vision & Pattern Recognition (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The disclosure relates to a method and a device for compiling a Rete network and a method and a device for executing a Rete algorithm, wherein the method for compiling the Rete network comprises the following steps: acquiring a rule to be added; the rule to be added comprises at least one double-input connecting word; the dual input connectives include either And Or; determining attribute information of the double input connecting words; the attribute information of the double-input connecting words is associated with the attribute information of two input objects connected by the double-input connecting words; acquiring attribute information of each node in a currently constructed Rete network; if the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, determining the node as the double-input node corresponding to the double-input connecting words; and if the attribute information of any existing node in the currently constructed Rete network is inconsistent with the attribute information of the double-input connecting words, generating the double-input node corresponding to the double-input connecting words. By adopting the technical scheme provided by the application, node redundancy can be effectively avoided.

Description

Compiling method and device of Rete network and executing method and device of Rete algorithm
Technical Field
The present disclosure relates to the field of data analysis technologies, and in particular, to a method and an apparatus for compiling a Rete network, and a method and an apparatus for executing a Rete algorithm.
Background
The Rete algorithm is an efficient pattern matching algorithm and is used for realizing a production rule system. Rete can be divided into two parts: rule compilation and runtime execution. Rule compiling refers to a process of generating a Rete network (inference network) according to a rule set, and run-time execution refers to a process of sending data to the inference network for screening.
When compiling Rete networks, it often occurs that different rules contain the same dual input connectives. For such a situation, in the existing technical scheme, a dual-input node corresponding to each dual-input connection word is created for each dual-input connection word according to each rule, and this way may cause redundancy of the dual-input node.
Disclosure of Invention
In order to solve the technical problems or at least partially solve the technical problems, the present disclosure provides a compiling method and device for a Rete network, and an execution method and device for a Rete algorithm.
In a first aspect, the present disclosure provides a method for compiling a Rete network, including:
acquiring a rule to be added; the rule to be added comprises at least one double-input connecting word; the dual-input connectives comprise either And Or;
determining attribute information of the double-input connecting words; the attribute information of the double-input connecting words is associated with the attribute information of two input objects connected by the double-input connecting words;
acquiring attribute information of each node in a currently constructed Rete network;
if the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, determining the node as the double-input node corresponding to the double-input connecting words;
and if the attribute information of any existing node in the currently constructed Rete network is inconsistent with the attribute information of the double-input connecting words, generating the double-input node corresponding to the double-input connecting words.
In a second aspect, the present disclosure also provides an execution method of a Rete algorithm, the execution method being applied to a Rete network compiled by using the compiling method of the Rete network as described above, the execution method including:
if the matching operation result of the current node is false, judging whether the next node is an Or node;
and if the next node is the Or node, continuing to execute the matching operation of the next node.
In a third aspect, the present disclosure further provides a compiling apparatus for a Rete network, including:
the first acquisition module is used for acquiring a rule to be added; the rule to be added comprises at least one double-input connecting word; the dual input connectives comprise either And Or
The determining module is used for determining attribute information of the double input connecting words; the attribute information of the double-input connecting words is associated with the attribute information of two input objects connected by the double-input connecting words;
the second acquisition module is used for acquiring the attribute information of each node in the currently constructed Rete network;
the judging module is used for judging whether the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words;
the creation module is used for determining a node as a double-input node corresponding to the double-input connecting word if the attribute information of the node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting word; and if the attribute information of any existing node in the currently constructed Rete network is inconsistent with the attribute information of the double-input connecting words, generating the double-input node corresponding to the double-input connecting words.
In a fourth aspect, the present disclosure also provides an apparatus for executing a Rete algorithm, where the apparatus is adapted to use a Rete network compiled by a compiling apparatus of the Rete network as described above, and the method includes:
the judging module is used for judging whether the next node is an Or node if the matching operation result of the current node is false;
and the execution module is used for continuously executing the matching operation of the next node if the next node is the Or node.
In a fifth aspect, the present disclosure also provides an electronic device, including: a processor and a memory;
the processor is configured to perform the steps of any of the methods described above by calling a program or instructions stored in the memory.
In a sixth aspect, the present disclosure also provides a computer readable storage medium storing a program or instructions for causing a computer to perform the steps of any of the above methods.
Compared with the prior art, the technical scheme provided by the embodiment of the disclosure has the following advantages:
in the technical scheme provided by the embodiment of the disclosure, if the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the double input connecting words, the node is determined as the double input node corresponding to the double input connecting words; and if the attribute information of any existing node in the currently constructed Rete network is inconsistent with the attribute information of the double-input connecting words, generating the double-input node corresponding to the double-input connecting words. The method is characterized in that whether the current Rete network comprises nodes corresponding to the dual-input connecting words needing to be processed currently is judged by means of attribute information, and the nodes corresponding to the dual-input connecting words are added only when the current Rete network does not comprise the nodes corresponding to the dual-input connecting words needing to be processed currently, so that node redundancy can be effectively avoided. In addition, since the judgment is performed by the attribute information, rather than the method of directly tracing the father node, the compiling efficiency of the Rete network can be improved.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the disclosure.
In order to more clearly illustrate the embodiments or technical solutions in the prior art of the present disclosure, the drawings used in the description of the embodiments or prior art will be briefly described below, and it is obvious for those skilled in the art that other drawings can be obtained according to the drawings without inventive exercise.
Fig. 1 is a flowchart of a compiling method of a Rete network according to an embodiment of the present disclosure;
fig. 2 and fig. 3 are schematic structural diagrams of a Rete network provided in an embodiment of the present disclosure;
fig. 4 is a flowchart of an execution method of a Rete algorithm provided in an embodiment of the present disclosure;
fig. 5 is a schematic structural diagram of a compiling device of a Rete network according to an embodiment of the present disclosure;
fig. 6 is a schematic structural diagram of an execution apparatus for Rete algorithm provided in the embodiment of the present disclosure;
fig. 7 is a schematic diagram of a hardware structure of an electronic device according to an embodiment of the present disclosure.
Detailed Description
In order that the above objects, features and advantages of the present disclosure may be more clearly understood, aspects of the present disclosure will be further described below. It should be noted that the embodiments and features of the embodiments of the present disclosure may be combined with each other without conflict.
In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present disclosure, but the present disclosure may be practiced in other ways than those described herein; it is to be understood that the embodiments disclosed in the specification are only a few embodiments of the present disclosure, and not all embodiments.
Fig. 1 is a flowchart of a compiling method of a Rete network according to an embodiment of the present disclosure. Referring to fig. 1, the method includes:
s110, acquiring a rule to be added; the rule to be added comprises at least one double-input connecting word; the dual input connectives include either And Or.
A Rule (Rule), an inference statement consisting of conditions and conclusions, is generally denoted as if \8230; then. Mode (Module), i.e. the conditional part of the IF statement. The condition portion of the IF statement can be decomposed into a plurality of atomic conditions connected by two input connectives, such as "AND (And)" Or "OR)". Illustratively, a rule is represented as: if Alpha1 And Alpha2, the Action3. Wherein the conditional part of the rule is Alpha1 And Alpha2. The rule includes two atomic conditions, alpha1 and Alpha2, respectively, with Action3 being the conclusion part of the rule.
It should be noted that in practice, the rules for constructing a Rete network may include only one atomic condition, which does not require the use of dual-input connectives. In other words, there is a case where a rule does not include a dual-input conjunction in practice. Since the technical solution of the present application is designed to reduce redundancy of the dual-input connectives, the present application mainly describes that a rule including the dual-input connectives is added to a Rete network.
S120, determining attribute information of the double input connecting words; and the attribute information of the double-input connecting words is associated with the attribute information of the two input objects connected by the double-input connecting words.
The attribute information of the double-input connecting words can reflect to a certain extent what atomic conditions are respectively of two input objects connected by the double-input connecting words.
There are various ways to implement this step, which should not be limited in this application. And in practice may be determined according to the specific content of the attribute information.
Illustratively, the to-join rule includes at least two atomic conditions; the implementation method of the step comprises the following steps:
firstly, determining prime numbers respectively corresponding to all atomic conditions in a rule to be added, and taking the prime numbers as attribute information of the atomic conditions corresponding to the prime numbers; the prime numbers corresponding to the same atomic condition are the same, and the prime numbers corresponding to different atomic conditions are different.
Prime numbers refer to natural numbers that have no other factors than 1 and itself, among natural numbers greater than 1.
When determining the prime numbers respectively corresponding to the atomic conditions in the rule to be added is executed, aiming at any atomic condition, if the atomic condition specifies the corresponding prime number before the current moment, the previously specified prime number is used when the step is executed; if the atomic condition does not specify its corresponding prime number before the current time, then a prime number is specified for it when this step is performed, and the specified prime number is different from any of the prime numbers previously specified for other atomic conditions.
Exemplarily, at the current time, the rule to be added includes the atomic condition a in the Rete network to which the rule m +1 is to be added, and at this time, m rules have been added to the Rete network, which are rule 1 to rule m respectively. If one or several of rules 1-rule m include atomic condition a, e.g. only rule m includes atomic condition a, since the atomic condition a is already assigned a prime number when rule m is added to the Rete network, the atomic condition a to be added to rule m +1 follows the previously assigned prime number when rule m +1 is added, i.e. the prime number assigned to atomic condition a when rule m is added. If the atomic condition a is not included in the rule 1-the rule m, the corresponding prime number is designated for the atomic condition a, and the prime number is different from the prime number corresponding to the atomic condition included in any one of the rule 1-the rule m.
Secondly, based on the prime numbers corresponding to the atomic conditions in the rule to be added, the prime numbers corresponding to two input objects connected by the double-input connecting words are respectively determined.
And thirdly, taking the product of the prime numbers of the two input objects connected by the double input connecting words as the attribute information of the double input connecting words.
The input object connected by the double-input connecting words can be an atomic condition, and other double-input connecting words can also be used.
Illustratively, in "If Alpha1 And Alpha2 And Alpha3, then Action4", according to the priority order of the operations, it can be determined that both input objects connected by the first And are atomic conditions, respectively Alpha1 And Alpha2. The second And connected two input objects, one is the other two-input connectives (i.e., the first And) And the other is the atomic condition (i.e., alpha 3). The first of the bad corresponds to prime equal to the product of the prime corresponding to Alpha1 And the prime corresponding to Alpha2. The second And prime numbers of an are equal to the product of the first And prime numbers of an And Alpha3.
And S130, acquiring attribute information of each node in the currently constructed Rete network.
When each node in the Rete network is generated, the attribute information corresponding to the node is recorded, so that the node can be directly read here.
Optionally, this step includes: and acquiring attribute information of each double input node in the currently constructed Rete network. By the arrangement, the calculation amount for judging whether the double input nodes need to be generated can be reduced, and the compiling efficiency of the Rete network is improved.
And S140, if the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, determining the node as the double-input node corresponding to the double-input connecting words.
And S150, if the attribute information of any existing node in the currently constructed Rete network is inconsistent with the attribute information of the double-input connecting words, generating the double-input node corresponding to the double-input connecting words.
Fig. 2 and fig. 3 are schematic structural diagrams of a Rete network provided in an embodiment of the present disclosure. In fig. 2, the Rete network is constructed based on three rules. The three rules are respectively: rule 1, if alpha1, then Action1; rule 2, if alpha2, then Action2; rule 3, if Alpha1 AND Alpha2, the Action3. Fig. 3 is a Rete network obtained by adding rule 4 to fig. 2.
Assume that the rules are added to the rete network in order of rule 1, rule 2, and rule 3. Before adding rule 1 to the Rete network, the Rete network only includes a root node and an Fact type node, the prime number corresponding to atomic condition Alpha1 is designated as 2, and a single input node Alpha1 corresponding to atomic condition Alpha1 is generated (the single input node is represented by a box in fig. 2), so that the attribute information of atomic condition Alpha1 and single input node Alpha1 is 2.
After adding rule 1 to the Rete network, the Rete network includes only the root node and type nodes and single input node Alpha1, and does not include single input node Alpha2 corresponding to atomic condition Alpha2, before adding rule 2 to the Rete network. If the prime number corresponding to atomic condition Alpha2 is designated as 3 and single input node Alpha2 corresponding to atomic condition Alpha2 is generated, the attribute information of atomic condition Alpha2 and single input node Alpha2 is 3.
After adding rule 2 to the Rete network, before adding rule 3 to the Rete network, the Rete network includes single-input node Alpha1 corresponding to atomic condition Alpha1 And single-input node Alpha2 corresponding to atomic condition Alpha2, does not include dual-input node ad 1 corresponding to dual-input connection word ad, and generates dual-input node ad 1 corresponding to dual-input connection word ad. Since the prime number corresponding to the atomic condition Alpha1 is 2, the prime number corresponding to the atomic condition Alpha2 is 3, and the product of 2 And 3 is used as the attribute information of the dual-input node ad 1, that is, the attribute information of ad 1 is 6.
The rule to be added is rule 4, denoted If Alpha1 And Alpha2 And Alpha3, the Action4. The rule to be added comprises two double-input connecting words And, according to the operation rule, two input objects of the first And are respectively atom condition Alpha1 And atom condition Alpha2. And the prime number corresponding to the atomic condition Alpha1 is designated to be 2, the prime number corresponding to the atomic condition Alpha2 is designated to be 3, the product of 2 And 3 is used as the attribute information of the first ant, namely the attribute information of the first ant is 6, the Rete network (namely the graph 2) added with the rules 1-3 is traversed, the attribute information of the existing dual-input node ant 1 is 6, and therefore the dual-input node ant 1 is determined to be the dual-input node corresponding to the first ant.
In rule 4, the two input objects of the second And are respectively the first And Alpha3, and it has been previously determined that the prime number corresponding to the first And is 6, the single input node Alpha3 corresponding to the atomic condition Alpha3 is not included in fig. 3. The prime number corresponding to atomic condition Alpha3 is designated as 5, and single input node Alpha3 corresponding to Alpha3 is generated, so that the attribute information of atomic condition Alpha3 and single input node Alpha3 is 5. Taking the product of 6 And 5 as the attribute information of the second And, namely, the attribute information of the second And is 30, traversing the Rete network (namely, fig. 2) added with rules 1-3, and storing no attribute information of 30 to two input nodes, thereby generating a two-input node And2 corresponding to the second And. The structure of the Rete network after rule 4 is added is shown in fig. 3.
In the technical scheme, if the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, the node is determined as the double-input node corresponding to the double-input connecting words; and if the attribute information of any existing node in the currently constructed Rete network is inconsistent with the attribute information of the double-input connecting words, generating the double-input node corresponding to the double-input connecting words. The method is characterized in that whether the current Rete network comprises nodes corresponding to the dual-input connecting words needing to be processed currently is judged by means of attribute information, and the nodes corresponding to the dual-input connecting words are added only when the current Rete network does not comprise the nodes corresponding to the dual-input connecting words needing to be processed currently, so that node redundancy can be effectively avoided. In addition, since the judgment is performed by the attribute information, rather than the method of directly tracing the father node, the compiling efficiency of the Rete network can be improved.
On the basis of the foregoing technical solutions, optionally, after generating a dual-input node corresponding to a dual-input connecting word, the method further includes: and taking the attribute information of the double input connecting words as the attribute information of the double input nodes corresponding to the double input connecting words. The set target is that when other rules are added subsequently, the attribute information of the double-input node generated at this time can be acquired conveniently.
On the basis of the above technical solutions, optionally, the rule to be added includes a first input object and a second input object connected by a dual input connective; before S140 and S150, the method further includes: and if the currently constructed Rete network comprises the node corresponding to the first input object and the node corresponding to the second input object, judging whether the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words.
The essence of the setting is that if the currently constructed Rete network includes a node corresponding to the first input object and a node corresponding to the second input object, it is determined whether a new dual-input node needs to be generated based on the attribute information. If the currently constructed Rete network only comprises the node corresponding to the first input object, not comprising the node corresponding to the second input object; or, the currently constructed Rete network only includes the node corresponding to the second input object, and does not include the node corresponding to the first input object; or, the currently constructed Rete network does not include the node corresponding to the second input object, and does not include the node corresponding to the first input object, and a new dual-input node is directly generated. The setting can further optimize the judgment step of generating new double input nodes, and improve the compiling efficiency of the Rete network. It should be noted that, this method is suitable for the case where, when a rule to be added is obtained, it is determined whether a dual input node needs to be generated first, and then a node corresponding to an original condition in the rule to be added is added.
On the basis of the above technical solutions, optionally, S140 is replaced with: if the attribute information of a first node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, the type of the first node is the same as that of the double-input connecting words, two father nodes of the first node correspond to two input objects of the double-input connecting words respectively, and the first node is determined to be the double-input node corresponding to the double-input connecting words; and if the attribute information of the first node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, but the types of the first node and the double-input connecting words are different, generating double-input nodes corresponding to the double-input connecting words. And if the attribute information of the first node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, but the two father nodes of the first node are not corresponding to the two input objects of the double-input connecting words, generating the double-input nodes corresponding to the double-input connecting words. Node redundancy can be further avoided in this way.
Fig. 4 is a flowchart of an execution method of a Rete algorithm provided in an embodiment of the present disclosure, where the execution method is applied to a Rete network compiled by using the compiling method of the Rete network. Referring to fig. 4, the execution method includes:
s210, if the matching operation result of the current node is false, judging whether the next node is an Or node;
s220, if the next node is an Or node, continuing to execute the matching operation of the next node.
In the logical operation, one of two atomic conditions of Or connection is true, and the matching operation result is true; the result of the matching operation is false only if both atomic conditions are false at the same time. Therefore, if the matching operation result of the current node is false, if the next node is the Or node and the matching operation result of another input node connected to the next node is true, the matching operation of the Or node and the nodes following the Or node still have to be performed.
According to the technical scheme, if the matching operation result of the current node is false, whether the next node is an Or node is judged; if the next node is an Or node, the matching operation of the next node is continuously executed, so that the matching operation of the Or node can be normally executed, and logic errors are avoided.
On the basis of the above embodiment, optionally, if the next node is a non-Or node, the next node and the matching operation of the nodes after the next node are stopped. non-Or nodes include single input nodes (e.g., alpha nodes), as well as add nodes.
The rule compiling process and the runtime execution process of the Rete algorithm are explained in detail below.
Rule compilation process
First, a root node is generated.
Secondly, traversing all facts (Fact), determining the types of all the facts, and generating a Fact type node for each type, wherein the Fact type node is a child node of the root node. Wherein facts are multivariate relationships between objects and between object attributes
And traversing all rules needing to be added, and generating corresponding single-input nodes and/or double-input nodes and action nodes of the rules.
If a rule only has one atomic condition, checking whether a corresponding single input node (namely, alpha node) exists under a corresponding Fact type node, and if not, generating the corresponding single input node (namely, alpha node) for the single input node.
If a certain rule has a plurality of atomic conditions, checking whether a single input node (namely Alpha node) corresponding to each atomic condition exists under a corresponding Fact type node, and if not, generating a corresponding single input node (namely Alpha node) for the single input node; and judging whether a dual-input node corresponding to the dual-input connecting word in the rule needs to be created or not according to the attribute information, and if so, generating a corresponding dual-input node for the dual-input node.
The dual-input nodes may be an And node Or an Or node, and in practice, the dual-input nodes need to be consistent with the dual-input connecting words used in the rules. Illustratively, if the dual-input connectives used in the rule are Add, the dual-input nodes generated for it are Add nodes. If the dual-input conjunction word used in the rule is Or, the dual-input node generated for the rule is an Or node.
Any input node of the dual-input nodes (including the Ant node And the Or node) can be other Ant nodes, or nodes Or Alpha nodes.
The Action of a rule is an Action type node.
When rules are added to the Rete network, index attributes are set for each Alpha node, and node And Or node. Specifically, each Alpha node is assigned a prime number and stored as an index attribute, and the prime numbers assigned to the Alpha nodes are unequal. When the Add node (Or the Or node) is compiled, the product of the parent node index attributes is calculated And stored as the index attributes of the Add node (Or the Or node).
If a new rule comprising a plurality of atomic conditions of the connection of the nd (Or) is required to be added, determining the index attribute of the nd (Or) in the rule, and if the existing nodes in the Rete network graph do not have the same index attribute, generating a new nd node (Or node); if the nodes with the same index attribute exist in the Rete network graph, the types of the nodes And the parent nodes are recursively compared, and if the types of the nodes are different And/Or the parent nodes are different, a new And node (Or node) is generated. Otherwise, the old And node (Or node) is used.
Optionally, in practice, in the process of compiling the rule, after obtaining a rule to be added, modifying the condition in the rule according to the index attribute of the currently constructed Alpha node and the logic operation rule, so that the modified rule includes the currently constructed double-input node in the Rete network; and adding the revised rule into the Rete network. Exemplarily, assume that on the basis of fig. 2, the rule to be added is rule 5, denoted If Alpha2 AndAlpha3 And Alpha1, the Action4. Rule 5 is modified to rule 4, denoted If Alpha1 And Alpha2 And Alpha3, the Action4. One And node can be reduced, and the purpose of further reducing redundancy is achieved.
The Rete network graph is a directed graph. Since the number of nodes and the number of connections are not known before compilation. The Rete network graph is saved using a linked list structure. In the linked list, each node points to its parent and child nodes. Specifically, the root node has no parent node (is empty). The Fact type node, the Alpha type node, the AND type node, AND the Or type node all have a parent node (not null), AND a child node (not null). Wherein the node of type either an Or has two parents. Both the Fact type node and the Alpha node have only one father node. An Action type node has no child nodes (is null).
Further, in order to increase the processing speed, the number of child nodes of each node is recorded in a linked list.
Runtime execution process
Three types of data structures are set, which are respectively:
ruleList: the executable Rule linked list composed of Rule is empty. .
And (4) making the factMap: the array type Map, key is a Fact type, and the initialization value is null according to each Fact initialization data.
nodeVals: the chain phenotype Map caches the calculation results of the Alpha, and Or nodes, and the key is the node.
And when the method is executed, according to the service characteristics, modifying the corresponding value in the factMap when every piece of Fact data is obtained.
From the Rete network, all next level nodes of the Fact type node (i.e., all atomic conditions under the Fact type node) are known. When the Fact data is acquired, whether all the atomic conditions (Alpha nodes) thereof match or not is calculated.
For nodes whose computation result is true (i.e., true), descendant child nodes are recursively judged. Specifically, if the Action node is an Action node, adding the Action node into the ruleList; if the node is an And node And the other father node has already calculated, saving the calculation result of the Add node to nodeVals; if it is an Or node, the calculation result of the Or node, tune, is saved to nodeVals.
For nodes whose computation result is False (i.e., false), descendant child nodes are recursively judged. Specifically, if the node is an And node, saving the calculation result False of the And node to nodeVals; if it is an Or node and another parent node has already been computed, save the value False of the Or node to nodeVals
Thus, all rules that can be executed will enter ruleList.
When all values of factMap are not null, the rules in ruleList are executed.
It should be noted that, for simplicity of description, the above-mentioned method embodiments are described as a series of acts or combination of acts, but those skilled in the art will recognize that the present invention is not limited by the order of acts, as some steps may occur in other orders or concurrently in accordance with the invention. Further, those skilled in the art should also appreciate that the embodiments described in the specification are preferred embodiments and that the acts and modules referred to are not necessarily required by the invention.
Fig. 5 is a schematic structural diagram of a compiling apparatus of a Rete network according to an embodiment of the present disclosure, and as shown in fig. 5, the apparatus includes:
a first obtaining module 310, configured to obtain a rule to be added; the rule to be added comprises at least one double-input connecting word; the dual input connectives comprise either And Or
A determining module 320, configured to determine attribute information of the dual-input connecting word; the attribute information of the double-input connecting words is associated with the attribute information of two input objects connected by the double-input connecting words;
a second obtaining module 330, configured to obtain attribute information of each node in a currently-constructed Rete network;
the judging module 340 is configured to judge whether attribute information of one node in a currently-constructed Rete network is consistent with attribute information of the dual-input connecting word;
a creating module 350, configured to determine a node as a dual-input node corresponding to the dual-input connecting word if attribute information of the node in a currently-constructed Rete network is consistent with attribute information of the dual-input connecting word; and if the attribute information of any existing node in the currently constructed Rete network is inconsistent with the attribute information of the double-input connecting words, generating the double-input node corresponding to the double-input connecting words.
Further, the device further comprises a configuration module, configured to, after the dual-input nodes corresponding to the dual-input connecting words are generated, take the attribute information of the dual-input connecting words as the attribute information of the dual-input nodes corresponding to the dual-input connecting words.
Further, the rule to be added comprises a first input object and a second input object which are connected by the double-input connecting words;
the device further comprises a judging module, which is used for judging whether the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words or not if the currently constructed Rete network comprises the node corresponding to the first input object and the node corresponding to the second input object.
Further, the rule to be added comprises at least two atomic conditions;
a determination module to:
determining prime numbers corresponding to the atomic conditions in the rule to be added respectively, and taking the prime numbers as attribute information of the atomic conditions corresponding to the prime numbers; the prime numbers corresponding to the same atomic conditions are the same, and the prime numbers corresponding to different atomic conditions are different;
respectively determining prime numbers corresponding to two input objects connected by the double-input connecting words based on the prime numbers corresponding to the atomic conditions in the rule to be added;
and taking the product of the prime numbers of the two input objects connected by the double input connecting words as the attribute information of the double input connecting words.
Further, a creating module 350 for:
if the attribute information of a first node in the currently constructed Rete network is consistent with the attribute information of the dual-input connecting words, the first node is the same as the dual-input connecting words in type, two father nodes of the first node correspond to two input objects of the dual-input connecting words respectively, and the first node is determined to be the dual-input node corresponding to the dual-input connecting words;
and if the attribute information of a first node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, but the types of the first node and the double-input connecting words are different, generating double-input nodes corresponding to the double-input connecting words.
And if the attribute information of a first node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, but two father nodes of the first node are not corresponding to the two input objects of the double-input connecting words, generating the double-input nodes corresponding to the double-input connecting words.
The device disclosed in the above embodiment can implement the processes of the methods disclosed in the above compiling method embodiments of Rete networks, and has the same or corresponding beneficial effects. To avoid repetition, further description is omitted here.
Fig. 6 is a schematic structural diagram of an execution apparatus for Rete algorithm provided in the embodiment of the present disclosure. The execution device is applicable to a Rete network compiled by a compiling device of the Rete network provided by the embodiment of the disclosure, and the execution device comprises:
a judging module 410, configured to judge whether a next node is an Or node if a matching operation result of the current node is false;
the executing module 420 is configured to continue to execute the matching operation of the next node if the next node is an Or node.
The device disclosed by the embodiment can realize the flow of the method disclosed by the execution method embodiment of each Rete algorithm, and has the same or corresponding beneficial effects. To avoid repetition, further description is omitted here.
Fig. 7 is a schematic diagram of a hardware structure of an electronic device according to an embodiment of the present disclosure, as shown in fig. 7, the electronic device may include a mobile phone, a PAD, and other intelligent terminals, and the electronic device includes:
one or more processors 301, one processor 301 being exemplified in fig. 7;
a memory 302;
the electronic device may further include: an input device 303 and an output device 304.
The processor 301, the memory 302, the input device 303 and the output device 304 in the electronic device may be connected by a bus or other means, and fig. 7 illustrates an example of connection by a bus.
The memory 302 is a non-transitory computer-readable storage medium, and can be used to store software programs, computer-executable programs, and modules, such as program instructions/modules corresponding to the compiling method of Rete network or the execution method of Rete algorithm in the embodiment of the present disclosure. The processor 301 executes various functional applications of the server and data processing, that is, a compiling method of a Rete network or an execution method of a Rete algorithm of the above-described method embodiments, by executing the software programs, instructions, and modules stored in the memory 302.
The memory 302 may include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required for at least one function; the storage data area may store data created according to use of the electronic device, and the like. Further, the memory 302 may include high speed random access memory, and may also include non-transitory memory, such as at least one magnetic disk storage device, flash memory device, or other non-transitory solid state storage device. In some embodiments, memory 302 optionally includes memory located remotely from processor 301, which may be connected to a terminal device via a network. Examples of such networks include, but are not limited to, the internet, intranets, local area networks, mobile communication networks, and combinations thereof.
The input device 303 may be used to receive input numeric or character information and generate signal inputs related to user settings and function control of the electronic device. The output means 304 may comprise a display device such as a display screen.
Embodiments of the present disclosure also provide a computer-readable storage medium containing a program or instructions, which when executed by a computer, is configured to perform a method for compiling a Rete network, the method including:
acquiring a rule to be added; the rule to be added comprises at least one double-input connecting word; the dual-input connectives comprise either And Or;
determining attribute information of the double-input connecting words; the attribute information of the double input connecting words is associated with the attribute information of two input objects connected by the double input connecting words;
acquiring attribute information of each node in a currently constructed Rete network;
if the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, determining the node as the double-input node corresponding to the double-input connecting words;
and if the attribute information of any existing node in the currently constructed Rete network is inconsistent with the attribute information of the double-input connecting words, generating the double-input node corresponding to the double-input connecting words.
Embodiments of the present disclosure also provide a computer-readable storage medium containing a program or instructions for executing a method for performing a Rete algorithm, the method including:
if the matching operation result of the current node is false, judging whether the next node is an Or node;
and if the next node is the Or node, continuing to execute the matching operation of the next node.
Optionally, the computer executable instruction, when executed by the computer processor, may be further used to execute a technical solution of a compiling method of a Rete network or an execution method of a Rete algorithm provided in any embodiment of the present disclosure.
From the above description of the embodiments, it is obvious for a person skilled in the art that the present disclosure can be implemented by software and necessary general hardware, and certainly can be implemented by hardware, but in many cases, the former is a better embodiment. Based on such understanding, the technical solutions of the present disclosure may be embodied in the form of a software product, which may be stored in a computer-readable storage medium, such as a floppy disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a FLASH Memory (FLASH), a hard disk or an optical disk of a computer, and includes several instructions for enabling a computer device (which may be a personal computer, a server, or a network device) to execute the methods according to the embodiments of the present disclosure.
It is noted that, in this document, relational terms such as "first" and "second," and the like, may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrases "comprising a," "8230," "8230," or "comprising" does not exclude the presence of additional like elements in a process, method, article, or apparatus that comprises the element.
The foregoing are merely exemplary embodiments of the present disclosure, which enable those skilled in the art to understand or practice the present disclosure. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the disclosure. Thus, the present disclosure is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims (8)

1. A compiling method of a Rete network, comprising:
acquiring a rule to be added; the rule to be added comprises at least one double-input connecting word; the dual-input connectives comprise either And Or;
determining attribute information of the double-input connecting words; the attribute information of the double-input connecting words is associated with the attribute information of two input objects connected by the double-input connecting words;
acquiring attribute information of each node in a currently constructed Rete network;
if the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, determining the node as the double-input node corresponding to the double-input connecting words;
if the attribute information of any existing node in the currently constructed Rete network is inconsistent with the attribute information of the double-input connecting words, generating double-input nodes corresponding to the double-input connecting words;
the rule to be added comprises at least two atomic conditions;
the determining the attribute information of the dual-input connecting words comprises the following steps:
determining prime numbers corresponding to the atomic conditions in the rule to be added respectively, and taking the prime numbers as attribute information of the atomic conditions corresponding to the prime numbers; the prime numbers corresponding to the same atomic conditions are the same, and the prime numbers corresponding to different atomic conditions are different;
respectively determining prime numbers corresponding to two input objects connected by the double-input connecting words based on the prime numbers corresponding to the atomic conditions in the rule to be added;
taking the product of prime numbers of two input objects connected by the double input connecting words as the attribute information of the double input connecting words;
in the Rete network, attribute information of a double-input node is equal to attribute information of a double-input connecting word for generating the double-input node;
if the attribute information of a node in the currently constructed Rete network is consistent with the attribute information of the dual-input connecting words, determining the node as the dual-input node corresponding to the dual-input connecting words, and the method comprises the following steps:
if the attribute information of a first node in the currently constructed Rete network is consistent with the attribute information of the dual-input connecting words, the first node is the same as the dual-input connecting words in type, two father nodes of the first node correspond to two input objects of the dual-input connecting words respectively, and the first node is determined to be the dual-input node corresponding to the dual-input connecting words;
if the attribute information of a first node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, but the types of the first node and the double-input connecting words are different, generating double-input nodes corresponding to the double-input connecting words;
and if the attribute information of the first node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, but the two father nodes of the first node are not corresponding to the two input objects of the double-input connecting words, generating the double-input nodes corresponding to the double-input connecting words.
2. The compiling method of claim 1 wherein, after generating the dual-input node corresponding to the dual-input connectives, further comprising:
and taking the attribute information of the double-input connecting words as the attribute information of the double-input nodes corresponding to the double-input connecting words.
3. The compiling method of claim 1 wherein the rules to be added comprise a first input object and a second input object connected by the dual-input connectives;
if the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the dual-input connecting words, determining the node as being before the dual-input node corresponding to the dual-input connecting words, and the method comprises the following steps:
and if the currently constructed Rete network comprises the node corresponding to the first input object and the node corresponding to the second input object, judging whether the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words.
4. An execution method of a Rete algorithm, wherein the execution method is applied to a Rete network compiled by the compiling method of the Rete network according to any one of claims 1 to 3, and the execution method comprises:
if the matching operation result of the current node is false, judging whether the next node is an Or node;
and if the next node is the Or node, continuing to execute the matching operation of the next node.
5. A compiling apparatus of a Rete network, characterized by comprising:
the first acquisition module is used for acquiring a rule to be added; the rule to be added comprises at least one double-input connecting word; the dual input connectives comprise either And Or
The determining module is used for determining attribute information of the double input connecting words; the attribute information of the double input connecting words is associated with the attribute information of two input objects connected by the double input connecting words;
the second acquisition module is used for acquiring the attribute information of each node in the currently constructed Rete network;
the judging module is used for judging whether the attribute information of one node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words;
the creation module is used for determining a node as a double-input node corresponding to the double-input connecting word if the attribute information of the node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting word; if the attribute information of any existing node in the currently constructed Rete network is inconsistent with the attribute information of the double-input connecting words, generating double-input nodes corresponding to the double-input connecting words;
the rule to be added comprises at least two atomic conditions;
a determination module to:
determining prime numbers corresponding to the atomic conditions in the rule to be added respectively, and taking the prime numbers as attribute information of the atomic conditions corresponding to the prime numbers; the prime numbers corresponding to the same atomic conditions are the same, and the prime numbers corresponding to different atomic conditions are different;
respectively determining prime numbers corresponding to two input objects connected by the double-input connecting words based on the prime numbers corresponding to the atomic conditions in the rule to be added;
taking the product of prime numbers of two input objects connected by the double input connecting words as the attribute information of the double input connecting words;
in the Rete network, attribute information of a double-input node is equal to attribute information of a double-input connecting word for generating the double-input node;
a creation module to:
if the attribute information of a first node in the currently constructed Rete network is consistent with the attribute information of the dual-input connecting words, the first node is the same as the dual-input connecting words in type, two father nodes of the first node correspond to two input objects of the dual-input connecting words respectively, and the first node is determined to be the dual-input node corresponding to the dual-input connecting words;
if the attribute information of a first node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, but the types of the first node and the double-input connecting words are different, generating double-input nodes corresponding to the double-input connecting words;
and if the attribute information of the first node in the currently constructed Rete network is consistent with the attribute information of the double-input connecting words, but the two father nodes of the first node are not corresponding to the two input objects of the double-input connecting words, generating the double-input nodes corresponding to the double-input connecting words.
6. An execution device of Rete algorithm, wherein the execution device is adapted to a Rete network compiled by a compiling device of the Rete network according to claim 5, and the execution device comprises:
the judging module is used for judging whether the next node is an Or node if the matching operation result of the current node is false;
and the execution module is used for continuously executing the matching operation of the next node if the next node is the Or node.
7. An electronic device, comprising: a processor and a memory;
the processor is adapted to perform the steps of the method of any one of claims 1 to 4 by calling a program or instructions stored in the memory.
8. A computer-readable storage medium, characterized in that it stores a program or instructions for causing a computer to carry out the steps of the method according to any one of claims 1 to 4.
CN202210677672.0A 2022-06-15 2022-06-15 Compiling method and device of Rete network and executing method and device of Rete algorithm Active CN115034392B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210677672.0A CN115034392B (en) 2022-06-15 2022-06-15 Compiling method and device of Rete network and executing method and device of Rete algorithm

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210677672.0A CN115034392B (en) 2022-06-15 2022-06-15 Compiling method and device of Rete network and executing method and device of Rete algorithm

Publications (2)

Publication Number Publication Date
CN115034392A CN115034392A (en) 2022-09-09
CN115034392B true CN115034392B (en) 2023-04-07

Family

ID=83125818

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210677672.0A Active CN115034392B (en) 2022-06-15 2022-06-15 Compiling method and device of Rete network and executing method and device of Rete algorithm

Country Status (1)

Country Link
CN (1) CN115034392B (en)

Family Cites Families (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20090248601A1 (en) * 2008-03-31 2009-10-01 International Business Machines Corporation Exploiting double resolutions for proof optimizations
GB2509539A (en) * 2013-01-08 2014-07-09 Ibm Production rule engine
CN105787563B (en) * 2014-12-18 2018-12-21 中国科学院沈阳自动化研究所 A kind of Rapid matching fuzzy reasoning method based on self-study mechanism
CN105095969B (en) * 2015-09-25 2018-08-17 沈阳农业大学 The device of autonomous learning model towards shared knowledge
WO2019028269A2 (en) * 2017-08-02 2019-02-07 Strong Force Iot Portfolio 2016, Llc Methods and systems for detection in an industrial internet of things data collection environment with large data sets

Also Published As

Publication number Publication date
CN115034392A (en) 2022-09-09

Similar Documents

Publication Publication Date Title
CN112101562A (en) Method and system for realizing machine learning modeling process
CN113946321B (en) Processing method of computing logic, electronic device and readable storage medium
US20230334292A1 (en) Node fusion method for computational graph and device
CN110941655B (en) Data format conversion method and device
CA3050159A1 (en) Artificial intelligence (ai) based automatic rule generation
CN111522806A (en) Big data cleaning processing method and device, server and readable storage medium
CN115202669A (en) Project construction method and system based on configuration file and related equipment
US20230394330A1 (en) A method and system for designing ai modeling processes based on graph algorithms
CN116560661A (en) Code optimization method, device, equipment and storage medium
CN113407752B (en) Graph database memory management method, system, electronic device and storage medium
CN116204847A (en) Calculation graph optimization method, device and equipment
CN115034392B (en) Compiling method and device of Rete network and executing method and device of Rete algorithm
CN117149770A (en) Method and device for executing storage process of database and computer equipment
CN114791865A (en) Method, system and medium for detecting self-consistency of configuration items based on relational graph
CN114995869A (en) Configuration file analysis method, device, equipment and medium
CN113051080A (en) Computation graph execution method and device and heterogeneous platform
US20210034333A1 (en) Boolean multi-flow programming
US20240242139A1 (en) Data processing
Fall et al. Consistency preserving for evolving megamodels through axiomatic semantics
CN117076332B (en) Test case testing method and device, electronic equipment and storage medium
CN114637492A (en) Object-oriented service logic processing method and device
CN114637492B (en) Processing method and device of object-oriented business logic
CN113821224B (en) Method, system, equipment and storage medium for replacing environment variables
CN115344297A (en) Configuration data searching method, storage medium and electronic device
CN110795076B (en) Data processing method and device for realizing basic logic algorithm

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant