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 + i; 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))
1448 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
1453 * propagate decision to all rules
1454 * return : 0 = everything is OK
1455 * watched rule = there is a conflict
1459 propagate(Solver *solv, int level)
1461 Pool *pool = solv->pool;
1466 Id *decisionmap = solv->decisionmap;
1467 Id *watches = solv->watches + pool->nsolvables;
1469 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
1471 while (solv->propagate_index < solv->decisionq.count)
1473 /* negate because our watches trigger if literal goes FALSE */
1474 pkg = -solv->decisionq.elements[solv->propagate_index++];
1475 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1477 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
1478 printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
1480 printWatches(solv, SAT_DEBUG_SCHUBI);
1483 for (rp = watches + pkg; *rp; rp = nrp)
1485 r = solv->rules + *rp;
1487 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1489 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
1490 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1495 ow = r->w2; /* regard the second watchpoint to come to a solution */
1500 ow = r->w1; /* regard the first watchpoint to come to a solution */
1503 /* if clause is TRUE, nothing to do */
1504 if (DECISIONMAP_TRUE(ow))
1509 /* not a binary clause, check if we need to move our watch */
1510 /* search for a literal that is not ow and not false */
1511 /* (true is also ok, in that case the rule is fulfilled) */
1512 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1515 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1516 if (p != ow && !DECISIONMAP_TRUE(-p))
1520 /* p is free to watch, move watch to p */
1521 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1524 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables + p));
1526 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables - p));
1540 watches[p] = r - solv->rules;
1544 /* unit clause found, set other watch to TRUE */
1545 if (DECISIONMAP_TRUE(-ow))
1546 return r; /* eek, a conflict! */
1547 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1549 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " unit ");
1550 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1553 decisionmap[ow] = level;
1555 decisionmap[-ow] = -level;
1556 queue_push(&solv->decisionq, ow);
1557 queue_push(&solv->decisionq_why, r - solv->rules);
1558 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1560 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1562 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", solvable2str(pool, s));
1564 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", solvable2str(pool, s));
1568 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
1570 return 0; /* all is well */
1574 /*-----------------------------------------------------------------*/
1583 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
1585 Pool *pool = solv->pool;
1588 Map seen; /* global? */
1591 int num = 0, l1num = 0;
1592 int learnt_why = solv->learnt_pool.count;
1593 Id *decisionmap = solv->decisionmap;
1597 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
1598 map_init(&seen, pool->nsolvables);
1599 idx = solv->decisionq.count;
1602 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1603 printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1604 queue_push(&solv->learnt_pool, c - solv->rules);
1605 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1606 /* go through all literals of the rule */
1618 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1620 vv = v > 0 ? v : -v;
1621 if (MAPTST(&seen, vv))
1623 l = solv->decisionmap[vv];
1628 /* a level 1 literal, mark it for later */
1629 MAPSET(&seen, vv); /* mark for scanning in level 1 phase */
1635 num++; /* need to do this one as well */
1646 v = solv->decisionq.elements[--idx];
1647 vv = v > 0 ? v : -v;
1648 if (MAPTST(&seen, vv))
1651 c = solv->rules + solv->decisionq_why.elements[idx];
1656 *pr = -v; /* so that v doesn't get lost */
1658 /* add involved level 1 rules */
1661 POOL_DEBUG(SAT_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
1665 v = solv->decisionq.elements[--idx];
1666 vv = v > 0 ? v : -v;
1667 if (!MAPTST(&seen, vv))
1669 why = solv->decisionq_why.elements[idx];
1672 queue_push(&solv->learnt_pool, -vv);
1673 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1675 POOL_DEBUG(SAT_DEBUG_ANALYZE, "RPM ASSERT Rule:\n");
1676 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, v);
1680 queue_push(&solv->learnt_pool, why);
1681 c = solv->rules + why;
1682 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1683 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1684 printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1695 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1697 vv = v > 0 ? v : -v;
1698 l = solv->decisionmap[vv];
1699 if (l != 1 && l != -1)
1709 else if (r.count == 1 && r.elements[0] < 0)
1710 *dr = r.elements[0];
1712 *dr = pool_queuetowhatprovides(pool, &r);
1713 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1715 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
1716 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, *pr);
1717 for (i = 0; i < r.count; i++)
1718 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
1720 /* push end marker on learnt reasons stack */
1721 queue_push(&solv->learnt_pool, 0);
1730 * reset the solver decisions to right after the rpm rules.
1731 * called after rules have been enabled/disabled
1735 reset_solver(Solver *solv)
1737 Pool *pool = solv->pool;
1741 /* rewind decisions to direct rpm rule assertions */
1742 for (i = solv->decisionq.count - 1; i >= solv->directdecisions; i--)
1744 v = solv->decisionq.elements[i];
1745 solv->decisionmap[v > 0 ? v : -v] = 0;
1748 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions done reduced from %d to %d\n", solv->decisionq.count, solv->directdecisions);
1750 solv->decisionq_why.count = solv->directdecisions;
1751 solv->decisionq.count = solv->directdecisions;
1752 solv->recommends_index = -1;
1753 solv->propagate_index = 0;
1755 /* adapt learnt rule status to new set of enabled/disabled rules */
1756 enabledisablelearntrules(solv);
1758 /* redo all job/system decisions */
1759 makeruledecisions(solv);
1760 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
1762 /* recreate watch chains */
1768 * analyze_unsolvable_rule
1772 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp)
1774 Pool *pool = solv->pool;
1776 Id why = r - solv->rules;
1778 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1779 printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1780 if (solv->learntrules && why >= solv->learntrules)
1782 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1783 if (solv->learnt_pool.elements[i] > 0)
1784 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp);
1787 if (MAPTST(&solv->weakrulemap, why))
1788 if (!*lastweakp || why > *lastweakp)
1790 /* do not add rpm rules to problem */
1791 if (why < solv->jobrules)
1793 /* turn rule into problem */
1794 if (why >= solv->jobrules && why < solv->systemrules)
1795 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1796 /* return if problem already countains our rule */
1797 if (solv->problems.count)
1799 for (i = solv->problems.count - 1; i >= 0; i--)
1800 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
1802 else if (solv->problems.elements[i] == why)
1805 queue_push(&solv->problems, why);
1810 * analyze_unsolvable
1812 * return: 1 - disabled some rules, try again
1817 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1819 Pool *pool = solv->pool;
1821 Map seen; /* global to speed things up? */
1824 Id *decisionmap = solv->decisionmap;
1825 int oldproblemcount;
1826 int oldlearntpoolcount;
1829 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
1830 oldproblemcount = solv->problems.count;
1831 oldlearntpoolcount = solv->learnt_pool.count;
1833 /* make room for proof index */
1834 /* must update it later, as analyze_unsolvable_rule would confuse
1835 * it with a rule index if we put the real value in already */
1836 queue_push(&solv->problems, 0);
1839 map_init(&seen, pool->nsolvables);
1840 queue_push(&solv->learnt_pool, r - solv->rules);
1842 analyze_unsolvable_rule(solv, r, &lastweak);
1843 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1854 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1856 vv = v > 0 ? v : -v;
1857 l = solv->decisionmap[vv];
1862 idx = solv->decisionq.count;
1865 v = solv->decisionq.elements[--idx];
1866 vv = v > 0 ? v : -v;
1867 if (!MAPTST(&seen, vv))
1869 why = solv->decisionq_why.elements[idx];
1874 /* level 1 and no why, must be an rpm assertion */
1875 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1877 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "RPM ");
1878 printruleelement(solv, SAT_DEBUG_UNSOLVABLE, 0, v);
1880 /* this is the only positive rpm assertion */
1881 if (v == SYSTEMSOLVABLE)
1884 queue_push(&solv->learnt_pool, v);
1888 queue_push(&solv->learnt_pool, why);
1889 r = solv->rules + why;
1890 analyze_unsolvable_rule(solv, r, &lastweak);
1891 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1902 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1904 vv = v > 0 ? v : -v;
1905 l = solv->decisionmap[vv];
1912 queue_push(&solv->problems, 0); /* mark end of this problem */
1916 /* disable last weak rule */
1917 solv->problems.count = oldproblemcount;
1918 solv->learnt_pool.count = oldlearntpoolcount;
1919 r = solv->rules + lastweak;
1920 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling ");
1921 printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1922 disablerule(solv, r);
1928 queue_push(&solv->learnt_pool, 0);
1929 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1933 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1934 disableproblem(solv, solv->problems.elements[i]);
1938 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1943 /*-----------------------------------------------------------------*/
1944 /* Decision revert */
1948 * revert decision at level
1952 revert(Solver *solv, int level)
1954 Pool *pool = solv->pool;
1956 while (solv->decisionq.count)
1958 v = solv->decisionq.elements[solv->decisionq.count - 1];
1959 vv = v > 0 ? v : -v;
1960 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1962 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1963 if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
1964 solv->recommendations.count--;
1965 solv->decisionmap[vv] = 0;
1966 solv->decisionq.count--;
1967 solv->decisionq_why.count--;
1968 solv->propagate_index = solv->decisionq.count;
1970 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1972 solv->branches.count--;
1973 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1974 solv->branches.count--;
1976 solv->recommends_index = -1;
1981 * watch2onhighest - put watch2 on literal with highest level
1985 watch2onhighest(Solver *solv, Rule *r)
1991 return; /* binary rule, both watches are set */
1992 dp = solv->pool->whatprovidesdata + r->d;
1993 while ((v = *dp++) != 0)
1995 l = solv->decisionmap[v < 0 ? -v : v];
2010 * add free decision to decisionq, increase level and
2011 * propagate decision, return if no conflict.
2012 * in conflict case, analyze conflict rule, add resulting
2013 * rule to learnt rule set, make decision from learnt
2014 * rule (always unit) and re-propagate.
2016 * returns the new solver level or 0 if unsolvable
2021 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
2023 Pool *pool = solv->pool;
2032 solv->decisionmap[decision] = level;
2034 solv->decisionmap[-decision] = -level;
2035 queue_push(&solv->decisionq, decision);
2036 queue_push(&solv->decisionq_why, 0);
2040 r = propagate(solv, level);
2044 return analyze_unsolvable(solv, r, disablerules);
2045 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
2046 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
2047 assert(l > 0 && l < level);
2048 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
2050 revert(solv, level);
2051 r = addrule(solv, p, d); /* p requires d */
2053 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
2054 queue_push(&solv->learnt_why, why);
2057 /* at least 2 literals, needs watches */
2058 watch2onhighest(solv, r);
2059 addwatches(solv, r);
2061 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
2062 queue_push(&solv->decisionq, p);
2063 queue_push(&solv->decisionq_why, r - solv->rules);
2064 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
2066 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
2067 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
2068 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
2069 printrule(solv, SAT_DEBUG_ANALYZE, r);
2077 * install best package from the queue. We add an extra package, inst, if
2078 * provided. See comment in weak install section.
2080 * returns the new solver level or 0 if unsolvable
2084 selectandinstall(Solver *solv, int level, Queue *dq, Id inst, int disablerules)
2086 Pool *pool = solv->pool;
2090 if (dq->count > 1 || inst)
2091 policy_filter_unwanted(solv, dq, inst, POLICY_MODE_CHOOSE);
2096 /* choose the supplemented one */
2097 for (i = 0; i < dq->count; i++)
2098 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
2102 for (i = 1; i < dq->count; i++)
2103 queue_push(&solv->branches, dq->elements[i]);
2104 queue_push(&solv->branches, -level);
2108 p = dq->elements[i];
2110 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvable2str(pool, pool->solvables + p));
2112 return setpropagatelearn(solv, level, p, disablerules);
2116 /*-----------------------------------------------------------------*/
2117 /* Main solver interface */
2122 * create solver structure
2124 * pool: all available solvables
2125 * installed: installed Solvables
2128 * Upon solving, rules are created to flag the Solvables
2129 * of the 'installed' Repo as installed.
2133 solver_create(Pool *pool, Repo *installed)
2136 solv = (Solver *)sat_calloc(1, sizeof(Solver));
2138 solv->installed = installed;
2140 queue_init(&solv->ruletojob);
2141 queue_init(&solv->decisionq);
2142 queue_init(&solv->decisionq_why);
2143 queue_init(&solv->problems);
2144 queue_init(&solv->suggestions);
2145 queue_init(&solv->recommendations);
2146 queue_init(&solv->learnt_why);
2147 queue_init(&solv->learnt_pool);
2148 queue_init(&solv->branches);
2149 queue_init(&solv->covenantq);
2150 queue_init(&solv->weakruleq);
2152 map_init(&solv->recommendsmap, pool->nsolvables);
2153 map_init(&solv->suggestsmap, pool->nsolvables);
2154 map_init(&solv->noupdate, installed ? installed->end - installed->start : 0);
2155 solv->recommends_index = 0;
2157 solv->decisionmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
2159 solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
2160 memset(solv->rules, 0, sizeof(Rule));
2171 solver_free(Solver *solv)
2173 queue_free(&solv->ruletojob);
2174 queue_free(&solv->decisionq);
2175 queue_free(&solv->decisionq_why);
2176 queue_free(&solv->learnt_why);
2177 queue_free(&solv->learnt_pool);
2178 queue_free(&solv->problems);
2179 queue_free(&solv->suggestions);
2180 queue_free(&solv->recommendations);
2181 queue_free(&solv->branches);
2182 queue_free(&solv->covenantq);
2183 queue_free(&solv->weakruleq);
2185 map_free(&solv->recommendsmap);
2186 map_free(&solv->suggestsmap);
2187 map_free(&solv->noupdate);
2188 map_free(&solv->weakrulemap);
2190 sat_free(solv->decisionmap);
2191 sat_free(solv->rules);
2192 sat_free(solv->watches);
2193 sat_free(solv->weaksystemrules);
2194 sat_free(solv->obsoletes);
2195 sat_free(solv->obsoletes_data);
2200 /*-------------------------------------------------------*/
2205 * all rules have been set up, now actually run the solver
2210 run_solver(Solver *solv, int disablerules, int doweak)
2218 Pool *pool = solv->pool;
2221 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
2223 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
2224 for (i = 1; i < solv->nrules; i++)
2225 printruleclass(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
2228 POOL_DEBUG(SAT_DEBUG_STATS, "initial decisions: %d\n", solv->decisionq.count);
2230 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2231 printdecisions(solv);
2233 /* start SAT algorithm */
2235 systemlevel = level + 1;
2236 POOL_DEBUG(SAT_DEBUG_STATS, "solving...\n");
2241 * here's the main loop:
2242 * 1) propagate new decisions (only needed for level 1)
2243 * 2) try to keep installed packages
2244 * 3) fulfill all unresolved rules
2245 * 4) install recommended packages
2246 * 5) minimalize solution if we had choices
2247 * if we encounter a problem, we rewind to a safe level and restart
2259 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
2260 if ((r = propagate(solv, level)) != 0)
2262 if (analyze_unsolvable(solv, r, disablerules))
2270 * installed packages
2273 if (level < systemlevel && solv->installed && solv->installed->nsolvables)
2275 if (!solv->updatesystem)
2277 /* try to keep as many packages as possible */
2278 POOL_DEBUG(SAT_DEBUG_STATS, "installing system packages\n");
2279 for (i = solv->installed->start; i < solv->installed->end; i++)
2281 s = pool->solvables + i;
2282 if (s->repo != solv->installed)
2284 if (solv->decisionmap[i] != 0)
2286 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "keeping %s\n", solvable2str(pool, s));
2288 level = setpropagatelearn(solv, level, i, disablerules);
2294 if (level <= olevel)
2297 if (i < solv->installed->end)
2300 if (solv->weaksystemrules)
2302 POOL_DEBUG(SAT_DEBUG_STATS, "installing weak system packages\n");
2303 for (i = solv->installed->start; i < solv->installed->end; i++)
2305 s = pool->solvables + i;
2306 if (s->repo != solv->installed)
2308 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->installed->start] == 0))
2310 /* noupdate is set if a job is erasing the installed solvable or installing a specific version */
2311 if (MAPTST(&solv->noupdate, i - solv->installed->start))
2314 if (solv->weaksystemrules[i - solv->installed->start])
2316 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->installed->start];
2317 while ((p = *dp++) != 0)
2319 if (solv->decisionmap[p] > 0)
2321 if (solv->decisionmap[p] == 0)
2325 continue; /* update package already installed */
2327 if (!dq.count && solv->decisionmap[i] != 0)
2330 /* FIXME: i is handled a bit different because we do not want
2331 * to have it pruned just because it is not recommened.
2332 * we should not prune installed packages instead */
2333 level = selectandinstall(solv, level, &dq, (solv->decisionmap[i] ? 0 : i), disablerules);
2339 if (level <= olevel)
2342 if (i < solv->installed->end)
2345 systemlevel = level;
2352 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unresolved rules\n");
2353 for (i = 1, n = 1; ; i++, n++)
2355 if (n == solv->nrules)
2357 if (i == solv->nrules)
2359 r = solv->rules + i;
2360 if (!r->w1) /* ignore disabled rules */
2365 /* binary or unary rule */
2366 /* need two positive undecided literals */
2367 if (r->p < 0 || r->w2 <= 0)
2369 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2371 queue_push(&dq, r->p);
2372 queue_push(&dq, r->w2);
2377 * all negative literals are installed
2378 * no positive literal is installed
2379 * i.e. the rule is not fulfilled and we
2380 * just need to decide on the positive literals
2384 if (solv->decisionmap[-r->p] <= 0)
2389 if (solv->decisionmap[r->p] > 0)
2391 if (solv->decisionmap[r->p] == 0)
2392 queue_push(&dq, r->p);
2394 dp = pool->whatprovidesdata + r->d;
2395 while ((p = *dp++) != 0)
2399 if (solv->decisionmap[-p] <= 0)
2404 if (solv->decisionmap[p] > 0)
2406 if (solv->decisionmap[p] == 0)
2413 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
2415 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
2416 printruleclass(solv, SAT_DEBUG_PROPAGATE, r);
2418 /* dq.count < 2 cannot happen as this means that
2419 * the rule is unit */
2420 assert(dq.count > 1);
2423 level = selectandinstall(solv, level, &dq, 0, disablerules);
2429 if (level < systemlevel)
2432 } /* for(), decide */
2434 if (n != solv->nrules) /* continue if level < systemlevel */
2437 if (doweak && !solv->problems.count)
2441 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended packages\n");
2443 if (!solv->dosplitprovides && !REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2445 for (i = 1; i < solv->decisionq.count; i++)
2447 p = solv->decisionq.elements[i];
2448 if (p > 0 && pool->solvables[p].repo == solv->installed)
2449 solv->decisionmap[p] = -solv->decisionmap[p];
2454 for (i = 1; i < pool->nsolvables; i++)
2456 if (solv->decisionmap[i] < 0)
2458 if (solv->decisionmap[i] > 0)
2460 Id *recp, rec, *pp, p;
2461 s = pool->solvables + i;
2462 /* installed, check for recommends */
2463 /* XXX need to special case AND ? */
2466 recp = s->repo->idarraydata + s->recommends;
2467 while ((rec = *recp++) != 0)
2470 FOR_PROVIDES(p, pp, rec)
2472 if (solv->decisionmap[p] > 0)
2477 else if (solv->decisionmap[p] == 0)
2479 queue_pushunique(&dq, p);
2487 s = pool->solvables + i;
2488 if (!s->supplements && !s->freshens)
2490 if (!pool_installable(pool, s))
2492 if (solver_is_supplementing(solv, s))
2493 queue_pushunique(&dq, i);
2497 if (!solv->dosplitprovides && !REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2499 for (i = 1; i < solv->decisionq.count; i++)
2501 p = solv->decisionq.elements[i];
2502 if (p > 0 && pool->solvables[p].repo == solv->installed)
2503 solv->decisionmap[p] = -solv->decisionmap[p];
2510 policy_filter_unwanted(solv, &dq, 0, POLICY_MODE_RECOMMEND);
2512 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended %s\n", solvable2str(pool, pool->solvables + p));
2513 queue_push(&solv->recommendations, p);
2514 level = setpropagatelearn(solv, level, p, 0);
2519 if (solv->solution_callback)
2521 solv->solution_callback(solv, solv->solution_callback_data);
2522 if (solv->branches.count)
2524 int i = solv->branches.count - 1;
2525 int l = -solv->branches.elements[i];
2527 if (solv->branches.elements[i - 1] < 0)
2529 p = solv->branches.elements[i];
2530 POOL_DEBUG(SAT_DEBUG_STATS, "branching with %s\n", solvable2str(pool, pool->solvables + p));
2532 for (j = i + 1; j < solv->branches.count; j++)
2533 queue_push(&dq, solv->branches.elements[j]);
2534 solv->branches.count = i;
2536 revert(solv, level);
2538 for (j = 0; j < dq.count; j++)
2539 queue_push(&solv->branches, dq.elements[j]);
2541 level = setpropagatelearn(solv, level, p, disablerules);
2549 /* all branches done, we're finally finished */
2553 /* minimization step */
2554 if (solv->branches.count)
2556 int l = 0, lasti = -1, lastl = -1;
2558 for (i = solv->branches.count - 1; i >= 0; i--)
2560 p = solv->branches.elements[i];
2563 else if (p > 0 && solv->decisionmap[p] > l + 1)
2571 /* kill old solvable so that we do not loop */
2572 p = solv->branches.elements[lasti];
2573 solv->branches.elements[lasti] = 0;
2574 POOL_DEBUG(SAT_DEBUG_STATS, "minimizing %d -> %d with %s\n", solv->decisionmap[p], l, solvable2str(pool, pool->solvables + p));
2577 revert(solv, level);
2579 level = setpropagatelearn(solv, level, p, disablerules);
2590 POOL_DEBUG(SAT_DEBUG_STATS, "done solving.\n\n");
2597 * at this point, all rules that led to conflicts are disabled.
2598 * we re-enable all rules of a problem set but rule "sug", then
2599 * continue to disable more rules until there as again a solution.
2602 /* FIXME: think about conflicting assertions */
2605 refine_suggestion(Solver *solv, Queue *job, Id *problem, Id sug, Queue *refined)
2607 Pool *pool = solv->pool;
2614 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2616 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion start\n");
2617 for (i = 0; problem[i]; i++)
2619 if (problem[i] == sug)
2620 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "=> ");
2621 printproblem(solv, problem[i]);
2624 queue_init(&disabled);
2625 queue_empty(refined);
2626 queue_push(refined, sug);
2628 /* re-enable all problem rules with the exception of "sug"(gests) */
2632 for (i = 0; problem[i]; i++)
2633 if (problem[i] != sug)
2634 enableproblem(solv, problem[i]);
2637 disableupdaterules(solv, job, -(sug + 1));
2641 /* re-enable as many weak rules as possible */
2642 for (i = solv->jobrules, r = solv->rules + i; i < solv->learntrules; i++, r++)
2646 if (!MAPTST(&solv->weakrulemap, i))
2648 enablerule(solv, r);
2651 queue_empty(&solv->problems);
2652 revert(solv, 1); /* XXX move to reset_solver? */
2655 if (!solv->problems.count)
2656 run_solver(solv, 0, 0);
2658 if (!solv->problems.count)
2660 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no more problems!\n");
2661 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2662 printdecisions(solv);
2663 break; /* great, no more problems */
2665 disabledcnt = disabled.count;
2666 /* start with 1 to skip over proof index */
2667 for (i = 1; i < solv->problems.count - 1; i++)
2669 /* ignore solutions in refined */
2670 v = solv->problems.elements[i];
2672 break; /* end of problem reached */
2673 for (j = 0; problem[j]; j++)
2674 if (problem[j] != sug && problem[j] == v)
2678 queue_push(&disabled, v);
2680 if (disabled.count == disabledcnt)
2682 /* no solution found, this was an invalid suggestion! */
2683 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no solution found!\n");
2687 if (disabled.count == disabledcnt + 1)
2689 /* just one suggestion, add it to refined list */
2690 v = disabled.elements[disabledcnt];
2691 queue_push(refined, v);
2692 disableproblem(solv, v);
2694 disableupdaterules(solv, job, -(v + 1));
2698 /* more than one solution, disable all */
2699 /* do not push anything on refine list */
2700 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2702 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "more than one solution found:\n");
2703 for (i = disabledcnt; i < disabled.count; i++)
2704 printproblem(solv, disabled.elements[i]);
2706 for (i = disabledcnt; i < disabled.count; i++)
2707 disableproblem(solv, disabled.elements[i]);
2710 /* all done, get us back into the same state as before */
2711 /* enable refined rules again */
2712 for (i = 0; i < disabled.count; i++)
2713 enableproblem(solv, disabled.elements[i]);
2714 /* disable problem rules again */
2715 for (i = 0; problem[i]; i++)
2716 disableproblem(solv, problem[i]);
2717 disableupdaterules(solv, job, -1);
2718 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion end\n");
2722 problems_to_solutions(Solver *solv, Queue *job)
2724 Pool *pool = solv->pool;
2732 if (!solv->problems.count)
2734 queue_clone(&problems, &solv->problems);
2735 queue_init(&solution);
2736 queue_init(&solutions);
2737 /* copy over proof index */
2738 queue_push(&solutions, problems.elements[0]);
2739 problem = problems.elements + 1;
2740 for (i = 1; i < problems.count; i++)
2742 Id v = problems.elements[i];
2745 /* mark end of this problem */
2746 queue_push(&solutions, 0);
2747 queue_push(&solutions, 0);
2748 if (i + 1 == problems.count)
2750 /* copy over proof of next problem */
2751 queue_push(&solutions, problems.elements[i + 1]);
2753 problem = problems.elements + i + 1;
2756 refine_suggestion(solv, job, problem, v, &solution);
2757 if (!solution.count)
2758 continue; /* this solution didn't work out */
2760 for (j = 0; j < solution.count; j++)
2762 why = solution.elements[j];
2763 /* must be either job descriptor or system rule */
2764 assert(why < 0 || (why >= solv->systemrules && why < solv->learntrules));
2766 printproblem(solv, why);
2770 /* job descriptor */
2771 queue_push(&solutions, 0);
2772 queue_push(&solutions, -why);
2776 /* system rule, find replacement package */
2778 p = solv->installed->start + (why - solv->systemrules);
2779 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
2781 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
2784 if (*dp >= solv->installed->start && *dp < solv->installed->start + solv->installed->nsolvables)
2786 if (solv->decisionmap[*dp] > 0)
2793 queue_push(&solutions, p);
2794 queue_push(&solutions, rp);
2797 /* mark end of this solution */
2798 queue_push(&solutions, 0);
2799 queue_push(&solutions, 0);
2801 queue_free(&solution);
2802 queue_free(&problems);
2803 /* copy queue over to solutions */
2804 queue_free(&solv->problems);
2805 queue_clone(&solv->problems, &solutions);
2807 printf("bring it back!\n");
2808 /* bring solver back into problem state */
2809 revert(solv, 1); /* XXX move to reset_solver? */
2812 printf("problems %d solutions %d\n", solv->problems.count, solutions.count);
2813 assert(solv->problems.count == solutions.count);
2814 queue_free(&solutions);
2818 solver_next_problem(Solver *solv, Id problem)
2822 return solv->problems.count ? 1 : 0;
2823 pp = solv->problems.elements + problem;
2824 while (pp[0] || pp[1])
2828 while (pp[0] || pp[1])
2833 problem = pp - solv->problems.elements;
2834 if (problem >= solv->problems.count)
2840 solver_next_solution(Solver *solv, Id problem, Id solution)
2846 pp = solv->problems.elements + solution;
2847 return pp[0] || pp[1] ? solution : 0;
2849 pp = solv->problems.elements + solution;
2850 while (pp[0] || pp[1])
2853 solution = pp - solv->problems.elements;
2854 return pp[0] || pp[1] ? solution : 0;
2858 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
2861 element = element ? element + 2 : solution;
2862 pp = solv->problems.elements + element;
2863 if (!(pp[0] || pp[1]))
2872 * create obsoletesmap from solver decisions
2873 * required for decision handling
2877 create_decisions_obsoletesmap(Solver *solv)
2879 Pool *pool = solv->pool;
2880 Repo *installed = solv->installed;
2881 Id p, *obsoletesmap = NULL;
2885 obsoletesmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
2888 for (i = 0; i < solv->decisionq.count; i++)
2892 n = solv->decisionq.elements[i];
2895 if (n == SYSTEMSOLVABLE)
2897 s = pool->solvables + n;
2898 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2900 FOR_PROVIDES(p, pp, s->name)
2901 if (s->name == pool->solvables[p].name)
2903 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2905 obsoletesmap[p] = n;
2910 for (i = 0; i < solv->decisionq.count; i++)
2915 n = solv->decisionq.elements[i];
2918 if (n == SYSTEMSOLVABLE)
2920 s = pool->solvables + n;
2921 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2925 obsp = s->repo->idarraydata + s->obsoletes;
2926 while ((obs = *obsp++) != 0)
2927 FOR_PROVIDES(p, pp, obs)
2929 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2931 obsoletesmap[p] = n;
2937 return obsoletesmap;
2946 printdecisions(Solver *solv)
2948 Pool *pool = solv->pool;
2949 Repo *installed = solv->installed;
2950 Id p, *obsoletesmap = create_decisions_obsoletesmap( solv );
2954 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions -----\n");
2956 /* print solvables to be erased */
2960 FOR_REPO_SOLVABLES(installed, p, s)
2962 if (solv->decisionmap[p] >= 0)
2964 if (obsoletesmap[p])
2966 POOL_DEBUG(SAT_DEBUG_RESULT, "erase %s\n", solvable2str(pool, s));
2970 /* print solvables to be installed */
2972 for (i = 0; i < solv->decisionq.count; i++)
2975 p = solv->decisionq.elements[i];
2978 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2981 s = pool->solvables + p;
2982 POOL_DEBUG(SAT_DEBUG_SCHUBI, "level of %s is %d\n", solvable2str(pool, s), p);
2986 if (p == SYSTEMSOLVABLE)
2988 POOL_DEBUG(SAT_DEBUG_SCHUBI, "SYSTEMSOLVABLE\n");
2991 s = pool->solvables + p;
2992 if (installed && s->repo == installed)
2995 if (!obsoletesmap[p])
2997 POOL_DEBUG(SAT_DEBUG_RESULT, "install %s", solvable2str(pool, s));
3001 POOL_DEBUG(SAT_DEBUG_RESULT, "update %s", solvable2str(pool, s));
3002 POOL_DEBUG(SAT_DEBUG_RESULT, " (obsoletes");
3003 for (j = installed->start; j < installed->end; j++)
3004 if (obsoletesmap[j] == p)
3005 POOL_DEBUG(SAT_DEBUG_RESULT, " %s", solvable2str(pool, pool->solvables + j));
3006 POOL_DEBUG(SAT_DEBUG_RESULT, ")");
3008 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3011 sat_free(obsoletesmap);
3013 if (solv->recommendations.count)
3015 POOL_DEBUG(SAT_DEBUG_RESULT, "\nrecommended packages:\n");
3016 for (i = 0; i < solv->recommendations.count; i++)
3018 s = pool->solvables + solv->recommendations.elements[i];
3019 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s\n", solvable2str(pool, s));
3023 if (solv->suggestions.count)
3025 POOL_DEBUG(SAT_DEBUG_RESULT, "\nsuggested packages:\n");
3026 for (i = 0; i < solv->suggestions.count; i++)
3028 s = pool->solvables + solv->suggestions.elements[i];
3029 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s\n", solvable2str(pool, s));
3032 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions end -----\n");
3035 /* this is basically the reverse of addrpmrulesforsolvable */
3037 solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp)
3039 Pool *pool = solv->pool;
3040 Repo *installed = solv->installed;
3044 Id p, *pp, req, *reqp, con, *conp, obs, *obsp, *dp;
3047 if (rid >= solv->systemrules)
3050 *sourcep = solv->installed->start + (rid - solv->systemrules);
3052 return SOLVER_PROBLEM_UPDATE_RULE;
3054 if (rid >= solv->jobrules)
3057 r = solv->rules + rid;
3058 p = solv->ruletojob.elements[rid - solv->jobrules];
3059 *depp = job->elements[p + 1];
3061 *targetp = job->elements[p];
3062 if (r->d == 0 && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
3063 return SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP;
3064 return SOLVER_PROBLEM_JOB_RULE;
3069 /*XXX OBSOLETE CODE */
3070 /* a rpm rule assertion */
3071 assert(rid != -SYSTEMSOLVABLE);
3072 s = pool->solvables - rid;
3073 if (installed && !solv->fixsystem && s->repo == installed)
3075 assert(!dontfix); /* dontfix packages never have a neg assertion */
3076 /* see why the package is not installable */
3077 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
3082 return SOLVER_PROBLEM_NOT_INSTALLABLE;
3084 /* check requires */
3085 assert(s->requires);
3086 reqp = s->repo->idarraydata + s->requires;
3087 while ((req = *reqp++) != 0)
3089 if (req == SOLVABLE_PREREQMARKER)
3091 dp = pool_whatprovides(pool, req);
3099 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
3102 r = solv->rules + rid;
3104 if (r->d == 0 && r->w2 == 0)
3106 /* a rpm rule assertion */
3107 s = pool->solvables - r->p;
3108 if (installed && !solv->fixsystem && s->repo == installed)
3110 assert(!dontfix); /* dontfix packages never have a neg assertion */
3113 /* see why the package is not installable */
3114 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
3117 return SOLVER_PROBLEM_NOT_INSTALLABLE;
3119 /* check requires */
3120 assert(s->requires);
3121 reqp = s->repo->idarraydata + s->requires;
3122 while ((req = *reqp++) != 0)
3124 if (req == SOLVABLE_PREREQMARKER)
3126 dp = pool_whatprovides(pool, req);
3132 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
3134 s = pool->solvables - r->p;
3135 if (installed && !solv->fixsystem && s->repo == installed)
3137 if (r->d == 0 && r->w2 < 0)
3139 /* a package conflict */
3140 Solvable *s2 = pool->solvables - r->w2;
3143 if (installed && !solv->fixsystem && s2->repo == installed)
3146 /* if both packages have the same name and at least one of them
3147 * is not installed, they conflict */
3148 if (s->name == s2->name && (!installed || (s->repo != installed || s2->repo != installed)))
3153 return SOLVER_PROBLEM_SAME_NAME;
3156 /* check conflicts in both directions */
3159 conp = s->repo->idarraydata + s->conflicts;
3160 while ((con = *conp++) != 0)
3162 FOR_PROVIDES(p, pp, con)
3164 if (dontfix && pool->solvables[p].repo == installed)
3171 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
3177 conp = s2->repo->idarraydata + s2->conflicts;
3178 while ((con = *conp++) != 0)
3180 FOR_PROVIDES(p, pp, con)
3182 if (dontfix2 && pool->solvables[p].repo == installed)
3189 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
3193 /* check obsoletes in both directions */
3194 if ((!installed || s->repo != installed) && s->obsoletes)
3196 obsp = s->repo->idarraydata + s->obsoletes;
3197 while ((obs = *obsp++) != 0)
3199 FOR_PROVIDES(p, pp, obs)
3206 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3210 if ((!installed || s2->repo != installed) && s2->obsoletes)
3212 obsp = s2->repo->idarraydata + s2->obsoletes;
3213 while ((obs = *obsp++) != 0)
3215 FOR_PROVIDES(p, pp, obs)
3222 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3226 /* all cases checked, can't happen */
3229 /* simple requires */
3230 assert(s->requires);
3231 reqp = s->repo->idarraydata + s->requires;
3232 while ((req = *reqp++) != 0)
3234 if (req == SOLVABLE_PREREQMARKER)
3236 dp = pool_whatprovides(pool, req);
3239 if (*dp == r->w2 && dp[1] == 0)
3242 else if (dp - pool->whatprovidesdata == r->d)
3249 return SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE;
3253 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp)
3256 Id lreqr, lconr, lsysr, ljobr;
3260 lreqr = lconr = lsysr = ljobr = 0;
3261 while ((rid = solv->learnt_pool.elements[idx++]) != 0)
3264 if (rid >= solv->learntrules)
3265 findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr);
3266 else if (rid >= solv->systemrules)
3271 else if (rid >= solv->jobrules)
3278 r = solv->rules + rid;
3279 if (!r->d && r->w2 < 0)
3286 if (r->d == 0 && r->w2 == 0 && !reqassert)
3288 /* prefer assertions (XXX: bad idea?) */
3294 else if (solv->installed && r->p < 0 && solv->pool->solvables[-r->p].repo == solv->installed)
3296 /* prefer rules of installed packages */
3297 Id op = *reqrp >= 0 ? solv->rules[*reqrp].p : -*reqrp;
3298 if (op <= 0 || solv->pool->solvables[op].repo != solv->installed)
3307 /* assertion, counts as require rule */
3308 /* ignore system solvable as we need useful info */
3309 if (rid == -SYSTEMSOLVABLE)
3311 if (!*reqrp || !reqassert)
3316 else if (solv->installed && solv->pool->solvables[-rid].repo == solv->installed)
3318 /* prefer rules of installed packages */
3319 Id op = *reqrp >= 0 ? solv->rules[*reqrp].p : -*reqrp;
3320 if (op <= 0 || solv->pool->solvables[op].repo != solv->installed)
3326 if (!*reqrp && lreqr)
3328 if (!*conrp && lconr)
3330 if (!*jobrp && ljobr)
3332 if (!*sysrp && lsysr)
3337 solver_findproblemrule(Solver *solv, Id problem)
3339 Id reqr, conr, sysr, jobr;
3340 Id idx = solv->problems.elements[problem - 1];
3341 reqr = conr = sysr = jobr = 0;
3342 findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr);
3355 printprobleminfo(Solver *solv, Queue *job, Id problem)
3357 Pool *pool = solv->pool;
3359 Id dep, source, target;
3362 probr = solver_findproblemrule(solv, problem);
3363 switch (solver_problemruleinfo(solv, job, probr, &dep, &source, &target))
3365 case SOLVER_PROBLEM_UPDATE_RULE:
3366 s = pool_id2solvable(pool, source);
3367 POOL_DEBUG(SAT_DEBUG_RESULT, "problem with installed package %s\n", solvable2str(pool, s));
3369 case SOLVER_PROBLEM_JOB_RULE:
3370 POOL_DEBUG(SAT_DEBUG_RESULT, "conflicting requests\n");
3372 case SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP:
3373 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides requested %s\n", dep2str(pool, dep));
3375 case SOLVER_PROBLEM_NOT_INSTALLABLE:
3376 s = pool_id2solvable(pool, source);
3377 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s is not installable\n", solvable2str(pool, s));
3379 case SOLVER_PROBLEM_NOTHING_PROVIDES_DEP:
3380 s = pool_id2solvable(pool, source);
3381 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides %s needed by %s\n", dep2str(pool, dep), solvable2str(pool, s));
3383 case SOLVER_PROBLEM_SAME_NAME:
3384 s = pool_id2solvable(pool, source);
3385 s2 = pool_id2solvable(pool, target);
3386 POOL_DEBUG(SAT_DEBUG_RESULT, "cannot install both %s and %s\n", solvable2str(pool, s), solvable2str(pool, s2));
3388 case SOLVER_PROBLEM_PACKAGE_CONFLICT:
3389 s = pool_id2solvable(pool, source);
3390 s2 = pool_id2solvable(pool, target);
3391 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s conflicts with %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3393 case SOLVER_PROBLEM_PACKAGE_OBSOLETES:
3394 s = pool_id2solvable(pool, source);
3395 s2 = pool_id2solvable(pool, target);
3396 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s obsoletes %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3398 case SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE:
3399 s = pool_id2solvable(pool, source);
3400 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s requires %s, but none of the providers can be installed\n", solvable2str(pool, s), dep2str(pool, dep));
3406 printsolutions(Solver *solv, Queue *job)
3408 Pool *pool = solv->pool;
3410 Id p, rp, how, what;
3411 Id problem, solution, element;
3414 POOL_DEBUG(SAT_DEBUG_RESULT, "Encountered problems! Here are the solutions:\n\n");
3417 while ((problem = solver_next_problem(solv, problem)) != 0)
3419 POOL_DEBUG(SAT_DEBUG_RESULT, "Problem %d:\n", pcnt++);
3420 POOL_DEBUG(SAT_DEBUG_RESULT, "====================================\n");
3421 printprobleminfo(solv, job, problem);
3422 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3424 while ((solution = solver_next_solution(solv, problem, solution)) != 0)
3427 while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
3431 /* job, rp is index into job queue */
3432 how = job->elements[rp - 1] & ~SOLVER_WEAK;
3433 what = job->elements[rp];
3436 case SOLVER_INSTALL_SOLVABLE:
3437 s = pool->solvables + what;
3438 if (solv->installed && s->repo == solv->installed)
3439 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not keep %s installed\n", solvable2str(pool, s));
3441 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", solvable2str(pool, s));
3443 case SOLVER_ERASE_SOLVABLE:
3444 s = pool->solvables + what;
3445 if (solv->installed && s->repo == solv->installed)
3446 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", solvable2str(pool, s));
3448 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not forbid installation of %s\n", solvable2str(pool, s));
3450 case SOLVER_INSTALL_SOLVABLE_NAME:
3451 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", dep2str(pool, what));
3453 case SOLVER_ERASE_SOLVABLE_NAME:
3454 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", dep2str(pool, what));
3456 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3457 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install a solvable providing %s\n", dep2str(pool, what));
3459 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3460 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall all solvables providing %s\n", dep2str(pool, what));
3462 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3463 s = pool->solvables + what;
3464 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install most recent version of %s\n", solvable2str(pool, s));
3467 POOL_DEBUG(SAT_DEBUG_RESULT, "- do something different\n");
3473 /* policy, replace p with rp */
3474 s = pool->solvables + p;
3475 sd = rp ? pool->solvables + rp : 0;
3479 if (!solv->allowdowngrade && evrcmp(pool, s->evr, sd->evr, EVRCMP_MATCH_RELEASE) > 0)
3481 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow downgrade of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3484 if (!solv->allowarchchange && s->name == sd->name && s->arch != sd->arch && policy_illegal_archchange(solv, s, sd))
3486 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow architecture change of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3489 if (!solv->allowvendorchange && s->name == sd->name && s->vendor != sd->vendor && policy_illegal_vendorchange(solv, s, sd))
3492 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));
3494 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));
3498 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow replacement of %s with %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3502 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow deinstallation of %s\n", solvable2str(pool, s));
3507 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3513 /* create reverse obsoletes map for installed solvables
3515 * for each installed solvable find which packages with *different* names
3516 * obsolete the solvable.
3517 * this index is used in policy_findupdatepackages if noupdateprovide is set.
3521 create_obsolete_index(Solver *solv)
3523 Pool *pool = solv->pool;
3525 Repo *installed = solv->installed;
3526 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
3529 if (!installed || !installed->nsolvables)
3531 solv->obsoletes = obsoletes = sat_calloc(installed->end - installed->start, sizeof(Id));
3532 for (i = 1; i < pool->nsolvables; i++)
3534 s = pool->solvables + i;
3535 if (s->repo == installed)
3539 if (!pool_installable(pool, s))
3541 obsp = s->repo->idarraydata + s->obsoletes;
3542 while ((obs = *obsp++) != 0)
3543 FOR_PROVIDES(p, pp, obs)
3545 if (pool->solvables[p].repo != installed)
3547 if (pool->solvables[p].name == s->name)
3549 obsoletes[p - installed->start]++;
3553 for (i = 0; i < installed->nsolvables; i++)
3556 n += obsoletes[i] + 1;
3559 solv->obsoletes_data = obsoletes_data = sat_calloc(n + 1, sizeof(Id));
3560 POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
3561 for (i = pool->nsolvables - 1; i > 0; i--)
3563 s = pool->solvables + i;
3564 if (s->repo == installed)
3568 if (!pool_installable(pool, s))
3570 obsp = s->repo->idarraydata + s->obsoletes;
3571 while ((obs = *obsp++) != 0)
3572 FOR_PROVIDES(p, pp, obs)
3574 if (pool->solvables[p].repo != installed)
3576 if (pool->solvables[p].name == s->name)
3578 p -= installed->start;
3579 if (obsoletes_data[obsoletes[p]] != i)
3580 obsoletes_data[--obsoletes[p]] = i;
3586 removedisabledconflicts(Solver *solv, Queue *removed)
3588 Pool *pool = solv->pool;
3593 Id *decisionmap = solv->decisionmap;
3595 POOL_DEBUG(SAT_DEBUG_STATS, "removedisabledconflicts\n");
3596 queue_empty(removed);
3597 for (i = 0; i < solv->decisionq.count; i++)
3599 p = solv->decisionq.elements[i];
3602 /* a conflict. we never do conflicts on free decisions, so there
3603 * must have been an unit rule */
3604 why = solv->decisionq_why.elements[i];
3606 r = solv->rules + why;
3607 if (!r->w1 && decisionmap[-p])
3609 /* rule is now disabled, remove from decisionmap */
3610 POOL_DEBUG(SAT_DEBUG_STATS, "removing conflict for package %s[%d]\n", solvable2str(pool, pool->solvables - p), -p);
3611 queue_push(removed, -p);
3612 queue_push(removed, decisionmap[-p]);
3613 decisionmap[-p] = 0;
3616 if (!removed->count)
3618 /* we removed some confliced packages. some of them might still
3619 * be in conflict, so search for unit rules and re-conflict */
3621 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
3623 if (i == solv->nrules)
3626 r = solv->rules + i;
3632 if (r->p < 0 && !decisionmap[-r->p])
3638 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
3640 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
3645 if (r->p < 0 && decisionmap[-r->p] == 0)
3647 if (new || DECISIONMAP_FALSE(r->p))
3649 dp = pool->whatprovidesdata + r->d;
3650 while ((p = *dp++) != 0)
3652 if (new && p == new)
3654 if (p < 0 && decisionmap[-p] == 0)
3663 else if (!DECISIONMAP_FALSE(p))
3673 POOL_DEBUG(SAT_DEBUG_STATS, "re-conflicting package %s[%d]\n", solvable2str(pool, pool->solvables - new), -new);
3674 decisionmap[-new] = -1;
3675 n = 0; /* redo all rules */
3681 /*-----------------------------------------------------------------*/
3691 solver_solve(Solver *solv, Queue *job)
3693 Pool *pool = solv->pool;
3694 Repo *installed = solv->installed;
3697 Map addedmap; /* '1' == have rpm-rules for solvable */
3698 Id how, what, weak, name, p, *pp, d;
3702 /* create whatprovides if not already there */
3703 if (!pool->whatprovides)
3704 pool_createwhatprovides(pool);
3706 /* create obsolete index if needed */
3707 if (solv->noupdateprovide)
3708 create_obsolete_index(solv);
3711 * create basic rule set of all involved packages
3712 * use addedmap bitmap to make sure we don't create rules twice
3716 map_init(&addedmap, pool->nsolvables);
3720 * always install our system solvable
3722 MAPSET(&addedmap, SYSTEMSOLVABLE);
3723 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
3724 queue_push(&solv->decisionq_why, 0);
3725 solv->decisionmap[SYSTEMSOLVABLE] = 1;
3728 * create rules for all package that could be involved with the solving
3729 * so called: rpm rules
3734 oldnrules = solv->nrules;
3735 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
3736 FOR_REPO_SOLVABLES(installed, p, s)
3737 addrpmrulesforsolvable(solv, s, &addedmap);
3738 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3739 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
3740 oldnrules = solv->nrules;
3741 FOR_REPO_SOLVABLES(installed, p, s)
3742 addrpmrulesforupdaters(solv, s, &addedmap, 1);
3743 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3746 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
3747 oldnrules = solv->nrules;
3748 for (i = 0; i < job->count; i += 2)
3750 how = job->elements[i] & ~SOLVER_WEAK;
3751 what = job->elements[i + 1];
3755 case SOLVER_INSTALL_SOLVABLE:
3756 addrpmrulesforsolvable(solv, pool->solvables + what, &addedmap);
3758 case SOLVER_INSTALL_SOLVABLE_NAME:
3759 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
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)
3771 addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3774 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3775 /* dont allow downgrade */
3776 addrpmrulesforupdaters(solv, pool->solvables + what, &addedmap, 0);
3780 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3782 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for recommended/suggested packages ***\n");
3784 oldnrules = solv->nrules;
3785 addrpmrulesforweak(solv, &addedmap);
3786 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3788 IF_POOLDEBUG (SAT_DEBUG_STATS)
3790 int possible = 0, installable = 0;
3791 for (i = 1; i < pool->nsolvables; i++)
3793 if (pool_installable(pool, pool->solvables + i))
3795 if (MAPTST(&addedmap, i))
3798 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
3802 * first pass done, we now have all the rpm rules we need.
3803 * unify existing rules before going over all job rules and
3805 * at this point the system is always solvable,
3806 * as an empty system (remove all packages) is a valid solution
3809 unifyrules(solv); /* remove duplicate rpm rules */
3810 solv->directdecisions = solv->decisionq.count;
3812 POOL_DEBUG(SAT_DEBUG_STATS, "decisions so far: %d\n", solv->decisionq.count);
3813 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
3814 printdecisions (solv);
3817 * now add all job rules
3820 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
3822 solv->jobrules = solv->nrules;
3824 for (i = 0; i < job->count; i += 2)
3826 oldnrules = solv->nrules;
3828 how = job->elements[i] & ~SOLVER_WEAK;
3829 weak = job->elements[i] & SOLVER_WEAK;
3830 what = job->elements[i + 1];
3833 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
3834 s = pool->solvables + what;
3835 POOL_DEBUG(SAT_DEBUG_JOB, "job: install solvable %s\n", solvable2str(pool, s));
3836 addrule(solv, what, 0); /* install by Id */
3837 queue_push(&solv->ruletojob, i);
3839 queue_push(&solv->weakruleq, solv->nrules - 1);
3841 case SOLVER_ERASE_SOLVABLE:
3842 s = pool->solvables + what;
3843 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase solvable %s\n", solvable2str(pool, s));
3844 addrule(solv, -what, 0); /* remove by Id */
3845 queue_push(&solv->ruletojob, i);
3847 queue_push(&solv->weakruleq, solv->nrules - 1);
3849 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
3850 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3851 if (how == SOLVER_INSTALL_SOLVABLE_NAME)
3852 POOL_DEBUG(SAT_DEBUG_JOB, "job: install name %s\n", dep2str(pool, what));
3853 if (how == SOLVER_INSTALL_SOLVABLE_PROVIDES)
3854 POOL_DEBUG(SAT_DEBUG_JOB, "job: install provides %s\n", dep2str(pool, what));
3856 name = (how == SOLVER_INSTALL_SOLVABLE_NAME) ? what : 0;
3857 while (ISRELDEP(name))
3859 Reldep *rd = GETRELDEP(pool, name);
3862 FOR_PROVIDES(p, pp, what)
3864 /* if by name, ensure that the name matches */
3865 if (name && pool->solvables[p].name != name)
3871 /* no provider, make this an impossible rule */
3872 queue_push(&q, -SYSTEMSOLVABLE);
3875 p = queue_shift(&q); /* get first provider */
3877 d = 0; /* single provider ? -> make assertion */
3879 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
3880 addrule(solv, p, d); /* add 'requires' rule */
3881 queue_push(&solv->ruletojob, i);
3883 queue_push(&solv->weakruleq, solv->nrules - 1);
3885 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
3886 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3887 if (how == SOLVER_ERASE_SOLVABLE_NAME)
3888 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase name %s\n", dep2str(pool, what));
3889 if (how == SOLVER_ERASE_SOLVABLE_PROVIDES)
3890 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase provides %s\n", dep2str(pool, what));
3891 name = (how == SOLVER_ERASE_SOLVABLE_NAME) ? what : 0;
3892 while (ISRELDEP(name))
3894 Reldep *rd = GETRELDEP(pool, name);
3897 FOR_PROVIDES(p, pp, what)
3899 /* if by name, ensure that the name matches */
3900 if (name && pool->solvables[p].name != name)
3902 addrule(solv, -p, 0); /* add 'remove' rule */
3903 queue_push(&solv->ruletojob, i);
3905 queue_push(&solv->weakruleq, solv->nrules - 1);
3908 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
3909 s = pool->solvables + what;
3910 POOL_DEBUG(SAT_DEBUG_JOB, "job: update %s\n", solvable2str(pool, s));
3911 addupdaterule(solv, s, 0);
3912 queue_push(&solv->ruletojob, i);
3914 queue_push(&solv->weakruleq, solv->nrules - 1);
3917 IF_POOLDEBUG (SAT_DEBUG_JOB)
3920 if (solv->nrules == oldnrules)
3921 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created\n");
3922 for (j = oldnrules; j < solv->nrules; j++)
3924 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
3925 printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
3929 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
3932 * now add system rules
3936 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add system rules ***\n");
3939 solv->systemrules = solv->nrules;
3942 * create rules for updating installed solvables
3946 if (installed && !solv->allowuninstall)
3947 { /* loop over all installed solvables */
3948 /* we create all update rules, but disable some later on depending on the job */
3949 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3951 /* no system rules for patch atoms */
3952 if (s->freshens && !s->supplements)
3954 const char *name = id2str(pool, s->name);
3955 if (name[0] == 'a' && !strncmp(name, "atom:", 5))
3957 addrule(solv, 0, 0);
3961 if (s->repo == installed)
3962 addupdaterule(solv, s, 0); /* allowall = 0 */
3964 addrule(solv, 0, 0); /* create dummy rule */
3966 /* consistency check: we added a rule for _every_ system solvable */
3967 assert(solv->nrules - solv->systemrules == installed->end - installed->start);
3970 /* create special weak system rules */
3971 /* those are used later on to keep a version of the installed packages in
3973 if (installed && installed->nsolvables)
3975 solv->weaksystemrules = sat_calloc(installed->end - installed->start, sizeof(Id));
3976 FOR_REPO_SOLVABLES(installed, p, s)
3978 policy_findupdatepackages(solv, s, &q, 1);
3980 solv->weaksystemrules[p - installed->start] = pool_queuetowhatprovides(pool, &q);
3984 /* free unneeded memory */
3985 map_free(&addedmap);
3988 /* create weak map */
3989 map_init(&solv->weakrulemap, solv->nrules);
3990 for (i = 0; i < solv->weakruleq.count; i++)
3992 p = solv->weakruleq.elements[i];
3993 MAPSET(&solv->weakrulemap, p);
3996 /* all new rules are learnt after this point */
3997 solv->learntrules = solv->nrules;
3999 /* disable system rules that conflict with our job */
4000 disableupdaterules(solv, job, -1);
4002 /* make decisions based on job/system assertions */
4003 makeruledecisions(solv);
4005 /* create watches chains */
4008 POOL_DEBUG(SAT_DEBUG_STATS, "problems so far: %d\n", solv->problems.count);
4011 run_solver(solv, 1, solv->dontinstallrecommended ? 0 : 1);
4014 if (!solv->problems.count)
4019 /* disable all weak erase rules for recommened/suggestes search */
4020 for (i = 1, r = solv->rules + i; i < solv->learntrules; i++, r++)
4022 if (!MAPTST(&solv->weakrulemap, i))
4026 disablerule(solv, r);
4031 enabledisablelearntrules(solv);
4032 removedisabledconflicts(solv, &redoq);
4036 /* find recommended packages */
4037 /* if q.count == 0 we already found all recommended in the
4039 if (!solv->problems.count && (redoq.count || solv->dontinstallrecommended))
4041 Id rec, *recp, p, *pp;
4043 /* create map of all suggests that are still open */
4044 solv->recommends_index = -1;
4045 MAPZERO(&solv->recommendsmap);
4046 for (i = 0; i < solv->decisionq.count; i++)
4048 p = solv->decisionq.elements[i];
4051 s = pool->solvables + p;
4054 recp = s->repo->idarraydata + s->recommends;
4055 while ((rec = *recp++) != 0)
4057 FOR_PROVIDES(p, pp, rec)
4058 if (solv->decisionmap[p] > 0)
4061 continue; /* p != 0: already fulfilled */
4062 FOR_PROVIDES(p, pp, rec)
4063 MAPSET(&solv->recommendsmap, p);
4067 for (i = 1; i < pool->nsolvables; i++)
4069 if (solv->decisionmap[i] != 0)
4071 s = pool->solvables + i;
4072 if (!MAPTST(&solv->recommendsmap, i))
4074 if (!s->supplements)
4076 if (!pool_installable(pool, s))
4078 if (!solver_is_supplementing(solv, s))
4081 if (solv->dontinstallrecommended)
4082 queue_push(&solv->recommendations, i);
4084 queue_pushunique(&solv->recommendations, i);
4086 /* we use MODE_SUGGEST here so that repo prio is ignored */
4087 policy_filter_unwanted(solv, &solv->recommendations, 0, POLICY_MODE_SUGGEST);
4090 /* find suggested packages */
4091 if (!solv->problems.count)
4093 Id sug, *sugp, p, *pp;
4095 /* create map of all suggests that are still open */
4096 solv->recommends_index = -1;
4097 MAPZERO(&solv->suggestsmap);
4098 for (i = 0; i < solv->decisionq.count; i++)
4100 p = solv->decisionq.elements[i];
4103 s = pool->solvables + p;
4106 sugp = s->repo->idarraydata + s->suggests;
4107 while ((sug = *sugp++) != 0)
4109 FOR_PROVIDES(p, pp, sug)
4110 if (solv->decisionmap[p] > 0)
4113 continue; /* already fulfilled */
4114 FOR_PROVIDES(p, pp, sug)
4115 MAPSET(&solv->suggestsmap, p);
4119 for (i = 1; i < pool->nsolvables; i++)
4121 if (solv->decisionmap[i] != 0)
4123 s = pool->solvables + i;
4124 if (!MAPTST(&solv->suggestsmap, i))
4128 if (!pool_installable(pool, s))
4130 if (!solver_is_enhancing(solv, s))
4133 queue_push(&solv->suggestions, i);
4135 policy_filter_unwanted(solv, &solv->suggestions, 0, POLICY_MODE_SUGGEST);
4140 /* restore decisionmap */
4141 for (i = 0; i < redoq.count; i += 2)
4142 solv->decisionmap[redoq.elements[i]] = redoq.elements[i + 1];
4146 if (solv->problems.count)
4147 problems_to_solutions(solv, job);
4150 /***********************************************************************/
4153 solver_calc_changed_pkgs(Solver *solv, Queue *pkgs)
4155 Pool *pool = solv->pool;
4161 /* create list of solvables that have to be installed */
4162 /* (this is actually just a simple sort) */
4163 map_init(&installmap, pool->nsolvables);
4164 for (i = 1; i < solv->decisionq.count; i++)
4166 Id p = solv->decisionq.elements[i];
4169 s = pool->solvables + p;
4172 if (solv->installed && s->repo == solv->installed)
4174 MAPSET(&installmap, p);
4176 for (p = 1; p < pool->nsolvables; p++)
4177 if (MAPTST(&installmap, p))
4178 queue_push(pkgs, p);
4179 map_free(&installmap);
4180 /* run through erase solvable dudata */
4181 if (solv->installed)
4183 FOR_REPO_SOLVABLES(solv->installed, p, s)
4185 if (solv->decisionmap[p] < 0)
4186 queue_push(pkgs, -p);
4192 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
4196 solver_calc_changed_pkgs(solv, &pkgs);
4197 pool_calc_duchanges(solv->pool, &pkgs, mps, nmps);
4202 solver_calc_installsizechange(Solver *solv)
4207 solver_calc_changed_pkgs(solv, &pkgs);
4208 change = pool_calc_installsizechange(solv->pool, &pkgs);