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->suggestions);
1269 queue_init(&solv->recommendations);
1270 queue_init(&solv->orphaned);
1271 queue_init(&solv->learnt_why);
1272 queue_init(&solv->learnt_pool);
1273 queue_init(&solv->branches);
1274 queue_init(&solv->weakruleq);
1275 queue_init(&solv->ruleassertions);
1277 queue_push(&solv->learnt_pool, 0); /* so that 0 does not describe a proof */
1279 map_init(&solv->recommendsmap, pool->nsolvables);
1280 map_init(&solv->suggestsmap, pool->nsolvables);
1281 map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1282 solv->recommends_index = 0;
1284 solv->decisionmap = (Id *)solv_calloc(pool->nsolvables, sizeof(Id));
1286 solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1287 memset(solv->rules, 0, sizeof(Rule));
1293 /*-------------------------------------------------------------------
1299 solver_free(Solver *solv)
1301 queue_free(&solv->job);
1302 queue_free(&solv->ruletojob);
1303 queue_free(&solv->decisionq);
1304 queue_free(&solv->decisionq_why);
1305 queue_free(&solv->learnt_why);
1306 queue_free(&solv->learnt_pool);
1307 queue_free(&solv->problems);
1308 queue_free(&solv->solutions);
1309 queue_free(&solv->suggestions);
1310 queue_free(&solv->recommendations);
1311 queue_free(&solv->orphaned);
1312 queue_free(&solv->branches);
1313 queue_free(&solv->weakruleq);
1314 queue_free(&solv->ruleassertions);
1316 map_free(&solv->recommendsmap);
1317 map_free(&solv->suggestsmap);
1318 map_free(&solv->noupdate);
1319 map_free(&solv->weakrulemap);
1320 map_free(&solv->noobsoletes);
1322 map_free(&solv->updatemap);
1323 map_free(&solv->fixmap);
1324 map_free(&solv->dupmap);
1325 map_free(&solv->dupinvolvedmap);
1326 map_free(&solv->droporphanedmap);
1327 map_free(&solv->cleandepsmap);
1329 solv_free(solv->decisionmap);
1330 solv_free(solv->rules);
1331 solv_free(solv->watches);
1332 solv_free(solv->obsoletes);
1333 solv_free(solv->obsoletes_data);
1334 solv_free(solv->multiversionupdaters);
1335 solv_free(solv->choicerules_ref);
1340 solver_get_flag(Solver *solv, int flag)
1344 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1345 return solv->allowdowngrade;
1346 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1347 return solv->allowarchchange;
1348 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1349 return solv->allowvendorchange;
1350 case SOLVER_FLAG_ALLOW_UNINSTALL:
1351 return solv->allowuninstall;
1352 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1353 return solv->noupdateprovide;
1354 case SOLVER_FLAG_SPLITPROVIDES:
1355 return solv->dosplitprovides;
1356 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1357 return solv->dontinstallrecommended;
1358 case SOLVER_FLAG_IGNORE_ALREADY_RECOMMENDED:
1359 return solv->ignorealreadyrecommended;
1360 case SOLVER_FLAG_NO_INFARCHCHECK:
1361 return solv->noinfarchcheck;
1369 solver_set_flag(Solver *solv, int flag, int value)
1371 int old = solver_get_flag(solv, flag);
1374 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1375 solv->allowdowngrade = value;
1377 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1378 solv->allowarchchange = value;
1380 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1381 solv->allowvendorchange = value;
1383 case SOLVER_FLAG_ALLOW_UNINSTALL:
1384 solv->allowuninstall = value;
1386 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1387 solv->noupdateprovide = value;
1389 case SOLVER_FLAG_SPLITPROVIDES:
1390 solv->dosplitprovides = value;
1392 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1393 solv->dontinstallrecommended = value;
1395 case SOLVER_FLAG_IGNORE_ALREADY_RECOMMENDED:
1396 solv->ignorealreadyrecommended = value;
1398 case SOLVER_FLAG_NO_INFARCHCHECK:
1399 solv->noinfarchcheck = value;
1408 /*-------------------------------------------------------------------
1412 * all rules have been set up, now actually run the solver
1417 solver_run_sat(Solver *solv, int disablerules, int doweak)
1419 Queue dq; /* local decisionqueue */
1420 Queue dqs; /* local decisionqueue for supplements */
1426 Pool *pool = solv->pool;
1428 int minimizationsteps;
1429 int installedpos = solv->installed ? solv->installed->start : 0;
1431 IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
1433 POOL_DEBUG (SOLV_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1434 for (i = 1; i < solv->nrules; i++)
1435 solver_printruleclass(solv, SOLV_DEBUG_RULE_CREATION, solv->rules + i);
1438 POOL_DEBUG(SOLV_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1440 /* start SAT algorithm */
1442 systemlevel = level + 1;
1443 POOL_DEBUG(SOLV_DEBUG_SOLVER, "solving...\n");
1449 * here's the main loop:
1450 * 1) propagate new decisions (only needed once)
1452 * 3) try to keep installed packages
1453 * 4) fulfill all unresolved rules
1454 * 5) install recommended packages
1455 * 6) minimalize solution if we had choices
1456 * if we encounter a problem, we rewind to a safe level and restart
1460 minimizationsteps = 0;
1464 * initial propagation of the assertions
1468 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1469 if ((r = propagate(solv, level)) != 0)
1471 if (analyze_unsolvable(solv, r, disablerules))
1474 break; /* unsolvable */
1479 * resolve jobs first
1481 if (level < systemlevel)
1483 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving job rules\n");
1484 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1487 if (r->d < 0) /* ignore disabled rules */
1490 FOR_RULELITERALS(l, dp, r)
1494 if (solv->decisionmap[-l] <= 0)
1499 if (solv->decisionmap[l] > 0)
1501 if (solv->decisionmap[l] == 0)
1507 /* prune to installed if not updating */
1508 if (dq.count > 1 && solv->installed && !solv->updatemap_all)
1511 for (j = k = 0; j < dq.count; j++)
1513 Solvable *s = pool->solvables + dq.elements[j];
1514 if (s->repo == solv->installed)
1516 dq.elements[k++] = dq.elements[j];
1517 if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
1519 k = 0; /* package wants to be updated, do not prune */
1528 level = selectandinstall(solv, level, &dq, disablerules, i);
1531 if (level <= olevel)
1535 break; /* unsolvable */
1536 systemlevel = level + 1;
1537 if (i < solv->jobrules_end)
1539 solv->decisioncnt_update = solv->decisionq.count;
1540 solv->decisioncnt_keep = solv->decisionq.count;
1544 * installed packages
1546 if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1548 Repo *installed = solv->installed;
1551 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving installed packages\n");
1552 /* we use two passes if we need to update packages
1553 * to create a better user experience */
1554 for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1556 int passlevel = level;
1558 solv->decisioncnt_keep = solv->decisionq.count;
1559 /* start with installedpos, the position that gave us problems last time */
1560 for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1565 if (i == installed->end)
1566 i = installed->start;
1567 s = pool->solvables + i;
1568 if (s->repo != installed)
1571 if (solv->decisionmap[i] > 0)
1573 if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1574 continue; /* updates first */
1575 r = solv->rules + solv->updaterules + (i - installed->start);
1577 if (!rr->p || rr->d < 0) /* disabled -> look at feature rule */
1578 rr -= solv->installed->end - solv->installed->start;
1579 if (!rr->p) /* identical to update rule? */
1582 continue; /* orpaned package */
1584 /* XXX: noupdate check is probably no longer needed, as all jobs should
1585 * already be satisfied */
1586 /* Actually we currently still need it because of erase jobs */
1587 /* if noupdate is set we do not look at update candidates */
1589 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))
1591 if (solv->noobsoletes.size && solv->multiversionupdaters
1592 && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1594 /* special multiversion handling, make sure best version is chosen */
1596 while ((p = pool->whatprovidesdata[d++]) != 0)
1597 if (solv->decisionmap[p] >= 0)
1599 policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1601 if (p != i && solv->decisionmap[p] == 0)
1603 rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1604 if (!rr->p) /* update rule == feature rule? */
1605 rr = rr - solv->featurerules + solv->updaterules;
1613 /* update to best package */
1614 FOR_RULELITERALS(p, dp, rr)
1616 if (solv->decisionmap[p] > 0)
1618 dq.count = 0; /* already fulfilled */
1621 if (!solv->decisionmap[p])
1626 /* install best version */
1630 level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1637 if (level <= olevel)
1639 if (level == 1 || level < passlevel)
1640 break; /* trouble */
1642 n = installed->start; /* redo all */
1648 /* if still undecided keep package */
1649 if (solv->decisionmap[i] == 0)
1652 if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
1654 POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
1655 level = setpropagatelearn(solv, level, -i, disablerules, 0);
1659 POOL_DEBUG(SOLV_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
1660 level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1664 if (level <= olevel)
1666 if (level == 1 || level < passlevel)
1667 break; /* trouble */
1669 n = installed->start; /* redo all */
1672 continue; /* retry with learnt rule */
1676 if (n < installed->end)
1678 installedpos = i; /* retry problem solvable next time */
1679 break; /* ran into trouble */
1681 installedpos = installed->start; /* reset installedpos */
1684 break; /* unsolvable */
1685 systemlevel = level + 1;
1687 continue; /* had trouble, retry */
1690 if (level < systemlevel)
1691 systemlevel = level;
1696 solv->decisioncnt_resolve = solv->decisionq.count;
1697 POOL_DEBUG(SOLV_DEBUG_POLICY, "deciding unresolved rules\n");
1698 for (i = 1, n = 1; n < solv->nrules; i++, n++)
1700 if (i == solv->nrules)
1702 r = solv->rules + i;
1703 if (r->d < 0) /* ignore disabled rules */
1708 /* binary or unary rule */
1709 /* need two positive undecided literals */
1710 if (r->p < 0 || r->w2 <= 0)
1712 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1714 queue_push(&dq, r->p);
1715 queue_push(&dq, r->w2);
1720 * all negative literals are installed
1721 * no positive literal is installed
1722 * i.e. the rule is not fulfilled and we
1723 * just need to decide on the positive literals
1727 if (solv->decisionmap[-r->p] <= 0)
1732 if (solv->decisionmap[r->p] > 0)
1734 if (solv->decisionmap[r->p] == 0)
1735 queue_push(&dq, r->p);
1737 dp = pool->whatprovidesdata + r->d;
1738 while ((p = *dp++) != 0)
1742 if (solv->decisionmap[-p] <= 0)
1747 if (solv->decisionmap[p] > 0)
1749 if (solv->decisionmap[p] == 0)
1756 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
1758 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "unfulfilled ");
1759 solver_printruleclass(solv, SOLV_DEBUG_PROPAGATE, r);
1761 /* dq.count < 2 cannot happen as this means that
1762 * the rule is unit */
1763 assert(dq.count > 1);
1766 level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
1768 break; /* unsolvable */
1769 if (level < systemlevel || level == 1)
1770 break; /* trouble */
1771 /* something changed, so look at all rules again */
1775 if (n != solv->nrules) /* ran into trouble? */
1778 break; /* unsolvable */
1779 continue; /* start over */
1782 /* at this point we have a consistent system. now do the extras... */
1784 solv->decisioncnt_weak = solv->decisionq.count;
1789 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended packages\n");
1790 queue_empty(&dq); /* recommended packages */
1791 queue_empty(&dqs); /* supplemented packages */
1792 for (i = 1; i < pool->nsolvables; i++)
1794 if (solv->decisionmap[i] < 0)
1796 if (solv->decisionmap[i] > 0)
1798 /* installed, check for recommends */
1799 Id *recp, rec, pp, p;
1800 s = pool->solvables + i;
1801 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
1803 /* XXX need to special case AND ? */
1806 recp = s->repo->idarraydata + s->recommends;
1807 while ((rec = *recp++) != 0)
1810 FOR_PROVIDES(p, pp, rec)
1812 if (solv->decisionmap[p] > 0)
1817 else if (solv->decisionmap[p] == 0)
1819 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))))
1821 queue_pushunique(&dq, p);
1829 s = pool->solvables + i;
1830 if (!s->supplements)
1832 if (!pool_installable(pool, s))
1834 if (!solver_is_supplementing(solv, s))
1836 if (solv->dupmap_all && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
1838 queue_push(&dqs, i);
1842 /* filter out all packages obsoleted by installed packages */
1843 /* this is no longer needed if we have reverse obsoletes */
1844 if ((dqs.count || dq.count) && solv->installed)
1847 Id obs, *obsp, po, ppo;
1849 map_init(&obsmap, pool->nsolvables);
1850 for (p = solv->installed->start; p < solv->installed->end; p++)
1852 s = pool->solvables + p;
1853 if (s->repo != solv->installed || !s->obsoletes)
1855 if (solv->decisionmap[p] <= 0)
1857 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
1859 obsp = s->repo->idarraydata + s->obsoletes;
1860 /* foreach obsoletes */
1861 while ((obs = *obsp++) != 0)
1862 FOR_PROVIDES(po, ppo, obs)
1863 MAPSET(&obsmap, po);
1865 for (i = j = 0; i < dqs.count; i++)
1866 if (!MAPTST(&obsmap, dqs.elements[i]))
1867 dqs.elements[j++] = dqs.elements[i];
1869 for (i = j = 0; i < dq.count; i++)
1870 if (!MAPTST(&obsmap, dq.elements[i]))
1871 dq.elements[j++] = dq.elements[i];
1876 /* filter out all already supplemented packages if requested */
1877 if (solv->ignorealreadyrecommended && dqs.count)
1879 /* turn off all new packages */
1880 for (i = 0; i < solv->decisionq.count; i++)
1882 p = solv->decisionq.elements[i];
1885 s = pool->solvables + p;
1886 if (s->repo && s->repo != solv->installed)
1887 solv->decisionmap[p] = -solv->decisionmap[p];
1889 /* filter out old supplements */
1890 for (i = j = 0; i < dqs.count; i++)
1892 p = dqs.elements[i];
1893 s = pool->solvables + p;
1894 if (!s->supplements)
1896 if (!solver_is_supplementing(solv, s))
1897 dqs.elements[j++] = p;
1900 /* undo turning off */
1901 for (i = 0; i < solv->decisionq.count; i++)
1903 p = solv->decisionq.elements[i];
1906 s = pool->solvables + p;
1907 if (s->repo && s->repo != solv->installed)
1908 solv->decisionmap[p] = -solv->decisionmap[p];
1912 /* multiversion doesn't mix well with supplements.
1913 * filter supplemented packages where we already decided
1914 * to install a different version (see bnc#501088) */
1915 if (dqs.count && solv->noobsoletes.size)
1917 for (i = j = 0; i < dqs.count; i++)
1919 p = dqs.elements[i];
1920 if (MAPTST(&solv->noobsoletes, p))
1923 s = pool->solvables + p;
1924 FOR_PROVIDES(p2, pp2, s->name)
1925 if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
1928 continue; /* ignore this package */
1930 dqs.elements[j++] = p;
1935 /* make dq contain both recommended and supplemented pkgs */
1938 for (i = 0; i < dqs.count; i++)
1939 queue_pushunique(&dq, dqs.elements[i]);
1945 int decisioncount = solv->decisionq.count;
1949 /* simple case, just one package. no need to choose to best version */
1952 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
1954 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
1955 level = setpropagatelearn(solv, level, p, 0, 0);
1958 continue; /* back to main loop */
1961 /* filter packages, this gives us the best versions */
1962 policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
1964 /* create map of result */
1965 map_init(&dqmap, pool->nsolvables);
1966 for (i = 0; i < dq.count; i++)
1967 MAPSET(&dqmap, dq.elements[i]);
1969 /* install all supplemented packages */
1970 for (i = 0; i < dqs.count; i++)
1972 p = dqs.elements[i];
1973 if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
1975 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
1977 level = setpropagatelearn(solv, level, p, 0, 0);
1978 if (level <= olevel)
1981 if (i < dqs.count || solv->decisionq.count < decisioncount)
1989 /* install all recommended packages */
1990 /* more work as we want to created branches if multiple
1991 * choices are valid */
1992 for (i = 0; i < decisioncount; i++)
1995 p = solv->decisionq.elements[i];
1998 s = pool->solvables + p;
1999 if (!s->repo || (solv->ignorealreadyrecommended && s->repo == solv->installed))
2003 recp = s->repo->idarraydata + s->recommends;
2004 while ((rec = *recp++) != 0)
2007 FOR_PROVIDES(p, pp, rec)
2009 if (solv->decisionmap[p] > 0)
2014 else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
2015 queue_pushunique(&dq, p);
2021 /* multiple candidates, open a branch */
2022 for (i = 1; i < dq.count; i++)
2023 queue_push(&solv->branches, dq.elements[i]);
2024 queue_push(&solv->branches, -level);
2027 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2029 level = setpropagatelearn(solv, level, p, 0, 0);
2030 if (level <= olevel || solv->decisionq.count < decisioncount)
2031 break; /* we had to revert some decisions */
2034 break; /* had a problem above, quit loop */
2039 continue; /* back to main loop so that all deps are checked */
2043 solv->decisioncnt_orphan = solv->decisionq.count;
2044 if (solv->dupmap_all && solv->installed)
2046 int installedone = 0;
2048 /* let's see if we can install some unsupported package */
2049 POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding orphaned packages\n");
2050 for (i = 0; i < solv->orphaned.count; i++)
2052 p = solv->orphaned.elements[i];
2053 if (solv->decisionmap[p])
2054 continue; /* already decided */
2056 if (solv->droporphanedmap_all)
2058 if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
2060 POOL_DEBUG(SOLV_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
2061 level = setpropagatelearn(solv, level, p, 0, 0);
2068 if (installedone || i < solv->orphaned.count)
2072 continue; /* back to main loop */
2074 for (i = 0; i < solv->orphaned.count; i++)
2076 p = solv->orphaned.elements[i];
2077 if (solv->decisionmap[p])
2078 continue; /* already decided */
2079 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2081 level = setpropagatelearn(solv, level, -p, 0, 0);
2085 if (i < solv->orphaned.count)
2089 continue; /* back to main loop */
2093 if (solv->solution_callback)
2095 solv->solution_callback(solv, solv->solution_callback_data);
2096 if (solv->branches.count)
2098 int i = solv->branches.count - 1;
2099 int l = -solv->branches.elements[i];
2103 if (solv->branches.elements[i - 1] < 0)
2105 p = solv->branches.elements[i];
2106 POOL_DEBUG(SOLV_DEBUG_SOLVER, "branching with %s\n", pool_solvid2str(pool, p));
2108 for (j = i + 1; j < solv->branches.count; j++)
2109 queue_push(&dq, solv->branches.elements[j]);
2110 solv->branches.count = i;
2112 revert(solv, level);
2114 for (j = 0; j < dq.count; j++)
2115 queue_push(&solv->branches, dq.elements[j]);
2117 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2119 level = setpropagatelearn(solv, level, p, disablerules, why);
2124 /* all branches done, we're finally finished */
2128 /* auto-minimization step */
2129 if (solv->branches.count)
2131 int l = 0, lasti = -1, lastl = -1;
2135 for (i = solv->branches.count - 1; i >= 0; i--)
2137 p = solv->branches.elements[i];
2140 else if (p > 0 && solv->decisionmap[p] > l + 1)
2148 /* kill old solvable so that we do not loop */
2149 p = solv->branches.elements[lasti];
2150 solv->branches.elements[lasti] = 0;
2151 POOL_DEBUG(SOLV_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, pool_solvid2str(pool, p));
2152 minimizationsteps++;
2155 revert(solv, level);
2156 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2159 level = setpropagatelearn(solv, level, p, disablerules, why);
2162 continue; /* back to main loop */
2165 /* no minimization found, we're finally finished! */
2169 POOL_DEBUG(SOLV_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2171 POOL_DEBUG(SOLV_DEBUG_STATS, "done solving.\n\n");
2177 solv->decisioncnt_update = solv->decisionq.count;
2178 solv->decisioncnt_keep = solv->decisionq.count;
2179 solv->decisioncnt_resolve = solv->decisionq.count;
2180 solv->decisioncnt_weak = solv->decisionq.count;
2181 solv->decisioncnt_orphan = solv->decisionq.count;
2184 solver_printdecisionq(solv, SOLV_DEBUG_RESULT);
2189 /*-------------------------------------------------------------------
2191 * remove disabled conflicts
2193 * purpose: update the decisionmap after some rules were disabled.
2194 * this is used to calculate the suggested/recommended package list.
2195 * Also returns a "removed" list to undo the discisionmap changes.
2199 removedisabledconflicts(Solver *solv, Queue *removed)
2201 Pool *pool = solv->pool;
2206 Id *decisionmap = solv->decisionmap;
2208 queue_empty(removed);
2209 for (i = 0; i < solv->decisionq.count; i++)
2211 p = solv->decisionq.elements[i];
2213 continue; /* conflicts only, please */
2214 why = solv->decisionq_why.elements[i];
2217 /* no rule involved, must be a orphan package drop */
2220 /* we never do conflicts on free decisions, so there
2221 * must have been an unit rule */
2223 r = solv->rules + why;
2224 if (r->d < 0 && decisionmap[-p])
2226 /* rule is now disabled, remove from decisionmap */
2227 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2228 queue_push(removed, -p);
2229 queue_push(removed, decisionmap[-p]);
2230 decisionmap[-p] = 0;
2233 if (!removed->count)
2235 /* we removed some confliced packages. some of them might still
2236 * be in conflict, so search for unit rules and re-conflict */
2238 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2240 if (i == solv->nrules)
2243 r = solv->rules + i;
2249 if (r->p < 0 && !decisionmap[-r->p])
2255 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2257 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2262 if (r->p < 0 && decisionmap[-r->p] == 0)
2264 if (new || DECISIONMAP_FALSE(r->p))
2266 dp = pool->whatprovidesdata + r->d;
2267 while ((p = *dp++) != 0)
2269 if (new && p == new)
2271 if (p < 0 && decisionmap[-p] == 0)
2280 else if (!DECISIONMAP_FALSE(p))
2290 POOL_DEBUG(SOLV_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2291 decisionmap[-new] = -1;
2293 n = 0; /* redo all rules */
2299 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2302 for (i = 0; i < removed->count; i += 2)
2303 solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2307 /*-------------------------------------------------------------------
2309 * weaken solvable dependencies
2313 weaken_solvable_deps(Solver *solv, Id p)
2318 for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2322 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2323 continue; /* conflict */
2324 queue_push(&solv->weakruleq, i);
2329 /********************************************************************/
2334 solver_findrecommendedsuggested(Solver *solv)
2336 Pool *pool = solv->pool;
2337 Queue redoq, disabledq;
2343 map_init(&obsmap, pool->nsolvables);
2344 if (solv->installed)
2346 Id obs, *obsp, p, po, ppo;
2347 for (p = solv->installed->start; p < solv->installed->end; p++)
2349 s = pool->solvables + p;
2350 if (s->repo != solv->installed || !s->obsoletes)
2352 if (solv->decisionmap[p] <= 0)
2354 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
2356 obsp = s->repo->idarraydata + s->obsoletes;
2357 /* foreach obsoletes */
2358 while ((obs = *obsp++) != 0)
2359 FOR_PROVIDES(po, ppo, obs)
2360 MAPSET(&obsmap, po);
2365 queue_init(&disabledq);
2367 /* disable all erase jobs (including weak "keep uninstalled" rules) */
2368 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2370 if (r->d < 0) /* disabled ? */
2372 if (r->p >= 0) /* install job? */
2374 queue_push(&disabledq, i);
2375 solver_disablerule(solv, r);
2381 enabledisablelearntrules(solv);
2382 removedisabledconflicts(solv, &redoq);
2386 * find recommended packages
2389 /* if redoq.count == 0 we already found all recommended in the
2393 Id rec, *recp, p, pp;
2395 /* create map of all recommened packages */
2396 solv->recommends_index = -1;
2397 MAPZERO(&solv->recommendsmap);
2399 /* put all packages the solver already chose in the map */
2400 if (solv->decisioncnt_weak)
2402 for (i = solv->decisioncnt_weak; i < solv->decisioncnt_orphan; i++)
2405 why = solv->decisionq_why.elements[i];
2407 continue; /* forced by unit rule */
2408 p = solv->decisionq.elements[i];
2411 MAPSET(&solv->recommendsmap, p);
2415 for (i = 0; i < solv->decisionq.count; i++)
2417 p = solv->decisionq.elements[i];
2420 s = pool->solvables + p;
2423 recp = s->repo->idarraydata + s->recommends;
2424 while ((rec = *recp++) != 0)
2426 FOR_PROVIDES(p, pp, rec)
2427 if (solv->decisionmap[p] > 0)
2431 if (!solv->dontshowinstalledrecommended)
2433 FOR_PROVIDES(p, pp, rec)
2434 if (solv->decisionmap[p] > 0)
2435 MAPSET(&solv->recommendsmap, p);
2437 continue; /* p != 0: already fulfilled */
2439 FOR_PROVIDES(p, pp, rec)
2440 MAPSET(&solv->recommendsmap, p);
2444 for (i = 1; i < pool->nsolvables; i++)
2446 if (solv->decisionmap[i] < 0)
2448 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2450 if (MAPTST(&obsmap, i))
2452 s = pool->solvables + i;
2453 if (!MAPTST(&solv->recommendsmap, i))
2455 if (!s->supplements)
2457 if (!pool_installable(pool, s))
2459 if (!solver_is_supplementing(solv, s))
2462 queue_push(&solv->recommendations, i);
2464 /* we use MODE_SUGGEST here so that repo prio is ignored */
2465 policy_filter_unwanted(solv, &solv->recommendations, POLICY_MODE_SUGGEST);
2469 * find suggested packages
2474 Id sug, *sugp, p, pp;
2476 /* create map of all suggests that are still open */
2477 solv->recommends_index = -1;
2478 MAPZERO(&solv->suggestsmap);
2479 for (i = 0; i < solv->decisionq.count; i++)
2481 p = solv->decisionq.elements[i];
2484 s = pool->solvables + p;
2487 sugp = s->repo->idarraydata + s->suggests;
2488 while ((sug = *sugp++) != 0)
2490 FOR_PROVIDES(p, pp, sug)
2491 if (solv->decisionmap[p] > 0)
2495 if (!solv->dontshowinstalledrecommended)
2497 FOR_PROVIDES(p, pp, sug)
2498 if (solv->decisionmap[p] > 0)
2499 MAPSET(&solv->suggestsmap, p);
2501 continue; /* already fulfilled */
2503 FOR_PROVIDES(p, pp, sug)
2504 MAPSET(&solv->suggestsmap, p);
2508 for (i = 1; i < pool->nsolvables; i++)
2510 if (solv->decisionmap[i] < 0)
2512 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2514 if (MAPTST(&obsmap, i))
2516 s = pool->solvables + i;
2517 if (!MAPTST(&solv->suggestsmap, i))
2521 if (!pool_installable(pool, s))
2523 if (!solver_is_enhancing(solv, s))
2526 queue_push(&solv->suggestions, i);
2528 policy_filter_unwanted(solv, &solv->suggestions, POLICY_MODE_SUGGEST);
2531 /* undo removedisabledconflicts */
2533 undo_removedisabledconflicts(solv, &redoq);
2536 /* undo job rule disabling */
2537 for (i = 0; i < disabledq.count; i++)
2538 solver_enablerule(solv, solv->rules + disabledq.elements[i]);
2539 queue_free(&disabledq);
2544 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap)
2547 Id how, what, select;
2549 for (i = 0; i < job->count; i += 2)
2551 how = job->elements[i];
2552 if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
2554 what = job->elements[i + 1];
2555 select = how & SOLVER_SELECTMASK;
2556 if (!noobsmap->size)
2557 map_grow(noobsmap, pool->nsolvables);
2558 FOR_JOB_SELECT(p, pp, select, what)
2559 MAPSET(noobsmap, p);
2564 * add a rule created by a job, record job number and weak flag
2567 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
2569 solver_addrule(solv, p, d);
2570 queue_push(&solv->ruletojob, job);
2572 queue_push(&solv->weakruleq, solv->nrules - 1);
2582 solver_solve(Solver *solv, Queue *job)
2584 Pool *pool = solv->pool;
2585 Repo *installed = solv->installed;
2588 Map addedmap; /* '1' == have rpm-rules for solvable */
2589 Map installcandidatemap;
2590 Id how, what, select, name, weak, p, pp, d;
2594 int now, solve_start;
2597 solve_start = solv_timems(0);
2599 /* log solver options */
2600 POOL_DEBUG(SOLV_DEBUG_STATS, "solver started\n");
2601 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);
2602 POOL_DEBUG(SOLV_DEBUG_STATS, "distupgrade=%d distupgrade_removeunsupported=%d\n", solv->distupgrade, solv->distupgrade_removeunsupported);
2603 POOL_DEBUG(SOLV_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allowarchchange, solv->allowvendorchange);
2604 POOL_DEBUG(SOLV_DEBUG_STATS, "promoteepoch=%d, allowselfconflicts=%d\n", pool->promoteepoch, pool->allowselfconflicts);
2605 POOL_DEBUG(SOLV_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2606 POOL_DEBUG(SOLV_DEBUG_STATS, "dontinstallrecommended=%d, ignorealreadyrecommended=%d, dontshowinstalledrecommended=%d\n", solv->dontinstallrecommended, solv->ignorealreadyrecommended, solv->dontshowinstalledrecommended);
2608 /* create whatprovides if not already there */
2609 if (!pool->whatprovides)
2610 pool_createwhatprovides(pool);
2612 /* create obsolete index */
2613 policy_create_obsolete_index(solv);
2616 queue_free(&solv->job);
2617 queue_init_clone(&solv->job, job);
2619 /* initialize with legacy values */
2620 solv->fixmap_all = solv->fixsystem;
2621 solv->updatemap_all = solv->updatesystem;
2622 solv->droporphanedmap_all = solv->distupgrade_removeunsupported;
2623 solv->dupmap_all = solv->distupgrade;
2626 * create basic rule set of all involved packages
2627 * use addedmap bitmap to make sure we don't create rules twice
2630 /* create noobsolete map if needed */
2631 solver_calculate_noobsmap(pool, job, &solv->noobsoletes);
2633 map_init(&addedmap, pool->nsolvables);
2634 MAPSET(&addedmap, SYSTEMSOLVABLE);
2636 map_init(&installcandidatemap, pool->nsolvables);
2639 now = solv_timems(0);
2641 * create rules for all package that could be involved with the solving
2642 * so called: rpm rules
2647 /* check for update/verify jobs as they need to be known early */
2648 for (i = 0; i < job->count; i += 2)
2650 how = job->elements[i];
2651 what = job->elements[i + 1];
2652 select = how & SOLVER_SELECTMASK;
2653 switch (how & SOLVER_JOBMASK)
2656 if (select == SOLVER_SOLVABLE_ALL)
2657 solv->fixmap_all = 1;
2658 FOR_JOB_SELECT(p, pp, select, what)
2660 s = pool->solvables + p;
2661 if (!solv->installed || s->repo != solv->installed)
2663 if (!solv->fixmap.size)
2664 map_grow(&solv->fixmap, solv->installed->end - solv->installed->start);
2665 MAPSET(&solv->fixmap, p - solv->installed->start);
2669 if (select == SOLVER_SOLVABLE_ALL)
2670 solv->updatemap_all = 1;
2671 FOR_JOB_SELECT(p, pp, select, what)
2673 s = pool->solvables + p;
2674 if (!solv->installed || s->repo != solv->installed)
2676 if (!solv->updatemap.size)
2677 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
2678 MAPSET(&solv->updatemap, p - solv->installed->start);
2686 oldnrules = solv->nrules;
2687 FOR_REPO_SOLVABLES(installed, p, s)
2688 solver_addrpmrulesforsolvable(solv, s, &addedmap);
2689 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
2690 oldnrules = solv->nrules;
2691 FOR_REPO_SOLVABLES(installed, p, s)
2692 solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
2693 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
2697 * create rules for all packages involved in the job
2698 * (to be installed or removed)
2701 oldnrules = solv->nrules;
2702 for (i = 0; i < job->count; i += 2)
2704 how = job->elements[i];
2705 what = job->elements[i + 1];
2706 select = how & SOLVER_SELECTMASK;
2708 switch (how & SOLVER_JOBMASK)
2710 case SOLVER_INSTALL:
2711 FOR_JOB_SELECT(p, pp, select, what)
2713 MAPSET(&installcandidatemap, p);
2714 solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
2717 case SOLVER_DISTUPGRADE:
2718 if (select == SOLVER_SOLVABLE_ALL)
2720 solv->dupmap_all = 1;
2721 solv->updatemap_all = 1;
2723 if (!solv->dupmap_all)
2730 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
2734 * add rules for suggests, enhances
2736 oldnrules = solv->nrules;
2737 solver_addrpmrulesforweak(solv, &addedmap);
2738 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
2741 * first pass done, we now have all the rpm rules we need.
2742 * unify existing rules before going over all job rules and
2744 * at this point the system is always solvable,
2745 * as an empty system (remove all packages) is a valid solution
2748 IF_POOLDEBUG (SOLV_DEBUG_STATS)
2750 int possible = 0, installable = 0;
2751 for (i = 1; i < pool->nsolvables; i++)
2753 if (pool_installable(pool, pool->solvables + i))
2755 if (MAPTST(&addedmap, i))
2758 POOL_DEBUG(SOLV_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
2761 solver_unifyrules(solv); /* remove duplicate rpm rules */
2762 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
2764 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule memory usage: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
2765 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule creation took %d ms\n", solv_timems(now));
2767 /* create dup maps if needed. We need the maps early to create our
2770 solver_createdupmaps(solv);
2773 * create feature rules
2775 * foreach installed:
2776 * create assertion (keep installed, if no update available)
2778 * create update rule (A|update1(A)|update2(A)|...)
2780 * those are used later on to keep a version of the installed packages in
2784 solv->featurerules = solv->nrules; /* mark start of feature rules */
2787 /* foreach possibly installed solvable */
2788 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2790 if (s->repo != installed)
2792 solver_addrule(solv, 0, 0); /* create dummy rule */
2795 solver_addupdaterule(solv, s, 1); /* allow s to be updated */
2797 /* make sure we accounted for all rules */
2798 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
2800 solv->featurerules_end = solv->nrules;
2803 * Add update rules for installed solvables
2805 * almost identical to feature rules
2806 * except that downgrades/archchanges/vendorchanges are not allowed
2809 solv->updaterules = solv->nrules;
2812 { /* foreach installed solvables */
2813 /* we create all update rules, but disable some later on depending on the job */
2814 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2818 if (s->repo != installed)
2820 solver_addrule(solv, 0, 0); /* create dummy rule */
2823 solver_addupdaterule(solv, s, 0); /* allowall = 0: downgrades not allowed */
2825 * check for and remove duplicate
2827 r = solv->rules + solv->nrules - 1; /* r: update rule */
2828 sr = r - (installed->end - installed->start); /* sr: feature rule */
2829 /* it's orphaned if there is no feature rule or the feature rule
2830 * consists just of the installed package */
2831 if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
2832 queue_push(&solv->orphaned, i);
2835 assert(solv->dupmap_all && !sr->p);
2838 if (!solver_samerule(solv, r, sr))
2840 /* identical rule, kill unneeded one */
2841 if (solv->allowuninstall)
2843 /* keep feature rule, make it weak */
2844 memset(r, 0, sizeof(*r));
2845 queue_push(&solv->weakruleq, sr - solv->rules);
2849 /* keep update rule */
2850 memset(sr, 0, sizeof(*sr));
2853 else if (solv->allowuninstall)
2855 /* make both feature and update rule weak */
2856 queue_push(&solv->weakruleq, r - solv->rules);
2857 queue_push(&solv->weakruleq, sr - solv->rules);
2860 solver_disablerule(solv, sr);
2862 /* consistency check: we added a rule for _every_ installed solvable */
2863 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
2865 solv->updaterules_end = solv->nrules;
2869 * now add all job rules
2872 solv->jobrules = solv->nrules;
2873 for (i = 0; i < job->count; i += 2)
2875 oldnrules = solv->nrules;
2877 how = job->elements[i];
2878 what = job->elements[i + 1];
2879 weak = how & SOLVER_WEAK;
2880 select = how & SOLVER_SELECTMASK;
2881 switch (how & SOLVER_JOBMASK)
2883 case SOLVER_INSTALL:
2884 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2885 if (select == SOLVER_SOLVABLE)
2893 FOR_JOB_SELECT(p, pp, select, what)
2897 /* no candidate found, make this an impossible rule */
2898 queue_push(&q, -SYSTEMSOLVABLE);
2900 p = queue_shift(&q); /* get first candidate */
2901 d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q); /* internalize */
2903 solver_addjobrule(solv, p, d, i, weak);
2906 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
2907 if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && solv->installed)
2908 map_grow(&solv->cleandepsmap, solv->installed->end - solv->installed->start);
2909 if (select == SOLVER_SOLVABLE && solv->installed && pool->solvables[what].repo == solv->installed)
2911 /* special case for "erase a specific solvable": we also
2912 * erase all other solvables with that name, so that they
2913 * don't get picked up as replacement */
2914 /* XXX: look also at packages that obsolete this package? */
2915 name = pool->solvables[what].name;
2916 FOR_PROVIDES(p, pp, name)
2920 s = pool->solvables + p;
2921 if (s->name != name)
2923 /* keep other versions installed */
2924 if (s->repo == solv->installed)
2926 /* keep installcandidates of other jobs */
2927 if (MAPTST(&installcandidatemap, p))
2929 solver_addjobrule(solv, -p, 0, i, weak); /* remove by id */
2932 FOR_JOB_SELECT(p, pp, select, what)
2933 solver_addjobrule(solv, -p, 0, i, weak);
2937 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2940 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2942 case SOLVER_WEAKENDEPS:
2943 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2944 if (select != SOLVER_SOLVABLE)
2946 s = pool->solvables + what;
2947 weaken_solvable_deps(solv, what);
2949 case SOLVER_NOOBSOLETES:
2950 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2953 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2954 FOR_JOB_SELECT(p, pp, select, what)
2956 s = pool->solvables + p;
2957 solver_addjobrule(solv, installed && s->repo == installed ? p : -p, 0, i, weak);
2960 case SOLVER_DISTUPGRADE:
2961 POOL_DEBUG(SOLV_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
2963 case SOLVER_DROP_ORPHANED:
2964 POOL_DEBUG(SOLV_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
2965 if (select == SOLVER_SOLVABLE_ALL)
2966 solv->droporphanedmap_all = 1;
2967 FOR_JOB_SELECT(p, pp, select, what)
2969 s = pool->solvables + p;
2970 if (!installed || s->repo != installed)
2972 if (!solv->droporphanedmap.size)
2973 map_grow(&solv->droporphanedmap, installed->end - installed->start);
2974 MAPSET(&solv->droporphanedmap, p - installed->start);
2977 case SOLVER_USERINSTALLED:
2978 POOL_DEBUG(SOLV_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
2981 POOL_DEBUG(SOLV_DEBUG_JOB, "job: unknown job\n");
2989 IF_POOLDEBUG (SOLV_DEBUG_JOB)
2992 if (solv->nrules == oldnrules)
2993 POOL_DEBUG(SOLV_DEBUG_JOB, " - no rule created\n");
2994 for (j = oldnrules; j < solv->nrules; j++)
2996 POOL_DEBUG(SOLV_DEBUG_JOB, " - job ");
2997 solver_printrule(solv, SOLV_DEBUG_JOB, solv->rules + j);
3001 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
3002 solv->jobrules_end = solv->nrules;
3004 /* now create infarch and dup rules */
3005 if (!solv->noinfarchcheck)
3007 solver_addinfarchrules(solv, &addedmap);
3008 if (pool->obsoleteusescolors)
3010 /* currently doesn't work well with infarch rules, so make
3012 for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
3013 queue_push(&solv->weakruleq, i);
3017 solv->infarchrules = solv->infarchrules_end = solv->nrules;
3021 solver_addduprules(solv, &addedmap);
3022 solver_freedupmaps(solv); /* no longer needed */
3025 solv->duprules = solv->duprules_end = solv->nrules;
3028 solver_addchoicerules(solv);
3030 solv->choicerules = solv->choicerules_end = solv->nrules;
3032 /* all rules created
3033 * --------------------------------------------------------------
3034 * prepare for solving
3037 /* free unneeded memory */
3038 map_free(&addedmap);
3039 map_free(&installcandidatemap);
3042 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);
3044 /* create weak map */
3045 map_init(&solv->weakrulemap, solv->nrules);
3046 for (i = 0; i < solv->weakruleq.count; i++)
3048 p = solv->weakruleq.elements[i];
3049 MAPSET(&solv->weakrulemap, p);
3052 /* all new rules are learnt after this point */
3053 solv->learntrules = solv->nrules;
3055 /* create watches chains */
3058 /* create assertion index. it is only used to speed up
3059 * makeruledecsions() a bit */
3060 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
3061 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
3062 queue_push(&solv->ruleassertions, i);
3064 /* disable update rules that conflict with our job */
3065 solver_disablepolicyrules(solv);
3067 /* make initial decisions based on assertion rules */
3068 makeruledecisions(solv);
3069 POOL_DEBUG(SOLV_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
3072 * ********************************************
3074 * ********************************************
3077 now = solv_timems(0);
3078 solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
3079 POOL_DEBUG(SOLV_DEBUG_STATS, "solver took %d ms\n", solv_timems(now));
3082 * calculate recommended/suggested packages
3084 solver_findrecommendedsuggested(solv);
3087 * prepare solution queue if there were problems
3089 solver_prepare_solutions(solv);
3091 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);
3092 POOL_DEBUG(SOLV_DEBUG_STATS, "solver_solve took %d ms\n", solv_timems(solve_start));
3094 /* return number of problems */
3095 return solv->problems.count ? solv->problems.count / 2 : 0;
3099 solver_create_transaction(Solver *solv)
3101 return transaction_create_decisionq(solv->pool, &solv->decisionq, &solv->noobsoletes);
3104 /***********************************************************************/
3105 /* disk usage computations */
3107 /*-------------------------------------------------------------------
3109 * calculate DU changes
3113 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
3117 solver_create_state_maps(solv, &installedmap, 0);
3118 pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
3119 map_free(&installedmap);
3123 /*-------------------------------------------------------------------
3125 * calculate changes in install size
3129 solver_calc_installsizechange(Solver *solv)
3134 solver_create_state_maps(solv, &installedmap, 0);
3135 change = pool_calc_installsizechange(solv->pool, &installedmap);
3136 map_free(&installedmap);
3141 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
3143 pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
3147 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
3150 pool_create_state_maps(solv->pool, &solv->decisionq, &installedmap, 0);
3151 pool_trivial_installable_noobsoletesmap(solv->pool, &installedmap, pkgs, res, solv->noobsoletes.size ? &solv->noobsoletes : 0);
3152 map_free(&installedmap);
3155 /*-------------------------------------------------------------------
3157 * decision introspection
3161 solver_get_decisionlevel(Solver *solv, Id p)
3163 return solv->decisionmap[p];
3167 solver_get_decisionqueue(Solver *solv, Queue *decisionq)
3169 queue_free(decisionq);
3170 queue_init_clone(decisionq, &solv->decisionq);
3174 solver_get_lastdecisionblocklevel(Solver *solv)
3177 if (solv->decisionq.count == 0)
3179 p = solv->decisionq.elements[solv->decisionq.count - 1];
3182 return solv->decisionmap[p] < 0 ? -solv->decisionmap[p] : solv->decisionmap[p];
3186 solver_get_decisionblock(Solver *solv, int level, Queue *decisionq)
3191 queue_empty(decisionq);
3192 for (i = 0; i < solv->decisionq.count; i++)
3194 p = solv->decisionq.elements[i];
3197 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3200 if (i == solv->decisionq.count)
3202 for (i = 0; i < solv->decisionq.count; i++)
3204 p = solv->decisionq.elements[i];
3207 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3208 queue_push(decisionq, p);
3215 solver_describe_decision(Solver *solv, Id p, Id *infop)
3222 if (!solv->decisionmap[p])
3223 return SOLVER_REASON_UNRELATED;
3224 pp = solv->decisionmap[p] < 0 ? -p : p;
3225 for (i = 0; i < solv->decisionq.count; i++)
3226 if (solv->decisionq.elements[i] == pp)
3228 if (i == solv->decisionq.count) /* just in case... */
3229 return SOLVER_REASON_UNRELATED;
3230 why = solv->decisionq_why.elements[i];
3235 return SOLVER_REASON_UNIT_RULE;
3238 if (i < solv->decisioncnt_update)
3243 *infop = SYSTEMSOLVABLE;
3244 return SOLVER_REASON_KEEP_INSTALLED;
3248 return SOLVER_REASON_RESOLVE_JOB;
3250 if (i < solv->decisioncnt_keep)
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_UPDATE_INSTALLED;
3263 if (i < solv->decisioncnt_resolve)
3265 if (why == 0 && pp < 0)
3266 return SOLVER_REASON_CLEANDEPS_ERASE;
3269 if (why >= solv->updaterules && why < solv->updaterules_end)
3270 *infop = why - solv->updaterules;
3271 else if (why >= solv->featurerules && why < solv->featurerules_end)
3272 *infop = why - solv->featurerules;
3274 return SOLVER_REASON_KEEP_INSTALLED;
3276 if (i < solv->decisioncnt_weak)
3280 return SOLVER_REASON_RESOLVE;
3282 if (solv->decisionq.count < solv->decisioncnt_orphan)
3283 return SOLVER_REASON_WEAKDEP;
3284 return SOLVER_REASON_RESOLVE_ORPHAN;
3289 solver_describe_weakdep_decision(Solver *solv, Id p, Queue *whyq)
3291 Pool *pool = solv->pool;
3293 int level = solv->decisionmap[p];
3300 for (decisionno = 0; decisionno < solv->decisionq.count; decisionno++)
3301 if (solv->decisionq.elements[decisionno] == p)
3303 if (decisionno == solv->decisionq.count)
3305 if (decisionno < solv->decisioncnt_weak || decisionno >= solv->decisioncnt_orphan)
3308 /* 1) list all packages that recommend us */
3309 for (i = 1; i < pool->nsolvables; i++)
3311 Id *recp, rec, pp2, p2;
3312 if (solv->decisionmap[i] < 0 || solv->decisionmap[i] >= level)
3314 s = pool->solvables + i;
3317 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
3319 recp = s->repo->idarraydata + s->recommends;
3320 while ((rec = *recp++) != 0)
3323 FOR_PROVIDES(p2, pp2, rec)
3329 /* if p2 is already installed, this recommends is ignored */
3330 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3336 queue_push(whyq, SOLVER_REASON_RECOMMENDED);
3337 queue_push2(whyq, p2, rec);
3341 /* 2) list all supplements */
3342 s = pool->solvables + p;
3343 if (s->supplements && level > 0)
3345 Id *supp, sup, pp2, p2;
3346 /* this is a hack. to use solver_dep_fulfilled we temporarily clear
3347 * everything above our level in the decisionmap */
3348 for (i = decisionno; i < solv->decisionq.count; i++ )
3350 p2 = solv->decisionq.elements[i];
3352 solv->decisionmap[p2] = -solv->decisionmap[p2];
3354 supp = s->repo->idarraydata + s->supplements;
3355 while ((sup = *supp++) != 0)
3356 if (solver_dep_fulfilled(solv, sup))
3359 /* let's see if this is an easy supp */
3360 FOR_PROVIDES(p2, pp2, sup)
3362 if (solv->ignorealreadyrecommended && solv->installed)
3364 if (pool->solvables[p2].repo == solv->installed)
3367 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3369 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3370 queue_push2(whyq, p2, sup);
3376 /* hard case, just note with no package */
3377 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3378 queue_push2(whyq, 0, sup);
3381 for (i = decisionno; i < solv->decisionq.count; i++)
3383 p2 = solv->decisionq.elements[i];
3385 solv->decisionmap[p2] = -solv->decisionmap[p2];