Skip to content

Commit

Permalink
Merge pull request #124 from niklasdewally/pr/refactor-crates-for-macros
Browse files Browse the repository at this point in the history
Refactor central ADTs into a core module for modularity
  • Loading branch information
ozgurakgun authored Jan 19, 2024
2 parents 0e803ca + e86f7b2 commit 8d8af4a
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 27 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"rust": {
{
"rust-analyzer.check.command": "check",
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true
"editor.formatOnSave": true,
}
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
resolver = "2"
members = [
"conjure_oxide",
"crates/conjure_core",
"solvers/kissat",
"solvers/minion",
"solvers/chuffed",
Expand Down
2 changes: 2 additions & 0 deletions conjure_oxide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ default-run = "conjure_oxide"
walkdir = "2.4.0"

[dependencies]
conjure_core = {path = "../crates/conjure_core" }

serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"
serde_with = "3.4.0"
Expand Down
6 changes: 4 additions & 2 deletions conjure_oxide/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub mod ast;
pub mod error;
pub mod find_conjure;
pub mod parse;
mod solvers;

pub use ast::Model;
pub use conjure_core::ast; // re-export core::ast as conjure_oxide::ast
pub use conjure_core::ast::Model; // rexport core::ast::Model as conjure_oxide::Model
pub use conjure_core::solvers::Solver;

pub use error::Error;
9 changes: 7 additions & 2 deletions conjure_oxide/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::PathBuf;
use anyhow::Result as AnyhowResult;
use clap::{arg, command, Parser};
use conjure_oxide::find_conjure::conjure_executable;
use conjure_oxide::parse::parse_json;
use conjure_oxide::parse::model_from_json;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -45,7 +45,12 @@ pub fn main() -> AnyhowResult<()> {

let astjson = String::from_utf8(output.stdout)?;

let model = parse_json(&astjson)?;
let model = model_from_json(&astjson)?;
println!("{:?}", model);

// for rule in get_rules_by_kind() {
// println!("Applying rule {:?}", rule);
// }

Ok(())
}
8 changes: 1 addition & 7 deletions conjure_oxide/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::ast::{DecisionVariable, Domain, Expression, Model, Name, Range};
use crate::error::{Error, Result};
use serde_json::Value as JsonValue;

pub fn parse_json(str: &str) -> Result<Model> {
pub fn model_from_json(str: &str) -> Result<Model> {
let mut m = Model::new();
let v: JsonValue = serde_json::from_str(str)?;
let statements = v["mStatements"]
Expand Down Expand Up @@ -217,9 +217,3 @@ fn parse_constant(constant: &serde_json::Map<String, Value>) -> Option<Expressio
otherwise => panic!("Unhandled parse_constant {:#?}", otherwise),
}
}

impl Model {
pub fn from_json(str: &str) -> Result<Model> {
parse_json(str)
}
}
3 changes: 1 addition & 2 deletions conjure_oxide/src/solvers/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::Solver;
use thiserror::Error;

use super::Solver;

#[derive(Error, Debug)]
pub enum SolverError {
#[error("not supported in solver `{0}`: `{1}`.")]
Expand Down
15 changes: 6 additions & 9 deletions conjure_oxide/src/solvers/minion.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Solver interface to minion_rs.

#[allow(unused_imports)]
use anyhow::anyhow;
use super::{FromConjureModel, SolverError};
use crate::Solver;

use crate::ast::{
DecisionVariable, Domain as ConjureDomain, Expression as ConjureExpression,
Expand All @@ -12,14 +12,10 @@ use minion_rs::ast::{
Var as MinionVar, VarDomain as MinionDomain,
};

use super::{Solver, SolverError};

const SOLVER: Solver = Solver::Minion;

impl TryFrom<ConjureModel> for MinionModel {
type Error = SolverError;

fn try_from(conjure_model: ConjureModel) -> Result<Self, Self::Error> {
impl FromConjureModel for MinionModel {
fn from_conjure(conjure_model: ConjureModel) -> Result<Self, SolverError> {
let mut minion_model = MinionModel::new();

// We assume (for now) that the conjure model is fully valid
Expand Down Expand Up @@ -211,6 +207,7 @@ fn name_to_string(name: ConjureName) -> String {

#[cfg(test)]
mod tests {
use anyhow::anyhow;
use std::collections::HashMap;

use minion_rs::ast::VarName;
Expand Down Expand Up @@ -248,7 +245,7 @@ mod tests {
model.constraints.push(leq);
model.constraints.push(ineq);

let minion_model = MinionModel::try_from(model)?;
let minion_model = MinionModel::from_conjure(model)?;
Ok(minion_rs::run_minion(minion_model, xyz_callback)?)
}

Expand Down
10 changes: 8 additions & 2 deletions conjure_oxide/src/solvers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
mod error;
pub mod minion;
pub use crate::ast::Model;
pub use error::*;
mod solver_list;
pub use solver_list::*;

pub trait FromConjureModel
where
Self: Sized,
{
fn from_conjure(model: Model) -> Result<Self, SolverError>;
}
3 changes: 2 additions & 1 deletion conjure_oxide/tests/generated_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use conjure_oxide::ast::Model;
use conjure_oxide::parse::model_from_json;
use serde_json::Value;
use std::env;
use std::error::Error;
Expand Down Expand Up @@ -37,7 +38,7 @@ fn integration_test(path: &str, essence_base: &str) -> Result<(), Box<dyn Error>
let astjson = String::from_utf8(output.stdout)?;

// "parsing" astjson as Model
let generated_mdl = Model::from_json(&astjson)?;
let generated_mdl = model_from_json(&astjson)?;

// a consistent sorting of the keys of json objects
// only required for the generated version
Expand Down
15 changes: 15 additions & 0 deletions crates/conjure_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "conjure_core"
version = "0.0.1"
edition = "2021"

[dependencies]
serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108"
serde_with = "3.4.0"
strum = "0.25.0"
strum_macros = "0.25.3"
thiserror = "1.0.50"

[lints]
workspace = true
4 changes: 4 additions & 0 deletions crates/conjure_core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Core datatypes for use in Conjure Oxide.

This crate is internal to conjure_oxide, and should not be treated as a stable
API. Relevant types are re-exported through conjure_oxide.
File renamed without changes.
4 changes: 4 additions & 0 deletions crates/conjure_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod ast;

pub mod solvers;
pub use solvers::Solver;
File renamed without changes.

0 comments on commit 8d8af4a

Please sign in to comment.