solverforge-cli 2.0.1

CLI for scaffolding and managing SolverForge constraint solver projects
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use super::{run_constraint, run_entity, run_fact, run_solution};
use crate::{commands::generate_constraint::remove_constraint_from_source, test_support};
use std::{
    fs,
    path::{Path, PathBuf},
};

struct CwdGuard {
    original_dir: PathBuf,
}

impl CwdGuard {
    fn enter(path: &Path) -> Self {
        let original_dir = std::env::current_dir().expect("failed to read current dir");
        std::env::set_current_dir(path).expect("failed to enter temp dir");
        Self { original_dir }
    }
}

impl Drop for CwdGuard {
    fn drop(&mut self) {
        std::env::set_current_dir(&self.original_dir).expect("failed to restore current dir");
    }
}

#[test]
fn removes_constraint_from_managed_tuple_entry() {
    let src = r#"pub use self::assemble::create_constraints;

// @solverforge:begin constraint-modules
mod all_assigned;
mod extra;
// @solverforge:end constraint-modules

mod assemble {
    use super::*;

    pub fn create_constraints() -> impl ConstraintSet<Plan, HardSoftScore> {
        // @solverforge:begin constraint-calls
        (
            all_assigned::constraint(),
            extra::constraint(),
        )
        // @solverforge:end constraint-calls
    }
}
"#;

    let updated =
        remove_constraint_from_source(src, "all_assigned").expect("source should be rewritten");
    assert!(!updated.contains("mod all_assigned;"));
    assert!(!updated.contains("all_assigned::constraint(),"));
    assert!(updated.contains("extra::constraint(),"));
}

#[test]
fn removes_last_constraint_to_empty_tuple() {
    let src = r#"pub use self::assemble::create_constraints;

// @solverforge:begin constraint-modules
mod extra;
// @solverforge:end constraint-modules

mod assemble {
    use super::*;

    pub fn create_constraints() -> impl ConstraintSet<Plan, HardSoftScore> {
        // @solverforge:begin constraint-calls
        (
            extra::constraint(),
        )
        // @solverforge:end constraint-calls
    }
}
"#;

    let updated = remove_constraint_from_source(src, "extra").expect("source should be rewritten");
    assert!(!updated.contains("mod extra;"));
    assert!(updated.contains("        ()"));
}

#[test]
fn destroy_solution_preflights_domain_mod_before_delete() {
    let _cwd_guard = test_support::lock_cwd();
    let tmp = tempfile::tempdir().expect("failed to create temp dir");
    let _dir_guard = CwdGuard::enter(tmp.path());
    write_minimal_domain_project(false);
    let plan_before = fs::read_to_string("src/domain/plan.rs").expect("failed to read plan");
    let mod_before = fs::read_to_string("src/domain/mod.rs").expect("failed to read domain mod");

    let err =
        run_solution(true).expect_err("invalid domain managed markers should fail before delete");

    assert!(
        err.to_string()
            .contains("src/domain/mod.rs must declare solverforge::planning_model! { ... }"),
        "unexpected error: {err}"
    );
    assert!(
        Path::new("src/domain/plan.rs").exists(),
        "failed preflight must not delete the solution file"
    );
    assert_eq!(
        fs::read_to_string("src/domain/plan.rs").expect("failed to read plan"),
        plan_before
    );
    assert_eq!(
        fs::read_to_string("src/domain/mod.rs").expect("failed to read domain mod"),
        mod_before
    );
}

#[test]
fn destroy_entity_preflights_domain_mod_before_delete() {
    let _cwd_guard = test_support::lock_cwd();
    let tmp = tempfile::tempdir().expect("failed to create temp dir");
    let _dir_guard = CwdGuard::enter(tmp.path());
    write_minimal_domain_project(false);
    let task_before = fs::read_to_string("src/domain/task.rs").expect("failed to read task");
    let plan_before = fs::read_to_string("src/domain/plan.rs").expect("failed to read plan");
    let mod_before = fs::read_to_string("src/domain/mod.rs").expect("failed to read domain mod");

    let err = run_entity("task", true)
        .expect_err("invalid domain managed markers should fail before delete");

    assert!(
        err.to_string()
            .contains("src/domain/mod.rs must declare solverforge::planning_model! { ... }"),
        "unexpected error: {err}"
    );
    assert!(
        Path::new("src/domain/task.rs").exists(),
        "failed preflight must not delete the entity file"
    );
    assert_eq!(
        fs::read_to_string("src/domain/task.rs").expect("failed to read task"),
        task_before
    );
    assert_eq!(
        fs::read_to_string("src/domain/plan.rs").expect("failed to read plan"),
        plan_before
    );
    assert_eq!(
        fs::read_to_string("src/domain/mod.rs").expect("failed to read domain mod"),
        mod_before
    );
}

#[test]
fn destroy_fact_preflights_domain_mod_before_delete() {
    let _cwd_guard = test_support::lock_cwd();
    let tmp = tempfile::tempdir().expect("failed to create temp dir");
    let _dir_guard = CwdGuard::enter(tmp.path());
    write_minimal_domain_project(false);
    let resource_before =
        fs::read_to_string("src/domain/resource.rs").expect("failed to read resource");
    let plan_before = fs::read_to_string("src/domain/plan.rs").expect("failed to read plan");
    let mod_before = fs::read_to_string("src/domain/mod.rs").expect("failed to read domain mod");

    let err = run_fact("resource", true)
        .expect_err("invalid domain managed markers should fail before delete");

    assert!(
        err.to_string()
            .contains("src/domain/mod.rs must declare solverforge::planning_model! { ... }"),
        "unexpected error: {err}"
    );
    assert!(
        Path::new("src/domain/resource.rs").exists(),
        "failed preflight must not delete the fact file"
    );
    assert_eq!(
        fs::read_to_string("src/domain/resource.rs").expect("failed to read resource"),
        resource_before
    );
    assert_eq!(
        fs::read_to_string("src/domain/plan.rs").expect("failed to read plan"),
        plan_before
    );
    assert_eq!(
        fs::read_to_string("src/domain/mod.rs").expect("failed to read domain mod"),
        mod_before
    );
}

#[test]
fn destroy_fact_rejects_still_referenced_fact_collection() {
    let _cwd_guard = test_support::lock_cwd();
    let tmp = tempfile::tempdir().expect("failed to create temp dir");
    let _dir_guard = CwdGuard::enter(tmp.path());
    write_minimal_domain_project(true);
    fs::write(
        "src/domain/task.rs",
        r#"use serde::{Deserialize, Serialize};
use solverforge::prelude::*;

#[planning_entity]
#[derive(Clone, Serialize, Deserialize)]
pub struct Task {
    #[planning_id]
    pub id: String,
    // @solverforge:begin entity-variables
    #[planning_variable(value_range = "resources", allows_unassigned = true)]
    pub resource_idx: Option<usize>,
    // @solverforge:end entity-variables
}

impl Task {
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            // @solverforge:begin entity-variable-init
            resource_idx: None,
            // @solverforge:end entity-variable-init
        }
    }
}
"#,
    )
    .expect("failed to rewrite task");
    let resource_before =
        fs::read_to_string("src/domain/resource.rs").expect("failed to read resource");
    let plan_before = fs::read_to_string("src/domain/plan.rs").expect("failed to read plan");
    let mod_before = fs::read_to_string("src/domain/mod.rs").expect("failed to read domain mod");

    let err = run_fact("resource", true)
        .expect_err("destroy fact should fail while variables still reference the collection");

    assert!(
        err.to_string().contains(
            "cannot destroy fact 'Resource' because managed planning variables still reference collection 'resources': Task.resource_idx (value_range = \"resources\")"
        ),
        "unexpected error: {err}"
    );
    assert!(
        Path::new("src/domain/resource.rs").exists(),
        "failed dependency check must not delete the fact file"
    );
    assert_eq!(
        fs::read_to_string("src/domain/resource.rs").expect("failed to read resource"),
        resource_before
    );
    assert_eq!(
        fs::read_to_string("src/domain/plan.rs").expect("failed to read plan"),
        plan_before
    );
    assert_eq!(
        fs::read_to_string("src/domain/mod.rs").expect("failed to read domain mod"),
        mod_before
    );
}

#[test]
fn destroy_entity_preflights_solution_rewrite_before_domain_update_or_delete() {
    let _cwd_guard = test_support::lock_cwd();
    let tmp = tempfile::tempdir().expect("failed to create temp dir");
    let _dir_guard = CwdGuard::enter(tmp.path());
    write_minimal_domain_project(true);
    let plan_without_collection_markers = fs::read_to_string("src/domain/plan.rs")
        .expect("failed to read plan")
        .replace("    // @solverforge:begin solution-collections\n", "")
        .replace("    // @solverforge:end solution-collections\n", "");
    fs::write("src/domain/plan.rs", &plan_without_collection_markers)
        .expect("failed to rewrite plan");
    let task_before = fs::read_to_string("src/domain/task.rs").expect("failed to read task");
    let mod_before = fs::read_to_string("src/domain/mod.rs").expect("failed to read domain mod");

    let err = run_entity("task", true)
        .expect_err("invalid solution managed markers should fail before mutation");

    assert!(
        err.to_string()
            .contains("missing or duplicated managed block markers for 'solution-collections'"),
        "unexpected error: {err}"
    );
    assert!(
        Path::new("src/domain/task.rs").exists(),
        "failed solution preflight must not delete the entity file"
    );
    assert_eq!(
        fs::read_to_string("src/domain/task.rs").expect("failed to read task"),
        task_before
    );
    assert_eq!(
        fs::read_to_string("src/domain/mod.rs").expect("failed to read domain mod"),
        mod_before
    );
    assert_eq!(
        fs::read_to_string("src/domain/plan.rs").expect("failed to read plan"),
        plan_without_collection_markers
    );
}

#[test]
fn destroy_constraint_preflights_constraint_mod_before_delete() {
    let _cwd_guard = test_support::lock_cwd();
    let tmp = tempfile::tempdir().expect("failed to create temp dir");
    let _dir_guard = CwdGuard::enter(tmp.path());
    fs::create_dir_all("src/constraints").expect("failed to create constraints dir");
    let invalid_mod = r#"pub use self::assemble::create_constraints;

mod all_assigned;

mod assemble {
    use super::*;

    pub fn create_constraints() -> impl ConstraintSet<Plan, HardSoftScore> {
        all_assigned::constraint()
    }
}
"#;
    fs::write("src/constraints/mod.rs", invalid_mod).expect("failed to write constraints mod");
    fs::write(
        "src/constraints/all_assigned.rs",
        "pub fn constraint() {}\n",
    )
    .expect("failed to write constraint file");

    let err = run_constraint("all_assigned", true)
        .expect_err("invalid constraint managed markers should fail before delete");

    assert!(
        err.to_string()
            .contains("missing or duplicated managed block markers for 'constraint-modules'"),
        "unexpected error: {err}"
    );
    assert!(
        Path::new("src/constraints/all_assigned.rs").exists(),
        "failed preflight must not delete the constraint file"
    );
    assert_eq!(
        fs::read_to_string("src/constraints/mod.rs").expect("failed to read constraints mod"),
        invalid_mod
    );
}

fn write_minimal_domain_project(managed_domain_mod: bool) {
    fs::create_dir_all("src/domain").expect("failed to create domain dir");
    let domain_mod = if managed_domain_mod {
        r#"solverforge::planning_model! {
    root = "src/domain";

    // @solverforge:begin domain-exports
mod resource;
mod task;
mod plan;

pub use resource::Resource;
pub use task::Task;
pub use plan::Plan;
// @solverforge:end domain-exports
}
"#
    } else {
        r#"mod resource;
mod task;
mod plan;

pub use resource::Resource;
pub use task::Task;
pub use plan::Plan;
"#
    };
    fs::write("src/domain/mod.rs", domain_mod).expect("failed to write domain mod");
    fs::write(
        "src/domain/resource.rs",
        r#"use serde::{Deserialize, Serialize};
use solverforge::prelude::*;

#[problem_fact]
#[derive(Clone, Serialize, Deserialize)]
pub struct Resource {
    #[planning_id]
    pub id: String,
}
"#,
    )
    .expect("failed to write resource");
    fs::write(
        "src/domain/task.rs",
        r#"use serde::{Deserialize, Serialize};
use solverforge::prelude::*;

#[planning_entity]
#[derive(Clone, Serialize, Deserialize)]
pub struct Task {
    #[planning_id]
    pub id: String,
}
"#,
    )
    .expect("failed to write task");
    fs::write(
        "src/domain/plan.rs",
        r#"use serde::{Deserialize, Serialize};
use solverforge::prelude::*;

// @solverforge:begin solution-imports
use super::Resource;
use super::Task;
// @solverforge:end solution-imports

#[planning_solution(
    constraints = "crate::constraints::create_constraints",
    solver_toml = "../../solver.toml"
)]
#[derive(Serialize, Deserialize)]
pub struct Plan {
    // @solverforge:begin solution-collections
    #[problem_fact_collection]
    pub resources: Vec<Resource>,
    #[planning_entity_collection]
    pub tasks: Vec<Task>,
    // @solverforge:end solution-collections
    #[planning_score]
    pub score: Option<HardSoftScore>,
}

impl Plan {
    pub fn new(
        // @solverforge:begin solution-constructor-params
        resources: Vec<Resource>,
        tasks: Vec<Task>,
        // @solverforge:end solution-constructor-params
    ) -> Self {
        Self {
            // @solverforge:begin solution-constructor-init
            resources,
            tasks,
            // @solverforge:end solution-constructor-init
            score: None,
        }
    }
}
"#,
    )
    .expect("failed to write plan");
}