2 * Copyright (c) 2007-2008, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * SAT based dependency solver
25 #include "solverdebug.h"
27 #define RULES_BLOCK 63
29 /********************************************************************
31 * dependency check helpers
35 /*-------------------------------------------------------------------
36 * handle split provides
40 solver_splitprovides(Solver *solv, Id dep)
42 Pool *pool = solv->pool;
47 if (!solv->dosplitprovides || !solv->installed)
51 rd = GETRELDEP(pool, dep);
52 if (rd->flags != REL_WITH)
54 FOR_PROVIDES(p, pp, dep)
56 s = pool->solvables + p;
57 if (s->repo == solv->installed && s->name == rd->name)
64 /*-------------------------------------------------------------------
65 * solver_dep_installed
69 solver_dep_installed(Solver *solv, Id dep)
72 Pool *pool = solv->pool;
77 Reldep *rd = GETRELDEP(pool, dep);
78 if (rd->flags == REL_AND)
80 if (!solver_dep_installed(solv, rd->name))
82 return solver_dep_installed(solv, rd->evr);
84 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
85 return solver_dep_installed(solv, rd->evr);
87 FOR_PROVIDES(p, pp, dep)
89 if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
98 /************************************************************************/
101 * make assertion rules into decisions
103 * Go through rules and add direct assertions to the decisionqueue.
104 * If we find a conflict, disable rules and add them to problem queue.
108 makeruledecisions(Solver *solv)
110 Pool *pool = solv->pool;
116 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions ; size decisionq: %d -----\n",solv->decisionq.count);
118 /* The system solvable is always installed first */
119 assert(solv->decisionq.count == 0);
120 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
121 queue_push(&solv->decisionq_why, 0);
122 solv->decisionmap[SYSTEMSOLVABLE] = 1; /* installed at level '1' */
124 decisionstart = solv->decisionq.count;
125 for (ii = 0; ii < solv->ruleassertions.count; ii++)
127 ri = solv->ruleassertions.elements[ii];
128 r = solv->rules + ri;
130 if (r->d < 0 || !r->p || r->w2) /* disabled, dummy or no assertion */
132 /* do weak rules in phase 2 */
133 if (ri < solv->learntrules && MAPTST(&solv->weakrulemap, ri))
139 if (!solv->decisionmap[vv]) /* if not yet decided */
144 queue_push(&solv->decisionq, v);
145 queue_push(&solv->decisionq_why, r - solv->rules);
146 solv->decisionmap[vv] = v > 0 ? 1 : -1;
147 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
149 Solvable *s = solv->pool->solvables + vv;
151 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", solvable2str(solv->pool, s));
153 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (assertion)\n", solvable2str(solv->pool, s));
158 * check previous decision: is it sane ?
161 if (v > 0 && solv->decisionmap[vv] > 0) /* ok to install */
163 if (v < 0 && solv->decisionmap[vv] < 0) /* ok to remove */
169 * The rule (r) we're currently processing says something
170 * different (v = r->p) than a previous decision (decisionmap[abs(v)])
174 if (ri >= solv->learntrules)
176 /* conflict with a learnt rule */
177 /* can happen when packages cannot be installed for
178 * multiple reasons. */
179 /* we disable the learnt rule in this case */
180 solver_disablerule(solv, r);
185 * find the decision which is the "opposite" of the rule
188 for (i = 0; i < solv->decisionq.count; i++)
189 if (solv->decisionq.elements[i] == -v)
191 assert(i < solv->decisionq.count); /* assert that we found it */
194 * conflict with system solvable ?
197 if (v == -SYSTEMSOLVABLE) {
198 /* conflict with system solvable */
199 queue_push(&solv->problems, solv->learnt_pool.count);
200 queue_push(&solv->learnt_pool, ri);
201 queue_push(&solv->learnt_pool, 0);
202 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
203 if (ri >= solv->jobrules && ri < solv->jobrules_end)
204 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
207 queue_push(&solv->problems, v);
208 queue_push(&solv->problems, 0);
209 solver_disableproblem(solv, v);
213 assert(solv->decisionq_why.elements[i] > 0);
216 * conflict with an rpm rule ?
219 if (solv->decisionq_why.elements[i] < solv->rpmrules_end)
221 /* conflict with rpm rule assertion */
222 queue_push(&solv->problems, solv->learnt_pool.count);
223 queue_push(&solv->learnt_pool, ri);
224 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
225 queue_push(&solv->learnt_pool, 0);
226 assert(v > 0 || v == -SYSTEMSOLVABLE);
227 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
228 if (ri >= solv->jobrules && ri < solv->jobrules_end)
229 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
232 queue_push(&solv->problems, v);
233 queue_push(&solv->problems, 0);
234 solver_disableproblem(solv, v);
239 * conflict with another job or update/feature rule
243 queue_push(&solv->problems, solv->learnt_pool.count);
244 queue_push(&solv->learnt_pool, ri);
245 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
246 queue_push(&solv->learnt_pool, 0);
248 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflicting update/job assertions over literal %d\n", vv);
251 * push all of our rules (can only be feature or job rules)
252 * asserting this literal on the problem stack
255 for (i = solv->featurerules, rr = solv->rules + i; i < solv->learntrules; i++, rr++)
257 if (rr->d < 0 /* disabled */
258 || rr->w2) /* or no assertion */
260 if (rr->p != vv /* not affecting the literal */
263 if (MAPTST(&solv->weakrulemap, i)) /* weak: silently ignore */
266 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
268 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, solv->rules + i);
271 /* is is a job rule ? */
272 if (i >= solv->jobrules && i < solv->jobrules_end)
273 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
275 queue_push(&solv->problems, v);
276 solver_disableproblem(solv, v);
278 queue_push(&solv->problems, 0);
282 * (back up from decisions)
284 while (solv->decisionq.count > decisionstart)
286 v = solv->decisionq.elements[--solv->decisionq.count];
287 --solv->decisionq_why.count;
289 solv->decisionmap[vv] = 0;
291 ii = -1; /* restarts loop at 0 */
295 * phase 2: now do the weak assertions
297 for (ii = 0; ii < solv->ruleassertions.count; ii++)
299 ri = solv->ruleassertions.elements[ii];
300 r = solv->rules + ri;
301 if (r->d < 0 || r->w2) /* disabled or no assertion */
303 if (ri >= solv->learntrules || !MAPTST(&solv->weakrulemap, ri)) /* skip non-weak */
309 * (if not yet decided)
311 if (!solv->decisionmap[vv])
313 queue_push(&solv->decisionq, v);
314 queue_push(&solv->decisionq_why, r - solv->rules);
315 solv->decisionmap[vv] = v > 0 ? 1 : -1;
316 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
318 Solvable *s = solv->pool->solvables + vv;
320 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", solvable2str(solv->pool, s));
322 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (weak assertion)\n", solvable2str(solv->pool, s));
327 * previously decided, sane ?
329 if (v > 0 && solv->decisionmap[vv] > 0)
331 if (v < 0 && solv->decisionmap[vv] < 0)
334 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
335 solver_printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
337 if (ri >= solv->jobrules && ri < solv->jobrules_end)
338 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
341 solver_disableproblem(solv, v);
343 solver_reenablepolicyrules(solv, -(v + 1));
346 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions end; size decisionq: %d -----\n",solv->decisionq.count);
350 /*-------------------------------------------------------------------
351 * enable/disable learnt rules
353 * we have enabled or disabled some of our rules. We now reenable all
354 * of our learnt rules except the ones that were learnt from rules that
358 enabledisablelearntrules(Solver *solv)
360 Pool *pool = solv->pool;
365 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
366 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
368 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
369 while ((why = *whyp++) != 0)
371 assert(why > 0 && why < i);
372 if (solv->rules[why].d < 0)
375 /* why != 0: we found a disabled rule, disable the learnt rule */
376 if (why && r->d >= 0)
378 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
380 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "disabling ");
381 solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
383 solver_disablerule(solv, r);
385 else if (!why && r->d < 0)
387 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
389 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "re-enabling ");
390 solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
392 solver_enablerule(solv, r);
398 /********************************************************************/
402 /*-------------------------------------------------------------------
405 * initial setup for all watches
409 makewatches(Solver *solv)
413 int nsolvables = solv->pool->nsolvables;
415 sat_free(solv->watches);
416 /* lower half for removals, upper half for installs */
417 solv->watches = sat_calloc(2 * nsolvables, sizeof(Id));
419 /* do it reverse so rpm rules get triggered first (XXX: obsolete?) */
420 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
422 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
425 if (!r->w2) /* assertions do not need watches */
428 /* see addwatches_rule(solv, r) */
429 r->n1 = solv->watches[nsolvables + r->w1];
430 solv->watches[nsolvables + r->w1] = r - solv->rules;
432 r->n2 = solv->watches[nsolvables + r->w2];
433 solv->watches[nsolvables + r->w2] = r - solv->rules;
438 /*-------------------------------------------------------------------
440 * add watches (for a new learned rule)
441 * sets up watches for a single rule
443 * see also makewatches() above.
447 addwatches_rule(Solver *solv, Rule *r)
449 int nsolvables = solv->pool->nsolvables;
451 r->n1 = solv->watches[nsolvables + r->w1];
452 solv->watches[nsolvables + r->w1] = r - solv->rules;
454 r->n2 = solv->watches[nsolvables + r->w2];
455 solv->watches[nsolvables + r->w2] = r - solv->rules;
459 /********************************************************************/
465 /* shortcuts to check if a literal (positive or negative) assignment
466 * evaluates to 'true' or 'false'
468 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
469 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
470 #define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
472 /*-------------------------------------------------------------------
476 * make decision and propagate to all rules
478 * Evaluate each term affected by the decision (linked through watches)
479 * If we find unit rules we make new decisions based on them
481 * Everything's fixed there, it's just finding rules that are
484 * return : 0 = everything is OK
485 * rule = conflict found in this rule
489 propagate(Solver *solv, int level)
491 Pool *pool = solv->pool;
492 Id *rp, *next_rp; /* rule pointer, next rule pointer in linked list */
494 Id p, pkg, other_watch;
496 Id *decisionmap = solv->decisionmap;
498 Id *watches = solv->watches + pool->nsolvables; /* place ptr in middle */
500 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
502 /* foreach non-propagated decision */
503 while (solv->propagate_index < solv->decisionq.count)
506 * 'pkg' was just decided
507 * negate because our watches trigger if literal goes FALSE
509 pkg = -solv->decisionq.elements[solv->propagate_index++];
511 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
513 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
514 solver_printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
517 /* foreach rule where 'pkg' is now FALSE */
518 for (rp = watches + pkg; *rp; rp = next_rp)
520 r = solv->rules + *rp;
523 /* rule is disabled, goto next */
531 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
533 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
534 solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
537 /* 'pkg' was just decided (was set to FALSE)
539 * now find other literal watch, check clause
540 * and advance on linked list
554 * This term is already true (through the other literal)
555 * so we have nothing to do
557 if (DECISIONMAP_TRUE(other_watch))
561 * The other literal is FALSE or UNDEF
567 /* Not a binary clause, try to move our watch.
569 * Go over all literals and find one that is
573 * (TRUE is also ok, in that case the rule is fulfilled)
575 if (r->p /* we have a 'p' */
576 && r->p != other_watch /* which is not watched */
577 && !DECISIONMAP_FALSE(r->p)) /* and not FALSE */
581 else /* go find a 'd' to make 'true' */
584 we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
586 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
588 if (p != other_watch /* which is not watched */
589 && !DECISIONMAP_FALSE(p)) /* and not FALSE */
597 * if we found some p that is UNDEF or TRUE, move
600 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
603 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvid2str(pool, p));
605 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvid2str(pool, -p));
621 watches[p] = r - solv->rules;
624 /* search failed, thus all unwatched literals are FALSE */
629 * unit clause found, set literal other_watch to TRUE
632 if (DECISIONMAP_FALSE(other_watch)) /* check if literal is FALSE */
633 return r; /* eek, a conflict! */
635 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
637 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " unit ");
638 solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
642 decisionmap[other_watch] = level; /* install! */
644 decisionmap[-other_watch] = -level; /* remove! */
646 queue_push(&solv->decisionq, other_watch);
647 queue_push(&solv->decisionq_why, r - solv->rules);
649 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
652 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", solvid2str(pool, other_watch));
654 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", solvid2str(pool, -other_watch));
657 } /* foreach rule involving 'pkg' */
659 } /* while we have non-decided decisions */
661 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
663 return 0; /* all is well */
667 /********************************************************************/
670 /*-------------------------------------------------------------------
677 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
679 Pool *pool = solv->pool;
682 Map seen; /* global? */
683 Id d, v, vv, *dp, why;
685 int num = 0, l1num = 0;
686 int learnt_why = solv->learnt_pool.count;
687 Id *decisionmap = solv->decisionmap;
691 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
692 map_init(&seen, pool->nsolvables);
693 idx = solv->decisionq.count;
696 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
697 solver_printruleclass(solv, SAT_DEBUG_ANALYZE, c);
698 queue_push(&solv->learnt_pool, c - solv->rules);
699 d = c->d < 0 ? -c->d - 1 : c->d;
700 dp = d ? pool->whatprovidesdata + d : 0;
701 /* go through all literals of the rule */
713 if (DECISIONMAP_TRUE(v)) /* the one true literal */
716 if (MAPTST(&seen, vv))
718 l = solv->decisionmap[vv];
723 l1num++; /* need to do this one in level1 pass */
725 num++; /* need to do this one as well */
728 queue_push(&r, v); /* not level1 or conflict level, add to new rule */
734 if (!num && !--l1num)
735 break; /* all level 1 literals done */
739 v = solv->decisionq.elements[--idx];
741 if (MAPTST(&seen, vv))
745 if (num && --num == 0)
747 *pr = -v; /* so that v doesn't get lost */
750 POOL_DEBUG(SAT_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
751 for (i = 0; i < r.count; i++)
754 MAPCLR(&seen, v > 0 ? v : -v);
756 /* only level 1 marks left */
760 why = solv->decisionq_why.elements[idx];
761 if (why <= 0) /* just in case, maybe for SYSTEMSOLVABLE */
763 c = solv->rules + why;
769 else if (r.count == 1 && r.elements[0] < 0)
772 *dr = pool_queuetowhatprovides(pool, &r);
773 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
775 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
776 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, *pr);
777 for (i = 0; i < r.count; i++)
778 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
780 /* push end marker on learnt reasons stack */
781 queue_push(&solv->learnt_pool, 0);
784 solv->stats_learned++;
789 /*-------------------------------------------------------------------
793 * reset the solver decisions to right after the rpm rules.
794 * called after rules have been enabled/disabled
798 solver_reset(Solver *solv)
800 Pool *pool = solv->pool;
804 /* rewind all decisions */
805 for (i = solv->decisionq.count - 1; i >= 0; i--)
807 v = solv->decisionq.elements[i];
808 solv->decisionmap[v > 0 ? v : -v] = 0;
810 solv->decisionq_why.count = 0;
811 solv->decisionq.count = 0;
812 solv->recommends_index = -1;
813 solv->propagate_index = 0;
814 solv->recommendations.count = 0;
815 solv->branches.count = 0;
817 /* adapt learnt rule status to new set of enabled/disabled rules */
818 enabledisablelearntrules(solv);
820 /* redo all job/update decisions */
821 makeruledecisions(solv);
822 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
826 /*-------------------------------------------------------------------
828 * analyze_unsolvable_rule
832 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp)
834 Pool *pool = solv->pool;
836 Id why = r - solv->rules;
838 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
839 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
840 if (solv->learntrules && why >= solv->learntrules)
842 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
843 if (solv->learnt_pool.elements[i] > 0)
844 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp);
847 if (MAPTST(&solv->weakrulemap, why))
848 if (!*lastweakp || why > *lastweakp)
850 /* do not add rpm rules to problem */
851 if (why < solv->rpmrules_end)
853 /* turn rule into problem */
854 if (why >= solv->jobrules && why < solv->jobrules_end)
855 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
856 /* normalize dup/infarch rules */
857 if (why > solv->infarchrules && why < solv->infarchrules_end)
859 Id name = pool->solvables[-solv->rules[why].p].name;
860 while (why > solv->infarchrules && pool->solvables[-solv->rules[why - 1].p].name == name)
863 if (why > solv->duprules && why < solv->duprules_end)
865 Id name = pool->solvables[-solv->rules[why].p].name;
866 while (why > solv->duprules && pool->solvables[-solv->rules[why - 1].p].name == name)
870 /* return if problem already countains our rule */
871 if (solv->problems.count)
873 for (i = solv->problems.count - 1; i >= 0; i--)
874 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
876 else if (solv->problems.elements[i] == why)
879 queue_push(&solv->problems, why);
883 /*-------------------------------------------------------------------
887 * return: 1 - disabled some rules, try again
892 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
894 Pool *pool = solv->pool;
896 Map seen; /* global to speed things up? */
897 Id d, v, vv, *dp, why;
899 Id *decisionmap = solv->decisionmap;
901 int oldlearntpoolcount;
904 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
905 solv->stats_unsolvable++;
906 oldproblemcount = solv->problems.count;
907 oldlearntpoolcount = solv->learnt_pool.count;
909 /* make room for proof index */
910 /* must update it later, as analyze_unsolvable_rule would confuse
911 * it with a rule index if we put the real value in already */
912 queue_push(&solv->problems, 0);
915 map_init(&seen, pool->nsolvables);
916 queue_push(&solv->learnt_pool, r - solv->rules);
918 analyze_unsolvable_rule(solv, r, &lastweak);
919 d = r->d < 0 ? -r->d - 1 : r->d;
920 dp = d ? pool->whatprovidesdata + d : 0;
931 if (DECISIONMAP_TRUE(v)) /* the one true literal */
934 l = solv->decisionmap[vv];
939 idx = solv->decisionq.count;
942 v = solv->decisionq.elements[--idx];
944 if (!MAPTST(&seen, vv))
946 why = solv->decisionq_why.elements[idx];
948 queue_push(&solv->learnt_pool, why);
949 r = solv->rules + why;
950 analyze_unsolvable_rule(solv, r, &lastweak);
951 d = r->d < 0 ? -r->d - 1 : r->d;
952 dp = d ? pool->whatprovidesdata + d : 0;
963 if (DECISIONMAP_TRUE(v)) /* the one true literal */
966 l = solv->decisionmap[vv];
973 queue_push(&solv->problems, 0); /* mark end of this problem */
978 extern void disablechoicerules(Solver *solv, Rule *r);
979 /* disable last weak rule */
980 solv->problems.count = oldproblemcount;
981 solv->learnt_pool.count = oldlearntpoolcount;
982 if (lastweak >= solv->jobrules && lastweak < solv->jobrules_end)
983 v = -(solv->ruletojob.elements[lastweak - solv->jobrules] + 1);
986 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling ");
987 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, solv->rules + lastweak);
988 if (lastweak >= solv->choicerules && lastweak < solv->choicerules_end)
989 disablechoicerules(solv, solv->rules + lastweak);
990 solver_disableproblem(solv, v);
992 solver_reenablepolicyrules(solv, -(v + 1));
998 queue_push(&solv->learnt_pool, 0);
999 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1003 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1004 solver_disableproblem(solv, solv->problems.elements[i]);
1005 /* XXX: might want to enable all weak rules again */
1009 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1014 /********************************************************************/
1015 /* Decision revert */
1017 /*-------------------------------------------------------------------
1020 * revert decision at level
1024 revert(Solver *solv, int level)
1026 Pool *pool = solv->pool;
1028 while (solv->decisionq.count)
1030 v = solv->decisionq.elements[solv->decisionq.count - 1];
1031 vv = v > 0 ? v : -v;
1032 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1034 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1035 if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
1036 solv->recommendations.count--;
1037 solv->decisionmap[vv] = 0;
1038 solv->decisionq.count--;
1039 solv->decisionq_why.count--;
1040 solv->propagate_index = solv->decisionq.count;
1042 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1044 solv->branches.count--;
1045 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1046 solv->branches.count--;
1048 solv->recommends_index = -1;
1052 /*-------------------------------------------------------------------
1054 * watch2onhighest - put watch2 on literal with highest level
1058 watch2onhighest(Solver *solv, Rule *r)
1063 d = r->d < 0 ? -r->d - 1 : r->d;
1065 return; /* binary rule, both watches are set */
1066 dp = solv->pool->whatprovidesdata + d;
1067 while ((v = *dp++) != 0)
1069 l = solv->decisionmap[v < 0 ? -v : v];
1081 /*-------------------------------------------------------------------
1085 * add free decision (solvable to install) to decisionq
1086 * increase level and propagate decision
1087 * return if no conflict.
1089 * in conflict case, analyze conflict rule, add resulting
1090 * rule to learnt rule set, make decision from learnt
1091 * rule (always unit) and re-propagate.
1093 * returns the new solver level or 0 if unsolvable
1098 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules, Id ruleid)
1100 Pool *pool = solv->pool;
1105 assert(ruleid >= 0);
1110 solv->decisionmap[decision] = level;
1112 solv->decisionmap[-decision] = -level;
1113 queue_push(&solv->decisionq, decision);
1114 queue_push(&solv->decisionq_why, -ruleid); /* <= 0 -> free decision */
1118 r = propagate(solv, level);
1122 return analyze_unsolvable(solv, r, disablerules);
1123 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1124 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
1125 assert(l > 0 && l < level);
1126 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1128 revert(solv, level);
1129 r = solver_addrule(solv, p, d);
1131 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
1132 queue_push(&solv->learnt_why, why);
1135 /* at least 2 literals, needs watches */
1136 watch2onhighest(solv, r);
1137 addwatches_rule(solv, r);
1141 /* learnt rule is an assertion */
1142 queue_push(&solv->ruleassertions, r - solv->rules);
1144 /* the new rule is unit by design */
1145 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1146 queue_push(&solv->decisionq, p);
1147 queue_push(&solv->decisionq_why, r - solv->rules);
1148 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1150 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
1151 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
1152 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
1153 solver_printrule(solv, SAT_DEBUG_ANALYZE, r);
1160 /*-------------------------------------------------------------------
1162 * select and install
1164 * install best package from the queue. We add an extra package, inst, if
1165 * provided. See comment in weak install section.
1167 * returns the new solver level or 0 if unsolvable
1172 selectandinstall(Solver *solv, int level, Queue *dq, int disablerules, Id ruleid)
1174 Pool *pool = solv->pool;
1179 policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1182 /* XXX: didn't we already do that? */
1183 /* XXX: shouldn't we prefer installed packages? */
1184 /* XXX: move to policy.c? */
1185 /* choose the supplemented one */
1186 for (i = 0; i < dq->count; i++)
1187 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1189 dq->elements[0] = dq->elements[i];
1196 /* multiple candidates, open a branch */
1197 for (i = 1; i < dq->count; i++)
1198 queue_push(&solv->branches, dq->elements[i]);
1199 queue_push(&solv->branches, -level);
1201 p = dq->elements[0];
1203 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvid2str(pool, p));
1205 return setpropagatelearn(solv, level, p, disablerules, ruleid);
1209 /********************************************************************/
1210 /* Main solver interface */
1213 /*-------------------------------------------------------------------
1216 * create solver structure
1218 * pool: all available solvables
1219 * installed: installed Solvables
1222 * Upon solving, rules are created to flag the Solvables
1223 * of the 'installed' Repo as installed.
1227 solver_create(Pool *pool)
1230 solv = (Solver *)sat_calloc(1, sizeof(Solver));
1232 solv->installed = pool->installed;
1234 transaction_init(&solv->trans, pool);
1235 queue_init(&solv->ruletojob);
1236 queue_init(&solv->decisionq);
1237 queue_init(&solv->decisionq_why);
1238 queue_init(&solv->problems);
1239 queue_init(&solv->suggestions);
1240 queue_init(&solv->recommendations);
1241 queue_init(&solv->orphaned);
1242 queue_init(&solv->learnt_why);
1243 queue_init(&solv->learnt_pool);
1244 queue_init(&solv->branches);
1245 queue_init(&solv->covenantq);
1246 queue_init(&solv->weakruleq);
1247 queue_init(&solv->ruleassertions);
1249 map_init(&solv->recommendsmap, pool->nsolvables);
1250 map_init(&solv->suggestsmap, pool->nsolvables);
1251 map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1252 solv->recommends_index = 0;
1254 solv->decisionmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
1256 solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1257 memset(solv->rules, 0, sizeof(Rule));
1263 /*-------------------------------------------------------------------
1269 solver_free(Solver *solv)
1271 transaction_free(&solv->trans);
1272 queue_free(&solv->job);
1273 queue_free(&solv->ruletojob);
1274 queue_free(&solv->decisionq);
1275 queue_free(&solv->decisionq_why);
1276 queue_free(&solv->learnt_why);
1277 queue_free(&solv->learnt_pool);
1278 queue_free(&solv->problems);
1279 queue_free(&solv->solutions);
1280 queue_free(&solv->suggestions);
1281 queue_free(&solv->recommendations);
1282 queue_free(&solv->orphaned);
1283 queue_free(&solv->branches);
1284 queue_free(&solv->covenantq);
1285 queue_free(&solv->weakruleq);
1286 queue_free(&solv->ruleassertions);
1288 map_free(&solv->recommendsmap);
1289 map_free(&solv->suggestsmap);
1290 map_free(&solv->noupdate);
1291 map_free(&solv->weakrulemap);
1292 map_free(&solv->noobsoletes);
1294 map_free(&solv->updatemap);
1295 map_free(&solv->fixmap);
1296 map_free(&solv->dupmap);
1297 map_free(&solv->dupinvolvedmap);
1299 sat_free(solv->decisionmap);
1300 sat_free(solv->rules);
1301 sat_free(solv->watches);
1302 sat_free(solv->obsoletes);
1303 sat_free(solv->obsoletes_data);
1304 sat_free(solv->multiversionupdaters);
1305 sat_free(solv->choicerules_ref);
1310 /*-------------------------------------------------------------------
1314 * all rules have been set up, now actually run the solver
1319 solver_run_sat(Solver *solv, int disablerules, int doweak)
1321 Queue dq; /* local decisionqueue */
1322 Queue dqs; /* local decisionqueue for supplements */
1328 Pool *pool = solv->pool;
1330 int minimizationsteps;
1331 int installedpos = solv->installed ? solv->installed->start : 0;
1333 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1335 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1336 for (i = 1; i < solv->nrules; i++)
1337 solver_printruleclass(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
1340 POOL_DEBUG(SAT_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1342 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
1343 solver_printdecisions(solv);
1345 /* start SAT algorithm */
1347 systemlevel = level + 1;
1348 POOL_DEBUG(SAT_DEBUG_SOLVER, "solving...\n");
1354 * here's the main loop:
1355 * 1) propagate new decisions (only needed for level 1)
1356 * 2) try to keep installed packages
1357 * 3) fulfill all unresolved rules
1358 * 4) install recommended packages
1359 * 5) minimalize solution if we had choices
1360 * if we encounter a problem, we rewind to a safe level and restart
1364 minimizationsteps = 0;
1372 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1373 if ((r = propagate(solv, level)) != 0)
1375 if (analyze_unsolvable(solv, r, disablerules))
1383 if (level < systemlevel)
1385 POOL_DEBUG(SAT_DEBUG_SOLVER, "resolving job rules\n");
1386 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1389 if (r->d < 0) /* ignore disabled rules */
1392 FOR_RULELITERALS(l, dp, r)
1396 if (solv->decisionmap[-l] <= 0)
1401 if (solv->decisionmap[l] > 0)
1403 if (solv->decisionmap[l] == 0)
1409 /* prune to installed if not updating */
1410 if (!solv->updatesystem && solv->installed && dq.count > 1)
1413 for (j = k = 0; j < dq.count; j++)
1415 Solvable *s = pool->solvables + dq.elements[j];
1416 if (s->repo == solv->installed)
1417 dq.elements[k++] = dq.elements[j];
1423 level = selectandinstall(solv, level, &dq, disablerules, i);
1430 if (level <= olevel)
1433 systemlevel = level + 1;
1434 if (i < solv->jobrules_end)
1440 * installed packages
1443 if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1445 Repo *installed = solv->installed;
1448 POOL_DEBUG(SAT_DEBUG_SOLVER, "resolving installed packages\n");
1449 /* we use two passes if we need to update packages
1450 * to create a better user experience */
1451 for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1453 int passlevel = level;
1454 /* start with installedpos, the position that gave us problems last time */
1455 for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1460 if (i == installed->end)
1461 i = installed->start;
1462 s = pool->solvables + i;
1463 if (s->repo != installed)
1466 if (solv->decisionmap[i] > 0)
1468 /* XXX: noupdate check is probably no longer needed, as all jobs should
1469 * already be satisfied */
1470 if (MAPTST(&solv->noupdate, i - installed->start))
1472 if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1473 continue; /* updates first */
1474 r = solv->rules + solv->updaterules + (i - installed->start);
1476 if (!rr->p || rr->d < 0) /* disabled -> look at feature rule */
1477 rr -= solv->installed->end - solv->installed->start;
1478 if (!rr->p) /* identical to update rule? */
1481 continue; /* orpaned package */
1484 if (solv->decisionmap[i] < 0 || solv->updatesystem || (solv->updatemap.size && MAPTST(&solv->updatemap, i - installed->start)) || rr->p != i)
1486 if (solv->noobsoletes.size && solv->multiversionupdaters
1487 && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1489 /* special multiversion handling, make sure best version is chosen */
1491 while ((p = pool->whatprovidesdata[d++]) != 0)
1492 if (solv->decisionmap[p] >= 0)
1494 policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1496 if (p != i && solv->decisionmap[p] == 0)
1498 rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1499 if (!rr->p) /* update rule == feature rule? */
1500 rr = rr - solv->featurerules + solv->updaterules;
1508 /* update to best package */
1509 FOR_RULELITERALS(p, dp, rr)
1511 if (solv->decisionmap[p] > 0)
1513 dq.count = 0; /* already fulfilled */
1516 if (!solv->decisionmap[p])
1521 /* install best version */
1525 level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1532 if (level <= olevel)
1534 if (level < passlevel)
1535 break; /* trouble */
1537 n = installed->start; /* redo all */
1543 /* if still undecided keep package */
1544 if (solv->decisionmap[i] == 0)
1547 POOL_DEBUG(SAT_DEBUG_POLICY, "keeping %s\n", solvid2str(pool, i));
1548 level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1555 if (level <= olevel)
1557 if (level < passlevel)
1558 break; /* trouble */
1560 n = installed->start; /* redo all */
1563 continue; /* retry with learnt rule */
1567 if (n < installed->end)
1569 installedpos = i; /* retry problem solvable next time */
1570 break; /* ran into trouble */
1572 installedpos = installed->start; /* reset installedpos */
1574 systemlevel = level + 1;
1576 continue; /* had trouble, retry */
1579 if (level < systemlevel)
1580 systemlevel = level;
1586 POOL_DEBUG(SAT_DEBUG_POLICY, "deciding unresolved rules\n");
1587 for (i = 1, n = 1; n < solv->nrules; i++, n++)
1589 if (i == solv->nrules)
1591 r = solv->rules + i;
1592 if (r->d < 0) /* ignore disabled rules */
1597 /* binary or unary rule */
1598 /* need two positive undecided literals */
1599 if (r->p < 0 || r->w2 <= 0)
1601 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1603 queue_push(&dq, r->p);
1604 queue_push(&dq, r->w2);
1609 * all negative literals are installed
1610 * no positive literal is installed
1611 * i.e. the rule is not fulfilled and we
1612 * just need to decide on the positive literals
1616 if (solv->decisionmap[-r->p] <= 0)
1621 if (solv->decisionmap[r->p] > 0)
1623 if (solv->decisionmap[r->p] == 0)
1624 queue_push(&dq, r->p);
1626 dp = pool->whatprovidesdata + r->d;
1627 while ((p = *dp++) != 0)
1631 if (solv->decisionmap[-p] <= 0)
1636 if (solv->decisionmap[p] > 0)
1638 if (solv->decisionmap[p] == 0)
1645 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1647 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
1648 solver_printruleclass(solv, SAT_DEBUG_PROPAGATE, r);
1650 /* dq.count < 2 cannot happen as this means that
1651 * the rule is unit */
1652 assert(dq.count > 1);
1655 level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
1662 if (level < systemlevel || level == 1)
1663 break; /* trouble */
1664 /* something changed, so look at all rules again */
1668 if (n != solv->nrules) /* ran into trouble, restart */
1675 POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended packages\n");
1676 queue_empty(&dq); /* recommended packages */
1677 queue_empty(&dqs); /* supplemented packages */
1678 for (i = 1; i < pool->nsolvables; i++)
1680 if (solv->decisionmap[i] < 0)
1682 if (solv->decisionmap[i] > 0)
1684 /* installed, check for recommends */
1685 Id *recp, rec, pp, p;
1686 s = pool->solvables + i;
1687 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
1689 /* XXX need to special case AND ? */
1692 recp = s->repo->idarraydata + s->recommends;
1693 while ((rec = *recp++) != 0)
1696 FOR_PROVIDES(p, pp, rec)
1698 if (solv->decisionmap[p] > 0)
1703 else if (solv->decisionmap[p] == 0)
1705 queue_pushunique(&dq, p);
1713 s = pool->solvables + i;
1714 if (!s->supplements)
1716 if (!pool_installable(pool, s))
1718 if (!solver_is_supplementing(solv, s))
1720 queue_push(&dqs, i);
1724 /* filter out all packages obsoleted by installed packages */
1725 /* this is no longer needed if we have reverse obsoletes */
1726 if ((dqs.count || dq.count) && solv->installed)
1729 Id obs, *obsp, po, ppo;
1731 map_init(&obsmap, pool->nsolvables);
1732 for (p = solv->installed->start; p < solv->installed->end; p++)
1734 s = pool->solvables + p;
1735 if (s->repo != solv->installed || !s->obsoletes)
1737 if (solv->decisionmap[p] <= 0)
1739 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
1741 obsp = s->repo->idarraydata + s->obsoletes;
1742 /* foreach obsoletes */
1743 while ((obs = *obsp++) != 0)
1744 FOR_PROVIDES(po, ppo, obs)
1745 MAPSET(&obsmap, po);
1747 for (i = j = 0; i < dqs.count; i++)
1748 if (!MAPTST(&obsmap, dqs.elements[i]))
1749 dqs.elements[j++] = dqs.elements[i];
1751 for (i = j = 0; i < dq.count; i++)
1752 if (!MAPTST(&obsmap, dq.elements[i]))
1753 dq.elements[j++] = dq.elements[i];
1758 /* filter out all already supplemented packages if requested */
1759 if (solv->ignorealreadyrecommended && dqs.count)
1761 /* turn off all new packages */
1762 for (i = 0; i < solv->decisionq.count; i++)
1764 p = solv->decisionq.elements[i];
1767 s = pool->solvables + p;
1768 if (s->repo && s->repo != solv->installed)
1769 solv->decisionmap[p] = -solv->decisionmap[p];
1771 /* filter out old supplements */
1772 for (i = j = 0; i < dqs.count; i++)
1774 p = dqs.elements[i];
1775 s = pool->solvables + p;
1776 if (!s->supplements)
1778 if (!solver_is_supplementing(solv, s))
1779 dqs.elements[j++] = p;
1782 /* undo turning off */
1783 for (i = 0; i < solv->decisionq.count; i++)
1785 p = solv->decisionq.elements[i];
1788 s = pool->solvables + p;
1789 if (s->repo && s->repo != solv->installed)
1790 solv->decisionmap[p] = -solv->decisionmap[p];
1794 /* multiversion doesn't mix well with supplements.
1795 * filter supplemented packages where we already decided
1796 * to install a different version (see bnc#501088) */
1797 if (dqs.count && solv->noobsoletes.size)
1799 for (i = j = 0; i < dqs.count; i++)
1801 p = dqs.elements[i];
1802 if (MAPTST(&solv->noobsoletes, p))
1805 s = pool->solvables + p;
1806 FOR_PROVIDES(p2, pp2, s->name)
1807 if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
1810 continue; /* ignore this package */
1812 dqs.elements[j++] = p;
1817 /* make dq contain both recommended and supplemented pkgs */
1820 for (i = 0; i < dqs.count; i++)
1821 queue_pushunique(&dq, dqs.elements[i]);
1827 int decisioncount = solv->decisionq.count;
1831 /* simple case, just one package. no need to choose */
1834 POOL_DEBUG(SAT_DEBUG_POLICY, "installing supplemented %s\n", solvid2str(pool, p));
1836 POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended %s\n", solvid2str(pool, p));
1837 queue_push(&solv->recommendations, p);
1838 level = setpropagatelearn(solv, level, p, 0, 0);
1839 continue; /* back to main loop */
1842 /* filter packages, this gives us the best versions */
1843 policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
1845 /* create map of result */
1846 map_init(&dqmap, pool->nsolvables);
1847 for (i = 0; i < dq.count; i++)
1848 MAPSET(&dqmap, dq.elements[i]);
1850 /* install all supplemented packages */
1851 for (i = 0; i < dqs.count; i++)
1853 p = dqs.elements[i];
1854 if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
1856 POOL_DEBUG(SAT_DEBUG_POLICY, "installing supplemented %s\n", solvid2str(pool, p));
1857 queue_push(&solv->recommendations, p);
1859 level = setpropagatelearn(solv, level, p, 0, 0);
1860 if (level <= olevel)
1863 if (i < dqs.count || solv->decisionq.count < decisioncount)
1869 /* install all recommended packages */
1870 /* more work as we want to created branches if multiple
1871 * choices are valid */
1872 for (i = 0; i < decisioncount; i++)
1875 p = solv->decisionq.elements[i];
1878 s = pool->solvables + p;
1879 if (!s->repo || (solv->ignorealreadyrecommended && s->repo == solv->installed))
1883 recp = s->repo->idarraydata + s->recommends;
1884 while ((rec = *recp++) != 0)
1887 FOR_PROVIDES(p, pp, rec)
1889 if (solv->decisionmap[p] > 0)
1894 else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
1895 queue_pushunique(&dq, p);
1901 /* multiple candidates, open a branch */
1902 for (i = 1; i < dq.count; i++)
1903 queue_push(&solv->branches, dq.elements[i]);
1904 queue_push(&solv->branches, -level);
1907 POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended %s\n", solvid2str(pool, p));
1908 queue_push(&solv->recommendations, p);
1910 level = setpropagatelearn(solv, level, p, 0, 0);
1911 if (level <= olevel || solv->decisionq.count < decisioncount)
1912 break; /* we had to revert some decisions */
1915 break; /* had a problem above, quit loop */
1919 continue; /* back to main loop */
1923 if (solv->distupgrade && solv->installed)
1925 int installedone = 0;
1927 /* let's see if we can install some unsupported package */
1928 POOL_DEBUG(SAT_DEBUG_SOLVER, "deciding unsupported packages\n");
1929 for (i = 0; i < solv->orphaned.count; i++)
1931 p = solv->orphaned.elements[i];
1932 if (solv->decisionmap[p])
1933 continue; /* already decided */
1935 if (solv->distupgrade_removeunsupported)
1937 POOL_DEBUG(SAT_DEBUG_SOLVER, "removing unsupported %s\n", solvid2str(pool, p));
1938 level = setpropagatelearn(solv, level, -p, 0, 0);
1942 POOL_DEBUG(SAT_DEBUG_SOLVER, "keeping unsupported %s\n", solvid2str(pool, p));
1943 level = setpropagatelearn(solv, level, p, 0, 0);
1949 if (installedone || i < solv->orphaned.count)
1953 if (solv->solution_callback)
1955 solv->solution_callback(solv, solv->solution_callback_data);
1956 if (solv->branches.count)
1958 int i = solv->branches.count - 1;
1959 int l = -solv->branches.elements[i];
1963 if (solv->branches.elements[i - 1] < 0)
1965 p = solv->branches.elements[i];
1966 POOL_DEBUG(SAT_DEBUG_SOLVER, "branching with %s\n", solvid2str(pool, p));
1968 for (j = i + 1; j < solv->branches.count; j++)
1969 queue_push(&dq, solv->branches.elements[j]);
1970 solv->branches.count = i;
1972 revert(solv, level);
1974 for (j = 0; j < dq.count; j++)
1975 queue_push(&solv->branches, dq.elements[j]);
1977 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
1979 level = setpropagatelearn(solv, level, p, disablerules, why);
1988 /* all branches done, we're finally finished */
1992 /* minimization step */
1993 if (solv->branches.count)
1995 int l = 0, lasti = -1, lastl = -1;
1999 for (i = solv->branches.count - 1; i >= 0; i--)
2001 p = solv->branches.elements[i];
2004 else if (p > 0 && solv->decisionmap[p] > l + 1)
2012 /* kill old solvable so that we do not loop */
2013 p = solv->branches.elements[lasti];
2014 solv->branches.elements[lasti] = 0;
2015 POOL_DEBUG(SAT_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, solvid2str(pool, p));
2016 minimizationsteps++;
2019 revert(solv, level);
2020 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2023 level = setpropagatelearn(solv, level, p, disablerules, why);
2033 /* no minimization found, we're finally finished! */
2036 POOL_DEBUG(SAT_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2038 POOL_DEBUG(SAT_DEBUG_STATS, "done solving.\n\n");
2042 solver_printdecisionq(solv, SAT_DEBUG_RESULT);
2047 /*-------------------------------------------------------------------
2049 * remove disabled conflicts
2051 * purpose: update the decisionmap after some rules were disabled.
2052 * this is used to calculate the suggested/recommended package list.
2053 * Also returns a "removed" list to undo the discisionmap changes.
2057 removedisabledconflicts(Solver *solv, Queue *removed)
2059 Pool *pool = solv->pool;
2064 Id *decisionmap = solv->decisionmap;
2066 POOL_DEBUG(SAT_DEBUG_SCHUBI, "removedisabledconflicts\n");
2067 queue_empty(removed);
2068 for (i = 0; i < solv->decisionq.count; i++)
2070 p = solv->decisionq.elements[i];
2073 /* a conflict. we never do conflicts on free decisions, so there
2074 * must have been an unit rule */
2075 why = solv->decisionq_why.elements[i];
2077 r = solv->rules + why;
2078 if (r->d < 0 && decisionmap[-p])
2080 /* rule is now disabled, remove from decisionmap */
2081 POOL_DEBUG(SAT_DEBUG_SCHUBI, "removing conflict for package %s[%d]\n", solvid2str(pool, -p), -p);
2082 queue_push(removed, -p);
2083 queue_push(removed, decisionmap[-p]);
2084 decisionmap[-p] = 0;
2087 if (!removed->count)
2089 /* we removed some confliced packages. some of them might still
2090 * be in conflict, so search for unit rules and re-conflict */
2092 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2094 if (i == solv->nrules)
2097 r = solv->rules + i;
2103 if (r->p < 0 && !decisionmap[-r->p])
2109 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2111 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2116 if (r->p < 0 && decisionmap[-r->p] == 0)
2118 if (new || DECISIONMAP_FALSE(r->p))
2120 dp = pool->whatprovidesdata + r->d;
2121 while ((p = *dp++) != 0)
2123 if (new && p == new)
2125 if (p < 0 && decisionmap[-p] == 0)
2134 else if (!DECISIONMAP_FALSE(p))
2144 POOL_DEBUG(SAT_DEBUG_SCHUBI, "re-conflicting package %s[%d]\n", solvid2str(pool, -new), -new);
2145 decisionmap[-new] = -1;
2147 n = 0; /* redo all rules */
2153 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2156 for (i = 0; i < removed->count; i += 2)
2157 solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2161 /*-------------------------------------------------------------------
2163 * weaken solvable dependencies
2167 weaken_solvable_deps(Solver *solv, Id p)
2172 for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2176 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2177 continue; /* conflict */
2178 queue_push(&solv->weakruleq, i);
2183 /********************************************************************/
2188 findrecommendedsuggested(Solver *solv)
2190 Pool *pool = solv->pool;
2191 Queue redoq, disabledq;
2197 map_init(&obsmap, pool->nsolvables);
2198 if (solv->installed)
2200 Id obs, *obsp, p, po, ppo;
2201 for (p = solv->installed->start; p < solv->installed->end; p++)
2203 s = pool->solvables + p;
2204 if (s->repo != solv->installed || !s->obsoletes)
2206 if (solv->decisionmap[p] <= 0)
2208 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
2210 obsp = s->repo->idarraydata + s->obsoletes;
2211 /* foreach obsoletes */
2212 while ((obs = *obsp++) != 0)
2213 FOR_PROVIDES(po, ppo, obs)
2214 MAPSET(&obsmap, po);
2219 queue_init(&disabledq);
2221 /* disable all erase jobs (including weak "keep uninstalled" rules) */
2222 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2224 if (r->d < 0) /* disabled ? */
2226 if (r->p >= 0) /* install job? */
2228 queue_push(&disabledq, i);
2229 solver_disablerule(solv, r);
2235 enabledisablelearntrules(solv);
2236 removedisabledconflicts(solv, &redoq);
2240 * find recommended packages
2243 /* if redoq.count == 0 we already found all recommended in the
2245 if (redoq.count || solv->dontinstallrecommended || !solv->dontshowinstalledrecommended || solv->ignorealreadyrecommended)
2247 Id rec, *recp, p, pp;
2249 /* create map of all recommened packages */
2250 solv->recommends_index = -1;
2251 MAPZERO(&solv->recommendsmap);
2252 for (i = 0; i < solv->decisionq.count; i++)
2254 p = solv->decisionq.elements[i];
2257 s = pool->solvables + p;
2260 recp = s->repo->idarraydata + s->recommends;
2261 while ((rec = *recp++) != 0)
2263 FOR_PROVIDES(p, pp, rec)
2264 if (solv->decisionmap[p] > 0)
2268 if (!solv->dontshowinstalledrecommended)
2270 FOR_PROVIDES(p, pp, rec)
2271 if (solv->decisionmap[p] > 0)
2272 MAPSET(&solv->recommendsmap, p);
2274 continue; /* p != 0: already fulfilled */
2276 FOR_PROVIDES(p, pp, rec)
2277 MAPSET(&solv->recommendsmap, p);
2281 for (i = 1; i < pool->nsolvables; i++)
2283 if (solv->decisionmap[i] < 0)
2285 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2287 if (MAPTST(&obsmap, i))
2289 s = pool->solvables + i;
2290 if (!MAPTST(&solv->recommendsmap, i))
2292 if (!s->supplements)
2294 if (!pool_installable(pool, s))
2296 if (!solver_is_supplementing(solv, s))
2299 if (solv->dontinstallrecommended)
2300 queue_push(&solv->recommendations, i);
2302 queue_pushunique(&solv->recommendations, i);
2304 /* we use MODE_SUGGEST here so that repo prio is ignored */
2305 policy_filter_unwanted(solv, &solv->recommendations, POLICY_MODE_SUGGEST);
2309 * find suggested packages
2314 Id sug, *sugp, p, pp;
2316 /* create map of all suggests that are still open */
2317 solv->recommends_index = -1;
2318 MAPZERO(&solv->suggestsmap);
2319 for (i = 0; i < solv->decisionq.count; i++)
2321 p = solv->decisionq.elements[i];
2324 s = pool->solvables + p;
2327 sugp = s->repo->idarraydata + s->suggests;
2328 while ((sug = *sugp++) != 0)
2330 FOR_PROVIDES(p, pp, sug)
2331 if (solv->decisionmap[p] > 0)
2335 if (!solv->dontshowinstalledrecommended)
2337 FOR_PROVIDES(p, pp, sug)
2338 if (solv->decisionmap[p] > 0)
2339 MAPSET(&solv->suggestsmap, p);
2341 continue; /* already fulfilled */
2343 FOR_PROVIDES(p, pp, sug)
2344 MAPSET(&solv->suggestsmap, p);
2348 for (i = 1; i < pool->nsolvables; i++)
2350 if (solv->decisionmap[i] < 0)
2352 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2354 if (MAPTST(&obsmap, i))
2356 s = pool->solvables + i;
2357 if (!MAPTST(&solv->suggestsmap, i))
2361 if (!pool_installable(pool, s))
2363 if (!solver_is_enhancing(solv, s))
2366 queue_push(&solv->suggestions, i);
2368 policy_filter_unwanted(solv, &solv->suggestions, POLICY_MODE_SUGGEST);
2371 /* undo removedisabledconflicts */
2373 undo_removedisabledconflicts(solv, &redoq);
2376 /* undo job rule disabling */
2377 for (i = 0; i < disabledq.count; i++)
2378 solver_enablerule(solv, solv->rules + disabledq.elements[i]);
2379 queue_free(&disabledq);
2384 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap)
2387 Id how, what, select;
2389 for (i = 0; i < job->count; i += 2)
2391 how = job->elements[i];
2392 if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
2394 what = job->elements[i + 1];
2395 select = how & SOLVER_SELECTMASK;
2396 if (!noobsmap->size)
2397 map_grow(noobsmap, pool->nsolvables);
2398 FOR_JOB_SELECT(p, pp, select, what)
2399 MAPSET(noobsmap, p);
2410 solver_solve(Solver *solv, Queue *job)
2412 Pool *pool = solv->pool;
2413 Repo *installed = solv->installed;
2416 Map addedmap; /* '1' == have rpm-rules for solvable */
2417 Map installcandidatemap;
2418 Id how, what, select, name, weak, p, pp, d;
2422 int now, solve_start;
2425 solve_start = sat_timems(0);
2427 /* log solver options */
2428 POOL_DEBUG(SAT_DEBUG_STATS, "solver started\n");
2429 POOL_DEBUG(SAT_DEBUG_STATS, "fixsystem=%d updatesystem=%d dosplitprovides=%d, noupdateprovide=%d noinfarchcheck=%d\n", solv->fixsystem, solv->updatesystem, solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
2430 POOL_DEBUG(SAT_DEBUG_STATS, "distupgrade=%d distupgrade_removeunsupported=%d\n", solv->distupgrade, solv->distupgrade_removeunsupported);
2431 POOL_DEBUG(SAT_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allowarchchange, solv->allowvendorchange);
2432 POOL_DEBUG(SAT_DEBUG_STATS, "promoteepoch=%d, novirtualconflicts=%d, allowselfconflicts=%d\n", pool->promoteepoch, pool->novirtualconflicts, pool->allowselfconflicts);
2433 POOL_DEBUG(SAT_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2434 POOL_DEBUG(SAT_DEBUG_STATS, "dontinstallrecommended=%d, ignorealreadyrecommended=%d, dontshowinstalledrecommended=%d\n", solv->dontinstallrecommended, solv->ignorealreadyrecommended, solv->dontshowinstalledrecommended);
2436 /* create whatprovides if not already there */
2437 if (!pool->whatprovides)
2438 pool_createwhatprovides(pool);
2440 /* create obsolete index */
2441 policy_create_obsolete_index(solv);
2444 queue_free(&solv->job);
2445 queue_init_clone(&solv->job, job);
2448 * create basic rule set of all involved packages
2449 * use addedmap bitmap to make sure we don't create rules twice
2452 /* create noobsolete map if needed */
2453 solver_calculate_noobsmap(pool, job, &solv->noobsoletes);
2455 map_init(&addedmap, pool->nsolvables);
2456 MAPSET(&addedmap, SYSTEMSOLVABLE);
2458 map_init(&installcandidatemap, pool->nsolvables);
2461 now = sat_timems(0);
2463 * create rules for all package that could be involved with the solving
2464 * so called: rpm rules
2469 /* check for verify jobs */
2470 for (i = 0; i < job->count; i += 2)
2472 how = job->elements[i];
2473 what = job->elements[i + 1];
2474 select = how & SOLVER_SELECTMASK;
2475 switch (how & SOLVER_JOBMASK)
2478 FOR_JOB_SELECT(p, pp, select, what)
2480 s = pool->solvables + p;
2481 if (!solv->installed || s->repo != solv->installed)
2483 if (!solv->fixmap.size)
2484 map_grow(&solv->fixmap, solv->installed->end - solv->installed->start);
2485 MAPSET(&solv->fixmap, p - solv->installed->start);
2493 oldnrules = solv->nrules;
2494 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
2495 FOR_REPO_SOLVABLES(installed, p, s)
2496 solver_addrpmrulesforsolvable(solv, s, &addedmap);
2497 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
2498 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
2499 oldnrules = solv->nrules;
2500 FOR_REPO_SOLVABLES(installed, p, s)
2501 solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
2502 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
2506 * create rules for all packages involved in the job
2507 * (to be installed or removed)
2510 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
2511 oldnrules = solv->nrules;
2512 for (i = 0; i < job->count; i += 2)
2514 how = job->elements[i];
2515 what = job->elements[i + 1];
2516 select = how & SOLVER_SELECTMASK;
2518 switch (how & SOLVER_JOBMASK)
2520 case SOLVER_INSTALL:
2521 FOR_JOB_SELECT(p, pp, select, what)
2523 MAPSET(&installcandidatemap, p);
2524 solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
2527 case SOLVER_DISTUPGRADE:
2528 if (!solv->distupgrade)
2535 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
2539 * add rules for suggests, enhances
2541 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for suggested/enhanced packages ***\n");
2542 oldnrules = solv->nrules;
2543 solver_addrpmrulesforweak(solv, &addedmap);
2544 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
2547 * first pass done, we now have all the rpm rules we need.
2548 * unify existing rules before going over all job rules and
2550 * at this point the system is always solvable,
2551 * as an empty system (remove all packages) is a valid solution
2554 IF_POOLDEBUG (SAT_DEBUG_STATS)
2556 int possible = 0, installable = 0;
2557 for (i = 1; i < pool->nsolvables; i++)
2559 if (pool_installable(pool, pool->solvables + i))
2561 if (MAPTST(&addedmap, i))
2564 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
2567 solver_unifyrules(solv); /* remove duplicate rpm rules */
2568 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
2570 POOL_DEBUG(SAT_DEBUG_STATS, "rpm rule memory usage: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
2571 POOL_DEBUG(SAT_DEBUG_STATS, "rpm rule creation took %d ms\n", sat_timems(now));
2573 /* create dup maps if needed. We need the maps early to create our
2576 solver_createdupmaps(solv);
2579 * create feature rules
2581 * foreach installed:
2582 * create assertion (keep installed, if no update available)
2584 * create update rule (A|update1(A)|update2(A)|...)
2586 * those are used later on to keep a version of the installed packages in
2590 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add feature rules ***\n");
2591 solv->featurerules = solv->nrules; /* mark start of feature rules */
2594 /* foreach possibly installed solvable */
2595 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2597 if (s->repo != installed)
2599 solver_addrule(solv, 0, 0); /* create dummy rule */
2602 solver_addupdaterule(solv, s, 1); /* allow s to be updated */
2604 /* make sure we accounted for all rules */
2605 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
2607 solv->featurerules_end = solv->nrules;
2610 * Add update rules for installed solvables
2612 * almost identical to feature rules
2613 * except that downgrades/archchanges/vendorchanges are not allowed
2616 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add update rules ***\n");
2617 solv->updaterules = solv->nrules;
2620 { /* foreach installed solvables */
2621 /* we create all update rules, but disable some later on depending on the job */
2622 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2626 if (s->repo != installed)
2628 solver_addrule(solv, 0, 0); /* create dummy rule */
2631 solver_addupdaterule(solv, s, 0); /* allowall = 0: downgrades not allowed */
2633 * check for and remove duplicate
2635 r = solv->rules + solv->nrules - 1; /* r: update rule */
2636 sr = r - (installed->end - installed->start); /* sr: feature rule */
2637 /* it's orphaned if there is no feature rule or the feature rule
2638 * consists just of the installed package */
2639 if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
2640 queue_push(&solv->orphaned, i);
2643 assert(solv->distupgrade && !sr->p);
2646 if (!solver_samerule(solv, r, sr))
2648 /* identical rule, kill unneeded one */
2649 if (solv->allowuninstall)
2651 /* keep feature rule, make it weak */
2652 memset(r, 0, sizeof(*r));
2653 queue_push(&solv->weakruleq, sr - solv->rules);
2657 /* keep update rule */
2658 memset(sr, 0, sizeof(*sr));
2661 else if (solv->allowuninstall)
2663 /* make both feature and update rule weak */
2664 queue_push(&solv->weakruleq, r - solv->rules);
2665 queue_push(&solv->weakruleq, sr - solv->rules);
2668 solver_disablerule(solv, sr);
2670 /* consistency check: we added a rule for _every_ installed solvable */
2671 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
2673 solv->updaterules_end = solv->nrules;
2677 * now add all job rules
2680 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
2682 solv->jobrules = solv->nrules;
2683 for (i = 0; i < job->count; i += 2)
2685 oldnrules = solv->nrules;
2687 how = job->elements[i];
2688 what = job->elements[i + 1];
2689 weak = how & SOLVER_WEAK;
2690 select = how & SOLVER_SELECTMASK;
2691 switch (how & SOLVER_JOBMASK)
2693 case SOLVER_INSTALL:
2694 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2695 if (select == SOLVER_SOLVABLE)
2703 FOR_JOB_SELECT(p, pp, select, what)
2707 /* no candidate found, make this an impossible rule */
2708 queue_push(&q, -SYSTEMSOLVABLE);
2710 p = queue_shift(&q); /* get first candidate */
2711 d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q); /* internalize */
2713 solver_addrule(solv, p, d); /* add install rule */
2714 queue_push(&solv->ruletojob, i);
2716 queue_push(&solv->weakruleq, solv->nrules - 1);
2719 POOL_DEBUG(SAT_DEBUG_JOB, "job: %serase %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2720 if (select == SOLVER_SOLVABLE && solv->installed && pool->solvables[what].repo == solv->installed)
2722 /* special case for "erase a specific solvable": we also
2723 * erase all other solvables with that name, so that they
2724 * don't get picked up as replacement */
2725 name = pool->solvables[what].name;
2726 FOR_PROVIDES(p, pp, name)
2730 s = pool->solvables + p;
2731 if (s->name != name)
2733 /* keep other versions installed */
2734 if (s->repo == solv->installed)
2736 /* keep installcandidates of other jobs */
2737 if (MAPTST(&installcandidatemap, p))
2739 solver_addrule(solv, -p, 0); /* remove by Id */
2740 queue_push(&solv->ruletojob, i);
2742 queue_push(&solv->weakruleq, solv->nrules - 1);
2745 FOR_JOB_SELECT(p, pp, select, what)
2747 solver_addrule(solv, -p, 0);
2748 queue_push(&solv->ruletojob, i);
2750 queue_push(&solv->weakruleq, solv->nrules - 1);
2755 POOL_DEBUG(SAT_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2756 FOR_JOB_SELECT(p, pp, select, what)
2758 s = pool->solvables + p;
2759 if (!solv->installed || s->repo != solv->installed)
2761 if (!solv->updatemap.size)
2762 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
2763 MAPSET(&solv->updatemap, p - solv->installed->start);
2767 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2769 case SOLVER_WEAKENDEPS:
2770 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2771 if (select != SOLVER_SOLVABLE)
2773 s = pool->solvables + what;
2774 weaken_solvable_deps(solv, what);
2776 case SOLVER_NOOBSOLETES:
2777 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2780 POOL_DEBUG(SAT_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2781 FOR_JOB_SELECT(p, pp, select, what)
2783 s = pool->solvables + p;
2784 if (installed && s->repo == installed)
2785 solver_addrule(solv, p, 0);
2787 solver_addrule(solv, -p, 0);
2788 queue_push(&solv->ruletojob, i);
2790 queue_push(&solv->weakruleq, solv->nrules - 1);
2793 case SOLVER_DISTUPGRADE:
2794 POOL_DEBUG(SAT_DEBUG_JOB, "job: distupgrade repo #%d\n", what);
2797 POOL_DEBUG(SAT_DEBUG_JOB, "job: unknown job\n");
2805 IF_POOLDEBUG (SAT_DEBUG_JOB)
2808 if (solv->nrules == oldnrules)
2809 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created\n");
2810 for (j = oldnrules; j < solv->nrules; j++)
2812 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
2813 solver_printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
2817 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
2818 solv->jobrules_end = solv->nrules;
2820 /* now create infarch and dup rules */
2821 if (!solv->noinfarchcheck)
2823 solver_addinfarchrules(solv, &addedmap);
2824 if (pool->obsoleteusescolors)
2826 /* currently doesn't work well with infarch rules, so make
2828 for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
2829 queue_push(&solv->weakruleq, i);
2833 solv->infarchrules = solv->infarchrules_end = solv->nrules;
2837 solver_addduprules(solv, &addedmap);
2838 solver_freedupmaps(solv); /* no longer needed */
2841 solv->duprules = solv->duprules_end = solv->nrules;
2845 extern void addchoicerules(Solver *solv);
2846 addchoicerules(solv);
2849 solv->choicerules = solv->choicerules_end = solv->nrules;
2851 /* all rules created
2852 * --------------------------------------------------------------
2853 * prepare for solving
2856 /* free unneeded memory */
2857 map_free(&addedmap);
2858 map_free(&installcandidatemap);
2861 POOL_DEBUG(SAT_DEBUG_STATS, "%d rpm rules, %d job rules, %d infarch rules, %d dup rules, %d choice rules\n", solv->rpmrules_end - 1, solv->jobrules_end - solv->jobrules, solv->infarchrules_end - solv->infarchrules, solv->duprules_end - solv->duprules, solv->choicerules_end - solv->choicerules);
2863 /* create weak map */
2864 map_init(&solv->weakrulemap, solv->nrules);
2865 for (i = 0; i < solv->weakruleq.count; i++)
2867 p = solv->weakruleq.elements[i];
2868 MAPSET(&solv->weakrulemap, p);
2871 /* all new rules are learnt after this point */
2872 solv->learntrules = solv->nrules;
2874 /* create watches chains */
2877 /* create assertion index. it is only used to speed up
2878 * makeruledecsions() a bit */
2879 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
2880 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
2881 queue_push(&solv->ruleassertions, i);
2883 /* disable update rules that conflict with our job */
2884 solver_disablepolicyrules(solv);
2886 /* make decisions based on job/update assertions */
2887 makeruledecisions(solv);
2888 POOL_DEBUG(SAT_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
2891 * ********************************************
2893 * ********************************************
2896 now = sat_timems(0);
2897 solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
2898 POOL_DEBUG(SAT_DEBUG_STATS, "solver took %d ms\n", sat_timems(now));
2901 * calculate recommended/suggested packages
2903 findrecommendedsuggested(solv);
2906 * prepare solution queue if there were problems
2908 solver_prepare_solutions(solv);
2911 * finally prepare transaction info
2913 transaction_calculate(&solv->trans, &solv->decisionq, &solv->noobsoletes);
2915 POOL_DEBUG(SAT_DEBUG_STATS, "final solver statistics: %d problems, %d learned rules, %d unsolvable\n", solv->problems.count / 2, solv->stats_learned, solv->stats_unsolvable);
2916 POOL_DEBUG(SAT_DEBUG_STATS, "solver_solve took %d ms\n", sat_timems(solve_start));
2919 /***********************************************************************/
2920 /* disk usage computations */
2922 /*-------------------------------------------------------------------
2924 * calculate DU changes
2928 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
2932 solver_create_state_maps(solv, &installedmap, 0);
2933 pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
2934 map_free(&installedmap);
2938 /*-------------------------------------------------------------------
2940 * calculate changes in install size
2944 solver_calc_installsizechange(Solver *solv)
2949 solver_create_state_maps(solv, &installedmap, 0);
2950 change = pool_calc_installsizechange(solv->pool, &installedmap);
2951 map_free(&installedmap);
2956 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
2959 solver_create_state_maps(solv, &installedmap, 0);
2960 pool_trivial_installable_noobsoletesmap(solv->pool, &installedmap, pkgs, res, solv->noobsoletes.size ? &solv->noobsoletes : 0);
2961 map_free(&installedmap);
2966 #define FIND_INVOLVED_DEBUG 0
2968 solver_find_involved(Solver *solv, Queue *installedq, Solvable *ts, Queue *q)
2970 Pool *pool = solv->pool;
2975 Queue installedq_internal;
2976 Id tp, ip, p, pp, req, *reqp, sup, *supp;
2979 tp = ts - pool->solvables;
2981 queue_init(&installedq_internal);
2982 map_init(&im, pool->nsolvables);
2983 map_init(&installedm, pool->nsolvables);
2987 installedq = &installedq_internal;
2988 if (solv->installed)
2990 for (ip = solv->installed->start; ip < solv->installed->end; ip++)
2992 s = pool->solvables + ip;
2993 if (s->repo != solv->installed)
2995 queue_push(installedq, ip);
2999 for (i = 0; i < installedq->count; i++)
3001 ip = installedq->elements[i];
3002 MAPSET(&installedm, ip);
3006 queue_push(&iq, ts - pool->solvables);
3009 ip = queue_shift(&iq);
3010 if (!MAPTST(&im, ip))
3012 if (!MAPTST(&installedm, ip))
3015 s = pool->solvables + ip;
3016 #if FIND_INVOLVED_DEBUG
3017 printf("hello %s\n", solvable2str(pool, s));
3021 reqp = s->repo->idarraydata + s->requires;
3022 while ((req = *reqp++) != 0)
3024 if (req == SOLVABLE_PREREQMARKER)
3026 /* count number of installed packages that match */
3028 FOR_PROVIDES(p, pp, req)
3029 if (MAPTST(&installedm, p))
3033 FOR_PROVIDES(p, pp, req)
3037 #if FIND_INVOLVED_DEBUG
3038 printf("%s requires %s\n", solvid2str(pool, ip), solvid2str(pool, p));
3047 reqp = s->repo->idarraydata + s->recommends;
3048 while ((req = *reqp++) != 0)
3051 FOR_PROVIDES(p, pp, req)
3052 if (MAPTST(&installedm, p))
3056 FOR_PROVIDES(p, pp, req)
3060 #if FIND_INVOLVED_DEBUG
3061 printf("%s recommends %s\n", solvid2str(pool, ip), solvid2str(pool, p));
3070 /* supplements pass */
3071 for (i = 0; i < installedq->count; i++)
3073 ip = installedq->elements[i];
3074 s = pool->solvables + ip;
3075 if (!s->supplements)
3077 if (!MAPTST(&im, ip))
3079 supp = s->repo->idarraydata + s->supplements;
3080 while ((sup = *supp++) != 0)
3081 if (!dep_possible(solv, sup, &im) && dep_possible(solv, sup, &installedm))
3083 /* no longer supplemented, also erase */
3086 #if FIND_INVOLVED_DEBUG
3087 printf("%s supplemented\n", solvid2str(pool, ip));
3089 queue_push(&iq, ip);
3095 for (i = 0; i < installedq->count; i++)
3097 ip = installedq->elements[i];
3098 if (MAPTST(&im, ip))
3099 queue_push(&iq, ip);
3104 ip = queue_shift(&iq);
3105 if (!MAPTST(&installedm, ip))
3107 s = pool->solvables + ip;
3108 #if FIND_INVOLVED_DEBUG
3109 printf("bye %s\n", solvable2str(pool, s));
3113 reqp = s->repo->idarraydata + s->requires;
3114 while ((req = *reqp++) != 0)
3116 FOR_PROVIDES(p, pp, req)
3118 if (!MAPTST(&im, p))
3122 #if FIND_INVOLVED_DEBUG
3123 printf("%s requires %s\n", solvid2str(pool, ip), solvid2str(pool, p));
3133 reqp = s->repo->idarraydata + s->recommends;
3134 while ((req = *reqp++) != 0)
3136 FOR_PROVIDES(p, pp, req)
3138 if (!MAPTST(&im, p))
3142 #if FIND_INVOLVED_DEBUG
3143 printf("%s recommends %s\n", solvid2str(pool, ip), solvid2str(pool, p));
3153 /* supplements pass */
3154 for (i = 0; i < installedq->count; i++)
3156 ip = installedq->elements[i];
3159 s = pool->solvables + ip;
3160 if (!s->supplements)
3162 if (MAPTST(&im, ip))
3164 supp = s->repo->idarraydata + s->supplements;
3165 while ((sup = *supp++) != 0)
3166 if (dep_possible(solv, sup, &im))
3170 #if FIND_INVOLVED_DEBUG
3171 printf("%s supplemented\n", solvid2str(pool, ip));
3174 queue_push(&iq, ip);
3182 /* convert map into result */
3183 for (i = 0; i < installedq->count; i++)
3185 ip = installedq->elements[i];
3186 if (MAPTST(&im, ip))
3188 if (ip == ts - pool->solvables)
3193 map_free(&installedm);
3194 queue_free(&installedq_internal);