# gaossr/onepro architecture

> Architecture diagram of the GitHub repository gaossr/onepro, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gaossr/onepro
- Repository: https://github.com/gaossr/onepro

## Overview

##### Project type and purpose
- This is a Java-based, in-memory, algorithm-driven “discount stacking” library with a small Spring-boot demo. Its purpose is to take a set of goods and a set of discount rules, explore all valid application orders (permutations), and return the globally optimal discount combination.

##### Main components
a. API / Demo Layer (blue)
- TestController (com.gch.discount.demo) – mocks input, invokes the pipeline
- DiscountGroupUtil – parses “share/exclude” group protocols into sets of DiscountWrapper
b. Orchestration Layer (green)
- Flowable – builder/DSL that wires CalculatorRouter and triggers Permutation.perm()
- CalculatorRouter – routes each DiscountWrapper.type to the correct Calculator implementation
c. Core Algorithm Layer (orange)
- Permutation<T> – pre-computes permutations up to SUPPORTEDSIZE, manages an Aₙ³ cache (Map<Integer,CalcState>)
- DiscountContext<T> – holds items, wrappers, preCompute results, and accumulates CalcStage[] and CalcResult
- CalcState – snapshot of partially applied permutation (first 3 steps) for cache reuse
d. Plugin & Extension Layer (purple)
- AbstractCalculator<T> & Calculator<T> – template method defining calcWarp() and abstract calc()
- Concrete calculators (ManjianCalc, ZhekouCalc) – @Component("type") beans implementing specific discount logic
- PreCompute<T> interface & PreComputeHolder – reflection-based loading of custom pre-computation strategies
e. Utilities & Config (grey)
- IdGenerator, LimitingUtil – ID generation and performance counters
- calculator-core.properties – defines precompute.path for scanning PreCompute implementations

##### Relationships & data flow
1. Client/TestController builds: List<GoodsItem> + List<DiscountWrapper>
2. → DiscountGroupUtil.transform() → List<Pair<Set<DiscountWrapper>,Set<DiscountWrapper>>>
3. For each “left” set:
a. DiscountContext.create(totalPrice, items, wrappers)
- runs PreCompute implementations, populates context.preCompute map
b. Flowable.perm(context)
- calls Permutation.permute(wrapperList, context)
- Permutation uses static PERMUTATIONS and its cache
- for each permutation step, calls CalculatorRouter.route(type).calcWarp(...)
- CalculatorRouter selects the correct AbstractCalculator → invokes calc()
- calc() modifies DiscountContext.calcResult and records CalcStage
4. After exploring all, TestController picks the lowest final price

##### Architectural patterns & principles
- Template Method: AbstractCalculator defines calcWarp() and delegates to calc()
- Strategy / Router: CalculatorRouter + @Component beans for each discount type
- Builder / Fluent API: Flowable.build(calculatorRouter).perm(ctx)
- Caching Optimization: static pre-computed permutations + Aₙ³ step-level cache
- Reflection-based plugin loading: PreComputeHolder scans precompute.path

## Diagram (Mermaid)

```mermaid
graph TB
    %% API / Demo Layer
    subgraph "API / Demo Layer"
        direction TB
        TC["TestController"]:::demo
        DG["DiscountGroupUtil"]:::demo
    end

    %% Orchestration Layer
    subgraph "Orchestration Layer"
        direction TB
        FL["Flowable"]:::orchestration
        CR["CalculatorRouter"]:::orchestration
    end

    %% Core Algorithm Layer
    subgraph "Core Algorithm Layer"
        direction TB
        PC["Permutation<T>"]:::core
        DC["DiscountContext<T>"]:::core
        CS["CalcState"]:::core
        PC_STATIC["PERMUTATIONS & cache"]:::core
    end

    %% Plugin & Extension Layer
    subgraph "Plugin & Extension Layer"
        direction TB
        CI["Calculator<T>"]:::plugin
        AC["AbstractCalculator<T>"]:::plugin
        MC["ManjianCalc"]:::plugin
        ZC["ZhekouCalc"]:::plugin
        PI["PreCompute<T>"]:::plugin
        PH["PreComputeHolder"]:::plugin
    end

    %% Utilities & Config
    subgraph "Utilities & Config"
        direction TB
        IG["IdGenerator"]:::util
        LU["LimitingUtil"]:::util
        CP["calculator-core.properties"]:::util
    end

    %% Data Flow
    TC -->|"builds input"| DG
    DG -->|"transform()"| FL
    FL -->|"perm()"| PC
    PC -->|"uses cache/static"| PC_STATIC
    PC -->|"permute()"| DC
    DC -->|"state updates"| CS
    PC <-->|"updates / reads"| DC
    PC -->|"route(type).calcWarp()"| CR
    CR -->|"delegates"| AC
    AC -->|"template calcWarp() calls"| AC
    AC -->|"calls calc()"| CI
    CR -->|"routes to"| MC
    CR -->|"routes to"| ZC
    DC -->|"preComputeItems() init"| PH
    PH -->|"loads via reflection"| PI
    CP -.->|"precompute.path"| PH

    %% Click Events
    click TC "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/demo/biz/TestController.java"
    click DG "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/core/utils/DiscountGroupUtil.java"
    click FL "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/demo/biz/Flowable.java"
    click CR "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/core/aware/CalculatorRouter.java"
    click PC "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/core/permutation/Permutation.java"
    click DC "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/core/model/common/DiscountContext.java"
    click CS "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/core/model/common/CalcState.java"
    click CI "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/core/discount/Calculator.java"
    click AC "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/core/discount/impl/AbstractCalculator.java"
    click MC "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/demo/biz/calc/ManjianCalc.java"
    click ZC "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/demo/biz/calc/ZhekouCalc.java"
    click PI "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/core/precompute/PreCompute.java"
    click PH "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/core/precompute/PreComputeHolder.java"
    click IG "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/core/utils/IdGenerator.java"
    click LU "https://github.com/gaossr/onepro/blob/main/src/main/java/com/gch/discount/core/utils/LimitingUtil.java"
    click CP "https://github.com/gaossr/onepro/blob/main/src/main/resources/calculator-core.properties"

    %% Styles
    classDef demo fill:#D6E9F8,stroke:#333,stroke-width:1px
    classDef orchestration fill:#D0F0C0,stroke:#333,stroke-width:1px
    classDef core fill:#FAE5B3,stroke:#333,stroke-width:1px
    classDef plugin fill:#E3E4FA,stroke:#333,stroke-width:1px
    classDef util fill:#D3D3D3,stroke:#333,stroke-width:1px
```
