Simulation

Forester provides a conception to execute the given tree, replacing the actions to stubs. The major intention is to test the tree behavior and test all branches under the specific conditions without coding the implementations at all. The profile enables to mix in the specific state of blackboard, trace the changes and visualize the tree.

Preparations

Configuration profile

All paths in the configuration files can be either absolute or relative to the root folder

The file contains the settings information alongside with the information about stubbed options.

Below, the example of the file:

config:
  tracer: 
    file: gen/main.trace
    dt_fmt: "%d %H:%M:%S%.3f"
  graph: gen/main.svg
  bb:
    dump: gen/bb.json
  max_ticks: 10

actions:
  -
    name: task
    stub: failure
    params:
      delay: 100

Config section:

SettingDescriptionDefaultExample
tracer.filethe tracer settings to write to a fileif it is absent, there will be no actiongen/main.trace
tracer.dt_fmtthe tracer settings to given data formatif it is absent, there will be no action"%d %H:%M:%S%.3f"
graphthe visualization fileif it is absent, there will be no actiongen/main.svg
bb.dumpthe dump of the bb at the endif it is absent, there will be no actiongen/bb.json
bb.loadthe dump that will be used to init bb before simif it is absent, there will be no actiongen/init_bb.json
max_ticksthe maximum amount of ticks to work.0 by default10
http.portThe port for http server to communicate with the remote actions .if it is absent, there will be no action8080

Actions sections:

The actions sections is an array to stub the actions

SettingDescriptionDefaultExample
namethe name of the stubbed actionshould be presentedname
stubthe stubbed implementationshould be presentedsuccess
params.delaydenotes the pause before start in millis0100
params.url(For remote stub) the url to connectshould be presentedhttp://localhost:10000/action
params.server(For remote stub) the url to provide to action to connect to bbhttp://localhosthttp://localhost:8080

Default profile

The simulation can be performed without giving the specific profile. In that case, all actions that need to implement will be replaced with the success stub. Other artifacts will not be generated.

Stubs

  • success: returns a success
  • failure: returns a failure
  • random: returns either a failure or a success randomly
  • remote: connects to the remote server and returns the result of the action. The details can be found in the Remote action.

The stubs success, failure, random have the following param:

  • delay: in millis, the time to delay the stub.

The remote stub has the following params:

  • url: the url to connect to the remote server
  • server: the url to provide to the remote server to connect to the blackboard

Process

The simulation can be performed in on of two ways:

  • using console application from console
  • using a library from rust code

In the code

Just use the builder and the simulator from the simulator module. For details, please see the doc for ForesterBuilder and SimulatorBuilder

#![allow(unused)]
fn main() {
fn smoke() {
     let mut sb = SimulatorBuilder::new();

     let root = PathBuf::from("simulator/smoke");

     sb.root(root.clone());
     sb.profile(PathBuf::from("sim.yaml"));
     
     let mut fb = ForesterBuilder::from_file_system();

     fb.main_file("main.tree".to_string());
     fb.root(root);

     sb.forester_builder(fb);
     
     let mut sim = sb.build().unwrap();
     sim.run().unwrap();
 }

 fn smoke_from_text() {
     let mut sb = SimulatorBuilder::new();

     let sim = PathBuf::from("simulator/smoke/sim.yaml");
     let mut fb = ForesterBuilder::from_text();
     sb.profile(sim);
     
     fb.text(
         r#"
 import "std::actions"

 root main sequence {
     store("info1", "initial")
     retryer(task(config = obj), success())
     store("info2","finish")
 }

 fallback retryer(t:tree, default:tree){
     retry(5) t(..)
     fail("just should fail")
     default(..)
 }

 impl task(config: object);
     "#
         .to_string(),
     );    
     sb.forester_builder(fb);
     let mut sim = sb.build().unwrap();
     sim.run().unwrap();
 }

}

In the console

Just use a f-tree console cli to run a simulation

f-tree sim --root tree\tests\simulator\smoke\  --profile sim.yaml
  • root can be omitted, the <pwd> folder will be taken by default
  • tree can be omitted if only one root definition in the file
  • main can be omitted, by default, the name main.tree will be taken.
  • profile can be omitted, the default profile will be taken.