Skip to main content

wasm4pm_types/
event_log.rs

1use serde::{Deserialize, Serialize};
2use chrono::{DateTime, FixedOffset};
3use uuid::Uuid;
4
5/// Possible attribute values according to the XES Standard
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
7#[serde(tag = "type", content = "content")]
8pub enum AttributeValue {
9    String(String),
10    Date(DateTime<FixedOffset>),
11    Int(i64),
12    Float(f64),
13    Boolean(bool),
14    ID(Uuid),
15    List(Vec<Attribute>),
16    Container(Vec<Attribute>),
17    None(),
18}
19
20impl AttributeValue {
21    pub fn as_string(&self) -> Option<&str> {
22        match self {
23            AttributeValue::String(s) => Some(s.as_str()),
24            _ => None,
25        }
26    }
27
28    pub fn as_i64(&self) -> Option<i64> {
29        match self {
30            AttributeValue::Int(i) => Some(*i),
31            _ => None,
32        }
33    }
34
35    pub fn as_f64(&self) -> Option<f64> {
36        match self {
37            AttributeValue::Float(f) => Some(*f),
38            _ => None,
39        }
40    }
41
42    pub fn as_bool(&self) -> Option<bool> {
43        match self {
44            AttributeValue::Boolean(b) => Some(*b),
45            _ => None,
46        }
47    }
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
51pub struct Attribute {
52    pub key: String,
53    pub value: AttributeValue,
54    pub own_attributes: Option<Vec<Attribute>>,
55}
56
57impl Attribute {
58    pub fn new(key: String, value: AttributeValue) -> Self {
59        Attribute {
60            key,
61            value,
62            own_attributes: None,
63        }
64    }
65}
66
67pub type Attributes = Vec<Attribute>;
68
69pub trait XESEditableAttribute {
70    fn add_to_attributes(&mut self, key: String, value: AttributeValue);
71    fn add_attribute(&mut self, attr: Attribute);
72    fn get_by_key(&self, key: &str) -> Option<&Attribute>;
73    fn get_by_key_mut(&mut self, key: &str) -> Option<&mut Attribute>;
74}
75
76impl XESEditableAttribute for Attributes {
77    fn add_to_attributes(&mut self, key: String, value: AttributeValue) {
78        self.push(Attribute::new(key, value));
79    }
80
81    fn add_attribute(&mut self, attr: Attribute) {
82        self.push(attr);
83    }
84
85    fn get_by_key(&self, key: &str) -> Option<&Attribute> {
86        self.iter().find(|a| a.key == key)
87    }
88
89    fn get_by_key_mut(&mut self, key: &str) -> Option<&mut Attribute> {
90        self.iter_mut().find(|a| a.key == key)
91    }
92}
93
94/// A single event in a trace
95#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
96pub struct Event {
97    pub attributes: Attributes,
98}
99
100impl Event {
101    pub fn new(attributes: Attributes) -> Self {
102        Event { attributes }
103    }
104
105    pub fn with_activity(activity: &str) -> Self {
106        let mut attributes = Vec::new();
107        attributes.add_to_attributes("concept:name".to_string(), AttributeValue::String(activity.to_string()));
108        Event { attributes }
109    }
110
111    pub fn get_activity(&self, key: &str) -> Option<String> {
112        self.attributes
113            .get_by_key(key)
114            .and_then(|a| a.value.as_string().map(|s| s.to_string()))
115    }
116}
117
118/// A trace (sequence of events for one case)
119#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
120pub struct Trace {
121    pub attributes: Attributes,
122    pub events: Vec<Event>,
123}
124
125impl Trace {
126    pub fn new(case_id: String, events: Vec<Event>) -> Self {
127        let mut attributes = Vec::new();
128        attributes.add_to_attributes("concept:name".to_string(), AttributeValue::String(case_id));
129        Trace {
130            attributes,
131            events,
132        }
133    }
134
135    pub fn len(&self) -> usize {
136        self.events.len()
137    }
138
139    pub fn is_empty(&self) -> bool {
140        self.events.is_empty()
141    }
142}
143
144/// An event log (collection of traces)
145#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
146pub struct EventLog {
147    pub attributes: Attributes,
148    pub traces: Vec<Trace>,
149    pub extensions: Option<Vec<EventLogExtension>>,
150    pub classifiers: Option<Vec<EventLogClassifier>>,
151    pub global_trace_attrs: Option<Attributes>,
152    pub global_event_attrs: Option<Attributes>,
153}
154
155impl EventLog {
156    pub fn new(traces: Vec<Trace>, attributes: Attributes) -> Self {
157        EventLog {
158            traces,
159            attributes,
160            ..Default::default()
161        }
162    }
163
164    pub fn len(&self) -> usize {
165        self.traces.len()
166    }
167
168    pub fn is_empty(&self) -> bool {
169        self.traces.is_empty()
170    }
171
172    pub fn event_count(&self) -> usize {
173        self.traces.iter().map(|t| t.len()).sum()
174    }
175}
176
177#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
178pub struct EventLogExtension {
179    pub name: String,
180    pub prefix: String,
181    pub uri: String,
182}
183
184#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
185pub struct EventLogClassifier {
186    pub name: String,
187    pub keys: Vec<String>,
188}