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 /*-------------------------------------------------------------------
1050 * add (install) rules for solvable
1052 * s: Solvable for which to add rules
1053 * m: m[s] = 1 for solvables which have rules, prevent rule duplication
1055 * Algorithm: 'visit all nodes of a graph'. The graph nodes are
1056 * solvables, the edges their dependencies.
1057 * Starting from an installed solvable, this will create all rules
1058 * representing the graph created by the solvables dependencies.
1060 * for unfulfilled requirements, conflicts, obsoletes,....
1061 * add a negative assertion for solvables that are not installable
1063 * It will also create rules for all solvables referenced by 's'
1064 * i.e. descend to all providers of requirements of 's'
1069 addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
1071 Pool *pool = solv->pool;
1072 Repo *installed = solv->installed;
1074 /* 'work' queue. keeps Ids of solvables we still have to work on.
1075 And buffer for it. */
1080 /* if to add rules for broken deps ('rpm -V' functionality)
1087 /* Id var and pointer for each dependency
1088 * (not used in parallel)
1095 /* var and ptr for loops */
1097 /* ptr to 'whatprovides' */
1099 /* Id for current solvable 's' */
1102 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable -----\n");
1104 queue_init_buffer(&workq, workqbuf, sizeof(workqbuf)/sizeof(*workqbuf));
1105 queue_push(&workq, s - pool->solvables); /* push solvable Id to work queue */
1107 /* loop until there's no more work left */
1112 * s: Pointer to solvable
1115 n = queue_shift(&workq); /* 'pop' next solvable to work on from queue */
1116 if (MAPTST(m, n)) /* continue if already visited */
1119 MAPSET(m, n); /* mark as visited */
1120 s = pool->solvables + n; /* s = Solvable in question */
1123 if (installed /* Installed system available */
1124 && !solv->fixsystem /* NOT repair errors in rpm dependency graph */
1125 && s->repo == installed) /* solvable is installed? */
1127 dontfix = 1; /* dont care about broken rpm deps */
1131 && s->arch != ARCH_SRC
1132 && s->arch != ARCH_NOSRC
1133 && !pool_installable(pool, s))
1135 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", solvable2str(pool, s), (Id)(s - pool->solvables));
1136 addrule(solv, -n, 0); /* uninstallable */
1139 /*-----------------------------------------
1140 * check requires of s
1145 reqp = s->repo->idarraydata + s->requires;
1146 while ((req = *reqp++) != 0) /* go through all requires */
1148 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
1151 /* find list of solvables providing 'req' */
1152 dp = pool_whatprovides(pool, req);
1154 if (*dp == SYSTEMSOLVABLE) /* always installed */
1160 addpatchatomrequires(solv, s, dp, &workq, m);
1166 /* the strategy here is to not insist on dependencies
1167 * that are already broken. so if we find one provider
1168 * that was already installed, we know that the
1169 * dependency was not broken before so we enforce it */
1171 /* check if any of the providers for 'req' is installed */
1172 for (i = 0; (p = dp[i]) != 0; i++)
1174 if (pool->solvables[p].repo == installed)
1175 break; /* provider was installed */
1177 /* didn't find an installed provider: previously broken dependency */
1180 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", dep2str(pool, req), solvable2str(pool, s));
1187 /* nothing provides req! */
1188 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", solvable2str(pool, s), (Id)(s - pool->solvables), dep2str(pool, req));
1189 addrule(solv, -n, 0); /* mark requestor as uninstallable */
1193 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1195 POOL_DEBUG(SAT_DEBUG_RULE_CREATION," %s requires %s\n", solvable2str(pool, s), dep2str(pool, req));
1196 for (i = 0; dp[i]; i++)
1197 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " provided by %s\n", solvable2str(pool, pool->solvables + dp[i]));
1200 /* add 'requires' dependency */
1201 /* rule: (-requestor|provider1|provider2|...|providerN) */
1202 addrule(solv, -n, dp - pool->whatprovidesdata);
1204 /* descend the dependency tree
1205 push all non-visited providers on the work queue */
1208 if (!MAPTST(m, *dp))
1209 queue_push(&workq, *dp);
1212 } /* while, requirements of n */
1214 } /* if, requirements */
1216 /* that's all we check for src packages */
1217 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
1220 /*-----------------------------------------
1221 * check conflicts of s
1226 conp = s->repo->idarraydata + s->conflicts;
1227 /* foreach conflicts of 's' */
1228 while ((con = *conp++) != 0)
1230 /* foreach providers of a conflict of 's' */
1231 FOR_PROVIDES(p, pp, con)
1233 /* dontfix: dont care about conflicts with already installed packs */
1234 if (dontfix && pool->solvables[p].repo == installed)
1236 /* p == n: self conflict */
1237 if (p == n && !solv->allowselfconflicts)
1238 p = 0; /* make it a negative assertion, aka 'uninstallable' */
1239 /* rule: -n|-p: either solvable _or_ provider of conflict */
1240 addrule(solv, -n, -p);
1245 /*-----------------------------------------
1246 * check obsoletes if not installed
1247 * (only installation will trigger the obsoletes in rpm)
1249 if (!installed || pool->solvables[n].repo != installed)
1250 { /* not installed */
1251 int noobs = solv->noobsoletes.size && MAPTST(&solv->noobsoletes, n);
1252 if (s->obsoletes && !noobs)
1254 obsp = s->repo->idarraydata + s->obsoletes;
1255 /* foreach obsoletes */
1256 while ((obs = *obsp++) != 0)
1258 /* foreach provider of an obsoletes of 's' */
1259 FOR_PROVIDES(p, pp, obs)
1261 if (!solv->obsoleteusesprovides /* obsoletes are matched names, not provides */
1262 && !pool_match_nevr(pool, pool->solvables + p, obs))
1264 addrule(solv, -n, -p);
1268 FOR_PROVIDES(p, pp, s->name)
1270 Solvable *ps = pool->solvables + p;
1271 /* we still obsolete packages with same nevra, like rpm does */
1272 /* (actually, rpm mixes those packages. yuck...) */
1273 if (noobs && (s->name != ps->name || s->evr != ps->evr || s->arch != ps->arch))
1275 if (!solv->implicitobsoleteusesprovides && s->name != ps->name)
1277 addrule(solv, -n, -p);
1281 /*-----------------------------------------
1282 * add recommends to the work queue
1286 recp = s->repo->idarraydata + s->recommends;
1287 while ((rec = *recp++) != 0)
1289 FOR_PROVIDES(p, pp, rec)
1291 queue_push(&workq, p);
1296 sugp = s->repo->idarraydata + s->suggests;
1297 while ((sug = *sugp++) != 0)
1299 FOR_PROVIDES(p, pp, sug)
1301 queue_push(&workq, p);
1306 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable end -----\n");
1310 /*-------------------------------------------------------------------
1312 * Add package rules for weak rules
1314 * m: visited solvables
1318 addrpmrulesforweak(Solver *solv, Map *m)
1320 Pool *pool = solv->pool;
1325 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak -----\n");
1326 /* foreach solvable in pool */
1327 for (i = n = 1; n < pool->nsolvables; i++, n++)
1329 if (i == pool->nsolvables) /* wrap i */
1331 if (MAPTST(m, i)) /* been there */
1334 s = pool->solvables + i;
1335 if (!pool_installable(pool, s)) /* only look at installable ones */
1341 /* find possible supplements */
1342 supp = s->repo->idarraydata + s->supplements;
1343 while ((sup = *supp++) != ID_NULL)
1344 if (dep_possible(solv, sup, m))
1348 /* if nothing found, check for enhances */
1349 if (!sup && s->enhances)
1351 supp = s->repo->idarraydata + s->enhances;
1352 while ((sup = *supp++) != ID_NULL)
1353 if (dep_possible(solv, sup, m))
1356 /* if nothing found, goto next solvables */
1359 addrpmrulesforsolvable(solv, s, m);
1362 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak end -----\n");
1366 /*-------------------------------------------------------------------
1368 * add package rules for possible updates
1371 * m: map of already visited solvables
1372 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
1376 addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allow_all)
1378 Pool *pool = solv->pool;
1380 /* queue and buffer for it */
1384 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1386 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1387 /* find update candidates for 's' */
1388 policy_findupdatepackages(solv, s, &qs, allow_all);
1389 /* add rule for 's' if not already done */
1390 if (!MAPTST(m, s - pool->solvables))
1391 addrpmrulesforsolvable(solv, s, m);
1392 /* foreach update candidate, add rule if not already done */
1393 for (i = 0; i < qs.count; i++)
1394 if (!MAPTST(m, qs.elements[i]))
1395 addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
1398 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1402 finddistupgradepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
1404 Pool *pool = solv->pool;
1407 policy_findupdatepackages(solv, s, qs, allow_all);
1412 policy_findupdatepackages(solv, s, qs, 1);
1416 return -SYSTEMSOLVABLE;
1419 return s - pool->solvables;
1420 /* check if it is ok to keep the installed package */
1421 for (i = 0; i < qs->count; i++)
1423 Solvable *ns = pool->solvables + qs->elements[i];
1424 if (s->evr == ns->evr && solvable_identical(pool, s, ns))
1425 return s - pool->solvables;
1427 /* nope, it must be some other package */
1428 return queue_shift(qs);
1431 /*-------------------------------------------------------------------
1433 * add rule for update
1434 * (A|A1|A2|A3...) An = update candidates for A
1436 * s = (installed) solvable
1440 addupdaterule(Solver *solv, Solvable *s, int allow_all)
1442 /* installed packages get a special upgrade allowed rule */
1443 Pool *pool = solv->pool;
1448 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule -----\n");
1449 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1450 p = s - pool->solvables;
1451 /* find update candidates for 's' */
1452 if (solv->distupgrade)
1453 p = finddistupgradepackages(solv, s, &qs, allow_all);
1455 policy_findupdatepackages(solv, s, &qs, allow_all);
1456 d = qs.count ? pool_queuetowhatprovides(pool, &qs) : 0;
1458 addrule(solv, p, d); /* allow update of s */
1459 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule end -----\n");
1463 /********************************************************************/
1467 /*-------------------------------------------------------------------
1470 * initial setup for all watches
1474 makewatches(Solver *solv)
1478 int nsolvables = solv->pool->nsolvables;
1480 sat_free(solv->watches);
1481 /* lower half for removals, upper half for installs */
1482 solv->watches = sat_calloc(2 * nsolvables, sizeof(Id));
1484 /* do it reverse so rpm rules get triggered first (XXX: obsolete?) */
1485 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1487 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1490 if (!r->w2) /* assertions do not need watches */
1493 /* see addwatches_rule(solv, r) */
1494 r->n1 = solv->watches[nsolvables + r->w1];
1495 solv->watches[nsolvables + r->w1] = r - solv->rules;
1497 r->n2 = solv->watches[nsolvables + r->w2];
1498 solv->watches[nsolvables + r->w2] = r - solv->rules;
1503 /*-------------------------------------------------------------------
1505 * add watches (for rule)
1506 * sets up watches for a single rule
1508 * see also makewatches()
1512 addwatches_rule(Solver *solv, Rule *r)
1514 int nsolvables = solv->pool->nsolvables;
1516 r->n1 = solv->watches[nsolvables + r->w1];
1517 solv->watches[nsolvables + r->w1] = r - solv->rules;
1519 r->n2 = solv->watches[nsolvables + r->w2];
1520 solv->watches[nsolvables + r->w2] = r - solv->rules;
1524 /********************************************************************/
1530 /* shortcuts to check if a literal (positive or negative) assignment
1531 * evaluates to 'true' or 'false'
1533 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1534 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
1535 #define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
1537 /*-------------------------------------------------------------------
1541 * make decision and propagate to all rules
1543 * Evaluate each term affected by the decision (linked through watches)
1544 * If we find unit rules we make new decisions based on them
1546 * Everything's fixed there, it's just finding rules that are
1549 * return : 0 = everything is OK
1550 * rule = conflict found in this rule
1554 propagate(Solver *solv, int level)
1556 Pool *pool = solv->pool;
1557 Id *rp, *next_rp; /* rule pointer, next rule pointer in linked list */
1559 Id p, pkg, other_watch;
1561 Id *decisionmap = solv->decisionmap;
1563 Id *watches = solv->watches + pool->nsolvables; /* place ptr in middle */
1565 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
1567 /* foreach non-propagated decision */
1568 while (solv->propagate_index < solv->decisionq.count)
1571 * 'pkg' was just decided
1572 * negate because our watches trigger if literal goes FALSE
1574 pkg = -solv->decisionq.elements[solv->propagate_index++];
1576 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1578 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
1579 solver_printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
1582 /* foreach rule where 'pkg' is now FALSE */
1583 for (rp = watches + pkg; *rp; rp = next_rp)
1585 r = solv->rules + *rp;
1588 /* rule is disabled, goto next */
1596 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1598 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
1599 solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
1602 /* 'pkg' was just decided (was set to FALSE)
1604 * now find other literal watch, check clause
1605 * and advance on linked list
1609 other_watch = r->w2;
1614 other_watch = r->w1;
1619 * This term is already true (through the other literal)
1620 * so we have nothing to do
1622 if (DECISIONMAP_TRUE(other_watch))
1626 * The other literal is FALSE or UNDEF
1632 /* Not a binary clause, try to move our watch.
1634 * Go over all literals and find one that is
1638 * (TRUE is also ok, in that case the rule is fulfilled)
1640 if (r->p /* we have a 'p' */
1641 && r->p != other_watch /* which is not watched */
1642 && !DECISIONMAP_FALSE(r->p)) /* and not FALSE */
1646 else /* go find a 'd' to make 'true' */
1649 we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
1651 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1653 if (p != other_watch /* which is not watched */
1654 && !DECISIONMAP_FALSE(p)) /* and not FALSE */
1662 * if we found some p that is UNDEF or TRUE, move
1665 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1668 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables + p));
1670 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables - p));
1686 watches[p] = r - solv->rules;
1689 /* search failed, thus all unwatched literals are FALSE */
1694 * unit clause found, set literal other_watch to TRUE
1697 if (DECISIONMAP_FALSE(other_watch)) /* check if literal is FALSE */
1698 return r; /* eek, a conflict! */
1700 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1702 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " unit ");
1703 solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
1706 if (other_watch > 0)
1707 decisionmap[other_watch] = level; /* install! */
1709 decisionmap[-other_watch] = -level; /* remove! */
1711 queue_push(&solv->decisionq, other_watch);
1712 queue_push(&solv->decisionq_why, r - solv->rules);
1714 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1716 Solvable *s = pool->solvables + (other_watch > 0 ? other_watch : -other_watch);
1717 if (other_watch > 0)
1718 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", solvable2str(pool, s));
1720 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", solvable2str(pool, s));
1723 } /* foreach rule involving 'pkg' */
1725 } /* while we have non-decided decisions */
1727 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
1729 return 0; /* all is well */
1733 /********************************************************************/
1736 /*-------------------------------------------------------------------
1743 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
1745 Pool *pool = solv->pool;
1748 Map seen; /* global? */
1749 Id d, v, vv, *dp, why;
1751 int num = 0, l1num = 0;
1752 int learnt_why = solv->learnt_pool.count;
1753 Id *decisionmap = solv->decisionmap;
1757 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
1758 map_init(&seen, pool->nsolvables);
1759 idx = solv->decisionq.count;
1762 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1763 solver_printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1764 queue_push(&solv->learnt_pool, c - solv->rules);
1765 d = c->d < 0 ? -c->d - 1 : c->d;
1766 dp = d ? pool->whatprovidesdata + d : 0;
1767 /* go through all literals of the rule */
1779 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1781 vv = v > 0 ? v : -v;
1782 if (MAPTST(&seen, vv))
1784 l = solv->decisionmap[vv];
1789 l1num++; /* need to do this one in level1 pass */
1790 else if (l == level)
1791 num++; /* need to do this one as well */
1794 queue_push(&r, v); /* not level1 or conflict level, add to new rule */
1800 if (!num && !--l1num)
1801 break; /* all level 1 literals done */
1805 v = solv->decisionq.elements[--idx];
1806 vv = v > 0 ? v : -v;
1807 if (MAPTST(&seen, vv))
1811 if (num && --num == 0)
1813 *pr = -v; /* so that v doesn't get lost */
1816 POOL_DEBUG(SAT_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
1817 for (i = 0; i < r.count; i++)
1820 MAPCLR(&seen, v > 0 ? v : -v);
1822 /* only level 1 marks left */
1826 why = solv->decisionq_why.elements[idx];
1827 if (!why) /* just in case, maybe for SYSTEMSOLVABLE */
1829 c = solv->rules + why;
1835 else if (r.count == 1 && r.elements[0] < 0)
1836 *dr = r.elements[0];
1838 *dr = pool_queuetowhatprovides(pool, &r);
1839 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1841 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
1842 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, *pr);
1843 for (i = 0; i < r.count; i++)
1844 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
1846 /* push end marker on learnt reasons stack */
1847 queue_push(&solv->learnt_pool, 0);
1850 solv->stats_learned++;
1855 /*-------------------------------------------------------------------
1859 * reset the solver decisions to right after the rpm rules.
1860 * called after rules have been enabled/disabled
1864 reset_solver(Solver *solv)
1866 Pool *pool = solv->pool;
1870 /* rewind decisions to direct rpm rule assertions */
1871 for (i = solv->decisionq.count - 1; i >= solv->directdecisions; i--)
1873 v = solv->decisionq.elements[i];
1874 solv->decisionmap[v > 0 ? v : -v] = 0;
1877 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions done reduced from %d to %d\n", solv->decisionq.count, solv->directdecisions);
1879 solv->decisionq_why.count = solv->directdecisions;
1880 solv->decisionq.count = solv->directdecisions;
1881 solv->recommends_index = -1;
1882 solv->propagate_index = 0;
1884 /* adapt learnt rule status to new set of enabled/disabled rules */
1885 enabledisablelearntrules(solv);
1887 /* redo all job/update decisions */
1888 makeruledecisions(solv);
1889 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
1893 /*-------------------------------------------------------------------
1895 * analyze_unsolvable_rule
1899 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp)
1901 Pool *pool = solv->pool;
1903 Id why = r - solv->rules;
1905 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1906 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1907 if (solv->learntrules && why >= solv->learntrules)
1909 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1910 if (solv->learnt_pool.elements[i] > 0)
1911 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp);
1914 if (MAPTST(&solv->weakrulemap, why))
1915 if (!*lastweakp || why > *lastweakp)
1917 /* do not add rpm rules to problem */
1918 if (why < solv->rpmrules_end)
1920 /* turn rule into problem */
1921 if (why >= solv->jobrules && why < solv->jobrules_end)
1922 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1923 /* return if problem already countains our rule */
1924 if (solv->problems.count)
1926 for (i = solv->problems.count - 1; i >= 0; i--)
1927 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
1929 else if (solv->problems.elements[i] == why)
1932 queue_push(&solv->problems, why);
1936 /*-------------------------------------------------------------------
1938 * analyze_unsolvable
1940 * return: 1 - disabled some rules, try again
1945 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1947 Pool *pool = solv->pool;
1949 Map seen; /* global to speed things up? */
1950 Id d, v, vv, *dp, why;
1952 Id *decisionmap = solv->decisionmap;
1953 int oldproblemcount;
1954 int oldlearntpoolcount;
1957 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
1958 solv->stats_unsolvable++;
1959 oldproblemcount = solv->problems.count;
1960 oldlearntpoolcount = solv->learnt_pool.count;
1962 /* make room for proof index */
1963 /* must update it later, as analyze_unsolvable_rule would confuse
1964 * it with a rule index if we put the real value in already */
1965 queue_push(&solv->problems, 0);
1968 map_init(&seen, pool->nsolvables);
1969 queue_push(&solv->learnt_pool, r - solv->rules);
1971 analyze_unsolvable_rule(solv, r, &lastweak);
1972 d = r->d < 0 ? -r->d - 1 : r->d;
1973 dp = d ? pool->whatprovidesdata + d : 0;
1984 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1986 vv = v > 0 ? v : -v;
1987 l = solv->decisionmap[vv];
1992 idx = solv->decisionq.count;
1995 v = solv->decisionq.elements[--idx];
1996 vv = v > 0 ? v : -v;
1997 if (!MAPTST(&seen, vv))
1999 why = solv->decisionq_why.elements[idx];
2000 queue_push(&solv->learnt_pool, why);
2001 r = solv->rules + why;
2002 analyze_unsolvable_rule(solv, r, &lastweak);
2003 d = r->d < 0 ? -r->d - 1 : r->d;
2004 dp = d ? pool->whatprovidesdata + d : 0;
2015 if (DECISIONMAP_TRUE(v)) /* the one true literal */
2017 vv = v > 0 ? v : -v;
2018 l = solv->decisionmap[vv];
2025 queue_push(&solv->problems, 0); /* mark end of this problem */
2029 /* disable last weak rule */
2030 solv->problems.count = oldproblemcount;
2031 solv->learnt_pool.count = oldlearntpoolcount;
2032 r = solv->rules + lastweak;
2033 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling ");
2034 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
2035 disablerule(solv, r);
2041 queue_push(&solv->learnt_pool, 0);
2042 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
2046 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
2047 disableproblem(solv, solv->problems.elements[i]);
2048 /* XXX: might want to enable all weak rules again */
2052 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
2057 /********************************************************************/
2058 /* Decision revert */
2060 /*-------------------------------------------------------------------
2063 * revert decision at level
2067 revert(Solver *solv, int level)
2069 Pool *pool = solv->pool;
2071 while (solv->decisionq.count)
2073 v = solv->decisionq.elements[solv->decisionq.count - 1];
2074 vv = v > 0 ? v : -v;
2075 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
2077 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
2078 if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
2079 solv->recommendations.count--;
2080 solv->decisionmap[vv] = 0;
2081 solv->decisionq.count--;
2082 solv->decisionq_why.count--;
2083 solv->propagate_index = solv->decisionq.count;
2085 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
2087 solv->branches.count--;
2088 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
2089 solv->branches.count--;
2091 solv->recommends_index = -1;
2095 /*-------------------------------------------------------------------
2097 * watch2onhighest - put watch2 on literal with highest level
2101 watch2onhighest(Solver *solv, Rule *r)
2106 d = r->d < 0 ? -r->d - 1 : r->d;
2108 return; /* binary rule, both watches are set */
2109 dp = solv->pool->whatprovidesdata + d;
2110 while ((v = *dp++) != 0)
2112 l = solv->decisionmap[v < 0 ? -v : v];
2124 /*-------------------------------------------------------------------
2128 * add free decision (solvable to install) to decisionq
2129 * increase level and propagate decision
2130 * return if no conflict.
2132 * in conflict case, analyze conflict rule, add resulting
2133 * rule to learnt rule set, make decision from learnt
2134 * rule (always unit) and re-propagate.
2136 * returns the new solver level or 0 if unsolvable
2141 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
2143 Pool *pool = solv->pool;
2152 solv->decisionmap[decision] = level;
2154 solv->decisionmap[-decision] = -level;
2155 queue_push(&solv->decisionq, decision);
2156 queue_push(&solv->decisionq_why, 0);
2160 r = propagate(solv, level);
2164 return analyze_unsolvable(solv, r, disablerules);
2165 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
2166 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
2167 assert(l > 0 && l < level);
2168 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
2170 revert(solv, level);
2171 r = addrule(solv, p, d); /* p requires d */
2173 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
2174 queue_push(&solv->learnt_why, why);
2177 /* at least 2 literals, needs watches */
2178 watch2onhighest(solv, r);
2179 addwatches_rule(solv, r);
2183 /* learnt rule is an assertion */
2184 queue_push(&solv->ruleassertions, r - solv->rules);
2186 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
2187 queue_push(&solv->decisionq, p);
2188 queue_push(&solv->decisionq_why, r - solv->rules);
2189 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
2191 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
2192 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
2193 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
2194 solver_printrule(solv, SAT_DEBUG_ANALYZE, r);
2201 /*-------------------------------------------------------------------
2203 * select and install
2205 * install best package from the queue. We add an extra package, inst, if
2206 * provided. See comment in weak install section.
2208 * returns the new solver level or 0 if unsolvable
2213 selectandinstall(Solver *solv, int level, Queue *dq, Id inst, int disablerules)
2215 Pool *pool = solv->pool;
2219 if (dq->count > 1 || inst)
2220 policy_filter_unwanted(solv, dq, inst, POLICY_MODE_CHOOSE);
2225 /* choose the supplemented one */
2226 for (i = 0; i < dq->count; i++)
2227 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
2231 for (i = 1; i < dq->count; i++)
2232 queue_push(&solv->branches, dq->elements[i]);
2233 queue_push(&solv->branches, -level);
2237 p = dq->elements[i];
2239 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvable2str(pool, pool->solvables + p));
2241 return setpropagatelearn(solv, level, p, disablerules);
2245 /********************************************************************/
2246 /* Main solver interface */
2249 /*-------------------------------------------------------------------
2252 * create solver structure
2254 * pool: all available solvables
2255 * installed: installed Solvables
2258 * Upon solving, rules are created to flag the Solvables
2259 * of the 'installed' Repo as installed.
2263 solver_create(Pool *pool, Repo *installed)
2266 solv = (Solver *)sat_calloc(1, sizeof(Solver));
2268 solv->installed = installed;
2270 queue_init(&solv->ruletojob);
2271 queue_init(&solv->decisionq);
2272 queue_init(&solv->decisionq_why);
2273 queue_init(&solv->problems);
2274 queue_init(&solv->suggestions);
2275 queue_init(&solv->recommendations);
2276 queue_init(&solv->orphaned);
2277 queue_init(&solv->learnt_why);
2278 queue_init(&solv->learnt_pool);
2279 queue_init(&solv->branches);
2280 queue_init(&solv->covenantq);
2281 queue_init(&solv->weakruleq);
2282 queue_init(&solv->ruleassertions);
2284 map_init(&solv->recommendsmap, pool->nsolvables);
2285 map_init(&solv->suggestsmap, pool->nsolvables);
2286 map_init(&solv->noupdate, installed ? installed->end - installed->start : 0);
2287 solv->recommends_index = 0;
2289 solv->decisionmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
2291 solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
2292 memset(solv->rules, 0, sizeof(Rule));
2298 /*-------------------------------------------------------------------
2304 solver_free(Solver *solv)
2306 queue_free(&solv->ruletojob);
2307 queue_free(&solv->decisionq);
2308 queue_free(&solv->decisionq_why);
2309 queue_free(&solv->learnt_why);
2310 queue_free(&solv->learnt_pool);
2311 queue_free(&solv->problems);
2312 queue_free(&solv->suggestions);
2313 queue_free(&solv->recommendations);
2314 queue_free(&solv->orphaned);
2315 queue_free(&solv->branches);
2316 queue_free(&solv->covenantq);
2317 queue_free(&solv->weakruleq);
2318 queue_free(&solv->ruleassertions);
2320 map_free(&solv->recommendsmap);
2321 map_free(&solv->suggestsmap);
2322 map_free(&solv->noupdate);
2323 map_free(&solv->weakrulemap);
2324 map_free(&solv->noobsoletes);
2326 sat_free(solv->decisionmap);
2327 sat_free(solv->rules);
2328 sat_free(solv->watches);
2329 sat_free(solv->obsoletes);
2330 sat_free(solv->obsoletes_data);
2335 /*-------------------------------------------------------------------
2339 * all rules have been set up, now actually run the solver
2344 run_solver(Solver *solv, int disablerules, int doweak)
2346 Queue dq; /* local decisionqueue */
2347 Queue dqs; /* local decisionqueue for supplements */
2353 Pool *pool = solv->pool;
2356 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
2358 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
2359 for (i = 1; i < solv->nrules; i++)
2360 solver_printruleclass(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
2363 POOL_DEBUG(SAT_DEBUG_STATS, "initial decisions: %d\n", solv->decisionq.count);
2365 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2366 solver_printdecisions(solv);
2368 /* start SAT algorithm */
2370 systemlevel = level + 1;
2371 POOL_DEBUG(SAT_DEBUG_STATS, "solving...\n");
2377 * here's the main loop:
2378 * 1) propagate new decisions (only needed for level 1)
2379 * 2) try to keep installed packages
2380 * 3) fulfill all unresolved rules
2381 * 4) install recommended packages
2382 * 5) minimalize solution if we had choices
2383 * if we encounter a problem, we rewind to a safe level and restart
2395 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
2396 if ((r = propagate(solv, level)) != 0)
2398 if (analyze_unsolvable(solv, r, disablerules))
2405 if (level < systemlevel)
2407 POOL_DEBUG(SAT_DEBUG_STATS, "resolving job rules\n");
2408 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2411 if (r->d < 0) /* ignore disabled rules */
2414 FOR_RULELITERALS(l, dp, r)
2418 if (solv->decisionmap[-l] <= 0)
2423 if (solv->decisionmap[l] > 0)
2425 if (solv->decisionmap[l] == 0)
2431 /* prune to installed if not updating */
2432 if (!solv->updatesystem && solv->installed && dq.count > 1)
2435 for (j = k = 0; j < dq.count; j++)
2437 Solvable *s = pool->solvables + dq.elements[j];
2438 if (s->repo == solv->installed)
2439 dq.elements[k++] = dq.elements[j];
2445 level = selectandinstall(solv, level, &dq, 0, disablerules);
2451 if (level <= olevel)
2454 systemlevel = level + 1;
2455 if (i < solv->jobrules_end)
2461 * installed packages
2464 if (level < systemlevel && solv->installed && solv->installed->nsolvables)
2466 if (!solv->updatesystem)
2469 * Normal run (non-updating)
2470 * Keep as many packages installed as possible
2472 POOL_DEBUG(SAT_DEBUG_STATS, "installing old packages\n");
2474 for (i = solv->installed->start; i < solv->installed->end; i++)
2476 s = pool->solvables + i;
2478 /* skip if not installed */
2479 if (s->repo != solv->installed)
2482 /* skip if already decided */
2483 if (solv->decisionmap[i] != 0)
2486 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "keeping %s\n", solvable2str(pool, s));
2489 level = setpropagatelearn(solv, level, i, disablerules);
2491 if (level == 0) /* unsolvable */
2496 if (level <= olevel)
2499 if (i < solv->installed->end)
2503 POOL_DEBUG(SAT_DEBUG_STATS, "resolving update/feature rules\n");
2505 for (i = solv->installed->start, r = solv->rules + solv->updaterules; i < solv->installed->end; i++, r++)
2509 s = pool->solvables + i;
2511 /* skip if not installed (can't update) */
2512 if (s->repo != solv->installed)
2514 /* skip if already decided */
2515 if (solv->decisionmap[i] > 0)
2518 /* noupdate is set if a job is erasing the installed solvable or installing a specific version */
2519 if (MAPTST(&solv->noupdate, i - solv->installed->start))
2525 if (rr->d < 0) /* disabled -> look at feature rule ? */
2526 rr -= solv->installed->end - solv->installed->start;
2527 if (!rr->p) /* identical to update rule? */
2532 FOR_RULELITERALS(p, dp, rr)
2534 if (solv->decisionmap[p] > 0)
2536 if (solv->decisionmap[p] == 0)
2539 if (p || !dq.count) /* already fulfilled or empty */
2541 if (dq.elements[0] == i)
2542 inst = queue_shift(&dq);
2546 /* FIXME: it is handled a bit different because we do not want
2547 * to have it pruned just because it is not recommened.
2548 * we should not prune installed packages instead.
2550 level = selectandinstall(solv, level, &dq, inst, disablerules);
2556 if (level <= olevel)
2559 systemlevel = level + 1;
2560 if (i < solv->installed->end)
2564 if (level < systemlevel)
2565 systemlevel = level;
2571 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unresolved rules\n");
2572 for (i = 1, n = 1; ; i++, n++)
2574 if (n == solv->nrules)
2576 if (i == solv->nrules)
2578 r = solv->rules + i;
2579 if (r->d < 0) /* ignore disabled rules */
2584 /* binary or unary rule */
2585 /* need two positive undecided literals */
2586 if (r->p < 0 || r->w2 <= 0)
2588 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2590 queue_push(&dq, r->p);
2591 queue_push(&dq, r->w2);
2596 * all negative literals are installed
2597 * no positive literal is installed
2598 * i.e. the rule is not fulfilled and we
2599 * just need to decide on the positive literals
2603 if (solv->decisionmap[-r->p] <= 0)
2608 if (solv->decisionmap[r->p] > 0)
2610 if (solv->decisionmap[r->p] == 0)
2611 queue_push(&dq, r->p);
2613 dp = pool->whatprovidesdata + r->d;
2614 while ((p = *dp++) != 0)
2618 if (solv->decisionmap[-p] <= 0)
2623 if (solv->decisionmap[p] > 0)
2625 if (solv->decisionmap[p] == 0)
2632 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
2634 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
2635 solver_printruleclass(solv, SAT_DEBUG_PROPAGATE, r);
2637 /* dq.count < 2 cannot happen as this means that
2638 * the rule is unit */
2639 assert(dq.count > 1);
2642 level = selectandinstall(solv, level, &dq, 0, disablerules);
2648 if (level < systemlevel)
2651 } /* for(), decide */
2653 if (n != solv->nrules) /* continue if level < systemlevel */
2660 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended packages\n");
2663 for (i = 1; i < pool->nsolvables; i++)
2665 if (solv->decisionmap[i] < 0)
2667 if (solv->decisionmap[i] > 0)
2669 /* installed, check for recommends */
2670 Id *recp, rec, *pp, p;
2671 s = pool->solvables + i;
2672 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
2674 /* XXX need to special case AND ? */
2677 recp = s->repo->idarraydata + s->recommends;
2678 while ((rec = *recp++) != 0)
2681 FOR_PROVIDES(p, pp, rec)
2683 if (solv->decisionmap[p] > 0)
2688 else if (solv->decisionmap[p] == 0)
2690 queue_pushunique(&dq, p);
2698 s = pool->solvables + i;
2699 if (!s->supplements)
2701 if (!pool_installable(pool, s))
2703 if (!solver_is_supplementing(solv, s))
2705 if (solv->ignorealreadyrecommended && solv->installed)
2706 queue_pushunique(&dqs, i); /* needs filter */
2708 queue_pushunique(&dq, i);
2711 if (solv->ignorealreadyrecommended && dqs.count)
2713 /* turn off all new packages */
2714 for (i = 0; i < solv->decisionq.count; i++)
2716 p = solv->decisionq.elements[i];
2719 s = pool->solvables + p;
2720 if (s->repo && s->repo != solv->installed)
2721 solv->decisionmap[p] = -solv->decisionmap[p];
2723 /* filter out old supplements */
2724 for (i = 0; i < dqs.count; i++)
2726 p = dqs.elements[i];
2727 s = pool->solvables + p;
2728 if (!s->supplements)
2730 if (!solver_is_supplementing(solv, s))
2731 queue_pushunique(&dq, p);
2733 /* undo turning off */
2734 for (i = 0; i < solv->decisionq.count; i++)
2736 p = solv->decisionq.elements[i];
2739 s = pool->solvables + p;
2740 if (s->repo && s->repo != solv->installed)
2741 solv->decisionmap[p] = -solv->decisionmap[p];
2747 policy_filter_unwanted(solv, &dq, 0, POLICY_MODE_RECOMMEND);
2749 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended %s\n", solvable2str(pool, pool->solvables + p));
2750 queue_push(&solv->recommendations, p);
2751 level = setpropagatelearn(solv, level, p, 0);
2756 if (solv->distupgrade && solv->installed)
2758 /* let's see if we can install some unsupported package */
2760 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unsupported packages\n");
2761 for (i = solv->installed->start, ri = 0; i < solv->installed->end; i++, ri++)
2763 s = pool->solvables + i;
2764 if (s->repo != solv->installed)
2766 if (solv->decisionmap[i])
2768 if (!solv->rules[solv->updaterules + ri].p && !solv->rules[solv->featurerules + ri].p)
2771 if (i < solv->installed->end)
2773 if (solv->distupgrade_removeunsupported)
2775 POOL_DEBUG(SAT_DEBUG_STATS, "removing unsupported %s\n", solvable2str(pool, pool->solvables + i));
2776 level = setpropagatelearn(solv, level, -i, 0);
2780 POOL_DEBUG(SAT_DEBUG_STATS, "keeping unsupported %s\n", solvable2str(pool, pool->solvables + i));
2781 level = setpropagatelearn(solv, level, i, 0);
2787 if (solv->solution_callback)
2789 solv->solution_callback(solv, solv->solution_callback_data);
2790 if (solv->branches.count)
2792 int i = solv->branches.count - 1;
2793 int l = -solv->branches.elements[i];
2795 if (solv->branches.elements[i - 1] < 0)
2797 p = solv->branches.elements[i];
2798 POOL_DEBUG(SAT_DEBUG_STATS, "branching with %s\n", solvable2str(pool, pool->solvables + p));
2800 for (j = i + 1; j < solv->branches.count; j++)
2801 queue_push(&dq, solv->branches.elements[j]);
2802 solv->branches.count = i;
2804 revert(solv, level);
2806 for (j = 0; j < dq.count; j++)
2807 queue_push(&solv->branches, dq.elements[j]);
2809 level = setpropagatelearn(solv, level, p, disablerules);
2817 /* all branches done, we're finally finished */
2821 /* minimization step */
2822 if (solv->branches.count)
2824 int l = 0, lasti = -1, lastl = -1;
2826 for (i = solv->branches.count - 1; i >= 0; i--)
2828 p = solv->branches.elements[i];
2831 else if (p > 0 && solv->decisionmap[p] > l + 1)
2839 /* kill old solvable so that we do not loop */
2840 p = solv->branches.elements[lasti];
2841 solv->branches.elements[lasti] = 0;
2842 POOL_DEBUG(SAT_DEBUG_STATS, "minimizing %d -> %d with %s\n", solv->decisionmap[p], l, solvable2str(pool, pool->solvables + p));
2845 revert(solv, level);
2847 level = setpropagatelearn(solv, level, p, disablerules);
2858 POOL_DEBUG(SAT_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable\n", solv->stats_learned, solv->stats_unsolvable);
2860 POOL_DEBUG(SAT_DEBUG_STATS, "done solving.\n\n");
2866 /*-------------------------------------------------------------------
2870 * at this point, all rules that led to conflicts are disabled.
2871 * we re-enable all rules of a problem set but rule "sug", then
2872 * continue to disable more rules until there as again a solution.
2875 /* FIXME: think about conflicting assertions */
2878 refine_suggestion(Solver *solv, Queue *job, Id *problem, Id sug, Queue *refined)
2880 Pool *pool = solv->pool;
2886 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2888 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion start\n");
2889 for (i = 0; problem[i]; i++)
2891 if (problem[i] == sug)
2892 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "=> ");
2893 solver_printproblem(solv, problem[i]);
2896 queue_init(&disabled);
2897 queue_empty(refined);
2898 queue_push(refined, sug);
2900 /* re-enable all problem rules with the exception of "sug"(gestion) */
2904 for (i = 0; problem[i]; i++)
2905 if (problem[i] != sug)
2906 enableproblem(solv, problem[i]);
2909 disableupdaterules(solv, job, -(sug + 1));
2910 else if (sug >= solv->updaterules && sug < solv->updaterules_end)
2912 /* enable feature rule */
2913 Rule *r = solv->rules + solv->featurerules + (sug - solv->updaterules);
2915 enablerule(solv, r);
2918 enableweakrules(solv);
2922 int njob, nfeature, nupdate;
2923 queue_empty(&solv->problems);
2924 revert(solv, 1); /* XXX no longer needed? */
2927 if (!solv->problems.count)
2928 run_solver(solv, 0, 0);
2930 if (!solv->problems.count)
2932 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no more problems!\n");
2933 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2934 solver_printdecisions(solv);
2935 break; /* great, no more problems */
2937 disabledcnt = disabled.count;
2938 /* start with 1 to skip over proof index */
2939 njob = nfeature = nupdate = 0;
2940 for (i = 1; i < solv->problems.count - 1; i++)
2942 /* ignore solutions in refined */
2943 v = solv->problems.elements[i];
2945 break; /* end of problem reached */
2946 for (j = 0; problem[j]; j++)
2947 if (problem[j] != sug && problem[j] == v)
2951 if (v >= solv->featurerules && v < solv->featurerules_end)
2957 queue_push(&disabled, v);
2959 if (disabled.count == disabledcnt)
2961 /* no solution found, this was an invalid suggestion! */
2962 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no solution found!\n");
2966 if (!njob && nupdate && nfeature)
2968 /* got only update rules, filter out feature rules */
2969 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "throwing away feature rules\n");
2970 for (i = j = disabledcnt; i < disabled.count; i++)
2972 v = disabled.elements[i];
2973 if (v < solv->featurerules || v >= solv->featurerules_end)
2974 disabled.elements[j++] = v;
2979 if (disabled.count == disabledcnt + 1)
2981 /* just one suggestion, add it to refined list */
2982 v = disabled.elements[disabledcnt];
2984 queue_push(refined, v); /* do not record feature rules */
2985 disableproblem(solv, v);
2986 if (v >= solv->updaterules && v < solv->updaterules_end)
2988 Rule *r = solv->rules + (v - solv->updaterules + solv->featurerules);
2990 enablerule(solv, r); /* enable corresponding feature rule */
2993 disableupdaterules(solv, job, -(v + 1));
2997 /* more than one solution, disable all */
2998 /* do not push anything on refine list, as we do not know which solution to choose */
2999 /* thus, the user will get another problem if he selects this solution, where he
3000 * can choose the right one */
3001 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
3003 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "more than one solution found:\n");
3004 for (i = disabledcnt; i < disabled.count; i++)
3005 solver_printproblem(solv, disabled.elements[i]);
3007 for (i = disabledcnt; i < disabled.count; i++)
3009 v = disabled.elements[i];
3010 disableproblem(solv, v);
3011 if (v >= solv->updaterules && v < solv->updaterules_end)
3013 Rule *r = solv->rules + (v - solv->updaterules + solv->featurerules);
3015 enablerule(solv, r);
3020 /* all done, get us back into the same state as before */
3021 /* enable refined rules again */
3022 for (i = 0; i < disabled.count; i++)
3023 enableproblem(solv, disabled.elements[i]);
3024 /* disable problem rules again */
3027 for (i = 0; problem[i]; i++)
3028 enableproblem(solv, problem[i]);
3029 disableupdaterules(solv, job, -1);
3031 /* disable problem rules again */
3032 for (i = 0; problem[i]; i++)
3033 disableproblem(solv, problem[i]);
3034 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion end\n");
3038 /*-------------------------------------------------------------------
3039 * sorting helper for problems
3043 problems_sortcmp(const void *ap, const void *bp)
3045 Id a = *(Id *)ap, b = *(Id *)bp;
3054 /*-------------------------------------------------------------------
3059 problems_sort(Solver *solv)
3062 if (!solv->problems.count)
3064 for (i = j = 1; i < solv->problems.count; i++)
3066 if (!solv->problems.elements[i])
3069 qsort(solv->problems.elements + j, i - j, sizeof(Id), problems_sortcmp);
3070 if (++i == solv->problems.count)
3078 /*-------------------------------------------------------------------
3079 * convert problems to solutions
3083 problems_to_solutions(Solver *solv, Queue *job)
3085 Pool *pool = solv->pool;
3093 if (!solv->problems.count)
3095 problems_sort(solv);
3096 queue_clone(&problems, &solv->problems);
3097 queue_init(&solution);
3098 queue_init(&solutions);
3099 /* copy over proof index */
3100 queue_push(&solutions, problems.elements[0]);
3101 problem = problems.elements + 1;
3102 for (i = 1; i < problems.count; i++)
3104 Id v = problems.elements[i];
3107 /* mark end of this problem */
3108 queue_push(&solutions, 0);
3109 queue_push(&solutions, 0);
3110 if (i + 1 == problems.count)
3112 /* copy over proof of next problem */
3113 queue_push(&solutions, problems.elements[i + 1]);
3115 problem = problems.elements + i + 1;
3118 refine_suggestion(solv, job, problem, v, &solution);
3119 if (!solution.count)
3120 continue; /* this solution didn't work out */
3123 for (j = 0; j < solution.count; j++)
3125 why = solution.elements[j];
3126 /* must be either job descriptor or update rule */
3127 assert(why < 0 || (why >= solv->updaterules && why < solv->updaterules_end));
3129 solver_printproblem(solv, why);
3133 /* job descriptor */
3134 queue_push(&solutions, 0);
3135 queue_push(&solutions, -why);
3139 /* update rule, find replacement package */
3140 Id p, d, *dp, rp = 0;
3142 p = solv->installed->start + (why - solv->updaterules);
3143 rr = solv->rules + solv->featurerules + (why - solv->updaterules);
3145 rr = solv->rules + why;
3146 if (solv->distupgrade && solv->rules[why].p != p && solv->decisionmap[p] > 0)
3148 /* distupgrade case, allow to keep old package */
3149 queue_push(&solutions, p);
3150 queue_push(&solutions, p);
3154 if (solv->decisionmap[p] > 0)
3155 continue; /* false alarm, turned out we can keep the package */
3158 d = rr->d < 0 ? -rr->d - 1 : rr->d;
3161 if (solv->decisionmap[rr->w2] > 0 && pool->solvables[rr->w2].repo != solv->installed)
3166 for (dp = pool->whatprovidesdata + d; *dp; dp++)
3168 if (solv->decisionmap[*dp] > 0 && pool->solvables[*dp].repo != solv->installed)
3176 queue_push(&solutions, p);
3177 queue_push(&solutions, rp);
3181 /* mark end of this solution */
3184 queue_push(&solutions, 0);
3185 queue_push(&solutions, 0);
3189 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "Oops, everything was fine?\n");
3192 queue_free(&solution);
3193 queue_free(&problems);
3194 /* copy queue over to solutions */
3195 queue_free(&solv->problems);
3196 queue_clone(&solv->problems, &solutions);
3198 /* bring solver back into problem state */
3199 revert(solv, 1); /* XXX move to reset_solver? */
3202 assert(solv->problems.count == solutions.count);
3203 queue_free(&solutions);
3207 /*-------------------------------------------------------------------
3211 * advance to next problem
3215 solver_next_problem(Solver *solv, Id problem)
3219 return solv->problems.count ? 1 : 0;
3220 pp = solv->problems.elements + problem;
3221 while (pp[0] || pp[1])
3225 while (pp[0] || pp[1])
3230 problem = pp - solv->problems.elements;
3231 if (problem >= solv->problems.count)
3237 /*-------------------------------------------------------------------
3243 solver_next_solution(Solver *solv, Id problem, Id solution)
3249 pp = solv->problems.elements + solution;
3250 return pp[0] || pp[1] ? solution : 0;
3252 pp = solv->problems.elements + solution;
3253 while (pp[0] || pp[1])
3256 solution = pp - solv->problems.elements;
3257 return pp[0] || pp[1] ? solution : 0;
3261 /*-------------------------------------------------------------------
3263 * solution element iterator
3267 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
3270 element = element ? element + 2 : solution;
3271 pp = solv->problems.elements + element;
3272 if (!(pp[0] || pp[1]))
3280 /*-------------------------------------------------------------------
3282 * Retrieve information about a problematic rule
3284 * this is basically the reverse of addrpmrulesforsolvable
3288 solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp)
3290 Pool *pool = solv->pool;
3291 Repo *installed = solv->installed;
3295 Id p, d, *pp, req, *reqp, con, *conp, obs, *obsp, *dp;
3298 if (rid >= solv->jobrules && rid < solv->jobrules_end)
3301 r = solv->rules + rid;
3302 p = solv->ruletojob.elements[rid - solv->jobrules];
3303 *depp = job->elements[p + 1];
3305 *targetp = job->elements[p];
3306 d = r->d < 0 ? -r->d - 1 : r->d;
3307 if (d == 0 && r->w2 == 0 && r->p == -SYSTEMSOLVABLE && (job->elements[p] & SOLVER_SELECTMASK) != SOLVER_SOLVABLE_ONE_OF)
3308 return SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP;
3309 return SOLVER_PROBLEM_JOB_RULE;
3311 if (rid >= solv->updaterules && rid < solv->updaterules_end)
3314 *sourcep = solv->installed->start + (rid - solv->updaterules);
3316 return SOLVER_PROBLEM_UPDATE_RULE;
3318 assert(rid < solv->rpmrules_end);
3319 r = solv->rules + rid;
3321 d = r->d < 0 ? -r->d - 1 : r->d;
3322 if (d == 0 && r->w2 == 0)
3324 /* a rpm rule assertion */
3325 s = pool->solvables - r->p;
3326 if (installed && !solv->fixsystem && s->repo == installed)
3328 assert(!dontfix); /* dontfix packages never have a neg assertion */
3331 /* see why the package is not installable */
3332 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
3335 return SOLVER_PROBLEM_NOT_INSTALLABLE;
3337 /* check requires */
3340 reqp = s->repo->idarraydata + s->requires;
3341 while ((req = *reqp++) != 0)
3343 if (req == SOLVABLE_PREREQMARKER)
3345 dp = pool_whatprovides(pool, req);
3352 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
3355 if (!solv->allowselfconflicts && s->conflicts)
3357 conp = s->repo->idarraydata + s->conflicts;
3358 while ((con = *conp++) != 0)
3359 FOR_PROVIDES(p, pp, con)
3363 return SOLVER_PROBLEM_SELF_CONFLICT;
3366 /* should never happen */
3368 return SOLVER_PROBLEM_RPM_RULE;
3370 s = pool->solvables - r->p;
3371 if (installed && !solv->fixsystem && s->repo == installed)
3373 if (d == 0 && r->w2 < 0)
3375 /* a package conflict */
3376 Solvable *s2 = pool->solvables - r->w2;
3379 if (installed && !solv->fixsystem && s2->repo == installed)
3382 /* if both packages have the same name and at least one of them
3383 * is not installed, they conflict */
3384 if (s->name == s2->name && !(installed && s->repo == installed && s2->repo == installed))
3386 /* also check noobsoletes map */
3387 if ((s->evr == s2->evr && s->arch == s2->arch) || !solv->noobsoletes.size
3388 || ((!installed || s->repo != installed) && !MAPTST(&solv->noobsoletes, -r->p))
3389 || ((!installed || s2->repo != installed) && !MAPTST(&solv->noobsoletes, -r->w2)))
3394 return SOLVER_PROBLEM_SAME_NAME;
3398 /* check conflicts in both directions */
3401 conp = s->repo->idarraydata + s->conflicts;
3402 while ((con = *conp++) != 0)
3404 FOR_PROVIDES(p, pp, con)
3406 if (dontfix && pool->solvables[p].repo == installed)
3413 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
3419 conp = s2->repo->idarraydata + s2->conflicts;
3420 while ((con = *conp++) != 0)
3422 FOR_PROVIDES(p, pp, con)
3424 if (dontfix2 && pool->solvables[p].repo == installed)
3431 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
3435 /* check obsoletes in both directions */
3436 if ((!installed || s->repo != installed) && s->obsoletes && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -r->p)))
3438 obsp = s->repo->idarraydata + s->obsoletes;
3439 while ((obs = *obsp++) != 0)
3441 FOR_PROVIDES(p, pp, obs)
3445 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3450 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3454 if ((!installed || s2->repo != installed) && s2->obsoletes && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -r->w2)))
3456 obsp = s2->repo->idarraydata + s2->obsoletes;
3457 while ((obs = *obsp++) != 0)
3459 FOR_PROVIDES(p, pp, obs)
3463 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3468 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3472 if (solv->implicitobsoleteusesprovides && (!installed || s->repo != installed) && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -r->p)))
3474 FOR_PROVIDES(p, pp, s->name)
3481 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3484 if (solv->implicitobsoleteusesprovides && (!installed || s2->repo != installed) && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -r->w2)))
3486 FOR_PROVIDES(p, pp, s2->name)
3493 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3496 /* all cases checked, can't happen */
3500 return SOLVER_PROBLEM_RPM_RULE;
3502 /* simple requires */
3505 reqp = s->repo->idarraydata + s->requires;
3506 while ((req = *reqp++) != 0)
3508 if (req == SOLVABLE_PREREQMARKER)
3510 dp = pool_whatprovides(pool, req);
3513 if (*dp == r->w2 && dp[1] == 0)
3516 else if (dp - pool->whatprovidesdata == d)
3524 return SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE;
3527 /* all cases checked, can't happen */
3531 return SOLVER_PROBLEM_RPM_RULE;
3535 /*-------------------------------------------------------------------
3541 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp)
3544 Id lreqr, lconr, lsysr, ljobr;
3548 lreqr = lconr = lsysr = ljobr = 0;
3549 while ((rid = solv->learnt_pool.elements[idx++]) != 0)
3552 if (rid >= solv->learntrules)
3553 findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr);
3554 else if (rid >= solv->jobrules && rid < solv->jobrules_end)
3559 else if (rid >= solv->updaterules && rid < solv->updaterules_end)
3566 assert(rid < solv->rpmrules_end);
3567 r = solv->rules + rid;
3568 d = r->d < 0 ? -r->d - 1 : r->d;
3569 if (!d && r->w2 < 0)
3576 if (!d && r->w2 == 0 && !reqassert)
3578 /* prefer assertions (XXX: bad idea?) */
3584 else if (solv->installed && r->p < 0 && solv->pool->solvables[-r->p].repo == solv->installed)
3586 /* prefer rules of installed packages */
3587 Id op = *reqrp >= 0 ? solv->rules[*reqrp].p : -*reqrp;
3588 if (op <= 0 || solv->pool->solvables[op].repo != solv->installed)
3594 if (!*reqrp && lreqr)
3596 if (!*conrp && lconr)
3598 if (!*jobrp && ljobr)
3600 if (!*sysrp && lsysr)
3605 /*-------------------------------------------------------------------
3609 * search for a rule that describes the problem to the
3610 * user. A pretty hopeless task, actually. We currently
3611 * prefer simple requires.
3615 solver_findproblemrule(Solver *solv, Id problem)
3617 Id reqr, conr, sysr, jobr;
3618 Id idx = solv->problems.elements[problem - 1];
3619 reqr = conr = sysr = jobr = 0;
3620 findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr);
3633 /*-------------------------------------------------------------------
3635 * create reverse obsoletes map for installed solvables
3637 * for each installed solvable find which packages with *different* names
3638 * obsolete the solvable.
3639 * this index is used in policy_findupdatepackages if noupdateprovide is set.
3643 create_obsolete_index(Solver *solv)
3645 Pool *pool = solv->pool;
3647 Repo *installed = solv->installed;
3648 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
3651 if (!installed || !installed->nsolvables)
3653 solv->obsoletes = obsoletes = sat_calloc(installed->end - installed->start, sizeof(Id));
3654 for (i = 1; i < pool->nsolvables; i++)
3656 s = pool->solvables + i;
3659 if (!pool_installable(pool, s))
3661 obsp = s->repo->idarraydata + s->obsoletes;
3662 while ((obs = *obsp++) != 0)
3664 FOR_PROVIDES(p, pp, obs)
3666 if (pool->solvables[p].repo != installed)
3668 if (pool->solvables[p].name == s->name)
3670 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3672 obsoletes[p - installed->start]++;
3677 for (i = 0; i < installed->nsolvables; i++)
3680 n += obsoletes[i] + 1;
3683 solv->obsoletes_data = obsoletes_data = sat_calloc(n + 1, sizeof(Id));
3684 POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
3685 for (i = pool->nsolvables - 1; i > 0; i--)
3687 s = pool->solvables + i;
3690 if (!pool_installable(pool, s))
3692 obsp = s->repo->idarraydata + s->obsoletes;
3693 while ((obs = *obsp++) != 0)
3695 FOR_PROVIDES(p, pp, obs)
3697 if (pool->solvables[p].repo != installed)
3699 if (pool->solvables[p].name == s->name)
3701 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3703 p -= installed->start;
3704 if (obsoletes_data[obsoletes[p]] != i)
3705 obsoletes_data[--obsoletes[p]] = i;
3712 /*-------------------------------------------------------------------
3714 * remove disabled conflicts
3718 removedisabledconflicts(Solver *solv, Queue *removed)
3720 Pool *pool = solv->pool;
3725 Id *decisionmap = solv->decisionmap;
3727 POOL_DEBUG(SAT_DEBUG_STATS, "removedisabledconflicts\n");
3728 queue_empty(removed);
3729 for (i = 0; i < solv->decisionq.count; i++)
3731 p = solv->decisionq.elements[i];
3734 /* a conflict. we never do conflicts on free decisions, so there
3735 * must have been an unit rule */
3736 why = solv->decisionq_why.elements[i];
3738 r = solv->rules + why;
3739 if (r->d < 0 && decisionmap[-p])
3741 /* rule is now disabled, remove from decisionmap */
3742 POOL_DEBUG(SAT_DEBUG_STATS, "removing conflict for package %s[%d]\n", solvable2str(pool, pool->solvables - p), -p);
3743 queue_push(removed, -p);
3744 queue_push(removed, decisionmap[-p]);
3745 decisionmap[-p] = 0;
3748 if (!removed->count)
3750 /* we removed some confliced packages. some of them might still
3751 * be in conflict, so search for unit rules and re-conflict */
3753 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
3755 if (i == solv->nrules)
3758 r = solv->rules + i;
3764 if (r->p < 0 && !decisionmap[-r->p])
3770 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
3772 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
3777 if (r->p < 0 && decisionmap[-r->p] == 0)
3779 if (new || DECISIONMAP_FALSE(r->p))
3781 dp = pool->whatprovidesdata + r->d;
3782 while ((p = *dp++) != 0)
3784 if (new && p == new)
3786 if (p < 0 && decisionmap[-p] == 0)
3795 else if (!DECISIONMAP_FALSE(p))
3805 POOL_DEBUG(SAT_DEBUG_STATS, "re-conflicting package %s[%d]\n", solvable2str(pool, pool->solvables - new), -new);
3806 decisionmap[-new] = -1;
3808 n = 0; /* redo all rules */
3814 /*-------------------------------------------------------------------
3816 * weaken solvable dependencies
3820 weaken_solvable_deps(Solver *solv, Id p)
3825 for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
3829 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
3830 continue; /* conflict */
3831 queue_push(&solv->weakruleq, i);
3835 /********************************************************************/
3845 solver_solve(Solver *solv, Queue *job)
3847 Pool *pool = solv->pool;
3848 Repo *installed = solv->installed;
3851 Map addedmap; /* '1' == have rpm-rules for solvable */
3852 Map installcandidatemap;
3853 Id how, what, select, name, weak, p, *pp, d;
3859 POOL_DEBUG(SAT_DEBUG_STATS, "solver started\n");
3860 POOL_DEBUG(SAT_DEBUG_STATS, "fixsystem=%d updatesystem=%d dosplitprovides=%d, noupdateprovide=%d\n", solv->fixsystem, solv->updatesystem, solv->dosplitprovides, solv->noupdateprovide);
3861 POOL_DEBUG(SAT_DEBUG_STATS, "distupgrade=%d distupgrade_removeunsupported=%d\n", solv->distupgrade, solv->distupgrade_removeunsupported);
3862 POOL_DEBUG(SAT_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allowarchchange, solv->allowvendorchange);
3863 POOL_DEBUG(SAT_DEBUG_STATS, "promoteepoch=%d, allowvirtualconflicts=%d, allowselfconflicts=%d\n", pool->promoteepoch, solv->allowvirtualconflicts, solv->allowselfconflicts);
3864 POOL_DEBUG(SAT_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d\n", solv->obsoleteusesprovides, solv->implicitobsoleteusesprovides);
3865 POOL_DEBUG(SAT_DEBUG_STATS, "dontinstallrecommended=%d, ignorealreadyrecommended=%d, dontshowinstalledrecommended=%d\n", solv->dontinstallrecommended, solv->ignorealreadyrecommended, solv->dontshowinstalledrecommended);
3866 /* create whatprovides if not already there */
3867 if (!pool->whatprovides)
3868 pool_createwhatprovides(pool);
3870 /* create obsolete index if needed */
3871 create_obsolete_index(solv);
3874 * create basic rule set of all involved packages
3875 * use addedmap bitmap to make sure we don't create rules twice
3879 /* create noobsolete map if needed */
3880 for (i = 0; i < job->count; i += 2)
3882 how = job->elements[i] & ~SOLVER_WEAK;
3883 if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
3885 what = job->elements[i + 1];
3886 select = how & SOLVER_SELECTMASK;
3887 if (!solv->noobsoletes.size)
3888 map_init(&solv->noobsoletes, pool->nsolvables);
3889 FOR_JOB_SELECT(p, pp, select, what)
3890 MAPSET(&solv->noobsoletes, p);
3893 map_init(&addedmap, pool->nsolvables);
3894 map_init(&installcandidatemap, pool->nsolvables);
3898 * always install our system solvable
3900 MAPSET(&addedmap, SYSTEMSOLVABLE);
3901 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
3902 queue_push(&solv->decisionq_why, 0);
3903 solv->decisionmap[SYSTEMSOLVABLE] = 1; /* installed at level '1' */
3906 * create rules for all package that could be involved with the solving
3907 * so called: rpm rules
3912 oldnrules = solv->nrules;
3913 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
3914 FOR_REPO_SOLVABLES(installed, p, s)
3915 addrpmrulesforsolvable(solv, s, &addedmap);
3916 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3917 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
3918 oldnrules = solv->nrules;
3919 FOR_REPO_SOLVABLES(installed, p, s)
3920 addrpmrulesforupdaters(solv, s, &addedmap, 1);
3921 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3925 * create rules for all packages involved in the job
3926 * (to be installed or removed)
3929 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
3930 oldnrules = solv->nrules;
3931 for (i = 0; i < job->count; i += 2)
3933 how = job->elements[i];
3934 what = job->elements[i + 1];
3935 select = how & SOLVER_SELECTMASK;
3937 switch (how & SOLVER_JOBMASK)
3939 case SOLVER_INSTALL:
3940 FOR_JOB_SELECT(p, pp, select, what)
3942 MAPSET(&installcandidatemap, p);
3943 addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3947 /* FIXME: semantics? */
3948 FOR_JOB_SELECT(p, pp, select, what)
3949 addrpmrulesforupdaters(solv, pool->solvables + what, &addedmap, 0);
3953 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3955 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for recommended/suggested packages ***\n");
3957 oldnrules = solv->nrules;
3960 * add rules for suggests, enhances
3962 addrpmrulesforweak(solv, &addedmap);
3963 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3965 IF_POOLDEBUG (SAT_DEBUG_STATS)
3967 int possible = 0, installable = 0;
3968 for (i = 1; i < pool->nsolvables; i++)
3970 if (pool_installable(pool, pool->solvables + i))
3972 if (MAPTST(&addedmap, i))
3975 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
3979 * first pass done, we now have all the rpm rules we need.
3980 * unify existing rules before going over all job rules and
3982 * at this point the system is always solvable,
3983 * as an empty system (remove all packages) is a valid solution
3986 unifyrules(solv); /* remove duplicate rpm rules */
3988 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
3990 solv->directdecisions = solv->decisionq.count;
3991 POOL_DEBUG(SAT_DEBUG_STATS, "decisions so far: %d\n", solv->decisionq.count);
3994 * create feature rules
3996 * foreach installed:
3997 * create assertion (keep installed, if no update available)
3999 * create update rule (A|update1(A)|update2(A)|...)
4001 * those are used later on to keep a version of the installed packages in
4005 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add feature rules ***\n");
4006 solv->featurerules = solv->nrules; /* mark start of feature rules */
4009 /* foreach possibly installed solvable */
4010 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
4012 if (s->repo != installed)
4014 addrule(solv, 0, 0); /* create dummy rule */
4017 addupdaterule(solv, s, 1); /* allow s to be updated */
4020 * assert one rule per installed solvable,
4021 * either an assertion (A)
4022 * or a possible update (A|update1(A)|update2(A)|...)
4024 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
4026 solv->featurerules_end = solv->nrules;
4029 * Add update rules for installed solvables
4031 * almost identical to feature rules
4032 * except that downgrades/archchanges/vendorchanges are not allowed
4035 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add update rules ***\n");
4036 solv->updaterules = solv->nrules;
4039 { /* foreach installed solvables */
4040 /* we create all update rules, but disable some later on depending on the job */
4041 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
4045 if (s->repo != installed)
4047 addrule(solv, 0, 0); /* create dummy rule */
4050 addupdaterule(solv, s, 0); /* allowall = 0: downgrades not allowed */
4052 * check for and remove duplicate
4054 r = solv->rules + solv->nrules - 1; /* r: update rule */
4055 sr = r - (installed->end - installed->start); /* sr: feature rule */
4056 /* it's orphaned if there is no feature rule or the feature rule
4057 * consists just of the installed package */
4058 if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
4059 queue_push(&solv->orphaned, i);
4062 assert(!sr->p); /* can't have feature rule and no update rule */
4065 unifyrules_sortcmp_data = pool;
4066 if (!unifyrules_sortcmp(r, sr))
4068 /* identical rule, kill unneeded rule */
4069 if (solv->allowuninstall)
4071 /* keep feature rule, make it weak */
4072 memset(r, 0, sizeof(*r));
4073 queue_push(&solv->weakruleq, sr - solv->rules);
4077 /* keep update rule */
4078 memset(sr, 0, sizeof(*sr));
4081 else if (solv->allowuninstall)
4083 /* make both feature and update rule weak */
4084 queue_push(&solv->weakruleq, r - solv->rules);
4085 queue_push(&solv->weakruleq, sr - solv->rules);
4088 disablerule(solv, sr);
4090 /* consistency check: we added a rule for _every_ installed solvable */
4091 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
4093 solv->updaterules_end = solv->nrules;
4097 * now add all job rules
4100 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
4102 solv->jobrules = solv->nrules;
4103 for (i = 0; i < job->count; i += 2)
4105 oldnrules = solv->nrules;
4107 how = job->elements[i];
4108 what = job->elements[i + 1];
4109 weak = how & SOLVER_WEAK;
4110 select = how & SOLVER_SELECTMASK;
4111 switch (how & SOLVER_JOBMASK)
4113 case SOLVER_INSTALL:
4114 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4115 if (select == SOLVER_SOLVABLE)
4123 FOR_JOB_SELECT(p, pp, select, what)
4127 /* no candidate found, make this an impossible rule */
4128 queue_push(&q, -SYSTEMSOLVABLE);
4130 p = queue_shift(&q); /* get first candidate */
4131 d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q); /* internalize */
4133 addrule(solv, p, d); /* add install rule */
4134 queue_push(&solv->ruletojob, i);
4136 queue_push(&solv->weakruleq, solv->nrules - 1);
4139 POOL_DEBUG(SAT_DEBUG_JOB, "job: %serase %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4140 if (select == SOLVER_SOLVABLE && solv->installed && pool->solvables[what].repo == solv->installed)
4142 /* special case for "erase a specific solvable": we also
4143 * erase all other solvables with that name, so that they
4144 * don't get picked up as replacement */
4145 name = pool->solvables[what].name;
4146 FOR_PROVIDES(p, pp, name)
4150 s = pool->solvables + p;
4151 if (s->name != name)
4153 /* keep other versions installed */
4154 if (s->repo == solv->installed)
4156 /* keep installcandidates of other jobs */
4157 if (MAPTST(&installcandidatemap, p))
4159 addrule(solv, -p, 0); /* remove by Id */
4160 queue_push(&solv->ruletojob, i);
4162 queue_push(&solv->weakruleq, solv->nrules - 1);
4165 FOR_JOB_SELECT(p, pp, select, what)
4167 addrule(solv, -p, 0);
4168 queue_push(&solv->ruletojob, i);
4170 queue_push(&solv->weakruleq, solv->nrules - 1);
4175 POOL_DEBUG(SAT_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4176 if (select != SOLVER_SOLVABLE)
4178 s = pool->solvables + what;
4179 POOL_DEBUG(SAT_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solvable2str(pool, s));
4180 addupdaterule(solv, s, 0);
4181 queue_push(&solv->ruletojob, i);
4183 queue_push(&solv->weakruleq, solv->nrules - 1);
4185 case SOLVER_WEAKENDEPS:
4186 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4187 if (select != SOLVER_SOLVABLE)
4189 s = pool->solvables + what;
4190 weaken_solvable_deps(solv, what);
4192 case SOLVER_NOOBSOLETES:
4193 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4196 POOL_DEBUG(SAT_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
4197 FOR_JOB_SELECT(p, pp, select, what)
4199 s = pool->solvables + p;
4200 if (installed && s->repo == installed)
4201 addrule(solv, p, 0);
4203 addrule(solv, -p, 0);
4204 queue_push(&solv->ruletojob, i);
4206 queue_push(&solv->weakruleq, solv->nrules - 1);
4210 POOL_DEBUG(SAT_DEBUG_JOB, "job: unknown job\n");
4218 IF_POOLDEBUG (SAT_DEBUG_JOB)
4221 if (solv->nrules == oldnrules)
4222 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created\n");
4223 for (j = oldnrules; j < solv->nrules; j++)
4225 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
4226 solver_printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
4230 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
4231 solv->jobrules_end = solv->nrules;
4233 /* all rules created
4234 * --------------------------------------------------------------
4235 * prepare for solving
4238 /* free unneeded memory */
4239 map_free(&addedmap);
4240 map_free(&installcandidatemap);
4243 /* create weak map */
4244 map_init(&solv->weakrulemap, solv->nrules);
4245 for (i = 0; i < solv->weakruleq.count; i++)
4247 p = solv->weakruleq.elements[i];
4248 MAPSET(&solv->weakrulemap, p);
4251 /* all new rules are learnt after this point */
4252 solv->learntrules = solv->nrules;
4254 /* create assertion index. it is only used to speed up
4255 * makeruledecsions() a bit */
4256 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
4257 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
4258 queue_push(&solv->ruleassertions, i);
4260 /* disable update rules that conflict with our job */
4261 disableupdaterules(solv, job, -1);
4263 /* make decisions based on job/update assertions */
4264 makeruledecisions(solv);
4266 /* create watches chains */
4269 POOL_DEBUG(SAT_DEBUG_STATS, "problems so far: %d\n", solv->problems.count);
4272 * ********************************************
4274 * ********************************************
4277 run_solver(solv, 1, solv->dontinstallrecommended ? 0 : 1);
4281 /* disable all erase jobs (including weak "keep uninstalled" rules) */
4282 for (i = solv->jobrules, r = solv->rules + i; i < solv->learntrules; i++, r++)
4284 if (r->d < 0) /* disabled ? */
4286 if (r->p > 0) /* install job? */
4288 disablerule(solv, r);
4294 enabledisablelearntrules(solv);
4295 removedisabledconflicts(solv, &redoq);
4299 * find recommended packages
4302 /* if redoq.count == 0 we already found all recommended in the
4304 if (redoq.count || solv->dontinstallrecommended || !solv->dontshowinstalledrecommended || solv->ignorealreadyrecommended)
4306 Id rec, *recp, p, *pp;
4308 /* create map of all recommened packages */
4309 solv->recommends_index = -1;
4310 MAPZERO(&solv->recommendsmap);
4311 for (i = 0; i < solv->decisionq.count; i++)
4313 p = solv->decisionq.elements[i];
4316 s = pool->solvables + p;
4319 recp = s->repo->idarraydata + s->recommends;
4320 while ((rec = *recp++) != 0)
4322 FOR_PROVIDES(p, pp, rec)
4323 if (solv->decisionmap[p] > 0)
4327 if (!solv->dontshowinstalledrecommended)
4329 FOR_PROVIDES(p, pp, rec)
4330 if (solv->decisionmap[p] > 0)
4331 MAPSET(&solv->recommendsmap, p);
4333 continue; /* p != 0: already fulfilled */
4335 FOR_PROVIDES(p, pp, rec)
4336 MAPSET(&solv->recommendsmap, p);
4340 for (i = 1; i < pool->nsolvables; i++)
4342 if (solv->decisionmap[i] < 0)
4344 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
4346 s = pool->solvables + i;
4347 if (!MAPTST(&solv->recommendsmap, i))
4349 if (!s->supplements)
4351 if (!pool_installable(pool, s))
4353 if (!solver_is_supplementing(solv, s))
4356 if (solv->dontinstallrecommended)
4357 queue_push(&solv->recommendations, i);
4359 queue_pushunique(&solv->recommendations, i);
4361 /* we use MODE_SUGGEST here so that repo prio is ignored */
4362 policy_filter_unwanted(solv, &solv->recommendations, 0, POLICY_MODE_SUGGEST);
4366 * find suggested packages
4371 Id sug, *sugp, p, *pp;
4373 /* create map of all suggests that are still open */
4374 solv->recommends_index = -1;
4375 MAPZERO(&solv->suggestsmap);
4376 for (i = 0; i < solv->decisionq.count; i++)
4378 p = solv->decisionq.elements[i];
4381 s = pool->solvables + p;
4384 sugp = s->repo->idarraydata + s->suggests;
4385 while ((sug = *sugp++) != 0)
4387 FOR_PROVIDES(p, pp, sug)
4388 if (solv->decisionmap[p] > 0)
4392 if (!solv->dontshowinstalledrecommended)
4394 FOR_PROVIDES(p, pp, sug)
4395 if (solv->decisionmap[p] > 0)
4396 MAPSET(&solv->suggestsmap, p);
4398 continue; /* already fulfilled */
4400 FOR_PROVIDES(p, pp, sug)
4401 MAPSET(&solv->suggestsmap, p);
4405 for (i = 1; i < pool->nsolvables; i++)
4407 if (solv->decisionmap[i] < 0)
4409 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
4411 s = pool->solvables + i;
4412 if (!MAPTST(&solv->suggestsmap, i))
4416 if (!pool_installable(pool, s))
4418 if (!solver_is_enhancing(solv, s))
4421 queue_push(&solv->suggestions, i);
4423 policy_filter_unwanted(solv, &solv->suggestions, 0, POLICY_MODE_SUGGEST);
4428 /* restore decisionmap */
4429 for (i = 0; i < redoq.count; i += 2)
4430 solv->decisionmap[redoq.elements[i]] = redoq.elements[i + 1];
4434 * if unsolvable, prepare solutions
4437 if (solv->problems.count)
4439 int recocount = solv->recommendations.count;
4440 solv->recommendations.count = 0; /* so that revert() doesn't mess with it */
4441 queue_empty(&redoq);
4442 for (i = 0; i < solv->decisionq.count; i++)
4444 Id p = solv->decisionq.elements[i];
4445 queue_push(&redoq, p);
4446 queue_push(&redoq, solv->decisionq_why.elements[i]);
4447 queue_push(&redoq, solv->decisionmap[p > 0 ? p : -p]);
4449 problems_to_solutions(solv, job);
4450 memset(solv->decisionmap, 0, pool->nsolvables * sizeof(Id));
4451 queue_empty(&solv->decisionq);
4452 queue_empty(&solv->decisionq_why);
4453 for (i = 0; i < redoq.count; i += 3)
4455 Id p = redoq.elements[i];
4456 queue_push(&solv->decisionq, p);
4457 queue_push(&solv->decisionq_why, redoq.elements[i + 1]);
4458 solv->decisionmap[p > 0 ? p : -p] = redoq.elements[i + 2];
4460 solv->recommendations.count = recocount;
4463 POOL_DEBUG(SAT_DEBUG_STATS, "final solver statistics: %d learned rules, %d unsolvable\n", solv->stats_learned, solv->stats_unsolvable);
4467 /***********************************************************************/
4468 /* disk usage computations */
4470 /*-------------------------------------------------------------------
4472 * calculate DU changes
4476 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
4480 solver_create_state_maps(solv, &installedmap, 0);
4481 pool_calc_duchanges(solv->pool, solv->installed, &installedmap, mps, nmps);
4482 map_free(&installedmap);
4486 /*-------------------------------------------------------------------
4488 * calculate changes in install size
4492 solver_calc_installsizechange(Solver *solv)
4497 solver_create_state_maps(solv, &installedmap, 0);
4498 change = pool_calc_installsizechange(solv->pool, solv->installed, &installedmap);
4499 map_free(&installedmap);