# W-TNME V2 Rule Implementation

## Overview
This document describes the implementation of the updated W-TNME (TNM Edition not valid) validation rule (version 2).

## Rule Description
**Rule Code**: W-TNME  
**Rule Type**: WARNING  
**Description**: TNM values are valid, but TNM edition is not reported  
**Protocol**: Incidence 2020 (Protocol ID: 11)  

### Rule Logic
The W-TNME message must be produced **only** when **all** of the following conditions are true:

1. **TNM_ED is not in_range**
2. **(cT or pT) (is in_range) AND (NOT like '%x%')**
3. **(cN or pN) (is in_range) AND (NOT like '%x%')**
4. **(cM and pM) (is in_range and NOT like '%1%') OR (not in_range)**

### M condition and Stage 4
If **cM or pM** indicates M1 (e.g. M1, 1a, 1b, Y1ski, Y1ADR, Y1D, 1BRA), TNM edition is **not** required for staging because **Stage is 4** (distant metastasis) regardless of the TNM edition. 
The rule therefore does **not** raise W-TNME when the effective M value (pM if in range, else cM if in range) contains `'1'` (i.e. is "like '%1%'").

## Implementation Files

### 1. Java Implementation
**File**: `src/main/java/eu/europa/ec/jrc/qcs/engine/rule/crossfield/encr2020/TNMEditionRule2.java`

**Class**: `TNMEditionRule2` extends `CrossFieldRule` (not `TNMRule`). 
            The rule uses only the seven fields (Edition, pT, pN, pM, cT, cN, cM) and does not rely on `TNMRule.selectTNMValues()` or TNM edition STAGE YAML tables.

## Testing

### Expected Behavior (FILE: incidence_1.csv))

#### TEST Case 1 (`PAT=ECARS-2315_W-TNME-`NOTtrigger1) -> NOT TRIGGER W-TNME
- **TNM_ed**: 8 -->  IN_RANGE (range from 1 TO 10) !

#### TEST Case 2 (`PAT=ECARS-2315_W-TNME-trigger2`) -> TRIGGER W-TNME
- **TNM_ed**: EMPTY --> NOT IN_RANGE (range from 1 TO 10)
- **cT/pT**: EMPTY/1 (pT IN_RANGE)         
- **cN/pN**: EMPTY/0 (pN IN_RANGE)
- **cM/pM**: EMPTY/0 (pM IN_RANGE and pM is NOT like '%1%')

#### TEST Case 3 (`PAT=ECARS-2315_W-TNME-trigger3`) -> TRIGGER W-TNME
- **TNM_ed**: 99 (UNKN) --> NOT IN_RANGE (range from 1 TO 10)
- **cT/pT**: EMPTY/1 (pT IN_RANGE)
- **cN/pN**: EMPTY/1 (pN IN_RANGE)
- **cM/pM**: EMPTY/0(I+) (pM IN_RANGE and pM is NOT like '%1%') 

#### TEST Case 4 (`PAT=ECARS-2315_W-TNME-NOTtrigger4`) -> NOT TRIGGER -  cT/pT NOT IN_RANGE AND pM like '%1%'
- **TNM_ed**: a --> NOT IN_RANGE (range from 1 TO 10)
- **cT/pT**: EMPTY/EMPTY (NOT IN_RANGE)
- **cN/pN**: EMPTY/0 (pN IN_RANGE)
- **cM/pM**: EMPTY/1 (pM IN_RANGE and pM is like '%1%') !

#### TEST Case 5 (`PAT=ECARS-2315_W-TNME-trigger5`) -> TRIGGER  
- **TNM_ed**: 20 --> NOT IN_RANGE (range from 1 TO 10)
- **cT/pT**: 1/9 (cT IN_RANGE)
- **cN/pN**: EMPTY/0 (pN IN_RANGE)
- **cM/pM**: EMPTY/x (pM IN_RANGE and pM is NOT like '%1%')

#### TEST Case 6 (`PAT=ECARS-2315_W-TNME-NOTtrigger6`) -> NOT TRIGGER
- **TNM_ed**: EMPTY --> NOT IN_RANGE (range from 1 TO 10)
- **cT/pT**: EMPTY/1 (cT IN_RANGE)
- **cN/pN**: EMPTY/9 (NOT IN_RANGE, pN is UNKN) !

#### TEST Case 7 (`PAT=ECARS-2315_W-TNME-NOTtrigger7`) -> NOT TRIGGER
- **TNM_ed**: 99 (UNKN) --> NOT IN_RANGE (range from 1 TO 10)
- **cT/pT**: EMPTY/EMPTY (NOT IN_RANGE) !

#### TEST Case 8 (`PAT=ECARS-2315_W-TNME-NOTtrigger8`) -> NOT TRIGGER
- **TNM_ed**: EMPTY --> NOT IN_RANGE (range from 1 TO 10)
- **cT/pT**: EMPTY/9 (NOT IN_RANGE) !

#### TEST Case 9 (`PAT=ECARS-2315_W-TNME-trigger9`) -> TRIGGER
- **TNM_ed**: EMPTY --> NOT IN_RANGE (range from 1 TO 10)
- **cT/pT**: EMPTY/1 (pT IN_RANGE)
- **cN/pN**: EMPTY/0 (pN IN_RANGE)
- **cM/pM**: y0/9 (pM UNKN, cM is IN_RANGE AND cM NOT like '%1%') !

## Files Modified/Created

### quality-check-software project:
1. `src/main/java/eu/europa/ec/jrc/qcs/engine/rule/crossfield/encr2020/TNMEditionRule2.java`
2. `docs/rules/W-TNME-2/W-TNME-2_rule_registration.sql`
3. `docs/rules/W-TNME-2/W-TNME-2_IMPLEMENTATION_README.md` (this file)

## Rule Positioning
The W-TNME rule replaces the old TNMEditionRule implementation:
- **Priority Level**: 6
- **Protocol**: ENCR 2020
