2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * SAT based dependency solver
26 #define RULES_BLOCK 63
27 #define REGARD_RECOMMENDS_OF_INSTALLED_ITEMS 0
31 solver_dep_installed(Solver *solv, Id dep)
33 /* disable for now, splitprovides don't work anyway and it breaks
36 Pool *pool = solv->pool;
41 Reldep *rd = GETRELDEP(pool, dep);
42 if (rd->flags == REL_AND)
44 if (!solver_dep_installed(solv, rd->name))
46 return solver_dep_installed(solv, rd->evr);
48 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
49 return solver_dep_installed(solv, rd->evr);
51 FOR_PROVIDES(p, pp, dep)
53 if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
61 /* this mirrors solver_dep_fulfilled but uses map m instead of the decisionmap */
63 dep_possible(Solver *solv, Id dep, Map *m)
65 Pool *pool = solv->pool;
70 Reldep *rd = GETRELDEP(pool, dep);
71 if (rd->flags == REL_AND)
73 if (!dep_possible(solv, rd->name, m))
75 return dep_possible(solv, rd->evr, m);
77 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
78 return solver_dep_installed(solv, rd->evr);
80 FOR_PROVIDES(p, pp, dep)
88 /*-----------------------------------------------------------------*/
95 printruleelement(Solver *solv, int type, Rule *r, Id v)
97 Pool *pool = solv->pool;
101 s = pool->solvables + -v;
102 POOL_DEBUG(type, " !%s [%d]", solvable2str(pool, s), -v);
106 s = pool->solvables + v;
107 POOL_DEBUG(type, " %s [%d]", solvable2str(pool, s), v);
112 POOL_DEBUG(type, " (w1)");
114 POOL_DEBUG(type, " (w2)");
116 if (solv->decisionmap[s - pool->solvables] > 0)
117 POOL_DEBUG(type, " Install.level%d", solv->decisionmap[s - pool->solvables]);
118 if (solv->decisionmap[s - pool->solvables] < 0)
119 POOL_DEBUG(type, " Conflict.level%d", -solv->decisionmap[s - pool->solvables]);
120 POOL_DEBUG(type, "\n");
129 printrule(Solver *solv, int type, Rule *r)
131 Pool *pool = solv->pool;
135 if (r >= solv->rules && r < solv->rules + solv->nrules) /* r is a solver rule */
136 POOL_DEBUG(type, "Rule #%d:", (int)(r - solv->rules));
138 POOL_DEBUG(type, "Rule:"); /* r is any rule */
140 POOL_DEBUG(type, " (disabled)");
141 POOL_DEBUG(type, "\n");
145 /* print direct literal */
147 else if (r->d == ID_NULL)
151 /* binary rule --> print w2 as second literal */
155 /* every other which is in d */
156 v = solv->pool->whatprovidesdata[r->d + i - 1];
159 printruleelement(solv, type, r, v);
161 POOL_DEBUG(type, " next rules: %d %d\n", r->n1, r->n2);
165 printruleclass(Solver *solv, int type, Rule *r)
167 Pool *pool = solv->pool;
168 if (r - solv->rules >= solv->learntrules)
169 POOL_DEBUG(type, "LEARNT ");
170 else if (r - solv->rules >= solv->weakrules)
171 POOL_DEBUG(type, "WEAK ");
172 else if (r - solv->rules >= solv->systemrules)
173 POOL_DEBUG(type, "SYSTEM ");
174 else if (r - solv->rules >= solv->jobrules)
175 POOL_DEBUG(type, "JOB ");
176 printrule(solv, type, r);
180 /*-----------------------------------------------------------------*/
186 static Pool *unifyrules_sortcmp_data;
189 * compare rules for unification sort
193 unifyrules_sortcmp(const void *ap, const void *bp)
195 Pool *pool = unifyrules_sortcmp_data;
196 Rule *a = (Rule *)ap;
197 Rule *b = (Rule *)bp;
203 return x; /* p differs */
206 if (a->d == 0 && b->d == 0)
207 return a->w2 - b->w2; /* assertion: return w2 diff */
209 if (a->d == 0) /* a is assertion, b not */
211 x = a->w2 - pool->whatprovidesdata[b->d];
215 if (b->d == 0) /* b is assertion, a not */
217 x = pool->whatprovidesdata[a->d] - b->w2;
221 /* compare whatprovidesdata */
222 ad = pool->whatprovidesdata + a->d;
223 bd = pool->whatprovidesdata + b->d;
225 if ((x = *ad++ - *bd++) != 0)
236 unifyrules(Solver *solv)
238 Pool *pool = solv->pool;
242 if (solv->nrules <= 1) /* nothing to unify */
245 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules -----\n");
247 /* sort rules first */
248 unifyrules_sortcmp_data = solv->pool;
249 qsort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp);
256 for (i = j = 1, ir = solv->rules + 1; i < solv->nrules; i++, ir++)
258 if (jr && !unifyrules_sortcmp(ir, jr))
259 continue; /* prune! */
260 jr = solv->rules + j++; /* keep! */
265 /* reduced count from nrules to j rules */
266 POOL_DEBUG(SAT_DEBUG_STATS, "pruned rules from %d to %d\n", solv->nrules, j);
268 /* adapt rule buffer */
270 solv->rules = (Rule *)xrealloc(solv->rules, ((solv->nrules + RULES_BLOCK) & ~RULES_BLOCK) * sizeof(Rule));
271 IF_POOLDEBUG (SAT_DEBUG_STATS)
278 for (i = 1; i < solv->nrules; i++)
285 dp = solv->pool->whatprovidesdata + r->d;
290 POOL_DEBUG(SAT_DEBUG_STATS, " binary: %d\n", binr);
291 POOL_DEBUG(SAT_DEBUG_STATS, " normal: %d, %d literals\n", solv->nrules - 1 - binr, lits);
293 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules end -----\n");
303 hashrule(Solver *solv, Id p, Id d, int n)
305 unsigned int x = (unsigned int)p;
309 return (x * 37) ^ (unsigned int)d;
310 dp = solv->pool->whatprovidesdata + d;
312 x = (x * 37) ^ (unsigned int)*dp++;
320 * p = direct literal; always < 0 for installed rpm rules
321 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
324 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
326 * p < 0 : pkg id of A
327 * d > 0 : Offset in whatprovidesdata (list of providers of b)
329 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
330 * p < 0 : pkg id of A
331 * d < 0 : Id of solvable (e.g. B1)
333 * d == 0: unary rule, assertion => (A) or (-A)
335 * Install: p > 0, d = 0 (A) user requested install
336 * Remove: p < 0, d = 0 (-A) user requested remove
337 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
338 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
339 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider) (binary rule)
340 * ? p > 0, d < 0 (A|-B)
341 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
345 * Direct assertion (no watch needed)( if d <0 ) --> d = 0, w1 = p, w2 = 0
346 * Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
347 * every other : w1 = p, w2 = whatprovidesdata[d];
348 * Disabled rule: w1 = 0
350 * always returns a rule for non-rpm rules
354 addrule(Solver *solv, Id p, Id d)
356 Pool *pool = solv->pool;
360 int n = 0; /* number of literals in rule - 1
361 0 = direct assertion (single literal)
365 /* it often happenes that requires lead to adding the same rpm rule
366 * multiple times, so we prune those duplicates right away to make
367 * the work for unifyrules a bit easier */
369 if (solv->nrules && !solv->jobrules)
371 r = solv->rules + solv->nrules - 1; /* get the last added rule */
372 if (r->p == p && r->d == d && d != 0) /* identical and not user requested */
378 /* always a binary rule */
380 return 0; /* ignore self conflict */
385 for (dp = pool->whatprovidesdata + d; *dp; dp++, n++)
387 return 0; /* rule is self-fulfilling */
389 d = dp[-1]; /* take single literal */
392 if (n == 0 && !solv->jobrules)
394 /* this is a rpm rule assertion, we do not have to allocate it */
395 /* it can be identified by a level of 1 and a zero reason */
396 /* we must not drop those rules from the decisionq when rewinding! */
399 if (solv->decisionmap[-p] > 0 || solv->decisionmap[-p] < -1)
401 if (solv->decisionmap[-p])
402 return 0; /* already got that one */
403 queue_push(&solv->decisionq, p);
404 queue_push(&solv->decisionq_why, 0);
405 solv->decisionmap[-p] = -1;
410 /* smallest literal first so we can find dups */
414 n = 1; /* re-set n, was used as temp var */
417 /* check if the last added rule is exactly the same as what we're looking for. */
418 if (r && n == 1 && !r->d && r->p == p && r->w2 == d)
419 return r; /* binary rule */
421 if (r && n > 1 && r->d && r->p == p)
423 /* Rule where d is an offset in whatprovidesdata */
427 dp2 = pool->whatprovidesdata + r->d;
428 for (dp = pool->whatprovidesdata + d; *dp; dp++, dp2++)
439 /* check and extend rule buffer */
440 if ((solv->nrules & RULES_BLOCK) == 0)
442 solv->rules = (Rule *)xrealloc(solv->rules, (solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
445 r = solv->rules + solv->nrules++; /* point to rule space */
450 /* direct assertion, no watch needed */
466 r->w2 = pool->whatprovidesdata[d];
471 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
473 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " Add rule: ");
474 printrule(solv, SAT_DEBUG_RULE_CREATION, r);
481 disablerule(Solver *solv, Rule *r)
487 enablerule(Solver *solv, Rule *r)
489 if (r->d == 0 || r->w2 != r->p)
492 r->w1 = solv->pool->whatprovidesdata[r->d];
496 /**********************************************************************************/
498 /* a problem is an item on the solver's problem list. It can either be >0, in that
499 * case it is a system (upgrade) rule, or it can be <0, which makes it refer to a job
500 * consisting of multiple job rules.
504 disableproblem(Solver *solv, Id v)
512 disablerule(solv, solv->rules + v);
516 jp = solv->ruletojob.elements;
517 for (i = solv->jobrules, r = solv->rules + i; i < solv->systemrules; i++, r++, jp++)
519 disablerule(solv, r);
523 enableproblem(Solver *solv, Id v)
531 enablerule(solv, solv->rules + v);
535 jp = solv->ruletojob.elements;
536 for (i = solv->jobrules, r = solv->rules + i; i < solv->systemrules; i++, r++, jp++)
542 printproblem(Solver *solv, Id v)
544 Pool *pool = solv->pool;
550 printrule(solv, SAT_DEBUG_SOLUTIONS, solv->rules + v);
554 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "JOB %d\n", v);
555 jp = solv->ruletojob.elements;
556 for (i = solv->jobrules, r = solv->rules + i; i < solv->systemrules; i++, r++, jp++)
559 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "- ");
560 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
562 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "ENDJOB\n");
568 /************************************************************************/
570 /* go through system and job rules and add direct assertions
571 * to the decisionqueue. If we find a conflict, disable rules and
572 * add them to problem queue.
573 * (disablerules is always true for the cases when we get called)
576 makeruledecisions(Solver *solv)
578 Pool *pool = solv->pool;
584 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions ; size decisionq: %d -----\n",solv->decisionq.count);
586 decisionstart = solv->decisionq.count;
587 /* rpm rules don't have assertions, so we can start with the job
589 for (ri = solv->jobrules, r = solv->rules + ri; ri < solv->nrules; ri++, r++)
591 if (!r->w1 || r->w2) /* disabled or no assertion */
595 if (solv->decisionmap[vv] == 0)
597 queue_push(&solv->decisionq, v);
598 queue_push(&solv->decisionq_why, r - solv->rules);
599 solv->decisionmap[vv] = v > 0 ? 1 : -1;
600 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
602 Solvable *s = solv->pool->solvables + vv;
604 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", solvable2str(solv->pool, s));
606 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (assertion)\n", solvable2str(solv->pool, s));
610 if (v > 0 && solv->decisionmap[vv] > 0)
612 if (v < 0 && solv->decisionmap[vv] < 0)
614 /* found a conflict! */
615 if (ri >= solv->learntrules)
617 /* cannot happen, as this would mean that the problem
618 * was not solvable, so we wouldn't have created the
619 * learnt rule at all */
622 /* if we are weak, just disable ourself */
623 if (ri >= solv->weakrules)
625 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
626 printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
627 disablerule(solv, r);
631 /* only job and system rules left in the decisionq*/
632 /* find the decision which is the "opposite" of the jobrule */
633 for (i = 0; i < solv->decisionq.count; i++)
634 if (solv->decisionq.elements[i] == -v)
636 if (i == solv->decisionq.count)
638 if (solv->decisionq_why.elements[i] == 0)
640 /* conflict with rpm rule, need only disable our rule */
641 if (v < 0 && v != -SYSTEMSOLVABLE)
644 queue_push(&solv->problems, solv->learnt_pool.count);
645 queue_push(&solv->learnt_pool, ri);
646 queue_push(&solv->learnt_pool, v != -SYSTEMSOLVABLE ? -v : v);
647 queue_push(&solv->learnt_pool, 0);
648 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
649 if (ri < solv->systemrules)
650 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
653 queue_push(&solv->problems, v);
654 disableproblem(solv, v);
655 queue_push(&solv->problems, 0);
659 queue_push(&solv->problems, solv->learnt_pool.count);
660 queue_push(&solv->learnt_pool, ri);
661 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
662 queue_push(&solv->learnt_pool, 0);
664 /* conflict with another job or system rule */
665 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflicting system/job assertions over literal %d\n", vv);
666 /* push all of our rules asserting this literal on the problem stack */
667 for (i = solv->jobrules, rr = solv->rules + i; i < solv->nrules; i++, rr++)
669 if (!rr->w1 || rr->w2)
671 if (rr->p != vv && rr->p != -vv)
673 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
675 if (i < solv->systemrules)
676 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
677 queue_push(&solv->problems, v);
678 disableproblem(solv, v);
680 queue_push(&solv->problems, 0);
683 while (solv->decisionq.count > decisionstart)
685 v = solv->decisionq.elements[--solv->decisionq.count];
686 --solv->decisionq_why.count;
688 solv->decisionmap[vv] = 0;
690 ri = solv->jobrules - 1;
691 r = solv->rules + ri;
694 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions end; size decisionq: %d -----\n",solv->decisionq.count);
698 * we have enabled or disabled some of our rules. We now reenable all
699 * of our learnt rules but the ones that were learnt from rules that
703 enabledisablelearntrules(Solver *solv)
705 Pool *pool = solv->pool;
710 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
711 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
713 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
714 while ((why = *whyp++) != 0)
716 if (why < 0 || why >= i)
717 abort(); /* cannot reference newer learnt rules */
718 if (!solv->rules[why].w1)
721 /* why != 0: we found a disabled rule, disable the learnt rule */
724 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
726 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "disabling learnt ");
727 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
729 disablerule(solv, r);
731 else if (!why && !r->w1)
733 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
735 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "re-enabling learnt ");
736 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
744 /* FIXME: bad code ahead, replace as soon as possible */
746 disableupdaterules(Solver *solv, Queue *job, int jobidx)
748 Pool *pool = solv->pool;
750 Id how, what, p, *pp;
756 installed = solv->installed;
762 how = job->elements[jobidx];
765 case SOLVER_INSTALL_SOLVABLE:
766 case SOLVER_ERASE_SOLVABLE:
767 case SOLVER_ERASE_SOLVABLE_NAME:
768 case SOLVER_ERASE_SOLVABLE_PROVIDES:
774 /* go through all enabled job rules */
775 MAPZERO(&solv->noupdate);
776 for (i = solv->jobrules; i < solv->systemrules; i++)
779 if (!r->w1) /* disabled? */
781 j = solv->ruletojob.elements[i - solv->jobrules];
785 how = job->elements[j];
786 what = job->elements[j + 1];
789 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
790 s = pool->solvables + what;
791 FOR_PROVIDES(p, pp, s->name)
793 if (pool->solvables[p].name != s->name)
795 if (pool->solvables[p].repo == installed)
796 MAPSET(&solv->noupdate, p - installed->start);
799 case SOLVER_ERASE_SOLVABLE:
800 s = pool->solvables + what;
801 if (s->repo == installed)
802 MAPSET(&solv->noupdate, what - installed->start);
804 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
805 case SOLVER_ERASE_SOLVABLE_PROVIDES:
806 FOR_PROVIDES(p, pp, what)
808 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
810 if (pool->solvables[p].repo == installed)
811 MAPSET(&solv->noupdate, p - installed->start);
819 /* fixup update rule status */
820 if (solv->allowuninstall)
821 return; /* no update rules at all */
825 /* we just disabled job #jobidx. enable all update rules
826 * that aren't disabled by the remaining job rules */
827 how = job->elements[jobidx];
828 what = job->elements[jobidx + 1];
831 case SOLVER_INSTALL_SOLVABLE:
832 s = pool->solvables + what;
833 FOR_PROVIDES(p, pp, s->name)
835 if (pool->solvables[p].name != s->name)
837 if (pool->solvables[p].repo != installed)
839 if (MAPTST(&solv->noupdate, p - installed->start))
841 r = solv->rules + solv->systemrules + (p - installed->start);
845 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
847 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
848 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
852 case SOLVER_ERASE_SOLVABLE:
853 s = pool->solvables + what;
854 if (s->repo != installed)
856 if (MAPTST(&solv->noupdate, what - installed->start))
858 r = solv->rules + solv->systemrules + (what - installed->start);
862 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
864 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
865 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
868 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
869 case SOLVER_ERASE_SOLVABLE_PROVIDES:
870 FOR_PROVIDES(p, pp, what)
872 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
874 if (pool->solvables[p].repo != installed)
876 if (MAPTST(&solv->noupdate, p - installed->start))
878 r = solv->rules + solv->systemrules + (p - installed->start);
882 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
884 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
885 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
895 for (i = 0; i < installed->nsolvables; i++)
897 r = solv->rules + solv->systemrules + i;
898 if (r->w1 && MAPTST(&solv->noupdate, i))
899 disablerule(solv, r); /* was enabled, need to disable */
905 * add (install) rules for solvable
906 * for unfulfilled requirements, conflicts, obsoletes,....
907 * add a negative assertion for solvables that are not installable
911 addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
913 Pool *pool = solv->pool;
914 Repo *installed = solv->installed;
928 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable -----\n");
930 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
931 queue_push(&q, s - pool->solvables); /* push solvable Id */
937 * s: Pointer to solvable
941 if (MAPTST(m, n)) /* continue if already done */
945 s = pool->solvables + n; /* s = Solvable in question */
948 if (installed /* Installed system available */
949 && !solv->fixsystem /* NOT repair errors in rpm dependency graph */
950 && s->repo == installed) /* solvable is installed? */
952 dontfix = 1; /* dont care about broken rpm deps */
955 if (!dontfix && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
957 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", solvable2str(pool, s), (Id)(s - pool->solvables));
958 addrule(solv, -n, 0); /* uninstallable */
961 /*-----------------------------------------
962 * check requires of s
967 reqp = s->repo->idarraydata + s->requires;
968 while ((req = *reqp++) != 0) /* go throw all requires */
970 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
973 dp = pool_whatprovides(pool, req);
975 if (*dp == SYSTEMSOLVABLE) /* always installed */
980 /* the strategy here is to not insist on dependencies
981 * that are already broken. so if we find one provider
982 * that was already installed, we know that the
983 * dependency was not broken before so we enforce it */
984 for (i = 0; (p = dp[i]) != 0; i++) /* for all providers */
986 if (pool->solvables[p].repo == installed)
987 break; /* provider was installed */
989 if (!p) /* previously broken dependency */
991 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", dep2str(pool, req), solvable2str(pool, s));
998 /* nothing provides req! */
999 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", solvable2str(pool, s), (Id)(s - pool->solvables), dep2str(pool, req));
1000 addrule(solv, -n, 0); /* mark requestor as uninstallable */
1004 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1006 POOL_DEBUG(SAT_DEBUG_RULE_CREATION," %s requires %s\n", solvable2str(pool, s), dep2str(pool, req));
1007 for (i = 0; dp[i]; i++)
1008 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " provided by %s\n", solvable2str(pool, pool->solvables + dp[i]));
1011 /* add 'requires' dependency */
1012 /* rule: (-requestor|provider1|provider2|...|providerN) */
1013 addrule(solv, -n, dp - pool->whatprovidesdata);
1015 /* descend the dependency tree */
1016 for (; *dp; dp++) /* loop through all providers */
1018 if (!MAPTST(m, *dp))
1019 queue_push(&q, *dp);
1022 } /* while, requirements of n */
1024 } /* if, requirements */
1026 /* that's all we check for src packages */
1027 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
1030 /*-----------------------------------------
1031 * check conflicts of s
1036 conp = s->repo->idarraydata + s->conflicts;
1037 while ((con = *conp++) != 0)
1039 FOR_PROVIDES(p, pp, con)
1041 /* dontfix: dont care about conflicts with already installed packs */
1042 if (dontfix && pool->solvables[p].repo == installed)
1044 /* rule: -n|-p: either solvable _or_ provider of conflict */
1045 addrule(solv, -n, -p);
1050 /*-----------------------------------------
1051 * check obsoletes if not installed
1053 if (!installed || pool->solvables[n].repo != installed)
1054 { /* not installed */
1057 obsp = s->repo->idarraydata + s->obsoletes;
1058 while ((obs = *obsp++) != 0)
1060 FOR_PROVIDES(p, pp, obs)
1061 addrule(solv, -n, -p);
1064 FOR_PROVIDES(p, pp, s->name)
1066 if (s->name == pool->solvables[p].name)
1067 addrule(solv, -n, -p);
1071 /*-----------------------------------------
1072 * add recommends to the rule list
1076 recp = s->repo->idarraydata + s->recommends;
1077 while ((rec = *recp++) != 0)
1079 FOR_PROVIDES(p, pp, rec)
1086 sugp = s->repo->idarraydata + s->suggests;
1087 while ((sug = *sugp++) != 0)
1089 FOR_PROVIDES(p, pp, sug)
1096 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable end -----\n");
1100 addrpmrulesforweak(Solver *solv, Map *m)
1102 Pool *pool = solv->pool;
1107 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak -----\n");
1108 for (i = n = 1; n < pool->nsolvables; i++, n++)
1110 if (i == pool->nsolvables)
1114 s = pool->solvables + i;
1115 if (!pool_installable(pool, s))
1120 supp = s->repo->idarraydata + s->supplements;
1121 while ((sup = *supp++) != ID_NULL)
1122 if (dep_possible(solv, sup, m))
1125 if (!sup && s->freshens)
1127 supp = s->repo->idarraydata + s->freshens;
1128 while ((sup = *supp++) != ID_NULL)
1129 if (dep_possible(solv, sup, m))
1132 if (!sup && s->enhances)
1134 supp = s->repo->idarraydata + s->enhances;
1135 while ((sup = *supp++) != ID_NULL)
1136 if (dep_possible(solv, sup, m))
1141 addrpmrulesforsolvable(solv, s, m);
1144 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak end -----\n");
1148 addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allowall)
1150 Pool *pool = solv->pool;
1155 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1157 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1158 policy_findupdatepackages(solv, s, &qs, allowall);
1159 if (!MAPTST(m, s - pool->solvables)) /* add rule for s if not already done */
1160 addrpmrulesforsolvable(solv, s, m);
1161 for (i = 0; i < qs.count; i++)
1162 if (!MAPTST(m, qs.elements[i]))
1163 addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
1166 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1170 * add rule for update
1171 * (A|A1|A2|A3...) An = update candidates for A
1173 * s = (installed) solvable
1177 addupdaterule(Solver *solv, Solvable *s, int allowall)
1179 /* installed packages get a special upgrade allowed rule */
1180 Pool *pool = solv->pool;
1185 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule -----\n");
1187 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1188 policy_findupdatepackages(solv, s, &qs, allowall);
1189 if (qs.count == 0) /* no updaters found */
1192 d = pool_queuetowhatprovides(pool, &qs); /* intern computed queue */
1194 addrule(solv, s - pool->solvables, d); /* allow update of s */
1195 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule end -----\n");
1199 /*-----------------------------------------------------------------*/
1208 printWatches(Solver *solv, int type)
1210 Pool *pool = solv->pool;
1213 POOL_DEBUG(type, "Watches: \n");
1215 for (counter = -(pool->nsolvables); counter <= pool->nsolvables; counter ++)
1217 POOL_DEBUG(type, " solvable [%d] -- rule [%d]\n", counter, solv->watches[counter+pool->nsolvables]);
1224 * initial setup for all watches
1228 makewatches(Solver *solv)
1232 int nsolvables = solv->pool->nsolvables;
1234 xfree(solv->watches);
1235 /* lower half for removals, upper half for installs */
1236 solv->watches = (Id *)xcalloc(2 * nsolvables, sizeof(Id));
1238 /* do it reverse so rpm rules get triggered first */
1239 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1241 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1244 if (!r->w1 /* rule is disabled */
1245 || !r->w2) /* rule is assertion */
1248 /* see addwatches(solv, r) */
1249 r->n1 = solv->watches[nsolvables + r->w1];
1250 solv->watches[nsolvables + r->w1] = r - solv->rules;
1252 r->n2 = solv->watches[nsolvables + r->w2];
1253 solv->watches[nsolvables + r->w2] = r - solv->rules;
1259 * add watches (for rule)
1263 addwatches(Solver *solv, Rule *r)
1265 int nsolvables = solv->pool->nsolvables;
1267 r->n1 = solv->watches[nsolvables + r->w1];
1268 solv->watches[nsolvables + r->w1] = r - solv->rules;
1270 r->n2 = solv->watches[nsolvables + r->w2];
1271 solv->watches[nsolvables + r->w2] = r - solv->rules;
1275 /*-----------------------------------------------------------------*/
1276 /* rule propagation */
1278 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1283 * propagate decision to all rules
1287 propagate(Solver *solv, int level)
1289 Pool *pool = solv->pool;
1294 Id *decisionmap = solv->decisionmap;
1295 Id *watches = solv->watches + pool->nsolvables;
1297 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
1299 while (solv->propagate_index < solv->decisionq.count)
1301 /* negate because our watches trigger if literal goes FALSE */
1302 pkg = -solv->decisionq.elements[solv->propagate_index++];
1303 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1305 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "popagate for decision %d level %d\n", -pkg, level);
1306 printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
1307 printWatches(solv, SAT_DEBUG_SCHUBI);
1310 for (rp = watches + pkg; *rp; rp = nrp)
1312 r = solv->rules + *rp;
1314 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1316 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
1317 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1322 ow = r->w2; /* regard the second watchpoint to come to a solution */
1327 ow = r->w1; /* regard the first watchpoint to come to a solution */
1330 /* if clause is TRUE, nothing to do */
1331 if (DECISIONMAP_TRUE(ow))
1336 /* not a binary clause, check if we need to move our watch */
1337 /* search for a literal that is not ow and not false */
1338 /* (true is also ok, in that case the rule is fulfilled) */
1339 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1342 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1343 if (p != ow && !DECISIONMAP_TRUE(-p))
1347 /* p is free to watch, move watch to p */
1348 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1351 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables + p));
1353 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables - p));
1367 watches[p] = r - solv->rules;
1371 /* unit clause found, set other watch to TRUE */
1372 if (DECISIONMAP_TRUE(-ow))
1373 return r; /* eek, a conflict! */
1374 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1376 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " unit ");
1377 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1380 decisionmap[ow] = level;
1382 decisionmap[-ow] = -level;
1383 queue_push(&solv->decisionq, ow);
1384 queue_push(&solv->decisionq_why, r - solv->rules);
1385 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1387 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1389 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", solvable2str(pool, s));
1391 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", solvable2str(pool, s));
1395 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
1397 return 0; /* all is well */
1401 /*-----------------------------------------------------------------*/
1410 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *why)
1412 Pool *pool = solv->pool;
1415 Map seen; /* global? */
1419 int learnt_why = solv->learnt_pool.count;
1420 Id *decisionmap = solv->decisionmap;
1424 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
1425 map_init(&seen, pool->nsolvables);
1426 idx = solv->decisionq.count;
1429 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1430 printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1431 queue_push(&solv->learnt_pool, c - solv->rules);
1432 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1443 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1445 vv = v > 0 ? v : -v;
1446 if (MAPTST(&seen, vv))
1448 l = solv->decisionmap[vv];
1455 for (j = 0; j < solv->decisionq.count; j++)
1456 if (solv->decisionq.elements[j] == v)
1458 if (j == solv->decisionq.count)
1460 queue_push(&rulq, -(j + 1));
1462 continue; /* initial setting */
1466 num++; /* need to do this one as well */
1478 v = solv->decisionq.elements[--idx];
1479 vv = v > 0 ? v : -v;
1480 if (MAPTST(&seen, vv))
1483 c = solv->rules + solv->decisionq_why.elements[idx];
1491 else if (r.count == 1 && r.elements[0] < 0)
1492 *dr = r.elements[0];
1494 *dr = pool_queuetowhatprovides(pool, &r);
1495 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1497 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
1498 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, -v);
1499 for (i = 0; i < r.count; i++)
1500 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
1503 /* push end marker on learnt reasons stack */
1504 queue_push(&solv->learnt_pool, 0);
1505 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1507 for (i = learnt_why; solv->learnt_pool.elements[i]; i++)
1509 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learnt_why ");
1510 printrule(solv, SAT_DEBUG_ANALYZE, solv->rules + solv->learnt_pool.elements[i]);
1521 * reset the solver decisions to right after the rpm rules.
1522 * called after rules have been enabled/disabled
1526 reset_solver(Solver *solv)
1528 Pool *pool = solv->pool;
1532 enabledisablelearntrules(solv);
1534 /* redo all direct rpm rule decisions */
1535 /* we break at the first decision with a why attached, this is
1536 * either a job/system rule assertion or a propagated decision */
1537 for (i = 0; i < solv->decisionq.count; i++)
1539 v = solv->decisionq.elements[i];
1540 solv->decisionmap[v > 0 ? v : -v] = 0;
1542 for (i = 0; i < solv->decisionq_why.count; i++)
1543 if (solv->decisionq_why.elements[i])
1547 v = solv->decisionq.elements[i];
1548 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1551 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions done reduced from %d to %d\n", solv->decisionq.count, i);
1553 solv->decisionq_why.count = i;
1554 solv->decisionq.count = i;
1555 solv->recommends_index = -1;
1556 solv->propagate_index = 0;
1558 /* redo all job/system decisions */
1559 makeruledecisions(solv);
1560 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
1561 /* recreate watch chains */
1567 * analyze_unsolvable_rule
1571 analyze_unsolvable_rule(Solver *solv, Rule *r)
1573 Pool *pool = solv->pool;
1575 Id why = r - solv->rules;
1576 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1577 printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1578 if (solv->learntrules && why >= solv->learntrules)
1580 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1581 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i]);
1584 /* do not add rpm rules to problem */
1585 if (why < solv->jobrules)
1587 /* turn rule into problem */
1588 if (why >= solv->jobrules && why < solv->systemrules)
1589 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1590 /* return if problem already countains our rule */
1591 if (solv->problems.count)
1593 for (i = solv->problems.count - 1; i >= 0; i--)
1594 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
1596 else if (solv->problems.elements[i] == why)
1599 queue_push(&solv->problems, why);
1604 * analyze_unsolvable
1606 * return: 1 - disabled some rules, try again
1611 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1613 Pool *pool = solv->pool;
1615 Map seen; /* global to speed things up? */
1618 Id *decisionmap = solv->decisionmap;
1619 int oldproblemcount;
1620 int oldlearntpoolcount;
1623 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
1624 oldproblemcount = solv->problems.count;
1625 oldlearntpoolcount = solv->learnt_pool.count;
1627 /* make room for proof index */
1628 /* must update it later, as analyze_unsolvable_rule would confuse
1629 * it with a rule index if we put the real value in already */
1630 queue_push(&solv->problems, 0);
1633 map_init(&seen, pool->nsolvables);
1634 queue_push(&solv->learnt_pool, r - solv->rules);
1635 analyze_unsolvable_rule(solv, r);
1636 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1647 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1649 vv = v > 0 ? v : -v;
1650 l = solv->decisionmap[vv];
1655 idx = solv->decisionq.count;
1658 v = solv->decisionq.elements[--idx];
1659 vv = v > 0 ? v : -v;
1660 if (!MAPTST(&seen, vv))
1662 why = solv->decisionq_why.elements[idx];
1665 /* level 1 and no why, must be an rpm assertion */
1666 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1668 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "RPM ");
1669 printruleelement(solv, SAT_DEBUG_UNSOLVABLE, 0, v);
1671 /* this is the only positive rpm assertion */
1672 if (v == SYSTEMSOLVABLE)
1676 queue_push(&solv->learnt_pool, v);
1679 r = solv->rules + why;
1680 queue_push(&solv->learnt_pool, why);
1681 analyze_unsolvable_rule(solv, r);
1682 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1693 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1695 vv = v > 0 ? v : -v;
1696 l = solv->decisionmap[vv];
1703 queue_push(&solv->problems, 0); /* mark end of this problem */
1706 if (solv->weakrules != solv->learntrules)
1708 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1710 why = solv->problems.elements[i];
1711 if (why < solv->weakrules || why >= solv->learntrules)
1713 if (!lastweak || lastweak < why)
1719 /* disable last weak rule */
1720 solv->problems.count = oldproblemcount;
1721 solv->learnt_pool.count = oldlearntpoolcount;
1722 r = solv->rules + lastweak;
1723 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling weak ");
1724 printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
1725 disablerule(solv, r);
1731 queue_push(&solv->learnt_pool, 0);
1732 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1736 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1737 disableproblem(solv, solv->problems.elements[i]);
1741 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1746 /*-----------------------------------------------------------------*/
1747 /* Decision revert */
1751 * revert decision at level
1755 revert(Solver *solv, int level)
1757 Pool *pool = solv->pool;
1759 while (solv->decisionq.count)
1761 v = solv->decisionq.elements[solv->decisionq.count - 1];
1762 vv = v > 0 ? v : -v;
1763 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1765 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1766 solv->decisionmap[vv] = 0;
1767 solv->decisionq.count--;
1768 solv->decisionq_why.count--;
1769 solv->propagate_index = solv->decisionq.count;
1771 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1773 solv->branches.count--;
1774 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1775 solv->branches.count--;
1777 solv->recommends_index = -1;
1782 * watch2onhighest - put watch2 on literal with highest level
1786 watch2onhighest(Solver *solv, Rule *r)
1792 return; /* binary rule, both watches are set */
1793 dp = solv->pool->whatprovidesdata + r->d;
1794 while ((v = *dp++) != 0)
1796 l = solv->decisionmap[v < 0 ? -v : v];
1811 * add free decision to decision q, increase level
1812 * propagate decision, return if no conflict.
1813 * in conflict case, analyze conflict rule, add resulting
1814 * rule to learnt rule set, make decision from learnt
1815 * rule (always unit) and re-propagate.
1819 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
1821 Pool *pool = solv->pool;
1830 solv->decisionmap[decision] = level;
1832 solv->decisionmap[-decision] = -level;
1833 queue_push(&solv->decisionq, decision);
1834 queue_push(&solv->decisionq_why, 0);
1838 r = propagate(solv, level);
1842 return analyze_unsolvable(solv, r, disablerules);
1843 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1844 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
1845 if (l >= level || l <= 0)
1847 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1849 revert(solv, level);
1850 r = addrule(solv, p, d); /* p requires d */
1853 if (solv->learnt_why.count != (r - solv->rules) - solv->learntrules)
1855 queue_push(&solv->learnt_why, why);
1858 /* at least 2 literals, needs watches */
1859 watch2onhighest(solv, r);
1860 addwatches(solv, r);
1862 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1863 queue_push(&solv->decisionq, p);
1864 queue_push(&solv->decisionq_why, r - solv->rules);
1865 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1867 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
1868 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
1869 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
1870 printrule(solv, SAT_DEBUG_ANALYZE, r);
1878 * install best package from the queue. We add an extra package, inst, if
1879 * provided. See comment in weak install section.
1882 selectandinstall(Solver *solv, int level, Queue *dq, Id inst, int disablerules)
1884 Pool *pool = solv->pool;
1888 if (dq->count > 1 || inst)
1889 policy_filter_unwanted(solv, dq, inst, POLICY_MODE_CHOOSE);
1894 /* choose the supplemented one */
1895 for (i = 0; i < dq->count; i++)
1896 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1900 for (i = 1; i < dq->count; i++)
1901 queue_push(&solv->branches, dq->elements[i]);
1902 queue_push(&solv->branches, -level);
1906 p = dq->elements[i];
1908 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvable2str(pool, pool->solvables + p));
1910 return setpropagatelearn(solv, level, p, disablerules);
1914 /*-----------------------------------------------------------------*/
1915 /* Main solver interface */
1920 * create solver structure
1922 * pool: all available solvables
1923 * installed: installed Solvables
1926 * Upon solving, rules are created to flag the Solvables
1927 * of the 'installed' Repo as installed.
1931 solver_create(Pool *pool, Repo *installed)
1934 solv = (Solver *)xcalloc(1, sizeof(Solver));
1936 solv->installed = installed;
1938 queue_init(&solv->ruletojob);
1939 queue_init(&solv->decisionq);
1940 queue_init(&solv->decisionq_why);
1941 queue_init(&solv->problems);
1942 queue_init(&solv->suggestions);
1943 queue_init(&solv->learnt_why);
1944 queue_init(&solv->learnt_pool);
1945 queue_init(&solv->branches);
1947 map_init(&solv->recommendsmap, pool->nsolvables);
1948 map_init(&solv->suggestsmap, pool->nsolvables);
1949 map_init(&solv->noupdate, installed ? installed->end - installed->start : 0);
1950 solv->recommends_index = 0;
1952 solv->decisionmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
1953 solv->rules = (Rule *)xmalloc((solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
1954 memset(solv->rules, 0, sizeof(Rule));
1966 solver_free(Solver *solv)
1968 queue_free(&solv->ruletojob);
1969 queue_free(&solv->decisionq);
1970 queue_free(&solv->decisionq_why);
1971 queue_free(&solv->learnt_why);
1972 queue_free(&solv->learnt_pool);
1973 queue_free(&solv->problems);
1974 queue_free(&solv->suggestions);
1975 queue_free(&solv->branches);
1977 map_free(&solv->recommendsmap);
1978 map_free(&solv->suggestsmap);
1979 map_free(&solv->noupdate);
1980 xfree(solv->decisionmap);
1982 xfree(solv->watches);
1983 xfree(solv->weaksystemrules);
1984 xfree(solv->obsoletes);
1985 xfree(solv->obsoletes_data);
1990 /*-------------------------------------------------------*/
1995 * all rules have been set up, now actually run the solver
2000 run_solver(Solver *solv, int disablerules, int doweak)
2008 Pool *pool = solv->pool;
2011 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
2013 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
2014 for (i = 0; i < solv->nrules; i++)
2015 printrule(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
2018 /* create watches chains */
2021 POOL_DEBUG(SAT_DEBUG_STATS, "initial decisions: %d\n", solv->decisionq.count);
2023 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2024 printdecisions(solv);
2026 /* start SAT algorithm */
2028 systemlevel = level + 1;
2029 POOL_DEBUG(SAT_DEBUG_STATS, "solving...\n");
2040 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
2041 if ((r = propagate(solv, level)) != 0)
2043 if (analyze_unsolvable(solv, r, disablerules))
2051 * installed packages
2054 if (level < systemlevel && solv->installed && solv->installed->nsolvables)
2056 if (!solv->updatesystem)
2058 /* try to keep as many packages as possible */
2059 POOL_DEBUG(SAT_DEBUG_STATS, "installing system packages\n");
2060 for (i = solv->installed->start, n = 0; ; i++)
2062 if (n == solv->installed->nsolvables)
2064 if (i == solv->installed->end)
2065 i = solv->installed->start;
2066 s = pool->solvables + i;
2067 if (s->repo != solv->installed)
2070 if (solv->decisionmap[i] != 0)
2072 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "keeping %s\n", solvable2str(pool, s));
2074 level = setpropagatelearn(solv, level, i, disablerules);
2080 if (level <= olevel)
2084 if (solv->weaksystemrules)
2086 POOL_DEBUG(SAT_DEBUG_STATS, "installing weak system packages\n");
2087 for (i = solv->installed->start; i < solv->installed->end; i++)
2089 if (pool->solvables[i].repo != solv->installed)
2091 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->installed->start] == 0))
2093 /* noupdate is set if a job is erasing the installed solvable or installing a specific version */
2094 if (MAPTST(&solv->noupdate, i - solv->installed->start))
2097 if (solv->weaksystemrules[i - solv->installed->start])
2099 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->installed->start];
2100 while ((p = *dp++) != 0)
2102 if (solv->decisionmap[p] > 0)
2104 if (solv->decisionmap[p] == 0)
2108 continue; /* update package already installed */
2110 if (!dq.count && solv->decisionmap[i] != 0)
2113 /* FIXME: i is handled a bit different because we do not want
2114 * to have it pruned just because it is not recommened.
2115 * we should not prune installed packages instead */
2116 level = selectandinstall(solv, level, &dq, (solv->decisionmap[i] ? 0 : i), disablerules);
2122 if (level <= olevel)
2125 if (i < solv->installed->end)
2128 systemlevel = level;
2135 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unresolved rules\n");
2136 for (i = 1, n = 1; ; i++, n++)
2138 if (n == solv->nrules)
2140 if (i == solv->nrules)
2142 r = solv->rules + i;
2148 /* binary or unary rule */
2149 /* need two positive undecided literals */
2150 if (r->p < 0 || r->w2 <= 0)
2152 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2154 queue_push(&dq, r->p);
2155 queue_push(&dq, r->w2);
2160 * all negative literals are installed
2161 * no positive literal is installed
2162 * i.e. the rule is not fulfilled and we
2163 * just need to decide on the positive literals
2167 if (solv->decisionmap[-r->p] <= 0)
2172 if (solv->decisionmap[r->p] > 0)
2174 if (solv->decisionmap[r->p] == 0)
2175 queue_push(&dq, r->p);
2177 dp = pool->whatprovidesdata + r->d;
2178 while ((p = *dp++) != 0)
2182 if (solv->decisionmap[-p] <= 0)
2187 if (solv->decisionmap[p] > 0)
2189 if (solv->decisionmap[p] == 0)
2198 /* cannot happen as this means that
2199 * the rule is unit */
2200 printrule(solv, SAT_FATAL, r);
2203 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
2205 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
2206 printrule(solv, SAT_DEBUG_PROPAGATE, r);
2210 level = selectandinstall(solv, level, &dq, 0, disablerules);
2216 if (level < systemlevel)
2219 } /* for(), decide */
2221 if (n != solv->nrules) /* continue if level < systemlevel */
2224 if (doweak && !solv->problems.count)
2228 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended packages\n");
2229 if (!REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2231 for (i = 0; i < solv->decisionq.count; i++)
2233 p = solv->decisionq.elements[i];
2234 if (p > 0 && pool->solvables[p].repo == solv->installed)
2235 solv->decisionmap[p] = -solv->decisionmap[p];
2239 for (i = 1; i < pool->nsolvables; i++)
2241 if (solv->decisionmap[i] < 0)
2243 if (solv->decisionmap[i] > 0)
2245 Id *recp, rec, *pp, p;
2246 s = pool->solvables + i;
2247 /* installed, check for recommends */
2248 /* XXX need to special case AND ? */
2251 recp = s->repo->idarraydata + s->recommends;
2252 while ((rec = *recp++) != 0)
2255 FOR_PROVIDES(p, pp, rec)
2257 if (solv->decisionmap[p] > 0)
2262 else if (solv->decisionmap[p] == 0)
2264 queue_pushunique(&dq, p);
2272 s = pool->solvables + i;
2273 if (!s->supplements && !s->freshens)
2275 if (!pool_installable(pool, s))
2277 if (solver_is_supplementing(solv, s))
2278 queue_pushunique(&dq, i);
2281 if (!REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2283 for (i = 0; i < solv->decisionq.count; i++)
2285 p = solv->decisionq.elements[i];
2286 if (p > 0 && pool->solvables[p].repo == solv->installed)
2287 solv->decisionmap[p] = -solv->decisionmap[p];
2293 policy_filter_unwanted(solv, &dq, 0, POLICY_MODE_RECOMMEND);
2295 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended %s\n", solvable2str(pool, pool->solvables + p));
2296 level = setpropagatelearn(solv, level, p, 0);
2301 if (solv->solution_callback)
2303 solv->solution_callback(solv, solv->solution_callback_data);
2304 if (solv->branches.count)
2306 int i = solv->branches.count - 1;
2307 int l = -solv->branches.elements[i];
2309 if (solv->branches.elements[i - 1] < 0)
2311 p = solv->branches.elements[i];
2312 POOL_DEBUG(SAT_DEBUG_STATS, "branching with %s\n", solvable2str(pool, pool->solvables + p));
2314 for (j = i + 1; j < solv->branches.count; j++)
2315 queue_push(&dq, solv->branches.elements[j]);
2316 solv->branches.count = i;
2318 revert(solv, level);
2320 for (j = 0; j < dq.count; j++)
2321 queue_push(&solv->branches, dq.elements[j]);
2323 level = setpropagatelearn(solv, level, p, disablerules);
2331 /* all branches done, we're finally finished */
2335 /* minimization step */
2336 if (solv->branches.count)
2338 int l = 0, lasti = -1, lastl = -1;
2340 for (i = solv->branches.count - 1; i >= 0; i--)
2342 p = solv->branches.elements[i];
2345 else if (p > 0 && solv->decisionmap[p] > l + 1)
2353 /* kill old solvable so that we do not loop */
2354 p = solv->branches.elements[lasti];
2355 solv->branches.elements[lasti] = 0;
2356 POOL_DEBUG(SAT_DEBUG_STATS, "minimizing %d -> %d with %s\n", solv->decisionmap[p], l, solvable2str(pool, pool->solvables + p));
2359 revert(solv, level);
2361 level = setpropagatelearn(solv, level, p, disablerules);
2378 * at this point, all rules that led to conflicts are disabled.
2379 * we re-enable all rules of a problem set but rule "sug", then
2380 * continue to disable more rules until there as again a solution.
2383 /* FIXME: think about conflicting assertions */
2386 refine_suggestion(Solver *solv, Queue *job, Id *problem, Id sug, Queue *refined)
2388 Pool *pool = solv->pool;
2395 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2397 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion start\n");
2398 for (i = 0; problem[i]; i++)
2400 if (problem[i] == sug)
2401 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "=> ");
2402 printproblem(solv, problem[i]);
2405 queue_init(&disabled);
2406 queue_empty(refined);
2407 queue_push(refined, sug);
2409 /* re-enable all problem rules with the exception of "sug" */
2413 for (i = 0; problem[i]; i++)
2414 if (problem[i] != sug)
2415 enableproblem(solv, problem[i]);
2418 disableupdaterules(solv, job, -(sug + 1));
2422 /* re-enable as many weak rules as possible */
2423 for (i = solv->weakrules; i < solv->learntrules; i++)
2425 r = solv->rules + i;
2427 enablerule(solv, r);
2430 queue_empty(&solv->problems);
2431 revert(solv, 1); /* XXX move to reset_solver? */
2434 run_solver(solv, 0, 0);
2435 if (!solv->problems.count)
2437 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no more problems!\n");
2438 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2439 printdecisions(solv);
2440 break; /* great, no more problems */
2442 disabledcnt = disabled.count;
2443 /* skip over proof index */
2444 for (i = 1; i < solv->problems.count - 1; i++)
2446 /* ignore solutions in refined */
2447 v = solv->problems.elements[i];
2449 break; /* end of problem reached */
2450 for (j = 0; problem[j]; j++)
2451 if (problem[j] != sug && problem[j] == v)
2455 queue_push(&disabled, v);
2457 if (disabled.count == disabledcnt)
2459 /* no solution found, this was an invalid suggestion! */
2460 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no solution found!\n");
2464 if (disabled.count == disabledcnt + 1)
2466 /* just one suggestion, add it to refined list */
2467 v = disabled.elements[disabledcnt];
2468 queue_push(refined, v);
2469 disableproblem(solv, v);
2471 disableupdaterules(solv, job, -(v + 1));
2475 /* more than one solution, disable all */
2476 /* do not push anything on refine list */
2477 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2479 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "more than one solution found:\n");
2480 for (i = disabledcnt; i < disabled.count; i++)
2481 printproblem(solv, disabled.elements[i]);
2483 for (i = disabledcnt; i < disabled.count; i++)
2484 disableproblem(solv, disabled.elements[i]);
2487 /* all done, get us back into the same state as before */
2488 /* enable refined rules again */
2489 for (i = 0; i < disabled.count; i++)
2490 enableproblem(solv, disabled.elements[i]);
2491 /* disable problem rules again */
2492 for (i = 0; problem[i]; i++)
2493 disableproblem(solv, problem[i]);
2494 disableupdaterules(solv, job, -1);
2495 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion end\n");
2499 problems_to_solutions(Solver *solv, Queue *job)
2501 Pool *pool = solv->pool;
2509 if (!solv->problems.count)
2511 queue_clone(&problems, &solv->problems);
2512 queue_init(&solution);
2513 queue_init(&solutions);
2514 /* copy over proof index */
2515 queue_push(&solutions, problems.elements[0]);
2516 problem = problems.elements + 1;
2517 for (i = 1; i < problems.count; i++)
2519 Id v = problems.elements[i];
2522 /* mark end of this problem */
2523 queue_push(&solutions, 0);
2524 queue_push(&solutions, 0);
2525 if (i + 1 == problems.count)
2527 /* copy over proof of next problem */
2528 queue_push(&solutions, problems.elements[i + 1]);
2530 problem = problems.elements + i + 1;
2533 refine_suggestion(solv, job, problem, v, &solution);
2534 if (!solution.count)
2535 continue; /* this solution didn't work out */
2537 for (j = 0; j < solution.count; j++)
2539 why = solution.elements[j];
2541 printproblem(solv, why);
2545 queue_push(&solutions, 0);
2546 queue_push(&solutions, -why);
2548 else if (why >= solv->systemrules && why < solv->weakrules)
2551 p = solv->installed->start + (why - solv->systemrules);
2552 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
2554 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
2557 if (*dp >= solv->installed->start && *dp < solv->installed->start + solv->installed->nsolvables)
2559 if (solv->decisionmap[*dp] > 0)
2566 queue_push(&solutions, p);
2567 queue_push(&solutions, rp);
2572 /* mark end of this solution */
2573 queue_push(&solutions, 0);
2574 queue_push(&solutions, 0);
2576 queue_free(&solution);
2577 queue_free(&problems);
2578 /* copy queue over to solutions */
2579 queue_free(&solv->problems);
2580 queue_clone(&solv->problems, &solutions);
2582 /* bring solver back into problem state */
2583 revert(solv, 1); /* XXX move to reset_solver? */
2586 if (solv->problems.count != solutions.count)
2588 queue_free(&solutions);
2592 solver_next_problem(Solver *solv, Id problem)
2596 return solv->problems.count ? 1 : 0;
2597 pp = solv->problems.elements + problem;
2598 while (pp[0] || pp[1])
2602 while (pp[0] || pp[1])
2607 problem = pp - solv->problems.elements;
2608 if (problem >= solv->problems.count)
2614 solver_next_solution(Solver *solv, Id problem, Id solution)
2620 pp = solv->problems.elements + solution;
2621 return pp[0] || pp[1] ? solution : 0;
2623 pp = solv->problems.elements + solution;
2624 while (pp[0] || pp[1])
2627 solution = pp - solv->problems.elements;
2628 return pp[0] || pp[1] ? solution : 0;
2632 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
2635 element = element ? element + 2 : solution;
2636 pp = solv->problems.elements + element;
2637 if (!(pp[0] || pp[1]))
2646 * create obsoletesmap from solver decisions
2647 * required for decision handling
2651 create_obsoletesmap(Solver *solv)
2653 Pool *pool = solv->pool;
2654 Repo *installed = solv->installed;
2655 Id p, *obsoletesmap = NULL;
2659 obsoletesmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
2662 for (i = 0; i < solv->decisionq.count; i++)
2666 n = solv->decisionq.elements[i];
2669 if (n == SYSTEMSOLVABLE)
2671 s = pool->solvables + n;
2672 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2674 FOR_PROVIDES(p, pp, s->name)
2675 if (s->name == pool->solvables[p].name)
2677 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2679 obsoletesmap[p] = n;
2684 for (i = 0; i < solv->decisionq.count; i++)
2689 n = solv->decisionq.elements[i];
2692 if (n == SYSTEMSOLVABLE)
2694 s = pool->solvables + n;
2695 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2699 obsp = s->repo->idarraydata + s->obsoletes;
2700 while ((obs = *obsp++) != 0)
2701 FOR_PROVIDES(p, pp, obs)
2703 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2705 obsoletesmap[p] = n;
2711 return obsoletesmap;
2720 printdecisions(Solver *solv)
2722 Pool *pool = solv->pool;
2723 Repo *installed = solv->installed;
2724 Id p, *obsoletesmap = create_obsoletesmap( solv );
2728 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions -----\n");
2730 /* print solvables to be erased */
2734 FOR_REPO_SOLVABLES(installed, p, s)
2736 if (solv->decisionmap[p] >= 0)
2738 if (obsoletesmap[p])
2740 POOL_DEBUG(SAT_DEBUG_RESULT, "erase %s\n", solvable2str(pool, s));
2744 /* print solvables to be installed */
2746 for (i = 0; i < solv->decisionq.count; i++)
2749 p = solv->decisionq.elements[i];
2752 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2755 s = pool->solvables + p;
2756 POOL_DEBUG(SAT_DEBUG_SCHUBI, "level of %s is %d\n", solvable2str(pool, s), p);
2760 if (p == SYSTEMSOLVABLE)
2762 POOL_DEBUG(SAT_DEBUG_SCHUBI, "SYSTEMSOLVABLE\n");
2765 s = pool->solvables + p;
2766 if (installed && s->repo == installed)
2769 if (!obsoletesmap[p])
2771 POOL_DEBUG(SAT_DEBUG_RESULT, "install %s", solvable2str(pool, s));
2775 POOL_DEBUG(SAT_DEBUG_RESULT, "update %s", solvable2str(pool, s));
2776 POOL_DEBUG(SAT_DEBUG_RESULT, " (obsoletes");
2777 for (j = installed->start; j < installed->end; j++)
2778 if (obsoletesmap[j] == p)
2779 POOL_DEBUG(SAT_DEBUG_RESULT, " %s", solvable2str(pool, pool->solvables + j));
2780 POOL_DEBUG(SAT_DEBUG_RESULT, ")");
2782 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
2785 xfree(obsoletesmap);
2787 if (solv->suggestions.count)
2789 POOL_DEBUG(SAT_DEBUG_RESULT, "\nsuggested packages:\n");
2790 for (i = 0; i < solv->suggestions.count; i++)
2792 s = pool->solvables + solv->suggestions.elements[i];
2793 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s\n", solvable2str(pool, s));
2796 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions end -----\n");
2799 /* this is basically the reverse of addrpmrulesforsolvable */
2801 solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp)
2803 Pool *pool = solv->pool;
2804 Repo *installed = solv->installed;
2808 Id p, *pp, req, *reqp, con, *conp, obs, *obsp, *dp;
2810 if (rid >= solv->weakrules)
2812 if (rid >= solv->systemrules)
2815 *sourcep = solv->installed->start + (rid - solv->systemrules);
2817 return SOLVER_PROBLEM_UPDATE_RULE;
2819 if (rid >= solv->jobrules)
2822 r = solv->rules + rid;
2823 p = solv->ruletojob.elements[rid - solv->jobrules];
2824 *depp = job->elements[p + 1];
2826 *targetp = job->elements[p];
2827 if (r->d == 0 && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
2828 return SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP;
2829 return SOLVER_PROBLEM_JOB_RULE;
2833 /* a rpm rule assertion */
2834 if (rid == -SYSTEMSOLVABLE)
2835 abort(); /* can happen only for job rules */
2836 s = pool->solvables - rid;
2837 if (installed && !solv->fixsystem && s->repo == installed)
2839 if (dontfix) /* dontfix packages never have a neg assertion */
2841 /* see why the package is not installable */
2842 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
2843 return SOLVER_PROBLEM_NOT_INSTALLABLE;
2844 /* check requires */
2847 reqp = s->repo->idarraydata + s->requires;
2848 while ((req = *reqp++) != 0)
2850 if (req == SOLVABLE_PREREQMARKER)
2852 dp = pool_whatprovides(pool, req);
2858 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
2863 r = solv->rules + rid;
2865 abort(); /* not a rpm rule */
2866 if (r->d == 0 && r->w2 == 0)
2868 /* an assertion. we don't store them as rpm rules, so
2872 s = pool->solvables - r->p;
2873 if (installed && !solv->fixsystem && s->repo == installed)
2875 if (r->d == 0 && r->w2 < 0)
2877 /* a package conflict */
2878 Solvable *s2 = pool->solvables - r->w2;
2881 if (installed && !solv->fixsystem && s2->repo == installed)
2884 /* if both packages have the same name and at least one of them
2885 * is not installed, they conflict */
2886 if (s->name == s2->name && (!installed || (s->repo != installed || s2->repo != installed)))
2891 return SOLVER_PROBLEM_SAME_NAME;
2894 /* check conflicts in both directions */
2897 conp = s->repo->idarraydata + s->conflicts;
2898 while ((con = *conp++) != 0)
2900 FOR_PROVIDES(p, pp, con)
2902 if (dontfix && pool->solvables[p].repo == installed)
2909 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
2915 conp = s2->repo->idarraydata + s2->conflicts;
2916 while ((con = *conp++) != 0)
2918 FOR_PROVIDES(p, pp, con)
2920 if (dontfix2 && pool->solvables[p].repo == installed)
2927 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
2931 /* check obsoletes in both directions */
2932 if ((!installed || s->repo != installed) && s->obsoletes)
2934 obsp = s->repo->idarraydata + s->obsoletes;
2935 while ((obs = *obsp++) != 0)
2937 FOR_PROVIDES(p, pp, obs)
2944 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
2948 if ((!installed || s2->repo != installed) && s2->obsoletes)
2950 obsp = s2->repo->idarraydata + s2->obsoletes;
2951 while ((obs = *obsp++) != 0)
2953 FOR_PROVIDES(p, pp, obs)
2960 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
2964 /* all cases checked, can't happen */
2967 /* simple requires */
2970 reqp = s->repo->idarraydata + s->requires;
2971 while ((req = *reqp++) != 0)
2973 if (req == SOLVABLE_PREREQMARKER)
2975 dp = pool_whatprovides(pool, req);
2978 if (*dp == r->w2 && dp[1] == 0)
2981 else if (dp - pool->whatprovidesdata == r->d)
2989 return SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE;
2993 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp)
2996 Id lreqr, lconr, lsysr, ljobr;
3000 lreqr = lconr = lsysr = ljobr = 0;
3001 while ((rid = solv->learnt_pool.elements[idx++]) != 0)
3003 if (rid >= solv->learntrules)
3004 findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr);
3005 else if (rid >= solv->systemrules)
3010 else if (rid >= solv->jobrules)
3017 r = solv->rules + rid;
3018 if (!r->d && r->w2 < 0)
3031 /* assertion, counts as require rule */
3032 /* ignore system solvable as we need useful info */
3033 if (rid == -SYSTEMSOLVABLE)
3035 if (!*reqrp || !reqassert)
3042 if (!*reqrp && lreqr)
3044 if (!*conrp && lconr)
3046 if (!*jobrp && ljobr)
3048 if (!*sysrp && lsysr)
3053 solver_findproblemrule(Solver *solv, Id problem)
3055 Id reqr, conr, sysr, jobr;
3056 Id idx = solv->problems.elements[problem - 1];
3057 reqr = conr = sysr = jobr = 0;
3058 findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr);
3071 printprobleminfo(Solver *solv, Queue *job, Id problem)
3073 Pool *pool = solv->pool;
3075 Id dep, source, target;
3078 probr = solver_findproblemrule(solv, problem);
3079 switch (solver_problemruleinfo(solv, job, probr, &dep, &source, &target))
3081 case SOLVER_PROBLEM_UPDATE_RULE:
3082 s = pool_id2solvable(pool, source);
3083 POOL_DEBUG(SAT_DEBUG_RESULT, "problem with installed package %s\n", solvable2str(pool, s));
3085 case SOLVER_PROBLEM_JOB_RULE:
3086 POOL_DEBUG(SAT_DEBUG_RESULT, "conflicting requests\n");
3088 case SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP:
3089 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides requested %s\n", dep2str(pool, dep));
3091 case SOLVER_PROBLEM_NOT_INSTALLABLE:
3092 s = pool_id2solvable(pool, source);
3093 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s is not installable\n", solvable2str(pool, s));
3095 case SOLVER_PROBLEM_NOTHING_PROVIDES_DEP:
3096 s = pool_id2solvable(pool, source);
3097 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides %s needed by %s\n", dep2str(pool, dep), solvable2str(pool, s));
3099 case SOLVER_PROBLEM_SAME_NAME:
3100 s = pool_id2solvable(pool, source);
3101 s2 = pool_id2solvable(pool, target);
3102 POOL_DEBUG(SAT_DEBUG_RESULT, "cannot install both %s and %s\n", solvable2str(pool, s), solvable2str(pool, s2));
3104 case SOLVER_PROBLEM_PACKAGE_CONFLICT:
3105 s = pool_id2solvable(pool, source);
3106 s2 = pool_id2solvable(pool, target);
3107 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s conflicts with %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3109 case SOLVER_PROBLEM_PACKAGE_OBSOLETES:
3110 s = pool_id2solvable(pool, source);
3111 s2 = pool_id2solvable(pool, target);
3112 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s obsoletes %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3114 case SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE:
3115 s = pool_id2solvable(pool, source);
3116 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s requires %s, but none of the providers can be installed\n", solvable2str(pool, s), dep2str(pool, dep));
3122 printsolutions(Solver *solv, Queue *job)
3124 Pool *pool = solv->pool;
3127 Id problem, solution, element;
3130 POOL_DEBUG(SAT_DEBUG_RESULT, "Encountered problems! Here are the solutions:\n\n");
3133 while ((problem = solver_next_problem(solv, problem)) != 0)
3135 POOL_DEBUG(SAT_DEBUG_RESULT, "Problem %d:\n", pcnt++);
3136 POOL_DEBUG(SAT_DEBUG_RESULT, "====================================\n");
3137 printprobleminfo(solv, job, problem);
3138 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3140 while ((solution = solver_next_solution(solv, problem, solution)) != 0)
3143 while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
3147 /* job, rp is index into job queue */
3148 what = job->elements[rp];
3149 switch (job->elements[rp - 1])
3151 case SOLVER_INSTALL_SOLVABLE:
3152 s = pool->solvables + what;
3153 if (solv->installed && s->repo == solv->installed)
3154 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not keep %s installed\n", solvable2str(pool, s));
3156 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", solvable2str(pool, s));
3158 case SOLVER_ERASE_SOLVABLE:
3159 s = pool->solvables + what;
3160 if (solv->installed && s->repo == solv->installed)
3161 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", solvable2str(pool, s));
3163 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not forbid installation of %s\n", solvable2str(pool, s));
3165 case SOLVER_INSTALL_SOLVABLE_NAME:
3166 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", id2str(pool, what));
3168 case SOLVER_ERASE_SOLVABLE_NAME:
3169 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", id2str(pool, what));
3171 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3172 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install a solvable providing %s\n", dep2str(pool, what));
3174 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3175 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall all solvables providing %s\n", dep2str(pool, what));
3177 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3178 s = pool->solvables + what;
3179 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install most recent version of %s\n", solvable2str(pool, s));
3182 POOL_DEBUG(SAT_DEBUG_RESULT, "- do something different\n");
3188 /* policy, replace p with rp */
3189 s = pool->solvables + p;
3190 sd = rp ? pool->solvables + rp : 0;
3194 if (!solv->allowdowngrade && evrcmp(pool, s->evr, sd->evr, EVRCMP_MATCH_RELEASE) > 0)
3196 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow downgrade of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3199 if (!solv->allowarchchange && s->name == sd->name && s->arch != sd->arch && policy_illegal_archchange(pool, s, sd))
3201 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow architecture change of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3204 if (!solv->allowvendorchange && s->name == sd->name && s->vendor != sd->vendor && policy_illegal_vendorchange(pool, s, sd))
3207 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow vendor change from '%s' (%s) to '%s' (%s)\n", id2str(pool, s->vendor), solvable2str(pool, s), id2str(pool, sd->vendor), solvable2str(pool, sd));
3209 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow vendor change from '%s' (%s) to no vendor (%s)\n", id2str(pool, s->vendor), solvable2str(pool, s), solvable2str(pool, sd));
3213 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow replacement of %s with %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3217 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow deinstallation of %s\n", solvable2str(pool, s));
3222 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3228 /* for each installed solvable find which packages with *different* names
3229 * obsolete the solvable.
3230 * this index is used in policy_findupdatepackages if noupdateprovide is set.
3234 create_obsolete_index(Solver *solv)
3236 Pool *pool = solv->pool;
3238 Repo *installed = solv->installed;
3239 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
3242 if (!installed || !installed->nsolvables)
3244 /* create reverse obsoletes map for installed solvables */
3245 solv->obsoletes = obsoletes = xcalloc(installed->end - installed->start, sizeof(Id));
3246 for (i = 1; i < pool->nsolvables; i++)
3248 s = pool->solvables + i;
3249 if (s->repo == installed)
3253 if (!pool_installable(pool, s))
3255 obsp = s->repo->idarraydata + s->obsoletes;
3256 while ((obs = *obsp++) != 0)
3257 FOR_PROVIDES(p, pp, obs)
3259 if (pool->solvables[p].repo != installed)
3261 if (pool->solvables[p].name == s->name)
3263 obsoletes[p - installed->start]++;
3267 for (i = 0; i < installed->nsolvables; i++)
3270 n += obsoletes[i] + 1;
3273 solv->obsoletes_data = obsoletes_data = xcalloc(n + 1, sizeof(Id));
3274 POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
3275 for (i = pool->nsolvables - 1; i > 0; i--)
3277 s = pool->solvables + i;
3278 if (s->repo == installed)
3282 if (!pool_installable(pool, s))
3284 obsp = s->repo->idarraydata + s->obsoletes;
3285 while ((obs = *obsp++) != 0)
3286 FOR_PROVIDES(p, pp, obs)
3288 if (pool->solvables[p].repo != installed)
3290 if (pool->solvables[p].name == s->name)
3292 p -= installed->start;
3293 if (obsoletes_data[obsoletes[p]] != i)
3294 obsoletes_data[--obsoletes[p]] = i;
3300 /*-----------------------------------------------------------------*/
3310 solver_solve(Solver *solv, Queue *job)
3312 Pool *pool = solv->pool;
3313 Repo *installed = solv->installed;
3316 Map addedmap; /* '1' == have rpm-rules for solvable */
3317 Id how, what, p, *pp, d;
3321 /* create whatprovides if not already there */
3322 if (!pool->whatprovides)
3323 pool_createwhatprovides(pool);
3325 /* create obsolete index if needed */
3326 if (solv->noupdateprovide)
3327 create_obsolete_index(solv);
3330 * create basic rule set of all involved packages
3331 * use addedmap bitmap to make sure we don't create rules twice
3335 map_init(&addedmap, pool->nsolvables);
3339 * always install our system solvable
3341 MAPSET(&addedmap, SYSTEMSOLVABLE);
3342 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
3343 queue_push(&solv->decisionq_why, 0);
3344 solv->decisionmap[SYSTEMSOLVABLE] = 1;
3347 * create rules for all package that could be involved with the solving
3348 * so called: rpm rules
3353 oldnrules = solv->nrules;
3354 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
3355 FOR_REPO_SOLVABLES(installed, p, s)
3356 addrpmrulesforsolvable(solv, s, &addedmap);
3357 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3358 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
3359 oldnrules = solv->nrules;
3360 FOR_REPO_SOLVABLES(installed, p, s)
3361 addrpmrulesforupdaters(solv, s, &addedmap, 1);
3362 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3365 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
3366 oldnrules = solv->nrules;
3367 for (i = 0; i < job->count; i += 2)
3369 how = job->elements[i];
3370 what = job->elements[i + 1];
3374 case SOLVER_INSTALL_SOLVABLE:
3375 addrpmrulesforsolvable(solv, pool->solvables + what, &addedmap);
3377 case SOLVER_INSTALL_SOLVABLE_NAME:
3378 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3379 FOR_PROVIDES(p, pp, what)
3381 /* if by name, ensure that the name matches */
3382 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
3384 addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3387 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3388 /* dont allow downgrade */
3389 addrpmrulesforupdaters(solv, pool->solvables + what, &addedmap, 0);
3393 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3395 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for recommended/suggested packages ***\n");
3397 oldnrules = solv->nrules;
3398 addrpmrulesforweak(solv, &addedmap);
3399 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3401 IF_POOLDEBUG (SAT_DEBUG_STATS)
3403 int possible = 0, installable = 0;
3404 for (i = 1; i < pool->nsolvables; i++)
3406 if (pool_installable(pool, pool->solvables + i))
3408 if (MAPTST(&addedmap, i))
3411 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving (rules has been generated for)\n", possible, installable);
3415 * first pass done, we now have all the rpm rules we need.
3416 * unify existing rules before going over all job rules and
3418 * at this point the system is always solvable,
3419 * as an empty system (remove all packages) is a valid solution
3422 unifyrules(solv); /* remove duplicate rpm rules */
3424 POOL_DEBUG(SAT_DEBUG_STATS, "decisions so far: %d\n", solv->decisionq.count);
3425 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
3426 printdecisions (solv);
3429 * now add all job rules
3432 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
3434 solv->jobrules = solv->nrules;
3436 for (i = 0; i < job->count; i += 2)
3438 oldnrules = solv->nrules;
3440 how = job->elements[i];
3441 what = job->elements[i + 1];
3444 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
3445 s = pool->solvables + what;
3446 POOL_DEBUG(SAT_DEBUG_JOB, "job: install solvable %s\n", solvable2str(pool, s));
3447 addrule(solv, what, 0); /* install by Id */
3448 queue_push(&solv->ruletojob, i);
3450 case SOLVER_ERASE_SOLVABLE:
3451 s = pool->solvables + what;
3452 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase solvable %s\n", solvable2str(pool, s));
3453 addrule(solv, -what, 0); /* remove by Id */
3454 queue_push(&solv->ruletojob, i);
3456 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
3457 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3458 if (how == SOLVER_INSTALL_SOLVABLE_NAME)
3459 POOL_DEBUG(SAT_DEBUG_JOB, "job: install name %s\n", id2str(pool, what));
3460 if (how == SOLVER_INSTALL_SOLVABLE_PROVIDES)
3461 POOL_DEBUG(SAT_DEBUG_JOB, "job: install provides %s\n", dep2str(pool, what));
3463 FOR_PROVIDES(p, pp, what)
3465 /* if by name, ensure that the name matches */
3466 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
3472 /* no provider, make this an impossible rule */
3473 queue_push(&q, -SYSTEMSOLVABLE);
3476 p = queue_shift(&q); /* get first provider */
3478 d = 0; /* single provider ? -> make assertion */
3480 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
3481 addrule(solv, p, d); /* add 'requires' rule */
3482 queue_push(&solv->ruletojob, i);
3484 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
3485 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3486 if (how == SOLVER_ERASE_SOLVABLE_NAME)
3487 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase name %s\n", id2str(pool, what));
3488 if (how == SOLVER_ERASE_SOLVABLE_PROVIDES)
3489 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase provides %s\n", dep2str(pool, what));
3490 FOR_PROVIDES(p, pp, what)
3492 /* if by name, ensure that the name matches */
3493 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
3495 addrule(solv, -p, 0); /* add 'remove' rule */
3496 queue_push(&solv->ruletojob, i);
3499 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
3500 s = pool->solvables + what;
3501 POOL_DEBUG(SAT_DEBUG_JOB, "job: update %s\n", solvable2str(pool, s));
3502 addupdaterule(solv, s, 0);
3503 queue_push(&solv->ruletojob, i);
3506 IF_POOLDEBUG (SAT_DEBUG_JOB)
3509 if (solv->nrules == oldnrules)
3510 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created");
3511 for (j = oldnrules; j < solv->nrules; j++)
3513 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
3514 printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
3519 if (solv->ruletojob.count != solv->nrules - solv->jobrules)
3523 * now add system rules
3527 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add system rules ***\n");
3530 solv->systemrules = solv->nrules;
3533 * create rules for updating installed solvables
3537 if (installed && !solv->allowuninstall)
3538 { /* loop over all installed solvables */
3539 /* we create all update rules, but disable some later on depending on the job */
3540 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3541 if (s->repo == installed)
3542 addupdaterule(solv, s, 0); /* allowall = 0 */
3544 addupdaterule(solv, 0, 0); /* create dummy rule; allowall = 0 */
3545 /* consistency check: we added a rule for _every_ system solvable */
3546 if (solv->nrules - solv->systemrules != installed->end - installed->start)
3550 /* create special weak system rules */
3551 /* those are used later on to keep a version of the installed packages in
3553 if (installed && installed->nsolvables)
3555 solv->weaksystemrules = xcalloc(installed->end - installed->start, sizeof(Id));
3556 FOR_REPO_SOLVABLES(installed, p, s)
3558 policy_findupdatepackages(solv, s, &q, 1);
3560 solv->weaksystemrules[p - installed->start] = pool_queuetowhatprovides(pool, &q);
3564 /* free unneeded memory */
3565 map_free(&addedmap);
3568 solv->weakrules = solv->nrules;
3570 /* try real hard to keep packages installed */
3573 FOR_REPO_SOLVABLES(installed, p, s)
3575 /* FIXME: can't work with refine_suggestion! */
3576 /* need to always add the rule but disable it */
3577 if (MAPTST(&solv->noupdate, p - installed->start))
3579 d = solv->weaksystemrules[p - installed->start];
3580 addrule(solv, p, d);
3584 /* all new rules are learnt after this point */
3585 solv->learntrules = solv->nrules;
3592 disableupdaterules(solv, job, -1);
3593 makeruledecisions(solv);
3595 POOL_DEBUG(SAT_DEBUG_STATS, "problems so far: %d\n", solv->problems.count);
3597 run_solver(solv, 1, 1);
3599 /* find suggested packages */
3600 if (!solv->problems.count)
3602 Id sug, *sugp, p, *pp;
3604 /* create map of all suggests that are still open */
3605 solv->recommends_index = -1;
3606 MAPZERO(&solv->suggestsmap);
3607 for (i = 0; i < solv->decisionq.count; i++)
3609 p = solv->decisionq.elements[i];
3612 s = pool->solvables + p;
3615 sugp = s->repo->idarraydata + s->suggests;
3616 while ((sug = *sugp++) != 0)
3618 FOR_PROVIDES(p, pp, sug)
3619 if (solv->decisionmap[p] > 0)
3622 continue; /* already fulfilled */
3623 FOR_PROVIDES(p, pp, sug)
3624 MAPSET(&solv->suggestsmap, p);
3628 for (i = 1; i < pool->nsolvables; i++)
3630 if (solv->decisionmap[i] != 0)
3632 s = pool->solvables + i;
3633 if (!MAPTST(&solv->suggestsmap, i))
3637 if (!pool_installable(pool, s))
3639 if (!solver_is_enhancing(solv, s))
3642 queue_push(&solv->suggestions, i);
3644 policy_filter_unwanted(solv, &solv->suggestions, 0, POLICY_MODE_SUGGEST);
3647 if (solv->problems.count)
3648 problems_to_solutions(solv, job);