# W-UNKN Rule Implementation

## Overview
This document describes the implementation of the W-UNKN (Value set to unknown) validation rule. 
The rule raises a warning when a field’s value equals that field’s configured “unknown” label (e.g. 9, 99, 9999) as defined in the schema.

## Rule Description
**Rule Code**: W-UNKN  
**Rule Type**: WARNING  
**Description**: Value set to missing
**Rule Definition ID**: 67  
**Validation Message ID**: 66  

### Rule Logic
The W-UNKN message is produced when:
- For each record, the rule evaluates one or more fields (depending on configuration).
- If the field’s value equals the field’s **unknown label** (schema `Field.unknownValue` / `getUnknownLabel()`), a warning is raised for that field.
- Empty/blank values are ignored (no warning).
- Fields with no unknown label configured are skipped.

### Configuration

ENCR field-level rules use explicit `ruleToField` targeting. For protocol 11, the list of fields checked by W-UNKN is configured with `target_id = 3` (`SOME_FIELDS`), one row per checked field.

The `FIELD.unknownValue` column is still used by the rule as the value to compare against, but it is not the source of truth for deciding whether W-UNKN applies to a field in the ENCR protocol. The target list is `ruleToField`.

**2** (ALL_FIELDS) | Check all fields in the schema | Every field that has a non-empty unknown label is checked; W-UNKN is raised when value equals that label. |
**3** (SOME_FIELDS) | Check only listed fields | Only the fields explicitly listed in `ruletofield` (by `field_id`) for this rule and protocol are checked. |

For a given protocol and rule 67, use either ALL_FIELDS or SOME_FIELDS, not both.
If both target_id 2 and 3 rows exist, the rule receives an inconsistent target and is skipped.

### Fields checked (protocol 11 – ENCR 2020 incidence, SOME_FIELDS)

For protocol 11, W-UNKN uses `target_id = 3` and checks only the fields listed in `ruleToField`. 
A warning is raised when the submitted value equals that field’s `unknownValue` in the `FIELD` table.

| field_id | Variable   | Unknown value | Notes                          |
|----------|------------|---------------|--------------------------------|
| 73       | Age        | 999           |                                |
| 74       | Sex        | 9             |                                |
| 77       | MoI        | 99            |                                |
| 79       | BoD        | 9             |                                |
| 83       | Grade      | 9             | ICD-O-3 grade not determined   |
| 84       | Autopsy    | 9             |                                |
| 85       | Vit_stat   | 9             |                                |
| 86       | MoF        | 99            |                                |
| 87       | YoF        | 9999          |                                |
| 88       | Surv_time  | 99999         |                                |

Other protocols that use ALL_FIELDS (`target_id = 2`) automatically check every field with a configured unknown.

## Implementation Files

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


Migration from legacy `FIELD.mandatory=3` to ruleToField (SOME_FIELDS) is described in:
`docs/migration/deprecate-mandatory-3-w-unkn-ruletofield.sql`.

## Testing

