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;
1488 case SOLVER_FLAG_NO_AUTOTARGET:
1489 return solv->noautotarget;
1497 solver_set_flag(Solver *solv, int flag, int value)
1499 int old = solver_get_flag(solv, flag);
1502 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1503 solv->allowdowngrade = value;
1505 case SOLVER_FLAG_ALLOW_NAMECHANGE:
1506 solv->allownamechange = value;
1508 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1509 solv->allowarchchange = value;
1511 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1512 solv->allowvendorchange = value;
1514 case SOLVER_FLAG_ALLOW_UNINSTALL:
1515 solv->allowuninstall = value;
1517 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1518 solv->noupdateprovide = value;
1520 case SOLVER_FLAG_SPLITPROVIDES:
1521 solv->dosplitprovides = value;
1523 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1524 solv->dontinstallrecommended = value;
1526 case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1527 solv->addalreadyrecommended = value;
1529 case SOLVER_FLAG_NO_INFARCHCHECK:
1530 solv->noinfarchcheck = value;
1532 case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1533 solv->keepexplicitobsoletes = value;
1535 case SOLVER_FLAG_BEST_OBEY_POLICY:
1536 solv->bestobeypolicy = value;
1538 case SOLVER_FLAG_NO_AUTOTARGET:
1539 solv->noautotarget = value;
1548 cleandeps_check_mistakes(Solver *solv, int level)
1550 Pool *pool = solv->pool;
1554 int mademistake = 0;
1556 if (!solv->cleandepsmap.size)
1558 /* check for mistakes */
1559 for (i = solv->installed->start; i < solv->installed->end; i++)
1561 if (!MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1563 r = solv->rules + solv->featurerules + (i - solv->installed->start);
1564 /* a mistake is when the featurerule is true but the updaterule is false */
1567 FOR_RULELITERALS(p, pp, r)
1568 if (p > 0 && solv->decisionmap[p] > 0)
1571 continue; /* feature rule is not true */
1572 r = solv->rules + solv->updaterules + (i - solv->installed->start);
1575 FOR_RULELITERALS(p, pp, r)
1576 if (p > 0 && solv->decisionmap[p] > 0)
1579 continue; /* update rule is true */
1580 POOL_DEBUG(SOLV_DEBUG_SOLVER, "cleandeps mistake: ");
1581 solver_printruleclass(solv, SOLV_DEBUG_SOLVER, r);
1582 POOL_DEBUG(SOLV_DEBUG_SOLVER, "feature rule: ");
1583 solver_printruleclass(solv, SOLV_DEBUG_SOLVER, solv->rules + solv->featurerules + (i - solv->installed->start));
1584 if (!solv->cleandeps_mistakes)
1586 solv->cleandeps_mistakes = solv_calloc(1, sizeof(Queue));
1587 queue_init(solv->cleandeps_mistakes);
1589 queue_push(solv->cleandeps_mistakes, i);
1590 MAPCLR(&solv->cleandepsmap, i - solv->installed->start);
1591 solver_reenablepolicyrules_cleandeps(solv, i);
1600 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
1604 for (i = j = 0; i < q->count; i++)
1607 for (cp2 = cp; *cp2; cp2++)
1610 q->elements[j++] = p;
1614 queue_truncate(q, j);
1617 /*-------------------------------------------------------------------
1621 * all rules have been set up, now actually run the solver
1626 solver_run_sat(Solver *solv, int disablerules, int doweak)
1628 Queue dq; /* local decisionqueue */
1629 Queue dqs; /* local decisionqueue for supplements */
1635 Pool *pool = solv->pool;
1637 int minimizationsteps;
1638 int installedpos = solv->installed ? solv->installed->start : 0;
1640 IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
1642 POOL_DEBUG (SOLV_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1643 for (i = 1; i < solv->nrules; i++)
1644 solver_printruleclass(solv, SOLV_DEBUG_RULE_CREATION, solv->rules + i);
1647 POOL_DEBUG(SOLV_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1649 /* start SAT algorithm */
1651 systemlevel = level + 1;
1652 POOL_DEBUG(SOLV_DEBUG_SOLVER, "solving...\n");
1658 * here's the main loop:
1659 * 1) propagate new decisions (only needed once)
1661 * 3) try to keep installed packages
1662 * 4) fulfill all unresolved rules
1663 * 5) install recommended packages
1664 * 6) minimalize solution if we had choices
1665 * if we encounter a problem, we rewind to a safe level and restart
1669 minimizationsteps = 0;
1673 * initial propagation of the assertions
1677 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1678 if ((r = propagate(solv, level)) != 0)
1680 if (analyze_unsolvable(solv, r, disablerules))
1683 break; /* unsolvable */
1688 * resolve jobs first
1690 if (level < systemlevel)
1692 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving job rules\n");
1693 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1696 if (r->d < 0) /* ignore disabled rules */
1699 FOR_RULELITERALS(l, pp, r)
1703 if (solv->decisionmap[-l] <= 0)
1708 if (solv->decisionmap[l] > 0)
1710 if (solv->decisionmap[l] == 0)
1716 /* prune to installed if not updating */
1717 if (dq.count > 1 && solv->installed && !solv->updatemap_all &&
1718 !(solv->job.elements[solv->ruletojob.elements[i - solv->jobrules]] & SOLVER_ORUPDATE))
1721 for (j = k = 0; j < dq.count; j++)
1723 Solvable *s = pool->solvables + dq.elements[j];
1724 if (s->repo == solv->installed)
1726 dq.elements[k++] = dq.elements[j];
1727 if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
1729 k = 0; /* package wants to be updated, do not prune */
1738 level = selectandinstall(solv, level, &dq, disablerules, i);
1741 if (level <= olevel)
1745 break; /* unsolvable */
1746 systemlevel = level + 1;
1747 if (i < solv->jobrules_end)
1749 solv->decisioncnt_update = solv->decisionq.count;
1750 solv->decisioncnt_keep = solv->decisionq.count;
1754 * installed packages
1756 if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1758 Repo *installed = solv->installed;
1761 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving installed packages\n");
1762 /* we use two passes if we need to update packages
1763 * to create a better user experience */
1764 for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1766 int passlevel = level;
1768 solv->decisioncnt_keep = solv->decisionq.count;
1769 /* start with installedpos, the position that gave us problems last time */
1770 for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1775 if (i == installed->end)
1776 i = installed->start;
1777 s = pool->solvables + i;
1778 if (s->repo != installed)
1781 if (solv->decisionmap[i] > 0)
1783 if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1784 continue; /* updates first */
1785 r = solv->rules + solv->updaterules + (i - installed->start);
1787 if (!rr->p || rr->d < 0) /* disabled -> look at feature rule */
1788 rr -= solv->installed->end - solv->installed->start;
1789 if (!rr->p) /* identical to update rule? */
1792 continue; /* orpaned package */
1794 /* XXX: noupdate check is probably no longer needed, as all jobs should
1795 * already be satisfied */
1796 /* Actually we currently still need it because of erase jobs */
1797 /* if noupdate is set we do not look at update candidates */
1799 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))
1801 if (solv->noobsoletes.size && solv->multiversionupdaters
1802 && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1804 /* special multiversion handling, make sure best version is chosen */
1806 while ((p = pool->whatprovidesdata[d++]) != 0)
1807 if (solv->decisionmap[p] >= 0)
1809 if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1810 prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
1813 policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1815 if (p != i && solv->decisionmap[p] == 0)
1817 rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1818 if (!rr->p) /* update rule == feature rule? */
1819 rr = rr - solv->featurerules + solv->updaterules;
1828 /* update to best package */
1829 FOR_RULELITERALS(p, pp, rr)
1831 if (solv->decisionmap[p] > 0)
1833 dq.count = 0; /* already fulfilled */
1836 if (!solv->decisionmap[p])
1841 if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1842 prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
1843 /* install best version */
1847 level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1854 if (level <= olevel)
1856 if (level == 1 || level < passlevel)
1857 break; /* trouble */
1859 n = installed->start; /* redo all */
1865 /* if still undecided keep package */
1866 if (solv->decisionmap[i] == 0)
1869 if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
1871 POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
1872 level = setpropagatelearn(solv, level, -i, disablerules, 0);
1876 POOL_DEBUG(SOLV_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
1877 level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1881 if (level <= olevel)
1883 if (level == 1 || level < passlevel)
1884 break; /* trouble */
1886 n = installed->start; /* redo all */
1889 continue; /* retry with learnt rule */
1893 if (n < installed->end)
1895 installedpos = i; /* retry problem solvable next time */
1896 break; /* ran into trouble */
1898 installedpos = installed->start; /* reset installedpos */
1901 break; /* unsolvable */
1902 systemlevel = level + 1;
1904 continue; /* had trouble, retry */
1907 if (level < systemlevel)
1908 systemlevel = level;
1913 solv->decisioncnt_resolve = solv->decisionq.count;
1914 POOL_DEBUG(SOLV_DEBUG_POLICY, "deciding unresolved rules\n");
1915 for (i = 1, n = 1; n < solv->nrules; i++, n++)
1917 if (i == solv->nrules)
1919 r = solv->rules + i;
1920 if (r->d < 0) /* ignore disabled rules */
1925 /* binary or unary rule */
1926 /* need two positive undecided literals */
1927 if (r->p < 0 || r->w2 <= 0)
1929 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1931 queue_push(&dq, r->p);
1932 queue_push(&dq, r->w2);
1937 * all negative literals are installed
1938 * no positive literal is installed
1939 * i.e. the rule is not fulfilled and we
1940 * just need to decide on the positive literals
1944 if (solv->decisionmap[-r->p] <= 0)
1949 if (solv->decisionmap[r->p] > 0)
1951 if (solv->decisionmap[r->p] == 0)
1952 queue_push(&dq, r->p);
1954 dp = pool->whatprovidesdata + r->d;
1955 while ((p = *dp++) != 0)
1959 if (solv->decisionmap[-p] <= 0)
1964 if (solv->decisionmap[p] > 0)
1966 if (solv->decisionmap[p] == 0)
1973 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
1975 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "unfulfilled ");
1976 solver_printruleclass(solv, SOLV_DEBUG_PROPAGATE, r);
1978 /* dq.count < 2 cannot happen as this means that
1979 * the rule is unit */
1980 assert(dq.count > 1);
1983 level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
1985 break; /* unsolvable */
1986 if (level < systemlevel || level == 1)
1987 break; /* trouble */
1988 /* something changed, so look at all rules again */
1992 if (n != solv->nrules) /* ran into trouble? */
1995 break; /* unsolvable */
1996 continue; /* start over */
1999 /* at this point we have a consistent system. now do the extras... */
2001 solv->decisioncnt_weak = solv->decisionq.count;
2006 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended packages\n");
2007 queue_empty(&dq); /* recommended packages */
2008 queue_empty(&dqs); /* supplemented packages */
2009 for (i = 1; i < pool->nsolvables; i++)
2011 if (solv->decisionmap[i] < 0)
2013 if (solv->decisionmap[i] > 0)
2015 /* installed, check for recommends */
2016 Id *recp, rec, pp, p;
2017 s = pool->solvables + i;
2018 if (!solv->addalreadyrecommended && s->repo == solv->installed)
2020 /* XXX need to special case AND ? */
2023 recp = s->repo->idarraydata + s->recommends;
2024 while ((rec = *recp++) != 0)
2027 FOR_PROVIDES(p, pp, rec)
2029 if (solv->decisionmap[p] > 0)
2034 else if (solv->decisionmap[p] == 0)
2036 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))))
2038 queue_pushunique(&dq, p);
2046 s = pool->solvables + i;
2047 if (!s->supplements)
2049 if (!pool_installable(pool, s))
2051 if (!solver_is_supplementing(solv, s))
2053 if (solv->dupmap_all && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
2055 queue_push(&dqs, i);
2059 /* filter out all packages obsoleted by installed packages */
2060 /* this is no longer needed if we have reverse obsoletes */
2061 if ((dqs.count || dq.count) && solv->installed)
2064 Id obs, *obsp, po, ppo;
2066 map_init(&obsmap, pool->nsolvables);
2067 for (p = solv->installed->start; p < solv->installed->end; p++)
2069 s = pool->solvables + p;
2070 if (s->repo != solv->installed || !s->obsoletes)
2072 if (solv->decisionmap[p] <= 0)
2074 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
2076 obsp = s->repo->idarraydata + s->obsoletes;
2077 /* foreach obsoletes */
2078 while ((obs = *obsp++) != 0)
2079 FOR_PROVIDES(po, ppo, obs)
2080 MAPSET(&obsmap, po);
2082 for (i = j = 0; i < dqs.count; i++)
2083 if (!MAPTST(&obsmap, dqs.elements[i]))
2084 dqs.elements[j++] = dqs.elements[i];
2086 for (i = j = 0; i < dq.count; i++)
2087 if (!MAPTST(&obsmap, dq.elements[i]))
2088 dq.elements[j++] = dq.elements[i];
2093 /* filter out all already supplemented packages if requested */
2094 if (!solv->addalreadyrecommended && dqs.count)
2096 /* turn off all new packages */
2097 for (i = 0; i < solv->decisionq.count; i++)
2099 p = solv->decisionq.elements[i];
2102 s = pool->solvables + p;
2103 if (s->repo && s->repo != solv->installed)
2104 solv->decisionmap[p] = -solv->decisionmap[p];
2106 /* filter out old supplements */
2107 for (i = j = 0; i < dqs.count; i++)
2109 p = dqs.elements[i];
2110 s = pool->solvables + p;
2111 if (!s->supplements)
2113 if (!solver_is_supplementing(solv, s))
2114 dqs.elements[j++] = p;
2117 /* undo turning off */
2118 for (i = 0; i < solv->decisionq.count; i++)
2120 p = solv->decisionq.elements[i];
2123 s = pool->solvables + p;
2124 if (s->repo && s->repo != solv->installed)
2125 solv->decisionmap[p] = -solv->decisionmap[p];
2129 /* multiversion doesn't mix well with supplements.
2130 * filter supplemented packages where we already decided
2131 * to install a different version (see bnc#501088) */
2132 if (dqs.count && solv->noobsoletes.size)
2134 for (i = j = 0; i < dqs.count; i++)
2136 p = dqs.elements[i];
2137 if (MAPTST(&solv->noobsoletes, p))
2140 s = pool->solvables + p;
2141 FOR_PROVIDES(p2, pp2, s->name)
2142 if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
2145 continue; /* ignore this package */
2147 dqs.elements[j++] = p;
2152 /* make dq contain both recommended and supplemented pkgs */
2155 for (i = 0; i < dqs.count; i++)
2156 queue_pushunique(&dq, dqs.elements[i]);
2162 int decisioncount = solv->decisionq.count;
2166 /* simple case, just one package. no need to choose to best version */
2169 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2171 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2172 level = setpropagatelearn(solv, level, p, 0, 0);
2175 continue; /* back to main loop */
2178 /* filter packages, this gives us the best versions */
2179 policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
2181 /* create map of result */
2182 map_init(&dqmap, pool->nsolvables);
2183 for (i = 0; i < dq.count; i++)
2184 MAPSET(&dqmap, dq.elements[i]);
2186 /* install all supplemented packages */
2187 for (i = 0; i < dqs.count; i++)
2189 p = dqs.elements[i];
2190 if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
2192 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2194 level = setpropagatelearn(solv, level, p, 0, 0);
2195 if (level <= olevel)
2198 if (i < dqs.count || solv->decisionq.count < decisioncount)
2206 /* install all recommended packages */
2207 /* more work as we want to created branches if multiple
2208 * choices are valid */
2209 for (i = 0; i < decisioncount; i++)
2212 p = solv->decisionq.elements[i];
2215 s = pool->solvables + p;
2216 if (!s->repo || (!solv->addalreadyrecommended && s->repo == solv->installed))
2220 recp = s->repo->idarraydata + s->recommends;
2221 while ((rec = *recp++) != 0)
2224 FOR_PROVIDES(p, pp, rec)
2226 if (solv->decisionmap[p] > 0)
2231 else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
2232 queue_pushunique(&dq, p);
2238 /* multiple candidates, open a branch */
2239 for (i = 1; i < dq.count; i++)
2240 queue_push(&solv->branches, dq.elements[i]);
2241 queue_push(&solv->branches, -level);
2244 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2246 level = setpropagatelearn(solv, level, p, 0, 0);
2247 if (level <= olevel || solv->decisionq.count < decisioncount)
2248 break; /* we had to revert some decisions */
2251 break; /* had a problem above, quit loop */
2256 continue; /* back to main loop so that all deps are checked */
2260 solv->decisioncnt_orphan = solv->decisionq.count;
2261 if (solv->dupmap_all && solv->installed)
2263 int installedone = 0;
2265 /* let's see if we can install some unsupported package */
2266 POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding orphaned packages\n");
2267 for (i = 0; i < solv->orphaned.count; i++)
2269 p = solv->orphaned.elements[i];
2270 if (solv->decisionmap[p])
2271 continue; /* already decided */
2272 if (solv->droporphanedmap_all)
2274 if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
2276 POOL_DEBUG(SOLV_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
2278 level = setpropagatelearn(solv, level, p, 0, 0);
2283 if (installedone || i < solv->orphaned.count)
2287 continue; /* back to main loop */
2289 for (i = 0; i < solv->orphaned.count; i++)
2291 p = solv->orphaned.elements[i];
2292 if (solv->decisionmap[p])
2293 continue; /* already decided */
2294 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2296 level = setpropagatelearn(solv, level, -p, 0, 0);
2300 if (i < solv->orphaned.count)
2304 continue; /* back to main loop */
2308 if (solv->installed && solv->cleandepsmap.size)
2310 if (cleandeps_check_mistakes(solv, level))
2312 level = 1; /* restart from scratch */
2317 if (solv->solution_callback)
2319 solv->solution_callback(solv, solv->solution_callback_data);
2320 if (solv->branches.count)
2322 int i = solv->branches.count - 1;
2323 int l = -solv->branches.elements[i];
2327 if (solv->branches.elements[i - 1] < 0)
2329 p = solv->branches.elements[i];
2330 POOL_DEBUG(SOLV_DEBUG_SOLVER, "branching with %s\n", pool_solvid2str(pool, p));
2332 for (j = i + 1; j < solv->branches.count; j++)
2333 queue_push(&dq, solv->branches.elements[j]);
2334 solv->branches.count = i;
2336 revert(solv, level);
2338 for (j = 0; j < dq.count; j++)
2339 queue_push(&solv->branches, dq.elements[j]);
2341 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2343 level = setpropagatelearn(solv, level, p, disablerules, why);
2348 /* all branches done, we're finally finished */
2352 /* auto-minimization step */
2353 if (solv->branches.count)
2355 int l = 0, lasti = -1, lastl = -1;
2359 for (i = solv->branches.count - 1; i >= 0; i--)
2361 p = solv->branches.elements[i];
2364 else if (p > 0 && solv->decisionmap[p] > l + 1)
2372 /* kill old solvable so that we do not loop */
2373 p = solv->branches.elements[lasti];
2374 solv->branches.elements[lasti] = 0;
2375 POOL_DEBUG(SOLV_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, pool_solvid2str(pool, p));
2376 minimizationsteps++;
2379 revert(solv, level);
2380 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2383 level = setpropagatelearn(solv, level, p, disablerules, why);
2386 continue; /* back to main loop */
2389 /* no minimization found, we're finally finished! */
2393 POOL_DEBUG(SOLV_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2395 POOL_DEBUG(SOLV_DEBUG_STATS, "done solving.\n\n");
2401 solv->decisioncnt_update = solv->decisionq.count;
2402 solv->decisioncnt_keep = solv->decisionq.count;
2403 solv->decisioncnt_resolve = solv->decisionq.count;
2404 solv->decisioncnt_weak = solv->decisionq.count;
2405 solv->decisioncnt_orphan = solv->decisionq.count;
2408 solver_printdecisionq(solv, SOLV_DEBUG_RESULT);
2413 /*-------------------------------------------------------------------
2415 * remove disabled conflicts
2417 * purpose: update the decisionmap after some rules were disabled.
2418 * this is used to calculate the suggested/recommended package list.
2419 * Also returns a "removed" list to undo the discisionmap changes.
2423 removedisabledconflicts(Solver *solv, Queue *removed)
2425 Pool *pool = solv->pool;
2430 Id *decisionmap = solv->decisionmap;
2432 queue_empty(removed);
2433 for (i = 0; i < solv->decisionq.count; i++)
2435 p = solv->decisionq.elements[i];
2437 continue; /* conflicts only, please */
2438 why = solv->decisionq_why.elements[i];
2441 /* no rule involved, must be a orphan package drop */
2444 /* we never do conflicts on free decisions, so there
2445 * must have been an unit rule */
2447 r = solv->rules + why;
2448 if (r->d < 0 && decisionmap[-p])
2450 /* rule is now disabled, remove from decisionmap */
2451 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2452 queue_push(removed, -p);
2453 queue_push(removed, decisionmap[-p]);
2454 decisionmap[-p] = 0;
2457 if (!removed->count)
2459 /* we removed some confliced packages. some of them might still
2460 * be in conflict, so search for unit rules and re-conflict */
2462 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2464 if (i == solv->nrules)
2467 r = solv->rules + i;
2473 if (r->p < 0 && !decisionmap[-r->p])
2479 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2481 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2486 if (r->p < 0 && decisionmap[-r->p] == 0)
2488 if (new || DECISIONMAP_FALSE(r->p))
2490 dp = pool->whatprovidesdata + r->d;
2491 while ((p = *dp++) != 0)
2493 if (new && p == new)
2495 if (p < 0 && decisionmap[-p] == 0)
2504 else if (!DECISIONMAP_FALSE(p))
2514 POOL_DEBUG(SOLV_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2515 decisionmap[-new] = -1;
2517 n = 0; /* redo all rules */
2523 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2526 for (i = 0; i < removed->count; i += 2)
2527 solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2531 /*-------------------------------------------------------------------
2533 * weaken solvable dependencies
2537 weaken_solvable_deps(Solver *solv, Id p)
2542 for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2546 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2547 continue; /* conflict */
2548 queue_push(&solv->weakruleq, i);
2553 /********************************************************************/
2558 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap)
2561 Id how, what, select;
2563 for (i = 0; i < job->count; i += 2)
2565 how = job->elements[i];
2566 if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
2568 what = job->elements[i + 1];
2569 select = how & SOLVER_SELECTMASK;
2570 if (!noobsmap->size)
2571 map_grow(noobsmap, pool->nsolvables);
2572 if (select == SOLVER_SOLVABLE_ALL)
2574 FOR_POOL_SOLVABLES(p)
2575 MAPSET(noobsmap, p);
2577 else if (select == SOLVER_SOLVABLE_REPO)
2580 Repo *repo = pool_id2repo(pool, what);
2582 FOR_REPO_SOLVABLES(repo, p, s)
2583 MAPSET(noobsmap, p);
2585 FOR_JOB_SELECT(p, pp, select, what)
2586 MAPSET(noobsmap, p);
2591 * add a rule created by a job, record job number and weak flag
2594 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
2596 solver_addrule(solv, p, d);
2597 queue_push(&solv->ruletojob, job);
2599 queue_push(&solv->weakruleq, solv->nrules - 1);
2603 add_update_target(Solver *solv, Id p, Id how)
2605 Pool *pool = solv->pool;
2606 Solvable *s = pool->solvables + p;
2607 Repo *installed = solv->installed;
2609 if (!solv->update_targets)
2611 solv->update_targets = solv_calloc(1, sizeof(Queue));
2612 queue_init(solv->update_targets);
2614 if (s->repo == installed)
2616 queue_push2(solv->update_targets, p, p);
2619 FOR_PROVIDES(pi, pip, s->name)
2621 Solvable *si = pool->solvables + pi;
2622 if (si->repo != installed || si->name != s->name)
2624 if (how & SOLVER_FORCEBEST)
2626 if (!solv->bestupdatemap.size)
2627 map_grow(&solv->bestupdatemap, installed->end - installed->start);
2628 MAPSET(&solv->bestupdatemap, pi - installed->start);
2630 queue_push2(solv->update_targets, pi, p);
2631 /* check if it's ok to keep the installed package */
2632 if (s->evr == si->evr && solvable_identical(s, si))
2633 queue_push2(solv->update_targets, pi, pi);
2637 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2638 while ((obs = *obsp++) != 0)
2640 FOR_PROVIDES(pi, pip, obs)
2642 Solvable *si = pool->solvables + pi;
2643 if (si->repo != installed)
2645 if (si->name == s->name)
2646 continue; /* already handled above */
2647 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
2649 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
2651 if (how & SOLVER_FORCEBEST)
2653 if (!solv->bestupdatemap.size)
2654 map_grow(&solv->bestupdatemap, installed->end - installed->start);
2655 MAPSET(&solv->bestupdatemap, pi - installed->start);
2657 queue_push2(solv->update_targets, pi, p);
2664 transform_update_targets_sortfn(const void *ap, const void *bp, void *dp)
2674 transform_update_targets(Solver *solv)
2676 Repo *installed = solv->installed;
2677 Queue *update_targets = solv->update_targets;
2679 Id p, q, lastp, lastq;
2681 if (!update_targets->count)
2683 queue_free(update_targets);
2684 solv->update_targets = solv_free(update_targets);
2687 if (update_targets->count > 2)
2688 solv_sort(update_targets->elements, update_targets->count >> 1, 2 * sizeof(Id), transform_update_targets_sortfn, solv);
2689 queue_insertn(update_targets, 0, installed->end - installed->start);
2691 for (i = j = installed->end - installed->start; i < update_targets->count; i += 2)
2693 if ((p = update_targets->elements[i]) != lastp)
2695 if (!solv->updatemap.size)
2696 map_grow(&solv->updatemap, installed->end - installed->start);
2697 MAPSET(&solv->updatemap, p - installed->start);
2698 update_targets->elements[j++] = 0; /* finish old set */
2699 update_targets->elements[p - installed->start] = j; /* start new set */
2703 if ((q = update_targets->elements[i + 1]) != lastq)
2705 update_targets->elements[j++] = q;
2709 queue_truncate(update_targets, j);
2710 queue_push(update_targets, 0); /* finish last set */
2721 solver_solve(Solver *solv, Queue *job)
2723 Pool *pool = solv->pool;
2724 Repo *installed = solv->installed;
2727 Map addedmap; /* '1' == have rpm-rules for solvable */
2728 Map installcandidatemap;
2729 Id how, what, select, name, weak, p, pp, d;
2733 int now, solve_start;
2735 int hasbestinstalljob = 0;
2737 solve_start = solv_timems(0);
2739 /* log solver options */
2740 POOL_DEBUG(SOLV_DEBUG_STATS, "solver started\n");
2741 POOL_DEBUG(SOLV_DEBUG_STATS, "dosplitprovides=%d, noupdateprovide=%d, noinfarchcheck=%d\n", solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
2742 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);
2743 POOL_DEBUG(SOLV_DEBUG_STATS, "promoteepoch=%d, forbidselfconflicts=%d\n", pool->promoteepoch, pool->forbidselfconflicts);
2744 POOL_DEBUG(SOLV_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2745 POOL_DEBUG(SOLV_DEBUG_STATS, "dontinstallrecommended=%d, addalreadyrecommended=%d\n", solv->dontinstallrecommended, solv->addalreadyrecommended);
2747 /* create whatprovides if not already there */
2748 if (!pool->whatprovides)
2749 pool_createwhatprovides(pool);
2751 /* create obsolete index */
2752 policy_create_obsolete_index(solv);
2755 queue_free(&solv->job);
2756 queue_init_clone(&solv->job, job);
2758 if (solv->update_targets)
2760 queue_free(solv->update_targets);
2761 solv->update_targets = solv_free(solv->update_targets);
2764 * create basic rule set of all involved packages
2765 * use addedmap bitmap to make sure we don't create rules twice
2768 /* create noobsolete map if needed */
2769 solver_calculate_noobsmap(pool, job, &solv->noobsoletes);
2771 map_init(&addedmap, pool->nsolvables);
2772 MAPSET(&addedmap, SYSTEMSOLVABLE);
2774 map_init(&installcandidatemap, pool->nsolvables);
2777 now = solv_timems(0);
2779 * create rules for all package that could be involved with the solving
2780 * so called: rpm rules
2785 /* check for update/verify jobs as they need to be known early */
2786 for (i = 0; i < job->count; i += 2)
2788 how = job->elements[i];
2789 what = job->elements[i + 1];
2790 select = how & SOLVER_SELECTMASK;
2791 switch (how & SOLVER_JOBMASK)
2794 if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
2795 solv->fixmap_all = 1;
2796 FOR_JOB_SELECT(p, pp, select, what)
2798 s = pool->solvables + p;
2799 if (s->repo != installed)
2801 if (!solv->fixmap.size)
2802 map_grow(&solv->fixmap, installed->end - installed->start);
2803 MAPSET(&solv->fixmap, p - installed->start);
2807 if (select == SOLVER_SOLVABLE_ALL)
2809 solv->updatemap_all = 1;
2810 if (how & SOLVER_FORCEBEST)
2811 solv->bestupdatemap_all = 1;
2813 else if (select == SOLVER_SOLVABLE_REPO)
2815 Repo *repo = pool_id2repo(pool, what);
2818 if (repo == installed && !(how & SOLVER_TARGETED))
2820 solv->updatemap_all = 1;
2821 if (how & SOLVER_FORCEBEST)
2822 solv->bestupdatemap_all = 1;
2825 if (solv->noautotarget && !(how & SOLVER_TARGETED))
2827 /* targeted update */
2828 FOR_REPO_SOLVABLES(repo, p, s)
2829 add_update_target(solv, p, how);
2833 if (!(how & SOLVER_TARGETED))
2836 FOR_JOB_SELECT(p, pp, select, what)
2838 s = pool->solvables + p;
2839 if (s->repo != installed)
2841 if (!solv->updatemap.size)
2842 map_grow(&solv->updatemap, installed->end - installed->start);
2843 MAPSET(&solv->updatemap, p - installed->start);
2844 if (how & SOLVER_FORCEBEST)
2846 if (!solv->bestupdatemap.size)
2847 map_grow(&solv->bestupdatemap, installed->end - installed->start);
2848 MAPSET(&solv->bestupdatemap, p - installed->start);
2852 if (!targeted || solv->noautotarget)
2855 FOR_JOB_SELECT(p, pp, select, what)
2856 add_update_target(solv, p, how);
2864 if (solv->update_targets)
2865 transform_update_targets(solv);
2867 oldnrules = solv->nrules;
2868 FOR_REPO_SOLVABLES(installed, p, s)
2869 solver_addrpmrulesforsolvable(solv, s, &addedmap);
2870 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
2871 oldnrules = solv->nrules;
2872 FOR_REPO_SOLVABLES(installed, p, s)
2873 solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
2874 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
2878 * create rules for all packages involved in the job
2879 * (to be installed or removed)
2882 oldnrules = solv->nrules;
2883 for (i = 0; i < job->count; i += 2)
2885 how = job->elements[i];
2886 what = job->elements[i + 1];
2887 select = how & SOLVER_SELECTMASK;
2889 switch (how & SOLVER_JOBMASK)
2891 case SOLVER_INSTALL:
2892 FOR_JOB_SELECT(p, pp, select, what)
2894 MAPSET(&installcandidatemap, p);
2895 solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
2898 case SOLVER_DISTUPGRADE:
2899 if (select == SOLVER_SOLVABLE_ALL)
2901 solv->dupmap_all = 1;
2902 solv->updatemap_all = 1;
2903 if (how & SOLVER_FORCEBEST)
2904 solv->bestupdatemap_all = 1;
2906 if (!solv->dupmap_all)
2913 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
2917 * add rules for suggests, enhances
2919 oldnrules = solv->nrules;
2920 solver_addrpmrulesforweak(solv, &addedmap);
2921 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
2924 * first pass done, we now have all the rpm rules we need.
2925 * unify existing rules before going over all job rules and
2927 * at this point the system is always solvable,
2928 * as an empty system (remove all packages) is a valid solution
2931 IF_POOLDEBUG (SOLV_DEBUG_STATS)
2933 int possible = 0, installable = 0;
2934 for (i = 1; i < pool->nsolvables; i++)
2936 if (pool_installable(pool, pool->solvables + i))
2938 if (MAPTST(&addedmap, i))
2941 POOL_DEBUG(SOLV_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
2944 solver_unifyrules(solv); /* remove duplicate rpm rules */
2945 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
2947 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
2948 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule creation took %d ms\n", solv_timems(now));
2950 /* create dup maps if needed. We need the maps early to create our
2953 solver_createdupmaps(solv);
2956 * create feature rules
2958 * foreach installed:
2959 * create assertion (keep installed, if no update available)
2961 * create update rule (A|update1(A)|update2(A)|...)
2963 * those are used later on to keep a version of the installed packages in
2967 solv->featurerules = solv->nrules; /* mark start of feature rules */
2970 /* foreach possibly installed solvable */
2971 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2973 if (s->repo != installed)
2975 solver_addrule(solv, 0, 0); /* create dummy rule */
2978 solver_addupdaterule(solv, s, 1); /* allow s to be updated */
2980 /* make sure we accounted for all rules */
2981 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
2983 solv->featurerules_end = solv->nrules;
2986 * Add update rules for installed solvables
2988 * almost identical to feature rules
2989 * except that downgrades/archchanges/vendorchanges are not allowed
2992 solv->updaterules = solv->nrules;
2995 { /* foreach installed solvables */
2996 /* we create all update rules, but disable some later on depending on the job */
2997 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3001 if (s->repo != installed)
3003 solver_addrule(solv, 0, 0); /* create dummy rule */
3006 solver_addupdaterule(solv, s, 0); /* allowall = 0: downgrades not allowed */
3008 * check for and remove duplicate
3010 r = solv->rules + solv->nrules - 1; /* r: update rule */
3011 sr = r - (installed->end - installed->start); /* sr: feature rule */
3012 /* it's orphaned if there is no feature rule or the feature rule
3013 * consists just of the installed package */
3014 if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
3015 queue_push(&solv->orphaned, i);
3018 assert(solv->dupmap_all && !sr->p);
3021 if (!solver_rulecmp(solv, r, sr))
3022 memset(sr, 0, sizeof(*sr)); /* delete unneeded feature rule */
3024 solver_disablerule(solv, sr); /* disable feature rule */
3026 /* consistency check: we added a rule for _every_ installed solvable */
3027 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
3029 solv->updaterules_end = solv->nrules;
3033 * now add all job rules
3036 solv->jobrules = solv->nrules;
3037 if (solv->cleandeps_updatepkgs)
3039 queue_free(solv->cleandeps_updatepkgs);
3040 solv->cleandeps_updatepkgs = solv_free(solv->cleandeps_updatepkgs);
3042 for (i = 0; i < job->count; i += 2)
3044 oldnrules = solv->nrules;
3046 how = job->elements[i];
3047 what = job->elements[i + 1];
3048 weak = how & SOLVER_WEAK;
3049 select = how & SOLVER_SELECTMASK;
3050 switch (how & SOLVER_JOBMASK)
3052 case SOLVER_INSTALL:
3053 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3054 if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3055 map_grow(&solv->cleandepsmap, installed->end - installed->start);
3056 if (select == SOLVER_SOLVABLE)
3064 FOR_JOB_SELECT(p, pp, select, what)
3068 /* no candidate found, make this an impossible rule */
3069 queue_push(&q, -SYSTEMSOLVABLE);
3071 p = queue_shift(&q); /* get first candidate */
3072 d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q); /* internalize */
3074 solver_addjobrule(solv, p, d, i, weak);
3075 if (how & SOLVER_FORCEBEST)
3076 hasbestinstalljob = 1;
3079 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
3080 if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3081 map_grow(&solv->cleandepsmap, installed->end - installed->start);
3082 /* specific solvable: by id or by nevra */
3083 name = (select == SOLVER_SOLVABLE || (select == SOLVER_SOLVABLE_NAME && ISRELDEP(what))) ? 0 : -1;
3084 if (select == SOLVER_SOLVABLE_ALL) /* hmmm ;) */
3086 FOR_POOL_SOLVABLES(p)
3087 solver_addjobrule(solv, -p, 0, i, weak);
3089 else if (select == SOLVER_SOLVABLE_REPO)
3091 Repo *repo = pool_id2repo(pool, what);
3093 FOR_REPO_SOLVABLES(repo, p, s)
3094 solver_addjobrule(solv, -p, 0, i, weak);
3096 FOR_JOB_SELECT(p, pp, select, what)
3098 s = pool->solvables + p;
3099 if (installed && s->repo == installed)
3100 name = !name ? s->name : -1;
3101 solver_addjobrule(solv, -p, 0, i, weak);
3103 /* special case for "erase a specific solvable": we also
3104 * erase all other solvables with that name, so that they
3105 * don't get picked up as replacement.
3106 * name is > 0 if exactly one installed solvable matched.
3108 /* XXX: look also at packages that obsolete this package? */
3113 FOR_PROVIDES(p, pp, name)
3115 s = pool->solvables + p;
3116 if (s->name != name)
3118 /* keep other versions installed */
3119 if (s->repo == installed)
3121 /* keep installcandidates of other jobs */
3122 if (MAPTST(&installcandidatemap, p))
3124 /* don't add the same rule twice */
3125 for (j = oldnrules; j < k; j++)
3126 if (solv->rules[j].p == -p)
3129 solver_addjobrule(solv, -p, 0, i, weak); /* remove by id */
3135 if ((how & SOLVER_CLEANDEPS) != 0 && installed)
3137 if (how == SOLVER_SOLVABLE_ALL || (how == SOLVER_SOLVABLE_REPO && what == installed->repoid))
3139 FOR_REPO_SOLVABLES(installed, p, s)
3141 if (!solv->cleandeps_updatepkgs)
3143 solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
3144 queue_init(solv->cleandeps_updatepkgs);
3146 queue_pushunique(solv->cleandeps_updatepkgs, p);
3147 if (!solv->cleandepsmap.size)
3148 map_grow(&solv->cleandepsmap, installed->end - installed->start);
3151 FOR_JOB_SELECT(p, pp, select, what)
3153 s = pool->solvables + p;
3154 if (s->repo != installed)
3156 if (!solv->cleandeps_updatepkgs)
3158 solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
3159 queue_init(solv->cleandeps_updatepkgs);
3161 queue_pushunique(solv->cleandeps_updatepkgs, p);
3162 if (!solv->cleandepsmap.size)
3163 map_grow(&solv->cleandepsmap, installed->end - installed->start);
3166 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3169 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3171 case SOLVER_WEAKENDEPS:
3172 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3173 if (select != SOLVER_SOLVABLE)
3175 s = pool->solvables + what;
3176 weaken_solvable_deps(solv, what);
3178 case SOLVER_NOOBSOLETES:
3179 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3182 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3183 if (select == SOLVER_SOLVABLE_ALL)
3185 FOR_POOL_SOLVABLES(p)
3186 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3188 else if (select == SOLVER_SOLVABLE_REPO)
3190 Repo *repo = pool_id2repo(pool, what);
3192 FOR_REPO_SOLVABLES(repo, p, s)
3193 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3195 FOR_JOB_SELECT(p, pp, select, what)
3196 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3198 case SOLVER_DISTUPGRADE:
3199 POOL_DEBUG(SOLV_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
3201 case SOLVER_DROP_ORPHANED:
3202 POOL_DEBUG(SOLV_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
3203 if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3204 solv->droporphanedmap_all = 1;
3205 FOR_JOB_SELECT(p, pp, select, what)
3207 s = pool->solvables + p;
3208 if (!installed || s->repo != installed)
3210 if (!solv->droporphanedmap.size)
3211 map_grow(&solv->droporphanedmap, installed->end - installed->start);
3212 MAPSET(&solv->droporphanedmap, p - installed->start);
3215 case SOLVER_USERINSTALLED:
3216 POOL_DEBUG(SOLV_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
3219 POOL_DEBUG(SOLV_DEBUG_JOB, "job: unknown job\n");
3227 IF_POOLDEBUG (SOLV_DEBUG_JOB)
3230 if (solv->nrules == oldnrules)
3231 POOL_DEBUG(SOLV_DEBUG_JOB, " - no rule created\n");
3232 for (j = oldnrules; j < solv->nrules; j++)
3234 POOL_DEBUG(SOLV_DEBUG_JOB, " - job ");
3235 solver_printrule(solv, SOLV_DEBUG_JOB, solv->rules + j);
3239 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
3240 solv->jobrules_end = solv->nrules;
3242 /* now create infarch and dup rules */
3243 if (!solv->noinfarchcheck)
3245 solver_addinfarchrules(solv, &addedmap);
3246 if (pool->obsoleteusescolors)
3248 /* currently doesn't work well with infarch rules, so make
3250 for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
3251 queue_push(&solv->weakruleq, i);
3255 solv->infarchrules = solv->infarchrules_end = solv->nrules;
3258 solver_addduprules(solv, &addedmap);
3260 solv->duprules = solv->duprules_end = solv->nrules;
3262 if (solv->bestupdatemap_all || solv->bestupdatemap.size || hasbestinstalljob)
3263 solver_addbestrules(solv, hasbestinstalljob);
3265 solv->bestrules = solv->bestrules_end = solv->nrules;
3268 solver_freedupmaps(solv); /* no longer needed */
3271 solver_addchoicerules(solv);
3273 solv->choicerules = solv->choicerules_end = solv->nrules;
3277 for (i = solv->featurerules; i < solv->nrules; i++)
3278 solver_printruleclass(solv, SOLV_DEBUG_RESULT, solv->rules + i);
3280 /* all rules created
3281 * --------------------------------------------------------------
3282 * prepare for solving
3285 /* free unneeded memory */
3286 map_free(&addedmap);
3287 map_free(&installcandidatemap);
3290 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);
3291 POOL_DEBUG(SOLV_DEBUG_STATS, "overall rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3293 /* create weak map */
3294 map_init(&solv->weakrulemap, solv->nrules);
3295 for (i = 0; i < solv->weakruleq.count; i++)
3297 p = solv->weakruleq.elements[i];
3298 MAPSET(&solv->weakrulemap, p);
3301 /* all new rules are learnt after this point */
3302 solv->learntrules = solv->nrules;
3304 /* create watches chains */
3307 /* create assertion index. it is only used to speed up
3308 * makeruledecsions() a bit */
3309 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
3310 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
3311 queue_push(&solv->ruleassertions, i);
3313 /* disable update rules that conflict with our job */
3314 solver_disablepolicyrules(solv);
3316 /* make initial decisions based on assertion rules */
3317 makeruledecisions(solv);
3318 POOL_DEBUG(SOLV_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
3321 if (solv->cleandeps_mistakes)
3323 queue_free(solv->cleandeps_mistakes);
3324 solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
3328 * ********************************************
3330 * ********************************************
3333 now = solv_timems(0);
3334 solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
3335 POOL_DEBUG(SOLV_DEBUG_STATS, "solver took %d ms\n", solv_timems(now));
3338 * prepare solution queue if there were problems
3340 solver_prepare_solutions(solv);
3342 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);
3343 POOL_DEBUG(SOLV_DEBUG_STATS, "solver_solve took %d ms\n", solv_timems(solve_start));
3345 /* return number of problems */
3346 return solv->problems.count ? solv->problems.count / 2 : 0;
3350 solver_create_transaction(Solver *solv)
3352 return transaction_create_decisionq(solv->pool, &solv->decisionq, &solv->noobsoletes);
3355 void solver_get_orphaned(Solver *solv, Queue *orphanedq)
3357 queue_free(orphanedq);
3358 queue_init_clone(orphanedq, &solv->orphaned);
3361 void solver_get_recommendations(Solver *solv, Queue *recommendationsq, Queue *suggestionsq, int noselected)
3363 Pool *pool = solv->pool;
3364 Queue redoq, disabledq;
3370 if (!recommendationsq && !suggestionsq)
3373 map_init(&obsmap, pool->nsolvables);
3374 if (solv->installed)
3376 Id obs, *obsp, p, po, ppo;
3377 for (p = solv->installed->start; p < solv->installed->end; p++)
3379 s = pool->solvables + p;
3380 if (s->repo != solv->installed || !s->obsoletes)
3382 if (solv->decisionmap[p] <= 0)
3384 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
3386 obsp = s->repo->idarraydata + s->obsoletes;
3387 /* foreach obsoletes */
3388 while ((obs = *obsp++) != 0)
3389 FOR_PROVIDES(po, ppo, obs)
3390 MAPSET(&obsmap, po);
3395 queue_init(&disabledq);
3397 /* disable all erase jobs (including weak "keep uninstalled" rules) */
3398 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
3400 if (r->d < 0) /* disabled ? */
3402 if (r->p >= 0) /* install job? */
3404 queue_push(&disabledq, i);
3405 solver_disablerule(solv, r);
3411 enabledisablelearntrules(solv);
3412 removedisabledconflicts(solv, &redoq);
3416 * find recommended packages
3418 if (recommendationsq)
3420 Id rec, *recp, p, pp;
3422 queue_empty(recommendationsq);
3423 /* create map of all recommened packages */
3424 solv->recommends_index = -1;
3425 MAPZERO(&solv->recommendsmap);
3427 /* put all packages the solver already chose in the map */
3428 if (solv->decisioncnt_weak)
3430 for (i = solv->decisioncnt_weak; i < solv->decisioncnt_orphan; i++)
3433 why = solv->decisionq_why.elements[i];
3435 continue; /* forced by unit rule */
3436 p = solv->decisionq.elements[i];
3439 MAPSET(&solv->recommendsmap, p);
3443 for (i = 0; i < solv->decisionq.count; i++)
3445 p = solv->decisionq.elements[i];
3448 s = pool->solvables + p;
3451 recp = s->repo->idarraydata + s->recommends;
3452 while ((rec = *recp++) != 0)
3454 FOR_PROVIDES(p, pp, rec)
3455 if (solv->decisionmap[p] > 0)
3461 FOR_PROVIDES(p, pp, rec)
3462 if (solv->decisionmap[p] > 0)
3463 MAPSET(&solv->recommendsmap, p);
3465 continue; /* p != 0: already fulfilled */
3467 FOR_PROVIDES(p, pp, rec)
3468 MAPSET(&solv->recommendsmap, p);
3472 for (i = 1; i < pool->nsolvables; i++)
3474 if (solv->decisionmap[i] < 0)
3476 if (solv->decisionmap[i] > 0 && noselected)
3478 if (MAPTST(&obsmap, i))
3480 s = pool->solvables + i;
3481 if (!MAPTST(&solv->recommendsmap, i))
3483 if (!s->supplements)
3485 if (!pool_installable(pool, s))
3487 if (!solver_is_supplementing(solv, s))
3490 queue_push(recommendationsq, i);
3492 /* we use MODE_SUGGEST here so that repo prio is ignored */
3493 policy_filter_unwanted(solv, recommendationsq, POLICY_MODE_SUGGEST);
3497 * find suggested packages
3502 Id sug, *sugp, p, pp;
3504 queue_empty(suggestionsq);
3505 /* create map of all suggests that are still open */
3506 solv->recommends_index = -1;
3507 MAPZERO(&solv->suggestsmap);
3508 for (i = 0; i < solv->decisionq.count; i++)
3510 p = solv->decisionq.elements[i];
3513 s = pool->solvables + p;
3516 sugp = s->repo->idarraydata + s->suggests;
3517 while ((sug = *sugp++) != 0)
3519 FOR_PROVIDES(p, pp, sug)
3520 if (solv->decisionmap[p] > 0)
3526 FOR_PROVIDES(p, pp, sug)
3527 if (solv->decisionmap[p] > 0)
3528 MAPSET(&solv->suggestsmap, p);
3530 continue; /* already fulfilled */
3532 FOR_PROVIDES(p, pp, sug)
3533 MAPSET(&solv->suggestsmap, p);
3537 for (i = 1; i < pool->nsolvables; i++)
3539 if (solv->decisionmap[i] < 0)
3541 if (solv->decisionmap[i] > 0 && noselected)
3543 if (MAPTST(&obsmap, i))
3545 s = pool->solvables + i;
3546 if (!MAPTST(&solv->suggestsmap, i))
3550 if (!pool_installable(pool, s))
3552 if (!solver_is_enhancing(solv, s))
3555 queue_push(suggestionsq, i);
3557 policy_filter_unwanted(solv, suggestionsq, POLICY_MODE_SUGGEST);
3560 /* undo removedisabledconflicts */
3562 undo_removedisabledconflicts(solv, &redoq);
3565 /* undo job rule disabling */
3566 for (i = 0; i < disabledq.count; i++)
3567 solver_enablerule(solv, solv->rules + disabledq.elements[i]);
3568 queue_free(&disabledq);
3573 /***********************************************************************/
3574 /* disk usage computations */
3576 /*-------------------------------------------------------------------
3578 * calculate DU changes
3582 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
3586 solver_create_state_maps(solv, &installedmap, 0);
3587 pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
3588 map_free(&installedmap);
3592 /*-------------------------------------------------------------------
3594 * calculate changes in install size
3598 solver_calc_installsizechange(Solver *solv)
3603 solver_create_state_maps(solv, &installedmap, 0);
3604 change = pool_calc_installsizechange(solv->pool, &installedmap);
3605 map_free(&installedmap);
3610 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
3612 pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
3616 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
3619 pool_create_state_maps(solv->pool, &solv->decisionq, &installedmap, 0);
3620 pool_trivial_installable_noobsoletesmap(solv->pool, &installedmap, pkgs, res, solv->noobsoletes.size ? &solv->noobsoletes : 0);
3621 map_free(&installedmap);
3624 /*-------------------------------------------------------------------
3626 * decision introspection
3630 solver_get_decisionlevel(Solver *solv, Id p)
3632 return solv->decisionmap[p];
3636 solver_get_decisionqueue(Solver *solv, Queue *decisionq)
3638 queue_free(decisionq);
3639 queue_init_clone(decisionq, &solv->decisionq);
3643 solver_get_lastdecisionblocklevel(Solver *solv)
3646 if (solv->decisionq.count == 0)
3648 p = solv->decisionq.elements[solv->decisionq.count - 1];
3651 return solv->decisionmap[p] < 0 ? -solv->decisionmap[p] : solv->decisionmap[p];
3655 solver_get_decisionblock(Solver *solv, int level, Queue *decisionq)
3660 queue_empty(decisionq);
3661 for (i = 0; i < solv->decisionq.count; i++)
3663 p = solv->decisionq.elements[i];
3666 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3669 if (i == solv->decisionq.count)
3671 for (i = 0; i < solv->decisionq.count; i++)
3673 p = solv->decisionq.elements[i];
3676 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3677 queue_push(decisionq, p);
3684 solver_describe_decision(Solver *solv, Id p, Id *infop)
3691 if (!solv->decisionmap[p])
3692 return SOLVER_REASON_UNRELATED;
3693 pp = solv->decisionmap[p] < 0 ? -p : p;
3694 for (i = 0; i < solv->decisionq.count; i++)
3695 if (solv->decisionq.elements[i] == pp)
3697 if (i == solv->decisionq.count) /* just in case... */
3698 return SOLVER_REASON_UNRELATED;
3699 why = solv->decisionq_why.elements[i];
3704 return SOLVER_REASON_UNIT_RULE;
3707 if (i < solv->decisioncnt_update)
3712 *infop = SYSTEMSOLVABLE;
3713 return SOLVER_REASON_KEEP_INSTALLED;
3717 return SOLVER_REASON_RESOLVE_JOB;
3719 if (i < solv->decisioncnt_keep)
3721 if (why == 0 && pp < 0)
3722 return SOLVER_REASON_CLEANDEPS_ERASE;
3725 if (why >= solv->updaterules && why < solv->updaterules_end)
3726 *infop = why - solv->updaterules;
3727 else if (why >= solv->featurerules && why < solv->featurerules_end)
3728 *infop = why - solv->featurerules;
3730 return SOLVER_REASON_UPDATE_INSTALLED;
3732 if (i < solv->decisioncnt_resolve)
3734 if (why == 0 && pp < 0)
3735 return SOLVER_REASON_CLEANDEPS_ERASE;
3738 if (why >= solv->updaterules && why < solv->updaterules_end)
3739 *infop = why - solv->updaterules;
3740 else if (why >= solv->featurerules && why < solv->featurerules_end)
3741 *infop = why - solv->featurerules;
3743 return SOLVER_REASON_KEEP_INSTALLED;
3745 if (i < solv->decisioncnt_weak)
3749 return SOLVER_REASON_RESOLVE;
3751 if (solv->decisionq.count < solv->decisioncnt_orphan)
3752 return SOLVER_REASON_WEAKDEP;
3753 return SOLVER_REASON_RESOLVE_ORPHAN;
3758 solver_describe_weakdep_decision(Solver *solv, Id p, Queue *whyq)
3760 Pool *pool = solv->pool;
3762 int level = solv->decisionmap[p];
3769 for (decisionno = 0; decisionno < solv->decisionq.count; decisionno++)
3770 if (solv->decisionq.elements[decisionno] == p)
3772 if (decisionno == solv->decisionq.count)
3774 if (decisionno < solv->decisioncnt_weak || decisionno >= solv->decisioncnt_orphan)
3777 /* 1) list all packages that recommend us */
3778 for (i = 1; i < pool->nsolvables; i++)
3780 Id *recp, rec, pp2, p2;
3781 if (solv->decisionmap[i] < 0 || solv->decisionmap[i] >= level)
3783 s = pool->solvables + i;
3786 if (!solv->addalreadyrecommended && s->repo == solv->installed)
3788 recp = s->repo->idarraydata + s->recommends;
3789 while ((rec = *recp++) != 0)
3792 FOR_PROVIDES(p2, pp2, rec)
3798 /* if p2 is already installed, this recommends is ignored */
3799 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3805 queue_push(whyq, SOLVER_REASON_RECOMMENDED);
3806 queue_push2(whyq, p2, rec);
3810 /* 2) list all supplements */
3811 s = pool->solvables + p;
3812 if (s->supplements && level > 0)
3814 Id *supp, sup, pp2, p2;
3815 /* this is a hack. to use solver_dep_fulfilled we temporarily clear
3816 * everything above our level in the decisionmap */
3817 for (i = decisionno; i < solv->decisionq.count; i++ )
3819 p2 = solv->decisionq.elements[i];
3821 solv->decisionmap[p2] = -solv->decisionmap[p2];
3823 supp = s->repo->idarraydata + s->supplements;
3824 while ((sup = *supp++) != 0)
3825 if (solver_dep_fulfilled(solv, sup))
3828 /* let's see if this is an easy supp */
3829 FOR_PROVIDES(p2, pp2, sup)
3831 if (!solv->addalreadyrecommended && solv->installed)
3833 if (pool->solvables[p2].repo == solv->installed)
3836 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3838 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3839 queue_push2(whyq, p2, sup);
3845 /* hard case, just note dependency with no package */
3846 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3847 queue_push2(whyq, 0, sup);
3850 for (i = decisionno; i < solv->decisionq.count; i++)
3852 p2 = solv->decisionq.elements[i];
3854 solv->decisionmap[p2] = -solv->decisionmap[p2];
3860 pool_job2solvables(Pool *pool, Queue *pkgs, Id how, Id what)
3863 how &= SOLVER_SELECTMASK;
3865 if (how == SOLVER_SOLVABLE_ALL)
3867 FOR_POOL_SOLVABLES(p)
3868 queue_push(pkgs, p);
3870 else if (how == SOLVER_SOLVABLE_REPO)
3872 Repo *repo = pool_id2repo(pool, what);
3875 FOR_REPO_SOLVABLES(repo, p, s)
3876 queue_push(pkgs, p);
3880 FOR_JOB_SELECT(p, pp, how, what)
3881 queue_push(pkgs, p);
3886 pool_isemptyupdatejob(Pool *pool, Id how, Id what)
3889 Id select = how & SOLVER_SELECTMASK;
3890 if ((how & SOLVER_JOBMASK) != SOLVER_UPDATE)
3892 if (select == SOLVER_SOLVABLE_ALL || select == SOLVER_SOLVABLE_REPO)
3894 if (!pool->installed)
3896 FOR_JOB_SELECT(p, pp, select, what)
3897 if (pool->solvables[p].repo == pool->installed)
3900 FOR_JOB_SELECT(p, pp, select, what)
3902 Solvable *s = pool->solvables + p;
3903 FOR_PROVIDES(pi, pip, s->name)
3905 Solvable *si = pool->solvables + pi;
3906 if (si->repo != pool->installed || si->name != s->name)
3912 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
3913 while ((obs = *obsp++) != 0)
3915 FOR_PROVIDES(pi, pip, obs)
3917 Solvable *si = pool->solvables + pi;
3918 if (si->repo != pool->installed)
3920 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
3922 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))