use std::collections::BTreeSet;
use serde_json::Value;
use crate::cpn::mcard_token::{PlaceMCard, TokenMCard, TransitionMCard};
use crate::cpn::place_type_lattice::{PlaceRole, PlaceTypeBinding, PlaceTypeLattice};
use crate::cpn::token_namespace::{FiringReceiptMCard, MCardTokenNamespace};
#[derive(Debug, Clone)]
pub struct PlaceSpec<'a> {
pub place_id: &'a str,
pub type_expr: &'a str,
pub role: PlaceRole,
pub binding_type_uri: Option<&'a str>,
}
impl<'a> PlaceSpec<'a> {
pub fn new(place_id: &'a str, type_expr: &'a str, role: PlaceRole) -> Self {
Self { place_id, type_expr, role, binding_type_uri: None }
}
pub fn with_binding_type_uri(mut self, type_uri: &'a str) -> Self {
self.binding_type_uri = Some(type_uri);
self
}
}
pub fn build_place(spec: &PlaceSpec<'_>) -> Result<PlaceMCard, String> {
PlaceMCard::create(spec.place_id, spec.type_expr, None, spec.role.as_str(), Some("blake3"), None)
}
pub fn register_place_specs(
namespace: &mut MCardTokenNamespace,
specs: &[PlaceSpec<'_>],
) -> Result<Vec<PlaceMCard>, String> {
let mut places = Vec::with_capacity(specs.len());
for spec in specs {
let place = build_place(spec)?;
let binding = PlaceTypeBinding::new(
place.clone(),
spec.role,
spec.binding_type_uri.map(|uri| uri.to_string()),
None,
);
namespace.register_place(place.clone(), Some(binding));
places.push(place);
}
Ok(places)
}
#[derive(Debug, Clone)]
pub struct TypedPetriNetKernel {
pub namespace: MCardTokenNamespace,
pub lattice: PlaceTypeLattice,
pub exception_place_id: String,
token_counter: usize,
}
impl TypedPetriNetKernel {
pub fn new(net_id: &str, exception_place_id: Option<&str>) -> Self {
let exc_id = exception_place_id.unwrap_or("p_exception").to_string();
let namespace = MCardTokenNamespace::new(net_id);
let mut exc_set = BTreeSet::new();
exc_set.insert(exc_id.clone());
let lattice = PlaceTypeLattice::from_net(&[], &[], Some(&exc_set));
Self {
namespace,
lattice,
exception_place_id: exc_id,
token_counter: 0,
}
}
pub fn rebuild_lattice(&mut self) -> Result<PlaceTypeLattice, String> {
let places: Vec<PlaceMCard> = self.namespace.places.values().cloned().collect();
let transitions: Vec<TransitionMCard> = self.namespace.transitions.values().cloned().collect();
let mut exc_set = BTreeSet::new();
exc_set.insert(self.exception_place_id.clone());
self.lattice = PlaceTypeLattice::from_net(&places, &transitions, Some(&exc_set));
Ok(self.lattice.clone())
}
pub fn deposit_token(
&mut self,
place_id: &str,
payload: Value,
color_type: &str,
handle: Option<&str>,
) -> Result<TokenMCard, String> {
self.token_counter += 1;
let token_id = match handle {
Some(h) => h.to_string(),
None => format!("tok_{}_{}", place_id, self.token_counter),
};
let token = TokenMCard::from_payload(
payload,
color_type,
&token_id,
Some("blake3"),
None,
)?;
self.namespace.deposit_token(place_id, token.clone(), None, None);
Ok(token)
}
pub fn fire_with_exception_trap<F>(
&mut self,
transition: &TransitionMCard,
handler: F,
) -> Result<FiringReceiptMCard, String>
where
F: Fn(&[Value]) -> Result<Value, String>,
{
if !self.namespace.transitions.contains_key(&transition.transition_id) {
self.namespace.register_transition(transition.clone());
}
let (receipt, _) = self.namespace.fire_in_cordis_fiber(
&transition.transition_id,
None,
Some(&self.exception_place_id),
None,
handler,
Some("blake3"),
)?;
Ok(receipt)
}
}