1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! Value conversion helpers — replaces `Session::string_to_value`.
//!
//! Mirrors session `Value` string vs tensor handling: PMML `Value` needs
//! `FieldId` + `DataType`/`OpType` to decide `Continuous` vs `Discrete`.
//! This module is `pub(crate)`; `Session` exposes `field_id`/`symbol_id` for callers.
//!
//! The helper is used by `Session::string_to_value` (CSV / CLI) and by
//! `Session::run_from_strings`. It handles empty / `"Missing"` → `Missing`,
//! numeric `parse::<f64>` → `Continuous` vs categorical intern → `Discrete`,
//! and unknown categorical → `Missing` (so predicates fail safely rather than wrong-match).
use crate;
/// Convert a raw string (e.g. from CSV) to `Value` using `Session`'s caches.
///
/// Returns `Missing` for empty/`"Missing"` or unknown categorical. Tries numeric `parse`
/// first, but if the field's `DataType::String` / `OpType::Categorical` and a `SymbolId`
/// exists for `s`, it prefers `Discrete` (so numeric-looking categoricals like `"0"` don't mis-fire).
///
/// # Parameters
///
/// - `_field_name`: reserved for diagnostics (kept for future diagnostics; field name that owns this value).
/// - `s`: raw string from CSV / user input.
/// - `field_id`: `Some(FieldId)` if field is known from `DataDictionary`, else `None`.
/// - `data_type`: `Some(DataType)` if `field_id` was found in `Ir.data_dictionary`, else `None`.
/// - `op_type`: `Some(OpType)` if `field_id` was found, else `None`.
/// - `symbol_str_to_id`: `String → SymbolId` map from `Session` (cold interning).
///
/// # Returns
///
/// `Value::Continuous(f)` if `s` parses as `f64` and is not categorical-with-symbol,
/// `Discrete(SymbolId)` if `s` is known categorical, else `Missing`.
///
/// # Examples
///
/// ```
/// use pmmlruntime::session::input::string_to_value;
/// use pmmlruntime::base::{FieldId, field::{DataType, OpType}, Value, SymbolId};
/// use std::collections::HashMap;
/// let mut sym = HashMap::new();
/// sym.insert("setosa".to_string(), SymbolId(1));
/// let v = string_to_value("Species", "setosa", Some(FieldId(0)), Some(DataType::String), Some(OpType::Categorical), &sym);
/// assert!(matches!(v, Value::Discrete(_)));
/// let v2 = string_to_value("x", "3.14", None, None, None, &sym);
/// assert_eq!(v2, Value::Continuous(3.14));
/// assert_eq!(string_to_value("x","", None, None, None, &sym), Value::Missing);
/// ```
/// Simple helper that converts `s` via `string_to_value` without field metadata.
///
/// Uses `""` for field name and `None` for `FieldId`/`DataType`/`OpType`, so categorical
/// detection relies only on `symbol_str_to_id`. Handy for tests / `InlineTable` bridging.
///
/// # Parameters
///
/// - `s`: raw string.
/// - `symbol_str_to_id`: map for `Discrete` interning.
///
/// # Returns
///
/// `Value` as per [`string_to_value`].