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: %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 */
632 for (i = 0; i < solv->decisionq.count; i++)
633 if (solv->decisionq.elements[i] == -v)
635 if (i == solv->decisionq.count)
637 if (solv->decisionq_why.elements[i] == 0)
639 /* conflict with rpm rule, need only disable our rule */
640 if (v < 0 && v != -SYSTEMSOLVABLE)
643 queue_push(&solv->problems, solv->learnt_pool.count);
644 queue_push(&solv->learnt_pool, ri);
645 queue_push(&solv->learnt_pool, v != -SYSTEMSOLVABLE ? -v : v);
646 queue_push(&solv->learnt_pool, 0);
647 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
648 if (ri < solv->systemrules)
649 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
652 queue_push(&solv->problems, v);
653 disableproblem(solv, v);
654 queue_push(&solv->problems, 0);
658 queue_push(&solv->problems, solv->learnt_pool.count);
659 queue_push(&solv->learnt_pool, ri);
660 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
661 queue_push(&solv->learnt_pool, 0);
663 /* conflict with another job or system rule */
664 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflicting system/job assertions over literal %d\n", vv);
665 /* push all of our rules asserting this literal on the problem stack */
666 for (i = solv->jobrules, rr = solv->rules + i; i < solv->nrules; i++, rr++)
668 if (!rr->w1 || rr->w2)
670 if (rr->p != vv && rr->p != -vv)
672 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
674 if (i < solv->systemrules)
675 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
676 queue_push(&solv->problems, v);
677 disableproblem(solv, v);
679 queue_push(&solv->problems, 0);
682 while (solv->decisionq.count > decisionstart)
684 v = solv->decisionq.elements[--solv->decisionq.count];
685 --solv->decisionq_why.count;
687 solv->decisionmap[vv] = 0;
689 ri = solv->jobrules - 1;
690 r = solv->rules + ri;
693 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions end; size decisionq: %d -----\n",solv->decisionq.count);
697 * we have enabled or disabled some of our rules. We now reenable all
698 * of our learnt rules but the ones that were learnt from rules that
702 enabledisablelearntrules(Solver *solv)
704 Pool *pool = solv->pool;
709 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
710 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
712 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
713 while ((why = *whyp++) != 0)
715 if (why < 0 || why >= i)
716 abort(); /* cannot reference newer learnt rules */
717 if (!solv->rules[why].w1)
720 /* why != 0: we found a disabled rule, disable the learnt rule */
723 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
725 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "disabling learnt ");
726 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
728 disablerule(solv, r);
730 else if (!why && !r->w1)
732 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
734 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "re-enabling learnt ");
735 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
743 /* FIXME: bad code ahead, replace as soon as possible */
745 disableupdaterules(Solver *solv, Queue *job, int jobidx)
747 Pool *pool = solv->pool;
749 Id how, what, p, *pp;
755 installed = solv->installed;
761 how = job->elements[jobidx];
764 case SOLVER_INSTALL_SOLVABLE:
765 case SOLVER_ERASE_SOLVABLE:
766 case SOLVER_ERASE_SOLVABLE_NAME:
767 case SOLVER_ERASE_SOLVABLE_PROVIDES:
773 /* go through all enabled job rules */
774 MAPZERO(&solv->noupdate);
775 for (i = solv->jobrules; i < solv->systemrules; i++)
778 if (!r->w1) /* disabled? */
780 j = solv->ruletojob.elements[i - solv->jobrules];
784 how = job->elements[j];
785 what = job->elements[j + 1];
788 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
789 s = pool->solvables + what;
790 FOR_PROVIDES(p, pp, s->name)
792 if (pool->solvables[p].name != s->name)
794 if (pool->solvables[p].repo == installed)
795 MAPSET(&solv->noupdate, p - installed->start);
798 case SOLVER_ERASE_SOLVABLE:
799 s = pool->solvables + what;
800 if (s->repo == installed)
801 MAPSET(&solv->noupdate, what - installed->start);
803 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
804 case SOLVER_ERASE_SOLVABLE_PROVIDES:
805 FOR_PROVIDES(p, pp, what)
807 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
809 if (pool->solvables[p].repo == installed)
810 MAPSET(&solv->noupdate, p - installed->start);
818 /* fixup update rule status */
819 if (solv->allowuninstall)
820 return; /* no update rules at all */
824 /* we just disabled job #jobidx. enable all update rules
825 * that aren't disabled by the remaining job rules */
826 how = job->elements[jobidx];
827 what = job->elements[jobidx + 1];
830 case SOLVER_INSTALL_SOLVABLE:
831 s = pool->solvables + what;
832 FOR_PROVIDES(p, pp, s->name)
834 if (pool->solvables[p].name != s->name)
836 if (pool->solvables[p].repo != installed)
838 if (MAPTST(&solv->noupdate, p - installed->start))
840 r = solv->rules + solv->systemrules + (p - installed->start);
844 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
846 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
847 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
851 case SOLVER_ERASE_SOLVABLE:
852 s = pool->solvables + what;
853 if (s->repo != installed)
855 if (MAPTST(&solv->noupdate, what - installed->start))
857 r = solv->rules + solv->systemrules + (what - installed->start);
861 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
863 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
864 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
867 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
868 case SOLVER_ERASE_SOLVABLE_PROVIDES:
869 FOR_PROVIDES(p, pp, what)
871 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
873 if (pool->solvables[p].repo != installed)
875 if (MAPTST(&solv->noupdate, p - installed->start))
877 r = solv->rules + solv->systemrules + (p - installed->start);
881 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
883 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
884 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
894 for (i = 0; i < installed->nsolvables; i++)
896 r = solv->rules + solv->systemrules + i;
897 if (r->w1 && MAPTST(&solv->noupdate, i))
898 disablerule(solv, r); /* was enabled, need to disable */
904 * add (install) rules for solvable
905 * for unfulfilled requirements, conflicts, obsoletes,....
906 * add a negative assertion for solvables that are not installable
910 addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
912 Pool *pool = solv->pool;
913 Repo *installed = solv->installed;
927 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable -----\n");
929 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
930 queue_push(&q, s - pool->solvables); /* push solvable Id */
936 * s: Pointer to solvable
940 if (MAPTST(m, n)) /* continue if already done */
944 s = pool->solvables + n; /* s = Solvable in question */
947 if (installed /* Installed system available */
948 && !solv->fixsystem /* NOT repair errors in rpm dependency graph */
949 && s->repo == installed) /* solvable is installed? */
951 dontfix = 1; /* dont care about broken rpm deps */
954 if (!dontfix && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
956 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", solvable2str(pool, s), (Id)(s - pool->solvables));
957 addrule(solv, -n, 0); /* uninstallable */
960 /*-----------------------------------------
961 * check requires of s
966 reqp = s->repo->idarraydata + s->requires;
967 while ((req = *reqp++) != 0) /* go throw all requires */
969 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
972 dp = pool_whatprovides(pool, req);
974 if (*dp == SYSTEMSOLVABLE) /* always installed */
979 /* the strategy here is to not insist on dependencies
980 * that are already broken. so if we find one provider
981 * that was already installed, we know that the
982 * dependency was not broken before so we enforce it */
983 for (i = 0; (p = dp[i]) != 0; i++) /* for all providers */
985 if (pool->solvables[p].repo == installed)
986 break; /* provider was installed */
988 if (!p) /* previously broken dependency */
990 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", dep2str(pool, req), solvable2str(pool, s));
997 /* nothing provides req! */
998 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", solvable2str(pool, s), (Id)(s - pool->solvables), dep2str(pool, req));
999 addrule(solv, -n, 0); /* mark requestor as uninstallable */
1003 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1005 POOL_DEBUG(SAT_DEBUG_RULE_CREATION," %s requires %s\n", solvable2str(pool, s), dep2str(pool, req));
1006 for (i = 0; dp[i]; i++)
1007 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " provided by %s\n", solvable2str(pool, pool->solvables + dp[i]));
1010 /* add 'requires' dependency */
1011 /* rule: (-requestor|provider1|provider2|...|providerN) */
1012 addrule(solv, -n, dp - pool->whatprovidesdata);
1014 /* descend the dependency tree */
1015 for (; *dp; dp++) /* loop through all providers */
1017 if (!MAPTST(m, *dp))
1018 queue_push(&q, *dp);
1021 } /* while, requirements of n */
1023 } /* if, requirements */
1025 /* that's all we check for src packages */
1026 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
1029 /*-----------------------------------------
1030 * check conflicts of s
1035 conp = s->repo->idarraydata + s->conflicts;
1036 while ((con = *conp++) != 0)
1038 FOR_PROVIDES(p, pp, con)
1040 /* dontfix: dont care about conflicts with already installed packs */
1041 if (dontfix && pool->solvables[p].repo == installed)
1043 /* rule: -n|-p: either solvable _or_ provider of conflict */
1044 addrule(solv, -n, -p);
1049 /*-----------------------------------------
1050 * check obsoletes if not installed
1052 if (!installed || pool->solvables[n].repo != installed)
1053 { /* not installed */
1056 obsp = s->repo->idarraydata + s->obsoletes;
1057 while ((obs = *obsp++) != 0)
1059 FOR_PROVIDES(p, pp, obs)
1060 addrule(solv, -n, -p);
1063 FOR_PROVIDES(p, pp, s->name)
1065 if (s->name == pool->solvables[p].name)
1066 addrule(solv, -n, -p);
1070 /*-----------------------------------------
1071 * add recommends to the rule list
1075 recp = s->repo->idarraydata + s->recommends;
1076 while ((rec = *recp++) != 0)
1078 FOR_PROVIDES(p, pp, rec)
1085 sugp = s->repo->idarraydata + s->suggests;
1086 while ((sug = *sugp++) != 0)
1088 FOR_PROVIDES(p, pp, sug)
1095 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable end -----\n");
1099 addrpmrulesforweak(Solver *solv, Map *m)
1101 Pool *pool = solv->pool;
1106 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak -----\n");
1107 for (i = n = 1; n < pool->nsolvables; i++, n++)
1109 if (i == pool->nsolvables)
1113 s = pool->solvables + i;
1114 if (!pool_installable(pool, s))
1119 supp = s->repo->idarraydata + s->supplements;
1120 while ((sup = *supp++) != ID_NULL)
1121 if (dep_possible(solv, sup, m))
1124 if (!sup && s->freshens)
1126 supp = s->repo->idarraydata + s->freshens;
1127 while ((sup = *supp++) != ID_NULL)
1128 if (dep_possible(solv, sup, m))
1131 if (!sup && s->enhances)
1133 supp = s->repo->idarraydata + s->enhances;
1134 while ((sup = *supp++) != ID_NULL)
1135 if (dep_possible(solv, sup, m))
1140 addrpmrulesforsolvable(solv, s, m);
1143 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak end -----\n");
1147 addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allowall)
1149 Pool *pool = solv->pool;
1154 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1156 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1157 policy_findupdatepackages(solv, s, &qs, allowall);
1158 if (!MAPTST(m, s - pool->solvables)) /* add rule for s if not already done */
1159 addrpmrulesforsolvable(solv, s, m);
1160 for (i = 0; i < qs.count; i++)
1161 if (!MAPTST(m, qs.elements[i]))
1162 addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
1165 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1169 * add rule for update
1170 * (A|A1|A2|A3...) An = update candidates for A
1172 * s = (installed) solvable
1176 addupdaterule(Solver *solv, Solvable *s, int allowall)
1178 /* installed packages get a special upgrade allowed rule */
1179 Pool *pool = solv->pool;
1184 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule -----\n");
1186 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1187 policy_findupdatepackages(solv, s, &qs, allowall);
1188 if (qs.count == 0) /* no updaters found */
1191 d = pool_queuetowhatprovides(pool, &qs); /* intern computed queue */
1193 addrule(solv, s - pool->solvables, d); /* allow update of s */
1194 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule end -----\n");
1198 /*-----------------------------------------------------------------*/
1205 * initial setup for all watches
1209 makewatches(Solver *solv)
1213 int nsolvables = solv->pool->nsolvables;
1215 xfree(solv->watches);
1216 /* lower half for removals, upper half for installs */
1217 solv->watches = (Id *)xcalloc(2 * nsolvables, sizeof(Id));
1219 /* do it reverse so rpm rules get triggered first */
1220 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1222 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1225 if (!r->w1 /* rule is disabled */
1226 || !r->w2) /* rule is assertion */
1229 /* see addwatches(solv, r) */
1230 r->n1 = solv->watches[nsolvables + r->w1];
1231 solv->watches[nsolvables + r->w1] = r - solv->rules;
1233 r->n2 = solv->watches[nsolvables + r->w2];
1234 solv->watches[nsolvables + r->w2] = r - solv->rules;
1240 * add watches (for rule)
1244 addwatches(Solver *solv, Rule *r)
1246 int nsolvables = solv->pool->nsolvables;
1248 r->n1 = solv->watches[nsolvables + r->w1];
1249 solv->watches[nsolvables + r->w1] = r - solv->rules;
1251 r->n2 = solv->watches[nsolvables + r->w2];
1252 solv->watches[nsolvables + r->w2] = r - solv->rules;
1256 /*-----------------------------------------------------------------*/
1257 /* rule propagation */
1259 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1264 * propagate decision to all rules
1268 propagate(Solver *solv, int level)
1270 Pool *pool = solv->pool;
1275 Id *decisionmap = solv->decisionmap;
1276 Id *watches = solv->watches + pool->nsolvables;
1278 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
1280 while (solv->propagate_index < solv->decisionq.count)
1282 /* negate because our watches trigger if literal goes FALSE */
1283 pkg = -solv->decisionq.elements[solv->propagate_index++];
1284 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1286 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "popagate for decision %d level %d\n", -pkg, level);
1287 printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
1290 for (rp = watches + pkg; *rp; rp = nrp)
1292 r = solv->rules + *rp;
1294 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1296 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
1297 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1310 /* if clause is TRUE, nothing to do */
1311 if (DECISIONMAP_TRUE(ow))
1316 /* not a binary clause, check if we need to move our watch */
1317 /* search for a literal that is not ow and not false */
1318 /* (true is also ok, in that case the rule is fulfilled) */
1319 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1322 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1323 if (p != ow && !DECISIONMAP_TRUE(-p))
1327 /* p is free to watch, move watch to p */
1328 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1331 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables + p));
1333 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables - p));
1347 watches[p] = r - solv->rules;
1351 /* unit clause found, set other watch to TRUE */
1352 if (DECISIONMAP_TRUE(-ow))
1353 return r; /* eek, a conflict! */
1354 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1356 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unit ");
1357 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1360 decisionmap[ow] = level;
1362 decisionmap[-ow] = -level;
1363 queue_push(&solv->decisionq, ow);
1364 queue_push(&solv->decisionq_why, r - solv->rules);
1365 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1367 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1369 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", solvable2str(pool, s));
1371 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", solvable2str(pool, s));
1375 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
1377 return 0; /* all is well */
1381 /*-----------------------------------------------------------------*/
1390 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *why)
1392 Pool *pool = solv->pool;
1395 Map seen; /* global? */
1399 int learnt_why = solv->learnt_pool.count;
1400 Id *decisionmap = solv->decisionmap;
1404 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
1405 map_init(&seen, pool->nsolvables);
1406 idx = solv->decisionq.count;
1409 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1410 printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1411 queue_push(&solv->learnt_pool, c - solv->rules);
1412 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1423 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1425 vv = v > 0 ? v : -v;
1426 if (MAPTST(&seen, vv))
1428 l = solv->decisionmap[vv];
1435 for (j = 0; j < solv->decisionq.count; j++)
1436 if (solv->decisionq.elements[j] == v)
1438 if (j == solv->decisionq.count)
1440 queue_push(&rulq, -(j + 1));
1442 continue; /* initial setting */
1446 num++; /* need to do this one as well */
1458 v = solv->decisionq.elements[--idx];
1459 vv = v > 0 ? v : -v;
1460 if (MAPTST(&seen, vv))
1463 c = solv->rules + solv->decisionq_why.elements[idx];
1471 else if (r.count == 1 && r.elements[0] < 0)
1472 *dr = r.elements[0];
1474 *dr = pool_queuetowhatprovides(pool, &r);
1475 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1477 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
1478 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, -v);
1479 for (i = 0; i < r.count; i++)
1480 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
1483 /* push end marker on learnt reasons stack */
1484 queue_push(&solv->learnt_pool, 0);
1485 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1487 for (i = learnt_why; solv->learnt_pool.elements[i]; i++)
1489 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learnt_why ");
1490 printrule(solv, SAT_DEBUG_ANALYZE, solv->rules + solv->learnt_pool.elements[i]);
1501 * reset the solver decisions to right after the rpm rules.
1502 * called after rules have been enabled/disabled
1506 reset_solver(Solver *solv)
1508 Pool *pool = solv->pool;
1512 enabledisablelearntrules(solv);
1514 /* redo all direct rpm rule decisions */
1515 /* we break at the first decision with a why attached, this is
1516 * either a job/system rule assertion or a propagated decision */
1517 for (i = 0; i < solv->decisionq.count; i++)
1519 v = solv->decisionq.elements[i];
1520 solv->decisionmap[v > 0 ? v : -v] = 0;
1522 for (i = 0; i < solv->decisionq_why.count; i++)
1523 if (solv->decisionq_why.elements[i])
1527 v = solv->decisionq.elements[i];
1528 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1531 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions done reduced from %d to %d\n", solv->decisionq.count, i);
1533 solv->decisionq_why.count = i;
1534 solv->decisionq.count = i;
1535 solv->recommends_index = -1;
1536 solv->propagate_index = 0;
1538 /* redo all job/system decisions */
1539 makeruledecisions(solv);
1540 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
1541 /* recreate watch chains */
1547 * analyze_unsolvable_rule
1551 analyze_unsolvable_rule(Solver *solv, Rule *r)
1553 Pool *pool = solv->pool;
1555 Id why = r - solv->rules;
1556 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1557 printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1558 if (solv->learntrules && why >= solv->learntrules)
1560 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1561 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i]);
1564 /* do not add rpm rules to problem */
1565 if (why < solv->jobrules)
1567 /* turn rule into problem */
1568 if (why >= solv->jobrules && why < solv->systemrules)
1569 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1570 /* return if problem already countains our rule */
1571 if (solv->problems.count)
1573 for (i = solv->problems.count - 1; i >= 0; i--)
1574 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
1576 else if (solv->problems.elements[i] == why)
1579 queue_push(&solv->problems, why);
1584 * analyze_unsolvable
1586 * return: 1 - disabled some rules, try again
1591 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1593 Pool *pool = solv->pool;
1595 Map seen; /* global to speed things up? */
1598 Id *decisionmap = solv->decisionmap;
1599 int oldproblemcount;
1600 int oldlearntpoolcount;
1603 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
1604 oldproblemcount = solv->problems.count;
1605 oldlearntpoolcount = solv->learnt_pool.count;
1607 /* make room for proof index */
1608 /* must update it later, as analyze_unsolvable_rule would confuse
1609 * it with a rule index if we put the real value in already */
1610 queue_push(&solv->problems, 0);
1613 map_init(&seen, pool->nsolvables);
1614 queue_push(&solv->learnt_pool, r - solv->rules);
1615 analyze_unsolvable_rule(solv, r);
1616 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1627 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1629 vv = v > 0 ? v : -v;
1630 l = solv->decisionmap[vv];
1635 idx = solv->decisionq.count;
1638 v = solv->decisionq.elements[--idx];
1639 vv = v > 0 ? v : -v;
1640 if (!MAPTST(&seen, vv))
1642 why = solv->decisionq_why.elements[idx];
1645 /* level 1 and no why, must be an rpm assertion */
1646 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1648 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "RPM ");
1649 printruleelement(solv, SAT_DEBUG_UNSOLVABLE, 0, v);
1651 /* this is the only positive rpm assertion */
1652 if (v == SYSTEMSOLVABLE)
1656 queue_push(&solv->learnt_pool, v);
1659 r = solv->rules + why;
1660 queue_push(&solv->learnt_pool, why);
1661 analyze_unsolvable_rule(solv, r);
1662 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1673 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1675 vv = v > 0 ? v : -v;
1676 l = solv->decisionmap[vv];
1683 queue_push(&solv->problems, 0); /* mark end of this problem */
1686 if (solv->weakrules != solv->learntrules)
1688 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1690 why = solv->problems.elements[i];
1691 if (why < solv->weakrules || why >= solv->learntrules)
1693 if (!lastweak || lastweak < why)
1699 /* disable last weak rule */
1700 solv->problems.count = oldproblemcount;
1701 solv->learnt_pool.count = oldlearntpoolcount;
1702 r = solv->rules + lastweak;
1703 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling weak ");
1704 printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
1705 disablerule(solv, r);
1711 queue_push(&solv->learnt_pool, 0);
1712 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1716 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1717 disableproblem(solv, solv->problems.elements[i]);
1721 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1726 /*-----------------------------------------------------------------*/
1727 /* Decision revert */
1731 * revert decision at level
1735 revert(Solver *solv, int level)
1737 Pool *pool = solv->pool;
1739 while (solv->decisionq.count)
1741 v = solv->decisionq.elements[solv->decisionq.count - 1];
1742 vv = v > 0 ? v : -v;
1743 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1745 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1746 solv->decisionmap[vv] = 0;
1747 solv->decisionq.count--;
1748 solv->decisionq_why.count--;
1749 solv->propagate_index = solv->decisionq.count;
1751 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1753 solv->branches.count--;
1754 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1755 solv->branches.count--;
1757 solv->recommends_index = -1;
1762 * watch2onhighest - put watch2 on literal with highest level
1766 watch2onhighest(Solver *solv, Rule *r)
1772 return; /* binary rule, both watches are set */
1773 dp = solv->pool->whatprovidesdata + r->d;
1774 while ((v = *dp++) != 0)
1776 l = solv->decisionmap[v < 0 ? -v : v];
1791 * add free decision to decision q, increase level
1792 * propagate decision, return if no conflict.
1793 * in conflict case, analyze conflict rule, add resulting
1794 * rule to learnt rule set, make decision from learnt
1795 * rule (always unit) and re-propagate.
1799 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
1801 Pool *pool = solv->pool;
1810 solv->decisionmap[decision] = level;
1812 solv->decisionmap[-decision] = -level;
1813 queue_push(&solv->decisionq, decision);
1814 queue_push(&solv->decisionq_why, 0);
1818 r = propagate(solv, level);
1822 return analyze_unsolvable(solv, r, disablerules);
1823 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1824 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
1825 if (l >= level || l <= 0)
1827 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1829 revert(solv, level);
1830 r = addrule(solv, p, d); /* p requires d */
1833 if (solv->learnt_why.count != (r - solv->rules) - solv->learntrules)
1835 queue_push(&solv->learnt_why, why);
1838 /* at least 2 literals, needs watches */
1839 watch2onhighest(solv, r);
1840 addwatches(solv, r);
1842 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1843 queue_push(&solv->decisionq, p);
1844 queue_push(&solv->decisionq_why, r - solv->rules);
1845 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1847 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
1848 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
1849 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
1850 printrule(solv, SAT_DEBUG_ANALYZE, r);
1858 * install best package from the queue. We add an extra package, inst, if
1859 * provided. See comment in weak install section.
1862 selectandinstall(Solver *solv, int level, Queue *dq, Id inst, int disablerules)
1864 Pool *pool = solv->pool;
1868 if (dq->count > 1 || inst)
1869 policy_filter_unwanted(solv, dq, inst, POLICY_MODE_CHOOSE);
1874 /* choose the supplemented one */
1875 for (i = 0; i < dq->count; i++)
1876 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1880 for (i = 1; i < dq->count; i++)
1881 queue_push(&solv->branches, dq->elements[i]);
1882 queue_push(&solv->branches, -level);
1886 p = dq->elements[i];
1888 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvable2str(pool, pool->solvables + p));
1890 return setpropagatelearn(solv, level, p, disablerules);
1894 /*-----------------------------------------------------------------*/
1895 /* Main solver interface */
1900 * create solver structure
1902 * pool: all available solvables
1903 * installed: installed Solvables
1906 * Upon solving, rules are created to flag the Solvables
1907 * of the 'installed' Repo as installed.
1911 solver_create(Pool *pool, Repo *installed)
1914 solv = (Solver *)xcalloc(1, sizeof(Solver));
1916 solv->installed = installed;
1918 queue_init(&solv->ruletojob);
1919 queue_init(&solv->decisionq);
1920 queue_init(&solv->decisionq_why);
1921 queue_init(&solv->problems);
1922 queue_init(&solv->suggestions);
1923 queue_init(&solv->learnt_why);
1924 queue_init(&solv->learnt_pool);
1925 queue_init(&solv->branches);
1927 map_init(&solv->recommendsmap, pool->nsolvables);
1928 map_init(&solv->suggestsmap, pool->nsolvables);
1929 map_init(&solv->noupdate, installed ? installed->end - installed->start : 0);
1930 solv->recommends_index = 0;
1932 solv->decisionmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
1933 solv->rules = (Rule *)xmalloc((solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
1934 memset(solv->rules, 0, sizeof(Rule));
1946 solver_free(Solver *solv)
1948 queue_free(&solv->ruletojob);
1949 queue_free(&solv->decisionq);
1950 queue_free(&solv->decisionq_why);
1951 queue_free(&solv->learnt_why);
1952 queue_free(&solv->learnt_pool);
1953 queue_free(&solv->problems);
1954 queue_free(&solv->suggestions);
1955 queue_free(&solv->branches);
1957 map_free(&solv->recommendsmap);
1958 map_free(&solv->suggestsmap);
1959 map_free(&solv->noupdate);
1960 xfree(solv->decisionmap);
1962 xfree(solv->watches);
1963 xfree(solv->weaksystemrules);
1964 xfree(solv->obsoletes);
1965 xfree(solv->obsoletes_data);
1970 /*-------------------------------------------------------*/
1975 * all rules have been set up, now actually run the solver
1980 run_solver(Solver *solv, int disablerules, int doweak)
1988 Pool *pool = solv->pool;
1991 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1993 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1994 for (i = 0; i < solv->nrules; i++)
1995 printrule(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
1998 /* create watches chains */
2001 POOL_DEBUG(SAT_DEBUG_STATS, "initial decisions: %d\n", solv->decisionq.count);
2003 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2004 printdecisions(solv);
2006 /* start SAT algorithm */
2008 systemlevel = level + 1;
2009 POOL_DEBUG(SAT_DEBUG_STATS, "solving...\n");
2020 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
2021 if ((r = propagate(solv, level)) != 0)
2023 if (analyze_unsolvable(solv, r, disablerules))
2031 * installed packages
2034 if (level < systemlevel && solv->installed && solv->installed->nsolvables)
2036 if (!solv->updatesystem)
2038 /* try to keep as many packages as possible */
2039 POOL_DEBUG(SAT_DEBUG_STATS, "installing system packages\n");
2040 for (i = solv->installed->start, n = 0; ; i++)
2042 if (n == solv->installed->nsolvables)
2044 if (i == solv->installed->end)
2045 i = solv->installed->start;
2046 s = pool->solvables + i;
2047 if (s->repo != solv->installed)
2050 if (solv->decisionmap[i] != 0)
2052 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "keeping %s\n", solvable2str(pool, s));
2054 level = setpropagatelearn(solv, level, i, disablerules);
2060 if (level <= olevel)
2064 if (solv->weaksystemrules)
2066 POOL_DEBUG(SAT_DEBUG_STATS, "installing weak system packages\n");
2067 for (i = solv->installed->start; i < solv->installed->end; i++)
2069 if (pool->solvables[i].repo != solv->installed)
2071 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->installed->start] == 0))
2073 /* noupdate is set if a job is erasing the installed solvable or installing a specific version */
2074 if (MAPTST(&solv->noupdate, i - solv->installed->start))
2077 if (solv->weaksystemrules[i - solv->installed->start])
2079 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->installed->start];
2080 while ((p = *dp++) != 0)
2082 if (solv->decisionmap[p] > 0)
2084 if (solv->decisionmap[p] == 0)
2088 continue; /* update package already installed */
2090 if (!dq.count && solv->decisionmap[i] != 0)
2093 /* FIXME: i is handled a bit different because we do not want
2094 * to have it pruned just because it is not recommened.
2095 * we should not prune installed packages instead */
2096 level = selectandinstall(solv, level, &dq, (solv->decisionmap[i] ? 0 : i), disablerules);
2102 if (level <= olevel)
2105 if (i < solv->installed->end)
2108 systemlevel = level;
2115 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unresolved rules\n");
2116 for (i = 1, n = 1; ; i++, n++)
2118 if (n == solv->nrules)
2120 if (i == solv->nrules)
2122 r = solv->rules + i;
2128 /* binary or unary rule */
2129 /* need two positive undecided literals */
2130 if (r->p < 0 || r->w2 <= 0)
2132 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2134 queue_push(&dq, r->p);
2135 queue_push(&dq, r->w2);
2140 * all negative literals are installed
2141 * no positive literal is installed
2142 * i.e. the rule is not fulfilled and we
2143 * just need to decide on the positive literals
2147 if (solv->decisionmap[-r->p] <= 0)
2152 if (solv->decisionmap[r->p] > 0)
2154 if (solv->decisionmap[r->p] == 0)
2155 queue_push(&dq, r->p);
2157 dp = pool->whatprovidesdata + r->d;
2158 while ((p = *dp++) != 0)
2162 if (solv->decisionmap[-p] <= 0)
2167 if (solv->decisionmap[p] > 0)
2169 if (solv->decisionmap[p] == 0)
2178 /* cannot happen as this means that
2179 * the rule is unit */
2180 printrule(solv, SAT_FATAL, r);
2183 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
2185 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
2186 printrule(solv, SAT_DEBUG_PROPAGATE, r);
2190 level = selectandinstall(solv, level, &dq, 0, disablerules);
2196 if (level < systemlevel)
2199 } /* for(), decide */
2201 if (n != solv->nrules) /* continue if level < systemlevel */
2204 if (doweak && !solv->problems.count)
2208 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended packages\n");
2209 if (!REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2211 for (i = 0; i < solv->decisionq.count; i++)
2213 p = solv->decisionq.elements[i];
2214 if (p > 0 && pool->solvables[p].repo == solv->installed)
2215 solv->decisionmap[p] = -solv->decisionmap[p];
2219 for (i = 1; i < pool->nsolvables; i++)
2221 if (solv->decisionmap[i] < 0)
2223 if (solv->decisionmap[i] > 0)
2225 Id *recp, rec, *pp, p;
2226 s = pool->solvables + i;
2227 /* installed, check for recommends */
2228 /* XXX need to special case AND ? */
2231 recp = s->repo->idarraydata + s->recommends;
2232 while ((rec = *recp++) != 0)
2235 FOR_PROVIDES(p, pp, rec)
2237 if (solv->decisionmap[p] > 0)
2242 else if (solv->decisionmap[p] == 0)
2244 queue_pushunique(&dq, p);
2252 s = pool->solvables + i;
2253 if (!s->supplements && !s->freshens)
2255 if (!pool_installable(pool, s))
2257 if (solver_is_supplementing(solv, s))
2258 queue_pushunique(&dq, i);
2261 if (!REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2263 for (i = 0; i < solv->decisionq.count; i++)
2265 p = solv->decisionq.elements[i];
2266 if (p > 0 && pool->solvables[p].repo == solv->installed)
2267 solv->decisionmap[p] = -solv->decisionmap[p];
2273 policy_filter_unwanted(solv, &dq, 0, POLICY_MODE_RECOMMEND);
2275 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended %s\n", solvable2str(pool, pool->solvables + p));
2276 level = setpropagatelearn(solv, level, p, 0);
2281 if (solv->solution_callback)
2283 solv->solution_callback(solv, solv->solution_callback_data);
2284 if (solv->branches.count)
2286 int i = solv->branches.count - 1;
2287 int l = -solv->branches.elements[i];
2289 if (solv->branches.elements[i - 1] < 0)
2291 p = solv->branches.elements[i];
2292 POOL_DEBUG(SAT_DEBUG_STATS, "branching with %s\n", solvable2str(pool, pool->solvables + p));
2294 for (j = i + 1; j < solv->branches.count; j++)
2295 queue_push(&dq, solv->branches.elements[j]);
2296 solv->branches.count = i;
2298 revert(solv, level);
2300 for (j = 0; j < dq.count; j++)
2301 queue_push(&solv->branches, dq.elements[j]);
2303 level = setpropagatelearn(solv, level, p, disablerules);
2311 /* all branches done, we're finally finished */
2315 /* minimization step */
2316 if (solv->branches.count)
2318 int l = 0, lasti = -1, lastl = -1;
2320 for (i = solv->branches.count - 1; i >= 0; i--)
2322 p = solv->branches.elements[i];
2325 else if (p > 0 && solv->decisionmap[p] > l + 1)
2333 /* kill old solvable so that we do not loop */
2334 p = solv->branches.elements[lasti];
2335 solv->branches.elements[lasti] = 0;
2336 POOL_DEBUG(SAT_DEBUG_STATS, "minimizing %d -> %d with %s\n", solv->decisionmap[p], l, solvable2str(pool, pool->solvables + p));
2339 revert(solv, level);
2341 level = setpropagatelearn(solv, level, p, disablerules);
2358 * at this point, all rules that led to conflicts are disabled.
2359 * we re-enable all rules of a problem set but rule "sug", then
2360 * continue to disable more rules until there as again a solution.
2363 /* FIXME: think about conflicting assertions */
2366 refine_suggestion(Solver *solv, Queue *job, Id *problem, Id sug, Queue *refined)
2368 Pool *pool = solv->pool;
2375 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2377 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion start\n");
2378 for (i = 0; problem[i]; i++)
2380 if (problem[i] == sug)
2381 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "=> ");
2382 printproblem(solv, problem[i]);
2385 queue_init(&disabled);
2386 queue_empty(refined);
2387 queue_push(refined, sug);
2389 /* re-enable all problem rules with the exception of "sug" */
2393 for (i = 0; problem[i]; i++)
2394 if (problem[i] != sug)
2395 enableproblem(solv, problem[i]);
2398 disableupdaterules(solv, job, -(sug + 1));
2402 /* re-enable as many weak rules as possible */
2403 for (i = solv->weakrules; i < solv->learntrules; i++)
2405 r = solv->rules + i;
2407 enablerule(solv, r);
2410 queue_empty(&solv->problems);
2411 revert(solv, 1); /* XXX move to reset_solver? */
2414 run_solver(solv, 0, 0);
2415 if (!solv->problems.count)
2417 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no more problems!\n");
2418 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2419 printdecisions(solv);
2420 break; /* great, no more problems */
2422 disabledcnt = disabled.count;
2423 /* skip over proof index */
2424 for (i = 1; i < solv->problems.count - 1; i++)
2426 /* ignore solutions in refined */
2427 v = solv->problems.elements[i];
2429 break; /* end of problem reached */
2430 for (j = 0; problem[j]; j++)
2431 if (problem[j] != sug && problem[j] == v)
2435 queue_push(&disabled, v);
2437 if (disabled.count == disabledcnt)
2439 /* no solution found, this was an invalid suggestion! */
2440 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no solution found!\n");
2444 if (disabled.count == disabledcnt + 1)
2446 /* just one suggestion, add it to refined list */
2447 v = disabled.elements[disabledcnt];
2448 queue_push(refined, v);
2449 disableproblem(solv, v);
2451 disableupdaterules(solv, job, -(v + 1));
2455 /* more than one solution, disable all */
2456 /* do not push anything on refine list */
2457 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2459 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "more than one solution found:\n");
2460 for (i = disabledcnt; i < disabled.count; i++)
2461 printproblem(solv, disabled.elements[i]);
2463 for (i = disabledcnt; i < disabled.count; i++)
2464 disableproblem(solv, disabled.elements[i]);
2467 /* all done, get us back into the same state as before */
2468 /* enable refined rules again */
2469 for (i = 0; i < disabled.count; i++)
2470 enableproblem(solv, disabled.elements[i]);
2471 /* disable problem rules again */
2472 for (i = 0; problem[i]; i++)
2473 disableproblem(solv, problem[i]);
2474 disableupdaterules(solv, job, -1);
2475 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion end\n");
2479 problems_to_solutions(Solver *solv, Queue *job)
2481 Pool *pool = solv->pool;
2489 if (!solv->problems.count)
2491 queue_clone(&problems, &solv->problems);
2492 queue_init(&solution);
2493 queue_init(&solutions);
2494 /* copy over proof index */
2495 queue_push(&solutions, problems.elements[0]);
2496 problem = problems.elements + 1;
2497 for (i = 1; i < problems.count; i++)
2499 Id v = problems.elements[i];
2502 /* mark end of this problem */
2503 queue_push(&solutions, 0);
2504 queue_push(&solutions, 0);
2505 if (i + 1 == problems.count)
2507 /* copy over proof of next problem */
2508 queue_push(&solutions, problems.elements[i + 1]);
2510 problem = problems.elements + i + 1;
2513 refine_suggestion(solv, job, problem, v, &solution);
2514 if (!solution.count)
2515 continue; /* this solution didn't work out */
2517 for (j = 0; j < solution.count; j++)
2519 why = solution.elements[j];
2521 printproblem(solv, why);
2525 queue_push(&solutions, 0);
2526 queue_push(&solutions, -why);
2528 else if (why >= solv->systemrules && why < solv->weakrules)
2531 p = solv->installed->start + (why - solv->systemrules);
2532 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
2534 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
2537 if (*dp >= solv->installed->start && *dp < solv->installed->start + solv->installed->nsolvables)
2539 if (solv->decisionmap[*dp] > 0)
2546 queue_push(&solutions, p);
2547 queue_push(&solutions, rp);
2552 /* mark end of this solution */
2553 queue_push(&solutions, 0);
2554 queue_push(&solutions, 0);
2556 queue_free(&solution);
2557 queue_free(&problems);
2558 /* copy queue over to solutions */
2559 queue_free(&solv->problems);
2560 queue_clone(&solv->problems, &solutions);
2562 /* bring solver back into problem state */
2563 revert(solv, 1); /* XXX move to reset_solver? */
2566 if (solv->problems.count != solutions.count)
2568 queue_free(&solutions);
2572 solver_next_problem(Solver *solv, Id problem)
2576 return solv->problems.count ? 1 : 0;
2577 pp = solv->problems.elements + problem;
2578 while (pp[0] || pp[1])
2582 while (pp[0] || pp[1])
2587 problem = pp - solv->problems.elements;
2588 if (problem >= solv->problems.count)
2594 solver_next_solution(Solver *solv, Id problem, Id solution)
2600 pp = solv->problems.elements + solution;
2601 return pp[0] || pp[1] ? solution : 0;
2603 pp = solv->problems.elements + solution;
2604 while (pp[0] || pp[1])
2607 solution = pp - solv->problems.elements;
2608 return pp[0] || pp[1] ? solution : 0;
2612 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
2615 element = element ? element + 2 : solution;
2616 pp = solv->problems.elements + element;
2617 if (!(pp[0] || pp[1]))
2626 * create obsoletesmap from solver decisions
2627 * required for decision handling
2631 create_obsoletesmap(Solver *solv)
2633 Pool *pool = solv->pool;
2634 Repo *installed = solv->installed;
2635 Id p, *obsoletesmap = NULL;
2639 obsoletesmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
2642 for (i = 0; i < solv->decisionq.count; i++)
2646 n = solv->decisionq.elements[i];
2649 if (n == SYSTEMSOLVABLE)
2651 s = pool->solvables + n;
2652 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2654 FOR_PROVIDES(p, pp, s->name)
2655 if (s->name == pool->solvables[p].name)
2657 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2659 obsoletesmap[p] = n;
2664 for (i = 0; i < solv->decisionq.count; i++)
2669 n = solv->decisionq.elements[i];
2672 if (n == SYSTEMSOLVABLE)
2674 s = pool->solvables + n;
2675 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2679 obsp = s->repo->idarraydata + s->obsoletes;
2680 while ((obs = *obsp++) != 0)
2681 FOR_PROVIDES(p, pp, obs)
2683 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2685 obsoletesmap[p] = n;
2691 return obsoletesmap;
2700 printdecisions(Solver *solv)
2702 Pool *pool = solv->pool;
2703 Repo *installed = solv->installed;
2704 Id p, *obsoletesmap = create_obsoletesmap( solv );
2708 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions -----\n");
2710 /* print solvables to be erased */
2714 FOR_REPO_SOLVABLES(installed, p, s)
2716 if (solv->decisionmap[p] >= 0)
2718 if (obsoletesmap[p])
2720 POOL_DEBUG(SAT_DEBUG_RESULT, "erase %s\n", solvable2str(pool, s));
2724 /* print solvables to be installed */
2726 for (i = 0; i < solv->decisionq.count; i++)
2729 p = solv->decisionq.elements[i];
2732 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2735 s = pool->solvables + p;
2736 POOL_DEBUG(SAT_DEBUG_SCHUBI, "level of %s is %d\n", solvable2str(pool, s), p);
2740 if (p == SYSTEMSOLVABLE)
2742 POOL_DEBUG(SAT_DEBUG_SCHUBI, "SYSTEMSOLVABLE\n");
2745 s = pool->solvables + p;
2746 if (installed && s->repo == installed)
2749 if (!obsoletesmap[p])
2751 POOL_DEBUG(SAT_DEBUG_RESULT, "install %s", solvable2str(pool, s));
2755 POOL_DEBUG(SAT_DEBUG_RESULT, "update %s", solvable2str(pool, s));
2756 POOL_DEBUG(SAT_DEBUG_RESULT, " (obsoletes");
2757 for (j = installed->start; j < installed->end; j++)
2758 if (obsoletesmap[j] == p)
2759 POOL_DEBUG(SAT_DEBUG_RESULT, " %s", solvable2str(pool, pool->solvables + j));
2760 POOL_DEBUG(SAT_DEBUG_RESULT, ")");
2762 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
2765 xfree(obsoletesmap);
2767 if (solv->suggestions.count)
2769 POOL_DEBUG(SAT_DEBUG_RESULT, "\nsuggested packages:\n");
2770 for (i = 0; i < solv->suggestions.count; i++)
2772 s = pool->solvables + solv->suggestions.elements[i];
2773 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s\n", solvable2str(pool, s));
2776 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions end -----\n");
2779 /* this is basically the reverse of addrpmrulesforsolvable */
2781 solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp)
2783 Pool *pool = solv->pool;
2784 Repo *installed = solv->installed;
2788 Id p, *pp, req, *reqp, con, *conp, obs, *obsp, *dp;
2790 if (rid >= solv->weakrules)
2792 if (rid >= solv->systemrules)
2795 *sourcep = solv->installed->start + (rid - solv->systemrules);
2797 return SOLVER_PROBLEM_UPDATE_RULE;
2799 if (rid >= solv->jobrules)
2802 r = solv->rules + rid;
2803 p = solv->ruletojob.elements[rid - solv->jobrules];
2804 *depp = job->elements[p + 1];
2806 *targetp = job->elements[p];
2807 if (r->d == 0 && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
2808 return SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP;
2809 return SOLVER_PROBLEM_JOB_RULE;
2813 /* a rpm rule assertion */
2814 if (rid == -SYSTEMSOLVABLE)
2815 abort(); /* can happen only for job rules */
2816 s = pool->solvables - rid;
2817 if (installed && !solv->fixsystem && s->repo == installed)
2819 if (dontfix) /* dontfix packages never have a neg assertion */
2821 /* see why the package is not installable */
2822 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
2823 return SOLVER_PROBLEM_NOT_INSTALLABLE;
2824 /* check requires */
2827 reqp = s->repo->idarraydata + s->requires;
2828 while ((req = *reqp++) != 0)
2830 if (req == SOLVABLE_PREREQMARKER)
2832 dp = pool_whatprovides(pool, req);
2838 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
2843 r = solv->rules + rid;
2845 abort(); /* not a rpm rule */
2846 if (r->d == 0 && r->w2 == 0)
2848 /* an assertion. we don't store them as rpm rules, so
2852 s = pool->solvables - r->p;
2853 if (installed && !solv->fixsystem && s->repo == installed)
2855 if (r->d == 0 && r->w2 < 0)
2857 /* a package conflict */
2858 Solvable *s2 = pool->solvables - r->w2;
2861 if (installed && !solv->fixsystem && s2->repo == installed)
2864 /* if both packages have the same name and at least one of them
2865 * is not installed, they conflict */
2866 if (s->name == s2->name && (!installed || (s->repo != installed || s2->repo != installed)))
2871 return SOLVER_PROBLEM_SAME_NAME;
2874 /* check conflicts in both directions */
2877 conp = s->repo->idarraydata + s->conflicts;
2878 while ((con = *conp++) != 0)
2880 FOR_PROVIDES(p, pp, con)
2882 if (dontfix && pool->solvables[p].repo == installed)
2889 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
2895 conp = s2->repo->idarraydata + s2->conflicts;
2896 while ((con = *conp++) != 0)
2898 FOR_PROVIDES(p, pp, con)
2900 if (dontfix2 && pool->solvables[p].repo == installed)
2907 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
2911 /* check obsoletes in both directions */
2912 if ((!installed || s->repo != installed) && s->obsoletes)
2914 obsp = s->repo->idarraydata + s->obsoletes;
2915 while ((obs = *obsp++) != 0)
2917 FOR_PROVIDES(p, pp, obs)
2924 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
2928 if ((!installed || s2->repo != installed) && s2->obsoletes)
2930 obsp = s2->repo->idarraydata + s2->obsoletes;
2931 while ((obs = *obsp++) != 0)
2933 FOR_PROVIDES(p, pp, obs)
2940 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
2944 /* all cases checked, can't happen */
2947 /* simple requires */
2950 reqp = s->repo->idarraydata + s->requires;
2951 while ((req = *reqp++) != 0)
2953 if (req == SOLVABLE_PREREQMARKER)
2955 dp = pool_whatprovides(pool, req);
2958 if (*dp == r->w2 && dp[1] == 0)
2961 else if (dp - pool->whatprovidesdata == r->d)
2969 return SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE;
2973 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp)
2976 Id lreqr, lconr, lsysr, ljobr;
2980 lreqr = lconr = lsysr = ljobr = 0;
2981 while ((rid = solv->learnt_pool.elements[idx++]) != 0)
2983 if (rid >= solv->learntrules)
2984 findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr);
2985 else if (rid >= solv->systemrules)
2990 else if (rid >= solv->jobrules)
2997 r = solv->rules + rid;
2998 if (!r->d && r->w2 < 0)
3011 /* assertion, counts as require rule */
3012 /* ignore system solvable as we need useful info */
3013 if (rid == -SYSTEMSOLVABLE)
3015 if (!*reqrp || !reqassert)
3022 if (!*reqrp && lreqr)
3024 if (!*conrp && lconr)
3026 if (!*jobrp && ljobr)
3028 if (!*sysrp && lsysr)
3033 solver_findproblemrule(Solver *solv, Id problem)
3035 Id reqr, conr, sysr, jobr;
3036 Id idx = solv->problems.elements[problem - 1];
3037 reqr = conr = sysr = jobr = 0;
3038 findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr);
3051 printprobleminfo(Solver *solv, Queue *job, Id problem)
3053 Pool *pool = solv->pool;
3055 Id dep, source, target;
3058 probr = solver_findproblemrule(solv, problem);
3059 switch (solver_problemruleinfo(solv, job, probr, &dep, &source, &target))
3061 case SOLVER_PROBLEM_UPDATE_RULE:
3062 s = pool_id2solvable(pool, source);
3063 POOL_DEBUG(SAT_DEBUG_RESULT, "problem with installed package %s\n", solvable2str(pool, s));
3065 case SOLVER_PROBLEM_JOB_RULE:
3066 POOL_DEBUG(SAT_DEBUG_RESULT, "conflicting requests\n");
3068 case SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP:
3069 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides requested %s\n", dep2str(pool, dep));
3071 case SOLVER_PROBLEM_NOT_INSTALLABLE:
3072 s = pool_id2solvable(pool, source);
3073 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s is not installable\n", solvable2str(pool, s));
3075 case SOLVER_PROBLEM_NOTHING_PROVIDES_DEP:
3076 s = pool_id2solvable(pool, source);
3077 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides %s needed by %s\n", dep2str(pool, dep), solvable2str(pool, s));
3079 case SOLVER_PROBLEM_SAME_NAME:
3080 s = pool_id2solvable(pool, source);
3081 s2 = pool_id2solvable(pool, target);
3082 POOL_DEBUG(SAT_DEBUG_RESULT, "cannot install both %s and %s\n", solvable2str(pool, s), solvable2str(pool, s2));
3084 case SOLVER_PROBLEM_PACKAGE_CONFLICT:
3085 s = pool_id2solvable(pool, source);
3086 s2 = pool_id2solvable(pool, target);
3087 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s conflicts with %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3089 case SOLVER_PROBLEM_PACKAGE_OBSOLETES:
3090 s = pool_id2solvable(pool, source);
3091 s2 = pool_id2solvable(pool, target);
3092 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s obsoletes %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3094 case SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE:
3095 s = pool_id2solvable(pool, source);
3096 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s requires %s, but none of the providers can be installed\n", solvable2str(pool, s), dep2str(pool, dep));
3102 printsolutions(Solver *solv, Queue *job)
3104 Pool *pool = solv->pool;
3107 Id problem, solution, element;
3110 POOL_DEBUG(SAT_DEBUG_RESULT, "Encountered problems! Here are the solutions:\n\n");
3113 while ((problem = solver_next_problem(solv, problem)) != 0)
3115 POOL_DEBUG(SAT_DEBUG_RESULT, "Problem %d:\n", pcnt++);
3116 POOL_DEBUG(SAT_DEBUG_RESULT, "====================================\n");
3117 printprobleminfo(solv, job, problem);
3118 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3120 while ((solution = solver_next_solution(solv, problem, solution)) != 0)
3123 while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
3127 /* job, rp is index into job queue */
3128 what = job->elements[rp];
3129 switch (job->elements[rp - 1])
3131 case SOLVER_INSTALL_SOLVABLE:
3132 s = pool->solvables + what;
3133 if (solv->installed && s->repo == solv->installed)
3134 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not keep %s installed\n", solvable2str(pool, s));
3136 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", solvable2str(pool, s));
3138 case SOLVER_ERASE_SOLVABLE:
3139 s = pool->solvables + what;
3140 if (solv->installed && s->repo == solv->installed)
3141 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", solvable2str(pool, s));
3143 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not forbid installation of %s\n", solvable2str(pool, s));
3145 case SOLVER_INSTALL_SOLVABLE_NAME:
3146 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", id2str(pool, what));
3148 case SOLVER_ERASE_SOLVABLE_NAME:
3149 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", id2str(pool, what));
3151 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3152 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install a solvable providing %s\n", dep2str(pool, what));
3154 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3155 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall all solvables providing %s\n", dep2str(pool, what));
3157 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3158 s = pool->solvables + what;
3159 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install most recent version of %s\n", solvable2str(pool, s));
3162 POOL_DEBUG(SAT_DEBUG_RESULT, "- do something different\n");
3168 /* policy, replace p with rp */
3169 s = pool->solvables + p;
3170 sd = rp ? pool->solvables + rp : 0;
3174 if (!solv->allowdowngrade && evrcmp(pool, s->evr, sd->evr, EVRCMP_MATCH_RELEASE) > 0)
3176 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow downgrade of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3179 if (!solv->allowarchchange && s->name == sd->name && s->arch != sd->arch && policy_illegal_archchange(pool, s, sd))
3181 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow architecture change of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3184 if (!solv->allowvendorchange && s->name == sd->name && s->vendor != sd->vendor && policy_illegal_vendorchange(pool, s, sd))
3187 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));
3189 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));
3193 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow replacement of %s with %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3197 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow deinstallation of %s\n", solvable2str(pool, s));
3202 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3208 /* for each installed solvable find which packages with *different* names
3209 * obsolete the solvable.
3210 * this index is used in policy_findupdatepackages if noupdateprovide is set.
3214 create_obsolete_index(Solver *solv)
3216 Pool *pool = solv->pool;
3218 Repo *installed = solv->installed;
3219 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
3222 if (!installed || !installed->nsolvables)
3224 /* create reverse obsoletes map for installed solvables */
3225 solv->obsoletes = obsoletes = xcalloc(installed->end - installed->start, sizeof(Id));
3226 for (i = 1; i < pool->nsolvables; i++)
3228 s = pool->solvables + i;
3229 if (s->repo == installed)
3233 if (!pool_installable(pool, s))
3235 obsp = s->repo->idarraydata + s->obsoletes;
3236 while ((obs = *obsp++) != 0)
3237 FOR_PROVIDES(p, pp, obs)
3239 if (pool->solvables[p].repo != installed)
3241 if (pool->solvables[p].name == s->name)
3243 obsoletes[p - installed->start]++;
3247 for (i = 0; i < installed->nsolvables; i++)
3250 n += obsoletes[i] + 1;
3253 solv->obsoletes_data = obsoletes_data = xcalloc(n + 1, sizeof(Id));
3254 POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
3255 for (i = pool->nsolvables - 1; i > 0; i--)
3257 s = pool->solvables + i;
3258 if (s->repo == installed)
3262 if (!pool_installable(pool, s))
3264 obsp = s->repo->idarraydata + s->obsoletes;
3265 while ((obs = *obsp++) != 0)
3266 FOR_PROVIDES(p, pp, obs)
3268 if (pool->solvables[p].repo != installed)
3270 if (pool->solvables[p].name == s->name)
3272 p -= installed->start;
3273 if (obsoletes_data[obsoletes[p]] != i)
3274 obsoletes_data[--obsoletes[p]] = i;
3280 /*-----------------------------------------------------------------*/
3290 solver_solve(Solver *solv, Queue *job)
3292 Pool *pool = solv->pool;
3293 Repo *installed = solv->installed;
3296 Map addedmap; /* '1' == have rpm-rules for solvable */
3297 Id how, what, p, *pp, d;
3301 /* create whatprovides if not already there */
3302 if (!pool->whatprovides)
3303 pool_createwhatprovides(pool);
3305 /* create obsolete index if needed */
3306 if (solv->noupdateprovide)
3307 create_obsolete_index(solv);
3310 * create basic rule set of all involved packages
3311 * use addedmap bitmap to make sure we don't create rules twice
3315 map_init(&addedmap, pool->nsolvables);
3319 * always install our system solvable
3321 MAPSET(&addedmap, SYSTEMSOLVABLE);
3322 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
3323 queue_push(&solv->decisionq_why, 0);
3324 solv->decisionmap[SYSTEMSOLVABLE] = 1;
3327 * create rules for all package that could be involved with the solving
3328 * so called: rpm rules
3333 oldnrules = solv->nrules;
3334 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
3335 FOR_REPO_SOLVABLES(installed, p, s)
3336 addrpmrulesforsolvable(solv, s, &addedmap);
3337 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3338 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
3339 oldnrules = solv->nrules;
3340 FOR_REPO_SOLVABLES(installed, p, s)
3341 addrpmrulesforupdaters(solv, s, &addedmap, 1);
3342 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3345 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
3346 oldnrules = solv->nrules;
3347 for (i = 0; i < job->count; i += 2)
3349 how = job->elements[i];
3350 what = job->elements[i + 1];
3354 case SOLVER_INSTALL_SOLVABLE:
3355 addrpmrulesforsolvable(solv, pool->solvables + what, &addedmap);
3357 case SOLVER_INSTALL_SOLVABLE_NAME:
3358 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3359 FOR_PROVIDES(p, pp, what)
3361 /* if by name, ensure that the name matches */
3362 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
3364 addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3367 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3368 /* dont allow downgrade */
3369 addrpmrulesforupdaters(solv, pool->solvables + what, &addedmap, 0);
3373 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3375 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for recommended/suggested packages ***\n");
3377 oldnrules = solv->nrules;
3378 addrpmrulesforweak(solv, &addedmap);
3379 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3381 IF_POOLDEBUG (SAT_DEBUG_STATS)
3383 int possible = 0, installable = 0;
3384 for (i = 1; i < pool->nsolvables; i++)
3386 if (pool_installable(pool, pool->solvables + i))
3388 if (MAPTST(&addedmap, i))
3391 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
3395 * first pass done, we now have all the rpm rules we need.
3396 * unify existing rules before going over all job rules and
3398 * at this point the system is always solvable,
3399 * as an empty system (remove all packages) is a valid solution
3402 unifyrules(solv); /* remove duplicate rpm rules */
3404 POOL_DEBUG(SAT_DEBUG_STATS, "decisions so far: %d\n", solv->decisionq.count);
3405 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
3406 printdecisions (solv);
3409 * now add all job rules
3412 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
3414 solv->jobrules = solv->nrules;
3416 for (i = 0; i < job->count; i += 2)
3418 int oldnrules = solv->nrules;
3420 how = job->elements[i];
3421 what = job->elements[i + 1];
3424 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
3425 s = pool->solvables + what;
3426 POOL_DEBUG(SAT_DEBUG_JOB, "job: install solvable %s\n", solvable2str(pool, s));
3427 addrule(solv, what, 0); /* install by Id */
3428 queue_push(&solv->ruletojob, i);
3430 case SOLVER_ERASE_SOLVABLE:
3431 s = pool->solvables + what;
3432 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase solvable %s\n", solvable2str(pool, s));
3433 addrule(solv, -what, 0); /* remove by Id */
3434 queue_push(&solv->ruletojob, i);
3436 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
3437 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3438 if (how == SOLVER_INSTALL_SOLVABLE_NAME)
3439 POOL_DEBUG(SAT_DEBUG_JOB, "job: install name %s\n", id2str(pool, what));
3440 if (how == SOLVER_INSTALL_SOLVABLE_PROVIDES)
3441 POOL_DEBUG(SAT_DEBUG_JOB, "job: install provides %s\n", dep2str(pool, what));
3443 FOR_PROVIDES(p, pp, what)
3445 /* if by name, ensure that the name matches */
3446 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
3452 /* no provider, make this an impossible rule */
3453 queue_push(&q, -SYSTEMSOLVABLE);
3456 p = queue_shift(&q); /* get first provider */
3458 d = 0; /* single provider ? -> make assertion */
3460 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
3461 addrule(solv, p, d); /* add 'requires' rule */
3462 queue_push(&solv->ruletojob, i);
3464 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
3465 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3466 if (how == SOLVER_ERASE_SOLVABLE_NAME)
3467 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase name %s\n", id2str(pool, what));
3468 if (how == SOLVER_ERASE_SOLVABLE_PROVIDES)
3469 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase provides %s\n", dep2str(pool, what));
3470 FOR_PROVIDES(p, pp, what)
3472 /* if by name, ensure that the name matches */
3473 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
3475 addrule(solv, -p, 0); /* add 'remove' rule */
3476 queue_push(&solv->ruletojob, i);
3479 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
3480 s = pool->solvables + what;
3481 POOL_DEBUG(SAT_DEBUG_JOB, "job: update %s\n", solvable2str(pool, s));
3482 addupdaterule(solv, s, 0);
3483 queue_push(&solv->ruletojob, i);
3486 IF_POOLDEBUG (SAT_DEBUG_JOB)
3489 if (solv->nrules == oldnrules)
3490 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created");
3491 for (j = oldnrules; j < solv->nrules; j++)
3493 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
3494 printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
3499 if (solv->ruletojob.count != solv->nrules - solv->jobrules)
3503 * now add system rules
3507 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add system rules ***\n");
3510 solv->systemrules = solv->nrules;
3513 * create rules for updating installed solvables
3517 if (installed && !solv->allowuninstall)
3518 { /* loop over all installed solvables */
3519 /* we create all update rules, but disable some later on depending on the job */
3520 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3521 if (s->repo == installed)
3522 addupdaterule(solv, s, 0); /* allowall = 0 */
3524 addupdaterule(solv, 0, 0); /* create dummy rule; allowall = 0 */
3525 /* consistency check: we added a rule for _every_ system solvable */
3526 if (solv->nrules - solv->systemrules != installed->end - installed->start)
3530 /* create special weak system rules */
3531 /* those are used later on to keep a version of the installed packages in
3533 if (installed && installed->nsolvables)
3535 solv->weaksystemrules = xcalloc(installed->end - installed->start, sizeof(Id));
3536 FOR_REPO_SOLVABLES(installed, p, s)
3538 policy_findupdatepackages(solv, s, &q, 1);
3540 solv->weaksystemrules[p - installed->start] = pool_queuetowhatprovides(pool, &q);
3544 /* free unneeded memory */
3545 map_free(&addedmap);
3548 solv->weakrules = solv->nrules;
3550 /* try real hard to keep packages installed */
3553 FOR_REPO_SOLVABLES(installed, p, s)
3555 /* FIXME: can't work with refine_suggestion! */
3556 /* need to always add the rule but disable it */
3557 if (MAPTST(&solv->noupdate, p - installed->start))
3559 d = solv->weaksystemrules[p - installed->start];
3560 addrule(solv, p, d);
3564 /* all new rules are learnt after this point */
3565 solv->learntrules = solv->nrules;
3572 disableupdaterules(solv, job, -1);
3573 makeruledecisions(solv);
3575 POOL_DEBUG(SAT_DEBUG_STATS, "problems so far: %d\n", solv->problems.count);
3577 run_solver(solv, 1, 1);
3579 /* find suggested packages */
3580 if (!solv->problems.count)
3582 Id sug, *sugp, p, *pp;
3584 /* create map of all suggests that are still open */
3585 solv->recommends_index = -1;
3586 MAPZERO(&solv->suggestsmap);
3587 for (i = 0; i < solv->decisionq.count; i++)
3589 p = solv->decisionq.elements[i];
3592 s = pool->solvables + p;
3595 sugp = s->repo->idarraydata + s->suggests;
3596 while ((sug = *sugp++) != 0)
3598 FOR_PROVIDES(p, pp, sug)
3599 if (solv->decisionmap[p] > 0)
3602 continue; /* already fulfilled */
3603 FOR_PROVIDES(p, pp, sug)
3604 MAPSET(&solv->suggestsmap, p);
3608 for (i = 1; i < pool->nsolvables; i++)
3610 if (solv->decisionmap[i] != 0)
3612 s = pool->solvables + i;
3613 if (!MAPTST(&solv->suggestsmap, i))
3617 if (!pool_installable(pool, s))
3619 if (!solver_is_enhancing(solv, s))
3622 queue_push(&solv->suggestions, i);
3624 policy_filter_unwanted(solv, &solv->suggestions, 0, POLICY_MODE_SUGGEST);
3627 if (solv->problems.count)
3628 problems_to_solutions(solv, job);