Skip to content

Commit

Permalink
Engine/world from config (#882)
Browse files Browse the repository at this point in the history
* feat: clean `from::engine` interface
  • Loading branch information
Autoparallel authored Feb 13, 2024
1 parent cc509b1 commit aea4a47
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
3 changes: 3 additions & 0 deletions arbiter-engine/src/examples/minter/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# id for the world
id = "minter_world"

# top level id for the `TokenAdmin` agent
[[admin]]
# named struct and arguments for initializing the `TokenAdmin` agent
Expand Down
5 changes: 2 additions & 3 deletions arbiter-engine/src/examples/minter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ enum Behaviors {

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn config_test() {
let mut world = World::new("world");
world.from_config::<Behaviors>("src/examples/minter/config.toml");

let mut world = World::from_config::<Behaviors>("src/examples/minter/config.toml").unwrap();
assert_eq!(world.id, "minter_world");
world.run().await;
}
2 changes: 2 additions & 0 deletions arbiter-engine/src/examples/timed_message/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
id = "timed_message_world"

[[ping]]
TimedMessage = { delay = 1, send_data = "ping", receive_data = "pong", startup_message = "ping" }

Expand Down
6 changes: 3 additions & 3 deletions arbiter-engine/src/examples/timed_message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ enum Behaviors {

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn config_test() {
let mut world = World::new("world");
world.from_config::<Behaviors>("src/examples/timed_message/config.toml");

let mut world =
World::from_config::<Behaviors>("src/examples/timed_message/config.toml").unwrap();
assert_eq!(world.id, "timed_message_world");
world.run().await;
}
20 changes: 14 additions & 6 deletions arbiter-engine/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ impl World {
/// BehaviorTypeC = { ... }
/// ```
pub fn from_config<C: CreateStateMachine + Serialize + DeserializeOwned + Debug>(
&mut self,
config_path: &str,
) -> Result<(), ArbiterEngineError> {
) -> Result<Self, ArbiterEngineError> {
let cwd = std::env::current_dir()?;
let path = cwd.join(config_path);
info!("Reading from path: {:?}", path);
Expand All @@ -109,17 +108,26 @@ impl World {
let mut contents = String::new();
file.read_to_string(&mut contents)?;

let agents_map: HashMap<String, Vec<C>> = toml::from_str(&contents)?;
#[derive(Deserialize)]
struct Config<C> {
id: Option<String>,
#[serde(flatten)]
agents_map: HashMap<String, Vec<C>>,
}

let config: Config<C> = toml::from_str(&contents)?;

for (agent, behaviors) in agents_map {
let mut world = World::new(&config.id.unwrap_or_else(|| "world".to_owned()));

for (agent, behaviors) in config.agents_map {
let mut next_agent = Agent::builder(&agent);
for behavior in behaviors {
let engine = behavior.create_state_machine();
next_agent = next_agent.with_engine(engine);
}
self.add_agent(next_agent);
world.add_agent(next_agent);
}
Ok(())
Ok(world)
}

/// Adds an agent, constructed from the provided `AgentBuilder`, to the
Expand Down
3 changes: 1 addition & 2 deletions arbiter-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ pub fn main(attr: TokenStream, item: TokenStream) -> TokenStream {
match &args.command {
Some(Commands::Simulate { config_path }) => {
println!("Simulating configuration: {}", config_path);
let mut world = World::new("world");
world.from_config::<#behaviors>(config_path);
let mut world = World::from_config::<#behaviors>(config_path)?;
world.run().await?;
},
None => {
Expand Down

0 comments on commit aea4a47

Please sign in to comment.