use std::error::Error;
use std::fmt::Write;
use crate::base::{foreign_class, ClassDef, Element, FastMap, FastSet, FieldValue, Value};
use crate::convert_nc::Prefixes;
use crate::decode::{CimDataset, FieldForm};
use crate::registry::type_registry;
const RDF: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
pub(crate) const DM: &str = "http://iec.ch/TC57/61970-552/DifferenceModel/1#";
pub(crate) const DCATCIM: &str = "https://cim4.eu/ns/dcatcim#";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Block {
Forward,
Reverse,
Preconditions,
}
#[derive(Clone)]
pub struct Differences {
pub header: String,
pub forward: CimDataset,
pub reverse: CimDataset,
pub preconditions: CimDataset,
pub blocks: Vec<Block>,
}
impl Differences {
pub(crate) fn new(header: &str) -> Self {
Self {
header: header.to_string(),
forward: CimDataset::new(),
reverse: CimDataset::new(),
preconditions: CimDataset::new(),
blocks: Vec::new(),
}
}
pub(crate) fn block_mut(&mut self, which: Block) -> &mut CimDataset {
if !self.blocks.contains(&which) {
self.blocks.push(which);
}
match which {
Block::Forward => &mut self.forward,
Block::Reverse => &mut self.reverse,
Block::Preconditions => &mut self.preconditions,
}
}
pub fn between(before: &CimDataset, after: &CimDataset) -> Self {
let mut d = Self::new("");
d.blocks = vec![Block::Reverse, Block::Forward];
let ids: FastSet<&str> = before
.entries
.keys()
.chain(before.foreign.keys())
.chain(after.entries.keys())
.chain(after.foreign.keys())
.map(String::as_str)
.collect();
let mut ids: Vec<&str> = ids.into_iter().collect();
ids.sort_unstable();
for id in ids {
let (was, now) = (lookup(before, id), lookup(after, id));
match (was, now) {
(Some((b, foreign)), None) => put(&mut d.reverse, b.clone(), foreign),
(None, Some((a, foreign))) => put(&mut d.forward, a.clone(), foreign),
(Some((b, foreign)), Some((a, _))) => {
let gone = without(b, a);
let came = without(a, b);
let types = |e: &Element| e.types().map(|c| c.qualified).collect::<FastSet<_>>();
let (tb, ta) = (types(b), types(a));
let lost: Vec<&'static ClassDef> = b.types().filter(|c| !ta.contains(c.qualified)).collect();
let gained: Vec<&'static ClassDef> = a.types().filter(|c| !tb.contains(c.qualified)).collect();
if !gone.is_empty() || !lost.is_empty() {
put(&mut d.reverse, statements(id, &lost, gone), foreign);
named(&mut d.reverse, before, id, b);
}
if !came.is_empty() || !gained.is_empty() {
put(&mut d.forward, statements(id, &gained, came), foreign);
named(&mut d.forward, after, id, a);
}
}
(None, None) => {}
}
}
d
}
}
fn lookup<'a>(ds: &'a CimDataset, id: &str) -> Option<(&'a Element, bool)> {
ds.entries
.get(id)
.or_else(|| ds.entries.get(fragment(id)))
.map(|e| (e, false))
.or_else(|| ds.foreign.get(id).map(|e| (e, true)))
}
fn fragment(id: &str) -> &str {
id.rsplit_once('#').map_or(id, |(_, f)| if f.is_empty() { id } else { f })
}
fn put(ds: &mut CimDataset, element: Element, foreign: bool) {
let id = element.mrid().to_string();
if foreign || element.class().qualified.starts_with(RDF) {
ds.foreign.insert(id, element);
} else {
ds.set(id, element);
}
}
fn without(a: &Element, b: &Element) -> Vec<(&'static str, FieldValue)> {
let mut out = Vec::new();
let mut keys: Vec<&'static str> = a.fields().keys().copied().collect();
keys.sort_unstable();
for key in keys {
for v in a.fields()[key].values() {
if !b.has_value(key, &v.owned()) {
out.push((key, v.owned()));
}
}
}
out
}
fn statements(id: &str, types: &[&'static ClassDef], values: Vec<(&'static str, FieldValue)>) -> Element {
let mut e = Element::new(foreign_class(RDF, "Description"), id.to_string());
e.set_types(types);
e.mark_described();
for (key, value) in values {
e.add_field(key, value);
}
e
}
fn named(block: &mut CimDataset, from: &CimDataset, id: &str, like: &Element) {
let Some(e) = block.foreign.get(id).filter(|e| untyped(e)) else { return };
let reg = type_registry();
let class = e.class().qualified;
for &key in e.fields().keys() {
let form = from.field_forms.get(&(like.type_name(), key)).copied().unwrap_or_default();
let ns = form.ns.or_else(|| reg.attr_of(like.class(), key).map(|a| a.ns));
let resource_ns = form.resource_ns;
block.field_forms.insert((class, key), FieldForm { ns, datatype: form.datatype, resource_ns });
}
}
pub(crate) fn write_blocks(out: &mut String, prefixes: &mut Prefixes, d: &Differences, ns: &'static str) -> Result<(), Box<dyn Error>> {
let nc = ns == DCATCIM;
let prefix = prefixes.of(ns);
for &which in &d.blocks {
let (name, ds) = match (which, nc) {
(Block::Forward, false) => ("forwardDifferences", &d.forward),
(Block::Forward, true) => ("forwardDifference", &d.forward),
(Block::Reverse, false) => ("reverseDifferences", &d.reverse),
(Block::Reverse, true) => ("reverseDifference", &d.reverse),
(Block::Preconditions, false) => ("preconditions", &d.preconditions),
(Block::Preconditions, true) => continue,
};
let mut inner = String::new();
crate::convert::write_objects(&mut inner, prefixes, ds)?;
if inner.is_empty() {
write!(out, "\n <{prefix}:{name} rdf:parseType=\"Statements\"/>")?;
} else {
write!(out, "\n <{prefix}:{name} rdf:parseType=\"Statements\">\n{inner} </{prefix}:{name}>")?;
}
}
Ok(())
}
fn sorted(ds: &CimDataset) -> Vec<(&String, &Element)> {
let mut all: Vec<(&String, &Element)> = ds.entries.iter().chain(&ds.foreign).collect();
all.sort_unstable_by_key(|(id, _)| id.as_str());
all
}
fn untyped(e: &Element) -> bool {
e.class().qualified == "http://www.w3.org/1999/02/22-rdf-syntax-ns#Description"
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Statement {
Type(&'static str),
Value(&'static str, FieldValue),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Unmatched {
pub block: Block,
pub subject: String,
pub statement: Statement,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FieldChange {
pub key: &'static str,
pub before: Option<FieldValue>,
pub after: Option<FieldValue>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Changed {
pub id: String,
pub retyped: Option<(&'static str, &'static str)>,
pub fields: Vec<FieldChange>,
}
#[derive(Debug, Default)]
pub struct Applied {
pub added: Vec<String>,
pub removed: Vec<Element>,
pub changed: Vec<Changed>,
pub unmatched: Vec<Unmatched>,
pub untyped: Vec<String>,
}
impl CimDataset {
pub fn apply(&mut self, d: &Differences) -> Applied {
let mut applied = Applied::default();
let miss = |applied: &mut Applied, block, subject: &str, statement| {
applied.unmatched.push(Unmatched { block, subject: subject.to_string(), statement });
};
for (id, e) in sorted(&d.preconditions) {
let held = lookup(self, id).map(|(o, _)| o);
if !untyped(e) {
for t in e.types() {
if !held.is_some_and(|o| o.types().any(|c| c.qualified == t.qualified)) {
miss(&mut applied, Block::Preconditions, id, Statement::Type(t.qualified));
}
}
}
for (key, fv) in e.fields() {
for v in fv.values() {
if !held.is_some_and(|o| o.has_value(key, &v.owned())) {
miss(&mut applied, Block::Preconditions, id, Statement::Value(key, v.owned()));
}
}
}
}
let mut touched: Vec<&str> = d
.reverse
.entries
.keys()
.chain(d.reverse.foreign.keys())
.chain(d.forward.entries.keys())
.chain(d.forward.foreign.keys())
.map(String::as_str)
.collect::<FastSet<&str>>()
.into_iter()
.collect();
touched.sort_unstable();
let mut was: FastMap<&str, Option<(Element, bool)>> = FastMap::default();
let mut types: FastMap<&str, Vec<&'static ClassDef>> = FastMap::default();
for &id in &touched {
let found = lookup(self, id).map(|(e, f)| (e.clone(), f));
types.insert(id, found.as_ref().map_or_else(Vec::new, |(e, _)| e.types().collect()));
was.insert(id, found);
}
for (id, e) in sorted(&d.reverse) {
let id = id.as_str();
let Some(object) = self.object_mut(id) else {
if !untyped(e) {
for t in e.types() {
miss(&mut applied, Block::Reverse, id, Statement::Type(t.qualified));
}
}
for (key, fv) in e.fields() {
for v in fv.values() {
miss(&mut applied, Block::Reverse, id, Statement::Value(key, v.owned()));
}
}
continue;
};
let mut missed = Vec::new();
for (key, fv) in e.fields() {
for v in fv.values() {
if !object.remove_value(key, &v.owned()) {
missed.push(Statement::Value(key, v.owned()));
}
}
}
if !untyped(e) {
let held = types.get_mut(id).expect("touched");
for t in e.types() {
match held.iter().position(|c| c.qualified == t.qualified) {
Some(i) => {
held.remove(i);
}
None => missed.push(Statement::Type(t.qualified)),
}
}
}
for statement in missed {
miss(&mut applied, Block::Reverse, id, statement);
}
}
for (id, e) in sorted(&d.forward) {
let id = id.as_str();
let stated: Vec<&'static ClassDef> = if untyped(e) { Vec::new() } else { e.types().collect() };
let held = types.get_mut(id).expect("touched");
for t in &stated {
if !held.iter().any(|c| c.qualified == t.qualified) {
held.push(t);
}
}
if self.object_mut(id).is_none() {
let Some(&class) = stated.first() else {
for (key, fv) in e.fields() {
for v in fv.values() {
miss(&mut applied, Block::Forward, id, Statement::Value(key, v.owned()));
}
}
continue;
};
let mut created = Element::new(class, e.mrid().to_string());
if !e.defined() {
created.mark_described();
}
let foreign = d.forward.foreign.contains_key(id);
put(self, created, foreign);
}
let object = self.object_mut(id).expect("just made");
for (key, fv) in e.fields() {
for v in fv.values() {
let value = v.owned();
if !object.has_value(key, &value) {
object.add_field(key, value);
}
}
}
}
for &id in &touched {
let now_types = &types[id];
let Some((key, foreign)) = self.key_of(id) else { continue };
match &was[id] {
None => applied.added.push(key),
Some((before, _)) => {
let object = if foreign { &self.foreign[&key] } else { &self.entries[&key] };
if now_types.is_empty() {
if object.fields().is_empty() {
let removed = if foreign { self.foreign.remove(&key) } else { self.remove(&key) };
applied.removed.extend(removed.map(|_| before.clone()));
} else {
applied.untyped.push(key.clone());
if let Some(c) = changes(&key, before, object) {
applied.changed.push(c);
}
}
continue;
}
let mut object = object.clone();
if !now_types.iter().map(|c| c.qualified).eq(object.types().map(|c| c.qualified)) {
object.set_types(now_types);
}
if let Some(c) = changes(&key, before, &object) {
applied.changed.push(c);
}
if foreign {
self.foreign.insert(key, object);
} else {
self.set(key, object);
}
}
}
}
applied.added.sort_unstable();
applied.added.dedup();
applied
}
fn object_mut(&mut self, id: &str) -> Option<&mut Element> {
let (key, foreign) = self.key_of(id)?;
if foreign { self.foreign.get_mut(&key) } else { self.entries.get_mut(&key) }
}
fn key_of(&self, id: &str) -> Option<(String, bool)> {
if self.entries.contains_key(id) {
Some((id.to_string(), false))
} else if self.entries.contains_key(fragment(id)) {
Some((fragment(id).to_string(), false))
} else if self.foreign.contains_key(id) {
Some((id.to_string(), true))
} else {
None
}
}
}
fn changes(id: &str, before: &Element, after: &Element) -> Option<Changed> {
let retyped = (before.class().qualified != after.class().qualified).then(|| (before.class().qualified, after.class().qualified));
let mut keys: Vec<&'static str> = before.fields().keys().chain(after.fields().keys()).copied().collect::<FastSet<_>>().into_iter().collect();
keys.sort_unstable();
let same = |a: Option<&FieldValue>, b: Option<&FieldValue>| match (a, b) {
(None, None) => true,
(Some(a), Some(b)) => {
let mut x: Vec<Value> = a.values().collect();
let mut y: Vec<Value> = b.values().collect();
x.sort_unstable();
y.sort_unstable();
x == y
}
_ => false,
};
let fields: Vec<FieldChange> = keys
.into_iter()
.filter(|k| !same(before.fields().get(k), after.fields().get(k)))
.map(|k| FieldChange { key: k, before: before.fields().get(k).cloned(), after: after.fields().get(k).cloned() })
.collect();
(retyped.is_some() || !fields.is_empty()).then(|| Changed { id: id.to_string(), retyped, fields })
}