oxirs-physics 0.3.1

Physics-informed digital twin simulation bridge for OxiRS
Documentation
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Mapping logic: physics objects → RDF triples, ontology bindings, and
//! reverse parsing from RDF triples back to physics parameters.

use crate::error::{PhysicsError, PhysicsResult};
use crate::rdf::physics_rdf_types::{
    PhysicsToRdfConfig, RdfBoundaryCondition, RdfMaterialProperty, Triple, NS_EX, NS_PHYS, NS_PROV,
    NS_QUDT, NS_RDF, NS_RDFS, NS_SOSA, NS_SSN, NS_UNIT, NS_XSD,
};
use crate::simulation::result_injection::SimulationResult;
use std::collections::HashMap;
use std::fmt::Write as FmtWrite;

// ──────────────────────────────────────────────────────────────────────────────
// Unit mapping: quantity name → QUDT unit suffix
// ──────────────────────────────────────────────────────────────────────────────

pub(crate) fn qudt_unit_for(property: &str) -> &'static str {
    match property {
        "temperature" => "DEG_C",
        "temperature_k" => "K",
        "pressure" => "PA",
        "velocity" | "velocity_x" | "velocity_y" | "velocity_z" => "M-PER-SEC",
        "mass" => "KiloGM",
        "energy" | "kinetic_energy" | "potential_energy" | "total_energy" => "J",
        "power" => "W",
        "force" | "force_x" | "force_y" | "force_z" => "N",
        "density" => "KiloGM-PER-M3",
        "viscosity" => "PA-SEC",
        "thermal_conductivity" => "W-PER-M-K",
        "specific_heat" => "J-PER-KiloGM-K",
        "length" | "position_x" | "position_y" | "position_z" => "M",
        "time" => "SEC",
        "frequency" => "HZ",
        "voltage" => "V",
        "current" => "A",
        "resistance" => "OHM",
        "entropy" => "J-PER-K",
        _ => "UNITLESS",
    }
}

/// Turtle preamble with all prefixes we use.
pub(crate) fn turtle_preamble() -> String {
    use crate::rdf::physics_rdf_types::{NS_RDF as RDF, NS_RDFS as RDFS};
    format!(
        "@prefix rdf:  <{RDF}> .\n\
         @prefix rdfs: <{RDFS}> .\n\
         @prefix xsd:  <{NS_XSD}> .\n\
         @prefix sosa: <{NS_SOSA}> .\n\
         @prefix ssn:  <{NS_SSN}> .\n\
         @prefix qudt: <{NS_QUDT}> .\n\
         @prefix unit: <{NS_UNIT}> .\n\
         @prefix ex:   <{NS_EX}> .\n\
         @prefix phys: <{NS_PHYS}> .\n\
         @prefix prov: <{NS_PROV}> .\n\n"
    )
}

// ──────────────────────────────────────────────────────────────────────────────
// PhysicsToRdf
// ──────────────────────────────────────────────────────────────────────────────

/// Converts physics simulation results into RDF triples following SOSA/SSN
/// and QUDT ontologies.
///
/// # SOSA modelling
///
/// Each scalar quantity in a [`SimulationResult`] is mapped to a
/// `sosa:Observation`:
///
/// ```turtle
/// ex:obs_<run>_<prop>_<t>  a sosa:Observation ;
///     sosa:observedProperty  phys:<prop> ;
///     sosa:hasSimpleResult   "<value>"^^xsd:double ;
///     sosa:resultTime        "<ts>"^^xsd:dateTime ;
///     sosa:hasFeatureOfInterest ex:dt_<entity> .
/// ```
///
/// The digital twin entity is represented as:
///
/// ```turtle
/// ex:dt_<entity>  a ex:DigitalTwin ;
///     ex:hasState  ex:state_<run>_<t> .
/// ```
pub struct PhysicsToRdf {
    /// Base IRI for generated entities.
    pub base_iri: String,
    /// Include provenance triples (W3C PROV).
    pub include_provenance: bool,
    /// Include digital twin state triples.
    pub include_digital_twin: bool,
    /// Include QUDT unit annotations.
    pub include_units: bool,
}

impl Default for PhysicsToRdf {
    fn default() -> Self {
        Self {
            base_iri: NS_EX.to_string(),
            include_provenance: true,
            include_digital_twin: true,
            include_units: true,
        }
    }
}

impl PhysicsToRdf {
    /// Create a converter with default settings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Build from a `PhysicsToRdfConfig`.
    pub fn from_config(cfg: PhysicsToRdfConfig) -> Self {
        Self {
            base_iri: cfg.base_iri,
            include_provenance: cfg.include_provenance,
            include_digital_twin: cfg.include_digital_twin,
            include_units: cfg.include_units,
        }
    }

    /// Convert a simulation result to a list of RDF triples.
    pub fn convert(&self, result: &SimulationResult) -> Vec<Triple> {
        let mut triples = Vec::new();

        let run_id = sanitize_iri_fragment(&result.simulation_run_id);
        let entity_frag = sanitize_iri_fragment(&result.entity_iri);
        let ts_lit = format!(
            "\"{}\"^^<{}dateTime>",
            result.timestamp.format("%Y-%m-%dT%H:%M:%SZ"),
            NS_XSD
        );

        // ── Digital twin entity ──────────────────────────────────────────────
        if self.include_digital_twin {
            let dt_iri = format!("<{}dt_{}>", self.base_iri, entity_frag);
            triples.push(Triple::new(
                dt_iri.clone(),
                format!("<{}type>", NS_RDF),
                format!("<{}DigitalTwin>", NS_EX),
            ));
            triples.push(Triple::new(
                dt_iri.clone(),
                format!("<{}label>", NS_RDFS),
                format!("\"Digital Twin of {}\"", result.entity_iri),
            ));
        }

        // ── Observations from state trajectory ───────────────────────────────
        for (t_idx, sv) in result.state_trajectory.iter().enumerate() {
            let state_iri = format!("<{}state_{}_t{}>", self.base_iri, run_id, t_idx);

            // Digital twin → state link
            if self.include_digital_twin {
                let dt_iri = format!("<{}dt_{}>", self.base_iri, entity_frag);
                triples.push(Triple::new(
                    dt_iri,
                    format!("<{}hasState>", NS_EX),
                    state_iri.clone(),
                ));
                triples.push(Triple::new(
                    state_iri.clone(),
                    format!("<{}type>", NS_RDF),
                    format!("<{}SimulationState>", NS_EX),
                ));
                triples.push(Triple::new(
                    state_iri.clone(),
                    format!("<{}simulationRunId>", NS_EX),
                    format!("\"{}\"^^<{}string>", result.simulation_run_id, NS_XSD),
                ));
                triples.push(Triple::new(
                    state_iri.clone(),
                    format!("<{}timestamp>", NS_EX),
                    ts_lit.clone(),
                ));
                triples.push(Triple::new(
                    state_iri.clone(),
                    format!("<{}simTime>", NS_PHYS),
                    format!("\"{}\"^^<{}double>", sv.time, NS_XSD),
                ));
            }

            // One SOSA Observation per measured quantity
            for (prop, &val) in &sv.state {
                let prop_frag = sanitize_iri_fragment(prop);
                let obs_iri = format!("<{}obs_{}_{}_{}>", self.base_iri, run_id, prop_frag, t_idx);
                let prop_iri = format!("<{}{}>", NS_PHYS, prop_frag);
                let feature_iri = format!("<{}dt_{}>", self.base_iri, entity_frag);

                // Observation type
                triples.push(Triple::new(
                    obs_iri.clone(),
                    format!("<{}type>", NS_RDF),
                    format!("<{}Observation>", NS_SOSA),
                ));
                // Observed property
                triples.push(Triple::new(
                    obs_iri.clone(),
                    format!("<{}observedProperty>", NS_SOSA),
                    prop_iri.clone(),
                ));
                // Simple result
                triples.push(Triple::new(
                    obs_iri.clone(),
                    format!("<{}hasSimpleResult>", NS_SOSA),
                    format!("\"{}\"^^<{}double>", val, NS_XSD),
                ));
                // Result time
                triples.push(Triple::new(
                    obs_iri.clone(),
                    format!("<{}resultTime>", NS_SOSA),
                    ts_lit.clone(),
                ));
                // Feature of interest
                triples.push(Triple::new(
                    obs_iri.clone(),
                    format!("<{}hasFeatureOfInterest>", NS_SOSA),
                    feature_iri,
                ));

                // Observable property type declaration
                triples.push(Triple::new(
                    prop_iri.clone(),
                    format!("<{}type>", NS_RDF),
                    format!("<{}ObservableProperty>", NS_SOSA),
                ));
                triples.push(Triple::new(
                    prop_iri.clone(),
                    format!("<{}label>", NS_RDFS),
                    format!("\"{}\"", prop),
                ));

                // QUDT unit annotation
                if self.include_units {
                    let qudt_unit = qudt_unit_for(prop);
                    triples.push(Triple::new(
                        obs_iri.clone(),
                        format!("<{}unit>", NS_QUDT),
                        format!("<{}{}>", NS_UNIT, qudt_unit),
                    ));
                    triples.push(Triple::new(
                        obs_iri.clone(),
                        format!("<{}numericValue>", NS_QUDT),
                        format!("\"{}\"^^<{}double>", val, NS_XSD),
                    ));
                }

                // Link observation to state
                if self.include_digital_twin {
                    triples.push(Triple::new(
                        state_iri.clone(),
                        format!("<{}hasObservation>", NS_SSN),
                        obs_iri,
                    ));
                }
            }
        }

        // ── Derived quantities ────────────────────────────────────────────────
        for (prop, &val) in &result.derived_quantities {
            let prop_frag = sanitize_iri_fragment(prop);
            let obs_iri = format!("<{}derived_{}_{}>", self.base_iri, run_id, prop_frag);
            triples.push(Triple::new(
                obs_iri.clone(),
                format!("<{}type>", NS_RDF),
                format!("<{}Observation>", NS_SOSA),
            ));
            triples.push(Triple::new(
                obs_iri.clone(),
                format!("<{}observedProperty>", NS_SOSA),
                format!("<{}{}>", NS_PHYS, prop_frag),
            ));
            triples.push(Triple::new(
                obs_iri.clone(),
                format!("<{}hasSimpleResult>", NS_SOSA),
                format!("\"{}\"^^<{}double>", val, NS_XSD),
            ));
        }

        // ── Provenance ────────────────────────────────────────────────────────
        if self.include_provenance {
            let activity_iri = format!("<{}activity_{}>", self.base_iri, run_id);
            triples.push(Triple::new(
                activity_iri.clone(),
                format!("<{}type>", NS_RDF),
                format!("<{}Activity>", NS_PROV),
            ));
            triples.push(Triple::new(
                activity_iri.clone(),
                format!("<{}startedAtTime>", NS_PROV),
                ts_lit.clone(),
            ));
            triples.push(Triple::new(
                activity_iri.clone(),
                format!("<{}wasAssociatedWith>", NS_PROV),
                format!(
                    "<{}software/{}>",
                    NS_PHYS,
                    sanitize_iri_fragment(&result.provenance.software)
                ),
            ));
            triples.push(Triple::new(
                activity_iri.clone(),
                format!("<{}converged>", NS_PHYS),
                format!(
                    "\"{}\"^^<{}boolean>",
                    result.convergence_info.converged, NS_XSD
                ),
            ));
        }

        triples
    }

    /// Serialise to Turtle format (uses prefix preamble + one triple per line).
    pub fn to_turtle(&self, result: &SimulationResult) -> String {
        let triples = self.convert(result);
        let mut out = turtle_preamble();
        for t in &triples {
            let _ = writeln!(out, "{} .\n", t.to_turtle_statement());
        }
        out
    }

    /// Return triples grouped by subject IRI.
    pub fn to_subject_map(&self, result: &SimulationResult) -> HashMap<String, Vec<Triple>> {
        let mut map: HashMap<String, Vec<Triple>> = HashMap::new();
        for t in self.convert(result) {
            map.entry(t.subject.clone()).or_default().push(t);
        }
        map
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// RdfToPhysics
// ──────────────────────────────────────────────────────────────────────────────

/// Parses a slice of [`Triple`] values to extract physics simulation
/// parameters (boundary conditions, material properties).
///
/// In a production context this would drive `oxirs-core`'s Turtle parser and
/// SPARQL engine.  The current implementation works directly from
/// `Vec<Triple>` to enable zero-copy roundtrips.
///
/// # Supported triple patterns
///
/// **Boundary conditions**:
/// ```text
/// <ex:bc_inlet>  rdf:type     <phys:BoundaryCondition> ;
///                phys:bcType      "inlet" ;
///                phys:bcProperty  "velocity" ;
///                phys:bcValue     "1.5"^^xsd:double ;
///                phys:bcUnit      "M-PER-SEC" .
/// ```
///
/// **Material properties**:
/// ```text
/// <ex:material_steel>  rdf:type    <phys:Material> ;
///                      rdfs:label  "Steel" ;
///                      phys:value  "50.2"^^xsd:double ;
///                      phys:unit   "W-PER-M-K" .
/// ```
pub struct RdfToPhysics {
    /// Physics namespace to look for.
    pub phys_ns: String,
    /// Ignore absence of boundary conditions (best-effort extraction).
    pub lenient: bool,
}

impl Default for RdfToPhysics {
    fn default() -> Self {
        Self {
            phys_ns: NS_PHYS.to_string(),
            lenient: true,
        }
    }
}

impl RdfToPhysics {
    /// Create a parser with default settings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Parse boundary conditions from a triple list.
    pub fn extract_boundary_conditions(
        &self,
        triples: &[Triple],
    ) -> PhysicsResult<Vec<RdfBoundaryCondition>> {
        let by_subject = group_by_subject(triples);
        let mut bcs = Vec::new();

        let bc_type_iri = format!("<{}BoundaryCondition>", NS_EX);
        let bc_type_iri2 = format!("<{}BoundaryCondition>", self.phys_ns);

        for (subj, props) in &by_subject {
            let is_bc = props
                .iter()
                .any(|t| is_rdf_type(t) && (t.object == bc_type_iri || t.object == bc_type_iri2));
            if !is_bc {
                continue;
            }

            let condition_type = find_object_str(props, &format!("<{}bcType>", self.phys_ns))
                .or_else(|| find_object_str(props, &format!("<{}conditionType>", self.phys_ns)))
                .unwrap_or_else(|| "unspecified".to_string());

            let property = find_object_str(props, &format!("<{}bcProperty>", self.phys_ns))
                .or_else(|| find_object_str(props, &format!("<{}observedProperty>", NS_SOSA)))
                .unwrap_or_else(|| "unknown".to_string());

            let value =
                find_object_double(props, &format!("<{}bcValue>", self.phys_ns)).unwrap_or(0.0);

            let unit = find_object_str(props, &format!("<{}bcUnit>", self.phys_ns))
                .unwrap_or_else(|| "UNITLESS".to_string());

            bcs.push(RdfBoundaryCondition {
                iri: subj.clone(),
                condition_type,
                property,
                value,
                unit,
            });
        }

        if bcs.is_empty() && !self.lenient {
            return Err(PhysicsError::ParameterExtraction(
                "no boundary conditions found in triples".to_string(),
            ));
        }
        Ok(bcs)
    }

    /// Extract material properties from the triple list.
    pub fn extract_material_properties(
        &self,
        triples: &[Triple],
    ) -> PhysicsResult<Vec<RdfMaterialProperty>> {
        let by_subject = group_by_subject(triples);
        let mut mats = Vec::new();

        let mat_type_iri = format!("<{}Material>", NS_EX);
        let mat_type_iri2 = format!("<{}Material>", self.phys_ns);

        for (subj, props) in &by_subject {
            let is_mat = props
                .iter()
                .any(|t| is_rdf_type(t) && (t.object == mat_type_iri || t.object == mat_type_iri2));
            if !is_mat {
                continue;
            }

            let name =
                find_object_str(props, &format!("<{}label>", NS_RDFS)).unwrap_or_else(|| {
                    // Fallback: derive from subject IRI
                    let stripped = subj
                        .strip_prefix('<')
                        .and_then(|s| s.strip_suffix('>'))
                        .unwrap_or(subj.as_str());
                    stripped
                        .rsplit(['/', '#'])
                        .next()
                        .unwrap_or("unknown")
                        .to_string()
                });

            let value =
                find_object_double(props, &format!("<{}value>", self.phys_ns)).unwrap_or(0.0);

            let unit = find_object_str(props, &format!("<{}unit>", self.phys_ns))
                .unwrap_or_else(|| "UNITLESS".to_string());

            let description = find_object_str(props, &format!("<{}description>", NS_RDFS));

            mats.push(RdfMaterialProperty {
                iri: subj.clone(),
                name,
                value,
                unit,
                description,
            });
        }
        Ok(mats)
    }

    /// Extract observations (property IRI → value) from a triple list.
    ///
    /// Scans for `sosa:Observation` triples and returns `(property, value)` pairs.
    pub fn extract_observations(&self, triples: &[Triple]) -> Vec<(String, f64)> {
        let by_subject = group_by_subject(triples);
        let obs_type = format!("<{}Observation>", NS_SOSA);
        let rdf_type_pred = format!("<{}type>", NS_RDF);
        let observed_prop_pred = format!("<{}observedProperty>", NS_SOSA);
        let simple_result_pred = format!("<{}hasSimpleResult>", NS_SOSA);

        let mut results = Vec::new();

        for props in by_subject.values() {
            let is_obs = props
                .iter()
                .any(|t| t.predicate == rdf_type_pred && t.object == obs_type);
            if !is_obs {
                continue;
            }
            let prop = find_object_str(props, &observed_prop_pred).unwrap_or_default();
            let value = find_object_double(props, &simple_result_pred);
            if let Some(v) = value {
                results.push((prop, v));
            }
        }
        results
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Helper functions (shared with physics_rdf_serializer)
// ──────────────────────────────────────────────────────────────────────────────

/// Replace characters that are not safe in IRI path segments.
pub(crate) fn sanitize_iri_fragment(s: &str) -> String {
    s.chars()
        .map(|c| match c {
            'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' | '.' => c,
            _ => '_',
        })
        .collect()
}

/// Group triples by subject.
pub(crate) fn group_by_subject(triples: &[Triple]) -> HashMap<String, Vec<Triple>> {
    let mut map: HashMap<String, Vec<Triple>> = HashMap::new();
    for t in triples {
        map.entry(t.subject.clone()).or_default().push(t.clone());
    }
    map
}

/// True if the triple has `rdf:type` as predicate.
pub(crate) fn is_rdf_type(t: &Triple) -> bool {
    t.predicate == format!("<{}type>", NS_RDF)
        || t.predicate == "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>"
        || t.predicate == "a"
}

/// Find the string value of the first triple with matching predicate.
pub(crate) fn find_object_str(props: &[Triple], predicate: &str) -> Option<String> {
    props
        .iter()
        .find(|t| t.predicate == predicate)
        .map(|t| strip_literal_quotes(&t.object))
}

/// Find the double value of the first triple with matching predicate.
pub(crate) fn find_object_double(props: &[Triple], predicate: &str) -> Option<f64> {
    props
        .iter()
        .find(|t| t.predicate == predicate)
        .and_then(|t| extract_double_literal(&t.object))
}

/// Strip Turtle literal quotes and datatype suffix: `"1.5"^^xsd:double` → `1.5`.
pub(crate) fn strip_literal_quotes(s: &str) -> String {
    let trimmed = s.trim();
    let without_dt = if let Some(pos) = trimmed.rfind("^^") {
        &trimmed[..pos]
    } else {
        trimmed
    };
    without_dt.trim_matches('"').to_string()
}

/// Parse a double from a typed Turtle literal: `"1.5"^^xsd:double`.
pub(crate) fn extract_double_literal(s: &str) -> Option<f64> {
    strip_literal_quotes(s).trim().parse::<f64>().ok()
}