Skip to content

Commit

Permalink
refactor(nodes): move complete pipeline out of runtime and remove exe…
Browse files Browse the repository at this point in the history
…cution planner (#639)
  • Loading branch information
maxjoehnk authored Jul 11, 2024
1 parent 22c81ee commit 201b912
Show file tree
Hide file tree
Showing 63 changed files with 1,152 additions and 2,538 deletions.
16 changes: 1 addition & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ members = [
"crates/runtime/debug-ui",
"crates/runtime/debug-ui/impl",
"crates/runtime/debug-ui/egui",
"crates/runtime/execution-planner",
"crates/runtime/injector",
"crates/runtime/layouts",
"crates/runtime/layouts/commands",
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/handlers/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ impl<R: RuntimeApi> NodesHandler<R> {
#[tracing::instrument(skip(self))]
#[profiling::function]
pub fn duplicate_nodes(&self, request: DuplicateNodesRequest) -> anyhow::Result<Vec<NodePath>> {
let nodes = self.runtime.run_command(DuplicateNodesCommand {
let paths = self.runtime.run_command(DuplicateNodesCommand {
paths: request.paths.into_iter().map(NodePath::from).collect(),
parent: request.parent.map(NodePath::from),
})?;

Ok(nodes.into_iter().map(|node| node.path).collect())
Ok(paths)
}

#[tracing::instrument(skip(self))]
Expand Down
2 changes: 1 addition & 1 deletion crates/components/fixtures/commands/src/add_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<'a> Command<'a> for AddGroupCommand {

fn apply(
&self,
(fixture_manager): (&FixtureManager),
fixture_manager: &FixtureManager,
) -> anyhow::Result<(Self::Result, Self::State)> {
let group_id = fixture_manager.add_group(self.name.clone());
let group = Group {
Expand Down
76 changes: 31 additions & 45 deletions crates/components/fixtures/commands/src/delete_fixtures.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use mizer_commander::{sub_command, Command, Ref, RefMut};
use serde::{Deserialize, Serialize};

use mizer_commander::{Command, Ref, sub_command, SubCommand, SubCommandRunner};
use mizer_fixtures::fixture::Fixture;
use mizer_fixtures::manager::FixtureManager;
use mizer_layouts::LayoutStorage;
use mizer_nodes::{Node, NodeDowncast};
use mizer_nodes::FixtureNode;
use mizer_runtime::commands::DeleteNodesCommand;
use mizer_runtime::pipeline_access::PipelineAccess;
use mizer_runtime::ExecutionPlanner;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use mizer_runtime::Pipeline;

#[derive(Debug, Deserialize, Serialize)]
pub struct DeleteFixturesCommand {
Expand All @@ -17,11 +15,10 @@ pub struct DeleteFixturesCommand {
impl<'a> Command<'a> for DeleteFixturesCommand {
type Dependencies = (
Ref<FixtureManager>,
RefMut<PipelineAccess>,
RefMut<ExecutionPlanner>,
Ref<LayoutStorage>,
Ref<Pipeline>,
SubCommand<DeleteNodesCommand>,
);
type State = Vec<(Fixture, sub_command!(DeleteNodesCommand))>;
type State = (Vec<Fixture>, sub_command!(DeleteNodesCommand));
type Result = ();

fn label(&self) -> String {
Expand All @@ -36,53 +33,42 @@ impl<'a> Command<'a> for DeleteFixturesCommand {

fn apply(
&self,
(fixture_manager, pipeline, planner, layout_storage): (
(fixture_manager, pipeline, delete_node_runner): (
&FixtureManager,
&mut PipelineAccess,
&mut ExecutionPlanner,
&LayoutStorage,
&Pipeline,
SubCommandRunner<DeleteNodesCommand>,
),
) -> anyhow::Result<(Self::Result, Self::State)> {
let mut nodes = pipeline
.nodes_view
.iter()
.filter_map(|node| {
if let Node::Fixture(fixture_node) = node.downcast() {
Some((node.key().clone(), fixture_node))
} else {
None
}
})
.filter(|(_, node)| self.fixture_ids.contains(&node.fixture_id))
.map(|(path, node)| (node.fixture_id, DeleteNodesCommand { paths: vec![path] }))
.collect::<HashMap<_, _>>();
let nodes =
pipeline.find_node_paths::<FixtureNode>(|node| self.fixture_ids.contains(&node.fixture_id));
let cmd = DeleteNodesCommand {
paths: nodes.into_iter().cloned().collect(),
};

let mut states = Vec::new();
let mut fixtures = Vec::new();

for (fixture, command) in self.fixture_ids.iter().filter_map(|fixture_id| {
let fixture = fixture_manager.delete_fixture(*fixture_id);
let fixture_node = nodes.remove(fixture_id);

fixture.zip(fixture_node)
}) {
let (_, state) = command.apply((pipeline, planner, layout_storage))?;
states.push((fixture, (command, state)));
for fixture in self
.fixture_ids
.iter()
.filter_map(|fixture_id| fixture_manager.delete_fixture(*fixture_id))
{
fixtures.push(fixture);
}
let (_, state) = delete_node_runner.apply(cmd)?;

Ok(((), states))
Ok(((), (fixtures, state)))
}

fn revert(
&self,
(fixture_manager, pipeline, planner, layout_storage): (
(fixture_manager, _, delete_node_runner): (
&FixtureManager,
&mut PipelineAccess,
&mut ExecutionPlanner,
&LayoutStorage,
&Pipeline,
SubCommandRunner<DeleteNodesCommand>,
),
state: Self::State,
(fixtures, sub_cmd): Self::State,
) -> anyhow::Result<()> {
for (fixture, (delete_node_cmd, state)) in state {
for fixture in fixtures {
fixture_manager.add_fixture(
fixture.id,
fixture.name,
Expand All @@ -92,8 +78,8 @@ impl<'a> Command<'a> for DeleteFixturesCommand {
fixture.universe.into(),
fixture.configuration,
);
delete_node_cmd.revert((pipeline, planner, layout_storage), state)?;
}
delete_node_runner.revert(sub_cmd)?;

Ok(())
}
Expand Down
58 changes: 20 additions & 38 deletions crates/components/fixtures/commands/src/delete_group.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
use mizer_commander::{Command, Ref, RefMut};
use serde::{Deserialize, Serialize};

use mizer_commander::{Command, Ref, sub_command, SubCommand, SubCommandRunner};
use mizer_fixtures::GroupId;
use mizer_fixtures::manager::FixtureManager;
use mizer_fixtures::programmer::Group;
use mizer_fixtures::GroupId;
use mizer_layouts::LayoutStorage;
use mizer_nodes::{Node, NodeDowncast};
use mizer_nodes::GroupNode;
use mizer_runtime::commands::DeleteNodesCommand;
use mizer_runtime::pipeline_access::PipelineAccess;
use mizer_runtime::ExecutionPlanner;
use serde::{Deserialize, Serialize};
use mizer_runtime::Pipeline;

#[derive(Debug, Deserialize, Serialize)]
pub struct DeleteGroupCommand {
pub id: GroupId,
}

impl<'a> Command<'a> for DeleteGroupCommand {
type Dependencies = (
Ref<FixtureManager>,
RefMut<PipelineAccess>,
RefMut<ExecutionPlanner>,
Ref<LayoutStorage>,
);
type Dependencies = (Ref<FixtureManager>, Ref<Pipeline>, SubCommand<DeleteNodesCommand>);
type State = (
Group,
DeleteNodesCommand,
<DeleteNodesCommand as Command<'a>>::State,
sub_command!(DeleteNodesCommand),
);
type Result = ();

Expand All @@ -34,48 +27,37 @@ impl<'a> Command<'a> for DeleteGroupCommand {

fn apply(
&self,
(fixture_manager, pipeline, planner, layout_storage): (
(fixture_manager, pipeline, delete_node_runner): (
&FixtureManager,
&mut PipelineAccess,
&mut ExecutionPlanner,
&LayoutStorage,
&Pipeline,
SubCommandRunner<DeleteNodesCommand>,
),
) -> anyhow::Result<(Self::Result, Self::State)> {
let group = fixture_manager
.delete_group(self.id)
.ok_or_else(|| anyhow::anyhow!("Unknown group {}", self.id))?;

let path = pipeline
.nodes_view
.iter()
.find(|node| {
if let Node::Group(node) = node.downcast() {
node.id == self.id
} else {
false
}
})
.map(|node| node.key().clone())
let path = pipeline.find_node_path::<GroupNode>(|node| node.id == self.id)
.cloned()
.ok_or_else(|| anyhow::anyhow!("Missing node for group {}", self.id))?;

let sub_cmd = DeleteNodesCommand { paths: vec![path] };
let (_, state) = sub_cmd.apply((pipeline, planner, layout_storage))?;
let (_, state) = delete_node_runner.apply(sub_cmd)?;

Ok(((), (group, sub_cmd, state)))
Ok(((), (group, state)))
}

fn revert(
&self,
(fixture_manager, pipeline, planner, layout_storage): (
(fixture_manager, _, delete_node_runner): (
&FixtureManager,
&mut PipelineAccess,
&mut ExecutionPlanner,
&LayoutStorage,
&Pipeline,
SubCommandRunner<DeleteNodesCommand>,
),
(group, sub_cmd, state): Self::State,
(group, sub_cmd): Self::State,
) -> anyhow::Result<()> {
fixture_manager.groups.insert(group.id, group);
sub_cmd.revert((pipeline, planner, layout_storage), state)?;
delete_node_runner.revert(sub_cmd)?;

Ok(())
}
Expand Down
23 changes: 7 additions & 16 deletions crates/components/fixtures/commands/src/delete_preset.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use mizer_commander::*;
use mizer_fixtures::manager::FixtureManager;
use mizer_fixtures::programmer::{GenericPreset, PresetId};
use mizer_nodes::{Node, NodeDowncast};
use mizer_nodes::{PresetNode};
use mizer_runtime::commands::DeleteNodesCommand;
use mizer_runtime::pipeline_access::PipelineAccess;
use serde::{Deserialize, Serialize};
use mizer_runtime::Pipeline;

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub struct DeletePresetCommand {
Expand All @@ -14,7 +14,7 @@ pub struct DeletePresetCommand {
impl<'a> Command<'a> for DeletePresetCommand {
type Dependencies = (
Ref<FixtureManager>,
Ref<PipelineAccess>,
Ref<Pipeline>,
SubCommand<DeleteNodesCommand>,
);
type State = (GenericPreset, sub_command!(DeleteNodesCommand));
Expand All @@ -28,25 +28,16 @@ impl<'a> Command<'a> for DeletePresetCommand {
&self,
(fixture_manager, pipeline, delete_node_runner): (
&FixtureManager,
&PipelineAccess,
&Pipeline,
SubCommandRunner<DeleteNodesCommand>,
),
) -> anyhow::Result<(Self::Result, Self::State)> {
let preset = fixture_manager
.delete_preset(self.id)
.ok_or_else(|| anyhow::anyhow!("Unknown preset {}", self.id))?;

let path = pipeline
.nodes_view
.iter()
.find(|node| {
if let Node::Preset(node) = node.downcast() {
node.id == self.id
} else {
false
}
})
.map(|node| node.key().clone())
let path = pipeline.find_node_path::<PresetNode>(|node| node.id == self.id)
.cloned()
.ok_or_else(|| anyhow::anyhow!("Missing node for preset {}", self.id))?;

let sub_cmd = DeleteNodesCommand { paths: vec![path] };
Expand All @@ -59,7 +50,7 @@ impl<'a> Command<'a> for DeletePresetCommand {
&self,
(fixture_manager, _, delete_node_runner): (
&FixtureManager,
&PipelineAccess,
&Pipeline,
SubCommandRunner<DeleteNodesCommand>,
),
(preset, sub_cmd): Self::State,
Expand Down
7 changes: 1 addition & 6 deletions crates/components/fixtures/commands/src/patch_fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ use std::str::FromStr;
use regex::Regex;
use serde::{Deserialize, Serialize};

use mizer_commander::{sub_command, Command, Ref, RefMut};
use mizer_commander::{Command, Ref};
use mizer_fixtures::definition::FixtureDefinition;
use mizer_fixtures::fixture::Fixture;
use mizer_fixtures::library::FixtureLibrary;
use mizer_fixtures::manager::FixtureManager;
use mizer_node::{NodeDesigner, NodeType};
use mizer_nodes::FixtureNode;
use mizer_runtime::commands::AddNodeCommand;
use mizer_runtime::pipeline_access::PipelineAccess;
use mizer_runtime::ExecutionPlanner;

lazy_static::lazy_static! {
static ref FIXTURE_NAME_REGEX: Regex = Regex::new("^(?P<name>.*?)(?P<counter>[0-9]+)?$").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/components/fixtures/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Processor for FixtureProcessor {
}

#[tracing::instrument]
fn process(&mut self, injector: &Injector, _: ClockFrame) {
fn process(&mut self, injector: &mut Injector, _: ClockFrame) {
profiling::scope!("FixtureProcessor::process");
let fixture_manager = injector.inject::<FixtureManager>();
fixture_manager.execute_programmers();
Expand Down
Loading

0 comments on commit 201b912

Please sign in to comment.