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
21 #include "solver_private.h"
26 #include "solverdebug.h"
28 #define RULES_BLOCK 63
30 /********************************************************************
32 * dependency check helpers
36 /*-------------------------------------------------------------------
37 * handle split provides
41 solver_splitprovides(Solver *solv, Id dep)
43 Pool *pool = solv->pool;
48 if (!solv->dosplitprovides || !solv->installed)
52 rd = GETRELDEP(pool, dep);
53 if (rd->flags != REL_WITH)
55 FOR_PROVIDES(p, pp, dep)
57 s = pool->solvables + p;
58 if (s->repo == solv->installed && s->name == rd->name)
65 /*-------------------------------------------------------------------
66 * solver_dep_installed
70 solver_dep_installed(Solver *solv, Id dep)
73 Pool *pool = solv->pool;
78 Reldep *rd = GETRELDEP(pool, dep);
79 if (rd->flags == REL_AND)
81 if (!solver_dep_installed(solv, rd->name))
83 return solver_dep_installed(solv, rd->evr);
85 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
86 return solver_dep_installed(solv, rd->evr);
88 FOR_PROVIDES(p, pp, dep)
90 if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
99 /************************************************************************/
102 * make assertion rules into decisions
104 * Go through rules and add direct assertions to the decisionqueue.
105 * If we find a conflict, disable rules and add them to problem queue.
109 makeruledecisions(Solver *solv)
111 Pool *pool = solv->pool;
116 int record_proof = 1;
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 (SOLV_DEBUG_PROPAGATE)
149 Solvable *s = solv->pool->solvables + vv;
151 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", pool_solvable2str(solv->pool, s));
153 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing %s (assertion)\n", pool_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)
199 /* conflict with system solvable */
202 queue_push(&solv->problems, solv->learnt_pool.count);
203 queue_push(&solv->learnt_pool, ri);
204 queue_push(&solv->learnt_pool, 0);
207 queue_push(&solv->problems, 0);
208 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
209 if (ri >= solv->jobrules && ri < solv->jobrules_end)
210 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
213 queue_push(&solv->problems, v);
214 queue_push(&solv->problems, 0);
215 solver_disableproblem(solv, v);
219 assert(solv->decisionq_why.elements[i] > 0);
222 * conflict with an rpm rule ?
225 if (solv->decisionq_why.elements[i] < solv->rpmrules_end)
227 /* conflict with rpm rule assertion */
230 queue_push(&solv->problems, solv->learnt_pool.count);
231 queue_push(&solv->learnt_pool, ri);
232 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
233 queue_push(&solv->learnt_pool, 0);
236 queue_push(&solv->problems, 0);
237 assert(v > 0 || v == -SYSTEMSOLVABLE);
238 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
239 if (ri >= solv->jobrules && ri < solv->jobrules_end)
240 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
243 queue_push(&solv->problems, v);
244 queue_push(&solv->problems, 0);
245 solver_disableproblem(solv, v);
250 * conflict with another job or update/feature rule
256 queue_push(&solv->problems, solv->learnt_pool.count);
257 queue_push(&solv->learnt_pool, ri);
258 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
259 queue_push(&solv->learnt_pool, 0);
262 queue_push(&solv->problems, 0);
264 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflicting update/job assertions over literal %d\n", vv);
267 * push all of our rules (can only be feature or job rules)
268 * asserting this literal on the problem stack
271 for (i = solv->featurerules, rr = solv->rules + i; i < solv->learntrules; i++, rr++)
273 if (rr->d < 0 /* disabled */
274 || rr->w2) /* or no assertion */
276 if (rr->p != vv /* not affecting the literal */
279 if (MAPTST(&solv->weakrulemap, i)) /* weak: silently ignore */
282 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
284 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + i);
287 /* is is a job rule ? */
288 if (i >= solv->jobrules && i < solv->jobrules_end)
289 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
291 queue_push(&solv->problems, v);
292 solver_disableproblem(solv, v);
294 queue_push(&solv->problems, 0);
298 * (back up from decisions)
300 while (solv->decisionq.count > decisionstart)
302 v = solv->decisionq.elements[--solv->decisionq.count];
303 --solv->decisionq_why.count;
305 solv->decisionmap[vv] = 0;
307 ii = -1; /* restarts loop at 0 */
311 * phase 2: now do the weak assertions
313 for (ii = 0; ii < solv->ruleassertions.count; ii++)
315 ri = solv->ruleassertions.elements[ii];
316 r = solv->rules + ri;
317 if (r->d < 0 || r->w2) /* disabled or no assertion */
319 if (ri >= solv->learntrules || !MAPTST(&solv->weakrulemap, ri)) /* skip non-weak */
325 * (if not yet decided)
327 if (!solv->decisionmap[vv])
329 queue_push(&solv->decisionq, v);
330 queue_push(&solv->decisionq_why, r - solv->rules);
331 solv->decisionmap[vv] = v > 0 ? 1 : -1;
332 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
334 Solvable *s = solv->pool->solvables + vv;
336 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
338 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
343 * previously decided, sane ?
345 if (v > 0 && solv->decisionmap[vv] > 0)
347 if (v < 0 && solv->decisionmap[vv] < 0)
350 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
351 solver_printrule(solv, SOLV_DEBUG_UNSOLVABLE, r);
353 if (ri >= solv->jobrules && ri < solv->jobrules_end)
354 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
357 solver_disableproblem(solv, v);
359 solver_reenablepolicyrules(solv, -v);
364 /*-------------------------------------------------------------------
365 * enable/disable learnt rules
367 * we have enabled or disabled some of our rules. We now reenable all
368 * of our learnt rules except the ones that were learnt from rules that
372 enabledisablelearntrules(Solver *solv)
374 Pool *pool = solv->pool;
379 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
380 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
382 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
383 while ((why = *whyp++) != 0)
385 assert(why > 0 && why < i);
386 if (solv->rules[why].d < 0)
389 /* why != 0: we found a disabled rule, disable the learnt rule */
390 if (why && r->d >= 0)
392 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
394 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "disabling ");
395 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
397 solver_disablerule(solv, r);
399 else if (!why && r->d < 0)
401 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
403 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "re-enabling ");
404 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
406 solver_enablerule(solv, r);
412 /********************************************************************/
416 /*-------------------------------------------------------------------
419 * initial setup for all watches
423 makewatches(Solver *solv)
427 int nsolvables = solv->pool->nsolvables;
429 solv_free(solv->watches);
430 /* lower half for removals, upper half for installs */
431 solv->watches = solv_calloc(2 * nsolvables, sizeof(Id));
433 /* do it reverse so rpm rules get triggered first (XXX: obsolete?) */
434 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
436 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
439 if (!r->w2) /* assertions do not need watches */
442 /* see addwatches_rule(solv, r) */
443 r->n1 = solv->watches[nsolvables + r->w1];
444 solv->watches[nsolvables + r->w1] = r - solv->rules;
446 r->n2 = solv->watches[nsolvables + r->w2];
447 solv->watches[nsolvables + r->w2] = r - solv->rules;
452 /*-------------------------------------------------------------------
454 * add watches (for a new learned rule)
455 * sets up watches for a single rule
457 * see also makewatches() above.
461 addwatches_rule(Solver *solv, Rule *r)
463 int nsolvables = solv->pool->nsolvables;
465 r->n1 = solv->watches[nsolvables + r->w1];
466 solv->watches[nsolvables + r->w1] = r - solv->rules;
468 r->n2 = solv->watches[nsolvables + r->w2];
469 solv->watches[nsolvables + r->w2] = r - solv->rules;
473 /********************************************************************/
479 /* shortcuts to check if a literal (positive or negative) assignment
480 * evaluates to 'true' or 'false'
482 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
483 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
484 #define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
486 /*-------------------------------------------------------------------
490 * make decision and propagate to all rules
492 * Evaluate each term affected by the decision (linked through watches)
493 * If we find unit rules we make new decisions based on them
495 * Everything's fixed there, it's just finding rules that are
498 * return : 0 = everything is OK
499 * rule = conflict found in this rule
503 propagate(Solver *solv, int level)
505 Pool *pool = solv->pool;
506 Id *rp, *next_rp; /* rule pointer, next rule pointer in linked list */
508 Id p, pkg, other_watch;
510 Id *decisionmap = solv->decisionmap;
512 Id *watches = solv->watches + pool->nsolvables; /* place ptr in middle */
514 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate -----\n");
516 /* foreach non-propagated decision */
517 while (solv->propagate_index < solv->decisionq.count)
520 * 'pkg' was just decided
521 * negate because our watches trigger if literal goes FALSE
523 pkg = -solv->decisionq.elements[solv->propagate_index++];
525 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
527 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
528 solver_printruleelement(solv, SOLV_DEBUG_PROPAGATE, 0, -pkg);
531 /* foreach rule where 'pkg' is now FALSE */
532 for (rp = watches + pkg; *rp; rp = next_rp)
534 r = solv->rules + *rp;
537 /* rule is disabled, goto next */
545 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
547 POOL_DEBUG(SOLV_DEBUG_PROPAGATE," watch triggered ");
548 solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
551 /* 'pkg' was just decided (was set to FALSE)
553 * now find other literal watch, check clause
554 * and advance on linked list
568 * This term is already true (through the other literal)
569 * so we have nothing to do
571 if (DECISIONMAP_TRUE(other_watch))
575 * The other literal is FALSE or UNDEF
581 /* Not a binary clause, try to move our watch.
583 * Go over all literals and find one that is
587 * (TRUE is also ok, in that case the rule is fulfilled)
589 if (r->p /* we have a 'p' */
590 && r->p != other_watch /* which is not watched */
591 && !DECISIONMAP_FALSE(r->p)) /* and not FALSE */
595 else /* go find a 'd' to make 'true' */
598 we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
600 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
602 if (p != other_watch /* which is not watched */
603 && !DECISIONMAP_FALSE(p)) /* and not FALSE */
611 * if we found some p that is UNDEF or TRUE, move
614 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
617 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, p));
619 POOL_DEBUG(SOLV_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, -p));
635 watches[p] = r - solv->rules;
638 /* search failed, thus all unwatched literals are FALSE */
643 * unit clause found, set literal other_watch to TRUE
646 if (DECISIONMAP_FALSE(other_watch)) /* check if literal is FALSE */
647 return r; /* eek, a conflict! */
649 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
651 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " unit ");
652 solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
656 decisionmap[other_watch] = level; /* install! */
658 decisionmap[-other_watch] = -level; /* remove! */
660 queue_push(&solv->decisionq, other_watch);
661 queue_push(&solv->decisionq_why, r - solv->rules);
663 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
666 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> decided to install %s\n", pool_solvid2str(pool, other_watch));
668 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> decided to conflict %s\n", pool_solvid2str(pool, -other_watch));
671 } /* foreach rule involving 'pkg' */
673 } /* while we have non-decided decisions */
675 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate end-----\n");
677 return 0; /* all is well */
681 /********************************************************************/
684 /*-------------------------------------------------------------------
691 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
693 Pool *pool = solv->pool;
697 Map seen; /* global? */
698 Id d, v, vv, *dp, why;
700 int num = 0, l1num = 0;
701 int learnt_why = solv->learnt_pool.count;
702 Id *decisionmap = solv->decisionmap;
704 queue_init_buffer(&r, r_buf, sizeof(r_buf)/sizeof(*r_buf));
706 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
707 map_init(&seen, pool->nsolvables);
708 idx = solv->decisionq.count;
711 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
712 solver_printruleclass(solv, SOLV_DEBUG_ANALYZE, c);
713 queue_push(&solv->learnt_pool, c - solv->rules);
714 d = c->d < 0 ? -c->d - 1 : c->d;
715 dp = d ? pool->whatprovidesdata + d : 0;
716 /* go through all literals of the rule */
728 if (DECISIONMAP_TRUE(v)) /* the one true literal */
731 if (MAPTST(&seen, vv))
733 l = solv->decisionmap[vv];
736 MAPSET(&seen, vv); /* mark that we also need to look at this literal */
738 l1num++; /* need to do this one in level1 pass */
740 num++; /* need to do this one as well */
743 queue_push(&r, v); /* not level1 or conflict level, add to new rule */
749 if (!num && !--l1num)
750 break; /* all level 1 literals done */
752 /* find the next literal to investigate */
753 /* (as num + l1num > 0, we know that we'll always find one) */
757 v = solv->decisionq.elements[--idx];
759 if (MAPTST(&seen, vv))
764 if (num && --num == 0)
766 *pr = -v; /* so that v doesn't get lost */
769 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
770 /* clear non-l1 bits from seen map */
771 for (i = 0; i < r.count; i++)
774 MAPCLR(&seen, v > 0 ? v : -v);
776 /* only level 1 marks left in seen map */
777 l1num++; /* as l1retry decrements it */
781 why = solv->decisionq_why.elements[idx];
782 if (why <= 0) /* just in case, maybe for SYSTEMSOLVABLE */
784 c = solv->rules + why;
790 else if (r.count == 1 && r.elements[0] < 0)
793 *dr = pool_queuetowhatprovides(pool, &r);
794 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
796 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
797 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, *pr);
798 for (i = 0; i < r.count; i++)
799 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, r.elements[i]);
801 /* push end marker on learnt reasons stack */
802 queue_push(&solv->learnt_pool, 0);
806 solv->stats_learned++;
811 /*-------------------------------------------------------------------
815 * reset the solver decisions to right after the rpm rules.
816 * called after rules have been enabled/disabled
820 solver_reset(Solver *solv)
822 Pool *pool = solv->pool;
826 /* rewind all decisions */
827 for (i = solv->decisionq.count - 1; i >= 0; i--)
829 v = solv->decisionq.elements[i];
830 solv->decisionmap[v > 0 ? v : -v] = 0;
832 solv->decisionq_why.count = 0;
833 solv->decisionq.count = 0;
834 solv->recommends_index = -1;
835 solv->propagate_index = 0;
836 solv->recommendations.count = 0;
837 solv->branches.count = 0;
839 /* adapt learnt rule status to new set of enabled/disabled rules */
840 enabledisablelearntrules(solv);
842 /* redo all assertion rule decisions */
843 makeruledecisions(solv);
844 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
848 /*-------------------------------------------------------------------
850 * analyze_unsolvable_rule
854 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp, Map *rseen)
856 Pool *pool = solv->pool;
858 Id why = r - solv->rules;
860 IF_POOLDEBUG (SOLV_DEBUG_UNSOLVABLE)
861 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, r);
862 if (solv->learntrules && why >= solv->learntrules)
864 if (MAPTST(rseen, why - solv->learntrules))
866 MAPSET(rseen, why - solv->learntrules);
867 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
868 if (solv->learnt_pool.elements[i] > 0)
869 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp, rseen);
872 if (MAPTST(&solv->weakrulemap, why))
873 if (!*lastweakp || why > *lastweakp)
875 /* do not add rpm rules to problem */
876 if (why < solv->rpmrules_end)
878 /* turn rule into problem */
879 if (why >= solv->jobrules && why < solv->jobrules_end)
880 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
881 /* normalize dup/infarch rules */
882 if (why > solv->infarchrules && why < solv->infarchrules_end)
884 Id name = pool->solvables[-solv->rules[why].p].name;
885 while (why > solv->infarchrules && pool->solvables[-solv->rules[why - 1].p].name == name)
888 if (why > solv->duprules && why < solv->duprules_end)
890 Id name = pool->solvables[-solv->rules[why].p].name;
891 while (why > solv->duprules && pool->solvables[-solv->rules[why - 1].p].name == name)
895 /* return if problem already countains our rule */
896 if (solv->problems.count)
898 for (i = solv->problems.count - 1; i >= 0; i--)
899 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
901 else if (solv->problems.elements[i] == why)
904 queue_push(&solv->problems, why);
908 /*-------------------------------------------------------------------
912 * return: 1 - disabled some rules, try again
917 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
919 Pool *pool = solv->pool;
921 Map seen; /* global to speed things up? */
923 Id d, v, vv, *dp, why;
925 Id *decisionmap = solv->decisionmap;
927 int oldlearntpoolcount;
929 int record_proof = 1;
931 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
932 solv->stats_unsolvable++;
933 oldproblemcount = solv->problems.count;
934 oldlearntpoolcount = solv->learnt_pool.count;
936 /* make room for proof index */
937 /* must update it later, as analyze_unsolvable_rule would confuse
938 * it with a rule index if we put the real value in already */
939 queue_push(&solv->problems, 0);
942 map_init(&seen, pool->nsolvables);
943 map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
945 queue_push(&solv->learnt_pool, r - solv->rules);
947 analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
948 d = r->d < 0 ? -r->d - 1 : r->d;
949 dp = d ? pool->whatprovidesdata + d : 0;
960 if (DECISIONMAP_TRUE(v)) /* the one true literal */
963 l = solv->decisionmap[vv];
968 idx = solv->decisionq.count;
971 v = solv->decisionq.elements[--idx];
973 if (!MAPTST(&seen, vv))
975 why = solv->decisionq_why.elements[idx];
978 queue_push(&solv->learnt_pool, why);
979 r = solv->rules + why;
980 analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
981 d = r->d < 0 ? -r->d - 1 : r->d;
982 dp = d ? pool->whatprovidesdata + d : 0;
993 if (DECISIONMAP_TRUE(v)) /* the one true literal */
996 l = solv->decisionmap[vv];
1004 queue_push(&solv->problems, 0); /* mark end of this problem */
1009 /* disable last weak rule */
1010 solv->problems.count = oldproblemcount;
1011 solv->learnt_pool.count = oldlearntpoolcount;
1012 if (lastweak >= solv->jobrules && lastweak < solv->jobrules_end)
1013 v = -(solv->ruletojob.elements[lastweak - solv->jobrules] + 1);
1016 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "disabling ");
1017 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + lastweak);
1018 if (lastweak >= solv->choicerules && lastweak < solv->choicerules_end)
1019 solver_disablechoicerules(solv, solv->rules + lastweak);
1020 solver_disableproblem(solv, v);
1022 solver_reenablepolicyrules(solv, -v);
1030 queue_push(&solv->learnt_pool, 0);
1031 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1036 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1037 solver_disableproblem(solv, solv->problems.elements[i]);
1038 /* XXX: might want to enable all weak rules again */
1042 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1047 /********************************************************************/
1048 /* Decision revert */
1050 /*-------------------------------------------------------------------
1053 * revert decisionq to a level
1057 revert(Solver *solv, int level)
1059 Pool *pool = solv->pool;
1061 while (solv->decisionq.count)
1063 v = solv->decisionq.elements[solv->decisionq.count - 1];
1064 vv = v > 0 ? v : -v;
1065 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1067 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1068 if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
1069 solv->recommendations.count--;
1070 solv->decisionmap[vv] = 0;
1071 solv->decisionq.count--;
1072 solv->decisionq_why.count--;
1073 solv->propagate_index = solv->decisionq.count;
1075 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1077 solv->branches.count--;
1078 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1079 solv->branches.count--;
1081 solv->recommends_index = -1;
1085 /*-------------------------------------------------------------------
1087 * watch2onhighest - put watch2 on literal with highest level
1091 watch2onhighest(Solver *solv, Rule *r)
1096 d = r->d < 0 ? -r->d - 1 : r->d;
1098 return; /* binary rule, both watches are set */
1099 dp = solv->pool->whatprovidesdata + d;
1100 while ((v = *dp++) != 0)
1102 l = solv->decisionmap[v < 0 ? -v : v];
1114 /*-------------------------------------------------------------------
1118 * add free decision (solvable to install) to decisionq
1119 * increase level and propagate decision
1120 * return if no conflict.
1122 * in conflict case, analyze conflict rule, add resulting
1123 * rule to learnt rule set, make decision from learnt
1124 * rule (always unit) and re-propagate.
1126 * returns the new solver level or 0 if unsolvable
1131 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules, Id ruleid)
1133 Pool *pool = solv->pool;
1138 assert(ruleid >= 0);
1143 solv->decisionmap[decision] = level;
1145 solv->decisionmap[-decision] = -level;
1146 queue_push(&solv->decisionq, decision);
1147 queue_push(&solv->decisionq_why, -ruleid); /* <= 0 -> free decision */
1151 r = propagate(solv, level);
1155 return analyze_unsolvable(solv, r, disablerules);
1156 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1157 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
1158 assert(l > 0 && l < level);
1159 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1161 revert(solv, level);
1162 r = solver_addrule(solv, p, d);
1164 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
1165 queue_push(&solv->learnt_why, why);
1168 /* at least 2 literals, needs watches */
1169 watch2onhighest(solv, r);
1170 addwatches_rule(solv, r);
1174 /* learnt rule is an assertion */
1175 queue_push(&solv->ruleassertions, r - solv->rules);
1177 /* the new rule is unit by design */
1178 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1179 queue_push(&solv->decisionq, p);
1180 queue_push(&solv->decisionq_why, r - solv->rules);
1181 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
1183 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "decision: ");
1184 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, p);
1185 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "new rule: ");
1186 solver_printrule(solv, SOLV_DEBUG_ANALYZE, r);
1193 /*-------------------------------------------------------------------
1195 * select and install
1197 * install best package from the queue. We add an extra package, inst, if
1198 * provided. See comment in weak install section.
1200 * returns the new solver level or 0 if unsolvable
1205 selectandinstall(Solver *solv, int level, Queue *dq, int disablerules, Id ruleid)
1207 Pool *pool = solv->pool;
1212 policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1215 /* XXX: didn't we already do that? */
1216 /* XXX: shouldn't we prefer installed packages? */
1217 /* XXX: move to policy.c? */
1218 /* choose the supplemented one */
1219 for (i = 0; i < dq->count; i++)
1220 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1222 dq->elements[0] = dq->elements[i];
1229 /* multiple candidates, open a branch */
1230 for (i = 1; i < dq->count; i++)
1231 queue_push(&solv->branches, dq->elements[i]);
1232 queue_push(&solv->branches, -level);
1234 p = dq->elements[0];
1236 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing %s\n", pool_solvid2str(pool, p));
1238 return setpropagatelearn(solv, level, p, disablerules, ruleid);
1242 /********************************************************************/
1243 /* Main solver interface */
1246 /*-------------------------------------------------------------------
1249 * create solver structure
1251 * pool: all available solvables
1252 * installed: installed Solvables
1255 * Upon solving, rules are created to flag the Solvables
1256 * of the 'installed' Repo as installed.
1260 solver_create(Pool *pool)
1263 solv = (Solver *)solv_calloc(1, sizeof(Solver));
1265 solv->installed = pool->installed;
1267 queue_init(&solv->ruletojob);
1268 queue_init(&solv->decisionq);
1269 queue_init(&solv->decisionq_why);
1270 queue_init(&solv->problems);
1271 queue_init(&solv->suggestions);
1272 queue_init(&solv->recommendations);
1273 queue_init(&solv->orphaned);
1274 queue_init(&solv->learnt_why);
1275 queue_init(&solv->learnt_pool);
1276 queue_init(&solv->branches);
1277 queue_init(&solv->weakruleq);
1278 queue_init(&solv->ruleassertions);
1280 queue_push(&solv->learnt_pool, 0); /* so that 0 does not describe a proof */
1282 map_init(&solv->recommendsmap, pool->nsolvables);
1283 map_init(&solv->suggestsmap, pool->nsolvables);
1284 map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1285 solv->recommends_index = 0;
1287 solv->decisionmap = (Id *)solv_calloc(pool->nsolvables, sizeof(Id));
1289 solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1290 memset(solv->rules, 0, sizeof(Rule));
1296 /*-------------------------------------------------------------------
1302 solver_free(Solver *solv)
1304 queue_free(&solv->job);
1305 queue_free(&solv->ruletojob);
1306 queue_free(&solv->decisionq);
1307 queue_free(&solv->decisionq_why);
1308 queue_free(&solv->learnt_why);
1309 queue_free(&solv->learnt_pool);
1310 queue_free(&solv->problems);
1311 queue_free(&solv->solutions);
1312 queue_free(&solv->suggestions);
1313 queue_free(&solv->recommendations);
1314 queue_free(&solv->orphaned);
1315 queue_free(&solv->branches);
1316 queue_free(&solv->weakruleq);
1317 queue_free(&solv->ruleassertions);
1319 map_free(&solv->recommendsmap);
1320 map_free(&solv->suggestsmap);
1321 map_free(&solv->noupdate);
1322 map_free(&solv->weakrulemap);
1323 map_free(&solv->noobsoletes);
1325 map_free(&solv->updatemap);
1326 map_free(&solv->fixmap);
1327 map_free(&solv->dupmap);
1328 map_free(&solv->dupinvolvedmap);
1329 map_free(&solv->droporphanedmap);
1330 map_free(&solv->cleandepsmap);
1332 solv_free(solv->decisionmap);
1333 solv_free(solv->rules);
1334 solv_free(solv->watches);
1335 solv_free(solv->obsoletes);
1336 solv_free(solv->obsoletes_data);
1337 solv_free(solv->multiversionupdaters);
1338 solv_free(solv->choicerules_ref);
1343 solver_get_flag(Solver *solv, int flag)
1347 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1348 return solv->allowdowngrade;
1349 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1350 return solv->allowarchchange;
1351 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1352 return solv->allowvendorchange;
1353 case SOLVER_FLAG_ALLOW_UNINSTALL:
1354 return solv->allowuninstall;
1355 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1356 return solv->noupdateprovide;
1357 case SOLVER_FLAG_SPLITPROVIDES:
1358 return solv->dosplitprovides;
1359 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1360 return solv->dontinstallrecommended;
1361 case SOLVER_FLAG_IGNORE_ALREADY_RECOMMENDED:
1362 return solv->ignorealreadyrecommended;
1363 case SOLVER_FLAG_NO_INFARCHCHECK:
1364 return solv->noinfarchcheck;
1372 solver_set_flag(Solver *solv, int flag, int value)
1374 int old = solver_get_flag(solv, flag);
1377 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1378 solv->allowdowngrade = value;
1380 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1381 solv->allowarchchange = value;
1383 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1384 solv->allowvendorchange = value;
1386 case SOLVER_FLAG_ALLOW_UNINSTALL:
1387 solv->allowuninstall = value;
1389 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1390 solv->noupdateprovide = value;
1392 case SOLVER_FLAG_SPLITPROVIDES:
1393 solv->dosplitprovides = value;
1395 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1396 solv->dontinstallrecommended = value;
1398 case SOLVER_FLAG_IGNORE_ALREADY_RECOMMENDED:
1399 solv->ignorealreadyrecommended = value;
1401 case SOLVER_FLAG_NO_INFARCHCHECK:
1402 solv->noinfarchcheck = value;
1411 /*-------------------------------------------------------------------
1415 * all rules have been set up, now actually run the solver
1420 solver_run_sat(Solver *solv, int disablerules, int doweak)
1422 Queue dq; /* local decisionqueue */
1423 Queue dqs; /* local decisionqueue for supplements */
1429 Pool *pool = solv->pool;
1431 int minimizationsteps;
1432 int installedpos = solv->installed ? solv->installed->start : 0;
1434 IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
1436 POOL_DEBUG (SOLV_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1437 for (i = 1; i < solv->nrules; i++)
1438 solver_printruleclass(solv, SOLV_DEBUG_RULE_CREATION, solv->rules + i);
1441 POOL_DEBUG(SOLV_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1443 /* start SAT algorithm */
1445 systemlevel = level + 1;
1446 POOL_DEBUG(SOLV_DEBUG_SOLVER, "solving...\n");
1452 * here's the main loop:
1453 * 1) propagate new decisions (only needed once)
1455 * 3) try to keep installed packages
1456 * 4) fulfill all unresolved rules
1457 * 5) install recommended packages
1458 * 6) minimalize solution if we had choices
1459 * if we encounter a problem, we rewind to a safe level and restart
1463 minimizationsteps = 0;
1467 * initial propagation of the assertions
1471 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1472 if ((r = propagate(solv, level)) != 0)
1474 if (analyze_unsolvable(solv, r, disablerules))
1477 break; /* unsolvable */
1482 * resolve jobs first
1484 if (level < systemlevel)
1486 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving job rules\n");
1487 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1490 if (r->d < 0) /* ignore disabled rules */
1493 FOR_RULELITERALS(l, dp, r)
1497 if (solv->decisionmap[-l] <= 0)
1502 if (solv->decisionmap[l] > 0)
1504 if (solv->decisionmap[l] == 0)
1510 /* prune to installed if not updating */
1511 if (dq.count > 1 && solv->installed && !solv->updatemap_all)
1514 for (j = k = 0; j < dq.count; j++)
1516 Solvable *s = pool->solvables + dq.elements[j];
1517 if (s->repo == solv->installed)
1519 dq.elements[k++] = dq.elements[j];
1520 if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
1522 k = 0; /* package wants to be updated, do not prune */
1531 level = selectandinstall(solv, level, &dq, disablerules, i);
1534 if (level <= olevel)
1538 break; /* unsolvable */
1539 systemlevel = level + 1;
1540 if (i < solv->jobrules_end)
1542 solv->decisioncnt_update = solv->decisionq.count;
1543 solv->decisioncnt_keep = solv->decisionq.count;
1547 * installed packages
1549 if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1551 Repo *installed = solv->installed;
1554 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving installed packages\n");
1555 /* we use two passes if we need to update packages
1556 * to create a better user experience */
1557 for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1559 int passlevel = level;
1561 solv->decisioncnt_keep = solv->decisionq.count;
1562 /* start with installedpos, the position that gave us problems last time */
1563 for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1568 if (i == installed->end)
1569 i = installed->start;
1570 s = pool->solvables + i;
1571 if (s->repo != installed)
1574 if (solv->decisionmap[i] > 0)
1576 if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1577 continue; /* updates first */
1578 r = solv->rules + solv->updaterules + (i - installed->start);
1580 if (!rr->p || rr->d < 0) /* disabled -> look at feature rule */
1581 rr -= solv->installed->end - solv->installed->start;
1582 if (!rr->p) /* identical to update rule? */
1585 continue; /* orpaned package */
1587 /* XXX: noupdate check is probably no longer needed, as all jobs should
1588 * already be satisfied */
1589 /* Actually we currently still need it because of erase jobs */
1590 /* if noupdate is set we do not look at update candidates */
1592 if (!MAPTST(&solv->noupdate, i - installed->start) && (solv->decisionmap[i] < 0 || solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, i - installed->start)) || rr->p != i))
1594 if (solv->noobsoletes.size && solv->multiversionupdaters
1595 && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1597 /* special multiversion handling, make sure best version is chosen */
1599 while ((p = pool->whatprovidesdata[d++]) != 0)
1600 if (solv->decisionmap[p] >= 0)
1602 policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1604 if (p != i && solv->decisionmap[p] == 0)
1606 rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1607 if (!rr->p) /* update rule == feature rule? */
1608 rr = rr - solv->featurerules + solv->updaterules;
1616 /* update to best package */
1617 FOR_RULELITERALS(p, dp, rr)
1619 if (solv->decisionmap[p] > 0)
1621 dq.count = 0; /* already fulfilled */
1624 if (!solv->decisionmap[p])
1629 /* install best version */
1633 level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1640 if (level <= olevel)
1642 if (level == 1 || level < passlevel)
1643 break; /* trouble */
1645 n = installed->start; /* redo all */
1651 /* if still undecided keep package */
1652 if (solv->decisionmap[i] == 0)
1655 if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
1657 POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
1658 level = setpropagatelearn(solv, level, -i, disablerules, 0);
1662 POOL_DEBUG(SOLV_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
1663 level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1667 if (level <= olevel)
1669 if (level == 1 || level < passlevel)
1670 break; /* trouble */
1672 n = installed->start; /* redo all */
1675 continue; /* retry with learnt rule */
1679 if (n < installed->end)
1681 installedpos = i; /* retry problem solvable next time */
1682 break; /* ran into trouble */
1684 installedpos = installed->start; /* reset installedpos */
1687 break; /* unsolvable */
1688 systemlevel = level + 1;
1690 continue; /* had trouble, retry */
1693 if (level < systemlevel)
1694 systemlevel = level;
1699 solv->decisioncnt_resolve = solv->decisionq.count;
1700 POOL_DEBUG(SOLV_DEBUG_POLICY, "deciding unresolved rules\n");
1701 for (i = 1, n = 1; n < solv->nrules; i++, n++)
1703 if (i == solv->nrules)
1705 r = solv->rules + i;
1706 if (r->d < 0) /* ignore disabled rules */
1711 /* binary or unary rule */
1712 /* need two positive undecided literals */
1713 if (r->p < 0 || r->w2 <= 0)
1715 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1717 queue_push(&dq, r->p);
1718 queue_push(&dq, r->w2);
1723 * all negative literals are installed
1724 * no positive literal is installed
1725 * i.e. the rule is not fulfilled and we
1726 * just need to decide on the positive literals
1730 if (solv->decisionmap[-r->p] <= 0)
1735 if (solv->decisionmap[r->p] > 0)
1737 if (solv->decisionmap[r->p] == 0)
1738 queue_push(&dq, r->p);
1740 dp = pool->whatprovidesdata + r->d;
1741 while ((p = *dp++) != 0)
1745 if (solv->decisionmap[-p] <= 0)
1750 if (solv->decisionmap[p] > 0)
1752 if (solv->decisionmap[p] == 0)
1759 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
1761 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "unfulfilled ");
1762 solver_printruleclass(solv, SOLV_DEBUG_PROPAGATE, r);
1764 /* dq.count < 2 cannot happen as this means that
1765 * the rule is unit */
1766 assert(dq.count > 1);
1769 level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
1771 break; /* unsolvable */
1772 if (level < systemlevel || level == 1)
1773 break; /* trouble */
1774 /* something changed, so look at all rules again */
1778 if (n != solv->nrules) /* ran into trouble? */
1781 break; /* unsolvable */
1782 continue; /* start over */
1785 /* at this point we have a consistent system. now do the extras... */
1787 solv->decisioncnt_weak = solv->decisionq.count;
1792 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended packages\n");
1793 queue_empty(&dq); /* recommended packages */
1794 queue_empty(&dqs); /* supplemented packages */
1795 for (i = 1; i < pool->nsolvables; i++)
1797 if (solv->decisionmap[i] < 0)
1799 if (solv->decisionmap[i] > 0)
1801 /* installed, check for recommends */
1802 Id *recp, rec, pp, p;
1803 s = pool->solvables + i;
1804 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
1806 /* XXX need to special case AND ? */
1809 recp = s->repo->idarraydata + s->recommends;
1810 while ((rec = *recp++) != 0)
1813 FOR_PROVIDES(p, pp, rec)
1815 if (solv->decisionmap[p] > 0)
1820 else if (solv->decisionmap[p] == 0)
1822 if (solv->dupmap_all && solv->installed && pool->solvables[p].repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))))
1824 queue_pushunique(&dq, p);
1832 s = pool->solvables + i;
1833 if (!s->supplements)
1835 if (!pool_installable(pool, s))
1837 if (!solver_is_supplementing(solv, s))
1839 if (solv->dupmap_all && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
1841 queue_push(&dqs, i);
1845 /* filter out all packages obsoleted by installed packages */
1846 /* this is no longer needed if we have reverse obsoletes */
1847 if ((dqs.count || dq.count) && solv->installed)
1850 Id obs, *obsp, po, ppo;
1852 map_init(&obsmap, pool->nsolvables);
1853 for (p = solv->installed->start; p < solv->installed->end; p++)
1855 s = pool->solvables + p;
1856 if (s->repo != solv->installed || !s->obsoletes)
1858 if (solv->decisionmap[p] <= 0)
1860 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
1862 obsp = s->repo->idarraydata + s->obsoletes;
1863 /* foreach obsoletes */
1864 while ((obs = *obsp++) != 0)
1865 FOR_PROVIDES(po, ppo, obs)
1866 MAPSET(&obsmap, po);
1868 for (i = j = 0; i < dqs.count; i++)
1869 if (!MAPTST(&obsmap, dqs.elements[i]))
1870 dqs.elements[j++] = dqs.elements[i];
1872 for (i = j = 0; i < dq.count; i++)
1873 if (!MAPTST(&obsmap, dq.elements[i]))
1874 dq.elements[j++] = dq.elements[i];
1879 /* filter out all already supplemented packages if requested */
1880 if (solv->ignorealreadyrecommended && dqs.count)
1882 /* turn off all new packages */
1883 for (i = 0; i < solv->decisionq.count; i++)
1885 p = solv->decisionq.elements[i];
1888 s = pool->solvables + p;
1889 if (s->repo && s->repo != solv->installed)
1890 solv->decisionmap[p] = -solv->decisionmap[p];
1892 /* filter out old supplements */
1893 for (i = j = 0; i < dqs.count; i++)
1895 p = dqs.elements[i];
1896 s = pool->solvables + p;
1897 if (!s->supplements)
1899 if (!solver_is_supplementing(solv, s))
1900 dqs.elements[j++] = p;
1903 /* undo turning off */
1904 for (i = 0; i < solv->decisionq.count; i++)
1906 p = solv->decisionq.elements[i];
1909 s = pool->solvables + p;
1910 if (s->repo && s->repo != solv->installed)
1911 solv->decisionmap[p] = -solv->decisionmap[p];
1915 /* multiversion doesn't mix well with supplements.
1916 * filter supplemented packages where we already decided
1917 * to install a different version (see bnc#501088) */
1918 if (dqs.count && solv->noobsoletes.size)
1920 for (i = j = 0; i < dqs.count; i++)
1922 p = dqs.elements[i];
1923 if (MAPTST(&solv->noobsoletes, p))
1926 s = pool->solvables + p;
1927 FOR_PROVIDES(p2, pp2, s->name)
1928 if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
1931 continue; /* ignore this package */
1933 dqs.elements[j++] = p;
1938 /* make dq contain both recommended and supplemented pkgs */
1941 for (i = 0; i < dqs.count; i++)
1942 queue_pushunique(&dq, dqs.elements[i]);
1948 int decisioncount = solv->decisionq.count;
1952 /* simple case, just one package. no need to choose */
1955 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
1957 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
1958 queue_push(&solv->recommendations, p);
1959 level = setpropagatelearn(solv, level, p, 0, 0);
1962 continue; /* back to main loop */
1965 /* filter packages, this gives us the best versions */
1966 policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
1968 /* create map of result */
1969 map_init(&dqmap, pool->nsolvables);
1970 for (i = 0; i < dq.count; i++)
1971 MAPSET(&dqmap, dq.elements[i]);
1973 /* install all supplemented packages */
1974 for (i = 0; i < dqs.count; i++)
1976 p = dqs.elements[i];
1977 if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
1979 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
1980 queue_push(&solv->recommendations, p);
1982 level = setpropagatelearn(solv, level, p, 0, 0);
1983 if (level <= olevel)
1986 if (i < dqs.count || solv->decisionq.count < decisioncount)
1994 /* install all recommended packages */
1995 /* more work as we want to created branches if multiple
1996 * choices are valid */
1997 for (i = 0; i < decisioncount; i++)
2000 p = solv->decisionq.elements[i];
2003 s = pool->solvables + p;
2004 if (!s->repo || (solv->ignorealreadyrecommended && s->repo == solv->installed))
2008 recp = s->repo->idarraydata + s->recommends;
2009 while ((rec = *recp++) != 0)
2012 FOR_PROVIDES(p, pp, rec)
2014 if (solv->decisionmap[p] > 0)
2019 else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
2020 queue_pushunique(&dq, p);
2026 /* multiple candidates, open a branch */
2027 for (i = 1; i < dq.count; i++)
2028 queue_push(&solv->branches, dq.elements[i]);
2029 queue_push(&solv->branches, -level);
2032 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2033 queue_push(&solv->recommendations, p);
2035 level = setpropagatelearn(solv, level, p, 0, 0);
2036 if (level <= olevel || solv->decisionq.count < decisioncount)
2037 break; /* we had to revert some decisions */
2040 break; /* had a problem above, quit loop */
2045 continue; /* back to main loop so that all deps are checked */
2049 solv->decisioncnt_orphan = solv->decisionq.count;
2050 if (solv->dupmap_all && solv->installed)
2052 int installedone = 0;
2054 /* let's see if we can install some unsupported package */
2055 POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding orphaned packages\n");
2056 for (i = 0; i < solv->orphaned.count; i++)
2058 p = solv->orphaned.elements[i];
2059 if (solv->decisionmap[p])
2060 continue; /* already decided */
2062 if (solv->droporphanedmap_all)
2064 if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
2066 POOL_DEBUG(SOLV_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
2067 level = setpropagatelearn(solv, level, p, 0, 0);
2074 if (installedone || i < solv->orphaned.count)
2078 continue; /* back to main loop */
2080 for (i = 0; i < solv->orphaned.count; i++)
2082 p = solv->orphaned.elements[i];
2083 if (solv->decisionmap[p])
2084 continue; /* already decided */
2085 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2087 level = setpropagatelearn(solv, level, -p, 0, 0);
2091 if (i < solv->orphaned.count)
2095 continue; /* back to main loop */
2099 if (solv->solution_callback)
2101 solv->solution_callback(solv, solv->solution_callback_data);
2102 if (solv->branches.count)
2104 int i = solv->branches.count - 1;
2105 int l = -solv->branches.elements[i];
2109 if (solv->branches.elements[i - 1] < 0)
2111 p = solv->branches.elements[i];
2112 POOL_DEBUG(SOLV_DEBUG_SOLVER, "branching with %s\n", pool_solvid2str(pool, p));
2114 for (j = i + 1; j < solv->branches.count; j++)
2115 queue_push(&dq, solv->branches.elements[j]);
2116 solv->branches.count = i;
2118 revert(solv, level);
2120 for (j = 0; j < dq.count; j++)
2121 queue_push(&solv->branches, dq.elements[j]);
2123 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2125 level = setpropagatelearn(solv, level, p, disablerules, why);
2130 /* all branches done, we're finally finished */
2134 /* auto-minimization step */
2135 if (solv->branches.count)
2137 int l = 0, lasti = -1, lastl = -1;
2141 for (i = solv->branches.count - 1; i >= 0; i--)
2143 p = solv->branches.elements[i];
2146 else if (p > 0 && solv->decisionmap[p] > l + 1)
2154 /* kill old solvable so that we do not loop */
2155 p = solv->branches.elements[lasti];
2156 solv->branches.elements[lasti] = 0;
2157 POOL_DEBUG(SOLV_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, pool_solvid2str(pool, p));
2158 minimizationsteps++;
2161 revert(solv, level);
2162 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2165 level = setpropagatelearn(solv, level, p, disablerules, why);
2168 continue; /* back to main loop */
2171 /* no minimization found, we're finally finished! */
2175 POOL_DEBUG(SOLV_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2177 POOL_DEBUG(SOLV_DEBUG_STATS, "done solving.\n\n");
2183 solv->decisioncnt_update = solv->decisionq.count;
2184 solv->decisioncnt_keep = solv->decisionq.count;
2185 solv->decisioncnt_resolve = solv->decisionq.count;
2186 solv->decisioncnt_weak = solv->decisionq.count;
2187 solv->decisioncnt_orphan = solv->decisionq.count;
2190 solver_printdecisionq(solv, SOLV_DEBUG_RESULT);
2195 /*-------------------------------------------------------------------
2197 * remove disabled conflicts
2199 * purpose: update the decisionmap after some rules were disabled.
2200 * this is used to calculate the suggested/recommended package list.
2201 * Also returns a "removed" list to undo the discisionmap changes.
2205 removedisabledconflicts(Solver *solv, Queue *removed)
2207 Pool *pool = solv->pool;
2212 Id *decisionmap = solv->decisionmap;
2214 queue_empty(removed);
2215 for (i = 0; i < solv->decisionq.count; i++)
2217 p = solv->decisionq.elements[i];
2219 continue; /* conflicts only, please */
2220 why = solv->decisionq_why.elements[i];
2223 /* no rule involved, must be a orphan package drop */
2226 /* we never do conflicts on free decisions, so there
2227 * must have been an unit rule */
2229 r = solv->rules + why;
2230 if (r->d < 0 && decisionmap[-p])
2232 /* rule is now disabled, remove from decisionmap */
2233 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2234 queue_push(removed, -p);
2235 queue_push(removed, decisionmap[-p]);
2236 decisionmap[-p] = 0;
2239 if (!removed->count)
2241 /* we removed some confliced packages. some of them might still
2242 * be in conflict, so search for unit rules and re-conflict */
2244 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2246 if (i == solv->nrules)
2249 r = solv->rules + i;
2255 if (r->p < 0 && !decisionmap[-r->p])
2261 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2263 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2268 if (r->p < 0 && decisionmap[-r->p] == 0)
2270 if (new || DECISIONMAP_FALSE(r->p))
2272 dp = pool->whatprovidesdata + r->d;
2273 while ((p = *dp++) != 0)
2275 if (new && p == new)
2277 if (p < 0 && decisionmap[-p] == 0)
2286 else if (!DECISIONMAP_FALSE(p))
2296 POOL_DEBUG(SOLV_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2297 decisionmap[-new] = -1;
2299 n = 0; /* redo all rules */
2305 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2308 for (i = 0; i < removed->count; i += 2)
2309 solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2313 /*-------------------------------------------------------------------
2315 * weaken solvable dependencies
2319 weaken_solvable_deps(Solver *solv, Id p)
2324 for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2328 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2329 continue; /* conflict */
2330 queue_push(&solv->weakruleq, i);
2335 /********************************************************************/
2340 findrecommendedsuggested(Solver *solv)
2342 Pool *pool = solv->pool;
2343 Queue redoq, disabledq;
2349 map_init(&obsmap, pool->nsolvables);
2350 if (solv->installed)
2352 Id obs, *obsp, p, po, ppo;
2353 for (p = solv->installed->start; p < solv->installed->end; p++)
2355 s = pool->solvables + p;
2356 if (s->repo != solv->installed || !s->obsoletes)
2358 if (solv->decisionmap[p] <= 0)
2360 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
2362 obsp = s->repo->idarraydata + s->obsoletes;
2363 /* foreach obsoletes */
2364 while ((obs = *obsp++) != 0)
2365 FOR_PROVIDES(po, ppo, obs)
2366 MAPSET(&obsmap, po);
2371 queue_init(&disabledq);
2373 /* disable all erase jobs (including weak "keep uninstalled" rules) */
2374 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2376 if (r->d < 0) /* disabled ? */
2378 if (r->p >= 0) /* install job? */
2380 queue_push(&disabledq, i);
2381 solver_disablerule(solv, r);
2387 enabledisablelearntrules(solv);
2388 removedisabledconflicts(solv, &redoq);
2392 * find recommended packages
2395 /* if redoq.count == 0 we already found all recommended in the
2397 if (redoq.count || solv->dontinstallrecommended || !solv->dontshowinstalledrecommended || solv->ignorealreadyrecommended)
2399 Id rec, *recp, p, pp;
2401 /* create map of all recommened packages */
2402 solv->recommends_index = -1;
2403 MAPZERO(&solv->recommendsmap);
2404 for (i = 0; i < solv->decisionq.count; i++)
2406 p = solv->decisionq.elements[i];
2409 s = pool->solvables + p;
2412 recp = s->repo->idarraydata + s->recommends;
2413 while ((rec = *recp++) != 0)
2415 FOR_PROVIDES(p, pp, rec)
2416 if (solv->decisionmap[p] > 0)
2420 if (!solv->dontshowinstalledrecommended)
2422 FOR_PROVIDES(p, pp, rec)
2423 if (solv->decisionmap[p] > 0)
2424 MAPSET(&solv->recommendsmap, p);
2426 continue; /* p != 0: already fulfilled */
2428 FOR_PROVIDES(p, pp, rec)
2429 MAPSET(&solv->recommendsmap, p);
2433 for (i = 1; i < pool->nsolvables; i++)
2435 if (solv->decisionmap[i] < 0)
2437 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2439 if (MAPTST(&obsmap, i))
2441 s = pool->solvables + i;
2442 if (!MAPTST(&solv->recommendsmap, i))
2444 if (!s->supplements)
2446 if (!pool_installable(pool, s))
2448 if (!solver_is_supplementing(solv, s))
2451 if (solv->dontinstallrecommended)
2452 queue_push(&solv->recommendations, i);
2454 queue_pushunique(&solv->recommendations, i);
2456 /* we use MODE_SUGGEST here so that repo prio is ignored */
2457 policy_filter_unwanted(solv, &solv->recommendations, POLICY_MODE_SUGGEST);
2461 * find suggested packages
2466 Id sug, *sugp, p, pp;
2468 /* create map of all suggests that are still open */
2469 solv->recommends_index = -1;
2470 MAPZERO(&solv->suggestsmap);
2471 for (i = 0; i < solv->decisionq.count; i++)
2473 p = solv->decisionq.elements[i];
2476 s = pool->solvables + p;
2479 sugp = s->repo->idarraydata + s->suggests;
2480 while ((sug = *sugp++) != 0)
2482 FOR_PROVIDES(p, pp, sug)
2483 if (solv->decisionmap[p] > 0)
2487 if (!solv->dontshowinstalledrecommended)
2489 FOR_PROVIDES(p, pp, sug)
2490 if (solv->decisionmap[p] > 0)
2491 MAPSET(&solv->suggestsmap, p);
2493 continue; /* already fulfilled */
2495 FOR_PROVIDES(p, pp, sug)
2496 MAPSET(&solv->suggestsmap, p);
2500 for (i = 1; i < pool->nsolvables; i++)
2502 if (solv->decisionmap[i] < 0)
2504 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2506 if (MAPTST(&obsmap, i))
2508 s = pool->solvables + i;
2509 if (!MAPTST(&solv->suggestsmap, i))
2513 if (!pool_installable(pool, s))
2515 if (!solver_is_enhancing(solv, s))
2518 queue_push(&solv->suggestions, i);
2520 policy_filter_unwanted(solv, &solv->suggestions, POLICY_MODE_SUGGEST);
2523 /* undo removedisabledconflicts */
2525 undo_removedisabledconflicts(solv, &redoq);
2528 /* undo job rule disabling */
2529 for (i = 0; i < disabledq.count; i++)
2530 solver_enablerule(solv, solv->rules + disabledq.elements[i]);
2531 queue_free(&disabledq);
2536 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap)
2539 Id how, what, select;
2541 for (i = 0; i < job->count; i += 2)
2543 how = job->elements[i];
2544 if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
2546 what = job->elements[i + 1];
2547 select = how & SOLVER_SELECTMASK;
2548 if (!noobsmap->size)
2549 map_grow(noobsmap, pool->nsolvables);
2550 FOR_JOB_SELECT(p, pp, select, what)
2551 MAPSET(noobsmap, p);
2556 * add a rule created by a job, record job number and weak flag
2559 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
2561 solver_addrule(solv, p, d);
2562 queue_push(&solv->ruletojob, job);
2564 queue_push(&solv->weakruleq, solv->nrules - 1);
2574 solver_solve(Solver *solv, Queue *job)
2576 Pool *pool = solv->pool;
2577 Repo *installed = solv->installed;
2580 Map addedmap; /* '1' == have rpm-rules for solvable */
2581 Map installcandidatemap;
2582 Id how, what, select, name, weak, p, pp, d;
2586 int now, solve_start;
2589 solve_start = solv_timems(0);
2591 /* log solver options */
2592 POOL_DEBUG(SOLV_DEBUG_STATS, "solver started\n");
2593 POOL_DEBUG(SOLV_DEBUG_STATS, "fixsystem=%d updatesystem=%d dosplitprovides=%d, noupdateprovide=%d noinfarchcheck=%d\n", solv->fixsystem, solv->updatesystem, solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
2594 POOL_DEBUG(SOLV_DEBUG_STATS, "distupgrade=%d distupgrade_removeunsupported=%d\n", solv->distupgrade, solv->distupgrade_removeunsupported);
2595 POOL_DEBUG(SOLV_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allowarchchange, solv->allowvendorchange);
2596 POOL_DEBUG(SOLV_DEBUG_STATS, "promoteepoch=%d, allowselfconflicts=%d\n", pool->promoteepoch, pool->allowselfconflicts);
2597 POOL_DEBUG(SOLV_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2598 POOL_DEBUG(SOLV_DEBUG_STATS, "dontinstallrecommended=%d, ignorealreadyrecommended=%d, dontshowinstalledrecommended=%d\n", solv->dontinstallrecommended, solv->ignorealreadyrecommended, solv->dontshowinstalledrecommended);
2600 /* create whatprovides if not already there */
2601 if (!pool->whatprovides)
2602 pool_createwhatprovides(pool);
2604 /* create obsolete index */
2605 policy_create_obsolete_index(solv);
2608 queue_free(&solv->job);
2609 queue_init_clone(&solv->job, job);
2611 /* initialize with legacy values */
2612 solv->fixmap_all = solv->fixsystem;
2613 solv->updatemap_all = solv->updatesystem;
2614 solv->droporphanedmap_all = solv->distupgrade_removeunsupported;
2615 solv->dupmap_all = solv->distupgrade;
2618 * create basic rule set of all involved packages
2619 * use addedmap bitmap to make sure we don't create rules twice
2622 /* create noobsolete map if needed */
2623 solver_calculate_noobsmap(pool, job, &solv->noobsoletes);
2625 map_init(&addedmap, pool->nsolvables);
2626 MAPSET(&addedmap, SYSTEMSOLVABLE);
2628 map_init(&installcandidatemap, pool->nsolvables);
2631 now = solv_timems(0);
2633 * create rules for all package that could be involved with the solving
2634 * so called: rpm rules
2639 /* check for update/verify jobs as they need to be known early */
2640 for (i = 0; i < job->count; i += 2)
2642 how = job->elements[i];
2643 what = job->elements[i + 1];
2644 select = how & SOLVER_SELECTMASK;
2645 switch (how & SOLVER_JOBMASK)
2648 if (select == SOLVER_SOLVABLE_ALL)
2649 solv->fixmap_all = 1;
2650 FOR_JOB_SELECT(p, pp, select, what)
2652 s = pool->solvables + p;
2653 if (!solv->installed || s->repo != solv->installed)
2655 if (!solv->fixmap.size)
2656 map_grow(&solv->fixmap, solv->installed->end - solv->installed->start);
2657 MAPSET(&solv->fixmap, p - solv->installed->start);
2661 if (select == SOLVER_SOLVABLE_ALL)
2662 solv->updatemap_all = 1;
2663 FOR_JOB_SELECT(p, pp, select, what)
2665 s = pool->solvables + p;
2666 if (!solv->installed || s->repo != solv->installed)
2668 if (!solv->updatemap.size)
2669 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
2670 MAPSET(&solv->updatemap, p - solv->installed->start);
2678 oldnrules = solv->nrules;
2679 FOR_REPO_SOLVABLES(installed, p, s)
2680 solver_addrpmrulesforsolvable(solv, s, &addedmap);
2681 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
2682 oldnrules = solv->nrules;
2683 FOR_REPO_SOLVABLES(installed, p, s)
2684 solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
2685 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
2689 * create rules for all packages involved in the job
2690 * (to be installed or removed)
2693 oldnrules = solv->nrules;
2694 for (i = 0; i < job->count; i += 2)
2696 how = job->elements[i];
2697 what = job->elements[i + 1];
2698 select = how & SOLVER_SELECTMASK;
2700 switch (how & SOLVER_JOBMASK)
2702 case SOLVER_INSTALL:
2703 FOR_JOB_SELECT(p, pp, select, what)
2705 MAPSET(&installcandidatemap, p);
2706 solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
2709 case SOLVER_DISTUPGRADE:
2710 if (select == SOLVER_SOLVABLE_ALL)
2712 solv->dupmap_all = 1;
2713 solv->updatemap_all = 1;
2715 if (!solv->dupmap_all)
2722 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
2726 * add rules for suggests, enhances
2728 oldnrules = solv->nrules;
2729 solver_addrpmrulesforweak(solv, &addedmap);
2730 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
2733 * first pass done, we now have all the rpm rules we need.
2734 * unify existing rules before going over all job rules and
2736 * at this point the system is always solvable,
2737 * as an empty system (remove all packages) is a valid solution
2740 IF_POOLDEBUG (SOLV_DEBUG_STATS)
2742 int possible = 0, installable = 0;
2743 for (i = 1; i < pool->nsolvables; i++)
2745 if (pool_installable(pool, pool->solvables + i))
2747 if (MAPTST(&addedmap, i))
2750 POOL_DEBUG(SOLV_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
2753 solver_unifyrules(solv); /* remove duplicate rpm rules */
2754 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
2756 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule memory usage: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
2757 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule creation took %d ms\n", solv_timems(now));
2759 /* create dup maps if needed. We need the maps early to create our
2762 solver_createdupmaps(solv);
2765 * create feature rules
2767 * foreach installed:
2768 * create assertion (keep installed, if no update available)
2770 * create update rule (A|update1(A)|update2(A)|...)
2772 * those are used later on to keep a version of the installed packages in
2776 solv->featurerules = solv->nrules; /* mark start of feature rules */
2779 /* foreach possibly installed solvable */
2780 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2782 if (s->repo != installed)
2784 solver_addrule(solv, 0, 0); /* create dummy rule */
2787 solver_addupdaterule(solv, s, 1); /* allow s to be updated */
2789 /* make sure we accounted for all rules */
2790 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
2792 solv->featurerules_end = solv->nrules;
2795 * Add update rules for installed solvables
2797 * almost identical to feature rules
2798 * except that downgrades/archchanges/vendorchanges are not allowed
2801 solv->updaterules = solv->nrules;
2804 { /* foreach installed solvables */
2805 /* we create all update rules, but disable some later on depending on the job */
2806 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2810 if (s->repo != installed)
2812 solver_addrule(solv, 0, 0); /* create dummy rule */
2815 solver_addupdaterule(solv, s, 0); /* allowall = 0: downgrades not allowed */
2817 * check for and remove duplicate
2819 r = solv->rules + solv->nrules - 1; /* r: update rule */
2820 sr = r - (installed->end - installed->start); /* sr: feature rule */
2821 /* it's orphaned if there is no feature rule or the feature rule
2822 * consists just of the installed package */
2823 if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
2824 queue_push(&solv->orphaned, i);
2827 assert(solv->dupmap_all && !sr->p);
2830 if (!solver_samerule(solv, r, sr))
2832 /* identical rule, kill unneeded one */
2833 if (solv->allowuninstall)
2835 /* keep feature rule, make it weak */
2836 memset(r, 0, sizeof(*r));
2837 queue_push(&solv->weakruleq, sr - solv->rules);
2841 /* keep update rule */
2842 memset(sr, 0, sizeof(*sr));
2845 else if (solv->allowuninstall)
2847 /* make both feature and update rule weak */
2848 queue_push(&solv->weakruleq, r - solv->rules);
2849 queue_push(&solv->weakruleq, sr - solv->rules);
2852 solver_disablerule(solv, sr);
2854 /* consistency check: we added a rule for _every_ installed solvable */
2855 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
2857 solv->updaterules_end = solv->nrules;
2861 * now add all job rules
2864 solv->jobrules = solv->nrules;
2865 for (i = 0; i < job->count; i += 2)
2867 oldnrules = solv->nrules;
2869 how = job->elements[i];
2870 what = job->elements[i + 1];
2871 weak = how & SOLVER_WEAK;
2872 select = how & SOLVER_SELECTMASK;
2873 switch (how & SOLVER_JOBMASK)
2875 case SOLVER_INSTALL:
2876 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2877 if (select == SOLVER_SOLVABLE)
2885 FOR_JOB_SELECT(p, pp, select, what)
2889 /* no candidate found, make this an impossible rule */
2890 queue_push(&q, -SYSTEMSOLVABLE);
2892 p = queue_shift(&q); /* get first candidate */
2893 d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q); /* internalize */
2895 solver_addjobrule(solv, p, d, i, weak);
2898 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
2899 if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && solv->installed)
2900 map_grow(&solv->cleandepsmap, solv->installed->end - solv->installed->start);
2901 if (select == SOLVER_SOLVABLE && solv->installed && pool->solvables[what].repo == solv->installed)
2903 /* special case for "erase a specific solvable": we also
2904 * erase all other solvables with that name, so that they
2905 * don't get picked up as replacement */
2906 /* XXX: look also at packages that obsolete this package? */
2907 name = pool->solvables[what].name;
2908 FOR_PROVIDES(p, pp, name)
2912 s = pool->solvables + p;
2913 if (s->name != name)
2915 /* keep other versions installed */
2916 if (s->repo == solv->installed)
2918 /* keep installcandidates of other jobs */
2919 if (MAPTST(&installcandidatemap, p))
2921 solver_addjobrule(solv, -p, 0, i, weak); /* remove by id */
2924 FOR_JOB_SELECT(p, pp, select, what)
2925 solver_addjobrule(solv, -p, 0, i, weak);
2929 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2932 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2934 case SOLVER_WEAKENDEPS:
2935 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2936 if (select != SOLVER_SOLVABLE)
2938 s = pool->solvables + what;
2939 weaken_solvable_deps(solv, what);
2941 case SOLVER_NOOBSOLETES:
2942 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2945 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2946 FOR_JOB_SELECT(p, pp, select, what)
2948 s = pool->solvables + p;
2949 solver_addjobrule(solv, installed && s->repo == installed ? p : -p, 0, i, weak);
2952 case SOLVER_DISTUPGRADE:
2953 POOL_DEBUG(SOLV_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
2955 case SOLVER_DROP_ORPHANED:
2956 POOL_DEBUG(SOLV_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
2957 if (select == SOLVER_SOLVABLE_ALL)
2958 solv->droporphanedmap_all = 1;
2959 FOR_JOB_SELECT(p, pp, select, what)
2961 s = pool->solvables + p;
2962 if (!installed || s->repo != installed)
2964 if (!solv->droporphanedmap.size)
2965 map_grow(&solv->droporphanedmap, installed->end - installed->start);
2966 MAPSET(&solv->droporphanedmap, p - installed->start);
2969 case SOLVER_USERINSTALLED:
2970 POOL_DEBUG(SOLV_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
2973 POOL_DEBUG(SOLV_DEBUG_JOB, "job: unknown job\n");
2981 IF_POOLDEBUG (SOLV_DEBUG_JOB)
2984 if (solv->nrules == oldnrules)
2985 POOL_DEBUG(SOLV_DEBUG_JOB, " - no rule created\n");
2986 for (j = oldnrules; j < solv->nrules; j++)
2988 POOL_DEBUG(SOLV_DEBUG_JOB, " - job ");
2989 solver_printrule(solv, SOLV_DEBUG_JOB, solv->rules + j);
2993 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
2994 solv->jobrules_end = solv->nrules;
2996 /* now create infarch and dup rules */
2997 if (!solv->noinfarchcheck)
2999 solver_addinfarchrules(solv, &addedmap);
3000 if (pool->obsoleteusescolors)
3002 /* currently doesn't work well with infarch rules, so make
3004 for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
3005 queue_push(&solv->weakruleq, i);
3009 solv->infarchrules = solv->infarchrules_end = solv->nrules;
3013 solver_addduprules(solv, &addedmap);
3014 solver_freedupmaps(solv); /* no longer needed */
3017 solv->duprules = solv->duprules_end = solv->nrules;
3020 solver_addchoicerules(solv);
3022 solv->choicerules = solv->choicerules_end = solv->nrules;
3024 /* all rules created
3025 * --------------------------------------------------------------
3026 * prepare for solving
3029 /* free unneeded memory */
3030 map_free(&addedmap);
3031 map_free(&installcandidatemap);
3034 POOL_DEBUG(SOLV_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);
3036 /* create weak map */
3037 map_init(&solv->weakrulemap, solv->nrules);
3038 for (i = 0; i < solv->weakruleq.count; i++)
3040 p = solv->weakruleq.elements[i];
3041 MAPSET(&solv->weakrulemap, p);
3044 /* all new rules are learnt after this point */
3045 solv->learntrules = solv->nrules;
3047 /* create watches chains */
3050 /* create assertion index. it is only used to speed up
3051 * makeruledecsions() a bit */
3052 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
3053 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
3054 queue_push(&solv->ruleassertions, i);
3056 /* disable update rules that conflict with our job */
3057 solver_disablepolicyrules(solv);
3059 /* make initial decisions based on assertion rules */
3060 makeruledecisions(solv);
3061 POOL_DEBUG(SOLV_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
3064 * ********************************************
3066 * ********************************************
3069 now = solv_timems(0);
3070 solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
3071 POOL_DEBUG(SOLV_DEBUG_STATS, "solver took %d ms\n", solv_timems(now));
3074 * calculate recommended/suggested packages
3076 findrecommendedsuggested(solv);
3079 * prepare solution queue if there were problems
3081 solver_prepare_solutions(solv);
3083 POOL_DEBUG(SOLV_DEBUG_STATS, "final solver statistics: %d problems, %d learned rules, %d unsolvable\n", solv->problems.count / 2, solv->stats_learned, solv->stats_unsolvable);
3084 POOL_DEBUG(SOLV_DEBUG_STATS, "solver_solve took %d ms\n", solv_timems(solve_start));
3086 /* return number of problems */
3087 return solv->problems.count ? solv->problems.count / 2 : 0;
3091 solver_create_transaction(Solver *solv)
3093 return transaction_create_decisionq(solv->pool, &solv->decisionq, &solv->noobsoletes);
3096 /***********************************************************************/
3097 /* disk usage computations */
3099 /*-------------------------------------------------------------------
3101 * calculate DU changes
3105 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
3109 solver_create_state_maps(solv, &installedmap, 0);
3110 pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
3111 map_free(&installedmap);
3115 /*-------------------------------------------------------------------
3117 * calculate changes in install size
3121 solver_calc_installsizechange(Solver *solv)
3126 solver_create_state_maps(solv, &installedmap, 0);
3127 change = pool_calc_installsizechange(solv->pool, &installedmap);
3128 map_free(&installedmap);
3133 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
3135 pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
3139 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
3142 pool_create_state_maps(solv->pool, &solv->decisionq, &installedmap, 0);
3143 pool_trivial_installable_noobsoletesmap(solv->pool, &installedmap, pkgs, res, solv->noobsoletes.size ? &solv->noobsoletes : 0);
3144 map_free(&installedmap);
3148 solver_get_decisionlevel(Solver *solv, Id p)
3150 return solv->decisionmap[p];
3154 solver_get_decisionqueue(Solver *solv, Queue *decisionq)
3156 queue_free(decisionq);
3157 queue_init_clone(decisionq, &solv->decisionq);
3161 solver_get_lastdecisionblocklevel(Solver *solv)
3164 if (solv->decisionq.count == 0)
3166 p = solv->decisionq.elements[solv->decisionq.count - 1];
3169 return solv->decisionmap[p] < 0 ? -solv->decisionmap[p] : solv->decisionmap[p];
3173 solver_get_decisionblock(Solver *solv, int level, Queue *decisionq)
3178 queue_empty(decisionq);
3179 for (i = 0; i < solv->decisionq.count; i++)
3181 p = solv->decisionq.elements[i];
3184 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3187 if (i == solv->decisionq.count)
3189 for (i = 0; i < solv->decisionq.count; i++)
3191 p = solv->decisionq.elements[i];
3194 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3195 queue_push(decisionq, p);
3202 solver_describe_decision(Solver *solv, Id p, Id *infop)
3209 if (!solv->decisionmap[p])
3210 return SOLVER_REASON_UNRELATED;
3211 pp = solv->decisionmap[p] < 0 ? -p : p;
3212 for (i = 0; i < solv->decisionq.count; i++)
3213 if (solv->decisionq.elements[i] == pp)
3215 if (i == solv->decisionq.count) /* just in case... */
3216 return SOLVER_REASON_UNRELATED;
3217 why = solv->decisionq_why.elements[i];
3222 return SOLVER_REASON_UNIT_RULE;
3225 if (i < solv->decisioncnt_update)
3230 *infop = SYSTEMSOLVABLE;
3231 return SOLVER_REASON_KEEP_INSTALLED;
3235 return SOLVER_REASON_RESOLVE_JOB;
3237 if (i < solv->decisioncnt_keep)
3239 if (why == 0 && pp < 0)
3240 return SOLVER_REASON_CLEANDEPS_ERASE;
3243 if (why >= solv->updaterules && why < solv->updaterules_end)
3244 *infop = why - solv->updaterules;
3245 else if (why >= solv->featurerules && why < solv->featurerules_end)
3246 *infop = why - solv->featurerules;
3248 return SOLVER_REASON_UPDATE_INSTALLED;
3250 if (i < solv->decisioncnt_resolve)
3252 if (why == 0 && pp < 0)
3253 return SOLVER_REASON_CLEANDEPS_ERASE;
3256 if (why >= solv->updaterules && why < solv->updaterules_end)
3257 *infop = why - solv->updaterules;
3258 else if (why >= solv->featurerules && why < solv->featurerules_end)
3259 *infop = why - solv->featurerules;
3261 return SOLVER_REASON_KEEP_INSTALLED;
3263 if (i < solv->decisioncnt_weak)
3267 return SOLVER_REASON_RESOLVE;
3269 if (solv->decisionq.count < solv->decisioncnt_orphan)
3270 return SOLVER_REASON_WEAKDEP;
3271 return SOLVER_REASON_RESOLVE_ORPHAN;
3276 solver_describe_weakdep_decision(Solver *solv, Id p, Queue *whyq)
3278 Pool *pool = solv->pool;
3280 int level = solv->decisionmap[p];
3287 for (decisionno = 0; decisionno < solv->decisionq.count; decisionno++)
3288 if (solv->decisionq.elements[decisionno] == p)
3290 if (decisionno == solv->decisionq.count)
3292 if (decisionno < solv->decisioncnt_weak || decisionno >= solv->decisioncnt_orphan)
3295 /* 1) list all packages that recommend us */
3296 for (i = 1; i < pool->nsolvables; i++)
3298 Id *recp, rec, pp2, p2;
3299 if (solv->decisionmap[i] < 0 || solv->decisionmap[i] >= level)
3301 s = pool->solvables + i;
3304 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
3306 recp = s->repo->idarraydata + s->recommends;
3307 while ((rec = *recp++) != 0)
3310 FOR_PROVIDES(p2, pp2, rec)
3316 /* if p2 is already installed, this recommends is ignored */
3317 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3323 queue_push(whyq, SOLVER_REASON_RECOMMENDED);
3324 queue_push2(whyq, p2, rec);
3328 /* 2) list all supplements */
3329 s = pool->solvables + p;
3330 if (s->supplements && level > 0)
3332 Id *supp, sup, pp2, p2;
3334 /* this is a hack. to use solver_dep_fulfilled we temporarily clear
3335 * everything above our level in the decisionmap */
3336 for (i = decisionno; i < solv->decisionq.count; i++ )
3338 p2 = solv->decisionq.elements[i];
3340 solv->decisionmap[p2] = -solv->decisionmap[p2];
3342 supp = s->repo->idarraydata + s->supplements;
3343 suppstart = whyq->count;
3344 while ((sup = *supp++) != 0)
3345 if (solver_dep_fulfilled(solv, sup))
3348 /* let's see if this is an easy supp */
3349 FOR_PROVIDES(p2, pp2, sup)
3351 if (solv->ignorealreadyrecommended && solv->installed)
3353 if (pool->solvables[p2].repo == solv->installed)
3356 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3358 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3359 queue_push2(whyq, p2, sup);
3365 /* hard case, just note with no package */
3366 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3367 queue_push2(whyq, 0, sup);
3370 for (i = decisionno; i < solv->decisionq.count; i++)
3372 p2 = solv->decisionq.elements[i];
3374 solv->decisionmap[p2] = -solv->decisionmap[p2];