Gene, flow and manipulations
This commit is contained in:
parent
6be5d415ab
commit
4ed86fda31
7 changed files with 119 additions and 48 deletions
|
|
@ -1,61 +1,123 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use crate::genetics::flow::Flow;
|
||||
|
||||
#[derive(PartialEq,Eq,Clone)]
|
||||
pub struct Gene {
|
||||
pub plant_name: String,
|
||||
pub place: String,
|
||||
pub kind: GeneType
|
||||
}
|
||||
|
||||
pub struct GeneContext {
|
||||
pub flow: Flow
|
||||
}
|
||||
|
||||
/// GeneType is an enum that defines behaviour of gene.
|
||||
#[derive(PartialEq,Eq,Clone)]
|
||||
pub enum GeneType {
|
||||
/// Without functional behaviour. Doesn't pass through flow.
|
||||
Dummy,
|
||||
/// Only creates flow. Can be set up to not create flow if flow isn't empty.
|
||||
PureProducer {
|
||||
create_flow: fn()
|
||||
create_flow: fn(GeneContext) -> Flow
|
||||
},
|
||||
/// Creates or modifies flow. Needs to decide whether to create or modify flow.
|
||||
Producer {
|
||||
create_flow: fn(GeneContext) -> Flow,
|
||||
modify_single: fn(GeneContext) -> Flow,
|
||||
modify_all: fn(GeneContext) -> Flow,
|
||||
decide_modification_type: fn(GeneContext) -> ProduceDecision,
|
||||
},
|
||||
/// Allows flow to pass through.
|
||||
Transport {
|
||||
distribute_between_children: fn()
|
||||
distribute_between_children: fn(GeneContext) -> ChildDistribution
|
||||
},
|
||||
/// Consumes flow, destroying it in process.
|
||||
Consumer {
|
||||
consume: fn()
|
||||
consume: fn(GeneContext)
|
||||
},
|
||||
/// Modifies flow
|
||||
Modifier {
|
||||
modify_flow: fn()
|
||||
modify_flow: fn(GeneContext) -> Option<Flow>
|
||||
},
|
||||
/// Conditionally passes through flow. Acts like transport.
|
||||
Trigger {
|
||||
check_trigger: fn()
|
||||
check_trigger: fn(GeneContext) -> bool,
|
||||
distribute_between_children: fn(GeneContext) -> ChildDistribution
|
||||
},
|
||||
/// Observes flow. Acts like transport.
|
||||
Observer {
|
||||
observe: fn()
|
||||
observe: fn(GeneContext),
|
||||
distribute_between_children: fn(GeneContext) -> ChildDistribution
|
||||
},
|
||||
}
|
||||
|
||||
pub enum ChildDistribution{
|
||||
Single,
|
||||
Multiple{amount: usize},
|
||||
All,
|
||||
Custom{child_indicies: Vec<usize>}
|
||||
}
|
||||
|
||||
pub enum ProduceDecision {
|
||||
Create,
|
||||
ModifyAll,
|
||||
ModifySingle,
|
||||
}
|
||||
|
||||
impl Gene {
|
||||
pub fn new(place_str: impl Into<String>, kind: GeneType) -> Self {
|
||||
pub fn new(plant_str: impl Into<String>, place_str: impl Into<String>, kind: GeneType) -> Self {
|
||||
Self {
|
||||
plant_name: plant_str.into(),
|
||||
place: place_str.into(),
|
||||
kind,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GeneType {
|
||||
pub fn pure_producer(flow_function: fn()) -> Self {
|
||||
Self::PureProducer { create_flow: flow_function }
|
||||
}
|
||||
pub fn random_distribution() -> Self {
|
||||
Self::Transport { distribute_between_children: ||{} }
|
||||
}
|
||||
pub fn fill_all() -> Self {
|
||||
Self::Transport { distribute_between_children: ||{} }
|
||||
}
|
||||
pub fn consumer(consumer_function: fn()) -> Self {
|
||||
Self::Consumer { consume: consumer_function }
|
||||
}
|
||||
pub fn modifier(modifier_function: fn()) -> Self {
|
||||
Self::Modifier { modify_flow: modifier_function }
|
||||
}
|
||||
pub fn trigger(trigger_function: fn()) -> Self {
|
||||
Self::Trigger { check_trigger: trigger_function }
|
||||
}
|
||||
pub fn observer(observer_function: fn()) -> Self {
|
||||
Self::Observer { observe: observer_function }
|
||||
impl Display for Gene {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f,"({}) {}",self.plant_name,self.place);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl GeneType {
|
||||
/// Creates pure producer with create function.
|
||||
pub fn pure_producer(flow_function: fn(GeneContext) -> Flow) -> Self {
|
||||
Self::PureProducer { create_flow: flow_function }
|
||||
}
|
||||
/// Creates transport with random distribution.
|
||||
pub fn random_distribution() -> Self {
|
||||
Self::Transport { distribute_between_children: Self::distribute_between_random }
|
||||
}
|
||||
/// Creates transport with distribution between all children.
|
||||
pub fn fill_all() -> Self {
|
||||
Self::Transport { distribute_between_children: Self::distribute_between_all }
|
||||
}
|
||||
/// Creates consumer with consumer function.
|
||||
pub fn consumer(consumer_function: fn(GeneContext)) -> Self {
|
||||
Self::Consumer { consume: consumer_function }
|
||||
}
|
||||
/// Creates modifier with modifier function.
|
||||
pub fn modifier(modifier_function: fn(GeneContext) -> Option<Flow>) -> Self {
|
||||
Self::Modifier { modify_flow: modifier_function }
|
||||
}
|
||||
/// Creates trigger with distribution between all children.
|
||||
pub fn trigger_all(trigger_function: fn(GeneContext) -> bool) -> Self {
|
||||
Self::Trigger { check_trigger: trigger_function, distribute_between_children: Self::distribute_between_all }
|
||||
}
|
||||
/// Creates observer with distribution between all children.
|
||||
pub fn observer_all(observer_function: fn(GeneContext)) -> Self {
|
||||
Self::Observer { observe: observer_function, distribute_between_children: Self::distribute_between_all }
|
||||
}
|
||||
|
||||
|
||||
fn distribute_between_random(_: GeneContext) -> ChildDistribution {
|
||||
ChildDistribution::Single
|
||||
}
|
||||
fn distribute_between_all(_: GeneContext) -> ChildDistribution {
|
||||
ChildDistribution::All
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue