use crate::mrid::Mrid;
pub const MD_NS: &str = "http://iec.ch/TC57/61970-552/ModelDescription/1#";
pub const DM_NS: &str = "http://iec.ch/TC57/61970-552/DifferenceModel/1#";
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum ModelKind {
#[default]
Full,
Difference,
}
#[derive(Clone, Debug, Default)]
pub struct ModelHeader {
pub kind: ModelKind,
pub id: Option<Mrid>,
pub created: Option<String>,
pub scenario_time: Option<String>,
pub description: Option<String>,
pub version: Option<String>,
pub modeling_authority_set: Option<String>,
pub profiles: Vec<String>,
pub dependent_on: Vec<Mrid>,
pub supersedes: Vec<Mrid>,
pub extra: Vec<HeaderProperty>,
pub source: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HeaderProperty {
pub prefix: String,
pub ns: String,
pub local: String,
pub value: HeaderValue,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum HeaderValue {
Text(String),
Resource(String),
}
impl HeaderProperty {
pub fn text(prefix: &str, ns: &str, local: &str, value: impl Into<String>) -> HeaderProperty {
HeaderProperty {
prefix: prefix.to_owned(),
ns: ns.to_owned(),
local: local.to_owned(),
value: HeaderValue::Text(value.into()),
}
}
pub fn resource(
prefix: &str,
ns: &str,
local: &str,
value: impl Into<String>,
) -> HeaderProperty {
HeaderProperty {
prefix: prefix.to_owned(),
ns: ns.to_owned(),
local: local.to_owned(),
value: HeaderValue::Resource(value.into()),
}
}
pub fn as_str(&self) -> &str {
match &self.value {
HeaderValue::Text(s) | HeaderValue::Resource(s) => s,
}
}
}
impl ModelHeader {
pub fn new(kind: ModelKind) -> ModelHeader {
ModelHeader {
kind,
..Default::default()
}
}
pub fn declares_profile(&self, profile_iri: &str) -> bool {
let stem = |s: &str| s.rsplit_once('/').map(|(a, _)| a).unwrap_or(s).to_owned();
let want = stem(profile_iri);
self.profiles
.iter()
.any(|p| p == profile_iri || stem(p) == want)
}
pub fn primary_profile_name(&self) -> Option<&str> {
let iri = self.profiles.first()?;
let without_version = iri.rsplit_once('/').map(|(a, _)| a).unwrap_or(iri);
without_version.rsplit('/').next()
}
}
#[derive(Clone, Debug, Default)]
pub struct DifferenceModel {
pub header: ModelHeader,
pub preconditions: Vec<Statement>,
pub reverse: Vec<Statement>,
pub forward: Vec<Statement>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Statement {
pub subject: Mrid,
pub class: Option<QualifiedName>,
pub defines_subject: bool,
pub predicate_ns: String,
pub predicate: String,
pub value: StatementValue,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct QualifiedName {
pub ns: String,
pub local: String,
}
#[derive(Clone, Debug, PartialEq)]
pub enum StatementValue {
Literal(String),
Resource(String),
}
impl DifferenceModel {
pub fn affected_objects(&self) -> Vec<Mrid> {
let mut seen = Vec::new();
for s in self.reverse.iter().chain(&self.forward) {
if !seen.contains(&s.subject) {
seen.push(s.subject.clone());
}
}
seen
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_matching_ignores_the_version_segment() {
let h = ModelHeader {
profiles: vec!["http://iec.ch/TC57/ns/CIM/CoreEquipment-EU/3.0".into()],
..Default::default()
};
assert!(h.declares_profile("http://iec.ch/TC57/ns/CIM/CoreEquipment-EU/3.0"));
assert!(h.declares_profile("http://iec.ch/TC57/ns/CIM/CoreEquipment-EU/4.0"));
assert!(!h.declares_profile("http://iec.ch/TC57/ns/CIM/Topology-EU/3.0"));
assert_eq!(h.primary_profile_name(), Some("CoreEquipment-EU"));
}
#[test]
fn affected_objects_are_deduplicated_in_order() {
let mk = |m: &str, p: &str| Statement {
subject: Mrid::parse(m),
class: None,
defines_subject: false,
predicate_ns: "http://iec.ch/TC57/CIM100#".into(),
predicate: p.into(),
value: StatementValue::Literal("1".into()),
};
let a = "70c4656c-f7a0-4319-98bb-84fb5e2e9b37";
let b = "c4e8eb3a-76b0-4192-91fe-c2f0a60af0df";
let d = DifferenceModel {
reverse: vec![mk(a, "X.y"), mk(b, "X.y")],
forward: vec![mk(a, "X.z")],
..Default::default()
};
assert_eq!(d.affected_objects(), vec![Mrid::parse(a), Mrid::parse(b)]);
}
}