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->branches.count = 0;
838 /* adapt learnt rule status to new set of enabled/disabled rules */
839 enabledisablelearntrules(solv);
841 /* redo all assertion rule decisions */
842 makeruledecisions(solv);
843 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
847 /*-------------------------------------------------------------------
849 * analyze_unsolvable_rule
853 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp, Map *rseen)
855 Pool *pool = solv->pool;
857 Id why = r - solv->rules;
859 IF_POOLDEBUG (SOLV_DEBUG_UNSOLVABLE)
860 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, r);
861 if (solv->learntrules && why >= solv->learntrules)
863 if (MAPTST(rseen, why - solv->learntrules))
865 MAPSET(rseen, why - solv->learntrules);
866 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
867 if (solv->learnt_pool.elements[i] > 0)
868 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp, rseen);
871 if (MAPTST(&solv->weakrulemap, why))
872 if (!*lastweakp || why > *lastweakp)
874 /* do not add rpm rules to problem */
875 if (why < solv->rpmrules_end)
877 /* turn rule into problem */
878 if (why >= solv->jobrules && why < solv->jobrules_end)
879 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
880 /* normalize dup/infarch rules */
881 if (why > solv->infarchrules && why < solv->infarchrules_end)
883 Id name = pool->solvables[-solv->rules[why].p].name;
884 while (why > solv->infarchrules && pool->solvables[-solv->rules[why - 1].p].name == name)
887 if (why > solv->duprules && why < solv->duprules_end)
889 Id name = pool->solvables[-solv->rules[why].p].name;
890 while (why > solv->duprules && pool->solvables[-solv->rules[why - 1].p].name == name)
894 /* return if problem already countains our rule */
895 if (solv->problems.count)
897 for (i = solv->problems.count - 1; i >= 0; i--)
898 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
900 else if (solv->problems.elements[i] == why)
903 queue_push(&solv->problems, why);
907 /*-------------------------------------------------------------------
911 * return: 1 - disabled some rules, try again
916 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
918 Pool *pool = solv->pool;
920 Map seen; /* global to speed things up? */
922 Id d, v, vv, *dp, why;
924 Id *decisionmap = solv->decisionmap;
926 int oldlearntpoolcount;
928 int record_proof = 1;
930 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
931 solv->stats_unsolvable++;
932 oldproblemcount = solv->problems.count;
933 oldlearntpoolcount = solv->learnt_pool.count;
935 /* make room for proof index */
936 /* must update it later, as analyze_unsolvable_rule would confuse
937 * it with a rule index if we put the real value in already */
938 queue_push(&solv->problems, 0);
941 map_init(&seen, pool->nsolvables);
942 map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
944 queue_push(&solv->learnt_pool, r - solv->rules);
946 analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
947 d = r->d < 0 ? -r->d - 1 : r->d;
948 dp = d ? pool->whatprovidesdata + d : 0;
959 if (DECISIONMAP_TRUE(v)) /* the one true literal */
962 l = solv->decisionmap[vv];
967 idx = solv->decisionq.count;
970 v = solv->decisionq.elements[--idx];
972 if (!MAPTST(&seen, vv))
974 why = solv->decisionq_why.elements[idx];
977 queue_push(&solv->learnt_pool, why);
978 r = solv->rules + why;
979 analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
980 d = r->d < 0 ? -r->d - 1 : r->d;
981 dp = d ? pool->whatprovidesdata + d : 0;
992 if (DECISIONMAP_TRUE(v)) /* the one true literal */
995 l = solv->decisionmap[vv];
1003 queue_push(&solv->problems, 0); /* mark end of this problem */
1008 /* disable last weak rule */
1009 solv->problems.count = oldproblemcount;
1010 solv->learnt_pool.count = oldlearntpoolcount;
1011 if (lastweak >= solv->jobrules && lastweak < solv->jobrules_end)
1012 v = -(solv->ruletojob.elements[lastweak - solv->jobrules] + 1);
1015 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "disabling ");
1016 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + lastweak);
1017 if (lastweak >= solv->choicerules && lastweak < solv->choicerules_end)
1018 solver_disablechoicerules(solv, solv->rules + lastweak);
1019 solver_disableproblem(solv, v);
1021 solver_reenablepolicyrules(solv, -v);
1029 queue_push(&solv->learnt_pool, 0);
1030 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1035 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1036 solver_disableproblem(solv, solv->problems.elements[i]);
1037 /* XXX: might want to enable all weak rules again */
1041 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1046 /********************************************************************/
1047 /* Decision revert */
1049 /*-------------------------------------------------------------------
1052 * revert decisionq to a level
1056 revert(Solver *solv, int level)
1058 Pool *pool = solv->pool;
1060 while (solv->decisionq.count)
1062 v = solv->decisionq.elements[solv->decisionq.count - 1];
1063 vv = v > 0 ? v : -v;
1064 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1066 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1067 solv->decisionmap[vv] = 0;
1068 solv->decisionq.count--;
1069 solv->decisionq_why.count--;
1070 solv->propagate_index = solv->decisionq.count;
1072 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1074 solv->branches.count--;
1075 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1076 solv->branches.count--;
1078 solv->recommends_index = -1;
1082 /*-------------------------------------------------------------------
1084 * watch2onhighest - put watch2 on literal with highest level
1088 watch2onhighest(Solver *solv, Rule *r)
1093 d = r->d < 0 ? -r->d - 1 : r->d;
1095 return; /* binary rule, both watches are set */
1096 dp = solv->pool->whatprovidesdata + d;
1097 while ((v = *dp++) != 0)
1099 l = solv->decisionmap[v < 0 ? -v : v];
1111 /*-------------------------------------------------------------------
1115 * add free decision (solvable to install) to decisionq
1116 * increase level and propagate decision
1117 * return if no conflict.
1119 * in conflict case, analyze conflict rule, add resulting
1120 * rule to learnt rule set, make decision from learnt
1121 * rule (always unit) and re-propagate.
1123 * returns the new solver level or 0 if unsolvable
1128 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules, Id ruleid)
1130 Pool *pool = solv->pool;
1135 assert(ruleid >= 0);
1140 solv->decisionmap[decision] = level;
1142 solv->decisionmap[-decision] = -level;
1143 queue_push(&solv->decisionq, decision);
1144 queue_push(&solv->decisionq_why, -ruleid); /* <= 0 -> free decision */
1148 r = propagate(solv, level);
1152 return analyze_unsolvable(solv, r, disablerules);
1153 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1154 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
1155 assert(l > 0 && l < level);
1156 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1158 revert(solv, level);
1159 r = solver_addrule(solv, p, d);
1161 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
1162 queue_push(&solv->learnt_why, why);
1165 /* at least 2 literals, needs watches */
1166 watch2onhighest(solv, r);
1167 addwatches_rule(solv, r);
1171 /* learnt rule is an assertion */
1172 queue_push(&solv->ruleassertions, r - solv->rules);
1174 /* the new rule is unit by design */
1175 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1176 queue_push(&solv->decisionq, p);
1177 queue_push(&solv->decisionq_why, r - solv->rules);
1178 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
1180 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "decision: ");
1181 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, p);
1182 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "new rule: ");
1183 solver_printrule(solv, SOLV_DEBUG_ANALYZE, r);
1190 /*-------------------------------------------------------------------
1192 * select and install
1194 * install best package from the queue. We add an extra package, inst, if
1195 * provided. See comment in weak install section.
1197 * returns the new solver level or 0 if unsolvable
1202 selectandinstall(Solver *solv, int level, Queue *dq, int disablerules, Id ruleid)
1204 Pool *pool = solv->pool;
1209 policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1212 /* XXX: didn't we already do that? */
1213 /* XXX: shouldn't we prefer installed packages? */
1214 /* XXX: move to policy.c? */
1215 /* choose the supplemented one */
1216 for (i = 0; i < dq->count; i++)
1217 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1219 dq->elements[0] = dq->elements[i];
1226 /* multiple candidates, open a branch */
1227 for (i = 1; i < dq->count; i++)
1228 queue_push(&solv->branches, dq->elements[i]);
1229 queue_push(&solv->branches, -level);
1231 p = dq->elements[0];
1233 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing %s\n", pool_solvid2str(pool, p));
1235 return setpropagatelearn(solv, level, p, disablerules, ruleid);
1239 /********************************************************************/
1240 /* Main solver interface */
1243 /*-------------------------------------------------------------------
1246 * create solver structure
1248 * pool: all available solvables
1249 * installed: installed Solvables
1252 * Upon solving, rules are created to flag the Solvables
1253 * of the 'installed' Repo as installed.
1257 solver_create(Pool *pool)
1260 solv = (Solver *)solv_calloc(1, sizeof(Solver));
1262 solv->installed = pool->installed;
1264 queue_init(&solv->ruletojob);
1265 queue_init(&solv->decisionq);
1266 queue_init(&solv->decisionq_why);
1267 queue_init(&solv->problems);
1268 queue_init(&solv->orphaned);
1269 queue_init(&solv->learnt_why);
1270 queue_init(&solv->learnt_pool);
1271 queue_init(&solv->branches);
1272 queue_init(&solv->weakruleq);
1273 queue_init(&solv->ruleassertions);
1275 queue_push(&solv->learnt_pool, 0); /* so that 0 does not describe a proof */
1277 map_init(&solv->recommendsmap, pool->nsolvables);
1278 map_init(&solv->suggestsmap, pool->nsolvables);
1279 map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1280 solv->recommends_index = 0;
1282 solv->decisionmap = (Id *)solv_calloc(pool->nsolvables, sizeof(Id));
1284 solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1285 memset(solv->rules, 0, sizeof(Rule));
1291 /*-------------------------------------------------------------------
1297 solver_free(Solver *solv)
1299 queue_free(&solv->job);
1300 queue_free(&solv->ruletojob);
1301 queue_free(&solv->decisionq);
1302 queue_free(&solv->decisionq_why);
1303 queue_free(&solv->learnt_why);
1304 queue_free(&solv->learnt_pool);
1305 queue_free(&solv->problems);
1306 queue_free(&solv->solutions);
1307 queue_free(&solv->orphaned);
1308 queue_free(&solv->branches);
1309 queue_free(&solv->weakruleq);
1310 queue_free(&solv->ruleassertions);
1312 map_free(&solv->recommendsmap);
1313 map_free(&solv->suggestsmap);
1314 map_free(&solv->noupdate);
1315 map_free(&solv->weakrulemap);
1316 map_free(&solv->noobsoletes);
1318 map_free(&solv->updatemap);
1319 map_free(&solv->fixmap);
1320 map_free(&solv->dupmap);
1321 map_free(&solv->dupinvolvedmap);
1322 map_free(&solv->droporphanedmap);
1323 map_free(&solv->cleandepsmap);
1325 solv_free(solv->decisionmap);
1326 solv_free(solv->rules);
1327 solv_free(solv->watches);
1328 solv_free(solv->obsoletes);
1329 solv_free(solv->obsoletes_data);
1330 solv_free(solv->multiversionupdaters);
1331 solv_free(solv->choicerules_ref);
1336 solver_get_flag(Solver *solv, int flag)
1340 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1341 return solv->allowdowngrade;
1342 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1343 return solv->allowarchchange;
1344 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1345 return solv->allowvendorchange;
1346 case SOLVER_FLAG_ALLOW_UNINSTALL:
1347 return solv->allowuninstall;
1348 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1349 return solv->noupdateprovide;
1350 case SOLVER_FLAG_SPLITPROVIDES:
1351 return solv->dosplitprovides;
1352 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1353 return solv->dontinstallrecommended;
1354 case SOLVER_FLAG_IGNORE_ALREADY_RECOMMENDED:
1355 return solv->ignorealreadyrecommended;
1356 case SOLVER_FLAG_NO_INFARCHCHECK:
1357 return solv->noinfarchcheck;
1365 solver_set_flag(Solver *solv, int flag, int value)
1367 int old = solver_get_flag(solv, flag);
1370 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1371 solv->allowdowngrade = value;
1373 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1374 solv->allowarchchange = value;
1376 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1377 solv->allowvendorchange = value;
1379 case SOLVER_FLAG_ALLOW_UNINSTALL:
1380 solv->allowuninstall = value;
1382 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1383 solv->noupdateprovide = value;
1385 case SOLVER_FLAG_SPLITPROVIDES:
1386 solv->dosplitprovides = value;
1388 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1389 solv->dontinstallrecommended = value;
1391 case SOLVER_FLAG_IGNORE_ALREADY_RECOMMENDED:
1392 solv->ignorealreadyrecommended = value;
1394 case SOLVER_FLAG_NO_INFARCHCHECK:
1395 solv->noinfarchcheck = value;
1404 /*-------------------------------------------------------------------
1408 * all rules have been set up, now actually run the solver
1413 solver_run_sat(Solver *solv, int disablerules, int doweak)
1415 Queue dq; /* local decisionqueue */
1416 Queue dqs; /* local decisionqueue for supplements */
1422 Pool *pool = solv->pool;
1424 int minimizationsteps;
1425 int installedpos = solv->installed ? solv->installed->start : 0;
1427 IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
1429 POOL_DEBUG (SOLV_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1430 for (i = 1; i < solv->nrules; i++)
1431 solver_printruleclass(solv, SOLV_DEBUG_RULE_CREATION, solv->rules + i);
1434 POOL_DEBUG(SOLV_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1436 /* start SAT algorithm */
1438 systemlevel = level + 1;
1439 POOL_DEBUG(SOLV_DEBUG_SOLVER, "solving...\n");
1445 * here's the main loop:
1446 * 1) propagate new decisions (only needed once)
1448 * 3) try to keep installed packages
1449 * 4) fulfill all unresolved rules
1450 * 5) install recommended packages
1451 * 6) minimalize solution if we had choices
1452 * if we encounter a problem, we rewind to a safe level and restart
1456 minimizationsteps = 0;
1460 * initial propagation of the assertions
1464 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1465 if ((r = propagate(solv, level)) != 0)
1467 if (analyze_unsolvable(solv, r, disablerules))
1470 break; /* unsolvable */
1475 * resolve jobs first
1477 if (level < systemlevel)
1479 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving job rules\n");
1480 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1483 if (r->d < 0) /* ignore disabled rules */
1486 FOR_RULELITERALS(l, dp, r)
1490 if (solv->decisionmap[-l] <= 0)
1495 if (solv->decisionmap[l] > 0)
1497 if (solv->decisionmap[l] == 0)
1503 /* prune to installed if not updating */
1504 if (dq.count > 1 && solv->installed && !solv->updatemap_all)
1507 for (j = k = 0; j < dq.count; j++)
1509 Solvable *s = pool->solvables + dq.elements[j];
1510 if (s->repo == solv->installed)
1512 dq.elements[k++] = dq.elements[j];
1513 if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
1515 k = 0; /* package wants to be updated, do not prune */
1524 level = selectandinstall(solv, level, &dq, disablerules, i);
1527 if (level <= olevel)
1531 break; /* unsolvable */
1532 systemlevel = level + 1;
1533 if (i < solv->jobrules_end)
1535 solv->decisioncnt_update = solv->decisionq.count;
1536 solv->decisioncnt_keep = solv->decisionq.count;
1540 * installed packages
1542 if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1544 Repo *installed = solv->installed;
1547 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving installed packages\n");
1548 /* we use two passes if we need to update packages
1549 * to create a better user experience */
1550 for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1552 int passlevel = level;
1554 solv->decisioncnt_keep = solv->decisionq.count;
1555 /* start with installedpos, the position that gave us problems last time */
1556 for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1561 if (i == installed->end)
1562 i = installed->start;
1563 s = pool->solvables + i;
1564 if (s->repo != installed)
1567 if (solv->decisionmap[i] > 0)
1569 if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1570 continue; /* updates first */
1571 r = solv->rules + solv->updaterules + (i - installed->start);
1573 if (!rr->p || rr->d < 0) /* disabled -> look at feature rule */
1574 rr -= solv->installed->end - solv->installed->start;
1575 if (!rr->p) /* identical to update rule? */
1578 continue; /* orpaned package */
1580 /* XXX: noupdate check is probably no longer needed, as all jobs should
1581 * already be satisfied */
1582 /* Actually we currently still need it because of erase jobs */
1583 /* if noupdate is set we do not look at update candidates */
1585 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))
1587 if (solv->noobsoletes.size && solv->multiversionupdaters
1588 && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1590 /* special multiversion handling, make sure best version is chosen */
1592 while ((p = pool->whatprovidesdata[d++]) != 0)
1593 if (solv->decisionmap[p] >= 0)
1595 policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1597 if (p != i && solv->decisionmap[p] == 0)
1599 rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1600 if (!rr->p) /* update rule == feature rule? */
1601 rr = rr - solv->featurerules + solv->updaterules;
1609 /* update to best package */
1610 FOR_RULELITERALS(p, dp, rr)
1612 if (solv->decisionmap[p] > 0)
1614 dq.count = 0; /* already fulfilled */
1617 if (!solv->decisionmap[p])
1622 /* install best version */
1626 level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1633 if (level <= olevel)
1635 if (level == 1 || level < passlevel)
1636 break; /* trouble */
1638 n = installed->start; /* redo all */
1644 /* if still undecided keep package */
1645 if (solv->decisionmap[i] == 0)
1648 if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
1650 POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
1651 level = setpropagatelearn(solv, level, -i, disablerules, 0);
1655 POOL_DEBUG(SOLV_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
1656 level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1660 if (level <= olevel)
1662 if (level == 1 || level < passlevel)
1663 break; /* trouble */
1665 n = installed->start; /* redo all */
1668 continue; /* retry with learnt rule */
1672 if (n < installed->end)
1674 installedpos = i; /* retry problem solvable next time */
1675 break; /* ran into trouble */
1677 installedpos = installed->start; /* reset installedpos */
1680 break; /* unsolvable */
1681 systemlevel = level + 1;
1683 continue; /* had trouble, retry */
1686 if (level < systemlevel)
1687 systemlevel = level;
1692 solv->decisioncnt_resolve = solv->decisionq.count;
1693 POOL_DEBUG(SOLV_DEBUG_POLICY, "deciding unresolved rules\n");
1694 for (i = 1, n = 1; n < solv->nrules; i++, n++)
1696 if (i == solv->nrules)
1698 r = solv->rules + i;
1699 if (r->d < 0) /* ignore disabled rules */
1704 /* binary or unary rule */
1705 /* need two positive undecided literals */
1706 if (r->p < 0 || r->w2 <= 0)
1708 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1710 queue_push(&dq, r->p);
1711 queue_push(&dq, r->w2);
1716 * all negative literals are installed
1717 * no positive literal is installed
1718 * i.e. the rule is not fulfilled and we
1719 * just need to decide on the positive literals
1723 if (solv->decisionmap[-r->p] <= 0)
1728 if (solv->decisionmap[r->p] > 0)
1730 if (solv->decisionmap[r->p] == 0)
1731 queue_push(&dq, r->p);
1733 dp = pool->whatprovidesdata + r->d;
1734 while ((p = *dp++) != 0)
1738 if (solv->decisionmap[-p] <= 0)
1743 if (solv->decisionmap[p] > 0)
1745 if (solv->decisionmap[p] == 0)
1752 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
1754 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "unfulfilled ");
1755 solver_printruleclass(solv, SOLV_DEBUG_PROPAGATE, r);
1757 /* dq.count < 2 cannot happen as this means that
1758 * the rule is unit */
1759 assert(dq.count > 1);
1762 level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
1764 break; /* unsolvable */
1765 if (level < systemlevel || level == 1)
1766 break; /* trouble */
1767 /* something changed, so look at all rules again */
1771 if (n != solv->nrules) /* ran into trouble? */
1774 break; /* unsolvable */
1775 continue; /* start over */
1778 /* at this point we have a consistent system. now do the extras... */
1780 solv->decisioncnt_weak = solv->decisionq.count;
1785 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended packages\n");
1786 queue_empty(&dq); /* recommended packages */
1787 queue_empty(&dqs); /* supplemented packages */
1788 for (i = 1; i < pool->nsolvables; i++)
1790 if (solv->decisionmap[i] < 0)
1792 if (solv->decisionmap[i] > 0)
1794 /* installed, check for recommends */
1795 Id *recp, rec, pp, p;
1796 s = pool->solvables + i;
1797 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
1799 /* XXX need to special case AND ? */
1802 recp = s->repo->idarraydata + s->recommends;
1803 while ((rec = *recp++) != 0)
1806 FOR_PROVIDES(p, pp, rec)
1808 if (solv->decisionmap[p] > 0)
1813 else if (solv->decisionmap[p] == 0)
1815 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))))
1817 queue_pushunique(&dq, p);
1825 s = pool->solvables + i;
1826 if (!s->supplements)
1828 if (!pool_installable(pool, s))
1830 if (!solver_is_supplementing(solv, s))
1832 if (solv->dupmap_all && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
1834 queue_push(&dqs, i);
1838 /* filter out all packages obsoleted by installed packages */
1839 /* this is no longer needed if we have reverse obsoletes */
1840 if ((dqs.count || dq.count) && solv->installed)
1843 Id obs, *obsp, po, ppo;
1845 map_init(&obsmap, pool->nsolvables);
1846 for (p = solv->installed->start; p < solv->installed->end; p++)
1848 s = pool->solvables + p;
1849 if (s->repo != solv->installed || !s->obsoletes)
1851 if (solv->decisionmap[p] <= 0)
1853 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
1855 obsp = s->repo->idarraydata + s->obsoletes;
1856 /* foreach obsoletes */
1857 while ((obs = *obsp++) != 0)
1858 FOR_PROVIDES(po, ppo, obs)
1859 MAPSET(&obsmap, po);
1861 for (i = j = 0; i < dqs.count; i++)
1862 if (!MAPTST(&obsmap, dqs.elements[i]))
1863 dqs.elements[j++] = dqs.elements[i];
1865 for (i = j = 0; i < dq.count; i++)
1866 if (!MAPTST(&obsmap, dq.elements[i]))
1867 dq.elements[j++] = dq.elements[i];
1872 /* filter out all already supplemented packages if requested */
1873 if (solv->ignorealreadyrecommended && dqs.count)
1875 /* turn off all new packages */
1876 for (i = 0; i < solv->decisionq.count; i++)
1878 p = solv->decisionq.elements[i];
1881 s = pool->solvables + p;
1882 if (s->repo && s->repo != solv->installed)
1883 solv->decisionmap[p] = -solv->decisionmap[p];
1885 /* filter out old supplements */
1886 for (i = j = 0; i < dqs.count; i++)
1888 p = dqs.elements[i];
1889 s = pool->solvables + p;
1890 if (!s->supplements)
1892 if (!solver_is_supplementing(solv, s))
1893 dqs.elements[j++] = p;
1896 /* undo turning off */
1897 for (i = 0; i < solv->decisionq.count; i++)
1899 p = solv->decisionq.elements[i];
1902 s = pool->solvables + p;
1903 if (s->repo && s->repo != solv->installed)
1904 solv->decisionmap[p] = -solv->decisionmap[p];
1908 /* multiversion doesn't mix well with supplements.
1909 * filter supplemented packages where we already decided
1910 * to install a different version (see bnc#501088) */
1911 if (dqs.count && solv->noobsoletes.size)
1913 for (i = j = 0; i < dqs.count; i++)
1915 p = dqs.elements[i];
1916 if (MAPTST(&solv->noobsoletes, p))
1919 s = pool->solvables + p;
1920 FOR_PROVIDES(p2, pp2, s->name)
1921 if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
1924 continue; /* ignore this package */
1926 dqs.elements[j++] = p;
1931 /* make dq contain both recommended and supplemented pkgs */
1934 for (i = 0; i < dqs.count; i++)
1935 queue_pushunique(&dq, dqs.elements[i]);
1941 int decisioncount = solv->decisionq.count;
1945 /* simple case, just one package. no need to choose to best version */
1948 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
1950 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
1951 level = setpropagatelearn(solv, level, p, 0, 0);
1954 continue; /* back to main loop */
1957 /* filter packages, this gives us the best versions */
1958 policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
1960 /* create map of result */
1961 map_init(&dqmap, pool->nsolvables);
1962 for (i = 0; i < dq.count; i++)
1963 MAPSET(&dqmap, dq.elements[i]);
1965 /* install all supplemented packages */
1966 for (i = 0; i < dqs.count; i++)
1968 p = dqs.elements[i];
1969 if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
1971 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
1973 level = setpropagatelearn(solv, level, p, 0, 0);
1974 if (level <= olevel)
1977 if (i < dqs.count || solv->decisionq.count < decisioncount)
1985 /* install all recommended packages */
1986 /* more work as we want to created branches if multiple
1987 * choices are valid */
1988 for (i = 0; i < decisioncount; i++)
1991 p = solv->decisionq.elements[i];
1994 s = pool->solvables + p;
1995 if (!s->repo || (solv->ignorealreadyrecommended && s->repo == solv->installed))
1999 recp = s->repo->idarraydata + s->recommends;
2000 while ((rec = *recp++) != 0)
2003 FOR_PROVIDES(p, pp, rec)
2005 if (solv->decisionmap[p] > 0)
2010 else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
2011 queue_pushunique(&dq, p);
2017 /* multiple candidates, open a branch */
2018 for (i = 1; i < dq.count; i++)
2019 queue_push(&solv->branches, dq.elements[i]);
2020 queue_push(&solv->branches, -level);
2023 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2025 level = setpropagatelearn(solv, level, p, 0, 0);
2026 if (level <= olevel || solv->decisionq.count < decisioncount)
2027 break; /* we had to revert some decisions */
2030 break; /* had a problem above, quit loop */
2035 continue; /* back to main loop so that all deps are checked */
2039 solv->decisioncnt_orphan = solv->decisionq.count;
2040 if (solv->dupmap_all && solv->installed)
2042 int installedone = 0;
2044 /* let's see if we can install some unsupported package */
2045 POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding orphaned packages\n");
2046 for (i = 0; i < solv->orphaned.count; i++)
2048 p = solv->orphaned.elements[i];
2049 if (solv->decisionmap[p])
2050 continue; /* already decided */
2052 if (solv->droporphanedmap_all)
2054 if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
2056 POOL_DEBUG(SOLV_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
2057 level = setpropagatelearn(solv, level, p, 0, 0);
2064 if (installedone || i < solv->orphaned.count)
2068 continue; /* back to main loop */
2070 for (i = 0; i < solv->orphaned.count; i++)
2072 p = solv->orphaned.elements[i];
2073 if (solv->decisionmap[p])
2074 continue; /* already decided */
2075 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2077 level = setpropagatelearn(solv, level, -p, 0, 0);
2081 if (i < solv->orphaned.count)
2085 continue; /* back to main loop */
2089 if (solv->solution_callback)
2091 solv->solution_callback(solv, solv->solution_callback_data);
2092 if (solv->branches.count)
2094 int i = solv->branches.count - 1;
2095 int l = -solv->branches.elements[i];
2099 if (solv->branches.elements[i - 1] < 0)
2101 p = solv->branches.elements[i];
2102 POOL_DEBUG(SOLV_DEBUG_SOLVER, "branching with %s\n", pool_solvid2str(pool, p));
2104 for (j = i + 1; j < solv->branches.count; j++)
2105 queue_push(&dq, solv->branches.elements[j]);
2106 solv->branches.count = i;
2108 revert(solv, level);
2110 for (j = 0; j < dq.count; j++)
2111 queue_push(&solv->branches, dq.elements[j]);
2113 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2115 level = setpropagatelearn(solv, level, p, disablerules, why);
2120 /* all branches done, we're finally finished */
2124 /* auto-minimization step */
2125 if (solv->branches.count)
2127 int l = 0, lasti = -1, lastl = -1;
2131 for (i = solv->branches.count - 1; i >= 0; i--)
2133 p = solv->branches.elements[i];
2136 else if (p > 0 && solv->decisionmap[p] > l + 1)
2144 /* kill old solvable so that we do not loop */
2145 p = solv->branches.elements[lasti];
2146 solv->branches.elements[lasti] = 0;
2147 POOL_DEBUG(SOLV_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, pool_solvid2str(pool, p));
2148 minimizationsteps++;
2151 revert(solv, level);
2152 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2155 level = setpropagatelearn(solv, level, p, disablerules, why);
2158 continue; /* back to main loop */
2161 /* no minimization found, we're finally finished! */
2165 POOL_DEBUG(SOLV_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2167 POOL_DEBUG(SOLV_DEBUG_STATS, "done solving.\n\n");
2173 solv->decisioncnt_update = solv->decisionq.count;
2174 solv->decisioncnt_keep = solv->decisionq.count;
2175 solv->decisioncnt_resolve = solv->decisionq.count;
2176 solv->decisioncnt_weak = solv->decisionq.count;
2177 solv->decisioncnt_orphan = solv->decisionq.count;
2180 solver_printdecisionq(solv, SOLV_DEBUG_RESULT);
2185 /*-------------------------------------------------------------------
2187 * remove disabled conflicts
2189 * purpose: update the decisionmap after some rules were disabled.
2190 * this is used to calculate the suggested/recommended package list.
2191 * Also returns a "removed" list to undo the discisionmap changes.
2195 removedisabledconflicts(Solver *solv, Queue *removed)
2197 Pool *pool = solv->pool;
2202 Id *decisionmap = solv->decisionmap;
2204 queue_empty(removed);
2205 for (i = 0; i < solv->decisionq.count; i++)
2207 p = solv->decisionq.elements[i];
2209 continue; /* conflicts only, please */
2210 why = solv->decisionq_why.elements[i];
2213 /* no rule involved, must be a orphan package drop */
2216 /* we never do conflicts on free decisions, so there
2217 * must have been an unit rule */
2219 r = solv->rules + why;
2220 if (r->d < 0 && decisionmap[-p])
2222 /* rule is now disabled, remove from decisionmap */
2223 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2224 queue_push(removed, -p);
2225 queue_push(removed, decisionmap[-p]);
2226 decisionmap[-p] = 0;
2229 if (!removed->count)
2231 /* we removed some confliced packages. some of them might still
2232 * be in conflict, so search for unit rules and re-conflict */
2234 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2236 if (i == solv->nrules)
2239 r = solv->rules + i;
2245 if (r->p < 0 && !decisionmap[-r->p])
2251 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2253 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2258 if (r->p < 0 && decisionmap[-r->p] == 0)
2260 if (new || DECISIONMAP_FALSE(r->p))
2262 dp = pool->whatprovidesdata + r->d;
2263 while ((p = *dp++) != 0)
2265 if (new && p == new)
2267 if (p < 0 && decisionmap[-p] == 0)
2276 else if (!DECISIONMAP_FALSE(p))
2286 POOL_DEBUG(SOLV_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2287 decisionmap[-new] = -1;
2289 n = 0; /* redo all rules */
2295 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2298 for (i = 0; i < removed->count; i += 2)
2299 solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2303 /*-------------------------------------------------------------------
2305 * weaken solvable dependencies
2309 weaken_solvable_deps(Solver *solv, Id p)
2314 for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2318 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2319 continue; /* conflict */
2320 queue_push(&solv->weakruleq, i);
2325 /********************************************************************/
2330 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap)
2333 Id how, what, select;
2335 for (i = 0; i < job->count; i += 2)
2337 how = job->elements[i];
2338 if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
2340 what = job->elements[i + 1];
2341 select = how & SOLVER_SELECTMASK;
2342 if (!noobsmap->size)
2343 map_grow(noobsmap, pool->nsolvables);
2344 FOR_JOB_SELECT(p, pp, select, what)
2345 MAPSET(noobsmap, p);
2350 * add a rule created by a job, record job number and weak flag
2353 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
2355 solver_addrule(solv, p, d);
2356 queue_push(&solv->ruletojob, job);
2358 queue_push(&solv->weakruleq, solv->nrules - 1);
2368 solver_solve(Solver *solv, Queue *job)
2370 Pool *pool = solv->pool;
2371 Repo *installed = solv->installed;
2374 Map addedmap; /* '1' == have rpm-rules for solvable */
2375 Map installcandidatemap;
2376 Id how, what, select, name, weak, p, pp, d;
2380 int now, solve_start;
2383 solve_start = solv_timems(0);
2385 /* log solver options */
2386 POOL_DEBUG(SOLV_DEBUG_STATS, "solver started\n");
2387 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);
2388 POOL_DEBUG(SOLV_DEBUG_STATS, "distupgrade=%d distupgrade_removeunsupported=%d\n", solv->distupgrade, solv->distupgrade_removeunsupported);
2389 POOL_DEBUG(SOLV_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allowarchchange, solv->allowvendorchange);
2390 POOL_DEBUG(SOLV_DEBUG_STATS, "promoteepoch=%d, allowselfconflicts=%d\n", pool->promoteepoch, pool->allowselfconflicts);
2391 POOL_DEBUG(SOLV_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2392 POOL_DEBUG(SOLV_DEBUG_STATS, "dontinstallrecommended=%d, ignorealreadyrecommended=%d\n", solv->dontinstallrecommended, solv->ignorealreadyrecommended);
2394 /* create whatprovides if not already there */
2395 if (!pool->whatprovides)
2396 pool_createwhatprovides(pool);
2398 /* create obsolete index */
2399 policy_create_obsolete_index(solv);
2402 queue_free(&solv->job);
2403 queue_init_clone(&solv->job, job);
2405 /* initialize with legacy values */
2406 solv->fixmap_all = solv->fixsystem;
2407 solv->updatemap_all = solv->updatesystem;
2408 solv->droporphanedmap_all = solv->distupgrade_removeunsupported;
2409 solv->dupmap_all = solv->distupgrade;
2412 * create basic rule set of all involved packages
2413 * use addedmap bitmap to make sure we don't create rules twice
2416 /* create noobsolete map if needed */
2417 solver_calculate_noobsmap(pool, job, &solv->noobsoletes);
2419 map_init(&addedmap, pool->nsolvables);
2420 MAPSET(&addedmap, SYSTEMSOLVABLE);
2422 map_init(&installcandidatemap, pool->nsolvables);
2425 now = solv_timems(0);
2427 * create rules for all package that could be involved with the solving
2428 * so called: rpm rules
2433 /* check for update/verify jobs as they need to be known early */
2434 for (i = 0; i < job->count; i += 2)
2436 how = job->elements[i];
2437 what = job->elements[i + 1];
2438 select = how & SOLVER_SELECTMASK;
2439 switch (how & SOLVER_JOBMASK)
2442 if (select == SOLVER_SOLVABLE_ALL)
2443 solv->fixmap_all = 1;
2444 FOR_JOB_SELECT(p, pp, select, what)
2446 s = pool->solvables + p;
2447 if (!solv->installed || s->repo != solv->installed)
2449 if (!solv->fixmap.size)
2450 map_grow(&solv->fixmap, solv->installed->end - solv->installed->start);
2451 MAPSET(&solv->fixmap, p - solv->installed->start);
2455 if (select == SOLVER_SOLVABLE_ALL)
2456 solv->updatemap_all = 1;
2457 FOR_JOB_SELECT(p, pp, select, what)
2459 s = pool->solvables + p;
2460 if (!solv->installed || s->repo != solv->installed)
2462 if (!solv->updatemap.size)
2463 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
2464 MAPSET(&solv->updatemap, p - solv->installed->start);
2472 oldnrules = solv->nrules;
2473 FOR_REPO_SOLVABLES(installed, p, s)
2474 solver_addrpmrulesforsolvable(solv, s, &addedmap);
2475 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
2476 oldnrules = solv->nrules;
2477 FOR_REPO_SOLVABLES(installed, p, s)
2478 solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
2479 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
2483 * create rules for all packages involved in the job
2484 * (to be installed or removed)
2487 oldnrules = solv->nrules;
2488 for (i = 0; i < job->count; i += 2)
2490 how = job->elements[i];
2491 what = job->elements[i + 1];
2492 select = how & SOLVER_SELECTMASK;
2494 switch (how & SOLVER_JOBMASK)
2496 case SOLVER_INSTALL:
2497 FOR_JOB_SELECT(p, pp, select, what)
2499 MAPSET(&installcandidatemap, p);
2500 solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
2503 case SOLVER_DISTUPGRADE:
2504 if (select == SOLVER_SOLVABLE_ALL)
2506 solv->dupmap_all = 1;
2507 solv->updatemap_all = 1;
2509 if (!solv->dupmap_all)
2516 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
2520 * add rules for suggests, enhances
2522 oldnrules = solv->nrules;
2523 solver_addrpmrulesforweak(solv, &addedmap);
2524 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
2527 * first pass done, we now have all the rpm rules we need.
2528 * unify existing rules before going over all job rules and
2530 * at this point the system is always solvable,
2531 * as an empty system (remove all packages) is a valid solution
2534 IF_POOLDEBUG (SOLV_DEBUG_STATS)
2536 int possible = 0, installable = 0;
2537 for (i = 1; i < pool->nsolvables; i++)
2539 if (pool_installable(pool, pool->solvables + i))
2541 if (MAPTST(&addedmap, i))
2544 POOL_DEBUG(SOLV_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
2547 solver_unifyrules(solv); /* remove duplicate rpm rules */
2548 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
2550 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule memory usage: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
2551 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule creation took %d ms\n", solv_timems(now));
2553 /* create dup maps if needed. We need the maps early to create our
2556 solver_createdupmaps(solv);
2559 * create feature rules
2561 * foreach installed:
2562 * create assertion (keep installed, if no update available)
2564 * create update rule (A|update1(A)|update2(A)|...)
2566 * those are used later on to keep a version of the installed packages in
2570 solv->featurerules = solv->nrules; /* mark start of feature rules */
2573 /* foreach possibly installed solvable */
2574 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2576 if (s->repo != installed)
2578 solver_addrule(solv, 0, 0); /* create dummy rule */
2581 solver_addupdaterule(solv, s, 1); /* allow s to be updated */
2583 /* make sure we accounted for all rules */
2584 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
2586 solv->featurerules_end = solv->nrules;
2589 * Add update rules for installed solvables
2591 * almost identical to feature rules
2592 * except that downgrades/archchanges/vendorchanges are not allowed
2595 solv->updaterules = solv->nrules;
2598 { /* foreach installed solvables */
2599 /* we create all update rules, but disable some later on depending on the job */
2600 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2604 if (s->repo != installed)
2606 solver_addrule(solv, 0, 0); /* create dummy rule */
2609 solver_addupdaterule(solv, s, 0); /* allowall = 0: downgrades not allowed */
2611 * check for and remove duplicate
2613 r = solv->rules + solv->nrules - 1; /* r: update rule */
2614 sr = r - (installed->end - installed->start); /* sr: feature rule */
2615 /* it's orphaned if there is no feature rule or the feature rule
2616 * consists just of the installed package */
2617 if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
2618 queue_push(&solv->orphaned, i);
2621 assert(solv->dupmap_all && !sr->p);
2624 if (!solver_samerule(solv, r, sr))
2626 /* identical rule, kill unneeded one */
2627 if (solv->allowuninstall)
2629 /* keep feature rule, make it weak */
2630 memset(r, 0, sizeof(*r));
2631 queue_push(&solv->weakruleq, sr - solv->rules);
2635 /* keep update rule */
2636 memset(sr, 0, sizeof(*sr));
2639 else if (solv->allowuninstall)
2641 /* make both feature and update rule weak */
2642 queue_push(&solv->weakruleq, r - solv->rules);
2643 queue_push(&solv->weakruleq, sr - solv->rules);
2646 solver_disablerule(solv, sr);
2648 /* consistency check: we added a rule for _every_ installed solvable */
2649 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
2651 solv->updaterules_end = solv->nrules;
2655 * now add all job rules
2658 solv->jobrules = solv->nrules;
2659 for (i = 0; i < job->count; i += 2)
2661 oldnrules = solv->nrules;
2663 how = job->elements[i];
2664 what = job->elements[i + 1];
2665 weak = how & SOLVER_WEAK;
2666 select = how & SOLVER_SELECTMASK;
2667 switch (how & SOLVER_JOBMASK)
2669 case SOLVER_INSTALL:
2670 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2671 if (select == SOLVER_SOLVABLE)
2679 FOR_JOB_SELECT(p, pp, select, what)
2683 /* no candidate found, make this an impossible rule */
2684 queue_push(&q, -SYSTEMSOLVABLE);
2686 p = queue_shift(&q); /* get first candidate */
2687 d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q); /* internalize */
2689 solver_addjobrule(solv, p, d, i, weak);
2692 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
2693 if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && solv->installed)
2694 map_grow(&solv->cleandepsmap, solv->installed->end - solv->installed->start);
2695 if (select == SOLVER_SOLVABLE && solv->installed && pool->solvables[what].repo == solv->installed)
2697 /* special case for "erase a specific solvable": we also
2698 * erase all other solvables with that name, so that they
2699 * don't get picked up as replacement */
2700 /* XXX: look also at packages that obsolete this package? */
2701 name = pool->solvables[what].name;
2702 FOR_PROVIDES(p, pp, name)
2706 s = pool->solvables + p;
2707 if (s->name != name)
2709 /* keep other versions installed */
2710 if (s->repo == solv->installed)
2712 /* keep installcandidates of other jobs */
2713 if (MAPTST(&installcandidatemap, p))
2715 solver_addjobrule(solv, -p, 0, i, weak); /* remove by id */
2718 FOR_JOB_SELECT(p, pp, select, what)
2719 solver_addjobrule(solv, -p, 0, i, weak);
2723 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2726 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2728 case SOLVER_WEAKENDEPS:
2729 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2730 if (select != SOLVER_SOLVABLE)
2732 s = pool->solvables + what;
2733 weaken_solvable_deps(solv, what);
2735 case SOLVER_NOOBSOLETES:
2736 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2739 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2740 FOR_JOB_SELECT(p, pp, select, what)
2742 s = pool->solvables + p;
2743 solver_addjobrule(solv, installed && s->repo == installed ? p : -p, 0, i, weak);
2746 case SOLVER_DISTUPGRADE:
2747 POOL_DEBUG(SOLV_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
2749 case SOLVER_DROP_ORPHANED:
2750 POOL_DEBUG(SOLV_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
2751 if (select == SOLVER_SOLVABLE_ALL)
2752 solv->droporphanedmap_all = 1;
2753 FOR_JOB_SELECT(p, pp, select, what)
2755 s = pool->solvables + p;
2756 if (!installed || s->repo != installed)
2758 if (!solv->droporphanedmap.size)
2759 map_grow(&solv->droporphanedmap, installed->end - installed->start);
2760 MAPSET(&solv->droporphanedmap, p - installed->start);
2763 case SOLVER_USERINSTALLED:
2764 POOL_DEBUG(SOLV_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
2767 POOL_DEBUG(SOLV_DEBUG_JOB, "job: unknown job\n");
2775 IF_POOLDEBUG (SOLV_DEBUG_JOB)
2778 if (solv->nrules == oldnrules)
2779 POOL_DEBUG(SOLV_DEBUG_JOB, " - no rule created\n");
2780 for (j = oldnrules; j < solv->nrules; j++)
2782 POOL_DEBUG(SOLV_DEBUG_JOB, " - job ");
2783 solver_printrule(solv, SOLV_DEBUG_JOB, solv->rules + j);
2787 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
2788 solv->jobrules_end = solv->nrules;
2790 /* now create infarch and dup rules */
2791 if (!solv->noinfarchcheck)
2793 solver_addinfarchrules(solv, &addedmap);
2794 if (pool->obsoleteusescolors)
2796 /* currently doesn't work well with infarch rules, so make
2798 for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
2799 queue_push(&solv->weakruleq, i);
2803 solv->infarchrules = solv->infarchrules_end = solv->nrules;
2807 solver_addduprules(solv, &addedmap);
2808 solver_freedupmaps(solv); /* no longer needed */
2811 solv->duprules = solv->duprules_end = solv->nrules;
2814 solver_addchoicerules(solv);
2816 solv->choicerules = solv->choicerules_end = solv->nrules;
2818 /* all rules created
2819 * --------------------------------------------------------------
2820 * prepare for solving
2823 /* free unneeded memory */
2824 map_free(&addedmap);
2825 map_free(&installcandidatemap);
2828 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);
2830 /* create weak map */
2831 map_init(&solv->weakrulemap, solv->nrules);
2832 for (i = 0; i < solv->weakruleq.count; i++)
2834 p = solv->weakruleq.elements[i];
2835 MAPSET(&solv->weakrulemap, p);
2838 /* all new rules are learnt after this point */
2839 solv->learntrules = solv->nrules;
2841 /* create watches chains */
2844 /* create assertion index. it is only used to speed up
2845 * makeruledecsions() a bit */
2846 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
2847 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
2848 queue_push(&solv->ruleassertions, i);
2850 /* disable update rules that conflict with our job */
2851 solver_disablepolicyrules(solv);
2853 /* make initial decisions based on assertion rules */
2854 makeruledecisions(solv);
2855 POOL_DEBUG(SOLV_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
2858 * ********************************************
2860 * ********************************************
2863 now = solv_timems(0);
2864 solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
2865 POOL_DEBUG(SOLV_DEBUG_STATS, "solver took %d ms\n", solv_timems(now));
2868 * prepare solution queue if there were problems
2870 solver_prepare_solutions(solv);
2872 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);
2873 POOL_DEBUG(SOLV_DEBUG_STATS, "solver_solve took %d ms\n", solv_timems(solve_start));
2875 /* return number of problems */
2876 return solv->problems.count ? solv->problems.count / 2 : 0;
2880 solver_create_transaction(Solver *solv)
2882 return transaction_create_decisionq(solv->pool, &solv->decisionq, &solv->noobsoletes);
2885 void solver_get_orphaned(Solver *solv, Queue *orphanedq)
2887 queue_free(orphanedq);
2888 queue_init_clone(orphanedq, &solv->orphaned);
2891 void solver_get_recommendations(Solver *solv, Queue *recommendationsq, Queue *suggestionsq, int noselected)
2893 Pool *pool = solv->pool;
2894 Queue redoq, disabledq;
2900 if (!recommendationsq && !suggestionsq)
2903 map_init(&obsmap, pool->nsolvables);
2904 if (solv->installed)
2906 Id obs, *obsp, p, po, ppo;
2907 for (p = solv->installed->start; p < solv->installed->end; p++)
2909 s = pool->solvables + p;
2910 if (s->repo != solv->installed || !s->obsoletes)
2912 if (solv->decisionmap[p] <= 0)
2914 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
2916 obsp = s->repo->idarraydata + s->obsoletes;
2917 /* foreach obsoletes */
2918 while ((obs = *obsp++) != 0)
2919 FOR_PROVIDES(po, ppo, obs)
2920 MAPSET(&obsmap, po);
2925 queue_init(&disabledq);
2927 /* disable all erase jobs (including weak "keep uninstalled" rules) */
2928 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2930 if (r->d < 0) /* disabled ? */
2932 if (r->p >= 0) /* install job? */
2934 queue_push(&disabledq, i);
2935 solver_disablerule(solv, r);
2941 enabledisablelearntrules(solv);
2942 removedisabledconflicts(solv, &redoq);
2946 * find recommended packages
2948 if (recommendationsq)
2950 Id rec, *recp, p, pp;
2952 queue_empty(recommendationsq);
2953 /* create map of all recommened packages */
2954 solv->recommends_index = -1;
2955 MAPZERO(&solv->recommendsmap);
2957 /* put all packages the solver already chose in the map */
2958 if (solv->decisioncnt_weak)
2960 for (i = solv->decisioncnt_weak; i < solv->decisioncnt_orphan; i++)
2963 why = solv->decisionq_why.elements[i];
2965 continue; /* forced by unit rule */
2966 p = solv->decisionq.elements[i];
2969 MAPSET(&solv->recommendsmap, p);
2973 for (i = 0; i < solv->decisionq.count; i++)
2975 p = solv->decisionq.elements[i];
2978 s = pool->solvables + p;
2981 recp = s->repo->idarraydata + s->recommends;
2982 while ((rec = *recp++) != 0)
2984 FOR_PROVIDES(p, pp, rec)
2985 if (solv->decisionmap[p] > 0)
2991 FOR_PROVIDES(p, pp, rec)
2992 if (solv->decisionmap[p] > 0)
2993 MAPSET(&solv->recommendsmap, p);
2995 continue; /* p != 0: already fulfilled */
2997 FOR_PROVIDES(p, pp, rec)
2998 MAPSET(&solv->recommendsmap, p);
3002 for (i = 1; i < pool->nsolvables; i++)
3004 if (solv->decisionmap[i] < 0)
3006 if (solv->decisionmap[i] > 0 && noselected)
3008 if (MAPTST(&obsmap, i))
3010 s = pool->solvables + i;
3011 if (!MAPTST(&solv->recommendsmap, i))
3013 if (!s->supplements)
3015 if (!pool_installable(pool, s))
3017 if (!solver_is_supplementing(solv, s))
3020 queue_push(recommendationsq, i);
3022 /* we use MODE_SUGGEST here so that repo prio is ignored */
3023 policy_filter_unwanted(solv, recommendationsq, POLICY_MODE_SUGGEST);
3027 * find suggested packages
3032 Id sug, *sugp, p, pp;
3034 queue_empty(suggestionsq);
3035 /* create map of all suggests that are still open */
3036 solv->recommends_index = -1;
3037 MAPZERO(&solv->suggestsmap);
3038 for (i = 0; i < solv->decisionq.count; i++)
3040 p = solv->decisionq.elements[i];
3043 s = pool->solvables + p;
3046 sugp = s->repo->idarraydata + s->suggests;
3047 while ((sug = *sugp++) != 0)
3049 FOR_PROVIDES(p, pp, sug)
3050 if (solv->decisionmap[p] > 0)
3056 FOR_PROVIDES(p, pp, sug)
3057 if (solv->decisionmap[p] > 0)
3058 MAPSET(&solv->suggestsmap, p);
3060 continue; /* already fulfilled */
3062 FOR_PROVIDES(p, pp, sug)
3063 MAPSET(&solv->suggestsmap, p);
3067 for (i = 1; i < pool->nsolvables; i++)
3069 if (solv->decisionmap[i] < 0)
3071 if (solv->decisionmap[i] > 0 && noselected)
3073 if (MAPTST(&obsmap, i))
3075 s = pool->solvables + i;
3076 if (!MAPTST(&solv->suggestsmap, i))
3080 if (!pool_installable(pool, s))
3082 if (!solver_is_enhancing(solv, s))
3085 queue_push(suggestionsq, i);
3087 policy_filter_unwanted(solv, suggestionsq, POLICY_MODE_SUGGEST);
3090 /* undo removedisabledconflicts */
3092 undo_removedisabledconflicts(solv, &redoq);
3095 /* undo job rule disabling */
3096 for (i = 0; i < disabledq.count; i++)
3097 solver_enablerule(solv, solv->rules + disabledq.elements[i]);
3098 queue_free(&disabledq);
3103 /***********************************************************************/
3104 /* disk usage computations */
3106 /*-------------------------------------------------------------------
3108 * calculate DU changes
3112 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
3116 solver_create_state_maps(solv, &installedmap, 0);
3117 pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
3118 map_free(&installedmap);
3122 /*-------------------------------------------------------------------
3124 * calculate changes in install size
3128 solver_calc_installsizechange(Solver *solv)
3133 solver_create_state_maps(solv, &installedmap, 0);
3134 change = pool_calc_installsizechange(solv->pool, &installedmap);
3135 map_free(&installedmap);
3140 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
3142 pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
3146 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
3149 pool_create_state_maps(solv->pool, &solv->decisionq, &installedmap, 0);
3150 pool_trivial_installable_noobsoletesmap(solv->pool, &installedmap, pkgs, res, solv->noobsoletes.size ? &solv->noobsoletes : 0);
3151 map_free(&installedmap);
3154 /*-------------------------------------------------------------------
3156 * decision introspection
3160 solver_get_decisionlevel(Solver *solv, Id p)
3162 return solv->decisionmap[p];
3166 solver_get_decisionqueue(Solver *solv, Queue *decisionq)
3168 queue_free(decisionq);
3169 queue_init_clone(decisionq, &solv->decisionq);
3173 solver_get_lastdecisionblocklevel(Solver *solv)
3176 if (solv->decisionq.count == 0)
3178 p = solv->decisionq.elements[solv->decisionq.count - 1];
3181 return solv->decisionmap[p] < 0 ? -solv->decisionmap[p] : solv->decisionmap[p];
3185 solver_get_decisionblock(Solver *solv, int level, Queue *decisionq)
3190 queue_empty(decisionq);
3191 for (i = 0; i < solv->decisionq.count; i++)
3193 p = solv->decisionq.elements[i];
3196 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3199 if (i == solv->decisionq.count)
3201 for (i = 0; i < solv->decisionq.count; i++)
3203 p = solv->decisionq.elements[i];
3206 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3207 queue_push(decisionq, p);
3214 solver_describe_decision(Solver *solv, Id p, Id *infop)
3221 if (!solv->decisionmap[p])
3222 return SOLVER_REASON_UNRELATED;
3223 pp = solv->decisionmap[p] < 0 ? -p : p;
3224 for (i = 0; i < solv->decisionq.count; i++)
3225 if (solv->decisionq.elements[i] == pp)
3227 if (i == solv->decisionq.count) /* just in case... */
3228 return SOLVER_REASON_UNRELATED;
3229 why = solv->decisionq_why.elements[i];
3234 return SOLVER_REASON_UNIT_RULE;
3237 if (i < solv->decisioncnt_update)
3242 *infop = SYSTEMSOLVABLE;
3243 return SOLVER_REASON_KEEP_INSTALLED;
3247 return SOLVER_REASON_RESOLVE_JOB;
3249 if (i < solv->decisioncnt_keep)
3251 if (why == 0 && pp < 0)
3252 return SOLVER_REASON_CLEANDEPS_ERASE;
3255 if (why >= solv->updaterules && why < solv->updaterules_end)
3256 *infop = why - solv->updaterules;
3257 else if (why >= solv->featurerules && why < solv->featurerules_end)
3258 *infop = why - solv->featurerules;
3260 return SOLVER_REASON_UPDATE_INSTALLED;
3262 if (i < solv->decisioncnt_resolve)
3264 if (why == 0 && pp < 0)
3265 return SOLVER_REASON_CLEANDEPS_ERASE;
3268 if (why >= solv->updaterules && why < solv->updaterules_end)
3269 *infop = why - solv->updaterules;
3270 else if (why >= solv->featurerules && why < solv->featurerules_end)
3271 *infop = why - solv->featurerules;
3273 return SOLVER_REASON_KEEP_INSTALLED;
3275 if (i < solv->decisioncnt_weak)
3279 return SOLVER_REASON_RESOLVE;
3281 if (solv->decisionq.count < solv->decisioncnt_orphan)
3282 return SOLVER_REASON_WEAKDEP;
3283 return SOLVER_REASON_RESOLVE_ORPHAN;
3288 solver_describe_weakdep_decision(Solver *solv, Id p, Queue *whyq)
3290 Pool *pool = solv->pool;
3292 int level = solv->decisionmap[p];
3299 for (decisionno = 0; decisionno < solv->decisionq.count; decisionno++)
3300 if (solv->decisionq.elements[decisionno] == p)
3302 if (decisionno == solv->decisionq.count)
3304 if (decisionno < solv->decisioncnt_weak || decisionno >= solv->decisioncnt_orphan)
3307 /* 1) list all packages that recommend us */
3308 for (i = 1; i < pool->nsolvables; i++)
3310 Id *recp, rec, pp2, p2;
3311 if (solv->decisionmap[i] < 0 || solv->decisionmap[i] >= level)
3313 s = pool->solvables + i;
3316 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
3318 recp = s->repo->idarraydata + s->recommends;
3319 while ((rec = *recp++) != 0)
3322 FOR_PROVIDES(p2, pp2, rec)
3328 /* if p2 is already installed, this recommends is ignored */
3329 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3335 queue_push(whyq, SOLVER_REASON_RECOMMENDED);
3336 queue_push2(whyq, p2, rec);
3340 /* 2) list all supplements */
3341 s = pool->solvables + p;
3342 if (s->supplements && level > 0)
3344 Id *supp, sup, pp2, p2;
3345 /* this is a hack. to use solver_dep_fulfilled we temporarily clear
3346 * everything above our level in the decisionmap */
3347 for (i = decisionno; i < solv->decisionq.count; i++ )
3349 p2 = solv->decisionq.elements[i];
3351 solv->decisionmap[p2] = -solv->decisionmap[p2];
3353 supp = s->repo->idarraydata + s->supplements;
3354 while ((sup = *supp++) != 0)
3355 if (solver_dep_fulfilled(solv, sup))
3358 /* let's see if this is an easy supp */
3359 FOR_PROVIDES(p2, pp2, sup)
3361 if (solv->ignorealreadyrecommended && solv->installed)
3363 if (pool->solvables[p2].repo == solv->installed)
3366 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3368 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3369 queue_push2(whyq, p2, sup);
3375 /* hard case, just note dependency with no package */
3376 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3377 queue_push2(whyq, 0, sup);
3380 for (i = decisionno; i < solv->decisionq.count; i++)
3382 p2 = solv->decisionq.elements[i];
3384 solv->decisionmap[p2] = -solv->decisionmap[p2];