Skip to content

Commit

Permalink
Remove namespace concept and integrate its functionality into program…
Browse files Browse the repository at this point in the history
… concept

The namespace concept has been removed and its functionality has been integrated directly into the program concept. This changes the structure of the program, simplifying it from a two-level Program containing Namespaces containing Commands, to a one-level Program containing Commands.
  • Loading branch information
Barsik-sus committed Mar 7, 2024
1 parent 7bd4315 commit 11c4800
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 677 deletions.
4 changes: 2 additions & 2 deletions module/move/wca/src/ca/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub( crate ) mod private

// xxx : qqq : qqq2 : for Bohdan : one level is obviously redundant
// Program< Namespace< ExecutableCommand_ > > -> Program< ExecutableCommand_ >
struct CommandsAggregatorCallback( Box< dyn Fn( &str, &Program< Namespace< ExecutableCommand_ > > ) > );
struct CommandsAggregatorCallback( Box< dyn Fn( &str, &Program< ExecutableCommand_ > ) > );

impl fmt::Debug for CommandsAggregatorCallback
{
Expand Down Expand Up @@ -235,7 +235,7 @@ pub( crate ) mod private
/// ```
pub fn callback< Callback >( mut self, callback : Callback ) -> Self
where
Callback : Fn( &str, &Program< Namespace< ExecutableCommand_ > > ) + 'static,
Callback : Fn( &str, &Program< ExecutableCommand_ > ) + 'static,
{
self.container.callback_fn = Some( CommandsAggregatorCallback( Box::new( callback ) ) );
self
Expand Down
20 changes: 4 additions & 16 deletions module/move/wca/src/ca/executor/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,14 @@ pub( crate ) mod private
impl ExecutorConverter
{
/// Converts raw program to executable
pub fn to_program( &self, raw_program : Program< Namespace< VerifiedCommand > > ) -> Result< Program< Namespace< ExecutableCommand_ > > >
pub fn to_program( &self, raw_program : Program< VerifiedCommand > ) -> Result< Program< ExecutableCommand_ > >
{
let namespaces = raw_program.namespaces
let commands = raw_program.commands
.into_iter()
.map( | n | self.to_namespace( n ) )
.collect::< Result< Vec< Namespace< ExecutableCommand_ > > > >()?;

Ok( Program { namespaces } )
}

// qqq : for Bohdan : probably redundant
/// Converts raw namespace to executable
pub fn to_namespace( &self, raw_namespace : Namespace< VerifiedCommand > ) -> Result< Namespace< ExecutableCommand_ > >
{
let commands = raw_namespace.commands
.into_iter()
.map( | c | self.to_command( c ) )
.map( | n | self.to_command( n ) )
.collect::< Result< Vec< ExecutableCommand_ > > >()?;

Ok( Namespace { commands } )
Ok( Program { commands } )
}

/// Converts raw command to executable
Expand Down
36 changes: 5 additions & 31 deletions module/move/wca/src/ca/executor/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ pub( crate ) mod private
/// Executes a program
///
/// Setup runtimes for each namespace into program and run it with specified execution type
pub fn program( &self, program : Program< Namespace< ExecutableCommand_ > > ) -> Result< () >
pub fn program( &self, program : Program< ExecutableCommand_ > ) -> Result< () >
{
let context = self.context.clone();
let runtimes_number = program.namespaces.len();
let runtimes = program.namespaces
let runtimes_number = program.commands.len();
let runtimes = program.commands
.into_iter()
.fold
(
Vec::with_capacity( runtimes_number ),
| mut acc, namespace |
| mut acc, command |
{
// local context for each namespace
let context = match self.kind
Expand All @@ -79,7 +79,7 @@ pub( crate ) mod private
{
context,
pos : 0,
namespace,
namespace : vec![ command ],
};
acc.push( runtime );
acc
Expand All @@ -95,32 +95,6 @@ pub( crate ) mod private
Ok( () )
}

/// Executes a namespace
///
/// Configure `Runtime` and run commands from namespace at runtime position while it isn't finished
pub fn namespace( &self, namespace : Namespace< ExecutableCommand_ > ) -> Result< () >
{
let context = self.context.clone();
let mut runtime = Runtime
{
context,
pos : 0,
namespace,
};

while !runtime.is_finished()
{
let state = runtime.context.get_or_default::< RuntimeState >();
state.pos = runtime.pos + 1;
runtime.r#do()?;
runtime.pos = runtime.context.get_ref::< RuntimeState >().unwrap().pos;
// qqq : for Bohdan : has `runtime.context` be used? seems not
// looks like unnecessary too complicated.
}

Ok( () )
}

/// Executes a command
///
/// Call command callback with context if it is necessary.
Expand Down
14 changes: 6 additions & 8 deletions module/move/wca/src/ca/executor/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,12 @@ pub( crate ) mod private
/// It performs callbacks to commands at the current execution position and, if necessary, provides context for them.
///
/// ```
/// # use wca::{ Runtime, Namespace, Context };
/// # use wca::{ Runtime, Context };
/// let runtime = Runtime
/// {
/// context : Context::default(),
/// pos : 0,
/// namespace : Namespace
/// {
/// commands: vec![]
/// }
/// namespace :vec![],
/// };
///
/// assert!( runtime.is_finished() );
Expand All @@ -56,7 +53,7 @@ pub( crate ) mod private
/// current execution position
pub pos : usize,
/// namespace which must be executed
pub namespace : Namespace< ExecutableCommand_ >, // qqq : for Bohdan : use VerifiedCommand
pub namespace : Vec< ExecutableCommand_ >, // qqq : for Bohdan : use VerifiedCommand
}
// qqq : for Bohdan : why both Runtime and RuntimeState exist? probably one should removed
// qqq : for Bohdan : why both Runtime and Context exist? What about incapsulating Context into Runtime maybe
Expand All @@ -67,13 +64,14 @@ pub( crate ) mod private
/// returns true if execution position at the end
pub fn is_finished( &self ) -> bool
{
self.namespace.commands.len() == self.pos
self.namespace.len() == self.pos
}

/// executes current command( command at current execution position )
pub fn r#do( &mut self ) -> Result< () >
{
self.namespace.commands
self
.namespace
.get( self.pos )
.ok_or_else( || err!( "No command here. Current execution pos was `{}`", self.pos ) )
.and_then( | cmd |
Expand Down
98 changes: 3 additions & 95 deletions module/move/wca/src/ca/parser/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,107 +4,16 @@ pub( crate ) mod private

/// Represents a program that contains one or more namespaces, where each namespace contains a list of commands.
///
/// A `Program` consists of one or more Namespaces, where each namespace contains a list of commands.
/// The `Namespace` can be any type that represents a namespace of commands, such as `ParsedCommand`, `VerifiedCommand`, or `ExecutableCommand_`.
///
/// The program can be executed by iterating over each namespace and executing its commands sequentially or in parallel.
///
/// # Example:
///
/// ```
/// # use wca::{ ParsedCommand, Namespace, Program };
/// # use std::collections::HashMap;
/// let namespace1 = Namespace
/// {
/// commands : vec!
/// [
/// ParsedCommand
/// {
/// name : "cmd1".to_string(),
/// subjects : vec![ "sub1".to_string() ],
/// properties: HashMap::new(),
/// },
/// ParsedCommand
/// {
/// name: "cmd2".to_string(),
/// subjects: vec![ "sub2".to_string(), "sub3".to_string() ],
/// properties: HashMap::new(),
/// },
/// ],
/// };
///
/// let namespace2 = Namespace
/// {
/// commands : vec!
/// [
/// ParsedCommand
/// {
/// name : "cmd1".to_string(),
/// subjects : vec![ "sub1".to_string() ],
/// properties: HashMap::new(),
/// },
/// ],
/// };
/// let program = Program { namespaces : vec![ namespace1, namespace2, /* ... */ ] };
/// ```
///
/// In the above example, a Program is created with two Namespace objects. Each namespace contains a different set of ParsedCommand objects with different sets of subjects. The Program can be executed by iterating over each namespace and executing its commands in sequence.
/// A `Program` consists of one or more commannd
///
/// The program can be executed by iterating over each commands and executing it
// qqq : xxx : for Bohdan : Commands should be here instead of Namespace
// qqq : remove concept Namespace
// qqq : introduce concept Dictionary for grammar
#[ derive( Debug, Clone, PartialEq, Eq ) ]
pub struct Program< Namespace >
pub struct Program< Command >
{
/// list of namespaces with commands
pub namespaces : Vec< Namespace >,
}

/// Represents a namespace of commands with the specified Command type. This is done to be flexible and not to duplicate code.
///
/// A `Namespace` contains a list of commands, where each command can be a `ParsedCommand`, `VerifiedCommand`, `ExecutableCommand_`, or any other command type that you define.
///
/// In the future, each namespace can be executed in parallel.
/// This means that commands in namespace will be executed synchronous but each namespace can be executed in parallel to each other.
///
/// # Example:
///
/// ```
/// # use wca::{ ParsedCommand, Namespace };
/// # use std::collections::HashMap;
///
/// let commands = vec!
/// [
/// ParsedCommand
/// {
/// name : "cmd1".to_string(),
/// subjects : vec![ "sub1".to_string() ],
/// properties : HashMap::new(),
/// },
/// ParsedCommand
/// {
/// name : "cmd2".to_string(),
/// subjects : vec![ "sub2".to_string(), "sub3".to_string() ],
/// properties : HashMap::new(),
/// },
/// ParsedCommand
/// {
/// name : "cmd3".to_string(),
/// subjects: vec![],
/// properties: HashMap::new(),
/// },
/// /* ... */
/// ];
///
/// let namespace = Namespace { commands };
/// ```
///
/// In the above example, a `Namespace` is created with three `ParsedCommand` objects. Each command has a different set of subjects.
///
#[ derive( Debug, Clone, PartialEq, Eq ) ]
pub struct Namespace< Command >
{
/// list of commands
pub commands : Vec< Command >,
}

Expand Down Expand Up @@ -148,6 +57,5 @@ pub( crate ) mod private
crate::mod_interface!
{
exposed use Program;
exposed use Namespace;
exposed use ParsedCommand;
}
2 changes: 0 additions & 2 deletions module/move/wca/src/ca/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ crate::mod_interface!
layer parser;
/// Implementation for parsing command
layer command;
/// Implementation for parsing namespace
layer namespace;
/// Implementation for parsing program
layer program;
/// Entities representation to interact with
Expand Down
87 changes: 0 additions & 87 deletions module/move/wca/src/ca/parser/namespace.rs

This file was deleted.

Loading

0 comments on commit 11c4800

Please sign in to comment.