Skip to content

Commit

Permalink
Add cellular automaton builder
Browse files Browse the repository at this point in the history
  • Loading branch information
be-next committed Dec 27, 2023
1 parent 42a7bd0 commit 321b334
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/core/cellular_automaton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ impl CA1D {
self.next_world = Box::new(next_world);
}

pub fn set_world_initialisation(&mut self, world_initialisation: Vec<i8>) {
self.current_world = Box::new(world_initialisation);
}

pub fn set_num_states(&mut self, num_states: u8) {
self.num_states = num_states;
}
Expand All @@ -107,5 +111,9 @@ impl CA1D {
std::mem::swap(&mut self.current_world, &mut self.next_world);
}

pub fn print(&self) {
println!("{}", self);
}

}

48 changes: 48 additions & 0 deletions src/core/cellular_automaton_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::core::cellular_automaton::CA1D;
use crate::core::cellular_automaton_configuration::CA1DConfiguration;
use crate::core::cellular_automaton_configuration::CA1DConfigurationError;
use crate::core::lookup_table_builder::LookupTableBuilder;
use crate::core::lookup_table_builder::LookupTableBuilderError;

pub struct CellularAutomatonBuilder;

#[derive(Debug)]
pub enum CellularAutomatonBuilderError {
ConfigurationError(CA1DConfigurationError),
LookupTableBuilderError(LookupTableBuilderError),
}

impl From<CA1DConfigurationError> for CellularAutomatonBuilderError {
fn from(err: CA1DConfigurationError) -> Self {
CellularAutomatonBuilderError::ConfigurationError(err)
}
}

impl From<LookupTableBuilderError> for CellularAutomatonBuilderError {
fn from(err: LookupTableBuilderError) -> Self {
CellularAutomatonBuilderError::LookupTableBuilderError(err)
}
}

impl CellularAutomatonBuilder {
pub fn new() -> Self {
Self {}
}

pub fn build(&self, configuration_file_name: &str) -> Result<CA1D, CellularAutomatonBuilderError> {
let ca1d_configuration = CA1DConfiguration::new_from_json_file(configuration_file_name)?;

let lookup_table_builder = LookupTableBuilder::new();
let lookup_table = lookup_table_builder.build(ca1d_configuration.get_rules_file_name())?;

let mut ca1d = CA1D::new(
ca1d_configuration.get_num_states(),
ca1d_configuration.get_num_cells(),
lookup_table,
);

ca1d.set_world_initialisation(ca1d_configuration.get_world_initialisation());

Ok(ca1d)
}
}
12 changes: 12 additions & 0 deletions src/core/cellular_automaton_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,16 @@ impl CA1DConfiguration {
pub fn get_world_initialisation(&self) -> Vec<i8> {
self.world_initialisation.clone().into()
}

pub fn get_rules_file_name(&self) -> &str {
&self.rules_file_name
}

pub fn get_num_states(&self) -> u8 {
self.num_states
}

pub fn get_num_cells(&self) -> usize {
self.num_cells
}
}
1 change: 1 addition & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod lookup_table_builder;
pub mod rules;
pub mod cellular_automaton;
pub mod cellular_automaton_configuration;
pub mod cellular_automaton_builder;
15 changes: 15 additions & 0 deletions tests/test_cellular_automaton_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use mono_axis::core::cellular_automaton_builder::CellularAutomatonBuilder;

#[test]
fn it_works() {
let cab = CellularAutomatonBuilder::new();
let mut ca = cab.build("examples/example_01/configuration.json").unwrap();

for _ in 0..7 {
ca.step();
}

let result = ca.get_current_world();

assert_eq!(*result, vec![0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]);
}

0 comments on commit 321b334

Please sign in to comment.