snark_tool/procedure/basic_procedures/chrom_props/
config.rs

1use crate::procedure::basic_procedures::colour::ColouriserType;
2use crate::procedure::error::Error;
3use crate::procedure::helpers::config_helper;
4use crate::procedure::procedure::Result;
5use serde::Deserialize;
6use std::collections::HashMap;
7
8pub const DFS_COLOURISER: &str = "dfs";
9pub const SAT_COLOURISER: &str = "sat";
10
11// property
12pub const CRITICAL: &str = "critical";
13pub const COCRITICAL: &str = "cocritical";
14pub const VERTEX_SUBCRITICAL: &str = "vertex-subcritical";
15pub const EDGE_SUBCRITICAL: &str = "edge-subcritical";
16pub const ACRITICAL: &str = "acritical";
17pub const STABLE: &str = "stable";
18pub const COSTABLE: &str = "costable";
19pub const RESISTANCE: &str = "resistance";
20pub const GIRTH: &str = "girth";
21pub const CYCLIC_EDGE_CONNECTIVITY: &str = "cyclic-edge-connectivity";
22pub const EDGE_RESISTIBILITY: &str = "edge-resistibility";
23pub const VERTEX_RESISTIBILITY: &str = "vertex-resistibility";
24pub const ODDNESS: &str = "oddness";
25
26pub const VERTEX_RESISTIBILITIES: &str = "vertex-resistibilities";
27pub const VERTEX_RESISTIBILITY_INDEX: &str = "vertex-resistibility-index";
28pub const EDGE_RESISTIBILITIES: &str = "edge-resistibilities";
29pub const EDGE_RESISTIBILITY_INDEX: &str = "edge-resistibility-index";
30
31// property name
32pub const COLOURISER_TYPE: &str = "colouriser-type";
33pub const PARALLELIZATION: &str = "parallelization";
34pub const PROPERTIES: &str = "properties";
35pub const GRAPH_INDEX: &str = "graph-index";
36pub const MAX_THREADS: &str = "max-threads";
37
38pub struct ChromaticPropsProcedureConfig {
39    pub colouriser_type: ColouriserType,
40    pub parallelization: ParallelizationType,
41    pub properties_to_compute: ChromaticPropertiesToCompute,
42    pub max_threads: usize,
43}
44
45impl ChromaticPropsProcedureConfig {
46    pub const PROC_TYPE: &'static str = "chromatic-properties";
47
48    pub fn new(
49        colouriser_type: ColouriserType,
50        parallelization: ParallelizationType,
51        properties_to_compute: ChromaticPropertiesToCompute,
52        max_threads: usize,
53    ) -> Self {
54        ChromaticPropsProcedureConfig {
55            colouriser_type,
56            parallelization,
57            properties_to_compute,
58            max_threads,
59        }
60    }
61
62    pub fn from_proc_config(config: &HashMap<String, serde_json::Value>) -> Result<Self> {
63        let colouriser_type = config_helper::resolve_value_or_default(
64            &config,
65            COLOURISER_TYPE,
66            DFS_COLOURISER.to_string(),
67            Self::PROC_TYPE,
68        )?;
69
70        let parallelization_str = config_helper::resolve_value_or_default(
71            &config,
72            PARALLELIZATION,
73            PARALL_NONE.to_string(),
74            Self::PROC_TYPE,
75        )?;
76
77        let properties = config_helper::resolve_value(&config, PROPERTIES, Self::PROC_TYPE)?;
78        let properties_to_compute = ChromaticPropertiesToCompute::new();
79
80        let cpus_count = num_cpus::get();
81        let max_cpus = config_helper::resolve_value_or_default(
82            &config,
83            MAX_THREADS,
84            cpus_count,
85            Self::PROC_TYPE,
86        )?;
87
88        let mut result = ChromaticPropsProcedureConfig {
89            colouriser_type: ColouriserType::from_string(&colouriser_type)?,
90            // parallel,
91            parallelization: ParallelizationType::from_string(parallelization_str)?,
92            properties_to_compute,
93            max_threads: max_cpus,
94        };
95        result.resolve_properties_to_compute(properties);
96        Ok(result)
97    }
98
99    fn resolve_properties_to_compute(&mut self, properties: Vec<String>) {
100        for property in properties.iter() {
101            match property.as_str() {
102                CRITICAL => {
103                    self.properties_to_compute.critical = true;
104                }
105                COCRITICAL => {
106                    self.properties_to_compute.cocritical = true;
107                }
108                VERTEX_SUBCRITICAL => {
109                    self.properties_to_compute.vertex_subcritical = true;
110                }
111                EDGE_SUBCRITICAL => {
112                    self.properties_to_compute.edge_subcritical = true;
113                }
114                ACRITICAL => {
115                    self.properties_to_compute.acritical = true;
116                }
117                STABLE => {
118                    self.properties_to_compute.stable = true;
119                }
120                COSTABLE => {
121                    self.properties_to_compute.costable = true;
122                }
123                GIRTH => {
124                    self.properties_to_compute.girth = true;
125                }
126                CYCLIC_EDGE_CONNECTIVITY => {
127                    self.properties_to_compute.cyclic_connectivity = true;
128                }
129                RESISTANCE => {
130                    self.properties_to_compute.resistance = true;
131                }
132                EDGE_RESISTIBILITY => {
133                    self.properties_to_compute.edge_resistibility = true;
134                }
135                VERTEX_RESISTIBILITY => {
136                    self.properties_to_compute.vertex_resistibility = true;
137                }
138                ODDNESS => {
139                    self.properties_to_compute.oddness = true;
140                }
141                _ => {}
142            }
143        }
144    }
145
146    pub fn colouriser_type(&self) -> &ColouriserType {
147        &self.colouriser_type
148    }
149
150    pub fn parallelization(&self) -> &ParallelizationType {
151        &self.parallelization
152    }
153}
154
155#[derive(Clone)]
156pub struct ChromaticPropertiesToCompute {
157    pub critical: bool,
158    pub cocritical: bool,
159    pub vertex_subcritical: bool,
160    pub edge_subcritical: bool,
161    pub acritical: bool,
162    pub stable: bool,
163    pub costable: bool,
164
165    pub resistance: bool,
166    pub edge_resistibility: bool,
167    pub vertex_resistibility: bool,
168    pub girth: bool,
169    pub cyclic_connectivity: bool,
170    pub oddness: bool,
171}
172
173impl ChromaticPropertiesToCompute {
174    pub fn new() -> Self {
175        ChromaticPropertiesToCompute {
176            critical: false,
177            cocritical: false,
178            vertex_subcritical: false,
179            edge_subcritical: false,
180            acritical: false,
181            stable: false,
182            costable: false,
183            resistance: false,
184            edge_resistibility: false,
185            vertex_resistibility: false,
186            girth: false,
187            cyclic_connectivity: false,
188            oddness: false,
189        }
190    }
191}
192
193// parallelization types
194const PARALL_BATCH: &str = "batch-based";
195const PARALL_GRAPH: &str = "graph-based";
196const PARALL_NONE: &str = "none";
197
198#[derive(Clone, Deserialize)]
199pub enum ParallelizationType {
200    BatchBased,
201    GraphBased,
202    None,
203}
204
205impl ParallelizationType {
206    pub fn from_string(string: impl AsRef<str>) -> Result<ParallelizationType> {
207        let parallel_type;
208        match string.as_ref() {
209            PARALL_BATCH => parallel_type = ParallelizationType::BatchBased,
210            PARALL_GRAPH => parallel_type = ParallelizationType::GraphBased,
211            PARALL_NONE => parallel_type = ParallelizationType::None,
212            _ => {
213                return Err(Error::ConfigError(String::from(format!(
214                    "unknown parallelization type: {}, did you mean {}, {} or {}?",
215                    string.as_ref(),
216                    PARALL_BATCH,
217                    PARALL_GRAPH,
218                    PARALL_NONE
219                ))));
220            }
221        }
222        Ok(parallel_type)
223    }
224}