|
| 1 | +use async_trait::async_trait; |
| 2 | +use eyre::Result; |
| 3 | +use kos::hal::{GetStateResponse, Policy, StartPolicyResponse, StopPolicyResponse}; |
| 4 | +use kos::kos_proto::common::{Error, ErrorCode}; |
| 5 | +use std::collections::HashMap; |
| 6 | +use std::sync::Mutex; |
| 7 | +use uuid::Uuid; |
| 8 | + |
| 9 | +pub struct StubPolicy { |
| 10 | + policy_uuid: Mutex<Option<String>>, |
| 11 | + state: Mutex<HashMap<String, String>>, |
| 12 | +} |
| 13 | + |
| 14 | +impl Default for StubPolicy { |
| 15 | + fn default() -> Self { |
| 16 | + Self::new() |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +impl StubPolicy { |
| 21 | + pub fn new() -> Self { |
| 22 | + StubPolicy { |
| 23 | + policy_uuid: Mutex::new(None), |
| 24 | + state: Mutex::new(HashMap::new()), |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#[async_trait] |
| 30 | +impl Policy for StubPolicy { |
| 31 | + async fn start_policy( |
| 32 | + &self, |
| 33 | + action: String, |
| 34 | + action_scale: f32, |
| 35 | + episode_length: i32, |
| 36 | + dry_run: bool, |
| 37 | + ) -> Result<StartPolicyResponse> { |
| 38 | + let mut policy_uuid = self.policy_uuid.lock().unwrap(); |
| 39 | + if policy_uuid.is_some() { |
| 40 | + return Ok(StartPolicyResponse { |
| 41 | + policy_uuid: None, |
| 42 | + error: Some(Error { |
| 43 | + code: ErrorCode::InvalidArgument as i32, |
| 44 | + message: "Policy is already running".to_string(), |
| 45 | + }), |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + let new_uuid = Uuid::new_v4().to_string(); |
| 50 | + *policy_uuid = Some(new_uuid.clone()); |
| 51 | + |
| 52 | + // Update state with policy parameters |
| 53 | + let mut state = self.state.lock().unwrap(); |
| 54 | + state.insert("action".to_string(), action); |
| 55 | + state.insert("action_scale".to_string(), action_scale.to_string()); |
| 56 | + state.insert("episode_length".to_string(), episode_length.to_string()); |
| 57 | + state.insert("dry_run".to_string(), dry_run.to_string()); |
| 58 | + |
| 59 | + Ok(StartPolicyResponse { |
| 60 | + policy_uuid: Some(new_uuid), |
| 61 | + error: None, |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + async fn stop_policy(&self) -> Result<StopPolicyResponse> { |
| 66 | + let mut policy_uuid = self.policy_uuid.lock().unwrap(); |
| 67 | + if policy_uuid.is_none() { |
| 68 | + return Ok(StopPolicyResponse { |
| 69 | + policy_uuid: None, |
| 70 | + error: Some(Error { |
| 71 | + code: ErrorCode::InvalidArgument as i32, |
| 72 | + message: "Policy is not running".to_string(), |
| 73 | + }), |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + let stopped_uuid = policy_uuid.take().unwrap(); |
| 78 | + |
| 79 | + // Clear the state when stopping |
| 80 | + let mut state = self.state.lock().unwrap(); |
| 81 | + state.clear(); |
| 82 | + |
| 83 | + Ok(StopPolicyResponse { |
| 84 | + policy_uuid: Some(stopped_uuid), |
| 85 | + error: None, |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + async fn get_state(&self) -> Result<GetStateResponse> { |
| 90 | + let state = self.state.lock().unwrap(); |
| 91 | + Ok(GetStateResponse { |
| 92 | + state: state.clone(), |
| 93 | + error: None, |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
0 commit comments