2 * Copyright (c) 2007-2008, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * SAT based dependency solver
25 #include "solverdebug.h"
27 #define RULES_BLOCK 63
29 /********************************************************************
31 * dependency check helpers
35 /*-------------------------------------------------------------------
36 * handle split provides
40 solver_splitprovides(Solver *solv, Id dep)
42 Pool *pool = solv->pool;
47 if (!solv->dosplitprovides || !solv->installed)
51 rd = GETRELDEP(pool, dep);
52 if (rd->flags != REL_WITH)
54 FOR_PROVIDES(p, pp, dep)
56 s = pool->solvables + p;
57 if (s->repo == solv->installed && s->name == rd->name)
64 /*-------------------------------------------------------------------
65 * solver_dep_installed
69 solver_dep_installed(Solver *solv, Id dep)
72 Pool *pool = solv->pool;
77 Reldep *rd = GETRELDEP(pool, dep);
78 if (rd->flags == REL_AND)
80 if (!solver_dep_installed(solv, rd->name))
82 return solver_dep_installed(solv, rd->evr);
84 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
85 return solver_dep_installed(solv, rd->evr);
87 FOR_PROVIDES(p, pp, dep)
89 if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
98 /************************************************************************/
101 * make assertion rules into decisions
103 * Go through rules and add direct assertions to the decisionqueue.
104 * If we find a conflict, disable rules and add them to problem queue.
108 makeruledecisions(Solver *solv)
110 Pool *pool = solv->pool;
115 int record_proof = 1;
117 /* The system solvable is always installed first */
118 assert(solv->decisionq.count == 0);
119 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
120 queue_push(&solv->decisionq_why, 0);
121 solv->decisionmap[SYSTEMSOLVABLE] = 1; /* installed at level '1' */
123 decisionstart = solv->decisionq.count;
124 for (ii = 0; ii < solv->ruleassertions.count; ii++)
126 ri = solv->ruleassertions.elements[ii];
127 r = solv->rules + ri;
129 if (r->d < 0 || !r->p || r->w2) /* disabled, dummy or no assertion */
131 /* do weak rules in phase 2 */
132 if (ri < solv->learntrules && MAPTST(&solv->weakrulemap, ri))
138 if (!solv->decisionmap[vv]) /* if not yet decided */
143 queue_push(&solv->decisionq, v);
144 queue_push(&solv->decisionq_why, r - solv->rules);
145 solv->decisionmap[vv] = v > 0 ? 1 : -1;
146 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
148 Solvable *s = solv->pool->solvables + vv;
150 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", pool_solvable2str(solv->pool, s));
152 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (assertion)\n", pool_solvable2str(solv->pool, s));
157 * check previous decision: is it sane ?
160 if (v > 0 && solv->decisionmap[vv] > 0) /* ok to install */
162 if (v < 0 && solv->decisionmap[vv] < 0) /* ok to remove */
168 * The rule (r) we're currently processing says something
169 * different (v = r->p) than a previous decision (decisionmap[abs(v)])
173 if (ri >= solv->learntrules)
175 /* conflict with a learnt rule */
176 /* can happen when packages cannot be installed for
177 * multiple reasons. */
178 /* we disable the learnt rule in this case */
179 solver_disablerule(solv, r);
184 * find the decision which is the "opposite" of the rule
187 for (i = 0; i < solv->decisionq.count; i++)
188 if (solv->decisionq.elements[i] == -v)
190 assert(i < solv->decisionq.count); /* assert that we found it */
193 * conflict with system solvable ?
196 if (v == -SYSTEMSOLVABLE)
198 /* conflict with system solvable */
201 queue_push(&solv->problems, solv->learnt_pool.count);
202 queue_push(&solv->learnt_pool, ri);
203 queue_push(&solv->learnt_pool, 0);
206 queue_push(&solv->problems, 0);
207 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
208 if (ri >= solv->jobrules && ri < solv->jobrules_end)
209 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
212 queue_push(&solv->problems, v);
213 queue_push(&solv->problems, 0);
214 solver_disableproblem(solv, v);
218 assert(solv->decisionq_why.elements[i] > 0);
221 * conflict with an rpm rule ?
224 if (solv->decisionq_why.elements[i] < solv->rpmrules_end)
226 /* conflict with rpm rule assertion */
229 queue_push(&solv->problems, solv->learnt_pool.count);
230 queue_push(&solv->learnt_pool, ri);
231 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
232 queue_push(&solv->learnt_pool, 0);
235 queue_push(&solv->problems, 0);
236 assert(v > 0 || v == -SYSTEMSOLVABLE);
237 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
238 if (ri >= solv->jobrules && ri < solv->jobrules_end)
239 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
242 queue_push(&solv->problems, v);
243 queue_push(&solv->problems, 0);
244 solver_disableproblem(solv, v);
249 * conflict with another job or update/feature rule
255 queue_push(&solv->problems, solv->learnt_pool.count);
256 queue_push(&solv->learnt_pool, ri);
257 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
258 queue_push(&solv->learnt_pool, 0);
261 queue_push(&solv->problems, 0);
263 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflicting update/job assertions over literal %d\n", vv);
266 * push all of our rules (can only be feature or job rules)
267 * asserting this literal on the problem stack
270 for (i = solv->featurerules, rr = solv->rules + i; i < solv->learntrules; i++, rr++)
272 if (rr->d < 0 /* disabled */
273 || rr->w2) /* or no assertion */
275 if (rr->p != vv /* not affecting the literal */
278 if (MAPTST(&solv->weakrulemap, i)) /* weak: silently ignore */
281 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
283 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, solv->rules + i);
286 /* is is a job rule ? */
287 if (i >= solv->jobrules && i < solv->jobrules_end)
288 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
290 queue_push(&solv->problems, v);
291 solver_disableproblem(solv, v);
293 queue_push(&solv->problems, 0);
297 * (back up from decisions)
299 while (solv->decisionq.count > decisionstart)
301 v = solv->decisionq.elements[--solv->decisionq.count];
302 --solv->decisionq_why.count;
304 solv->decisionmap[vv] = 0;
306 ii = -1; /* restarts loop at 0 */
310 * phase 2: now do the weak assertions
312 for (ii = 0; ii < solv->ruleassertions.count; ii++)
314 ri = solv->ruleassertions.elements[ii];
315 r = solv->rules + ri;
316 if (r->d < 0 || r->w2) /* disabled or no assertion */
318 if (ri >= solv->learntrules || !MAPTST(&solv->weakrulemap, ri)) /* skip non-weak */
324 * (if not yet decided)
326 if (!solv->decisionmap[vv])
328 queue_push(&solv->decisionq, v);
329 queue_push(&solv->decisionq_why, r - solv->rules);
330 solv->decisionmap[vv] = v > 0 ? 1 : -1;
331 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
333 Solvable *s = solv->pool->solvables + vv;
335 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
337 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
342 * previously decided, sane ?
344 if (v > 0 && solv->decisionmap[vv] > 0)
346 if (v < 0 && solv->decisionmap[vv] < 0)
349 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
350 solver_printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
352 if (ri >= solv->jobrules && ri < solv->jobrules_end)
353 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
356 solver_disableproblem(solv, v);
358 solver_reenablepolicyrules(solv, -(v + 1));
363 /*-------------------------------------------------------------------
364 * enable/disable learnt rules
366 * we have enabled or disabled some of our rules. We now reenable all
367 * of our learnt rules except the ones that were learnt from rules that
371 enabledisablelearntrules(Solver *solv)
373 Pool *pool = solv->pool;
378 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
379 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
381 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
382 while ((why = *whyp++) != 0)
384 assert(why > 0 && why < i);
385 if (solv->rules[why].d < 0)
388 /* why != 0: we found a disabled rule, disable the learnt rule */
389 if (why && r->d >= 0)
391 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
393 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "disabling ");
394 solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
396 solver_disablerule(solv, r);
398 else if (!why && r->d < 0)
400 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
402 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "re-enabling ");
403 solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
405 solver_enablerule(solv, r);
411 /********************************************************************/
415 /*-------------------------------------------------------------------
418 * initial setup for all watches
422 makewatches(Solver *solv)
426 int nsolvables = solv->pool->nsolvables;
428 sat_free(solv->watches);
429 /* lower half for removals, upper half for installs */
430 solv->watches = sat_calloc(2 * nsolvables, sizeof(Id));
432 /* do it reverse so rpm rules get triggered first (XXX: obsolete?) */
433 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
435 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
438 if (!r->w2) /* assertions do not need watches */
441 /* see addwatches_rule(solv, r) */
442 r->n1 = solv->watches[nsolvables + r->w1];
443 solv->watches[nsolvables + r->w1] = r - solv->rules;
445 r->n2 = solv->watches[nsolvables + r->w2];
446 solv->watches[nsolvables + r->w2] = r - solv->rules;
451 /*-------------------------------------------------------------------
453 * add watches (for a new learned rule)
454 * sets up watches for a single rule
456 * see also makewatches() above.
460 addwatches_rule(Solver *solv, Rule *r)
462 int nsolvables = solv->pool->nsolvables;
464 r->n1 = solv->watches[nsolvables + r->w1];
465 solv->watches[nsolvables + r->w1] = r - solv->rules;
467 r->n2 = solv->watches[nsolvables + r->w2];
468 solv->watches[nsolvables + r->w2] = r - solv->rules;
472 /********************************************************************/
478 /* shortcuts to check if a literal (positive or negative) assignment
479 * evaluates to 'true' or 'false'
481 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
482 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
483 #define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
485 /*-------------------------------------------------------------------
489 * make decision and propagate to all rules
491 * Evaluate each term affected by the decision (linked through watches)
492 * If we find unit rules we make new decisions based on them
494 * Everything's fixed there, it's just finding rules that are
497 * return : 0 = everything is OK
498 * rule = conflict found in this rule
502 propagate(Solver *solv, int level)
504 Pool *pool = solv->pool;
505 Id *rp, *next_rp; /* rule pointer, next rule pointer in linked list */
507 Id p, pkg, other_watch;
509 Id *decisionmap = solv->decisionmap;
511 Id *watches = solv->watches + pool->nsolvables; /* place ptr in middle */
513 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
515 /* foreach non-propagated decision */
516 while (solv->propagate_index < solv->decisionq.count)
519 * 'pkg' was just decided
520 * negate because our watches trigger if literal goes FALSE
522 pkg = -solv->decisionq.elements[solv->propagate_index++];
524 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
526 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
527 solver_printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
530 /* foreach rule where 'pkg' is now FALSE */
531 for (rp = watches + pkg; *rp; rp = next_rp)
533 r = solv->rules + *rp;
536 /* rule is disabled, goto next */
544 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
546 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
547 solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
550 /* 'pkg' was just decided (was set to FALSE)
552 * now find other literal watch, check clause
553 * and advance on linked list
567 * This term is already true (through the other literal)
568 * so we have nothing to do
570 if (DECISIONMAP_TRUE(other_watch))
574 * The other literal is FALSE or UNDEF
580 /* Not a binary clause, try to move our watch.
582 * Go over all literals and find one that is
586 * (TRUE is also ok, in that case the rule is fulfilled)
588 if (r->p /* we have a 'p' */
589 && r->p != other_watch /* which is not watched */
590 && !DECISIONMAP_FALSE(r->p)) /* and not FALSE */
594 else /* go find a 'd' to make 'true' */
597 we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
599 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
601 if (p != other_watch /* which is not watched */
602 && !DECISIONMAP_FALSE(p)) /* and not FALSE */
610 * if we found some p that is UNDEF or TRUE, move
613 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
616 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, p));
618 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, -p));
634 watches[p] = r - solv->rules;
637 /* search failed, thus all unwatched literals are FALSE */
642 * unit clause found, set literal other_watch to TRUE
645 if (DECISIONMAP_FALSE(other_watch)) /* check if literal is FALSE */
646 return r; /* eek, a conflict! */
648 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
650 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " unit ");
651 solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
655 decisionmap[other_watch] = level; /* install! */
657 decisionmap[-other_watch] = -level; /* remove! */
659 queue_push(&solv->decisionq, other_watch);
660 queue_push(&solv->decisionq_why, r - solv->rules);
662 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
665 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", pool_solvid2str(pool, other_watch));
667 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", pool_solvid2str(pool, -other_watch));
670 } /* foreach rule involving 'pkg' */
672 } /* while we have non-decided decisions */
674 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
676 return 0; /* all is well */
680 /********************************************************************/
683 /*-------------------------------------------------------------------
690 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
692 Pool *pool = solv->pool;
696 Map seen; /* global? */
697 Id d, v, vv, *dp, why;
699 int num = 0, l1num = 0;
700 int learnt_why = solv->learnt_pool.count;
701 Id *decisionmap = solv->decisionmap;
703 queue_init_buffer(&r, r_buf, sizeof(r_buf)/sizeof(*r_buf));
705 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
706 map_init(&seen, pool->nsolvables);
707 idx = solv->decisionq.count;
710 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
711 solver_printruleclass(solv, SAT_DEBUG_ANALYZE, c);
712 queue_push(&solv->learnt_pool, c - solv->rules);
713 d = c->d < 0 ? -c->d - 1 : c->d;
714 dp = d ? pool->whatprovidesdata + d : 0;
715 /* go through all literals of the rule */
727 if (DECISIONMAP_TRUE(v)) /* the one true literal */
730 if (MAPTST(&seen, vv))
732 l = solv->decisionmap[vv];
735 MAPSET(&seen, vv); /* mark that we also need to look at this literal */
737 l1num++; /* need to do this one in level1 pass */
739 num++; /* need to do this one as well */
742 queue_push(&r, v); /* not level1 or conflict level, add to new rule */
748 if (!num && !--l1num)
749 break; /* all level 1 literals done */
751 /* find the next literal to investigate */
752 /* (as num + l1num > 0, we know that we'll always find one) */
756 v = solv->decisionq.elements[--idx];
758 if (MAPTST(&seen, vv))
763 if (num && --num == 0)
765 *pr = -v; /* so that v doesn't get lost */
768 POOL_DEBUG(SAT_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
769 /* clear non-l1 bits from seen map */
770 for (i = 0; i < r.count; i++)
773 MAPCLR(&seen, v > 0 ? v : -v);
775 /* only level 1 marks left in seen map */
776 l1num++; /* as l1retry decrements it */
780 why = solv->decisionq_why.elements[idx];
781 if (why <= 0) /* just in case, maybe for SYSTEMSOLVABLE */
783 c = solv->rules + why;
789 else if (r.count == 1 && r.elements[0] < 0)
792 *dr = pool_queuetowhatprovides(pool, &r);
793 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
795 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
796 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, *pr);
797 for (i = 0; i < r.count; i++)
798 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
800 /* push end marker on learnt reasons stack */
801 queue_push(&solv->learnt_pool, 0);
805 solv->stats_learned++;
810 /*-------------------------------------------------------------------
814 * reset the solver decisions to right after the rpm rules.
815 * called after rules have been enabled/disabled
819 solver_reset(Solver *solv)
821 Pool *pool = solv->pool;
825 /* rewind all decisions */
826 for (i = solv->decisionq.count - 1; i >= 0; i--)
828 v = solv->decisionq.elements[i];
829 solv->decisionmap[v > 0 ? v : -v] = 0;
831 solv->decisionq_why.count = 0;
832 solv->decisionq.count = 0;
833 solv->recommends_index = -1;
834 solv->propagate_index = 0;
835 solv->recommendations.count = 0;
836 solv->branches.count = 0;
838 /* adapt learnt rule status to new set of enabled/disabled rules */
839 enabledisablelearntrules(solv);
841 /* redo all assertion rule decisions */
842 makeruledecisions(solv);
843 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
847 /*-------------------------------------------------------------------
849 * analyze_unsolvable_rule
853 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp, Map *rseen)
855 Pool *pool = solv->pool;
857 Id why = r - solv->rules;
859 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
860 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
861 if (solv->learntrules && why >= solv->learntrules)
863 if (MAPTST(rseen, why - solv->learntrules))
865 MAPSET(rseen, why - solv->learntrules);
866 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
867 if (solv->learnt_pool.elements[i] > 0)
868 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp, rseen);
871 if (MAPTST(&solv->weakrulemap, why))
872 if (!*lastweakp || why > *lastweakp)
874 /* do not add rpm rules to problem */
875 if (why < solv->rpmrules_end)
877 /* turn rule into problem */
878 if (why >= solv->jobrules && why < solv->jobrules_end)
879 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
880 /* normalize dup/infarch rules */
881 if (why > solv->infarchrules && why < solv->infarchrules_end)
883 Id name = pool->solvables[-solv->rules[why].p].name;
884 while (why > solv->infarchrules && pool->solvables[-solv->rules[why - 1].p].name == name)
887 if (why > solv->duprules && why < solv->duprules_end)
889 Id name = pool->solvables[-solv->rules[why].p].name;
890 while (why > solv->duprules && pool->solvables[-solv->rules[why - 1].p].name == name)
894 /* return if problem already countains our rule */
895 if (solv->problems.count)
897 for (i = solv->problems.count - 1; i >= 0; i--)
898 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
900 else if (solv->problems.elements[i] == why)
903 queue_push(&solv->problems, why);
907 /*-------------------------------------------------------------------
911 * return: 1 - disabled some rules, try again
916 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
918 Pool *pool = solv->pool;
920 Map seen; /* global to speed things up? */
922 Id d, v, vv, *dp, why;
924 Id *decisionmap = solv->decisionmap;
926 int oldlearntpoolcount;
928 int record_proof = 1;
930 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
931 solv->stats_unsolvable++;
932 oldproblemcount = solv->problems.count;
933 oldlearntpoolcount = solv->learnt_pool.count;
935 /* make room for proof index */
936 /* must update it later, as analyze_unsolvable_rule would confuse
937 * it with a rule index if we put the real value in already */
938 queue_push(&solv->problems, 0);
941 map_init(&seen, pool->nsolvables);
942 map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
944 queue_push(&solv->learnt_pool, r - solv->rules);
946 analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
947 d = r->d < 0 ? -r->d - 1 : r->d;
948 dp = d ? pool->whatprovidesdata + d : 0;
959 if (DECISIONMAP_TRUE(v)) /* the one true literal */
962 l = solv->decisionmap[vv];
967 idx = solv->decisionq.count;
970 v = solv->decisionq.elements[--idx];
972 if (!MAPTST(&seen, vv))
974 why = solv->decisionq_why.elements[idx];
977 queue_push(&solv->learnt_pool, why);
978 r = solv->rules + why;
979 analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
980 d = r->d < 0 ? -r->d - 1 : r->d;
981 dp = d ? pool->whatprovidesdata + d : 0;
992 if (DECISIONMAP_TRUE(v)) /* the one true literal */
995 l = solv->decisionmap[vv];
1003 queue_push(&solv->problems, 0); /* mark end of this problem */
1008 /* disable last weak rule */
1009 solv->problems.count = oldproblemcount;
1010 solv->learnt_pool.count = oldlearntpoolcount;
1011 if (lastweak >= solv->jobrules && lastweak < solv->jobrules_end)
1012 v = -(solv->ruletojob.elements[lastweak - solv->jobrules] + 1);
1015 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling ");
1016 solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, solv->rules + lastweak);
1017 if (lastweak >= solv->choicerules && lastweak < solv->choicerules_end)
1018 solver_disablechoicerules(solv, solv->rules + lastweak);
1019 solver_disableproblem(solv, v);
1021 solver_reenablepolicyrules(solv, -(v + 1));
1029 queue_push(&solv->learnt_pool, 0);
1030 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1035 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1036 solver_disableproblem(solv, solv->problems.elements[i]);
1037 /* XXX: might want to enable all weak rules again */
1041 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1046 /********************************************************************/
1047 /* Decision revert */
1049 /*-------------------------------------------------------------------
1052 * revert decisionq to a level
1056 revert(Solver *solv, int level)
1058 Pool *pool = solv->pool;
1060 while (solv->decisionq.count)
1062 v = solv->decisionq.elements[solv->decisionq.count - 1];
1063 vv = v > 0 ? v : -v;
1064 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1066 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1067 if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
1068 solv->recommendations.count--;
1069 solv->decisionmap[vv] = 0;
1070 solv->decisionq.count--;
1071 solv->decisionq_why.count--;
1072 solv->propagate_index = solv->decisionq.count;
1074 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1076 solv->branches.count--;
1077 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1078 solv->branches.count--;
1080 solv->recommends_index = -1;
1084 /*-------------------------------------------------------------------
1086 * watch2onhighest - put watch2 on literal with highest level
1090 watch2onhighest(Solver *solv, Rule *r)
1095 d = r->d < 0 ? -r->d - 1 : r->d;
1097 return; /* binary rule, both watches are set */
1098 dp = solv->pool->whatprovidesdata + d;
1099 while ((v = *dp++) != 0)
1101 l = solv->decisionmap[v < 0 ? -v : v];
1113 /*-------------------------------------------------------------------
1117 * add free decision (solvable to install) to decisionq
1118 * increase level and propagate decision
1119 * return if no conflict.
1121 * in conflict case, analyze conflict rule, add resulting
1122 * rule to learnt rule set, make decision from learnt
1123 * rule (always unit) and re-propagate.
1125 * returns the new solver level or 0 if unsolvable
1130 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules, Id ruleid)
1132 Pool *pool = solv->pool;
1137 assert(ruleid >= 0);
1142 solv->decisionmap[decision] = level;
1144 solv->decisionmap[-decision] = -level;
1145 queue_push(&solv->decisionq, decision);
1146 queue_push(&solv->decisionq_why, -ruleid); /* <= 0 -> free decision */
1150 r = propagate(solv, level);
1154 return analyze_unsolvable(solv, r, disablerules);
1155 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1156 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
1157 assert(l > 0 && l < level);
1158 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1160 revert(solv, level);
1161 r = solver_addrule(solv, p, d);
1163 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
1164 queue_push(&solv->learnt_why, why);
1167 /* at least 2 literals, needs watches */
1168 watch2onhighest(solv, r);
1169 addwatches_rule(solv, r);
1173 /* learnt rule is an assertion */
1174 queue_push(&solv->ruleassertions, r - solv->rules);
1176 /* the new rule is unit by design */
1177 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1178 queue_push(&solv->decisionq, p);
1179 queue_push(&solv->decisionq_why, r - solv->rules);
1180 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1182 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
1183 solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
1184 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
1185 solver_printrule(solv, SAT_DEBUG_ANALYZE, r);
1192 /*-------------------------------------------------------------------
1194 * select and install
1196 * install best package from the queue. We add an extra package, inst, if
1197 * provided. See comment in weak install section.
1199 * returns the new solver level or 0 if unsolvable
1204 selectandinstall(Solver *solv, int level, Queue *dq, int disablerules, Id ruleid)
1206 Pool *pool = solv->pool;
1211 policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1214 /* XXX: didn't we already do that? */
1215 /* XXX: shouldn't we prefer installed packages? */
1216 /* XXX: move to policy.c? */
1217 /* choose the supplemented one */
1218 for (i = 0; i < dq->count; i++)
1219 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1221 dq->elements[0] = dq->elements[i];
1228 /* multiple candidates, open a branch */
1229 for (i = 1; i < dq->count; i++)
1230 queue_push(&solv->branches, dq->elements[i]);
1231 queue_push(&solv->branches, -level);
1233 p = dq->elements[0];
1235 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", pool_solvid2str(pool, p));
1237 return setpropagatelearn(solv, level, p, disablerules, ruleid);
1241 /********************************************************************/
1242 /* Main solver interface */
1245 /*-------------------------------------------------------------------
1248 * create solver structure
1250 * pool: all available solvables
1251 * installed: installed Solvables
1254 * Upon solving, rules are created to flag the Solvables
1255 * of the 'installed' Repo as installed.
1259 solver_create(Pool *pool)
1262 solv = (Solver *)sat_calloc(1, sizeof(Solver));
1264 solv->installed = pool->installed;
1266 transaction_init(&solv->trans, pool);
1267 queue_init(&solv->ruletojob);
1268 queue_init(&solv->decisionq);
1269 queue_init(&solv->decisionq_why);
1270 queue_init(&solv->problems);
1271 queue_init(&solv->suggestions);
1272 queue_init(&solv->recommendations);
1273 queue_init(&solv->orphaned);
1274 queue_init(&solv->learnt_why);
1275 queue_init(&solv->learnt_pool);
1276 queue_init(&solv->branches);
1277 queue_init(&solv->covenantq);
1278 queue_init(&solv->weakruleq);
1279 queue_init(&solv->ruleassertions);
1281 queue_push(&solv->learnt_pool, 0); /* so that 0 does not describe a proof */
1283 map_init(&solv->recommendsmap, pool->nsolvables);
1284 map_init(&solv->suggestsmap, pool->nsolvables);
1285 map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1286 solv->recommends_index = 0;
1288 solv->decisionmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
1290 solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1291 memset(solv->rules, 0, sizeof(Rule));
1297 /*-------------------------------------------------------------------
1303 solver_free(Solver *solv)
1305 transaction_free(&solv->trans);
1306 queue_free(&solv->job);
1307 queue_free(&solv->ruletojob);
1308 queue_free(&solv->decisionq);
1309 queue_free(&solv->decisionq_why);
1310 queue_free(&solv->learnt_why);
1311 queue_free(&solv->learnt_pool);
1312 queue_free(&solv->problems);
1313 queue_free(&solv->solutions);
1314 queue_free(&solv->suggestions);
1315 queue_free(&solv->recommendations);
1316 queue_free(&solv->orphaned);
1317 queue_free(&solv->branches);
1318 queue_free(&solv->covenantq);
1319 queue_free(&solv->weakruleq);
1320 queue_free(&solv->ruleassertions);
1322 map_free(&solv->recommendsmap);
1323 map_free(&solv->suggestsmap);
1324 map_free(&solv->noupdate);
1325 map_free(&solv->weakrulemap);
1326 map_free(&solv->noobsoletes);
1328 map_free(&solv->updatemap);
1329 map_free(&solv->fixmap);
1330 map_free(&solv->dupmap);
1331 map_free(&solv->dupinvolvedmap);
1332 map_free(&solv->droporphanedmap);
1333 map_free(&solv->cleandepsmap);
1335 sat_free(solv->decisionmap);
1336 sat_free(solv->rules);
1337 sat_free(solv->watches);
1338 sat_free(solv->obsoletes);
1339 sat_free(solv->obsoletes_data);
1340 sat_free(solv->multiversionupdaters);
1341 sat_free(solv->choicerules_ref);
1346 /*-------------------------------------------------------------------
1350 * all rules have been set up, now actually run the solver
1355 solver_run_sat(Solver *solv, int disablerules, int doweak)
1357 Queue dq; /* local decisionqueue */
1358 Queue dqs; /* local decisionqueue for supplements */
1364 Pool *pool = solv->pool;
1366 int minimizationsteps;
1367 int installedpos = solv->installed ? solv->installed->start : 0;
1369 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1371 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1372 for (i = 1; i < solv->nrules; i++)
1373 solver_printruleclass(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
1376 POOL_DEBUG(SAT_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1378 /* start SAT algorithm */
1380 systemlevel = level + 1;
1381 POOL_DEBUG(SAT_DEBUG_SOLVER, "solving...\n");
1387 * here's the main loop:
1388 * 1) propagate new decisions (only needed once)
1390 * 3) try to keep installed packages
1391 * 4) fulfill all unresolved rules
1392 * 5) install recommended packages
1393 * 6) minimalize solution if we had choices
1394 * if we encounter a problem, we rewind to a safe level and restart
1398 minimizationsteps = 0;
1402 * initial propagation of the assertions
1406 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1407 if ((r = propagate(solv, level)) != 0)
1409 if (analyze_unsolvable(solv, r, disablerules))
1418 * resolve jobs first
1420 if (level < systemlevel)
1422 POOL_DEBUG(SAT_DEBUG_SOLVER, "resolving job rules\n");
1423 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1426 if (r->d < 0) /* ignore disabled rules */
1429 FOR_RULELITERALS(l, dp, r)
1433 if (solv->decisionmap[-l] <= 0)
1438 if (solv->decisionmap[l] > 0)
1440 if (solv->decisionmap[l] == 0)
1446 /* prune to installed if not updating */
1447 if (dq.count > 1 && solv->installed && !solv->updatemap_all)
1450 for (j = k = 0; j < dq.count; j++)
1452 Solvable *s = pool->solvables + dq.elements[j];
1453 if (s->repo == solv->installed)
1455 dq.elements[k++] = dq.elements[j];
1456 if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
1458 k = 0; /* package wants to be updated, do not prune */
1467 level = selectandinstall(solv, level, &dq, disablerules, i);
1474 if (level <= olevel)
1477 systemlevel = level + 1;
1478 if (i < solv->jobrules_end)
1484 * installed packages
1486 if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1488 Repo *installed = solv->installed;
1491 POOL_DEBUG(SAT_DEBUG_SOLVER, "resolving installed packages\n");
1492 /* we use two passes if we need to update packages
1493 * to create a better user experience */
1494 for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1496 int passlevel = level;
1497 /* start with installedpos, the position that gave us problems last time */
1498 for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1503 if (i == installed->end)
1504 i = installed->start;
1505 s = pool->solvables + i;
1506 if (s->repo != installed)
1509 if (solv->decisionmap[i] > 0)
1511 if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1512 continue; /* updates first */
1513 r = solv->rules + solv->updaterules + (i - installed->start);
1515 if (!rr->p || rr->d < 0) /* disabled -> look at feature rule */
1516 rr -= solv->installed->end - solv->installed->start;
1517 if (!rr->p) /* identical to update rule? */
1520 continue; /* orpaned package */
1522 /* XXX: noupdate check is probably no longer needed, as all jobs should
1523 * already be satisfied */
1524 /* Actually we currently still need it because of erase jobs */
1525 /* if noupdate is set we do not look at update candidates */
1527 if (!MAPTST(&solv->noupdate, i - installed->start) && (solv->decisionmap[i] < 0 || solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, i - installed->start)) || rr->p != i))
1529 if (solv->noobsoletes.size && solv->multiversionupdaters
1530 && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1532 /* special multiversion handling, make sure best version is chosen */
1534 while ((p = pool->whatprovidesdata[d++]) != 0)
1535 if (solv->decisionmap[p] >= 0)
1537 policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1539 if (p != i && solv->decisionmap[p] == 0)
1541 rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1542 if (!rr->p) /* update rule == feature rule? */
1543 rr = rr - solv->featurerules + solv->updaterules;
1551 /* update to best package */
1552 FOR_RULELITERALS(p, dp, rr)
1554 if (solv->decisionmap[p] > 0)
1556 dq.count = 0; /* already fulfilled */
1559 if (!solv->decisionmap[p])
1564 /* install best version */
1568 level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1575 if (level <= olevel)
1577 if (level == 1 || level < passlevel)
1578 break; /* trouble */
1580 n = installed->start; /* redo all */
1586 /* if still undecided keep package */
1587 if (solv->decisionmap[i] == 0)
1590 if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
1592 POOL_DEBUG(SAT_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
1593 level = setpropagatelearn(solv, level, -i, disablerules, 0);
1597 POOL_DEBUG(SAT_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
1598 level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1606 if (level <= olevel)
1608 if (level == 1 || level < passlevel)
1609 break; /* trouble */
1611 n = installed->start; /* redo all */
1614 continue; /* retry with learnt rule */
1618 if (n < installed->end)
1620 installedpos = i; /* retry problem solvable next time */
1621 break; /* ran into trouble */
1623 installedpos = installed->start; /* reset installedpos */
1625 systemlevel = level + 1;
1627 continue; /* had trouble, retry */
1630 if (level < systemlevel)
1631 systemlevel = level;
1636 POOL_DEBUG(SAT_DEBUG_POLICY, "deciding unresolved rules\n");
1637 for (i = 1, n = 1; n < solv->nrules; i++, n++)
1639 if (i == solv->nrules)
1641 r = solv->rules + i;
1642 if (r->d < 0) /* ignore disabled rules */
1647 /* binary or unary rule */
1648 /* need two positive undecided literals */
1649 if (r->p < 0 || r->w2 <= 0)
1651 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1653 queue_push(&dq, r->p);
1654 queue_push(&dq, r->w2);
1659 * all negative literals are installed
1660 * no positive literal is installed
1661 * i.e. the rule is not fulfilled and we
1662 * just need to decide on the positive literals
1666 if (solv->decisionmap[-r->p] <= 0)
1671 if (solv->decisionmap[r->p] > 0)
1673 if (solv->decisionmap[r->p] == 0)
1674 queue_push(&dq, r->p);
1676 dp = pool->whatprovidesdata + r->d;
1677 while ((p = *dp++) != 0)
1681 if (solv->decisionmap[-p] <= 0)
1686 if (solv->decisionmap[p] > 0)
1688 if (solv->decisionmap[p] == 0)
1695 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1697 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
1698 solver_printruleclass(solv, SAT_DEBUG_PROPAGATE, r);
1700 /* dq.count < 2 cannot happen as this means that
1701 * the rule is unit */
1702 assert(dq.count > 1);
1705 level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
1712 if (level < systemlevel || level == 1)
1713 break; /* trouble */
1714 /* something changed, so look at all rules again */
1718 if (n != solv->nrules) /* ran into trouble, restart */
1721 /* at this point we have a consistent system. now do the extras... */
1727 POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended packages\n");
1728 queue_empty(&dq); /* recommended packages */
1729 queue_empty(&dqs); /* supplemented packages */
1730 for (i = 1; i < pool->nsolvables; i++)
1732 if (solv->decisionmap[i] < 0)
1734 if (solv->decisionmap[i] > 0)
1736 /* installed, check for recommends */
1737 Id *recp, rec, pp, p;
1738 s = pool->solvables + i;
1739 if (solv->ignorealreadyrecommended && s->repo == solv->installed)
1741 /* XXX need to special case AND ? */
1744 recp = s->repo->idarraydata + s->recommends;
1745 while ((rec = *recp++) != 0)
1748 FOR_PROVIDES(p, pp, rec)
1750 if (solv->decisionmap[p] > 0)
1755 else if (solv->decisionmap[p] == 0)
1757 if (solv->dupmap_all && solv->installed && pool->solvables[p].repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))))
1759 queue_pushunique(&dq, p);
1767 s = pool->solvables + i;
1768 if (!s->supplements)
1770 if (!pool_installable(pool, s))
1772 if (!solver_is_supplementing(solv, s))
1774 if (solv->dupmap_all && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
1776 queue_push(&dqs, i);
1780 /* filter out all packages obsoleted by installed packages */
1781 /* this is no longer needed if we have reverse obsoletes */
1782 if ((dqs.count || dq.count) && solv->installed)
1785 Id obs, *obsp, po, ppo;
1787 map_init(&obsmap, pool->nsolvables);
1788 for (p = solv->installed->start; p < solv->installed->end; p++)
1790 s = pool->solvables + p;
1791 if (s->repo != solv->installed || !s->obsoletes)
1793 if (solv->decisionmap[p] <= 0)
1795 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
1797 obsp = s->repo->idarraydata + s->obsoletes;
1798 /* foreach obsoletes */
1799 while ((obs = *obsp++) != 0)
1800 FOR_PROVIDES(po, ppo, obs)
1801 MAPSET(&obsmap, po);
1803 for (i = j = 0; i < dqs.count; i++)
1804 if (!MAPTST(&obsmap, dqs.elements[i]))
1805 dqs.elements[j++] = dqs.elements[i];
1807 for (i = j = 0; i < dq.count; i++)
1808 if (!MAPTST(&obsmap, dq.elements[i]))
1809 dq.elements[j++] = dq.elements[i];
1814 /* filter out all already supplemented packages if requested */
1815 if (solv->ignorealreadyrecommended && dqs.count)
1817 /* turn off all new packages */
1818 for (i = 0; i < solv->decisionq.count; i++)
1820 p = solv->decisionq.elements[i];
1823 s = pool->solvables + p;
1824 if (s->repo && s->repo != solv->installed)
1825 solv->decisionmap[p] = -solv->decisionmap[p];
1827 /* filter out old supplements */
1828 for (i = j = 0; i < dqs.count; i++)
1830 p = dqs.elements[i];
1831 s = pool->solvables + p;
1832 if (!s->supplements)
1834 if (!solver_is_supplementing(solv, s))
1835 dqs.elements[j++] = p;
1838 /* undo turning off */
1839 for (i = 0; i < solv->decisionq.count; i++)
1841 p = solv->decisionq.elements[i];
1844 s = pool->solvables + p;
1845 if (s->repo && s->repo != solv->installed)
1846 solv->decisionmap[p] = -solv->decisionmap[p];
1850 /* multiversion doesn't mix well with supplements.
1851 * filter supplemented packages where we already decided
1852 * to install a different version (see bnc#501088) */
1853 if (dqs.count && solv->noobsoletes.size)
1855 for (i = j = 0; i < dqs.count; i++)
1857 p = dqs.elements[i];
1858 if (MAPTST(&solv->noobsoletes, p))
1861 s = pool->solvables + p;
1862 FOR_PROVIDES(p2, pp2, s->name)
1863 if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
1866 continue; /* ignore this package */
1868 dqs.elements[j++] = p;
1873 /* make dq contain both recommended and supplemented pkgs */
1876 for (i = 0; i < dqs.count; i++)
1877 queue_pushunique(&dq, dqs.elements[i]);
1883 int decisioncount = solv->decisionq.count;
1887 /* simple case, just one package. no need to choose */
1890 POOL_DEBUG(SAT_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
1892 POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
1893 queue_push(&solv->recommendations, p);
1894 level = setpropagatelearn(solv, level, p, 0, 0);
1895 continue; /* back to main loop */
1898 /* filter packages, this gives us the best versions */
1899 policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
1901 /* create map of result */
1902 map_init(&dqmap, pool->nsolvables);
1903 for (i = 0; i < dq.count; i++)
1904 MAPSET(&dqmap, dq.elements[i]);
1906 /* install all supplemented packages */
1907 for (i = 0; i < dqs.count; i++)
1909 p = dqs.elements[i];
1910 if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
1912 POOL_DEBUG(SAT_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
1913 queue_push(&solv->recommendations, p);
1915 level = setpropagatelearn(solv, level, p, 0, 0);
1916 if (level <= olevel)
1919 if (i < dqs.count || solv->decisionq.count < decisioncount)
1925 /* install all recommended packages */
1926 /* more work as we want to created branches if multiple
1927 * choices are valid */
1928 for (i = 0; i < decisioncount; i++)
1931 p = solv->decisionq.elements[i];
1934 s = pool->solvables + p;
1935 if (!s->repo || (solv->ignorealreadyrecommended && s->repo == solv->installed))
1939 recp = s->repo->idarraydata + s->recommends;
1940 while ((rec = *recp++) != 0)
1943 FOR_PROVIDES(p, pp, rec)
1945 if (solv->decisionmap[p] > 0)
1950 else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
1951 queue_pushunique(&dq, p);
1957 /* multiple candidates, open a branch */
1958 for (i = 1; i < dq.count; i++)
1959 queue_push(&solv->branches, dq.elements[i]);
1960 queue_push(&solv->branches, -level);
1963 POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
1964 queue_push(&solv->recommendations, p);
1966 level = setpropagatelearn(solv, level, p, 0, 0);
1967 if (level <= olevel || solv->decisionq.count < decisioncount)
1968 break; /* we had to revert some decisions */
1971 break; /* had a problem above, quit loop */
1975 continue; /* back to main loop so that all deps are checked */
1979 if (solv->dupmap_all && solv->installed)
1981 int installedone = 0;
1983 /* let's see if we can install some unsupported package */
1984 POOL_DEBUG(SAT_DEBUG_SOLVER, "deciding orphaned packages\n");
1985 for (i = 0; i < solv->orphaned.count; i++)
1987 p = solv->orphaned.elements[i];
1988 if (solv->decisionmap[p])
1989 continue; /* already decided */
1991 if (solv->droporphanedmap_all)
1993 if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
1995 POOL_DEBUG(SAT_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
1996 level = setpropagatelearn(solv, level, p, 0, 0);
2001 if (installedone || i < solv->orphaned.count)
2002 continue; /* back to main loop */
2003 for (i = 0; i < solv->orphaned.count; i++)
2005 p = solv->orphaned.elements[i];
2006 if (solv->decisionmap[p])
2007 continue; /* already decided */
2008 POOL_DEBUG(SAT_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2010 level = setpropagatelearn(solv, level, -p, 0, 0);
2014 if (i < solv->orphaned.count)
2015 continue; /* back to main loop */
2018 if (solv->solution_callback)
2020 solv->solution_callback(solv, solv->solution_callback_data);
2021 if (solv->branches.count)
2023 int i = solv->branches.count - 1;
2024 int l = -solv->branches.elements[i];
2028 if (solv->branches.elements[i - 1] < 0)
2030 p = solv->branches.elements[i];
2031 POOL_DEBUG(SAT_DEBUG_SOLVER, "branching with %s\n", pool_solvid2str(pool, p));
2033 for (j = i + 1; j < solv->branches.count; j++)
2034 queue_push(&dq, solv->branches.elements[j]);
2035 solv->branches.count = i;
2037 revert(solv, level);
2039 for (j = 0; j < dq.count; j++)
2040 queue_push(&solv->branches, dq.elements[j]);
2042 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2044 level = setpropagatelearn(solv, level, p, disablerules, why);
2053 /* all branches done, we're finally finished */
2057 /* minimization step */
2058 if (solv->branches.count)
2060 int l = 0, lasti = -1, lastl = -1;
2064 for (i = solv->branches.count - 1; i >= 0; i--)
2066 p = solv->branches.elements[i];
2069 else if (p > 0 && solv->decisionmap[p] > l + 1)
2077 /* kill old solvable so that we do not loop */
2078 p = solv->branches.elements[lasti];
2079 solv->branches.elements[lasti] = 0;
2080 POOL_DEBUG(SAT_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, pool_solvid2str(pool, p));
2081 minimizationsteps++;
2084 revert(solv, level);
2085 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2088 level = setpropagatelearn(solv, level, p, disablerules, why);
2095 continue; /* back to main loop */
2098 /* no minimization found, we're finally finished! */
2102 POOL_DEBUG(SAT_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2104 POOL_DEBUG(SAT_DEBUG_STATS, "done solving.\n\n");
2108 solver_printdecisionq(solv, SAT_DEBUG_RESULT);
2113 /*-------------------------------------------------------------------
2115 * remove disabled conflicts
2117 * purpose: update the decisionmap after some rules were disabled.
2118 * this is used to calculate the suggested/recommended package list.
2119 * Also returns a "removed" list to undo the discisionmap changes.
2123 removedisabledconflicts(Solver *solv, Queue *removed)
2125 Pool *pool = solv->pool;
2130 Id *decisionmap = solv->decisionmap;
2132 queue_empty(removed);
2133 for (i = 0; i < solv->decisionq.count; i++)
2135 p = solv->decisionq.elements[i];
2137 continue; /* conflicts only, please */
2138 why = solv->decisionq_why.elements[i];
2141 /* no rule involved, must be a orphan package drop */
2144 /* we never do conflicts on free decisions, so there
2145 * must have been an unit rule */
2147 r = solv->rules + why;
2148 if (r->d < 0 && decisionmap[-p])
2150 /* rule is now disabled, remove from decisionmap */
2151 POOL_DEBUG(SAT_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2152 queue_push(removed, -p);
2153 queue_push(removed, decisionmap[-p]);
2154 decisionmap[-p] = 0;
2157 if (!removed->count)
2159 /* we removed some confliced packages. some of them might still
2160 * be in conflict, so search for unit rules and re-conflict */
2162 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2164 if (i == solv->nrules)
2167 r = solv->rules + i;
2173 if (r->p < 0 && !decisionmap[-r->p])
2179 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2181 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2186 if (r->p < 0 && decisionmap[-r->p] == 0)
2188 if (new || DECISIONMAP_FALSE(r->p))
2190 dp = pool->whatprovidesdata + r->d;
2191 while ((p = *dp++) != 0)
2193 if (new && p == new)
2195 if (p < 0 && decisionmap[-p] == 0)
2204 else if (!DECISIONMAP_FALSE(p))
2214 POOL_DEBUG(SAT_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2215 decisionmap[-new] = -1;
2217 n = 0; /* redo all rules */
2223 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2226 for (i = 0; i < removed->count; i += 2)
2227 solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2231 /*-------------------------------------------------------------------
2233 * weaken solvable dependencies
2237 weaken_solvable_deps(Solver *solv, Id p)
2242 for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2246 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2247 continue; /* conflict */
2248 queue_push(&solv->weakruleq, i);
2253 /********************************************************************/
2258 findrecommendedsuggested(Solver *solv)
2260 Pool *pool = solv->pool;
2261 Queue redoq, disabledq;
2267 map_init(&obsmap, pool->nsolvables);
2268 if (solv->installed)
2270 Id obs, *obsp, p, po, ppo;
2271 for (p = solv->installed->start; p < solv->installed->end; p++)
2273 s = pool->solvables + p;
2274 if (s->repo != solv->installed || !s->obsoletes)
2276 if (solv->decisionmap[p] <= 0)
2278 if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
2280 obsp = s->repo->idarraydata + s->obsoletes;
2281 /* foreach obsoletes */
2282 while ((obs = *obsp++) != 0)
2283 FOR_PROVIDES(po, ppo, obs)
2284 MAPSET(&obsmap, po);
2289 queue_init(&disabledq);
2291 /* disable all erase jobs (including weak "keep uninstalled" rules) */
2292 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2294 if (r->d < 0) /* disabled ? */
2296 if (r->p >= 0) /* install job? */
2298 queue_push(&disabledq, i);
2299 solver_disablerule(solv, r);
2305 enabledisablelearntrules(solv);
2306 removedisabledconflicts(solv, &redoq);
2310 * find recommended packages
2313 /* if redoq.count == 0 we already found all recommended in the
2315 if (redoq.count || solv->dontinstallrecommended || !solv->dontshowinstalledrecommended || solv->ignorealreadyrecommended)
2317 Id rec, *recp, p, pp;
2319 /* create map of all recommened packages */
2320 solv->recommends_index = -1;
2321 MAPZERO(&solv->recommendsmap);
2322 for (i = 0; i < solv->decisionq.count; i++)
2324 p = solv->decisionq.elements[i];
2327 s = pool->solvables + p;
2330 recp = s->repo->idarraydata + s->recommends;
2331 while ((rec = *recp++) != 0)
2333 FOR_PROVIDES(p, pp, rec)
2334 if (solv->decisionmap[p] > 0)
2338 if (!solv->dontshowinstalledrecommended)
2340 FOR_PROVIDES(p, pp, rec)
2341 if (solv->decisionmap[p] > 0)
2342 MAPSET(&solv->recommendsmap, p);
2344 continue; /* p != 0: already fulfilled */
2346 FOR_PROVIDES(p, pp, rec)
2347 MAPSET(&solv->recommendsmap, p);
2351 for (i = 1; i < pool->nsolvables; i++)
2353 if (solv->decisionmap[i] < 0)
2355 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2357 if (MAPTST(&obsmap, i))
2359 s = pool->solvables + i;
2360 if (!MAPTST(&solv->recommendsmap, i))
2362 if (!s->supplements)
2364 if (!pool_installable(pool, s))
2366 if (!solver_is_supplementing(solv, s))
2369 if (solv->dontinstallrecommended)
2370 queue_push(&solv->recommendations, i);
2372 queue_pushunique(&solv->recommendations, i);
2374 /* we use MODE_SUGGEST here so that repo prio is ignored */
2375 policy_filter_unwanted(solv, &solv->recommendations, POLICY_MODE_SUGGEST);
2379 * find suggested packages
2384 Id sug, *sugp, p, pp;
2386 /* create map of all suggests that are still open */
2387 solv->recommends_index = -1;
2388 MAPZERO(&solv->suggestsmap);
2389 for (i = 0; i < solv->decisionq.count; i++)
2391 p = solv->decisionq.elements[i];
2394 s = pool->solvables + p;
2397 sugp = s->repo->idarraydata + s->suggests;
2398 while ((sug = *sugp++) != 0)
2400 FOR_PROVIDES(p, pp, sug)
2401 if (solv->decisionmap[p] > 0)
2405 if (!solv->dontshowinstalledrecommended)
2407 FOR_PROVIDES(p, pp, sug)
2408 if (solv->decisionmap[p] > 0)
2409 MAPSET(&solv->suggestsmap, p);
2411 continue; /* already fulfilled */
2413 FOR_PROVIDES(p, pp, sug)
2414 MAPSET(&solv->suggestsmap, p);
2418 for (i = 1; i < pool->nsolvables; i++)
2420 if (solv->decisionmap[i] < 0)
2422 if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2424 if (MAPTST(&obsmap, i))
2426 s = pool->solvables + i;
2427 if (!MAPTST(&solv->suggestsmap, i))
2431 if (!pool_installable(pool, s))
2433 if (!solver_is_enhancing(solv, s))
2436 queue_push(&solv->suggestions, i);
2438 policy_filter_unwanted(solv, &solv->suggestions, POLICY_MODE_SUGGEST);
2441 /* undo removedisabledconflicts */
2443 undo_removedisabledconflicts(solv, &redoq);
2446 /* undo job rule disabling */
2447 for (i = 0; i < disabledq.count; i++)
2448 solver_enablerule(solv, solv->rules + disabledq.elements[i]);
2449 queue_free(&disabledq);
2454 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap)
2457 Id how, what, select;
2459 for (i = 0; i < job->count; i += 2)
2461 how = job->elements[i];
2462 if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
2464 what = job->elements[i + 1];
2465 select = how & SOLVER_SELECTMASK;
2466 if (!noobsmap->size)
2467 map_grow(noobsmap, pool->nsolvables);
2468 FOR_JOB_SELECT(p, pp, select, what)
2469 MAPSET(noobsmap, p);
2474 * add a rule created by a job, record job number and weak flag
2477 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
2479 solver_addrule(solv, p, d);
2480 queue_push(&solv->ruletojob, job);
2482 queue_push(&solv->weakruleq, solv->nrules - 1);
2492 solver_solve(Solver *solv, Queue *job)
2494 Pool *pool = solv->pool;
2495 Repo *installed = solv->installed;
2498 Map addedmap; /* '1' == have rpm-rules for solvable */
2499 Map installcandidatemap;
2500 Id how, what, select, name, weak, p, pp, d;
2504 int now, solve_start;
2507 solve_start = sat_timems(0);
2509 /* log solver options */
2510 POOL_DEBUG(SAT_DEBUG_STATS, "solver started\n");
2511 POOL_DEBUG(SAT_DEBUG_STATS, "fixsystem=%d updatesystem=%d dosplitprovides=%d, noupdateprovide=%d noinfarchcheck=%d\n", solv->fixsystem, solv->updatesystem, solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
2512 POOL_DEBUG(SAT_DEBUG_STATS, "distupgrade=%d distupgrade_removeunsupported=%d\n", solv->distupgrade, solv->distupgrade_removeunsupported);
2513 POOL_DEBUG(SAT_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allowarchchange, solv->allowvendorchange);
2514 POOL_DEBUG(SAT_DEBUG_STATS, "promoteepoch=%d, novirtualconflicts=%d, allowselfconflicts=%d\n", pool->promoteepoch, pool->novirtualconflicts, pool->allowselfconflicts);
2515 POOL_DEBUG(SAT_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2516 POOL_DEBUG(SAT_DEBUG_STATS, "dontinstallrecommended=%d, ignorealreadyrecommended=%d, dontshowinstalledrecommended=%d\n", solv->dontinstallrecommended, solv->ignorealreadyrecommended, solv->dontshowinstalledrecommended);
2518 /* create whatprovides if not already there */
2519 if (!pool->whatprovides)
2520 pool_createwhatprovides(pool);
2522 /* create obsolete index */
2523 policy_create_obsolete_index(solv);
2526 queue_free(&solv->job);
2527 queue_init_clone(&solv->job, job);
2529 /* initialize with legacy values */
2530 solv->fixmap_all = solv->fixsystem;
2531 solv->updatemap_all = solv->updatesystem;
2532 solv->droporphanedmap_all = solv->distupgrade_removeunsupported;
2533 solv->dupmap_all = solv->distupgrade;
2536 * create basic rule set of all involved packages
2537 * use addedmap bitmap to make sure we don't create rules twice
2540 /* create noobsolete map if needed */
2541 solver_calculate_noobsmap(pool, job, &solv->noobsoletes);
2543 map_init(&addedmap, pool->nsolvables);
2544 MAPSET(&addedmap, SYSTEMSOLVABLE);
2546 map_init(&installcandidatemap, pool->nsolvables);
2549 now = sat_timems(0);
2551 * create rules for all package that could be involved with the solving
2552 * so called: rpm rules
2557 /* check for update/verify jobs as they need to be known early */
2558 for (i = 0; i < job->count; i += 2)
2560 how = job->elements[i];
2561 what = job->elements[i + 1];
2562 select = how & SOLVER_SELECTMASK;
2563 switch (how & SOLVER_JOBMASK)
2566 if (select == SOLVER_SOLVABLE_ALL)
2567 solv->fixmap_all = 1;
2568 FOR_JOB_SELECT(p, pp, select, what)
2570 s = pool->solvables + p;
2571 if (!solv->installed || s->repo != solv->installed)
2573 if (!solv->fixmap.size)
2574 map_grow(&solv->fixmap, solv->installed->end - solv->installed->start);
2575 MAPSET(&solv->fixmap, p - solv->installed->start);
2579 if (select == SOLVER_SOLVABLE_ALL)
2580 solv->updatemap_all = 1;
2581 FOR_JOB_SELECT(p, pp, select, what)
2583 s = pool->solvables + p;
2584 if (!solv->installed || s->repo != solv->installed)
2586 if (!solv->updatemap.size)
2587 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
2588 MAPSET(&solv->updatemap, p - solv->installed->start);
2596 oldnrules = solv->nrules;
2597 FOR_REPO_SOLVABLES(installed, p, s)
2598 solver_addrpmrulesforsolvable(solv, s, &addedmap);
2599 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
2600 oldnrules = solv->nrules;
2601 FOR_REPO_SOLVABLES(installed, p, s)
2602 solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
2603 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
2607 * create rules for all packages involved in the job
2608 * (to be installed or removed)
2611 oldnrules = solv->nrules;
2612 for (i = 0; i < job->count; i += 2)
2614 how = job->elements[i];
2615 what = job->elements[i + 1];
2616 select = how & SOLVER_SELECTMASK;
2618 switch (how & SOLVER_JOBMASK)
2620 case SOLVER_INSTALL:
2621 FOR_JOB_SELECT(p, pp, select, what)
2623 MAPSET(&installcandidatemap, p);
2624 solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
2627 case SOLVER_DISTUPGRADE:
2628 if (select == SOLVER_SOLVABLE_ALL)
2630 solv->dupmap_all = 1;
2631 solv->updatemap_all = 1;
2633 if (!solv->dupmap_all)
2640 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
2644 * add rules for suggests, enhances
2646 oldnrules = solv->nrules;
2647 solver_addrpmrulesforweak(solv, &addedmap);
2648 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
2651 * first pass done, we now have all the rpm rules we need.
2652 * unify existing rules before going over all job rules and
2654 * at this point the system is always solvable,
2655 * as an empty system (remove all packages) is a valid solution
2658 IF_POOLDEBUG (SAT_DEBUG_STATS)
2660 int possible = 0, installable = 0;
2661 for (i = 1; i < pool->nsolvables; i++)
2663 if (pool_installable(pool, pool->solvables + i))
2665 if (MAPTST(&addedmap, i))
2668 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
2671 solver_unifyrules(solv); /* remove duplicate rpm rules */
2672 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
2674 POOL_DEBUG(SAT_DEBUG_STATS, "rpm rule memory usage: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
2675 POOL_DEBUG(SAT_DEBUG_STATS, "rpm rule creation took %d ms\n", sat_timems(now));
2677 /* create dup maps if needed. We need the maps early to create our
2680 solver_createdupmaps(solv);
2683 * create feature rules
2685 * foreach installed:
2686 * create assertion (keep installed, if no update available)
2688 * create update rule (A|update1(A)|update2(A)|...)
2690 * those are used later on to keep a version of the installed packages in
2694 solv->featurerules = solv->nrules; /* mark start of feature rules */
2697 /* foreach possibly installed solvable */
2698 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2700 if (s->repo != installed)
2702 solver_addrule(solv, 0, 0); /* create dummy rule */
2705 solver_addupdaterule(solv, s, 1); /* allow s to be updated */
2707 /* make sure we accounted for all rules */
2708 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
2710 solv->featurerules_end = solv->nrules;
2713 * Add update rules for installed solvables
2715 * almost identical to feature rules
2716 * except that downgrades/archchanges/vendorchanges are not allowed
2719 solv->updaterules = solv->nrules;
2722 { /* foreach installed solvables */
2723 /* we create all update rules, but disable some later on depending on the job */
2724 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2728 if (s->repo != installed)
2730 solver_addrule(solv, 0, 0); /* create dummy rule */
2733 solver_addupdaterule(solv, s, 0); /* allowall = 0: downgrades not allowed */
2735 * check for and remove duplicate
2737 r = solv->rules + solv->nrules - 1; /* r: update rule */
2738 sr = r - (installed->end - installed->start); /* sr: feature rule */
2739 /* it's orphaned if there is no feature rule or the feature rule
2740 * consists just of the installed package */
2741 if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
2742 queue_push(&solv->orphaned, i);
2745 assert(solv->dupmap_all && !sr->p);
2748 if (!solver_samerule(solv, r, sr))
2750 /* identical rule, kill unneeded one */
2751 if (solv->allowuninstall)
2753 /* keep feature rule, make it weak */
2754 memset(r, 0, sizeof(*r));
2755 queue_push(&solv->weakruleq, sr - solv->rules);
2759 /* keep update rule */
2760 memset(sr, 0, sizeof(*sr));
2763 else if (solv->allowuninstall)
2765 /* make both feature and update rule weak */
2766 queue_push(&solv->weakruleq, r - solv->rules);
2767 queue_push(&solv->weakruleq, sr - solv->rules);
2770 solver_disablerule(solv, sr);
2772 /* consistency check: we added a rule for _every_ installed solvable */
2773 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
2775 solv->updaterules_end = solv->nrules;
2779 * now add all job rules
2782 solv->jobrules = solv->nrules;
2783 for (i = 0; i < job->count; i += 2)
2785 oldnrules = solv->nrules;
2787 how = job->elements[i];
2788 what = job->elements[i + 1];
2789 weak = how & SOLVER_WEAK;
2790 select = how & SOLVER_SELECTMASK;
2791 switch (how & SOLVER_JOBMASK)
2793 case SOLVER_INSTALL:
2794 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2795 if (select == SOLVER_SOLVABLE)
2803 FOR_JOB_SELECT(p, pp, select, what)
2807 /* no candidate found, make this an impossible rule */
2808 queue_push(&q, -SYSTEMSOLVABLE);
2810 p = queue_shift(&q); /* get first candidate */
2811 d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q); /* internalize */
2813 solver_addjobrule(solv, p, d, i, weak);
2816 POOL_DEBUG(SAT_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
2817 if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && solv->installed)
2818 map_grow(&solv->cleandepsmap, solv->installed->end - solv->installed->start);
2819 if (select == SOLVER_SOLVABLE && solv->installed && pool->solvables[what].repo == solv->installed)
2821 /* special case for "erase a specific solvable": we also
2822 * erase all other solvables with that name, so that they
2823 * don't get picked up as replacement */
2824 /* XXX: look also at packages that obsolete this package? */
2825 name = pool->solvables[what].name;
2826 FOR_PROVIDES(p, pp, name)
2830 s = pool->solvables + p;
2831 if (s->name != name)
2833 /* keep other versions installed */
2834 if (s->repo == solv->installed)
2836 /* keep installcandidates of other jobs */
2837 if (MAPTST(&installcandidatemap, p))
2839 solver_addjobrule(solv, -p, 0, i, weak); /* remove by id */
2842 FOR_JOB_SELECT(p, pp, select, what)
2843 solver_addjobrule(solv, -p, 0, i, weak);
2847 POOL_DEBUG(SAT_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2850 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2852 case SOLVER_WEAKENDEPS:
2853 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2854 if (select != SOLVER_SOLVABLE)
2856 s = pool->solvables + what;
2857 weaken_solvable_deps(solv, what);
2859 case SOLVER_NOOBSOLETES:
2860 POOL_DEBUG(SAT_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2863 POOL_DEBUG(SAT_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2864 FOR_JOB_SELECT(p, pp, select, what)
2866 s = pool->solvables + p;
2867 solver_addjobrule(solv, installed && s->repo == installed ? p : -p, 0, i, weak);
2870 case SOLVER_DISTUPGRADE:
2871 POOL_DEBUG(SAT_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
2873 case SOLVER_DROP_ORPHANED:
2874 POOL_DEBUG(SAT_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
2875 if (select == SOLVER_SOLVABLE_ALL)
2876 solv->droporphanedmap_all = 1;
2877 FOR_JOB_SELECT(p, pp, select, what)
2879 s = pool->solvables + p;
2880 if (!installed || s->repo != installed)
2882 if (!solv->droporphanedmap.size)
2883 map_grow(&solv->droporphanedmap, installed->end - installed->start);
2884 MAPSET(&solv->droporphanedmap, p - installed->start);
2887 case SOLVER_USERINSTALLED:
2888 POOL_DEBUG(SAT_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
2891 POOL_DEBUG(SAT_DEBUG_JOB, "job: unknown job\n");
2899 IF_POOLDEBUG (SAT_DEBUG_JOB)
2902 if (solv->nrules == oldnrules)
2903 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created\n");
2904 for (j = oldnrules; j < solv->nrules; j++)
2906 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
2907 solver_printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
2911 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
2912 solv->jobrules_end = solv->nrules;
2914 /* now create infarch and dup rules */
2915 if (!solv->noinfarchcheck)
2917 solver_addinfarchrules(solv, &addedmap);
2918 if (pool->obsoleteusescolors)
2920 /* currently doesn't work well with infarch rules, so make
2922 for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
2923 queue_push(&solv->weakruleq, i);
2927 solv->infarchrules = solv->infarchrules_end = solv->nrules;
2931 solver_addduprules(solv, &addedmap);
2932 solver_freedupmaps(solv); /* no longer needed */
2935 solv->duprules = solv->duprules_end = solv->nrules;
2938 solver_addchoicerules(solv);
2940 solv->choicerules = solv->choicerules_end = solv->nrules;
2942 /* all rules created
2943 * --------------------------------------------------------------
2944 * prepare for solving
2947 /* free unneeded memory */
2948 map_free(&addedmap);
2949 map_free(&installcandidatemap);
2952 POOL_DEBUG(SAT_DEBUG_STATS, "%d rpm rules, %d job rules, %d infarch rules, %d dup rules, %d choice rules\n", solv->rpmrules_end - 1, solv->jobrules_end - solv->jobrules, solv->infarchrules_end - solv->infarchrules, solv->duprules_end - solv->duprules, solv->choicerules_end - solv->choicerules);
2954 /* create weak map */
2955 map_init(&solv->weakrulemap, solv->nrules);
2956 for (i = 0; i < solv->weakruleq.count; i++)
2958 p = solv->weakruleq.elements[i];
2959 MAPSET(&solv->weakrulemap, p);
2962 /* all new rules are learnt after this point */
2963 solv->learntrules = solv->nrules;
2965 /* create watches chains */
2968 /* create assertion index. it is only used to speed up
2969 * makeruledecsions() a bit */
2970 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
2971 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
2972 queue_push(&solv->ruleassertions, i);
2974 /* disable update rules that conflict with our job */
2975 solver_disablepolicyrules(solv);
2977 /* make initial decisions based on assertion rules */
2978 makeruledecisions(solv);
2979 POOL_DEBUG(SAT_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
2982 * ********************************************
2984 * ********************************************
2987 now = sat_timems(0);
2988 solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
2989 POOL_DEBUG(SAT_DEBUG_STATS, "solver took %d ms\n", sat_timems(now));
2992 * calculate recommended/suggested packages
2994 findrecommendedsuggested(solv);
2997 * prepare solution queue if there were problems
2999 solver_prepare_solutions(solv);
3002 * finally prepare transaction info
3004 transaction_calculate(&solv->trans, &solv->decisionq, &solv->noobsoletes);
3006 POOL_DEBUG(SAT_DEBUG_STATS, "final solver statistics: %d problems, %d learned rules, %d unsolvable\n", solv->problems.count / 2, solv->stats_learned, solv->stats_unsolvable);
3007 POOL_DEBUG(SAT_DEBUG_STATS, "solver_solve took %d ms\n", sat_timems(solve_start));
3010 /***********************************************************************/
3011 /* disk usage computations */
3013 /*-------------------------------------------------------------------
3015 * calculate DU changes
3019 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
3023 solver_create_state_maps(solv, &installedmap, 0);
3024 pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
3025 map_free(&installedmap);
3029 /*-------------------------------------------------------------------
3031 * calculate changes in install size
3035 solver_calc_installsizechange(Solver *solv)
3040 solver_create_state_maps(solv, &installedmap, 0);
3041 change = pool_calc_installsizechange(solv->pool, &installedmap);
3042 map_free(&installedmap);
3047 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
3050 solver_create_state_maps(solv, &installedmap, 0);
3051 pool_trivial_installable_noobsoletesmap(solv->pool, &installedmap, pkgs, res, solv->noobsoletes.size ? &solv->noobsoletes : 0);
3052 map_free(&installedmap);