Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
8 changes: 6 additions & 2 deletions crates/conjure-cp-cli/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::{path::PathBuf, sync::Arc};
use anyhow::anyhow;
use clap::ValueHint;

use conjure_cp_cli::utils::testing::serialize_model;
use conjure_cp_cli::utils::testing::{
serialize_domains, serialize_domains_json, serialize_model,
};

use crate::cli::{GlobalArgs, LOGGING_HELP_HEADING};
use crate::solve::{init_context, parse};
Expand All @@ -28,10 +30,12 @@ pub fn run_pretty_command(global_args: GlobalArgs, pretty_args: Args) -> anyhow:
// Running the correct method to acquire pretty string
let output = match pretty_args.output_format.as_str() {
"ast-json" => serialize_model(&model),
"expression-domains" => serialize_domains(&model),
"expression-domains-json" => serialize_domains_json(&model),
// "add_new_flag" => method(),
_ => {
return Err(anyhow!(
"Unknown output format {}; supports [ast-json]",
"Unknown output format {}; supports [ast-json, expression-domains, expression-domains-json]",
&pretty_args.output_format
));
}
Expand Down
28 changes: 27 additions & 1 deletion crates/conjure-cp-cli/src/utils/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::io::{BufRead, BufReader, Write};
use std::sync::{Arc, RwLock};
use uniplate::Uniplate;

use conjure_cp::ast::{AbstractLiteral, GroundDomain, Moo, SerdeModel};
use conjure_cp::ast::{AbstractLiteral, ExprInfo, Expression, GroundDomain, Moo, SerdeModel};
use conjure_cp::context::Context;
use serde_json::{Error as JsonError, Value as JsonValue};

Expand Down Expand Up @@ -115,6 +115,32 @@ pub fn serialize_model(model: &ConjureModel) -> Result<String, JsonError> {
serde_json::to_string_pretty(&sorted_json)
}

pub fn serialize_domains(model: &ConjureModel) -> Result<String, JsonError> {
let mut output = String::new();
for constraint in model.constraints() {
serialize_domains_expr(constraint, 0, &mut output);
}
Ok(output)
}

fn serialize_domains_expr(expr: &Expression, depth: usize, output: &mut String) {
let domain = expr
.domain_of()
.map(|domain| domain.to_string())
.unwrap_or_else(|| "<unknown>".to_owned());
output.push_str(&" ".repeat(depth));
output.push_str(&format!("{expr} :: {domain}\n"));

for child in expr.children() {
serialize_domains_expr(&child, depth + 1, output);
}
}

pub fn serialize_domains_json(model: &ConjureModel) -> Result<String, JsonError> {
let exprs: Vec<ExprInfo> = model.constraints().iter().map(ExprInfo::create).collect();
serde_json::to_string_pretty(&exprs)
}

pub fn save_model_json(
model: &ConjureModel,
path: &str,
Expand Down
24 changes: 24 additions & 0 deletions crates/conjure-cp-core/src/ast/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::{Debug, Display};
use std::hash::Hash;
use std::sync::{Arc, RwLock};

use crate::ast::Domain;
use crate::context::Context;
use crate::{bug, into_matrix_expr};
use derivative::Derivative;
Expand Down Expand Up @@ -549,3 +550,26 @@ impl SerdeModel {
model.collect_stable_id_mapping()
}
}

/// A struct for the information about expressions
#[serde_as]
#[derive(Serialize)]
pub struct ExprInfo {
pretty: String,
domain: Option<Moo<Domain>>,
children: Vec<ExprInfo>,
}

impl ExprInfo {
pub fn create(expr: &Expression) -> ExprInfo {
let pretty = expr.to_string();
let domain = expr.domain_of();
let children = expr.children().iter().map(Self::create).collect();

ExprInfo {
pretty,
domain,
children,
}
}
}
3 changes: 3 additions & 0 deletions tests-integration/tests/custom/pretty-02/model.essence
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
find x,y,z : int(1..3)
find a,b,c : int(0..4)
such that (x+y < z-2) -> a + b > c
1 change: 1 addition & 0 deletions tests-integration/tests/custom/pretty-02/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
conjure-oxide pretty model.essence --output-format=expression-domains
15 changes: 15 additions & 0 deletions tests-integration/tests/custom/pretty-02/stdout.expected
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do you like this? I kept yours as expression-domains-json but I am tempted to keep just this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be clear indentation indicates nesting

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and :: is meant to mean domain-of. like how : is type-of.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
((sum([x,y;int(1..2)]) < (z - 2))) -> ((sum([a,b;int(1..2)]) > c)) :: bool
(sum([x,y;int(1..2)]) < (z - 2)) :: bool
sum([x,y;int(1..2)]) :: int(2..6)
[x,y;int(1..2)] :: matrix indexed by [int(1..2)] of int(1..3)
x :: int(1..3)
y :: int(1..3)
(z - 2) :: int(-1..1)
z :: int(1..3)
2 :: int(2)
(sum([a,b;int(1..2)]) > c) :: bool
sum([a,b;int(1..2)]) :: int(0..8)
[a,b;int(1..2)] :: matrix indexed by [int(1..2)] of int(0..4)
a :: int(0..4)
b :: int(0..4)
c :: int(0..4)
3 changes: 3 additions & 0 deletions tests-integration/tests/custom/pretty-03/model.essence
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
find x,y,z : int(1..3)
find a,b,c : int(0..4)
such that (x+y < z-2) -> a + b > c
1 change: 1 addition & 0 deletions tests-integration/tests/custom/pretty-03/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
conjure-oxide pretty model.essence --output-format=expression-domains-json
252 changes: 252 additions & 0 deletions tests-integration/tests/custom/pretty-03/stdout.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
[
{
"pretty": "((sum([x,y;int(1..2)]) < (z - 2))) -> ((sum([a,b;int(1..2)]) > c))",
"domain": {
"Ground": "Bool"
},
"children": [
{
"pretty": "(sum([x,y;int(1..2)]) < (z - 2))",
"domain": {
"Ground": "Bool"
},
"children": [
{
"pretty": "sum([x,y;int(1..2)])",
"domain": {
"Ground": {
"Int": [
{
"Bounded": [
2,
6
]
}
]
}
},
"children": [
{
"pretty": "[x,y;int(1..2)]",
"domain": {
"Ground": {
"Matrix": [
{
"Int": [
{
"Bounded": [
1,
3
]
}
]
},
[
{
"Int": [
{
"Bounded": [
1,
2
]
}
]
}
]
]
}
},
"children": [
{
"pretty": "x",
"domain": {
"Ground": {
"Int": [
{
"Bounded": [
1,
3
]
}
]
}
},
"children": []
},
{
"pretty": "y",
"domain": {
"Ground": {
"Int": [
{
"Bounded": [
1,
3
]
}
]
}
},
"children": []
}
]
}
]
},
{
"pretty": "(z - 2)",
"domain": {
"Ground": {
"Int": [
{
"Bounded": [
-1,
1
]
}
]
}
},
"children": [
{
"pretty": "z",
"domain": {
"Ground": {
"Int": [
{
"Bounded": [
1,
3
]
}
]
}
},
"children": []
},
{
"pretty": "2",
"domain": {
"Ground": {
"Int": [
{
"Single": 2
}
]
}
},
"children": []
}
]
}
]
},
{
"pretty": "(sum([a,b;int(1..2)]) > c)",
"domain": {
"Ground": "Bool"
},
"children": [
{
"pretty": "sum([a,b;int(1..2)])",
"domain": {
"Ground": {
"Int": [
{
"Bounded": [
0,
8
]
}
]
}
},
"children": [
{
"pretty": "[a,b;int(1..2)]",
"domain": {
"Ground": {
"Matrix": [
{
"Int": [
{
"Bounded": [
0,
4
]
}
]
},
[
{
"Int": [
{
"Bounded": [
1,
2
]
}
]
}
]
]
}
},
"children": [
{
"pretty": "a",
"domain": {
"Ground": {
"Int": [
{
"Bounded": [
0,
4
]
}
]
}
},
"children": []
},
{
"pretty": "b",
"domain": {
"Ground": {
"Int": [
{
"Bounded": [
0,
4
]
}
]
}
},
"children": []
}
]
}
]
},
{
"pretty": "c",
"domain": {
"Ground": {
"Int": [
{
"Bounded": [
0,
4
]
}
]
}
},
"children": []
}
]
}
]
}
]
Loading