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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
//! The planner module implements a basic system for backtracking planner.
//!
//! It supports features like:
//!
//! * specifying a goal with constraints that must be satisfied by the resultant plan.
//!
//! * the ability to solve a conjunction of goal
//!
//! * rendering of a plan in graphviz format for easier visualization
//!

use constraints::{Constraint, ConstraintValue};
use core::{Operation, Bindings, Unify};
use itertools::FoldWhile::{Continue, Done};
use itertools::Itertools;
use std;
use std::collections::HashSet;
use std::marker::PhantomData;
use utils;

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum UnificationIndex {
    #[serde(rename="init")]
    Init,
    #[serde(rename="actor")]
    Actor(usize),
    #[serde(rename="datum")]
    Datum(usize),
    #[serde(rename="exhausted")]
    Exhausted,
}

impl UnificationIndex {
    pub fn datum_idx(&self) -> Option<usize> {
        match *self {
            UnificationIndex::Datum(datum_idx) => Some(datum_idx),
            _ => None,
        }
    }

    pub fn actor_idx(&self) -> Option<usize> {
        match *self {
            UnificationIndex::Actor(actor_idx) => Some(actor_idx),
            _ => None,
        }
    }

    pub fn is_exhausted(&self) -> bool {
        match *self {
            UnificationIndex::Exhausted => true,
            _ => false,
        }
    }

    pub fn is_init(&self) -> bool {
        match *self {
            UnificationIndex::Init => true,
            _ => false,
        }
    }
}

impl std::fmt::Display for UnificationIndex {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            UnificationIndex::Init => write!(f, "Init"),
            UnificationIndex::Actor(idx) => write!(f, "Actor({})", idx),
            UnificationIndex::Datum(idx) => write!(f, "Datum({})", idx),
            UnificationIndex::Exhausted => write!(f, "Exhausted"),
        }
    }
}

impl Default for UnificationIndex {
    fn default() -> Self {
        UnificationIndex::Init
    }
}

fn increment_unification_index(current_unification_index: &UnificationIndex, datum_count: usize, rule_count: usize) -> UnificationIndex {
    let initial_rule_index = if rule_count > 0 {
        UnificationIndex::Actor(0)
    } else {
        UnificationIndex::Exhausted
    };

    let initial_datum_index = if datum_count > 0 {
        UnificationIndex::Datum(0)
    } else {
        initial_rule_index.clone()
    };

    match *current_unification_index {
        UnificationIndex::Exhausted => UnificationIndex::Exhausted,
        UnificationIndex::Init => initial_datum_index.clone(),
        UnificationIndex::Datum(current_idx) => {
            if current_idx + 1 < datum_count {
                UnificationIndex::Datum(current_idx + 1)
            } else {
                initial_rule_index
            }
        }
        UnificationIndex::Actor(current_idx) => {
            if current_idx + 1 < rule_count {
                UnificationIndex::Actor(current_idx + 1)
            } else {
                UnificationIndex::Exhausted
            }
        }
    }
}

/// Determine the first goal to increment
pub fn first_goal_to_increment(unification_indices: &Vec<UnificationIndex>) -> Option<usize> {
    if unification_indices.is_empty() {
        None
    } else {
        // Check subgoals find index that is in the Init state
        let last_index = unification_indices.len() - 1;
        let mut idx = 0;

        loop {
            if unification_indices[idx] == UnificationIndex::Init {
                return Some(idx);
            }
            if unification_indices[idx] == UnificationIndex::Exhausted {
                if idx == 0 {
                    return None;
                } else {
                    return Some(idx - 1);
                }
            }
            if idx == last_index {
                return Some(idx);
            }
            idx += 1;
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Goal<T: ConstraintValue, U: Unify<T>, A: Operation<T, U>> {
    #[serde(default)]
    pub bindings_at_creation: Bindings<T>,
    #[serde(default="Vec::new")]
    pub constraints: Vec<Constraint>,
    #[serde(default="Vec::new")]
    pub parental_constraints: Vec<Constraint>,
    pub pattern: U,
    #[serde(default="Vec::new")]
    pub subgoals: Vec<Goal<T, U, A>>,
    #[serde(default)]
    pub unification_index: UnificationIndex,
    #[serde(default)]
    _a_marker: PhantomData<A>,
    #[serde(default)]
    _t_marker: PhantomData<T>,
}

impl<T, U, A> Goal<T, U, A>
    where T: ConstraintValue,
          U: Unify<T>,
          A: Operation<T, U>
{
    pub fn new(pattern: U,
               parental_constraints: Vec<Constraint>,
               constraints: Vec<Constraint>,
               bindings_at_creation: Bindings<T>,
               unification_index: UnificationIndex,
               subgoals: Vec<Self>)
               -> Self {
        Goal {
            bindings_at_creation: bindings_at_creation,
            constraints: constraints,
            pattern: pattern,
            parental_constraints: parental_constraints,
            subgoals: subgoals,
            unification_index: unification_index,
            _a_marker: PhantomData,
            _t_marker: PhantomData,
        }
    }

    pub fn constraints(&self) -> Vec<&Constraint> {
        self.parental_constraints.iter().chain(self.constraints.iter()).collect()
    }

    pub fn with_pattern(pattern: U) -> Self {
        Goal::new(pattern,
                  Vec::new(),
                  Vec::new(),
                  Bindings::new(),
                  UnificationIndex::Init,
                  Vec::new())
    }

    pub fn solve(goal: &Self, data: &Vec<&U>, rules: &Vec<&A>, increments: usize, config: &PlanningConfig) -> Option<(usize, Self, Bindings<T>)> {
        if increments < config.max_increments {
            goal.increment(&data, &rules, increments, config.max_depth).and_then(|goal| match goal.validate(data, rules, &Bindings::new(), config) {
                Ok(bindings) => Some((increments, goal.clone(), bindings.clone())),
                Err(_) => Goal::solve(&goal, data, rules, increments + 1, config),
            })
        } else {
            None
        }
    }

    pub fn solve_conjunction(goals: Vec<&Self>,
                             data: &Vec<&U>,
                             rules: &Vec<&A>,
                             increments: usize,
                             config: &PlanningConfig)
                             -> Option<(Vec<Self>, Bindings<T>)> {
        if increments < config.max_increments {
            Goal::increment_conjunction(goals, data, rules, increments, config.max_depth).and_then(|goals| {
                let validated_bindings = utils::fold_while_some(Bindings::new(),
                                                                &mut goals.iter(),
                                                                &|bindings, goal| goal.validate(data, rules, &bindings, config).ok());
                match validated_bindings {
                    Some(bindings) => Some((goals, bindings)),
                    None => Goal::solve_conjunction(goals.iter().collect(), data, rules, increments + 1, config),
                }
            })
        } else {
            None
        }
    }

    pub fn solve_conjunction_with_criteria<X>(goals: Vec<&Self>,
                                              data: &Vec<&U>,
                                              rules: &Vec<&A>,
                                              increments: usize,
                                              config: &PlanningConfig,
                                              criteria: &Fn(&Vec<Self>, &Bindings<T>) -> Option<X>)
                                              -> Option<(Vec<Self>, Bindings<T>, X)> {
        if increments < config.max_increments {
            Goal::solve_conjunction(goals, data, rules, increments, config).and_then(|(goals, bindings)| match criteria(&goals, &bindings) {
                Some(result) => Some((goals, bindings, result)),
                None => {
                    Goal::solve_conjunction_with_criteria(goals.iter().collect(),
                                                          data,
                                                          rules,
                                                          increments,
                                                          config,
                                                          criteria)
                }
            })
        } else {
            None
        }
    }

    /// Verify that this plan does not break any of the planning specifications and that it is consistent
    pub fn validate(&self, data: &Vec<&U>, rules: &Vec<&A>, bindings: &Bindings<T>, config: &PlanningConfig) -> Result<Bindings<T>, InvalidPlan> {
        config.validate_plan(self).and_then(|_| match self.satisified(&data, &rules, bindings) {
            Some(bindings) => Ok(bindings),
            None => Err(InvalidPlan::BindingsConflict),
        })
    }

    /// Construct a mutated plan
    pub fn increment(&self, data: &Vec<&U>, rules: &Vec<&A>, snowflake_prefix_id: usize, max_depth: usize) -> Option<Self> {
        if max_depth == 0 {
            return None;
        }
        let mut goal = self.clone();

        // If there are any subgoals, increment them first
        if !self.subgoals.is_empty() {
            if let Some(subgoals) = Goal::increment_conjunction(self.subgoals.iter().collect(),
                                                                data,
                                                                rules,
                                                                snowflake_prefix_id,
                                                                max_depth) {
                goal.subgoals = subgoals;
                return Some(goal);
            }
        }

        // If subgoals cannot be incremented, increment this goal
        loop {
            goal.unification_index = increment_unification_index(&goal.unification_index, data.len(), rules.len());

            // Attempt to increment this goal
            match goal.unification_index {
                UnificationIndex::Datum(_idx) => {
                    if goal.satisified(data, rules, &goal.bindings_at_creation).is_some() {
                        return Some(goal);
                    }
                }
                UnificationIndex::Actor(idx) => {
                    let rule = rules[idx].snowflake(format!("{}", snowflake_prefix_id));

                    if let Some(subgoals) = Self::create_subgoals(&self.pattern,
                                                                  &rule,
                                                                  &goal.constraints(),
                                                                  data,
                                                                  rules,
                                                                  snowflake_prefix_id,
                                                                  max_depth) {
                        goal.subgoals = subgoals;
                        return Some(goal);
                    }
                }
                // If this goal cannot be incremented, return None
                UnificationIndex::Exhausted => return None,
                UnificationIndex::Init => panic!("Init after incrementing; this should never happen"),
            }
        }
    }

    /// Determine if the plan is valid
    pub fn satisified(&self, data: &Vec<&U>, rules: &Vec<&A>, bindings: &Bindings<T>) -> Option<Bindings<T>> {
        let bindings = bindings.merge(&self.bindings_at_creation);
        match self.unification_index {
            UnificationIndex::Datum(datum_idx) => {
                self.pattern
                    .unify(data[datum_idx], &bindings)
                    .and_then(|bindings| Constraint::solve_many(self.constraints(), &bindings).ok())
            }
            UnificationIndex::Actor(_actor_idx) => {
                self.subgoals
                    .iter()
                    .fold_while(Some(bindings),
                                |bindings, subgoal| match subgoal.satisified(data, rules, bindings.as_ref().unwrap()) {
                                    Some(subgoal_bindings) => Continue(Some(subgoal_bindings.clone())),
                                    None => Done(None),
                                })
            }
            UnificationIndex::Init => self.pattern.unify(&U::nil(), &bindings),
            UnificationIndex::Exhausted => None,
        }
    }

    pub fn create_subgoals(r_pattern: &U,
                           rule: &A,
                           parent_constraints: &Vec<&Constraint>,
                           data: &Vec<&U>,
                           rules: &Vec<&A>,
                           snowflake_prefix_id: usize,
                           max_depth: usize)
                           -> Option<Vec<Self>> {
        rule.r_apply_match(r_pattern).and_then(|(subgoal_patterns, bindings)| {
            let subgoals: Vec<Option<Self>> = subgoal_patterns.into_iter()
                .map(|pattern| {
                    Goal::new(pattern.apply_bindings(&bindings).unwrap(),
                              parent_constraints.iter().map(|c| (*c).clone()).collect(),
                              rule.constraints().iter().map(|c| (*c).clone()).collect(),
                              bindings.clone(),
                              UnificationIndex::default(),
                              Vec::new())
                        .increment(data, rules, snowflake_prefix_id + 1, max_depth - 1)
                })
                .collect();

            if subgoals.iter().any(|x| x.is_none()) {
                None
            } else {
                Some(subgoals.into_iter().map(|sg| sg.unwrap()).collect())
            }
        })
    }

    pub fn increment_conjunction(goals: Vec<&Self>,
                                 data: &Vec<&U>,
                                 rules: &Vec<&A>,
                                 snowflake_prefix_id: usize,
                                 max_depth: usize)
                                 -> Option<Vec<Self>> {
        let mut goals: Vec<Self> = goals.into_iter().map(|g| g.clone()).collect();
        let goal_count = goals.len();
        let is_last_goal = |idx| idx + 1 == goal_count;
        loop {
            let unification_indices = goals.iter().map(|sg| sg.unification_index.clone()).collect();
            let goal_idx_to_increment = first_goal_to_increment(&unification_indices);
            match goal_idx_to_increment {
                None => return None,
                Some(idx) => {
                    if let Some(new_goal) = goals[idx].increment(data, rules, snowflake_prefix_id, max_depth - 1) {
                        goals[idx] = new_goal;

                        if is_last_goal(idx) {
                            return Some(goals);
                        } else {
                            goals[idx + 1].unification_index = UnificationIndex::Init;
                        }
                    } else {
                        goals[idx].unification_index = UnificationIndex::Exhausted;
                    }
                }
            }
        }
    }

    pub fn render_as_graphviz(&self) -> String {
        let subtree_string = self.render_subtree_as_graphviz(None);
        format!("graph \"goal tree {}\" {{\n{}\n}}",
                self.pattern,
                subtree_string)
    }

    fn render_subtree_as_graphviz(&self, parent: Option<String>) -> String {
        let goal_rendering = format!("{} [{}]", self.pattern, self.unification_index);
        let subtree_string_vec: Vec<String> = self.subgoals
            .iter()
            .map(|subgoal| subgoal.render_subtree_as_graphviz(Some(goal_rendering.clone())))
            .collect();
        let subtree_string = subtree_string_vec.join("\n");
        let goal_parent_str = if let Some(parent_goal) = parent {
            format!("\"{}\" -- \"{}\";", parent_goal, goal_rendering)
        } else {
            String::new()
        };
        format!("{}\n{}", goal_parent_str, subtree_string)
    }

    pub fn pprint(&self, ntabs: usize, only_render_spine: bool) -> String {
        fn concat_tabs(ntabs: usize) -> String {
            let tabv: Vec<String> = (0..ntabs).map(|_| "\t".to_string()).collect();
            tabv.join("")
        }
        let tabs = concat_tabs(ntabs);
        let subgoal_v: Vec<String> = self.subgoals.iter().map(|sg| sg.pprint(ntabs + 1, only_render_spine)).collect();
        let subgoal_s = subgoal_v.join("\n");

        let parental_constraint_v: Vec<String> = self.parental_constraints.iter().map(|c| format!("{}{}", concat_tabs(ntabs + 1), c)).collect();
        let parental_constraint_s = parental_constraint_v.join("\n");

        let constraint_v: Vec<String> = self.constraints.iter().map(|c| format!("{}{}", concat_tabs(ntabs + 1), c)).collect();
        let constraint_s = constraint_v.join("\n");

        if only_render_spine {
            format!("{}{} @ {}\n{}",
                    tabs,
                    self.pattern,
                    self.unification_index,
                    subgoal_s)
        } else {
            format!("{}{} @ {}\n\t{}bindings at creation: {}\n\t{}parental constraints:\n{}\n\t{}constraints:\n{}\n{}",
                    tabs,
                    self.pattern,
                    self.unification_index,
                    tabs,
                    self.bindings_at_creation,
                    tabs,
                    parental_constraint_s,
                    tabs,
                    constraint_s,
                    subgoal_s)
        }
    }

    pub fn apply_bindings(&self, bindings: &Bindings<T>) -> Option<Self> {
        self.pattern.apply_bindings(&bindings).and_then(|pattern| {
            let mut clone = Goal::new(pattern,
                                      self.parental_constraints.clone(),
                                      self.constraints.clone(),
                                      self.bindings_at_creation.clone(),
                                      self.unification_index.clone(),
                                      Vec::with_capacity(self.subgoals.len()));
            for subgoal in self.subgoals.iter() {
                if let Some(applied_subgoal) = subgoal.apply_bindings(&bindings) {
                    clone.subgoals.push(applied_subgoal);
                } else {
                    return None;
                }
            }
            Some(clone)
        })
    }

    /// Traverse the tree and determine if any datum is being used more than once
    pub fn find_reused_datum(&self, used_data: &mut HashSet<usize>) -> Option<usize> {
        match self.unification_index {
            UnificationIndex::Datum(ref datum_idx) => {
                if used_data.contains(datum_idx) {
                    return Some(datum_idx.clone());
                } else {
                    used_data.insert(datum_idx.clone());
                    return None;
                }
            }
            UnificationIndex::Actor(_actor_idx) => {
                for subgoal in self.subgoals.iter() {
                    if let Some(idx) = subgoal.find_reused_datum(used_data) {
                        return Some(idx);
                    }
                }
                None
            }
            _ => None,
        }
    }

    /// Traverse the goal tree using a depth-first search and gather the leaves of the plan
    pub fn gather_leaves(&self, bindings: &Bindings<T>) -> Vec<U> {
        let mut leaves = Vec::new();

        if self.subgoals.is_empty() {
            leaves.push(self.pattern.apply_bindings(&bindings).expect("Bindings should be applicable"));
        } else {
            for sg in self.subgoals.iter() {
                leaves.extend(sg.gather_leaves(bindings).into_iter());
            }
        }

        leaves
    }
}

impl<T, U, A> std::fmt::Display for Goal<T, U, A>
    where T: ConstraintValue,
          U: Unify<T>,
          A: Operation<T, U>
{
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        write!(f, "Goal tree:\n{}", self.pprint(1, true))
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PlanningConfig {
    pub max_depth: usize,
    pub max_increments: usize,
    pub reuse_data: bool,
}

impl Default for PlanningConfig {
    fn default() -> Self {
        PlanningConfig {
            max_depth: 3,
            max_increments: 100,
            reuse_data: true,
        }
    }
}

impl PlanningConfig {
    /// Verify that nothing in plan contradicts specifications set in the PlanningConfig
    pub fn validate_plan<T, U, A>(&self, goal: &Goal<T, U, A>) -> Result<(), InvalidPlan>
        where T: ConstraintValue,
              U: Unify<T>,
              A: Operation<T, U>
    {
        if !self.reuse_data {
            if let Some(idx) = goal.find_reused_datum(&mut HashSet::new()) {
                return Err(InvalidPlan::ReusedData { idx: idx });
            }
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InvalidPlan {
    BindingsConflict,
    ReusedData { idx: usize },
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Planner<'a, T, U, A>
    where T: ConstraintValue,
          U: 'a + Unify<T>,
          A: 'a + Operation<T, U>
{
    bindings: Bindings<T>,
    config: PlanningConfig,
    data: Vec<&'a U>,
    goal: Goal<T, U, A>,
    total_increments: usize,
    rules: Vec<&'a A>,
}

impl<'a, T, U, A> Planner<'a, T, U, A>
    where T: ConstraintValue,
          U: 'a + Unify<T>,
          A: 'a + Operation<T, U>
{
    pub fn new(goal: &Goal<T, U, A>, bindings: &Bindings<T>, config: &PlanningConfig, data: Vec<&'a U>, rules: Vec<&'a A>) -> Self {
        Planner {
            bindings: bindings.clone(),
            config: config.clone(),
            data: data,
            goal: goal.clone(),
            total_increments: 0,
            rules: rules,
        }
    }
}

impl<'a, T, U, A> Iterator for Planner<'a, T, U, A>
    where T: ConstraintValue,
          U: 'a + Unify<T>,
          A: 'a + Operation<T, U>
{
    type Item = (Goal<T, U, A>, Bindings<T>);

    fn next(&mut self) -> Option<(Goal<T, U, A>, Bindings<T>)> {
        for _i in self.total_increments..self.config.max_increments {
            self.total_increments += 1;

            if let Some((increments, goal, bindings)) =
                Goal::solve(&self.goal,
                            &self.data,
                            &self.rules,
                            self.total_increments,
                            &self.config) {
                self.goal = goal;
                self.total_increments += increments;
                return Some((self.goal.clone(), bindings.clone()));
            } else {
                break;
            }
        }
        None
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ConjunctivePlanner<'a, T, U, A>
    where T: ConstraintValue,
          U: 'a + Unify<T>,
          A: 'a + Operation<T, U>
{
    bindings: Bindings<T>,
    config: PlanningConfig,
    data: Vec<&'a U>,
    goals: Vec<Goal<T, U, A>>,
    total_increments: usize,
    rules: Vec<&'a A>,
}

impl<'a, T, U, A> ConjunctivePlanner<'a, T, U, A>
    where T: ConstraintValue,
          U: 'a + Unify<T>,
          A: 'a + Operation<T, U>
{
    pub fn new(goals: Vec<Goal<T, U, A>>, bindings: &Bindings<T>, config: &PlanningConfig, data: Vec<&'a U>, rules: Vec<&'a A>) -> Self {
        ConjunctivePlanner {
            bindings: bindings.clone(),
            config: config.clone(),
            data: data,
            goals: goals,
            total_increments: 0,
            rules: rules,
        }
    }
}

impl<'a, T, U, A> Iterator for ConjunctivePlanner<'a, T, U, A>
    where T: ConstraintValue,
          U: 'a + Unify<T>,
          A: 'a + Operation<T, U>
{
    type Item = (Vec<Goal<T, U, A>>, Bindings<T>);

    fn next(&mut self) -> Option<(Vec<Goal<T, U, A>>, Bindings<T>)> {
        for _i in self.total_increments..self.config.max_increments {
            self.total_increments += 1;

            if let Some((goals, bindings)) =
                Goal::solve_conjunction(self.goals.iter().collect(),
                                        &self.data,
                                        &self.rules,
                                        self.total_increments,
                                        &self.config) {
                self.goals = goals;
                return Some((self.goals.clone(), bindings.clone()));
            } else {
                break;
            }
        }
        None
    }
}

#[cfg(test)]
mod tests;