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
26 #include "solverdebug.h"
28 #define CODE10 0 /* set to '1' to enable patch atoms */
30 #define RULES_BLOCK 63
32 /********************************************************************
34 * dependency check helpers
38 /*-------------------------------------------------------------------
39 * handle split provides
43 solver_splitprovides(Solver *solv, Id dep)
45 Pool *pool = solv->pool;
50 if (!solv->dosplitprovides || !solv->installed)
54 rd = GETRELDEP(pool, dep);
55 if (rd->flags != REL_WITH)
57 FOR_PROVIDES(p, pp, dep)
59 s = pool->solvables + p;
60 if (s->repo == solv->installed && s->name == rd->name)
67 /*-------------------------------------------------------------------
68 * solver_dep_installed
72 solver_dep_installed(Solver *solv, Id dep)
75 Pool *pool = solv->pool;
80 Reldep *rd = GETRELDEP(pool, dep);
81 if (rd->flags == REL_AND)
83 if (!solver_dep_installed(solv, rd->name))
85 return solver_dep_installed(solv, rd->evr);
87 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
88 return solver_dep_installed(solv, rd->evr);
90 FOR_PROVIDES(p, pp, dep)
92 if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
100 /*-------------------------------------------------------------------
101 * Check if dependenc is possible
103 * this mirrors solver_dep_fulfilled
104 * but uses map m instead of the decisionmap
108 dep_possible(Solver *solv, Id dep, Map *m)
110 Pool *pool = solv->pool;
115 Reldep *rd = GETRELDEP(pool, dep);
116 if (rd->flags == REL_AND)
118 if (!dep_possible(solv, rd->name, m))
120 return dep_possible(solv, rd->evr, m);
122 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
123 return solver_splitprovides(solv, rd->evr);
124 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
125 return solver_dep_installed(solv, rd->evr);
127 FOR_PROVIDES(p, pp, dep)
135 /********************************************************************
139 * - unify rules, remove duplicates
142 static Pool *unifyrules_sortcmp_data;
144 /*-------------------------------------------------------------------
146 * compare rules for unification sort
151 unifyrules_sortcmp(const void *ap, const void *bp)
153 Pool *pool = unifyrules_sortcmp_data;
154 Rule *a = (Rule *)ap;
155 Rule *b = (Rule *)bp;
161 return x; /* p differs */
164 if (a->d == 0 && b->d == 0)
165 return a->w2 - b->w2; /* assertion: return w2 diff */
167 if (a->d == 0) /* a is assertion, b not */
169 x = a->w2 - pool->whatprovidesdata[b->d];
173 if (b->d == 0) /* b is assertion, a not */
175 x = pool->whatprovidesdata[a->d] - b->w2;
179 /* compare whatprovidesdata */
180 ad = pool->whatprovidesdata + a->d;
181 bd = pool->whatprovidesdata + b->d;
183 if ((x = *ad++ - *bd++) != 0)
189 /*-------------------------------------------------------------------
192 * go over all rules and remove duplicates
196 unifyrules(Solver *solv)
198 Pool *pool = solv->pool;
202 if (solv->nrules <= 1) /* nothing to unify */
205 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules -----\n");
207 /* sort rules first */
208 unifyrules_sortcmp_data = solv->pool;
209 qsort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp);
216 for (i = j = 1, ir = solv->rules + i; i < solv->nrules; i++, ir++)
218 if (jr && !unifyrules_sortcmp(ir, jr))
219 continue; /* prune! */
220 jr = solv->rules + j++; /* keep! */
225 /* reduced count from nrules to j rules */
226 POOL_DEBUG(SAT_DEBUG_STATS, "pruned rules from %d to %d\n", solv->nrules, j);
228 /* adapt rule buffer */
230 solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
234 IF_POOLDEBUG (SAT_DEBUG_STATS)
241 for (i = 1; i < solv->nrules; i++)
248 dp = solv->pool->whatprovidesdata + r->d;
253 POOL_DEBUG(SAT_DEBUG_STATS, " binary: %d\n", binr);
254 POOL_DEBUG(SAT_DEBUG_STATS, " normal: %d, %d literals\n", solv->nrules - 1 - binr, lits);
256 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules end -----\n");
266 hashrule(Solver *solv, Id p, Id d, int n)
268 unsigned int x = (unsigned int)p;
272 return (x * 37) ^ (unsigned int)d;
273 dp = solv->pool->whatprovidesdata + d;
275 x = (x * 37) ^ (unsigned int)*dp++;
281 /*-------------------------------------------------------------------
287 * p = direct literal; always < 0 for installed rpm rules
288 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
291 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
293 * p < 0 : pkg id of A
294 * d > 0 : Offset in whatprovidesdata (list of providers of b)
296 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
297 * p < 0 : pkg id of A
298 * d < 0 : Id of solvable (e.g. B1)
300 * d == 0: unary rule, assertion => (A) or (-A)
302 * Install: p > 0, d = 0 (A) user requested install
303 * Remove: p < 0, d = 0 (-A) user requested remove
304 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
305 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
306 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider) (binary rule)
307 * ? p > 0, d < 0 (A|-B)
308 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
312 * Direct assertion (no watch needed)( if d <0 ) --> d = 0, w1 = p, w2 = 0
313 * Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
314 * every other : w1 = p, w2 = whatprovidesdata[d];
315 * Disabled rule: w1 = 0
317 * always returns a rule for non-rpm rules
321 addrule(Solver *solv, Id p, Id d)
323 Pool *pool = solv->pool;
327 int n = 0; /* number of literals in rule - 1
328 0 = direct assertion (single literal)
333 /* it often happenes that requires lead to adding the same rpm rule
334 * multiple times, so we prune those duplicates right away to make
335 * the work for unifyrules a bit easier */
337 if (solv->nrules /* we already have rules */
338 && !solv->rpmrules_end) /* but are not done with rpm rules */
340 r = solv->rules + solv->nrules - 1; /* get the last added rule */
341 if (r->p == p && r->d == d && d != 0) /* identical and not user requested */
346 * compute number of literals (n) in rule
351 /* always a binary rule */
353 return 0; /* ignore self conflict */
358 for (dp = pool->whatprovidesdata + d; *dp; dp++, n++)
360 return 0; /* rule is self-fulfilling */
362 if (n == 1) /* have single provider */
363 d = dp[-1]; /* take single literal */
367 if (n == 0 && !solv->rpmrules_end)
369 /* this is a rpm rule assertion, we do not have to allocate it */
370 /* it can be identified by a level of 1 and a zero reason */
371 /* we must not drop those rules from the decisionq when rewinding! */
373 assert(solv->decisionmap[-p] == 0 || solv->decisionmap[-p] == -1);
374 if (solv->decisionmap[-p])
375 return 0; /* already got that one */
376 queue_push(&solv->decisionq, p);
377 queue_push(&solv->decisionq_why, 0);
378 solv->decisionmap[-p] = -1;
383 if (n == 1 && p > d && !solv->rpmrules_end)
385 /* smallest literal first so we can find dups */
386 n = p; p = d; d = n; /* p <-> d */
387 n = 1; /* re-set n, was used as temp var */
391 * check for duplicate
394 /* check if the last added rule (r) is exactly the same as what we're looking for. */
395 if (r && n == 1 && !r->d && r->p == p && r->w2 == d)
396 return r; /* binary rule */
398 /* have n-ary rule with same first literal, check other literals */
399 if (r && n > 1 && r->d && r->p == p)
401 /* Rule where d is an offset in whatprovidesdata */
405 dp2 = pool->whatprovidesdata + r->d;
406 for (dp = pool->whatprovidesdata + d; *dp; dp++, dp2++)
417 /* extend rule buffer */
418 solv->rules = sat_extend(solv->rules, solv->nrules, 1, sizeof(Rule), RULES_BLOCK);
419 r = solv->rules + solv->nrules++; /* point to rule space */
428 /* direct assertion, no watch needed */
444 r->w2 = pool->whatprovidesdata[d];
449 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
451 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " Add rule: ");
452 solver_printrule(solv, SAT_DEBUG_RULE_CREATION, r);
458 /*-------------------------------------------------------------------
463 disablerule(Solver *solv, Rule *r)
469 /*-------------------------------------------------------------------
474 enablerule(Solver *solv, Rule *r)
481 /**********************************************************************************/
483 /* a problem is an item on the solver's problem list. It can either be >0, in that
484 * case it is a update rule, or it can be <0, which makes it refer to a job
485 * consisting of multiple job rules.
489 disableproblem(Solver *solv, Id v)
497 disablerule(solv, solv->rules + v);
501 jp = solv->ruletojob.elements;
502 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++, jp++)
504 disablerule(solv, r);
507 /*-------------------------------------------------------------------
512 enableproblem(Solver *solv, Id v)
520 if (v >= solv->featurerules && v < solv->featurerules_end)
522 /* do not enable feature rule if update rule is enabled */
523 r = solv->rules + (v - solv->featurerules + solv->updaterules);
527 enablerule(solv, solv->rules + v);
528 if (v >= solv->updaterules && v < solv->updaterules_end)
530 /* disable feature rule when enabling update rule */
531 r = solv->rules + (v - solv->updaterules + solv->featurerules);
533 disablerule(solv, r);
538 jp = solv->ruletojob.elements;
539 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++, jp++)
545 /************************************************************************/
548 * make assertion rules into decisions
550 * go through update and job rules and add direct assertions
551 * to the decisionqueue. If we find a conflict, disable rules and
552 * add them to problem queue.
556 makeruledecisions(Solver *solv)
558 Pool *pool = solv->pool;
564 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions ; size decisionq: %d -----\n",solv->decisionq.count);
566 decisionstart = solv->decisionq.count;
567 for (ii = 0; ii < solv->ruleassertions.count; ii++)
569 ri = solv->ruleassertions.elements[ii];
570 r = solv->rules + ri;
572 if (r->d < 0 || !r->p || r->w2) /* disabled, dummy or no assertion */
574 /* do weak rules in phase 2 */
575 if (ri < solv->learntrules && MAPTST(&solv->weakrulemap, ri))
581 if (!solv->decisionmap[vv]) /* if not yet decided */
586 queue_push(&solv->decisionq, v);
587 queue_push(&solv->decisionq_why, r - solv->rules);
588 solv->decisionmap[vv] = v > 0 ? 1 : -1;
589 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
591 Solvable *s = solv->pool->solvables + vv;
593 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", solvable2str(solv->pool, s));
595 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (assertion)\n", solvable2str(solv->pool, s));
600 * check previous decision: is it sane ?
603 if (v > 0 && solv->decisionmap[vv] > 0) /* ok to install */
605 if (v < 0 && solv->decisionmap[vv] < 0) /* ok to remove */
611 * The rule (r) we're currently processing says something
612 * different (v = r->p) than a previous decision (decisionmap[abs(v)])
616 if (ri >= solv->learntrules)
618 /* conflict with a learnt rule */
619 /* can happen when packages cannot be installed for
620 * multiple reasons. */
621 /* we disable the learnt rule in this case */
622 disablerule(solv, r);
627 * find the decision which is the "opposite" of the rule
630 for (i = 0; i < solv->decisionq.count; i++)
631 if (solv->decisionq.elements[i] == -v)
633 assert(i < solv->decisionq.count); /* assert that we found it */
636 * conflict with system solvable ?
639 if (v == -SYSTEMSOLVABLE) {
640 /* conflict with system solvable */
641 queue_push(&solv->problems, solv->learnt_pool.count);
642 queue_push(&solv->learnt_pool, ri);
643 queue_push(&solv->learnt_pool, 0);
644 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
645 if (ri >= solv->jobrules && ri < solv->jobrules_end)
646 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
649 queue_push(&solv->problems, v);
650 queue_push(&solv->problems, 0);
651 disableproblem(solv, v);
655 assert(solv->decisionq_why.elements[i]);
658 * conflict with an rpm rule ?
661 if (solv->decisionq_why.elements[i] < solv->rpmrules_end)
663 /* conflict with rpm rule assertion */
664 queue_push(&solv->problems, solv->learnt_pool.count);
665 queue_push(&solv->learnt_pool, ri);
666 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
667 queue_push(&solv->learnt_pool, 0);
668 assert(v > 0 || v == -SYSTEMSOLVABLE);
669 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
670 if (ri >= solv->jobrules && ri < solv->jobrules_end)
671 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
674 queue_push(&solv->problems, v);
675 queue_push(&solv->problems, 0);
676 disableproblem(solv, v);
681 * conflict with another job or update/feature rule
685 queue_push(&solv->problems, solv->learnt_pool.count);
686 queue_push(&solv->learnt_pool, ri);
687 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
688 queue_push(&solv->learnt_pool, 0);
690 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflicting update/job assertions over literal %d\n", vv);
693 * push all of our rules (can only be feature or job rules)
694 * asserting this literal on the problem stack
697 for (i = solv->featurerules, rr = solv->rules + i; i < solv->learntrules; i++, rr++)
699 if (rr->d < 0 /* disabled */
700 || rr->w2) /* or no assertion */
702 if (rr->p != vv /* not affecting the literal */
705 if (MAPTST(&solv->weakrulemap, i)) /* weak: silently ignore */
708 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
710 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, solv->rules + i);
713 /* is is a job rule ? */
714 if (i >= solv->jobrules && i < solv->jobrules_end)
715 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
717 queue_push(&solv->problems, v);
718 disableproblem(solv, v);
720 queue_push(&solv->problems, 0);
724 * (back up from decisions)
726 while (solv->decisionq.count > decisionstart)
728 v = solv->decisionq.elements[--solv->decisionq.count];
729 --solv->decisionq_why.count;
731 solv->decisionmap[vv] = 0;
733 ii = -1; /* restarts loop at 0 */
737 * phase 2: now do the weak assertions
739 for (ii = 0; ii < solv->ruleassertions.count; ii++)
741 ri = solv->ruleassertions.elements[ii];
742 r = solv->rules + ri;
743 if (r->d < 0 || r->w2) /* disabled or no assertion */
745 if (!MAPTST(&solv->weakrulemap, ri)) /* skip non-weak */
751 * (if not yet decided)
753 if (!solv->decisionmap[vv])
755 queue_push(&solv->decisionq, v);
756 queue_push(&solv->decisionq_why, r - solv->rules);
757 solv->decisionmap[vv] = v > 0 ? 1 : -1;
758 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
760 Solvable *s = solv->pool->solvables + vv;
762 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", solvable2str(solv->pool, s));
764 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (weak assertion)\n", solvable2str(solv->pool, s));
769 * previously decided, sane ?
771 if (v > 0 && solv->decisionmap[vv] > 0)
773 if (v < 0 && solv->decisionmap[vv] < 0)
776 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
777 solver_printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
778 disablerule(solv, r);
781 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions end; size decisionq: %d -----\n",solv->decisionq.count);
785 /*-------------------------------------------------------------------
786 * enable/disable learnt rules
788 * we have enabled or disabled some of our rules. We now reenable all
789 * of our learnt rules but the ones that were learnt from rules that
793 enabledisablelearntrules(Solver *solv)
795 Pool *pool = solv->pool;
800 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
801 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
803 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
804 while ((why = *whyp++) != 0)
806 assert(why > 0 && why < i);
807 if (solv->rules[why].d < 0)
810 /* why != 0: we found a disabled rule, disable the learnt rule */
811 if (why && r->d >= 0)
813 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
815 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "disabling ");
816 solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
818 disablerule(solv, r);
820 else if (!why && r->d < 0)
822 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
824 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "re-enabling ");
825 solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
833 /*-------------------------------------------------------------------
836 * Enable all rules, except learnt rules, which are
837 * - disabled and weak (set in weakrulemap)
842 enableweakrules(Solver *solv)
847 for (i = 1, r = solv->rules + i; i < solv->learntrules; i++, r++)
849 if (r->d >= 0) /* skip non-direct literals */
851 if (!MAPTST(&solv->weakrulemap, i))
858 /* FIXME: bad code ahead, replace as soon as possible */
859 /* FIXME: should probably look at SOLVER_INSTALL|SOLVABLE_ONE_OF */
861 /*-------------------------------------------------------------------
862 * disable update rules
866 disableupdaterules(Solver *solv, Queue *job, int jobidx)
868 Pool *pool = solv->pool;
870 Id how, select, what, p, *pp;
876 installed = solv->installed;
882 how = job->elements[jobidx];
883 select = how & SOLVER_SELECTMASK;
884 switch (how & SOLVER_JOBMASK)
889 if (select != SOLVER_SOLVABLE)
896 /* go through all enabled job rules */
897 MAPZERO(&solv->noupdate);
898 for (i = solv->jobrules; i < solv->jobrules_end; i++)
901 if (r->d < 0) /* disabled? */
903 j = solv->ruletojob.elements[i - solv->jobrules];
907 how = job->elements[j];
908 what = job->elements[j + 1];
909 select = how & SOLVER_SELECTMASK;
910 switch (how & SOLVER_JOBMASK)
913 if (select != SOLVER_SOLVABLE)
915 s = pool->solvables + what;
916 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, what))
918 if (s->repo == installed)
923 obsp = s->repo->idarraydata + s->obsoletes;
924 while ((obs = *obsp++) != 0)
925 FOR_PROVIDES(p, pp, obs)
927 if (pool->solvables[p].repo != installed)
929 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
931 MAPSET(&solv->noupdate, p - installed->start);
934 FOR_PROVIDES(p, pp, s->name)
936 if (!solv->implicitobsoleteusesprovides && pool->solvables[p].name != s->name)
938 if (pool->solvables[p].repo == installed)
939 MAPSET(&solv->noupdate, p - installed->start);
943 FOR_JOB_SELECT(p, pp, select, what)
944 if (pool->solvables[p].repo == installed)
945 MAPSET(&solv->noupdate, p - installed->start);
952 /* fixup update rule status */
955 /* we just disabled job #jobidx. enable all update rules
956 * that aren't disabled by the remaining job rules */
957 how = job->elements[jobidx];
958 what = job->elements[jobidx + 1];
959 select = how & SOLVER_SELECTMASK;
960 switch (how & SOLVER_JOBMASK)
963 if (select != SOLVER_SOLVABLE)
965 s = pool->solvables + what;
966 if (s->repo == installed)
971 obsp = s->repo->idarraydata + s->obsoletes;
972 while ((obs = *obsp++) != 0)
973 FOR_PROVIDES(p, pp, obs)
975 if (pool->solvables[p].repo != installed)
977 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
979 if (MAPTST(&solv->noupdate, p - installed->start))
981 r = solv->rules + solv->updaterules + (p - installed->start);
985 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
987 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
988 solver_printrule(solv, SAT_DEBUG_SOLUTIONS, r);
992 FOR_PROVIDES(p, pp, s->name)
994 if (!solv->implicitobsoleteusesprovides && pool->solvables[p].name != s->name)
996 if (pool->solvables[p].repo != installed)
998 if (MAPTST(&solv->noupdate, p - installed->start))
1000 r = solv->rules + solv->updaterules + (p - installed->start);
1003 enablerule(solv, r);
1004 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
1006 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1007 solver_printrule(solv, SAT_DEBUG_SOLUTIONS, r);
1012 FOR_JOB_SELECT(p, pp, select, what)
1014 if (pool->solvables[p].repo != installed)
1016 if (MAPTST(&solv->noupdate, p - installed->start))
1018 r = solv->rules + solv->updaterules + (p - installed->start);
1021 enablerule(solv, r);
1022 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
1024 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1025 solver_printrule(solv, SAT_DEBUG_SOLUTIONS, r);
1035 for (i = 0; i < installed->nsolvables; i++)
1037 r = solv->rules + solv->updaterules + i;
1038 if (r->d >= 0 && MAPTST(&solv->noupdate, i))
1039 disablerule(solv, r); /* was enabled, need to disable */
1040 r = solv->rules + solv->featurerules + i;
1041 if (r->d >= 0 && MAPTST(&solv->noupdate, i))
1042 disablerule(solv, r); /* was enabled, need to disable */
1048 * special multiversion patch conflict handling:
1049 * a patch conflict is also satisfied, if some other
1050 * version with the same name/arch that doesn't conflict
1051 * get's installed. The generated rule is thus:
1052 * -patch|-cpack|opack1|opack2|...
1055 makemultiversionconflict(Solver *solv, Id n, Id con)
1057 Pool *pool = solv->pool;
1060 Id p, *pp, qbuf[64];
1062 sn = pool->solvables + n;
1063 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
1065 FOR_PROVIDES(p, pp, sn->name)
1067 if (s->name != sn->name || s->arch != sn->arch)
1069 if (!MAPTST(&solv->noobsoletes, p))
1071 if (pool_match_nevr(pool, pool->solvables + p, con))
1073 /* here we have a multiversion solvable that doesn't conflict */
1074 /* thus we're not in conflict if it is installed */
1078 return -n; /* no other package found, generate normal conflict */
1079 return pool_queuetowhatprovides(pool, &q);
1083 /*-------------------------------------------------------------------
1085 * add (install) rules for solvable
1087 * s: Solvable for which to add rules
1088 * m: m[s] = 1 for solvables which have rules, prevent rule duplication
1090 * Algorithm: 'visit all nodes of a graph'. The graph nodes are
1091 * solvables, the edges their dependencies.
1092 * Starting from an installed solvable, this will create all rules
1093 * representing the graph created by the solvables dependencies.
1095 * for unfulfilled requirements, conflicts, obsoletes,....
1096 * add a negative assertion for solvables that are not installable
1098 * It will also create rules for all solvables referenced by 's'
1099 * i.e. descend to all providers of requirements of 's'
1104 addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
1106 Pool *pool = solv->pool;
1107 Repo *installed = solv->installed;
1109 /* 'work' queue. keeps Ids of solvables we still have to work on.
1110 And buffer for it. */
1115 /* if to add rules for broken deps ('rpm -V' functionality)
1122 /* Id var and pointer for each dependency
1123 * (not used in parallel)
1130 /* var and ptr for loops */
1132 /* ptr to 'whatprovides' */
1134 /* Id for current solvable 's' */
1137 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable -----\n");
1139 queue_init_buffer(&workq, workqbuf, sizeof(workqbuf)/sizeof(*workqbuf));
1140 queue_push(&workq, s - pool->solvables); /* push solvable Id to work queue */
1142 /* loop until there's no more work left */
1147 * s: Pointer to solvable
1150 n = queue_shift(&workq); /* 'pop' next solvable to work on from queue */
1151 if (MAPTST(m, n)) /* continue if already visited */
1154 MAPSET(m, n); /* mark as visited */
1155 s = pool->solvables + n; /* s = Solvable in question */
1158 if (installed /* Installed system available */
1159 && !solv->fixsystem /* NOT repair errors in rpm dependency graph */
1160 && s->repo == installed) /* solvable is installed? */
1162 dontfix = 1; /* dont care about broken rpm deps */
1166 && s->arch != ARCH_SRC
1167 && s->arch != ARCH_NOSRC
1168 && !pool_installable(pool, s))
1170 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", solvable2str(pool, s), (Id)(s - pool->solvables));
1171 addrule(solv, -n, 0); /* uninstallable */
1174 /*-----------------------------------------
1175 * check requires of s
1180 reqp = s->repo->idarraydata + s->requires;
1181 while ((req = *reqp++) != 0) /* go through all requires */
1183 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
1186 /* find list of solvables providing 'req' */
1187 dp = pool_whatprovides(pool, req);
1189 if (*dp == SYSTEMSOLVABLE) /* always installed */
1195 addpatchatomrequires(solv, s, dp, &workq, m);
1201 /* the strategy here is to not insist on dependencies
1202 * that are already broken. so if we find one provider
1203 * that was already installed, we know that the
1204 * dependency was not broken before so we enforce it */
1206 /* check if any of the providers for 'req' is installed */
1207 for (i = 0; (p = dp[i]) != 0; i++)
1209 if (pool->solvables[p].repo == installed)
1210 break; /* provider was installed */
1212 /* didn't find an installed provider: previously broken dependency */
1215 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", dep2str(pool, req), solvable2str(pool, s));
1222 /* nothing provides req! */
1223 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", solvable2str(pool, s), (Id)(s - pool->solvables), dep2str(pool, req));
1224 addrule(solv, -n, 0); /* mark requestor as uninstallable */
1228 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1230 POOL_DEBUG(SAT_DEBUG_RULE_CREATION," %s requires %s\n", solvable2str(pool, s), dep2str(pool, req));
1231 for (i = 0; dp[i]; i++)
1232 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " provided by %s\n", solvable2str(pool, pool->solvables + dp[i]));
1235 /* add 'requires' dependency */
1236 /* rule: (-requestor|provider1|provider2|...|providerN) */
1237 addrule(solv, -n, dp - pool->whatprovidesdata);
1239 /* descend the dependency tree
1240 push all non-visited providers on the work queue */
1243 if (!MAPTST(m, *dp))
1244 queue_push(&workq, *dp);
1247 } /* while, requirements of n */
1249 } /* if, requirements */
1251 /* that's all we check for src packages */
1252 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
1255 /*-----------------------------------------
1256 * check conflicts of s
1263 /* we treat conflicts in patches a bit differen:
1265 * - multiversion handling
1267 if (!strncmp("patch:", id2str(pool, s->name), 6))
1269 conp = s->repo->idarraydata + s->conflicts;
1270 /* foreach conflicts of 's' */
1271 while ((con = *conp++) != 0)
1273 /* foreach providers of a conflict of 's' */
1274 FOR_PROVIDES(p, pp, con)
1276 if (ispatch && !pool_match_nevr(pool, pool->solvables + p, con))
1278 /* dontfix: dont care about conflicts with already installed packs */
1279 if (dontfix && pool->solvables[p].repo == installed)
1281 /* p == n: self conflict */
1282 if (p == n && !solv->allowselfconflicts)
1283 p = 0; /* make it a negative assertion, aka 'uninstallable' */
1284 if (p && ispatch && solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p) && ISRELDEP(con))
1286 /* our patch conflicts with a noobsoletes (aka multiversion) package */
1287 p = -makemultiversionconflict(solv, p, con);
1289 /* rule: -n|-p: either solvable _or_ provider of conflict */
1290 addrule(solv, -n, -p);
1295 /*-----------------------------------------
1296 * check obsoletes if not installed
1297 * (only installation will trigger the obsoletes in rpm)
1299 if (!installed || pool->solvables[n].repo != installed)
1300 { /* not installed */
1301 int noobs = solv->noobsoletes.size && MAPTST(&solv->noobsoletes, n);
1302 if (s->obsoletes && !noobs)
1304 obsp = s->repo->idarraydata + s->obsoletes;
1305 /* foreach obsoletes */
1306 while ((obs = *obsp++) != 0)
1308 /* foreach provider of an obsoletes of 's' */
1309 FOR_PROVIDES(p, pp, obs)
1311 if (!solv->obsoleteusesprovides /* obsoletes are matched names, not provides */
1312 && !pool_match_nevr(pool, pool->solvables + p, obs))
1314 addrule(solv, -n, -p);
1318 FOR_PROVIDES(p, pp, s->name)
1320 Solvable *ps = pool->solvables + p;
1321 /* we still obsolete packages with same nevra, like rpm does */
1322 /* (actually, rpm mixes those packages. yuck...) */
1323 if (noobs && (s->name != ps->name || s->evr != ps->evr || s->arch != ps->arch))
1325 if (!solv->implicitobsoleteusesprovides && s->name != ps->name)
1327 addrule(solv, -n, -p);
1331 /*-----------------------------------------
1332 * add recommends to the work queue
1336 recp = s->repo->idarraydata + s->recommends;
1337 while ((rec = *recp++) != 0)
1339 FOR_PROVIDES(p, pp, rec)
1341 queue_push(&workq, p);
1346 sugp = s->repo->idarraydata + s->suggests;
1347 while ((sug = *sugp++) != 0)
1349 FOR_PROVIDES(p, pp, sug)
1351 queue_push(&workq, p);
1356 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable end -----\n");
1360 /*-------------------------------------------------------------------
1362 * Add package rules for weak rules
1364 * m: visited solvables
1368 addrpmrulesforweak(Solver *solv, Map *m)
1370 Pool *pool = solv->pool;
1375 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak -----\n");
1376 /* foreach solvable in pool */
1377 for (i = n = 1; n < pool->nsolvables; i++, n++)
1379 if (i == pool->nsolvables) /* wrap i */
1381 if (MAPTST(m, i)) /* been there */
1384 s = pool->solvables + i;
1385 if (!pool_installable(pool, s)) /* only look at installable ones */
1391 /* find possible supplements */
1392 supp = s->repo->idarraydata + s->supplements;
1393 while ((sup = *supp++) != ID_NULL)
1394 if (dep_possible(solv, sup, m))
1398 /* if nothing found, check for enhances */
1399 if (!sup && s->enhances)
1401 supp = s->repo->idarraydata + s->enhances;
1402 while ((sup = *supp++) != ID_NULL)
1403 if (dep_possible(solv, sup, m))
1406 /* if nothing found, goto next solvables */
1409 addrpmrulesforsolvable(solv, s, m);
1412 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak end -----\n");
1416 /*-------------------------------------------------------------------
1418 * add package rules for possible updates
1421 * m: map of already visited solvables
1422 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
1426 addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allow_all)
1428 Pool *pool = solv->pool;
1430 /* queue and buffer for it */
1434 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1436 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1437 /* find update candidates for 's' */
1438 policy_findupdatepackages(solv, s, &qs, allow_all);
1439 /* add rule for 's' if not already done */
1440 if (!MAPTST(m, s - pool->solvables))
1441 addrpmrulesforsolvable(solv, s, m);
1442 /* foreach update candidate, add rule if not already done */
1443 for (i = 0; i < qs.count; i++)
1444 if (!MAPTST(m, qs.elements[i]))
1445 addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
1448 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1452 finddistupgradepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
1454 Pool *pool = solv->pool;
1457 policy_findupdatepackages(solv, s, qs, allow_all);
1462 policy_findupdatepackages(solv, s, qs, 1);
1466 return -SYSTEMSOLVABLE;
1469 return s - pool->solvables;
1470 /* check if it is ok to keep the installed package */
1471 for (i = 0; i < qs->count; i++)
1473 Solvable *ns = pool->solvables + qs->elements[i];
1474 if (s->evr == ns->evr && solvable_identical(pool, s, ns))
1475 return s - pool->solvables;
1477 /* nope, it must be some other package */
1478 return queue_shift(qs);
1481 /*-------------------------------------------------------------------
1483 * add rule for update
1484 * (A|A1|A2|A3...) An = update candidates for A
1486 * s = (installed) solvable
1490 addupdaterule(Solver *solv, Solvable *s, int allow_all)
1492 /* installed packages get a special upgrade allowed rule */
1493 Pool *pool = solv->pool;
1498 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule -----\n");
1499 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1500 p = s - pool->solvables;
1501 /* find update candidates for 's' */
1502 if (solv->distupgrade)
1503 p = finddistupgradepackages(solv, s, &qs, allow_all);
1505 policy_findupdatepackages(solv, s, &qs, allow_all);
1506 d = qs.count ? pool_queuetowhatprovides(pool, &qs) : 0;
1508 addrule(solv, p, d); /* allow update of s */
1509 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule end -----\n");
1513 /********************************************************************/
1517 /*-------------------------------------------------------------------
1520 * initial setup for all watches
1524 makewatches(Solver *solv)
1528 int nsolvables = solv->pool->nsolvables;
1530 sat_free(solv->watches);
1531 /* lower half for removals, upper half for installs */
1532 solv->watches = sat_calloc(2 * nsolvables, sizeof(Id));
1534 /* do it reverse so rpm rules get triggered first (XXX: obsolete?) */
1535 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1537 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1540 if (!r->w2) /* assertions do not need watches */
1543 /* see addwatches_rule(solv, r) */
1544 r->n1 = solv->watches[nsolvables + r->w1];
1545 solv->watches[nsolvables + r->w1] = r - solv->rules;
1547 r->n2 = solv->watches[nsolvables + r->w2];
1548 solv->watches[nsolvables + r->w2] = r - solv->rules;
1553 /*-------------------------------------------------------------------
1555 * add watches (for rule)
1556 * sets up watches for a single rule
1558 * see also makewatches()
1562 addwatches_rule(Solver *solv, Rule *r)
1564 int nsolvables = solv->pool->nsolvables;
1566 r->n1 = solv->watches[nsolvables + r->w1];
1567 solv->watches[nsolvables + r->w1] = r - solv->rules;
1569 r->n2 = solv->watches[nsolvables + r->w2];
1570 solv->watches[nsolvables + r->w2] = r - solv->rules;
1574 /********************************************************************/
1580 /* shortcuts to check if a literal (positive or negative) assignment
1581 * evaluates to 'true' or 'false'
1583 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1584 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
1585 #define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
1587 /*-------------------------------------------------------------------
1591 * make decision and propagate to all rules
1593 * Evaluate each term affected by the decision (linked through watches)
1594 * If we find unit rules we make new decisions based on them
1596 * Everything's fixed there, it's just finding rules that are
1599 * return : 0 = everything is OK
1600 * rule = conflict found in this rule
1604 propagate(Solver *solv, int level)
1606 Pool *pool = solv->pool;
1607 Id *rp, *next_rp; /* rule pointer, next rule pointer in linked list */
1609 Id p, pkg, other_watch;
1611 Id *decisionmap = solv->decisionmap;
1613 Id *watches = solv->watches + pool->nsolvables; /* place ptr in middle */
1615 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
1617 /* foreach non-propagated decision */
1618 while (solv->propagate_index < solv->decisionq.count)
1621 * 'pkg' was just decided
1622 * negate because our watches trigger if literal goes FALSE
1624 pkg = -solv->decisionq.elements[solv->propagate_index++];
1626 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1628 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
1629 solver_printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
1632 /* foreach rule where 'pkg' is now FALSE */
1633 for (rp = watches + pkg; *rp; rp = next_rp)
1635 r = solv->rules + *rp;
1638 /* rule is disabled, goto next */
1646 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1648 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
1649 solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
1652 /* 'pkg' was just decided (was set to FALSE)
1654 * now find other literal watch, check clause
1655 * and advance on linked list
1659 other_watch = r->w2;
1664 other_watch = r->w1;
1669 * This term is already true (through the other literal)
1670 * so we have nothing to do
1672 if (DECISIONMAP_TRUE(other_watch))
1676 * The other literal is FALSE or UNDEF
1682 /* Not a binary clause, try to move our watch.
1684 * Go over all literals and find one that is
1688 * (TRUE is also ok, in that case the rule is fulfilled)
1690 if (r->p /* we have a 'p' */
1691 && r->p != other_watch /* which is not watched */
1692 && !DECISIONMAP_FALSE(r->p)) /* and not FALSE */
1696 else /* go find a 'd' to make 'true' */
1699 we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
1701 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1703 if (p != other_watch /* which is not watched */
1704 && !DECISIONMAP_FALSE(p)) /* and not FALSE */
1712 * if we found some p that is UNDEF or TRUE, move
1715 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1718 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables + p));
1720 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables - p));
1736 watches[p] = r - solv->rules;
1739 /* search failed, thus all unwatched literals are FALSE */
1744 * unit clause found, set literal other_watch to TRUE
1747 if (DECISIONMAP_FALSE(other_watch)) /* check if literal is FALSE */
1748 return r; /* eek, a conflict! */
1750 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1752 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " unit ");
1753 solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
1756 if (other_watch > 0)
1757 decisionmap[other_watch] = level; /* install! */
1759 decisionmap[-other_watch] = -level; /* remove! */
1761 queue_push(&solv->decisionq, other_watch);
1762 queue_push(&solv->decisionq_why, r - solv->rules);
1764 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1766 Solvable *s = pool->solvables + (other_watch > 0 ? other_watch : -other_watch);
1767 if (other_watch > 0)
1768 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", solvable2str(pool, s));
1770 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", solvable2str(pool, s));
1773 } /* foreach rule involving 'pkg' */
1775 } /* while we have non-decided decisions */
1777 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
1779 return 0; /* all is well */
1783 /********************************************************************/
1786 /*-------------------------------------------------------------------
1793 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
1795 Pool *pool = solv->pool;
1798 Map seen; /* global? */
1799 Id d, v, vv, *dp, why;
1801 int num = 0, l1num = 0;
1802 int learnt_why = solv->learnt_pool.count;
1803 Id *decisionmap = solv->decisionmap;
1807 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
1808 map_init(&seen, pool->nsolvables);
1809 idx = solv->decisionq.count;
1812 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1813 solver_printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1814 queue_push(&solv->learnt_pool, c - solv->rules);
1815 d = c->d < 0 ? -c->d - 1 : c->d;
1816 dp = d ? pool->whatprovidesdata + d : 0;
1817 /* go through all literals of the rule */
1829 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1831 vv = v > 0 ? v : -v;
1832 if (MAPTST(&seen, vv))
1834 l = solv->decisionmap[vv];
1839 l1num++; /* need to do this one in level1 pass */
1840 else if (l == level)
1841 num++; /* need to do this one as well */
1844 queue_push(&r, v); /* not level1 or conflict level, add to new rule */
1850 if (!num && !--l1num)
1851 break; /* all level 1 literals done */
1855 v = solv->decisionq.elements[--idx];
1856 vv = v > 0 ? v : -v;
1857 if (MAPTST(&seen, vv))
1861 if (num && --num == 0)
1863 *pr = -v; /* so that v doesn't get lost */
1866 POOL_DEBUG(SAT_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
1867 for (i = 0; i < r.count; i++)
1870 MAPCLR(&seen, v > 0 ? v : -v);
1872 /* only level 1 marks left */
1876 why = solv->decisionq_why.elements[idx];
1877 if (!why) /* just in case, maybe for SYSTEMSOLVABLE */
1879 c = solv->rules + why;
1885 else if (r.count == 1 && r.elements[0] < 0)
1886 *dr = r.elements[0];
1888 *dr = pool_queuetowhatprovides(pool, &r);
1889 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1891 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
1892 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, *pr);
1893 for (i = 0; i < r.count; i++)
1894 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
1896 /* push end marker on learnt reasons stack */
1897 queue_push(&solv->learnt_pool, 0);
1900 solv->stats_learned++;
1905 /*-------------------------------------------------------------------
1909 * reset the solver decisions to right after the rpm rules.
1910 * called after rules have been enabled/disabled
1914 reset_solver(Solver *solv)
1916 Pool *pool = solv->pool;
1920 /* rewind decisions to direct rpm rule assertions */
1921 for (i = solv->decisionq.count - 1; i >= solv->directdecisions; i--)
1923 v = solv->decisionq.elements[i];
1924 solv->decisionmap[v > 0 ? v : -v] = 0;
1927 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions done reduced from %d to %d\n", solv->decisionq.count, solv->directdecisions);
1929 solv->decisionq_why.count = solv->directdecisions;
1930 solv->decisionq.count = solv->directdecisions;
1931 solv->recommends_index = -1;
1932 solv->propagate_index = 0;
1934 /* adapt learnt rule status to new set of enabled/disabled rules */
1935 enabledisablelearntrules(solv);
1937 /* redo all job/update decisions */
1938 makeruledecisions(solv);
1939 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
1943 /*-------------------------------------------------------------------
1945 * analyze_unsolvable_rule
1949 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp)
1951 Pool *pool = solv->pool;
1953 Id why = r - solv->rules;
1955 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1956 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1957 if (solv->learntrules && why >= solv->learntrules)
1959 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1960 if (solv->learnt_pool.elements[i] > 0)
1961 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp);
1964 if (MAPTST(&solv->weakrulemap, why))
1965 if (!*lastweakp || why > *lastweakp)
1967 /* do not add rpm rules to problem */
1968 if (why < solv->rpmrules_end)
1970 /* turn rule into problem */
1971 if (why >= solv->jobrules && why < solv->jobrules_end)
1972 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1973 /* return if problem already countains our rule */
1974 if (solv->problems.count)
1976 for (i = solv->problems.count - 1; i >= 0; i--)
1977 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
1979 else if (solv->problems.elements[i] == why)
1982 queue_push(&solv->problems, why);
1986 /*-------------------------------------------------------------------
1988 * analyze_unsolvable
1990 * return: 1 - disabled some rules, try again
1995 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1997 Pool *pool = solv->pool;
1999 Map seen; /* global to speed things up? */
2000 Id d, v, vv, *dp, why;
2002 Id *decisionmap = solv->decisionmap;
2003 int oldproblemcount;
2004 int oldlearntpoolcount;
2007 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
2008 solv->stats_unsolvable++;
2009 oldproblemcount = solv->problems.count;
2010 oldlearntpoolcount = solv->learnt_pool.count;
2012 /* make room for proof index */
2013 /* must update it later, as analyze_unsolvable_rule would confuse
2014 * it with a rule index if we put the real value in already */
2015 queue_push(&solv->problems, 0);
2018 map_init(&seen, pool->nsolvables);
2019 queue_push(&solv->learnt_pool, r - solv->rules);
2021 analyze_unsolvable_rule(solv, r, &lastweak);
2022 d = r->d < 0 ? -r->d - 1 : r->d;
2023 dp = d ? pool->whatprovidesdata + d : 0;
2034 if (DECISIONMAP_TRUE(v)) /* the one true literal */
2036 vv = v > 0 ? v : -v;
2037 l = solv->decisionmap[vv];
2042 idx = solv->decisionq.count;
2045 v = solv->decisionq.elements[--idx];
2046 vv = v > 0 ? v : -v;
2047 if (!MAPTST(&seen, vv))
2049 why = solv->decisionq_why.elements[idx];
2050 queue_push(&solv->learnt_pool, why);
2051 r = solv->rules + why;
2052 analyze_unsolvable_rule(solv, r, &lastweak);
2053 d = r->d < 0 ? -r->d - 1 : r->d;
2054 dp = d ? pool->whatprovidesdata + d : 0;
2065 if (DECISIONMAP_TRUE(v)) /* the one true literal */
2067 vv = v > 0 ? v : -v;
2068 l = solv->decisionmap[vv];
2075 queue_push(&solv->problems, 0); /* mark end of this problem */
2079 /* disable last weak rule */
2080 solv->problems.count = oldproblemcount;
2081 solv->learnt_pool.count = oldlearntpoolcount;
2082 r = solv->rules + lastweak;
2083 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling ");
2084 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
2085 disablerule(solv, r);
2091 queue_push(&solv->learnt_pool, 0);
2092 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
2096 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
2097 disableproblem(solv, solv->problems.elements[i]);
2098 /* XXX: might want to enable all weak rules again */
2102 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
2107 /********************************************************************/
2108 /* Decision revert */
2110 /*-------------------------------------------------------------------
2113 * revert decision at level
2117 revert(Solver *solv, int level)
2119 Pool *pool = solv->pool;
2121 while (solv->decisionq.count)
2123 v = solv->decisionq.elements[solv->decisionq.count - 1];
2124 vv = v > 0 ? v : -v;
2125 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
2127 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
2128 if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
2129 solv->recommendations.count--;
2130 solv->decisionmap[vv] = 0;
2131 solv->decisionq.count--;
2132 solv->decisionq_why.count--;
2133 solv->propagate_index = solv->decisionq.count;
2135 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
2137 solv->branches.count--;
2138 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
2139 solv->branches.count--;
2141 solv->recommends_index = -1;
2145 /*-------------------------------------------------------------------
2147 * watch2onhighest - put watch2 on literal with highest level
2151 watch2onhighest(Solver *solv, Rule *r)
2156 d = r->d < 0 ? -r->d - 1 : r->d;
2158 return; /* binary rule, both watches are set */
2159 dp = solv->pool->whatprovidesdata + d;
2160 while ((v = *dp++) != 0)
2162 l = solv->decisionmap[v < 0 ? -v : v];
2174 /*-------------------------------------------------------------------
2178 * add free decision (solvable to install) to decisionq
2179 * increase level and propagate decision
2180 * return if no conflict.
2182 * in conflict case, analyze conflict rule, add resulting
2183 * rule to learnt rule set, make decision from learnt
2184 * rule (always unit) and re-propagate.
2186 * returns the new solver level or 0 if unsolvable
2191 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
2193 Pool *pool = solv->pool;
2202 solv->decisionmap[decision] = level;
2204 solv->decisionmap[-decision] = -level;
2205 queue_push(&solv->decisionq, decision);
2206 queue_push(&solv->decisionq_why, 0);
2210 r = propagate(solv, level);
2214 return analyze_unsolvable(solv, r, disablerules);
2215 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
2216 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
2217 assert(l > 0 && l < level);
2218 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
2220 revert(solv, level);
2221 r = addrule(solv, p, d); /* p requires d */
2223 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
2224 queue_push(&solv->learnt_why, why);
2227 /* at least 2 literals, needs watches */
2228 watch2onhighest(solv, r);
2229 addwatches_rule(solv, r);
2233 /* learnt rule is an assertion */
2234 queue_push(&solv->ruleassertions, r - solv->rules);
2236 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
2237 queue_push(&solv->decisionq, p);
2238 queue_push(&solv->decisionq_why, r - solv->rules);
2239 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
2241 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
2242 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
2243 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
2244 solver_printrule(solv, SAT_DEBUG_ANALYZE, r);
2251 /*-------------------------------------------------------------------
2253 * select and install
2255 * install best package from the queue. We add an extra package, inst, if
2256 * provided. See comment in weak install section.
2258 * returns the new solver level or 0 if unsolvable
2263 selectandinstall(Solver *solv, int level, Queue *dq, Id inst, int disablerules)
2265 Pool *pool = solv->pool;
2269 if (dq->count > 1 || inst)
2270 policy_filter_unwanted(solv, dq, inst, POLICY_MODE_CHOOSE);
2275 /* choose the supplemented one */
2276 for (i = 0; i < dq->count; i++)
2277 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
2281 for (i = 1; i < dq->count; i++)
2282 queue_push(&solv->branches, dq->elements[i]);
2283 queue_push(&solv->branches, -level);
2287 p = dq->elements[i];
2289 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvable2str(pool, pool->solvables + p));
2291 return setpropagatelearn(solv, level, p, disablerules);
2295 /********************************************************************/
2296 /* Main solver interface */
2299 /*-------------------------------------------------------------------
2302 * create solver structure
2304 * pool: all available solvables
2305 * installed: installed Solvables
2308 * Upon solving, rules are created to flag the Solvables
2309 * of the 'installed' Repo as installed.
2313 solver_create(Pool *pool, Repo *installed)
2316 solv = (Solver *)sat_calloc(1, sizeof(Solver));
2318 solv->installed = installed;
2320 queue_init(&solv->ruletojob);
2321 queue_init(&solv->decisionq);
2322 queue_init(&solv->decisionq_why);
2323 queue_init(&solv->problems);
2324 queue_init(&solv->suggestions);
2325 queue_init(&solv->recommendations);
2326 queue_init(&solv->orphaned);
2327 queue_init(&solv->learnt_why);
2328 queue_init(&solv->learnt_pool);
2329 queue_init(&solv->branches);
2330 queue_init(&solv->covenantq);
2331 queue_init(&solv->weakruleq);
2332 queue_init(&solv->ruleassertions);
2334 map_init(&solv->recommendsmap, pool->nsolvables);
2335 map_init(&solv->suggestsmap, pool->nsolvables);
2336 map_init(&solv->noupdate, installed ? installed->end - installed->start : 0);
2337 solv->recommends_index = 0;
2339 solv->decisionmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
2341 solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
2342 memset(solv->rules, 0, sizeof(Rule));
2348 /*-------------------------------------------------------------------
2354 solver_free(Solver *solv)
2356 queue_free(&solv->ruletojob);
2357 queue_free(&solv->decisionq);
2358 queue_free(&solv->decisionq_why);
2359 queue_free(&solv->learnt_why);
2360 queue_free(&solv->learnt_pool);
2361 queue_free(&solv->problems);
2362 queue_free(&solv->suggestions);
2363 queue_free(&solv->recommendations);
2364 queue_free(&solv->orphaned);
2365 queue_free(&solv->branches);
2366 queue_free(&solv->covenantq);
2367 queue_free(&solv->weakruleq);
2368 queue_free(&solv->ruleassertions);
2370 map_free(&solv->recommendsmap);
2371 map_free(&solv->suggestsmap);
2372 map_free(&solv->noupdate);
2373 map_free(&solv->weakrulemap);
2374 map_free(&solv->noobsoletes);
2376 sat_free(solv->decisionmap);
2377 sat_free(solv->rules);
2378 sat_free(solv->watches);
2379 sat_free(solv->obsoletes);
2380 sat_free(solv->obsoletes_data);
2385 /*-------------------------------------------------------------------
2389 * all rules have been set up, now actually run the solver
2394 run_solver(Solver *solv, int disablerules, int doweak)
2396 Queue dq; /* local decisionqueue */
2397 Queue dqs; /* local decisionqueue for supplements */
2403 Pool *pool = solv->pool;
2406 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
2408 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
2409 for (i = 1; i < solv->nrules; i++)
2410 solver_printruleclass(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
2413 POOL_DEBUG(SAT_DEBUG_STATS, "initial decisions: %d\n", solv->decisionq.count);
2415 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2416 solver_printdecisions(solv);
2418 /* start SAT algorithm */
2420 systemlevel = level + 1;
2421 POOL_DEBUG(SAT_DEBUG_STATS, "solving...\n");
2427 * here's the main loop:
2428 * 1) propagate new decisions (only needed for level 1)
2429 * 2) try to keep installed packages
2430 * 3) fulfill all unresolved rules
2431 * 4) install recommended packages
2432 * 5) minimalize solution if we had choices
2433 * if we encounter a problem, we rewind to a safe level and restart
2445 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
2446 if ((r = propagate(solv, level)) != 0)
2448 if (analyze_unsolvable(solv, r, disablerules))
2455 if (level < systemlevel)
2457 POOL_DEBUG(SAT_DEBUG_STATS, "resolving job rules\n");
2458 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2461 if (r->d < 0) /* ignore disabled rules */
2464 FOR_RULELITERALS(l, dp, r)
2468 if (solv->decisionmap[-l] <= 0)
2473 if (solv->decisionmap[l] > 0)
2475 if (solv->decisionmap[l] == 0)
2481 /* prune to installed if not updating */
2482 if (!solv->updatesystem && solv->installed && dq.count > 1)
2485 for (j = k = 0; j < dq.count; j++)
2487 Solvable *s = pool->solvables + dq.elements[j];
2488 if (s->repo == solv->installed)
2489 dq.elements[k++] = dq.elements[j];
2495 level = selectandinstall(solv, level, &dq, 0, disablerules);
2501 if (level <= olevel)
2504 systemlevel = level + 1;
2505 if (i < solv->jobrules_end)
2511 * installed packages
2514 if (level < systemlevel && solv->installed && solv->installed->nsolvables)
2516 if (!solv->updatesystem)
2519 * Normal run (non-updating)
2520 * Keep as many packages installed as possible
2522 POOL_DEBUG(SAT_DEBUG_STATS, "installing old packages\n");
2524 for (i = solv->installed->start; i < solv->installed->end; i++)
2526 s = pool->solvables + i;
2528 /* skip if not installed */
2529 if (s->repo != solv->installed)
2532 /* skip if already decided */
2533 if (solv->decisionmap[i] != 0)
2536 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "keeping %s\n", solvable2str(pool, s));
2539 level = setpropagatelearn(solv, level, i, disablerules);
2541 if (level == 0) /* unsolvable */
2546 if (level <= olevel)
2549 if (i < solv->installed->end)
2553 POOL_DEBUG(SAT_DEBUG_STATS, "resolving update/feature rules\n");
2555 for (i = solv->installed->start, r = solv->rules + solv->updaterules; i < solv->installed->end; i++, r++)
2559 s = pool->solvables + i;
2561 /* skip if not installed (can't update) */
2562 if (s->repo != solv->installed)
2564 /* skip if already decided */
2565 if (solv->decisionmap[i] > 0)
2568 /* noupdate is set if a job is erasing the installed solvable or installing a specific version */
2569 if (MAPTST(&solv->noupdate, i - solv->installed->start))
2575 if (rr->d < 0) /* disabled -> look at feature rule ? */
2576 rr -= solv->installed->end - solv->installed->start;
2577 if (!rr->p) /* identical to update rule? */
2582 FOR_RULELITERALS(p, dp, rr)
2584 if (solv->decisionmap[p] > 0)
2586 if (solv->decisionmap[p] == 0)
2589 if (p || !dq.count) /* already fulfilled or empty */
2591 if (dq.elements[0] == i)
2592 inst = queue_shift(&dq);
2596 /* FIXME: it is handled a bit different because we do not want
2597 * to have it pruned just because it is not recommened.
2598 * we should not prune installed packages instead.
2600 level = selectandinstall(solv, level, &dq, inst, disablerules);
2606 if (level <= olevel)
2609 systemlevel = level + 1;
2610 if (i < solv->installed->end)
2614 if (level < systemlevel)
2615 systemlevel = level;
2621 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unresolved rules\n");
2622 for (i = 1, n = 1; ; i++, n++)
2624 if (n == solv->nrules)
2626 if (i == solv->nrules)
2628 r = solv->rules + i;
2629 if (r->d < 0) /* ignore disabled rules */
2634 /* binary or unary rule */
2635 /* need two positive undecided literals */
2636 if (r->p < 0 || r->w2 <= 0)
2638 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2640 queue_push(&dq, r->p);
2641 queue_push(&dq, r->w2);
2646 * all negative literals are installed
2647 * no positive literal is installed
2648 * i.e. the rule is not fulfilled and we
2649 * just need to decide on the positive literals
2653 if (solv->decisionmap[-r->p] <= 0)
2658 if (solv->decisionmap[r->p] > 0)
2660 if (solv->decisionmap[r->p] == 0)
2661 queue_push(&dq, r->p);
2663 dp = pool->whatprovidesdata + r->d;
2664 while ((p = *dp++) != 0)
2668 if (solv->decisionmap[-p] <= 0)
2673 if (solv->decisionmap[p] > 0)
2675 if (solv->decisionmap[p] == 0)
2682 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
2684 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
2685 solver_printruleclass(solv, SAT_DEBUG_PROPAGATE, r);
2687 /* dq.count < 2 cannot happen as this means that
2688 * the rule is unit */
2689 assert(dq.count > 1);
2692 level = selectandinstall(solv, level, &dq, 0, disablerules);
2698 if (level < systemlevel)
2701 } /* for(), decide */
2703 if (n != solv->nrules) /* continue if level < systemlevel */
2710 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended packages\n");
2713 for (i = 1; i < pool->nsolvables; i++)
2715 if (solv->decisionmap[i] < 0)
2717 if (solv->decisionmap[i] > 0)
2719 /* installed, check for recommends */
2720 Id *recp, rec, *pp, p;
2721 s = pool->solvables + i;
2722 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
2724 /* XXX need to special case AND ? */
2727 recp = s->repo->idarraydata + s->recommends;
2728 while ((rec = *recp++) != 0)
2731 FOR_PROVIDES(p, pp, rec)
2733 if (solv->decisionmap[p] > 0)
2738 else if (solv->decisionmap[p] == 0)
2740 queue_pushunique(&dq, p);
2748 s = pool->solvables + i;
2749 if (!s->supplements)
2751 if (!pool_installable(pool, s))
2753 if (!solver_is_supplementing(solv, s))
2755 if (solv->ignorealreadyrecommended && solv->installed)
2756 queue_pushunique(&dqs, i); /* needs filter */
2758 queue_pushunique(&dq, i);
2761 if (solv->ignorealreadyrecommended && dqs.count)
2763 /* turn off all new packages */
2764 for (i = 0; i < solv->decisionq.count; i++)
2766 p = solv->decisionq.elements[i];
2769 s = pool->solvables + p;
2770 if (s->repo && s->repo != solv->installed)
2771 solv->decisionmap[p] = -solv->decisionmap[p];
2773 /* filter out old supplements */
2774 for (i = 0; i < dqs.count; i++)
2776 p = dqs.elements[i];
2777 s = pool->solvables + p;
2778 if (!s->supplements)
2780 if (!solver_is_supplementing(solv, s))
2781 queue_pushunique(&dq, p);
2783 /* undo turning off */
2784 for (i = 0; i < solv->decisionq.count; i++)
2786 p = solv->decisionq.elements[i];
2789 s = pool->solvables + p;
2790 if (s->repo && s->repo != solv->installed)
2791 solv->decisionmap[p] = -solv->decisionmap[p];
2797 policy_filter_unwanted(solv, &dq, 0, POLICY_MODE_RECOMMEND);
2799 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended %s\n", solvable2str(pool, pool->solvables + p));
2800 queue_push(&solv->recommendations, p);
2801 level = setpropagatelearn(solv, level, p, 0);
2806 if (solv->distupgrade && solv->installed)
2808 /* let's see if we can install some unsupported package */
2810 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unsupported packages\n");
2811 for (i = solv->installed->start, ri = 0; i < solv->installed->end; i++, ri++)
2813 s = pool->solvables + i;
2814 if (s->repo != solv->installed)
2816 if (solv->decisionmap[i])
2818 if (!solv->rules[solv->updaterules + ri].p && !solv->rules[solv->featurerules + ri].p)
2821 if (i < solv->installed->end)
2823 if (solv->distupgrade_removeunsupported)
2825 POOL_DEBUG(SAT_DEBUG_STATS, "removing unsupported %s\n", solvable2str(pool, pool->solvables + i));
2826 level = setpropagatelearn(solv, level, -i, 0);
2830 POOL_DEBUG(SAT_DEBUG_STATS, "keeping unsupported %s\n", solvable2str(pool, pool->solvables + i));
2831 level = setpropagatelearn(solv, level, i, 0);
2837 if (solv->solution_callback)
2839 solv->solution_callback(solv, solv->solution_callback_data);
2840 if (solv->branches.count)
2842 int i = solv->branches.count - 1;
2843 int l = -solv->branches.elements[i];
2845 if (solv->branches.elements[i - 1] < 0)
2847 p = solv->branches.elements[i];
2848 POOL_DEBUG(SAT_DEBUG_STATS, "branching with %s\n", solvable2str(pool, pool->solvables + p));
2850 for (j = i + 1; j < solv->branches.count; j++)
2851 queue_push(&dq, solv->branches.elements[j]);
2852 solv->branches.count = i;
2854 revert(solv, level);
2856 for (j = 0; j < dq.count; j++)
2857 queue_push(&solv->branches, dq.elements[j]);
2859 level = setpropagatelearn(solv, level, p, disablerules);
2867 /* all branches done, we're finally finished */
2871 /* minimization step */
2872 if (solv->branches.count)
2874 int l = 0, lasti = -1, lastl = -1;
2876 for (i = solv->branches.count - 1; i >= 0; i--)
2878 p = solv->branches.elements[i];
2881 else if (p > 0 && solv->decisionmap[p] > l + 1)
2889 /* kill old solvable so that we do not loop */
2890 p = solv->branches.elements[lasti];
2891 solv->branches.elements[lasti] = 0;
2892 POOL_DEBUG(SAT_DEBUG_STATS, "minimizing %d -> %d with %s\n", solv->decisionmap[p], l, solvable2str(pool, pool->solvables + p));
2895 revert(solv, level);
2897 level = setpropagatelearn(solv, level, p, disablerules);
2908 POOL_DEBUG(SAT_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable\n", solv->stats_learned, solv->stats_unsolvable);
2910 POOL_DEBUG(SAT_DEBUG_STATS, "done solving.\n\n");
2916 /*-------------------------------------------------------------------
2920 * at this point, all rules that led to conflicts are disabled.
2921 * we re-enable all rules of a problem set but rule "sug", then
2922 * continue to disable more rules until there as again a solution.
2925 /* FIXME: think about conflicting assertions */
2928 refine_suggestion(Solver *solv, Queue *job, Id *problem, Id sug, Queue *refined)
2930 Pool *pool = solv->pool;
2936 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2938 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion start\n");
2939 for (i = 0; problem[i]; i++)
2941 if (problem[i] == sug)
2942 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "=> ");
2943 solver_printproblem(solv, problem[i]);
2946 queue_init(&disabled);
2947 queue_empty(refined);
2948 queue_push(refined, sug);
2950 /* re-enable all problem rules with the exception of "sug"(gestion) */
2954 for (i = 0; problem[i]; i++)
2955 if (problem[i] != sug)
2956 enableproblem(solv, problem[i]);
2959 disableupdaterules(solv, job, -(sug + 1));
2960 else if (sug >= solv->updaterules && sug < solv->updaterules_end)
2962 /* enable feature rule */
2963 Rule *r = solv->rules + solv->featurerules + (sug - solv->updaterules);
2965 enablerule(solv, r);
2968 enableweakrules(solv);
2972 int njob, nfeature, nupdate;
2973 queue_empty(&solv->problems);
2974 revert(solv, 1); /* XXX no longer needed? */
2977 if (!solv->problems.count)
2978 run_solver(solv, 0, 0);
2980 if (!solv->problems.count)
2982 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no more problems!\n");
2983 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2984 solver_printdecisions(solv);
2985 break; /* great, no more problems */
2987 disabledcnt = disabled.count;
2988 /* start with 1 to skip over proof index */
2989 njob = nfeature = nupdate = 0;
2990 for (i = 1; i < solv->problems.count - 1; i++)
2992 /* ignore solutions in refined */
2993 v = solv->problems.elements[i];
2995 break; /* end of problem reached */
2996 for (j = 0; problem[j]; j++)
2997 if (problem[j] != sug && problem[j] == v)
3001 if (v >= solv->featurerules && v < solv->featurerules_end)
3007 queue_push(&disabled, v);
3009 if (disabled.count == disabledcnt)
3011 /* no solution found, this was an invalid suggestion! */
3012 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no solution found!\n");
3016 if (!njob && nupdate && nfeature)
3018 /* got only update rules, filter out feature rules */
3019 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "throwing away feature rules\n");
3020 for (i = j = disabledcnt; i < disabled.count; i++)
3022 v = disabled.elements[i];
3023 if (v < solv->featurerules || v >= solv->featurerules_end)
3024 disabled.elements[j++] = v;
3029 if (disabled.count == disabledcnt + 1)
3031 /* just one suggestion, add it to refined list */
3032 v = disabled.elements[disabledcnt];
3034 queue_push(refined, v); /* do not record feature rules */
3035 disableproblem(solv, v);
3036 if (v >= solv->updaterules && v < solv->updaterules_end)
3038 Rule *r = solv->rules + (v - solv->updaterules + solv->featurerules);
3040 enablerule(solv, r); /* enable corresponding feature rule */
3043 disableupdaterules(solv, job, -(v + 1));
3047 /* more than one solution, disable all */
3048 /* do not push anything on refine list, as we do not know which solution to choose */
3049 /* thus, the user will get another problem if he selects this solution, where he
3050 * can choose the right one */
3051 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
3053 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "more than one solution found:\n");
3054 for (i = disabledcnt; i < disabled.count; i++)
3055 solver_printproblem(solv, disabled.elements[i]);
3057 for (i = disabledcnt; i < disabled.count; i++)
3059 v = disabled.elements[i];
3060 disableproblem(solv, v);
3061 if (v >= solv->updaterules && v < solv->updaterules_end)
3063 Rule *r = solv->rules + (v - solv->updaterules + solv->featurerules);
3065 enablerule(solv, r);
3070 /* all done, get us back into the same state as before */
3071 /* enable refined rules again */
3072 for (i = 0; i < disabled.count; i++)
3073 enableproblem(solv, disabled.elements[i]);
3074 /* disable problem rules again */
3077 for (i = 0; problem[i]; i++)
3078 enableproblem(solv, problem[i]);
3079 disableupdaterules(solv, job, -1);
3081 /* disable problem rules again */
3082 for (i = 0; problem[i]; i++)
3083 disableproblem(solv, problem[i]);
3084 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion end\n");
3088 /*-------------------------------------------------------------------
3089 * sorting helper for problems
3093 problems_sortcmp(const void *ap, const void *bp)
3095 Id a = *(Id *)ap, b = *(Id *)bp;
3104 /*-------------------------------------------------------------------
3109 problems_sort(Solver *solv)
3112 if (!solv->problems.count)
3114 for (i = j = 1; i < solv->problems.count; i++)
3116 if (!solv->problems.elements[i])
3119 qsort(solv->problems.elements + j, i - j, sizeof(Id), problems_sortcmp);
3120 if (++i == solv->problems.count)
3128 /*-------------------------------------------------------------------
3129 * convert problems to solutions
3133 problems_to_solutions(Solver *solv, Queue *job)
3135 Pool *pool = solv->pool;
3143 if (!solv->problems.count)
3145 problems_sort(solv);
3146 queue_clone(&problems, &solv->problems);
3147 queue_init(&solution);
3148 queue_init(&solutions);
3149 /* copy over proof index */
3150 queue_push(&solutions, problems.elements[0]);
3151 problem = problems.elements + 1;
3152 for (i = 1; i < problems.count; i++)
3154 Id v = problems.elements[i];
3157 /* mark end of this problem */
3158 queue_push(&solutions, 0);
3159 queue_push(&solutions, 0);
3160 if (i + 1 == problems.count)
3162 /* copy over proof of next problem */
3163 queue_push(&solutions, problems.elements[i + 1]);
3165 problem = problems.elements + i + 1;
3168 refine_suggestion(solv, job, problem, v, &solution);
3169 if (!solution.count)
3170 continue; /* this solution didn't work out */
3173 for (j = 0; j < solution.count; j++)
3175 why = solution.elements[j];
3176 /* must be either job descriptor or update rule */
3177 assert(why < 0 || (why >= solv->updaterules && why < solv->updaterules_end));
3179 solver_printproblem(solv, why);
3183 /* job descriptor */
3184 queue_push(&solutions, 0);
3185 queue_push(&solutions, -why);
3189 /* update rule, find replacement package */
3190 Id p, d, *dp, rp = 0;
3192 p = solv->installed->start + (why - solv->updaterules);
3193 rr = solv->rules + solv->featurerules + (why - solv->updaterules);
3195 rr = solv->rules + why;
3196 if (solv->distupgrade && solv->rules[why].p != p && solv->decisionmap[p] > 0)
3198 /* distupgrade case, allow to keep old package */
3199 queue_push(&solutions, p);
3200 queue_push(&solutions, p);
3204 if (solv->decisionmap[p] > 0)
3205 continue; /* false alarm, turned out we can keep the package */
3208 d = rr->d < 0 ? -rr->d - 1 : rr->d;
3211 if (solv->decisionmap[rr->w2] > 0 && pool->solvables[rr->w2].repo != solv->installed)
3216 for (dp = pool->whatprovidesdata + d; *dp; dp++)
3218 if (solv->decisionmap[*dp] > 0 && pool->solvables[*dp].repo != solv->installed)
3226 queue_push(&solutions, p);
3227 queue_push(&solutions, rp);
3231 /* mark end of this solution */
3234 queue_push(&solutions, 0);
3235 queue_push(&solutions, 0);
3239 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "Oops, everything was fine?\n");
3242 queue_free(&solution);
3243 queue_free(&problems);
3244 /* copy queue over to solutions */
3245 queue_free(&solv->problems);
3246 queue_clone(&solv->problems, &solutions);
3248 /* bring solver back into problem state */
3249 revert(solv, 1); /* XXX move to reset_solver? */
3252 assert(solv->problems.count == solutions.count);
3253 queue_free(&solutions);
3257 /*-------------------------------------------------------------------
3261 * advance to next problem
3265 solver_next_problem(Solver *solv, Id problem)
3269 return solv->problems.count ? 1 : 0;
3270 pp = solv->problems.elements + problem;
3271 while (pp[0] || pp[1])
3275 while (pp[0] || pp[1])
3280 problem = pp - solv->problems.elements;
3281 if (problem >= solv->problems.count)
3287 /*-------------------------------------------------------------------
3293 solver_next_solution(Solver *solv, Id problem, Id solution)
3299 pp = solv->problems.elements + solution;
3300 return pp[0] || pp[1] ? solution : 0;
3302 pp = solv->problems.elements + solution;
3303 while (pp[0] || pp[1])
3306 solution = pp - solv->problems.elements;
3307 return pp[0] || pp[1] ? solution : 0;
3311 /*-------------------------------------------------------------------
3313 * solution element iterator
3317 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
3320 element = element ? element + 2 : solution;
3321 pp = solv->problems.elements + element;
3322 if (!(pp[0] || pp[1]))
3330 /*-------------------------------------------------------------------
3332 * Retrieve information about a problematic rule
3334 * this is basically the reverse of addrpmrulesforsolvable
3338 solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp)
3340 Pool *pool = solv->pool;
3341 Repo *installed = solv->installed;
3345 Id p, d, w2, *pp, req, *reqp, con, *conp, obs, *obsp, *dp;
3348 if (rid >= solv->jobrules && rid < solv->jobrules_end)
3351 r = solv->rules + rid;
3352 p = solv->ruletojob.elements[rid - solv->jobrules];
3353 *depp = job->elements[p + 1];
3355 *targetp = job->elements[p];
3356 d = r->d < 0 ? -r->d - 1 : r->d;
3357 if (d == 0 && r->w2 == 0 && r->p == -SYSTEMSOLVABLE && (job->elements[p] & SOLVER_SELECTMASK) != SOLVER_SOLVABLE_ONE_OF)
3358 return SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP;
3359 return SOLVER_PROBLEM_JOB_RULE;
3361 if (rid >= solv->updaterules && rid < solv->updaterules_end)
3364 *sourcep = solv->installed->start + (rid - solv->updaterules);
3366 return SOLVER_PROBLEM_UPDATE_RULE;
3368 assert(rid < solv->rpmrules_end);
3369 r = solv->rules + rid;
3371 d = r->d < 0 ? -r->d - 1 : r->d;
3372 if (d == 0 && r->w2 == 0)
3374 /* a rpm rule assertion */
3375 s = pool->solvables - r->p;
3376 if (installed && !solv->fixsystem && s->repo == installed)
3378 assert(!dontfix); /* dontfix packages never have a neg assertion */
3381 /* see why the package is not installable */
3382 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
3385 return SOLVER_PROBLEM_NOT_INSTALLABLE;
3387 /* check requires */
3390 reqp = s->repo->idarraydata + s->requires;
3391 while ((req = *reqp++) != 0)
3393 if (req == SOLVABLE_PREREQMARKER)
3395 dp = pool_whatprovides(pool, req);
3402 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
3405 if (!solv->allowselfconflicts && s->conflicts)
3407 conp = s->repo->idarraydata + s->conflicts;
3408 while ((con = *conp++) != 0)
3409 FOR_PROVIDES(p, pp, con)
3413 return SOLVER_PROBLEM_SELF_CONFLICT;
3416 /* should never happen */
3418 return SOLVER_PROBLEM_RPM_RULE;
3420 s = pool->solvables - r->p;
3421 if (installed && !solv->fixsystem && s->repo == installed)
3424 if (d && pool->whatprovidesdata[d] < 0)
3426 /* rule looks like -p|-c|x|x|x..., we only create this for patches with multiversion */
3427 /* reduce it to -p|-c case */
3428 w2 = pool->whatprovidesdata[d];
3430 if (d == 0 && w2 < 0)
3432 /* a package conflict */
3433 Solvable *s2 = pool->solvables - w2;
3436 if (installed && !solv->fixsystem && s2->repo == installed)
3439 /* if both packages have the same name and at least one of them
3440 * is not installed, they conflict */
3441 if (s->name == s2->name && !(installed && s->repo == installed && s2->repo == installed))
3443 /* also check noobsoletes map */
3444 if ((s->evr == s2->evr && s->arch == s2->arch) || !solv->noobsoletes.size
3445 || ((!installed || s->repo != installed) && !MAPTST(&solv->noobsoletes, -r->p))
3446 || ((!installed || s2->repo != installed) && !MAPTST(&solv->noobsoletes, -w2)))
3451 return SOLVER_PROBLEM_SAME_NAME;
3455 /* check conflicts in both directions */
3458 conp = s->repo->idarraydata + s->conflicts;
3459 while ((con = *conp++) != 0)
3461 FOR_PROVIDES(p, pp, con)
3463 if (dontfix && pool->solvables[p].repo == installed)
3470 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
3476 conp = s2->repo->idarraydata + s2->conflicts;
3477 while ((con = *conp++) != 0)
3479 FOR_PROVIDES(p, pp, con)
3481 if (dontfix2 && pool->solvables[p].repo == installed)
3488 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
3492 /* check obsoletes in both directions */
3493 if ((!installed || s->repo != installed) && s->obsoletes && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -r->p)))
3495 obsp = s->repo->idarraydata + s->obsoletes;
3496 while ((obs = *obsp++) != 0)
3498 FOR_PROVIDES(p, pp, obs)
3502 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3507 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3511 if ((!installed || s2->repo != installed) && s2->obsoletes && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -w2)))
3513 obsp = s2->repo->idarraydata + s2->obsoletes;
3514 while ((obs = *obsp++) != 0)
3516 FOR_PROVIDES(p, pp, obs)
3520 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3525 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3529 if (solv->implicitobsoleteusesprovides && (!installed || s->repo != installed) && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -r->p)))
3531 FOR_PROVIDES(p, pp, s->name)
3538 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3541 if (solv->implicitobsoleteusesprovides && (!installed || s2->repo != installed) && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -w2)))
3543 FOR_PROVIDES(p, pp, s2->name)
3550 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3553 /* all cases checked, can't happen */
3557 return SOLVER_PROBLEM_RPM_RULE;
3559 /* simple requires */
3562 reqp = s->repo->idarraydata + s->requires;
3563 while ((req = *reqp++) != 0)
3565 if (req == SOLVABLE_PREREQMARKER)
3567 dp = pool_whatprovides(pool, req);
3570 if (*dp == r->w2 && dp[1] == 0)
3573 else if (dp - pool->whatprovidesdata == d)
3581 return SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE;
3584 /* all cases checked, can't happen */
3588 return SOLVER_PROBLEM_RPM_RULE;
3592 /*-------------------------------------------------------------------
3598 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp)
3601 Id lreqr, lconr, lsysr, ljobr;
3605 lreqr = lconr = lsysr = ljobr = 0;
3606 while ((rid = solv->learnt_pool.elements[idx++]) != 0)
3609 if (rid >= solv->learntrules)
3610 findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr);
3611 else if (rid >= solv->jobrules && rid < solv->jobrules_end)
3616 else if (rid >= solv->updaterules && rid < solv->updaterules_end)
3623 assert(rid < solv->rpmrules_end);
3624 r = solv->rules + rid;
3625 d = r->d < 0 ? -r->d - 1 : r->d;
3626 if (!d && r->w2 < 0)
3633 if (!d && r->w2 == 0 && !reqassert)
3635 /* prefer assertions (XXX: bad idea?) */
3641 else if (solv->installed && r->p < 0 && solv->pool->solvables[-r->p].repo == solv->installed)
3643 /* prefer rules of installed packages */
3644 Id op = *reqrp >= 0 ? solv->rules[*reqrp].p : -*reqrp;
3645 if (op <= 0 || solv->pool->solvables[op].repo != solv->installed)
3651 if (!*reqrp && lreqr)
3653 if (!*conrp && lconr)
3655 if (!*jobrp && ljobr)
3657 if (!*sysrp && lsysr)
3662 /*-------------------------------------------------------------------
3666 * search for a rule that describes the problem to the
3667 * user. A pretty hopeless task, actually. We currently
3668 * prefer simple requires.
3672 solver_findproblemrule(Solver *solv, Id problem)
3674 Id reqr, conr, sysr, jobr;
3675 Id idx = solv->problems.elements[problem - 1];
3676 reqr = conr = sysr = jobr = 0;
3677 findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr);
3690 /*-------------------------------------------------------------------
3692 * create reverse obsoletes map for installed solvables
3694 * for each installed solvable find which packages with *different* names
3695 * obsolete the solvable.
3696 * this index is used in policy_findupdatepackages if noupdateprovide is set.
3700 create_obsolete_index(Solver *solv)
3702 Pool *pool = solv->pool;
3704 Repo *installed = solv->installed;
3705 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
3708 if (!installed || !installed->nsolvables)
3710 solv->obsoletes = obsoletes = sat_calloc(installed->end - installed->start, sizeof(Id));
3711 for (i = 1; i < pool->nsolvables; i++)
3713 s = pool->solvables + i;
3716 if (!pool_installable(pool, s))
3718 obsp = s->repo->idarraydata + s->obsoletes;
3719 while ((obs = *obsp++) != 0)
3721 FOR_PROVIDES(p, pp, obs)
3723 if (pool->solvables[p].repo != installed)
3725 if (pool->solvables[p].name == s->name)
3727 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3729 obsoletes[p - installed->start]++;
3734 for (i = 0; i < installed->nsolvables; i++)
3737 n += obsoletes[i] + 1;
3740 solv->obsoletes_data = obsoletes_data = sat_calloc(n + 1, sizeof(Id));
3741 POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
3742 for (i = pool->nsolvables - 1; i > 0; i--)
3744 s = pool->solvables + i;
3747 if (!pool_installable(pool, s))
3749 obsp = s->repo->idarraydata + s->obsoletes;
3750 while ((obs = *obsp++) != 0)
3752 FOR_PROVIDES(p, pp, obs)
3754 if (pool->solvables[p].repo != installed)
3756 if (pool->solvables[p].name == s->name)
3758 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3760 p -= installed->start;
3761 if (obsoletes_data[obsoletes[p]] != i)
3762 obsoletes_data[--obsoletes[p]] = i;
3769 /*-------------------------------------------------------------------
3771 * remove disabled conflicts
3775 removedisabledconflicts(Solver *solv, Queue *removed)
3777 Pool *pool = solv->pool;
3782 Id *decisionmap = solv->decisionmap;
3784 POOL_DEBUG(SAT_DEBUG_STATS, "removedisabledconflicts\n");
3785 queue_empty(removed);
3786 for (i = 0; i < solv->decisionq.count; i++)
3788 p = solv->decisionq.elements[i];
3791 /* a conflict. we never do conflicts on free decisions, so there
3792 * must have been an unit rule */
3793 why = solv->decisionq_why.elements[i];
3795 r = solv->rules + why;
3796 if (r->d < 0 && decisionmap[-p])
3798 /* rule is now disabled, remove from decisionmap */
3799 POOL_DEBUG(SAT_DEBUG_STATS, "removing conflict for package %s[%d]\n", solvable2str(pool, pool->solvables - p), -p);
3800 queue_push(removed, -p);
3801 queue_push(removed, decisionmap[-p]);
3802 decisionmap[-p] = 0;
3805 if (!removed->count)
3807 /* we removed some confliced packages. some of them might still
3808 * be in conflict, so search for unit rules and re-conflict */
3810 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
3812 if (i == solv->nrules)
3815 r = solv->rules + i;
3821 if (r->p < 0 && !decisionmap[-r->p])
3827 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
3829 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
3834 if (r->p < 0 && decisionmap[-r->p] == 0)
3836 if (new || DECISIONMAP_FALSE(r->p))
3838 dp = pool->whatprovidesdata + r->d;
3839 while ((p = *dp++) != 0)
3841 if (new && p == new)
3843 if (p < 0 && decisionmap[-p] == 0)
3852 else if (!DECISIONMAP_FALSE(p))
3862 POOL_DEBUG(SAT_DEBUG_STATS, "re-conflicting package %s[%d]\n", solvable2str(pool, pool->solvables - new), -new);
3863 decisionmap[-new] = -1;
3865 n = 0; /* redo all rules */
3871 /*-------------------------------------------------------------------
3873 * weaken solvable dependencies
3877 weaken_solvable_deps(Solver *solv, Id p)
3882 for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
3886 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
3887 continue; /* conflict */
3888 queue_push(&solv->weakruleq, i);
3892 /********************************************************************/
3902 solver_solve(Solver *solv, Queue *job)
3904 Pool *pool = solv->pool;
3905 Repo *installed = solv->installed;
3908 Map addedmap; /* '1' == have rpm-rules for solvable */
3909 Map installcandidatemap;
3910 Id how, what, select, name, weak, p, *pp, d;
3916 POOL_DEBUG(SAT_DEBUG_STATS, "solver started\n");
3917 POOL_DEBUG(SAT_DEBUG_STATS, "fixsystem=%d updatesystem=%d dosplitprovides=%d, noupdateprovide=%d\n", solv->fixsystem, solv->updatesystem, solv->dosplitprovides, solv->noupdateprovide);
3918 POOL_DEBUG(SAT_DEBUG_STATS, "distupgrade=%d distupgrade_removeunsupported=%d\n", solv->distupgrade, solv->distupgrade_removeunsupported);
3919 POOL_DEBUG(SAT_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allowarchchange, solv->allowvendorchange);
3920 POOL_DEBUG(SAT_DEBUG_STATS, "promoteepoch=%d, allowvirtualconflicts=%d, allowselfconflicts=%d\n", pool->promoteepoch, solv->allowvirtualconflicts, solv->allowselfconflicts);
3921 POOL_DEBUG(SAT_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d\n", solv->obsoleteusesprovides, solv->implicitobsoleteusesprovides);
3922 POOL_DEBUG(SAT_DEBUG_STATS, "dontinstallrecommended=%d, ignorealreadyrecommended=%d, dontshowinstalledrecommended=%d\n", solv->dontinstallrecommended, solv->ignorealreadyrecommended, solv->dontshowinstalledrecommended);
3923 /* create whatprovides if not already there */
3924 if (!pool->whatprovides)
3925 pool_createwhatprovides(pool);
3927 /* create obsolete index if needed */
3928 create_obsolete_index(solv);
3931 * create basic rule set of all involved packages
3932 * use addedmap bitmap to make sure we don't create rules twice
3936 /* create noobsolete map if needed */
3937 for (i = 0; i < job->count; i += 2)
3939 how = job->elements[i] & ~SOLVER_WEAK;
3940 if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
3942 what = job->elements[i + 1];
3943 select = how & SOLVER_SELECTMASK;
3944 if (!solv->noobsoletes.size)
3945 map_init(&solv->noobsoletes, pool->nsolvables);
3946 FOR_JOB_SELECT(p, pp, select, what)
3947 MAPSET(&solv->noobsoletes, p);
3950 map_init(&addedmap, pool->nsolvables);
3951 map_init(&installcandidatemap, pool->nsolvables);
3955 * always install our system solvable
3957 MAPSET(&addedmap, SYSTEMSOLVABLE);
3958 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
3959 queue_push(&solv->decisionq_why, 0);
3960 solv->decisionmap[SYSTEMSOLVABLE] = 1; /* installed at level '1' */
3963 * create rules for all package that could be involved with the solving
3964 * so called: rpm rules
3969 oldnrules = solv->nrules;
3970 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
3971 FOR_REPO_SOLVABLES(installed, p, s)
3972 addrpmrulesforsolvable(solv, s, &addedmap);
3973 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3974 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
3975 oldnrules = solv->nrules;
3976 FOR_REPO_SOLVABLES(installed, p, s)
3977 addrpmrulesforupdaters(solv, s, &addedmap, 1);
3978 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3982 * create rules for all packages involved in the job
3983 * (to be installed or removed)
3986 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
3987 oldnrules = solv->nrules;
3988 for (i = 0; i < job->count; i += 2)
3990 how = job->elements[i];
3991 what = job->elements[i + 1];
3992 select = how & SOLVER_SELECTMASK;
3994 switch (how & SOLVER_JOBMASK)
3996 case SOLVER_INSTALL:
3997 FOR_JOB_SELECT(p, pp, select, what)
3999 MAPSET(&installcandidatemap, p);
4000 addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
4004 /* FIXME: semantics? */
4005 FOR_JOB_SELECT(p, pp, select, what)
4006 addrpmrulesforupdaters(solv, pool->solvables + what, &addedmap, 0);
4010 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
4012 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for recommended/suggested packages ***\n");
4014 oldnrules = solv->nrules;
4017 * add rules for suggests, enhances
4019 addrpmrulesforweak(solv, &addedmap);
4020 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
4022 IF_POOLDEBUG (SAT_DEBUG_STATS)
4024 int possible = 0, installable = 0;
4025 for (i = 1; i < pool->nsolvables; i++)
4027 if (pool_installable(pool, pool->solvables + i))
4029 if (MAPTST(&addedmap, i))
4032 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
4036 * first pass done, we now have all the rpm rules we need.
4037 * unify existing rules before going over all job rules and
4039 * at this point the system is always solvable,
4040 * as an empty system (remove all packages) is a valid solution
4043 unifyrules(solv); /* remove duplicate rpm rules */
4045 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
4047 solv->directdecisions = solv->decisionq.count;
4048 POOL_DEBUG(SAT_DEBUG_STATS, "decisions so far: %d\n", solv->decisionq.count);
4051 * create feature rules
4053 * foreach installed:
4054 * create assertion (keep installed, if no update available)
4056 * create update rule (A|update1(A)|update2(A)|...)
4058 * those are used later on to keep a version of the installed packages in
4062 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add feature rules ***\n");
4063 solv->featurerules = solv->nrules; /* mark start of feature rules */
4066 /* foreach possibly installed solvable */
4067 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
4069 if (s->repo != installed)
4071 addrule(solv, 0, 0); /* create dummy rule */
4074 addupdaterule(solv, s, 1); /* allow s to be updated */
4077 * assert one rule per installed solvable,
4078 * either an assertion (A)
4079 * or a possible update (A|update1(A)|update2(A)|...)
4081 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
4083 solv->featurerules_end = solv->nrules;
4086 * Add update rules for installed solvables
4088 * almost identical to feature rules
4089 * except that downgrades/archchanges/vendorchanges are not allowed
4092 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add update rules ***\n");
4093 solv->updaterules = solv->nrules;
4096 { /* foreach installed solvables */
4097 /* we create all update rules, but disable some later on depending on the job */
4098 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
4102 if (s->repo != installed)
4104 addrule(solv, 0, 0); /* create dummy rule */
4107 addupdaterule(solv, s, 0); /* allowall = 0: downgrades not allowed */
4109 * check for and remove duplicate
4111 r = solv->rules + solv->nrules - 1; /* r: update rule */
4112 sr = r - (installed->end - installed->start); /* sr: feature rule */
4113 /* it's orphaned if there is no feature rule or the feature rule
4114 * consists just of the installed package */
4115 if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
4116 queue_push(&solv->orphaned, i);
4119 assert(!sr->p); /* can't have feature rule and no update rule */
4122 unifyrules_sortcmp_data = pool;
4123 if (!unifyrules_sortcmp(r, sr))
4125 /* identical rule, kill unneeded rule */
4126 if (solv->allowuninstall)
4128 /* keep feature rule, make it weak */
4129 memset(r, 0, sizeof(*r));
4130 queue_push(&solv->weakruleq, sr - solv->rules);
4134 /* keep update rule */
4135 memset(sr, 0, sizeof(*sr));
4138 else if (solv->allowuninstall)
4140 /* make both feature and update rule weak */
4141 queue_push(&solv->weakruleq, r - solv->rules);
4142 queue_push(&solv->weakruleq, sr - solv->rules);
4145 disablerule(solv, sr);
4147 /* consistency check: we added a rule for _every_ installed solvable */
4148 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
4150 solv->updaterules_end = solv->nrules;
4154 * now add all job rules
4157 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
4159 solv->jobrules = solv->nrules;
4160 for (i = 0; i < job->count; i += 2)
4162 oldnrules = solv->nrules;
4164 how = job->elements[i];
4165 what = job->elements[i + 1];
4166 weak = how & SOLVER_WEAK;
4167 select = how & SOLVER_SELECTMASK;
4168 switch (how & SOLVER_JOBMASK)
4170 case SOLVER_INSTALL:
4171 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4172 if (select == SOLVER_SOLVABLE)
4180 FOR_JOB_SELECT(p, pp, select, what)
4184 /* no candidate found, make this an impossible rule */
4185 queue_push(&q, -SYSTEMSOLVABLE);
4187 p = queue_shift(&q); /* get first candidate */
4188 d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q); /* internalize */
4190 addrule(solv, p, d); /* add install rule */
4191 queue_push(&solv->ruletojob, i);
4193 queue_push(&solv->weakruleq, solv->nrules - 1);
4196 POOL_DEBUG(SAT_DEBUG_JOB, "job: %serase %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4197 if (select == SOLVER_SOLVABLE && solv->installed && pool->solvables[what].repo == solv->installed)
4199 /* special case for "erase a specific solvable": we also
4200 * erase all other solvables with that name, so that they
4201 * don't get picked up as replacement */
4202 name = pool->solvables[what].name;
4203 FOR_PROVIDES(p, pp, name)
4207 s = pool->solvables + p;
4208 if (s->name != name)
4210 /* keep other versions installed */
4211 if (s->repo == solv->installed)
4213 /* keep installcandidates of other jobs */
4214 if (MAPTST(&installcandidatemap, p))
4216 addrule(solv, -p, 0); /* remove by Id */
4217 queue_push(&solv->ruletojob, i);
4219 queue_push(&solv->weakruleq, solv->nrules - 1);
4222 FOR_JOB_SELECT(p, pp, select, what)
4224 addrule(solv, -p, 0);
4225 queue_push(&solv->ruletojob, i);
4227 queue_push(&solv->weakruleq, solv->nrules - 1);
4232 POOL_DEBUG(SAT_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4233 if (select != SOLVER_SOLVABLE)
4235 s = pool->solvables + what;
4236 POOL_DEBUG(SAT_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solvable2str(pool, s));
4237 addupdaterule(solv, s, 0);
4238 queue_push(&solv->ruletojob, i);
4240 queue_push(&solv->weakruleq, solv->nrules - 1);
4242 case SOLVER_WEAKENDEPS:
4243 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4244 if (select != SOLVER_SOLVABLE)
4246 s = pool->solvables + what;
4247 weaken_solvable_deps(solv, what);
4249 case SOLVER_NOOBSOLETES:
4250 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4253 POOL_DEBUG(SAT_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4254 FOR_JOB_SELECT(p, pp, select, what)
4256 s = pool->solvables + p;
4257 if (installed && s->repo == installed)
4258 addrule(solv, p, 0);
4260 addrule(solv, -p, 0);
4261 queue_push(&solv->ruletojob, i);
4263 queue_push(&solv->weakruleq, solv->nrules - 1);
4267 POOL_DEBUG(SAT_DEBUG_JOB, "job: unknown job\n");
4275 IF_POOLDEBUG (SAT_DEBUG_JOB)
4278 if (solv->nrules == oldnrules)
4279 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created\n");
4280 for (j = oldnrules; j < solv->nrules; j++)
4282 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
4283 solver_printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
4287 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
4288 solv->jobrules_end = solv->nrules;
4290 /* all rules created
4291 * --------------------------------------------------------------
4292 * prepare for solving
4295 /* free unneeded memory */
4296 map_free(&addedmap);
4297 map_free(&installcandidatemap);
4300 /* create weak map */
4301 map_init(&solv->weakrulemap, solv->nrules);
4302 for (i = 0; i < solv->weakruleq.count; i++)
4304 p = solv->weakruleq.elements[i];
4305 MAPSET(&solv->weakrulemap, p);
4308 /* all new rules are learnt after this point */
4309 solv->learntrules = solv->nrules;
4311 /* create assertion index. it is only used to speed up
4312 * makeruledecsions() a bit */
4313 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
4314 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
4315 queue_push(&solv->ruleassertions, i);
4317 /* disable update rules that conflict with our job */
4318 disableupdaterules(solv, job, -1);
4320 /* make decisions based on job/update assertions */
4321 makeruledecisions(solv);
4323 /* create watches chains */
4326 POOL_DEBUG(SAT_DEBUG_STATS, "problems so far: %d\n", solv->problems.count);
4329 * ********************************************
4331 * ********************************************
4334 run_solver(solv, 1, solv->dontinstallrecommended ? 0 : 1);
4338 /* disable all erase jobs (including weak "keep uninstalled" rules) */
4339 for (i = solv->jobrules, r = solv->rules + i; i < solv->learntrules; i++, r++)
4341 if (r->d < 0) /* disabled ? */
4343 if (r->p > 0) /* install job? */
4345 disablerule(solv, r);
4351 enabledisablelearntrules(solv);
4352 removedisabledconflicts(solv, &redoq);
4356 * find recommended packages
4359 /* if redoq.count == 0 we already found all recommended in the
4361 if (redoq.count || solv->dontinstallrecommended || !solv->dontshowinstalledrecommended || solv->ignorealreadyrecommended)
4363 Id rec, *recp, p, *pp;
4365 /* create map of all recommened packages */
4366 solv->recommends_index = -1;
4367 MAPZERO(&solv->recommendsmap);
4368 for (i = 0; i < solv->decisionq.count; i++)
4370 p = solv->decisionq.elements[i];
4373 s = pool->solvables + p;
4376 recp = s->repo->idarraydata + s->recommends;
4377 while ((rec = *recp++) != 0)
4379 FOR_PROVIDES(p, pp, rec)
4380 if (solv->decisionmap[p] > 0)
4384 if (!solv->dontshowinstalledrecommended)
4386 FOR_PROVIDES(p, pp, rec)
4387 if (solv->decisionmap[p] > 0)
4388 MAPSET(&solv->recommendsmap, p);
4390 continue; /* p != 0: already fulfilled */
4392 FOR_PROVIDES(p, pp, rec)
4393 MAPSET(&solv->recommendsmap, p);
4397 for (i = 1; i < pool->nsolvables; i++)
4399 if (solv->decisionmap[i] < 0)
4401 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
4403 s = pool->solvables + i;
4404 if (!MAPTST(&solv->recommendsmap, i))
4406 if (!s->supplements)
4408 if (!pool_installable(pool, s))
4410 if (!solver_is_supplementing(solv, s))
4413 if (solv->dontinstallrecommended)
4414 queue_push(&solv->recommendations, i);
4416 queue_pushunique(&solv->recommendations, i);
4418 /* we use MODE_SUGGEST here so that repo prio is ignored */
4419 policy_filter_unwanted(solv, &solv->recommendations, 0, POLICY_MODE_SUGGEST);
4423 * find suggested packages
4428 Id sug, *sugp, p, *pp;
4430 /* create map of all suggests that are still open */
4431 solv->recommends_index = -1;
4432 MAPZERO(&solv->suggestsmap);
4433 for (i = 0; i < solv->decisionq.count; i++)
4435 p = solv->decisionq.elements[i];
4438 s = pool->solvables + p;
4441 sugp = s->repo->idarraydata + s->suggests;
4442 while ((sug = *sugp++) != 0)
4444 FOR_PROVIDES(p, pp, sug)
4445 if (solv->decisionmap[p] > 0)
4449 if (!solv->dontshowinstalledrecommended)
4451 FOR_PROVIDES(p, pp, sug)
4452 if (solv->decisionmap[p] > 0)
4453 MAPSET(&solv->suggestsmap, p);
4455 continue; /* already fulfilled */
4457 FOR_PROVIDES(p, pp, sug)
4458 MAPSET(&solv->suggestsmap, p);
4462 for (i = 1; i < pool->nsolvables; i++)
4464 if (solv->decisionmap[i] < 0)
4466 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
4468 s = pool->solvables + i;
4469 if (!MAPTST(&solv->suggestsmap, i))
4473 if (!pool_installable(pool, s))
4475 if (!solver_is_enhancing(solv, s))
4478 queue_push(&solv->suggestions, i);
4480 policy_filter_unwanted(solv, &solv->suggestions, 0, POLICY_MODE_SUGGEST);
4485 /* restore decisionmap */
4486 for (i = 0; i < redoq.count; i += 2)
4487 solv->decisionmap[redoq.elements[i]] = redoq.elements[i + 1];
4491 * if unsolvable, prepare solutions
4494 if (solv->problems.count)
4496 int recocount = solv->recommendations.count;
4497 solv->recommendations.count = 0; /* so that revert() doesn't mess with it */
4498 queue_empty(&redoq);
4499 for (i = 0; i < solv->decisionq.count; i++)
4501 Id p = solv->decisionq.elements[i];
4502 queue_push(&redoq, p);
4503 queue_push(&redoq, solv->decisionq_why.elements[i]);
4504 queue_push(&redoq, solv->decisionmap[p > 0 ? p : -p]);
4506 problems_to_solutions(solv, job);
4507 memset(solv->decisionmap, 0, pool->nsolvables * sizeof(Id));
4508 queue_empty(&solv->decisionq);
4509 queue_empty(&solv->decisionq_why);
4510 for (i = 0; i < redoq.count; i += 3)
4512 Id p = redoq.elements[i];
4513 queue_push(&solv->decisionq, p);
4514 queue_push(&solv->decisionq_why, redoq.elements[i + 1]);
4515 solv->decisionmap[p > 0 ? p : -p] = redoq.elements[i + 2];
4517 solv->recommendations.count = recocount;
4520 POOL_DEBUG(SAT_DEBUG_STATS, "final solver statistics: %d learned rules, %d unsolvable\n", solv->stats_learned, solv->stats_unsolvable);
4524 /***********************************************************************/
4525 /* disk usage computations */
4527 /*-------------------------------------------------------------------
4529 * calculate DU changes
4533 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
4537 solver_create_state_maps(solv, &installedmap, 0);
4538 pool_calc_duchanges(solv->pool, solv->installed, &installedmap, mps, nmps);
4539 map_free(&installedmap);
4543 /*-------------------------------------------------------------------
4545 * calculate changes in install size
4549 solver_calc_installsizechange(Solver *solv)
4554 solver_create_state_maps(solv, &installedmap, 0);
4555 change = pool_calc_installsizechange(solv->pool, solv->installed, &installedmap);
4556 map_free(&installedmap);