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 s = pool->solvables + p;
1068 if (s->name != sn->name || s->arch != sn->arch)
1070 if (!MAPTST(&solv->noobsoletes, p))
1072 if (pool_match_nevr(pool, pool->solvables + p, con))
1074 /* here we have a multiversion solvable that doesn't conflict */
1075 /* thus we're not in conflict if it is installed */
1079 return -n; /* no other package found, generate normal conflict */
1080 return pool_queuetowhatprovides(pool, &q);
1084 /*-------------------------------------------------------------------
1086 * add (install) rules for solvable
1088 * s: Solvable for which to add rules
1089 * m: m[s] = 1 for solvables which have rules, prevent rule duplication
1091 * Algorithm: 'visit all nodes of a graph'. The graph nodes are
1092 * solvables, the edges their dependencies.
1093 * Starting from an installed solvable, this will create all rules
1094 * representing the graph created by the solvables dependencies.
1096 * for unfulfilled requirements, conflicts, obsoletes,....
1097 * add a negative assertion for solvables that are not installable
1099 * It will also create rules for all solvables referenced by 's'
1100 * i.e. descend to all providers of requirements of 's'
1105 addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
1107 Pool *pool = solv->pool;
1108 Repo *installed = solv->installed;
1110 /* 'work' queue. keeps Ids of solvables we still have to work on.
1111 And buffer for it. */
1116 /* if to add rules for broken deps ('rpm -V' functionality)
1123 /* Id var and pointer for each dependency
1124 * (not used in parallel)
1131 /* var and ptr for loops */
1133 /* ptr to 'whatprovides' */
1135 /* Id for current solvable 's' */
1138 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable -----\n");
1140 queue_init_buffer(&workq, workqbuf, sizeof(workqbuf)/sizeof(*workqbuf));
1141 queue_push(&workq, s - pool->solvables); /* push solvable Id to work queue */
1143 /* loop until there's no more work left */
1148 * s: Pointer to solvable
1151 n = queue_shift(&workq); /* 'pop' next solvable to work on from queue */
1152 if (MAPTST(m, n)) /* continue if already visited */
1155 MAPSET(m, n); /* mark as visited */
1156 s = pool->solvables + n; /* s = Solvable in question */
1159 if (installed /* Installed system available */
1160 && !solv->fixsystem /* NOT repair errors in rpm dependency graph */
1161 && s->repo == installed) /* solvable is installed? */
1163 dontfix = 1; /* dont care about broken rpm deps */
1167 && s->arch != ARCH_SRC
1168 && s->arch != ARCH_NOSRC
1169 && !pool_installable(pool, s))
1171 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", solvable2str(pool, s), (Id)(s - pool->solvables));
1172 addrule(solv, -n, 0); /* uninstallable */
1175 /*-----------------------------------------
1176 * check requires of s
1181 reqp = s->repo->idarraydata + s->requires;
1182 while ((req = *reqp++) != 0) /* go through all requires */
1184 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
1187 /* find list of solvables providing 'req' */
1188 dp = pool_whatprovides(pool, req);
1190 if (*dp == SYSTEMSOLVABLE) /* always installed */
1196 addpatchatomrequires(solv, s, dp, &workq, m);
1202 /* the strategy here is to not insist on dependencies
1203 * that are already broken. so if we find one provider
1204 * that was already installed, we know that the
1205 * dependency was not broken before so we enforce it */
1207 /* check if any of the providers for 'req' is installed */
1208 for (i = 0; (p = dp[i]) != 0; i++)
1210 if (pool->solvables[p].repo == installed)
1211 break; /* provider was installed */
1213 /* didn't find an installed provider: previously broken dependency */
1216 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", dep2str(pool, req), solvable2str(pool, s));
1223 /* nothing provides req! */
1224 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", solvable2str(pool, s), (Id)(s - pool->solvables), dep2str(pool, req));
1225 addrule(solv, -n, 0); /* mark requestor as uninstallable */
1229 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1231 POOL_DEBUG(SAT_DEBUG_RULE_CREATION," %s requires %s\n", solvable2str(pool, s), dep2str(pool, req));
1232 for (i = 0; dp[i]; i++)
1233 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " provided by %s\n", solvable2str(pool, pool->solvables + dp[i]));
1236 /* add 'requires' dependency */
1237 /* rule: (-requestor|provider1|provider2|...|providerN) */
1238 addrule(solv, -n, dp - pool->whatprovidesdata);
1240 /* descend the dependency tree
1241 push all non-visited providers on the work queue */
1244 if (!MAPTST(m, *dp))
1245 queue_push(&workq, *dp);
1248 } /* while, requirements of n */
1250 } /* if, requirements */
1252 /* that's all we check for src packages */
1253 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
1256 /*-----------------------------------------
1257 * check conflicts of s
1264 /* we treat conflicts in patches a bit differen:
1266 * - multiversion handling
1268 if (!strncmp("patch:", id2str(pool, s->name), 6))
1270 conp = s->repo->idarraydata + s->conflicts;
1271 /* foreach conflicts of 's' */
1272 while ((con = *conp++) != 0)
1274 /* foreach providers of a conflict of 's' */
1275 FOR_PROVIDES(p, pp, con)
1277 if (ispatch && !pool_match_nevr(pool, pool->solvables + p, con))
1279 /* dontfix: dont care about conflicts with already installed packs */
1280 if (dontfix && pool->solvables[p].repo == installed)
1282 /* p == n: self conflict */
1283 if (p == n && !solv->allowselfconflicts)
1287 Reldep *rd = GETRELDEP(pool, con);
1288 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_OTHERPROVIDERS)
1291 p = 0; /* make it a negative assertion, aka 'uninstallable' */
1293 if (p && ispatch && solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p) && ISRELDEP(con))
1295 /* our patch conflicts with a noobsoletes (aka multiversion) package */
1296 p = -makemultiversionconflict(solv, p, con);
1298 /* rule: -n|-p: either solvable _or_ provider of conflict */
1299 addrule(solv, -n, -p);
1304 /*-----------------------------------------
1305 * check obsoletes if not installed
1306 * (only installation will trigger the obsoletes in rpm)
1308 if (!installed || pool->solvables[n].repo != installed)
1309 { /* not installed */
1310 int noobs = solv->noobsoletes.size && MAPTST(&solv->noobsoletes, n);
1311 if (s->obsoletes && !noobs)
1313 obsp = s->repo->idarraydata + s->obsoletes;
1314 /* foreach obsoletes */
1315 while ((obs = *obsp++) != 0)
1317 /* foreach provider of an obsoletes of 's' */
1318 FOR_PROVIDES(p, pp, obs)
1320 if (!solv->obsoleteusesprovides /* obsoletes are matched names, not provides */
1321 && !pool_match_nevr(pool, pool->solvables + p, obs))
1323 addrule(solv, -n, -p);
1327 FOR_PROVIDES(p, pp, s->name)
1329 Solvable *ps = pool->solvables + p;
1330 /* we still obsolete packages with same nevra, like rpm does */
1331 /* (actually, rpm mixes those packages. yuck...) */
1332 if (noobs && (s->name != ps->name || s->evr != ps->evr || s->arch != ps->arch))
1334 if (!solv->implicitobsoleteusesprovides && s->name != ps->name)
1336 addrule(solv, -n, -p);
1340 /*-----------------------------------------
1341 * add recommends to the work queue
1345 recp = s->repo->idarraydata + s->recommends;
1346 while ((rec = *recp++) != 0)
1348 FOR_PROVIDES(p, pp, rec)
1350 queue_push(&workq, p);
1355 sugp = s->repo->idarraydata + s->suggests;
1356 while ((sug = *sugp++) != 0)
1358 FOR_PROVIDES(p, pp, sug)
1360 queue_push(&workq, p);
1365 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable end -----\n");
1369 /*-------------------------------------------------------------------
1371 * Add package rules for weak rules
1373 * m: visited solvables
1377 addrpmrulesforweak(Solver *solv, Map *m)
1379 Pool *pool = solv->pool;
1384 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak -----\n");
1385 /* foreach solvable in pool */
1386 for (i = n = 1; n < pool->nsolvables; i++, n++)
1388 if (i == pool->nsolvables) /* wrap i */
1390 if (MAPTST(m, i)) /* been there */
1393 s = pool->solvables + i;
1394 if (!pool_installable(pool, s)) /* only look at installable ones */
1400 /* find possible supplements */
1401 supp = s->repo->idarraydata + s->supplements;
1402 while ((sup = *supp++) != ID_NULL)
1403 if (dep_possible(solv, sup, m))
1407 /* if nothing found, check for enhances */
1408 if (!sup && s->enhances)
1410 supp = s->repo->idarraydata + s->enhances;
1411 while ((sup = *supp++) != ID_NULL)
1412 if (dep_possible(solv, sup, m))
1415 /* if nothing found, goto next solvables */
1418 addrpmrulesforsolvable(solv, s, m);
1421 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak end -----\n");
1425 /*-------------------------------------------------------------------
1427 * add package rules for possible updates
1430 * m: map of already visited solvables
1431 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
1435 addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allow_all)
1437 Pool *pool = solv->pool;
1439 /* queue and buffer for it */
1443 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1445 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1446 /* find update candidates for 's' */
1447 policy_findupdatepackages(solv, s, &qs, allow_all);
1448 /* add rule for 's' if not already done */
1449 if (!MAPTST(m, s - pool->solvables))
1450 addrpmrulesforsolvable(solv, s, m);
1451 /* foreach update candidate, add rule if not already done */
1452 for (i = 0; i < qs.count; i++)
1453 if (!MAPTST(m, qs.elements[i]))
1454 addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
1457 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1461 finddistupgradepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
1463 Pool *pool = solv->pool;
1466 policy_findupdatepackages(solv, s, qs, allow_all);
1471 policy_findupdatepackages(solv, s, qs, 1);
1475 return -SYSTEMSOLVABLE;
1478 return s - pool->solvables;
1479 /* check if it is ok to keep the installed package */
1480 for (i = 0; i < qs->count; i++)
1482 Solvable *ns = pool->solvables + qs->elements[i];
1483 if (s->evr == ns->evr && solvable_identical(pool, s, ns))
1484 return s - pool->solvables;
1486 /* nope, it must be some other package */
1487 return queue_shift(qs);
1490 /*-------------------------------------------------------------------
1492 * add rule for update
1493 * (A|A1|A2|A3...) An = update candidates for A
1495 * s = (installed) solvable
1499 addupdaterule(Solver *solv, Solvable *s, int allow_all)
1501 /* installed packages get a special upgrade allowed rule */
1502 Pool *pool = solv->pool;
1507 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule -----\n");
1508 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1509 p = s - pool->solvables;
1510 /* find update candidates for 's' */
1511 if (solv->distupgrade)
1512 p = finddistupgradepackages(solv, s, &qs, allow_all);
1514 policy_findupdatepackages(solv, s, &qs, allow_all);
1515 d = qs.count ? pool_queuetowhatprovides(pool, &qs) : 0;
1517 addrule(solv, p, d); /* allow update of s */
1518 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule end -----\n");
1522 /********************************************************************/
1526 /*-------------------------------------------------------------------
1529 * initial setup for all watches
1533 makewatches(Solver *solv)
1537 int nsolvables = solv->pool->nsolvables;
1539 sat_free(solv->watches);
1540 /* lower half for removals, upper half for installs */
1541 solv->watches = sat_calloc(2 * nsolvables, sizeof(Id));
1543 /* do it reverse so rpm rules get triggered first (XXX: obsolete?) */
1544 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1546 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1549 if (!r->w2) /* assertions do not need watches */
1552 /* see addwatches_rule(solv, r) */
1553 r->n1 = solv->watches[nsolvables + r->w1];
1554 solv->watches[nsolvables + r->w1] = r - solv->rules;
1556 r->n2 = solv->watches[nsolvables + r->w2];
1557 solv->watches[nsolvables + r->w2] = r - solv->rules;
1562 /*-------------------------------------------------------------------
1564 * add watches (for rule)
1565 * sets up watches for a single rule
1567 * see also makewatches()
1571 addwatches_rule(Solver *solv, Rule *r)
1573 int nsolvables = solv->pool->nsolvables;
1575 r->n1 = solv->watches[nsolvables + r->w1];
1576 solv->watches[nsolvables + r->w1] = r - solv->rules;
1578 r->n2 = solv->watches[nsolvables + r->w2];
1579 solv->watches[nsolvables + r->w2] = r - solv->rules;
1583 /********************************************************************/
1589 /* shortcuts to check if a literal (positive or negative) assignment
1590 * evaluates to 'true' or 'false'
1592 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1593 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
1594 #define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
1596 /*-------------------------------------------------------------------
1600 * make decision and propagate to all rules
1602 * Evaluate each term affected by the decision (linked through watches)
1603 * If we find unit rules we make new decisions based on them
1605 * Everything's fixed there, it's just finding rules that are
1608 * return : 0 = everything is OK
1609 * rule = conflict found in this rule
1613 propagate(Solver *solv, int level)
1615 Pool *pool = solv->pool;
1616 Id *rp, *next_rp; /* rule pointer, next rule pointer in linked list */
1618 Id p, pkg, other_watch;
1620 Id *decisionmap = solv->decisionmap;
1622 Id *watches = solv->watches + pool->nsolvables; /* place ptr in middle */
1624 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
1626 /* foreach non-propagated decision */
1627 while (solv->propagate_index < solv->decisionq.count)
1630 * 'pkg' was just decided
1631 * negate because our watches trigger if literal goes FALSE
1633 pkg = -solv->decisionq.elements[solv->propagate_index++];
1635 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1637 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
1638 solver_printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
1641 /* foreach rule where 'pkg' is now FALSE */
1642 for (rp = watches + pkg; *rp; rp = next_rp)
1644 r = solv->rules + *rp;
1647 /* rule is disabled, goto next */
1655 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1657 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
1658 solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
1661 /* 'pkg' was just decided (was set to FALSE)
1663 * now find other literal watch, check clause
1664 * and advance on linked list
1668 other_watch = r->w2;
1673 other_watch = r->w1;
1678 * This term is already true (through the other literal)
1679 * so we have nothing to do
1681 if (DECISIONMAP_TRUE(other_watch))
1685 * The other literal is FALSE or UNDEF
1691 /* Not a binary clause, try to move our watch.
1693 * Go over all literals and find one that is
1697 * (TRUE is also ok, in that case the rule is fulfilled)
1699 if (r->p /* we have a 'p' */
1700 && r->p != other_watch /* which is not watched */
1701 && !DECISIONMAP_FALSE(r->p)) /* and not FALSE */
1705 else /* go find a 'd' to make 'true' */
1708 we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
1710 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1712 if (p != other_watch /* which is not watched */
1713 && !DECISIONMAP_FALSE(p)) /* and not FALSE */
1721 * if we found some p that is UNDEF or TRUE, move
1724 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1727 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables + p));
1729 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables - p));
1745 watches[p] = r - solv->rules;
1748 /* search failed, thus all unwatched literals are FALSE */
1753 * unit clause found, set literal other_watch to TRUE
1756 if (DECISIONMAP_FALSE(other_watch)) /* check if literal is FALSE */
1757 return r; /* eek, a conflict! */
1759 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1761 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " unit ");
1762 solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
1765 if (other_watch > 0)
1766 decisionmap[other_watch] = level; /* install! */
1768 decisionmap[-other_watch] = -level; /* remove! */
1770 queue_push(&solv->decisionq, other_watch);
1771 queue_push(&solv->decisionq_why, r - solv->rules);
1773 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1775 Solvable *s = pool->solvables + (other_watch > 0 ? other_watch : -other_watch);
1776 if (other_watch > 0)
1777 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", solvable2str(pool, s));
1779 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", solvable2str(pool, s));
1782 } /* foreach rule involving 'pkg' */
1784 } /* while we have non-decided decisions */
1786 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
1788 return 0; /* all is well */
1792 /********************************************************************/
1795 /*-------------------------------------------------------------------
1802 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
1804 Pool *pool = solv->pool;
1807 Map seen; /* global? */
1808 Id d, v, vv, *dp, why;
1810 int num = 0, l1num = 0;
1811 int learnt_why = solv->learnt_pool.count;
1812 Id *decisionmap = solv->decisionmap;
1816 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
1817 map_init(&seen, pool->nsolvables);
1818 idx = solv->decisionq.count;
1821 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1822 solver_printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1823 queue_push(&solv->learnt_pool, c - solv->rules);
1824 d = c->d < 0 ? -c->d - 1 : c->d;
1825 dp = d ? pool->whatprovidesdata + d : 0;
1826 /* go through all literals of the rule */
1838 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1840 vv = v > 0 ? v : -v;
1841 if (MAPTST(&seen, vv))
1843 l = solv->decisionmap[vv];
1848 l1num++; /* need to do this one in level1 pass */
1849 else if (l == level)
1850 num++; /* need to do this one as well */
1853 queue_push(&r, v); /* not level1 or conflict level, add to new rule */
1859 if (!num && !--l1num)
1860 break; /* all level 1 literals done */
1864 v = solv->decisionq.elements[--idx];
1865 vv = v > 0 ? v : -v;
1866 if (MAPTST(&seen, vv))
1870 if (num && --num == 0)
1872 *pr = -v; /* so that v doesn't get lost */
1875 POOL_DEBUG(SAT_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
1876 for (i = 0; i < r.count; i++)
1879 MAPCLR(&seen, v > 0 ? v : -v);
1881 /* only level 1 marks left */
1885 why = solv->decisionq_why.elements[idx];
1886 if (!why) /* just in case, maybe for SYSTEMSOLVABLE */
1888 c = solv->rules + why;
1894 else if (r.count == 1 && r.elements[0] < 0)
1895 *dr = r.elements[0];
1897 *dr = pool_queuetowhatprovides(pool, &r);
1898 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1900 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
1901 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, *pr);
1902 for (i = 0; i < r.count; i++)
1903 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
1905 /* push end marker on learnt reasons stack */
1906 queue_push(&solv->learnt_pool, 0);
1909 solv->stats_learned++;
1914 /*-------------------------------------------------------------------
1918 * reset the solver decisions to right after the rpm rules.
1919 * called after rules have been enabled/disabled
1923 reset_solver(Solver *solv)
1925 Pool *pool = solv->pool;
1929 /* rewind decisions to direct rpm rule assertions */
1930 for (i = solv->decisionq.count - 1; i >= solv->directdecisions; i--)
1932 v = solv->decisionq.elements[i];
1933 solv->decisionmap[v > 0 ? v : -v] = 0;
1936 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions done reduced from %d to %d\n", solv->decisionq.count, solv->directdecisions);
1938 solv->decisionq_why.count = solv->directdecisions;
1939 solv->decisionq.count = solv->directdecisions;
1940 solv->recommends_index = -1;
1941 solv->propagate_index = 0;
1943 /* adapt learnt rule status to new set of enabled/disabled rules */
1944 enabledisablelearntrules(solv);
1946 /* redo all job/update decisions */
1947 makeruledecisions(solv);
1948 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
1952 /*-------------------------------------------------------------------
1954 * analyze_unsolvable_rule
1958 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp)
1960 Pool *pool = solv->pool;
1962 Id why = r - solv->rules;
1964 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1965 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1966 if (solv->learntrules && why >= solv->learntrules)
1968 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1969 if (solv->learnt_pool.elements[i] > 0)
1970 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp);
1973 if (MAPTST(&solv->weakrulemap, why))
1974 if (!*lastweakp || why > *lastweakp)
1976 /* do not add rpm rules to problem */
1977 if (why < solv->rpmrules_end)
1979 /* turn rule into problem */
1980 if (why >= solv->jobrules && why < solv->jobrules_end)
1981 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1982 /* return if problem already countains our rule */
1983 if (solv->problems.count)
1985 for (i = solv->problems.count - 1; i >= 0; i--)
1986 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
1988 else if (solv->problems.elements[i] == why)
1991 queue_push(&solv->problems, why);
1995 /*-------------------------------------------------------------------
1997 * analyze_unsolvable
1999 * return: 1 - disabled some rules, try again
2004 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
2006 Pool *pool = solv->pool;
2008 Map seen; /* global to speed things up? */
2009 Id d, v, vv, *dp, why;
2011 Id *decisionmap = solv->decisionmap;
2012 int oldproblemcount;
2013 int oldlearntpoolcount;
2016 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
2017 solv->stats_unsolvable++;
2018 oldproblemcount = solv->problems.count;
2019 oldlearntpoolcount = solv->learnt_pool.count;
2021 /* make room for proof index */
2022 /* must update it later, as analyze_unsolvable_rule would confuse
2023 * it with a rule index if we put the real value in already */
2024 queue_push(&solv->problems, 0);
2027 map_init(&seen, pool->nsolvables);
2028 queue_push(&solv->learnt_pool, r - solv->rules);
2030 analyze_unsolvable_rule(solv, r, &lastweak);
2031 d = r->d < 0 ? -r->d - 1 : r->d;
2032 dp = d ? pool->whatprovidesdata + d : 0;
2043 if (DECISIONMAP_TRUE(v)) /* the one true literal */
2045 vv = v > 0 ? v : -v;
2046 l = solv->decisionmap[vv];
2051 idx = solv->decisionq.count;
2054 v = solv->decisionq.elements[--idx];
2055 vv = v > 0 ? v : -v;
2056 if (!MAPTST(&seen, vv))
2058 why = solv->decisionq_why.elements[idx];
2059 queue_push(&solv->learnt_pool, why);
2060 r = solv->rules + why;
2061 analyze_unsolvable_rule(solv, r, &lastweak);
2062 d = r->d < 0 ? -r->d - 1 : r->d;
2063 dp = d ? pool->whatprovidesdata + d : 0;
2074 if (DECISIONMAP_TRUE(v)) /* the one true literal */
2076 vv = v > 0 ? v : -v;
2077 l = solv->decisionmap[vv];
2084 queue_push(&solv->problems, 0); /* mark end of this problem */
2088 /* disable last weak rule */
2089 solv->problems.count = oldproblemcount;
2090 solv->learnt_pool.count = oldlearntpoolcount;
2091 r = solv->rules + lastweak;
2092 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling ");
2093 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
2094 disablerule(solv, r);
2100 queue_push(&solv->learnt_pool, 0);
2101 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
2105 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
2106 disableproblem(solv, solv->problems.elements[i]);
2107 /* XXX: might want to enable all weak rules again */
2111 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
2116 /********************************************************************/
2117 /* Decision revert */
2119 /*-------------------------------------------------------------------
2122 * revert decision at level
2126 revert(Solver *solv, int level)
2128 Pool *pool = solv->pool;
2130 while (solv->decisionq.count)
2132 v = solv->decisionq.elements[solv->decisionq.count - 1];
2133 vv = v > 0 ? v : -v;
2134 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
2136 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
2137 if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
2138 solv->recommendations.count--;
2139 solv->decisionmap[vv] = 0;
2140 solv->decisionq.count--;
2141 solv->decisionq_why.count--;
2142 solv->propagate_index = solv->decisionq.count;
2144 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
2146 solv->branches.count--;
2147 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
2148 solv->branches.count--;
2150 solv->recommends_index = -1;
2154 /*-------------------------------------------------------------------
2156 * watch2onhighest - put watch2 on literal with highest level
2160 watch2onhighest(Solver *solv, Rule *r)
2165 d = r->d < 0 ? -r->d - 1 : r->d;
2167 return; /* binary rule, both watches are set */
2168 dp = solv->pool->whatprovidesdata + d;
2169 while ((v = *dp++) != 0)
2171 l = solv->decisionmap[v < 0 ? -v : v];
2183 /*-------------------------------------------------------------------
2187 * add free decision (solvable to install) to decisionq
2188 * increase level and propagate decision
2189 * return if no conflict.
2191 * in conflict case, analyze conflict rule, add resulting
2192 * rule to learnt rule set, make decision from learnt
2193 * rule (always unit) and re-propagate.
2195 * returns the new solver level or 0 if unsolvable
2200 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
2202 Pool *pool = solv->pool;
2211 solv->decisionmap[decision] = level;
2213 solv->decisionmap[-decision] = -level;
2214 queue_push(&solv->decisionq, decision);
2215 queue_push(&solv->decisionq_why, 0);
2219 r = propagate(solv, level);
2223 return analyze_unsolvable(solv, r, disablerules);
2224 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
2225 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
2226 assert(l > 0 && l < level);
2227 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
2229 revert(solv, level);
2230 r = addrule(solv, p, d); /* p requires d */
2232 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
2233 queue_push(&solv->learnt_why, why);
2236 /* at least 2 literals, needs watches */
2237 watch2onhighest(solv, r);
2238 addwatches_rule(solv, r);
2242 /* learnt rule is an assertion */
2243 queue_push(&solv->ruleassertions, r - solv->rules);
2245 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
2246 queue_push(&solv->decisionq, p);
2247 queue_push(&solv->decisionq_why, r - solv->rules);
2248 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
2250 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
2251 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
2252 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
2253 solver_printrule(solv, SAT_DEBUG_ANALYZE, r);
2260 /*-------------------------------------------------------------------
2262 * select and install
2264 * install best package from the queue. We add an extra package, inst, if
2265 * provided. See comment in weak install section.
2267 * returns the new solver level or 0 if unsolvable
2272 selectandinstall(Solver *solv, int level, Queue *dq, Id inst, int disablerules)
2274 Pool *pool = solv->pool;
2278 if (dq->count > 1 || inst)
2279 policy_filter_unwanted(solv, dq, inst, POLICY_MODE_CHOOSE);
2284 /* choose the supplemented one */
2285 for (i = 0; i < dq->count; i++)
2286 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
2290 for (i = 1; i < dq->count; i++)
2291 queue_push(&solv->branches, dq->elements[i]);
2292 queue_push(&solv->branches, -level);
2296 p = dq->elements[i];
2298 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvable2str(pool, pool->solvables + p));
2300 return setpropagatelearn(solv, level, p, disablerules);
2304 /********************************************************************/
2305 /* Main solver interface */
2308 /*-------------------------------------------------------------------
2311 * create solver structure
2313 * pool: all available solvables
2314 * installed: installed Solvables
2317 * Upon solving, rules are created to flag the Solvables
2318 * of the 'installed' Repo as installed.
2322 solver_create(Pool *pool, Repo *installed)
2325 solv = (Solver *)sat_calloc(1, sizeof(Solver));
2327 solv->installed = installed;
2329 queue_init(&solv->ruletojob);
2330 queue_init(&solv->decisionq);
2331 queue_init(&solv->decisionq_why);
2332 queue_init(&solv->problems);
2333 queue_init(&solv->suggestions);
2334 queue_init(&solv->recommendations);
2335 queue_init(&solv->orphaned);
2336 queue_init(&solv->learnt_why);
2337 queue_init(&solv->learnt_pool);
2338 queue_init(&solv->branches);
2339 queue_init(&solv->covenantq);
2340 queue_init(&solv->weakruleq);
2341 queue_init(&solv->ruleassertions);
2343 map_init(&solv->recommendsmap, pool->nsolvables);
2344 map_init(&solv->suggestsmap, pool->nsolvables);
2345 map_init(&solv->noupdate, installed ? installed->end - installed->start : 0);
2346 solv->recommends_index = 0;
2348 solv->decisionmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
2350 solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
2351 memset(solv->rules, 0, sizeof(Rule));
2357 /*-------------------------------------------------------------------
2363 solver_free(Solver *solv)
2365 queue_free(&solv->ruletojob);
2366 queue_free(&solv->decisionq);
2367 queue_free(&solv->decisionq_why);
2368 queue_free(&solv->learnt_why);
2369 queue_free(&solv->learnt_pool);
2370 queue_free(&solv->problems);
2371 queue_free(&solv->suggestions);
2372 queue_free(&solv->recommendations);
2373 queue_free(&solv->orphaned);
2374 queue_free(&solv->branches);
2375 queue_free(&solv->covenantq);
2376 queue_free(&solv->weakruleq);
2377 queue_free(&solv->ruleassertions);
2379 map_free(&solv->recommendsmap);
2380 map_free(&solv->suggestsmap);
2381 map_free(&solv->noupdate);
2382 map_free(&solv->weakrulemap);
2383 map_free(&solv->noobsoletes);
2385 sat_free(solv->decisionmap);
2386 sat_free(solv->rules);
2387 sat_free(solv->watches);
2388 sat_free(solv->obsoletes);
2389 sat_free(solv->obsoletes_data);
2394 /*-------------------------------------------------------------------
2398 * all rules have been set up, now actually run the solver
2403 run_solver(Solver *solv, int disablerules, int doweak)
2405 Queue dq; /* local decisionqueue */
2406 Queue dqs; /* local decisionqueue for supplements */
2412 Pool *pool = solv->pool;
2415 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
2417 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
2418 for (i = 1; i < solv->nrules; i++)
2419 solver_printruleclass(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
2422 POOL_DEBUG(SAT_DEBUG_STATS, "initial decisions: %d\n", solv->decisionq.count);
2424 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2425 solver_printdecisions(solv);
2427 /* start SAT algorithm */
2429 systemlevel = level + 1;
2430 POOL_DEBUG(SAT_DEBUG_STATS, "solving...\n");
2436 * here's the main loop:
2437 * 1) propagate new decisions (only needed for level 1)
2438 * 2) try to keep installed packages
2439 * 3) fulfill all unresolved rules
2440 * 4) install recommended packages
2441 * 5) minimalize solution if we had choices
2442 * if we encounter a problem, we rewind to a safe level and restart
2454 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
2455 if ((r = propagate(solv, level)) != 0)
2457 if (analyze_unsolvable(solv, r, disablerules))
2464 if (level < systemlevel)
2466 POOL_DEBUG(SAT_DEBUG_STATS, "resolving job rules\n");
2467 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2470 if (r->d < 0) /* ignore disabled rules */
2473 FOR_RULELITERALS(l, dp, r)
2477 if (solv->decisionmap[-l] <= 0)
2482 if (solv->decisionmap[l] > 0)
2484 if (solv->decisionmap[l] == 0)
2490 /* prune to installed if not updating */
2491 if (!solv->updatesystem && solv->installed && dq.count > 1)
2494 for (j = k = 0; j < dq.count; j++)
2496 Solvable *s = pool->solvables + dq.elements[j];
2497 if (s->repo == solv->installed)
2498 dq.elements[k++] = dq.elements[j];
2504 level = selectandinstall(solv, level, &dq, 0, disablerules);
2510 if (level <= olevel)
2513 systemlevel = level + 1;
2514 if (i < solv->jobrules_end)
2520 * installed packages
2523 if (level < systemlevel && solv->installed && solv->installed->nsolvables)
2525 if (!solv->updatesystem)
2528 * Normal run (non-updating)
2529 * Keep as many packages installed as possible
2531 POOL_DEBUG(SAT_DEBUG_STATS, "installing old packages\n");
2533 for (i = solv->installed->start; i < solv->installed->end; i++)
2535 s = pool->solvables + i;
2537 /* skip if not installed */
2538 if (s->repo != solv->installed)
2541 /* skip if already decided */
2542 if (solv->decisionmap[i] != 0)
2545 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "keeping %s\n", solvable2str(pool, s));
2548 level = setpropagatelearn(solv, level, i, disablerules);
2550 if (level == 0) /* unsolvable */
2555 if (level <= olevel)
2558 if (i < solv->installed->end)
2562 POOL_DEBUG(SAT_DEBUG_STATS, "resolving update/feature rules\n");
2564 for (i = solv->installed->start, r = solv->rules + solv->updaterules; i < solv->installed->end; i++, r++)
2568 s = pool->solvables + i;
2570 /* skip if not installed (can't update) */
2571 if (s->repo != solv->installed)
2573 /* skip if already decided */
2574 if (solv->decisionmap[i] > 0)
2577 /* noupdate is set if a job is erasing the installed solvable or installing a specific version */
2578 if (MAPTST(&solv->noupdate, i - solv->installed->start))
2584 if (rr->d < 0) /* disabled -> look at feature rule ? */
2585 rr -= solv->installed->end - solv->installed->start;
2586 if (!rr->p) /* identical to update rule? */
2591 FOR_RULELITERALS(p, dp, rr)
2593 if (solv->decisionmap[p] > 0)
2595 if (solv->decisionmap[p] == 0)
2598 if (p || !dq.count) /* already fulfilled or empty */
2600 if (dq.elements[0] == i)
2601 inst = queue_shift(&dq);
2605 /* FIXME: it is handled a bit different because we do not want
2606 * to have it pruned just because it is not recommened.
2607 * we should not prune installed packages instead.
2609 level = selectandinstall(solv, level, &dq, inst, disablerules);
2615 if (level <= olevel)
2618 systemlevel = level + 1;
2619 if (i < solv->installed->end)
2623 if (level < systemlevel)
2624 systemlevel = level;
2630 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unresolved rules\n");
2631 for (i = 1, n = 1; ; i++, n++)
2633 if (n == solv->nrules)
2635 if (i == solv->nrules)
2637 r = solv->rules + i;
2638 if (r->d < 0) /* ignore disabled rules */
2643 /* binary or unary rule */
2644 /* need two positive undecided literals */
2645 if (r->p < 0 || r->w2 <= 0)
2647 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2649 queue_push(&dq, r->p);
2650 queue_push(&dq, r->w2);
2655 * all negative literals are installed
2656 * no positive literal is installed
2657 * i.e. the rule is not fulfilled and we
2658 * just need to decide on the positive literals
2662 if (solv->decisionmap[-r->p] <= 0)
2667 if (solv->decisionmap[r->p] > 0)
2669 if (solv->decisionmap[r->p] == 0)
2670 queue_push(&dq, r->p);
2672 dp = pool->whatprovidesdata + r->d;
2673 while ((p = *dp++) != 0)
2677 if (solv->decisionmap[-p] <= 0)
2682 if (solv->decisionmap[p] > 0)
2684 if (solv->decisionmap[p] == 0)
2691 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
2693 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
2694 solver_printruleclass(solv, SAT_DEBUG_PROPAGATE, r);
2696 /* dq.count < 2 cannot happen as this means that
2697 * the rule is unit */
2698 assert(dq.count > 1);
2701 level = selectandinstall(solv, level, &dq, 0, disablerules);
2707 if (level < systemlevel)
2710 } /* for(), decide */
2712 if (n != solv->nrules) /* continue if level < systemlevel */
2719 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended packages\n");
2722 for (i = 1; i < pool->nsolvables; i++)
2724 if (solv->decisionmap[i] < 0)
2726 if (solv->decisionmap[i] > 0)
2728 /* installed, check for recommends */
2729 Id *recp, rec, *pp, p;
2730 s = pool->solvables + i;
2731 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
2733 /* XXX need to special case AND ? */
2736 recp = s->repo->idarraydata + s->recommends;
2737 while ((rec = *recp++) != 0)
2740 FOR_PROVIDES(p, pp, rec)
2742 if (solv->decisionmap[p] > 0)
2747 else if (solv->decisionmap[p] == 0)
2749 queue_pushunique(&dq, p);
2757 s = pool->solvables + i;
2758 if (!s->supplements)
2760 if (!pool_installable(pool, s))
2762 if (!solver_is_supplementing(solv, s))
2764 if (solv->ignorealreadyrecommended && solv->installed)
2765 queue_pushunique(&dqs, i); /* needs filter */
2767 queue_pushunique(&dq, i);
2770 if (solv->ignorealreadyrecommended && dqs.count)
2772 /* turn off all new packages */
2773 for (i = 0; i < solv->decisionq.count; i++)
2775 p = solv->decisionq.elements[i];
2778 s = pool->solvables + p;
2779 if (s->repo && s->repo != solv->installed)
2780 solv->decisionmap[p] = -solv->decisionmap[p];
2782 /* filter out old supplements */
2783 for (i = 0; i < dqs.count; i++)
2785 p = dqs.elements[i];
2786 s = pool->solvables + p;
2787 if (!s->supplements)
2789 if (!solver_is_supplementing(solv, s))
2790 queue_pushunique(&dq, p);
2792 /* undo turning off */
2793 for (i = 0; i < solv->decisionq.count; i++)
2795 p = solv->decisionq.elements[i];
2798 s = pool->solvables + p;
2799 if (s->repo && s->repo != solv->installed)
2800 solv->decisionmap[p] = -solv->decisionmap[p];
2806 policy_filter_unwanted(solv, &dq, 0, POLICY_MODE_RECOMMEND);
2808 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended %s\n", solvable2str(pool, pool->solvables + p));
2809 queue_push(&solv->recommendations, p);
2810 level = setpropagatelearn(solv, level, p, 0);
2815 if (solv->distupgrade && solv->installed)
2817 /* let's see if we can install some unsupported package */
2819 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unsupported packages\n");
2820 for (i = solv->installed->start, ri = 0; i < solv->installed->end; i++, ri++)
2822 s = pool->solvables + i;
2823 if (s->repo != solv->installed)
2825 if (solv->decisionmap[i])
2827 if (!solv->rules[solv->updaterules + ri].p && !solv->rules[solv->featurerules + ri].p)
2830 if (i < solv->installed->end)
2832 if (solv->distupgrade_removeunsupported)
2834 POOL_DEBUG(SAT_DEBUG_STATS, "removing unsupported %s\n", solvable2str(pool, pool->solvables + i));
2835 level = setpropagatelearn(solv, level, -i, 0);
2839 POOL_DEBUG(SAT_DEBUG_STATS, "keeping unsupported %s\n", solvable2str(pool, pool->solvables + i));
2840 level = setpropagatelearn(solv, level, i, 0);
2846 if (solv->solution_callback)
2848 solv->solution_callback(solv, solv->solution_callback_data);
2849 if (solv->branches.count)
2851 int i = solv->branches.count - 1;
2852 int l = -solv->branches.elements[i];
2854 if (solv->branches.elements[i - 1] < 0)
2856 p = solv->branches.elements[i];
2857 POOL_DEBUG(SAT_DEBUG_STATS, "branching with %s\n", solvable2str(pool, pool->solvables + p));
2859 for (j = i + 1; j < solv->branches.count; j++)
2860 queue_push(&dq, solv->branches.elements[j]);
2861 solv->branches.count = i;
2863 revert(solv, level);
2865 for (j = 0; j < dq.count; j++)
2866 queue_push(&solv->branches, dq.elements[j]);
2868 level = setpropagatelearn(solv, level, p, disablerules);
2876 /* all branches done, we're finally finished */
2880 /* minimization step */
2881 if (solv->branches.count)
2883 int l = 0, lasti = -1, lastl = -1;
2885 for (i = solv->branches.count - 1; i >= 0; i--)
2887 p = solv->branches.elements[i];
2890 else if (p > 0 && solv->decisionmap[p] > l + 1)
2898 /* kill old solvable so that we do not loop */
2899 p = solv->branches.elements[lasti];
2900 solv->branches.elements[lasti] = 0;
2901 POOL_DEBUG(SAT_DEBUG_STATS, "minimizing %d -> %d with %s\n", solv->decisionmap[p], l, solvable2str(pool, pool->solvables + p));
2904 revert(solv, level);
2906 level = setpropagatelearn(solv, level, p, disablerules);
2917 POOL_DEBUG(SAT_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable\n", solv->stats_learned, solv->stats_unsolvable);
2919 POOL_DEBUG(SAT_DEBUG_STATS, "done solving.\n\n");
2925 /*-------------------------------------------------------------------
2929 * at this point, all rules that led to conflicts are disabled.
2930 * we re-enable all rules of a problem set but rule "sug", then
2931 * continue to disable more rules until there as again a solution.
2934 /* FIXME: think about conflicting assertions */
2937 refine_suggestion(Solver *solv, Queue *job, Id *problem, Id sug, Queue *refined)
2939 Pool *pool = solv->pool;
2945 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2947 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion start\n");
2948 for (i = 0; problem[i]; i++)
2950 if (problem[i] == sug)
2951 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "=> ");
2952 solver_printproblem(solv, problem[i]);
2955 queue_init(&disabled);
2956 queue_empty(refined);
2957 queue_push(refined, sug);
2959 /* re-enable all problem rules with the exception of "sug"(gestion) */
2963 for (i = 0; problem[i]; i++)
2964 if (problem[i] != sug)
2965 enableproblem(solv, problem[i]);
2968 disableupdaterules(solv, job, -(sug + 1));
2969 else if (sug >= solv->updaterules && sug < solv->updaterules_end)
2971 /* enable feature rule */
2972 Rule *r = solv->rules + solv->featurerules + (sug - solv->updaterules);
2974 enablerule(solv, r);
2977 enableweakrules(solv);
2981 int njob, nfeature, nupdate;
2982 queue_empty(&solv->problems);
2983 revert(solv, 1); /* XXX no longer needed? */
2986 if (!solv->problems.count)
2987 run_solver(solv, 0, 0);
2989 if (!solv->problems.count)
2991 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no more problems!\n");
2992 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2993 solver_printdecisions(solv);
2994 break; /* great, no more problems */
2996 disabledcnt = disabled.count;
2997 /* start with 1 to skip over proof index */
2998 njob = nfeature = nupdate = 0;
2999 for (i = 1; i < solv->problems.count - 1; i++)
3001 /* ignore solutions in refined */
3002 v = solv->problems.elements[i];
3004 break; /* end of problem reached */
3005 for (j = 0; problem[j]; j++)
3006 if (problem[j] != sug && problem[j] == v)
3010 if (v >= solv->featurerules && v < solv->featurerules_end)
3016 queue_push(&disabled, v);
3018 if (disabled.count == disabledcnt)
3020 /* no solution found, this was an invalid suggestion! */
3021 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no solution found!\n");
3025 if (!njob && nupdate && nfeature)
3027 /* got only update rules, filter out feature rules */
3028 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "throwing away feature rules\n");
3029 for (i = j = disabledcnt; i < disabled.count; i++)
3031 v = disabled.elements[i];
3032 if (v < solv->featurerules || v >= solv->featurerules_end)
3033 disabled.elements[j++] = v;
3038 if (disabled.count == disabledcnt + 1)
3040 /* just one suggestion, add it to refined list */
3041 v = disabled.elements[disabledcnt];
3043 queue_push(refined, v); /* do not record feature rules */
3044 disableproblem(solv, v);
3045 if (v >= solv->updaterules && v < solv->updaterules_end)
3047 Rule *r = solv->rules + (v - solv->updaterules + solv->featurerules);
3049 enablerule(solv, r); /* enable corresponding feature rule */
3052 disableupdaterules(solv, job, -(v + 1));
3056 /* more than one solution, disable all */
3057 /* do not push anything on refine list, as we do not know which solution to choose */
3058 /* thus, the user will get another problem if he selects this solution, where he
3059 * can choose the right one */
3060 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
3062 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "more than one solution found:\n");
3063 for (i = disabledcnt; i < disabled.count; i++)
3064 solver_printproblem(solv, disabled.elements[i]);
3066 for (i = disabledcnt; i < disabled.count; i++)
3068 v = disabled.elements[i];
3069 disableproblem(solv, v);
3070 if (v >= solv->updaterules && v < solv->updaterules_end)
3072 Rule *r = solv->rules + (v - solv->updaterules + solv->featurerules);
3074 enablerule(solv, r);
3079 /* all done, get us back into the same state as before */
3080 /* enable refined rules again */
3081 for (i = 0; i < disabled.count; i++)
3082 enableproblem(solv, disabled.elements[i]);
3083 /* disable problem rules again */
3086 for (i = 0; problem[i]; i++)
3087 enableproblem(solv, problem[i]);
3088 disableupdaterules(solv, job, -1);
3090 /* disable problem rules again */
3091 for (i = 0; problem[i]; i++)
3092 disableproblem(solv, problem[i]);
3093 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion end\n");
3097 /*-------------------------------------------------------------------
3098 * sorting helper for problems
3102 problems_sortcmp(const void *ap, const void *bp)
3104 Id a = *(Id *)ap, b = *(Id *)bp;
3113 /*-------------------------------------------------------------------
3118 problems_sort(Solver *solv)
3121 if (!solv->problems.count)
3123 for (i = j = 1; i < solv->problems.count; i++)
3125 if (!solv->problems.elements[i])
3128 qsort(solv->problems.elements + j, i - j, sizeof(Id), problems_sortcmp);
3129 if (++i == solv->problems.count)
3137 /*-------------------------------------------------------------------
3138 * convert problems to solutions
3142 problems_to_solutions(Solver *solv, Queue *job)
3144 Pool *pool = solv->pool;
3152 if (!solv->problems.count)
3154 problems_sort(solv);
3155 queue_clone(&problems, &solv->problems);
3156 queue_init(&solution);
3157 queue_init(&solutions);
3158 /* copy over proof index */
3159 queue_push(&solutions, problems.elements[0]);
3160 problem = problems.elements + 1;
3161 for (i = 1; i < problems.count; i++)
3163 Id v = problems.elements[i];
3166 /* mark end of this problem */
3167 queue_push(&solutions, 0);
3168 queue_push(&solutions, 0);
3169 if (i + 1 == problems.count)
3171 /* copy over proof of next problem */
3172 queue_push(&solutions, problems.elements[i + 1]);
3174 problem = problems.elements + i + 1;
3177 refine_suggestion(solv, job, problem, v, &solution);
3178 if (!solution.count)
3179 continue; /* this solution didn't work out */
3182 for (j = 0; j < solution.count; j++)
3184 why = solution.elements[j];
3185 /* must be either job descriptor or update rule */
3186 assert(why < 0 || (why >= solv->updaterules && why < solv->updaterules_end));
3188 solver_printproblem(solv, why);
3192 /* job descriptor */
3193 queue_push(&solutions, 0);
3194 queue_push(&solutions, -why);
3198 /* update rule, find replacement package */
3199 Id p, d, *dp, rp = 0;
3201 p = solv->installed->start + (why - solv->updaterules);
3202 rr = solv->rules + solv->featurerules + (why - solv->updaterules);
3204 rr = solv->rules + why;
3205 if (solv->distupgrade && solv->rules[why].p != p && solv->decisionmap[p] > 0)
3207 /* distupgrade case, allow to keep old package */
3208 queue_push(&solutions, p);
3209 queue_push(&solutions, p);
3213 if (solv->decisionmap[p] > 0)
3214 continue; /* false alarm, turned out we can keep the package */
3217 d = rr->d < 0 ? -rr->d - 1 : rr->d;
3220 if (solv->decisionmap[rr->w2] > 0 && pool->solvables[rr->w2].repo != solv->installed)
3225 for (dp = pool->whatprovidesdata + d; *dp; dp++)
3227 if (solv->decisionmap[*dp] > 0 && pool->solvables[*dp].repo != solv->installed)
3235 queue_push(&solutions, p);
3236 queue_push(&solutions, rp);
3240 /* mark end of this solution */
3243 queue_push(&solutions, 0);
3244 queue_push(&solutions, 0);
3248 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "Oops, everything was fine?\n");
3251 queue_free(&solution);
3252 queue_free(&problems);
3253 /* copy queue over to solutions */
3254 queue_free(&solv->problems);
3255 queue_clone(&solv->problems, &solutions);
3257 /* bring solver back into problem state */
3258 revert(solv, 1); /* XXX move to reset_solver? */
3261 assert(solv->problems.count == solutions.count);
3262 queue_free(&solutions);
3266 /*-------------------------------------------------------------------
3270 * advance to next problem
3274 solver_next_problem(Solver *solv, Id problem)
3278 return solv->problems.count ? 1 : 0;
3279 pp = solv->problems.elements + problem;
3280 while (pp[0] || pp[1])
3284 while (pp[0] || pp[1])
3289 problem = pp - solv->problems.elements;
3290 if (problem >= solv->problems.count)
3296 /*-------------------------------------------------------------------
3302 solver_next_solution(Solver *solv, Id problem, Id solution)
3308 pp = solv->problems.elements + solution;
3309 return pp[0] || pp[1] ? solution : 0;
3311 pp = solv->problems.elements + solution;
3312 while (pp[0] || pp[1])
3315 solution = pp - solv->problems.elements;
3316 return pp[0] || pp[1] ? solution : 0;
3320 /*-------------------------------------------------------------------
3322 * solution element iterator
3326 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
3329 element = element ? element + 2 : solution;
3330 pp = solv->problems.elements + element;
3331 if (!(pp[0] || pp[1]))
3339 /*-------------------------------------------------------------------
3341 * Retrieve information about a problematic rule
3343 * this is basically the reverse of addrpmrulesforsolvable
3347 solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp)
3349 Pool *pool = solv->pool;
3350 Repo *installed = solv->installed;
3354 Id p, d, w2, *pp, req, *reqp, con, *conp, obs, *obsp, *dp;
3357 if (rid >= solv->jobrules && rid < solv->jobrules_end)
3360 r = solv->rules + rid;
3361 p = solv->ruletojob.elements[rid - solv->jobrules];
3362 *depp = job->elements[p + 1];
3364 *targetp = job->elements[p];
3365 d = r->d < 0 ? -r->d - 1 : r->d;
3366 if (d == 0 && r->w2 == 0 && r->p == -SYSTEMSOLVABLE && (job->elements[p] & SOLVER_SELECTMASK) != SOLVER_SOLVABLE_ONE_OF)
3367 return SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP;
3368 return SOLVER_PROBLEM_JOB_RULE;
3370 if (rid >= solv->updaterules && rid < solv->updaterules_end)
3373 *sourcep = solv->installed->start + (rid - solv->updaterules);
3375 return SOLVER_PROBLEM_UPDATE_RULE;
3377 assert(rid < solv->rpmrules_end);
3378 r = solv->rules + rid;
3380 d = r->d < 0 ? -r->d - 1 : r->d;
3381 if (d == 0 && r->w2 == 0)
3383 /* a rpm rule assertion */
3384 s = pool->solvables - r->p;
3385 if (installed && !solv->fixsystem && s->repo == installed)
3387 assert(!dontfix); /* dontfix packages never have a neg assertion */
3390 /* see why the package is not installable */
3391 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
3394 return SOLVER_PROBLEM_NOT_INSTALLABLE;
3396 /* check requires */
3399 reqp = s->repo->idarraydata + s->requires;
3400 while ((req = *reqp++) != 0)
3402 if (req == SOLVABLE_PREREQMARKER)
3404 dp = pool_whatprovides(pool, req);
3411 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
3414 if (!solv->allowselfconflicts && s->conflicts)
3416 conp = s->repo->idarraydata + s->conflicts;
3417 while ((con = *conp++) != 0)
3418 FOR_PROVIDES(p, pp, con)
3422 return SOLVER_PROBLEM_SELF_CONFLICT;
3425 /* should never happen */
3427 return SOLVER_PROBLEM_RPM_RULE;
3429 s = pool->solvables - r->p;
3430 if (installed && !solv->fixsystem && s->repo == installed)
3433 if (d && pool->whatprovidesdata[d] < 0)
3435 /* rule looks like -p|-c|x|x|x..., we only create this for patches with multiversion */
3436 /* reduce it to -p|-c case */
3437 w2 = pool->whatprovidesdata[d];
3439 if (d == 0 && w2 < 0)
3441 /* a package conflict */
3442 Solvable *s2 = pool->solvables - w2;
3445 if (installed && !solv->fixsystem && s2->repo == installed)
3448 /* if both packages have the same name and at least one of them
3449 * is not installed, they conflict */
3450 if (s->name == s2->name && !(installed && s->repo == installed && s2->repo == installed))
3452 /* also check noobsoletes map */
3453 if ((s->evr == s2->evr && s->arch == s2->arch) || !solv->noobsoletes.size
3454 || ((!installed || s->repo != installed) && !MAPTST(&solv->noobsoletes, -r->p))
3455 || ((!installed || s2->repo != installed) && !MAPTST(&solv->noobsoletes, -w2)))
3460 return SOLVER_PROBLEM_SAME_NAME;
3464 /* check conflicts in both directions */
3467 conp = s->repo->idarraydata + s->conflicts;
3468 while ((con = *conp++) != 0)
3470 FOR_PROVIDES(p, pp, con)
3472 if (dontfix && pool->solvables[p].repo == installed)
3479 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
3485 conp = s2->repo->idarraydata + s2->conflicts;
3486 while ((con = *conp++) != 0)
3488 FOR_PROVIDES(p, pp, con)
3490 if (dontfix2 && pool->solvables[p].repo == installed)
3497 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
3501 /* check obsoletes in both directions */
3502 if ((!installed || s->repo != installed) && s->obsoletes && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -r->p)))
3504 obsp = s->repo->idarraydata + s->obsoletes;
3505 while ((obs = *obsp++) != 0)
3507 FOR_PROVIDES(p, pp, obs)
3511 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3516 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3520 if ((!installed || s2->repo != installed) && s2->obsoletes && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -w2)))
3522 obsp = s2->repo->idarraydata + s2->obsoletes;
3523 while ((obs = *obsp++) != 0)
3525 FOR_PROVIDES(p, pp, obs)
3529 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3534 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3538 if (solv->implicitobsoleteusesprovides && (!installed || s->repo != installed) && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -r->p)))
3540 FOR_PROVIDES(p, pp, s->name)
3547 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3550 if (solv->implicitobsoleteusesprovides && (!installed || s2->repo != installed) && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -w2)))
3552 FOR_PROVIDES(p, pp, s2->name)
3559 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3562 /* all cases checked, can't happen */
3566 return SOLVER_PROBLEM_RPM_RULE;
3568 /* simple requires */
3571 reqp = s->repo->idarraydata + s->requires;
3572 while ((req = *reqp++) != 0)
3574 if (req == SOLVABLE_PREREQMARKER)
3576 dp = pool_whatprovides(pool, req);
3579 if (*dp == r->w2 && dp[1] == 0)
3582 else if (dp - pool->whatprovidesdata == d)
3590 return SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE;
3593 /* all cases checked, can't happen */
3597 return SOLVER_PROBLEM_RPM_RULE;
3601 /*-------------------------------------------------------------------
3607 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp)
3610 Id lreqr, lconr, lsysr, ljobr;
3614 lreqr = lconr = lsysr = ljobr = 0;
3615 while ((rid = solv->learnt_pool.elements[idx++]) != 0)
3618 if (rid >= solv->learntrules)
3619 findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr);
3620 else if (rid >= solv->jobrules && rid < solv->jobrules_end)
3625 else if (rid >= solv->updaterules && rid < solv->updaterules_end)
3632 assert(rid < solv->rpmrules_end);
3633 r = solv->rules + rid;
3634 d = r->d < 0 ? -r->d - 1 : r->d;
3635 if (!d && r->w2 < 0)
3642 if (!d && r->w2 == 0 && !reqassert)
3644 /* prefer assertions (XXX: bad idea?) */
3650 else if (solv->installed && r->p < 0 && solv->pool->solvables[-r->p].repo == solv->installed)
3652 /* prefer rules of installed packages */
3653 Id op = *reqrp >= 0 ? solv->rules[*reqrp].p : -*reqrp;
3654 if (op <= 0 || solv->pool->solvables[op].repo != solv->installed)
3660 if (!*reqrp && lreqr)
3662 if (!*conrp && lconr)
3664 if (!*jobrp && ljobr)
3666 if (!*sysrp && lsysr)
3671 /*-------------------------------------------------------------------
3675 * search for a rule that describes the problem to the
3676 * user. A pretty hopeless task, actually. We currently
3677 * prefer simple requires.
3681 solver_findproblemrule(Solver *solv, Id problem)
3683 Id reqr, conr, sysr, jobr;
3684 Id idx = solv->problems.elements[problem - 1];
3685 reqr = conr = sysr = jobr = 0;
3686 findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr);
3699 /*-------------------------------------------------------------------
3701 * create reverse obsoletes map for installed solvables
3703 * for each installed solvable find which packages with *different* names
3704 * obsolete the solvable.
3705 * this index is used in policy_findupdatepackages if noupdateprovide is set.
3709 create_obsolete_index(Solver *solv)
3711 Pool *pool = solv->pool;
3713 Repo *installed = solv->installed;
3714 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
3717 if (!installed || !installed->nsolvables)
3719 solv->obsoletes = obsoletes = sat_calloc(installed->end - installed->start, sizeof(Id));
3720 for (i = 1; i < pool->nsolvables; i++)
3722 s = pool->solvables + i;
3725 if (!pool_installable(pool, s))
3727 obsp = s->repo->idarraydata + s->obsoletes;
3728 while ((obs = *obsp++) != 0)
3730 FOR_PROVIDES(p, pp, obs)
3732 if (pool->solvables[p].repo != installed)
3734 if (pool->solvables[p].name == s->name)
3736 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3738 obsoletes[p - installed->start]++;
3743 for (i = 0; i < installed->nsolvables; i++)
3746 n += obsoletes[i] + 1;
3749 solv->obsoletes_data = obsoletes_data = sat_calloc(n + 1, sizeof(Id));
3750 POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
3751 for (i = pool->nsolvables - 1; i > 0; i--)
3753 s = pool->solvables + i;
3756 if (!pool_installable(pool, s))
3758 obsp = s->repo->idarraydata + s->obsoletes;
3759 while ((obs = *obsp++) != 0)
3761 FOR_PROVIDES(p, pp, obs)
3763 if (pool->solvables[p].repo != installed)
3765 if (pool->solvables[p].name == s->name)
3767 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3769 p -= installed->start;
3770 if (obsoletes_data[obsoletes[p]] != i)
3771 obsoletes_data[--obsoletes[p]] = i;
3778 /*-------------------------------------------------------------------
3780 * remove disabled conflicts
3784 removedisabledconflicts(Solver *solv, Queue *removed)
3786 Pool *pool = solv->pool;
3791 Id *decisionmap = solv->decisionmap;
3793 POOL_DEBUG(SAT_DEBUG_STATS, "removedisabledconflicts\n");
3794 queue_empty(removed);
3795 for (i = 0; i < solv->decisionq.count; i++)
3797 p = solv->decisionq.elements[i];
3800 /* a conflict. we never do conflicts on free decisions, so there
3801 * must have been an unit rule */
3802 why = solv->decisionq_why.elements[i];
3804 r = solv->rules + why;
3805 if (r->d < 0 && decisionmap[-p])
3807 /* rule is now disabled, remove from decisionmap */
3808 POOL_DEBUG(SAT_DEBUG_STATS, "removing conflict for package %s[%d]\n", solvable2str(pool, pool->solvables - p), -p);
3809 queue_push(removed, -p);
3810 queue_push(removed, decisionmap[-p]);
3811 decisionmap[-p] = 0;
3814 if (!removed->count)
3816 /* we removed some confliced packages. some of them might still
3817 * be in conflict, so search for unit rules and re-conflict */
3819 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
3821 if (i == solv->nrules)
3824 r = solv->rules + i;
3830 if (r->p < 0 && !decisionmap[-r->p])
3836 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
3838 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
3843 if (r->p < 0 && decisionmap[-r->p] == 0)
3845 if (new || DECISIONMAP_FALSE(r->p))
3847 dp = pool->whatprovidesdata + r->d;
3848 while ((p = *dp++) != 0)
3850 if (new && p == new)
3852 if (p < 0 && decisionmap[-p] == 0)
3861 else if (!DECISIONMAP_FALSE(p))
3871 POOL_DEBUG(SAT_DEBUG_STATS, "re-conflicting package %s[%d]\n", solvable2str(pool, pool->solvables - new), -new);
3872 decisionmap[-new] = -1;
3874 n = 0; /* redo all rules */
3880 /*-------------------------------------------------------------------
3882 * weaken solvable dependencies
3886 weaken_solvable_deps(Solver *solv, Id p)
3891 for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
3895 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
3896 continue; /* conflict */
3897 queue_push(&solv->weakruleq, i);
3901 /********************************************************************/
3911 solver_solve(Solver *solv, Queue *job)
3913 Pool *pool = solv->pool;
3914 Repo *installed = solv->installed;
3917 Map addedmap; /* '1' == have rpm-rules for solvable */
3918 Map installcandidatemap;
3919 Id how, what, select, name, weak, p, *pp, d;
3925 POOL_DEBUG(SAT_DEBUG_STATS, "solver started\n");
3926 POOL_DEBUG(SAT_DEBUG_STATS, "fixsystem=%d updatesystem=%d dosplitprovides=%d, noupdateprovide=%d\n", solv->fixsystem, solv->updatesystem, solv->dosplitprovides, solv->noupdateprovide);
3927 POOL_DEBUG(SAT_DEBUG_STATS, "distupgrade=%d distupgrade_removeunsupported=%d\n", solv->distupgrade, solv->distupgrade_removeunsupported);
3928 POOL_DEBUG(SAT_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allowarchchange, solv->allowvendorchange);
3929 POOL_DEBUG(SAT_DEBUG_STATS, "promoteepoch=%d, allowvirtualconflicts=%d, allowselfconflicts=%d\n", pool->promoteepoch, solv->allowvirtualconflicts, solv->allowselfconflicts);
3930 POOL_DEBUG(SAT_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d\n", solv->obsoleteusesprovides, solv->implicitobsoleteusesprovides);
3931 POOL_DEBUG(SAT_DEBUG_STATS, "dontinstallrecommended=%d, ignorealreadyrecommended=%d, dontshowinstalledrecommended=%d\n", solv->dontinstallrecommended, solv->ignorealreadyrecommended, solv->dontshowinstalledrecommended);
3932 /* create whatprovides if not already there */
3933 if (!pool->whatprovides)
3934 pool_createwhatprovides(pool);
3936 /* create obsolete index if needed */
3937 create_obsolete_index(solv);
3940 * create basic rule set of all involved packages
3941 * use addedmap bitmap to make sure we don't create rules twice
3945 /* create noobsolete map if needed */
3946 for (i = 0; i < job->count; i += 2)
3948 how = job->elements[i] & ~SOLVER_WEAK;
3949 if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
3951 what = job->elements[i + 1];
3952 select = how & SOLVER_SELECTMASK;
3953 if (!solv->noobsoletes.size)
3954 map_init(&solv->noobsoletes, pool->nsolvables);
3955 FOR_JOB_SELECT(p, pp, select, what)
3956 MAPSET(&solv->noobsoletes, p);
3959 map_init(&addedmap, pool->nsolvables);
3960 map_init(&installcandidatemap, pool->nsolvables);
3964 * always install our system solvable
3966 MAPSET(&addedmap, SYSTEMSOLVABLE);
3967 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
3968 queue_push(&solv->decisionq_why, 0);
3969 solv->decisionmap[SYSTEMSOLVABLE] = 1; /* installed at level '1' */
3972 * create rules for all package that could be involved with the solving
3973 * so called: rpm rules
3978 oldnrules = solv->nrules;
3979 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
3980 FOR_REPO_SOLVABLES(installed, p, s)
3981 addrpmrulesforsolvable(solv, s, &addedmap);
3982 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3983 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
3984 oldnrules = solv->nrules;
3985 FOR_REPO_SOLVABLES(installed, p, s)
3986 addrpmrulesforupdaters(solv, s, &addedmap, 1);
3987 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3991 * create rules for all packages involved in the job
3992 * (to be installed or removed)
3995 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
3996 oldnrules = solv->nrules;
3997 for (i = 0; i < job->count; i += 2)
3999 how = job->elements[i];
4000 what = job->elements[i + 1];
4001 select = how & SOLVER_SELECTMASK;
4003 switch (how & SOLVER_JOBMASK)
4005 case SOLVER_INSTALL:
4006 FOR_JOB_SELECT(p, pp, select, what)
4008 MAPSET(&installcandidatemap, p);
4009 addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
4013 /* FIXME: semantics? */
4014 FOR_JOB_SELECT(p, pp, select, what)
4015 addrpmrulesforupdaters(solv, pool->solvables + what, &addedmap, 0);
4019 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
4021 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for recommended/suggested packages ***\n");
4023 oldnrules = solv->nrules;
4026 * add rules for suggests, enhances
4028 addrpmrulesforweak(solv, &addedmap);
4029 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
4031 IF_POOLDEBUG (SAT_DEBUG_STATS)
4033 int possible = 0, installable = 0;
4034 for (i = 1; i < pool->nsolvables; i++)
4036 if (pool_installable(pool, pool->solvables + i))
4038 if (MAPTST(&addedmap, i))
4041 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
4045 * first pass done, we now have all the rpm rules we need.
4046 * unify existing rules before going over all job rules and
4048 * at this point the system is always solvable,
4049 * as an empty system (remove all packages) is a valid solution
4052 unifyrules(solv); /* remove duplicate rpm rules */
4054 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
4056 solv->directdecisions = solv->decisionq.count;
4057 POOL_DEBUG(SAT_DEBUG_STATS, "decisions so far: %d\n", solv->decisionq.count);
4060 * create feature rules
4062 * foreach installed:
4063 * create assertion (keep installed, if no update available)
4065 * create update rule (A|update1(A)|update2(A)|...)
4067 * those are used later on to keep a version of the installed packages in
4071 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add feature rules ***\n");
4072 solv->featurerules = solv->nrules; /* mark start of feature rules */
4075 /* foreach possibly installed solvable */
4076 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
4078 if (s->repo != installed)
4080 addrule(solv, 0, 0); /* create dummy rule */
4083 addupdaterule(solv, s, 1); /* allow s to be updated */
4086 * assert one rule per installed solvable,
4087 * either an assertion (A)
4088 * or a possible update (A|update1(A)|update2(A)|...)
4090 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
4092 solv->featurerules_end = solv->nrules;
4095 * Add update rules for installed solvables
4097 * almost identical to feature rules
4098 * except that downgrades/archchanges/vendorchanges are not allowed
4101 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add update rules ***\n");
4102 solv->updaterules = solv->nrules;
4105 { /* foreach installed solvables */
4106 /* we create all update rules, but disable some later on depending on the job */
4107 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
4111 if (s->repo != installed)
4113 addrule(solv, 0, 0); /* create dummy rule */
4116 addupdaterule(solv, s, 0); /* allowall = 0: downgrades not allowed */
4118 * check for and remove duplicate
4120 r = solv->rules + solv->nrules - 1; /* r: update rule */
4121 sr = r - (installed->end - installed->start); /* sr: feature rule */
4122 /* it's orphaned if there is no feature rule or the feature rule
4123 * consists just of the installed package */
4124 if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
4125 queue_push(&solv->orphaned, i);
4128 assert(!sr->p); /* can't have feature rule and no update rule */
4131 unifyrules_sortcmp_data = pool;
4132 if (!unifyrules_sortcmp(r, sr))
4134 /* identical rule, kill unneeded rule */
4135 if (solv->allowuninstall)
4137 /* keep feature rule, make it weak */
4138 memset(r, 0, sizeof(*r));
4139 queue_push(&solv->weakruleq, sr - solv->rules);
4143 /* keep update rule */
4144 memset(sr, 0, sizeof(*sr));
4147 else if (solv->allowuninstall)
4149 /* make both feature and update rule weak */
4150 queue_push(&solv->weakruleq, r - solv->rules);
4151 queue_push(&solv->weakruleq, sr - solv->rules);
4154 disablerule(solv, sr);
4156 /* consistency check: we added a rule for _every_ installed solvable */
4157 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
4159 solv->updaterules_end = solv->nrules;
4163 * now add all job rules
4166 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
4168 solv->jobrules = solv->nrules;
4169 for (i = 0; i < job->count; i += 2)
4171 oldnrules = solv->nrules;
4173 how = job->elements[i];
4174 what = job->elements[i + 1];
4175 weak = how & SOLVER_WEAK;
4176 select = how & SOLVER_SELECTMASK;
4177 switch (how & SOLVER_JOBMASK)
4179 case SOLVER_INSTALL:
4180 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4181 if (select == SOLVER_SOLVABLE)
4189 FOR_JOB_SELECT(p, pp, select, what)
4193 /* no candidate found, make this an impossible rule */
4194 queue_push(&q, -SYSTEMSOLVABLE);
4196 p = queue_shift(&q); /* get first candidate */
4197 d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q); /* internalize */
4199 addrule(solv, p, d); /* add install rule */
4200 queue_push(&solv->ruletojob, i);
4202 queue_push(&solv->weakruleq, solv->nrules - 1);
4205 POOL_DEBUG(SAT_DEBUG_JOB, "job: %serase %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4206 if (select == SOLVER_SOLVABLE && solv->installed && pool->solvables[what].repo == solv->installed)
4208 /* special case for "erase a specific solvable": we also
4209 * erase all other solvables with that name, so that they
4210 * don't get picked up as replacement */
4211 name = pool->solvables[what].name;
4212 FOR_PROVIDES(p, pp, name)
4216 s = pool->solvables + p;
4217 if (s->name != name)
4219 /* keep other versions installed */
4220 if (s->repo == solv->installed)
4222 /* keep installcandidates of other jobs */
4223 if (MAPTST(&installcandidatemap, p))
4225 addrule(solv, -p, 0); /* remove by Id */
4226 queue_push(&solv->ruletojob, i);
4228 queue_push(&solv->weakruleq, solv->nrules - 1);
4231 FOR_JOB_SELECT(p, pp, select, what)
4233 addrule(solv, -p, 0);
4234 queue_push(&solv->ruletojob, i);
4236 queue_push(&solv->weakruleq, solv->nrules - 1);
4241 POOL_DEBUG(SAT_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4242 if (select != SOLVER_SOLVABLE)
4244 s = pool->solvables + what;
4245 POOL_DEBUG(SAT_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solvable2str(pool, s));
4246 addupdaterule(solv, s, 0);
4247 queue_push(&solv->ruletojob, i);
4249 queue_push(&solv->weakruleq, solv->nrules - 1);
4251 case SOLVER_WEAKENDEPS:
4252 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4253 if (select != SOLVER_SOLVABLE)
4255 s = pool->solvables + what;
4256 weaken_solvable_deps(solv, what);
4258 case SOLVER_NOOBSOLETES:
4259 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4262 POOL_DEBUG(SAT_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4263 FOR_JOB_SELECT(p, pp, select, what)
4265 s = pool->solvables + p;
4266 if (installed && s->repo == installed)
4267 addrule(solv, p, 0);
4269 addrule(solv, -p, 0);
4270 queue_push(&solv->ruletojob, i);
4272 queue_push(&solv->weakruleq, solv->nrules - 1);
4276 POOL_DEBUG(SAT_DEBUG_JOB, "job: unknown job\n");
4284 IF_POOLDEBUG (SAT_DEBUG_JOB)
4287 if (solv->nrules == oldnrules)
4288 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created\n");
4289 for (j = oldnrules; j < solv->nrules; j++)
4291 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
4292 solver_printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
4296 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
4297 solv->jobrules_end = solv->nrules;
4299 /* all rules created
4300 * --------------------------------------------------------------
4301 * prepare for solving
4304 /* free unneeded memory */
4305 map_free(&addedmap);
4306 map_free(&installcandidatemap);
4309 /* create weak map */
4310 map_init(&solv->weakrulemap, solv->nrules);
4311 for (i = 0; i < solv->weakruleq.count; i++)
4313 p = solv->weakruleq.elements[i];
4314 MAPSET(&solv->weakrulemap, p);
4317 /* all new rules are learnt after this point */
4318 solv->learntrules = solv->nrules;
4320 /* create assertion index. it is only used to speed up
4321 * makeruledecsions() a bit */
4322 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
4323 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
4324 queue_push(&solv->ruleassertions, i);
4326 /* disable update rules that conflict with our job */
4327 disableupdaterules(solv, job, -1);
4329 /* make decisions based on job/update assertions */
4330 makeruledecisions(solv);
4332 /* create watches chains */
4335 POOL_DEBUG(SAT_DEBUG_STATS, "problems so far: %d\n", solv->problems.count);
4338 * ********************************************
4340 * ********************************************
4343 run_solver(solv, 1, solv->dontinstallrecommended ? 0 : 1);
4347 /* disable all erase jobs (including weak "keep uninstalled" rules) */
4348 for (i = solv->jobrules, r = solv->rules + i; i < solv->learntrules; i++, r++)
4350 if (r->d < 0) /* disabled ? */
4352 if (r->p > 0) /* install job? */
4354 disablerule(solv, r);
4360 enabledisablelearntrules(solv);
4361 removedisabledconflicts(solv, &redoq);
4365 * find recommended packages
4368 /* if redoq.count == 0 we already found all recommended in the
4370 if (redoq.count || solv->dontinstallrecommended || !solv->dontshowinstalledrecommended || solv->ignorealreadyrecommended)
4372 Id rec, *recp, p, *pp;
4374 /* create map of all recommened packages */
4375 solv->recommends_index = -1;
4376 MAPZERO(&solv->recommendsmap);
4377 for (i = 0; i < solv->decisionq.count; i++)
4379 p = solv->decisionq.elements[i];
4382 s = pool->solvables + p;
4385 recp = s->repo->idarraydata + s->recommends;
4386 while ((rec = *recp++) != 0)
4388 FOR_PROVIDES(p, pp, rec)
4389 if (solv->decisionmap[p] > 0)
4393 if (!solv->dontshowinstalledrecommended)
4395 FOR_PROVIDES(p, pp, rec)
4396 if (solv->decisionmap[p] > 0)
4397 MAPSET(&solv->recommendsmap, p);
4399 continue; /* p != 0: already fulfilled */
4401 FOR_PROVIDES(p, pp, rec)
4402 MAPSET(&solv->recommendsmap, p);
4406 for (i = 1; i < pool->nsolvables; i++)
4408 if (solv->decisionmap[i] < 0)
4410 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
4412 s = pool->solvables + i;
4413 if (!MAPTST(&solv->recommendsmap, i))
4415 if (!s->supplements)
4417 if (!pool_installable(pool, s))
4419 if (!solver_is_supplementing(solv, s))
4422 if (solv->dontinstallrecommended)
4423 queue_push(&solv->recommendations, i);
4425 queue_pushunique(&solv->recommendations, i);
4427 /* we use MODE_SUGGEST here so that repo prio is ignored */
4428 policy_filter_unwanted(solv, &solv->recommendations, 0, POLICY_MODE_SUGGEST);
4432 * find suggested packages
4437 Id sug, *sugp, p, *pp;
4439 /* create map of all suggests that are still open */
4440 solv->recommends_index = -1;
4441 MAPZERO(&solv->suggestsmap);
4442 for (i = 0; i < solv->decisionq.count; i++)
4444 p = solv->decisionq.elements[i];
4447 s = pool->solvables + p;
4450 sugp = s->repo->idarraydata + s->suggests;
4451 while ((sug = *sugp++) != 0)
4453 FOR_PROVIDES(p, pp, sug)
4454 if (solv->decisionmap[p] > 0)
4458 if (!solv->dontshowinstalledrecommended)
4460 FOR_PROVIDES(p, pp, sug)
4461 if (solv->decisionmap[p] > 0)
4462 MAPSET(&solv->suggestsmap, p);
4464 continue; /* already fulfilled */
4466 FOR_PROVIDES(p, pp, sug)
4467 MAPSET(&solv->suggestsmap, p);
4471 for (i = 1; i < pool->nsolvables; i++)
4473 if (solv->decisionmap[i] < 0)
4475 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
4477 s = pool->solvables + i;
4478 if (!MAPTST(&solv->suggestsmap, i))
4482 if (!pool_installable(pool, s))
4484 if (!solver_is_enhancing(solv, s))
4487 queue_push(&solv->suggestions, i);
4489 policy_filter_unwanted(solv, &solv->suggestions, 0, POLICY_MODE_SUGGEST);
4494 /* restore decisionmap */
4495 for (i = 0; i < redoq.count; i += 2)
4496 solv->decisionmap[redoq.elements[i]] = redoq.elements[i + 1];
4500 * if unsolvable, prepare solutions
4503 if (solv->problems.count)
4505 int recocount = solv->recommendations.count;
4506 solv->recommendations.count = 0; /* so that revert() doesn't mess with it */
4507 queue_empty(&redoq);
4508 for (i = 0; i < solv->decisionq.count; i++)
4510 Id p = solv->decisionq.elements[i];
4511 queue_push(&redoq, p);
4512 queue_push(&redoq, solv->decisionq_why.elements[i]);
4513 queue_push(&redoq, solv->decisionmap[p > 0 ? p : -p]);
4515 problems_to_solutions(solv, job);
4516 memset(solv->decisionmap, 0, pool->nsolvables * sizeof(Id));
4517 queue_empty(&solv->decisionq);
4518 queue_empty(&solv->decisionq_why);
4519 for (i = 0; i < redoq.count; i += 3)
4521 Id p = redoq.elements[i];
4522 queue_push(&solv->decisionq, p);
4523 queue_push(&solv->decisionq_why, redoq.elements[i + 1]);
4524 solv->decisionmap[p > 0 ? p : -p] = redoq.elements[i + 2];
4526 solv->recommendations.count = recocount;
4529 POOL_DEBUG(SAT_DEBUG_STATS, "final solver statistics: %d learned rules, %d unsolvable\n", solv->stats_learned, solv->stats_unsolvable);
4533 /***********************************************************************/
4534 /* disk usage computations */
4536 /*-------------------------------------------------------------------
4538 * calculate DU changes
4542 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
4546 solver_create_state_maps(solv, &installedmap, 0);
4547 pool_calc_duchanges(solv->pool, solv->installed, &installedmap, mps, nmps);
4548 map_free(&installedmap);
4552 /*-------------------------------------------------------------------
4554 * calculate changes in install size
4558 solver_calc_installsizechange(Solver *solv)
4563 solver_create_state_maps(solv, &installedmap, 0);
4564 change = pool_calc_installsizechange(solv->pool, solv->installed, &installedmap);
4565 map_free(&installedmap);