From 85803cacb58e2b720c1c7762f6b2db4022dd4391 Mon Sep 17 00:00:00 2001 From: Barsik Date: Mon, 25 Mar 2024 15:11:20 +0200 Subject: [PATCH] Refactor executor and context code in wca module Removed unused files command.rs and runtime.rs under the executor module and simplified executor.rs structure for clearer logic and readability. Updated context.rs to use get() function instead of get_ref(). Also, revised the function signature in help.rs for enhanced clarity. --- module/move/wca/src/ca/executor/command.rs | 46 -------- module/move/wca/src/ca/executor/context.rs | 9 +- module/move/wca/src/ca/executor/converter.rs | 0 module/move/wca/src/ca/executor/executor.rs | 62 ++++++----- module/move/wca/src/ca/executor/mod.rs | 6 +- module/move/wca/src/ca/executor/runtime.rs | 107 ------------------- module/move/wca/src/ca/help.rs | 2 +- 7 files changed, 43 insertions(+), 189 deletions(-) delete mode 100644 module/move/wca/src/ca/executor/command.rs delete mode 100644 module/move/wca/src/ca/executor/converter.rs delete mode 100644 module/move/wca/src/ca/executor/runtime.rs diff --git a/module/move/wca/src/ca/executor/command.rs b/module/move/wca/src/ca/executor/command.rs deleted file mode 100644 index 247a1ce9d9..0000000000 --- a/module/move/wca/src/ca/executor/command.rs +++ /dev/null @@ -1,46 +0,0 @@ -pub( crate ) mod private -{ - use crate::*; - use std::collections::HashMap; - - /// Represents a command that can be executed, with a list of command subjects and a set of command options, and a callback function that defines the command logic. - /// - /// # Example: - /// - /// ``` - /// # use wca::{ ExecutableCommand_, Routine, Value }; - /// # use std::collections::HashMap; - /// ExecutableCommand_ - /// { - /// subjects : vec![ Value::String( "subject_value".to_string() ), /* ... */ ], - /// properties : HashMap::from_iter - /// ([ - /// ( "prop_name".to_string(), Value::Number( 42.0 ) ), - /// /* ... */ - /// ]), - /// routine : Routine::new( |( args, props )| Ok( () ) ) - /// }; - /// ``` - /// - #[ derive( Debug, Clone ) ] - pub struct ExecutableCommand_ - { - /// subjects values - pub subjects : Vec< Value >, - /// properties value - pub properties : HashMap< String, Value >, - /// function that will be called - pub routine : Routine, - } - // qqq : for Bohdan : rid off the structure. VerifiedCommand should be used and passed to userland. - -} - -// - -crate::mod_interface! -{ - exposed use ExecutableCommand_; -} - -// qqq : use orphan instead of exposed for ALL files in the folder, dont use prelude for structs \ No newline at end of file diff --git a/module/move/wca/src/ca/executor/context.rs b/module/move/wca/src/ca/executor/context.rs index ab0b8921ce..ea83670921 100644 --- a/module/move/wca/src/ca/executor/context.rs +++ b/module/move/wca/src/ca/executor/context.rs @@ -13,19 +13,20 @@ pub( crate ) mod private /// let ctx = Context::default(); /// /// ctx.insert( 42 ); - /// assert_eq!( 42, *ctx.get_ref().unwrap() ); + /// assert_eq!( 42, ctx.get().unwrap() ); /// ``` /// /// ``` /// # use wca::{ Routine, Context, Value, Args, Props }; + /// # use std::sync::{ Arc, Mutex }; /// let routine = Routine::new_with_ctx /// ( /// | ( args, props ), ctx | /// { /// let first_arg : i32 = args.get_owned( 0 ).unwrap_or_default(); - /// let ctx_value : &mut i32 = ctx.get_or_default(); + /// let ctx_value : Arc< Mutex< i32 > > = ctx.get_or_default(); /// - /// *ctx_value += first_arg; + /// *ctx_value.lock().unwrap() += first_arg; /// /// Ok( () ) /// } @@ -35,7 +36,7 @@ pub( crate ) mod private /// { /// callback( ( Args( vec![ Value::Number( 1.0 ) ] ), Props( Default::default() ) ), ctx.clone() ).unwrap(); /// } - /// assert_eq!( 1, *ctx.get_ref().unwrap() ); + /// assert_eq!( 1, *ctx.get::< Arc< Mutex< i32 > > >().unwrap().lock().unwrap() ); /// ``` // CloneAny needs to deep clone of Context // qqq : ? diff --git a/module/move/wca/src/ca/executor/converter.rs b/module/move/wca/src/ca/executor/converter.rs deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/module/move/wca/src/ca/executor/executor.rs b/module/move/wca/src/ca/executor/executor.rs index 68717f6762..88a1e1adee 100644 --- a/module/move/wca/src/ca/executor/executor.rs +++ b/module/move/wca/src/ca/executor/executor.rs @@ -2,10 +2,7 @@ pub( crate ) mod private { use crate::*; - use ca::executor::runtime::_exec_command; use wtools::error::Result; - use std::sync::Arc; - use std::sync::atomic::Ordering; // aaa : for Bohdan : how is it useful? where is it used? // aaa : `ExecutorType` has been removed @@ -25,45 +22,56 @@ pub( crate ) mod private { /// Executes a program /// - /// Setup runtimes for each namespace into program and run it with specified execution type + /// Iterates over the commands in the program and executes each command using the provided dictionary. + /// This method returns a `Result` indicating whether the execution was successful or not. + /// + /// # Arguments + /// + /// * `dictionary` - A reference to the dictionary used to look up the command routine. + /// * `program` - The program to be executed, which is a `Program` object consisting of a list of commands. + /// + /// # Returns + /// + /// A `Result` with `Ok(())` if the execution was successful, or an `Err` containing an error message if an error occurred. + /// pub fn program( &self, dictionary : &Dictionary, program : Program< VerifiedCommand > ) -> Result< () > { - let context = self.context.clone(); - let runtime = Runtime + for command in program.commands { - context, - pos : 0, - namespace : program.commands, - }; - - Self::sequential_execution_loop( dictionary, runtime )?; + self.command( dictionary, command )?; + } Ok( () ) } - /// Executes a command + /// Executes a given command using a provided dictionary and command. + /// + /// Calls the command callback with the given context if it is necessary. + /// + /// # Arguments + /// + /// * `dictionary` - A reference to the dictionary used to look up the command routine. + /// * `command` - The verified command that needs to be executed. /// - /// Call command callback with context if it is necessary. + /// # Returns + /// + /// Returns a Result indicating success or failure. If successful, returns `Ok(())`, otherwise returns an error. pub fn command( &self, dictionary : &Dictionary, command : VerifiedCommand ) -> Result< () > { let routine = dictionary.command( &command.phrase ).unwrap().routine.clone(); _exec_command( command, routine, self.context.clone() ) } - - // qqq : for Bohdan : probably redundant + + // aaa : for Bohdan : probably redundant // aaa : removed `parallel_execution_loop` - - fn sequential_execution_loop( dictionary : &Dictionary, mut runtime : Runtime ) -> Result< () > + } + + fn _exec_command( command : VerifiedCommand, routine : Routine, ctx : Context ) -> Result< () > + { + match routine { - while !runtime.is_finished() - { - let state = runtime.context.get_or_default::< Arc< RuntimeState > >(); - state.pos.store( runtime.pos + 1, Ordering::Release ); - runtime.r#do( &dictionary )?; - runtime.pos = runtime.context.get::< Arc< RuntimeState > >().unwrap().pos.load( Ordering::Relaxed ); - } - - Ok( () ) + Routine::WithoutContext( routine ) => routine(( Args( command.subjects ), Props( command.properties ) )), + Routine::WithContext( routine ) => routine( ( Args( command.subjects ), Props( command.properties ) ), ctx ), } } } diff --git a/module/move/wca/src/ca/executor/mod.rs b/module/move/wca/src/ca/executor/mod.rs index f0fd5e3997..77789544d0 100644 --- a/module/move/wca/src/ca/executor/mod.rs +++ b/module/move/wca/src/ca/executor/mod.rs @@ -1,12 +1,10 @@ crate::mod_interface! { - /// Executor that is responsible for executing the program’s commands - layer executor; - /// Represents the state of the program's runtime - layer runtime; /// Container for contexts values layer context; + /// Executor that is responsible for executing the program’s commands + layer executor; /// Command callback representation layer routine; diff --git a/module/move/wca/src/ca/executor/runtime.rs b/module/move/wca/src/ca/executor/runtime.rs deleted file mode 100644 index 80ac92f2f6..0000000000 --- a/module/move/wca/src/ca/executor/runtime.rs +++ /dev/null @@ -1,107 +0,0 @@ -pub( crate ) mod private -{ - use crate::*; - use wtools::{ error::Result, err }; - use std::sync::Arc; - use std::sync::atomic::AtomicUsize; - - /// State of a program runtime - /// - /// `RuntimeState` contains information about the current state of a running program. - /// It is used to store information that can be modified during program execution. - /// - /// Can be used to change execution position at runtime. - /// - /// # Examples - /// - /// ``` - /// # use wca::RuntimeState; - /// let mut state = RuntimeState::default(); - /// - /// state.pos = 5; // modify current execution position - /// - /// assert_eq!( state.pos, 5 ); - /// ``` - #[ derive( Debug, Default, Clone ) ] - pub struct RuntimeState - { - /// current execution position that can be changed by user - pub pos : Arc< AtomicUsize >, - } - // qqq : for Bohdan : why? how is it useful? is it? - - /// Represents the state of the program's runtime, including the current context, execution position, and namespace of executable commands. - /// - /// Cloned Runtime will work with the same context. - /// - /// It performs callbacks to commands at the current execution position and, if necessary, provides context for them. - /// - /// ``` - /// # use wca::{ Runtime, Context }; - /// let runtime = Runtime - /// { - /// context : Context::default(), - /// pos : 0, - /// namespace :vec![], - /// }; - /// - /// assert!( runtime.is_finished() ); - /// ``` - #[ derive( Debug, Clone ) ] - pub struct Runtime - { - /// context for current runtime - pub context : Context, - /// current execution position - pub pos : usize, - /// namespace which must be executed - pub namespace : Vec< VerifiedCommand >, // 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 - // qqq : for Bohdan : why both Runtime and Executor exist? rid off of Executor. Incapsulating Executor into Runtime. - - impl Runtime - { - /// returns true if execution position at the end - pub fn is_finished( &self ) -> bool - { - self.namespace.len() == self.pos - } - - /// executes current command( command at current execution position ) - pub fn r#do( &mut self, dictionary : &Dictionary ) -> Result< () > - { - self - .namespace - .get( self.pos ) - .ok_or_else( || err!( "No command here. Current execution pos was `{}`", self.pos ) ) - .and_then( | cmd | - { - let routine = dictionary.command( &cmd.phrase ).unwrap().routine.clone(); - _exec_command( cmd.clone(), routine, self.context.clone() ) - }) - } - } - - // qqq : for Bohdan : _exec_command probably should be method of Runtime. - // qqq : for Bohdan : Accept reference instead of copy. - /// executes a command - pub fn _exec_command( command : VerifiedCommand, routine : Routine, ctx : Context ) -> Result< () > - { - match routine - { - Routine::WithoutContext( routine ) => routine(( Args( command.subjects ), Props( command.properties ) )), - Routine::WithContext( routine ) => routine( ( Args( command.subjects ), Props( command.properties ) ), ctx ), - } - } -} - -// - -crate::mod_interface! -{ - exposed use RuntimeState; - exposed use Runtime; - protected use _exec_command; -} diff --git a/module/move/wca/src/ca/help.rs b/module/move/wca/src/ca/help.rs index 95a5a04d5b..eb374e093f 100644 --- a/module/move/wca/src/ca/help.rs +++ b/module/move/wca/src/ca/help.rs @@ -358,7 +358,7 @@ pub( crate ) mod private /// # use wca::ca::help::{ HelpGeneratorArgs, HelpGeneratorFn }; /// use wca::{ Command, Dictionary }; /// - /// fn my_help_generator( grammar : &Dictionary, command : Option< &Command > ) -> String + /// fn my_help_generator( dictionary : &Dictionary, args : HelpGeneratorArgs< '_ > ) -> String /// { /// format!( "Help content based on grammar and command" ) /// }