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"
27 #include "solverdebug.h"
29 #define RULES_BLOCK 63
31 /********************************************************************
33 * dependency check helpers
37 /*-------------------------------------------------------------------
38 * handle split provides
40 * a splitprovides dep looks like
41 * namespace:splitprovides(pkg REL_WITH path)
42 * and is only true if pkg is installed and contains the specified path.
43 * we also make sure that pkg is selected for an update, otherwise the
44 * update would always be forced onto the user.
47 solver_splitprovides(Solver *solv, Id dep)
49 Pool *pool = solv->pool;
54 if (!solv->dosplitprovides || !solv->installed || (!solv->updatemap_all && !solv->updatemap.size))
58 rd = GETRELDEP(pool, dep);
59 if (rd->flags != REL_WITH)
61 FOR_PROVIDES(p, pp, dep)
63 /* here we have packages that provide the correct name and contain the path,
64 * now do extra filtering */
65 s = pool->solvables + p;
66 if (s->repo == solv->installed && s->name == rd->name &&
67 (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, p - solv->installed->start))))
74 /*-------------------------------------------------------------------
75 * solver_dep_installed
79 solver_dep_installed(Solver *solv, Id dep)
82 Pool *pool = solv->pool;
87 Reldep *rd = GETRELDEP(pool, dep);
88 if (rd->flags == REL_AND)
90 if (!solver_dep_installed(solv, rd->name))
92 return solver_dep_installed(solv, rd->evr);
94 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
95 return solver_dep_installed(solv, rd->evr);
97 FOR_PROVIDES(p, pp, dep)
99 if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
108 autouninstall(Solver *solv, Id *problem)
110 Pool *pool = solv->pool;
112 int lastfeature = 0, lastupdate = 0;
116 for (i = 0; (v = problem[i]) != 0; i++)
119 extraflags &= solv->job.elements[-v - 1];
120 if (v >= solv->featurerules && v < solv->featurerules_end)
123 if (v >= solv->updaterules && v < solv->updaterules_end)
125 /* check if identical to feature rule */
126 Id p = solv->rules[v].p;
129 Rule *r = solv->rules + solv->featurerules + (p - solv->installed->start);
132 /* update rule == feature rule */
141 if (!lastupdate && !lastfeature)
143 v = lastupdate ? lastupdate : lastfeature;
144 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "allowuninstall disabling ");
145 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + v);
146 solver_disableproblem(solv, v);
147 if (extraflags != -1 && (extraflags & SOLVER_CLEANDEPS) != 0 && solv->cleandepsmap.size)
149 /* add the package to the updatepkgs list, this will automatically turn
150 * on cleandeps mode */
151 Id p = solv->rules[v].p;
152 if (!solv->cleandeps_updatepkgs)
154 solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
155 queue_init(solv->cleandeps_updatepkgs);
159 int oldupdatepkgscnt = solv->cleandeps_updatepkgs->count;
160 queue_pushunique(solv->cleandeps_updatepkgs, p);
161 if (solv->cleandeps_updatepkgs->count != oldupdatepkgscnt)
162 solver_disablepolicyrules(solv);
168 /************************************************************************/
171 * enable/disable learnt rules
173 * we have enabled or disabled some of our rules. We now reenable all
174 * of our learnt rules except the ones that were learnt from rules that
178 enabledisablelearntrules(Solver *solv)
180 Pool *pool = solv->pool;
185 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
186 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
188 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
189 while ((why = *whyp++) != 0)
191 assert(why > 0 && why < i);
192 if (solv->rules[why].d < 0)
195 /* why != 0: we found a disabled rule, disable the learnt rule */
196 if (why && r->d >= 0)
198 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
200 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "disabling ");
201 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
203 solver_disablerule(solv, r);
205 else if (!why && r->d < 0)
207 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
209 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "re-enabling ");
210 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
212 solver_enablerule(solv, r);
219 * make assertion rules into decisions
221 * Go through rules and add direct assertions to the decisionqueue.
222 * If we find a conflict, disable rules and add them to problem queue.
226 makeruledecisions(Solver *solv)
228 Pool *pool = solv->pool;
233 int record_proof = 1;
235 int havedisabled = 0;
237 /* The system solvable is always installed first */
238 assert(solv->decisionq.count == 0);
239 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
240 queue_push(&solv->decisionq_why, 0);
241 solv->decisionmap[SYSTEMSOLVABLE] = 1; /* installed at level '1' */
243 decisionstart = solv->decisionq.count;
246 /* if we needed to re-run, back up decisions to decisionstart */
247 while (solv->decisionq.count > decisionstart)
249 v = solv->decisionq.elements[--solv->decisionq.count];
250 --solv->decisionq_why.count;
252 solv->decisionmap[vv] = 0;
255 /* note that the ruleassertions queue is ordered */
256 for (ii = 0; ii < solv->ruleassertions.count; ii++)
258 ri = solv->ruleassertions.elements[ii];
259 r = solv->rules + ri;
261 if (havedisabled && ri >= solv->learntrules)
263 /* just started with learnt rule assertions. If we have disabled
264 * some rules, adapt the learnt rule status */
265 enabledisablelearntrules(solv);
269 if (r->d < 0 || !r->p || r->w2) /* disabled, dummy or no assertion */
272 /* do weak rules in phase 2 */
273 if (ri < solv->learntrules && MAPTST(&solv->weakrulemap, ri))
279 if (!solv->decisionmap[vv]) /* if not yet decided */
281 queue_push(&solv->decisionq, v);
282 queue_push(&solv->decisionq_why, r - solv->rules);
283 solv->decisionmap[vv] = v > 0 ? 1 : -1;
284 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
286 Solvable *s = solv->pool->solvables + vv;
288 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", pool_solvable2str(solv->pool, s));
290 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing %s (assertion)\n", pool_solvable2str(solv->pool, s));
295 /* check against previous decision: is there a conflict? */
296 if (v > 0 && solv->decisionmap[vv] > 0) /* ok to install */
298 if (v < 0 && solv->decisionmap[vv] < 0) /* ok to remove */
304 * The rule (r) we're currently processing says something
305 * different (v = r->p) than a previous decision (decisionmap[abs(v)])
309 if (ri >= solv->learntrules)
311 /* conflict with a learnt rule */
312 /* can happen when packages cannot be installed for multiple reasons. */
313 /* we disable the learnt rule in this case */
314 /* (XXX: we should really call analyze_unsolvable_rule here!) */
315 solver_disablerule(solv, r);
320 * find the decision which is the "opposite" of the rule
322 for (i = 0; i < solv->decisionq.count; i++)
323 if (solv->decisionq.elements[i] == -v)
325 assert(i < solv->decisionq.count); /* assert that we found it */
326 oldproblemcount = solv->problems.count;
329 * conflict with system solvable ?
331 if (v == -SYSTEMSOLVABLE)
335 queue_push(&solv->problems, solv->learnt_pool.count);
336 queue_push(&solv->learnt_pool, ri);
337 queue_push(&solv->learnt_pool, 0);
340 queue_push(&solv->problems, 0);
341 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
342 if (ri >= solv->jobrules && ri < solv->jobrules_end)
343 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
346 queue_push(&solv->problems, v);
347 queue_push(&solv->problems, 0);
348 if (solv->allowuninstall && v >= solv->featurerules && v < solv->updaterules_end)
349 solv->problems.count = oldproblemcount;
350 solver_disableproblem(solv, v);
352 break; /* start over */
355 assert(solv->decisionq_why.elements[i] > 0);
358 * conflict with an rpm rule ?
360 if (solv->decisionq_why.elements[i] < solv->rpmrules_end)
364 queue_push(&solv->problems, solv->learnt_pool.count);
365 queue_push(&solv->learnt_pool, ri);
366 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
367 queue_push(&solv->learnt_pool, 0);
370 queue_push(&solv->problems, 0);
371 assert(v > 0 || v == -SYSTEMSOLVABLE);
372 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
373 if (ri >= solv->jobrules && ri < solv->jobrules_end)
374 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
377 queue_push(&solv->problems, v);
378 queue_push(&solv->problems, 0);
379 if (solv->allowuninstall && v >= solv->featurerules && v < solv->updaterules_end)
380 solv->problems.count = oldproblemcount;
381 solver_disableproblem(solv, v);
383 break; /* start over */
387 * conflict with another job or update/feature rule
393 queue_push(&solv->problems, solv->learnt_pool.count);
394 queue_push(&solv->learnt_pool, ri);
395 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
396 queue_push(&solv->learnt_pool, 0);
399 queue_push(&solv->problems, 0);
401 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflicting update/job assertions over literal %d\n", vv);
404 * push all of our rules (can only be feature or job rules)
405 * asserting this literal on the problem stack
407 for (i = solv->featurerules, rr = solv->rules + i; i < solv->learntrules; i++, rr++)
409 if (rr->d < 0 /* disabled */
410 || rr->w2) /* or no assertion */
412 if (rr->p != vv /* not affecting the literal */
415 if (MAPTST(&solv->weakrulemap, i)) /* weak: silently ignore */
418 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
419 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + i);
422 if (i >= solv->jobrules && i < solv->jobrules_end)
423 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
424 queue_push(&solv->problems, v);
426 queue_push(&solv->problems, 0);
428 if (solv->allowuninstall && (v = autouninstall(solv, solv->problems.elements + oldproblemcount + 1)) != 0)
429 solv->problems.count = oldproblemcount;
431 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
432 solver_disableproblem(solv, solv->problems.elements[i]);
434 break; /* start over */
436 if (ii < solv->ruleassertions.count)
440 * phase 2: now do the weak assertions
442 for (ii = 0; ii < solv->ruleassertions.count; ii++)
444 ri = solv->ruleassertions.elements[ii];
445 r = solv->rules + ri;
446 if (r->d < 0 || r->w2) /* disabled or no assertion */
448 if (ri >= solv->learntrules || !MAPTST(&solv->weakrulemap, ri)) /* skip non-weak */
453 if (!solv->decisionmap[vv]) /* if not yet decided */
455 queue_push(&solv->decisionq, v);
456 queue_push(&solv->decisionq_why, r - solv->rules);
457 solv->decisionmap[vv] = v > 0 ? 1 : -1;
458 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
460 Solvable *s = solv->pool->solvables + vv;
462 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
464 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
468 /* check against previous decision: is there a conflict? */
469 if (v > 0 && solv->decisionmap[vv] > 0)
471 if (v < 0 && solv->decisionmap[vv] < 0)
474 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
475 solver_printrule(solv, SOLV_DEBUG_UNSOLVABLE, r);
477 if (ri >= solv->jobrules && ri < solv->jobrules_end)
478 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
481 solver_disableproblem(solv, v);
483 solver_reenablepolicyrules(solv, -v);
485 break; /* start over */
487 if (ii == solv->ruleassertions.count)
488 break; /* finished! */
493 /********************************************************************/
497 /*-------------------------------------------------------------------
500 * initial setup for all watches
504 makewatches(Solver *solv)
508 int nsolvables = solv->pool->nsolvables;
510 solv_free(solv->watches);
511 /* lower half for removals, upper half for installs */
512 solv->watches = solv_calloc(2 * nsolvables, sizeof(Id));
514 /* do it reverse so rpm rules get triggered first (XXX: obsolete?) */
515 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
517 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
520 if (!r->w2) /* assertions do not need watches */
523 /* see addwatches_rule(solv, r) */
524 r->n1 = solv->watches[nsolvables + r->w1];
525 solv->watches[nsolvables + r->w1] = r - solv->rules;
527 r->n2 = solv->watches[nsolvables + r->w2];
528 solv->watches[nsolvables + r->w2] = r - solv->rules;
533 /*-------------------------------------------------------------------
535 * add watches (for a new learned rule)
536 * sets up watches for a single rule
538 * see also makewatches() above.
542 addwatches_rule(Solver *solv, Rule *r)
544 int nsolvables = solv->pool->nsolvables;
546 r->n1 = solv->watches[nsolvables + r->w1];
547 solv->watches[nsolvables + r->w1] = r - solv->rules;
549 r->n2 = solv->watches[nsolvables + r->w2];
550 solv->watches[nsolvables + r->w2] = r - solv->rules;
554 /********************************************************************/
560 /* shortcuts to check if a literal (positive or negative) assignment
561 * evaluates to 'true' or 'false'
563 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
564 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
565 #define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
567 /*-------------------------------------------------------------------
571 * make decision and propagate to all rules
573 * Evaluate each term affected by the decision (linked through watches).
574 * If we find unit rules we make new decisions based on them.
576 * return : 0 = everything is OK
577 * rule = conflict found in this rule
581 propagate(Solver *solv, int level)
583 Pool *pool = solv->pool;
584 Id *rp, *next_rp; /* rule pointer, next rule pointer in linked list */
586 Id p, pkg, other_watch;
588 Id *decisionmap = solv->decisionmap;
590 Id *watches = solv->watches + pool->nsolvables; /* place ptr in middle */
592 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate -----\n");
594 /* foreach non-propagated decision */
595 while (solv->propagate_index < solv->decisionq.count)
598 * 'pkg' was just decided
599 * negate because our watches trigger if literal goes FALSE
601 pkg = -solv->decisionq.elements[solv->propagate_index++];
603 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
605 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
606 solver_printruleelement(solv, SOLV_DEBUG_PROPAGATE, 0, -pkg);
609 /* foreach rule where 'pkg' is now FALSE */
610 for (rp = watches + pkg; *rp; rp = next_rp)
612 r = solv->rules + *rp;
615 /* rule is disabled, goto next */
623 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
625 POOL_DEBUG(SOLV_DEBUG_PROPAGATE," watch triggered ");
626 solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
629 /* 'pkg' was just decided (was set to FALSE)
631 * now find other literal watch, check clause
632 * and advance on linked list
646 * This term is already true (through the other literal)
647 * so we have nothing to do
649 if (DECISIONMAP_TRUE(other_watch))
653 * The other literal is FALSE or UNDEF
659 /* Not a binary clause, try to move our watch.
661 * Go over all literals and find one that is
665 * (TRUE is also ok, in that case the rule is fulfilled)
667 if (r->p /* we have a 'p' */
668 && r->p != other_watch /* which is not watched */
669 && !DECISIONMAP_FALSE(r->p)) /* and not FALSE */
673 else /* go find a 'd' to make 'true' */
676 we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
678 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
680 if (p != other_watch /* which is not watched */
681 && !DECISIONMAP_FALSE(p)) /* and not FALSE */
689 * if we found some p that is UNDEF or TRUE, move
692 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
695 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, p));
697 POOL_DEBUG(SOLV_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, -p));
713 watches[p] = r - solv->rules;
716 /* search failed, thus all unwatched literals are FALSE */
721 * unit clause found, set literal other_watch to TRUE
724 if (DECISIONMAP_FALSE(other_watch)) /* check if literal is FALSE */
725 return r; /* eek, a conflict! */
727 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
729 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " unit ");
730 solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
734 decisionmap[other_watch] = level; /* install! */
736 decisionmap[-other_watch] = -level; /* remove! */
738 queue_push(&solv->decisionq, other_watch);
739 queue_push(&solv->decisionq_why, r - solv->rules);
741 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
744 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> decided to install %s\n", pool_solvid2str(pool, other_watch));
746 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> decided to conflict %s\n", pool_solvid2str(pool, -other_watch));
749 } /* foreach rule involving 'pkg' */
751 } /* while we have non-decided decisions */
753 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate end-----\n");
755 return 0; /* all is well */
759 /********************************************************************/
762 /*-------------------------------------------------------------------
769 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
771 Pool *pool = solv->pool;
775 Map seen; /* global? */
776 Id d, v, vv, *dp, why;
778 int num = 0, l1num = 0;
779 int learnt_why = solv->learnt_pool.count;
780 Id *decisionmap = solv->decisionmap;
782 queue_init_buffer(&r, r_buf, sizeof(r_buf)/sizeof(*r_buf));
784 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
785 map_init(&seen, pool->nsolvables);
786 idx = solv->decisionq.count;
789 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
790 solver_printruleclass(solv, SOLV_DEBUG_ANALYZE, c);
791 queue_push(&solv->learnt_pool, c - solv->rules);
792 d = c->d < 0 ? -c->d - 1 : c->d;
793 dp = d ? pool->whatprovidesdata + d : 0;
794 /* go through all literals of the rule */
806 if (DECISIONMAP_TRUE(v)) /* the one true literal */
809 if (MAPTST(&seen, vv))
811 l = solv->decisionmap[vv];
814 MAPSET(&seen, vv); /* mark that we also need to look at this literal */
816 l1num++; /* need to do this one in level1 pass */
818 num++; /* need to do this one as well */
821 queue_push(&r, v); /* not level1 or conflict level, add to new rule */
827 if (!num && !--l1num)
828 break; /* all level 1 literals done */
830 /* find the next literal to investigate */
831 /* (as num + l1num > 0, we know that we'll always find one) */
835 v = solv->decisionq.elements[--idx];
837 if (MAPTST(&seen, vv))
842 if (num && --num == 0)
844 *pr = -v; /* so that v doesn't get lost */
847 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
848 /* clear non-l1 bits from seen map */
849 for (i = 0; i < r.count; i++)
852 MAPCLR(&seen, v > 0 ? v : -v);
854 /* only level 1 marks left in seen map */
855 l1num++; /* as l1retry decrements it */
859 why = solv->decisionq_why.elements[idx];
860 if (why <= 0) /* just in case, maybe for SYSTEMSOLVABLE */
862 c = solv->rules + why;
868 else if (r.count == 1 && r.elements[0] < 0)
871 *dr = pool_queuetowhatprovides(pool, &r);
872 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
874 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
875 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, *pr);
876 for (i = 0; i < r.count; i++)
877 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, r.elements[i]);
879 /* push end marker on learnt reasons stack */
880 queue_push(&solv->learnt_pool, 0);
884 solv->stats_learned++;
889 /*-------------------------------------------------------------------
893 * reset all solver decisions
894 * called after rules have been enabled/disabled
898 solver_reset(Solver *solv)
900 Pool *pool = solv->pool;
904 /* rewind all decisions */
905 for (i = solv->decisionq.count - 1; i >= 0; i--)
907 v = solv->decisionq.elements[i];
908 solv->decisionmap[v > 0 ? v : -v] = 0;
910 solv->decisionq_why.count = 0;
911 solv->decisionq.count = 0;
912 solv->recommends_index = -1;
913 solv->propagate_index = 0;
914 solv->branches.count = 0;
916 /* adapt learnt rule status to new set of enabled/disabled rules */
917 enabledisablelearntrules(solv);
919 /* redo all assertion rule decisions */
920 makeruledecisions(solv);
921 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
925 /*-------------------------------------------------------------------
927 * analyze_unsolvable_rule
929 * recursion helper used by analyze_unsolvable
933 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp, Map *rseen)
935 Pool *pool = solv->pool;
937 Id why = r - solv->rules;
939 IF_POOLDEBUG (SOLV_DEBUG_UNSOLVABLE)
940 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, r);
941 if (solv->learntrules && why >= solv->learntrules)
943 if (MAPTST(rseen, why - solv->learntrules))
945 MAPSET(rseen, why - solv->learntrules);
946 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
947 if (solv->learnt_pool.elements[i] > 0)
948 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp, rseen);
951 if (MAPTST(&solv->weakrulemap, why))
952 if (!*lastweakp || why > *lastweakp)
954 /* do not add rpm rules to problem */
955 if (why < solv->rpmrules_end)
957 /* turn rule into problem */
958 if (why >= solv->jobrules && why < solv->jobrules_end)
959 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
960 /* normalize dup/infarch rules */
961 if (why > solv->infarchrules && why < solv->infarchrules_end)
963 Id name = pool->solvables[-solv->rules[why].p].name;
964 while (why > solv->infarchrules && pool->solvables[-solv->rules[why - 1].p].name == name)
967 if (why > solv->duprules && why < solv->duprules_end)
969 Id name = pool->solvables[-solv->rules[why].p].name;
970 while (why > solv->duprules && pool->solvables[-solv->rules[why - 1].p].name == name)
974 /* return if problem already countains our rule */
975 if (solv->problems.count)
977 for (i = solv->problems.count - 1; i >= 0; i--)
978 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
980 else if (solv->problems.elements[i] == why)
983 queue_push(&solv->problems, why);
987 /*-------------------------------------------------------------------
991 * We know that the problem is not solvable. Record all involved
992 * rules (i.e. the "proof") into solv->learnt_pool.
993 * Record the learnt pool index and all non-rpm rules into
994 * solv->problems. (Our solutions to fix the problems are to
995 * disable those rules.)
997 * If the proof contains at least one weak rule, we disable the
1000 * Otherwise we return 0 if disablerules is not set or disable
1001 * _all_ of the problem rules and return 1.
1003 * return: 1 - disabled some rules, try again
1008 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1010 Pool *pool = solv->pool;
1012 Map seen; /* global to speed things up? */
1014 Id d, v, vv, *dp, why;
1016 Id *decisionmap = solv->decisionmap;
1017 int oldproblemcount;
1018 int oldlearntpoolcount;
1020 int record_proof = 1;
1022 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
1023 solv->stats_unsolvable++;
1024 oldproblemcount = solv->problems.count;
1025 oldlearntpoolcount = solv->learnt_pool.count;
1027 /* make room for proof index */
1028 /* must update it later, as analyze_unsolvable_rule would confuse
1029 * it with a rule index if we put the real value in already */
1030 queue_push(&solv->problems, 0);
1033 map_init(&seen, pool->nsolvables);
1034 map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
1036 queue_push(&solv->learnt_pool, r - solv->rules);
1038 analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
1039 d = r->d < 0 ? -r->d - 1 : r->d;
1040 dp = d ? pool->whatprovidesdata + d : 0;
1051 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1053 vv = v > 0 ? v : -v;
1054 l = solv->decisionmap[vv];
1059 idx = solv->decisionq.count;
1062 v = solv->decisionq.elements[--idx];
1063 vv = v > 0 ? v : -v;
1064 if (!MAPTST(&seen, vv))
1066 why = solv->decisionq_why.elements[idx];
1069 queue_push(&solv->learnt_pool, why);
1070 r = solv->rules + why;
1071 analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
1072 d = r->d < 0 ? -r->d - 1 : r->d;
1073 dp = d ? pool->whatprovidesdata + d : 0;
1084 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1086 vv = v > 0 ? v : -v;
1087 l = solv->decisionmap[vv];
1095 queue_push(&solv->problems, 0); /* mark end of this problem */
1099 /* disable last weak rule */
1100 solv->problems.count = oldproblemcount;
1101 solv->learnt_pool.count = oldlearntpoolcount;
1102 if (lastweak >= solv->jobrules && lastweak < solv->jobrules_end)
1103 v = -(solv->ruletojob.elements[lastweak - solv->jobrules] + 1);
1106 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "disabling ");
1107 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + lastweak);
1108 if (lastweak >= solv->choicerules && lastweak < solv->choicerules_end)
1109 solver_disablechoicerules(solv, solv->rules + lastweak);
1110 solver_disableproblem(solv, v);
1112 solver_reenablepolicyrules(solv, -v);
1117 if (solv->allowuninstall && (v = autouninstall(solv, solv->problems.elements + oldproblemcount + 1)) != 0)
1119 solv->problems.count = oldproblemcount;
1120 solv->learnt_pool.count = oldlearntpoolcount;
1128 queue_push(&solv->learnt_pool, 0);
1129 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1132 /* + 2: index + trailing zero */
1133 if (disablerules && oldproblemcount + 2 < solv->problems.count)
1135 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1136 solver_disableproblem(solv, solv->problems.elements[i]);
1137 /* XXX: might want to enable all weak rules again */
1141 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1146 /********************************************************************/
1147 /* Decision revert */
1149 /*-------------------------------------------------------------------
1152 * revert decisionq to a level
1156 revert(Solver *solv, int level)
1158 Pool *pool = solv->pool;
1160 while (solv->decisionq.count)
1162 v = solv->decisionq.elements[solv->decisionq.count - 1];
1163 vv = v > 0 ? v : -v;
1164 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1166 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1167 solv->decisionmap[vv] = 0;
1168 solv->decisionq.count--;
1169 solv->decisionq_why.count--;
1170 solv->propagate_index = solv->decisionq.count;
1172 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1174 solv->branches.count--;
1175 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1176 solv->branches.count--;
1178 solv->recommends_index = -1;
1182 /*-------------------------------------------------------------------
1184 * watch2onhighest - put watch2 on literal with highest level
1188 watch2onhighest(Solver *solv, Rule *r)
1193 d = r->d < 0 ? -r->d - 1 : r->d;
1195 return; /* binary rule, both watches are set */
1196 dp = solv->pool->whatprovidesdata + d;
1197 while ((v = *dp++) != 0)
1199 l = solv->decisionmap[v < 0 ? -v : v];
1211 /*-------------------------------------------------------------------
1215 * add free decision (solvable to install) to decisionq
1216 * increase level and propagate decision
1217 * return if no conflict.
1219 * in conflict case, analyze conflict rule, add resulting
1220 * rule to learnt rule set, make decision from learnt
1221 * rule (always unit) and re-propagate.
1223 * returns the new solver level or 0 if unsolvable
1228 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules, Id ruleid)
1230 Pool *pool = solv->pool;
1235 assert(ruleid >= 0);
1240 solv->decisionmap[decision] = level;
1242 solv->decisionmap[-decision] = -level;
1243 queue_push(&solv->decisionq, decision);
1244 queue_push(&solv->decisionq_why, -ruleid); /* <= 0 -> free decision */
1248 r = propagate(solv, level);
1252 return analyze_unsolvable(solv, r, disablerules);
1253 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1254 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
1255 assert(l > 0 && l < level);
1256 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1258 revert(solv, level);
1259 r = solver_addrule(solv, p, d);
1261 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
1262 queue_push(&solv->learnt_why, why);
1265 /* at least 2 literals, needs watches */
1266 watch2onhighest(solv, r);
1267 addwatches_rule(solv, r);
1271 /* learnt rule is an assertion */
1272 queue_push(&solv->ruleassertions, r - solv->rules);
1274 /* the new rule is unit by design */
1275 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1276 queue_push(&solv->decisionq, p);
1277 queue_push(&solv->decisionq_why, r - solv->rules);
1278 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
1280 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "decision: ");
1281 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, p);
1282 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "new rule: ");
1283 solver_printrule(solv, SOLV_DEBUG_ANALYZE, r);
1290 /*-------------------------------------------------------------------
1292 * select and install
1294 * install best package from the queue. We add an extra package, inst, if
1295 * provided. See comment in weak install section.
1297 * returns the new solver level or 0 if unsolvable
1302 selectandinstall(Solver *solv, int level, Queue *dq, int disablerules, Id ruleid)
1304 Pool *pool = solv->pool;
1309 policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1312 /* XXX: didn't we already do that? */
1313 /* XXX: shouldn't we prefer installed packages? */
1314 /* XXX: move to policy.c? */
1315 /* choose the supplemented one */
1316 for (i = 0; i < dq->count; i++)
1317 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1319 dq->elements[0] = dq->elements[i];
1326 /* multiple candidates, open a branch */
1327 for (i = 1; i < dq->count; i++)
1328 queue_push(&solv->branches, dq->elements[i]);
1329 queue_push(&solv->branches, -level);
1331 p = dq->elements[0];
1333 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing %s\n", pool_solvid2str(pool, p));
1335 return setpropagatelearn(solv, level, p, disablerules, ruleid);
1339 /********************************************************************/
1340 /* Main solver interface */
1343 /*-------------------------------------------------------------------
1346 * create solver structure
1348 * pool: all available solvables
1349 * installed: installed Solvables
1352 * Upon solving, rules are created to flag the Solvables
1353 * of the 'installed' Repo as installed.
1357 solver_create(Pool *pool)
1360 solv = (Solver *)solv_calloc(1, sizeof(Solver));
1362 solv->installed = pool->installed;
1364 solv->allownamechange = 1;
1366 solv->dup_allowdowngrade = 1;
1367 solv->dup_allownamechange = 1;
1368 solv->dup_allowarchchange = 1;
1369 solv->dup_allowvendorchange = 1;
1371 queue_init(&solv->ruletojob);
1372 queue_init(&solv->decisionq);
1373 queue_init(&solv->decisionq_why);
1374 queue_init(&solv->problems);
1375 queue_init(&solv->orphaned);
1376 queue_init(&solv->learnt_why);
1377 queue_init(&solv->learnt_pool);
1378 queue_init(&solv->branches);
1379 queue_init(&solv->weakruleq);
1380 queue_init(&solv->ruleassertions);
1382 queue_push(&solv->learnt_pool, 0); /* so that 0 does not describe a proof */
1384 map_init(&solv->recommendsmap, pool->nsolvables);
1385 map_init(&solv->suggestsmap, pool->nsolvables);
1386 map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1387 solv->recommends_index = 0;
1389 solv->decisionmap = (Id *)solv_calloc(pool->nsolvables, sizeof(Id));
1391 solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1392 memset(solv->rules, 0, sizeof(Rule));
1398 /*-------------------------------------------------------------------
1404 solver_free(Solver *solv)
1406 queue_free(&solv->job);
1407 queue_free(&solv->ruletojob);
1408 queue_free(&solv->decisionq);
1409 queue_free(&solv->decisionq_why);
1410 queue_free(&solv->learnt_why);
1411 queue_free(&solv->learnt_pool);
1412 queue_free(&solv->problems);
1413 queue_free(&solv->solutions);
1414 queue_free(&solv->orphaned);
1415 queue_free(&solv->branches);
1416 queue_free(&solv->weakruleq);
1417 queue_free(&solv->ruleassertions);
1418 if (solv->cleandeps_updatepkgs)
1420 queue_free(solv->cleandeps_updatepkgs);
1421 solv->cleandeps_updatepkgs = solv_free(solv->cleandeps_updatepkgs);
1423 if (solv->cleandeps_mistakes)
1425 queue_free(solv->cleandeps_mistakes);
1426 solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
1428 if (solv->update_targets)
1430 queue_free(solv->update_targets);
1431 solv->update_targets = solv_free(solv->update_targets);
1434 map_free(&solv->recommendsmap);
1435 map_free(&solv->suggestsmap);
1436 map_free(&solv->noupdate);
1437 map_free(&solv->weakrulemap);
1438 map_free(&solv->noobsoletes);
1440 map_free(&solv->updatemap);
1441 map_free(&solv->bestupdatemap);
1442 map_free(&solv->fixmap);
1443 map_free(&solv->dupmap);
1444 map_free(&solv->dupinvolvedmap);
1445 map_free(&solv->droporphanedmap);
1446 map_free(&solv->cleandepsmap);
1448 solv_free(solv->decisionmap);
1449 solv_free(solv->rules);
1450 solv_free(solv->watches);
1451 solv_free(solv->obsoletes);
1452 solv_free(solv->obsoletes_data);
1453 solv_free(solv->multiversionupdaters);
1454 solv_free(solv->choicerules_ref);
1455 solv_free(solv->bestrules_pkg);
1460 solver_get_flag(Solver *solv, int flag)
1464 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1465 return solv->allowdowngrade;
1466 case SOLVER_FLAG_ALLOW_NAMECHANGE:
1467 return solv->allownamechange;
1468 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1469 return solv->allowarchchange;
1470 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1471 return solv->allowvendorchange;
1472 case SOLVER_FLAG_ALLOW_UNINSTALL:
1473 return solv->allowuninstall;
1474 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1475 return solv->noupdateprovide;
1476 case SOLVER_FLAG_SPLITPROVIDES:
1477 return solv->dosplitprovides;
1478 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1479 return solv->dontinstallrecommended;
1480 case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1481 return solv->addalreadyrecommended;
1482 case SOLVER_FLAG_NO_INFARCHCHECK:
1483 return solv->noinfarchcheck;
1484 case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1485 return solv->keepexplicitobsoletes;
1486 case SOLVER_FLAG_BEST_OBEY_POLICY:
1487 return solv->bestobeypolicy;
1495 solver_set_flag(Solver *solv, int flag, int value)
1497 int old = solver_get_flag(solv, flag);
1500 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1501 solv->allowdowngrade = value;
1503 case SOLVER_FLAG_ALLOW_NAMECHANGE:
1504 solv->allownamechange = value;
1506 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1507 solv->allowarchchange = value;
1509 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1510 solv->allowvendorchange = value;
1512 case SOLVER_FLAG_ALLOW_UNINSTALL:
1513 solv->allowuninstall = value;
1515 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1516 solv->noupdateprovide = value;
1518 case SOLVER_FLAG_SPLITPROVIDES:
1519 solv->dosplitprovides = value;
1521 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1522 solv->dontinstallrecommended = value;
1524 case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1525 solv->addalreadyrecommended = value;
1527 case SOLVER_FLAG_NO_INFARCHCHECK:
1528 solv->noinfarchcheck = value;
1530 case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1531 solv->keepexplicitobsoletes = value;
1533 case SOLVER_FLAG_BEST_OBEY_POLICY:
1534 solv->bestobeypolicy = value;
1543 cleandeps_check_mistakes(Solver *solv, int level)
1545 Pool *pool = solv->pool;
1549 int mademistake = 0;
1551 if (!solv->cleandepsmap.size)
1553 /* check for mistakes */
1554 for (i = solv->installed->start; i < solv->installed->end; i++)
1556 if (!MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1558 r = solv->rules + solv->featurerules + (i - solv->installed->start);
1559 /* a mistake is when the featurerule is true but the updaterule is false */
1562 FOR_RULELITERALS(p, dp, r)
1563 if (p > 0 && solv->decisionmap[p] > 0)
1567 r = solv->rules + solv->updaterules + (i - solv->installed->start);
1570 FOR_RULELITERALS(p, dp, r)
1571 if (p > 0 && solv->decisionmap[p] > 0)
1575 POOL_DEBUG(SOLV_DEBUG_SOLVER, "cleandeps mistake: ");
1576 solver_printruleclass(solv, SOLV_DEBUG_SOLVER, r);
1577 POOL_DEBUG(SOLV_DEBUG_SOLVER, "feature rule: ");
1578 solver_printruleclass(solv, SOLV_DEBUG_SOLVER, solv->rules + solv->featurerules + (i - solv->installed->start));
1579 if (!solv->cleandeps_mistakes)
1581 solv->cleandeps_mistakes = solv_calloc(1, sizeof(Queue));
1582 queue_init(solv->cleandeps_mistakes);
1584 queue_push(solv->cleandeps_mistakes, i);
1585 MAPCLR(&solv->cleandepsmap, i - solv->installed->start);
1586 solver_reenablepolicyrules_cleandeps(solv, i);
1598 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
1602 for (i = j = 0; i < q->count; i++)
1605 for (cp2 = cp; *cp2; cp2++)
1608 q->elements[j++] = p;
1612 queue_truncate(q, j);
1615 /*-------------------------------------------------------------------
1619 * all rules have been set up, now actually run the solver
1624 solver_run_sat(Solver *solv, int disablerules, int doweak)
1626 Queue dq; /* local decisionqueue */
1627 Queue dqs; /* local decisionqueue for supplements */
1633 Pool *pool = solv->pool;
1635 int minimizationsteps;
1636 int installedpos = solv->installed ? solv->installed->start : 0;
1638 IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
1640 POOL_DEBUG (SOLV_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1641 for (i = 1; i < solv->nrules; i++)
1642 solver_printruleclass(solv, SOLV_DEBUG_RULE_CREATION, solv->rules + i);
1645 POOL_DEBUG(SOLV_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1647 /* start SAT algorithm */
1649 systemlevel = level + 1;
1650 POOL_DEBUG(SOLV_DEBUG_SOLVER, "solving...\n");
1656 * here's the main loop:
1657 * 1) propagate new decisions (only needed once)
1659 * 3) try to keep installed packages
1660 * 4) fulfill all unresolved rules
1661 * 5) install recommended packages
1662 * 6) minimalize solution if we had choices
1663 * if we encounter a problem, we rewind to a safe level and restart
1667 minimizationsteps = 0;
1671 * initial propagation of the assertions
1675 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1676 if ((r = propagate(solv, level)) != 0)
1678 if (analyze_unsolvable(solv, r, disablerules))
1681 break; /* unsolvable */
1686 * resolve jobs first
1688 if (level < systemlevel)
1690 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving job rules\n");
1691 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1694 if (r->d < 0) /* ignore disabled rules */
1697 FOR_RULELITERALS(l, dp, r)
1701 if (solv->decisionmap[-l] <= 0)
1706 if (solv->decisionmap[l] > 0)
1708 if (solv->decisionmap[l] == 0)
1714 /* prune to installed if not updating */
1715 if (dq.count > 1 && solv->installed && !solv->updatemap_all &&
1716 !(solv->job.elements[solv->ruletojob.elements[i - solv->jobrules]] & SOLVER_ORUPDATE))
1719 for (j = k = 0; j < dq.count; j++)
1721 Solvable *s = pool->solvables + dq.elements[j];
1722 if (s->repo == solv->installed)
1724 dq.elements[k++] = dq.elements[j];
1725 if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
1727 k = 0; /* package wants to be updated, do not prune */
1736 level = selectandinstall(solv, level, &dq, disablerules, i);
1739 if (level <= olevel)
1743 break; /* unsolvable */
1744 systemlevel = level + 1;
1745 if (i < solv->jobrules_end)
1747 solv->decisioncnt_update = solv->decisionq.count;
1748 solv->decisioncnt_keep = solv->decisionq.count;
1752 * installed packages
1754 if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1756 Repo *installed = solv->installed;
1759 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving installed packages\n");
1760 /* we use two passes if we need to update packages
1761 * to create a better user experience */
1762 for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1764 int passlevel = level;
1766 solv->decisioncnt_keep = solv->decisionq.count;
1767 /* start with installedpos, the position that gave us problems last time */
1768 for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1773 if (i == installed->end)
1774 i = installed->start;
1775 s = pool->solvables + i;
1776 if (s->repo != installed)
1779 if (solv->decisionmap[i] > 0)
1781 if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1782 continue; /* updates first */
1783 r = solv->rules + solv->updaterules + (i - installed->start);
1785 if (!rr->p || rr->d < 0) /* disabled -> look at feature rule */
1786 rr -= solv->installed->end - solv->installed->start;
1787 if (!rr->p) /* identical to update rule? */
1790 continue; /* orpaned package */
1792 /* XXX: noupdate check is probably no longer needed, as all jobs should
1793 * already be satisfied */
1794 /* Actually we currently still need it because of erase jobs */
1795 /* if noupdate is set we do not look at update candidates */
1797 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))
1799 if (solv->noobsoletes.size && solv->multiversionupdaters
1800 && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1802 /* special multiversion handling, make sure best version is chosen */
1804 while ((p = pool->whatprovidesdata[d++]) != 0)
1805 if (solv->decisionmap[p] >= 0)
1807 if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1808 prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
1811 policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1813 if (p != i && solv->decisionmap[p] == 0)
1815 rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1816 if (!rr->p) /* update rule == feature rule? */
1817 rr = rr - solv->featurerules + solv->updaterules;
1826 /* update to best package */
1827 FOR_RULELITERALS(p, dp, rr)
1829 if (solv->decisionmap[p] > 0)
1831 dq.count = 0; /* already fulfilled */
1834 if (!solv->decisionmap[p])
1839 if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1840 prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
1841 /* install best version */
1845 level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1852 if (level <= olevel)
1854 if (level == 1 || level < passlevel)
1855 break; /* trouble */
1857 n = installed->start; /* redo all */
1863 /* if still undecided keep package */
1864 if (solv->decisionmap[i] == 0)
1867 if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
1869 POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
1870 level = setpropagatelearn(solv, level, -i, disablerules, 0);
1874 POOL_DEBUG(SOLV_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
1875 level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1879 if (level <= olevel)
1881 if (level == 1 || level < passlevel)
1882 break; /* trouble */
1884 n = installed->start; /* redo all */
1887 continue; /* retry with learnt rule */
1891 if (n < installed->end)
1893 installedpos = i; /* retry problem solvable next time */
1894 break; /* ran into trouble */
1896 installedpos = installed->start; /* reset installedpos */
1899 break; /* unsolvable */
1900 systemlevel = level + 1;
1902 continue; /* had trouble, retry */
1905 if (level < systemlevel)
1906 systemlevel = level;
1911 solv->decisioncnt_resolve = solv->decisionq.count;
1912 POOL_DEBUG(SOLV_DEBUG_POLICY, "deciding unresolved rules\n");
1913 for (i = 1, n = 1; n < solv->nrules; i++, n++)
1915 if (i == solv->nrules)
1917 r = solv->rules + i;
1918 if (r->d < 0) /* ignore disabled rules */
1923 /* binary or unary rule */
1924 /* need two positive undecided literals */
1925 if (r->p < 0 || r->w2 <= 0)
1927 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1929 queue_push(&dq, r->p);
1930 queue_push(&dq, r->w2);
1935 * all negative literals are installed
1936 * no positive literal is installed
1937 * i.e. the rule is not fulfilled and we
1938 * just need to decide on the positive literals
1942 if (solv->decisionmap[-r->p] <= 0)
1947 if (solv->decisionmap[r->p] > 0)
1949 if (solv->decisionmap[r->p] == 0)
1950 queue_push(&dq, r->p);
1952 dp = pool->whatprovidesdata + r->d;
1953 while ((p = *dp++) != 0)
1957 if (solv->decisionmap[-p] <= 0)
1962 if (solv->decisionmap[p] > 0)
1964 if (solv->decisionmap[p] == 0)
1971 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
1973 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "unfulfilled ");
1974 solver_printruleclass(solv, SOLV_DEBUG_PROPAGATE, r);
1976 /* dq.count < 2 cannot happen as this means that
1977 * the rule is unit */
1978 assert(dq.count > 1);
1981 level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
1983 break; /* unsolvable */
1984 if (level < systemlevel || level == 1)
1985 break; /* trouble */
1986 /* something changed, so look at all rules again */
1990 if (n != solv->nrules) /* ran into trouble? */
1993 break; /* unsolvable */
1994 continue; /* start over */
1997 /* at this point we have a consistent system. now do the extras... */
1999 solv->decisioncnt_weak = solv->decisionq.count;
2004 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended packages\n");
2005 queue_empty(&dq); /* recommended packages */
2006 queue_empty(&dqs); /* supplemented packages */
2007 for (i = 1; i < pool->nsolvables; i++)
2009 if (solv->decisionmap[i] < 0)
2011 if (solv->decisionmap[i] > 0)
2013 /* installed, check for recommends */
2014 Id *recp, rec, pp, p;
2015 s = pool->solvables + i;
2016 if (!solv->addalreadyrecommended && s->repo == solv->installed)
2018 /* XXX need to special case AND ? */
2021 recp = s->repo->idarraydata + s->recommends;
2022 while ((rec = *recp++) != 0)
2025 FOR_PROVIDES(p, pp, rec)
2027 if (solv->decisionmap[p] > 0)
2032 else if (solv->decisionmap[p] == 0)
2034 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))))
2036 queue_pushunique(&dq, p);
2044 s = pool->solvables + i;
2045 if (!s->supplements)
2047 if (!pool_installable(pool, s))
2049 if (!solver_is_supplementing(solv, s))
2051 if (solv->dupmap_all && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
2053 queue_push(&dqs, i);
2057 /* filter out all packages obsoleted by installed packages */
2058 /* this is no longer needed if we have reverse obsoletes */
2059 if ((dqs.count || dq.count) && solv->installed)
2062 Id obs, *obsp, po, ppo;
2064 map_init(&obsmap, pool->nsolvables);
2065 for (p = solv->installed->start; p < solv->installed->end; p++)
2067 s = pool->solvables + p;
2068 if (s->repo != solv->installed || !s->obsoletes)
2070 if (solv->decisionmap[p] <= 0)
2072 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
2074 obsp = s->repo->idarraydata + s->obsoletes;
2075 /* foreach obsoletes */
2076 while ((obs = *obsp++) != 0)
2077 FOR_PROVIDES(po, ppo, obs)
2078 MAPSET(&obsmap, po);
2080 for (i = j = 0; i < dqs.count; i++)
2081 if (!MAPTST(&obsmap, dqs.elements[i]))
2082 dqs.elements[j++] = dqs.elements[i];
2084 for (i = j = 0; i < dq.count; i++)
2085 if (!MAPTST(&obsmap, dq.elements[i]))
2086 dq.elements[j++] = dq.elements[i];
2091 /* filter out all already supplemented packages if requested */
2092 if (!solv->addalreadyrecommended && dqs.count)
2094 /* turn off all new packages */
2095 for (i = 0; i < solv->decisionq.count; i++)
2097 p = solv->decisionq.elements[i];
2100 s = pool->solvables + p;
2101 if (s->repo && s->repo != solv->installed)
2102 solv->decisionmap[p] = -solv->decisionmap[p];
2104 /* filter out old supplements */
2105 for (i = j = 0; i < dqs.count; i++)
2107 p = dqs.elements[i];
2108 s = pool->solvables + p;
2109 if (!s->supplements)
2111 if (!solver_is_supplementing(solv, s))
2112 dqs.elements[j++] = p;
2115 /* undo turning off */
2116 for (i = 0; i < solv->decisionq.count; i++)
2118 p = solv->decisionq.elements[i];
2121 s = pool->solvables + p;
2122 if (s->repo && s->repo != solv->installed)
2123 solv->decisionmap[p] = -solv->decisionmap[p];
2127 /* multiversion doesn't mix well with supplements.
2128 * filter supplemented packages where we already decided
2129 * to install a different version (see bnc#501088) */
2130 if (dqs.count && solv->noobsoletes.size)
2132 for (i = j = 0; i < dqs.count; i++)
2134 p = dqs.elements[i];
2135 if (MAPTST(&solv->noobsoletes, p))
2138 s = pool->solvables + p;
2139 FOR_PROVIDES(p2, pp2, s->name)
2140 if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
2143 continue; /* ignore this package */
2145 dqs.elements[j++] = p;
2150 /* make dq contain both recommended and supplemented pkgs */
2153 for (i = 0; i < dqs.count; i++)
2154 queue_pushunique(&dq, dqs.elements[i]);
2160 int decisioncount = solv->decisionq.count;
2164 /* simple case, just one package. no need to choose to best version */
2167 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2169 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2170 level = setpropagatelearn(solv, level, p, 0, 0);
2173 continue; /* back to main loop */
2176 /* filter packages, this gives us the best versions */
2177 policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
2179 /* create map of result */
2180 map_init(&dqmap, pool->nsolvables);
2181 for (i = 0; i < dq.count; i++)
2182 MAPSET(&dqmap, dq.elements[i]);
2184 /* install all supplemented packages */
2185 for (i = 0; i < dqs.count; i++)
2187 p = dqs.elements[i];
2188 if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
2190 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2192 level = setpropagatelearn(solv, level, p, 0, 0);
2193 if (level <= olevel)
2196 if (i < dqs.count || solv->decisionq.count < decisioncount)
2204 /* install all recommended packages */
2205 /* more work as we want to created branches if multiple
2206 * choices are valid */
2207 for (i = 0; i < decisioncount; i++)
2210 p = solv->decisionq.elements[i];
2213 s = pool->solvables + p;
2214 if (!s->repo || (!solv->addalreadyrecommended && s->repo == solv->installed))
2218 recp = s->repo->idarraydata + s->recommends;
2219 while ((rec = *recp++) != 0)
2222 FOR_PROVIDES(p, pp, rec)
2224 if (solv->decisionmap[p] > 0)
2229 else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
2230 queue_pushunique(&dq, p);
2236 /* multiple candidates, open a branch */
2237 for (i = 1; i < dq.count; i++)
2238 queue_push(&solv->branches, dq.elements[i]);
2239 queue_push(&solv->branches, -level);
2242 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2244 level = setpropagatelearn(solv, level, p, 0, 0);
2245 if (level <= olevel || solv->decisionq.count < decisioncount)
2246 break; /* we had to revert some decisions */
2249 break; /* had a problem above, quit loop */
2254 continue; /* back to main loop so that all deps are checked */
2258 solv->decisioncnt_orphan = solv->decisionq.count;
2259 if (solv->dupmap_all && solv->installed)
2261 int installedone = 0;
2263 /* let's see if we can install some unsupported package */
2264 POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding orphaned packages\n");
2265 for (i = 0; i < solv->orphaned.count; i++)
2267 p = solv->orphaned.elements[i];
2268 if (solv->decisionmap[p])
2269 continue; /* already decided */
2270 if (solv->droporphanedmap_all)
2272 if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
2274 POOL_DEBUG(SOLV_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
2276 level = setpropagatelearn(solv, level, p, 0, 0);
2281 if (installedone || i < solv->orphaned.count)
2285 continue; /* back to main loop */
2287 for (i = 0; i < solv->orphaned.count; i++)
2289 p = solv->orphaned.elements[i];
2290 if (solv->decisionmap[p])
2291 continue; /* already decided */
2292 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2294 level = setpropagatelearn(solv, level, -p, 0, 0);
2298 if (i < solv->orphaned.count)
2302 continue; /* back to main loop */
2306 if (solv->installed && solv->cleandepsmap.size)
2308 if (cleandeps_check_mistakes(solv, level))
2310 level = 1; /* restart from scratch */
2315 if (solv->solution_callback)
2317 solv->solution_callback(solv, solv->solution_callback_data);
2318 if (solv->branches.count)
2320 int i = solv->branches.count - 1;
2321 int l = -solv->branches.elements[i];
2325 if (solv->branches.elements[i - 1] < 0)
2327 p = solv->branches.elements[i];
2328 POOL_DEBUG(SOLV_DEBUG_SOLVER, "branching with %s\n", pool_solvid2str(pool, p));
2330 for (j = i + 1; j < solv->branches.count; j++)
2331 queue_push(&dq, solv->branches.elements[j]);
2332 solv->branches.count = i;
2334 revert(solv, level);
2336 for (j = 0; j < dq.count; j++)
2337 queue_push(&solv->branches, dq.elements[j]);
2339 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2341 level = setpropagatelearn(solv, level, p, disablerules, why);
2346 /* all branches done, we're finally finished */
2350 /* auto-minimization step */
2351 if (solv->branches.count)
2353 int l = 0, lasti = -1, lastl = -1;
2357 for (i = solv->branches.count - 1; i >= 0; i--)
2359 p = solv->branches.elements[i];
2362 else if (p > 0 && solv->decisionmap[p] > l + 1)
2370 /* kill old solvable so that we do not loop */
2371 p = solv->branches.elements[lasti];
2372 solv->branches.elements[lasti] = 0;
2373 POOL_DEBUG(SOLV_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, pool_solvid2str(pool, p));
2374 minimizationsteps++;
2377 revert(solv, level);
2378 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2381 level = setpropagatelearn(solv, level, p, disablerules, why);
2384 continue; /* back to main loop */
2387 /* no minimization found, we're finally finished! */
2391 POOL_DEBUG(SOLV_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2393 POOL_DEBUG(SOLV_DEBUG_STATS, "done solving.\n\n");
2399 solv->decisioncnt_update = solv->decisionq.count;
2400 solv->decisioncnt_keep = solv->decisionq.count;
2401 solv->decisioncnt_resolve = solv->decisionq.count;
2402 solv->decisioncnt_weak = solv->decisionq.count;
2403 solv->decisioncnt_orphan = solv->decisionq.count;
2406 solver_printdecisionq(solv, SOLV_DEBUG_RESULT);
2411 /*-------------------------------------------------------------------
2413 * remove disabled conflicts
2415 * purpose: update the decisionmap after some rules were disabled.
2416 * this is used to calculate the suggested/recommended package list.
2417 * Also returns a "removed" list to undo the discisionmap changes.
2421 removedisabledconflicts(Solver *solv, Queue *removed)
2423 Pool *pool = solv->pool;
2428 Id *decisionmap = solv->decisionmap;
2430 queue_empty(removed);
2431 for (i = 0; i < solv->decisionq.count; i++)
2433 p = solv->decisionq.elements[i];
2435 continue; /* conflicts only, please */
2436 why = solv->decisionq_why.elements[i];
2439 /* no rule involved, must be a orphan package drop */
2442 /* we never do conflicts on free decisions, so there
2443 * must have been an unit rule */
2445 r = solv->rules + why;
2446 if (r->d < 0 && decisionmap[-p])
2448 /* rule is now disabled, remove from decisionmap */
2449 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2450 queue_push(removed, -p);
2451 queue_push(removed, decisionmap[-p]);
2452 decisionmap[-p] = 0;
2455 if (!removed->count)
2457 /* we removed some confliced packages. some of them might still
2458 * be in conflict, so search for unit rules and re-conflict */
2460 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2462 if (i == solv->nrules)
2465 r = solv->rules + i;
2471 if (r->p < 0 && !decisionmap[-r->p])
2477 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2479 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2484 if (r->p < 0 && decisionmap[-r->p] == 0)
2486 if (new || DECISIONMAP_FALSE(r->p))
2488 dp = pool->whatprovidesdata + r->d;
2489 while ((p = *dp++) != 0)
2491 if (new && p == new)
2493 if (p < 0 && decisionmap[-p] == 0)
2502 else if (!DECISIONMAP_FALSE(p))
2512 POOL_DEBUG(SOLV_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2513 decisionmap[-new] = -1;
2515 n = 0; /* redo all rules */
2521 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2524 for (i = 0; i < removed->count; i += 2)
2525 solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2529 /*-------------------------------------------------------------------
2531 * weaken solvable dependencies
2535 weaken_solvable_deps(Solver *solv, Id p)
2540 for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2544 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2545 continue; /* conflict */
2546 queue_push(&solv->weakruleq, i);
2551 /********************************************************************/
2556 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap)
2559 Id how, what, select;
2561 for (i = 0; i < job->count; i += 2)
2563 how = job->elements[i];
2564 if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
2566 what = job->elements[i + 1];
2567 select = how & SOLVER_SELECTMASK;
2568 if (!noobsmap->size)
2569 map_grow(noobsmap, pool->nsolvables);
2570 if (select == SOLVER_SOLVABLE_ALL)
2572 FOR_POOL_SOLVABLES(p)
2573 MAPSET(noobsmap, p);
2575 else if (select == SOLVER_SOLVABLE_REPO)
2578 Repo *repo = pool_id2repo(pool, what);
2580 FOR_REPO_SOLVABLES(repo, p, s)
2581 MAPSET(noobsmap, p);
2583 FOR_JOB_SELECT(p, pp, select, what)
2584 MAPSET(noobsmap, p);
2589 * add a rule created by a job, record job number and weak flag
2592 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
2594 solver_addrule(solv, p, d);
2595 queue_push(&solv->ruletojob, job);
2597 queue_push(&solv->weakruleq, solv->nrules - 1);
2601 add_update_target(Solver *solv, Id p, Id how)
2603 Pool *pool = solv->pool;
2604 Solvable *s = pool->solvables + p;
2605 Repo *installed = solv->installed;
2607 if (!solv->update_targets)
2609 solv->update_targets = solv_calloc(1, sizeof(Queue));
2610 queue_init(solv->update_targets);
2612 FOR_PROVIDES(pi, pip, s->name)
2614 Solvable *si = pool->solvables + pi;
2615 if (si->repo != installed || si->name != s->name)
2617 if (how & SOLVER_FORCEBEST)
2619 if (!solv->bestupdatemap.size)
2620 map_grow(&solv->bestupdatemap, installed->end - installed->start);
2621 MAPSET(&solv->bestupdatemap, pi - installed->start);
2623 queue_push2(solv->update_targets, pi, p);
2624 /* check if it's ok to keep the installed package */
2625 if (s->evr == si->evr && solvable_identical(s, si))
2626 queue_push2(solv->update_targets, pi, pi);
2630 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2631 while ((obs = *obsp++) != 0)
2633 FOR_PROVIDES(pi, pip, obs)
2635 Solvable *si = pool->solvables + pi;
2636 if (si->repo != installed)
2638 if (si->name == s->name)
2639 continue; /* already handled above */
2640 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
2642 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
2644 if (how & SOLVER_FORCEBEST)
2646 if (!solv->bestupdatemap.size)
2647 map_grow(&solv->bestupdatemap, installed->end - installed->start);
2648 MAPSET(&solv->bestupdatemap, pi - installed->start);
2650 queue_push2(solv->update_targets, pi, p);
2657 transform_update_targets_sortfn(const void *ap, const void *bp, void *dp)
2667 transform_update_targets(Solver *solv)
2669 Repo *installed = solv->installed;
2670 Queue *update_targets = solv->update_targets;
2672 Id p, q, lastp, lastq;
2674 if (!update_targets->count)
2676 queue_free(update_targets);
2677 solv->update_targets = solv_free(update_targets);
2680 if (update_targets->count > 2)
2681 solv_sort(update_targets->elements, update_targets->count >> 1, 2 * sizeof(Id), transform_update_targets_sortfn, solv);
2682 queue_insertn(update_targets, 0, installed->end - installed->start);
2684 for (i = j = installed->end - installed->start; i < update_targets->count; i += 2)
2686 if ((p = update_targets->elements[i]) != lastp)
2688 if (!solv->updatemap.size)
2689 map_grow(&solv->updatemap, installed->end - installed->start);
2690 MAPSET(&solv->updatemap, p - installed->start);
2691 update_targets->elements[j++] = 0; /* finish old set */
2692 update_targets->elements[p - installed->start] = j; /* start new set */
2696 if ((q = update_targets->elements[i + 1]) != lastq)
2698 update_targets->elements[j++] = q;
2702 queue_truncate(update_targets, j);
2703 queue_push(update_targets, 0); /* finish last set */
2714 solver_solve(Solver *solv, Queue *job)
2716 Pool *pool = solv->pool;
2717 Repo *installed = solv->installed;
2720 Map addedmap; /* '1' == have rpm-rules for solvable */
2721 Map installcandidatemap;
2722 Id how, what, select, name, weak, p, pp, d;
2726 int now, solve_start;
2728 int hasbestinstalljob = 0;
2730 solve_start = solv_timems(0);
2732 /* log solver options */
2733 POOL_DEBUG(SOLV_DEBUG_STATS, "solver started\n");
2734 POOL_DEBUG(SOLV_DEBUG_STATS, "dosplitprovides=%d, noupdateprovide=%d, noinfarchcheck=%d\n", solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
2735 POOL_DEBUG(SOLV_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allownamechange=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allownamechange, solv->allowarchchange, solv->allowvendorchange);
2736 POOL_DEBUG(SOLV_DEBUG_STATS, "promoteepoch=%d, forbidselfconflicts=%d\n", pool->promoteepoch, pool->forbidselfconflicts);
2737 POOL_DEBUG(SOLV_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2738 POOL_DEBUG(SOLV_DEBUG_STATS, "dontinstallrecommended=%d, addalreadyrecommended=%d\n", solv->dontinstallrecommended, solv->addalreadyrecommended);
2740 /* create whatprovides if not already there */
2741 if (!pool->whatprovides)
2742 pool_createwhatprovides(pool);
2744 /* create obsolete index */
2745 policy_create_obsolete_index(solv);
2748 queue_free(&solv->job);
2749 queue_init_clone(&solv->job, job);
2751 if (solv->update_targets)
2753 queue_free(solv->update_targets);
2754 solv->update_targets = solv_free(solv->update_targets);
2757 * create basic rule set of all involved packages
2758 * use addedmap bitmap to make sure we don't create rules twice
2761 /* create noobsolete map if needed */
2762 solver_calculate_noobsmap(pool, job, &solv->noobsoletes);
2764 map_init(&addedmap, pool->nsolvables);
2765 MAPSET(&addedmap, SYSTEMSOLVABLE);
2767 map_init(&installcandidatemap, pool->nsolvables);
2770 now = solv_timems(0);
2772 * create rules for all package that could be involved with the solving
2773 * so called: rpm rules
2778 /* check for update/verify jobs as they need to be known early */
2779 for (i = 0; i < job->count; i += 2)
2781 how = job->elements[i];
2782 what = job->elements[i + 1];
2783 select = how & SOLVER_SELECTMASK;
2784 switch (how & SOLVER_JOBMASK)
2787 if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
2788 solv->fixmap_all = 1;
2789 FOR_JOB_SELECT(p, pp, select, what)
2791 s = pool->solvables + p;
2792 if (s->repo != installed)
2794 if (!solv->fixmap.size)
2795 map_grow(&solv->fixmap, installed->end - installed->start);
2796 MAPSET(&solv->fixmap, p - installed->start);
2800 if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
2802 solv->updatemap_all = 1;
2803 if (how & SOLVER_FORCEBEST)
2804 solv->bestupdatemap_all = 1;
2806 else if (select == SOLVER_SOLVABLE_REPO && what != installed->repoid)
2808 Repo *repo = pool_id2repo(pool, what);
2811 /* targeted update */
2812 FOR_REPO_SOLVABLES(repo, p, s)
2813 add_update_target(solv, p, how);
2818 FOR_JOB_SELECT(p, pp, select, what)
2820 s = pool->solvables + p;
2821 if (s->repo != installed)
2823 if (!solv->updatemap.size)
2824 map_grow(&solv->updatemap, installed->end - installed->start);
2825 MAPSET(&solv->updatemap, p - installed->start);
2826 if (how & SOLVER_FORCEBEST)
2828 if (!solv->bestupdatemap.size)
2829 map_grow(&solv->bestupdatemap, installed->end - installed->start);
2830 MAPSET(&solv->bestupdatemap, p - installed->start);
2836 FOR_JOB_SELECT(p, pp, select, what)
2837 add_update_target(solv, p, how);
2846 if (solv->update_targets)
2847 transform_update_targets(solv);
2849 oldnrules = solv->nrules;
2850 FOR_REPO_SOLVABLES(installed, p, s)
2851 solver_addrpmrulesforsolvable(solv, s, &addedmap);
2852 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
2853 oldnrules = solv->nrules;
2854 FOR_REPO_SOLVABLES(installed, p, s)
2855 solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
2856 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
2860 * create rules for all packages involved in the job
2861 * (to be installed or removed)
2864 oldnrules = solv->nrules;
2865 for (i = 0; i < job->count; i += 2)
2867 how = job->elements[i];
2868 what = job->elements[i + 1];
2869 select = how & SOLVER_SELECTMASK;
2871 switch (how & SOLVER_JOBMASK)
2873 case SOLVER_INSTALL:
2874 FOR_JOB_SELECT(p, pp, select, what)
2876 MAPSET(&installcandidatemap, p);
2877 solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
2880 case SOLVER_DISTUPGRADE:
2881 if (select == SOLVER_SOLVABLE_ALL)
2883 solv->dupmap_all = 1;
2884 solv->updatemap_all = 1;
2885 if (how & SOLVER_FORCEBEST)
2886 solv->bestupdatemap_all = 1;
2888 if (!solv->dupmap_all)
2895 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
2899 * add rules for suggests, enhances
2901 oldnrules = solv->nrules;
2902 solver_addrpmrulesforweak(solv, &addedmap);
2903 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
2906 * first pass done, we now have all the rpm rules we need.
2907 * unify existing rules before going over all job rules and
2909 * at this point the system is always solvable,
2910 * as an empty system (remove all packages) is a valid solution
2913 IF_POOLDEBUG (SOLV_DEBUG_STATS)
2915 int possible = 0, installable = 0;
2916 for (i = 1; i < pool->nsolvables; i++)
2918 if (pool_installable(pool, pool->solvables + i))
2920 if (MAPTST(&addedmap, i))
2923 POOL_DEBUG(SOLV_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
2926 solver_unifyrules(solv); /* remove duplicate rpm rules */
2927 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
2929 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
2930 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule creation took %d ms\n", solv_timems(now));
2932 /* create dup maps if needed. We need the maps early to create our
2935 solver_createdupmaps(solv);
2938 * create feature rules
2940 * foreach installed:
2941 * create assertion (keep installed, if no update available)
2943 * create update rule (A|update1(A)|update2(A)|...)
2945 * those are used later on to keep a version of the installed packages in
2949 solv->featurerules = solv->nrules; /* mark start of feature rules */
2952 /* foreach possibly installed solvable */
2953 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2955 if (s->repo != installed)
2957 solver_addrule(solv, 0, 0); /* create dummy rule */
2960 solver_addupdaterule(solv, s, 1); /* allow s to be updated */
2962 /* make sure we accounted for all rules */
2963 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
2965 solv->featurerules_end = solv->nrules;
2968 * Add update rules for installed solvables
2970 * almost identical to feature rules
2971 * except that downgrades/archchanges/vendorchanges are not allowed
2974 solv->updaterules = solv->nrules;
2977 { /* foreach installed solvables */
2978 /* we create all update rules, but disable some later on depending on the job */
2979 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2983 if (s->repo != installed)
2985 solver_addrule(solv, 0, 0); /* create dummy rule */
2988 solver_addupdaterule(solv, s, 0); /* allowall = 0: downgrades not allowed */
2990 * check for and remove duplicate
2992 r = solv->rules + solv->nrules - 1; /* r: update rule */
2993 sr = r - (installed->end - installed->start); /* sr: feature rule */
2994 /* it's orphaned if there is no feature rule or the feature rule
2995 * consists just of the installed package */
2996 if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
2997 queue_push(&solv->orphaned, i);
3000 assert(solv->dupmap_all && !sr->p);
3003 if (!solver_samerule(solv, r, sr))
3004 memset(sr, 0, sizeof(*sr)); /* delete unneeded feature rule */
3006 solver_disablerule(solv, sr); /* disable feature rule */
3008 /* consistency check: we added a rule for _every_ installed solvable */
3009 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
3011 solv->updaterules_end = solv->nrules;
3015 * now add all job rules
3018 solv->jobrules = solv->nrules;
3019 if (solv->cleandeps_updatepkgs)
3021 queue_free(solv->cleandeps_updatepkgs);
3022 solv->cleandeps_updatepkgs = solv_free(solv->cleandeps_updatepkgs);
3024 for (i = 0; i < job->count; i += 2)
3026 oldnrules = solv->nrules;
3028 how = job->elements[i];
3029 what = job->elements[i + 1];
3030 weak = how & SOLVER_WEAK;
3031 select = how & SOLVER_SELECTMASK;
3032 switch (how & SOLVER_JOBMASK)
3034 case SOLVER_INSTALL:
3035 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3036 if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3037 map_grow(&solv->cleandepsmap, installed->end - installed->start);
3038 if (select == SOLVER_SOLVABLE)
3046 FOR_JOB_SELECT(p, pp, select, what)
3050 /* no candidate found, make this an impossible rule */
3051 queue_push(&q, -SYSTEMSOLVABLE);
3053 p = queue_shift(&q); /* get first candidate */
3054 d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q); /* internalize */
3056 solver_addjobrule(solv, p, d, i, weak);
3057 if (how & SOLVER_FORCEBEST)
3058 hasbestinstalljob = 1;
3061 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
3062 if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3063 map_grow(&solv->cleandepsmap, installed->end - installed->start);
3064 /* specific solvable: by id or by nevra */
3065 name = (select == SOLVER_SOLVABLE || (select == SOLVER_SOLVABLE_NAME && ISRELDEP(what))) ? 0 : -1;
3066 if (select == SOLVER_SOLVABLE_ALL) /* hmmm ;) */
3068 FOR_POOL_SOLVABLES(p)
3069 solver_addjobrule(solv, -p, 0, i, weak);
3071 else if (select == SOLVER_SOLVABLE_REPO)
3073 Repo *repo = pool_id2repo(pool, what);
3075 FOR_REPO_SOLVABLES(repo, p, s)
3076 solver_addjobrule(solv, -p, 0, i, weak);
3078 FOR_JOB_SELECT(p, pp, select, what)
3080 s = pool->solvables + p;
3081 if (installed && s->repo == installed)
3082 name = !name ? s->name : -1;
3083 solver_addjobrule(solv, -p, 0, i, weak);
3085 /* special case for "erase a specific solvable": we also
3086 * erase all other solvables with that name, so that they
3087 * don't get picked up as replacement.
3088 * name is > 0 if exactly one installed solvable matched.
3090 /* XXX: look also at packages that obsolete this package? */
3095 FOR_PROVIDES(p, pp, name)
3097 s = pool->solvables + p;
3098 if (s->name != name)
3100 /* keep other versions installed */
3101 if (s->repo == installed)
3103 /* keep installcandidates of other jobs */
3104 if (MAPTST(&installcandidatemap, p))
3106 /* don't add the same rule twice */
3107 for (j = oldnrules; j < k; j++)
3108 if (solv->rules[j].p == -p)
3111 solver_addjobrule(solv, -p, 0, i, weak); /* remove by id */
3117 if ((how & SOLVER_CLEANDEPS) != 0 && installed)
3119 if (how == SOLVER_SOLVABLE_ALL || (how == SOLVER_SOLVABLE_REPO && what == installed->repoid))
3121 FOR_REPO_SOLVABLES(installed, p, s)
3123 if (!solv->cleandeps_updatepkgs)
3125 solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
3126 queue_init(solv->cleandeps_updatepkgs);
3128 queue_pushunique(solv->cleandeps_updatepkgs, p);
3129 if (!solv->cleandepsmap.size)
3130 map_grow(&solv->cleandepsmap, installed->end - installed->start);
3133 FOR_JOB_SELECT(p, pp, select, what)
3135 s = pool->solvables + p;
3136 if (s->repo != installed)
3138 if (!solv->cleandeps_updatepkgs)
3140 solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
3141 queue_init(solv->cleandeps_updatepkgs);
3143 queue_pushunique(solv->cleandeps_updatepkgs, p);
3144 if (!solv->cleandepsmap.size)
3145 map_grow(&solv->cleandepsmap, installed->end - installed->start);
3148 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3151 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3153 case SOLVER_WEAKENDEPS:
3154 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3155 if (select != SOLVER_SOLVABLE)
3157 s = pool->solvables + what;
3158 weaken_solvable_deps(solv, what);
3160 case SOLVER_NOOBSOLETES:
3161 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3164 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3165 if (select == SOLVER_SOLVABLE_ALL)
3167 FOR_POOL_SOLVABLES(p)
3168 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3170 else if (select == SOLVER_SOLVABLE_REPO)
3172 Repo *repo = pool_id2repo(pool, what);
3174 FOR_REPO_SOLVABLES(repo, p, s)
3175 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3177 FOR_JOB_SELECT(p, pp, select, what)
3178 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3180 case SOLVER_DISTUPGRADE:
3181 POOL_DEBUG(SOLV_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
3183 case SOLVER_DROP_ORPHANED:
3184 POOL_DEBUG(SOLV_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
3185 if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3186 solv->droporphanedmap_all = 1;
3187 FOR_JOB_SELECT(p, pp, select, what)
3189 s = pool->solvables + p;
3190 if (!installed || s->repo != installed)
3192 if (!solv->droporphanedmap.size)
3193 map_grow(&solv->droporphanedmap, installed->end - installed->start);
3194 MAPSET(&solv->droporphanedmap, p - installed->start);
3197 case SOLVER_USERINSTALLED:
3198 POOL_DEBUG(SOLV_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
3201 POOL_DEBUG(SOLV_DEBUG_JOB, "job: unknown job\n");
3209 IF_POOLDEBUG (SOLV_DEBUG_JOB)
3212 if (solv->nrules == oldnrules)
3213 POOL_DEBUG(SOLV_DEBUG_JOB, " - no rule created\n");
3214 for (j = oldnrules; j < solv->nrules; j++)
3216 POOL_DEBUG(SOLV_DEBUG_JOB, " - job ");
3217 solver_printrule(solv, SOLV_DEBUG_JOB, solv->rules + j);
3221 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
3222 solv->jobrules_end = solv->nrules;
3224 /* now create infarch and dup rules */
3225 if (!solv->noinfarchcheck)
3227 solver_addinfarchrules(solv, &addedmap);
3228 if (pool->obsoleteusescolors)
3230 /* currently doesn't work well with infarch rules, so make
3232 for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
3233 queue_push(&solv->weakruleq, i);
3237 solv->infarchrules = solv->infarchrules_end = solv->nrules;
3240 solver_addduprules(solv, &addedmap);
3242 solv->duprules = solv->duprules_end = solv->nrules;
3244 if (solv->bestupdatemap_all || solv->bestupdatemap.size || hasbestinstalljob)
3245 solver_addbestrules(solv, hasbestinstalljob);
3247 solv->bestrules = solv->bestrules_end = solv->nrules;
3250 solver_freedupmaps(solv); /* no longer needed */
3253 solver_addchoicerules(solv);
3255 solv->choicerules = solv->choicerules_end = solv->nrules;
3259 for (i = solv->featurerules; i < solv->nrules; i++)
3260 solver_printruleclass(solv, SOLV_DEBUG_RESULT, solv->rules + i);
3262 /* all rules created
3263 * --------------------------------------------------------------
3264 * prepare for solving
3267 /* free unneeded memory */
3268 map_free(&addedmap);
3269 map_free(&installcandidatemap);
3272 POOL_DEBUG(SOLV_DEBUG_STATS, "%d rpm rules, 2 * %d update rules, %d job rules, %d infarch rules, %d dup rules, %d choice rules, %d best rules\n", solv->rpmrules_end - 1, solv->updaterules_end - solv->updaterules, solv->jobrules_end - solv->jobrules, solv->infarchrules_end - solv->infarchrules, solv->duprules_end - solv->duprules, solv->choicerules_end - solv->choicerules, solv->bestrules_end - solv->bestrules);
3273 POOL_DEBUG(SOLV_DEBUG_STATS, "overall rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3275 /* create weak map */
3276 map_init(&solv->weakrulemap, solv->nrules);
3277 for (i = 0; i < solv->weakruleq.count; i++)
3279 p = solv->weakruleq.elements[i];
3280 MAPSET(&solv->weakrulemap, p);
3283 /* all new rules are learnt after this point */
3284 solv->learntrules = solv->nrules;
3286 /* create watches chains */
3289 /* create assertion index. it is only used to speed up
3290 * makeruledecsions() a bit */
3291 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
3292 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
3293 queue_push(&solv->ruleassertions, i);
3295 /* disable update rules that conflict with our job */
3296 solver_disablepolicyrules(solv);
3298 /* make initial decisions based on assertion rules */
3299 makeruledecisions(solv);
3300 POOL_DEBUG(SOLV_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
3303 if (solv->cleandeps_mistakes)
3305 queue_free(solv->cleandeps_mistakes);
3306 solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
3310 * ********************************************
3312 * ********************************************
3315 now = solv_timems(0);
3316 solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
3317 POOL_DEBUG(SOLV_DEBUG_STATS, "solver took %d ms\n", solv_timems(now));
3320 * prepare solution queue if there were problems
3322 solver_prepare_solutions(solv);
3324 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);
3325 POOL_DEBUG(SOLV_DEBUG_STATS, "solver_solve took %d ms\n", solv_timems(solve_start));
3327 /* return number of problems */
3328 return solv->problems.count ? solv->problems.count / 2 : 0;
3332 solver_create_transaction(Solver *solv)
3334 return transaction_create_decisionq(solv->pool, &solv->decisionq, &solv->noobsoletes);
3337 void solver_get_orphaned(Solver *solv, Queue *orphanedq)
3339 queue_free(orphanedq);
3340 queue_init_clone(orphanedq, &solv->orphaned);
3343 void solver_get_recommendations(Solver *solv, Queue *recommendationsq, Queue *suggestionsq, int noselected)
3345 Pool *pool = solv->pool;
3346 Queue redoq, disabledq;
3352 if (!recommendationsq && !suggestionsq)
3355 map_init(&obsmap, pool->nsolvables);
3356 if (solv->installed)
3358 Id obs, *obsp, p, po, ppo;
3359 for (p = solv->installed->start; p < solv->installed->end; p++)
3361 s = pool->solvables + p;
3362 if (s->repo != solv->installed || !s->obsoletes)
3364 if (solv->decisionmap[p] <= 0)
3366 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
3368 obsp = s->repo->idarraydata + s->obsoletes;
3369 /* foreach obsoletes */
3370 while ((obs = *obsp++) != 0)
3371 FOR_PROVIDES(po, ppo, obs)
3372 MAPSET(&obsmap, po);
3377 queue_init(&disabledq);
3379 /* disable all erase jobs (including weak "keep uninstalled" rules) */
3380 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
3382 if (r->d < 0) /* disabled ? */
3384 if (r->p >= 0) /* install job? */
3386 queue_push(&disabledq, i);
3387 solver_disablerule(solv, r);
3393 enabledisablelearntrules(solv);
3394 removedisabledconflicts(solv, &redoq);
3398 * find recommended packages
3400 if (recommendationsq)
3402 Id rec, *recp, p, pp;
3404 queue_empty(recommendationsq);
3405 /* create map of all recommened packages */
3406 solv->recommends_index = -1;
3407 MAPZERO(&solv->recommendsmap);
3409 /* put all packages the solver already chose in the map */
3410 if (solv->decisioncnt_weak)
3412 for (i = solv->decisioncnt_weak; i < solv->decisioncnt_orphan; i++)
3415 why = solv->decisionq_why.elements[i];
3417 continue; /* forced by unit rule */
3418 p = solv->decisionq.elements[i];
3421 MAPSET(&solv->recommendsmap, p);
3425 for (i = 0; i < solv->decisionq.count; i++)
3427 p = solv->decisionq.elements[i];
3430 s = pool->solvables + p;
3433 recp = s->repo->idarraydata + s->recommends;
3434 while ((rec = *recp++) != 0)
3436 FOR_PROVIDES(p, pp, rec)
3437 if (solv->decisionmap[p] > 0)
3443 FOR_PROVIDES(p, pp, rec)
3444 if (solv->decisionmap[p] > 0)
3445 MAPSET(&solv->recommendsmap, p);
3447 continue; /* p != 0: already fulfilled */
3449 FOR_PROVIDES(p, pp, rec)
3450 MAPSET(&solv->recommendsmap, p);
3454 for (i = 1; i < pool->nsolvables; i++)
3456 if (solv->decisionmap[i] < 0)
3458 if (solv->decisionmap[i] > 0 && noselected)
3460 if (MAPTST(&obsmap, i))
3462 s = pool->solvables + i;
3463 if (!MAPTST(&solv->recommendsmap, i))
3465 if (!s->supplements)
3467 if (!pool_installable(pool, s))
3469 if (!solver_is_supplementing(solv, s))
3472 queue_push(recommendationsq, i);
3474 /* we use MODE_SUGGEST here so that repo prio is ignored */
3475 policy_filter_unwanted(solv, recommendationsq, POLICY_MODE_SUGGEST);
3479 * find suggested packages
3484 Id sug, *sugp, p, pp;
3486 queue_empty(suggestionsq);
3487 /* create map of all suggests that are still open */
3488 solv->recommends_index = -1;
3489 MAPZERO(&solv->suggestsmap);
3490 for (i = 0; i < solv->decisionq.count; i++)
3492 p = solv->decisionq.elements[i];
3495 s = pool->solvables + p;
3498 sugp = s->repo->idarraydata + s->suggests;
3499 while ((sug = *sugp++) != 0)
3501 FOR_PROVIDES(p, pp, sug)
3502 if (solv->decisionmap[p] > 0)
3508 FOR_PROVIDES(p, pp, sug)
3509 if (solv->decisionmap[p] > 0)
3510 MAPSET(&solv->suggestsmap, p);
3512 continue; /* already fulfilled */
3514 FOR_PROVIDES(p, pp, sug)
3515 MAPSET(&solv->suggestsmap, p);
3519 for (i = 1; i < pool->nsolvables; i++)
3521 if (solv->decisionmap[i] < 0)
3523 if (solv->decisionmap[i] > 0 && noselected)
3525 if (MAPTST(&obsmap, i))
3527 s = pool->solvables + i;
3528 if (!MAPTST(&solv->suggestsmap, i))
3532 if (!pool_installable(pool, s))
3534 if (!solver_is_enhancing(solv, s))
3537 queue_push(suggestionsq, i);
3539 policy_filter_unwanted(solv, suggestionsq, POLICY_MODE_SUGGEST);
3542 /* undo removedisabledconflicts */
3544 undo_removedisabledconflicts(solv, &redoq);
3547 /* undo job rule disabling */
3548 for (i = 0; i < disabledq.count; i++)
3549 solver_enablerule(solv, solv->rules + disabledq.elements[i]);
3550 queue_free(&disabledq);
3555 /***********************************************************************/
3556 /* disk usage computations */
3558 /*-------------------------------------------------------------------
3560 * calculate DU changes
3564 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
3568 solver_create_state_maps(solv, &installedmap, 0);
3569 pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
3570 map_free(&installedmap);
3574 /*-------------------------------------------------------------------
3576 * calculate changes in install size
3580 solver_calc_installsizechange(Solver *solv)
3585 solver_create_state_maps(solv, &installedmap, 0);
3586 change = pool_calc_installsizechange(solv->pool, &installedmap);
3587 map_free(&installedmap);
3592 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
3594 pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
3598 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
3601 pool_create_state_maps(solv->pool, &solv->decisionq, &installedmap, 0);
3602 pool_trivial_installable_noobsoletesmap(solv->pool, &installedmap, pkgs, res, solv->noobsoletes.size ? &solv->noobsoletes : 0);
3603 map_free(&installedmap);
3606 /*-------------------------------------------------------------------
3608 * decision introspection
3612 solver_get_decisionlevel(Solver *solv, Id p)
3614 return solv->decisionmap[p];
3618 solver_get_decisionqueue(Solver *solv, Queue *decisionq)
3620 queue_free(decisionq);
3621 queue_init_clone(decisionq, &solv->decisionq);
3625 solver_get_lastdecisionblocklevel(Solver *solv)
3628 if (solv->decisionq.count == 0)
3630 p = solv->decisionq.elements[solv->decisionq.count - 1];
3633 return solv->decisionmap[p] < 0 ? -solv->decisionmap[p] : solv->decisionmap[p];
3637 solver_get_decisionblock(Solver *solv, int level, Queue *decisionq)
3642 queue_empty(decisionq);
3643 for (i = 0; i < solv->decisionq.count; i++)
3645 p = solv->decisionq.elements[i];
3648 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3651 if (i == solv->decisionq.count)
3653 for (i = 0; i < solv->decisionq.count; i++)
3655 p = solv->decisionq.elements[i];
3658 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3659 queue_push(decisionq, p);
3666 solver_describe_decision(Solver *solv, Id p, Id *infop)
3673 if (!solv->decisionmap[p])
3674 return SOLVER_REASON_UNRELATED;
3675 pp = solv->decisionmap[p] < 0 ? -p : p;
3676 for (i = 0; i < solv->decisionq.count; i++)
3677 if (solv->decisionq.elements[i] == pp)
3679 if (i == solv->decisionq.count) /* just in case... */
3680 return SOLVER_REASON_UNRELATED;
3681 why = solv->decisionq_why.elements[i];
3686 return SOLVER_REASON_UNIT_RULE;
3689 if (i < solv->decisioncnt_update)
3694 *infop = SYSTEMSOLVABLE;
3695 return SOLVER_REASON_KEEP_INSTALLED;
3699 return SOLVER_REASON_RESOLVE_JOB;
3701 if (i < solv->decisioncnt_keep)
3703 if (why == 0 && pp < 0)
3704 return SOLVER_REASON_CLEANDEPS_ERASE;
3707 if (why >= solv->updaterules && why < solv->updaterules_end)
3708 *infop = why - solv->updaterules;
3709 else if (why >= solv->featurerules && why < solv->featurerules_end)
3710 *infop = why - solv->featurerules;
3712 return SOLVER_REASON_UPDATE_INSTALLED;
3714 if (i < solv->decisioncnt_resolve)
3716 if (why == 0 && pp < 0)
3717 return SOLVER_REASON_CLEANDEPS_ERASE;
3720 if (why >= solv->updaterules && why < solv->updaterules_end)
3721 *infop = why - solv->updaterules;
3722 else if (why >= solv->featurerules && why < solv->featurerules_end)
3723 *infop = why - solv->featurerules;
3725 return SOLVER_REASON_KEEP_INSTALLED;
3727 if (i < solv->decisioncnt_weak)
3731 return SOLVER_REASON_RESOLVE;
3733 if (solv->decisionq.count < solv->decisioncnt_orphan)
3734 return SOLVER_REASON_WEAKDEP;
3735 return SOLVER_REASON_RESOLVE_ORPHAN;
3740 solver_describe_weakdep_decision(Solver *solv, Id p, Queue *whyq)
3742 Pool *pool = solv->pool;
3744 int level = solv->decisionmap[p];
3751 for (decisionno = 0; decisionno < solv->decisionq.count; decisionno++)
3752 if (solv->decisionq.elements[decisionno] == p)
3754 if (decisionno == solv->decisionq.count)
3756 if (decisionno < solv->decisioncnt_weak || decisionno >= solv->decisioncnt_orphan)
3759 /* 1) list all packages that recommend us */
3760 for (i = 1; i < pool->nsolvables; i++)
3762 Id *recp, rec, pp2, p2;
3763 if (solv->decisionmap[i] < 0 || solv->decisionmap[i] >= level)
3765 s = pool->solvables + i;
3768 if (!solv->addalreadyrecommended && s->repo == solv->installed)
3770 recp = s->repo->idarraydata + s->recommends;
3771 while ((rec = *recp++) != 0)
3774 FOR_PROVIDES(p2, pp2, rec)
3780 /* if p2 is already installed, this recommends is ignored */
3781 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3787 queue_push(whyq, SOLVER_REASON_RECOMMENDED);
3788 queue_push2(whyq, p2, rec);
3792 /* 2) list all supplements */
3793 s = pool->solvables + p;
3794 if (s->supplements && level > 0)
3796 Id *supp, sup, pp2, p2;
3797 /* this is a hack. to use solver_dep_fulfilled we temporarily clear
3798 * everything above our level in the decisionmap */
3799 for (i = decisionno; i < solv->decisionq.count; i++ )
3801 p2 = solv->decisionq.elements[i];
3803 solv->decisionmap[p2] = -solv->decisionmap[p2];
3805 supp = s->repo->idarraydata + s->supplements;
3806 while ((sup = *supp++) != 0)
3807 if (solver_dep_fulfilled(solv, sup))
3810 /* let's see if this is an easy supp */
3811 FOR_PROVIDES(p2, pp2, sup)
3813 if (!solv->addalreadyrecommended && solv->installed)
3815 if (pool->solvables[p2].repo == solv->installed)
3818 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3820 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3821 queue_push2(whyq, p2, sup);
3827 /* hard case, just note dependency with no package */
3828 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3829 queue_push2(whyq, 0, sup);
3832 for (i = decisionno; i < solv->decisionq.count; i++)
3834 p2 = solv->decisionq.elements[i];
3836 solv->decisionmap[p2] = -solv->decisionmap[p2];
3842 pool_job2solvables(Pool *pool, Queue *pkgs, Id how, Id what)
3845 how &= SOLVER_SELECTMASK;
3847 if (how == SOLVER_SOLVABLE_ALL)
3849 FOR_POOL_SOLVABLES(p)
3850 queue_push(pkgs, p);
3852 else if (how == SOLVER_SOLVABLE_REPO)
3854 Repo *repo = pool_id2repo(pool, what);
3857 FOR_REPO_SOLVABLES(repo, p, s)
3858 queue_push(pkgs, p);
3862 FOR_JOB_SELECT(p, pp, how, what)
3863 queue_push(pkgs, p);
3868 pool_isemptyupdatejob(Pool *pool, Id how, Id what)
3871 Id select = how & SOLVER_SELECTMASK;
3872 if ((how & SOLVER_JOBMASK) != SOLVER_UPDATE)
3874 if (select == SOLVER_SOLVABLE_ALL || select == SOLVER_SOLVABLE_REPO)
3876 if (!pool->installed)
3878 FOR_JOB_SELECT(p, pp, select, what)
3879 if (pool->solvables[p].repo == pool->installed)
3882 FOR_JOB_SELECT(p, pp, select, what)
3884 Solvable *s = pool->solvables + p;
3885 FOR_PROVIDES(pi, pip, s->name)
3887 Solvable *si = pool->solvables + pi;
3888 if (si->repo != pool->installed || si->name != s->name)
3894 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
3895 while ((obs = *obsp++) != 0)
3897 FOR_PROVIDES(pi, pip, obs)
3899 Solvable *si = pool->solvables + pi;
3900 if (si->repo != pool->installed)
3902 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
3904 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))