Skip to content

Commit

Permalink
Fix clippy errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
daemontus committed Feb 16, 2021
1 parent d0812a1 commit 3072431
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 105 deletions.
6 changes: 6 additions & 0 deletions src/_impl_graph_task_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ use crate::GraphTaskContext;
use biodivine_lib_param_bn::symbolic_async_graph::{GraphColoredVertices, SymbolicAsyncGraph};
use std::sync::atomic::{AtomicBool, Ordering};

impl Default for GraphTaskContext {
fn default() -> Self {
GraphTaskContext::new()
}
}

impl GraphTaskContext {
/// Create a new task context.
pub fn new() -> GraphTaskContext {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/benchmark_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn erase_regulation_bounds(network: &BooleanNetwork) -> BooleanNetwork {
.as_graph()
.find_variable(network.get_variable_name(old_v))
.unwrap();
let is_input = network.regulators(old_v).len() == 0;
let is_input = network.regulators(old_v).is_empty();
if !is_input {
if let Some(fn_update) = network.get_update_function(old_v).clone() {
bn.add_update_function(new_v, fn_update).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/bin/benchmark_translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
}

fn network_to_bnet(network: &BooleanNetwork) -> String {
let mut model = format!("targets,factors\n");
let mut model = "targets,factors\n".to_string();
for v in network.variables() {
let v_id: usize = v.into();
let line = format!(
Expand All @@ -47,9 +47,9 @@ fn fnupdate_to_bnet_string(fn_update: &FnUpdate) -> String {
FnUpdate::Const(value) => {
if *value {
// There is always v1
format!("v1 | !v1",)
"v1 | !v1".to_string()
} else {
format!("v1 & !v1",)
"v1 & !v1".to_string()
}
}
FnUpdate::Var(id) => {
Expand Down
4 changes: 3 additions & 1 deletion src/bin/benchmark_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ fn main() {
let graph = SymbolicAsyncGraph::new(model);
match graph {
Ok(graph) => {
if graph.unit_colors().approx_cardinality() != 1.0 {
if graph.unit_colors().as_bdd()
!= graph.unit_colors().pick_singleton().as_bdd()
{
errors += 1;
eprintln!(
"ERROR: Default model has {} colors in {}.",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/rbn_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
let mut remaining_regs = 0;
// Each variable must have in/out-degree at least one...
for source in rg.variables() {
if rg.regulators(source).len() > 0 || rg.targets(source).len() > 0 {
if !rg.regulators(source).is_empty() || !rg.targets(source).is_empty() {
continue;
}
let source: usize = source.into();
Expand Down
109 changes: 48 additions & 61 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,18 @@ struct Computation {

impl Computation {
pub fn start_timestamp(&self) -> u128 {
return self
.timestamp
self.timestamp
.duration_since(UNIX_EPOCH)
.expect("Time error")
.as_millis();
.as_millis()
}

pub fn end_timestamp(&self) -> Option<u128> {
return self.finished_timestamp.map(|t| {
self.finished_timestamp.map(|t| {
t.duration_since(UNIX_EPOCH)
.expect("Time error")
.as_millis()
});
})
}
}

Expand All @@ -68,15 +67,15 @@ lazy_static! {
}

fn max_parameter_cardinality(function: &FnUpdate) -> usize {
return match function {
match function {
FnUpdate::Const(_) | FnUpdate::Var(_) => 0,
FnUpdate::Param(_, args) => args.len(),
FnUpdate::Not(inner) => max_parameter_cardinality(inner),
FnUpdate::Binary(_, left, right) => max(
max_parameter_cardinality(left),
max_parameter_cardinality(right),
),
};
}
}

/// Accept a partial model containing only the necessary regulations and one update function.
Expand Down Expand Up @@ -130,7 +129,7 @@ fn check_update_function(data: Data) -> BackendResponse {
};
}

const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const VERSION: &str = env!("CARGO_PKG_VERSION");

#[get("/ping")]
fn ping() -> BackendResponse {
Expand Down Expand Up @@ -165,7 +164,7 @@ fn ping() -> BackendResponse {
}
}
}
return BackendResponse::ok(&response.to_string());
BackendResponse::ok(&response.to_string())
}

// Try to obtain current class data or none if classifier is busy
Expand All @@ -188,7 +187,7 @@ fn try_get_class_params(classifier: &Classifier, class: &Class) -> Option<Option
// wait a little - maybe the lock will become free
std::thread::sleep(Duration::new(1, 0));
}
return None;
None
}

#[get("/get_results")]
Expand Down Expand Up @@ -242,8 +241,8 @@ fn get_results() -> BackendResponse {
let elapsed = if let Some(e) = elapsed { e } else { 0 };

let mut json = String::new();
for index in 0..lines.len() - 1 {
json += &format!("{},", lines[index]);
for line in lines.iter().take(lines.len() - 1) {
json += &format!("{},", line);
}
json = format!(
"{{ \"isPartial\":{}, \"data\":[{}{}], \"elapsed\":{} }}",
Expand All @@ -253,7 +252,7 @@ fn get_results() -> BackendResponse {
elapsed,
);

return BackendResponse::ok(&json);
BackendResponse::ok(&json)
}

#[get("/get_witness/<class_str>")]
Expand Down Expand Up @@ -293,28 +292,28 @@ fn get_witness(class_str: String) -> BackendResponse {
}
BackendResponse::ok(&object! { "model" => model_string }.to_string())
} else {
return BackendResponse::err(&"No results available.".to_string());
BackendResponse::err(&"No results available.".to_string())
}
} else {
return BackendResponse::err(
&"Specified class has no witness.".to_string(),
);
BackendResponse::err(&"Specified class has no witness.".to_string())
}
} else {
return BackendResponse::err(
BackendResponse::err(
&"Classification in progress. Cannot extract witness right now."
.to_string(),
);
)
}
} else {
return BackendResponse::err(&"No results available.".to_string());
BackendResponse::err(&"No results available.".to_string())
}
} else {
return BackendResponse::err(&"No results available.".to_string());
BackendResponse::err(&"No results available.".to_string())
}
}
}

type EdgeList = Vec<(ArrayBitVector, ArrayBitVector)>;

#[get("/get_attractors/<class_str>")]
fn get_attractors(class_str: String) -> BackendResponse {
let mut class = Class::new_empty();
Expand Down Expand Up @@ -347,11 +346,8 @@ fn get_attractors(class_str: String) -> BackendResponse {
.variables()
.map(|id| format!("\"{}\"", witness_network.get_variable_name(id)));

let mut all_attractors: Vec<(
Behaviour,
Vec<(ArrayBitVector, ArrayBitVector)>,
HashSet<usize>,
)> = Vec::new();
let mut all_attractors: Vec<(Behaviour, EdgeList, HashSet<usize>)> =
Vec::new();

// Note that the choice of graph/witness_graph is not arbitrary.
// The attractor set is from the original graph, but source_set/target_set
Expand Down Expand Up @@ -427,11 +423,7 @@ fn get_attractors(class_str: String) -> BackendResponse {
}
}

all_attractors.push((
behaviour.clone(),
attractor_graph,
not_fixed_vars,
));
all_attractors.push((*behaviour, attractor_graph, not_fixed_vars));
}

// now the data is stored in `all_attractors`, just convert it to json:
Expand Down Expand Up @@ -462,7 +454,7 @@ fn get_attractors(class_str: String) -> BackendResponse {
});
}
}
return result;
result
}
let from: String = state_to_binary(&edge.0, not_fixed);
let to: String = state_to_binary(&edge.1, not_fixed);
Expand All @@ -488,24 +480,22 @@ fn get_attractors(class_str: String) -> BackendResponse {
);
BackendResponse::ok(&(json + "}"))
} else {
return BackendResponse::err(&"No results available.".to_string());
BackendResponse::err(&"No results available.".to_string())
}
} else {
return BackendResponse::err(
&"Specified class has no witness.".to_string(),
);
BackendResponse::err(&"Specified class has no witness.".to_string())
}
} else {
return BackendResponse::err(
BackendResponse::err(
&"Classification still in progress. Cannot explore attractors now."
.to_string(),
);
)
}
} else {
return BackendResponse::err(&"No results available.".to_string());
BackendResponse::err(&"No results available.".to_string())
}
} else {
return BackendResponse::err(&"No results available.".to_string());
BackendResponse::err(&"No results available.".to_string())
}
}
}
Expand Down Expand Up @@ -632,9 +622,8 @@ fn start_computation(data: Data) -> BackendResponse {
cmp.thread = None;
} else {
panic!("Cannot finalize thread. No computation found.");
}
};
}
return ();
});
new_cmp.thread = Some(cmp_thread);

Expand Down Expand Up @@ -673,7 +662,7 @@ fn cancel_computation() -> BackendResponse {
}
}
let cmp = cmp.read().unwrap();
return if let Some(cmp) = &*cmp {
if let Some(cmp) = &*cmp {
if cmp.thread.is_none() {
return BackendResponse::err(
&"Nothing to cancel. Computation already done.".to_string(),
Expand All @@ -686,7 +675,7 @@ fn cancel_computation() -> BackendResponse {
}
} else {
BackendResponse::err(&"No computation to cancel.".to_string())
};
}
}

/// Accept an SBML (XML) file and try to parse it into a `BooleanNetwork`.
Expand All @@ -696,7 +685,7 @@ fn cancel_computation() -> BackendResponse {
fn sbml_to_aeon(data: Data) -> BackendResponse {
let mut stream = data.open().take(10_000_000); // limit model to 10MB
let mut sbml_string = String::new();
return match stream.read_to_string(&mut sbml_string) {
match stream.read_to_string(&mut sbml_string) {
Ok(_) => {
match BooleanNetwork::from_sbml(&sbml_string) {
Ok((model, layout)) => {
Expand All @@ -711,7 +700,7 @@ fn sbml_to_aeon(data: Data) -> BackendResponse {
}
}
Err(error) => BackendResponse::err(&format!("{}", error)),
};
}
}

/// Try to read the model layout metadata from the given aeon file.
Expand All @@ -729,7 +718,7 @@ fn read_layout(aeon_string: &str) -> HashMap<String, (f64, f64)> {
layout.insert(var, (x, y));
}
}
return layout;
layout
}

fn read_metadata(aeon_string: &str) -> (Option<String>, Option<String>) {
Expand All @@ -745,7 +734,7 @@ fn read_metadata(aeon_string: &str) -> (Option<String>, Option<String>) {
model_description = Some(captures["desc"].to_string());
}
}
return (model_name, model_description);
(model_name, model_description)
}

/// Accept an Aeon file, try to parse it into a `BooleanNetwork`
Expand All @@ -755,7 +744,7 @@ fn read_metadata(aeon_string: &str) -> (Option<String>, Option<String>) {
fn aeon_to_sbml(data: Data) -> BackendResponse {
let mut stream = data.open().take(10_000_000); // limit model to 10MB
let mut aeon_string = String::new();
return match stream.read_to_string(&mut aeon_string) {
match stream.read_to_string(&mut aeon_string) {
Ok(_) => match BooleanNetwork::try_from(aeon_string.as_str()) {
Ok(network) => {
let layout = read_layout(&aeon_string);
Expand All @@ -765,7 +754,7 @@ fn aeon_to_sbml(data: Data) -> BackendResponse {
Err(error) => BackendResponse::err(&error),
},
Err(error) => BackendResponse::err(&format!("{}", error)),
};
}
}

/// Accept an Aeon file and create an SBML version with all parameters instantiated (a witness model).
Expand All @@ -777,9 +766,7 @@ fn aeon_to_sbml_instantiated(data: Data) -> BackendResponse {
let mut aeon_string = String::new();
return match stream.read_to_string(&mut aeon_string) {
Ok(_) => {
match BooleanNetwork::try_from(aeon_string.as_str())
.and_then(|bn| SymbolicAsyncGraph::new(bn))
{
match BooleanNetwork::try_from(aeon_string.as_str()).and_then(SymbolicAsyncGraph::new) {
Ok(graph) => {
let witness = graph.pick_witness(graph.unit_colors());
let layout = read_layout(&aeon_string);
Expand All @@ -796,7 +783,7 @@ fn aeon_to_sbml_instantiated(data: Data) -> BackendResponse {

fn main() {
//test_main::run();
let address = std::env::var("AEON_ADDR").unwrap_or("localhost".to_string());
let address = std::env::var("AEON_ADDR").unwrap_or_else(|_| "localhost".to_string());
let port: u16 = std::env::var("AEON_PORT")
.ok()
.and_then(|s| s.parse::<u16>().ok())
Expand Down Expand Up @@ -830,20 +817,20 @@ struct BackendResponse {
}

impl BackendResponse {
fn ok(message: &String) -> Self {
return BackendResponse {
fn ok(message: &str) -> Self {
BackendResponse {
message: format!("{{ \"status\": true, \"result\": {} }}", message),
};
}
}

fn err(message: &String) -> Self {
return BackendResponse {
fn err(message: &str) -> Self {
BackendResponse {
message: object! {
"status" => false,
"message" => message.replace("\n", "<br>").clone(),
"message" => message.replace("\n", "<br>"),
}
.to_string(),
};
}
}
}

Expand Down
Loading

0 comments on commit 3072431

Please sign in to comment.