-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `Submodel`, and refactor `Model` to be a wrapper over `Submodel`. This commit is part of on-going work to add lexical scope to Conjure Oxide and is a follow up to 8636432 (feat!: add parent symbol tables (#680), 2025-02-17). DETAILS A `Submodel` represents a particular scope and holds the symbol-table and constraints tree for that scope. This commit refactors `Model` to be a wrapper over `Submodel`, and removes methods operating on constraints and the symbol table from `Model`, placing them in `Submodel` instead. A `Model` can be borrowed as a `Submodel` using `as_submodel()` and `as_submodel_mut()`. The language semantics of a top level model and a sub-model are identical, so treating it as `Submodel` in most cases is valid. `Model` is a separate type than `Submodel` for the following reasons: + It will hold global-only information in the future, such as dominance constraints. + It holds a pointer to the context. + We need special initialisation and de-serialisation logic for the top level model that we do not want for `Submodel`. See `SerdeModel`.
- Loading branch information
1 parent
4043234
commit 542c89e
Showing
22 changed files
with
407 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
// Tests for various functionalities of the Model | ||
|
||
use std::{cell::RefCell, rc::Rc}; | ||
use std::rc::Rc; | ||
|
||
use conjure_core::ast::Model; | ||
use conjure_oxide::ast::*; | ||
use declaration::Declaration; | ||
|
||
#[test] | ||
fn modify_domain() { | ||
let mut m = Model::new(Default::default()); | ||
|
||
let mut symbols = m.as_submodel_mut().symbols_mut(); | ||
|
||
let a = Name::UserName(String::from("a")); | ||
|
||
let d1 = Domain::IntDomain(vec![Range::Bounded(1, 3)]); | ||
let d2 = Domain::IntDomain(vec![Range::Bounded(1, 2)]); | ||
|
||
let mut symbols = SymbolTable::new(); | ||
symbols | ||
.insert(Rc::new(Declaration::new_var(a.clone(), d1.clone()))) | ||
.unwrap(); | ||
|
||
let m = Model::new(Rc::new(RefCell::new(symbols)), vec![], Default::default()); | ||
|
||
assert_eq!(&m.symbols().domain(&a).unwrap(), &d1); | ||
assert_eq!(symbols.domain(&a).unwrap(), d1); | ||
|
||
let mut decl_a = m.symbols().lookup(&a).unwrap(); | ||
let mut decl_a = symbols.lookup(&a).unwrap(); | ||
|
||
Rc::make_mut(&mut decl_a).as_var_mut().unwrap().domain = d2.clone(); | ||
|
||
m.symbols_mut().update_insert(decl_a); | ||
symbols.update_insert(decl_a); | ||
|
||
assert_eq!(&m.symbols().domain(&a).unwrap(), &d2); | ||
assert_eq!(symbols.domain(&a).unwrap(), d2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
pub mod pretty; | ||
pub mod types; | ||
pub mod serde; | ||
|
||
mod atom; | ||
pub mod declaration; | ||
mod declaration; | ||
mod domains; | ||
mod expressions; | ||
mod literals; | ||
pub mod model; | ||
mod model; | ||
mod name; | ||
pub mod serde; | ||
mod submodel; | ||
mod symbol_table; | ||
mod types; | ||
mod variables; | ||
|
||
pub use atom::Atom; | ||
pub use declaration::*; | ||
pub use domains::Domain; | ||
pub use domains::Range; | ||
pub use expressions::Expression; | ||
pub use literals::Literal; | ||
pub use model::Model; | ||
pub use model::*; | ||
pub use name::Name; | ||
pub use submodel::SubModel; | ||
pub use symbol_table::SymbolTable; | ||
pub use types::ReturnType; | ||
pub use types::*; | ||
pub use variables::DecisionVariable; |
Oops, something went wrong.