Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model builder pattern #22

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/common/ast/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct Model {
pub variables: HashMap<Name, DecisionVariable>,
pub constraints: Vec<Expression>,
Expand Down
3 changes: 3 additions & 0 deletions src/common/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
mod ast;
pub use ast::*;

mod model_builder;
pub use model_builder::*;
48 changes: 48 additions & 0 deletions src/common/ast/model_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::collections::HashMap;
use super::ast::*;

pub struct ModelBuilder {
pub variables: HashMap<Name, DecisionVariable>,
pub constraints: Vec<Expression>,
}

impl ModelBuilder {
pub fn new() -> ModelBuilder {
ModelBuilder {
variables: HashMap::new(),
constraints: Vec::new(),
}
}

pub fn add_constraint(mut self, constraint: Expression) -> Self {
self.constraints.push(constraint);
self
}

pub fn add_var(mut self, name: Name, domain: Domain) -> Self {
self.variables.insert(
lixitrixi marked this conversation as resolved.
Show resolved Hide resolved
name,
DecisionVariable {
domain,
},
);
self
}

pub fn add_var_str(mut self, name: &str, domain: Domain) -> Self {
self.variables.insert(
Name::UserName(String::from(name)),
DecisionVariable {
domain,
},
);
self
}

pub fn build(self) -> Model {
Model {
variables: self.variables,
constraints: self.constraints,
}
}
}
68 changes: 68 additions & 0 deletions tests/builder_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::collections::HashMap;

use conjure_oxide::ast::*;

#[test]
fn abc_equality() {
let a = Name::UserName(String::from("a"));
let b = Name::UserName(String::from("b"));
let c = Name::UserName(String::from("c"));

let mut variables = HashMap::new();
variables.insert(
a.clone(),
DecisionVariable {
domain: Domain::IntDomain(vec![Range::Bounded(1, 3)]),
},
);
variables.insert(
b.clone(),
DecisionVariable {
domain: Domain::IntDomain(vec![Range::Bounded(1, 3)]),
},
);
variables.insert(
c.clone(),
DecisionVariable {
domain: Domain::IntDomain(vec![Range::Bounded(1, 3)]),
},
);

let m1 = Model {
variables,
constraints: vec![
Expression::Eq(
Box::new(Expression::Sum(vec![
Expression::Reference(a.clone()),
Expression::Reference(b.clone()),
Expression::Reference(c.clone()),
])),
Box::new(Expression::ConstantInt(4)),
),
Expression::Geq(
Box::new(Expression::Reference(a.clone())),
Box::new(Expression::Reference(b.clone())),
),
],
};

let m2 = ModelBuilder::new()
.add_var_str("a", Domain::IntDomain(vec![Range::Bounded(1, 3)]))
.add_var_str("b", Domain::IntDomain(vec![Range::Bounded(1, 3)]))
.add_var_str("c", Domain::IntDomain(vec![Range::Bounded(1, 3)]))
.add_constraint(Expression::Eq(
Box::new(Expression::Sum(vec![
Expression::Reference(a.clone()),
Expression::Reference(b.clone()),
Expression::Reference(c.clone()),
])),
Box::new(Expression::ConstantInt(4)),
))
.add_constraint(Expression::Geq(
Box::new(Expression::Reference(a.clone())),
Box::new(Expression::Reference(b.clone())),
))
.build();

assert!(m1 == m2);
}
Loading