From 5abbbc3bf9dea9936c37af366edf9bc9a7b0e212 Mon Sep 17 00:00:00 2001 From: rendo Date: Wed, 1 Apr 2026 15:58:12 +0500 Subject: [PATCH] Gene refactor --- .../src/genetics/gene/gene_place.rs | 34 ++++++++ .../src/genetics/gene/gene_source.rs | 20 +++++ .../genetics/{gene.rs => gene/gene_types.rs} | 87 +------------------ rust-pvz-genetics/src/genetics/gene/mod.rs | 34 ++++++++ rust-pvz-genetics/src/genetics/genome.rs | 2 +- rust-pvz-genetics/src/genetics/tests.rs | 42 ++++----- rust-pvz-genetics/src/lib.rs | 1 + rust-pvz-genetics/src/wrapper/mod.rs | 0 8 files changed, 114 insertions(+), 106 deletions(-) create mode 100644 rust-pvz-genetics/src/genetics/gene/gene_place.rs create mode 100644 rust-pvz-genetics/src/genetics/gene/gene_source.rs rename rust-pvz-genetics/src/genetics/{gene.rs => gene/gene_types.rs} (64%) create mode 100644 rust-pvz-genetics/src/genetics/gene/mod.rs create mode 100644 rust-pvz-genetics/src/wrapper/mod.rs diff --git a/rust-pvz-genetics/src/genetics/gene/gene_place.rs b/rust-pvz-genetics/src/genetics/gene/gene_place.rs new file mode 100644 index 0000000..6704cab --- /dev/null +++ b/rust-pvz-genetics/src/genetics/gene/gene_place.rs @@ -0,0 +1,34 @@ +use std::fmt::Display; + +#[derive(PartialEq,Eq,Clone,Copy)] +pub enum GenePlace { + Root, + Stem, + Head, + Face, + Leaf, +} + +impl Display for GenePlace { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f,"{}",match self { + GenePlace::Root => "Root", + GenePlace::Stem => "Stem", + GenePlace::Head => "Head", + GenePlace::Face => "Face", + GenePlace::Leaf => "Leaf", + }) + } +} + +impl GenePlace { + pub fn broad(&self) -> GenePlace { + match self { + GenePlace::Root => GenePlace::Root, + GenePlace::Stem => GenePlace::Stem, + GenePlace::Head => GenePlace::Head, + GenePlace::Face => GenePlace::Face, + GenePlace::Leaf => GenePlace::Leaf, + } + } +} diff --git a/rust-pvz-genetics/src/genetics/gene/gene_source.rs b/rust-pvz-genetics/src/genetics/gene/gene_source.rs new file mode 100644 index 0000000..4d63c6f --- /dev/null +++ b/rust-pvz-genetics/src/genetics/gene/gene_source.rs @@ -0,0 +1,20 @@ +use std::fmt::Display; + +#[derive(PartialEq,Eq,Clone,Copy)] +pub enum GeneSource { + Peashooter, + Sunflower, + CherryBomb, + PotatoMine, +} + +impl Display for GeneSource { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f,"{}",match self { + GeneSource::Peashooter => "Peashooter", + GeneSource::Sunflower => "Sunflower", + GeneSource::CherryBomb => "CherryBomb", + GeneSource::PotatoMine => "PotatoMine", + }) + } +} diff --git a/rust-pvz-genetics/src/genetics/gene.rs b/rust-pvz-genetics/src/genetics/gene/gene_types.rs similarity index 64% rename from rust-pvz-genetics/src/genetics/gene.rs rename to rust-pvz-genetics/src/genetics/gene/gene_types.rs index dac4bf9..ba861e4 100644 --- a/rust-pvz-genetics/src/genetics/gene.rs +++ b/rust-pvz-genetics/src/genetics/gene/gene_types.rs @@ -1,35 +1,5 @@ -use std::fmt::Display; - use crate::genetics::flow::Flow; -#[derive(PartialEq,Eq,Clone)] -pub struct Gene { - pub plant_source: GeneSource, - pub place: GenePlace, - pub kind: GeneType -} - -pub struct GeneContext { - pub flow: Flow -} - -#[derive(PartialEq,Eq,Clone,Copy)] -pub enum GeneSource { - Peashooter, - Sunflower, - CherryBomb, - PotatoMine, -} - -#[derive(PartialEq,Eq,Clone,Copy)] -pub enum GenePlace { - Root, - Stem, - Head, - Face, - Leaf, -} - /// GeneType is an enum that defines behaviour of gene. #[derive(PartialEq,Eq,Clone)] pub enum GeneType { @@ -70,6 +40,9 @@ pub enum GeneType { }, } +pub struct GeneContext { + pub flow: Flow +} pub enum ChildDistribution{ Single, Multiple{amount: usize}, @@ -83,48 +56,6 @@ pub enum ProduceDecision { ModifySingle, } -impl Gene { - pub fn new(plant_source: GeneSource, place: GenePlace, kind: GeneType) -> Self { - Self { - plant_source, - place, - kind, - } - } -} - -impl Display for GeneSource { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f,"{}",match self { - GeneSource::Peashooter => "Peashooter", - GeneSource::Sunflower => "Sunflower", - GeneSource::CherryBomb => "CherryBomb", - GeneSource::PotatoMine => "PotatoMine", - }); - Ok(()) - } -} - -impl Display for GenePlace { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f,"{}",match self { - GenePlace::Root => "Root", - GenePlace::Stem => "Stem", - GenePlace::Head => "Head", - GenePlace::Face => "Face", - GenePlace::Leaf => "Leaf", - }); - Ok(()) - } -} - -impl Display for Gene { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f,"({}) {}",self.plant_source,self.place); - Ok(()) - } -} - impl GeneType { /// Creates pure producer with create function. pub fn pure_producer(flow_function: fn(GeneContext) -> Flow) -> Self { @@ -163,15 +94,3 @@ impl GeneType { ChildDistribution::All } } - -impl GenePlace { - pub fn broad(&self) -> GenePlace { - match self { - GenePlace::Root => GenePlace::Root, - GenePlace::Stem => GenePlace::Stem, - GenePlace::Head => GenePlace::Head, - GenePlace::Face => GenePlace::Face, - GenePlace::Leaf => GenePlace::Leaf, - } - } -} diff --git a/rust-pvz-genetics/src/genetics/gene/mod.rs b/rust-pvz-genetics/src/genetics/gene/mod.rs new file mode 100644 index 0000000..5c10536 --- /dev/null +++ b/rust-pvz-genetics/src/genetics/gene/mod.rs @@ -0,0 +1,34 @@ +use std::fmt::Display; + +pub mod gene_types; +pub mod gene_place; +pub mod gene_source; + +pub use gene_types::GeneType; +pub use gene_source::GeneSource; +pub use gene_place::GenePlace; + +#[derive(PartialEq,Eq,Clone)] +pub struct Gene { + pub plant_source: GeneSource, + pub place: GenePlace, + pub kind: GeneType +} + +impl Gene { + pub fn new(plant_source: GeneSource, place: GenePlace, kind: GeneType) -> Self { + Self { + plant_source, + place, + kind, + } + } +} + + + +impl Display for Gene { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f,"({}) {}",self.plant_source,self.place) + } +} diff --git a/rust-pvz-genetics/src/genetics/genome.rs b/rust-pvz-genetics/src/genetics/genome.rs index 42fe494..f63b345 100644 --- a/rust-pvz-genetics/src/genetics/genome.rs +++ b/rust-pvz-genetics/src/genetics/genome.rs @@ -1,4 +1,4 @@ -use std::{collections::VecDeque, fmt::Display}; +use std::fmt::Display; use crate::genetics::gene::Gene; diff --git a/rust-pvz-genetics/src/genetics/tests.rs b/rust-pvz-genetics/src/genetics/tests.rs index 9ef85b3..2fe62bf 100644 --- a/rust-pvz-genetics/src/genetics/tests.rs +++ b/rust-pvz-genetics/src/genetics/tests.rs @@ -1,10 +1,10 @@ -use crate::genetics::{flow::Flow, gene::{Gene, GeneSource, GeneType}, genome::Genome, manipulations::PairGenomeModificator, plant_templates::{cherry_bomb_template, peashooter_template, sunflower_template}}; +use crate::genetics::{flow::Flow, gene::{Gene,GenePlace, GeneSource, GeneType}, genome::Genome, manipulations::PairGenomeModificator, plant_templates::{cherry_bomb_template, peashooter_template, sunflower_template}}; #[test] fn test_display() { let display_target = cherry_bomb_template(); println!("{}",display_target); - assert!(display_target.to_string() == "(CherryBomb) root\n\t(CherryBomb) head\n\t\t(CherryBomb) face\n\t(CherryBomb) head\n\t\t(CherryBomb) face\n"); + assert!(display_target.to_string() == "(CherryBomb) Root\n\t(CherryBomb) Head\n\t\t(CherryBomb) Face\n\t(CherryBomb) Head\n\t\t(CherryBomb) Face\n"); } #[test] @@ -21,11 +21,11 @@ fn test_allelic_crossingover() { println!("Sunflower:\n{}",sunflower); assert!(peashooter.to_string() == Genome::from_edges(vec![ - Gene::new(GeneSource::Sunflower,"root", GeneType::Structural), - Gene::new(GeneSource::Peashooter,"stem", GeneType::fill_all()), - Gene::new(GeneSource::Peashooter,"head",GeneType::random_distribution()), - Gene::new(GeneSource::Sunflower,"face", GeneType::consumer(|_|{})), - Gene::new(GeneSource::Peashooter,"face",GeneType::consumer(|_|{})) + Gene::new(GeneSource::Sunflower,GenePlace::Root, GeneType::Structural), + Gene::new(GeneSource::Peashooter,GenePlace::Stem, GeneType::fill_all()), + Gene::new(GeneSource::Peashooter,GenePlace::Head,GeneType::random_distribution()), + Gene::new(GeneSource::Sunflower,GenePlace::Face, GeneType::consumer(|_|{})), + Gene::new(GeneSource::Peashooter,GenePlace::Face,GeneType::consumer(|_|{})) ],vec![ (0,1), (1,2), @@ -34,10 +34,10 @@ fn test_allelic_crossingover() { ]).unwrap().to_string()); assert!(sunflower.to_string() == Genome::from_edges(vec![ - Gene::new(GeneSource::Peashooter,"root", GeneType::pure_producer(|_|{Flow::empty()})), - Gene::new(GeneSource::Sunflower,"stem", GeneType::Structural), - Gene::new(GeneSource::Sunflower,"head", GeneType::modifier(|_|{Flow::empty()})), - Gene::new(GeneSource::Peashooter,"leaf",GeneType::modifier(|_|{Flow::empty()})), + Gene::new(GeneSource::Peashooter,GenePlace::Root, GeneType::pure_producer(|_|{Flow::empty()})), + Gene::new(GeneSource::Sunflower,GenePlace::Stem, GeneType::Structural), + Gene::new(GeneSource::Sunflower,GenePlace::Head, GeneType::modifier(|_|{Flow::empty()})), + Gene::new(GeneSource::Peashooter,GenePlace::Leaf,GeneType::modifier(|_|{Flow::empty()})), ], vec![ (0,1), (1,2), @@ -59,11 +59,11 @@ fn test_categoric_crossingover() { println!("CherryBomb:\n{}",cherry); assert!(peashooter.to_string() == Genome::from_edges(vec![ - Gene::new(GeneSource::CherryBomb,"root", GeneType::modifier(|_|{Flow::empty()})), - Gene::new(GeneSource::Peashooter,"stem", GeneType::fill_all()), - Gene::new(GeneSource::CherryBomb,"head", GeneType::consumer(|_|{})), - Gene::new(GeneSource::Peashooter,"leaf",GeneType::modifier(|_|{Flow::empty()})), - Gene::new(GeneSource::CherryBomb,"face", GeneType::Structural), + Gene::new(GeneSource::CherryBomb,GenePlace::Root, GeneType::modifier(|_|{Flow::empty()})), + Gene::new(GeneSource::Peashooter,GenePlace::Stem, GeneType::fill_all()), + Gene::new(GeneSource::CherryBomb,GenePlace::Head, GeneType::consumer(|_|{})), + Gene::new(GeneSource::Peashooter,GenePlace::Leaf,GeneType::modifier(|_|{Flow::empty()})), + Gene::new(GeneSource::CherryBomb,GenePlace::Face, GeneType::Structural), ],vec![ (0,1), (1,2), @@ -72,11 +72,11 @@ fn test_categoric_crossingover() { ]).unwrap().to_string()); assert!(cherry.to_string() == Genome::from_edges(vec![ - Gene::new(GeneSource::Peashooter,"root", GeneType::pure_producer(|_|{Flow::empty()})), - Gene::new(GeneSource::CherryBomb,"head", GeneType::consumer(|_|{})), - Gene::new(GeneSource::Peashooter,"head",GeneType::random_distribution()), - Gene::new(GeneSource::CherryBomb,"face", GeneType::Structural), - Gene::new(GeneSource::Peashooter,"face",GeneType::consumer(|_|{})), + Gene::new(GeneSource::Peashooter,GenePlace::Root, GeneType::pure_producer(|_|{Flow::empty()})), + Gene::new(GeneSource::CherryBomb,GenePlace::Head, GeneType::consumer(|_|{})), + Gene::new(GeneSource::Peashooter,GenePlace::Head,GeneType::random_distribution()), + Gene::new(GeneSource::CherryBomb,GenePlace::Face, GeneType::Structural), + Gene::new(GeneSource::Peashooter,GenePlace::Face,GeneType::consumer(|_|{})), ], vec![ (0,1), (0,2), diff --git a/rust-pvz-genetics/src/lib.rs b/rust-pvz-genetics/src/lib.rs index 8bf847c..9f8d80d 100644 --- a/rust-pvz-genetics/src/lib.rs +++ b/rust-pvz-genetics/src/lib.rs @@ -1 +1,2 @@ pub mod genetics; +pub mod wrapper; diff --git a/rust-pvz-genetics/src/wrapper/mod.rs b/rust-pvz-genetics/src/wrapper/mod.rs new file mode 100644 index 0000000..e69de29