forked from openSUSE/libsolv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.c
4900 lines (4541 loc) · 136 KB
/
solver.c
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2007-2008, Novell Inc.
*
* This program is licensed under the BSD license, read LICENSE.BSD
* for further information
*/
/*
* solver.c
*
* SAT based dependency solver
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include "solver.h"
#include "solver_private.h"
#include "bitmap.h"
#include "pool.h"
#include "util.h"
#include "policy.h"
#include "poolarch.h"
#include "solverdebug.h"
#include "cplxdeps.h"
#include "linkedpkg.h"
#define RULES_BLOCK 63
/************************************************************************/
/*
* enable/disable learnt rules
*
* we have enabled or disabled some of our rules. We now reenable all
* of our learnt rules except the ones that were learnt from rules that
* are now disabled.
*/
static void
enabledisablelearntrules(Solver *solv)
{
Pool *pool = solv->pool;
Rule *r;
Id why, *whyp;
int i;
POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
{
whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
while ((why = *whyp++) != 0)
{
assert(why > 0 && why < i);
if (solv->rules[why].d < 0)
break;
}
/* why != 0: we found a disabled rule, disable the learnt rule */
if (why && r->d >= 0)
{
IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
{
POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "disabling ");
solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
}
solver_disablerule(solv, r);
}
else if (!why && r->d < 0)
{
IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
{
POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "re-enabling ");
solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
}
solver_enablerule(solv, r);
}
}
}
/*
* make assertion rules into decisions
*
* Go through rules and add direct assertions to the decisionqueue.
* If we find a conflict, disable rules and add them to problem queue.
*/
static int
makeruledecisions(Solver *solv, int disablerules)
{
Pool *pool = solv->pool;
int i, ri, ii, ori;
Rule *r, *rr;
Id v, vv;
int decisionstart;
int record_proof = 1;
int oldproblemcount;
int havedisabled = 0;
int doautouninstall;
/* The system solvable is always installed first */
assert(solv->decisionq.count == 0);
queue_push(&solv->decisionq, SYSTEMSOLVABLE);
queue_push(&solv->decisionq_why, 0);
queue_push2(&solv->decisionq_reason, 0, 0);
solv->decisionmap[SYSTEMSOLVABLE] = 1; /* installed at level '1' */
decisionstart = solv->decisionq.count;
for (;;)
{
/* if we needed to re-run, back up decisions to decisionstart */
while (solv->decisionq.count > decisionstart)
{
v = solv->decisionq.elements[--solv->decisionq.count];
--solv->decisionq_why.count;
vv = v > 0 ? v : -v;
solv->decisionmap[vv] = 0;
}
/* note that the ruleassertions queue is ordered */
for (ii = 0; ii < solv->ruleassertions.count; ii++)
{
ri = solv->ruleassertions.elements[ii];
r = solv->rules + ri;
if (havedisabled && ri >= solv->learntrules)
{
/* just started with learnt rule assertions. If we have disabled
* some rules, adapt the learnt rule status */
enabledisablelearntrules(solv);
havedisabled = 0;
}
if (r->d < 0 || !r->p || r->w2) /* disabled, dummy or no assertion */
continue;
/* do weak rules in phase 2 */
if (ri < solv->learntrules && solv->weakrulemap.size && MAPTST(&solv->weakrulemap, ri))
continue;
v = r->p;
vv = v > 0 ? v : -v;
if (!solv->decisionmap[vv]) /* if not yet decided */
{
queue_push(&solv->decisionq, v);
queue_push(&solv->decisionq_why, ri);
solv->decisionmap[vv] = v > 0 ? 1 : -1;
IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
{
Solvable *s = pool->solvables + vv;
if (v < 0)
POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", pool_solvable2str(pool, s));
else
POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing %s (assertion)\n", pool_solvable2str(pool, s));
}
continue;
}
/* check against previous decision: is there a conflict? */
if (v > 0 && solv->decisionmap[vv] > 0) /* ok to install */
continue;
if (v < 0 && solv->decisionmap[vv] < 0) /* ok to remove */
continue;
/*
* found a conflict!
*
* The rule (r) we're currently processing says something
* different (v = r->p) than a previous decision (decisionmap[abs(v)])
* on this literal
*/
if (ri >= solv->learntrules)
{
/* conflict with a learnt rule */
/* can happen when packages cannot be installed for multiple reasons. */
/* we disable the learnt rule in this case */
/* (XXX: we should really do something like analyze_unsolvable_rule here!) */
solver_disablerule(solv, r);
continue;
}
POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ASSERTION ----------------------\n");
assert(ri >= solv->pkgrules_end); /* must not have a conflict in the pkg rules! */
/*
* find the decision which is the "opposite" of the rule
*/
for (i = 0; i < solv->decisionq.count; i++)
if (solv->decisionq.elements[i] == -v)
break;
assert(i < solv->decisionq.count); /* assert that we found it */
if (v == -SYSTEMSOLVABLE)
ori = 0;
else
{
ori = solv->decisionq_why.elements[i]; /* the conflicting rule */
assert(ori > 0);
}
/*
* record the problem
*/
doautouninstall = 0;
oldproblemcount = solv->problems.count;
queue_push(&solv->problems, 0); /* start problem */
if (ori < solv->pkgrules_end)
{
/* easy: conflict with system solvable or pkg rule */
assert(v > 0 || v == -SYSTEMSOLVABLE);
IF_POOLDEBUG (SOLV_DEBUG_UNSOLVABLE)
{
if (ori)
POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with pkg rule, disabling rule #%d\n", ri);
else
POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + ri);
if (ori)
solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + ori);
}
solver_recordproblem(solv, ri);
if (ri >= solv->featurerules && ri < solv->updaterules_end)
doautouninstall = 1;
}
else
{
POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflicting update/job assertions over literal %d\n", vv);
/*
* push all of our rules (can only be feature or job rules)
* asserting this literal on the problem stack
*/
for (i = solv->pkgrules_end, rr = solv->rules + i; i < solv->learntrules; i++, rr++)
{
if (rr->d < 0 /* disabled */
|| rr->w2) /* or no assertion */
continue;
if (rr->p != vv /* not affecting the literal */
&& rr->p != -vv)
continue;
if (solv->weakrulemap.size && MAPTST(&solv->weakrulemap, i)) /* weak: silently ignore */
continue;
POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + i);
solver_recordproblem(solv, i);
if (i >= solv->featurerules && i < solv->updaterules_end)
doautouninstall = 1;
}
}
queue_push(&solv->problems, 0); /* finish problem */
/* try autouninstall if requested */
if (doautouninstall)
{
if (solv->allowuninstall || solv->allowuninstall_all || solv->allowuninstallmap.size)
if (solver_autouninstall(solv, oldproblemcount) != 0)
{
solv->problems.count = oldproblemcount;
havedisabled = 1;
break; /* start over */
}
}
/* record the proof if requested */
if (record_proof)
{
solv->problems.elements[oldproblemcount] = solv->learnt_pool.count;
queue_push(&solv->learnt_pool, ri);
if (ori)
queue_push(&solv->learnt_pool, ori);
queue_push(&solv->learnt_pool, 0);
}
if (!disablerules)
{
POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
return -1;
}
/* disable all problem rules */
solver_disableproblemset(solv, oldproblemcount);
havedisabled = 1;
break; /* start over */
}
if (ii < solv->ruleassertions.count)
continue;
/*
* phase 2: now do the weak assertions
*/
if (!solv->weakrulemap.size)
break; /* no weak rules, no phase 2 */
for (ii = 0; ii < solv->ruleassertions.count; ii++)
{
ri = solv->ruleassertions.elements[ii];
r = solv->rules + ri;
if (r->d < 0 || r->w2) /* disabled or no assertion */
continue;
if (ri >= solv->learntrules || !MAPTST(&solv->weakrulemap, ri)) /* skip non-weak */
continue;
v = r->p;
vv = v > 0 ? v : -v;
if (!solv->decisionmap[vv]) /* if not yet decided */
{
queue_push(&solv->decisionq, v);
queue_push(&solv->decisionq_why, r - solv->rules);
solv->decisionmap[vv] = v > 0 ? 1 : -1;
IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
{
Solvable *s = pool->solvables + vv;
if (v < 0)
POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", pool_solvable2str(pool, s));
else
POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing %s (weak assertion)\n", pool_solvable2str(pool, s));
}
continue;
}
/* check against previous decision: is there a conflict? */
if (v > 0 && solv->decisionmap[vv] > 0)
continue;
if (v < 0 && solv->decisionmap[vv] < 0)
continue;
POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, r);
solver_fixproblem(solv, ri);
havedisabled = 1;
break; /* start over */
}
if (ii == solv->ruleassertions.count)
break; /* finished! */
}
return 1; /* the new level */
}
/********************************************************************/
/* watches */
/*-------------------------------------------------------------------
* makewatches
*
* initial setup for all watches
*/
static void
makewatches(Solver *solv)
{
Rule *r;
int i;
int nsolvables = solv->pool->nsolvables;
solv_free(solv->watches);
/* lower half for removals, upper half for installs */
solv->watches = solv_calloc(2 * nsolvables, sizeof(Id));
for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
{
if (!r->w2) /* assertions do not need watches */
continue;
/* see addwatches_rule(solv, r) */
r->n1 = solv->watches[nsolvables + r->w1];
solv->watches[nsolvables + r->w1] = r - solv->rules;
r->n2 = solv->watches[nsolvables + r->w2];
solv->watches[nsolvables + r->w2] = r - solv->rules;
}
}
/*-------------------------------------------------------------------
*
* add watches (for a new learned rule)
* sets up watches for a single rule
*
* see also makewatches() above.
*/
static inline void
addwatches_rule(Solver *solv, Rule *r)
{
int nsolvables = solv->pool->nsolvables;
r->n1 = solv->watches[nsolvables + r->w1];
solv->watches[nsolvables + r->w1] = r - solv->rules;
r->n2 = solv->watches[nsolvables + r->w2];
solv->watches[nsolvables + r->w2] = r - solv->rules;
}
/********************************************************************/
/*
* rule propagation
*/
/* shortcuts to check if a literal (positive or negative) assignment
* evaluates to 'true' or 'false'
*/
#define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
#define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
#define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
/*-------------------------------------------------------------------
*
* propagate
*
* make decision and propagate to all rules
*
* Evaluate each term affected by the decision (linked through watches).
* If we find unit rules we make new decisions based on them.
*
* return : 0 = everything is OK
* rule = conflict found in this rule
*/
static Rule *
propagate(Solver *solv, int level)
{
Pool *pool = solv->pool;
Id *rp, *next_rp; /* rule pointer, next rule pointer in linked list */
Rule *r; /* rule */
Id p, pkg, other_watch;
Id *dp;
Id *decisionmap = solv->decisionmap;
Id *watches = solv->watches + pool->nsolvables; /* place ptr in middle */
POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate level %d -----\n", level);
/* foreach non-propagated decision */
while (solv->propagate_index < solv->decisionq.count)
{
/*
* 'pkg' was just decided
* negate because our watches trigger if literal goes FALSE
*/
pkg = -solv->decisionq.elements[solv->propagate_index++];
IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
{
POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagate decision %d:", -pkg);
solver_printruleelement(solv, SOLV_DEBUG_PROPAGATE, 0, -pkg);
}
/* foreach rule where 'pkg' is now FALSE */
for (rp = watches + pkg; *rp; rp = next_rp)
{
r = solv->rules + *rp;
if (r->d < 0)
{
/* rule is disabled, goto next */
if (pkg == r->w1)
next_rp = &r->n1;
else
next_rp = &r->n2;
continue;
}
IF_POOLDEBUG (SOLV_DEBUG_WATCHES)
{
POOL_DEBUG(SOLV_DEBUG_WATCHES, " watch triggered ");
solver_printrule(solv, SOLV_DEBUG_WATCHES, r);
}
/*
* 'pkg' was just decided (was set to FALSE), so this rule
* may now be unit.
*/
/* find the other watch */
if (pkg == r->w1)
{
other_watch = r->w2;
next_rp = &r->n1;
}
else
{
other_watch = r->w1;
next_rp = &r->n2;
}
/*
* if the other watch is true we have nothing to do
*/
if (DECISIONMAP_TRUE(other_watch))
continue;
/*
* The other literal is FALSE or UNDEF
*
*/
if (r->d)
{
/* Not a binary clause, try to move our watch.
*
* Go over all literals and find one that is
* not other_watch
* and not FALSE
*
* (TRUE is also ok, in that case the rule is fulfilled)
* As speed matters here we do not use the FOR_RULELITERALS
* macro.
*/
if (r->p /* we have a 'p' */
&& r->p != other_watch /* which is not watched */
&& !DECISIONMAP_FALSE(r->p)) /* and not FALSE */
{
p = r->p;
}
else /* go find a 'd' to make 'true' */
{
/* foreach p in 'd'
we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
*/
for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
{
if (p != other_watch /* which is not watched */
&& !DECISIONMAP_FALSE(p)) /* and not FALSE */
break;
}
}
if (p)
{
/*
* if we found some p that is UNDEF or TRUE, move
* watch to it
*/
IF_POOLDEBUG (SOLV_DEBUG_WATCHES)
{
if (p > 0)
POOL_DEBUG(SOLV_DEBUG_WATCHES, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, p));
else
POOL_DEBUG(SOLV_DEBUG_WATCHES, " -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, -p));
}
*rp = *next_rp;
next_rp = rp;
if (pkg == r->w1)
{
r->w1 = p;
r->n1 = watches[p];
}
else
{
r->w2 = p;
r->n2 = watches[p];
}
watches[p] = r - solv->rules;
continue;
}
/* search failed, thus all unwatched literals are FALSE */
} /* not binary */
/*
* unit clause found, set literal other_watch to TRUE
*/
if (DECISIONMAP_FALSE(other_watch)) /* check if literal is FALSE */
return r; /* eek, a conflict! */
IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
{
POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " unit ");
solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
}
if (other_watch > 0)
decisionmap[other_watch] = level; /* install! */
else
decisionmap[-other_watch] = -level; /* remove! */
queue_push(&solv->decisionq, other_watch);
queue_push(&solv->decisionq_why, r - solv->rules);
IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
{
if (other_watch > 0)
POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> decided to install %s\n", pool_solvid2str(pool, other_watch));
else
POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> decided to conflict %s\n", pool_solvid2str(pool, -other_watch));
}
} /* foreach rule involving 'pkg' */
} /* while we have non-decided decisions */
POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate end -----\n");
return 0; /* all is well */
}
/********************************************************************/
/* Analysis */
/*-------------------------------------------------------------------
*
* revert
* revert decisionq to a level
*/
static void
revert(Solver *solv, int level)
{
Pool *pool = solv->pool;
Id v, vv;
while (solv->decisionq.count)
{
v = solv->decisionq.elements[solv->decisionq.count - 1];
vv = v > 0 ? v : -v;
if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
break;
POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
solv->decisionmap[vv] = 0;
solv->decisionq.count--;
solv->decisionq_why.count--;
solv->propagate_index = solv->decisionq.count;
}
while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= level)
solv->branches.count -= solv->branches.elements[solv->branches.count - 2];
if (solv->recommends_index > solv->decisionq.count)
solv->recommends_index = -1; /* rebuild recommends/suggests maps */
solv->decisionq_reason.count = level + 1;
}
/*-------------------------------------------------------------------
*
* watch2onhighest - put watch2 on literal with highest level
*/
static inline void
watch2onhighest(Solver *solv, Rule *r)
{
int l, wl = 0;
Id d, v, *dp;
d = r->d < 0 ? -r->d - 1 : r->d;
if (!d)
return; /* binary rule, both watches are set */
dp = solv->pool->whatprovidesdata + d;
while ((v = *dp++) != 0)
{
l = solv->decisionmap[v < 0 ? -v : v];
if (l < 0)
l = -l;
if (l > wl)
{
r->w2 = dp[-1];
wl = l;
}
}
}
/*-------------------------------------------------------------------
*
* analyze
* and learn
*/
static int
analyze(Solver *solv, int level, Rule *c, Rule **lrp)
{
Pool *pool = solv->pool;
Queue q;
Rule *r;
Id q_buf[8];
int rlevel = 1;
Map seen; /* global? */
Id p = 0, pp, v, vv, why;
int l, i, idx;
int num = 0, l1num = 0;
int learnt_why = solv->learnt_pool.count;
Id *decisionmap = solv->decisionmap;
queue_init_buffer(&q, q_buf, sizeof(q_buf)/sizeof(*q_buf));
POOL_DEBUG(SOLV_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
map_init(&seen, pool->nsolvables);
idx = solv->decisionq.count;
for (;;)
{
IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
solver_printruleclass(solv, SOLV_DEBUG_ANALYZE, c);
queue_push(&solv->learnt_pool, c - solv->rules);
FOR_RULELITERALS(v, pp, c)
{
if (DECISIONMAP_TRUE(v)) /* the one true literal */
continue;
vv = v > 0 ? v : -v;
if (MAPTST(&seen, vv))
continue;
MAPSET(&seen, vv); /* mark that we also need to look at this literal */
l = solv->decisionmap[vv];
if (l < 0)
l = -l;
if (l == 1)
l1num++; /* need to do this one in level1 pass */
else if (l == level)
num++; /* need to do this one as well */
else
{
queue_push(&q, v); /* not level1 or conflict level, add to new rule */
if (l > rlevel)
rlevel = l;
}
}
l1retry:
if (!num && !--l1num)
break; /* all literals done */
/* find the next literal to investigate */
/* (as num + l1num > 0, we know that we'll always find one) */
for (;;)
{
assert(idx > 0);
v = solv->decisionq.elements[--idx];
vv = v > 0 ? v : -v;
if (MAPTST(&seen, vv))
break;
}
MAPCLR(&seen, vv);
if (num && --num == 0)
{
/* done with normal literals, now start level 1 literal processing */
p = -v; /* so that v doesn't get lost */
if (!l1num)
break;
POOL_DEBUG(SOLV_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
/* clear non-l1 bits from seen map */
for (i = 0; i < q.count; i++)
{
v = q.elements[i];
MAPCLR(&seen, v > 0 ? v : -v);
}
/* only level 1 marks left in seen map */
l1num++; /* as l1retry decrements it */
goto l1retry;
}
why = solv->decisionq_why.elements[idx];
if (why <= 0) /* just in case, maybe for SYSTEMSOLVABLE */
goto l1retry;
c = solv->rules + why;
}
map_free(&seen);
assert(p != 0);
assert(rlevel > 0 && rlevel < level);
IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
{
POOL_DEBUG(SOLV_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, p);
for (i = 0; i < q.count; i++)
solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, q.elements[i]);
}
/* push end marker on learnt reasons stack */
queue_push(&solv->learnt_pool, 0);
solv->stats_learned++;
POOL_DEBUG(SOLV_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, rlevel);
level = rlevel;
revert(solv, level);
if (q.count < 2)
{
Id d = q.count ? q.elements[0] : 0;
queue_free(&q);
r = solver_addrule(solv, p, d, 0);
}
else
{
Id d = pool_queuetowhatprovides(pool, &q);
queue_free(&q);
r = solver_addrule(solv, p, 0, d);
}
assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
queue_push(&solv->learnt_why, learnt_why);
if (r->w2)
{
/* needs watches */
watch2onhighest(solv, r);
addwatches_rule(solv, r);
}
else
{
/* rule is an assertion */
queue_push(&solv->ruleassertions, r - solv->rules);
}
*lrp = r;
return level;
}
/*-------------------------------------------------------------------
*
* solver_reset
*
* reset all solver decisions
* called after rules have been enabled/disabled
*/
void
solver_reset(Solver *solv)
{
int i;
Id v;
/* rewind all decisions */
for (i = solv->decisionq.count - 1; i >= 0; i--)
{
v = solv->decisionq.elements[i];
solv->decisionmap[v > 0 ? v : -v] = 0;
}
queue_empty(&solv->decisionq_why);
queue_empty(&solv->decisionq);
queue_empty(&solv->decisionq_reason);
solv->recommends_index = -1;
solv->propagate_index = 0;
queue_empty(&solv->branches);
/* adapt learnt rule status to new set of enabled/disabled rules */
enabledisablelearntrules(solv);
}
static inline int
queue_contains(Queue *q, Id id)
{
int i;
for (i = 0; i < q->count; i++)
if (q->elements[i] == id)
return 1;
return 0;
}
static void
disable_recommendsrules(Solver *solv, Queue *weakq)
{
Pool *pool = solv->pool;
int i, rid;
for (i = 0; i < weakq->count; i++)
{
rid = weakq->elements[i];
if ((rid >= solv->recommendsrules && rid < solv->recommendsrules_end) || queue_contains(solv->recommendsruleq, rid))
{
Rule *r = solv->rules + rid;
if (r->d >= 0)
{
POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "disabling ");
solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, r);
solver_disablerule(solv, r);
}
}
}
}
/*-------------------------------------------------------------------
*
* analyze_unsolvable_rule
*
* recursion helper used by analyze_unsolvable
*/
static void
analyze_unsolvable_rule(Solver *solv, Rule *r, Queue *weakq, Map *rseen)
{
Pool *pool = solv->pool;
int i;
Id why = r - solv->rules;
IF_POOLDEBUG (SOLV_DEBUG_UNSOLVABLE)
solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, r);
if (solv->learntrules && why >= solv->learntrules)
{
if (MAPTST(rseen, why - solv->learntrules))
return;
MAPSET(rseen, why - solv->learntrules);
for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
if (solv->learnt_pool.elements[i] > 0)
analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], weakq, rseen);
return;
}
if (solv->weakrulemap.size && MAPTST(&solv->weakrulemap, why) && weakq)
queue_push(weakq, why);
/* add non-pkg rules to problem and disable */
if (why >= solv->pkgrules_end)
solver_recordproblem(solv, why);
}
/* fix a problem by disabling one or more weak rules */
static void
disable_weakrules(Solver *solv, Queue *weakq)
{
Pool *pool = solv->pool;
int i;
Id lastweak = 0;
for (i = 0; i < weakq->count; i++)
if (weakq->elements[i] > lastweak)
lastweak = weakq->elements[i];
if (lastweak >= solv->recommendsrules && lastweak < solv->recommendsrules_end)
{
lastweak = 0;
for (i = 0; i < weakq->count; i++)
if (weakq->elements[i] < solv->recommendsrules && weakq->elements[i] > lastweak)
lastweak = weakq->elements[i];
if (lastweak < solv->pkgrules_end)
{
disable_recommendsrules(solv, weakq);
return;
}
}
if (lastweak < solv->pkgrules_end && solv->strongrecommends && solv->recommendsruleq && queue_contains(solv->recommendsruleq, lastweak))
{
disable_recommendsrules(solv, weakq);
return;
}
POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "disabling ");
solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + lastweak);
/* choice rules need special handling */
if (lastweak >= solv->choicerules && lastweak < solv->choicerules_end)
solver_disablechoicerules(solv, solv->rules + lastweak);
else
solver_fixproblem(solv, lastweak);
}
/*-------------------------------------------------------------------
*
* analyze_unsolvable (called from setpropagatelearn)
*
* We know that the problem is not solvable. Record all involved
* rules (i.e. the "proof") into solv->learnt_pool.
* Record the learnt pool index and all non-pkg rules into
* solv->problems. (Our solutions to fix the problems are to
* disable those rules.)
*
* If the proof contains at least one weak rule, we disable the
* last of them.
*
* Otherwise we return -1 if disablerules is not set or disable
* _all_ of the problem rules and return 0.
*
* return: 0 - disabled some rules, try again
* -1 - hopeless
*/
static int
analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
{
Pool *pool = solv->pool;
Rule *r;
Map involved; /* global to speed things up? */
Map rseen;
Queue weakq;
Id pp, v, vv, why;
int idx;
Id *decisionmap = solv->decisionmap;
int oldproblemcount;
int oldlearntpoolcount;
int record_proof = 1;
POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
solv->stats_unsolvable++;
oldproblemcount = solv->problems.count;
oldlearntpoolcount = solv->learnt_pool.count;
/* make room for proof index */
/* must update it later, as analyze_unsolvable_rule would confuse
* it with a rule index if we put the real value in already */
queue_push(&solv->problems, 0);
r = cr;
map_init(&involved, pool->nsolvables);
map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
queue_init(&weakq);
if (record_proof)
queue_push(&solv->learnt_pool, r - solv->rules);
analyze_unsolvable_rule(solv, r, &weakq, &rseen);
FOR_RULELITERALS(v, pp, r)
{
if (DECISIONMAP_TRUE(v)) /* the one true literal */
abort();
vv = v > 0 ? v : -v;
MAPSET(&involved, vv);
}
idx = solv->decisionq.count;
while (idx > 0)
{
v = solv->decisionq.elements[--idx];
vv = v > 0 ? v : -v;
if (!MAPTST(&involved, vv) || vv == SYSTEMSOLVABLE)
continue;
why = solv->decisionq_why.elements[idx];