Preserve exchange allocations during atomic replacement and reserve generated aliases
This commit is contained in:
+95
-24
@@ -251,6 +251,20 @@ pub(crate) fn functional_exchange_commands(
|
||||
) -> Result<Vec<Command>, ModelError> {
|
||||
vacant(p, request.id)?;
|
||||
let mut working = p.clone();
|
||||
let mut relation = Relation {
|
||||
id: request.id,
|
||||
alias: request.alias.clone(),
|
||||
name: request.name.clone(),
|
||||
description: request.description.clone(),
|
||||
body: RelationBody::new(
|
||||
RelationKind::FunctionalExchange,
|
||||
request.source,
|
||||
request.target,
|
||||
),
|
||||
};
|
||||
// Reserve the authored alias as well as identity before naming generated
|
||||
// endpoints. This draft exists only in the planner's private candidate.
|
||||
working.relations.insert(relation.id, relation.clone());
|
||||
let mut commands = Vec::new();
|
||||
let label = if request.name.trim().is_empty() {
|
||||
"Exchange"
|
||||
@@ -289,19 +303,14 @@ pub(crate) fn functional_exchange_commands(
|
||||
&request.items,
|
||||
&mut commands,
|
||||
)?;
|
||||
commands.push(Command::CreateRelation(Relation {
|
||||
id: request.id,
|
||||
alias: request.alias.clone(),
|
||||
name: request.name.clone(),
|
||||
description: request.description.clone(),
|
||||
body: RelationBody::FunctionalExchange {
|
||||
source: request.source,
|
||||
target: request.target,
|
||||
source_port,
|
||||
target_port,
|
||||
items: request.items.clone(),
|
||||
},
|
||||
}));
|
||||
relation.body = RelationBody::FunctionalExchange {
|
||||
source: request.source,
|
||||
target: request.target,
|
||||
source_port,
|
||||
target_port,
|
||||
items: request.items.clone(),
|
||||
};
|
||||
commands.push(Command::CreateRelation(relation));
|
||||
Ok(commands)
|
||||
}
|
||||
fn add_allocation(
|
||||
@@ -392,6 +401,14 @@ pub fn plan_component_exchange(
|
||||
target_port,
|
||||
} => {
|
||||
vacant(&working, *id)?;
|
||||
let mut relation = Relation {
|
||||
id: *id,
|
||||
alias: alias.clone(),
|
||||
name: name.clone(),
|
||||
description: description.clone(),
|
||||
body: RelationBody::new(RelationKind::BoundaryExchange, Id::nil(), Id::nil()),
|
||||
};
|
||||
working.relations.insert(*id, relation.clone());
|
||||
let contract = |outgoing| PortContract::Component {
|
||||
kind: *port_kind,
|
||||
orientation: if *port_kind == ComponentPortKind::Standard {
|
||||
@@ -424,17 +441,11 @@ pub fn plan_component_exchange(
|
||||
contract(false),
|
||||
)?;
|
||||
let b = select_port(&mut working, *target_port, input_port, items, &mut commands)?;
|
||||
let relation = Relation {
|
||||
id: *id,
|
||||
alias: alias.clone(),
|
||||
name: name.clone(),
|
||||
description: description.clone(),
|
||||
body: RelationBody::BoundaryExchange {
|
||||
source: a,
|
||||
target: b,
|
||||
oriented: *oriented,
|
||||
items: items.clone(),
|
||||
},
|
||||
relation.body = RelationBody::BoundaryExchange {
|
||||
source: a,
|
||||
target: b,
|
||||
oriented: *oriented,
|
||||
items: items.clone(),
|
||||
};
|
||||
working.relations.insert(*id, relation.clone());
|
||||
commands.push(Command::CreateRelation(relation));
|
||||
@@ -646,6 +657,66 @@ pub fn plan_function_port_items(
|
||||
finish(p, vec![Command::EditElement(port, e)])
|
||||
}
|
||||
|
||||
/// Whether removing these allocation facts breaks a typed exchange mapping.
|
||||
/// Legacy or malformed endpoints are left to their own validation rules; this
|
||||
/// helper only determines the conditional dependency on PortAllocation facts.
|
||||
pub(crate) fn missing_exchange_port_allocations(
|
||||
p: &Project,
|
||||
relation: &Relation,
|
||||
removed: &BTreeSet<Id>,
|
||||
) -> bool {
|
||||
let Some(Relation {
|
||||
body:
|
||||
RelationBody::BoundaryExchange {
|
||||
source: a,
|
||||
target: b,
|
||||
oriented,
|
||||
..
|
||||
},
|
||||
..
|
||||
}) = p.relations.get(&relation.source())
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
let Some(Relation {
|
||||
body:
|
||||
RelationBody::FunctionalExchange {
|
||||
source_port,
|
||||
target_port,
|
||||
..
|
||||
},
|
||||
..
|
||||
}) = p.relations.get(&relation.target())
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
if ![*a, *b].iter().all(|id| {
|
||||
p.elements
|
||||
.get(id)
|
||||
.is_some_and(|e| matches!(port_contract(e), Some(PortContract::Component { .. })))
|
||||
}) || !p
|
||||
.elements
|
||||
.get(source_port)
|
||||
.is_some_and(|e| matches!(port_contract(e), Some(PortContract::FunctionOutput { .. })))
|
||||
|| !p
|
||||
.elements
|
||||
.get(target_port)
|
||||
.is_some_and(|e| matches!(port_contract(e), Some(PortContract::FunctionInput { .. })))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let allocated = |component, function| {
|
||||
p.relations.values().any(|allocation| {
|
||||
allocation.kind() == RelationKind::PortAllocation
|
||||
&& !removed.contains(&allocation.id)
|
||||
&& allocation.source() == component
|
||||
&& allocation.target() == function
|
||||
})
|
||||
};
|
||||
!((allocated(*a, *source_port) && allocated(*b, *target_port))
|
||||
|| (!*oriented && allocated(*b, *source_port) && allocated(*a, *target_port)))
|
||||
}
|
||||
|
||||
pub(crate) fn validate_port(p: &Project, e: &Element, out: &mut Vec<Issue>) {
|
||||
let Some(SharedBody::Port { contract, legacy }) = e.shared() else {
|
||||
return;
|
||||
|
||||
@@ -203,6 +203,9 @@ pub fn remap_references(p: &mut Project, old: Id, new: Id) {
|
||||
}
|
||||
/// IDs removed by a cascade. Cross-layer traced counterparts are never included.
|
||||
pub fn deletion_impact(p: &Project, id: Id) -> BTreeSet<Id> {
|
||||
deletion_impact_inner(p, id, false)
|
||||
}
|
||||
fn deletion_impact_inner(p: &Project, id: Id, defer_exchange_allocations: bool) -> BTreeSet<Id> {
|
||||
let mut removed = BTreeSet::from([id]);
|
||||
loop {
|
||||
let before = removed.len();
|
||||
@@ -254,12 +257,15 @@ pub fn deletion_impact(p: &Project, id: Id) -> BTreeSet<Id> {
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
if p.relations.values().any(|allocation| {
|
||||
allocation.kind() == RelationKind::PortAllocation
|
||||
&& removed.contains(&allocation.id)
|
||||
&& [boundary.source(), boundary.target()].contains(&allocation.source())
|
||||
&& [*source_port, *target_port].contains(&allocation.target())
|
||||
}) {
|
||||
if !defer_exchange_allocations
|
||||
&& p.relations.values().any(|allocation| {
|
||||
allocation.kind() == RelationKind::PortAllocation
|
||||
&& removed.contains(&allocation.id)
|
||||
&& [boundary.source(), boundary.target()].contains(&allocation.source())
|
||||
&& [*source_port, *target_port].contains(&allocation.target())
|
||||
})
|
||||
&& crate::ports::missing_exchange_port_allocations(p, r, &removed)
|
||||
{
|
||||
removed.insert(r.id);
|
||||
}
|
||||
}
|
||||
@@ -288,6 +294,40 @@ pub fn deletion_impact(p: &Project, id: Id) -> BTreeSet<Id> {
|
||||
}
|
||||
pub fn delete_cascade(p: &mut Project, id: Id) {
|
||||
let removed = deletion_impact(p, id);
|
||||
remove_objects(p, removed);
|
||||
}
|
||||
/// Preserve conditional exchange-allocation dependents until the transaction
|
||||
/// has had the opportunity to supply explicit replacement allocation facts.
|
||||
pub(crate) fn delete_cascade_deferred(p: &mut Project, id: Id, pending: &mut BTreeSet<Id>) {
|
||||
let complete = deletion_impact(p, id);
|
||||
let removed = deletion_impact_inner(p, id, true);
|
||||
// An explicitly removed relation cannot pass its conditional cascade on
|
||||
// to a new relation that later reuses the same identity in this batch.
|
||||
pending.retain(|id| !removed.contains(id));
|
||||
pending.extend(
|
||||
complete
|
||||
.difference(&removed)
|
||||
.filter(|id| {
|
||||
p.relations
|
||||
.get(id)
|
||||
.is_some_and(|r| r.kind() == RelationKind::ExchangeAllocation)
|
||||
})
|
||||
.copied(),
|
||||
);
|
||||
remove_objects(p, removed);
|
||||
}
|
||||
pub(crate) fn finish_deletions(p: &mut Project, pending: &BTreeSet<Id>) {
|
||||
let none = BTreeSet::new();
|
||||
for id in pending {
|
||||
if p.relations.get(id).is_some_and(|r| {
|
||||
r.kind() == RelationKind::ExchangeAllocation
|
||||
&& crate::ports::missing_exchange_port_allocations(p, r, &none)
|
||||
}) {
|
||||
delete_cascade(p, *id);
|
||||
}
|
||||
}
|
||||
}
|
||||
fn remove_objects(p: &mut Project, removed: BTreeSet<Id>) {
|
||||
let removed_names: std::collections::BTreeMap<_, _> = removed
|
||||
.iter()
|
||||
.map(|id| (*id, p.named(*id).to_string()))
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeSet;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Clone, Error, Serialize, Deserialize)]
|
||||
@@ -135,7 +136,9 @@ impl Session {
|
||||
}
|
||||
pub fn execute(&mut self, command: Command) -> Result<Option<Id>, ModelError> {
|
||||
let mut candidate = self.project.clone();
|
||||
let selected = apply_command(&mut candidate, &command, 0)?;
|
||||
let mut pending_deletions = BTreeSet::new();
|
||||
let selected = apply_command(&mut candidate, &command, 0, &mut pending_deletions)?;
|
||||
crate::references::finish_deletions(&mut candidate, &pending_deletions);
|
||||
validate_candidate(&candidate)?;
|
||||
if candidate != self.project {
|
||||
self.past
|
||||
@@ -161,9 +164,11 @@ pub fn validate_candidate(p: &Project) -> Result<(), ModelError> {
|
||||
}
|
||||
pub fn execute_batch(project: &Project, commands: &[Command]) -> Result<Project, ModelError> {
|
||||
let mut candidate = project.clone();
|
||||
let mut pending_deletions = BTreeSet::new();
|
||||
for command in commands {
|
||||
apply_command(&mut candidate, command, 0)?;
|
||||
apply_command(&mut candidate, command, 0, &mut pending_deletions)?;
|
||||
}
|
||||
crate::references::finish_deletions(&mut candidate, &pending_deletions);
|
||||
validate_candidate(&candidate)?;
|
||||
Ok(candidate)
|
||||
}
|
||||
@@ -224,7 +229,12 @@ fn attach(p: &mut Project, id: Id) {
|
||||
}
|
||||
}
|
||||
}
|
||||
fn apply_command(p: &mut Project, c: &Command, depth: usize) -> Result<Option<Id>, ModelError> {
|
||||
fn apply_command(
|
||||
p: &mut Project,
|
||||
c: &Command,
|
||||
depth: usize,
|
||||
pending_deletions: &mut BTreeSet<Id>,
|
||||
) -> Result<Option<Id>, ModelError> {
|
||||
if depth > 64 {
|
||||
return Err(ModelError(
|
||||
"Command batch nesting exceeds 64 levels.".into(),
|
||||
@@ -233,12 +243,13 @@ fn apply_command(p: &mut Project, c: &Command, depth: usize) -> Result<Option<Id
|
||||
let selected = match c {
|
||||
Command::Replace(value) => {
|
||||
*p = value.clone();
|
||||
pending_deletions.clear();
|
||||
None
|
||||
}
|
||||
Command::Batch(commands) => {
|
||||
let mut selected = None;
|
||||
for command in commands {
|
||||
let id = apply_command(p, command, depth + 1)?;
|
||||
let id = apply_command(p, command, depth + 1, pending_deletions)?;
|
||||
if id.is_some() {
|
||||
selected = id;
|
||||
}
|
||||
@@ -318,6 +329,9 @@ fn apply_command(p: &mut Project, c: &Command, depth: usize) -> Result<Option<Id
|
||||
v.id = *old;
|
||||
p.relations.insert(*old, v);
|
||||
reidentify(p, *old, value.id)?;
|
||||
if pending_deletions.remove(old) {
|
||||
pending_deletions.insert(value.id);
|
||||
}
|
||||
Some(value.id)
|
||||
}
|
||||
Command::CreateDiagram(value) => {
|
||||
@@ -438,6 +452,9 @@ fn apply_command(p: &mut Project, c: &Command, depth: usize) -> Result<Option<Id
|
||||
}
|
||||
Command::Reidentify(old, new) => {
|
||||
reidentify(p, *old, *new)?;
|
||||
if pending_deletions.remove(old) {
|
||||
pending_deletions.insert(*new);
|
||||
}
|
||||
Some(*new)
|
||||
}
|
||||
Command::Rename(id, value) => {
|
||||
@@ -469,7 +486,7 @@ fn apply_command(p: &mut Project, c: &Command, depth: usize) -> Result<Option<Id
|
||||
items: vec![],
|
||||
},
|
||||
)?;
|
||||
return apply_command(p, &Command::Batch(commands), depth + 1);
|
||||
return apply_command(p, &Command::Batch(commands), depth + 1, pending_deletions);
|
||||
}
|
||||
Some(p.relate(*kind, name, *source, *target))
|
||||
}
|
||||
@@ -480,7 +497,7 @@ fn apply_command(p: &mut Project, c: &Command, depth: usize) -> Result<Option<Id
|
||||
if !p.contains(*id) {
|
||||
return Err(missing());
|
||||
}
|
||||
crate::references::delete_cascade(p, *id);
|
||||
crate::references::delete_cascade_deferred(p, *id, pending_deletions);
|
||||
None
|
||||
}
|
||||
Command::AddStep(owner, exchange) => {
|
||||
@@ -519,7 +536,7 @@ fn apply_command(p: &mut Project, c: &Command, depth: usize) -> Result<Option<Id
|
||||
.and_then(Element::steps)
|
||||
.and_then(|s| s.get(*index))
|
||||
.ok_or_else(|| ModelError("Step index is out of range.".into()))?;
|
||||
crate::references::delete_cascade(p, id);
|
||||
crate::references::delete_cascade_deferred(p, id, pending_deletions);
|
||||
Some(*owner)
|
||||
}
|
||||
Command::MoveStep(owner, from, to) => {
|
||||
|
||||
Reference in New Issue
Block a user