Rule-based power marketing risk streaming code generation method and system
By decomposing SQL statements into streaming computing rule element models and generating streaming computing jobs without hard coding, the problems of high code coupling and high false alarm rate in power marketing risk management are solved, and the streaming computing system is made capable of rapid adaptation and self-correction.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- FUJIAN ELECTRIC POWER CO LTD XIAMEN ELECTRIC POWER SUPPLY CO
- Filing Date
- 2026-05-09
- Publication Date
- 2026-07-21
AI Technical Summary
When migrating from offline business to a streaming framework, the existing power marketing risk management system suffers from high code coupling, high development and maintenance costs, and is prone to generating a large number of false alarms during high-frequency real-time monitoring. It also lacks feature state evolution analysis of abnormal objects across time windows.
By decomposing the Structured Query Language (SQL) statement into a rule element model under the streaming computing framework, and abstracting business thresholds and judgment conditions into independent parameters, and combining preset templates to generate streaming computing jobs without hard coding, a cross-time window state evolution stability analysis and closed-loop feedback mechanism are introduced.
It enables the streaming computing system to quickly adapt to complex marketing scenarios and has self-correcting capabilities, reducing development and maintenance costs, improving the accuracy of anomaly identification and the scalability of the system, and reducing the false alarm rate.
Smart Images

Figure CN122431649A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of power system optimization technology, and specifically relates to a rule-based method and system for generating streaming codes for power marketing risks. Background Technology
[0002] In recent years, with the advancement of new power systems and digital grid construction, risk management in electricity marketing has gradually shifted from post-event verification to pre-event identification. Electricity marketing, as a crucial business area connecting power grid companies to the user side, encompasses multiple key aspects such as metering and data collection, electricity price execution, billing and volume settlement, and customer service. Its operation highly relies on the real-time acquisition and comprehensive analysis of multi-source data. Traditional electricity marketing risk assessment often relies on fixed code logic, making it difficult to adapt to complex and ever-changing electricity consumption scenarios.
[0003] To improve business adaptability, Chinese invention patent application CN113821540A proposes a rule engine-based method for judging abnormal electricity consumption. This method establishes a multi-layered data warehouse by analyzing data flow and then uses a rule engine to perform anomaly calculations. However, such solutions heavily rely on offline full-volume scanning or micro-batch processing of the data warehouse, resulting in huge computational resource consumption and high data latency, making it difficult to support the high-frequency, real-time marketing risk monitoring needs of new market entities.
[0004] To address the real-time challenges of big data processing, the industry has begun introducing streaming computing frameworks. Real-time processing architectures based on database log capture, message queues, and streaming computing engines can transform changes in marketing business data into a continuously consumable data stream, providing technical support for anomaly identification and business decision-making. For example, existing technology, such as the Chinese invention patent application CN120973532A, proposes a computational topology optimization method based on Flink SQL, which can decompose SQL into streaming operators such as filtering and aggregation and perform execution plan rearrangement. However, when such general streaming frameworks are applied to power marketing risk management, they still face the following significant drawbacks: The marketing system has accumulated a large number of offline business exception SQL statements for databases. General streaming frameworks cannot directly decouple their business semantics from the underlying streaming engineering structure. Developers still need to manually hardcode streaming processing logic for different exception topics, resulting in extremely high development and maintenance costs and making it impossible to achieve rapid business-oriented configuration and automatic code generation.
[0005] Electricity marketing data covers a large scale, and equipment data is susceptible to instantaneous fluctuations caused by communication jitter. Existing streaming SQL frameworks mostly rely on transient threshold judgments and lack analysis of the characteristic state evolution of abnormal objects across time windows. This makes the system extremely sensitive to instantaneous fluctuations, easily triggering a large number of false alarms, and making it difficult to achieve a complete closed loop from anomaly stability identification to the automatic issuance of risk self-healing control instructions.
[0006] Therefore, there is an urgent need for a self-healing and self-controlling streaming computing architecture for power marketing risks that can break down the barriers between offline business SQL and streaming code generation, and has the ability to analyze abnormal evolution. Summary of the Invention
[0007] This invention provides a rule-based method and system for generating streaming code for power marketing risks. By decomposing SQL business semantics layer by layer and decoupling operator parameters, and combining preset templates, it automatically assembles streaming tasks without hard coding. It also introduces a cross-time window state evolution stability measurement and a closed-loop result feedback mechanism. The aim is to solve the problems of high latency caused by heavy reliance on offline full calculation in existing power marketing risk audits, high code coupling and difficulty in reuse when migrating business logic to a streaming framework, and the lack of automated collaborative closed-loop handling of transient data false alarms caused by equipment communication jitter in high-frequency real-time monitoring.
[0008] To address the aforementioned technical problems, this invention proposes a rule-based method for generating streaming codes for electricity marketing risks, comprising the following steps: Obtain the structured query language SQL statement containing business anomaly identification logic and perform semantic decomposition, mapping it to a rule element model under the streaming computing framework; Based on the operator mapping rule base, the rule element model is transformed into a streaming computing operator, and the business thresholds and judgment conditions in the SQL statement are abstracted into an independent set of parameters. Based on the preset rule template, the streaming computing operators and parameter sets are injected and assembled to generate streaming computing execution jobs without hard-coded business logic; The streaming computing execution job processes the business data stream, maintains a historical state sequence across time windows for the monitored object, and performs anomaly stability analysis by measuring the change amplitude of old and new computing characteristics. When an anomaly is determined to meet the stable triggering conditions, closed-loop control actions are generated and executed based on the anomaly characteristics.
[0009] Preferably, the mapping method for the rule element model is as follows: The SQL statement is broken down into core operations including data source definition, condition filtering, table joins, statistical aggregation, and anomaly detection. The core operations are mapped to data source definition rules, filtering rules, keying dimension logic, aggregation operator set, and anomaly judgment rules in streaming computing.
[0010] Preferably, the method for obtaining the parameter set is as follows: Extract the anomaly detection rules and the fixed constants in the statistical aggregation operation; The extracted constants are used to construct a set of configuration parameters that include business type, anomaly detection threshold and state evolution time window.
[0011] Preferably, the method for generating the streaming computing execution job is as follows: The underlying execution chain is generated according to the sequence of the streaming processing pipeline, which includes multi-source data access, streaming correlation processing, state grouping and statistical calculation, anomaly detection and result output. The parameter set is loaded into the corresponding node of the underlying execution chain via parameter injection, thereby completing the automated construction of the code.
[0012] Preferably, the step of performing anomaly stability analysis by measuring the magnitude of change in the old and new computational features specifically involves: A multi-dimensional anomaly feature vector is constructed based on the business performance of the monitored object within the time window; Extract the latest feature components obtained from the current calculation and the old feature components in the historical state cache respectively, calculate the sum of squares of the differences between the two in each feature dimension and take the square root to obtain a measure value representing the intensity of feature change; When the metric value exceeds the preset evolution threshold in multiple consecutive time windows, instantaneous data fluctuations are filtered out, and the anomaly is determined to be a stable anomaly.
[0013] Preferably, the step of running the streaming computing execution job to process the business data stream specifically includes: A business change event is constructed based on the comparison results between the latest feature components and the old feature components; Incremental streaming determination is triggered based on the business change event, and subsequent rule calculation paths are only executed for monitored objects whose feature values have undergone substantial changes.
[0014] Preferably, the step of generating and executing closed-loop control actions based on abnormal characteristics includes: Extract the category, duration, and scope of impact of anomalies that meet stable triggering conditions as input parameters for the judgment dimension; Based on a preset mapping mechanism, the input parameters are converted into self-healing control rules that include parameter correction, work order triggering, or rule rollback for execution. The execution results of the self-healing control rules are fed back to the streaming computing stage to optimize the independent parameter set.
[0015] A second aspect of the invention also proposes a rule-based electricity marketing risk streaming code generation system, said system for implementing the method as described in the first aspect of the invention, comprising: The SQL semantic decomposition and element modeling module is used to obtain structured query language SQL statements containing business anomaly identification logic, perform semantic decomposition on the SQL statements to extract core operation statements, and map them into rule element models adapted to the streaming computing framework. The operator mapping and parameter decoupling module is used to transform the rule units in the rule element model into equivalent streaming computing operators based on a preset operator mapping rule library, and to abstract the business thresholds and judgment conditions implicit in the SQL statement into a set of configurable parameters independent of the streaming computing operators. The templated code generation and construction module is used to inject and assemble the streaming computing operators and the configurable parameter set according to the preset rule template and operator assembly order, and automatically generate deployable streaming computing execution jobs without hard-coded business logic; The state evolution analysis module is used to run the streaming computing execution job to continuously process the incoming data stream. During the streaming computing process, it maintains the historical state sequence of the monitored object across time windows and performs anomaly stability analysis by measuring the change range of the old and new computing results. The closed-loop control self-healing module is used to automatically generate and execute corresponding closed-loop control actions based on the characteristics of the anomaly when it is determined that the current anomaly meets the stable triggering conditions.
[0016] A third aspect of the present invention also provides an electronic device comprising: One or more processors; Memory, used to store one or more computer programs; One or more computer programs stored in the memory are executed by the one or more processors, causing the one or more processors to perform the method as described in the first aspect of the invention.
[0017] In a fourth aspect, the present invention also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method described in the first aspect of the present invention.
[0018] Compared with the prior art, the present invention has the following technical effects: 1. The streaming code generation method proposed in this invention abstracts exception topics into rule objects composed of a set of business tables, a set of primary keys, a set of attributes, constraints, and decision functions, thereby achieving structured expression and parameterized configuration of exception rules. Compared with existing technologies that rely on fixed SQL or hard-coded logic, this approach enables rapid switching and adjustment of different marketing exception topics without modifying the program code. It effectively supports the dynamic adaptation of rules under policy changes, electricity pricing mechanism adjustments, and business model evolution, significantly improving the versatility and sustainable operation capability of streaming computing systems in complex marketing scenarios.
[0019] 2. The streaming code generation method proposed in this invention extracts rule element models and deeply decouples streaming operators from business threshold parameters. Combined with preset templates, it achieves automatic generation of streaming computation jobs without hard-coding of business logic. This mechanism enables the system to quickly adapt to changes in electricity pricing mechanisms or regulatory policies without modifying the underlying streaming code or restarting the job. Only parameter configuration changes are needed, reducing the development threshold and maintenance costs of the streaming rule base and enhancing the system's scalability.
[0020] 3. The streaming code generation method proposed in this invention maintains historical feature sequences across time windows and introduces metrics such as Euclidean distance to evaluate the magnitude of changes in old and new computational features. The system can effectively filter out instantaneous data fluctuations caused by terminal communication drops or occasional meter reading delays, and accurately identify stable anomalies with persistence. Combined with an incremental streaming computing mechanism triggered by business change events, it not only significantly reduces the consumption of computing resources under massive marketing data, but also fundamentally solves the pain point of easily triggering massive false alarms in high-frequency streaming monitoring.
[0021] 4. The streaming code generation method proposed in this invention automatically transforms the characteristics of stable anomalies, such as their category and duration, into self-healing control actions that cover parameter correction and work order triggering. It also feeds back the execution results of the handling to the streaming computing stage, enabling the underlying computing system to use experience feedback to perform parameter self-correction and rule adaptive optimization, thus achieving true self-healing of business risks. Attached Figure Description
[0022] Figure 1 This is a flowchart illustrating the method described in this invention; Figure 2 This is a schematic diagram illustrating the mapping relationship between SQL rules and streaming computing code in an embodiment of the present invention. Detailed Implementation
[0023] To make the objectives, technical solutions, and advantages of the present invention clearer, the technical solutions of the present invention will be clearly and completely described below in conjunction with specific embodiments of the present application and with reference to the accompanying drawings.
[0024] Example 1 This embodiment presents a rule-based method for generating streaming code for electricity marketing risks. In electricity marketing operations, to efficiently migrate traditional offline script logic to a real-time streaming computing system, this embodiment employs the following technical solution: Figure 1 As shown, it includes the following steps one through five: Step 1: Obtain the structured query language SQL statement containing the business anomaly identification logic and perform semantic decomposition, mapping it to the rule element model under the streaming computing framework.
[0025] The mapping method of the rule element model is as follows: the SQL statement is decomposed into five core operations, including data source definition, condition filtering, inter-table association, statistical aggregation and anomaly judgment; the core operations are respectively mapped to data source definition rules, filtering rules, keyed dimension logic, aggregation operator set and anomaly judgment rules in streaming computing.
[0026] In practical implementation, this step aims to address the problem that a large amount of existing logic in power marketing risk management is embedded in offline SQL and difficult to execute directly in a streaming engine. Through layered semantic decomposition, the database-oriented batch processing logic is transformed into an event-stream-oriented rule description model.
[0027] First, the abnormal SQL statements accumulated in the marketing system are broken down into layers. Let the original abnormal SQL be expressed as: SQL=<T,W,J,G,H> In this context, T represents the set of business data tables in the FROM clause, representing the original data source required for analysis; W represents the set of filtering conditions in the WHERE clause, used to initially filter data that meets the business scope; J represents the JOIN relationship, defining the logical connection method between different business tables; G represents the GROUP BY aggregation field, determining the dimension of statistical analysis; and H represents the HAVING or judgment condition, which is the core logic for identifying business anomalies.
[0028] Based on the decomposition, the original abnormal SQL is transformed into a unified rule structure R= adapted to the streaming computing framework.<S,F,K,A,C> Here, S is the definition of the streaming data source, mapped from T, which transforms the static table definition into a real-time consumption message queue (such as Kafka) or streaming event source; F is the filtering rule, mapped from W, which defines the filtering logic for a single event in streaming computation; K is the Key dimension, mapped from G, which determines the KeyedStream dimension in streaming computation and is used for state management; A is the set of aggregation operators, mapped from statistical logic, which represents aggregation operations (such as SUM, COUNT) performed within a window or state; C is the exception judgment rule, mapped from H, which serves as the judgment function at the end of streaming processing.
[0029] Taking "Identification of Abnormalities in Residents' Failure to Implement Peak-Valley Rate Display" as an example, its typical SQL form is as follows: SELECT C.MGT_ORG_CODE FROM INST_ELEC_CONS A, ELEC_CONS_CUST C, CUST_AGRT B WHERE A.CUST_ID = C.CUST_ID AND A.MGT_ORG_CODE = C.MGT_ORG_CODE The original SQL for this business mainly involves join queries on the residential user table, metering data table, and management organization table, forming the basic data set required for anomaly analysis. During this step, the system semantically parses the SQL as follows: Residential User Basic Data Source → Electricity Metering Object Association → Anomaly Analysis Object Determination: Residential user basic data is mapped to the initial object for streaming analysis; based on predefined filtering logic, peak and valley readings in the streaming data are filtered in real time and aggregated by user dimension; the peak-valley reading ratio is calculated and mapped to anomaly judgment rule units. On this basis, the system further combines the predefined peak-valley reading statistical logic in the "Residents Not Executing Peak-Valley Reading Rate Anomaly" rule to filter and aggregate peak and valley readings in the streaming data by user dimension, calculate the peak-valley reading ratio, and finally complete the anomaly judgment.
[0030] By using the above-mentioned hierarchical semantic decomposition method, the complete exception logic originally hidden in multiple SQL segments is broken down into three types of streaming rule units: "data preparation - feature calculation - exception determination", providing a unified input model for the subsequent construction of streaming operator chains.
[0031] Step 2: Based on the operator mapping rule base, the rule element model is transformed into a streaming computation operator, and the business thresholds and judgment conditions in the SQL statement are abstracted into an independent set of parameters.
[0032] The method for obtaining the parameter set is as follows: extract the constants fixed in the anomaly judgment rules and statistical aggregation operations; and use the extracted constants to construct a configuration parameter set that includes business type, anomaly judgment threshold and state evolution time window.
[0033] After completing the SQL semantic decomposition and rule element mapping, this step aims to solve the technical problem of how offline logic can be equivalently expressed in the underlying streaming framework and achieve complete decoupling between running code and business parameters.
[0034] The system has a built-in rule base for mapping SQL operations to streaming computing processing units. While maintaining the semantic equivalence of the original SQL business logic, the rule element model extracted in step one is transformed into a specific streaming computing operator chain. That is, operations such as WHERE, JOIN, GROUP BY, and HAVING in SQL are mapped to corresponding processing logics in streaming computing, such as filtering, association, state grouping, statistical calculation, and anomaly detection. These are organized in the form of composable operator chains to achieve continuous and progressive computing oriented towards event streams.
[0035] The abstract expression of the mapping relationship can be formalized as:
[0036] in, Indicates the operation type in offline SQL; It represents a semantically equivalent processing unit or combination of operators in a streaming computing framework.
[0037] To illustrate the mapping logic above more intuitively, please refer to [link / reference]. Figure 2 The diagram illustrates the mapping relationship between SQL rules for identifying and handling power marketing risks and streaming computing code. The system decomposes the original SQL script into layers: data source layer, condition filtering layer, association dimension layer, statistical aggregation layer, anomaly judgment layer, and result output layer. At the data source layer, the business table defined by the FROM clause is broken down into multiple independent business event streams (such as InstElecConsSourceData). At the condition filtering layer, the WHERE condition is transformed into a rule-based filtering function based on configuration files. At the association dimension layer, progressive association of multi-source data is achieved through key dimension alignment and state caching. At the anomaly judgment layer, the HAVING clause is mapped to a specific anomaly judgment and processing unit (such as AbnormalResult). This layered mapping realizes the transformation from database-oriented batch processing logic to event stream-oriented continuous computing logic.
[0038] For example, specific transformation relationships include, but are not limited to: Conditional filtering transformation: Transform WHERE (or filtering rule F in the rule structure) into filtering logic based on stream processing functions (such as ArchiveProcess); State grouping transformation: Transform GROUP BY (or Key dimension K in the rule structure) into state groups based on key business identifiers and Keyed state management operators (such as keyBy). Streaming join transformation: Transform JOIN into a streaming join processing function based on multiple data sources (such as CoMap or other cross-stream join functions) to achieve progressive join after alignment; Statistical aggregation transformation: Transform aggregation operations such as SUM / COUNT into statistical calculation logic based on state backends or process functions; Exception detection transformation: Transform HAVING (or the judgment rule C in the rule structure) into the terminal exception detection and result output logic.
[0039] Traditional anomaly detection logic often hard-codes thresholds and conditions into the code, requiring the entire streaming job to be redeveloped, compiled, and deployed when policies are fine-tuned. To address this issue, this step removes the hard-coded parameters from the rules. Specifically, the system extracts the fixed constants from anomaly judgment rules and statistical aggregation operations, constructing a unified set of configurable parameters. :
[0040] in, Indicates a user or business type parameter; This represents the threshold parameter for anomaly detection. This represents the statistical period or state evolution window parameter, thereby decoupling the rule logic from the parameter configuration and enabling the anomaly identification rules to have good configurability and scalability.
[0041] Let's take the example of "residents not implementing peak-valley pricing abnormalities". The original SQL might contain anomaly detection conditions like "C.REG_COUNT NOT IN (1,3)". In this embodiment's decoupling mechanism, the value (1,3) is no longer hard-coded logic in the streaming program, but is abstracted and extracted as anomaly detection threshold parameter. When the power grid company adjusts its peak-valley pricing policy or indicator configuration rules (for example, the judgment condition changes to NOT IN (1,3,5)), maintenance personnel only need to update the configuration interface. The parameter values are specified. The streaming engine can dynamically load the updated parameter set and inject it into the decision operator without requiring downtime to refactor the underlying streaming processing code. This significantly improves the dynamic adaptability and engineering maintainability of power marketing risk rules in self-healing and self-control scenarios.
[0042] Step 3: Based on the preset rule template, inject and assemble the streaming computing operators and parameter sets to generate a streaming computing execution job without hard-coded business logic.
[0043] The method for generating the streaming computing execution job is as follows: a bottom-level execution chain is generated according to the streaming processing pipeline sequence of multi-source data access, streaming association processing, state grouping and statistical calculation, anomaly judgment and result output; the parameter set is loaded into the corresponding node of the bottom-level execution chain through parameter injection to complete the automated construction of code.
[0044] After completing the rule element modeling and parameter decoupling, this step uses a rule template-driven mechanism to determine the rule structure. To unify input, a rule template-driven code generation mechanism is used to automatically convert rule elements that have completed semantic mapping and parameter decoupling into deployable streaming computing jobs. This achieves automated transformation from abstract rule descriptions to deployable streaming code, enabling the anomaly recognition logic to run stably without relying on manual coding.
[0045] Based on the types and combinations of rule units in the rule structure, the system generates a complete streaming computation execution chain according to a predefined assembly order of processing units. The generated execution logic includes key steps such as multi-source data access, streaming association processing, state grouping and statistical calculation, anomaly detection, and result output. This process does not directly rely on fixed operator code but is completed through templated processing logic and parameter injection, thereby ensuring the reusability and maintainability of the generated code.
[0046] The code generation process can be represented as:
[0047] in, This indicates the generated streaming computing execution task (such as a deployable Java streaming job) that can be directly submitted to the streaming engine cluster for execution; This represents the code generation function for streaming computing. Its core logic is to map the rule structure into specific code implementations based on the predefined assembly order of processing units. This represents the rule structure model constructed in step one, which serves as the logical input for code generation.
[0048] Next, the technology predefines a logical skeleton template covering the entire lifecycle. Based on the streaming computation operators obtained in step two, the system automatically assembles the underlying execution chain according to the following pipeline sequence: Data access layer: Based on the data source definition S in the rules, automatically configure Kafka or data subscription interface to achieve unified access to multi-source business event streams; Streaming association layer: Based on the association key dimension K, multi-source event streams are progressively aligned using predefined CoMap or association processing templates; State maintenance and statistics layer: Based on the keyBy operator, maintain the state snapshot of business objects, and automatically generate statistical calculation logic according to the aggregation operator set A; Anomaly detection and output layer: Assemble the final logical verification operator according to the judgment rule C, and output the recognition result through a unified Sink interface.
[0049] During the chain assembly process, the parameter set extracted in step two will be used. (including anomaly detection thresholds) Statistics window (etc.) are injected as runtime variables into the corresponding nodes of the template. The generated code maintains a one-to-one correspondence between the semantics of each processing unit and the original SQL, and is traceable.
[0050] Taking "residents failing to comply with peak-valley pricing rates abnormally" as an example, the system calls the code generation function. First, the system loads the streaming processing job template; then, it automatically injects the logic code block for residential users and the code block for display statistics calculation; finally, it injects the configuration threshold for the peak-valley display ratio into the judgment and processing unit. The final generated Java task does not contain hard-coded values; all calculation logic is completed through templated logic and parameter injection. In this way, power marketing personnel only need to adjust the rule template or parameters to quickly generate and deploy new anomaly identification tasks, significantly improving the system's maintainability and scalability.
[0051] Step 4: Run the streaming computing execution job to process the business data stream, maintain the historical state sequence across time windows for the monitored object, and perform anomaly stability analysis by measuring the change amplitude of old and new computing characteristics.
[0052] The method of performing anomaly stability analysis by measuring the magnitude of changes in new and old calculated features specifically involves: constructing a multi-dimensional anomaly feature vector based on the business performance of the monitored object within a time window; extracting the latest feature component obtained from the current calculation and the old feature component from the historical state cache, calculating the sum of squares of the differences between the two in each feature dimension and taking the square root to obtain a metric value representing the intensity of feature change; when the metric value exceeds a preset evolution threshold in multiple consecutive time windows, instantaneous data fluctuations are filtered out, and the anomaly is determined to be a stable anomaly.
[0053] The process of running the streaming computing job to process business data streams specifically involves: constructing a business change event based on the comparison results between the latest feature component and the old feature component; triggering incremental streaming judgment based on the business change event, and executing subsequent rule calculation paths only for monitored objects whose feature values have undergone substantial changes.
[0054] This step aims to address the performance bottleneck of high-frequency calculations of massive amounts of electricity marketing data, as well as the problem of momentary false alarms caused by communication or data latency in underlying devices. To make the logic of the manual clearer, the specific implementation process is detailed below in the order of data flow: Electricity marketing data covers a large number of users and undergoes numerous daily changes. Using a traditional full-data scan would not only be computationally expensive but also easily lead to task backlogs. This step introduces an incremental calculation mode.
[0055] This embodiment maintains a feature cache for each monitored object (such as a specific user or area) in memory (e.g., the state backend of a streaming engine). When new data is received, the latest feature components are first compared with the old feature components. Only when the comparison result indicates a substantial business change will a business change event be constructed. Driven by this event, subsequent complex rule calculations and anomaly detection paths are triggered only for the monitored objects that have undergone changes. This mechanism transforms anomaly identification from a full scan to an incremental mode triggered by key business changes, significantly reducing the consumption of computing resources and latency.
[0056] To track the state trajectory of business objects, the system defines a multi-dimensional anomaly feature vector. Let the anomaly feature vector constructed within the time window t for the k-th anomaly monitoring object be:
[0057] in, This represents the overall abnormal state of the k-th object at time t (or within the time window); n represents the total number of dimensions of the feature, such as multi-dimensional indicators like electricity consumption deviation, rate mismatch frequency, and terminal disconnection frequency. These represent the specific business feature components calculated within the given time window. The streaming engine utilizes state management techniques (such as Flink's Keyed State) to maintain a historical feature sequence across time windows for this object.
[0058] In power systems, sporadic data jumps often occur due to terminal outages and communication congestion. Relying directly on single-event thresholds for judgment is highly prone to false alarms. This embodiment introduces an anomaly evolution metric function to calculate the intensity of state changes:
[0059] in, It represents the overall change intensity of the abnormal features of the k-th monitored object within adjacent time windows, that is, the Euclidean distance between the old and new feature vectors in multidimensional space; This represents the latest feature component calculated within the current time window; This represents the old feature components from the previous time window extracted from the historical state cache.
[0060] Continuous observation If a data jump only lasts for a window before returning to normal, the system determines it as a transient fluctuation and filters it out; only when... In multiple consecutive time windows (the parameters decoupled in step two) Only when the evolution threshold is exceeded within a certain period (as determined by the decision) is the anomaly confirmed to be persistent and stable, formally judged as a stable anomaly, and allowed to proceed to the subsequent self-healing control process.
[0061] In the scenario of "residents not implementing peak-valley reading rates abnormally", due to a communication delay at the main station on a certain day, the peak-valley readings of some users may not be correctly aligned in the streaming window on that day, causing a sudden change in the single calculation result (at this time). (Significantly increased). However, under the stability analysis mechanism of this embodiment, if communication is restored and data is re-collected and aligned the next day, and the abrupt change in this feature is not maintained within multiple consecutive windows, the system will identify it as an occasional meter reading deviation and handle it silently; conversely, if the system calculates that the user has a peak-valley ratio imbalance for several consecutive days, it will be confirmed as a stable anomaly of long-term non-execution of rates, thereby fundamentally solving the pain point of high false alarm rate in high-frequency streaming monitoring.
[0062] Step 5: When the anomaly is determined to meet the stable triggering conditions, generate and execute closed-loop control actions based on the anomaly characteristics.
[0063] The step of generating and executing closed-loop control actions based on abnormal characteristics includes: extracting the category, duration, and impact range of abnormalities that meet stable triggering conditions as input parameters for judgment; converting the input parameters into self-healing control rules that include parameter correction, work order triggering, or rule rollback based on a preset mapping mechanism; and feeding back the execution results of the self-healing control rules to the streaming computing stage to optimize the independent parameter set.
[0064] After completing the anomaly stability analysis and filtering out transient fluctuations in step four, the actual business risks have been identified. This step aims to address the issues of automated handling and system self-correction after risk exposure, achieving a closed-loop risk management system. Based on the anomaly type, impact level, and historical handling strategies, the system automatically generates corresponding self-healing control rules, including parameter correction, work order triggering, rule rollback, or manual review.
[0065] Once an anomaly is determined to be a stable anomaly, this embodiment no longer uses a single alarm method, but instead performs a comprehensive risk assessment, automatically extracts various characteristics of the anomaly, and constructs a self-healing strategy function:
[0066] Among them, Category represents the category parameter of the anomaly, such as metering anomaly, rate execution anomaly, suspected electricity theft, etc., which determines the professional direction of handling; Time represents the duration parameter of the anomaly, reflecting the depth of the risk in the time dimension; Level represents the scope or level parameter of the anomaly's impact, such as single user, entire transformer area, or cross-business hall, which determines the scope and severity of the risk; Healing represents the specific self-healing control rules or strategy combinations calculated and output by the system according to preset logic.
[0067] The system is internally configured with a strategy decision tree or rule engine. After input parameters (Category, Time, Level) are passed in, the system automatically matches and generates corresponding self-healing control actions based on a preset mapping mechanism. These actions cover different levels of automation: Parameter correction: If the evaluation finds that a large number of similar anomalies are due to the system's default judgment threshold being too strict, the system can automatically issue instructions to fine-tune the relevant configuration parameters in the underlying streaming computing job; Work order triggering: For business involving on-site verification, such as meter malfunctions and unauthorized electricity use, the system automatically calls the marketing business system through the interface to generate an on-site inspection work order with detailed descriptions of abnormal characteristics, and dispatches it to the designated person in charge. Rule rollback: If it is determined that the launch of streaming rules due to a policy adjustment has caused widespread unexpected risks, the system can automatically block the current rules and roll back to the operator chain of the previous stable version.
[0068] The core of closed-loop management lies in experience feedback. After the management action is completed, the system will extract the final actual handling result and feed it back to the rule evaluation stage of streaming computing. If the verification result proves that this type of stable anomaly is a normal phenomenon under the new business model, the system will use this feedback data to automatically optimize the set of independent parameters extracted in step two (such as the anomaly judgment threshold). This avoids repeated blocking and enables the adaptive growth of risk rules.
[0069] Taking the scenario of "residents not implementing peak-valley tariff rates abnormally" as an example, after the system confirms for several consecutive days that a user has a stable anomaly of not implementing tariff rates, the system extracts the anomaly category (rate mismatch), duration (Time = 7 consecutive days), and scope of impact (Level = specific high-voltage community). According to the strategy function Healing, the system determines that the risk level is high, and then automatically triggers a rate on-site verification work order to be issued to the local power supply station. At the same time, the user is marked as high-risk in the marketing main station system, restricting some of their online services. After the grid worker verifies and corrects the file on-site, the correction result is fed back to the system, completing a complete closed loop of self-healing and self-control of electricity marketing risks.
[0070] Example 2 This embodiment is a rule-based electricity marketing risk streaming code generation system. The system is used to implement the method described in Embodiment 1, including: The SQL semantic decomposition and element modeling module is used to obtain structured query language SQL statements containing business anomaly identification logic, perform semantic decomposition on the SQL statements to extract core operation statements, and map them into rule element models adapted to the streaming computing framework. The operator mapping and parameter decoupling module is used to transform the rule units in the rule element model into equivalent streaming computing operators based on a preset operator mapping rule library, and to abstract the business thresholds and judgment conditions implicit in the SQL statement into a set of configurable parameters independent of the streaming computing operators. The templated code generation and construction module is used to inject and assemble the streaming computing operators and the configurable parameter set according to the preset rule template and operator assembly order, and automatically generate deployable streaming computing execution jobs without hard-coded business logic; The state evolution analysis module is used to run the streaming computing execution job to continuously process the incoming data stream. During the streaming computing process, it maintains the historical state sequence of the monitored object across time windows and performs anomaly stability analysis by measuring the change range of the old and new computing results. The closed-loop control self-healing module is used to automatically generate and execute corresponding closed-loop control actions based on the characteristics of the anomaly when it is determined that the current anomaly meets the stable triggering conditions.
[0071] Example 3 This embodiment is an electronic device, including: One or more processors; Memory, used to store one or more computer programs; One or more computer programs stored in the memory are executed by the one or more processors, causing the one or more processors to perform the method as described in Embodiment 1.
[0072] Example 4 This embodiment is a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the method described in Embodiment 1.
[0073] The above description is only a preferred embodiment of the present invention. It should be noted that those skilled in the art can make several modifications and improvements without departing from the inventive concept of the present invention, and these all fall within the protection scope of the present invention.
Claims
1. A rule-based method for generating streaming codes for electricity marketing risks, characterized in that, Includes the following steps: Obtain the structured query language SQL statement containing business anomaly identification logic and perform semantic decomposition, mapping it to a rule element model under the streaming computing framework; Based on the operator mapping rule base, the rule element model is transformed into a streaming computing operator, and the business thresholds and judgment conditions in the SQL statement are abstracted into an independent set of parameters. Based on the preset rule template, the streaming computing operators and parameter sets are injected and assembled to generate streaming computing execution jobs without hard-coded business logic; The streaming computing execution job processes the business data stream, maintains a historical state sequence across time windows for the monitored object, and performs anomaly stability analysis by measuring the change amplitude of old and new computing characteristics. When an anomaly is determined to meet the stable triggering conditions, closed-loop control actions are generated and executed based on the anomaly characteristics.
2. The method according to claim 1, characterized in that, The mapping method for the rule element model is as follows: The SQL statement is broken down into core operations including data source definition, condition filtering, table joins, statistical aggregation, and anomaly detection. The core operations are mapped to data source definition rules, filtering rules, keying dimension logic, aggregation operator set, and anomaly judgment rules in streaming computing.
3. The method according to claim 2, characterized in that, The method for obtaining the parameter set is as follows: Extract the anomaly detection rules and the fixed constants in the statistical aggregation operation; The extracted constants are used to construct a set of configuration parameters that include business type, anomaly detection threshold and state evolution time window.
4. The method according to claim 1, characterized in that, The method for generating the streaming computing execution job is as follows: The underlying execution chain is generated according to the sequence of the streaming processing pipeline, which includes multi-source data access, streaming correlation processing, state grouping and statistical calculation, anomaly detection and result output. The parameter set is loaded into the corresponding node of the underlying execution chain via parameter injection, thereby completing the automated construction of the code.
5. The method according to claim 1, characterized in that, The anomaly stability analysis, which measures the magnitude of change in the old and new computational features, specifically involves: A multi-dimensional anomaly feature vector is constructed based on the business performance of the monitored object within the time window; Extract the latest feature components obtained from the current calculation and the old feature components in the historical state cache respectively, calculate the sum of squares of the differences between the two in each feature dimension and take the square root to obtain a measure value representing the intensity of feature change; When the metric value exceeds the preset evolution threshold in multiple consecutive time windows, instantaneous data fluctuations are filtered out, and the anomaly is determined to be a stable anomaly.
6. The method according to claim 5, characterized in that, The process of running the streaming computing execution job to process the business data stream specifically includes: A business change event is constructed based on the comparison results between the latest feature components and the old feature components; Incremental streaming determination is triggered based on the business change event, and subsequent rule calculation paths are only executed for monitored objects whose feature values have undergone substantial changes.
7. The method according to claim 1, characterized in that, The process of generating and executing closed-loop control actions based on abnormal characteristics includes: Extract the category, duration, and scope of impact of anomalies that meet stable triggering conditions as input parameters for the judgment dimension; Based on a preset mapping mechanism, the input parameters are converted into self-healing control rules that include parameter correction, work order triggering, or rule rollback for execution. The execution results of the self-healing control rules are fed back to the streaming computing stage to optimize the independent parameter set.
8. A rule-based electricity marketing risk streaming code generation system, characterized in that, The system is used to implement the method as described in any one of claims 1-7, comprising: The SQL semantic decomposition and element modeling module is used to obtain structured query language SQL statements containing business anomaly identification logic, perform semantic decomposition on the SQL statements to extract core operation statements, and map them into rule element models adapted to the streaming computing framework. The operator mapping and parameter decoupling module is used to transform the rule units in the rule element model into equivalent streaming computing operators based on a preset operator mapping rule library, and to abstract the business thresholds and judgment conditions implicit in the SQL statement into a set of configurable parameters independent of the streaming computing operators. The templated code generation and construction module is used to inject and assemble the streaming computing operators and the configurable parameter set according to the preset rule template and operator assembly order, and automatically generate deployable streaming computing execution jobs without hard-coded business logic; The state evolution analysis module is used to run the streaming computing execution job to continuously process the incoming data stream. During the streaming computing process, it maintains the historical state sequence of the monitored object across time windows and performs anomaly stability analysis by measuring the change range of the old and new computing results. The closed-loop control self-healing module is used to automatically generate and execute corresponding closed-loop control actions based on the characteristics of the anomaly when it is determined that the current anomaly meets the stable triggering conditions.
9. An electronic device, comprising: One or more processors; Memory, used to store one or more computer programs; The feature is that one or more computer programs stored in the memory are executed by the one or more processors, causing the one or more processors to implement the method as described in any one of claims 1-7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-7.