Skip to content

Commit

Permalink
correct behavior for rtvalue, fix decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris Zhguchev committed Mar 24, 2024
1 parent d739fc2 commit 814a82b
Show file tree
Hide file tree
Showing 12 changed files with 12,673 additions and 47 deletions.
3 changes: 2 additions & 1 deletion src/runtime/action/builtin/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ impl Impl for StoreData {

let value = args
.find_or_ith("value".to_string(), 1)
.ok_or(RuntimeError::fail("the value is expected".to_string()))?;
.ok_or(RuntimeError::fail("the value is expected".to_string()))
.and_then(|v|v.with_ptr(ctx.clone()))?;

ctx.bb().lock()?.put(key, value)?;
Ok(TickResult::Success)
Expand Down
10 changes: 5 additions & 5 deletions src/runtime/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,15 @@ impl RtArgument {
p: Param,
parent_args: Arguments,
parent_params: Params,
) -> Result<(RtArgument,ArgumentRhs), TreeError> {
) -> Result<(RtArgument, ArgumentRhs), TreeError> {
RtArgument::validate_type(a.clone(), p.clone().tpe)?;
match &a {
ArgumentRhs::Id(id) => match find_arg_value(id, &parent_params, &parent_args).ok() {
None => Ok((RtArgument::new(p.name, RtValue::Pointer(id.clone())),a)),
None => Ok((RtArgument::new(p.name, RtValue::Pointer(id.clone())), a)),
Some(v) => RtArgument::try_from(v, p, Arguments::default(), Params::default()),
},
ArgumentRhs::Mes(m) => Ok((RtArgument::new(p.name, m.clone().into()),a)),
ArgumentRhs::Call(c) => Ok((RtArgument::new(p.name, RtValue::Call(c.clone())),a)),
ArgumentRhs::Mes(m) => Ok((RtArgument::new(p.name, m.clone().into()), a)),
ArgumentRhs::Call(c) => Ok((RtArgument::new(p.name, RtValue::Call(c.clone())), a)),
}
}
/// validates the type of the argument in accordance with the type of the parameter
Expand Down Expand Up @@ -399,4 +399,4 @@ impl RtArgument {
}
}
}
}
}
9 changes: 8 additions & 1 deletion src/runtime/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ pub struct TreeContextRef {
env: RtEnvRef,
}

impl From<&mut TreeContext> for TreeContextRef {
fn from(value: &mut TreeContext) -> Self {
TreeContextRef::from_ctx(value, Default::default())
}
}

impl TreeContextRef {
pub fn from_ctx(ctx: &TreeContext, trimmer: Arc<Mutex<TrimmingQueue>>) -> Self {
TreeContextRef::new(ctx.bb.clone(), ctx.tracer.clone(), ctx.curr_ts, trimmer, ctx.rt_env.clone())
Expand Down Expand Up @@ -86,7 +92,7 @@ impl TreeContextRef {
tracer,
curr_ts,
trimmer,
env
env,
}
}
}
Expand Down Expand Up @@ -118,6 +124,7 @@ pub struct TreeContext {
rt_env: RtEnvRef,
}


impl TreeContext {
pub fn state(&self) -> &HashMap<RNodeId, RNodeState> {
&self.state
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ pub struct RtEnv {
pub daemons: Vec<DaemonTask>,
}

impl Default for RtEnv {
fn default() -> Self {
RtEnv::try_new().unwrap()
}
}

impl From<JoinError> for RuntimeError {
fn from(value: JoinError) -> Self {
RuntimeError::fail(value.to_string())
Expand Down
22 changes: 18 additions & 4 deletions src/runtime/forester/decorator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::runtime::args::{RtArgs, RtArgument, RtValue, RtValueNumber};
use crate::runtime::context::{RNodeState, TreeContext};
use crate::runtime::context::{RNodeState, TreeContext, TreeContextRef};
use crate::runtime::forester::flow::{run_with, LEN, REASON};
use crate::runtime::rtree::rnode::DecoratorType;
use crate::runtime::{RtResult, RuntimeError, TickResult};
Expand Down Expand Up @@ -28,6 +28,7 @@ pub(crate) fn prepare(
_ => Ok(RNodeState::Running(tick_args.with(LEN, RtValue::int(1)))),
}
}

// This runs when the child returns running.
// It works for timeout and other controlling decorators
pub(crate) fn monitor(
Expand Down Expand Up @@ -81,14 +82,18 @@ pub(crate) fn finalize(
}
_ => Ok(RNodeState::Success(run_with(tick_args, 0, 1))),
},
DecoratorType::ForceSuccess => Ok(RNodeState::Success(run_with(tick_args, 0, 1))) ,
DecoratorType::ForceSuccess => Ok(RNodeState::Success(run_with(tick_args, 0, 1))),
DecoratorType::ForceFail => Ok(RNodeState::Failure(run_with(tick_args, 0, 1).with(
REASON,
RtValue::str("decorator force fail.".to_string()),
))),
DecoratorType::Repeat => match child_res {
TickResult::Success => {
let count = init_args.first_as(RtValue::as_int).unwrap_or(1);
let count = init_args
.first()
.and_then(|v| v.cast(ctx.into()).int().ok())
.flatten()
.unwrap_or(1);
let attempt = tick_args.first_as(RtValue::as_int).unwrap_or(1);
if attempt >= count {
Ok(RNodeState::Success(run_with(tick_args, 0, 1)))
Expand All @@ -111,9 +116,15 @@ pub(crate) fn finalize(
DecoratorType::Delay => Ok(RNodeState::from(run_with(tick_args, 0, 1), child_res)),
DecoratorType::Retry => match child_res {
TickResult::Failure(v) => {
let count = init_args.first_as(RtValue::as_int).unwrap_or(0);
let count = init_args
.first()
.and_then(|v| v.cast(ctx.into()).int().ok())
.flatten()
.unwrap_or(0);

let attempts = tick_args.first_as(RtValue::as_int).unwrap_or(0);


if attempts >= count {
let args = run_with(tick_args, 0, 1).with(REASON, RtValue::str(v));
Ok(RNodeState::Failure(args))
Expand All @@ -122,6 +133,7 @@ pub(crate) fn finalize(
Ok(RNodeState::Running(run_with(args, 0, 1)))
}
}
TickResult::Success => Ok(RNodeState::Success(tick_args)),
_ => Ok(RNodeState::Running(run_with(tick_args, 0, 1))),
},
}
Expand All @@ -133,11 +145,13 @@ fn get_ts() -> i64 {
.expect("")
.as_secs() as i64
}

fn start_args() -> RtArgs {
RtArgs(vec![RtArgument::new_noname(RtValue::Number(
RtValueNumber::Int(get_ts()),
))])
}

fn get_delay(args: RtArgs) -> RtResult<i64> {
let err = "the decorator delay accepts one integer param, denoting duration of delay in millis"
.to_string();
Expand Down
64 changes: 32 additions & 32 deletions src/runtime/ros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,38 +101,38 @@ impl SubscriberDaemon {

impl DaemonFn for SubscriberDaemon {
fn perform(&mut self, ctx: DaemonContext, signal: StopFlag) {
loop {
if signal.load(Ordering::Relaxed) {
break;
}
let msg = self.ws().read()?;

match self {
SubscriberDaemon::Last(_, key) => {
if msg.is_text() {
let string = msg.into_text().expect("text");
let value = RtValue::deserialize(string).unwrap();
ctx.bb.lock().unwrap().put(key.clone(), value).unwrap();
} else {
debug!(target: "ws-subscriber" ,"Subscriber Daemon: Received not text: {:#?}", msg);
}
}
SubscriberDaemon::All(_, key) => {
if msg.is_text() {
let string = msg.into_text().expect("text");
let value = RtValue::deserialize(string).unwrap();
push_to_arr(ctx.bb.clone(), key.clone(), value).unwrap();
} else {
debug!(target: "ws-subscriber" ,"Subscriber Daemon: Received not text: {:#?}", msg);
}
}
}


// let msg = serde_json::from_str::<ros::RosMessage>(&msg)?;
// let mut bb = ctx.bb.lock()?;
// bb.insert(topic.clone(), RtValue::from(msg));
}
// loop {
// if signal.load(Ordering::Relaxed) {
// break;
// }
// let msg = self.ws().read().unwrap();
//
// match self {
// SubscriberDaemon::Last(_, key) => {
// if msg.is_text() {
// let string = msg.into_text().expect("text");
// let value = RtValue::deserialize(string).unwrap();
// ctx.bb.lock().unwrap().put(key.clone(), value).unwrap();
// } else {
// debug!(target: "ws-subscriber" ,"Subscriber Daemon: Received not text: {:#?}", msg);
// }
// }
// SubscriberDaemon::All(_, key) => {
// if msg.is_text() {
// let string = msg.into_text().expect("text");
// let value = RtValue::deserialize(string).unwrap();
// push_to_arr(ctx.bb.clone(), key.clone(), value).unwrap();
// } else {
// debug!(target: "ws-subscriber" ,"Subscriber Daemon: Received not text: {:#?}", msg);
// }
// }
// }
//
//
// // let msg = serde_json::from_str::<ros::RosMessage>(&msg)?;
// // let mut bb = ctx.bb.lock()?;
// // bb.insert(topic.clone(), RtValue::from(msg));
// }
}
}

Expand Down
1 change: 0 additions & 1 deletion src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ mod tests {
r#"[1]next tick
[2]1 : Success()
"#
.replace("\n", "\r\n")
)
}
}
10 changes: 10 additions & 0 deletions tree/tests/simulator/smoke/gen\bb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"storage": {
"info1": {
"Unlocked": "initial"
},
"info2": {
"Unlocked": "finish"
}
}
}
127 changes: 127 additions & 0 deletions tree/tests/simulator/smoke/gen\main.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions tree/tests/simulator/smoke/sim_absolute_t.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
config:
trace: C:\projects\forester\tree/tests\simulator/smoke/gen\main.log
graph: C:\projects\forester\tree/tests\simulator/smoke/gen\main.svg
trace: /Users/boris.zhguchev/PetProjects/forester/tree/tests/simulator/smoke/gen\main.log
graph: /Users/boris.zhguchev/PetProjects/forester/tree/tests/simulator/smoke/gen\main.svg
bb:
dump: C:\projects\forester\tree/tests\simulator/smoke/gen\bb.json
dump: /Users/boris.zhguchev/PetProjects/forester/tree/tests/simulator/smoke/gen\bb.json
max_ticks: 10

actions:
Expand Down
Loading

0 comments on commit 814a82b

Please sign in to comment.