2 * Copyright (c) 2007-2008, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * SAT based dependency solver
21 #include "solver_private.h"
26 #include "solverdebug.h"
28 #define RULES_BLOCK 63
30 /********************************************************************
32 * dependency check helpers
36 /*-------------------------------------------------------------------
37 * handle split provides
39 * a splitprovides dep looks like
40 * namespace:splitprovides(pkg REL_WITH path)
41 * and is only true if pkg is installed and contains the specified path.
42 * we also make sure that pkg is selected for an update, otherwise the
43 * update would always be forced onto the user.
46 solver_splitprovides(Solver *solv, Id dep)
48 Pool *pool = solv->pool;
53 if (!solv->dosplitprovides || !solv->installed || (!solv->updatemap_all && !solv->updatemap.size))
57 rd = GETRELDEP(pool, dep);
58 if (rd->flags != REL_WITH)
60 FOR_PROVIDES(p, pp, dep)
62 /* here we have packages that provide the correct name and contain the path,
63 * now do extra filtering */
64 s = pool->solvables + p;
65 if (s->repo == solv->installed && s->name == rd->name &&
66 (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, p - solv->installed->start))))
73 /*-------------------------------------------------------------------
74 * solver_dep_installed
78 solver_dep_installed(Solver *solv, Id dep)
81 Pool *pool = solv->pool;
86 Reldep *rd = GETRELDEP(pool, dep);
87 if (rd->flags == REL_AND)
89 if (!solver_dep_installed(solv, rd->name))
91 return solver_dep_installed(solv, rd->evr);
93 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
94 return solver_dep_installed(solv, rd->evr);
96 FOR_PROVIDES(p, pp, dep)
98 if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
107 /************************************************************************/
110 * make assertion rules into decisions
112 * Go through rules and add direct assertions to the decisionqueue.
113 * If we find a conflict, disable rules and add them to problem queue.
117 makeruledecisions(Solver *solv)
119 Pool *pool = solv->pool;
124 int record_proof = 1;
126 /* The system solvable is always installed first */
127 assert(solv->decisionq.count == 0);
128 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
129 queue_push(&solv->decisionq_why, 0);
130 solv->decisionmap[SYSTEMSOLVABLE] = 1; /* installed at level '1' */
132 decisionstart = solv->decisionq.count;
133 for (ii = 0; ii < solv->ruleassertions.count; ii++)
135 ri = solv->ruleassertions.elements[ii];
136 r = solv->rules + ri;
138 if (r->d < 0 || !r->p || r->w2) /* disabled, dummy or no assertion */
140 /* do weak rules in phase 2 */
141 if (ri < solv->learntrules && MAPTST(&solv->weakrulemap, ri))
147 if (!solv->decisionmap[vv]) /* if not yet decided */
152 queue_push(&solv->decisionq, v);
153 queue_push(&solv->decisionq_why, r - solv->rules);
154 solv->decisionmap[vv] = v > 0 ? 1 : -1;
155 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
157 Solvable *s = solv->pool->solvables + vv;
159 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", pool_solvable2str(solv->pool, s));
161 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing %s (assertion)\n", pool_solvable2str(solv->pool, s));
166 * check previous decision: is it sane ?
169 if (v > 0 && solv->decisionmap[vv] > 0) /* ok to install */
171 if (v < 0 && solv->decisionmap[vv] < 0) /* ok to remove */
177 * The rule (r) we're currently processing says something
178 * different (v = r->p) than a previous decision (decisionmap[abs(v)])
182 if (ri >= solv->learntrules)
184 /* conflict with a learnt rule */
185 /* can happen when packages cannot be installed for
186 * multiple reasons. */
187 /* we disable the learnt rule in this case */
188 solver_disablerule(solv, r);
193 * find the decision which is the "opposite" of the rule
196 for (i = 0; i < solv->decisionq.count; i++)
197 if (solv->decisionq.elements[i] == -v)
199 assert(i < solv->decisionq.count); /* assert that we found it */
202 * conflict with system solvable ?
205 if (v == -SYSTEMSOLVABLE)
207 /* conflict with system solvable */
210 queue_push(&solv->problems, solv->learnt_pool.count);
211 queue_push(&solv->learnt_pool, ri);
212 queue_push(&solv->learnt_pool, 0);
215 queue_push(&solv->problems, 0);
216 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
217 if (ri >= solv->jobrules && ri < solv->jobrules_end)
218 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
221 queue_push(&solv->problems, v);
222 queue_push(&solv->problems, 0);
223 solver_disableproblem(solv, v);
227 assert(solv->decisionq_why.elements[i] > 0);
230 * conflict with an rpm rule ?
233 if (solv->decisionq_why.elements[i] < solv->rpmrules_end)
235 /* conflict with rpm rule assertion */
238 queue_push(&solv->problems, solv->learnt_pool.count);
239 queue_push(&solv->learnt_pool, ri);
240 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
241 queue_push(&solv->learnt_pool, 0);
244 queue_push(&solv->problems, 0);
245 assert(v > 0 || v == -SYSTEMSOLVABLE);
246 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
247 if (ri >= solv->jobrules && ri < solv->jobrules_end)
248 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
251 queue_push(&solv->problems, v);
252 queue_push(&solv->problems, 0);
253 solver_disableproblem(solv, v);
258 * conflict with another job or update/feature rule
264 queue_push(&solv->problems, solv->learnt_pool.count);
265 queue_push(&solv->learnt_pool, ri);
266 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
267 queue_push(&solv->learnt_pool, 0);
270 queue_push(&solv->problems, 0);
272 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflicting update/job assertions over literal %d\n", vv);
275 * push all of our rules (can only be feature or job rules)
276 * asserting this literal on the problem stack
279 for (i = solv->featurerules, rr = solv->rules + i; i < solv->learntrules; i++, rr++)
281 if (rr->d < 0 /* disabled */
282 || rr->w2) /* or no assertion */
284 if (rr->p != vv /* not affecting the literal */
287 if (MAPTST(&solv->weakrulemap, i)) /* weak: silently ignore */
290 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
292 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + i);
295 /* is is a job rule ? */
296 if (i >= solv->jobrules && i < solv->jobrules_end)
297 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
299 queue_push(&solv->problems, v);
300 solver_disableproblem(solv, v);
302 queue_push(&solv->problems, 0);
306 * (back up from decisions)
308 while (solv->decisionq.count > decisionstart)
310 v = solv->decisionq.elements[--solv->decisionq.count];
311 --solv->decisionq_why.count;
313 solv->decisionmap[vv] = 0;
315 ii = -1; /* restarts loop at 0 */
319 * phase 2: now do the weak assertions
321 for (ii = 0; ii < solv->ruleassertions.count; ii++)
323 ri = solv->ruleassertions.elements[ii];
324 r = solv->rules + ri;
325 if (r->d < 0 || r->w2) /* disabled or no assertion */
327 if (ri >= solv->learntrules || !MAPTST(&solv->weakrulemap, ri)) /* skip non-weak */
333 * (if not yet decided)
335 if (!solv->decisionmap[vv])
337 queue_push(&solv->decisionq, v);
338 queue_push(&solv->decisionq_why, r - solv->rules);
339 solv->decisionmap[vv] = v > 0 ? 1 : -1;
340 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
342 Solvable *s = solv->pool->solvables + vv;
344 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
346 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
351 * previously decided, sane ?
353 if (v > 0 && solv->decisionmap[vv] > 0)
355 if (v < 0 && solv->decisionmap[vv] < 0)
358 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
359 solver_printrule(solv, SOLV_DEBUG_UNSOLVABLE, r);
361 if (ri >= solv->jobrules && ri < solv->jobrules_end)
362 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
365 solver_disableproblem(solv, v);
367 solver_reenablepolicyrules(solv, -v);
372 /*-------------------------------------------------------------------
373 * enable/disable learnt rules
375 * we have enabled or disabled some of our rules. We now reenable all
376 * of our learnt rules except the ones that were learnt from rules that
380 enabledisablelearntrules(Solver *solv)
382 Pool *pool = solv->pool;
387 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
388 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
390 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
391 while ((why = *whyp++) != 0)
393 assert(why > 0 && why < i);
394 if (solv->rules[why].d < 0)
397 /* why != 0: we found a disabled rule, disable the learnt rule */
398 if (why && r->d >= 0)
400 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
402 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "disabling ");
403 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
405 solver_disablerule(solv, r);
407 else if (!why && r->d < 0)
409 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
411 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "re-enabling ");
412 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
414 solver_enablerule(solv, r);
420 /********************************************************************/
424 /*-------------------------------------------------------------------
427 * initial setup for all watches
431 makewatches(Solver *solv)
435 int nsolvables = solv->pool->nsolvables;
437 solv_free(solv->watches);
438 /* lower half for removals, upper half for installs */
439 solv->watches = solv_calloc(2 * nsolvables, sizeof(Id));
441 /* do it reverse so rpm rules get triggered first (XXX: obsolete?) */
442 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
444 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
447 if (!r->w2) /* assertions do not need watches */
450 /* see addwatches_rule(solv, r) */
451 r->n1 = solv->watches[nsolvables + r->w1];
452 solv->watches[nsolvables + r->w1] = r - solv->rules;
454 r->n2 = solv->watches[nsolvables + r->w2];
455 solv->watches[nsolvables + r->w2] = r - solv->rules;
460 /*-------------------------------------------------------------------
462 * add watches (for a new learned rule)
463 * sets up watches for a single rule
465 * see also makewatches() above.
469 addwatches_rule(Solver *solv, Rule *r)
471 int nsolvables = solv->pool->nsolvables;
473 r->n1 = solv->watches[nsolvables + r->w1];
474 solv->watches[nsolvables + r->w1] = r - solv->rules;
476 r->n2 = solv->watches[nsolvables + r->w2];
477 solv->watches[nsolvables + r->w2] = r - solv->rules;
481 /********************************************************************/
487 /* shortcuts to check if a literal (positive or negative) assignment
488 * evaluates to 'true' or 'false'
490 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
491 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
492 #define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
494 /*-------------------------------------------------------------------
498 * make decision and propagate to all rules
500 * Evaluate each term affected by the decision (linked through watches).
501 * If we find unit rules we make new decisions based on them.
503 * return : 0 = everything is OK
504 * rule = conflict found in this rule
508 propagate(Solver *solv, int level)
510 Pool *pool = solv->pool;
511 Id *rp, *next_rp; /* rule pointer, next rule pointer in linked list */
513 Id p, pkg, other_watch;
515 Id *decisionmap = solv->decisionmap;
517 Id *watches = solv->watches + pool->nsolvables; /* place ptr in middle */
519 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate -----\n");
521 /* foreach non-propagated decision */
522 while (solv->propagate_index < solv->decisionq.count)
525 * 'pkg' was just decided
526 * negate because our watches trigger if literal goes FALSE
528 pkg = -solv->decisionq.elements[solv->propagate_index++];
530 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
532 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
533 solver_printruleelement(solv, SOLV_DEBUG_PROPAGATE, 0, -pkg);
536 /* foreach rule where 'pkg' is now FALSE */
537 for (rp = watches + pkg; *rp; rp = next_rp)
539 r = solv->rules + *rp;
542 /* rule is disabled, goto next */
550 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
552 POOL_DEBUG(SOLV_DEBUG_PROPAGATE," watch triggered ");
553 solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
556 /* 'pkg' was just decided (was set to FALSE)
558 * now find other literal watch, check clause
559 * and advance on linked list
573 * This term is already true (through the other literal)
574 * so we have nothing to do
576 if (DECISIONMAP_TRUE(other_watch))
580 * The other literal is FALSE or UNDEF
586 /* Not a binary clause, try to move our watch.
588 * Go over all literals and find one that is
592 * (TRUE is also ok, in that case the rule is fulfilled)
594 if (r->p /* we have a 'p' */
595 && r->p != other_watch /* which is not watched */
596 && !DECISIONMAP_FALSE(r->p)) /* and not FALSE */
600 else /* go find a 'd' to make 'true' */
603 we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
605 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
607 if (p != other_watch /* which is not watched */
608 && !DECISIONMAP_FALSE(p)) /* and not FALSE */
616 * if we found some p that is UNDEF or TRUE, move
619 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
622 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, p));
624 POOL_DEBUG(SOLV_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, -p));
640 watches[p] = r - solv->rules;
643 /* search failed, thus all unwatched literals are FALSE */
648 * unit clause found, set literal other_watch to TRUE
651 if (DECISIONMAP_FALSE(other_watch)) /* check if literal is FALSE */
652 return r; /* eek, a conflict! */
654 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
656 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " unit ");
657 solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
661 decisionmap[other_watch] = level; /* install! */
663 decisionmap[-other_watch] = -level; /* remove! */
665 queue_push(&solv->decisionq, other_watch);
666 queue_push(&solv->decisionq_why, r - solv->rules);
668 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
671 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> decided to install %s\n", pool_solvid2str(pool, other_watch));
673 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> decided to conflict %s\n", pool_solvid2str(pool, -other_watch));
676 } /* foreach rule involving 'pkg' */
678 } /* while we have non-decided decisions */
680 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate end-----\n");
682 return 0; /* all is well */
686 /********************************************************************/
689 /*-------------------------------------------------------------------
696 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
698 Pool *pool = solv->pool;
702 Map seen; /* global? */
703 Id d, v, vv, *dp, why;
705 int num = 0, l1num = 0;
706 int learnt_why = solv->learnt_pool.count;
707 Id *decisionmap = solv->decisionmap;
709 queue_init_buffer(&r, r_buf, sizeof(r_buf)/sizeof(*r_buf));
711 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
712 map_init(&seen, pool->nsolvables);
713 idx = solv->decisionq.count;
716 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
717 solver_printruleclass(solv, SOLV_DEBUG_ANALYZE, c);
718 queue_push(&solv->learnt_pool, c - solv->rules);
719 d = c->d < 0 ? -c->d - 1 : c->d;
720 dp = d ? pool->whatprovidesdata + d : 0;
721 /* go through all literals of the rule */
733 if (DECISIONMAP_TRUE(v)) /* the one true literal */
736 if (MAPTST(&seen, vv))
738 l = solv->decisionmap[vv];
741 MAPSET(&seen, vv); /* mark that we also need to look at this literal */
743 l1num++; /* need to do this one in level1 pass */
745 num++; /* need to do this one as well */
748 queue_push(&r, v); /* not level1 or conflict level, add to new rule */
754 if (!num && !--l1num)
755 break; /* all level 1 literals done */
757 /* find the next literal to investigate */
758 /* (as num + l1num > 0, we know that we'll always find one) */
762 v = solv->decisionq.elements[--idx];
764 if (MAPTST(&seen, vv))
769 if (num && --num == 0)
771 *pr = -v; /* so that v doesn't get lost */
774 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
775 /* clear non-l1 bits from seen map */
776 for (i = 0; i < r.count; i++)
779 MAPCLR(&seen, v > 0 ? v : -v);
781 /* only level 1 marks left in seen map */
782 l1num++; /* as l1retry decrements it */
786 why = solv->decisionq_why.elements[idx];
787 if (why <= 0) /* just in case, maybe for SYSTEMSOLVABLE */
789 c = solv->rules + why;
795 else if (r.count == 1 && r.elements[0] < 0)
798 *dr = pool_queuetowhatprovides(pool, &r);
799 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
801 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
802 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, *pr);
803 for (i = 0; i < r.count; i++)
804 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, r.elements[i]);
806 /* push end marker on learnt reasons stack */
807 queue_push(&solv->learnt_pool, 0);
811 solv->stats_learned++;
816 /*-------------------------------------------------------------------
820 * reset all solver decisions
821 * called after rules have been enabled/disabled
825 solver_reset(Solver *solv)
827 Pool *pool = solv->pool;
831 /* rewind all decisions */
832 for (i = solv->decisionq.count - 1; i >= 0; i--)
834 v = solv->decisionq.elements[i];
835 solv->decisionmap[v > 0 ? v : -v] = 0;
837 solv->decisionq_why.count = 0;
838 solv->decisionq.count = 0;
839 solv->recommends_index = -1;
840 solv->propagate_index = 0;
841 solv->branches.count = 0;
843 /* adapt learnt rule status to new set of enabled/disabled rules */
844 enabledisablelearntrules(solv);
846 /* redo all assertion rule decisions */
847 makeruledecisions(solv);
848 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
852 /*-------------------------------------------------------------------
854 * analyze_unsolvable_rule
856 * recursion helper used by analyze_unsolvable
860 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp, Map *rseen)
862 Pool *pool = solv->pool;
864 Id why = r - solv->rules;
866 IF_POOLDEBUG (SOLV_DEBUG_UNSOLVABLE)
867 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, r);
868 if (solv->learntrules && why >= solv->learntrules)
870 if (MAPTST(rseen, why - solv->learntrules))
872 MAPSET(rseen, why - solv->learntrules);
873 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
874 if (solv->learnt_pool.elements[i] > 0)
875 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp, rseen);
878 if (MAPTST(&solv->weakrulemap, why))
879 if (!*lastweakp || why > *lastweakp)
881 /* do not add rpm rules to problem */
882 if (why < solv->rpmrules_end)
884 /* turn rule into problem */
885 if (why >= solv->jobrules && why < solv->jobrules_end)
886 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
887 /* normalize dup/infarch rules */
888 if (why > solv->infarchrules && why < solv->infarchrules_end)
890 Id name = pool->solvables[-solv->rules[why].p].name;
891 while (why > solv->infarchrules && pool->solvables[-solv->rules[why - 1].p].name == name)
894 if (why > solv->duprules && why < solv->duprules_end)
896 Id name = pool->solvables[-solv->rules[why].p].name;
897 while (why > solv->duprules && pool->solvables[-solv->rules[why - 1].p].name == name)
901 /* return if problem already countains our rule */
902 if (solv->problems.count)
904 for (i = solv->problems.count - 1; i >= 0; i--)
905 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
907 else if (solv->problems.elements[i] == why)
910 queue_push(&solv->problems, why);
914 /*-------------------------------------------------------------------
918 * We know that the problem is not solvable. Record all involved
919 * rules (i.e. the "proof") into solv->learnt_pool.
920 * Record the learnt pool index and all non-rpm rules into
921 * solv->problems. (Our solutions to fix the problems are to
922 * disable those rules.)
924 * If the proof contains at least one weak rule, we disable the
927 * Otherwise we return 0 if disablerules is not set or disable
928 * _all_ of the problem rules and return 1.
930 * return: 1 - disabled some rules, try again
935 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
937 Pool *pool = solv->pool;
939 Map seen; /* global to speed things up? */
941 Id d, v, vv, *dp, why;
943 Id *decisionmap = solv->decisionmap;
945 int oldlearntpoolcount;
947 int record_proof = 1;
949 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
950 solv->stats_unsolvable++;
951 oldproblemcount = solv->problems.count;
952 oldlearntpoolcount = solv->learnt_pool.count;
954 /* make room for proof index */
955 /* must update it later, as analyze_unsolvable_rule would confuse
956 * it with a rule index if we put the real value in already */
957 queue_push(&solv->problems, 0);
960 map_init(&seen, pool->nsolvables);
961 map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
963 queue_push(&solv->learnt_pool, r - solv->rules);
965 analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
966 d = r->d < 0 ? -r->d - 1 : r->d;
967 dp = d ? pool->whatprovidesdata + d : 0;
978 if (DECISIONMAP_TRUE(v)) /* the one true literal */
981 l = solv->decisionmap[vv];
986 idx = solv->decisionq.count;
989 v = solv->decisionq.elements[--idx];
991 if (!MAPTST(&seen, vv))
993 why = solv->decisionq_why.elements[idx];
996 queue_push(&solv->learnt_pool, why);
997 r = solv->rules + why;
998 analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
999 d = r->d < 0 ? -r->d - 1 : r->d;
1000 dp = d ? pool->whatprovidesdata + d : 0;
1011 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1013 vv = v > 0 ? v : -v;
1014 l = solv->decisionmap[vv];
1022 queue_push(&solv->problems, 0); /* mark end of this problem */
1027 /* disable last weak rule */
1028 solv->problems.count = oldproblemcount;
1029 solv->learnt_pool.count = oldlearntpoolcount;
1030 if (lastweak >= solv->jobrules && lastweak < solv->jobrules_end)
1031 v = -(solv->ruletojob.elements[lastweak - solv->jobrules] + 1);
1034 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "disabling ");
1035 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + lastweak);
1036 if (lastweak >= solv->choicerules && lastweak < solv->choicerules_end)
1037 solver_disablechoicerules(solv, solv->rules + lastweak);
1038 solver_disableproblem(solv, v);
1040 solver_reenablepolicyrules(solv, -v);
1048 queue_push(&solv->learnt_pool, 0);
1049 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1052 /* + 2: index + trailing zero */
1053 if (disablerules && oldproblemcount + 2 < solv->problems.count)
1055 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1056 solver_disableproblem(solv, solv->problems.elements[i]);
1057 /* XXX: might want to enable all weak rules again */
1061 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1066 /********************************************************************/
1067 /* Decision revert */
1069 /*-------------------------------------------------------------------
1072 * revert decisionq to a level
1076 revert(Solver *solv, int level)
1078 Pool *pool = solv->pool;
1080 while (solv->decisionq.count)
1082 v = solv->decisionq.elements[solv->decisionq.count - 1];
1083 vv = v > 0 ? v : -v;
1084 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1086 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1087 solv->decisionmap[vv] = 0;
1088 solv->decisionq.count--;
1089 solv->decisionq_why.count--;
1090 solv->propagate_index = solv->decisionq.count;
1092 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1094 solv->branches.count--;
1095 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1096 solv->branches.count--;
1098 solv->recommends_index = -1;
1102 /*-------------------------------------------------------------------
1104 * watch2onhighest - put watch2 on literal with highest level
1108 watch2onhighest(Solver *solv, Rule *r)
1113 d = r->d < 0 ? -r->d - 1 : r->d;
1115 return; /* binary rule, both watches are set */
1116 dp = solv->pool->whatprovidesdata + d;
1117 while ((v = *dp++) != 0)
1119 l = solv->decisionmap[v < 0 ? -v : v];
1131 /*-------------------------------------------------------------------
1135 * add free decision (solvable to install) to decisionq
1136 * increase level and propagate decision
1137 * return if no conflict.
1139 * in conflict case, analyze conflict rule, add resulting
1140 * rule to learnt rule set, make decision from learnt
1141 * rule (always unit) and re-propagate.
1143 * returns the new solver level or 0 if unsolvable
1148 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules, Id ruleid)
1150 Pool *pool = solv->pool;
1155 assert(ruleid >= 0);
1160 solv->decisionmap[decision] = level;
1162 solv->decisionmap[-decision] = -level;
1163 queue_push(&solv->decisionq, decision);
1164 queue_push(&solv->decisionq_why, -ruleid); /* <= 0 -> free decision */
1168 r = propagate(solv, level);
1172 return analyze_unsolvable(solv, r, disablerules);
1173 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1174 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
1175 assert(l > 0 && l < level);
1176 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1178 revert(solv, level);
1179 r = solver_addrule(solv, p, d);
1181 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
1182 queue_push(&solv->learnt_why, why);
1185 /* at least 2 literals, needs watches */
1186 watch2onhighest(solv, r);
1187 addwatches_rule(solv, r);
1191 /* learnt rule is an assertion */
1192 queue_push(&solv->ruleassertions, r - solv->rules);
1194 /* the new rule is unit by design */
1195 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1196 queue_push(&solv->decisionq, p);
1197 queue_push(&solv->decisionq_why, r - solv->rules);
1198 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
1200 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "decision: ");
1201 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, p);
1202 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "new rule: ");
1203 solver_printrule(solv, SOLV_DEBUG_ANALYZE, r);
1210 /*-------------------------------------------------------------------
1212 * select and install
1214 * install best package from the queue. We add an extra package, inst, if
1215 * provided. See comment in weak install section.
1217 * returns the new solver level or 0 if unsolvable
1222 selectandinstall(Solver *solv, int level, Queue *dq, int disablerules, Id ruleid)
1224 Pool *pool = solv->pool;
1229 policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1232 /* XXX: didn't we already do that? */
1233 /* XXX: shouldn't we prefer installed packages? */
1234 /* XXX: move to policy.c? */
1235 /* choose the supplemented one */
1236 for (i = 0; i < dq->count; i++)
1237 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1239 dq->elements[0] = dq->elements[i];
1246 /* multiple candidates, open a branch */
1247 for (i = 1; i < dq->count; i++)
1248 queue_push(&solv->branches, dq->elements[i]);
1249 queue_push(&solv->branches, -level);
1251 p = dq->elements[0];
1253 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing %s\n", pool_solvid2str(pool, p));
1255 return setpropagatelearn(solv, level, p, disablerules, ruleid);
1259 /********************************************************************/
1260 /* Main solver interface */
1263 /*-------------------------------------------------------------------
1266 * create solver structure
1268 * pool: all available solvables
1269 * installed: installed Solvables
1272 * Upon solving, rules are created to flag the Solvables
1273 * of the 'installed' Repo as installed.
1277 solver_create(Pool *pool)
1280 solv = (Solver *)solv_calloc(1, sizeof(Solver));
1282 solv->installed = pool->installed;
1284 solv->dup_allowdowngrade = 1;
1285 solv->dup_allowarchchange = 1;
1286 solv->dup_allowvendorchange = 1;
1288 queue_init(&solv->ruletojob);
1289 queue_init(&solv->decisionq);
1290 queue_init(&solv->decisionq_why);
1291 queue_init(&solv->problems);
1292 queue_init(&solv->orphaned);
1293 queue_init(&solv->learnt_why);
1294 queue_init(&solv->learnt_pool);
1295 queue_init(&solv->branches);
1296 queue_init(&solv->weakruleq);
1297 queue_init(&solv->ruleassertions);
1299 queue_push(&solv->learnt_pool, 0); /* so that 0 does not describe a proof */
1301 map_init(&solv->recommendsmap, pool->nsolvables);
1302 map_init(&solv->suggestsmap, pool->nsolvables);
1303 map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1304 solv->recommends_index = 0;
1306 solv->decisionmap = (Id *)solv_calloc(pool->nsolvables, sizeof(Id));
1308 solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1309 memset(solv->rules, 0, sizeof(Rule));
1315 /*-------------------------------------------------------------------
1321 solver_free(Solver *solv)
1323 queue_free(&solv->job);
1324 queue_free(&solv->ruletojob);
1325 queue_free(&solv->decisionq);
1326 queue_free(&solv->decisionq_why);
1327 queue_free(&solv->learnt_why);
1328 queue_free(&solv->learnt_pool);
1329 queue_free(&solv->problems);
1330 queue_free(&solv->solutions);
1331 queue_free(&solv->orphaned);
1332 queue_free(&solv->branches);
1333 queue_free(&solv->weakruleq);
1334 queue_free(&solv->ruleassertions);
1336 map_free(&solv->recommendsmap);
1337 map_free(&solv->suggestsmap);
1338 map_free(&solv->noupdate);
1339 map_free(&solv->weakrulemap);
1340 map_free(&solv->noobsoletes);
1342 map_free(&solv->updatemap);
1343 map_free(&solv->fixmap);
1344 map_free(&solv->dupmap);
1345 map_free(&solv->dupinvolvedmap);
1346 map_free(&solv->droporphanedmap);
1347 map_free(&solv->cleandepsmap);
1349 solv_free(solv->decisionmap);
1350 solv_free(solv->rules);
1351 solv_free(solv->watches);
1352 solv_free(solv->obsoletes);
1353 solv_free(solv->obsoletes_data);
1354 solv_free(solv->multiversionupdaters);
1355 solv_free(solv->choicerules_ref);
1360 solver_get_flag(Solver *solv, int flag)
1364 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1365 return solv->allowdowngrade;
1366 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1367 return solv->allowarchchange;
1368 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1369 return solv->allowvendorchange;
1370 case SOLVER_FLAG_ALLOW_UNINSTALL:
1371 return solv->allowuninstall;
1372 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1373 return solv->noupdateprovide;
1374 case SOLVER_FLAG_SPLITPROVIDES:
1375 return solv->dosplitprovides;
1376 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1377 return solv->dontinstallrecommended;
1378 case SOLVER_FLAG_IGNORE_ALREADY_RECOMMENDED:
1379 return solv->ignorealreadyrecommended;
1380 case SOLVER_FLAG_NO_INFARCHCHECK:
1381 return solv->noinfarchcheck;
1389 solver_set_flag(Solver *solv, int flag, int value)
1391 int old = solver_get_flag(solv, flag);
1394 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1395 solv->allowdowngrade = value;
1397 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1398 solv->allowarchchange = value;
1400 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1401 solv->allowvendorchange = value;
1403 case SOLVER_FLAG_ALLOW_UNINSTALL:
1404 solv->allowuninstall = value;
1406 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1407 solv->noupdateprovide = value;
1409 case SOLVER_FLAG_SPLITPROVIDES:
1410 solv->dosplitprovides = value;
1412 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1413 solv->dontinstallrecommended = value;
1415 case SOLVER_FLAG_IGNORE_ALREADY_RECOMMENDED:
1416 solv->ignorealreadyrecommended = value;
1418 case SOLVER_FLAG_NO_INFARCHCHECK:
1419 solv->noinfarchcheck = value;
1428 /*-------------------------------------------------------------------
1432 * all rules have been set up, now actually run the solver
1437 solver_run_sat(Solver *solv, int disablerules, int doweak)
1439 Queue dq; /* local decisionqueue */
1440 Queue dqs; /* local decisionqueue for supplements */
1446 Pool *pool = solv->pool;
1448 int minimizationsteps;
1449 int installedpos = solv->installed ? solv->installed->start : 0;
1451 IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
1453 POOL_DEBUG (SOLV_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1454 for (i = 1; i < solv->nrules; i++)
1455 solver_printruleclass(solv, SOLV_DEBUG_RULE_CREATION, solv->rules + i);
1458 POOL_DEBUG(SOLV_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1460 /* start SAT algorithm */
1462 systemlevel = level + 1;
1463 POOL_DEBUG(SOLV_DEBUG_SOLVER, "solving...\n");
1469 * here's the main loop:
1470 * 1) propagate new decisions (only needed once)
1472 * 3) try to keep installed packages
1473 * 4) fulfill all unresolved rules
1474 * 5) install recommended packages
1475 * 6) minimalize solution if we had choices
1476 * if we encounter a problem, we rewind to a safe level and restart
1480 minimizationsteps = 0;
1484 * initial propagation of the assertions
1488 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1489 if ((r = propagate(solv, level)) != 0)
1491 if (analyze_unsolvable(solv, r, disablerules))
1494 break; /* unsolvable */
1499 * resolve jobs first
1501 if (level < systemlevel)
1503 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving job rules\n");
1504 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1507 if (r->d < 0) /* ignore disabled rules */
1510 FOR_RULELITERALS(l, dp, r)
1514 if (solv->decisionmap[-l] <= 0)
1519 if (solv->decisionmap[l] > 0)
1521 if (solv->decisionmap[l] == 0)
1527 /* prune to installed if not updating */
1528 if (dq.count > 1 && solv->installed && !solv->updatemap_all)
1531 for (j = k = 0; j < dq.count; j++)
1533 Solvable *s = pool->solvables + dq.elements[j];
1534 if (s->repo == solv->installed)
1536 dq.elements[k++] = dq.elements[j];
1537 if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
1539 k = 0; /* package wants to be updated, do not prune */
1548 level = selectandinstall(solv, level, &dq, disablerules, i);
1551 if (level <= olevel)
1555 break; /* unsolvable */
1556 systemlevel = level + 1;
1557 if (i < solv->jobrules_end)
1559 solv->decisioncnt_update = solv->decisionq.count;
1560 solv->decisioncnt_keep = solv->decisionq.count;
1564 * installed packages
1566 if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1568 Repo *installed = solv->installed;
1571 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving installed packages\n");
1572 /* we use two passes if we need to update packages
1573 * to create a better user experience */
1574 for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1576 int passlevel = level;
1578 solv->decisioncnt_keep = solv->decisionq.count;
1579 /* start with installedpos, the position that gave us problems last time */
1580 for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1585 if (i == installed->end)
1586 i = installed->start;
1587 s = pool->solvables + i;
1588 if (s->repo != installed)
1591 if (solv->decisionmap[i] > 0)
1593 if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1594 continue; /* updates first */
1595 r = solv->rules + solv->updaterules + (i - installed->start);
1597 if (!rr->p || rr->d < 0) /* disabled -> look at feature rule */
1598 rr -= solv->installed->end - solv->installed->start;
1599 if (!rr->p) /* identical to update rule? */
1602 continue; /* orpaned package */
1604 /* XXX: noupdate check is probably no longer needed, as all jobs should
1605 * already be satisfied */
1606 /* Actually we currently still need it because of erase jobs */
1607 /* if noupdate is set we do not look at update candidates */
1609 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))
1611 if (solv->noobsoletes.size && solv->multiversionupdaters
1612 && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1614 /* special multiversion handling, make sure best version is chosen */
1616 while ((p = pool->whatprovidesdata[d++]) != 0)
1617 if (solv->decisionmap[p] >= 0)
1619 policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1621 if (p != i && solv->decisionmap[p] == 0)
1623 rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1624 if (!rr->p) /* update rule == feature rule? */
1625 rr = rr - solv->featurerules + solv->updaterules;
1633 /* update to best package */
1634 FOR_RULELITERALS(p, dp, rr)
1636 if (solv->decisionmap[p] > 0)
1638 dq.count = 0; /* already fulfilled */
1641 if (!solv->decisionmap[p])
1646 /* install best version */
1650 level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1657 if (level <= olevel)
1659 if (level == 1 || level < passlevel)
1660 break; /* trouble */
1662 n = installed->start; /* redo all */
1668 /* if still undecided keep package */
1669 if (solv->decisionmap[i] == 0)
1672 if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
1674 POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
1675 level = setpropagatelearn(solv, level, -i, disablerules, 0);
1679 POOL_DEBUG(SOLV_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
1680 level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1684 if (level <= olevel)
1686 if (level == 1 || level < passlevel)
1687 break; /* trouble */
1689 n = installed->start; /* redo all */
1692 continue; /* retry with learnt rule */
1696 if (n < installed->end)
1698 installedpos = i; /* retry problem solvable next time */
1699 break; /* ran into trouble */
1701 installedpos = installed->start; /* reset installedpos */
1704 break; /* unsolvable */
1705 systemlevel = level + 1;
1707 continue; /* had trouble, retry */
1710 if (level < systemlevel)
1711 systemlevel = level;
1716 solv->decisioncnt_resolve = solv->decisionq.count;
1717 POOL_DEBUG(SOLV_DEBUG_POLICY, "deciding unresolved rules\n");
1718 for (i = 1, n = 1; n < solv->nrules; i++, n++)
1720 if (i == solv->nrules)
1722 r = solv->rules + i;
1723 if (r->d < 0) /* ignore disabled rules */
1728 /* binary or unary rule */
1729 /* need two positive undecided literals */
1730 if (r->p < 0 || r->w2 <= 0)
1732 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1734 queue_push(&dq, r->p);
1735 queue_push(&dq, r->w2);
1740 * all negative literals are installed
1741 * no positive literal is installed
1742 * i.e. the rule is not fulfilled and we
1743 * just need to decide on the positive literals
1747 if (solv->decisionmap[-r->p] <= 0)
1752 if (solv->decisionmap[r->p] > 0)
1754 if (solv->decisionmap[r->p] == 0)
1755 queue_push(&dq, r->p);
1757 dp = pool->whatprovidesdata + r->d;
1758 while ((p = *dp++) != 0)
1762 if (solv->decisionmap[-p] <= 0)
1767 if (solv->decisionmap[p] > 0)
1769 if (solv->decisionmap[p] == 0)
1776 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
1778 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "unfulfilled ");
1779 solver_printruleclass(solv, SOLV_DEBUG_PROPAGATE, r);
1781 /* dq.count < 2 cannot happen as this means that
1782 * the rule is unit */
1783 assert(dq.count > 1);
1786 level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
1788 break; /* unsolvable */
1789 if (level < systemlevel || level == 1)
1790 break; /* trouble */
1791 /* something changed, so look at all rules again */
1795 if (n != solv->nrules) /* ran into trouble? */
1798 break; /* unsolvable */
1799 continue; /* start over */
1802 /* at this point we have a consistent system. now do the extras... */
1804 solv->decisioncnt_weak = solv->decisionq.count;
1809 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended packages\n");
1810 queue_empty(&dq); /* recommended packages */
1811 queue_empty(&dqs); /* supplemented packages */
1812 for (i = 1; i < pool->nsolvables; i++)
1814 if (solv->decisionmap[i] < 0)
1816 if (solv->decisionmap[i] > 0)
1818 /* installed, check for recommends */
1819 Id *recp, rec, pp, p;
1820 s = pool->solvables + i;
1821 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
1823 /* XXX need to special case AND ? */
1826 recp = s->repo->idarraydata + s->recommends;
1827 while ((rec = *recp++) != 0)
1830 FOR_PROVIDES(p, pp, rec)
1832 if (solv->decisionmap[p] > 0)
1837 else if (solv->decisionmap[p] == 0)
1839 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))))
1841 queue_pushunique(&dq, p);
1849 s = pool->solvables + i;
1850 if (!s->supplements)
1852 if (!pool_installable(pool, s))
1854 if (!solver_is_supplementing(solv, s))
1856 if (solv->dupmap_all && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
1858 queue_push(&dqs, i);
1862 /* filter out all packages obsoleted by installed packages */
1863 /* this is no longer needed if we have reverse obsoletes */
1864 if ((dqs.count || dq.count) && solv->installed)
1867 Id obs, *obsp, po, ppo;
1869 map_init(&obsmap, pool->nsolvables);
1870 for (p = solv->installed->start; p < solv->installed->end; p++)
1872 s = pool->solvables + p;
1873 if (s->repo != solv->installed || !s->obsoletes)
1875 if (solv->decisionmap[p] <= 0)
1877 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
1879 obsp = s->repo->idarraydata + s->obsoletes;
1880 /* foreach obsoletes */
1881 while ((obs = *obsp++) != 0)
1882 FOR_PROVIDES(po, ppo, obs)
1883 MAPSET(&obsmap, po);
1885 for (i = j = 0; i < dqs.count; i++)
1886 if (!MAPTST(&obsmap, dqs.elements[i]))
1887 dqs.elements[j++] = dqs.elements[i];
1889 for (i = j = 0; i < dq.count; i++)
1890 if (!MAPTST(&obsmap, dq.elements[i]))
1891 dq.elements[j++] = dq.elements[i];
1896 /* filter out all already supplemented packages if requested */
1897 if (solv->ignorealreadyrecommended && dqs.count)
1899 /* turn off all new packages */
1900 for (i = 0; i < solv->decisionq.count; i++)
1902 p = solv->decisionq.elements[i];
1905 s = pool->solvables + p;
1906 if (s->repo && s->repo != solv->installed)
1907 solv->decisionmap[p] = -solv->decisionmap[p];
1909 /* filter out old supplements */
1910 for (i = j = 0; i < dqs.count; i++)
1912 p = dqs.elements[i];
1913 s = pool->solvables + p;
1914 if (!s->supplements)
1916 if (!solver_is_supplementing(solv, s))
1917 dqs.elements[j++] = p;
1920 /* undo turning off */
1921 for (i = 0; i < solv->decisionq.count; i++)
1923 p = solv->decisionq.elements[i];
1926 s = pool->solvables + p;
1927 if (s->repo && s->repo != solv->installed)
1928 solv->decisionmap[p] = -solv->decisionmap[p];
1932 /* multiversion doesn't mix well with supplements.
1933 * filter supplemented packages where we already decided
1934 * to install a different version (see bnc#501088) */
1935 if (dqs.count && solv->noobsoletes.size)
1937 for (i = j = 0; i < dqs.count; i++)
1939 p = dqs.elements[i];
1940 if (MAPTST(&solv->noobsoletes, p))
1943 s = pool->solvables + p;
1944 FOR_PROVIDES(p2, pp2, s->name)
1945 if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
1948 continue; /* ignore this package */
1950 dqs.elements[j++] = p;
1955 /* make dq contain both recommended and supplemented pkgs */
1958 for (i = 0; i < dqs.count; i++)
1959 queue_pushunique(&dq, dqs.elements[i]);
1965 int decisioncount = solv->decisionq.count;
1969 /* simple case, just one package. no need to choose to best version */
1972 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
1974 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
1975 level = setpropagatelearn(solv, level, p, 0, 0);
1978 continue; /* back to main loop */
1981 /* filter packages, this gives us the best versions */
1982 policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
1984 /* create map of result */
1985 map_init(&dqmap, pool->nsolvables);
1986 for (i = 0; i < dq.count; i++)
1987 MAPSET(&dqmap, dq.elements[i]);
1989 /* install all supplemented packages */
1990 for (i = 0; i < dqs.count; i++)
1992 p = dqs.elements[i];
1993 if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
1995 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
1997 level = setpropagatelearn(solv, level, p, 0, 0);
1998 if (level <= olevel)
2001 if (i < dqs.count || solv->decisionq.count < decisioncount)
2009 /* install all recommended packages */
2010 /* more work as we want to created branches if multiple
2011 * choices are valid */
2012 for (i = 0; i < decisioncount; i++)
2015 p = solv->decisionq.elements[i];
2018 s = pool->solvables + p;
2019 if (!s->repo || (solv->ignorealreadyrecommended && s->repo == solv->installed))
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 && MAPTST(&dqmap, p))
2035 queue_pushunique(&dq, p);
2041 /* multiple candidates, open a branch */
2042 for (i = 1; i < dq.count; i++)
2043 queue_push(&solv->branches, dq.elements[i]);
2044 queue_push(&solv->branches, -level);
2047 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2049 level = setpropagatelearn(solv, level, p, 0, 0);
2050 if (level <= olevel || solv->decisionq.count < decisioncount)
2051 break; /* we had to revert some decisions */
2054 break; /* had a problem above, quit loop */
2059 continue; /* back to main loop so that all deps are checked */
2063 solv->decisioncnt_orphan = solv->decisionq.count;
2064 if (solv->dupmap_all && solv->installed)
2066 int installedone = 0;
2068 /* let's see if we can install some unsupported package */
2069 POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding orphaned packages\n");
2070 for (i = 0; i < solv->orphaned.count; i++)
2072 p = solv->orphaned.elements[i];
2073 if (solv->decisionmap[p])
2074 continue; /* already decided */
2076 if (solv->droporphanedmap_all)
2078 if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
2080 POOL_DEBUG(SOLV_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
2081 level = setpropagatelearn(solv, level, p, 0, 0);
2088 if (installedone || i < solv->orphaned.count)
2092 continue; /* back to main loop */
2094 for (i = 0; i < solv->orphaned.count; i++)
2096 p = solv->orphaned.elements[i];
2097 if (solv->decisionmap[p])
2098 continue; /* already decided */
2099 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2101 level = setpropagatelearn(solv, level, -p, 0, 0);
2105 if (i < solv->orphaned.count)
2109 continue; /* back to main loop */
2113 if (solv->solution_callback)
2115 solv->solution_callback(solv, solv->solution_callback_data);
2116 if (solv->branches.count)
2118 int i = solv->branches.count - 1;
2119 int l = -solv->branches.elements[i];
2123 if (solv->branches.elements[i - 1] < 0)
2125 p = solv->branches.elements[i];
2126 POOL_DEBUG(SOLV_DEBUG_SOLVER, "branching with %s\n", pool_solvid2str(pool, p));
2128 for (j = i + 1; j < solv->branches.count; j++)
2129 queue_push(&dq, solv->branches.elements[j]);
2130 solv->branches.count = i;
2132 revert(solv, level);
2134 for (j = 0; j < dq.count; j++)
2135 queue_push(&solv->branches, dq.elements[j]);
2137 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2139 level = setpropagatelearn(solv, level, p, disablerules, why);
2144 /* all branches done, we're finally finished */
2148 /* auto-minimization step */
2149 if (solv->branches.count)
2151 int l = 0, lasti = -1, lastl = -1;
2155 for (i = solv->branches.count - 1; i >= 0; i--)
2157 p = solv->branches.elements[i];
2160 else if (p > 0 && solv->decisionmap[p] > l + 1)
2168 /* kill old solvable so that we do not loop */
2169 p = solv->branches.elements[lasti];
2170 solv->branches.elements[lasti] = 0;
2171 POOL_DEBUG(SOLV_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, pool_solvid2str(pool, p));
2172 minimizationsteps++;
2175 revert(solv, level);
2176 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2179 level = setpropagatelearn(solv, level, p, disablerules, why);
2182 continue; /* back to main loop */
2185 /* no minimization found, we're finally finished! */
2189 POOL_DEBUG(SOLV_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2191 POOL_DEBUG(SOLV_DEBUG_STATS, "done solving.\n\n");
2197 solv->decisioncnt_update = solv->decisionq.count;
2198 solv->decisioncnt_keep = solv->decisionq.count;
2199 solv->decisioncnt_resolve = solv->decisionq.count;
2200 solv->decisioncnt_weak = solv->decisionq.count;
2201 solv->decisioncnt_orphan = solv->decisionq.count;
2204 solver_printdecisionq(solv, SOLV_DEBUG_RESULT);
2209 /*-------------------------------------------------------------------
2211 * remove disabled conflicts
2213 * purpose: update the decisionmap after some rules were disabled.
2214 * this is used to calculate the suggested/recommended package list.
2215 * Also returns a "removed" list to undo the discisionmap changes.
2219 removedisabledconflicts(Solver *solv, Queue *removed)
2221 Pool *pool = solv->pool;
2226 Id *decisionmap = solv->decisionmap;
2228 queue_empty(removed);
2229 for (i = 0; i < solv->decisionq.count; i++)
2231 p = solv->decisionq.elements[i];
2233 continue; /* conflicts only, please */
2234 why = solv->decisionq_why.elements[i];
2237 /* no rule involved, must be a orphan package drop */
2240 /* we never do conflicts on free decisions, so there
2241 * must have been an unit rule */
2243 r = solv->rules + why;
2244 if (r->d < 0 && decisionmap[-p])
2246 /* rule is now disabled, remove from decisionmap */
2247 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2248 queue_push(removed, -p);
2249 queue_push(removed, decisionmap[-p]);
2250 decisionmap[-p] = 0;
2253 if (!removed->count)
2255 /* we removed some confliced packages. some of them might still
2256 * be in conflict, so search for unit rules and re-conflict */
2258 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2260 if (i == solv->nrules)
2263 r = solv->rules + i;
2269 if (r->p < 0 && !decisionmap[-r->p])
2275 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2277 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2282 if (r->p < 0 && decisionmap[-r->p] == 0)
2284 if (new || DECISIONMAP_FALSE(r->p))
2286 dp = pool->whatprovidesdata + r->d;
2287 while ((p = *dp++) != 0)
2289 if (new && p == new)
2291 if (p < 0 && decisionmap[-p] == 0)
2300 else if (!DECISIONMAP_FALSE(p))
2310 POOL_DEBUG(SOLV_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2311 decisionmap[-new] = -1;
2313 n = 0; /* redo all rules */
2319 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2322 for (i = 0; i < removed->count; i += 2)
2323 solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2327 /*-------------------------------------------------------------------
2329 * weaken solvable dependencies
2333 weaken_solvable_deps(Solver *solv, Id p)
2338 for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2342 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2343 continue; /* conflict */
2344 queue_push(&solv->weakruleq, i);
2349 /********************************************************************/
2354 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap)
2357 Id how, what, select;
2359 for (i = 0; i < job->count; i += 2)
2361 how = job->elements[i];
2362 if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
2364 what = job->elements[i + 1];
2365 select = how & SOLVER_SELECTMASK;
2366 if (!noobsmap->size)
2367 map_grow(noobsmap, pool->nsolvables);
2368 FOR_JOB_SELECT(p, pp, select, what)
2369 MAPSET(noobsmap, p);
2374 * add a rule created by a job, record job number and weak flag
2377 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
2379 solver_addrule(solv, p, d);
2380 queue_push(&solv->ruletojob, job);
2382 queue_push(&solv->weakruleq, solv->nrules - 1);
2392 solver_solve(Solver *solv, Queue *job)
2394 Pool *pool = solv->pool;
2395 Repo *installed = solv->installed;
2398 Map addedmap; /* '1' == have rpm-rules for solvable */
2399 Map installcandidatemap;
2400 Id how, what, select, name, weak, p, pp, d;
2404 int now, solve_start;
2407 solve_start = solv_timems(0);
2409 /* log solver options */
2410 POOL_DEBUG(SOLV_DEBUG_STATS, "solver started\n");
2411 POOL_DEBUG(SOLV_DEBUG_STATS, "dosplitprovides=%d, noupdateprovide=%d noinfarchcheck=%d\n", solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
2412 POOL_DEBUG(SOLV_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allowarchchange, solv->allowvendorchange);
2413 POOL_DEBUG(SOLV_DEBUG_STATS, "promoteepoch=%d, allowselfconflicts=%d\n", pool->promoteepoch, pool->allowselfconflicts);
2414 POOL_DEBUG(SOLV_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2415 POOL_DEBUG(SOLV_DEBUG_STATS, "dontinstallrecommended=%d, ignorealreadyrecommended=%d\n", solv->dontinstallrecommended, solv->ignorealreadyrecommended);
2417 /* create whatprovides if not already there */
2418 if (!pool->whatprovides)
2419 pool_createwhatprovides(pool);
2421 /* create obsolete index */
2422 policy_create_obsolete_index(solv);
2425 queue_free(&solv->job);
2426 queue_init_clone(&solv->job, job);
2429 * create basic rule set of all involved packages
2430 * use addedmap bitmap to make sure we don't create rules twice
2433 /* create noobsolete map if needed */
2434 solver_calculate_noobsmap(pool, job, &solv->noobsoletes);
2436 map_init(&addedmap, pool->nsolvables);
2437 MAPSET(&addedmap, SYSTEMSOLVABLE);
2439 map_init(&installcandidatemap, pool->nsolvables);
2442 now = solv_timems(0);
2444 * create rules for all package that could be involved with the solving
2445 * so called: rpm rules
2450 /* check for update/verify jobs as they need to be known early */
2451 for (i = 0; i < job->count; i += 2)
2453 how = job->elements[i];
2454 what = job->elements[i + 1];
2455 select = how & SOLVER_SELECTMASK;
2456 switch (how & SOLVER_JOBMASK)
2459 if (select == SOLVER_SOLVABLE_ALL)
2460 solv->fixmap_all = 1;
2461 FOR_JOB_SELECT(p, pp, select, what)
2463 s = pool->solvables + p;
2464 if (!solv->installed || s->repo != solv->installed)
2466 if (!solv->fixmap.size)
2467 map_grow(&solv->fixmap, solv->installed->end - solv->installed->start);
2468 MAPSET(&solv->fixmap, p - solv->installed->start);
2472 if (select == SOLVER_SOLVABLE_ALL)
2473 solv->updatemap_all = 1;
2474 FOR_JOB_SELECT(p, pp, select, what)
2476 s = pool->solvables + p;
2477 if (!solv->installed || s->repo != solv->installed)
2479 if (!solv->updatemap.size)
2480 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
2481 MAPSET(&solv->updatemap, p - solv->installed->start);
2489 oldnrules = solv->nrules;
2490 FOR_REPO_SOLVABLES(installed, p, s)
2491 solver_addrpmrulesforsolvable(solv, s, &addedmap);
2492 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
2493 oldnrules = solv->nrules;
2494 FOR_REPO_SOLVABLES(installed, p, s)
2495 solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
2496 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
2500 * create rules for all packages involved in the job
2501 * (to be installed or removed)
2504 oldnrules = solv->nrules;
2505 for (i = 0; i < job->count; i += 2)
2507 how = job->elements[i];
2508 what = job->elements[i + 1];
2509 select = how & SOLVER_SELECTMASK;
2511 switch (how & SOLVER_JOBMASK)
2513 case SOLVER_INSTALL:
2514 FOR_JOB_SELECT(p, pp, select, what)
2516 MAPSET(&installcandidatemap, p);
2517 solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
2520 case SOLVER_DISTUPGRADE:
2521 if (select == SOLVER_SOLVABLE_ALL)
2523 solv->dupmap_all = 1;
2524 solv->updatemap_all = 1;
2526 if (!solv->dupmap_all)
2533 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
2537 * add rules for suggests, enhances
2539 oldnrules = solv->nrules;
2540 solver_addrpmrulesforweak(solv, &addedmap);
2541 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
2544 * first pass done, we now have all the rpm rules we need.
2545 * unify existing rules before going over all job rules and
2547 * at this point the system is always solvable,
2548 * as an empty system (remove all packages) is a valid solution
2551 IF_POOLDEBUG (SOLV_DEBUG_STATS)
2553 int possible = 0, installable = 0;
2554 for (i = 1; i < pool->nsolvables; i++)
2556 if (pool_installable(pool, pool->solvables + i))
2558 if (MAPTST(&addedmap, i))
2561 POOL_DEBUG(SOLV_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
2564 solver_unifyrules(solv); /* remove duplicate rpm rules */
2565 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
2567 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule memory usage: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
2568 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule creation took %d ms\n", solv_timems(now));
2570 /* create dup maps if needed. We need the maps early to create our
2573 solver_createdupmaps(solv);
2576 * create feature rules
2578 * foreach installed:
2579 * create assertion (keep installed, if no update available)
2581 * create update rule (A|update1(A)|update2(A)|...)
2583 * those are used later on to keep a version of the installed packages in
2587 solv->featurerules = solv->nrules; /* mark start of feature rules */
2590 /* foreach possibly installed solvable */
2591 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2593 if (s->repo != installed)
2595 solver_addrule(solv, 0, 0); /* create dummy rule */
2598 solver_addupdaterule(solv, s, 1); /* allow s to be updated */
2600 /* make sure we accounted for all rules */
2601 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
2603 solv->featurerules_end = solv->nrules;
2606 * Add update rules for installed solvables
2608 * almost identical to feature rules
2609 * except that downgrades/archchanges/vendorchanges are not allowed
2612 solv->updaterules = solv->nrules;
2615 { /* foreach installed solvables */
2616 /* we create all update rules, but disable some later on depending on the job */
2617 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2621 if (s->repo != installed)
2623 solver_addrule(solv, 0, 0); /* create dummy rule */
2626 solver_addupdaterule(solv, s, 0); /* allowall = 0: downgrades not allowed */
2628 * check for and remove duplicate
2630 r = solv->rules + solv->nrules - 1; /* r: update rule */
2631 sr = r - (installed->end - installed->start); /* sr: feature rule */
2632 /* it's orphaned if there is no feature rule or the feature rule
2633 * consists just of the installed package */
2634 if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
2635 queue_push(&solv->orphaned, i);
2638 assert(solv->dupmap_all && !sr->p);
2641 if (!solver_samerule(solv, r, sr))
2643 /* identical rule, kill unneeded one */
2644 if (solv->allowuninstall)
2646 /* keep feature rule, make it weak */
2647 memset(r, 0, sizeof(*r));
2648 queue_push(&solv->weakruleq, sr - solv->rules);
2652 /* keep update rule */
2653 memset(sr, 0, sizeof(*sr));
2656 else if (solv->allowuninstall)
2658 /* make both feature and update rule weak */
2659 queue_push(&solv->weakruleq, r - solv->rules);
2660 queue_push(&solv->weakruleq, sr - solv->rules);
2663 solver_disablerule(solv, sr);
2665 /* consistency check: we added a rule for _every_ installed solvable */
2666 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
2668 solv->updaterules_end = solv->nrules;
2672 * now add all job rules
2675 solv->jobrules = solv->nrules;
2676 for (i = 0; i < job->count; i += 2)
2678 oldnrules = solv->nrules;
2680 how = job->elements[i];
2681 what = job->elements[i + 1];
2682 weak = how & SOLVER_WEAK;
2683 select = how & SOLVER_SELECTMASK;
2684 switch (how & SOLVER_JOBMASK)
2686 case SOLVER_INSTALL:
2687 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2688 if (select == SOLVER_SOLVABLE)
2696 FOR_JOB_SELECT(p, pp, select, what)
2700 /* no candidate found, make this an impossible rule */
2701 queue_push(&q, -SYSTEMSOLVABLE);
2703 p = queue_shift(&q); /* get first candidate */
2704 d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q); /* internalize */
2706 solver_addjobrule(solv, p, d, i, weak);
2709 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
2710 if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && solv->installed)
2711 map_grow(&solv->cleandepsmap, solv->installed->end - solv->installed->start);
2712 if (select == SOLVER_SOLVABLE && solv->installed && pool->solvables[what].repo == solv->installed)
2714 /* special case for "erase a specific solvable": we also
2715 * erase all other solvables with that name, so that they
2716 * don't get picked up as replacement */
2717 /* XXX: look also at packages that obsolete this package? */
2718 name = pool->solvables[what].name;
2719 FOR_PROVIDES(p, pp, name)
2723 s = pool->solvables + p;
2724 if (s->name != name)
2726 /* keep other versions installed */
2727 if (s->repo == solv->installed)
2729 /* keep installcandidates of other jobs */
2730 if (MAPTST(&installcandidatemap, p))
2732 solver_addjobrule(solv, -p, 0, i, weak); /* remove by id */
2735 FOR_JOB_SELECT(p, pp, select, what)
2736 solver_addjobrule(solv, -p, 0, i, weak);
2740 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2743 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2745 case SOLVER_WEAKENDEPS:
2746 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2747 if (select != SOLVER_SOLVABLE)
2749 s = pool->solvables + what;
2750 weaken_solvable_deps(solv, what);
2752 case SOLVER_NOOBSOLETES:
2753 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2756 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2757 FOR_JOB_SELECT(p, pp, select, what)
2759 s = pool->solvables + p;
2760 solver_addjobrule(solv, installed && s->repo == installed ? p : -p, 0, i, weak);
2763 case SOLVER_DISTUPGRADE:
2764 POOL_DEBUG(SOLV_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
2766 case SOLVER_DROP_ORPHANED:
2767 POOL_DEBUG(SOLV_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
2768 if (select == SOLVER_SOLVABLE_ALL)
2769 solv->droporphanedmap_all = 1;
2770 FOR_JOB_SELECT(p, pp, select, what)
2772 s = pool->solvables + p;
2773 if (!installed || s->repo != installed)
2775 if (!solv->droporphanedmap.size)
2776 map_grow(&solv->droporphanedmap, installed->end - installed->start);
2777 MAPSET(&solv->droporphanedmap, p - installed->start);
2780 case SOLVER_USERINSTALLED:
2781 POOL_DEBUG(SOLV_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
2784 POOL_DEBUG(SOLV_DEBUG_JOB, "job: unknown job\n");
2792 IF_POOLDEBUG (SOLV_DEBUG_JOB)
2795 if (solv->nrules == oldnrules)
2796 POOL_DEBUG(SOLV_DEBUG_JOB, " - no rule created\n");
2797 for (j = oldnrules; j < solv->nrules; j++)
2799 POOL_DEBUG(SOLV_DEBUG_JOB, " - job ");
2800 solver_printrule(solv, SOLV_DEBUG_JOB, solv->rules + j);
2804 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
2805 solv->jobrules_end = solv->nrules;
2807 /* now create infarch and dup rules */
2808 if (!solv->noinfarchcheck)
2810 solver_addinfarchrules(solv, &addedmap);
2811 if (pool->obsoleteusescolors)
2813 /* currently doesn't work well with infarch rules, so make
2815 for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
2816 queue_push(&solv->weakruleq, i);
2820 solv->infarchrules = solv->infarchrules_end = solv->nrules;
2824 solver_addduprules(solv, &addedmap);
2825 solver_freedupmaps(solv); /* no longer needed */
2828 solv->duprules = solv->duprules_end = solv->nrules;
2831 solver_addchoicerules(solv);
2833 solv->choicerules = solv->choicerules_end = solv->nrules;
2835 /* all rules created
2836 * --------------------------------------------------------------
2837 * prepare for solving
2840 /* free unneeded memory */
2841 map_free(&addedmap);
2842 map_free(&installcandidatemap);
2845 POOL_DEBUG(SOLV_DEBUG_STATS, "%d rpm rules, %d job rules, %d infarch rules, %d dup rules, %d choice rules\n", solv->rpmrules_end - 1, solv->jobrules_end - solv->jobrules, solv->infarchrules_end - solv->infarchrules, solv->duprules_end - solv->duprules, solv->choicerules_end - solv->choicerules);
2847 /* create weak map */
2848 map_init(&solv->weakrulemap, solv->nrules);
2849 for (i = 0; i < solv->weakruleq.count; i++)
2851 p = solv->weakruleq.elements[i];
2852 MAPSET(&solv->weakrulemap, p);
2855 /* all new rules are learnt after this point */
2856 solv->learntrules = solv->nrules;
2858 /* create watches chains */
2861 /* create assertion index. it is only used to speed up
2862 * makeruledecsions() a bit */
2863 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
2864 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
2865 queue_push(&solv->ruleassertions, i);
2867 /* disable update rules that conflict with our job */
2868 solver_disablepolicyrules(solv);
2870 /* make initial decisions based on assertion rules */
2871 makeruledecisions(solv);
2872 POOL_DEBUG(SOLV_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
2875 * ********************************************
2877 * ********************************************
2880 now = solv_timems(0);
2881 solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
2882 POOL_DEBUG(SOLV_DEBUG_STATS, "solver took %d ms\n", solv_timems(now));
2885 * prepare solution queue if there were problems
2887 solver_prepare_solutions(solv);
2889 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);
2890 POOL_DEBUG(SOLV_DEBUG_STATS, "solver_solve took %d ms\n", solv_timems(solve_start));
2892 /* return number of problems */
2893 return solv->problems.count ? solv->problems.count / 2 : 0;
2897 solver_create_transaction(Solver *solv)
2899 return transaction_create_decisionq(solv->pool, &solv->decisionq, &solv->noobsoletes);
2902 void solver_get_orphaned(Solver *solv, Queue *orphanedq)
2904 queue_free(orphanedq);
2905 queue_init_clone(orphanedq, &solv->orphaned);
2908 void solver_get_recommendations(Solver *solv, Queue *recommendationsq, Queue *suggestionsq, int noselected)
2910 Pool *pool = solv->pool;
2911 Queue redoq, disabledq;
2917 if (!recommendationsq && !suggestionsq)
2920 map_init(&obsmap, pool->nsolvables);
2921 if (solv->installed)
2923 Id obs, *obsp, p, po, ppo;
2924 for (p = solv->installed->start; p < solv->installed->end; p++)
2926 s = pool->solvables + p;
2927 if (s->repo != solv->installed || !s->obsoletes)
2929 if (solv->decisionmap[p] <= 0)
2931 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
2933 obsp = s->repo->idarraydata + s->obsoletes;
2934 /* foreach obsoletes */
2935 while ((obs = *obsp++) != 0)
2936 FOR_PROVIDES(po, ppo, obs)
2937 MAPSET(&obsmap, po);
2942 queue_init(&disabledq);
2944 /* disable all erase jobs (including weak "keep uninstalled" rules) */
2945 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2947 if (r->d < 0) /* disabled ? */
2949 if (r->p >= 0) /* install job? */
2951 queue_push(&disabledq, i);
2952 solver_disablerule(solv, r);
2958 enabledisablelearntrules(solv);
2959 removedisabledconflicts(solv, &redoq);
2963 * find recommended packages
2965 if (recommendationsq)
2967 Id rec, *recp, p, pp;
2969 queue_empty(recommendationsq);
2970 /* create map of all recommened packages */
2971 solv->recommends_index = -1;
2972 MAPZERO(&solv->recommendsmap);
2974 /* put all packages the solver already chose in the map */
2975 if (solv->decisioncnt_weak)
2977 for (i = solv->decisioncnt_weak; i < solv->decisioncnt_orphan; i++)
2980 why = solv->decisionq_why.elements[i];
2982 continue; /* forced by unit rule */
2983 p = solv->decisionq.elements[i];
2986 MAPSET(&solv->recommendsmap, p);
2990 for (i = 0; i < solv->decisionq.count; i++)
2992 p = solv->decisionq.elements[i];
2995 s = pool->solvables + p;
2998 recp = s->repo->idarraydata + s->recommends;
2999 while ((rec = *recp++) != 0)
3001 FOR_PROVIDES(p, pp, rec)
3002 if (solv->decisionmap[p] > 0)
3008 FOR_PROVIDES(p, pp, rec)
3009 if (solv->decisionmap[p] > 0)
3010 MAPSET(&solv->recommendsmap, p);
3012 continue; /* p != 0: already fulfilled */
3014 FOR_PROVIDES(p, pp, rec)
3015 MAPSET(&solv->recommendsmap, p);
3019 for (i = 1; i < pool->nsolvables; i++)
3021 if (solv->decisionmap[i] < 0)
3023 if (solv->decisionmap[i] > 0 && noselected)
3025 if (MAPTST(&obsmap, i))
3027 s = pool->solvables + i;
3028 if (!MAPTST(&solv->recommendsmap, i))
3030 if (!s->supplements)
3032 if (!pool_installable(pool, s))
3034 if (!solver_is_supplementing(solv, s))
3037 queue_push(recommendationsq, i);
3039 /* we use MODE_SUGGEST here so that repo prio is ignored */
3040 policy_filter_unwanted(solv, recommendationsq, POLICY_MODE_SUGGEST);
3044 * find suggested packages
3049 Id sug, *sugp, p, pp;
3051 queue_empty(suggestionsq);
3052 /* create map of all suggests that are still open */
3053 solv->recommends_index = -1;
3054 MAPZERO(&solv->suggestsmap);
3055 for (i = 0; i < solv->decisionq.count; i++)
3057 p = solv->decisionq.elements[i];
3060 s = pool->solvables + p;
3063 sugp = s->repo->idarraydata + s->suggests;
3064 while ((sug = *sugp++) != 0)
3066 FOR_PROVIDES(p, pp, sug)
3067 if (solv->decisionmap[p] > 0)
3073 FOR_PROVIDES(p, pp, sug)
3074 if (solv->decisionmap[p] > 0)
3075 MAPSET(&solv->suggestsmap, p);
3077 continue; /* already fulfilled */
3079 FOR_PROVIDES(p, pp, sug)
3080 MAPSET(&solv->suggestsmap, p);
3084 for (i = 1; i < pool->nsolvables; i++)
3086 if (solv->decisionmap[i] < 0)
3088 if (solv->decisionmap[i] > 0 && noselected)
3090 if (MAPTST(&obsmap, i))
3092 s = pool->solvables + i;
3093 if (!MAPTST(&solv->suggestsmap, i))
3097 if (!pool_installable(pool, s))
3099 if (!solver_is_enhancing(solv, s))
3102 queue_push(suggestionsq, i);
3104 policy_filter_unwanted(solv, suggestionsq, POLICY_MODE_SUGGEST);
3107 /* undo removedisabledconflicts */
3109 undo_removedisabledconflicts(solv, &redoq);
3112 /* undo job rule disabling */
3113 for (i = 0; i < disabledq.count; i++)
3114 solver_enablerule(solv, solv->rules + disabledq.elements[i]);
3115 queue_free(&disabledq);
3120 /***********************************************************************/
3121 /* disk usage computations */
3123 /*-------------------------------------------------------------------
3125 * calculate DU changes
3129 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
3133 solver_create_state_maps(solv, &installedmap, 0);
3134 pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
3135 map_free(&installedmap);
3139 /*-------------------------------------------------------------------
3141 * calculate changes in install size
3145 solver_calc_installsizechange(Solver *solv)
3150 solver_create_state_maps(solv, &installedmap, 0);
3151 change = pool_calc_installsizechange(solv->pool, &installedmap);
3152 map_free(&installedmap);
3157 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
3159 pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
3163 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
3166 pool_create_state_maps(solv->pool, &solv->decisionq, &installedmap, 0);
3167 pool_trivial_installable_noobsoletesmap(solv->pool, &installedmap, pkgs, res, solv->noobsoletes.size ? &solv->noobsoletes : 0);
3168 map_free(&installedmap);
3171 /*-------------------------------------------------------------------
3173 * decision introspection
3177 solver_get_decisionlevel(Solver *solv, Id p)
3179 return solv->decisionmap[p];
3183 solver_get_decisionqueue(Solver *solv, Queue *decisionq)
3185 queue_free(decisionq);
3186 queue_init_clone(decisionq, &solv->decisionq);
3190 solver_get_lastdecisionblocklevel(Solver *solv)
3193 if (solv->decisionq.count == 0)
3195 p = solv->decisionq.elements[solv->decisionq.count - 1];
3198 return solv->decisionmap[p] < 0 ? -solv->decisionmap[p] : solv->decisionmap[p];
3202 solver_get_decisionblock(Solver *solv, int level, Queue *decisionq)
3207 queue_empty(decisionq);
3208 for (i = 0; i < solv->decisionq.count; i++)
3210 p = solv->decisionq.elements[i];
3213 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3216 if (i == solv->decisionq.count)
3218 for (i = 0; i < solv->decisionq.count; i++)
3220 p = solv->decisionq.elements[i];
3223 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3224 queue_push(decisionq, p);
3231 solver_describe_decision(Solver *solv, Id p, Id *infop)
3238 if (!solv->decisionmap[p])
3239 return SOLVER_REASON_UNRELATED;
3240 pp = solv->decisionmap[p] < 0 ? -p : p;
3241 for (i = 0; i < solv->decisionq.count; i++)
3242 if (solv->decisionq.elements[i] == pp)
3244 if (i == solv->decisionq.count) /* just in case... */
3245 return SOLVER_REASON_UNRELATED;
3246 why = solv->decisionq_why.elements[i];
3251 return SOLVER_REASON_UNIT_RULE;
3254 if (i < solv->decisioncnt_update)
3259 *infop = SYSTEMSOLVABLE;
3260 return SOLVER_REASON_KEEP_INSTALLED;
3264 return SOLVER_REASON_RESOLVE_JOB;
3266 if (i < solv->decisioncnt_keep)
3268 if (why == 0 && pp < 0)
3269 return SOLVER_REASON_CLEANDEPS_ERASE;
3272 if (why >= solv->updaterules && why < solv->updaterules_end)
3273 *infop = why - solv->updaterules;
3274 else if (why >= solv->featurerules && why < solv->featurerules_end)
3275 *infop = why - solv->featurerules;
3277 return SOLVER_REASON_UPDATE_INSTALLED;
3279 if (i < solv->decisioncnt_resolve)
3281 if (why == 0 && pp < 0)
3282 return SOLVER_REASON_CLEANDEPS_ERASE;
3285 if (why >= solv->updaterules && why < solv->updaterules_end)
3286 *infop = why - solv->updaterules;
3287 else if (why >= solv->featurerules && why < solv->featurerules_end)
3288 *infop = why - solv->featurerules;
3290 return SOLVER_REASON_KEEP_INSTALLED;
3292 if (i < solv->decisioncnt_weak)
3296 return SOLVER_REASON_RESOLVE;
3298 if (solv->decisionq.count < solv->decisioncnt_orphan)
3299 return SOLVER_REASON_WEAKDEP;
3300 return SOLVER_REASON_RESOLVE_ORPHAN;
3305 solver_describe_weakdep_decision(Solver *solv, Id p, Queue *whyq)
3307 Pool *pool = solv->pool;
3309 int level = solv->decisionmap[p];
3316 for (decisionno = 0; decisionno < solv->decisionq.count; decisionno++)
3317 if (solv->decisionq.elements[decisionno] == p)
3319 if (decisionno == solv->decisionq.count)
3321 if (decisionno < solv->decisioncnt_weak || decisionno >= solv->decisioncnt_orphan)
3324 /* 1) list all packages that recommend us */
3325 for (i = 1; i < pool->nsolvables; i++)
3327 Id *recp, rec, pp2, p2;
3328 if (solv->decisionmap[i] < 0 || solv->decisionmap[i] >= level)
3330 s = pool->solvables + i;
3333 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
3335 recp = s->repo->idarraydata + s->recommends;
3336 while ((rec = *recp++) != 0)
3339 FOR_PROVIDES(p2, pp2, rec)
3345 /* if p2 is already installed, this recommends is ignored */
3346 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3352 queue_push(whyq, SOLVER_REASON_RECOMMENDED);
3353 queue_push2(whyq, p2, rec);
3357 /* 2) list all supplements */
3358 s = pool->solvables + p;
3359 if (s->supplements && level > 0)
3361 Id *supp, sup, pp2, p2;
3362 /* this is a hack. to use solver_dep_fulfilled we temporarily clear
3363 * everything above our level in the decisionmap */
3364 for (i = decisionno; i < solv->decisionq.count; i++ )
3366 p2 = solv->decisionq.elements[i];
3368 solv->decisionmap[p2] = -solv->decisionmap[p2];
3370 supp = s->repo->idarraydata + s->supplements;
3371 while ((sup = *supp++) != 0)
3372 if (solver_dep_fulfilled(solv, sup))
3375 /* let's see if this is an easy supp */
3376 FOR_PROVIDES(p2, pp2, sup)
3378 if (solv->ignorealreadyrecommended && solv->installed)
3380 if (pool->solvables[p2].repo == solv->installed)
3383 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3385 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3386 queue_push2(whyq, p2, sup);
3392 /* hard case, just note dependency with no package */
3393 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3394 queue_push2(whyq, 0, sup);
3397 for (i = decisionno; i < solv->decisionq.count; i++)
3399 p2 = solv->decisionq.elements[i];
3401 solv->decisionmap[p2] = -solv->decisionmap[p2];