Skip to content

Commit 2565920

Browse files
committed
Finish workflow spawning implementation
Signed-off-by: Michael X. Grey <grey@openrobotics.org>
1 parent f03c7ca commit 2565920

File tree

4 files changed

+59
-27
lines changed

4 files changed

+59
-27
lines changed

src/operation/scope.rs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ use crate::{
2222
Cancellation, Unreachability, InspectDisposals, execute_operation,
2323
BufferSettings, Buffer, CancellableBundle, OperationRoster, ManageCancellation,
2424
OperationError, OperationCancel, Cancel, UnhandledErrors, check_reachability,
25-
Blocker, Stream, StreamTargetStorage, StreamRequest,
25+
Blocker, Stream, StreamTargetStorage, StreamRequest, AddOperation,
26+
ScopeSettings,
2627
};
2728

2829
use backtrace::Backtrace;
2930

30-
use bevy::prelude::{Component, Entity, World};
31+
use bevy::prelude::{Component, Entity, World, Commands};
3132

3233
use smallvec::SmallVec;
3334

@@ -46,6 +47,7 @@ impl ParentSession {
4647
}
4748
}
4849

50+
#[derive(Clone)]
4951
pub(crate) struct OperateScope<Request, Response, Streams> {
5052
/// The first node that is inside of the scope
5153
enter_scope: Entity,
@@ -64,13 +66,12 @@ pub(crate) struct OperateScope<Request, Response, Streams> {
6466
}
6567

6668
impl<Request, Response, Streams> OperateScope<Request, Response, Streams> {
67-
pub(crate) fn new(
68-
enter_scope: Entity,
69-
terminal: Entity,
70-
exit_scope: Option<Entity>,
71-
finish_cancel: Entity,
72-
) -> Self {
73-
Self { enter_scope, terminal, exit_scope, finish_cancel, _ignore: Default::default() }
69+
pub(crate) fn terminal(&self) -> Entity {
70+
self.terminal
71+
}
72+
73+
pub(crate) fn enter_scope(&self) -> Entity {
74+
self.enter_scope
7475
}
7576
}
7677

@@ -139,7 +140,7 @@ impl TerminalStorage {
139140
}
140141
}
141142

142-
impl<Request, Streams, Response> Operation for OperateScope<Request, Streams, Response>
143+
impl<Request, Response, Streams> Operation for OperateScope<Request, Response, Streams>
143144
where
144145
Request: 'static + Send + Sync,
145146
Streams: StreamPack,
@@ -294,12 +295,37 @@ where
294295
Ok(())
295296
}
296297

297-
impl<Request, Streams, Response> OperateScope<Request, Streams, Response>
298+
impl<Request, Response, Streams> OperateScope<Request, Response, Streams>
298299
where
299300
Request: 'static + Send + Sync,
300-
Streams: StreamPack,
301301
Response: 'static + Send + Sync,
302+
Streams: StreamPack,
302303
{
304+
pub(crate) fn new(
305+
scope_id: Entity,
306+
exit_scope: Option<Entity>,
307+
settings: ScopeSettings,
308+
commands: &mut Commands,
309+
) -> Self {
310+
let enter_scope = commands.spawn(()).id();
311+
let terminal = commands.spawn(()).id();
312+
let finish_cancel = commands.spawn(()).id();
313+
commands.add(AddOperation::new(
314+
finish_cancel,
315+
FinishCancel { from_scope: scope_id },
316+
));
317+
318+
let scope = OperateScope {
319+
enter_scope,
320+
terminal,
321+
exit_scope,
322+
finish_cancel,
323+
_ignore: Default::default(),
324+
};
325+
326+
scope
327+
}
328+
303329
fn receive_cancel(
304330
OperationCancel {
305331
cancel: Cancel { source: _origin, target: source, session, cancellation },
@@ -810,7 +836,6 @@ pub(crate) struct FinishCancel {
810836

811837
impl Operation for FinishCancel {
812838
fn setup(self, OperationSetup { source, world }: OperationSetup) -> OperationResult {
813-
// world.get_entity_mut(entity)
814839
world.entity_mut(source).insert((
815840
CancelFromScope(self.from_scope),
816841
InputBundle::<()>::new(),

src/service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ impl<Request, Response, Streams> Service<Request, Response, Streams> {
195195
/// following:
196196
/// - App::add_*_service
197197
/// - Commands::spawn_*_service
198+
/// - Commands::spawn_workflow
198199
/// - ServiceDiscovery::iter()
199200
fn new(entity: Entity) -> Self {
200201
Self { entity, _ignore: Default::default() }

src/service/workflow.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
OrBroken, Input, ManageInput, DeliveryInstructions, ParentSession,
2121
OperationError, Delivery, DeliveryOrder, DeliveryUpdate, Blocker,
2222
OperationRoster, Disposal, Cancellation, Cancel, Deliver, SingleTargetStorage,
23-
ExitTargetStorage, ExitTarget,
23+
ExitTargetStorage, ExitTarget, Service,
2424
begin_scope, dispose_for_despawned_service, insert_new_order, emit_disposal,
2525
pop_next_delivery,
2626
};
@@ -34,10 +34,16 @@ struct WorkflowStorage {
3434
scope: Entity,
3535
}
3636

37-
struct WorkflowService<Request, Response, Streams> {
37+
pub(crate) struct WorkflowService<Request, Response, Streams> {
3838
_ignore: std::marker::PhantomData<(Request, Response, Streams)>,
3939
}
4040

41+
impl<Request, Response, Streams> WorkflowService<Request, Response, Streams> {
42+
pub(crate) fn cast(scope_id: Entity) -> Service<Request, Response, Streams> {
43+
Service { entity: scope_id, _ignore: Default::default() }
44+
}
45+
}
46+
4147
impl<Request, Response, Streams> ServiceTrait for WorkflowService<Request, Response, Streams>
4248
where
4349
Request: 'static + Send + Sync,

src/workflow.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use bevy::{
2222

2323
use crate::{
2424
Service, InputSlot, Output, StreamPack, AddOperation, OperateScope,
25-
Terminate,
25+
Terminate, WorkflowService,
2626
};
2727

2828
/// Trait to allow workflows to be spawned from a [`Commands`] or a [`World`].
@@ -138,7 +138,7 @@ impl WorkflowSettings {
138138

139139
/// Transform the settings to be uninterruptible
140140
pub fn uninterruptible(mut self) -> Self {
141-
self.scope.uninterruptible = true;
141+
self.scope.set_uninterruptible(true);
142142
self
143143
}
144144
}
@@ -196,6 +196,7 @@ impl ScopeSettings {
196196
}
197197
}
198198

199+
199200
impl<'w, 's> SpawnWorkflow for Commands<'w, 's> {
200201
fn spawn_workflow<Request, Response, Streams>(
201202
&mut self,
@@ -208,13 +209,10 @@ impl<'w, 's> SpawnWorkflow for Commands<'w, 's> {
208209
Streams: StreamPack,
209210
{
210211
let scope_id = self.spawn(()).id();
211-
let enter_scope = self.spawn(()).id();
212-
let terminal = self.spawn(()).id();
213-
self.add(AddOperation::new(
214-
scope_id,
215-
OperateScope::new(enter_scope, terminal, None, self.spawn(()).id()),
216-
));
217-
self.add(AddOperation::new(terminal, Terminate::new()));
212+
let scope = OperateScope::<Request, Response, Streams>::new(
213+
scope_id, None, settings.scope, self,
214+
);
215+
self.add(AddOperation::new(scope.terminal(), Terminate::<Response>::new()));
218216

219217
let (
220218
stream_storage,
@@ -223,13 +221,15 @@ impl<'w, 's> SpawnWorkflow for Commands<'w, 's> {
223221
self.entity(scope_id).insert(stream_storage);
224222

225223
let scope = Scope {
226-
input: Output::new(scope_id, enter_scope),
227-
terminate: InputSlot::new(scope_id, terminal),
224+
input: Output::new(scope_id, scope.enter_scope()),
225+
terminate: InputSlot::new(scope_id, scope.terminal()),
228226
streams,
229227
};
230228

231-
let builder = Builder { scope: scope_id, commands: self };
229+
let mut builder = Builder { scope: scope_id, commands: self };
232230
build(scope, &mut builder);
231+
232+
WorkflowService::<Request, Response, Streams>::cast(scope_id)
233233
}
234234
}
235235

0 commit comments

Comments
 (0)