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
29 #define RULES_BLOCK 63
30 #define REGARD_RECOMMENDS_OF_INSTALLED_ITEMS 1
34 solver_splitprovides(Solver *solv, Id dep)
36 Pool *pool = solv->pool;
41 if (!solv->dosplitprovides || !solv->installed)
45 rd = GETRELDEP(pool, dep);
46 if (rd->flags != REL_WITH)
48 FOR_PROVIDES(p, pp, dep)
50 s = pool->solvables + p;
51 if (s->repo == solv->installed && s->name == rd->name)
58 solver_dep_installed(Solver *solv, Id dep)
61 Pool *pool = solv->pool;
66 Reldep *rd = GETRELDEP(pool, dep);
67 if (rd->flags == REL_AND)
69 if (!solver_dep_installed(solv, rd->name))
71 return solver_dep_installed(solv, rd->evr);
73 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
74 return solver_dep_installed(solv, rd->evr);
76 FOR_PROVIDES(p, pp, dep)
78 if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
86 /* this mirrors solver_dep_fulfilled but uses map m instead of the decisionmap */
88 dep_possible(Solver *solv, Id dep, Map *m)
90 Pool *pool = solv->pool;
95 Reldep *rd = GETRELDEP(pool, dep);
96 if (rd->flags == REL_AND)
98 if (!dep_possible(solv, rd->name, m))
100 return dep_possible(solv, rd->evr, m);
102 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
103 return solver_splitprovides(solv, rd->evr);
104 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
105 return solver_dep_installed(solv, rd->evr);
107 FOR_PROVIDES(p, pp, dep)
115 /*-----------------------------------------------------------------*/
122 printruleelement(Solver *solv, int type, Rule *r, Id v)
124 Pool *pool = solv->pool;
128 s = pool->solvables + -v;
129 POOL_DEBUG(type, " !%s [%d]", solvable2str(pool, s), -v);
133 s = pool->solvables + v;
134 POOL_DEBUG(type, " %s [%d]", solvable2str(pool, s), v);
139 POOL_DEBUG(type, " (w1)");
141 POOL_DEBUG(type, " (w2)");
143 if (solv->decisionmap[s - pool->solvables] > 0)
144 POOL_DEBUG(type, " Install.level%d", solv->decisionmap[s - pool->solvables]);
145 if (solv->decisionmap[s - pool->solvables] < 0)
146 POOL_DEBUG(type, " Conflict.level%d", -solv->decisionmap[s - pool->solvables]);
147 POOL_DEBUG(type, "\n");
156 printrule(Solver *solv, int type, Rule *r)
158 Pool *pool = solv->pool;
162 if (r >= solv->rules && r < solv->rules + solv->nrules) /* r is a solver rule */
163 POOL_DEBUG(type, "Rule #%d:", (int)(r - solv->rules));
165 POOL_DEBUG(type, "Rule:"); /* r is any rule */
167 POOL_DEBUG(type, " (disabled)");
168 POOL_DEBUG(type, "\n");
172 /* print direct literal */
174 else if (r->d == ID_NULL)
178 /* binary rule --> print w2 as second literal */
182 /* every other which is in d */
183 v = solv->pool->whatprovidesdata[r->d + i - 1];
186 printruleelement(solv, type, r, v);
188 POOL_DEBUG(type, " next rules: %d %d\n", r->n1, r->n2);
192 printruleclass(Solver *solv, int type, Rule *r)
194 Pool *pool = solv->pool;
195 Id p = r - solv->rules;
197 if (p < solv->learntrules)
198 if (MAPTST(&solv->weakrulemap, p))
199 POOL_DEBUG(type, "WEAK ");
200 if (p >= solv->learntrules)
201 POOL_DEBUG(type, "LEARNT ");
202 else if (p >= solv->systemrules)
203 POOL_DEBUG(type, "SYSTEM ");
204 else if (p >= solv->jobrules)
205 POOL_DEBUG(type, "JOB ");
206 printrule(solv, type, r);
210 /*-----------------------------------------------------------------*/
216 static Pool *unifyrules_sortcmp_data;
219 * compare rules for unification sort
223 unifyrules_sortcmp(const void *ap, const void *bp)
225 Pool *pool = unifyrules_sortcmp_data;
226 Rule *a = (Rule *)ap;
227 Rule *b = (Rule *)bp;
233 return x; /* p differs */
236 if (a->d == 0 && b->d == 0)
237 return a->w2 - b->w2; /* assertion: return w2 diff */
239 if (a->d == 0) /* a is assertion, b not */
241 x = a->w2 - pool->whatprovidesdata[b->d];
245 if (b->d == 0) /* b is assertion, a not */
247 x = pool->whatprovidesdata[a->d] - b->w2;
251 /* compare whatprovidesdata */
252 ad = pool->whatprovidesdata + a->d;
253 bd = pool->whatprovidesdata + b->d;
255 if ((x = *ad++ - *bd++) != 0)
266 unifyrules(Solver *solv)
268 Pool *pool = solv->pool;
272 if (solv->nrules <= 1) /* nothing to unify */
275 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules -----\n");
277 /* sort rules first */
278 unifyrules_sortcmp_data = solv->pool;
279 qsort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp);
286 for (i = j = 1, ir = solv->rules + 1; i < solv->nrules; i++, ir++)
288 if (jr && !unifyrules_sortcmp(ir, jr))
289 continue; /* prune! */
290 jr = solv->rules + j++; /* keep! */
295 /* reduced count from nrules to j rules */
296 POOL_DEBUG(SAT_DEBUG_STATS, "pruned rules from %d to %d\n", solv->nrules, j);
298 /* adapt rule buffer */
300 solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
301 IF_POOLDEBUG (SAT_DEBUG_STATS)
308 for (i = 1; i < solv->nrules; i++)
315 dp = solv->pool->whatprovidesdata + r->d;
320 POOL_DEBUG(SAT_DEBUG_STATS, " binary: %d\n", binr);
321 POOL_DEBUG(SAT_DEBUG_STATS, " normal: %d, %d literals\n", solv->nrules - 1 - binr, lits);
323 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules end -----\n");
333 hashrule(Solver *solv, Id p, Id d, int n)
335 unsigned int x = (unsigned int)p;
339 return (x * 37) ^ (unsigned int)d;
340 dp = solv->pool->whatprovidesdata + d;
342 x = (x * 37) ^ (unsigned int)*dp++;
350 * p = direct literal; always < 0 for installed rpm rules
351 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
354 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
356 * p < 0 : pkg id of A
357 * d > 0 : Offset in whatprovidesdata (list of providers of b)
359 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
360 * p < 0 : pkg id of A
361 * d < 0 : Id of solvable (e.g. B1)
363 * d == 0: unary rule, assertion => (A) or (-A)
365 * Install: p > 0, d = 0 (A) user requested install
366 * Remove: p < 0, d = 0 (-A) user requested remove
367 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
368 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
369 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider) (binary rule)
370 * ? p > 0, d < 0 (A|-B)
371 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
375 * Direct assertion (no watch needed)( if d <0 ) --> d = 0, w1 = p, w2 = 0
376 * Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
377 * every other : w1 = p, w2 = whatprovidesdata[d];
378 * Disabled rule: w1 = 0
380 * always returns a rule for non-rpm rules
384 addrule(Solver *solv, Id p, Id d)
386 Pool *pool = solv->pool;
390 int n = 0; /* number of literals in rule - 1
391 0 = direct assertion (single literal)
395 /* it often happenes that requires lead to adding the same rpm rule
396 * multiple times, so we prune those duplicates right away to make
397 * the work for unifyrules a bit easier */
399 if (solv->nrules && !solv->jobrules)
401 r = solv->rules + solv->nrules - 1; /* get the last added rule */
402 if (r->p == p && r->d == d && d != 0) /* identical and not user requested */
408 /* always a binary rule */
410 return 0; /* ignore self conflict */
415 for (dp = pool->whatprovidesdata + d; *dp; dp++, n++)
417 return 0; /* rule is self-fulfilling */
419 d = dp[-1]; /* take single literal */
423 if (n == 0 && !solv->jobrules)
425 /* this is a rpm rule assertion, we do not have to allocate it */
426 /* it can be identified by a level of 1 and a zero reason */
427 /* we must not drop those rules from the decisionq when rewinding! */
429 assert(solv->decisionmap[-p] == 0 || solv->decisionmap[-p] == -1);
430 if (solv->decisionmap[-p])
431 return 0; /* already got that one */
432 queue_push(&solv->decisionq, p);
433 queue_push(&solv->decisionq_why, 0);
434 solv->decisionmap[-p] = -1;
441 /* smallest literal first so we can find dups */
445 n = 1; /* re-set n, was used as temp var */
448 /* check if the last added rule is exactly the same as what we're looking for. */
449 if (r && n == 1 && !r->d && r->p == p && r->w2 == d)
450 return r; /* binary rule */
452 if (r && n > 1 && r->d && r->p == p)
454 /* Rule where d is an offset in whatprovidesdata */
458 dp2 = pool->whatprovidesdata + r->d;
459 for (dp = pool->whatprovidesdata + d; *dp; dp++, dp2++)
470 /* extend rule buffer */
471 solv->rules = sat_extend(solv->rules, solv->nrules, 1, sizeof(Rule), RULES_BLOCK);
472 r = solv->rules + solv->nrules++; /* point to rule space */
477 /* direct assertion, no watch needed */
493 r->w2 = pool->whatprovidesdata[d];
498 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
500 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " Add rule: ");
501 printrule(solv, SAT_DEBUG_RULE_CREATION, r);
508 disablerule(Solver *solv, Rule *r)
514 enablerule(Solver *solv, Rule *r)
516 if (r->d == 0 || r->w2 != r->p)
519 r->w1 = solv->pool->whatprovidesdata[r->d];
523 /**********************************************************************************/
525 /* a problem is an item on the solver's problem list. It can either be >0, in that
526 * case it is a system (upgrade) rule, or it can be <0, which makes it refer to a job
527 * consisting of multiple job rules.
531 disableproblem(Solver *solv, Id v)
539 disablerule(solv, solv->rules + v);
543 jp = solv->ruletojob.elements;
544 for (i = solv->jobrules, r = solv->rules + i; i < solv->systemrules; i++, r++, jp++)
546 disablerule(solv, r);
550 enableproblem(Solver *solv, Id v)
558 enablerule(solv, solv->rules + v);
562 jp = solv->ruletojob.elements;
563 for (i = solv->jobrules, r = solv->rules + i; i < solv->systemrules; i++, r++, jp++)
569 printproblem(Solver *solv, Id v)
571 Pool *pool = solv->pool;
577 printrule(solv, SAT_DEBUG_SOLUTIONS, solv->rules + v);
581 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "JOB %d\n", v);
582 jp = solv->ruletojob.elements;
583 for (i = solv->jobrules, r = solv->rules + i; i < solv->systemrules; i++, r++, jp++)
586 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "- ");
587 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
589 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "ENDJOB\n");
595 /************************************************************************/
597 /* go through system and job rules and add direct assertions
598 * to the decisionqueue. If we find a conflict, disable rules and
599 * add them to problem queue.
602 makeruledecisions(Solver *solv)
604 Pool *pool = solv->pool;
610 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions ; size decisionq: %d -----\n",solv->decisionq.count);
612 decisionstart = solv->decisionq.count;
613 /* rpm rules don't have assertions, so we can start with the job
614 * rules (rpm assertions are not resulting in a rule, but cause a
615 * immediate decision) */
616 /* nowadays they can be weak, so start with rule 1 */
617 for (ri = 1, r = solv->rules + ri; ri < solv->nrules; ri++, r++)
619 if (!r->w1 || r->w2) /* disabled or no assertion */
621 /* do weak rules in phase 2 */
622 if (ri < solv->learntrules && MAPTST(&solv->weakrulemap, ri))
626 if (!solv->decisionmap[vv])
628 queue_push(&solv->decisionq, v);
629 queue_push(&solv->decisionq_why, r - solv->rules);
630 solv->decisionmap[vv] = v > 0 ? 1 : -1;
631 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
633 Solvable *s = solv->pool->solvables + vv;
635 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", solvable2str(solv->pool, s));
637 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (assertion)\n", solvable2str(solv->pool, s));
641 if (v > 0 && solv->decisionmap[vv] > 0)
643 if (v < 0 && solv->decisionmap[vv] < 0)
645 /* found a conflict! */
646 if (ri >= solv->learntrules)
648 /* conflict with a learnt rule */
649 /* can happen when packages cannot be installed for
650 * multiple reasons. */
651 /* we disable the learnt rule in this case */
652 disablerule(solv, r);
655 /* only job and system rules left in the decisionq */
656 /* find the decision which is the "opposite" of the jobrule */
657 for (i = 0; i < solv->decisionq.count; i++)
658 if (solv->decisionq.elements[i] == -v)
660 assert(i < solv->decisionq.count);
661 if (v == -SYSTEMSOLVABLE) {
662 /* conflict with system solvable */
663 queue_push(&solv->problems, solv->learnt_pool.count);
664 queue_push(&solv->learnt_pool, ri);
665 queue_push(&solv->learnt_pool, 0);
666 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
667 if (ri < solv->systemrules)
668 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
671 queue_push(&solv->problems, v);
672 queue_push(&solv->problems, 0);
673 disableproblem(solv, v);
676 assert(solv->decisionq_why.elements[i]);
679 if (solv->decisionq_why.elements[i] == 0)
681 /* conflict with rpm rule, need only disable our rule */
682 assert(v > 0 || v == -SYSTEMSOLVABLE);
684 queue_push(&solv->problems, solv->learnt_pool.count);
685 queue_push(&solv->learnt_pool, ri);
686 queue_push(&solv->learnt_pool, v != -SYSTEMSOLVABLE ? -v : v);
687 queue_push(&solv->learnt_pool, 0);
688 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
689 if (ri < solv->systemrules)
690 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
693 queue_push(&solv->problems, v);
694 queue_push(&solv->problems, 0);
695 disableproblem(solv, v);
699 if (solv->decisionq_why.elements[i] < solv->jobrules)
701 /* conflict with rpm rule assertion */
702 queue_push(&solv->problems, solv->learnt_pool.count);
703 queue_push(&solv->learnt_pool, ri);
704 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
705 queue_push(&solv->learnt_pool, 0);
706 assert(v > 0 || v == -SYSTEMSOLVABLE);
707 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
708 if (ri < solv->systemrules)
709 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
712 queue_push(&solv->problems, v);
713 queue_push(&solv->problems, 0);
714 disableproblem(solv, v);
718 /* conflict with another job or system rule */
720 queue_push(&solv->problems, solv->learnt_pool.count);
721 queue_push(&solv->learnt_pool, ri);
722 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
723 queue_push(&solv->learnt_pool, 0);
725 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflicting system/job assertions over literal %d\n", vv);
727 /* push all of our rules asserting this literal on the problem stack */
728 for (i = solv->jobrules, rr = solv->rules + i; i < solv->nrules; i++, rr++)
730 if (!rr->w1 || rr->w2)
732 if (rr->p != vv && rr->p != -vv)
734 if (i >= solv->learntrules)
736 if (MAPTST(&solv->weakrulemap, i))
738 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
740 if (i < solv->systemrules)
741 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
742 queue_push(&solv->problems, v);
743 disableproblem(solv, v);
745 queue_push(&solv->problems, 0);
748 while (solv->decisionq.count > decisionstart)
750 v = solv->decisionq.elements[--solv->decisionq.count];
751 --solv->decisionq_why.count;
753 solv->decisionmap[vv] = 0;
755 ri = solv->jobrules - 1;
756 r = solv->rules + ri;
759 /* phase 2: now do the weak assertions */
760 for (ri = 1, r = solv->rules + ri; ri < solv->learntrules; ri++, r++)
762 if (!r->w1 || r->w2) /* disabled or no assertion */
764 if (!MAPTST(&solv->weakrulemap, ri))
768 if (!solv->decisionmap[vv])
770 queue_push(&solv->decisionq, v);
771 queue_push(&solv->decisionq_why, r - solv->rules);
772 solv->decisionmap[vv] = v > 0 ? 1 : -1;
773 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
775 Solvable *s = solv->pool->solvables + vv;
777 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", solvable2str(solv->pool, s));
779 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (weak assertion)\n", solvable2str(solv->pool, s));
783 if (v > 0 && solv->decisionmap[vv] > 0)
785 if (v < 0 && solv->decisionmap[vv] < 0)
787 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
788 printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
789 disablerule(solv, r);
792 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions end; size decisionq: %d -----\n",solv->decisionq.count);
796 * we have enabled or disabled some of our rules. We now reenable all
797 * of our learnt rules but the ones that were learnt from rules that
801 enabledisablelearntrules(Solver *solv)
803 Pool *pool = solv->pool;
808 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
809 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
811 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
812 while ((why = *whyp++) != 0)
816 continue; /* rpm assertion */
819 if (!solv->rules[why].w1)
822 /* why != 0: we found a disabled rule, disable the learnt rule */
825 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
827 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "disabling ");
828 printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
830 disablerule(solv, r);
832 else if (!why && !r->w1)
834 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
836 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "re-enabling ");
837 printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
845 /* FIXME: bad code ahead, replace as soon as possible */
847 disableupdaterules(Solver *solv, Queue *job, int jobidx)
849 Pool *pool = solv->pool;
851 Id name, how, what, p, *pp;
857 installed = solv->installed;
863 how = job->elements[jobidx] & ~SOLVER_WEAK;
866 case SOLVER_INSTALL_SOLVABLE:
867 case SOLVER_ERASE_SOLVABLE:
868 case SOLVER_ERASE_SOLVABLE_NAME:
869 case SOLVER_ERASE_SOLVABLE_PROVIDES:
875 /* go through all enabled job rules */
876 MAPZERO(&solv->noupdate);
877 for (i = solv->jobrules; i < solv->systemrules; i++)
880 if (!r->w1) /* disabled? */
882 j = solv->ruletojob.elements[i - solv->jobrules];
886 how = job->elements[j] & ~SOLVER_WEAK;
887 what = job->elements[j + 1];
890 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
891 s = pool->solvables + what;
892 FOR_PROVIDES(p, pp, s->name)
894 if (pool->solvables[p].name != s->name)
896 if (pool->solvables[p].repo == installed)
897 MAPSET(&solv->noupdate, p - installed->start);
900 case SOLVER_ERASE_SOLVABLE:
901 s = pool->solvables + what;
902 if (s->repo == installed)
903 MAPSET(&solv->noupdate, what - installed->start);
905 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
906 case SOLVER_ERASE_SOLVABLE_PROVIDES:
907 name = (how == SOLVER_ERASE_SOLVABLE_NAME) ? what : 0;
908 while (ISRELDEP(name))
910 Reldep *rd = GETRELDEP(pool, name);
913 FOR_PROVIDES(p, pp, what)
915 if (name && pool->solvables[p].name != name)
917 if (pool->solvables[p].repo == installed)
918 MAPSET(&solv->noupdate, p - installed->start);
926 /* fixup update rule status */
927 if (solv->allowuninstall)
928 return; /* no update rules at all */
932 /* we just disabled job #jobidx. enable all update rules
933 * that aren't disabled by the remaining job rules */
934 how = job->elements[jobidx] & ~SOLVER_WEAK;
935 what = job->elements[jobidx + 1];
938 case SOLVER_INSTALL_SOLVABLE:
939 s = pool->solvables + what;
940 FOR_PROVIDES(p, pp, s->name)
942 if (pool->solvables[p].name != s->name)
944 if (pool->solvables[p].repo != installed)
946 if (MAPTST(&solv->noupdate, p - installed->start))
948 r = solv->rules + solv->systemrules + (p - installed->start);
952 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
954 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
955 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
959 case SOLVER_ERASE_SOLVABLE:
960 s = pool->solvables + what;
961 if (s->repo != installed)
963 if (MAPTST(&solv->noupdate, what - installed->start))
965 r = solv->rules + solv->systemrules + (what - installed->start);
969 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
971 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
972 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
975 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
976 case SOLVER_ERASE_SOLVABLE_PROVIDES:
977 name = (how == SOLVER_ERASE_SOLVABLE_NAME) ? what : 0;
978 while (ISRELDEP(name))
980 Reldep *rd = GETRELDEP(pool, name);
983 FOR_PROVIDES(p, pp, what)
985 if (name && pool->solvables[p].name != name)
987 if (pool->solvables[p].repo != installed)
989 if (MAPTST(&solv->noupdate, p - installed->start))
991 r = solv->rules + solv->systemrules + (p - installed->start);
995 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
997 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
998 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
1008 for (i = 0; i < installed->nsolvables; i++)
1010 r = solv->rules + solv->systemrules + i;
1011 if (r->w1 && MAPTST(&solv->noupdate, i))
1012 disablerule(solv, r); /* was enabled, need to disable */
1018 addpatchatomrequires(Solver *solv, Solvable *s, Id *dp, Queue *q, Map *m)
1020 Pool *pool = solv->pool;
1021 Id fre, *frep, p, *pp, ndp;
1027 queue_init_buffer(&fq, qbuf, sizeof(qbuf)/sizeof(*qbuf));
1028 queue_push(&fq, -(s - pool->solvables));
1030 queue_push(&fq, *dp);
1031 ndp = pool_queuetowhatprovides(pool, &fq);
1032 frep = s->repo->idarraydata + s->freshens;
1033 while ((fre = *frep++) != 0)
1035 FOR_PROVIDES(p, pp, fre)
1037 ps = pool->solvables + p;
1038 addrule(solv, -p, ndp);
1046 for (i = 1; i < fq.count; i++)
1058 * add (install) rules for solvable
1059 * for unfulfilled requirements, conflicts, obsoletes,....
1060 * add a negative assertion for solvables that are not installable
1064 addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
1066 Pool *pool = solv->pool;
1067 Repo *installed = solv->installed;
1082 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable -----\n");
1084 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
1085 queue_push(&q, s - pool->solvables); /* push solvable Id */
1091 * s: Pointer to solvable
1094 n = queue_shift(&q);
1095 if (MAPTST(m, n)) /* continue if already done */
1099 s = pool->solvables + n; /* s = Solvable in question */
1102 if (installed /* Installed system available */
1103 && !solv->fixsystem /* NOT repair errors in rpm dependency graph */
1104 && s->repo == installed) /* solvable is installed? */
1106 dontfix = 1; /* dont care about broken rpm deps */
1109 if (!dontfix && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
1111 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", solvable2str(pool, s), (Id)(s - pool->solvables));
1112 addrule(solv, -n, 0); /* uninstallable */
1116 if (s->freshens && !s->supplements)
1118 const char *name = id2str(pool, s->name);
1119 if (name[0] == 'a' && !strncmp(name, "atom:", 5))
1123 /*-----------------------------------------
1124 * check requires of s
1129 reqp = s->repo->idarraydata + s->requires;
1130 while ((req = *reqp++) != 0) /* go throw all requires */
1132 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
1135 dp = pool_whatprovides(pool, req);
1137 if (*dp == SYSTEMSOLVABLE) /* always installed */
1143 addpatchatomrequires(solv, s, dp, &q, m);
1149 /* the strategy here is to not insist on dependencies
1150 * that are already broken. so if we find one provider
1151 * that was already installed, we know that the
1152 * dependency was not broken before so we enforce it */
1153 for (i = 0; (p = dp[i]) != 0; i++) /* for all providers */
1155 if (pool->solvables[p].repo == installed)
1156 break; /* provider was installed */
1158 if (!p) /* previously broken dependency */
1160 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", dep2str(pool, req), solvable2str(pool, s));
1167 /* nothing provides req! */
1168 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", solvable2str(pool, s), (Id)(s - pool->solvables), dep2str(pool, req));
1169 addrule(solv, -n, 0); /* mark requestor as uninstallable */
1173 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1175 POOL_DEBUG(SAT_DEBUG_RULE_CREATION," %s requires %s\n", solvable2str(pool, s), dep2str(pool, req));
1176 for (i = 0; dp[i]; i++)
1177 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " provided by %s\n", solvable2str(pool, pool->solvables + dp[i]));
1180 /* add 'requires' dependency */
1181 /* rule: (-requestor|provider1|provider2|...|providerN) */
1182 addrule(solv, -n, dp - pool->whatprovidesdata);
1184 /* descend the dependency tree */
1185 for (; *dp; dp++) /* loop through all providers */
1187 if (!MAPTST(m, *dp))
1188 queue_push(&q, *dp);
1191 } /* while, requirements of n */
1193 } /* if, requirements */
1195 /* that's all we check for src packages */
1196 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
1199 /*-----------------------------------------
1200 * check conflicts of s
1205 conp = s->repo->idarraydata + s->conflicts;
1206 while ((con = *conp++) != 0)
1208 FOR_PROVIDES(p, pp, con)
1210 /* dontfix: dont care about conflicts with already installed packs */
1211 if (dontfix && pool->solvables[p].repo == installed)
1213 /* rule: -n|-p: either solvable _or_ provider of conflict */
1214 addrule(solv, -n, -p);
1219 /*-----------------------------------------
1220 * check obsoletes if not installed
1222 if (!installed || pool->solvables[n].repo != installed)
1223 { /* not installed */
1226 obsp = s->repo->idarraydata + s->obsoletes;
1227 while ((obs = *obsp++) != 0)
1229 FOR_PROVIDES(p, pp, obs)
1230 addrule(solv, -n, -p);
1233 FOR_PROVIDES(p, pp, s->name)
1235 if (s->name == pool->solvables[p].name)
1236 addrule(solv, -n, -p);
1240 /*-----------------------------------------
1241 * add recommends to the rule list
1245 recp = s->repo->idarraydata + s->recommends;
1246 while ((rec = *recp++) != 0)
1248 FOR_PROVIDES(p, pp, rec)
1255 sugp = s->repo->idarraydata + s->suggests;
1256 while ((sug = *sugp++) != 0)
1258 FOR_PROVIDES(p, pp, sug)
1265 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable end -----\n");
1269 addrpmrulesforweak(Solver *solv, Map *m)
1271 Pool *pool = solv->pool;
1276 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak -----\n");
1277 for (i = n = 1; n < pool->nsolvables; i++, n++)
1279 if (i == pool->nsolvables)
1283 s = pool->solvables + i;
1284 if (!pool_installable(pool, s))
1289 supp = s->repo->idarraydata + s->supplements;
1290 while ((sup = *supp++) != ID_NULL)
1291 if (dep_possible(solv, sup, m))
1294 if (!sup && s->freshens)
1296 supp = s->repo->idarraydata + s->freshens;
1297 while ((sup = *supp++) != ID_NULL)
1298 if (dep_possible(solv, sup, m))
1301 if (!sup && s->enhances)
1303 supp = s->repo->idarraydata + s->enhances;
1304 while ((sup = *supp++) != ID_NULL)
1305 if (dep_possible(solv, sup, m))
1310 addrpmrulesforsolvable(solv, s, m);
1313 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak end -----\n");
1317 addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allowall)
1319 Pool *pool = solv->pool;
1324 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1326 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1327 policy_findupdatepackages(solv, s, &qs, allowall);
1328 if (!MAPTST(m, s - pool->solvables)) /* add rule for s if not already done */
1329 addrpmrulesforsolvable(solv, s, m);
1330 for (i = 0; i < qs.count; i++)
1331 if (!MAPTST(m, qs.elements[i]))
1332 addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
1335 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1339 * add rule for update
1340 * (A|A1|A2|A3...) An = update candidates for A
1342 * s = (installed) solvable
1346 addupdaterule(Solver *solv, Solvable *s, int allowall)
1348 /* installed packages get a special upgrade allowed rule */
1349 Pool *pool = solv->pool;
1354 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule -----\n");
1356 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1357 policy_findupdatepackages(solv, s, &qs, allowall);
1358 if (qs.count == 0) /* no updaters found */
1361 d = pool_queuetowhatprovides(pool, &qs); /* intern computed queue */
1363 addrule(solv, s - pool->solvables, d); /* allow update of s */
1364 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule end -----\n");
1368 /*-----------------------------------------------------------------*/
1377 printWatches(Solver *solv, int type)
1379 Pool *pool = solv->pool;
1382 POOL_DEBUG(type, "Watches: \n");
1384 for (counter = -(pool->nsolvables); counter <= pool->nsolvables; counter ++)
1386 POOL_DEBUG(type, " solvable [%d] -- rule [%d]\n", counter, solv->watches[counter+pool->nsolvables]);
1393 * initial setup for all watches
1397 makewatches(Solver *solv)
1401 int nsolvables = solv->pool->nsolvables;
1403 sat_free(solv->watches);
1404 /* lower half for removals, upper half for installs */
1405 solv->watches = sat_calloc(2 * nsolvables, sizeof(Id));
1407 /* do it reverse so rpm rules get triggered first */
1408 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1410 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1413 if (!r->w1 /* rule is disabled */
1414 || !r->w2) /* rule is assertion */
1417 /* see addwatches(solv, r) */
1418 r->n1 = solv->watches[nsolvables + r->w1];
1419 solv->watches[nsolvables + r->w1] = r - solv->rules;
1421 r->n2 = solv->watches[nsolvables + r->w2];
1422 solv->watches[nsolvables + r->w2] = r - solv->rules;
1428 * add watches (for rule)
1432 addwatches(Solver *solv, Rule *r)
1434 int nsolvables = solv->pool->nsolvables;
1436 r->n1 = solv->watches[nsolvables + r->w1];
1437 solv->watches[nsolvables + r->w1] = r - solv->rules;
1439 r->n2 = solv->watches[nsolvables + r->w2];
1440 solv->watches[nsolvables + r->w2] = r - solv->rules;
1444 /*-----------------------------------------------------------------*/
1445 /* rule propagation */
1447 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1452 * propagate decision to all rules
1453 * return : 0 = everything is OK
1454 * watched rule = there is a conflict
1458 propagate(Solver *solv, int level)
1460 Pool *pool = solv->pool;
1465 Id *decisionmap = solv->decisionmap;
1466 Id *watches = solv->watches + pool->nsolvables;
1468 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
1470 while (solv->propagate_index < solv->decisionq.count)
1472 /* negate because our watches trigger if literal goes FALSE */
1473 pkg = -solv->decisionq.elements[solv->propagate_index++];
1474 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1476 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
1477 printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
1479 printWatches(solv, SAT_DEBUG_SCHUBI);
1482 for (rp = watches + pkg; *rp; rp = nrp)
1484 r = solv->rules + *rp;
1486 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1488 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
1489 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1494 ow = r->w2; /* regard the second watchpoint to come to a solution */
1499 ow = r->w1; /* regard the first watchpoint to come to a solution */
1502 /* if clause is TRUE, nothing to do */
1503 if (DECISIONMAP_TRUE(ow))
1508 /* not a binary clause, check if we need to move our watch */
1509 /* search for a literal that is not ow and not false */
1510 /* (true is also ok, in that case the rule is fulfilled) */
1511 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1514 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1515 if (p != ow && !DECISIONMAP_TRUE(-p))
1519 /* p is free to watch, move watch to p */
1520 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1523 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables + p));
1525 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables - p));
1539 watches[p] = r - solv->rules;
1543 /* unit clause found, set other watch to TRUE */
1544 if (DECISIONMAP_TRUE(-ow))
1545 return r; /* eek, a conflict! */
1546 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1548 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " unit ");
1549 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1552 decisionmap[ow] = level;
1554 decisionmap[-ow] = -level;
1555 queue_push(&solv->decisionq, ow);
1556 queue_push(&solv->decisionq_why, r - solv->rules);
1557 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1559 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1561 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", solvable2str(pool, s));
1563 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", solvable2str(pool, s));
1567 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
1569 return 0; /* all is well */
1573 /*-----------------------------------------------------------------*/
1582 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
1584 Pool *pool = solv->pool;
1587 Map seen; /* global? */
1590 int num = 0, l1num = 0;
1591 int learnt_why = solv->learnt_pool.count;
1592 Id *decisionmap = solv->decisionmap;
1596 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
1597 map_init(&seen, pool->nsolvables);
1598 idx = solv->decisionq.count;
1601 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1602 printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1603 queue_push(&solv->learnt_pool, c - solv->rules);
1604 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1605 /* go through all literals of the rule */
1617 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1619 vv = v > 0 ? v : -v;
1620 if (MAPTST(&seen, vv))
1622 l = solv->decisionmap[vv];
1627 /* a level 1 literal, mark it for later */
1628 MAPSET(&seen, vv); /* mark for scanning in level 1 phase */
1634 num++; /* need to do this one as well */
1645 v = solv->decisionq.elements[--idx];
1646 vv = v > 0 ? v : -v;
1647 if (MAPTST(&seen, vv))
1650 c = solv->rules + solv->decisionq_why.elements[idx];
1655 *pr = -v; /* so that v doesn't get lost */
1657 /* add involved level 1 rules */
1660 POOL_DEBUG(SAT_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
1664 v = solv->decisionq.elements[--idx];
1665 vv = v > 0 ? v : -v;
1666 if (!MAPTST(&seen, vv))
1668 why = solv->decisionq_why.elements[idx];
1671 queue_push(&solv->learnt_pool, -vv);
1672 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1674 POOL_DEBUG(SAT_DEBUG_ANALYZE, "RPM ASSERT Rule:\n");
1675 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, v);
1679 queue_push(&solv->learnt_pool, why);
1680 c = solv->rules + why;
1681 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1682 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1683 printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1694 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1696 vv = v > 0 ? v : -v;
1697 l = solv->decisionmap[vv];
1698 if (l != 1 && l != -1)
1708 else if (r.count == 1 && r.elements[0] < 0)
1709 *dr = r.elements[0];
1711 *dr = pool_queuetowhatprovides(pool, &r);
1712 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1714 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
1715 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, *pr);
1716 for (i = 0; i < r.count; i++)
1717 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
1719 /* push end marker on learnt reasons stack */
1720 queue_push(&solv->learnt_pool, 0);
1729 * reset the solver decisions to right after the rpm rules.
1730 * called after rules have been enabled/disabled
1734 reset_solver(Solver *solv)
1736 Pool *pool = solv->pool;
1740 /* rewind decisions to direct rpm rule assertions */
1741 for (i = solv->decisionq.count - 1; i >= solv->directdecisions; i--)
1743 v = solv->decisionq.elements[i];
1744 solv->decisionmap[v > 0 ? v : -v] = 0;
1747 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions done reduced from %d to %d\n", solv->decisionq.count, solv->directdecisions);
1749 solv->decisionq_why.count = solv->directdecisions;
1750 solv->decisionq.count = solv->directdecisions;
1751 solv->recommends_index = -1;
1752 solv->propagate_index = 0;
1754 /* adapt learnt rule status to new set of enabled/disabled rules */
1755 enabledisablelearntrules(solv);
1757 /* redo all job/system decisions */
1758 makeruledecisions(solv);
1759 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
1761 /* recreate watch chains */
1767 * analyze_unsolvable_rule
1771 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp)
1773 Pool *pool = solv->pool;
1775 Id why = r - solv->rules;
1777 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1778 printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1779 if (solv->learntrules && why >= solv->learntrules)
1781 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1782 if (solv->learnt_pool.elements[i] > 0)
1783 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp);
1786 if (MAPTST(&solv->weakrulemap, why))
1787 if (!*lastweakp || why > *lastweakp)
1789 /* do not add rpm rules to problem */
1790 if (why < solv->jobrules)
1792 /* turn rule into problem */
1793 if (why >= solv->jobrules && why < solv->systemrules)
1794 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1795 /* return if problem already countains our rule */
1796 if (solv->problems.count)
1798 for (i = solv->problems.count - 1; i >= 0; i--)
1799 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
1801 else if (solv->problems.elements[i] == why)
1804 queue_push(&solv->problems, why);
1809 * analyze_unsolvable
1811 * return: 1 - disabled some rules, try again
1816 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1818 Pool *pool = solv->pool;
1820 Map seen; /* global to speed things up? */
1823 Id *decisionmap = solv->decisionmap;
1824 int oldproblemcount;
1825 int oldlearntpoolcount;
1828 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
1829 oldproblemcount = solv->problems.count;
1830 oldlearntpoolcount = solv->learnt_pool.count;
1832 /* make room for proof index */
1833 /* must update it later, as analyze_unsolvable_rule would confuse
1834 * it with a rule index if we put the real value in already */
1835 queue_push(&solv->problems, 0);
1838 map_init(&seen, pool->nsolvables);
1839 queue_push(&solv->learnt_pool, r - solv->rules);
1841 analyze_unsolvable_rule(solv, r, &lastweak);
1842 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1853 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1855 vv = v > 0 ? v : -v;
1856 l = solv->decisionmap[vv];
1861 idx = solv->decisionq.count;
1864 v = solv->decisionq.elements[--idx];
1865 vv = v > 0 ? v : -v;
1866 if (!MAPTST(&seen, vv))
1868 why = solv->decisionq_why.elements[idx];
1873 /* level 1 and no why, must be an rpm assertion */
1874 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1876 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "RPM ");
1877 printruleelement(solv, SAT_DEBUG_UNSOLVABLE, 0, v);
1879 /* this is the only positive rpm assertion */
1880 if (v == SYSTEMSOLVABLE)
1883 queue_push(&solv->learnt_pool, v);
1887 queue_push(&solv->learnt_pool, why);
1888 r = solv->rules + why;
1889 analyze_unsolvable_rule(solv, r, &lastweak);
1890 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1901 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1903 vv = v > 0 ? v : -v;
1904 l = solv->decisionmap[vv];
1911 queue_push(&solv->problems, 0); /* mark end of this problem */
1915 /* disable last weak rule */
1916 solv->problems.count = oldproblemcount;
1917 solv->learnt_pool.count = oldlearntpoolcount;
1918 r = solv->rules + lastweak;
1919 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling ");
1920 printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1921 disablerule(solv, r);
1927 queue_push(&solv->learnt_pool, 0);
1928 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1932 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1933 disableproblem(solv, solv->problems.elements[i]);
1937 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1942 /*-----------------------------------------------------------------*/
1943 /* Decision revert */
1947 * revert decision at level
1951 revert(Solver *solv, int level)
1953 Pool *pool = solv->pool;
1955 while (solv->decisionq.count)
1957 v = solv->decisionq.elements[solv->decisionq.count - 1];
1958 vv = v > 0 ? v : -v;
1959 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1961 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1962 if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
1963 solv->recommendations.count--;
1964 solv->decisionmap[vv] = 0;
1965 solv->decisionq.count--;
1966 solv->decisionq_why.count--;
1967 solv->propagate_index = solv->decisionq.count;
1969 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1971 solv->branches.count--;
1972 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1973 solv->branches.count--;
1975 solv->recommends_index = -1;
1980 * watch2onhighest - put watch2 on literal with highest level
1984 watch2onhighest(Solver *solv, Rule *r)
1990 return; /* binary rule, both watches are set */
1991 dp = solv->pool->whatprovidesdata + r->d;
1992 while ((v = *dp++) != 0)
1994 l = solv->decisionmap[v < 0 ? -v : v];
2009 * add free decision to decisionq, increase level and
2010 * propagate decision, return if no conflict.
2011 * in conflict case, analyze conflict rule, add resulting
2012 * rule to learnt rule set, make decision from learnt
2013 * rule (always unit) and re-propagate.
2015 * returns the new solver level or 0 if unsolvable
2020 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
2022 Pool *pool = solv->pool;
2031 solv->decisionmap[decision] = level;
2033 solv->decisionmap[-decision] = -level;
2034 queue_push(&solv->decisionq, decision);
2035 queue_push(&solv->decisionq_why, 0);
2039 r = propagate(solv, level);
2043 return analyze_unsolvable(solv, r, disablerules);
2044 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
2045 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
2046 assert(l > 0 && l < level);
2047 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
2049 revert(solv, level);
2050 r = addrule(solv, p, d); /* p requires d */
2052 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
2053 queue_push(&solv->learnt_why, why);
2056 /* at least 2 literals, needs watches */
2057 watch2onhighest(solv, r);
2058 addwatches(solv, r);
2060 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
2061 queue_push(&solv->decisionq, p);
2062 queue_push(&solv->decisionq_why, r - solv->rules);
2063 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
2065 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
2066 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
2067 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
2068 printrule(solv, SAT_DEBUG_ANALYZE, r);
2076 * install best package from the queue. We add an extra package, inst, if
2077 * provided. See comment in weak install section.
2079 * returns the new solver level or 0 if unsolvable
2083 selectandinstall(Solver *solv, int level, Queue *dq, Id inst, int disablerules)
2085 Pool *pool = solv->pool;
2089 if (dq->count > 1 || inst)
2090 policy_filter_unwanted(solv, dq, inst, POLICY_MODE_CHOOSE);
2095 /* choose the supplemented one */
2096 for (i = 0; i < dq->count; i++)
2097 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
2101 for (i = 1; i < dq->count; i++)
2102 queue_push(&solv->branches, dq->elements[i]);
2103 queue_push(&solv->branches, -level);
2107 p = dq->elements[i];
2109 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvable2str(pool, pool->solvables + p));
2111 return setpropagatelearn(solv, level, p, disablerules);
2115 /*-----------------------------------------------------------------*/
2116 /* Main solver interface */
2121 * create solver structure
2123 * pool: all available solvables
2124 * installed: installed Solvables
2127 * Upon solving, rules are created to flag the Solvables
2128 * of the 'installed' Repo as installed.
2132 solver_create(Pool *pool, Repo *installed)
2135 solv = (Solver *)sat_calloc(1, sizeof(Solver));
2137 solv->installed = installed;
2139 queue_init(&solv->ruletojob);
2140 queue_init(&solv->decisionq);
2141 queue_init(&solv->decisionq_why);
2142 queue_init(&solv->problems);
2143 queue_init(&solv->suggestions);
2144 queue_init(&solv->recommendations);
2145 queue_init(&solv->learnt_why);
2146 queue_init(&solv->learnt_pool);
2147 queue_init(&solv->branches);
2148 queue_init(&solv->covenantq);
2149 queue_init(&solv->weakruleq);
2151 map_init(&solv->recommendsmap, pool->nsolvables);
2152 map_init(&solv->suggestsmap, pool->nsolvables);
2153 map_init(&solv->noupdate, installed ? installed->end - installed->start : 0);
2154 solv->recommends_index = 0;
2156 solv->decisionmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
2158 solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
2159 memset(solv->rules, 0, sizeof(Rule));
2170 solver_free(Solver *solv)
2172 queue_free(&solv->ruletojob);
2173 queue_free(&solv->decisionq);
2174 queue_free(&solv->decisionq_why);
2175 queue_free(&solv->learnt_why);
2176 queue_free(&solv->learnt_pool);
2177 queue_free(&solv->problems);
2178 queue_free(&solv->suggestions);
2179 queue_free(&solv->recommendations);
2180 queue_free(&solv->branches);
2181 queue_free(&solv->covenantq);
2182 queue_free(&solv->weakruleq);
2184 map_free(&solv->recommendsmap);
2185 map_free(&solv->suggestsmap);
2186 map_free(&solv->noupdate);
2187 map_free(&solv->weakrulemap);
2189 sat_free(solv->decisionmap);
2190 sat_free(solv->rules);
2191 sat_free(solv->watches);
2192 sat_free(solv->weaksystemrules);
2193 sat_free(solv->obsoletes);
2194 sat_free(solv->obsoletes_data);
2199 /*-------------------------------------------------------*/
2204 * all rules have been set up, now actually run the solver
2209 run_solver(Solver *solv, int disablerules, int doweak)
2217 Pool *pool = solv->pool;
2220 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
2222 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
2223 for (i = 1; i < solv->nrules; i++)
2224 printruleclass(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
2227 POOL_DEBUG(SAT_DEBUG_STATS, "initial decisions: %d\n", solv->decisionq.count);
2229 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2230 printdecisions(solv);
2232 /* start SAT algorithm */
2234 systemlevel = level + 1;
2235 POOL_DEBUG(SAT_DEBUG_STATS, "solving...\n");
2240 * here's the main loop:
2241 * 1) propagate new decisions (only needed for level 1)
2242 * 2) try to keep installed packages
2243 * 3) fulfill all unresolved rules
2244 * 4) install recommended packages
2245 * 5) minimalize solution if we had choices
2246 * if we encounter a problem, we rewind to a safe level and restart
2258 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
2259 if ((r = propagate(solv, level)) != 0)
2261 if (analyze_unsolvable(solv, r, disablerules))
2269 * installed packages
2272 if (level < systemlevel && solv->installed && solv->installed->nsolvables)
2274 if (!solv->updatesystem)
2276 /* try to keep as many packages as possible */
2277 POOL_DEBUG(SAT_DEBUG_STATS, "installing system packages\n");
2278 for (i = solv->installed->start; i < solv->installed->end; i++)
2280 s = pool->solvables + i;
2281 if (s->repo != solv->installed)
2283 if (solv->decisionmap[i] != 0)
2285 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "keeping %s\n", solvable2str(pool, s));
2287 level = setpropagatelearn(solv, level, i, disablerules);
2293 if (level <= olevel)
2296 if (i < solv->installed->end)
2299 if (solv->weaksystemrules)
2301 POOL_DEBUG(SAT_DEBUG_STATS, "installing weak system packages\n");
2302 for (i = solv->installed->start; i < solv->installed->end; i++)
2304 s = pool->solvables + i;
2305 if (s->repo != solv->installed)
2307 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->installed->start] == 0))
2309 /* noupdate is set if a job is erasing the installed solvable or installing a specific version */
2310 if (MAPTST(&solv->noupdate, i - solv->installed->start))
2313 if (solv->weaksystemrules[i - solv->installed->start])
2315 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->installed->start];
2316 while ((p = *dp++) != 0)
2318 if (solv->decisionmap[p] > 0)
2320 if (solv->decisionmap[p] == 0)
2324 continue; /* update package already installed */
2326 if (!dq.count && solv->decisionmap[i] != 0)
2329 /* FIXME: i is handled a bit different because we do not want
2330 * to have it pruned just because it is not recommened.
2331 * we should not prune installed packages instead */
2332 level = selectandinstall(solv, level, &dq, (solv->decisionmap[i] ? 0 : i), disablerules);
2338 if (level <= olevel)
2341 if (i < solv->installed->end)
2344 systemlevel = level;
2351 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unresolved rules\n");
2352 for (i = 1, n = 1; ; i++, n++)
2354 if (n == solv->nrules)
2356 if (i == solv->nrules)
2358 r = solv->rules + i;
2359 if (!r->w1) /* ignore disabled rules */
2364 /* binary or unary rule */
2365 /* need two positive undecided literals */
2366 if (r->p < 0 || r->w2 <= 0)
2368 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2370 queue_push(&dq, r->p);
2371 queue_push(&dq, r->w2);
2376 * all negative literals are installed
2377 * no positive literal is installed
2378 * i.e. the rule is not fulfilled and we
2379 * just need to decide on the positive literals
2383 if (solv->decisionmap[-r->p] <= 0)
2388 if (solv->decisionmap[r->p] > 0)
2390 if (solv->decisionmap[r->p] == 0)
2391 queue_push(&dq, r->p);
2393 dp = pool->whatprovidesdata + r->d;
2394 while ((p = *dp++) != 0)
2398 if (solv->decisionmap[-p] <= 0)
2403 if (solv->decisionmap[p] > 0)
2405 if (solv->decisionmap[p] == 0)
2412 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
2414 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
2415 printruleclass(solv, SAT_DEBUG_PROPAGATE, r);
2417 /* dq.count < 2 cannot happen as this means that
2418 * the rule is unit */
2419 assert(dq.count > 1);
2422 level = selectandinstall(solv, level, &dq, 0, disablerules);
2428 if (level < systemlevel)
2431 } /* for(), decide */
2433 if (n != solv->nrules) /* continue if level < systemlevel */
2436 if (doweak && !solv->problems.count)
2440 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended packages\n");
2442 if (!solv->dosplitprovides && !REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2444 for (i = 1; i < solv->decisionq.count; i++)
2446 p = solv->decisionq.elements[i];
2447 if (p > 0 && pool->solvables[p].repo == solv->installed)
2448 solv->decisionmap[p] = -solv->decisionmap[p];
2453 for (i = 1; i < pool->nsolvables; i++)
2455 if (solv->decisionmap[i] < 0)
2457 if (solv->decisionmap[i] > 0)
2459 Id *recp, rec, *pp, p;
2460 s = pool->solvables + i;
2461 /* installed, check for recommends */
2462 /* XXX need to special case AND ? */
2465 recp = s->repo->idarraydata + s->recommends;
2466 while ((rec = *recp++) != 0)
2469 FOR_PROVIDES(p, pp, rec)
2471 if (solv->decisionmap[p] > 0)
2476 else if (solv->decisionmap[p] == 0)
2478 queue_pushunique(&dq, p);
2486 s = pool->solvables + i;
2487 if (!s->supplements && !s->freshens)
2489 if (!pool_installable(pool, s))
2491 if (solver_is_supplementing(solv, s))
2492 queue_pushunique(&dq, i);
2496 if (!solv->dosplitprovides && !REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2498 for (i = 1; i < solv->decisionq.count; i++)
2500 p = solv->decisionq.elements[i];
2501 if (p > 0 && pool->solvables[p].repo == solv->installed)
2502 solv->decisionmap[p] = -solv->decisionmap[p];
2509 policy_filter_unwanted(solv, &dq, 0, POLICY_MODE_RECOMMEND);
2511 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended %s\n", solvable2str(pool, pool->solvables + p));
2512 queue_push(&solv->recommendations, p);
2513 level = setpropagatelearn(solv, level, p, 0);
2518 if (solv->solution_callback)
2520 solv->solution_callback(solv, solv->solution_callback_data);
2521 if (solv->branches.count)
2523 int i = solv->branches.count - 1;
2524 int l = -solv->branches.elements[i];
2526 if (solv->branches.elements[i - 1] < 0)
2528 p = solv->branches.elements[i];
2529 POOL_DEBUG(SAT_DEBUG_STATS, "branching with %s\n", solvable2str(pool, pool->solvables + p));
2531 for (j = i + 1; j < solv->branches.count; j++)
2532 queue_push(&dq, solv->branches.elements[j]);
2533 solv->branches.count = i;
2535 revert(solv, level);
2537 for (j = 0; j < dq.count; j++)
2538 queue_push(&solv->branches, dq.elements[j]);
2540 level = setpropagatelearn(solv, level, p, disablerules);
2548 /* all branches done, we're finally finished */
2552 /* minimization step */
2553 if (solv->branches.count)
2555 int l = 0, lasti = -1, lastl = -1;
2557 for (i = solv->branches.count - 1; i >= 0; i--)
2559 p = solv->branches.elements[i];
2562 else if (p > 0 && solv->decisionmap[p] > l + 1)
2570 /* kill old solvable so that we do not loop */
2571 p = solv->branches.elements[lasti];
2572 solv->branches.elements[lasti] = 0;
2573 POOL_DEBUG(SAT_DEBUG_STATS, "minimizing %d -> %d with %s\n", solv->decisionmap[p], l, solvable2str(pool, pool->solvables + p));
2576 revert(solv, level);
2578 level = setpropagatelearn(solv, level, p, disablerules);
2589 POOL_DEBUG(SAT_DEBUG_STATS, "done solving.\n\n");
2596 * at this point, all rules that led to conflicts are disabled.
2597 * we re-enable all rules of a problem set but rule "sug", then
2598 * continue to disable more rules until there as again a solution.
2601 /* FIXME: think about conflicting assertions */
2604 refine_suggestion(Solver *solv, Queue *job, Id *problem, Id sug, Queue *refined)
2606 Pool *pool = solv->pool;
2613 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2615 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion start\n");
2616 for (i = 0; problem[i]; i++)
2618 if (problem[i] == sug)
2619 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "=> ");
2620 printproblem(solv, problem[i]);
2623 queue_init(&disabled);
2624 queue_empty(refined);
2625 queue_push(refined, sug);
2627 /* re-enable all problem rules with the exception of "sug"(gests) */
2631 for (i = 0; problem[i]; i++)
2632 if (problem[i] != sug)
2633 enableproblem(solv, problem[i]);
2636 disableupdaterules(solv, job, -(sug + 1));
2640 /* re-enable as many weak rules as possible */
2641 for (i = solv->jobrules, r = solv->rules + i; i < solv->learntrules; i++, r++)
2645 if (!MAPTST(&solv->weakrulemap, i))
2647 enablerule(solv, r);
2650 queue_empty(&solv->problems);
2651 revert(solv, 1); /* XXX move to reset_solver? */
2654 if (!solv->problems.count)
2655 run_solver(solv, 0, 0);
2657 if (!solv->problems.count)
2659 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no more problems!\n");
2660 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2661 printdecisions(solv);
2662 break; /* great, no more problems */
2664 disabledcnt = disabled.count;
2665 /* start with 1 to skip over proof index */
2666 for (i = 1; i < solv->problems.count - 1; i++)
2668 /* ignore solutions in refined */
2669 v = solv->problems.elements[i];
2671 break; /* end of problem reached */
2672 for (j = 0; problem[j]; j++)
2673 if (problem[j] != sug && problem[j] == v)
2677 queue_push(&disabled, v);
2679 if (disabled.count == disabledcnt)
2681 /* no solution found, this was an invalid suggestion! */
2682 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no solution found!\n");
2686 if (disabled.count == disabledcnt + 1)
2688 /* just one suggestion, add it to refined list */
2689 v = disabled.elements[disabledcnt];
2690 queue_push(refined, v);
2691 disableproblem(solv, v);
2693 disableupdaterules(solv, job, -(v + 1));
2697 /* more than one solution, disable all */
2698 /* do not push anything on refine list */
2699 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2701 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "more than one solution found:\n");
2702 for (i = disabledcnt; i < disabled.count; i++)
2703 printproblem(solv, disabled.elements[i]);
2705 for (i = disabledcnt; i < disabled.count; i++)
2706 disableproblem(solv, disabled.elements[i]);
2709 /* all done, get us back into the same state as before */
2710 /* enable refined rules again */
2711 for (i = 0; i < disabled.count; i++)
2712 enableproblem(solv, disabled.elements[i]);
2713 /* disable problem rules again */
2714 for (i = 0; problem[i]; i++)
2715 disableproblem(solv, problem[i]);
2716 disableupdaterules(solv, job, -1);
2717 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion end\n");
2721 problems_to_solutions(Solver *solv, Queue *job)
2723 Pool *pool = solv->pool;
2731 if (!solv->problems.count)
2733 queue_clone(&problems, &solv->problems);
2734 queue_init(&solution);
2735 queue_init(&solutions);
2736 /* copy over proof index */
2737 queue_push(&solutions, problems.elements[0]);
2738 problem = problems.elements + 1;
2739 for (i = 1; i < problems.count; i++)
2741 Id v = problems.elements[i];
2744 /* mark end of this problem */
2745 queue_push(&solutions, 0);
2746 queue_push(&solutions, 0);
2747 if (i + 1 == problems.count)
2749 /* copy over proof of next problem */
2750 queue_push(&solutions, problems.elements[i + 1]);
2752 problem = problems.elements + i + 1;
2755 refine_suggestion(solv, job, problem, v, &solution);
2756 if (!solution.count)
2757 continue; /* this solution didn't work out */
2759 for (j = 0; j < solution.count; j++)
2761 why = solution.elements[j];
2762 /* must be either job descriptor or system rule */
2763 assert(why < 0 || (why >= solv->systemrules && why < solv->learntrules));
2765 printproblem(solv, why);
2769 /* job descriptor */
2770 queue_push(&solutions, 0);
2771 queue_push(&solutions, -why);
2775 /* system rule, find replacement package */
2777 p = solv->installed->start + (why - solv->systemrules);
2778 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
2780 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
2783 if (*dp >= solv->installed->start && *dp < solv->installed->start + solv->installed->nsolvables)
2785 if (solv->decisionmap[*dp] > 0)
2792 queue_push(&solutions, p);
2793 queue_push(&solutions, rp);
2796 /* mark end of this solution */
2797 queue_push(&solutions, 0);
2798 queue_push(&solutions, 0);
2800 queue_free(&solution);
2801 queue_free(&problems);
2802 /* copy queue over to solutions */
2803 queue_free(&solv->problems);
2804 queue_clone(&solv->problems, &solutions);
2806 printf("bring it back!\n");
2807 /* bring solver back into problem state */
2808 revert(solv, 1); /* XXX move to reset_solver? */
2811 printf("problems %d solutions %d\n", solv->problems.count, solutions.count);
2812 assert(solv->problems.count == solutions.count);
2813 queue_free(&solutions);
2817 solver_next_problem(Solver *solv, Id problem)
2821 return solv->problems.count ? 1 : 0;
2822 pp = solv->problems.elements + problem;
2823 while (pp[0] || pp[1])
2827 while (pp[0] || pp[1])
2832 problem = pp - solv->problems.elements;
2833 if (problem >= solv->problems.count)
2839 solver_next_solution(Solver *solv, Id problem, Id solution)
2845 pp = solv->problems.elements + solution;
2846 return pp[0] || pp[1] ? solution : 0;
2848 pp = solv->problems.elements + solution;
2849 while (pp[0] || pp[1])
2852 solution = pp - solv->problems.elements;
2853 return pp[0] || pp[1] ? solution : 0;
2857 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
2860 element = element ? element + 2 : solution;
2861 pp = solv->problems.elements + element;
2862 if (!(pp[0] || pp[1]))
2871 * create obsoletesmap from solver decisions
2872 * required for decision handling
2876 create_decisions_obsoletesmap(Solver *solv)
2878 Pool *pool = solv->pool;
2879 Repo *installed = solv->installed;
2880 Id p, *obsoletesmap = NULL;
2884 obsoletesmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
2887 for (i = 0; i < solv->decisionq.count; i++)
2891 n = solv->decisionq.elements[i];
2894 if (n == SYSTEMSOLVABLE)
2896 s = pool->solvables + n;
2897 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2899 FOR_PROVIDES(p, pp, s->name)
2900 if (s->name == pool->solvables[p].name)
2902 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2904 obsoletesmap[p] = n;
2909 for (i = 0; i < solv->decisionq.count; i++)
2914 n = solv->decisionq.elements[i];
2917 if (n == SYSTEMSOLVABLE)
2919 s = pool->solvables + n;
2920 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2924 obsp = s->repo->idarraydata + s->obsoletes;
2925 while ((obs = *obsp++) != 0)
2926 FOR_PROVIDES(p, pp, obs)
2928 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2930 obsoletesmap[p] = n;
2936 return obsoletesmap;
2945 printdecisions(Solver *solv)
2947 Pool *pool = solv->pool;
2948 Repo *installed = solv->installed;
2949 Id p, *obsoletesmap = create_decisions_obsoletesmap( solv );
2953 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions -----\n");
2955 /* print solvables to be erased */
2959 FOR_REPO_SOLVABLES(installed, p, s)
2961 if (solv->decisionmap[p] >= 0)
2963 if (obsoletesmap[p])
2965 POOL_DEBUG(SAT_DEBUG_RESULT, "erase %s\n", solvable2str(pool, s));
2969 /* print solvables to be installed */
2971 for (i = 0; i < solv->decisionq.count; i++)
2974 p = solv->decisionq.elements[i];
2977 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2980 s = pool->solvables + p;
2981 POOL_DEBUG(SAT_DEBUG_SCHUBI, "level of %s is %d\n", solvable2str(pool, s), p);
2985 if (p == SYSTEMSOLVABLE)
2987 POOL_DEBUG(SAT_DEBUG_SCHUBI, "SYSTEMSOLVABLE\n");
2990 s = pool->solvables + p;
2991 if (installed && s->repo == installed)
2994 if (!obsoletesmap[p])
2996 POOL_DEBUG(SAT_DEBUG_RESULT, "install %s", solvable2str(pool, s));
3000 POOL_DEBUG(SAT_DEBUG_RESULT, "update %s", solvable2str(pool, s));
3001 POOL_DEBUG(SAT_DEBUG_RESULT, " (obsoletes");
3002 for (j = installed->start; j < installed->end; j++)
3003 if (obsoletesmap[j] == p)
3004 POOL_DEBUG(SAT_DEBUG_RESULT, " %s", solvable2str(pool, pool->solvables + j));
3005 POOL_DEBUG(SAT_DEBUG_RESULT, ")");
3007 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3010 sat_free(obsoletesmap);
3012 if (solv->recommendations.count)
3014 POOL_DEBUG(SAT_DEBUG_RESULT, "\nrecommended packages:\n");
3015 for (i = 0; i < solv->recommendations.count; i++)
3017 s = pool->solvables + solv->recommendations.elements[i];
3018 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s\n", solvable2str(pool, s));
3022 if (solv->suggestions.count)
3024 POOL_DEBUG(SAT_DEBUG_RESULT, "\nsuggested packages:\n");
3025 for (i = 0; i < solv->suggestions.count; i++)
3027 s = pool->solvables + solv->suggestions.elements[i];
3028 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s\n", solvable2str(pool, s));
3031 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions end -----\n");
3034 /* this is basically the reverse of addrpmrulesforsolvable */
3036 solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp)
3038 Pool *pool = solv->pool;
3039 Repo *installed = solv->installed;
3043 Id p, *pp, req, *reqp, con, *conp, obs, *obsp, *dp;
3046 if (rid >= solv->systemrules)
3049 *sourcep = solv->installed->start + (rid - solv->systemrules);
3051 return SOLVER_PROBLEM_UPDATE_RULE;
3053 if (rid >= solv->jobrules)
3056 r = solv->rules + rid;
3057 p = solv->ruletojob.elements[rid - solv->jobrules];
3058 *depp = job->elements[p + 1];
3060 *targetp = job->elements[p];
3061 if (r->d == 0 && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
3062 return SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP;
3063 return SOLVER_PROBLEM_JOB_RULE;
3068 /*XXX OBSOLETE CODE */
3069 /* a rpm rule assertion */
3070 assert(rid != -SYSTEMSOLVABLE);
3071 s = pool->solvables - rid;
3072 if (installed && !solv->fixsystem && s->repo == installed)
3074 assert(!dontfix); /* dontfix packages never have a neg assertion */
3075 /* see why the package is not installable */
3076 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
3081 return SOLVER_PROBLEM_NOT_INSTALLABLE;
3083 /* check requires */
3084 assert(s->requires);
3085 reqp = s->repo->idarraydata + s->requires;
3086 while ((req = *reqp++) != 0)
3088 if (req == SOLVABLE_PREREQMARKER)
3090 dp = pool_whatprovides(pool, req);
3098 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
3101 r = solv->rules + rid;
3103 if (r->d == 0 && r->w2 == 0)
3105 /* a rpm rule assertion */
3106 s = pool->solvables - r->p;
3107 if (installed && !solv->fixsystem && s->repo == installed)
3109 assert(!dontfix); /* dontfix packages never have a neg assertion */
3112 /* see why the package is not installable */
3113 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
3116 return SOLVER_PROBLEM_NOT_INSTALLABLE;
3118 /* check requires */
3119 assert(s->requires);
3120 reqp = s->repo->idarraydata + s->requires;
3121 while ((req = *reqp++) != 0)
3123 if (req == SOLVABLE_PREREQMARKER)
3125 dp = pool_whatprovides(pool, req);
3131 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
3133 s = pool->solvables - r->p;
3134 if (installed && !solv->fixsystem && s->repo == installed)
3136 if (r->d == 0 && r->w2 < 0)
3138 /* a package conflict */
3139 Solvable *s2 = pool->solvables - r->w2;
3142 if (installed && !solv->fixsystem && s2->repo == installed)
3145 /* if both packages have the same name and at least one of them
3146 * is not installed, they conflict */
3147 if (s->name == s2->name && (!installed || (s->repo != installed || s2->repo != installed)))
3152 return SOLVER_PROBLEM_SAME_NAME;
3155 /* check conflicts in both directions */
3158 conp = s->repo->idarraydata + s->conflicts;
3159 while ((con = *conp++) != 0)
3161 FOR_PROVIDES(p, pp, con)
3163 if (dontfix && pool->solvables[p].repo == installed)
3170 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
3176 conp = s2->repo->idarraydata + s2->conflicts;
3177 while ((con = *conp++) != 0)
3179 FOR_PROVIDES(p, pp, con)
3181 if (dontfix2 && pool->solvables[p].repo == installed)
3188 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
3192 /* check obsoletes in both directions */
3193 if ((!installed || s->repo != installed) && s->obsoletes)
3195 obsp = s->repo->idarraydata + s->obsoletes;
3196 while ((obs = *obsp++) != 0)
3198 FOR_PROVIDES(p, pp, obs)
3205 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3209 if ((!installed || s2->repo != installed) && s2->obsoletes)
3211 obsp = s2->repo->idarraydata + s2->obsoletes;
3212 while ((obs = *obsp++) != 0)
3214 FOR_PROVIDES(p, pp, obs)
3221 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3225 /* all cases checked, can't happen */
3228 /* simple requires */
3229 assert(s->requires);
3230 reqp = s->repo->idarraydata + s->requires;
3231 while ((req = *reqp++) != 0)
3233 if (req == SOLVABLE_PREREQMARKER)
3235 dp = pool_whatprovides(pool, req);
3238 if (*dp == r->w2 && dp[1] == 0)
3241 else if (dp - pool->whatprovidesdata == r->d)
3248 return SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE;
3252 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp)
3255 Id lreqr, lconr, lsysr, ljobr;
3259 lreqr = lconr = lsysr = ljobr = 0;
3260 while ((rid = solv->learnt_pool.elements[idx++]) != 0)
3263 if (rid >= solv->learntrules)
3264 findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr);
3265 else if (rid >= solv->systemrules)
3270 else if (rid >= solv->jobrules)
3277 r = solv->rules + rid;
3278 if (!r->d && r->w2 < 0)
3285 if (r->d == 0 && r->w2 == 0 && !reqassert)
3287 /* prefer assertions (XXX: bad idea?) */
3293 else if (solv->installed && r->p < 0 && solv->pool->solvables[-r->p].repo == solv->installed)
3295 /* prefer rules of installed packages */
3296 Id op = *reqrp >= 0 ? solv->rules[*reqrp].p : -*reqrp;
3297 if (op <= 0 || solv->pool->solvables[op].repo != solv->installed)
3306 /* assertion, counts as require rule */
3307 /* ignore system solvable as we need useful info */
3308 if (rid == -SYSTEMSOLVABLE)
3310 if (!*reqrp || !reqassert)
3315 else if (solv->installed && solv->pool->solvables[-rid].repo == solv->installed)
3317 /* prefer rules of installed packages */
3318 Id op = *reqrp >= 0 ? solv->rules[*reqrp].p : -*reqrp;
3319 if (op <= 0 || solv->pool->solvables[op].repo != solv->installed)
3325 if (!*reqrp && lreqr)
3327 if (!*conrp && lconr)
3329 if (!*jobrp && ljobr)
3331 if (!*sysrp && lsysr)
3336 solver_findproblemrule(Solver *solv, Id problem)
3338 Id reqr, conr, sysr, jobr;
3339 Id idx = solv->problems.elements[problem - 1];
3340 reqr = conr = sysr = jobr = 0;
3341 findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr);
3354 printprobleminfo(Solver *solv, Queue *job, Id problem)
3356 Pool *pool = solv->pool;
3358 Id dep, source, target;
3361 probr = solver_findproblemrule(solv, problem);
3362 switch (solver_problemruleinfo(solv, job, probr, &dep, &source, &target))
3364 case SOLVER_PROBLEM_UPDATE_RULE:
3365 s = pool_id2solvable(pool, source);
3366 POOL_DEBUG(SAT_DEBUG_RESULT, "problem with installed package %s\n", solvable2str(pool, s));
3368 case SOLVER_PROBLEM_JOB_RULE:
3369 POOL_DEBUG(SAT_DEBUG_RESULT, "conflicting requests\n");
3371 case SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP:
3372 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides requested %s\n", dep2str(pool, dep));
3374 case SOLVER_PROBLEM_NOT_INSTALLABLE:
3375 s = pool_id2solvable(pool, source);
3376 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s is not installable\n", solvable2str(pool, s));
3378 case SOLVER_PROBLEM_NOTHING_PROVIDES_DEP:
3379 s = pool_id2solvable(pool, source);
3380 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides %s needed by %s\n", dep2str(pool, dep), solvable2str(pool, s));
3382 case SOLVER_PROBLEM_SAME_NAME:
3383 s = pool_id2solvable(pool, source);
3384 s2 = pool_id2solvable(pool, target);
3385 POOL_DEBUG(SAT_DEBUG_RESULT, "cannot install both %s and %s\n", solvable2str(pool, s), solvable2str(pool, s2));
3387 case SOLVER_PROBLEM_PACKAGE_CONFLICT:
3388 s = pool_id2solvable(pool, source);
3389 s2 = pool_id2solvable(pool, target);
3390 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s conflicts with %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3392 case SOLVER_PROBLEM_PACKAGE_OBSOLETES:
3393 s = pool_id2solvable(pool, source);
3394 s2 = pool_id2solvable(pool, target);
3395 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s obsoletes %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3397 case SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE:
3398 s = pool_id2solvable(pool, source);
3399 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s requires %s, but none of the providers can be installed\n", solvable2str(pool, s), dep2str(pool, dep));
3405 printsolutions(Solver *solv, Queue *job)
3407 Pool *pool = solv->pool;
3409 Id p, rp, how, what;
3410 Id problem, solution, element;
3413 POOL_DEBUG(SAT_DEBUG_RESULT, "Encountered problems! Here are the solutions:\n\n");
3416 while ((problem = solver_next_problem(solv, problem)) != 0)
3418 POOL_DEBUG(SAT_DEBUG_RESULT, "Problem %d:\n", pcnt++);
3419 POOL_DEBUG(SAT_DEBUG_RESULT, "====================================\n");
3420 printprobleminfo(solv, job, problem);
3421 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3423 while ((solution = solver_next_solution(solv, problem, solution)) != 0)
3426 while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
3430 /* job, rp is index into job queue */
3431 how = job->elements[rp - 1] & ~SOLVER_WEAK;
3432 what = job->elements[rp];
3435 case SOLVER_INSTALL_SOLVABLE:
3436 s = pool->solvables + what;
3437 if (solv->installed && s->repo == solv->installed)
3438 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not keep %s installed\n", solvable2str(pool, s));
3440 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", solvable2str(pool, s));
3442 case SOLVER_ERASE_SOLVABLE:
3443 s = pool->solvables + what;
3444 if (solv->installed && s->repo == solv->installed)
3445 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", solvable2str(pool, s));
3447 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not forbid installation of %s\n", solvable2str(pool, s));
3449 case SOLVER_INSTALL_SOLVABLE_NAME:
3450 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", dep2str(pool, what));
3452 case SOLVER_ERASE_SOLVABLE_NAME:
3453 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", dep2str(pool, what));
3455 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3456 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install a solvable providing %s\n", dep2str(pool, what));
3458 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3459 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall all solvables providing %s\n", dep2str(pool, what));
3461 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3462 s = pool->solvables + what;
3463 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install most recent version of %s\n", solvable2str(pool, s));
3466 POOL_DEBUG(SAT_DEBUG_RESULT, "- do something different\n");
3472 /* policy, replace p with rp */
3473 s = pool->solvables + p;
3474 sd = rp ? pool->solvables + rp : 0;
3478 if (!solv->allowdowngrade && evrcmp(pool, s->evr, sd->evr, EVRCMP_MATCH_RELEASE) > 0)
3480 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow downgrade of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3483 if (!solv->allowarchchange && s->name == sd->name && s->arch != sd->arch && policy_illegal_archchange(solv, s, sd))
3485 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow architecture change of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3488 if (!solv->allowvendorchange && s->name == sd->name && s->vendor != sd->vendor && policy_illegal_vendorchange(solv, s, sd))
3491 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));
3493 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));
3497 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow replacement of %s with %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3501 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow deinstallation of %s\n", solvable2str(pool, s));
3506 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3512 /* create reverse obsoletes map for installed solvables
3514 * for each installed solvable find which packages with *different* names
3515 * obsolete the solvable.
3516 * this index is used in policy_findupdatepackages if noupdateprovide is set.
3520 create_obsolete_index(Solver *solv)
3522 Pool *pool = solv->pool;
3524 Repo *installed = solv->installed;
3525 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
3528 if (!installed || !installed->nsolvables)
3530 solv->obsoletes = obsoletes = sat_calloc(installed->end - installed->start, sizeof(Id));
3531 for (i = 1; i < pool->nsolvables; i++)
3533 s = pool->solvables + i;
3534 if (s->repo == installed)
3538 if (!pool_installable(pool, s))
3540 obsp = s->repo->idarraydata + s->obsoletes;
3541 while ((obs = *obsp++) != 0)
3542 FOR_PROVIDES(p, pp, obs)
3544 if (pool->solvables[p].repo != installed)
3546 if (pool->solvables[p].name == s->name)
3548 obsoletes[p - installed->start]++;
3552 for (i = 0; i < installed->nsolvables; i++)
3555 n += obsoletes[i] + 1;
3558 solv->obsoletes_data = obsoletes_data = sat_calloc(n + 1, sizeof(Id));
3559 POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
3560 for (i = pool->nsolvables - 1; i > 0; i--)
3562 s = pool->solvables + i;
3563 if (s->repo == installed)
3567 if (!pool_installable(pool, s))
3569 obsp = s->repo->idarraydata + s->obsoletes;
3570 while ((obs = *obsp++) != 0)
3571 FOR_PROVIDES(p, pp, obs)
3573 if (pool->solvables[p].repo != installed)
3575 if (pool->solvables[p].name == s->name)
3577 p -= installed->start;
3578 if (obsoletes_data[obsoletes[p]] != i)
3579 obsoletes_data[--obsoletes[p]] = i;
3585 /*-----------------------------------------------------------------*/
3595 solver_solve(Solver *solv, Queue *job)
3597 Pool *pool = solv->pool;
3598 Repo *installed = solv->installed;
3601 Map addedmap; /* '1' == have rpm-rules for solvable */
3602 Id how, what, weak, name, p, *pp, d;
3606 /* create whatprovides if not already there */
3607 if (!pool->whatprovides)
3608 pool_createwhatprovides(pool);
3610 /* create obsolete index if needed */
3611 if (solv->noupdateprovide)
3612 create_obsolete_index(solv);
3615 * create basic rule set of all involved packages
3616 * use addedmap bitmap to make sure we don't create rules twice
3620 map_init(&addedmap, pool->nsolvables);
3624 * always install our system solvable
3626 MAPSET(&addedmap, SYSTEMSOLVABLE);
3627 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
3628 queue_push(&solv->decisionq_why, 0);
3629 solv->decisionmap[SYSTEMSOLVABLE] = 1;
3632 * create rules for all package that could be involved with the solving
3633 * so called: rpm rules
3638 oldnrules = solv->nrules;
3639 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
3640 FOR_REPO_SOLVABLES(installed, p, s)
3641 addrpmrulesforsolvable(solv, s, &addedmap);
3642 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3643 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
3644 oldnrules = solv->nrules;
3645 FOR_REPO_SOLVABLES(installed, p, s)
3646 addrpmrulesforupdaters(solv, s, &addedmap, 1);
3647 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3650 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
3651 oldnrules = solv->nrules;
3652 for (i = 0; i < job->count; i += 2)
3654 how = job->elements[i] & ~SOLVER_WEAK;
3655 what = job->elements[i + 1];
3659 case SOLVER_INSTALL_SOLVABLE:
3660 addrpmrulesforsolvable(solv, pool->solvables + what, &addedmap);
3662 case SOLVER_INSTALL_SOLVABLE_NAME:
3663 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3664 name = (how == SOLVER_INSTALL_SOLVABLE_NAME) ? what : 0;
3665 while (ISRELDEP(name))
3667 Reldep *rd = GETRELDEP(pool, name);
3670 FOR_PROVIDES(p, pp, what)
3672 /* if by name, ensure that the name matches */
3673 if (name && pool->solvables[p].name != name)
3675 addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3678 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3679 /* dont allow downgrade */
3680 addrpmrulesforupdaters(solv, pool->solvables + what, &addedmap, 0);
3684 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3686 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for recommended/suggested packages ***\n");
3688 oldnrules = solv->nrules;
3689 addrpmrulesforweak(solv, &addedmap);
3690 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3692 IF_POOLDEBUG (SAT_DEBUG_STATS)
3694 int possible = 0, installable = 0;
3695 for (i = 1; i < pool->nsolvables; i++)
3697 if (pool_installable(pool, pool->solvables + i))
3699 if (MAPTST(&addedmap, i))
3702 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
3706 * first pass done, we now have all the rpm rules we need.
3707 * unify existing rules before going over all job rules and
3709 * at this point the system is always solvable,
3710 * as an empty system (remove all packages) is a valid solution
3713 unifyrules(solv); /* remove duplicate rpm rules */
3714 solv->directdecisions = solv->decisionq.count;
3716 POOL_DEBUG(SAT_DEBUG_STATS, "decisions so far: %d\n", solv->decisionq.count);
3717 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
3718 printdecisions (solv);
3721 * now add all job rules
3724 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
3726 solv->jobrules = solv->nrules;
3728 for (i = 0; i < job->count; i += 2)
3730 oldnrules = solv->nrules;
3732 how = job->elements[i] & ~SOLVER_WEAK;
3733 weak = job->elements[i] & SOLVER_WEAK;
3734 what = job->elements[i + 1];
3737 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
3738 s = pool->solvables + what;
3739 POOL_DEBUG(SAT_DEBUG_JOB, "job: install solvable %s\n", solvable2str(pool, s));
3740 addrule(solv, what, 0); /* install by Id */
3741 queue_push(&solv->ruletojob, i);
3743 queue_push(&solv->weakruleq, solv->nrules - 1);
3745 case SOLVER_ERASE_SOLVABLE:
3746 s = pool->solvables + what;
3747 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase solvable %s\n", solvable2str(pool, s));
3748 addrule(solv, -what, 0); /* remove by Id */
3749 queue_push(&solv->ruletojob, i);
3751 queue_push(&solv->weakruleq, solv->nrules - 1);
3753 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
3754 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3755 if (how == SOLVER_INSTALL_SOLVABLE_NAME)
3756 POOL_DEBUG(SAT_DEBUG_JOB, "job: install name %s\n", dep2str(pool, what));
3757 if (how == SOLVER_INSTALL_SOLVABLE_PROVIDES)
3758 POOL_DEBUG(SAT_DEBUG_JOB, "job: install provides %s\n", dep2str(pool, what));
3760 name = (how == SOLVER_INSTALL_SOLVABLE_NAME) ? what : 0;
3761 while (ISRELDEP(name))
3763 Reldep *rd = GETRELDEP(pool, name);
3766 FOR_PROVIDES(p, pp, what)
3768 /* if by name, ensure that the name matches */
3769 if (name && pool->solvables[p].name != name)
3775 /* no provider, make this an impossible rule */
3776 queue_push(&q, -SYSTEMSOLVABLE);
3779 p = queue_shift(&q); /* get first provider */
3781 d = 0; /* single provider ? -> make assertion */
3783 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
3784 addrule(solv, p, d); /* add 'requires' rule */
3785 queue_push(&solv->ruletojob, i);
3787 queue_push(&solv->weakruleq, solv->nrules - 1);
3789 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
3790 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3791 if (how == SOLVER_ERASE_SOLVABLE_NAME)
3792 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase name %s\n", dep2str(pool, what));
3793 if (how == SOLVER_ERASE_SOLVABLE_PROVIDES)
3794 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase provides %s\n", dep2str(pool, what));
3795 name = (how == SOLVER_ERASE_SOLVABLE_NAME) ? what : 0;
3796 while (ISRELDEP(name))
3798 Reldep *rd = GETRELDEP(pool, name);
3801 FOR_PROVIDES(p, pp, what)
3803 /* if by name, ensure that the name matches */
3804 if (name && pool->solvables[p].name != name)
3806 addrule(solv, -p, 0); /* add 'remove' rule */
3807 queue_push(&solv->ruletojob, i);
3809 queue_push(&solv->weakruleq, solv->nrules - 1);
3812 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
3813 s = pool->solvables + what;
3814 POOL_DEBUG(SAT_DEBUG_JOB, "job: update %s\n", solvable2str(pool, s));
3815 addupdaterule(solv, s, 0);
3816 queue_push(&solv->ruletojob, i);
3818 queue_push(&solv->weakruleq, solv->nrules - 1);
3821 IF_POOLDEBUG (SAT_DEBUG_JOB)
3824 if (solv->nrules == oldnrules)
3825 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created\n");
3826 for (j = oldnrules; j < solv->nrules; j++)
3828 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
3829 printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
3833 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
3836 * now add system rules
3840 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add system rules ***\n");
3843 solv->systemrules = solv->nrules;
3846 * create rules for updating installed solvables
3850 if (installed && !solv->allowuninstall)
3851 { /* loop over all installed solvables */
3852 /* we create all update rules, but disable some later on depending on the job */
3853 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3855 /* no system rules for patch atoms */
3856 if (s->freshens && !s->supplements)
3858 const char *name = id2str(pool, s->name);
3859 if (name[0] == 'a' && !strncmp(name, "atom:", 5))
3861 addrule(solv, 0, 0);
3865 if (s->repo == installed)
3866 addupdaterule(solv, s, 0); /* allowall = 0 */
3868 addrule(solv, 0, 0); /* create dummy rule */
3870 /* consistency check: we added a rule for _every_ system solvable */
3871 assert(solv->nrules - solv->systemrules == installed->end - installed->start);
3874 /* create special weak system rules */
3875 /* those are used later on to keep a version of the installed packages in
3877 if (installed && installed->nsolvables)
3879 solv->weaksystemrules = sat_calloc(installed->end - installed->start, sizeof(Id));
3880 FOR_REPO_SOLVABLES(installed, p, s)
3882 policy_findupdatepackages(solv, s, &q, 1);
3884 solv->weaksystemrules[p - installed->start] = pool_queuetowhatprovides(pool, &q);
3888 /* free unneeded memory */
3889 map_free(&addedmap);
3892 /* create weak map */
3893 map_init(&solv->weakrulemap, solv->nrules);
3894 for (i = 0; i < solv->weakruleq.count; i++)
3896 p = solv->weakruleq.elements[i];
3897 MAPSET(&solv->weakrulemap, p);
3900 /* all new rules are learnt after this point */
3901 solv->learntrules = solv->nrules;
3903 /* disable system rules that conflict with our job */
3904 disableupdaterules(solv, job, -1);
3906 /* make decisions based on job/system assertions */
3907 makeruledecisions(solv);
3909 /* create watches chains */
3912 POOL_DEBUG(SAT_DEBUG_STATS, "problems so far: %d\n", solv->problems.count);
3915 run_solver(solv, 1, solv->dontinstallrecommended ? 0 : 1);
3917 /* find recommended packages */
3918 if (!solv->problems.count)
3920 Id rec, *recp, p, *pp;
3925 /* disable all weak erase rules */
3926 for (i = 1, r = solv->rules + i; i < solv->learntrules; i++, r++)
3928 if (!MAPTST(&solv->weakrulemap, i))
3932 disablerule(solv, r);
3937 queue_clone(&olddecisions, &solv->decisionq);
3939 r = propagate(solv, 1);
3941 for (i = 0; i < olddecisions.count; i++)
3943 p = olddecisions.elements[i];
3946 if (solv->decisionmap[p] > 0)
3948 assert(solv->decisionmap[p] == 0);
3949 solv->decisionmap[p] = 1;
3950 queue_push(&solv->decisionq, p);
3951 queue_push(&solv->decisionq_why, 0);
3952 r = propagate(solv, 1);
3957 if (gotweak || solv->dontinstallrecommended)
3959 /* create map of all suggests that are still open */
3960 solv->recommends_index = -1;
3961 MAPZERO(&solv->recommendsmap);
3962 for (i = 0; i < solv->decisionq.count; i++)
3964 p = solv->decisionq.elements[i];
3967 s = pool->solvables + p;
3970 recp = s->repo->idarraydata + s->recommends;
3971 while ((rec = *recp++) != 0)
3973 FOR_PROVIDES(p, pp, rec)
3974 if (solv->decisionmap[p] > 0)
3977 continue; /* already fulfilled */
3978 FOR_PROVIDES(p, pp, rec)
3979 MAPSET(&solv->recommendsmap, p);
3983 for (i = 1; i < pool->nsolvables; i++)
3985 if (solv->decisionmap[i] != 0)
3987 s = pool->solvables + i;
3988 if (!MAPTST(&solv->recommendsmap, i))
3990 if (!s->supplements)
3992 if (!pool_installable(pool, s))
3994 if (!solver_is_supplementing(solv, s))
3997 if (solv->dontinstallrecommended)
3998 queue_push(&solv->recommendations, i);
4000 queue_pushunique(&solv->recommendations, i);
4002 /* we use MODE_SUGGEST here so that repo prio is ignored */
4003 policy_filter_unwanted(solv, &solv->recommendations, 0, POLICY_MODE_SUGGEST);
4007 queue_clone(&solv->decisionq, &olddecisions);
4008 queue_free(&olddecisions);
4009 /* XXX restore everything */
4013 /* find suggested packages */
4014 if (!solv->problems.count)
4016 Id sug, *sugp, p, *pp;
4018 /* create map of all suggests that are still open */
4019 solv->recommends_index = -1;
4020 MAPZERO(&solv->suggestsmap);
4021 for (i = 0; i < solv->decisionq.count; i++)
4023 p = solv->decisionq.elements[i];
4026 s = pool->solvables + p;
4029 sugp = s->repo->idarraydata + s->suggests;
4030 while ((sug = *sugp++) != 0)
4032 FOR_PROVIDES(p, pp, sug)
4033 if (solv->decisionmap[p] > 0)
4036 continue; /* already fulfilled */
4037 FOR_PROVIDES(p, pp, sug)
4038 MAPSET(&solv->suggestsmap, p);
4042 for (i = 1; i < pool->nsolvables; i++)
4044 if (solv->decisionmap[i] != 0)
4046 s = pool->solvables + i;
4047 if (!MAPTST(&solv->suggestsmap, i))
4051 if (!pool_installable(pool, s))
4053 if (!solver_is_enhancing(solv, s))
4056 queue_push(&solv->suggestions, i);
4058 policy_filter_unwanted(solv, &solv->suggestions, 0, POLICY_MODE_SUGGEST);
4061 if (solv->problems.count)
4062 problems_to_solutions(solv, job);
4065 /***********************************************************************/
4068 solver_calc_changed_pkgs(Solver *solv, Queue *pkgs)
4070 Pool *pool = solv->pool;
4076 /* create list of solvables that have to be installed */
4077 /* (this is actually just a simple sort) */
4078 map_init(&installmap, pool->nsolvables);
4079 for (i = 1; i < solv->decisionq.count; i++)
4081 Id p = solv->decisionq.elements[i];
4084 s = pool->solvables + p;
4087 if (solv->installed && s->repo == solv->installed)
4089 MAPSET(&installmap, p);
4091 for (p = 1; p < pool->nsolvables; p++)
4092 if (MAPTST(&installmap, p))
4093 queue_push(pkgs, p);
4094 map_free(&installmap);
4095 /* run through erase solvable dudata */
4096 if (solv->installed)
4098 FOR_REPO_SOLVABLES(solv->installed, p, s)
4100 if (solv->decisionmap[p] < 0)
4101 queue_push(pkgs, -p);
4107 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
4111 solver_calc_changed_pkgs(solv, &pkgs);
4112 pool_calc_duchanges(solv->pool, &pkgs, mps, nmps);
4117 solver_calc_installsizechange(Solver *solv)
4122 solver_calc_changed_pkgs(solv, &pkgs);
4123 change = pool_calc_installsizechange(solv->pool, &pkgs);