diff --git a/docs/notizen/LIVE-ZEILE.txt b/docs/notizen/LIVE-ZEILE.txt new file mode 100644 index 00000000..1ab646cd --- /dev/null +++ b/docs/notizen/LIVE-ZEILE.txt @@ -0,0 +1 @@ +R0=1;MOTOR_RAKE_REGEL=1;MOTOR_ALLIN_UEBERSETZUNG=1;MOTOR_FLOP_EXAKT=1;MOTOR_FLOP_EXAKT_MS=2000;MOTOR_FLOP_EXAKT_VOR_MS=2000;MOTOR_FLOP_EXAKT_S_JE_GB=22;MOTOR_FLOP_EXAKT_ZIEL=1.0;MOTOR_FLOP_EXAKT_GB=4;MOTOR_TEMPERATUR=1;MOTOR_TEMPERATUR_STRASSEN=3;MOTOR_TEMPERATUR_DATEI=D:\Projects\pf-temperatur\live\temperatur-v2-mitte.txt;MOTOR_GEDAECHTNIS2=D:\Projects\pokerfreak-daten\gedaechtnis2-live;MOTOR_SPIELER_LINIE=1;MOTOR_SPIELER_LINIE_TURN=1;MOTOR_POOL_UNSERE=D:\Projects\pokerfreak-daten\gedaechtnis3-pool-unsere\tafel.json;MOTOR_BAYES_RIVER=32;MOTOR_VORFLOP_JAMPROBE=1;MOTOR_ECHTER_VERLAUF=1;MOTOR_AUSSETZER=1;MOTOR_AUSSER_BAUM=1;MOTOR_FLOP_BIBLIOTHEK=C:\Projects\pokerfreak-daten\flopbib\flopbib-v2;MOTOR_UHR=1;MOTOR_UHR_PUFFER_MS=300;MOTOR_UHR_ALLIN_LUECKE=1;MOTOR_JAMPROBE_POOL=D:\Projects\pokerfreak-daten\innovation\jamprobe_pool\jamprobe-pool-v2.txt;MOTOR_ALLIN_PROBE=1;MOTOR_ALLIN_PROBE_BAND=D:\Projects\pokerfreak-daten\allinprobe\jamband-tafel-0512.txt diff --git a/docs/notizen/SCHALTER.md b/docs/notizen/SCHALTER.md new file mode 100644 index 00000000..a1e43ead --- /dev/null +++ b/docs/notizen/SCHALTER.md @@ -0,0 +1,87 @@ +# Schalter des Motors (gepflegt; geprueft von src/waechter_spec.rs) + +Jeder Schalter, den der Motor-Quelltext liest, steht hier genau einmal. Spalten: +- **Art**: `entscheidung` (aendert Zug oder Groesse), `mess` (nur Messung/Diagnose, aendert keine Entscheidung), `pfad` (Datei-/Ordnerangabe zu einem anderen Schalter), `betrieb` (Laufzeit, Faeden, Speicher; keine Entscheidung). +- **Live**: steht der Schalter in docs/notizen/LIVE-ZEILE.txt (der Live-Zeile)? +- **Beleg**: Herleitung oder Messung (Pfad/Karte) bzw. warum er existiert. + +Regel (Waechter): ein Schalter der Art `entscheidung`, den die Live-Zeile nicht setzt, ist toter Entscheidungscode und schlaegt fehl; `?` schlaegt immer fehl. + +| Schalter | Art | Live | Beleg | +|---|---|---|---| +| MOTOR_ALLIN_EICHUNG | ? | nein | | +| MOTOR_ALLIN_EICHUNG_TAFEL | ? | nein | | +| MOTOR_ALLIN_PROBE | ? | ja | | +| MOTOR_ALLIN_PROBE_ALT_FLACH | ? | nein | | +| MOTOR_ALLIN_PROBE_BAND | ? | ja | | +| MOTOR_ALLIN_UEBERSETZUNG | ? | ja | | +| MOTOR_ANTWORTKURVE | ? | nein | | +| MOTOR_AUSSER_BAUM | ? | ja | | +| MOTOR_AUSSETZER | ? | ja | | +| MOTOR_BAYES_REGEL | ? | nein | | +| MOTOR_BAYES_RIVER | ? | ja | | +| MOTOR_BLUFF | ? | nein | | +| MOTOR_BODEN | ? | nein | | +| MOTOR_ECHTER_VERLAUF | ? | ja | | +| MOTOR_EINSATZ | ? | nein | | +| MOTOR_FLOPBIB_DUMP | ? | nein | | +| MOTOR_FLOPBLUFF | ? | nein | | +| MOTOR_FLOP_BIBLIOTHEK | ? | ja | | +| MOTOR_FLOP_EXAKT | ? | ja | | +| MOTOR_FLOP_EXAKT_GB | ? | ja | | +| MOTOR_FLOP_EXAKT_MS | ? | ja | | +| MOTOR_FLOP_EXAKT_S_JE_GB | ? | ja | | +| MOTOR_FLOP_EXAKT_VOR_MS | ? | ja | | +| MOTOR_FLOP_EXAKT_ZIEL | ? | ja | | +| MOTOR_GEDAECHTNIS | ? | nein | | +| MOTOR_GEDAECHTNIS2 | ? | ja | | +| MOTOR_GEDAECHTNIS2_SCHREIBT | ? | nein | | +| MOTOR_GED_FBL | ? | nein | | +| MOTOR_ISO | ? | nein | | +| MOTOR_JAMPROBE | ? | nein | | +| MOTOR_JAMPROBE_POOL | ? | ja | | +| MOTOR_JEHAND | ? | nein | | +| MOTOR_LAUT | ? | nein | | +| MOTOR_LIMP | ? | nein | | +| MOTOR_MEHRWEG | ? | nein | | +| MOTOR_MISCH | ? | nein | | +| MOTOR_NL2 | ? | nein | | +| MOTOR_POOL_UNSERE | ? | ja | | +| MOTOR_R0 | ? | nein | | +| MOTOR_RAISE | ? | nein | | +| MOTOR_RAKE_REGEL | ? | ja | | +| MOTOR_RIVER_WERT | ? | nein | | +| MOTOR_RIVER_WERT_KLASSEN | ? | nein | | +| MOTOR_RIVER_WERT_PRUEF | ? | nein | | +| MOTOR_RIVER_WERT_TAFEL | ? | nein | | +| MOTOR_SPIELER_LINIE | ? | ja | | +| MOTOR_SPIELER_LINIE_TURN | ? | ja | | +| MOTOR_STAPEL_PREIS_TAFEL | ? | nein | | +| MOTOR_STAPEL_PREIS_VORFLOP_TAFEL | ? | nein | | +| MOTOR_STRADDLE_TAFEL | ? | nein | | +| MOTOR_STRASSEN | ? | nein | | +| MOTOR_TEMPERATUR | ? | ja | | +| MOTOR_TEMPERATUR_DATEI | ? | ja | | +| MOTOR_TEMPERATUR_EINSTUFIG | ? | nein | | +| MOTOR_TEMPERATUR_GPU | ? | nein | | +| MOTOR_TEMPERATUR_GPU_FAEDEN | ? | nein | | +| MOTOR_TEMPERATUR_GPU_TEST | ? | nein | | +| MOTOR_TEMPERATUR_JAM | ? | nein | | +| MOTOR_TEMPERATUR_KL | ? | nein | | +| MOTOR_TEMPERATUR_MS | ? | nein | | +| MOTOR_TEMPERATUR_RESERVE_MS | ? | nein | | +| MOTOR_TEMPERATUR_STRASSEN | ? | ja | | +| MOTOR_TEMPERATUR_WARM | ? | nein | | +| MOTOR_TIEF | ? | nein | | +| MOTOR_TIEF_AB | ? | nein | | +| MOTOR_TURN | ? | nein | | +| MOTOR_TYP | ? | nein | | +| MOTOR_UHR | ? | ja | | +| MOTOR_UHR_ALLIN_LUECKE | ? | ja | | +| MOTOR_UHR_PUFFER_MS | ? | ja | | +| MOTOR_VORFLOP_ALLIN | ? | nein | | +| MOTOR_VORFLOP_ALLIN_TAFEL | ? | nein | | +| MOTOR_VORFLOP_JAMPROBE | ? | ja | | +| MOTOR_VORFLOP_SELBST | ? | nein | | +| MOTOR_ZEITBANK | ? | nein | | +| R0 | ? | ja | | diff --git a/entscheiden/motor/src/main.rs b/entscheiden/motor/src/main.rs index 43c9e952..25e7001b 100644 --- a/entscheiden/motor/src/main.rs +++ b/entscheiden/motor/src/main.rs @@ -3,6 +3,8 @@ mod allin_probe; mod antwortkurve; mod antwortkurve_schluessel; #[cfg(test)] +mod waechter_spec; +#[cfg(test)] mod antwortkurve_spec; mod ausbeuten; mod ausbeute_feld; diff --git a/entscheiden/motor/src/waechter_spec.rs b/entscheiden/motor/src/waechter_spec.rs new file mode 100644 index 00000000..9d887f7f --- /dev/null +++ b/entscheiden/motor/src/waechter_spec.rs @@ -0,0 +1,126 @@ +//! Waechter (Owner 05.12.: "unsere architektur darf so einen garbage ... gar nicht zulassen"): maschinelle Pruefungen statt Disziplin. +//! +//! Waechter 1, Schalter: jeder Schalter, den der Motor-Quelltext liest (Stringliteral `MOTOR_*`, `HYBRID_*`, `RICHTER_*`, `R0`), steht genau einmal in +//! docs/notizen/SCHALTER.md, und jede Zeile dort hat einen Schalter im Quelltext. Jede Zeile traegt eine Art aus der geschlossenen Menge +//! (entscheidung, mess, pfad, betrieb). Ein Schalter der Art `entscheidung`, den die Live-Zeile (docs/notizen/LIVE-ZEILE.txt) nicht setzt, ist toter +//! Entscheidungscode: Fehler. Die Live-Spalte der Tabelle muss mit der Live-Zeile uebereinstimmen. +use std::collections::{BTreeMap, BTreeSet}; +use std::path::PathBuf; + +const ARTEN: [&str; 4] = ["entscheidung", "mess", "pfad", "betrieb"]; + +fn wurzel() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) +} + +/// Schalternamen aus einem Quelltext: Stringliterale "MOTOR_...", "HYBRID_...", "RICHTER_..." und "R0". +pub fn schalter_im_text(text: &str) -> BTreeSet { + let mut aus = BTreeSet::new(); + let b = text.as_bytes(); + let mut i = 0; + while i < b.len() { + if b[i] == b'"' { + let mut j = i + 1; + while j < b.len() && (b[j].is_ascii_uppercase() || b[j].is_ascii_digit() || b[j] == b'_') { + j += 1; + } + if j < b.len() && b[j] == b'"' && j > i + 1 { + let w = &text[i + 1..j]; + if w == "R0" || ["MOTOR_", "HYBRID_", "RICHTER_"].iter().any(|p| w.starts_with(p) && w.len() > p.len()) { + aus.insert(w.to_string()); + } + } + i = j.max(i + 1); + } else { + i += 1; + } + } + aus +} + +/// Zeilen der Tabelle in SCHALTER.md: Name -> (Art, Live-Spalte). +pub fn tabelle(text: &str) -> Result, String> { + let mut t = BTreeMap::new(); + for z in text.lines().filter(|z| z.starts_with("| ") && !z.starts_with("| Schalter") && !z.starts_with("|---")) { + let f: Vec<&str> = z.trim_matches('|').split('|').map(str::trim).collect(); + if f.len() < 3 { + return Err(format!("Zeile ohne Spalten: {z}")); + } + if t.insert(f[0].to_string(), (f[1].to_string(), f[2].to_string())).is_some() { + return Err(format!("{} steht doppelt", f[0])); + } + } + Ok(t) +} + +pub fn live_schalter(zeile: &str) -> BTreeSet { + zeile.trim().split(';').filter_map(|kv| kv.split_once('=').map(|(k, _)| k.trim().to_string())).filter(|k| !k.is_empty()).collect() +} + +/// Alle Befunde des Schalter-Waechters (leer = gruen). +pub fn schalter_befunde(quelle: &BTreeSet, tab: &BTreeMap, live: &BTreeSet) -> Vec { + let mut b = Vec::new(); + for n in quelle.difference(&tab.keys().cloned().collect()) { + b.push(format!("{n}: im Quelltext, fehlt in SCHALTER.md")); + } + for n in tab.keys().filter(|n| !quelle.contains(*n)) { + b.push(format!("{n}: in SCHALTER.md, im Quelltext nicht gelesen (tot)")); + } + for (n, (art, live_spalte)) in tab { + if !ARTEN.contains(&art.as_str()) { + b.push(format!("{n}: Art '{art}' nicht aus {ARTEN:?}")); + } + let ist_live = live.contains(n); + if (live_spalte == "ja") != ist_live { + b.push(format!("{n}: Live-Spalte '{live_spalte}', Live-Zeile {}", if ist_live { "setzt ihn" } else { "setzt ihn nicht" })); + } + if art == "entscheidung" && !ist_live { + b.push(format!("{n}: Entscheidungsschalter, den die Live-Zeile nicht setzt (toter Entscheidungscode)")); + } + } + b +} + +#[cfg(test)] +mod spec { + use super::*; + + #[test] + fn findet_schalterliterale_und_sonst_nichts() { + let t = r#"let a = std::env::var("MOTOR_X_1"); f("R0"); g("MOTOR_"); h("motor_klein"); k("HYBRID_UEB"); "TEXT_MOTOR_Y""#; + let s = schalter_im_text(t); + assert_eq!(s, ["HYBRID_UEB", "MOTOR_X_1", "R0"].iter().map(|x| x.to_string()).collect()); + } + + #[test] + fn befunde_fuer_fehlende_tote_unbekannte_und_tote_entscheidungsschalter() { + let quelle: BTreeSet = ["MOTOR_A", "MOTOR_B", "MOTOR_C"].iter().map(|x| x.to_string()).collect(); + let tab = tabelle("| Schalter | Art | Live | Beleg |\n|---|---|---|---|\n| MOTOR_A | entscheidung | ja | x |\n| MOTOR_B | entscheidung | nein | x |\n| MOTOR_D | mess | nein | x |\n").unwrap(); + let live: BTreeSet = ["MOTOR_A"].iter().map(|x| x.to_string()).collect(); + let b = schalter_befunde(&quelle, &tab, &live); + assert!(b.iter().any(|x| x.starts_with("MOTOR_C: im Quelltext"))); + assert!(b.iter().any(|x| x.starts_with("MOTOR_D: in SCHALTER.md"))); + assert!(b.iter().any(|x| x.starts_with("MOTOR_B: Entscheidungsschalter"))); + assert!(!b.iter().any(|x| x.starts_with("MOTOR_A"))); + let tab2 = tabelle("| MOTOR_A | ? | ja | |\n").unwrap(); + assert!(schalter_befunde(&["MOTOR_A".to_string()].into(), &tab2, &live).iter().any(|x| x.contains("Art '?'"))); + } + + /// Der Waechter selbst, auf dem echten Stand: rot, solange ein Befund bleibt. + #[test] + fn waechter_schalter() { + let mut quelle = BTreeSet::new(); + for e in std::fs::read_dir(wurzel().join("src")).unwrap() { + let p = e.unwrap().path(); + let n = p.file_name().unwrap().to_string_lossy().to_string(); + if n.ends_with(".rs") && !n.ends_with("_spec.rs") { + quelle.extend(schalter_im_text(&std::fs::read_to_string(&p).unwrap())); + } + } + let doku = wurzel().join("../../docs/notizen"); + let tab = tabelle(&std::fs::read_to_string(doku.join("SCHALTER.md")).expect("docs/notizen/SCHALTER.md fehlt")).unwrap(); + let live = live_schalter(&std::fs::read_to_string(doku.join("LIVE-ZEILE.txt")).expect("docs/notizen/LIVE-ZEILE.txt fehlt")); + let b = schalter_befunde(&quelle, &tab, &live); + assert!(b.is_empty(), "Schalter-Waechter: {} Befunde\n{}", b.len(), b.join("\n")); + } +}