-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |