2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * SAT based dependency solver
26 #include "solverdebug.h"
28 #define CODE10 0 /* set to '1' to enable patch atoms */
30 #define RULES_BLOCK 63
32 /********************************************************************
34 * dependency check helpers
38 /*-------------------------------------------------------------------
39 * handle split provides
43 solver_splitprovides(Solver *solv, Id dep)
45 Pool *pool = solv->pool;
50 if (!solv->dosplitprovides || !solv->installed)
54 rd = GETRELDEP(pool, dep);
55 if (rd->flags != REL_WITH)
57 FOR_PROVIDES(p, pp, dep)
59 s = pool->solvables + p;
60 if (s->repo == solv->installed && s->name == rd->name)
67 /*-------------------------------------------------------------------
68 * solver_dep_installed
72 solver_dep_installed(Solver *solv, Id dep)
75 Pool *pool = solv->pool;
80 Reldep *rd = GETRELDEP(pool, dep);
81 if (rd->flags == REL_AND)
83 if (!solver_dep_installed(solv, rd->name))
85 return solver_dep_installed(solv, rd->evr);
87 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
88 return solver_dep_installed(solv, rd->evr);
90 FOR_PROVIDES(p, pp, dep)
92 if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
100 /*-------------------------------------------------------------------
101 * Check if dependenc is possible
103 * this mirrors solver_dep_fulfilled
104 * but uses map m instead of the decisionmap
108 dep_possible(Solver *solv, Id dep, Map *m)
110 Pool *pool = solv->pool;
115 Reldep *rd = GETRELDEP(pool, dep);
116 if (rd->flags == REL_AND)
118 if (!dep_possible(solv, rd->name, m))
120 return dep_possible(solv, rd->evr, m);
122 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
123 return solver_splitprovides(solv, rd->evr);
124 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
125 return solver_dep_installed(solv, rd->evr);
127 FOR_PROVIDES(p, pp, dep)
135 /********************************************************************
139 * - unify rules, remove duplicates
142 static Pool *unifyrules_sortcmp_data;
144 /*-------------------------------------------------------------------
146 * compare rules for unification sort
151 unifyrules_sortcmp(const void *ap, const void *bp)
153 Pool *pool = unifyrules_sortcmp_data;
154 Rule *a = (Rule *)ap;
155 Rule *b = (Rule *)bp;
161 return x; /* p differs */
164 if (a->d == 0 && b->d == 0)
165 return a->w2 - b->w2; /* assertion: return w2 diff */
167 if (a->d == 0) /* a is assertion, b not */
169 x = a->w2 - pool->whatprovidesdata[b->d];
173 if (b->d == 0) /* b is assertion, a not */
175 x = pool->whatprovidesdata[a->d] - b->w2;
179 /* compare whatprovidesdata */
180 ad = pool->whatprovidesdata + a->d;
181 bd = pool->whatprovidesdata + b->d;
183 if ((x = *ad++ - *bd++) != 0)
189 /*-------------------------------------------------------------------
192 * go over all rules and remove duplicates
196 unifyrules(Solver *solv)
198 Pool *pool = solv->pool;
202 if (solv->nrules <= 1) /* nothing to unify */
205 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules -----\n");
207 /* sort rules first */
208 unifyrules_sortcmp_data = solv->pool;
209 qsort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp);
216 for (i = j = 1, ir = solv->rules + i; i < solv->nrules; i++, ir++)
218 if (jr && !unifyrules_sortcmp(ir, jr))
219 continue; /* prune! */
220 jr = solv->rules + j++; /* keep! */
225 /* reduced count from nrules to j rules */
226 POOL_DEBUG(SAT_DEBUG_STATS, "pruned rules from %d to %d\n", solv->nrules, j);
228 /* adapt rule buffer */
230 solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
234 IF_POOLDEBUG (SAT_DEBUG_STATS)
241 for (i = 1; i < solv->nrules; i++)
248 dp = solv->pool->whatprovidesdata + r->d;
253 POOL_DEBUG(SAT_DEBUG_STATS, " binary: %d\n", binr);
254 POOL_DEBUG(SAT_DEBUG_STATS, " normal: %d, %d literals\n", solv->nrules - 1 - binr, lits);
256 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules end -----\n");
266 hashrule(Solver *solv, Id p, Id d, int n)
268 unsigned int x = (unsigned int)p;
272 return (x * 37) ^ (unsigned int)d;
273 dp = solv->pool->whatprovidesdata + d;
275 x = (x * 37) ^ (unsigned int)*dp++;
281 /*-------------------------------------------------------------------
287 * p = direct literal; always < 0 for installed rpm rules
288 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
291 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
293 * p < 0 : pkg id of A
294 * d > 0 : Offset in whatprovidesdata (list of providers of b)
296 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
297 * p < 0 : pkg id of A
298 * d < 0 : Id of solvable (e.g. B1)
300 * d == 0: unary rule, assertion => (A) or (-A)
302 * Install: p > 0, d = 0 (A) user requested install
303 * Remove: p < 0, d = 0 (-A) user requested remove
304 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
305 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
306 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider) (binary rule)
307 * ? p > 0, d < 0 (A|-B)
308 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
312 * Direct assertion (no watch needed)( if d <0 ) --> d = 0, w1 = p, w2 = 0
313 * Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
314 * every other : w1 = p, w2 = whatprovidesdata[d];
315 * Disabled rule: w1 = 0
317 * always returns a rule for non-rpm rules
321 addrule(Solver *solv, Id p, Id d)
323 Pool *pool = solv->pool;
327 int n = 0; /* number of literals in rule - 1
328 0 = direct assertion (single literal)
333 /* it often happenes that requires lead to adding the same rpm rule
334 * multiple times, so we prune those duplicates right away to make
335 * the work for unifyrules a bit easier */
337 if (solv->nrules /* we already have rules */
338 && !solv->rpmrules_end) /* but are not done with rpm rules */
340 r = solv->rules + solv->nrules - 1; /* get the last added rule */
341 if (r->p == p && r->d == d && d != 0) /* identical and not user requested */
346 * compute number of literals (n) in rule
351 /* always a binary rule */
353 return 0; /* ignore self conflict */
358 for (dp = pool->whatprovidesdata + d; *dp; dp++, n++)
360 return 0; /* rule is self-fulfilling */
362 if (n == 1) /* have single provider */
363 d = dp[-1]; /* take single literal */
367 if (n == 0 && !solv->rpmrules_end)
369 /* this is a rpm rule assertion, we do not have to allocate it */
370 /* it can be identified by a level of 1 and a zero reason */
371 /* we must not drop those rules from the decisionq when rewinding! */
373 assert(solv->decisionmap[-p] == 0 || solv->decisionmap[-p] == -1);
374 if (solv->decisionmap[-p])
375 return 0; /* already got that one */
376 queue_push(&solv->decisionq, p);
377 queue_push(&solv->decisionq_why, 0);
378 solv->decisionmap[-p] = -1;
385 /* smallest literal first so we can find dups */
386 n = p; p = d; d = n; /* p <-> d */
387 n = 1; /* re-set n, was used as temp var */
391 * check for duplicate
394 /* check if the last added rule (r) is exactly the same as what we're looking for. */
395 if (r && n == 1 && !r->d && r->p == p && r->w2 == d)
396 return r; /* binary rule */
398 /* have n-ary rule with same first literal, check other literals */
399 if (r && n > 1 && r->d && r->p == p)
401 /* Rule where d is an offset in whatprovidesdata */
405 dp2 = pool->whatprovidesdata + r->d;
406 for (dp = pool->whatprovidesdata + d; *dp; dp++, dp2++)
417 /* extend rule buffer */
418 solv->rules = sat_extend(solv->rules, solv->nrules, 1, sizeof(Rule), RULES_BLOCK);
419 r = solv->rules + solv->nrules++; /* point to rule space */
428 /* direct assertion, no watch needed */
444 r->w2 = pool->whatprovidesdata[d];
449 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
451 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " Add rule: ");
452 solver_printrule(solv, SAT_DEBUG_RULE_CREATION, r);
458 /*-------------------------------------------------------------------
463 disablerule(Solver *solv, Rule *r)
469 /*-------------------------------------------------------------------
474 enablerule(Solver *solv, Rule *r)
481 /**********************************************************************************/
483 /* a problem is an item on the solver's problem list. It can either be >0, in that
484 * case it is a update rule, or it can be <0, which makes it refer to a job
485 * consisting of multiple job rules.
489 disableproblem(Solver *solv, Id v)
497 disablerule(solv, solv->rules + v);
501 jp = solv->ruletojob.elements;
502 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++, jp++)
504 disablerule(solv, r);
507 /*-------------------------------------------------------------------
512 enableproblem(Solver *solv, Id v)
520 if (v >= solv->featurerules && v < solv->featurerules_end)
522 /* do not enable feature rule if update rule is enabled */
523 r = solv->rules + (v - solv->featurerules + solv->updaterules);
527 enablerule(solv, solv->rules + v);
528 if (v >= solv->updaterules && v < solv->updaterules_end)
530 /* disable feature rule when enabling update rule */
531 r = solv->rules + (v - solv->updaterules + solv->featurerules);
533 disablerule(solv, r);
538 jp = solv->ruletojob.elements;
539 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++, jp++)
545 /************************************************************************/
548 * make assertion rules into decisions
550 * go through update and job rules and add direct assertions
551 * to the decisionqueue. If we find a conflict, disable rules and
552 * add them to problem queue.
556 makeruledecisions(Solver *solv)
558 Pool *pool = solv->pool;
564 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions ; size decisionq: %d -----\n",solv->decisionq.count);
566 decisionstart = solv->decisionq.count;
567 for (ii = 0; ii < solv->ruleassertions.count; ii++)
569 ri = solv->ruleassertions.elements[ii];
570 r = solv->rules + ri;
572 if (r->d < 0 || !r->p || r->w2) /* disabled, dummy or no assertion */
574 /* do weak rules in phase 2 */
575 if (ri < solv->learntrules && MAPTST(&solv->weakrulemap, ri))
581 if (!solv->decisionmap[vv]) /* if not yet decided */
586 queue_push(&solv->decisionq, v);
587 queue_push(&solv->decisionq_why, r - solv->rules);
588 solv->decisionmap[vv] = v > 0 ? 1 : -1;
589 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
591 Solvable *s = solv->pool->solvables + vv;
593 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", solvable2str(solv->pool, s));
595 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (assertion)\n", solvable2str(solv->pool, s));
600 * check previous decision: is it sane ?
603 if (v > 0 && solv->decisionmap[vv] > 0) /* ok to install */
605 if (v < 0 && solv->decisionmap[vv] < 0) /* ok to remove */
611 * The rule (r) we're currently processing says something
612 * different (v = r->p) than a previous decision (decisionmap[abs(v)])
616 if (ri >= solv->learntrules)
618 /* conflict with a learnt rule */
619 /* can happen when packages cannot be installed for
620 * multiple reasons. */
621 /* we disable the learnt rule in this case */
622 disablerule(solv, r);
627 * find the decision which is the "opposite" of the rule
630 for (i = 0; i < solv->decisionq.count; i++)
631 if (solv->decisionq.elements[i] == -v)
633 assert(i < solv->decisionq.count); /* assert that we found it */
636 * conflict with system solvable ?
639 if (v == -SYSTEMSOLVABLE) {
640 /* conflict with system solvable */
641 queue_push(&solv->problems, solv->learnt_pool.count);
642 queue_push(&solv->learnt_pool, ri);
643 queue_push(&solv->learnt_pool, 0);
644 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
645 if (ri >= solv->jobrules && ri < solv->jobrules_end)
646 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
649 queue_push(&solv->problems, v);
650 queue_push(&solv->problems, 0);
651 disableproblem(solv, v);
655 assert(solv->decisionq_why.elements[i]);
658 * conflict with an rpm rule ?
661 if (solv->decisionq_why.elements[i] < solv->rpmrules_end)
663 /* conflict with rpm rule assertion */
664 queue_push(&solv->problems, solv->learnt_pool.count);
665 queue_push(&solv->learnt_pool, ri);
666 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
667 queue_push(&solv->learnt_pool, 0);
668 assert(v > 0 || v == -SYSTEMSOLVABLE);
669 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
670 if (ri >= solv->jobrules && ri < solv->jobrules_end)
671 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
674 queue_push(&solv->problems, v);
675 queue_push(&solv->problems, 0);
676 disableproblem(solv, v);
681 * conflict with another job or update/feature rule
685 queue_push(&solv->problems, solv->learnt_pool.count);
686 queue_push(&solv->learnt_pool, ri);
687 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
688 queue_push(&solv->learnt_pool, 0);
690 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflicting update/job assertions over literal %d\n", vv);
693 * push all of our rules (can only be feature or job rules)
694 * asserting this literal on the problem stack
697 for (i = solv->featurerules, rr = solv->rules + i; i < solv->learntrules; i++, rr++)
699 if (rr->d < 0 /* disabled */
700 || rr->w2) /* or no assertion */
702 if (rr->p != vv /* not affecting the literal */
705 if (MAPTST(&solv->weakrulemap, i)) /* weak: silently ignore */
708 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
710 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, solv->rules + i);
713 /* is is a job rule ? */
714 if (i >= solv->jobrules && i < solv->jobrules_end)
715 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
717 queue_push(&solv->problems, v);
718 disableproblem(solv, v);
720 queue_push(&solv->problems, 0);
724 * (back up from decisions)
726 while (solv->decisionq.count > decisionstart)
728 v = solv->decisionq.elements[--solv->decisionq.count];
729 --solv->decisionq_why.count;
731 solv->decisionmap[vv] = 0;
733 ii = -1; /* restarts loop at 0 */
737 * phase 2: now do the weak assertions
739 for (ii = 0; ii < solv->ruleassertions.count; ii++)
741 ri = solv->ruleassertions.elements[ii];
742 r = solv->rules + ri;
743 if (r->d < 0 || r->w2) /* disabled or no assertion */
745 if (!MAPTST(&solv->weakrulemap, ri)) /* skip non-weak */
751 * (if not yet decided)
753 if (!solv->decisionmap[vv])
755 queue_push(&solv->decisionq, v);
756 queue_push(&solv->decisionq_why, r - solv->rules);
757 solv->decisionmap[vv] = v > 0 ? 1 : -1;
758 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
760 Solvable *s = solv->pool->solvables + vv;
762 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", solvable2str(solv->pool, s));
764 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (weak assertion)\n", solvable2str(solv->pool, s));
769 * previously decided, sane ?
771 if (v > 0 && solv->decisionmap[vv] > 0)
773 if (v < 0 && solv->decisionmap[vv] < 0)
776 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
777 solver_printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
778 disablerule(solv, r);
781 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions end; size decisionq: %d -----\n",solv->decisionq.count);
785 /*-------------------------------------------------------------------
786 * enable/disable learnt rules
788 * we have enabled or disabled some of our rules. We now reenable all
789 * of our learnt rules but the ones that were learnt from rules that
793 enabledisablelearntrules(Solver *solv)
795 Pool *pool = solv->pool;
800 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
801 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
803 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
804 while ((why = *whyp++) != 0)
806 assert(why > 0 && why < i);
807 if (solv->rules[why].d < 0)
810 /* why != 0: we found a disabled rule, disable the learnt rule */
811 if (why && r->d >= 0)
813 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
815 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "disabling ");
816 solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
818 disablerule(solv, r);
820 else if (!why && r->d < 0)
822 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
824 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "re-enabling ");
825 solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
833 /*-------------------------------------------------------------------
836 * Enable all rules, except learnt rules, which are
837 * - disabled and weak (set in weakrulemap)
842 enableweakrules(Solver *solv)
847 for (i = 1, r = solv->rules + i; i < solv->learntrules; i++, r++)
849 if (r->d >= 0) /* skip non-direct literals */
851 if (!MAPTST(&solv->weakrulemap, i))
858 /* FIXME: bad code ahead, replace as soon as possible */
859 /* FIXME: should probably look at SOLVER_INSTALL_SOLVABLE_ONE_OF */
861 /*-------------------------------------------------------------------
862 * disable update rules
866 disableupdaterules(Solver *solv, Queue *job, int jobidx)
868 Pool *pool = solv->pool;
870 Id how, what, p, *pp;
876 installed = solv->installed;
882 how = job->elements[jobidx] & ~SOLVER_WEAK;
885 case SOLVER_INSTALL_SOLVABLE:
886 case SOLVER_ERASE_SOLVABLE:
887 case SOLVER_ERASE_SOLVABLE_NAME:
888 case SOLVER_ERASE_SOLVABLE_PROVIDES:
894 /* go through all enabled job rules */
895 MAPZERO(&solv->noupdate);
896 for (i = solv->jobrules; i < solv->jobrules_end; i++)
899 if (r->d < 0) /* disabled? */
901 j = solv->ruletojob.elements[i - solv->jobrules];
905 how = job->elements[j] & ~SOLVER_WEAK;
906 what = job->elements[j + 1];
909 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
910 s = pool->solvables + what;
911 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, what))
913 FOR_PROVIDES(p, pp, s->name)
915 if (pool->solvables[p].name != s->name)
917 if (pool->solvables[p].repo == installed)
918 MAPSET(&solv->noupdate, p - installed->start);
921 case SOLVER_ERASE_SOLVABLE:
922 s = pool->solvables + what;
923 if (s->repo == installed)
924 MAPSET(&solv->noupdate, what - installed->start);
926 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
927 case SOLVER_ERASE_SOLVABLE_PROVIDES:
928 FOR_PROVIDES(p, pp, what)
930 if (how == SOLVER_ERASE_SOLVABLE_NAME && !pool_match_nevr(pool, pool->solvables + p, what))
932 if (pool->solvables[p].repo == installed)
933 MAPSET(&solv->noupdate, p - installed->start);
941 /* fixup update rule status */
944 /* we just disabled job #jobidx. enable all update rules
945 * that aren't disabled by the remaining job rules */
946 how = job->elements[jobidx] & ~SOLVER_WEAK;
947 what = job->elements[jobidx + 1];
950 case SOLVER_INSTALL_SOLVABLE:
951 s = pool->solvables + what;
952 FOR_PROVIDES(p, pp, s->name)
954 if (pool->solvables[p].name != s->name)
956 if (pool->solvables[p].repo != installed)
958 if (MAPTST(&solv->noupdate, p - installed->start))
960 r = solv->rules + solv->updaterules + (p - installed->start);
964 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
966 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
967 solver_printrule(solv, SAT_DEBUG_SOLUTIONS, r);
971 case SOLVER_ERASE_SOLVABLE:
972 s = pool->solvables + what;
973 if (s->repo != installed)
975 if (MAPTST(&solv->noupdate, what - installed->start))
977 r = solv->rules + solv->updaterules + (what - installed->start);
981 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
983 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
984 solver_printrule(solv, SAT_DEBUG_SOLUTIONS, r);
987 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
988 case SOLVER_ERASE_SOLVABLE_PROVIDES:
989 FOR_PROVIDES(p, pp, what)
991 if (how == SOLVER_ERASE_SOLVABLE_NAME && !pool_match_nevr(pool, pool->solvables + p, what))
993 if (pool->solvables[p].repo != installed)
995 if (MAPTST(&solv->noupdate, p - installed->start))
997 r = solv->rules + solv->updaterules + (p - installed->start);
1000 enablerule(solv, r);
1001 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
1003 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
1004 solver_printrule(solv, SAT_DEBUG_SOLUTIONS, r);
1014 for (i = 0; i < installed->nsolvables; i++)
1016 r = solv->rules + solv->updaterules + i;
1017 if (r->d >= 0 && MAPTST(&solv->noupdate, i))
1018 disablerule(solv, r); /* was enabled, need to disable */
1019 r = solv->rules + solv->featurerules + i;
1020 if (r->d >= 0 && MAPTST(&solv->noupdate, i))
1021 disablerule(solv, r); /* was enabled, need to disable */
1026 /*-------------------------------------------------------------------
1027 * add patch atom requires
1031 addpatchatomrequires(Solver *solv, Solvable *s, Id *dp, Queue *q, Map *m)
1033 Pool *pool = solv->pool;
1034 Id fre, *frep, p, *pp, ndp;
1040 queue_init_buffer(&fq, qbuf, sizeof(qbuf)/sizeof(*qbuf));
1041 queue_push(&fq, -(s - pool->solvables));
1043 queue_push(&fq, *dp);
1044 ndp = pool_queuetowhatprovides(pool, &fq);
1045 frep = s->repo->idarraydata + s->freshens;
1046 while ((fre = *frep++) != 0)
1048 FOR_PROVIDES(p, pp, fre)
1050 ps = pool->solvables + p;
1051 addrule(solv, -p, ndp);
1059 for (i = 1; i < fq.count; i++)
1071 /*-------------------------------------------------------------------
1073 * add (install) rules for solvable
1075 * s: Solvable for which to add rules
1076 * m: m[s] = 1 for solvables which have rules, prevent rule duplication
1078 * Algorithm: 'visit all nodes of a graph'. The graph nodes are
1079 * solvables, the edges their dependencies.
1080 * Starting from an installed solvable, this will create all rules
1081 * representing the graph created by the solvables dependencies.
1083 * for unfulfilled requirements, conflicts, obsoletes,....
1084 * add a negative assertion for solvables that are not installable
1086 * It will also create rules for all solvables referenced by 's'
1087 * i.e. descend to all providers of requirements of 's'
1092 addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
1094 Pool *pool = solv->pool;
1095 Repo *installed = solv->installed;
1097 /* 'work' queue. keeps Ids of solvables we still have to work on.
1098 And buffer for it. */
1103 /* if to add rules for broken deps ('rpm -V' functionality)
1110 /* Id var and pointer for each dependency
1111 * (not used in parallel)
1118 /* var and ptr for loops */
1120 /* ptr to 'whatprovides' */
1122 /* Id for current solvable 's' */
1125 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable -----\n");
1127 queue_init_buffer(&workq, workqbuf, sizeof(workqbuf)/sizeof(*workqbuf));
1128 queue_push(&workq, s - pool->solvables); /* push solvable Id to work queue */
1130 /* loop until there's no more work left */
1135 * s: Pointer to solvable
1138 n = queue_shift(&workq); /* 'pop' next solvable to work on from queue */
1139 if (MAPTST(m, n)) /* continue if already visited */
1142 MAPSET(m, n); /* mark as visited */
1143 s = pool->solvables + n; /* s = Solvable in question */
1146 if (installed /* Installed system available */
1147 && !solv->fixsystem /* NOT repair errors in rpm dependency graph */
1148 && s->repo == installed) /* solvable is installed? */
1150 dontfix = 1; /* dont care about broken rpm deps */
1154 && s->arch != ARCH_SRC
1155 && s->arch != ARCH_NOSRC
1156 && !pool_installable(pool, s))
1158 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", solvable2str(pool, s), (Id)(s - pool->solvables));
1159 addrule(solv, -n, 0); /* uninstallable */
1163 if (s->freshens && !s->supplements)
1165 const char *name = id2str(pool, s->name);
1166 if (name[0] == 'a' && !strncmp(name, "atom:", 5))
1171 /*-----------------------------------------
1172 * check requires of s
1177 reqp = s->repo->idarraydata + s->requires;
1178 while ((req = *reqp++) != 0) /* go through all requires */
1180 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
1183 /* find list of solvables providing 'req' */
1184 dp = pool_whatprovides(pool, req);
1186 if (*dp == SYSTEMSOLVABLE) /* always installed */
1192 addpatchatomrequires(solv, s, dp, &workq, m);
1198 /* the strategy here is to not insist on dependencies
1199 * that are already broken. so if we find one provider
1200 * that was already installed, we know that the
1201 * dependency was not broken before so we enforce it */
1203 /* check if any of the providers for 'req' is installed */
1204 for (i = 0; (p = dp[i]) != 0; i++)
1206 if (pool->solvables[p].repo == installed)
1207 break; /* provider was installed */
1209 /* didn't find an installed provider: previously broken dependency */
1212 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", dep2str(pool, req), solvable2str(pool, s));
1219 /* nothing provides req! */
1220 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", solvable2str(pool, s), (Id)(s - pool->solvables), dep2str(pool, req));
1221 addrule(solv, -n, 0); /* mark requestor as uninstallable */
1225 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1227 POOL_DEBUG(SAT_DEBUG_RULE_CREATION," %s requires %s\n", solvable2str(pool, s), dep2str(pool, req));
1228 for (i = 0; dp[i]; i++)
1229 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " provided by %s\n", solvable2str(pool, pool->solvables + dp[i]));
1232 /* add 'requires' dependency */
1233 /* rule: (-requestor|provider1|provider2|...|providerN) */
1234 addrule(solv, -n, dp - pool->whatprovidesdata);
1236 /* descend the dependency tree
1237 push all non-visited providers on the work queue */
1240 if (!MAPTST(m, *dp))
1241 queue_push(&workq, *dp);
1244 } /* while, requirements of n */
1246 } /* if, requirements */
1248 /* that's all we check for src packages */
1249 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
1252 /*-----------------------------------------
1253 * check conflicts of s
1258 conp = s->repo->idarraydata + s->conflicts;
1259 /* foreach conflicts of 's' */
1260 while ((con = *conp++) != 0)
1262 /* foreach providers of a conflict of 's' */
1263 FOR_PROVIDES(p, pp, con)
1265 /* dontfix: dont care about conflicts with already installed packs */
1266 if (dontfix && pool->solvables[p].repo == installed)
1268 /* p == n: self conflict */
1269 if (p == n && !solv->allowselfconflicts)
1270 p = 0; /* make it a negative assertion, aka 'uninstallable' */
1271 /* rule: -n|-p: either solvable _or_ provider of conflict */
1272 addrule(solv, -n, -p);
1277 /*-----------------------------------------
1278 * check obsoletes if not installed
1279 * (only installation will trigger the obsoletes in rpm)
1281 if (!installed || pool->solvables[n].repo != installed)
1282 { /* not installed */
1283 int noobs = solv->noobsoletes.size && MAPTST(&solv->noobsoletes, n);
1284 if (s->obsoletes && !noobs)
1286 obsp = s->repo->idarraydata + s->obsoletes;
1287 /* foreach obsoletes */
1288 while ((obs = *obsp++) != 0)
1290 /* foreach provider of an obsoletes of 's' */
1291 FOR_PROVIDES(p, pp, obs)
1293 if (!solv->obsoleteusesprovides /* obsoletes are matched names, not provides */
1294 && !pool_match_nevr(pool, pool->solvables + p, obs))
1296 addrule(solv, -n, -p);
1300 FOR_PROVIDES(p, pp, s->name)
1302 Solvable *ps = pool->solvables + p;
1303 /* we still obsolete packages with same nevra, like rpm does */
1304 /* (actually, rpm mixes those packages. yuck...) */
1305 if (noobs && (s->name != ps->name || s->evr != ps->evr || s->arch != ps->arch))
1307 if (!solv->implicitobsoleteusesprovides && s->name != ps->name)
1309 addrule(solv, -n, -p);
1313 /*-----------------------------------------
1314 * add recommends to the work queue
1318 recp = s->repo->idarraydata + s->recommends;
1319 while ((rec = *recp++) != 0)
1321 FOR_PROVIDES(p, pp, rec)
1323 queue_push(&workq, p);
1328 sugp = s->repo->idarraydata + s->suggests;
1329 while ((sug = *sugp++) != 0)
1331 FOR_PROVIDES(p, pp, sug)
1333 queue_push(&workq, p);
1338 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable end -----\n");
1342 /*-------------------------------------------------------------------
1344 * Add package rules for weak rules
1346 * m: visited solvables
1350 addrpmrulesforweak(Solver *solv, Map *m)
1352 Pool *pool = solv->pool;
1357 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak -----\n");
1358 /* foreach solvable in pool */
1359 for (i = n = 1; n < pool->nsolvables; i++, n++)
1361 if (i == pool->nsolvables) /* wrap i */
1363 if (MAPTST(m, i)) /* been there */
1366 s = pool->solvables + i;
1367 if (!pool_installable(pool, s)) /* only look at installable ones */
1373 /* find possible supplements */
1374 supp = s->repo->idarraydata + s->supplements;
1375 while ((sup = *supp++) != ID_NULL)
1376 if (dep_possible(solv, sup, m))
1380 /* if nothing found, check for freshens
1381 * (patterns use this)
1383 if (!sup && s->freshens)
1385 supp = s->repo->idarraydata + s->freshens;
1386 while ((sup = *supp++) != ID_NULL)
1387 if (dep_possible(solv, sup, m))
1391 /* if nothing found, check for enhances */
1392 if (!sup && s->enhances)
1394 supp = s->repo->idarraydata + s->enhances;
1395 while ((sup = *supp++) != ID_NULL)
1396 if (dep_possible(solv, sup, m))
1399 /* if notthing found, goto next solvables */
1402 addrpmrulesforsolvable(solv, s, m);
1405 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak end -----\n");
1409 /*-------------------------------------------------------------------
1411 * add package rules for possible updates
1414 * m: map of already visited solvables
1415 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
1419 addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allow_all)
1421 Pool *pool = solv->pool;
1423 /* queue and buffer for it */
1427 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1429 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1430 /* find update candidates for 's' */
1431 policy_findupdatepackages(solv, s, &qs, allow_all);
1432 /* add rule for 's' if not already done */
1433 if (!MAPTST(m, s - pool->solvables))
1434 addrpmrulesforsolvable(solv, s, m);
1435 /* foreach update candidate, add rule if not already done */
1436 for (i = 0; i < qs.count; i++)
1437 if (!MAPTST(m, qs.elements[i]))
1438 addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
1441 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1445 /*-------------------------------------------------------------------
1447 * add rule for update
1448 * (A|A1|A2|A3...) An = update candidates for A
1450 * s = (installed) solvable
1454 addupdaterule(Solver *solv, Solvable *s, int allow_all)
1456 /* installed packages get a special upgrade allowed rule */
1457 Pool *pool = solv->pool;
1462 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule -----\n");
1464 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1465 /* find update candidates for 's' */
1466 policy_findupdatepackages(solv, s, &qs, allow_all);
1467 if (qs.count == 0) /* no updaters found */
1468 d = 0; /* assertion (keep installed) */
1470 d = pool_queuetowhatprovides(pool, &qs); /* intern computed queue */
1472 addrule(solv, s - pool->solvables, d); /* allow update of s */
1473 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule end -----\n");
1477 /********************************************************************/
1481 /*-------------------------------------------------------------------
1484 * initial setup for all watches
1488 makewatches(Solver *solv)
1492 int nsolvables = solv->pool->nsolvables;
1494 sat_free(solv->watches);
1495 /* lower half for removals, upper half for installs */
1496 solv->watches = sat_calloc(2 * nsolvables, sizeof(Id));
1498 /* do it reverse so rpm rules get triggered first (XXX: obsolete?) */
1499 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1501 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1504 if (!r->w2) /* assertions do not need watches */
1507 /* see addwatches_rule(solv, r) */
1508 r->n1 = solv->watches[nsolvables + r->w1];
1509 solv->watches[nsolvables + r->w1] = r - solv->rules;
1511 r->n2 = solv->watches[nsolvables + r->w2];
1512 solv->watches[nsolvables + r->w2] = r - solv->rules;
1517 /*-------------------------------------------------------------------
1519 * add watches (for rule)
1520 * sets up watches for a single rule
1522 * see also makewatches()
1526 addwatches_rule(Solver *solv, Rule *r)
1528 int nsolvables = solv->pool->nsolvables;
1530 r->n1 = solv->watches[nsolvables + r->w1];
1531 solv->watches[nsolvables + r->w1] = r - solv->rules;
1533 r->n2 = solv->watches[nsolvables + r->w2];
1534 solv->watches[nsolvables + r->w2] = r - solv->rules;
1538 /********************************************************************/
1544 /* shortcuts to check if a literal (positive or negative) assignment
1545 * evaluates to 'true' or 'false'
1547 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1548 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
1550 /*-------------------------------------------------------------------
1554 * make decision and propagate to all rules
1556 * Evaluate each term affected by the decision (linked through watches)
1558 * If the decision was positive (true), we're done since the whole term is true
1559 * If the decision was negative (false), we must force the other literal of
1560 * the conjunctive normal form (CNF) to true.
1562 * return : 0 = everything is OK
1563 * watched rule = there is a conflict
1567 propagate(Solver *solv, int level)
1569 Pool *pool = solv->pool;
1570 Id *rp, *next_rp; /* rule pointer, next rule pointer in linked list */
1572 Id p, pkg, other_watch;
1574 Id *decisionmap = solv->decisionmap;
1576 Id *watches = solv->watches + pool->nsolvables; /* place ptr in middle */
1578 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
1580 /* foreach non-propagated decision */
1581 while (solv->propagate_index < solv->decisionq.count)
1584 * 'pkg' was just decided
1586 * negate because our watches trigger if literal goes FALSE
1587 * If a literal goes TRUE in a CNF (terms of (x|y)), we're done for this term
1589 pkg = -solv->decisionq.elements[solv->propagate_index++];
1591 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1593 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
1594 solver_printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
1597 /* foreach rule involving 'pkg' */
1598 for (rp = watches + pkg; *rp; rp = next_rp)
1600 r = solv->rules + *rp;
1603 /* rule is disabled, goto next */
1611 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1613 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
1614 solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
1617 /* 'pkg' was just decided
1618 * now find other literal watch, check clause
1619 * and advance on linked list
1623 other_watch = r->w2;
1628 other_watch = r->w1;
1633 * This term is already true (through the other literal)
1635 if (DECISIONMAP_TRUE(other_watch))
1639 * The other literal is false, try to get a 'true'
1644 /* not a binary clause, check if we need to move our watch.
1646 * search for a literal that is not other_watch and not false
1647 * (true is also ok, in that case the rule is fulfilled)
1649 if (r->p /* we have a 'p' */
1650 && r->p != other_watch /* which is not what we just checked */
1651 && !DECISIONMAP_TRUE(-r->p)) /* and its not already decided 'negative' */
1653 p = r->p; /* we must get this to 'true' */
1655 else /* go find a 'd' to make 'true' */
1658 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1659 if (p != other_watch /* which is not what we just checked */
1660 && !DECISIONMAP_TRUE(-p)) /* and its not already decided 'negative' */
1665 * if p is free to watch, move watch to p
1669 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1672 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables + p));
1674 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables - p));
1690 watches[p] = r - solv->rules;
1699 * unit clause found, force other watch to TRUE
1702 if (DECISIONMAP_TRUE(-other_watch)) /* decided before to 'negative' */
1703 return r; /* eek, a conflict! */
1705 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1707 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " unit ");
1708 solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
1712 * decide: 'other_watch' to 'true'
1714 if (other_watch > 0)
1715 decisionmap[other_watch] = level; /* install! */
1717 decisionmap[-other_watch] = -level; /* remove! */
1719 queue_push(&solv->decisionq, other_watch);
1720 queue_push(&solv->decisionq_why, r - solv->rules);
1722 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1724 Solvable *s = pool->solvables + (other_watch > 0 ? other_watch : -other_watch);
1725 if (other_watch > 0)
1726 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", solvable2str(pool, s));
1728 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", solvable2str(pool, s));
1731 } /* foreach rule involving 'pkg' */
1733 } /* while we have non-decided decisions */
1735 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
1737 return 0; /* all is well */
1741 /********************************************************************/
1744 /*-------------------------------------------------------------------
1751 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
1753 Pool *pool = solv->pool;
1756 Map seen; /* global? */
1757 Id d, v, vv, *dp, why;
1759 int num = 0, l1num = 0;
1760 int learnt_why = solv->learnt_pool.count;
1761 Id *decisionmap = solv->decisionmap;
1765 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
1766 map_init(&seen, pool->nsolvables);
1767 idx = solv->decisionq.count;
1770 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1771 solver_printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1772 queue_push(&solv->learnt_pool, c - solv->rules);
1773 d = c->d < 0 ? -c->d - 1 : c->d;
1774 dp = d ? pool->whatprovidesdata + d : 0;
1775 /* go through all literals of the rule */
1787 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1789 vv = v > 0 ? v : -v;
1790 if (MAPTST(&seen, vv))
1792 l = solv->decisionmap[vv];
1797 l1num++; /* need to do this one in level1 pass */
1798 else if (l == level)
1799 num++; /* need to do this one as well */
1802 queue_push(&r, v); /* not level1 or conflict level, add to new rule */
1808 if (!num && !--l1num)
1809 break; /* all level 1 literals done */
1813 v = solv->decisionq.elements[--idx];
1814 vv = v > 0 ? v : -v;
1815 if (MAPTST(&seen, vv))
1819 if (num && --num == 0)
1821 *pr = -v; /* so that v doesn't get lost */
1824 POOL_DEBUG(SAT_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
1825 for (i = 0; i < r.count; i++)
1828 MAPCLR(&seen, v > 0 ? v : -v);
1830 /* only level 1 marks left */
1834 why = solv->decisionq_why.elements[idx];
1835 if (!why) /* just in case, maye for SYSTEMSOLVABLE */
1837 c = solv->rules + why;
1843 else if (r.count == 1 && r.elements[0] < 0)
1844 *dr = r.elements[0];
1846 *dr = pool_queuetowhatprovides(pool, &r);
1847 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1849 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
1850 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, *pr);
1851 for (i = 0; i < r.count; i++)
1852 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
1854 /* push end marker on learnt reasons stack */
1855 queue_push(&solv->learnt_pool, 0);
1858 solv->stats_learned++;
1863 /*-------------------------------------------------------------------
1867 * reset the solver decisions to right after the rpm rules.
1868 * called after rules have been enabled/disabled
1872 reset_solver(Solver *solv)
1874 Pool *pool = solv->pool;
1878 /* rewind decisions to direct rpm rule assertions */
1879 for (i = solv->decisionq.count - 1; i >= solv->directdecisions; i--)
1881 v = solv->decisionq.elements[i];
1882 solv->decisionmap[v > 0 ? v : -v] = 0;
1885 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions done reduced from %d to %d\n", solv->decisionq.count, solv->directdecisions);
1887 solv->decisionq_why.count = solv->directdecisions;
1888 solv->decisionq.count = solv->directdecisions;
1889 solv->recommends_index = -1;
1890 solv->propagate_index = 0;
1892 /* adapt learnt rule status to new set of enabled/disabled rules */
1893 enabledisablelearntrules(solv);
1895 /* redo all job/update decisions */
1896 makeruledecisions(solv);
1897 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
1901 /*-------------------------------------------------------------------
1903 * analyze_unsolvable_rule
1907 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp)
1909 Pool *pool = solv->pool;
1911 Id why = r - solv->rules;
1913 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1914 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1915 if (solv->learntrules && why >= solv->learntrules)
1917 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1918 if (solv->learnt_pool.elements[i] > 0)
1919 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp);
1922 if (MAPTST(&solv->weakrulemap, why))
1923 if (!*lastweakp || why > *lastweakp)
1925 /* do not add rpm rules to problem */
1926 if (why < solv->rpmrules_end)
1928 /* turn rule into problem */
1929 if (why >= solv->jobrules && why < solv->jobrules_end)
1930 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1931 /* return if problem already countains our rule */
1932 if (solv->problems.count)
1934 for (i = solv->problems.count - 1; i >= 0; i--)
1935 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
1937 else if (solv->problems.elements[i] == why)
1940 queue_push(&solv->problems, why);
1944 /*-------------------------------------------------------------------
1946 * analyze_unsolvable
1948 * return: 1 - disabled some rules, try again
1953 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1955 Pool *pool = solv->pool;
1957 Map seen; /* global to speed things up? */
1958 Id d, v, vv, *dp, why;
1960 Id *decisionmap = solv->decisionmap;
1961 int oldproblemcount;
1962 int oldlearntpoolcount;
1965 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
1966 solv->stats_unsolvable++;
1967 oldproblemcount = solv->problems.count;
1968 oldlearntpoolcount = solv->learnt_pool.count;
1970 /* make room for proof index */
1971 /* must update it later, as analyze_unsolvable_rule would confuse
1972 * it with a rule index if we put the real value in already */
1973 queue_push(&solv->problems, 0);
1976 map_init(&seen, pool->nsolvables);
1977 queue_push(&solv->learnt_pool, r - solv->rules);
1979 analyze_unsolvable_rule(solv, r, &lastweak);
1980 d = r->d < 0 ? -r->d - 1 : r->d;
1981 dp = d ? pool->whatprovidesdata + d : 0;
1992 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1994 vv = v > 0 ? v : -v;
1995 l = solv->decisionmap[vv];
2000 idx = solv->decisionq.count;
2003 v = solv->decisionq.elements[--idx];
2004 vv = v > 0 ? v : -v;
2005 if (!MAPTST(&seen, vv))
2007 why = solv->decisionq_why.elements[idx];
2008 queue_push(&solv->learnt_pool, why);
2009 r = solv->rules + why;
2010 analyze_unsolvable_rule(solv, r, &lastweak);
2011 d = r->d < 0 ? -r->d - 1 : r->d;
2012 dp = d ? pool->whatprovidesdata + d : 0;
2023 if (DECISIONMAP_TRUE(v)) /* the one true literal */
2025 vv = v > 0 ? v : -v;
2026 l = solv->decisionmap[vv];
2033 queue_push(&solv->problems, 0); /* mark end of this problem */
2037 /* disable last weak rule */
2038 solv->problems.count = oldproblemcount;
2039 solv->learnt_pool.count = oldlearntpoolcount;
2040 r = solv->rules + lastweak;
2041 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling ");
2042 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
2043 disablerule(solv, r);
2049 queue_push(&solv->learnt_pool, 0);
2050 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
2054 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
2055 disableproblem(solv, solv->problems.elements[i]);
2056 /* XXX: might want to enable all weak rules again */
2060 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
2065 /********************************************************************/
2066 /* Decision revert */
2068 /*-------------------------------------------------------------------
2071 * revert decision at level
2075 revert(Solver *solv, int level)
2077 Pool *pool = solv->pool;
2079 while (solv->decisionq.count)
2081 v = solv->decisionq.elements[solv->decisionq.count - 1];
2082 vv = v > 0 ? v : -v;
2083 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
2085 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
2086 if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
2087 solv->recommendations.count--;
2088 solv->decisionmap[vv] = 0;
2089 solv->decisionq.count--;
2090 solv->decisionq_why.count--;
2091 solv->propagate_index = solv->decisionq.count;
2093 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
2095 solv->branches.count--;
2096 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
2097 solv->branches.count--;
2099 solv->recommends_index = -1;
2103 /*-------------------------------------------------------------------
2105 * watch2onhighest - put watch2 on literal with highest level
2109 watch2onhighest(Solver *solv, Rule *r)
2114 d = r->d < 0 ? -r->d - 1 : r->d;
2116 return; /* binary rule, both watches are set */
2117 dp = solv->pool->whatprovidesdata + d;
2118 while ((v = *dp++) != 0)
2120 l = solv->decisionmap[v < 0 ? -v : v];
2132 /*-------------------------------------------------------------------
2136 * add free decision (solvable to install) to decisionq
2137 * increase level and propagate decision
2138 * return if no conflict.
2140 * in conflict case, analyze conflict rule, add resulting
2141 * rule to learnt rule set, make decision from learnt
2142 * rule (always unit) and re-propagate.
2144 * returns the new solver level or 0 if unsolvable
2149 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
2151 Pool *pool = solv->pool;
2160 solv->decisionmap[decision] = level;
2162 solv->decisionmap[-decision] = -level;
2163 queue_push(&solv->decisionq, decision);
2164 queue_push(&solv->decisionq_why, 0);
2168 r = propagate(solv, level);
2172 return analyze_unsolvable(solv, r, disablerules);
2173 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
2174 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
2175 assert(l > 0 && l < level);
2176 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
2178 revert(solv, level);
2179 r = addrule(solv, p, d); /* p requires d */
2181 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
2182 queue_push(&solv->learnt_why, why);
2185 /* at least 2 literals, needs watches */
2186 watch2onhighest(solv, r);
2187 addwatches_rule(solv, r);
2191 /* learnt rule is an assertion */
2192 queue_push(&solv->ruleassertions, r - solv->rules);
2194 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
2195 queue_push(&solv->decisionq, p);
2196 queue_push(&solv->decisionq_why, r - solv->rules);
2197 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
2199 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
2200 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
2201 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
2202 solver_printrule(solv, SAT_DEBUG_ANALYZE, r);
2209 /*-------------------------------------------------------------------
2211 * select and install
2213 * install best package from the queue. We add an extra package, inst, if
2214 * provided. See comment in weak install section.
2216 * returns the new solver level or 0 if unsolvable
2221 selectandinstall(Solver *solv, int level, Queue *dq, Id inst, int disablerules)
2223 Pool *pool = solv->pool;
2227 if (dq->count > 1 || inst)
2228 policy_filter_unwanted(solv, dq, inst, POLICY_MODE_CHOOSE);
2233 /* choose the supplemented one */
2234 for (i = 0; i < dq->count; i++)
2235 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
2239 for (i = 1; i < dq->count; i++)
2240 queue_push(&solv->branches, dq->elements[i]);
2241 queue_push(&solv->branches, -level);
2245 p = dq->elements[i];
2247 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvable2str(pool, pool->solvables + p));
2249 return setpropagatelearn(solv, level, p, disablerules);
2253 /********************************************************************/
2254 /* Main solver interface */
2257 /*-------------------------------------------------------------------
2260 * create solver structure
2262 * pool: all available solvables
2263 * installed: installed Solvables
2266 * Upon solving, rules are created to flag the Solvables
2267 * of the 'installed' Repo as installed.
2271 solver_create(Pool *pool, Repo *installed)
2274 solv = (Solver *)sat_calloc(1, sizeof(Solver));
2276 solv->installed = installed;
2278 queue_init(&solv->ruletojob);
2279 queue_init(&solv->decisionq);
2280 queue_init(&solv->decisionq_why);
2281 queue_init(&solv->problems);
2282 queue_init(&solv->suggestions);
2283 queue_init(&solv->recommendations);
2284 queue_init(&solv->learnt_why);
2285 queue_init(&solv->learnt_pool);
2286 queue_init(&solv->branches);
2287 queue_init(&solv->covenantq);
2288 queue_init(&solv->weakruleq);
2289 queue_init(&solv->ruleassertions);
2291 map_init(&solv->recommendsmap, pool->nsolvables);
2292 map_init(&solv->suggestsmap, pool->nsolvables);
2293 map_init(&solv->noupdate, installed ? installed->end - installed->start : 0);
2294 solv->recommends_index = 0;
2296 solv->decisionmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
2298 solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
2299 memset(solv->rules, 0, sizeof(Rule));
2305 /*-------------------------------------------------------------------
2311 solver_free(Solver *solv)
2313 queue_free(&solv->ruletojob);
2314 queue_free(&solv->decisionq);
2315 queue_free(&solv->decisionq_why);
2316 queue_free(&solv->learnt_why);
2317 queue_free(&solv->learnt_pool);
2318 queue_free(&solv->problems);
2319 queue_free(&solv->suggestions);
2320 queue_free(&solv->recommendations);
2321 queue_free(&solv->branches);
2322 queue_free(&solv->covenantq);
2323 queue_free(&solv->weakruleq);
2324 queue_free(&solv->ruleassertions);
2326 map_free(&solv->recommendsmap);
2327 map_free(&solv->suggestsmap);
2328 map_free(&solv->noupdate);
2329 map_free(&solv->weakrulemap);
2330 map_free(&solv->noobsoletes);
2332 sat_free(solv->decisionmap);
2333 sat_free(solv->rules);
2334 sat_free(solv->watches);
2335 sat_free(solv->obsoletes);
2336 sat_free(solv->obsoletes_data);
2341 /*-------------------------------------------------------------------
2345 * all rules have been set up, now actually run the solver
2350 run_solver(Solver *solv, int disablerules, int doweak)
2352 Queue dq; /* local decisionqueue */
2358 Pool *pool = solv->pool;
2361 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
2363 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
2364 for (i = 1; i < solv->nrules; i++)
2365 solver_printruleclass(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
2368 POOL_DEBUG(SAT_DEBUG_STATS, "initial decisions: %d\n", solv->decisionq.count);
2370 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2371 solver_printdecisions(solv);
2373 /* start SAT algorithm */
2375 systemlevel = level + 1;
2376 POOL_DEBUG(SAT_DEBUG_STATS, "solving...\n");
2381 * here's the main loop:
2382 * 1) propagate new decisions (only needed for level 1)
2383 * 2) try to keep installed packages
2384 * 3) fulfill all unresolved rules
2385 * 4) install recommended packages
2386 * 5) minimalize solution if we had choices
2387 * if we encounter a problem, we rewind to a safe level and restart
2399 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
2400 if ((r = propagate(solv, level)) != 0)
2402 if (analyze_unsolvable(solv, r, disablerules))
2410 * installed packages
2413 if (level < systemlevel && solv->installed && solv->installed->nsolvables)
2416 * Normal run (non-updating)
2417 * Keep as many packages as possible
2419 if (!solv->updatesystem)
2421 POOL_DEBUG(SAT_DEBUG_STATS, "installing old packages\n");
2423 for (i = solv->installed->start; i < solv->installed->end; i++)
2425 s = pool->solvables + i;
2427 /* skip if not installed */
2428 if (s->repo != solv->installed)
2431 /* skip if already decided */
2432 if (solv->decisionmap[i] != 0)
2435 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "keeping %s\n", solvable2str(pool, s));
2438 level = setpropagatelearn(solv, level, i, disablerules);
2440 if (level == 0) /* done */
2445 if (level <= olevel)
2448 if (i < solv->installed->end)
2452 POOL_DEBUG(SAT_DEBUG_STATS, "resolving update/feature rules\n");
2454 for (i = solv->installed->start, r = solv->rules + solv->updaterules; i < solv->installed->end; i++, r++)
2458 s = pool->solvables + i;
2460 /* skip if not installed (can't update) */
2461 if (s->repo != solv->installed)
2463 /* skip if already decided */
2464 if (solv->decisionmap[i] > 0)
2467 /* noupdate is set if a job is erasing the installed solvable or installing a specific version */
2468 if (MAPTST(&solv->noupdate, i - solv->installed->start))
2473 if (rr->d < 0) /* disabled -> look at feature rule ? */
2474 rr -= solv->installed->end - solv->installed->start;
2475 if (!rr->p) /* identical to update rule? */
2477 d = (rr->d < 0) ? -rr->d - 1 : rr->d;
2480 if (!rr->w2 || solv->decisionmap[rr->w2] > 0)
2482 /* decide w2 if yet undecided */
2483 if (solv->decisionmap[rr->w2] == 0)
2484 queue_push(&dq, rr->w2);
2488 dp = pool->whatprovidesdata + d;
2489 while ((p = *dp++) != 0)
2491 if (solv->decisionmap[p] > 0)
2493 /* decide p if yet undecided */
2494 if (solv->decisionmap[p] == 0)
2500 if (!dq.count && solv->decisionmap[i] != 0)
2503 /* FIXME: i is handled a bit different because we do not want
2504 * to have it pruned just because it is not recommened.
2505 * we should not prune installed packages instead
2507 level = selectandinstall(solv, level, &dq, (solv->decisionmap[i] ? 0 : i), disablerules);
2513 if (level <= olevel)
2516 if (i < solv->installed->end)
2518 systemlevel = level;
2525 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unresolved rules\n");
2526 for (i = 1, n = 1; ; i++, n++)
2528 if (n == solv->nrules)
2530 if (i == solv->nrules)
2532 r = solv->rules + i;
2533 if (r->d < 0) /* ignore disabled rules */
2538 /* binary or unary rule */
2539 /* need two positive undecided literals */
2540 if (r->p < 0 || r->w2 <= 0)
2542 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2544 queue_push(&dq, r->p);
2545 queue_push(&dq, r->w2);
2550 * all negative literals are installed
2551 * no positive literal is installed
2552 * i.e. the rule is not fulfilled and we
2553 * just need to decide on the positive literals
2557 if (solv->decisionmap[-r->p] <= 0)
2562 if (solv->decisionmap[r->p] > 0)
2564 if (solv->decisionmap[r->p] == 0)
2565 queue_push(&dq, r->p);
2567 dp = pool->whatprovidesdata + r->d;
2568 while ((p = *dp++) != 0)
2572 if (solv->decisionmap[-p] <= 0)
2577 if (solv->decisionmap[p] > 0)
2579 if (solv->decisionmap[p] == 0)
2586 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
2588 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
2589 solver_printruleclass(solv, SAT_DEBUG_PROPAGATE, r);
2591 /* dq.count < 2 cannot happen as this means that
2592 * the rule is unit */
2593 assert(dq.count > 1);
2596 level = selectandinstall(solv, level, &dq, 0, disablerules);
2602 if (level < systemlevel)
2605 } /* for(), decide */
2607 if (n != solv->nrules) /* continue if level < systemlevel */
2614 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended packages\n");
2616 for (i = 1; i < pool->nsolvables; i++)
2618 if (solv->decisionmap[i] < 0)
2620 if (solv->decisionmap[i] > 0)
2622 Id *recp, rec, *pp, p;
2623 s = pool->solvables + i;
2624 /* installed, check for recommends */
2625 /* XXX need to special case AND ? */
2628 recp = s->repo->idarraydata + s->recommends;
2629 while ((rec = *recp++) != 0)
2632 FOR_PROVIDES(p, pp, rec)
2634 if (solv->decisionmap[p] > 0)
2639 else if (solv->decisionmap[p] == 0)
2641 queue_pushunique(&dq, p);
2649 s = pool->solvables + i;
2650 if (!s->supplements && !s->freshens)
2652 if (!pool_installable(pool, s))
2654 if (solver_is_supplementing(solv, s))
2655 queue_pushunique(&dq, i);
2661 policy_filter_unwanted(solv, &dq, 0, POLICY_MODE_RECOMMEND);
2663 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended %s\n", solvable2str(pool, pool->solvables + p));
2664 queue_push(&solv->recommendations, p);
2665 level = setpropagatelearn(solv, level, p, 0);
2670 if (solv->solution_callback)
2672 solv->solution_callback(solv, solv->solution_callback_data);
2673 if (solv->branches.count)
2675 int i = solv->branches.count - 1;
2676 int l = -solv->branches.elements[i];
2678 if (solv->branches.elements[i - 1] < 0)
2680 p = solv->branches.elements[i];
2681 POOL_DEBUG(SAT_DEBUG_STATS, "branching with %s\n", solvable2str(pool, pool->solvables + p));
2683 for (j = i + 1; j < solv->branches.count; j++)
2684 queue_push(&dq, solv->branches.elements[j]);
2685 solv->branches.count = i;
2687 revert(solv, level);
2689 for (j = 0; j < dq.count; j++)
2690 queue_push(&solv->branches, dq.elements[j]);
2692 level = setpropagatelearn(solv, level, p, disablerules);
2700 /* all branches done, we're finally finished */
2704 /* minimization step */
2705 if (solv->branches.count)
2707 int l = 0, lasti = -1, lastl = -1;
2709 for (i = solv->branches.count - 1; i >= 0; i--)
2711 p = solv->branches.elements[i];
2714 else if (p > 0 && solv->decisionmap[p] > l + 1)
2722 /* kill old solvable so that we do not loop */
2723 p = solv->branches.elements[lasti];
2724 solv->branches.elements[lasti] = 0;
2725 POOL_DEBUG(SAT_DEBUG_STATS, "minimizing %d -> %d with %s\n", solv->decisionmap[p], l, solvable2str(pool, pool->solvables + p));
2728 revert(solv, level);
2730 level = setpropagatelearn(solv, level, p, disablerules);
2741 POOL_DEBUG(SAT_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable\n", solv->stats_learned, solv->stats_unsolvable);
2743 POOL_DEBUG(SAT_DEBUG_STATS, "done solving.\n\n");
2748 /*-------------------------------------------------------------------
2752 * at this point, all rules that led to conflicts are disabled.
2753 * we re-enable all rules of a problem set but rule "sug", then
2754 * continue to disable more rules until there as again a solution.
2757 /* FIXME: think about conflicting assertions */
2760 refine_suggestion(Solver *solv, Queue *job, Id *problem, Id sug, Queue *refined)
2762 Pool *pool = solv->pool;
2768 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2770 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion start\n");
2771 for (i = 0; problem[i]; i++)
2773 if (problem[i] == sug)
2774 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "=> ");
2775 solver_printproblem(solv, problem[i]);
2778 queue_init(&disabled);
2779 queue_empty(refined);
2780 queue_push(refined, sug);
2782 /* re-enable all problem rules with the exception of "sug"(gestion) */
2786 for (i = 0; problem[i]; i++)
2787 if (problem[i] != sug)
2788 enableproblem(solv, problem[i]);
2791 disableupdaterules(solv, job, -(sug + 1));
2792 else if (sug >= solv->updaterules && sug < solv->updaterules_end)
2794 /* enable feature rule */
2795 Rule *r = solv->rules + solv->featurerules + (sug - solv->updaterules);
2797 enablerule(solv, r);
2800 enableweakrules(solv);
2804 int njob, nfeature, nupdate;
2805 queue_empty(&solv->problems);
2806 revert(solv, 1); /* XXX no longer needed? */
2809 if (!solv->problems.count)
2810 run_solver(solv, 0, 0);
2812 if (!solv->problems.count)
2814 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no more problems!\n");
2815 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2816 solver_printdecisions(solv);
2817 break; /* great, no more problems */
2819 disabledcnt = disabled.count;
2820 /* start with 1 to skip over proof index */
2821 njob = nfeature = nupdate = 0;
2822 for (i = 1; i < solv->problems.count - 1; i++)
2824 /* ignore solutions in refined */
2825 v = solv->problems.elements[i];
2827 break; /* end of problem reached */
2828 for (j = 0; problem[j]; j++)
2829 if (problem[j] != sug && problem[j] == v)
2833 if (v >= solv->featurerules && v < solv->featurerules_end)
2839 queue_push(&disabled, v);
2841 if (disabled.count == disabledcnt)
2843 /* no solution found, this was an invalid suggestion! */
2844 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no solution found!\n");
2848 if (!njob && nupdate && nfeature)
2850 /* got only update rules, filter out feature rules */
2851 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "throwing away feature rules\n");
2852 for (i = j = disabledcnt; i < disabled.count; i++)
2854 v = disabled.elements[i];
2855 if (v < solv->featurerules || v >= solv->featurerules_end)
2856 disabled.elements[j++] = v;
2861 if (disabled.count == disabledcnt + 1)
2863 /* just one suggestion, add it to refined list */
2864 v = disabled.elements[disabledcnt];
2866 queue_push(refined, v); /* do not record feature rules */
2867 disableproblem(solv, v);
2868 if (v >= solv->updaterules && v < solv->updaterules_end)
2870 Rule *r = solv->rules + (v - solv->updaterules + solv->featurerules);
2872 enablerule(solv, r); /* enable corresponding feature rule */
2875 disableupdaterules(solv, job, -(v + 1));
2879 /* more than one solution, disable all */
2880 /* do not push anything on refine list, as we do not know which solution to choose */
2881 /* thus, the user will get another problem if he selects this solution, where he
2882 * can choose the right one */
2883 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2885 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "more than one solution found:\n");
2886 for (i = disabledcnt; i < disabled.count; i++)
2887 solver_printproblem(solv, disabled.elements[i]);
2889 for (i = disabledcnt; i < disabled.count; i++)
2891 v = disabled.elements[i];
2892 disableproblem(solv, v);
2893 if (v >= solv->updaterules && v < solv->updaterules_end)
2895 Rule *r = solv->rules + (v - solv->updaterules + solv->featurerules);
2897 enablerule(solv, r);
2902 /* all done, get us back into the same state as before */
2903 /* enable refined rules again */
2904 for (i = 0; i < disabled.count; i++)
2905 enableproblem(solv, disabled.elements[i]);
2906 /* disable problem rules again */
2909 for (i = 0; problem[i]; i++)
2910 enableproblem(solv, problem[i]);
2911 disableupdaterules(solv, job, -1);
2913 /* disable problem rules again */
2914 for (i = 0; problem[i]; i++)
2915 disableproblem(solv, problem[i]);
2916 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion end\n");
2920 /*-------------------------------------------------------------------
2921 * sorting helper for problems
2925 problems_sortcmp(const void *ap, const void *bp)
2927 Id a = *(Id *)ap, b = *(Id *)bp;
2936 /*-------------------------------------------------------------------
2941 problems_sort(Solver *solv)
2944 if (!solv->problems.count)
2946 for (i = j = 1; i < solv->problems.count; i++)
2948 if (!solv->problems.elements[i])
2951 qsort(solv->problems.elements + j, i - j, sizeof(Id), problems_sortcmp);
2952 if (++i == solv->problems.count)
2960 /*-------------------------------------------------------------------
2961 * convert problems to solutions
2965 problems_to_solutions(Solver *solv, Queue *job)
2967 Pool *pool = solv->pool;
2975 if (!solv->problems.count)
2977 problems_sort(solv);
2978 queue_clone(&problems, &solv->problems);
2979 queue_init(&solution);
2980 queue_init(&solutions);
2981 /* copy over proof index */
2982 queue_push(&solutions, problems.elements[0]);
2983 problem = problems.elements + 1;
2984 for (i = 1; i < problems.count; i++)
2986 Id v = problems.elements[i];
2989 /* mark end of this problem */
2990 queue_push(&solutions, 0);
2991 queue_push(&solutions, 0);
2992 if (i + 1 == problems.count)
2994 /* copy over proof of next problem */
2995 queue_push(&solutions, problems.elements[i + 1]);
2997 problem = problems.elements + i + 1;
3000 refine_suggestion(solv, job, problem, v, &solution);
3001 if (!solution.count)
3002 continue; /* this solution didn't work out */
3005 for (j = 0; j < solution.count; j++)
3007 why = solution.elements[j];
3008 /* must be either job descriptor or update rule */
3009 assert(why < 0 || (why >= solv->updaterules && why < solv->updaterules_end));
3011 solver_printproblem(solv, why);
3015 /* job descriptor */
3016 queue_push(&solutions, 0);
3017 queue_push(&solutions, -why);
3021 /* update rule, find replacement package */
3022 Id p, d, *dp, rp = 0;
3024 p = solv->installed->start + (why - solv->updaterules);
3025 if (solv->decisionmap[p] > 0)
3026 continue; /* false alarm, turned out we can keep the package */
3027 rr = solv->rules + solv->featurerules + (why - solv->updaterules);
3029 rr = solv->rules + why;
3032 d = rr->d < 0 ? -rr->d - 1 : rr->d;
3035 if (solv->decisionmap[rr->w2] > 0 && pool->solvables[rr->w2].repo != solv->installed)
3040 for (dp = pool->whatprovidesdata + d; *dp; dp++)
3042 if (solv->decisionmap[*dp] > 0 && pool->solvables[*dp].repo != solv->installed)
3050 queue_push(&solutions, p);
3051 queue_push(&solutions, rp);
3055 /* mark end of this solution */
3058 queue_push(&solutions, 0);
3059 queue_push(&solutions, 0);
3063 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "Oops, everything was fine?\n");
3066 queue_free(&solution);
3067 queue_free(&problems);
3068 /* copy queue over to solutions */
3069 queue_free(&solv->problems);
3070 queue_clone(&solv->problems, &solutions);
3072 /* bring solver back into problem state */
3073 revert(solv, 1); /* XXX move to reset_solver? */
3076 assert(solv->problems.count == solutions.count);
3077 queue_free(&solutions);
3081 /*-------------------------------------------------------------------
3085 * advance to next problem
3089 solver_next_problem(Solver *solv, Id problem)
3093 return solv->problems.count ? 1 : 0;
3094 pp = solv->problems.elements + problem;
3095 while (pp[0] || pp[1])
3099 while (pp[0] || pp[1])
3104 problem = pp - solv->problems.elements;
3105 if (problem >= solv->problems.count)
3111 /*-------------------------------------------------------------------
3117 solver_next_solution(Solver *solv, Id problem, Id solution)
3123 pp = solv->problems.elements + solution;
3124 return pp[0] || pp[1] ? solution : 0;
3126 pp = solv->problems.elements + solution;
3127 while (pp[0] || pp[1])
3130 solution = pp - solv->problems.elements;
3131 return pp[0] || pp[1] ? solution : 0;
3135 /*-------------------------------------------------------------------
3137 * solution element iterator
3141 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
3144 element = element ? element + 2 : solution;
3145 pp = solv->problems.elements + element;
3146 if (!(pp[0] || pp[1]))
3154 /*-------------------------------------------------------------------
3156 * Retrieve information about a problematic rule
3158 * this is basically the reverse of addrpmrulesforsolvable
3162 solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp)
3164 Pool *pool = solv->pool;
3165 Repo *installed = solv->installed;
3169 Id p, d, *pp, req, *reqp, con, *conp, obs, *obsp, *dp;
3172 if (rid >= solv->jobrules && rid < solv->jobrules_end)
3175 r = solv->rules + rid;
3176 p = solv->ruletojob.elements[rid - solv->jobrules];
3177 *depp = job->elements[p + 1];
3179 *targetp = job->elements[p];
3180 d = r->d < 0 ? -r->d - 1 : r->d;
3181 if (d == 0 && r->w2 == 0 && r->p == -SYSTEMSOLVABLE && job->elements[p] != SOLVER_INSTALL_SOLVABLE_ONE_OF)
3182 return SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP;
3183 return SOLVER_PROBLEM_JOB_RULE;
3185 if (rid >= solv->updaterules && rid < solv->updaterules_end)
3188 *sourcep = solv->installed->start + (rid - solv->updaterules);
3190 return SOLVER_PROBLEM_UPDATE_RULE;
3192 assert(rid < solv->rpmrules_end);
3193 r = solv->rules + rid;
3195 d = r->d < 0 ? -r->d - 1 : r->d;
3196 if (d == 0 && r->w2 == 0)
3198 /* a rpm rule assertion */
3199 s = pool->solvables - r->p;
3200 if (installed && !solv->fixsystem && s->repo == installed)
3202 assert(!dontfix); /* dontfix packages never have a neg assertion */
3205 /* see why the package is not installable */
3206 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
3209 return SOLVER_PROBLEM_NOT_INSTALLABLE;
3211 /* check requires */
3214 reqp = s->repo->idarraydata + s->requires;
3215 while ((req = *reqp++) != 0)
3217 if (req == SOLVABLE_PREREQMARKER)
3219 dp = pool_whatprovides(pool, req);
3226 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
3229 assert(!solv->allowselfconflicts);
3230 assert(s->conflicts);
3231 conp = s->repo->idarraydata + s->conflicts;
3232 while ((con = *conp++) != 0)
3233 FOR_PROVIDES(p, pp, con)
3237 return SOLVER_PROBLEM_SELF_CONFLICT;
3241 s = pool->solvables - r->p;
3242 if (installed && !solv->fixsystem && s->repo == installed)
3244 if (d == 0 && r->w2 < 0)
3246 /* a package conflict */
3247 Solvable *s2 = pool->solvables - r->w2;
3250 if (installed && !solv->fixsystem && s2->repo == installed)
3253 /* if both packages have the same name and at least one of them
3254 * is not installed, they conflict */
3255 if (s->name == s2->name && !(installed && s->repo == installed && s2->repo == installed))
3257 /* also check noobsoletes map */
3258 if ((s->evr == s2->evr && s->arch == s2->arch) || !solv->noobsoletes.size
3259 || ((!installed || s->repo != installed) && !MAPTST(&solv->noobsoletes, -r->p))
3260 || ((!installed || s2->repo != installed) && !MAPTST(&solv->noobsoletes, -r->w2)))
3265 return SOLVER_PROBLEM_SAME_NAME;
3269 /* check conflicts in both directions */
3272 conp = s->repo->idarraydata + s->conflicts;
3273 while ((con = *conp++) != 0)
3275 FOR_PROVIDES(p, pp, con)
3277 if (dontfix && pool->solvables[p].repo == installed)
3284 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
3290 conp = s2->repo->idarraydata + s2->conflicts;
3291 while ((con = *conp++) != 0)
3293 FOR_PROVIDES(p, pp, con)
3295 if (dontfix2 && pool->solvables[p].repo == installed)
3302 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
3306 /* check obsoletes in both directions */
3307 if ((!installed || s->repo != installed) && s->obsoletes && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -r->p)))
3309 obsp = s->repo->idarraydata + s->obsoletes;
3310 while ((obs = *obsp++) != 0)
3312 FOR_PROVIDES(p, pp, obs)
3316 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3321 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3325 if ((!installed || s2->repo != installed) && s2->obsoletes && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -r->w2)))
3327 obsp = s2->repo->idarraydata + s2->obsoletes;
3328 while ((obs = *obsp++) != 0)
3330 FOR_PROVIDES(p, pp, obs)
3334 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3339 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3343 if (solv->implicitobsoleteusesprovides && (!installed || s->repo != installed) && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -r->p)))
3345 FOR_PROVIDES(p, pp, s->name)
3352 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3355 if (solv->implicitobsoleteusesprovides && (!installed || s2->repo != installed) && !(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, -r->w2)))
3357 FOR_PROVIDES(p, pp, s2->name)
3364 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
3367 /* all cases checked, can't happen */
3370 /* simple requires */
3371 assert(s->requires);
3372 reqp = s->repo->idarraydata + s->requires;
3373 while ((req = *reqp++) != 0)
3375 if (req == SOLVABLE_PREREQMARKER)
3377 dp = pool_whatprovides(pool, req);
3380 if (*dp == r->w2 && dp[1] == 0)
3383 else if (dp - pool->whatprovidesdata == d)
3390 return SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE;
3394 /*-------------------------------------------------------------------
3400 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp)
3403 Id lreqr, lconr, lsysr, ljobr;
3407 lreqr = lconr = lsysr = ljobr = 0;
3408 while ((rid = solv->learnt_pool.elements[idx++]) != 0)
3411 if (rid >= solv->learntrules)
3412 findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr);
3413 else if (rid >= solv->jobrules && rid < solv->jobrules_end)
3418 else if (rid >= solv->updaterules && rid < solv->updaterules_end)
3425 assert(rid < solv->rpmrules_end);
3426 r = solv->rules + rid;
3427 d = r->d < 0 ? -r->d - 1 : r->d;
3428 if (!d && r->w2 < 0)
3435 if (!d && r->w2 == 0 && !reqassert)
3437 /* prefer assertions (XXX: bad idea?) */
3443 else if (solv->installed && r->p < 0 && solv->pool->solvables[-r->p].repo == solv->installed)
3445 /* prefer rules of installed packages */
3446 Id op = *reqrp >= 0 ? solv->rules[*reqrp].p : -*reqrp;
3447 if (op <= 0 || solv->pool->solvables[op].repo != solv->installed)
3453 if (!*reqrp && lreqr)
3455 if (!*conrp && lconr)
3457 if (!*jobrp && ljobr)
3459 if (!*sysrp && lsysr)
3464 /*-------------------------------------------------------------------
3468 * search for a rule that describes the problem to the
3469 * user. A pretty hopeless task, actually. We currently
3470 * prefer simple requires.
3474 solver_findproblemrule(Solver *solv, Id problem)
3476 Id reqr, conr, sysr, jobr;
3477 Id idx = solv->problems.elements[problem - 1];
3478 reqr = conr = sysr = jobr = 0;
3479 findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr);
3492 /*-------------------------------------------------------------------
3494 * create reverse obsoletes map for installed solvables
3496 * for each installed solvable find which packages with *different* names
3497 * obsolete the solvable.
3498 * this index is used in policy_findupdatepackages if noupdateprovide is set.
3502 create_obsolete_index(Solver *solv)
3504 Pool *pool = solv->pool;
3506 Repo *installed = solv->installed;
3507 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
3510 if (!installed || !installed->nsolvables)
3512 solv->obsoletes = obsoletes = sat_calloc(installed->end - installed->start, sizeof(Id));
3513 for (i = 1; i < pool->nsolvables; i++)
3515 s = pool->solvables + i;
3518 if (!pool_installable(pool, s))
3520 obsp = s->repo->idarraydata + s->obsoletes;
3521 while ((obs = *obsp++) != 0)
3523 FOR_PROVIDES(p, pp, obs)
3525 if (pool->solvables[p].repo != installed)
3527 if (pool->solvables[p].name == s->name)
3529 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3531 obsoletes[p - installed->start]++;
3536 for (i = 0; i < installed->nsolvables; i++)
3539 n += obsoletes[i] + 1;
3542 solv->obsoletes_data = obsoletes_data = sat_calloc(n + 1, sizeof(Id));
3543 POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
3544 for (i = pool->nsolvables - 1; i > 0; i--)
3546 s = pool->solvables + i;
3549 if (!pool_installable(pool, s))
3551 obsp = s->repo->idarraydata + s->obsoletes;
3552 while ((obs = *obsp++) != 0)
3554 FOR_PROVIDES(p, pp, obs)
3556 if (pool->solvables[p].repo != installed)
3558 if (pool->solvables[p].name == s->name)
3560 if (!solv->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p, obs))
3562 p -= installed->start;
3563 if (obsoletes_data[obsoletes[p]] != i)
3564 obsoletes_data[--obsoletes[p]] = i;
3571 /*-------------------------------------------------------------------
3573 * remove disabled conflicts
3577 removedisabledconflicts(Solver *solv, Queue *removed)
3579 Pool *pool = solv->pool;
3584 Id *decisionmap = solv->decisionmap;
3586 POOL_DEBUG(SAT_DEBUG_STATS, "removedisabledconflicts\n");
3587 queue_empty(removed);
3588 for (i = 0; i < solv->decisionq.count; i++)
3590 p = solv->decisionq.elements[i];
3593 /* a conflict. we never do conflicts on free decisions, so there
3594 * must have been an unit rule */
3595 why = solv->decisionq_why.elements[i];
3597 r = solv->rules + why;
3598 if (r->d < 0 && decisionmap[-p])
3600 /* rule is now disabled, remove from decisionmap */
3601 POOL_DEBUG(SAT_DEBUG_STATS, "removing conflict for package %s[%d]\n", solvable2str(pool, pool->solvables - p), -p);
3602 queue_push(removed, -p);
3603 queue_push(removed, decisionmap[-p]);
3604 decisionmap[-p] = 0;
3607 if (!removed->count)
3609 /* we removed some confliced packages. some of them might still
3610 * be in conflict, so search for unit rules and re-conflict */
3612 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
3614 if (i == solv->nrules)
3617 r = solv->rules + i;
3623 if (r->p < 0 && !decisionmap[-r->p])
3629 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
3631 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
3636 if (r->p < 0 && decisionmap[-r->p] == 0)
3638 if (new || DECISIONMAP_FALSE(r->p))
3640 dp = pool->whatprovidesdata + r->d;
3641 while ((p = *dp++) != 0)
3643 if (new && p == new)
3645 if (p < 0 && decisionmap[-p] == 0)
3654 else if (!DECISIONMAP_FALSE(p))
3664 POOL_DEBUG(SAT_DEBUG_STATS, "re-conflicting package %s[%d]\n", solvable2str(pool, pool->solvables - new), -new);
3665 decisionmap[-new] = -1;
3667 n = 0; /* redo all rules */
3673 /*-------------------------------------------------------------------
3675 * weaken solvable dependencies
3679 weaken_solvable_deps(Solver *solv, Id p)
3684 for (i = 1, r = solv->rules + i; i < solv->featurerules; i++, r++)
3688 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
3689 continue; /* conflict */
3690 queue_push(&solv->weakruleq, i);
3694 /********************************************************************/
3704 solver_solve(Solver *solv, Queue *job)
3706 Pool *pool = solv->pool;
3707 Repo *installed = solv->installed;
3710 Map addedmap; /* '1' == have rpm-rules for solvable */
3711 Id how, what, weak, p, *pp, d;
3717 /* create whatprovides if not already there */
3718 if (!pool->whatprovides)
3719 pool_createwhatprovides(pool);
3721 /* create obsolete index if needed */
3722 if (solv->noupdateprovide)
3723 create_obsolete_index(solv);
3726 * create basic rule set of all involved packages
3727 * use addedmap bitmap to make sure we don't create rules twice
3731 /* create noobsolete map if needed */
3732 for (i = 0; i < job->count; i += 2)
3734 how = job->elements[i] & ~SOLVER_WEAK;
3735 what = job->elements[i + 1];
3738 case SOLVER_NOOBSOLETES_SOLVABLE:
3739 case SOLVER_NOOBSOLETES_SOLVABLE_NAME:
3740 case SOLVER_NOOBSOLETES_SOLVABLE_PROVIDES:
3741 if (!solv->noobsoletes.size)
3742 map_init(&solv->noobsoletes, pool->nsolvables);
3743 if (how == SOLVER_NOOBSOLETES_SOLVABLE)
3745 MAPSET(&solv->noobsoletes, what);
3748 FOR_PROVIDES(p, pp, what)
3750 if (how == SOLVER_NOOBSOLETES_SOLVABLE_NAME && !pool_match_nevr(pool, pool->solvables + p, what))
3752 MAPSET(&solv->noobsoletes, p);
3760 map_init(&addedmap, pool->nsolvables);
3764 * always install our system solvable
3766 MAPSET(&addedmap, SYSTEMSOLVABLE);
3767 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
3768 queue_push(&solv->decisionq_why, 0);
3769 solv->decisionmap[SYSTEMSOLVABLE] = 1; /* installed at level '1' */
3772 * create rules for all package that could be involved with the solving
3773 * so called: rpm rules
3778 oldnrules = solv->nrules;
3779 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
3780 FOR_REPO_SOLVABLES(installed, p, s)
3781 addrpmrulesforsolvable(solv, s, &addedmap);
3782 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3783 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
3784 oldnrules = solv->nrules;
3785 FOR_REPO_SOLVABLES(installed, p, s)
3786 addrpmrulesforupdaters(solv, s, &addedmap, 1);
3787 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3791 * create rules for all packages involved in the job
3792 * (to be installed or removed)
3795 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
3796 oldnrules = solv->nrules;
3797 for (i = 0; i < job->count; i += 2)
3799 how = job->elements[i] & ~SOLVER_WEAK;
3800 what = job->elements[i + 1];
3804 case SOLVER_INSTALL_SOLVABLE:
3805 addrpmrulesforsolvable(solv, pool->solvables + what, &addedmap);
3807 case SOLVER_INSTALL_SOLVABLE_NAME:
3808 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3809 FOR_PROVIDES(p, pp, what)
3811 /* if by name, ensure that the name matches */
3812 if (how == SOLVER_INSTALL_SOLVABLE_NAME && !pool_match_nevr(pool, pool->solvables + p, what))
3814 addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3817 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3818 /* dont allow downgrade */
3819 addrpmrulesforupdaters(solv, pool->solvables + what, &addedmap, 0);
3821 case SOLVER_INSTALL_SOLVABLE_ONE_OF:
3822 pp = pool->whatprovidesdata + what;
3823 while ((p = *pp++) != 0)
3824 addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3828 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3830 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for recommended/suggested packages ***\n");
3832 oldnrules = solv->nrules;
3835 * add rules for suggests, [freshens,] enhances
3837 addrpmrulesforweak(solv, &addedmap);
3838 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3840 IF_POOLDEBUG (SAT_DEBUG_STATS)
3842 int possible = 0, installable = 0;
3843 for (i = 1; i < pool->nsolvables; i++)
3845 if (pool_installable(pool, pool->solvables + i))
3847 if (MAPTST(&addedmap, i))
3850 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
3854 * first pass done, we now have all the rpm rules we need.
3855 * unify existing rules before going over all job rules and
3857 * at this point the system is always solvable,
3858 * as an empty system (remove all packages) is a valid solution
3861 unifyrules(solv); /* remove duplicate rpm rules */
3863 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
3865 solv->directdecisions = solv->decisionq.count;
3866 POOL_DEBUG(SAT_DEBUG_STATS, "decisions so far: %d\n", solv->decisionq.count);
3869 * create feature rules
3871 * foreach installed:
3872 * create assertion (keep installed, if no update available)
3874 * create update rule (A|update1(A)|update2(A)|...)
3876 * those are used later on to keep a version of the installed packages in
3880 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add feature rules ***\n");
3881 solv->featurerules = solv->nrules; /* mark start of feature rules */
3884 /* foreach installed solvable */
3885 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3891 if (s->freshens && !s->supplements)
3893 const char *name = id2str(pool, s->name);
3894 if (name[0] == 'a' && !strncmp(name, "atom:", 5))
3896 addrule(solv, 0, 0);
3901 if (s->repo != installed)
3903 addrule(solv, 0, 0); /* create dummy rule */
3906 addupdaterule(solv, s, 1); /* allow s to be updated */
3909 * assert one rule per installed solvable,
3910 * either an assertion (A)
3911 * or a possible update (A|update1(A)|update2(A)|...)
3913 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
3915 solv->featurerules_end = solv->nrules;
3918 * Add update rules for installed solvables
3920 * almost identical to feature rules
3921 * except that downgrades are allowed
3924 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add update rules ***\n");
3925 solv->updaterules = solv->nrules;
3927 { /* foreach installed solvables */
3928 /* we create all update rules, but disable some later on depending on the job */
3929 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3934 /* no update rules for patch atoms */
3935 if (s->freshens && !s->supplements)
3937 const char *name = id2str(pool, s->name);
3938 if (name[0] == 'a' && !strncmp(name, "atom:", 5))
3940 addrule(solv, 0, 0);
3945 if (s->repo != installed)
3947 addrule(solv, 0, 0); /* create dummy rule */
3950 addupdaterule(solv, s, 0); /* allowall = 0: downgrades allowed */
3953 * check for and remove duplicate
3956 r = solv->rules + solv->nrules - 1; /* r: update rule */
3957 sr = r - (installed->end - installed->start); /* sr: feature rule */
3958 unifyrules_sortcmp_data = pool;
3959 if (!unifyrules_sortcmp(r, sr))
3961 /* identical rule, kill unneeded rule */
3962 if (solv->allowuninstall)
3964 /* keep feature rule */
3965 memset(r, 0, sizeof(*r));
3966 queue_push(&solv->weakruleq, sr - solv->rules);
3970 /* keep update rule */
3971 memset(sr, 0, sizeof(*sr));
3974 else if (solv->allowuninstall)
3976 /* make both feature and update rule weak */
3977 queue_push(&solv->weakruleq, r - solv->rules);
3978 queue_push(&solv->weakruleq, sr - solv->rules);
3981 disablerule(solv, sr);
3983 /* consistency check: we added a rule for _every_ installed solvable */
3984 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
3986 solv->updaterules_end = solv->nrules;
3990 * now add all job rules
3993 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
3995 solv->jobrules = solv->nrules;
3996 for (i = 0; i < job->count; i += 2)
3998 oldnrules = solv->nrules;
4000 how = job->elements[i] & ~SOLVER_WEAK;
4001 weak = job->elements[i] & SOLVER_WEAK;
4002 what = job->elements[i + 1];
4005 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
4006 s = pool->solvables + what;
4007 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sinstall solvable %s\n", weak ? "weak " : "", solvable2str(pool, s));
4008 addrule(solv, what, 0); /* install by Id */
4009 queue_push(&solv->ruletojob, i);
4011 queue_push(&solv->weakruleq, solv->nrules - 1);
4013 case SOLVER_ERASE_SOLVABLE:
4014 s = pool->solvables + what;
4015 POOL_DEBUG(SAT_DEBUG_JOB, "job: %serase solvable %s\n", weak ? "weak " : "", solvable2str(pool, s));
4016 addrule(solv, -what, 0); /* remove by Id */
4017 queue_push(&solv->ruletojob, i);
4019 queue_push(&solv->weakruleq, solv->nrules - 1);
4021 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
4022 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
4023 if (how == SOLVER_INSTALL_SOLVABLE_NAME)
4024 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sinstall name %s\n", weak ? "weak " : "", dep2str(pool, what));
4025 if (how == SOLVER_INSTALL_SOLVABLE_PROVIDES)
4026 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sinstall provides %s\n", weak ? "weak " : "", dep2str(pool, what));
4028 FOR_PROVIDES(p, pp, what)
4030 /* if by name, ensure that the name matches */
4031 if (how == SOLVER_INSTALL_SOLVABLE_NAME && !pool_match_nevr(pool, pool->solvables + p, what))
4037 /* no provider, make this an impossible rule */
4038 queue_push(&q, -SYSTEMSOLVABLE);
4041 p = queue_shift(&q); /* get first provider */
4043 d = 0; /* single provider ? -> make assertion */
4045 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
4046 addrule(solv, p, d); /* add 'requires' rule */
4047 queue_push(&solv->ruletojob, i);
4049 queue_push(&solv->weakruleq, solv->nrules - 1);
4051 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
4052 case SOLVER_ERASE_SOLVABLE_PROVIDES:
4053 if (how == SOLVER_ERASE_SOLVABLE_NAME)
4054 POOL_DEBUG(SAT_DEBUG_JOB, "job: %serase name %s\n", weak ? "weak " : "", dep2str(pool, what));
4055 if (how == SOLVER_ERASE_SOLVABLE_PROVIDES)
4056 POOL_DEBUG(SAT_DEBUG_JOB, "job: %serase provides %s\n", weak ? "weak " : "", dep2str(pool, what));
4057 FOR_PROVIDES(p, pp, what)
4059 /* if by name, ensure that the name matches */
4060 if (how == SOLVER_ERASE_SOLVABLE_NAME && !pool_match_nevr(pool, pool->solvables + p, what))
4062 addrule(solv, -p, 0); /* add 'remove' rule */
4063 queue_push(&solv->ruletojob, i);
4065 queue_push(&solv->weakruleq, solv->nrules - 1);
4068 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
4069 s = pool->solvables + what;
4070 POOL_DEBUG(SAT_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solvable2str(pool, s));
4071 addupdaterule(solv, s, 0);
4072 queue_push(&solv->ruletojob, i);
4074 queue_push(&solv->weakruleq, solv->nrules - 1);
4076 case SOLVER_INSTALL_SOLVABLE_ONE_OF:
4077 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sone of\n", weak ? "weak " : "");
4078 for (pp = pool->whatprovidesdata + what; *pp; pp++)
4079 POOL_DEBUG(SAT_DEBUG_JOB, " %s\n", solvable2str(pool, pool->solvables + *pp));
4080 addrule(solv, -SYSTEMSOLVABLE, what);
4081 queue_push(&solv->ruletojob, i);
4083 queue_push(&solv->weakruleq, solv->nrules - 1);
4085 case SOLVER_WEAKEN_SOLVABLE_DEPS:
4086 s = pool->solvables + what;
4087 POOL_DEBUG(SAT_DEBUG_JOB, "job: weaken deps %s\n", solvable2str(pool, s));
4088 weaken_solvable_deps(solv, what);
4090 case SOLVER_NOOBSOLETES_SOLVABLE:
4091 POOL_DEBUG(SAT_DEBUG_JOB, "job: no obsolete %s\n", solvable2str(pool, pool->solvables + what));
4093 case SOLVER_NOOBSOLETES_SOLVABLE_NAME:
4094 POOL_DEBUG(SAT_DEBUG_JOB, "job: no obsolete name %s\n", dep2str(pool, what));
4096 case SOLVER_NOOBSOLETES_SOLVABLE_PROVIDES:
4097 POOL_DEBUG(SAT_DEBUG_JOB, "job: no obsolete provides %s\n", dep2str(pool, what));
4105 IF_POOLDEBUG (SAT_DEBUG_JOB)
4108 if (solv->nrules == oldnrules)
4109 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created\n");
4110 for (j = oldnrules; j < solv->nrules; j++)
4112 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
4113 solver_printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
4117 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
4118 solv->jobrules_end = solv->nrules;
4120 /* all rules created
4121 * --------------------------------------------------------------
4122 * prepare for solving
4125 /* free unneeded memory */
4126 map_free(&addedmap);
4129 /* create weak map */
4130 map_init(&solv->weakrulemap, solv->nrules);
4131 for (i = 0; i < solv->weakruleq.count; i++)
4133 p = solv->weakruleq.elements[i];
4134 MAPSET(&solv->weakrulemap, p);
4137 /* all new rules are learnt after this point */
4138 solv->learntrules = solv->nrules;
4140 /* create assertion index. it is only used to speed up
4141 * makeruledecsions() a bit */
4142 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
4143 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
4144 queue_push(&solv->ruleassertions, i);
4146 /* disable update rules that conflict with our job */
4147 disableupdaterules(solv, job, -1);
4149 /* make decisions based on job/update assertions */
4150 makeruledecisions(solv);
4152 /* create watches chains */
4155 POOL_DEBUG(SAT_DEBUG_STATS, "problems so far: %d\n", solv->problems.count);
4158 * ********************************************
4160 * ********************************************
4163 run_solver(solv, 1, solv->dontinstallrecommended ? 0 : 1);
4167 /* disable all erase jobs (including weak "keep uninstalled" rules) */
4168 for (i = solv->jobrules, r = solv->rules + i; i < solv->learntrules; i++, r++)
4170 if (r->d < 0) /* disabled ? */
4172 if (r->p > 0) /* install job? */
4174 disablerule(solv, r);
4180 enabledisablelearntrules(solv);
4181 removedisabledconflicts(solv, &redoq);
4185 * find recommended packages
4188 /* if redoq.count == 0 we already found all recommended in the
4190 if (redoq.count || solv->dontinstallrecommended || !solv->dontshowinstalledrecommended)
4192 Id rec, *recp, p, *pp;
4194 /* create map of all recommened packages */
4195 solv->recommends_index = -1;
4196 MAPZERO(&solv->recommendsmap);
4197 for (i = 0; i < solv->decisionq.count; i++)
4199 p = solv->decisionq.elements[i];
4202 s = pool->solvables + p;
4205 recp = s->repo->idarraydata + s->recommends;
4206 while ((rec = *recp++) != 0)
4208 FOR_PROVIDES(p, pp, rec)
4209 if (solv->decisionmap[p] > 0)
4213 if (!solv->dontshowinstalledrecommended)
4215 FOR_PROVIDES(p, pp, rec)
4216 if (solv->decisionmap[p] > 0)
4217 MAPSET(&solv->recommendsmap, p);
4219 continue; /* p != 0: already fulfilled */
4221 FOR_PROVIDES(p, pp, rec)
4222 MAPSET(&solv->recommendsmap, p);
4226 for (i = 1; i < pool->nsolvables; i++)
4228 if (solv->decisionmap[i] < 0)
4230 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
4232 s = pool->solvables + i;
4233 if (!MAPTST(&solv->recommendsmap, i))
4235 if (!s->supplements)
4237 if (!pool_installable(pool, s))
4239 if (!solver_is_supplementing(solv, s))
4242 if (solv->dontinstallrecommended)
4243 queue_push(&solv->recommendations, i);
4245 queue_pushunique(&solv->recommendations, i);
4247 /* we use MODE_SUGGEST here so that repo prio is ignored */
4248 policy_filter_unwanted(solv, &solv->recommendations, 0, POLICY_MODE_SUGGEST);
4252 * find suggested packages
4257 Id sug, *sugp, p, *pp;
4259 /* create map of all suggests that are still open */
4260 solv->recommends_index = -1;
4261 MAPZERO(&solv->suggestsmap);
4262 for (i = 0; i < solv->decisionq.count; i++)
4264 p = solv->decisionq.elements[i];
4267 s = pool->solvables + p;
4270 sugp = s->repo->idarraydata + s->suggests;
4271 while ((sug = *sugp++) != 0)
4273 FOR_PROVIDES(p, pp, sug)
4274 if (solv->decisionmap[p] > 0)
4278 if (!solv->dontshowinstalledrecommended)
4280 FOR_PROVIDES(p, pp, sug)
4281 if (solv->decisionmap[p] > 0)
4282 MAPSET(&solv->suggestsmap, p);
4284 continue; /* already fulfilled */
4286 FOR_PROVIDES(p, pp, sug)
4287 MAPSET(&solv->suggestsmap, p);
4291 for (i = 1; i < pool->nsolvables; i++)
4293 if (solv->decisionmap[i] < 0)
4295 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
4297 s = pool->solvables + i;
4298 if (!MAPTST(&solv->suggestsmap, i))
4302 if (!pool_installable(pool, s))
4304 if (!solver_is_enhancing(solv, s))
4307 queue_push(&solv->suggestions, i);
4309 policy_filter_unwanted(solv, &solv->suggestions, 0, POLICY_MODE_SUGGEST);
4314 /* restore decisionmap */
4315 for (i = 0; i < redoq.count; i += 2)
4316 solv->decisionmap[redoq.elements[i]] = redoq.elements[i + 1];
4320 * if unsolvable, prepare solutions
4323 if (solv->problems.count)
4325 int recocount = solv->recommendations.count;
4326 solv->recommendations.count = 0; /* so that revert() doesn't mess with it */
4327 queue_empty(&redoq);
4328 for (i = 0; i < solv->decisionq.count; i++)
4330 Id p = solv->decisionq.elements[i];
4331 queue_push(&redoq, p);
4332 queue_push(&redoq, solv->decisionq_why.elements[i]);
4333 queue_push(&redoq, solv->decisionmap[p > 0 ? p : -p]);
4335 problems_to_solutions(solv, job);
4336 memset(solv->decisionmap, 0, pool->nsolvables * sizeof(Id));
4337 queue_empty(&solv->decisionq);
4338 queue_empty(&solv->decisionq_why);
4339 for (i = 0; i < redoq.count; i += 3)
4341 Id p = redoq.elements[i];
4342 queue_push(&solv->decisionq, p);
4343 queue_push(&solv->decisionq_why, redoq.elements[i + 1]);
4344 solv->decisionmap[p > 0 ? p : -p] = redoq.elements[i + 2];
4346 solv->recommendations.count = recocount;
4349 POOL_DEBUG(SAT_DEBUG_STATS, "final solver statistics: %d learned rules, %d unsolvable\n", solv->stats_learned, solv->stats_unsolvable);
4353 /***********************************************************************/
4354 /* disk usage computations */
4356 /*-------------------------------------------------------------------
4358 * calculate DU changes
4362 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
4366 solver_create_state_maps(solv, &installedmap, 0);
4367 pool_calc_duchanges(solv->pool, solv->installed, &installedmap, mps, nmps);
4368 map_free(&installedmap);
4372 /*-------------------------------------------------------------------
4374 * calculate changes in install size
4378 solver_calc_installsizechange(Solver *solv)
4383 solver_create_state_maps(solv, &installedmap, 0);
4384 change = pool_calc_installsizechange(solv->pool, solv->installed, &installedmap);
4385 map_free(&installedmap);