dimas 0.5.1

dimas - a framework for Distributed Multi Agent Systems
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// Copyright © 2023 Stephan Kunz

//! Primary module of `DiMAS` implementing [`Agent`]
//!
//! An agent is a physical or virtual unit that
//! - can act in an environment
//! - communicates directly with other units/agents
//! - is driven by a set of tendencies (individual goals and/or satisfaction-/survival-mechanisms)
//! - has own resources
//! - can perceive its environment to a limited extent
//! - has no or only a partial representation of its environment
//! - has  capabilities and offers services
//! - can possibly reproduce itself
//! - whose behaviour is aimed at fulfilling its objectives,
//!   taking into account the resources and capabilities available to it and
//!   depending on its perception, representations and abilities
//!
//! # Examples
//! ```rust,no_run
//! use dimas::prelude::*;
//! use core::time::Duration;
//!
//! #[derive(Debug)]
//! struct AgentProps {}
//!
//! // we need a tokio runtime
//! #[dimas::main]
//! async fn main() -> Result<()> {
//!   // create & initialize agents properties
//!   let properties = AgentProps {};
//!
//!   // create an agent with the properties and a default configuration
//!   let mut agent = Agent::new(properties).config(&Config::default())?;
//!
//!   // configuration of the agent
//!   // ...
//!
//!   // start the agent
//!   agent.start().await?;
//!   Ok(())
//! }
//! ```
//!
//! A running agent can be properly stopped with `ctrl-c`
//!

// region:		--- modules
use crate::context::ContextImpl;
use crate::error::Error;
use chrono::Local;
use core::{fmt::Debug, time::Duration};
use dimas_com::builder::LivelinessSubscriberBuilder;
use dimas_com::builder::{
	ObservableBuilder, ObserverBuilder, PublisherBuilder, QuerierBuilder, QueryableBuilder,
	SubscriberBuilder,
};
use dimas_com::traits::LivelinessSubscriber;
use dimas_com::traits::{Observer, Publisher, Querier, Responder};
use dimas_commands::messages::{AboutEntity, PingEntity};
use dimas_config::Config;
use dimas_core::{
	Result,
	builder_states::{NoCallback, NoInterval, NoSelector, Storage},
	enums::{OperationState, Signal, TaskSignal},
	message_types::{Message, QueryMsg},
	traits::{Capability, Context, ContextAbstraction},
};
use dimas_time::{Timer, TimerBuilder};
use std::sync::Arc;
use std::sync::RwLock;
use tokio::{select, signal, sync::mpsc};
use tracing::{error, info, warn};
use zenoh::liveliness::LivelinessToken;
// endregion:	--- modules

// region:	   --- callbacks
async fn callback_dispatcher<P>(ctx: Context<P>, request: QueryMsg) -> Result<()>
where
	P: Send + Sync + 'static,
{
	if let Some(value) = request.payload() {
		let content: Vec<u8> = value.to_bytes().into_owned();
		let msg = Message::new(content);
		let signal: Signal = Message::decode(msg)?;
		match signal {
			Signal::About => about_handler(ctx, request)?,
			Signal::Ping { sent } => ping_handler(ctx, request, sent)?,
			Signal::Shutdown => shutdown_handler(ctx, request)?,
			Signal::State { state } => state_handler(ctx, request, state)?,
		}
	}
	Ok(())
}

fn about_handler<P>(ctx: Context<P>, request: QueryMsg) -> Result<()>
where
	P: Send + Sync + 'static,
{
	let name = ctx
		.fq_name()
		.unwrap_or_else(|| String::from("--"));
	let mode = ctx.mode().clone();
	let zid = ctx.uuid();
	let state = ctx.state();
	let value = AboutEntity::new(name, mode, zid, state);
	drop(ctx);
	request.reply(value)?;
	Ok(())
}

fn ping_handler<P>(ctx: Context<P>, request: QueryMsg, sent: i64) -> Result<()>
where
	P: Send + Sync + 'static,
{
	let now = Local::now()
		.naive_utc()
		.and_utc()
		.timestamp_nanos_opt()
		.unwrap_or(0);

	let name = ctx
		.fq_name()
		.unwrap_or_else(|| String::from("--"));
	let zid = ctx.uuid();
	let value = PingEntity::new(name, zid, now - sent);
	drop(ctx);
	request.reply(value)?;
	Ok(())
}

fn shutdown_handler<P>(ctx: Context<P>, request: QueryMsg) -> Result<()>
where
	P: Send + Sync + 'static,
{
	// send back current infos
	let name = ctx
		.fq_name()
		.unwrap_or_else(|| String::from("--"));
	let mode = ctx.mode().clone();
	let zid = ctx.uuid();
	let state = ctx.state();
	let value = AboutEntity::new(name, mode, zid, state);
	request.reply(value)?;

	// shutdown agent after a short wait time to be able to send response
	tokio::task::spawn(async move {
		tokio::time::sleep(Duration::from_millis(10)).await;
		// gracefully end agent
		let _ = ctx.set_state(OperationState::Standby);
		tokio::time::sleep(Duration::from_millis(100)).await;
		let _ = ctx.set_state(OperationState::Created);
		let _ = ctx.sender().send(TaskSignal::Shutdown).await;
	});
	Ok(())
}

fn state_handler<P>(ctx: Context<P>, request: QueryMsg, state: Option<OperationState>) -> Result<()>
where
	P: Send + Sync + 'static,
{
	// is a state value given?
	if let Some(value) = state {
		let _ = ctx.set_state(value);
	}

	// send back result
	let name = ctx
		.fq_name()
		.unwrap_or_else(|| String::from("--"));
	let mode = ctx.mode().clone();
	let zid = ctx.uuid();
	let state = ctx.state();
	let value = AboutEntity::new(name, mode, zid, state);
	drop(ctx);
	request.reply(value)?;
	Ok(())
}
// endregion:	--- callbacks

// region:	   --- UnconfiguredAgent
/// A new Agent without the basic configuration decisions
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub struct UnconfiguredAgent<P>
where
	P: Debug + Send + Sync + 'static,
{
	name: Option<String>,
	prefix: Option<String>,
	props: P,
}

impl<P> UnconfiguredAgent<P>
where
	P: Debug + Send + Sync + 'static,
{
	/// Constructor
	const fn new(properties: P) -> Self {
		Self {
			name: None,
			prefix: None,
			props: properties,
		}
	}

	/// Set a name
	#[must_use]
	pub fn name(mut self, name: impl Into<String>) -> Self {
		self.name = Some(name.into());
		self
	}

	/// Set a prefix
	#[must_use]
	pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
		self.prefix = Some(prefix.into());
		self
	}

	/// Set the [`Config`]uration.
	/// An agent with [`OperationState`] `Configured` can be started
	/// and will respond to commands from dimasctl/dimasmon
	///
	/// # Errors
	///
	pub fn config(self, config: &Config) -> Result<Agent<P>> {
		// we need an mpsc channel with a receiver behind a mutex guard
		let (tx, rx) = mpsc::channel(32);
		let context: Arc<ContextImpl<P>> = Arc::new(ContextImpl::new(
			config,
			self.props,
			self.name,
			tx,
			self.prefix,
		)?);

		let agent = Agent {
			rx,
			context,
			liveliness: false,
			liveliness_token: RwLock::new(None),
		};

		// add signal queryables
		// for zid
		let selector = format!("{}/signal", agent.context.uuid());
		agent
			.queryable()
			.selector(&selector)
			.callback(callback_dispatcher)
			.activation_state(OperationState::Created)
			.add()?;
		// for fully qualified name
		if let Some(fq_name) = agent.context.fq_name() {
			let selector = format!("{fq_name}/*");
			agent
				.queryable()
				.selector(&selector)
				.callback(callback_dispatcher)
				.activation_state(OperationState::Created)
				.add()?;
		}

		// set [`OperationState`] to Created
		// This will also start the basic queryables
		agent.context.set_state(OperationState::Created)?;

		Ok(agent)
	}
}
// endregion:   --- UnconfiguredAgent

// region:	   --- Agent
/// An Agent with the basic configuration decisions fixed, but not running
pub struct Agent<P>
where
	P: Debug + Send + Sync + 'static,
{
	/// A reciever for signals from tasks
	rx: mpsc::Receiver<TaskSignal>,
	/// The agents context structure
	context: Arc<ContextImpl<P>>,
	/// Flag to control whether sending liveliness or not
	liveliness: bool,
	/// The liveliness token - typically the uuid sent to other participants.
	/// Is available in the [`LivelinessSubscriber`] callback
	liveliness_token: RwLock<Option<LivelinessToken>>,
}

impl<P> Debug for Agent<P>
where
	P: Debug + Send + Sync + 'static,
{
	#[allow(clippy::or_fun_call)]
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("Agent")
			.field("id", &self.context.uuid())
			.field(
				"prefix",
				self.context
					.prefix()
					.unwrap_or(&"None".to_string()),
			)
			.field("name", &self.context.name())
			.finish_non_exhaustive()
	}
}

impl<P> Agent<P>
where
	P: Debug + Send + Sync + 'static,
{
	/// Builder
	#[allow(clippy::new_ret_no_self)]
	pub const fn new(properties: P) -> UnconfiguredAgent<P> {
		UnconfiguredAgent::new(properties)
	}

	/// Activate sending liveliness information
	pub const fn liveliness(&mut self, activate: bool) {
		self.liveliness = activate;
	}

	/// Get a [`LivelinessSubscriberBuilder`], the builder for a `LivelinessSubscriber`.
	#[must_use]
	pub fn liveliness_subscriber(
		&self,
	) -> LivelinessSubscriberBuilder<P, NoCallback, Storage<Box<dyn LivelinessSubscriber>>> {
		LivelinessSubscriberBuilder::new("default", self.context.clone())
			.storage(self.context.liveliness_subscribers())
	}

	/// Get a [`LivelinessSubscriberBuilder`], the builder for a `LivelinessSubscriber`.
	#[must_use]
	pub fn liveliness_subscriber_for(
		&self,
		session_id: impl Into<String>,
	) -> LivelinessSubscriberBuilder<P, NoCallback, Storage<Box<dyn LivelinessSubscriber>>> {
		LivelinessSubscriberBuilder::new(session_id, self.context.clone())
			.storage(self.context.liveliness_subscribers())
	}

	/// Get an [`ObservableBuilder`], the builder for an `Observable`.
	#[must_use]
	pub fn observable(
		&self,
	) -> ObservableBuilder<
		P,
		NoSelector,
		NoCallback,
		NoCallback,
		NoCallback,
		Storage<Box<dyn Responder>>,
	> {
		ObservableBuilder::new("default", self.context.clone()).storage(self.context.responders())
	}

	/// Get an [`ObservableBuilder`], the builder for an `Observable`.
	#[must_use]
	pub fn observable_for(
		&self,
		session_id: impl Into<String>,
	) -> ObservableBuilder<
		P,
		NoSelector,
		NoCallback,
		NoCallback,
		NoCallback,
		Storage<Box<dyn Responder>>,
	> {
		ObservableBuilder::new(session_id, self.context.clone()).storage(self.context.responders())
	}

	/// Get an [`ObserverBuilder`], the builder for an `Observer`.
	#[must_use]
	pub fn observer(
		&self,
	) -> ObserverBuilder<P, NoSelector, NoCallback, NoCallback, Storage<Box<dyn Observer>>> {
		ObserverBuilder::new("default", self.context.clone()).storage(self.context.observers())
	}

	/// Get an [`ObserverBuilder`], the builder for an `Observer`.
	#[must_use]
	pub fn observer_for(
		&self,
		session_id: impl Into<String>,
	) -> ObserverBuilder<P, NoSelector, NoCallback, NoCallback, Storage<Box<dyn Observer>>> {
		ObserverBuilder::new(session_id, self.context.clone()).storage(self.context.observers())
	}

	/// Get a [`PublisherBuilder`], the builder for a s`Publisher`.
	#[must_use]
	pub fn publisher(&self) -> PublisherBuilder<P, NoSelector, Storage<Box<dyn Publisher>>> {
		PublisherBuilder::new("default", self.context.clone()).storage(self.context.publishers())
	}

	/// Get a [`PublisherBuilder`], the builder for a s`Publisher`.
	#[must_use]
	pub fn publisher_for(
		&self,
		session_id: impl Into<String>,
	) -> PublisherBuilder<P, NoSelector, Storage<Box<dyn Publisher>>> {
		PublisherBuilder::new(session_id, self.context.clone()).storage(self.context.publishers())
	}

	/// Get a [`QuerierBuilder`], the builder for a `Querier`.
	#[must_use]
	pub fn querier(&self) -> QuerierBuilder<P, NoSelector, NoCallback, Storage<Box<dyn Querier>>> {
		QuerierBuilder::new("default", self.context.clone()).storage(self.context.queriers())
	}

	/// Get a [`QuerierBuilder`], the builder for a `Querier`.
	#[must_use]
	pub fn querier_for(
		&self,
		session_id: impl Into<String>,
	) -> QuerierBuilder<P, NoSelector, NoCallback, Storage<Box<dyn Querier>>> {
		QuerierBuilder::new(session_id, self.context.clone()).storage(self.context.queriers())
	}

	/// Get a [`QueryableBuilder`], the builder for a `Queryable`.
	#[must_use]
	pub fn queryable(
		&self,
	) -> QueryableBuilder<P, NoSelector, NoCallback, Storage<Box<dyn Responder>>> {
		QueryableBuilder::new("default", self.context.clone()).storage(self.context.responders())
	}

	/// Get a [`QueryableBuilder`], the builder for a `Queryable`.
	#[must_use]
	pub fn queryable_for(
		&self,
		session_id: impl Into<String>,
	) -> QueryableBuilder<P, NoSelector, NoCallback, Storage<Box<dyn Responder>>> {
		QueryableBuilder::new(session_id, self.context.clone()).storage(self.context.responders())
	}

	/// Get a [`SubscriberBuilder`], the builder for a `Subscriber`.
	#[must_use]
	pub fn subscriber(
		&self,
	) -> SubscriberBuilder<P, NoSelector, NoCallback, Storage<Box<dyn Responder>>> {
		SubscriberBuilder::new("default", self.context.clone()).storage(self.context.responders())
	}

	/// Get a [`SubscriberBuilder`], the builder for a `Subscriber`.
	#[must_use]
	pub fn subscriber_for(
		&self,
		session_id: impl Into<String>,
	) -> SubscriberBuilder<P, NoSelector, NoCallback, Storage<Box<dyn Responder>>> {
		SubscriberBuilder::new(session_id, self.context.clone()).storage(self.context.responders())
	}

	/// Get a [`TimerBuilder`], the builder for a [`Timer`].
	#[must_use]
	pub fn timer(&self) -> TimerBuilder<P, NoSelector, NoInterval, NoCallback, Storage<Timer<P>>> {
		TimerBuilder::new(self.context.clone()).storage(self.context.timers())
	}

	/// Start the agent.
	///
	/// The agent can be stopped properly using `ctrl-c`
	///
	/// # Errors
	///
	/// # Panics
	#[tracing::instrument(skip_all)]
	pub async fn start(self) -> Result<Self> {
		// activate sending liveliness
		if self.liveliness {
			let session = self.context.session("default");
			let token_str = self.context.prefix().map_or_else(
				|| self.context.uuid(),
				|prefix| format!("{}/{}", prefix, self.context.uuid()),
			);

			let token = session
				.expect("snh")
				.liveliness()
				.declare_token(&token_str)
				.await
				.map_err(|source| Error::ActivateLiveliness { source })?;

			self.liveliness_token
				.write()
				.map_err(|_| Error::ModifyStruct("liveliness".into()))?
				.replace(token);
		}

		self.context.set_state(OperationState::Active)?;

		RunningAgent {
			rx: self.rx,
			context: self.context,
			liveliness: self.liveliness,
			liveliness_token: self.liveliness_token,
		}
		.run()
		.await
	}
}
// endregion:   --- Agent

// region:	   --- RunningAgent
/// A running Agent, which can't be modified while running
#[allow(clippy::module_name_repetitions)]
pub struct RunningAgent<P>
where
	P: Debug + Send + Sync + 'static,
{
	/// The receiver for signals from tasks
	rx: mpsc::Receiver<TaskSignal>,
	/// The agents context structure
	context: Arc<ContextImpl<P>>,
	/// Flag to control whether sending liveliness or not
	liveliness: bool,
	/// The liveliness token - typically the uuid sent to other participants.
	/// Is available in the [`LivelinessSubscriber`] callback
	liveliness_token: RwLock<Option<LivelinessToken>>,
}

impl<P> RunningAgent<P>
where
	P: Debug + Send + Sync + 'static,
{
	/// run
	async fn run(mut self) -> Result<Agent<P>> {
		loop {
			// different possibilities that can happen
			select! {
				// `TaskSignal`s
				Some(signal) = self.rx.recv() => {
					match signal {
						TaskSignal::RestartLiveliness(selector) => {
							self.context.liveliness_subscribers()
								.write()
								.map_err(|_| Error::WriteAccess)?
								.get_mut(&selector)
								.ok_or_else(|| Error::GetMut("liveliness".into()))?
								.manage_operation_state(&self.context.state())?;
						},
						TaskSignal::RestartQueryable(selector) => {
							self.context.responders()
								.write()
								.map_err(|_| Error::WriteAccess)?
								.get_mut(&selector)
								.ok_or_else(|| Error::GetMut("queryables".into()))?
								.manage_operation_state(&self.context.state())?;
						},
						TaskSignal::RestartObservable(selector) => {
							self.context.responders()
								.write()
								.map_err(|_| Error::WriteAccess)?
								.get_mut(&selector)
								.ok_or_else(|| Error::GetMut("observables".into()))?
								.manage_operation_state(&self.context.state())?;
						},
						TaskSignal::RestartSubscriber(selector) => {
							self.context.responders()
								.write()
								.map_err(|_| Error::WriteAccess)?
								.get_mut(&selector)
								.ok_or_else(|| Error::GetMut("subscribers".into()))?
								.manage_operation_state(&self.context.state())?;
						},
						TaskSignal::RestartTimer(selector) => {
							self.context.timers()
								.write()
								.map_err(|_| Error::WriteAccess)?
								.get_mut(&selector)
								.ok_or_else(|| Error::GetMut("timers".into()))?
								.manage_operation_state(&self.context.state())?;
						},
						TaskSignal::Shutdown => {
							return self.stop();
						}
					}
				}

				// shutdown signal "ctrl-c"
				signal = signal::ctrl_c() => {
					match signal {
						Ok(()) => {
							info!("shutdown due to 'ctrl-c'");
							return self.stop();
						}
						Err(err) => {
							error!("Unable to listen for 'Ctrl-C': {err}");
							// we also try to shut down the agent properly
							return self.stop();
						}
					}
				}
			}
		}
	}

	/// Stop the agent
	///
	/// # Errors
	#[tracing::instrument(skip_all)]
	pub fn stop(self) -> Result<Agent<P>> {
		self.context.set_state(OperationState::Created)?;

		// stop liveliness
		if self.liveliness {
			self.liveliness_token
				.write()
				.map_err(|_| Error::ModifyStruct("liveliness".into()))?
				.take();
		}
		let r = Agent {
			rx: self.rx,
			context: self.context,
			liveliness: self.liveliness,
			liveliness_token: self.liveliness_token,
		};
		Ok(r)
	}
}
// endregion:   --- RunningAgent

#[cfg(test)]
mod tests {
	use super::*;

	// check, that the auto traits are available
	const fn is_normal<T: Sized + Send + Sync>() {}

	#[derive(Debug)]
	struct Props {}

	#[test]
	const fn normal_types() {
		is_normal::<UnconfiguredAgent<Props>>();
		is_normal::<Agent<Props>>();
		is_normal::<RunningAgent<Props>>();
		is_normal::<TaskSignal>();
	}
}