Disclosure of Invention
In order to solve the defects in the prior art, the invention aims to provide a method for realizing a business rule engine library system of an electric power marketing system, which aims to reduce code redundancy and simplify code logic for program coding, improve business rule arrangement flexibility, simplify product development, realize coding difficulty and the like, sort and optimize early-stage rule engine design results, and unify workflow engines and business rule engine codes into one engine design for realization.
The invention adopts the following technical scheme.
A method for realizing a business rule engine library system of a power marketing system comprises the following steps:
step 1, acquiring a service data object of a power marketing system and configuring a permission model;
step 2, designing a rule engine based on a Drools framework; classifying the rules in the rule base according to the service types, and designing and configuring interfaces of the rule base;
step 3, executing a rule engine;
and 4, packaging the components of the rule engine into each micro-service component to realize distributed deployment and independent operation and calling of the rule engine.
Preferably, in step 1, the service data of the power marketing system includes user information, service information and cost information;
the permission model is realized through an Oracle database, and the configuration permission model comprises a configuration permission parameter table, a role permission relation table, a personnel permission relation table and a permission application table.
Preferably, step 2 comprises:
step 2.1, designing a rule engine and a rule base based on a Drools framework, and classifying rules in the rule base according to business types;
2.2, designing an application program interface of the rule base by adopting JSR94 standard;
step 2.2, configuring the service data object in the rule base, namely configuring the Java object as a static service data object, and configuring an object generated by dynamic construction after acquiring data by calling interfaces of other micro services as a dynamic service data object;
step 2.3, configuring the service rule factors in the rule base, namely configuring the combination of the service data object and the logic judgment as service comparison rule factors, and configuring the service execution rule factors as the execution state of the service execution rule factors is completed when the state flags of all the mandatory required preposed service execution rule factors are completed;
and 2.4, configuring the execution scene text description, the business rule factors and the execution sequence of the business rules into the scene rules in the rule base through a front-end framework of the rule editor.
Preferably, step 3 comprises:
step 3.1, loading the rule set in the rule base into an engine actuator by using a rule loader, and analyzing each rule;
step 3.2, matching the business data object with the existing rules in the rule base, finding data which accord with the inference mechanism of the rule set from the business data object, and activating the successfully matched rules;
3.3, generating a rule execution instance by using the activated rule, and putting the instance into a rule schedule to be executed; if the activated rule has branches, putting the corresponding instances into a rule schedule according to the activation sequence;
step 3.4, executing the rule execution examples one by one according to the priority sequence in the rule schedule;
and 3.5, if the exception occurs in the execution process, ending the execution process according to a set return mode.
Further, the rule execution instance generated in step 3.3 includes: the rule execution instance for data generation conforming to the inference mechanism originally does not conform to the inference mechanism, but conforms to the rule execution instance for data generation conforming to the inference mechanism after the preamble rule is executed.
Further, step 3.3 also includes, revoking the failed rule execution instance from the rule execution queue; the failed rule execution instance is a rule execution instance of data generation which originally conforms to the inference mechanism but does not conform to the inference mechanism after the preamble rule is executed.
Further, in step 3.5, the set return mode includes: merging and actively throwing out, merging and not throwing out, unfixing and actively throwing out, unfixing and not throwing out;
merging means that output results of all business rule factors are output in a merged form, and the merge includes: all pass, part pass;
the throwing refers to throwing the executed result in a form of business exception when the execution result of the business rule factor is true, and interrupting the execution of the current scene.
Preferably, step 4 comprises:
step 4.1, packaging the components of the rule engine into each micro-service component to realize that the rule engine and the micro-service are loaded in a program package;
step 4.2, deploying the rule base on a cloud platform;
4.3, the workflow engine calls the microservice and transmits the business data object through a remote procedure call protocol;
step 4.4, the JVM memory of the microservice loads the business data object and calls a rule engine;
and 4.5, the rule engine calls a rule base deployed on the cloud platform through a remote procedure call protocol and executes a rule expression in combination with the service data object.
Compared with the prior art, the method has the advantages of obviously improving the code reuse rate, improving the channel operation efficiency, reducing unnecessary investment of manpower, material resources and the like, and saving more development and operation cost for enterprises.
Detailed Description
The present application is further described below with reference to the accompanying drawings. The following examples are only for illustrating the technical solutions of the present invention more clearly, and the protection scope of the present application is not limited thereby.
Referring to fig. 1, a business rules engine library system of a power marketing system includes: the system comprises a task micro-service, a workflow engine, a rule base, a rule engine API bus, a rule visual editor and a workflow visual editor;
the task micro-service is connected with the rule base through a rule engine API bus by a rule engine, and the rule visual editor is connected with the rule base through the rule engine API bus; the workflow visualization editor is connected with the workflow engine through a rule engine API bus, and the workflow engine is respectively connected with each task microservice through the rule engine API bus;
a rule engine and a task micro-service are loaded in a program package, a workflow engine calls the task micro-service and transmits service data, a memory of the micro-service loads the service data and calls a corresponding rule engine, and the rule engine calls a rule set in a rule base and executes a rule expression by combining the service data; wherein, the workflow engine and the rule engine are unified into one engine.
The task micro-service is used for completing a single task in the business process; each task microserver faces JVM memory object, service data object and service activity object.
In the preferred embodiment of the present invention, a JVM memory object refers to an object loaded into a memory by a microservice during operation, and mainly comes from input of a front-end interface and transmission data between the microservices; the JVM memory object mainly includes a service data object and a service activity object, the service data object refers to data generated in a service process, and the service activity object refers to operation records of a service, such as whether to confirm or not to pass, and the like.
And the workflow engine is used for driving the micro-services to run according to a set flow, transmitting business data among the task micro-services and controlling the running state of the micro-services based on a remote procedure call protocol.
In the preferred embodiment of the present invention, the workflow engine is a program that drives the workflow model to run according to a predetermined flow, and is responsible for information transfer and control between microservices. The functions of the method include arranging the calling relation of each micro service, transmitting information, monitoring the state of a workflow, providing a mechanism for processing service failure and the like. The workflow engine framework is realized by adopting a Flowable framework, and the cooperation relationship among the microservices is configured through an XML file. Each microservice has a unique id, which is used as a unique identifier in the XML file to operate the microservice. The Remote Procedure Call Protocol (RPC) calls are made between microservices using the FlowableRestfulAPI. An interface triggers an event to form a scene, and the interface transmits data in an interface service data object to the micro-service through an event interface; after receiving the data, splitting the data according to the service object; after the splitting is finished, executing interface calling according to an Api-Flows affair member program sequence; before the interface is called, mapping data in the corresponding service data object to a middle API (application programming interface), and realizing data transmission at the back end; and finally returning the result to the front-end interface after the Flow execution node returns. Because the Flowable self-contained flow establishing frame Modeler is compiled by Angular. js, and the transformation difficulty is high, the front-end interface adopts a bpmn. js frame, the flow is designed by a visual interface, xml is exported, and then the xml is transmitted to a flow engine to execute configuration. Once the process is started and executed, the object is used as a context parameter, each micro-service needs to realize an IPprocessor interface, a rollback () method is designed at the IPprocessor interface, and when an exception occurs in the processing process of one node of a task in the process, the service which needs to be called rolls back, so that an exception handling mechanism is realized.
The workflow model adopts a micro-service architecture, the realization of tasks and the cooperation relation of the tasks are separated, each task is independently deployed into one micro-service, and only the atomic logic code of the micro-service is realized. When the new logic needs to multiplex the existing task nodes, only the calling sequence of the microservice needs to be adjusted, and the existing codes do not need to be modified. The business arrangement of the workflow model adopts an Orestration mode, so a workflow engine is needed to provide a flow control service.
The workflow visualization editor is used for configuring the workflow; the workflow file exported from the workflow visualization editor is passed to the workflow engine for execution.
The rule visualization editor comprises a rule editor and a rule debugging tool; the rule editor is used for designing business rule factors and scene rules and writing the business rule factors and the scene rules into the rule base, the rule debugging tool is used for changing the existing business rule factors and the existing scene rules in the rule base, and the changing operation comprises the following steps: addition, deletion and modification.
In a preferred embodiment of the present invention, the rule visualization editor is a front-end program that visually combines rule logic and passes the combined result to the rule engine for execution. The rule visualization editor is implemented using a Guvnor framework.
And the rule base is deployed at the cloud end and used for storing the business rule factors and the scene rules.
The rule engine includes: the rule loader, the engine executor and the rule inference machine; the rule loader loads scene rules in the rule base into the engine actuator based on a remote procedure call protocol, and the rule inference machine responds to the engine actuator to perform rule inference;
the engine executor comprises a rule transaction processor and a rule transaction buffer; the rule transaction processor is used for obtaining the calculation result of the rule expression, and the rule transaction buffer is used for storing the rule to be executed after the pre-rule is completed.
In the preferred embodiment of the present invention, the rule event processing is performed by: the rule library, the rule engine API bus and the rule visualization editor component.
And the data which is input by user operation and generated by automatic system processing in a service scene needs to be subjected to service rule verification before the data falls into a database. The business requirements, data constraint rules and the like in the business operation process are extracted from the business logic, the business behavior is expressed by structured business rule data and logic expressions, the business rules are stored in a rule base in a centralized mode, the rules in the rule base can be created, updated and debugged in real time, and the complex logic relationship among the business rules is processed by a rule engine. And the rule engine is a program that combines rules in the business rule base into business decisions. The rule engine accepts data input, interprets and executes the business rules, and makes decision-making judgment according to the business rules.
In the preferred embodiment of the invention, the rule engine APIs bus is responsible for information transmission among the rule base, the rule engine, the rule visualization editor and the external program and is realized by adopting the RESTful API standard based on HTTP.
As shown in fig. 2, an implementation method of a business rule engine library system of a power marketing system includes:
step 1, acquiring a service data object of a power marketing system and configuring a permission model;
step 2, designing a rule engine based on a Drools framework; classifying the rules in the rule base according to the service types, and designing and configuring interfaces of the rule base;
step 3, executing a rule engine;
and 4, packaging the components of the rule engine into each micro-service component to realize distributed deployment and independent operation and calling of the rule engine.
Specifically, in step 1, the service data of the power marketing system includes user information, service information and cost information;
the permission model is realized through an Oracle database, and the configuration permission model comprises a configuration permission parameter table, a role permission relation table, a personnel permission relation table and a permission application table. The authority model comprises role authority and personnel authority, user positions, role and authority adjustment and configurability are realized, and the content and rule range of subsequent workflow are limited.
Specifically, step 2 comprises:
step 2.1, designing a rule engine and a rule base based on a Drools framework, and classifying rules in the rule base according to business types;
2.2, designing an application program interface of the rule base by adopting JSR94 standard;
step 2.2, configuring the service data object in the rule base, namely configuring the Java object as a static service data object, and configuring an object generated by dynamic construction after acquiring data by calling interfaces of other micro services as a dynamic service data object;
wherein, the business data object includes but is not limited to: user information, service information, fee information, etc., such as a personal mobile phone number, an identification number, contract capacity, and a charge amount.
Step 2.3, configuring the service rule factors in the rule base, namely configuring the combination of the service data object and the logic judgment as service comparison rule factors, and configuring the service execution rule factors as the execution state of the service execution rule factors is completed when the state flags of all the mandatory required preposed service execution rule factors are completed;
and 2.4, configuring the execution scene text description, the business rule factors and the execution sequence of the business rules into the scene rules in the rule base through a front-end framework of the rule editor.
The step 3 comprises the following steps:
and 3.1, loading the rule set in the rule base into an engine executor by using a rule loader, and analyzing each rule.
And 3.2, matching the business data object with the existing rules in the rule base, finding data which accord with the inference mechanism of the rule set from the business data object, and activating the rules which are successfully matched.
The rule set can be understood as a scene rule, and is a set of all rules of a scene to be checked.
In the preferred embodiment of the present invention, for rule matching, the following is generally defined:
conditioning section, also known as LHS (left-hand side)
Fact part, also called RHS (right-hand side)
Assuming that there are N rules in the system, there are P patterns in the conditional part of each rule on average, and there are M facts to be processed at a certain point. The matter to be done for rule matching is: for each rule r, it is determined whether the current fact o satisfies lhs (r) = True, and if so, the instance r (o) of the rule r, i.e., rule + fact that satisfies the rule, is added to the conflict set to wait for processing.
The following procedure is generally adopted:
1. taking one r out of N rules;
2. taking a combination c of P facts from the M facts;
3. testing LHS (r) with c, and if LHS (r (c)) = True, adding RHS (r (c)) to the queue;
if there are M facts other combinations c, goto 3;
taking out the next rule r, goto 2;
3.3, generating a rule execution instance by using the activated rule, and putting the instance into a rule schedule to be executed; if the activated rule has branches, putting the corresponding instances into a rule schedule according to the activation sequence;
and 3.4, executing the rule execution examples one by one according to the priority sequence in the rule schedule.
Further, the rule execution instance generated in step 3.3 includes: the rule execution instance for data generation conforming to the inference mechanism originally does not conform to the inference mechanism, but conforms to the rule execution instance for data generation conforming to the inference mechanism after the preamble rule is executed.
Further, step 3.3 also includes, revoking the failed rule execution instance from the rule execution queue; the failed rule execution instance is a rule execution instance of data generation which originally conforms to the inference mechanism but does not conform to the inference mechanism after the preamble rule is executed.
And 3.5, if the exception occurs in the execution process, ending the execution process according to a set return mode.
Further, in step 3.5, the set return mode includes: merging and actively throwing out, merging and not throwing out, unfixing and actively throwing out, unfixing and not throwing out;
merging means that output results of all business rule factors are output in a merged form, and the merge includes: all pass, part pass;
the throwing refers to throwing the executed result in a form of business exception when the execution result of the business rule factor is true, and interrupting the execution of the current scene.
Specifically, step 4 includes:
step 4.1, packaging the components of the rule engine into each micro-service component to realize that the rule engine and the micro-service are loaded in a program package;
step 4.2, deploying the rule base on a cloud platform;
4.3, the workflow engine calls the microservice and transmits the business data object through a remote procedure call protocol;
step 4.4, the JVM memory of the microservice loads the business data object and calls a rule engine;
and 4.5, the rule engine calls a rule base deployed on the cloud platform through a remote procedure call protocol and executes a rule expression in combination with the service data object.
Example 1.
The high-voltage new-installation capacity increasing is that when a customer puts forward new installation electricity, increases the power consumption capacity agreed by contracts and finally the voltage grade reaches 10 (6) kV or above, the power supply enterprise establishes or changes an electricity customer file through the work of business acceptance, scheme establishment, measurement equipment assembly and disassembly, acceptance and power transmission and the like, and the business expansion and installation business developed by the customer for obtaining the electric power is realized.
When a business person operates the electric power marketing system, the authority model can firstly judge whether the business person and the role thereof have the authority of high-voltage new installation and capacity increase. The flow engine will then initiate the relevant microservice, for example: uploading client information, submitting client requirements, informing an application acceptance unit for transacting the call, and the like. And triggering a rule engine in each micro service, calling and executing relevant scene rules, and interrupting the flow if an error exists. For example, customer information upload microservices require verification of the system for customer profile information, ensuring one-to-one profiles. Verifying whether the arrearage is present before submitting the customer requirements, clearing the arrearage by the customer, and judging whether the incoming service application form is correct according to the customer requirements;
if the rule factor is added, the operation in the visual interface can be directly performed, for example, the client age is required to be more than 18 years old or the client score is required to be more than 1000, the user age and the user score of the business data object are selected, the judgment condition is respectively more than 18 and more than 1000, and the condition connection relationship is set as or. The rule factor may then be stored in a rule base, and the scene rules requiring the rule factor may all add the rule factor.
The present applicant has described and illustrated embodiments of the present invention in detail with reference to the accompanying drawings, but it should be understood by those skilled in the art that the above embodiments are merely preferred embodiments of the present invention, and the detailed description is only for the purpose of helping the reader to better understand the spirit of the present invention, and not for limiting the scope of the present invention, and on the contrary, any improvement or modification made based on the spirit of the present invention should fall within the scope of the present invention.