182a0e84b21a942588850168578b073eb71ce3f0
[platform/upstream/libsolv.git] / src / solver.c
1 /*
2  * Copyright (c) 2007-2008, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * solver.c
10  *
11  * SAT based dependency solver
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <assert.h>
19
20 #include "solver.h"
21 #include "solver_private.h"
22 #include "bitmap.h"
23 #include "pool.h"
24 #include "util.h"
25 #include "policy.h"
26 #include "poolarch.h"
27 #include "solverdebug.h"
28
29 #define RULES_BLOCK 63
30
31 /********************************************************************
32  *
33  * dependency check helpers
34  *
35  */
36
37 /*-------------------------------------------------------------------
38  * handle split provides
39  *
40  * a splitprovides dep looks like
41  *     namespace:splitprovides(pkg REL_WITH path)
42  * and is only true if pkg is installed and contains the specified path.
43  * we also make sure that pkg is selected for an update, otherwise the
44  * update would always be forced onto the user.
45  */
46 int
47 solver_splitprovides(Solver *solv, Id dep)
48 {
49   Pool *pool = solv->pool;
50   Id p, pp;
51   Reldep *rd;
52   Solvable *s;
53
54   if (!solv->dosplitprovides || !solv->installed || (!solv->updatemap_all && !solv->updatemap.size))
55     return 0;
56   if (!ISRELDEP(dep))
57     return 0;
58   rd = GETRELDEP(pool, dep);
59   if (rd->flags != REL_WITH)
60     return 0;
61   /*
62    * things are a bit tricky here if pool->addedprovides == 1, because most split-provides are in
63    * a non-standard location. If we simply call pool_whatprovides, we'll drag in the complete
64    * file list. Instead we rely on pool_addfileprovides ignoring the addfileprovidesfiltered flag
65    * for installed packages and check the lazywhatprovidesq (ignoring the REL_WITH part, but
66    * we filter the package name further down anyway).
67    */
68   if (pool->addedfileprovides == 1 && !ISRELDEP(rd->evr) && !pool->whatprovides[rd->evr])
69     pp = pool_searchlazywhatprovidesq(pool, rd->evr);
70   else
71     pp = pool_whatprovides(pool, dep);
72   while ((p = pool->whatprovidesdata[pp++]) != 0)
73     {
74       /* here we have packages that provide the correct name and contain the path,
75        * now do extra filtering */
76       s = pool->solvables + p;
77       if (s->repo == solv->installed && s->name == rd->name &&
78           (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, p - solv->installed->start))))
79         return 1;
80     }
81   return 0;
82 }
83
84
85 /*-------------------------------------------------------------------
86  * solver_dep_installed
87  */
88
89 int
90 solver_dep_installed(Solver *solv, Id dep)
91 {
92 #if 0
93   Pool *pool = solv->pool;
94   Id p, pp;
95
96   if (ISRELDEP(dep))
97     {
98       Reldep *rd = GETRELDEP(pool, dep);
99       if (rd->flags == REL_AND)
100         {
101           if (!solver_dep_installed(solv, rd->name))
102             return 0;
103           return solver_dep_installed(solv, rd->evr);
104         }
105       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
106         return solver_dep_installed(solv, rd->evr);
107     }
108   FOR_PROVIDES(p, pp, dep)
109     {
110       if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
111         return 1;
112     }
113 #endif
114   return 0;
115 }
116
117 /* mirrors solver_dep_installed, but returns 2 if a
118  * dependency listed in solv->installsuppdepq was involved */
119 static int
120 solver_check_installsuppdepq_dep(Solver *solv, Id dep)
121 {
122   Pool *pool = solv->pool;
123   Id p, pp;
124   Queue *q;
125
126   if (ISRELDEP(dep))
127     {
128       Reldep *rd = GETRELDEP(pool, dep);
129       if (rd->flags == REL_AND)
130         {
131           int r2, r1 = solver_check_installsuppdepq_dep(solv, rd->name);
132           if (!r1)
133             return 0;
134           r2 = solver_check_installsuppdepq_dep(solv, rd->evr);
135           if (!r2)
136             return 0;
137           return r1 == 2 || r2 == 2 ? 2 : 1;
138         }
139       if (rd->flags == REL_OR)
140         {
141           int r2, r1 = solver_check_installsuppdepq_dep(solv, rd->name);
142           r2 = solver_check_installsuppdepq_dep(solv, rd->evr);
143           if (!r1 && !r2)
144             return 0;
145           return r1 == 2 || r2 == 2 ? 2 : 1;
146         }
147       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
148         return solver_splitprovides(solv, rd->evr);
149       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
150         return solver_dep_installed(solv, rd->evr);
151       if (rd->flags == REL_NAMESPACE && (q = solv->installsuppdepq) != 0)
152         {
153           int i;
154           for (i = 0; i < q->count; i++)
155             if (q->elements[i] == dep || q->elements[i] == rd->name)
156               return 2;
157         }
158     }
159   FOR_PROVIDES(p, pp, dep)
160     if (solv->decisionmap[p] > 0)
161       return 1;
162   return 0;
163 }
164
165 static int
166 solver_check_installsuppdepq(Solver *solv, Solvable *s)
167 {
168   Id sup, *supp;
169   supp = s->repo->idarraydata + s->supplements;
170   while ((sup = *supp++) != 0)
171     if (solver_check_installsuppdepq_dep(solv, sup) == 2)
172       return 1;
173   return 0;
174 }
175
176 static Id
177 autouninstall(Solver *solv, Id *problem)
178 {
179   Pool *pool = solv->pool;
180   int i;
181   int lastfeature = 0, lastupdate = 0;
182   Id v;
183   Id extraflags = -1;
184
185   for (i = 0; (v = problem[i]) != 0; i++)
186     {
187       if (v < 0)
188         extraflags &= solv->job.elements[-v - 1];
189       if (v >= solv->featurerules && v < solv->featurerules_end)
190         if (v > lastfeature)
191           lastfeature = v;
192       if (v >= solv->updaterules && v < solv->updaterules_end)
193         {
194           /* check if identical to feature rule */
195           Id p = solv->rules[v].p;
196           Rule *r;
197           if (p <= 0)
198             continue;
199           r = solv->rules + solv->featurerules + (p - solv->installed->start);
200           if (!r->p)
201             {
202               /* update rule == feature rule */
203               if (v > lastfeature)
204                 lastfeature = v;
205               continue;
206             }
207           if (v > lastupdate)
208             lastupdate = v;
209         }
210     }
211   if (!lastupdate && !lastfeature)
212     return 0;
213   v = lastupdate ? lastupdate : lastfeature;
214   POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "allowuninstall disabling ");
215   solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + v);
216   solver_disableproblem(solv, v);
217   if (extraflags != -1 && (extraflags & SOLVER_CLEANDEPS) != 0 && solv->cleandepsmap.size)
218     {
219       /* add the package to the updatepkgs list, this will automatically turn
220        * on cleandeps mode */
221       Id p = solv->rules[v].p;
222       if (!solv->cleandeps_updatepkgs)
223         {
224           solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
225           queue_init(solv->cleandeps_updatepkgs);
226         }
227       if (p > 0)
228         {
229           int oldupdatepkgscnt = solv->cleandeps_updatepkgs->count;
230           queue_pushunique(solv->cleandeps_updatepkgs, p);
231           if (solv->cleandeps_updatepkgs->count != oldupdatepkgscnt)
232             solver_disablepolicyrules(solv);
233         }
234     }
235   return v;
236 }
237
238 /************************************************************************/
239
240 /*
241  * enable/disable learnt rules 
242  *
243  * we have enabled or disabled some of our rules. We now reenable all
244  * of our learnt rules except the ones that were learnt from rules that
245  * are now disabled.
246  */
247 static void
248 enabledisablelearntrules(Solver *solv)
249 {
250   Pool *pool = solv->pool;
251   Rule *r;
252   Id why, *whyp;
253   int i;
254
255   POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
256   for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
257     {
258       whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
259       while ((why = *whyp++) != 0)
260         {
261           assert(why > 0 && why < i);
262           if (solv->rules[why].d < 0)
263             break;
264         }
265       /* why != 0: we found a disabled rule, disable the learnt rule */
266       if (why && r->d >= 0)
267         {
268           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
269             {
270               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "disabling ");
271               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
272             }
273           solver_disablerule(solv, r);
274         }
275       else if (!why && r->d < 0)
276         {
277           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
278             {
279               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "re-enabling ");
280               solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
281             }
282           solver_enablerule(solv, r);
283         }
284     }
285 }
286
287
288 /*
289  * make assertion rules into decisions
290  * 
291  * Go through rules and add direct assertions to the decisionqueue.
292  * If we find a conflict, disable rules and add them to problem queue.
293  */
294
295 static void
296 makeruledecisions(Solver *solv)
297 {
298   Pool *pool = solv->pool;
299   int i, ri, ii;
300   Rule *r, *rr;
301   Id v, vv;
302   int decisionstart;
303   int record_proof = 1;
304   int oldproblemcount;
305   int havedisabled = 0;
306
307   /* The system solvable is always installed first */
308   assert(solv->decisionq.count == 0);
309   queue_push(&solv->decisionq, SYSTEMSOLVABLE);
310   queue_push(&solv->decisionq_why, 0);
311   solv->decisionmap[SYSTEMSOLVABLE] = 1;        /* installed at level '1' */
312
313   decisionstart = solv->decisionq.count;
314   for (;;)
315     {
316       /* if we needed to re-run, back up decisions to decisionstart */
317       while (solv->decisionq.count > decisionstart)
318         {
319           v = solv->decisionq.elements[--solv->decisionq.count];
320           --solv->decisionq_why.count;
321           vv = v > 0 ? v : -v;
322           solv->decisionmap[vv] = 0;
323         }
324
325       /* note that the ruleassertions queue is ordered */
326       for (ii = 0; ii < solv->ruleassertions.count; ii++)
327         {
328           ri = solv->ruleassertions.elements[ii];
329           r = solv->rules + ri;
330             
331           if (havedisabled && ri >= solv->learntrules)
332             {
333               /* just started with learnt rule assertions. If we have disabled
334                * some rules, adapt the learnt rule status */
335               enabledisablelearntrules(solv);
336               havedisabled = 0;
337             }
338             
339           if (r->d < 0 || !r->p || r->w2)       /* disabled, dummy or no assertion */
340             continue;
341
342           /* do weak rules in phase 2 */
343           if (ri < solv->learntrules && MAPTST(&solv->weakrulemap, ri))
344             continue;
345
346           v = r->p;
347           vv = v > 0 ? v : -v;
348             
349           if (!solv->decisionmap[vv])          /* if not yet decided */
350             {
351               queue_push(&solv->decisionq, v);
352               queue_push(&solv->decisionq_why, r - solv->rules);
353               solv->decisionmap[vv] = v > 0 ? 1 : -1;
354               IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
355                 {
356                   Solvable *s = solv->pool->solvables + vv;
357                   if (v < 0)
358                     POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", pool_solvable2str(solv->pool, s));
359                   else
360                     POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing  %s (assertion)\n", pool_solvable2str(solv->pool, s));
361                 }
362               continue;
363             }
364
365           /* check against previous decision: is there a conflict? */
366           if (v > 0 && solv->decisionmap[vv] > 0)    /* ok to install */
367             continue;
368           if (v < 0 && solv->decisionmap[vv] < 0)    /* ok to remove */
369             continue;
370             
371           /*
372            * found a conflict!
373            * 
374            * The rule (r) we're currently processing says something
375            * different (v = r->p) than a previous decision (decisionmap[abs(v)])
376            * on this literal
377            */
378             
379           if (ri >= solv->learntrules)
380             {
381               /* conflict with a learnt rule */
382               /* can happen when packages cannot be installed for multiple reasons. */
383               /* we disable the learnt rule in this case */
384               /* (XXX: we should really call analyze_unsolvable_rule here!) */
385               solver_disablerule(solv, r);
386               continue;
387             }
388             
389           /*
390            * find the decision which is the "opposite" of the rule
391            */
392           for (i = 0; i < solv->decisionq.count; i++)
393             if (solv->decisionq.elements[i] == -v)
394               break;
395           assert(i < solv->decisionq.count);         /* assert that we found it */
396           oldproblemcount = solv->problems.count;
397             
398           /*
399            * conflict with system solvable ?
400            */
401           if (v == -SYSTEMSOLVABLE)
402             {
403               if (record_proof)
404                 {
405                   queue_push(&solv->problems, solv->learnt_pool.count);
406                   queue_push(&solv->learnt_pool, ri);
407                   queue_push(&solv->learnt_pool, 0);
408                 }
409               else
410                 queue_push(&solv->problems, 0);
411               POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
412               if  (ri >= solv->jobrules && ri < solv->jobrules_end)
413                 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
414               else
415                 v = ri;
416               queue_push(&solv->problems, v);
417               queue_push(&solv->problems, 0);
418               if (solv->allowuninstall && v >= solv->featurerules && v < solv->updaterules_end)
419                 solv->problems.count = oldproblemcount;
420               solver_disableproblem(solv, v);
421               havedisabled = 1;
422               break;    /* start over */
423             }
424
425           assert(solv->decisionq_why.elements[i] > 0);
426
427           /*
428            * conflict with an rpm rule ?
429            */
430           if (solv->decisionq_why.elements[i] < solv->rpmrules_end)
431             {
432               if (record_proof)
433                 {
434                   queue_push(&solv->problems, solv->learnt_pool.count);
435                   queue_push(&solv->learnt_pool, ri);
436                   queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
437                   queue_push(&solv->learnt_pool, 0);
438                 }
439               else
440                 queue_push(&solv->problems, 0);
441               assert(v > 0 || v == -SYSTEMSOLVABLE);
442               POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
443               if (ri >= solv->jobrules && ri < solv->jobrules_end)
444                 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
445               else
446                 v = ri;
447               queue_push(&solv->problems, v);
448               queue_push(&solv->problems, 0);
449               if (solv->allowuninstall && v >= solv->featurerules && v < solv->updaterules_end)
450                 solv->problems.count = oldproblemcount;
451               solver_disableproblem(solv, v);
452               havedisabled = 1;
453               break;    /* start over */
454             }
455
456           /*
457            * conflict with another job or update/feature rule
458            */
459             
460           /* record proof */
461           if (record_proof)
462             {
463               queue_push(&solv->problems, solv->learnt_pool.count);
464               queue_push(&solv->learnt_pool, ri);
465               queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
466               queue_push(&solv->learnt_pool, 0);
467             }
468           else
469             queue_push(&solv->problems, 0);
470
471           POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflicting update/job assertions over literal %d\n", vv);
472
473           /*
474            * push all of our rules (can only be feature or job rules)
475            * asserting this literal on the problem stack
476            */
477           for (i = solv->featurerules, rr = solv->rules + i; i < solv->learntrules; i++, rr++)
478             {
479               if (rr->d < 0                          /* disabled */
480                   || rr->w2)                         /*  or no assertion */
481                 continue;
482               if (rr->p != vv                        /* not affecting the literal */
483                   && rr->p != -vv)
484                 continue;
485               if (MAPTST(&solv->weakrulemap, i))     /* weak: silently ignore */
486                 continue;
487                 
488               POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
489               solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + i);
490                 
491               v = i;
492               if (i >= solv->jobrules && i < solv->jobrules_end)
493                 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
494               queue_push(&solv->problems, v);
495             }
496           queue_push(&solv->problems, 0);
497
498           if (solv->allowuninstall && (v = autouninstall(solv, solv->problems.elements + oldproblemcount + 1)) != 0)
499             solv->problems.count = oldproblemcount;
500
501           for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
502             solver_disableproblem(solv, solv->problems.elements[i]);
503           havedisabled = 1;
504           break;        /* start over */
505         }
506       if (ii < solv->ruleassertions.count)
507         continue;
508
509       /*
510        * phase 2: now do the weak assertions
511        */
512       for (ii = 0; ii < solv->ruleassertions.count; ii++)
513         {
514           ri = solv->ruleassertions.elements[ii];
515           r = solv->rules + ri;
516           if (r->d < 0 || r->w2)                         /* disabled or no assertion */
517             continue;
518           if (ri >= solv->learntrules || !MAPTST(&solv->weakrulemap, ri))       /* skip non-weak */
519             continue;
520           v = r->p;
521           vv = v > 0 ? v : -v;
522
523           if (!solv->decisionmap[vv])          /* if not yet decided */
524             {
525               queue_push(&solv->decisionq, v);
526               queue_push(&solv->decisionq_why, r - solv->rules);
527               solv->decisionmap[vv] = v > 0 ? 1 : -1;
528               IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
529                 {
530                   Solvable *s = solv->pool->solvables + vv;
531                   if (v < 0)
532                     POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
533                   else
534                     POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing  %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
535                 }
536               continue;
537             }
538           /* check against previous decision: is there a conflict? */
539           if (v > 0 && solv->decisionmap[vv] > 0)
540             continue;
541           if (v < 0 && solv->decisionmap[vv] < 0)
542             continue;
543             
544           POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
545           solver_printrule(solv, SOLV_DEBUG_UNSOLVABLE, r);
546
547           if (ri >= solv->jobrules && ri < solv->jobrules_end)
548             v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
549           else
550             v = ri;
551           solver_disableproblem(solv, v);
552           if (v < 0)
553             solver_reenablepolicyrules(solv, -v);
554           havedisabled = 1;
555           break;        /* start over */
556         }
557       if (ii == solv->ruleassertions.count)
558         break;  /* finished! */
559     }
560 }
561
562
563 /********************************************************************/
564 /* watches */
565
566
567 /*-------------------------------------------------------------------
568  * makewatches
569  *
570  * initial setup for all watches
571  */
572
573 static void
574 makewatches(Solver *solv)
575 {
576   Rule *r;
577   int i;
578   int nsolvables = solv->pool->nsolvables;
579
580   solv_free(solv->watches);
581                                        /* lower half for removals, upper half for installs */
582   solv->watches = solv_calloc(2 * nsolvables, sizeof(Id));
583 #if 1
584   /* do it reverse so rpm rules get triggered first (XXX: obsolete?) */
585   for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
586 #else
587   for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
588 #endif
589     {
590       if (!r->w2)               /* assertions do not need watches */
591         continue;
592
593       /* see addwatches_rule(solv, r) */
594       r->n1 = solv->watches[nsolvables + r->w1];
595       solv->watches[nsolvables + r->w1] = r - solv->rules;
596
597       r->n2 = solv->watches[nsolvables + r->w2];
598       solv->watches[nsolvables + r->w2] = r - solv->rules;
599     }
600 }
601
602
603 /*-------------------------------------------------------------------
604  *
605  * add watches (for a new learned rule)
606  * sets up watches for a single rule
607  * 
608  * see also makewatches() above.
609  */
610
611 static inline void
612 addwatches_rule(Solver *solv, Rule *r)
613 {
614   int nsolvables = solv->pool->nsolvables;
615
616   r->n1 = solv->watches[nsolvables + r->w1];
617   solv->watches[nsolvables + r->w1] = r - solv->rules;
618
619   r->n2 = solv->watches[nsolvables + r->w2];
620   solv->watches[nsolvables + r->w2] = r - solv->rules;
621 }
622
623
624 /********************************************************************/
625 /*
626  * rule propagation
627  */
628
629
630 /* shortcuts to check if a literal (positive or negative) assignment
631  * evaluates to 'true' or 'false'
632  */
633 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
634 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
635 #define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
636
637 /*-------------------------------------------------------------------
638  * 
639  * propagate
640  *
641  * make decision and propagate to all rules
642  * 
643  * Evaluate each term affected by the decision (linked through watches).
644  * If we find unit rules we make new decisions based on them.
645  * 
646  * return : 0 = everything is OK
647  *          rule = conflict found in this rule
648  */
649
650 static Rule *
651 propagate(Solver *solv, int level)
652 {
653   Pool *pool = solv->pool;
654   Id *rp, *next_rp;           /* rule pointer, next rule pointer in linked list */
655   Rule *r;                    /* rule */
656   Id p, pkg, other_watch;
657   Id *dp;
658   Id *decisionmap = solv->decisionmap;
659     
660   Id *watches = solv->watches + pool->nsolvables;   /* place ptr in middle */
661
662   POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate -----\n");
663
664   /* foreach non-propagated decision */
665   while (solv->propagate_index < solv->decisionq.count)
666     {
667         /*
668          * 'pkg' was just decided
669          * negate because our watches trigger if literal goes FALSE
670          */
671       pkg = -solv->decisionq.elements[solv->propagate_index++];
672         
673       IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
674         {
675           POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
676           solver_printruleelement(solv, SOLV_DEBUG_PROPAGATE, 0, -pkg);
677         }
678
679       /* foreach rule where 'pkg' is now FALSE */
680       for (rp = watches + pkg; *rp; rp = next_rp)
681         {
682           r = solv->rules + *rp;
683           if (r->d < 0)
684             {
685               /* rule is disabled, goto next */
686               if (pkg == r->w1)
687                 next_rp = &r->n1;
688               else
689                 next_rp = &r->n2;
690               continue;
691             }
692
693           IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
694             {
695               POOL_DEBUG(SOLV_DEBUG_PROPAGATE,"  watch triggered ");
696               solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
697             }
698
699             /* 'pkg' was just decided (was set to FALSE)
700              * 
701              *  now find other literal watch, check clause
702              *   and advance on linked list
703              */
704           if (pkg == r->w1)
705             {
706               other_watch = r->w2;
707               next_rp = &r->n1;
708             }
709           else
710             {
711               other_watch = r->w1;
712               next_rp = &r->n2;
713             }
714             
715             /* 
716              * This term is already true (through the other literal)
717              * so we have nothing to do
718              */
719           if (DECISIONMAP_TRUE(other_watch))
720             continue;
721
722             /*
723              * The other literal is FALSE or UNDEF
724              * 
725              */
726             
727           if (r->d)
728             {
729               /* Not a binary clause, try to move our watch.
730                * 
731                * Go over all literals and find one that is
732                *   not other_watch
733                *   and not FALSE
734                * 
735                * (TRUE is also ok, in that case the rule is fulfilled)
736                */
737               if (r->p                                /* we have a 'p' */
738                   && r->p != other_watch              /* which is not watched */
739                   && !DECISIONMAP_FALSE(r->p))        /* and not FALSE */
740                 {
741                   p = r->p;
742                 }
743               else                                    /* go find a 'd' to make 'true' */
744                 {
745                   /* foreach p in 'd'
746                      we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
747                    */
748                   for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
749                     {
750                       if (p != other_watch              /* which is not watched */
751                           && !DECISIONMAP_FALSE(p))     /* and not FALSE */
752                         break;
753                     }
754                 }
755
756               if (p)
757                 {
758                   /*
759                    * if we found some p that is UNDEF or TRUE, move
760                    * watch to it
761                    */
762                   IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
763                     {
764                       if (p > 0)
765                         POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "    -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, p));
766                       else
767                         POOL_DEBUG(SOLV_DEBUG_PROPAGATE,"    -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, -p));
768                     }
769                     
770                   *rp = *next_rp;
771                   next_rp = rp;
772                     
773                   if (pkg == r->w1)
774                     {
775                       r->w1 = p;
776                       r->n1 = watches[p];
777                     }
778                   else
779                     {
780                       r->w2 = p;
781                       r->n2 = watches[p];
782                     }
783                   watches[p] = r - solv->rules;
784                   continue;
785                 }
786               /* search failed, thus all unwatched literals are FALSE */
787                 
788             } /* not binary */
789             
790           /*
791            * unit clause found, set literal other_watch to TRUE
792            */
793
794           if (DECISIONMAP_FALSE(other_watch))      /* check if literal is FALSE */
795             return r;                              /* eek, a conflict! */
796             
797           IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
798             {
799               POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "  unit ");
800               solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
801             }
802
803           if (other_watch > 0)
804             decisionmap[other_watch] = level;    /* install! */
805           else
806             decisionmap[-other_watch] = -level;  /* remove! */
807             
808           queue_push(&solv->decisionq, other_watch);
809           queue_push(&solv->decisionq_why, r - solv->rules);
810
811           IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
812             {
813               if (other_watch > 0)
814                 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "    -> decided to install %s\n", pool_solvid2str(pool, other_watch));
815               else
816                 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "    -> decided to conflict %s\n", pool_solvid2str(pool, -other_watch));
817             }
818             
819         } /* foreach rule involving 'pkg' */
820         
821     } /* while we have non-decided decisions */
822     
823   POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate end-----\n");
824
825   return 0;     /* all is well */
826 }
827
828
829 /********************************************************************/
830 /* Analysis */
831
832 /*-------------------------------------------------------------------
833  * 
834  * analyze
835  *   and learn
836  */
837
838 static int
839 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
840 {
841   Pool *pool = solv->pool;
842   Queue r;
843   Id r_buf[4];
844   int rlevel = 1;
845   Map seen;             /* global? */
846   Id d, v, vv, *dp, why;
847   int l, i, idx;
848   int num = 0, l1num = 0;
849   int learnt_why = solv->learnt_pool.count;
850   Id *decisionmap = solv->decisionmap;
851
852   queue_init_buffer(&r, r_buf, sizeof(r_buf)/sizeof(*r_buf));
853
854   POOL_DEBUG(SOLV_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
855   map_init(&seen, pool->nsolvables);
856   idx = solv->decisionq.count;
857   for (;;)
858     {
859       IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
860         solver_printruleclass(solv, SOLV_DEBUG_ANALYZE, c);
861       queue_push(&solv->learnt_pool, c - solv->rules);
862       d = c->d < 0 ? -c->d - 1 : c->d;
863       dp = d ? pool->whatprovidesdata + d : 0;
864       /* go through all literals of the rule */
865       for (i = -1; ; i++)
866         {
867           if (i == -1)
868             v = c->p;
869           else if (d == 0)
870             v = i ? 0 : c->w2;
871           else
872             v = *dp++;
873           if (v == 0)
874             break;
875
876           if (DECISIONMAP_TRUE(v))      /* the one true literal */
877             continue;
878           vv = v > 0 ? v : -v;
879           if (MAPTST(&seen, vv))
880             continue;
881           l = solv->decisionmap[vv];
882           if (l < 0)
883             l = -l;
884           MAPSET(&seen, vv);            /* mark that we also need to look at this literal */
885           if (l == 1)
886             l1num++;                    /* need to do this one in level1 pass */
887           else if (l == level)
888             num++;                      /* need to do this one as well */
889           else
890             {
891               queue_push(&r, v);        /* not level1 or conflict level, add to new rule */
892               if (l > rlevel)
893                 rlevel = l;
894             }
895         }
896 l1retry:
897       if (!num && !--l1num)
898         break;  /* all level 1 literals done */
899
900       /* find the next literal to investigate */
901       /* (as num + l1num > 0, we know that we'll always find one) */
902       for (;;)
903         {
904           assert(idx > 0);
905           v = solv->decisionq.elements[--idx];
906           vv = v > 0 ? v : -v;
907           if (MAPTST(&seen, vv))
908             break;
909         }
910       MAPCLR(&seen, vv);
911
912       if (num && --num == 0)
913         {
914           *pr = -v;     /* so that v doesn't get lost */
915           if (!l1num)
916             break;
917           POOL_DEBUG(SOLV_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
918           /* clear non-l1 bits from seen map */
919           for (i = 0; i < r.count; i++)
920             {
921               v = r.elements[i];
922               MAPCLR(&seen, v > 0 ? v : -v);
923             }
924           /* only level 1 marks left in seen map */
925           l1num++;      /* as l1retry decrements it */
926           goto l1retry;
927         }
928
929       why = solv->decisionq_why.elements[idx];
930       if (why <= 0)     /* just in case, maybe for SYSTEMSOLVABLE */
931         goto l1retry;
932       c = solv->rules + why;
933     }
934   map_free(&seen);
935
936   if (r.count == 0)
937     *dr = 0;
938   else if (r.count == 1 && r.elements[0] < 0)
939     *dr = r.elements[0];
940   else
941     *dr = pool_queuetowhatprovides(pool, &r);
942   IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
943     {
944       POOL_DEBUG(SOLV_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
945       solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, *pr);
946       for (i = 0; i < r.count; i++)
947         solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, r.elements[i]);
948     }
949   /* push end marker on learnt reasons stack */
950   queue_push(&solv->learnt_pool, 0);
951   if (whyp)
952     *whyp = learnt_why;
953   queue_free(&r);
954   solv->stats_learned++;
955   return rlevel;
956 }
957
958
959 /*-------------------------------------------------------------------
960  * 
961  * solver_reset
962  * 
963  * reset all solver decisions
964  * called after rules have been enabled/disabled
965  */
966
967 void
968 solver_reset(Solver *solv)
969 {
970   Pool *pool = solv->pool;
971   int i;
972   Id v;
973
974   /* rewind all decisions */
975   for (i = solv->decisionq.count - 1; i >= 0; i--)
976     {
977       v = solv->decisionq.elements[i];
978       solv->decisionmap[v > 0 ? v : -v] = 0;
979     }
980   queue_empty(&solv->decisionq_why);
981   queue_empty(&solv->decisionq);
982   solv->recommends_index = -1;
983   solv->propagate_index = 0;
984   solv->decisioncnt_update = solv->decisioncnt_keep = solv->decisioncnt_resolve = solv->decisioncnt_weak = solv->decisioncnt_orphan = 0;
985   queue_empty(&solv->branches);
986
987   /* adapt learnt rule status to new set of enabled/disabled rules */
988   enabledisablelearntrules(solv);
989
990   /* redo all assertion rule decisions */
991   makeruledecisions(solv);
992   POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
993 }
994
995
996 /*-------------------------------------------------------------------
997  * 
998  * analyze_unsolvable_rule
999  *
1000  * recursion helper used by analyze_unsolvable
1001  */
1002
1003 static void
1004 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp, Map *rseen)
1005 {
1006   Pool *pool = solv->pool;
1007   int i;
1008   Id why = r - solv->rules;
1009
1010   IF_POOLDEBUG (SOLV_DEBUG_UNSOLVABLE)
1011     solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, r);
1012   if (solv->learntrules && why >= solv->learntrules)
1013     {
1014       if (MAPTST(rseen, why - solv->learntrules))
1015         return;
1016       MAPSET(rseen, why - solv->learntrules);
1017       for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1018         if (solv->learnt_pool.elements[i] > 0)
1019           analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp, rseen);
1020       return;
1021     }
1022   if (MAPTST(&solv->weakrulemap, why))
1023     if (!*lastweakp || why > *lastweakp)
1024       *lastweakp = why;
1025   /* do not add rpm rules to problem */
1026   if (why < solv->rpmrules_end)
1027     return;
1028   /* turn rule into problem */
1029   if (why >= solv->jobrules && why < solv->jobrules_end)
1030     why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1031   /* normalize dup/infarch rules */
1032   if (why > solv->infarchrules && why < solv->infarchrules_end)
1033     {
1034       Id name = pool->solvables[-solv->rules[why].p].name;
1035       while (why > solv->infarchrules && pool->solvables[-solv->rules[why - 1].p].name == name)
1036         why--;
1037     }
1038   if (why > solv->duprules && why < solv->duprules_end)
1039     {
1040       Id name = pool->solvables[-solv->rules[why].p].name;
1041       while (why > solv->duprules && pool->solvables[-solv->rules[why - 1].p].name == name)
1042         why--;
1043     }
1044
1045   /* return if problem already countains our rule */
1046   if (solv->problems.count)
1047     {
1048       for (i = solv->problems.count - 1; i >= 0; i--)
1049         if (solv->problems.elements[i] == 0)    /* end of last problem reached? */
1050           break;
1051         else if (solv->problems.elements[i] == why)
1052           return;
1053     }
1054   queue_push(&solv->problems, why);
1055 }
1056
1057
1058 /*-------------------------------------------------------------------
1059  * 
1060  * analyze_unsolvable
1061  *
1062  * We know that the problem is not solvable. Record all involved
1063  * rules (i.e. the "proof") into solv->learnt_pool.
1064  * Record the learnt pool index and all non-rpm rules into
1065  * solv->problems. (Our solutions to fix the problems are to
1066  * disable those rules.)
1067  *
1068  * If the proof contains at least one weak rule, we disable the
1069  * last of them.
1070  *
1071  * Otherwise we return 0 if disablerules is not set or disable
1072  * _all_ of the problem rules and return 1.
1073  *
1074  * return: 1 - disabled some rules, try again
1075  *         0 - hopeless
1076  */
1077
1078 static int
1079 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1080 {
1081   Pool *pool = solv->pool;
1082   Rule *r;
1083   Map seen;             /* global to speed things up? */
1084   Map rseen;
1085   Id d, v, vv, *dp, why;
1086   int l, i, idx;
1087   Id *decisionmap = solv->decisionmap;
1088   int oldproblemcount;
1089   int oldlearntpoolcount;
1090   Id lastweak;
1091   int record_proof = 1;
1092
1093   POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
1094   solv->stats_unsolvable++;
1095   oldproblemcount = solv->problems.count;
1096   oldlearntpoolcount = solv->learnt_pool.count;
1097
1098   /* make room for proof index */
1099   /* must update it later, as analyze_unsolvable_rule would confuse
1100    * it with a rule index if we put the real value in already */
1101   queue_push(&solv->problems, 0);
1102
1103   r = cr;
1104   map_init(&seen, pool->nsolvables);
1105   map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
1106   if (record_proof)
1107     queue_push(&solv->learnt_pool, r - solv->rules);
1108   lastweak = 0;
1109   analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
1110   d = r->d < 0 ? -r->d - 1 : r->d;
1111   dp = d ? pool->whatprovidesdata + d : 0;
1112   for (i = -1; ; i++)
1113     {
1114       if (i == -1)
1115         v = r->p;
1116       else if (d == 0)
1117         v = i ? 0 : r->w2;
1118       else
1119         v = *dp++;
1120       if (v == 0)
1121         break;
1122       if (DECISIONMAP_TRUE(v))  /* the one true literal */
1123           continue;
1124       vv = v > 0 ? v : -v;
1125       l = solv->decisionmap[vv];
1126       if (l < 0)
1127         l = -l;
1128       MAPSET(&seen, vv);
1129     }
1130   idx = solv->decisionq.count;
1131   while (idx > 0)
1132     {
1133       v = solv->decisionq.elements[--idx];
1134       vv = v > 0 ? v : -v;
1135       if (!MAPTST(&seen, vv))
1136         continue;
1137       why = solv->decisionq_why.elements[idx];
1138       assert(why > 0);
1139       if (record_proof)
1140         queue_push(&solv->learnt_pool, why);
1141       r = solv->rules + why;
1142       analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
1143       d = r->d < 0 ? -r->d - 1 : r->d;
1144       dp = d ? pool->whatprovidesdata + d : 0;
1145       for (i = -1; ; i++)
1146         {
1147           if (i == -1)
1148             v = r->p;
1149           else if (d == 0)
1150             v = i ? 0 : r->w2;
1151           else
1152             v = *dp++;
1153           if (v == 0)
1154             break;
1155           if (DECISIONMAP_TRUE(v))      /* the one true literal */
1156               continue;
1157           vv = v > 0 ? v : -v;
1158           l = solv->decisionmap[vv];
1159           if (l < 0)
1160             l = -l;
1161           MAPSET(&seen, vv);
1162         }
1163     }
1164   map_free(&seen);
1165   map_free(&rseen);
1166   queue_push(&solv->problems, 0);       /* mark end of this problem */
1167
1168   if (lastweak)
1169     {
1170       /* disable last weak rule */
1171       solv->problems.count = oldproblemcount;
1172       solv->learnt_pool.count = oldlearntpoolcount;
1173       if (lastweak >= solv->jobrules && lastweak < solv->jobrules_end)
1174         v = -(solv->ruletojob.elements[lastweak - solv->jobrules] + 1);
1175       else
1176         v = lastweak;
1177       POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "disabling ");
1178       solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + lastweak);
1179       if (lastweak >= solv->choicerules && lastweak < solv->choicerules_end)
1180         solver_disablechoicerules(solv, solv->rules + lastweak);
1181       solver_disableproblem(solv, v);
1182       if (v < 0)
1183         solver_reenablepolicyrules(solv, -v);
1184       solver_reset(solv);
1185       return 1;
1186     }
1187
1188   if (solv->allowuninstall && (v = autouninstall(solv, solv->problems.elements + oldproblemcount + 1)) != 0)
1189     {
1190       solv->problems.count = oldproblemcount;
1191       solv->learnt_pool.count = oldlearntpoolcount;
1192       solver_reset(solv);
1193       return 1;
1194     }
1195
1196   /* finish proof */
1197   if (record_proof)
1198     {
1199       queue_push(&solv->learnt_pool, 0);
1200       solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1201     }
1202
1203   /* + 2: index + trailing zero */
1204   if (disablerules && oldproblemcount + 2 < solv->problems.count)
1205     {
1206       for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1207         solver_disableproblem(solv, solv->problems.elements[i]);
1208       /* XXX: might want to enable all weak rules again */
1209       solver_reset(solv);
1210       return 1;
1211     }
1212   POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1213   return 0;
1214 }
1215
1216
1217 /********************************************************************/
1218 /* Decision revert */
1219
1220 /*-------------------------------------------------------------------
1221  * 
1222  * revert
1223  * revert decisionq to a level
1224  */
1225
1226 static void
1227 revert(Solver *solv, int level)
1228 {
1229   Pool *pool = solv->pool;
1230   Id v, vv;
1231   while (solv->decisionq.count)
1232     {
1233       v = solv->decisionq.elements[solv->decisionq.count - 1];
1234       vv = v > 0 ? v : -v;
1235       if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1236         break;
1237       POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1238       solv->decisionmap[vv] = 0;
1239       solv->decisionq.count--;
1240       solv->decisionq_why.count--;
1241       solv->propagate_index = solv->decisionq.count;
1242     }
1243   while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1244     {
1245       solv->branches.count--;
1246       while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1247         solv->branches.count--;
1248     }
1249   solv->recommends_index = -1;
1250   if (solv->decisionq.count < solv->decisioncnt_update)
1251     solv->decisioncnt_update = 0; 
1252   if (solv->decisionq.count < solv->decisioncnt_keep)
1253     solv->decisioncnt_keep = 0; 
1254   if (solv->decisionq.count < solv->decisioncnt_resolve)
1255     solv->decisioncnt_resolve = 0; 
1256   if (solv->decisionq.count < solv->decisioncnt_weak)
1257     solv->decisioncnt_weak= 0;
1258   if (solv->decisionq.count < solv->decisioncnt_orphan)
1259     solv->decisioncnt_orphan = 0; 
1260 }
1261
1262
1263 /*-------------------------------------------------------------------
1264  * 
1265  * watch2onhighest - put watch2 on literal with highest level
1266  */
1267
1268 static inline void
1269 watch2onhighest(Solver *solv, Rule *r)
1270 {
1271   int l, wl = 0;
1272   Id d, v, *dp;
1273
1274   d = r->d < 0 ? -r->d - 1 : r->d;
1275   if (!d)
1276     return;     /* binary rule, both watches are set */
1277   dp = solv->pool->whatprovidesdata + d;
1278   while ((v = *dp++) != 0)
1279     {
1280       l = solv->decisionmap[v < 0 ? -v : v];
1281       if (l < 0)
1282         l = -l;
1283       if (l > wl)
1284         {
1285           r->w2 = dp[-1];
1286           wl = l;
1287         }
1288     }
1289 }
1290
1291
1292 /*-------------------------------------------------------------------
1293  * 
1294  * setpropagatelearn
1295  *
1296  * add free decision (solvable to install) to decisionq
1297  * increase level and propagate decision
1298  * return if no conflict.
1299  *
1300  * in conflict case, analyze conflict rule, add resulting
1301  * rule to learnt rule set, make decision from learnt
1302  * rule (always unit) and re-propagate.
1303  *
1304  * returns the new solver level or 0 if unsolvable
1305  *
1306  */
1307
1308 static int
1309 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules, Id ruleid)
1310 {
1311   Pool *pool = solv->pool;
1312   Rule *r;
1313   Id p = 0, d = 0;
1314   int l, why;
1315
1316   assert(ruleid >= 0);
1317   if (decision)
1318     {
1319       level++;
1320       if (decision > 0)
1321         solv->decisionmap[decision] = level;
1322       else
1323         solv->decisionmap[-decision] = -level;
1324       queue_push(&solv->decisionq, decision);
1325       queue_push(&solv->decisionq_why, -ruleid);        /* <= 0 -> free decision */
1326     }
1327   for (;;)
1328     {
1329       r = propagate(solv, level);
1330       if (!r)
1331         break;
1332       if (level == 1)
1333         return analyze_unsolvable(solv, r, disablerules);
1334       POOL_DEBUG(SOLV_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1335       l = analyze(solv, level, r, &p, &d, &why);        /* learnt rule in p and d */
1336       assert(l > 0 && l < level);
1337       POOL_DEBUG(SOLV_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1338       level = l;
1339       revert(solv, level);
1340       r = solver_addrule(solv, p, d);
1341       assert(r);
1342       assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
1343       queue_push(&solv->learnt_why, why);
1344       if (d)
1345         {
1346           /* at least 2 literals, needs watches */
1347           watch2onhighest(solv, r);
1348           addwatches_rule(solv, r);
1349         }
1350       else
1351         {
1352           /* learnt rule is an assertion */
1353           queue_push(&solv->ruleassertions, r - solv->rules);
1354         }
1355       /* the new rule is unit by design */
1356       solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1357       queue_push(&solv->decisionq, p);
1358       queue_push(&solv->decisionq_why, r - solv->rules);
1359       IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
1360         {
1361           POOL_DEBUG(SOLV_DEBUG_ANALYZE, "decision: ");
1362           solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, p);
1363           POOL_DEBUG(SOLV_DEBUG_ANALYZE, "new rule: ");
1364           solver_printrule(solv, SOLV_DEBUG_ANALYZE, r);
1365         }
1366     }
1367   return level;
1368 }
1369
1370 static void
1371 reorder_dq_for_jobrules(Solver *solv, int level, Queue *dq)
1372 {
1373   Pool *pool = solv->pool;
1374   int i, j, haveone = 0, dqcount = dq->count;
1375   Id p;
1376   Solvable *s;
1377
1378   /* at the time we process jobrules the installed packages are not kept yet */
1379   /* reorder so that "future-supplemented" packages come first */
1380   FOR_REPO_SOLVABLES(solv->installed, p, s)
1381     {
1382       if (MAPTST(&solv->noupdate, p - solv->installed->start))
1383         continue;
1384       if (solv->decisionmap[p] == 0)
1385         {
1386           solv->decisionmap[p] = level;
1387           haveone = 1;
1388         }
1389     }
1390   if (!haveone)
1391     return;
1392   for (i = 0; i < dqcount; i++)
1393     if (!solver_is_enhancing(solv, pool->solvables + dq->elements[i]))
1394       {
1395         queue_push(dq, dq->elements[i]);
1396         dq->elements[i] = 0;
1397       }
1398   dqcount = dq->count;
1399   for (i = 0; i < dqcount; i++)
1400     if (dq->elements[i] && !solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1401       {
1402         queue_push(dq, dq->elements[i]);
1403         dq->elements[i] = 0;
1404       }
1405   for (i = j = 0; i < dq->count; i++)
1406     if (dq->elements[i])
1407       dq->elements[j++] = dq->elements[i];
1408   queue_truncate(dq, j);
1409   FOR_REPO_SOLVABLES(solv->installed, p, s)
1410     if (solv->decisionmap[p] == level)
1411       solv->decisionmap[p] = 0;
1412 }
1413
1414 /*-------------------------------------------------------------------
1415  * 
1416  * select and install
1417  * 
1418  * install best package from the queue. We add an extra package, inst, if
1419  * provided. See comment in weak install section.
1420  *
1421  * returns the new solver level or 0 if unsolvable
1422  *
1423  */
1424
1425 static int
1426 selectandinstall(Solver *solv, int level, Queue *dq, int disablerules, Id ruleid)
1427 {
1428   Pool *pool = solv->pool;
1429   Id p;
1430   int i;
1431
1432   if (dq->count > 1)
1433     policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1434   if (dq->count > 1)
1435     {
1436       /* XXX: didn't we already do that? */
1437       /* XXX: shouldn't we prefer installed packages? */
1438       /* XXX: move to policy.c? */
1439       /* choose the supplemented one */
1440       for (i = 0; i < dq->count; i++)
1441         if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1442           {
1443             dq->elements[0] = dq->elements[i];
1444             dq->count = 1;
1445             break;
1446           }
1447     }
1448   if (dq->count > 1 && ruleid >= solv->jobrules && ruleid < solv->jobrules_end && solv->installed)
1449     reorder_dq_for_jobrules(solv, level, dq);
1450   if (dq->count > 1)
1451     {
1452       /* multiple candidates, open a branch */
1453       for (i = 1; i < dq->count; i++)
1454         queue_push(&solv->branches, dq->elements[i]);
1455       queue_push(&solv->branches, -level);
1456     }
1457   p = dq->elements[0];
1458
1459   POOL_DEBUG(SOLV_DEBUG_POLICY, "installing %s\n", pool_solvid2str(pool, p));
1460
1461   return setpropagatelearn(solv, level, p, disablerules, ruleid);
1462 }
1463
1464
1465 /********************************************************************/
1466 /* Main solver interface */
1467
1468
1469 /*-------------------------------------------------------------------
1470  * 
1471  * solver_create
1472  * create solver structure
1473  *
1474  * pool: all available solvables
1475  * installed: installed Solvables
1476  *
1477  *
1478  * Upon solving, rules are created to flag the Solvables
1479  * of the 'installed' Repo as installed.
1480  */
1481
1482 Solver *
1483 solver_create(Pool *pool)
1484 {
1485   Solver *solv;
1486   solv = (Solver *)solv_calloc(1, sizeof(Solver));
1487   solv->pool = pool;
1488   solv->installed = pool->installed;
1489
1490   solv->allownamechange = 1;
1491
1492   solv->dup_allowdowngrade = 1;
1493   solv->dup_allownamechange = 1;
1494   solv->dup_allowarchchange = 1;
1495   solv->dup_allowvendorchange = 1;
1496
1497   solv->keepexplicitobsoletes = pool->noobsoletesmultiversion ? 0 : 1;
1498
1499   queue_init(&solv->ruletojob);
1500   queue_init(&solv->decisionq);
1501   queue_init(&solv->decisionq_why);
1502   queue_init(&solv->problems);
1503   queue_init(&solv->orphaned);
1504   queue_init(&solv->learnt_why);
1505   queue_init(&solv->learnt_pool);
1506   queue_init(&solv->branches);
1507   queue_init(&solv->weakruleq);
1508   queue_init(&solv->ruleassertions);
1509   queue_init(&solv->addedmap_deduceq);
1510
1511   queue_push(&solv->learnt_pool, 0);    /* so that 0 does not describe a proof */
1512
1513   map_init(&solv->recommendsmap, pool->nsolvables);
1514   map_init(&solv->suggestsmap, pool->nsolvables);
1515   map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1516   solv->recommends_index = 0;
1517
1518   solv->decisionmap = (Id *)solv_calloc(pool->nsolvables, sizeof(Id));
1519   solv->nrules = 1;
1520   solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1521   memset(solv->rules, 0, sizeof(Rule));
1522
1523   return solv;
1524 }
1525
1526
1527 /*-------------------------------------------------------------------
1528  * 
1529  * solver_free
1530  */
1531
1532 void
1533 solver_free(Solver *solv)
1534 {
1535   queue_free(&solv->job);
1536   queue_free(&solv->ruletojob);
1537   queue_free(&solv->decisionq);
1538   queue_free(&solv->decisionq_why);
1539   queue_free(&solv->learnt_why);
1540   queue_free(&solv->learnt_pool);
1541   queue_free(&solv->problems);
1542   queue_free(&solv->solutions);
1543   queue_free(&solv->orphaned);
1544   queue_free(&solv->branches);
1545   queue_free(&solv->weakruleq);
1546   queue_free(&solv->ruleassertions);
1547   queue_free(&solv->addedmap_deduceq);
1548   if (solv->cleandeps_updatepkgs)
1549     {
1550       queue_free(solv->cleandeps_updatepkgs);
1551       solv->cleandeps_updatepkgs = solv_free(solv->cleandeps_updatepkgs);
1552     }
1553   if (solv->cleandeps_mistakes)
1554     {
1555       queue_free(solv->cleandeps_mistakes);
1556       solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
1557     }
1558   if (solv->update_targets)
1559     {
1560       queue_free(solv->update_targets);
1561       solv->update_targets = solv_free(solv->update_targets);
1562     }
1563   if (solv->installsuppdepq)
1564     {
1565       queue_free(solv->installsuppdepq);
1566       solv->installsuppdepq = solv_free(solv->installsuppdepq);
1567     }
1568
1569   map_free(&solv->recommendsmap);
1570   map_free(&solv->suggestsmap);
1571   map_free(&solv->noupdate);
1572   map_free(&solv->weakrulemap);
1573   map_free(&solv->multiversion);
1574
1575   map_free(&solv->updatemap);
1576   map_free(&solv->bestupdatemap);
1577   map_free(&solv->fixmap);
1578   map_free(&solv->dupmap);
1579   map_free(&solv->dupinvolvedmap);
1580   map_free(&solv->droporphanedmap);
1581   map_free(&solv->cleandepsmap);
1582
1583   solv_free(solv->decisionmap);
1584   solv_free(solv->rules);
1585   solv_free(solv->watches);
1586   solv_free(solv->obsoletes);
1587   solv_free(solv->obsoletes_data);
1588   solv_free(solv->multiversionupdaters);
1589   solv_free(solv->choicerules_ref);
1590   solv_free(solv->bestrules_pkg);
1591   solv_free(solv);
1592 }
1593
1594 int
1595 solver_get_flag(Solver *solv, int flag)
1596 {
1597   switch (flag)
1598   {
1599   case SOLVER_FLAG_ALLOW_DOWNGRADE:
1600     return solv->allowdowngrade;
1601   case SOLVER_FLAG_ALLOW_NAMECHANGE:
1602     return solv->allownamechange;
1603   case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1604     return solv->allowarchchange;
1605   case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1606     return solv->allowvendorchange;
1607   case SOLVER_FLAG_ALLOW_UNINSTALL:
1608     return solv->allowuninstall;
1609   case SOLVER_FLAG_NO_UPDATEPROVIDE:
1610     return solv->noupdateprovide;
1611   case SOLVER_FLAG_SPLITPROVIDES:
1612     return solv->dosplitprovides;
1613   case SOLVER_FLAG_IGNORE_RECOMMENDED:
1614     return solv->dontinstallrecommended;
1615   case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1616     return solv->addalreadyrecommended;
1617   case SOLVER_FLAG_NO_INFARCHCHECK:
1618     return solv->noinfarchcheck;
1619   case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1620     return solv->keepexplicitobsoletes;
1621   case SOLVER_FLAG_BEST_OBEY_POLICY:
1622     return solv->bestobeypolicy;
1623   case SOLVER_FLAG_NO_AUTOTARGET:
1624     return solv->noautotarget;
1625   default:
1626     break;
1627   }
1628   return -1;
1629 }
1630
1631 int
1632 solver_set_flag(Solver *solv, int flag, int value)
1633 {
1634   int old = solver_get_flag(solv, flag);
1635   switch (flag)
1636   {
1637   case SOLVER_FLAG_ALLOW_DOWNGRADE:
1638     solv->allowdowngrade = value;
1639     break;
1640   case SOLVER_FLAG_ALLOW_NAMECHANGE:
1641     solv->allownamechange = value;
1642     break;
1643   case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1644     solv->allowarchchange = value;
1645     break;
1646   case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1647     solv->allowvendorchange = value;
1648     break;
1649   case SOLVER_FLAG_ALLOW_UNINSTALL:
1650     solv->allowuninstall = value;
1651     break;
1652   case SOLVER_FLAG_NO_UPDATEPROVIDE:
1653     solv->noupdateprovide = value;
1654     break;
1655   case SOLVER_FLAG_SPLITPROVIDES:
1656     solv->dosplitprovides = value;
1657     break;
1658   case SOLVER_FLAG_IGNORE_RECOMMENDED:
1659     solv->dontinstallrecommended = value;
1660     break;
1661   case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1662     solv->addalreadyrecommended = value;
1663     break;
1664   case SOLVER_FLAG_NO_INFARCHCHECK:
1665     solv->noinfarchcheck = value;
1666     break;
1667   case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1668     solv->keepexplicitobsoletes = value;
1669     break;
1670   case SOLVER_FLAG_BEST_OBEY_POLICY:
1671     solv->bestobeypolicy = value;
1672     break;
1673   case SOLVER_FLAG_NO_AUTOTARGET:
1674     solv->noautotarget = value;
1675     break;
1676   default:
1677     break;
1678   }
1679   return old;
1680 }
1681
1682 int
1683 cleandeps_check_mistakes(Solver *solv, int level)
1684 {
1685   Pool *pool = solv->pool;
1686   Rule *r;
1687   Id p, pp;
1688   int i;
1689   int mademistake = 0;
1690
1691   if (!solv->cleandepsmap.size)
1692     return 0;
1693   /* check for mistakes */
1694   for (i = solv->installed->start; i < solv->installed->end; i++)
1695     {
1696       if (!MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1697         continue;
1698       r = solv->rules + solv->featurerules + (i - solv->installed->start);
1699       /* a mistake is when the featurerule is true but the updaterule is false */
1700       if (!r->p)
1701         continue;
1702       FOR_RULELITERALS(p, pp, r)
1703         if (p > 0 && solv->decisionmap[p] > 0)
1704           break;
1705       if (!p)
1706         continue;       /* feature rule is not true */
1707       r = solv->rules + solv->updaterules + (i - solv->installed->start);
1708       if (!r->p)
1709         continue;
1710       FOR_RULELITERALS(p, pp, r)
1711         if (p > 0 && solv->decisionmap[p] > 0)
1712           break;
1713       if (p)
1714         continue;       /* update rule is true */
1715       POOL_DEBUG(SOLV_DEBUG_SOLVER, "cleandeps mistake: ");
1716       solver_printruleclass(solv, SOLV_DEBUG_SOLVER, r);
1717       POOL_DEBUG(SOLV_DEBUG_SOLVER, "feature rule: ");
1718       solver_printruleclass(solv, SOLV_DEBUG_SOLVER, solv->rules + solv->featurerules + (i - solv->installed->start));
1719       if (!solv->cleandeps_mistakes)
1720         {
1721           solv->cleandeps_mistakes = solv_calloc(1, sizeof(Queue));
1722           queue_init(solv->cleandeps_mistakes);
1723         }
1724       queue_push(solv->cleandeps_mistakes, i);
1725       MAPCLR(&solv->cleandepsmap, i - solv->installed->start);
1726       solver_reenablepolicyrules_cleandeps(solv, i);
1727       mademistake = 1;
1728     }
1729   if (mademistake)
1730     solver_reset(solv);
1731   return mademistake;
1732 }
1733
1734 static void
1735 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
1736 {
1737   int i, j;
1738   Id p, *cp2;
1739   for (i = j = 0; i < q->count; i++)
1740     {
1741       p = q->elements[i];
1742       for (cp2 = cp; *cp2; cp2++)
1743         if (*cp2 == p)
1744           {
1745             q->elements[j++] = p;
1746             break;
1747           }
1748     }
1749   queue_truncate(q, j);
1750 }
1751
1752 /*-------------------------------------------------------------------
1753  * 
1754  * solver_run_sat
1755  *
1756  * all rules have been set up, now actually run the solver
1757  *
1758  */
1759
1760 void
1761 solver_run_sat(Solver *solv, int disablerules, int doweak)
1762 {
1763   Queue dq;             /* local decisionqueue */
1764   Queue dqs;            /* local decisionqueue for supplements */
1765   int systemlevel;
1766   int level, olevel;
1767   Rule *r;
1768   int i, j, n;
1769   Solvable *s;
1770   Pool *pool = solv->pool;
1771   Id p, pp, *dp;
1772   int minimizationsteps;
1773   int installedpos = solv->installed ? solv->installed->start : 0;
1774
1775   IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
1776     {
1777       POOL_DEBUG (SOLV_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1778       for (i = 1; i < solv->nrules; i++)
1779         solver_printruleclass(solv, SOLV_DEBUG_RULE_CREATION, solv->rules + i);
1780     }
1781
1782   POOL_DEBUG(SOLV_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1783
1784   /* start SAT algorithm */
1785   level = 1;
1786   systemlevel = level + 1;
1787   POOL_DEBUG(SOLV_DEBUG_SOLVER, "solving...\n");
1788
1789   queue_init(&dq);
1790   queue_init(&dqs);
1791
1792   /*
1793    * here's the main loop:
1794    * 1) propagate new decisions (only needed once)
1795    * 2) fulfill jobs
1796    * 3) try to keep installed packages
1797    * 4) fulfill all unresolved rules
1798    * 5) install recommended packages
1799    * 6) minimalize solution if we had choices
1800    * if we encounter a problem, we rewind to a safe level and restart
1801    * with step 1
1802    */
1803    
1804   minimizationsteps = 0;
1805   for (;;)
1806     {
1807       /*
1808        * initial propagation of the assertions
1809        */
1810       if (level == 1)
1811         {
1812           POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagating (propagate_index: %d;  size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1813           if ((r = propagate(solv, level)) != 0)
1814             {
1815               if (analyze_unsolvable(solv, r, disablerules))
1816                 continue;
1817               level = 0;
1818               break;    /* unsolvable */
1819             }
1820         }
1821
1822       /*
1823        * resolve jobs first
1824        */
1825      if (level < systemlevel)
1826         {
1827           POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving job rules\n");
1828           for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1829             {
1830               Id l;
1831               if (r->d < 0)             /* ignore disabled rules */
1832                 continue;
1833               queue_empty(&dq);
1834               FOR_RULELITERALS(l, pp, r)
1835                 {
1836                   if (l < 0)
1837                     {
1838                       if (solv->decisionmap[-l] <= 0)
1839                         break;
1840                     }
1841                   else
1842                     {
1843                       if (solv->decisionmap[l] > 0)
1844                         break;
1845                       if (solv->decisionmap[l] == 0)
1846                         queue_push(&dq, l);
1847                     }
1848                 }
1849               if (l || !dq.count)
1850                 continue;
1851               /* prune to installed if not updating */
1852               if (dq.count > 1 && solv->installed && !solv->updatemap_all &&
1853                   !(solv->job.elements[solv->ruletojob.elements[i - solv->jobrules]] & SOLVER_ORUPDATE))
1854                 {
1855                   int j, k;
1856                   for (j = k = 0; j < dq.count; j++)
1857                     {
1858                       Solvable *s = pool->solvables + dq.elements[j];
1859                       if (s->repo == solv->installed)
1860                         {
1861                           dq.elements[k++] = dq.elements[j];
1862                           if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
1863                             {
1864                               k = 0;    /* package wants to be updated, do not prune */
1865                               break;
1866                             }
1867                         }
1868                     }
1869                   if (k)
1870                     dq.count = k;
1871                 }
1872               olevel = level;
1873               level = selectandinstall(solv, level, &dq, disablerules, i);
1874               if (level == 0)
1875                 break;
1876               if (level <= olevel)
1877                 break;
1878             }
1879           if (level == 0)
1880             break;      /* unsolvable */
1881           systemlevel = level + 1;
1882           if (i < solv->jobrules_end)
1883             continue;
1884           if (!solv->decisioncnt_update)
1885             solv->decisioncnt_update = solv->decisionq.count;
1886         }
1887
1888       /*
1889        * installed packages
1890        */
1891       if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1892         {
1893           Repo *installed = solv->installed;
1894           int pass;
1895
1896           POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving installed packages\n");
1897           /* we use two passes if we need to update packages 
1898            * to create a better user experience */
1899           for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1900             {
1901               int passlevel = level;
1902               Id *multiversionupdaters = solv->multiversion.size ? solv->multiversionupdaters : 0;
1903               if (pass == 1 && !solv->decisioncnt_keep)
1904                 solv->decisioncnt_keep = solv->decisionq.count;
1905               /* start with installedpos, the position that gave us problems the last time */
1906               for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1907                 {
1908                   Rule *rr;
1909                   Id d;
1910
1911                   if (i == installed->end)
1912                     i = installed->start;
1913                   s = pool->solvables + i;
1914                   if (s->repo != installed)
1915                     continue;
1916
1917                   if (solv->decisionmap[i] > 0 && (!multiversionupdaters || !multiversionupdaters[i - installed->start]))
1918                     continue;           /* already decided */
1919                   if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1920                     continue;           /* updates first */
1921                   r = solv->rules + solv->updaterules + (i - installed->start);
1922                   rr = r;
1923                   if (!rr->p || rr->d < 0)      /* disabled -> look at feature rule */
1924                     rr -= solv->installed->end - solv->installed->start;
1925                   if (!rr->p)           /* identical to update rule? */
1926                     rr = r;
1927                   if (!rr->p)
1928                     continue;           /* orpaned package */
1929
1930                   /* check if we should update this package to the latest version
1931                    * noupdate is set for erase jobs, in that case we want to deinstall
1932                    * the installed package and not replace it with a newer version */
1933                   queue_empty(&dq);
1934                   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))
1935                     {
1936                       if (multiversionupdaters && (d = multiversionupdaters[i - installed->start]) != 0)
1937                         {
1938                           /* special multiversion handling, make sure best version is chosen */
1939                           if (rr->p == i && solv->decisionmap[i] >= 0)
1940                             queue_push(&dq, i);
1941                           while ((p = pool->whatprovidesdata[d++]) != 0)
1942                             if (solv->decisionmap[p] >= 0)
1943                               queue_push(&dq, p);
1944                           if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1945                             prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
1946                           if (dq.count)
1947                             {
1948                               policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1949                               p = dq.elements[0];
1950                               if (p != i && solv->decisionmap[p] == 0)
1951                                 {
1952                                   rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1953                                   if (!rr->p)           /* update rule == feature rule? */
1954                                     rr = rr - solv->featurerules + solv->updaterules;
1955                                   dq.count = 1;
1956                                 }
1957                               else
1958                                 dq.count = 0;
1959                             }
1960                         }
1961                       else
1962                         {
1963                           /* update to best package of the update rule */
1964                           FOR_RULELITERALS(p, pp, rr)
1965                             {
1966                               if (solv->decisionmap[p] > 0)
1967                                 {
1968                                   dq.count = 0;         /* already fulfilled */
1969                                   break;
1970                                 }
1971                               if (!solv->decisionmap[p])
1972                                 queue_push(&dq, p);
1973                             }
1974                         }
1975                     }
1976                   if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1977                     prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
1978                   /* install best version */
1979                   if (dq.count)
1980                     {
1981                       olevel = level;
1982                       level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1983                       if (level == 0)
1984                         {
1985                           queue_free(&dq);
1986                           queue_free(&dqs);
1987                           return;
1988                         }
1989                       if (level <= olevel)
1990                         {
1991                           if (level == 1 || level < passlevel)
1992                             break;      /* trouble */
1993                           if (level < olevel)
1994                             n = installed->start;       /* redo all */
1995                           i--;
1996                           n--;
1997                           continue;
1998                         }
1999                     }
2000                   /* if still undecided keep package */
2001                   if (solv->decisionmap[i] == 0)
2002                     {
2003                       olevel = level;
2004                       if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
2005                         {
2006 #if 0
2007                           POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
2008                           level = setpropagatelearn(solv, level, -i, disablerules, 0);
2009 #else
2010                           continue;
2011 #endif
2012                         }
2013                       else
2014                         {
2015                           POOL_DEBUG(SOLV_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
2016                           level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
2017                         }
2018                       if (level == 0)
2019                         break;
2020                       if (level <= olevel)
2021                         {
2022                           if (level == 1 || level < passlevel)
2023                             break;      /* trouble */
2024                           if (level < olevel)
2025                             n = installed->start;       /* redo all */
2026                           i--;
2027                           n--;
2028                           continue;     /* retry with learnt rule */
2029                         }
2030                     }
2031                 }
2032               if (n < installed->end)
2033                 {
2034                   installedpos = i;     /* retry problem solvable next time */
2035                   break;                /* ran into trouble */
2036                 }
2037               installedpos = installed->start;  /* reset installedpos */
2038             }
2039           if (level == 0)
2040             break;              /* unsolvable */
2041           systemlevel = level + 1;
2042           if (pass < 2)
2043             continue;           /* had trouble, retry */
2044         }
2045       if (!solv->decisioncnt_keep)
2046         solv->decisioncnt_keep = solv->decisionq.count;
2047
2048       if (level < systemlevel)
2049         systemlevel = level;
2050
2051       /*
2052        * decide
2053        */
2054       if (!solv->decisioncnt_resolve)
2055         solv->decisioncnt_resolve = solv->decisionq.count;
2056       POOL_DEBUG(SOLV_DEBUG_POLICY, "deciding unresolved rules\n");
2057       for (i = 1, n = 1; n < solv->nrules; i++, n++)
2058         {
2059           if (i == solv->nrules)
2060             i = 1;
2061           r = solv->rules + i;
2062           if (r->d < 0)         /* ignore disabled rules */
2063             continue;
2064           queue_empty(&dq);
2065           if (r->d == 0)
2066             {
2067               /* binary or unary rule */
2068               /* need two positive undecided literals */
2069               if (r->p < 0 || r->w2 <= 0)
2070                 continue;
2071               if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2072                 continue;
2073               queue_push(&dq, r->p);
2074               queue_push(&dq, r->w2);
2075             }
2076           else
2077             {
2078               /* make sure that
2079                * all negative literals are installed
2080                * no positive literal is installed
2081                * i.e. the rule is not fulfilled and we
2082                * just need to decide on the positive literals
2083                */
2084               if (r->p < 0)
2085                 {
2086                   if (solv->decisionmap[-r->p] <= 0)
2087                     continue;
2088                 }
2089               else
2090                 {
2091                   if (solv->decisionmap[r->p] > 0)
2092                     continue;
2093                   if (solv->decisionmap[r->p] == 0)
2094                     queue_push(&dq, r->p);
2095                 }
2096               dp = pool->whatprovidesdata + r->d;
2097               while ((p = *dp++) != 0)
2098                 {
2099                   if (p < 0)
2100                     {
2101                       if (solv->decisionmap[-p] <= 0)
2102                         break;
2103                     }
2104                   else
2105                     {
2106                       if (solv->decisionmap[p] > 0)
2107                         break;
2108                       if (solv->decisionmap[p] == 0)
2109                         queue_push(&dq, p);
2110                     }
2111                 }
2112               if (p)
2113                 continue;
2114             }
2115           IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
2116             {
2117               POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "unfulfilled ");
2118               solver_printruleclass(solv, SOLV_DEBUG_PROPAGATE, r);
2119             }
2120           /* dq.count < 2 cannot happen as this means that
2121            * the rule is unit */
2122           assert(dq.count > 1);
2123
2124           /* prune to cleandeps packages */
2125           if (solv->cleandepsmap.size && solv->installed)
2126             {
2127               Repo *installed = solv->installed;
2128               for (j = 0; j < dq.count; j++)
2129                 if (pool->solvables[dq.elements[j]].repo == installed && MAPTST(&solv->cleandepsmap, dq.elements[j] - installed->start))
2130                   break;
2131               if (j < dq.count)
2132                 {
2133                   dq.elements[0] = dq.elements[j];
2134                   queue_truncate(&dq, 1);
2135                 }
2136             }
2137
2138           olevel = level;
2139           level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
2140           if (level == 0)
2141             break;              /* unsolvable */
2142           if (level < systemlevel || level == 1)
2143             break;              /* trouble */
2144           /* something changed, so look at all rules again */
2145           n = 0;
2146         }
2147
2148       if (n != solv->nrules)    /* ran into trouble? */
2149         {
2150           if (level == 0)
2151             break;              /* unsolvable */
2152           continue;             /* start over */
2153         }
2154
2155       /* at this point we have a consistent system. now do the extras... */
2156
2157       /* first decide leftover cleandeps packages */
2158       if (solv->cleandepsmap.size && solv->installed)
2159         {
2160           for (p = solv->installed->start; p < solv->installed->end; p++)
2161             {
2162               s = pool->solvables + p;
2163               if (s->repo != solv->installed)
2164                 continue;
2165               if (solv->decisionmap[p] == 0 && MAPTST(&solv->cleandepsmap, p - solv->installed->start))
2166                 {
2167                   POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, p));
2168                   olevel = level;
2169                   level = setpropagatelearn(solv, level, -p, 0, 0);
2170                   if (level < olevel)
2171                     break;
2172                 }
2173             }
2174           if (p < solv->installed->end)
2175             continue;
2176         }
2177
2178       if (!solv->decisioncnt_weak)
2179         solv->decisioncnt_weak = solv->decisionq.count;
2180       if (doweak)
2181         {
2182           int qcount;
2183
2184           POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended packages\n");
2185           queue_empty(&dq);     /* recommended packages */
2186           queue_empty(&dqs);    /* supplemented packages */
2187           for (i = 1; i < pool->nsolvables; i++)
2188             {
2189               if (solv->decisionmap[i] < 0)
2190                 continue;
2191               if (solv->decisionmap[i] > 0)
2192                 {
2193                   /* installed, check for recommends */
2194                   Id *recp, rec, pp, p;
2195                   s = pool->solvables + i;
2196                   if (!solv->addalreadyrecommended && s->repo == solv->installed)
2197                     continue;
2198                   /* XXX need to special case AND ? */
2199                   if (s->recommends)
2200                     {
2201                       recp = s->repo->idarraydata + s->recommends;
2202                       while ((rec = *recp++) != 0)
2203                         {
2204                           qcount = dq.count;
2205                           FOR_PROVIDES(p, pp, rec)
2206                             {
2207                               if (solv->decisionmap[p] > 0)
2208                                 {
2209                                   dq.count = qcount;
2210                                   break;
2211                                 }
2212                               else if (solv->decisionmap[p] == 0)
2213                                 {
2214                                   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))))
2215                                     continue;
2216                                   queue_pushunique(&dq, p);
2217                                 }
2218                             }
2219                         }
2220                     }
2221                 }
2222               else
2223                 {
2224                   s = pool->solvables + i;
2225                   if (!s->supplements)
2226                     continue;
2227                   if (!pool_installable(pool, s))
2228                     continue;
2229                   if (!solver_is_supplementing(solv, s))
2230                     continue;
2231                   if (solv->dupmap_all && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
2232                     continue;
2233                   queue_push(&dqs, i);
2234                 }
2235             }
2236
2237           /* filter out all packages obsoleted by installed packages */
2238           /* this is no longer needed if we have reverse obsoletes */
2239           if ((dqs.count || dq.count) && solv->installed)
2240             {
2241               Map obsmap;
2242               Id obs, *obsp, po, ppo;
2243
2244               map_init(&obsmap, pool->nsolvables);
2245               for (p = solv->installed->start; p < solv->installed->end; p++)
2246                 {
2247                   s = pool->solvables + p;
2248                   if (s->repo != solv->installed || !s->obsoletes)
2249                     continue;
2250                   if (solv->decisionmap[p] <= 0)
2251                     continue;
2252                   if (solv->multiversion.size && MAPTST(&solv->multiversion, p))
2253                     continue;
2254                   obsp = s->repo->idarraydata + s->obsoletes;
2255                   /* foreach obsoletes */
2256                   while ((obs = *obsp++) != 0)
2257                     FOR_PROVIDES(po, ppo, obs)
2258                       MAPSET(&obsmap, po);
2259                 }
2260               for (i = j = 0; i < dqs.count; i++)
2261                 if (!MAPTST(&obsmap, dqs.elements[i]))
2262                   dqs.elements[j++] = dqs.elements[i];
2263               dqs.count = j;
2264               for (i = j = 0; i < dq.count; i++)
2265                 if (!MAPTST(&obsmap, dq.elements[i]))
2266                   dq.elements[j++] = dq.elements[i];
2267               dq.count = j;
2268               map_free(&obsmap);
2269             }
2270
2271           /* filter out all already supplemented packages if requested */
2272           if (!solv->addalreadyrecommended && dqs.count)
2273             {
2274               int dosplitprovides_old = solv->dosplitprovides;
2275               /* turn off all new packages */
2276               for (i = 0; i < solv->decisionq.count; i++)
2277                 {
2278                   p = solv->decisionq.elements[i];
2279                   if (p < 0)
2280                     continue;
2281                   s = pool->solvables + p;
2282                   if (s->repo && s->repo != solv->installed)
2283                     solv->decisionmap[p] = -solv->decisionmap[p];
2284                 }
2285               solv->dosplitprovides = 0;
2286               /* filter out old supplements */
2287               for (i = j = 0; i < dqs.count; i++)
2288                 {
2289                   p = dqs.elements[i];
2290                   s = pool->solvables + p;
2291                   if (!s->supplements)
2292                     continue;
2293                   if (!solver_is_supplementing(solv, s))
2294                     dqs.elements[j++] = p;
2295                   else if (solv->installsuppdepq && solver_check_installsuppdepq(solv, s))
2296                     dqs.elements[j++] = p;
2297                 }
2298               dqs.count = j;
2299               /* undo turning off */
2300               for (i = 0; i < solv->decisionq.count; i++)
2301                 {
2302                   p = solv->decisionq.elements[i];
2303                   if (p < 0)
2304                     continue;
2305                   s = pool->solvables + p;
2306                   if (s->repo && s->repo != solv->installed)
2307                     solv->decisionmap[p] = -solv->decisionmap[p];
2308                 }
2309               solv->dosplitprovides = dosplitprovides_old;
2310             }
2311
2312           /* multiversion doesn't mix well with supplements.
2313            * filter supplemented packages where we already decided
2314            * to install a different version (see bnc#501088) */
2315           if (dqs.count && solv->multiversion.size)
2316             {
2317               for (i = j = 0; i < dqs.count; i++)
2318                 {
2319                   p = dqs.elements[i];
2320                   if (MAPTST(&solv->multiversion, p))
2321                     {
2322                       Id p2, pp2;
2323                       s = pool->solvables + p;
2324                       FOR_PROVIDES(p2, pp2, s->name)
2325                         if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
2326                           break;
2327                       if (p2)
2328                         continue;       /* ignore this package */
2329                     }
2330                   dqs.elements[j++] = p;
2331                 }
2332               dqs.count = j;
2333             }
2334
2335           /* make dq contain both recommended and supplemented pkgs */
2336           if (dqs.count)
2337             {
2338               for (i = 0; i < dqs.count; i++)
2339                 queue_pushunique(&dq, dqs.elements[i]);
2340             }
2341
2342           if (dq.count)
2343             {
2344               Map dqmap;
2345               int decisioncount = solv->decisionq.count;
2346
2347               if (dq.count == 1)
2348                 {
2349                   /* simple case, just one package. no need to choose to best version */
2350                   p = dq.elements[0];
2351                   if (dqs.count)
2352                     POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2353                   else
2354                     POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2355                   level = setpropagatelearn(solv, level, p, 0, 0);
2356                   if (level == 0)
2357                     break;
2358                   continue;     /* back to main loop */
2359                 }
2360
2361               /* filter packages, this gives us the best versions */
2362               policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
2363
2364               /* create map of result */
2365               map_init(&dqmap, pool->nsolvables);
2366               for (i = 0; i < dq.count; i++)
2367                 MAPSET(&dqmap, dq.elements[i]);
2368
2369               /* install all supplemented packages */
2370               for (i = 0; i < dqs.count; i++)
2371                 {
2372                   p = dqs.elements[i];
2373                   if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
2374                     continue;
2375                   POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2376                   olevel = level;
2377                   level = setpropagatelearn(solv, level, p, 0, 0);
2378                   if (level <= olevel)
2379                     break;
2380                 }
2381               if (i < dqs.count || solv->decisionq.count < decisioncount)
2382                 {
2383                   map_free(&dqmap);
2384                   if (level == 0)
2385                     break;
2386                   continue;
2387                 }
2388
2389               /* install all recommended packages */
2390               /* more work as we want to created branches if multiple
2391                * choices are valid */
2392               for (i = 0; i < decisioncount; i++)
2393                 {
2394                   Id rec, *recp, pp;
2395                   p = solv->decisionq.elements[i];
2396                   if (p < 0)
2397                     continue;
2398                   s = pool->solvables + p;
2399                   if (!s->repo || (!solv->addalreadyrecommended && s->repo == solv->installed))
2400                     continue;
2401                   if (!s->recommends)
2402                     continue;
2403                   recp = s->repo->idarraydata + s->recommends;
2404                   while ((rec = *recp++) != 0)
2405                     {
2406                       queue_empty(&dq);
2407                       FOR_PROVIDES(p, pp, rec)
2408                         {
2409                           if (solv->decisionmap[p] > 0)
2410                             {
2411                               dq.count = 0;
2412                               break;
2413                             }
2414                           else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
2415                             queue_pushunique(&dq, p);
2416                         }
2417                       if (!dq.count)
2418                         continue;
2419                       if (dq.count > 1)
2420                         {
2421                           /* multiple candidates, open a branch */
2422                           for (i = 1; i < dq.count; i++)
2423                             queue_push(&solv->branches, dq.elements[i]);
2424                           queue_push(&solv->branches, -level);
2425                         }
2426                       p = dq.elements[0];
2427                       POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2428                       olevel = level;
2429                       level = setpropagatelearn(solv, level, p, 0, 0);
2430                       if (level <= olevel || solv->decisionq.count < decisioncount)
2431                         break;  /* we had to revert some decisions */
2432                     }
2433                   if (rec)
2434                     break;      /* had a problem above, quit loop */
2435                 }
2436               map_free(&dqmap);
2437               if (level == 0)
2438                 break;
2439               continue;         /* back to main loop so that all deps are checked */
2440             }
2441         }
2442
2443       if (!solv->decisioncnt_orphan)
2444         solv->decisioncnt_orphan = solv->decisionq.count;
2445       if (solv->dupmap_all && solv->installed)
2446         {
2447           int installedone = 0;
2448
2449           /* let's see if we can install some unsupported package */
2450           POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding orphaned packages\n");
2451           for (i = 0; i < solv->orphaned.count; i++)
2452             {
2453               p = solv->orphaned.elements[i];
2454               if (solv->decisionmap[p])
2455                 continue;       /* already decided */
2456               if (solv->droporphanedmap_all)
2457                 continue;
2458               if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
2459                 continue;
2460               POOL_DEBUG(SOLV_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
2461               olevel = level;
2462               level = setpropagatelearn(solv, level, p, 0, 0);
2463               installedone = 1;
2464               if (level < olevel)
2465                 break;
2466             }
2467           if (installedone || i < solv->orphaned.count)
2468             {
2469               if (level == 0)
2470                 break;
2471               continue;         /* back to main loop */
2472             }
2473           for (i = 0; i < solv->orphaned.count; i++)
2474             {
2475               p = solv->orphaned.elements[i];
2476               if (solv->decisionmap[p])
2477                 continue;       /* already decided */
2478               POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2479               olevel = level;
2480               level = setpropagatelearn(solv, level, -p, 0, 0);
2481               if (level < olevel)
2482                 break;
2483             }
2484           if (i < solv->orphaned.count)
2485             {
2486               if (level == 0)
2487                 break;
2488               continue;         /* back to main loop */
2489             }
2490         }
2491
2492      if (solv->installed && solv->cleandepsmap.size)
2493         {
2494           if (cleandeps_check_mistakes(solv, level))
2495             {
2496               level = 1;        /* restart from scratch */
2497               systemlevel = level + 1;
2498               continue;
2499             }
2500         }
2501
2502      if (solv->solution_callback)
2503         {
2504           solv->solution_callback(solv, solv->solution_callback_data);
2505           if (solv->branches.count)
2506             {
2507               int i = solv->branches.count - 1;
2508               int l = -solv->branches.elements[i];
2509               Id why;
2510
2511               for (; i > 0; i--)
2512                 if (solv->branches.elements[i - 1] < 0)
2513                   break;
2514               p = solv->branches.elements[i];
2515               POOL_DEBUG(SOLV_DEBUG_SOLVER, "branching with %s\n", pool_solvid2str(pool, p));
2516               queue_empty(&dq);
2517               for (j = i + 1; j < solv->branches.count; j++)
2518                 queue_push(&dq, solv->branches.elements[j]);
2519               solv->branches.count = i;
2520               level = l;
2521               revert(solv, level);
2522               if (dq.count > 1)
2523                 for (j = 0; j < dq.count; j++)
2524                   queue_push(&solv->branches, dq.elements[j]);
2525               olevel = level;
2526               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2527               assert(why >= 0);
2528               level = setpropagatelearn(solv, level, p, disablerules, why);
2529               if (level == 0)
2530                 break;
2531               continue;
2532             }
2533           /* all branches done, we're finally finished */
2534           break;
2535         }
2536
2537       /* auto-minimization step */
2538      if (solv->branches.count)
2539         {
2540           int l = 0, lasti = -1, lastl = -1;
2541           Id why;
2542
2543           p = 0;
2544           for (i = solv->branches.count - 1; i >= 0; i--)
2545             {
2546               p = solv->branches.elements[i];
2547               if (p < 0)
2548                 l = -p;
2549               else if (p > 0 && solv->decisionmap[p] > l + 1)
2550                 {
2551                   lasti = i;
2552                   lastl = l;
2553                 }
2554             }
2555           if (lasti >= 0)
2556             {
2557               /* kill old solvable so that we do not loop */
2558               p = solv->branches.elements[lasti];
2559               solv->branches.elements[lasti] = 0;
2560               POOL_DEBUG(SOLV_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, pool_solvid2str(pool, p));
2561               minimizationsteps++;
2562
2563               level = lastl;
2564               revert(solv, level);
2565               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2566               assert(why >= 0);
2567               olevel = level;
2568               level = setpropagatelearn(solv, level, p, disablerules, why);
2569               if (level == 0)
2570                 break;
2571               continue;         /* back to main loop */
2572             }
2573         }
2574       /* no minimization found, we're finally finished! */
2575       break;
2576     }
2577
2578   POOL_DEBUG(SOLV_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2579
2580   POOL_DEBUG(SOLV_DEBUG_STATS, "done solving.\n\n");
2581   queue_free(&dq);
2582   queue_free(&dqs);
2583   if (level == 0)
2584     {
2585       /* unsolvable */
2586       solv->decisioncnt_update = solv->decisionq.count;
2587       solv->decisioncnt_keep = solv->decisionq.count;
2588       solv->decisioncnt_resolve = solv->decisionq.count;
2589       solv->decisioncnt_weak = solv->decisionq.count;
2590       solv->decisioncnt_orphan = solv->decisionq.count;
2591     }
2592 #if 0
2593   solver_printdecisionq(solv, SOLV_DEBUG_RESULT);
2594 #endif
2595 }
2596
2597
2598 /*-------------------------------------------------------------------
2599  * 
2600  * remove disabled conflicts
2601  *
2602  * purpose: update the decisionmap after some rules were disabled.
2603  * this is used to calculate the suggested/recommended package list.
2604  * Also returns a "removed" list to undo the discisionmap changes.
2605  */
2606
2607 static void
2608 removedisabledconflicts(Solver *solv, Queue *removed)
2609 {
2610   Pool *pool = solv->pool;
2611   int i, n;
2612   Id p, why, *dp;
2613   Id new;
2614   Rule *r;
2615   Id *decisionmap = solv->decisionmap;
2616
2617   queue_empty(removed);
2618   for (i = 0; i < solv->decisionq.count; i++)
2619     {
2620       p = solv->decisionq.elements[i];
2621       if (p > 0)
2622         continue;       /* conflicts only, please */
2623       why = solv->decisionq_why.elements[i];
2624       if (why == 0)
2625         {
2626           /* no rule involved, must be a orphan package drop */
2627           continue;
2628         }
2629       /* we never do conflicts on free decisions, so there
2630        * must have been an unit rule */
2631       assert(why > 0);
2632       r = solv->rules + why;
2633       if (r->d < 0 && decisionmap[-p])
2634         {
2635           /* rule is now disabled, remove from decisionmap */
2636           POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2637           queue_push(removed, -p);
2638           queue_push(removed, decisionmap[-p]);
2639           decisionmap[-p] = 0;
2640         }
2641     }
2642   if (!removed->count)
2643     return;
2644   /* we removed some confliced packages. some of them might still
2645    * be in conflict, so search for unit rules and re-conflict */
2646   new = 0;
2647   for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2648     {
2649       if (i == solv->nrules)
2650         {
2651           i = 1;
2652           r = solv->rules + i;
2653         }
2654       if (r->d < 0)
2655         continue;
2656       if (!r->w2)
2657         {
2658           if (r->p < 0 && !decisionmap[-r->p])
2659             new = r->p;
2660         }
2661       else if (!r->d)
2662         {
2663           /* binary rule */
2664           if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2665             new = r->p;
2666           else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2667             new = r->w2;
2668         }
2669       else
2670         {
2671           if (r->p < 0 && decisionmap[-r->p] == 0)
2672             new = r->p;
2673           if (new || DECISIONMAP_FALSE(r->p))
2674             {
2675               dp = pool->whatprovidesdata + r->d;
2676               while ((p = *dp++) != 0)
2677                 {
2678                   if (new && p == new)
2679                     continue;
2680                   if (p < 0 && decisionmap[-p] == 0)
2681                     {
2682                       if (new)
2683                         {
2684                           new = 0;
2685                           break;
2686                         }
2687                       new = p;
2688                     }
2689                   else if (!DECISIONMAP_FALSE(p))
2690                     {
2691                       new = 0;
2692                       break;
2693                     }
2694                 }
2695             }
2696         }
2697       if (new)
2698         {
2699           POOL_DEBUG(SOLV_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2700           decisionmap[-new] = -1;
2701           new = 0;
2702           n = 0;        /* redo all rules */
2703         }
2704     }
2705 }
2706
2707 static inline void
2708 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2709 {
2710   int i;
2711   for (i = 0; i < removed->count; i += 2)
2712     solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2713 }
2714
2715
2716 /*-------------------------------------------------------------------
2717  *
2718  * weaken solvable dependencies
2719  */
2720
2721 static void
2722 weaken_solvable_deps(Solver *solv, Id p)
2723 {
2724   int i;
2725   Rule *r;
2726
2727   for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2728     {
2729       if (r->p != -p)
2730         continue;
2731       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2732         continue;       /* conflict */
2733       queue_push(&solv->weakruleq, i);
2734     }
2735 }
2736
2737
2738 /********************************************************************/
2739 /* main() */
2740
2741
2742 void
2743 solver_calculate_multiversionmap(Pool *pool, Queue *job, Map *multiversionmap)
2744 {
2745   int i;
2746   Id how, what, select;
2747   Id p, pp;
2748   for (i = 0; i < job->count; i += 2)
2749     {
2750       how = job->elements[i];
2751       if ((how & SOLVER_JOBMASK) != SOLVER_MULTIVERSION)
2752         continue;
2753       what = job->elements[i + 1];
2754       select = how & SOLVER_SELECTMASK;
2755       if (!multiversionmap->size)
2756         map_grow(multiversionmap, pool->nsolvables);
2757       if (select == SOLVER_SOLVABLE_ALL)
2758         {
2759           FOR_POOL_SOLVABLES(p)
2760             MAPSET(multiversionmap, p);
2761         }
2762       else if (select == SOLVER_SOLVABLE_REPO)
2763         {
2764           Solvable *s;
2765           Repo *repo = pool_id2repo(pool, what);
2766           if (repo)
2767             FOR_REPO_SOLVABLES(repo, p, s)
2768               MAPSET(multiversionmap, p);
2769         }
2770       FOR_JOB_SELECT(p, pp, select, what)
2771         MAPSET(multiversionmap, p);
2772     }
2773 }
2774
2775 void
2776 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *multiversionmap)
2777 {
2778   solver_calculate_multiversionmap(pool, job, multiversionmap);
2779 }
2780
2781 /*
2782  * add a rule created by a job, record job number and weak flag
2783  */
2784 static inline void
2785 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
2786 {
2787   solver_addrule(solv, p, d);
2788   queue_push(&solv->ruletojob, job);
2789   if (weak)
2790     queue_push(&solv->weakruleq, solv->nrules - 1);
2791 }
2792
2793 static inline void
2794 add_cleandeps_package(Solver *solv, Id p)
2795 {
2796   if (!solv->cleandeps_updatepkgs)
2797     {
2798       solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
2799       queue_init(solv->cleandeps_updatepkgs);
2800     }
2801   queue_pushunique(solv->cleandeps_updatepkgs, p);
2802 }
2803
2804 static void
2805 add_update_target(Solver *solv, Id p, Id how)
2806 {
2807   Pool *pool = solv->pool;
2808   Solvable *s = pool->solvables + p;
2809   Repo *installed = solv->installed;
2810   Id pi, pip;
2811   if (!solv->update_targets)
2812     {
2813       solv->update_targets = solv_calloc(1, sizeof(Queue));
2814       queue_init(solv->update_targets);
2815     }
2816   if (s->repo == installed)
2817     {
2818       queue_push2(solv->update_targets, p, p);
2819       return;
2820     }
2821   FOR_PROVIDES(pi, pip, s->name)
2822     {
2823       Solvable *si = pool->solvables + pi;
2824       if (si->repo != installed || si->name != s->name)
2825         continue;
2826       if (how & SOLVER_FORCEBEST)
2827         {
2828           if (!solv->bestupdatemap.size)
2829             map_grow(&solv->bestupdatemap, installed->end - installed->start);
2830           MAPSET(&solv->bestupdatemap, pi - installed->start);
2831         }
2832       if (how & SOLVER_CLEANDEPS)
2833         add_cleandeps_package(solv, pi);
2834       queue_push2(solv->update_targets, pi, p);
2835       /* check if it's ok to keep the installed package */
2836       if (s->evr == si->evr && solvable_identical(s, si))
2837         queue_push2(solv->update_targets, pi, pi);
2838     }
2839   if (s->obsoletes)
2840     {
2841       Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2842       while ((obs = *obsp++) != 0)
2843         {
2844           FOR_PROVIDES(pi, pip, obs) 
2845             {
2846               Solvable *si = pool->solvables + pi;
2847               if (si->repo != installed)
2848                 continue;
2849               if (si->name == s->name)
2850                 continue;       /* already handled above */
2851               if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
2852                 continue;
2853               if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
2854                 continue;
2855               if (how & SOLVER_FORCEBEST)
2856                 {
2857                   if (!solv->bestupdatemap.size)
2858                     map_grow(&solv->bestupdatemap, installed->end - installed->start);
2859                   MAPSET(&solv->bestupdatemap, pi - installed->start);
2860                 }
2861               if (how & SOLVER_CLEANDEPS)
2862                 add_cleandeps_package(solv, pi);
2863               queue_push2(solv->update_targets, pi, p);
2864             }
2865         }
2866     }
2867 }
2868
2869 static int
2870 transform_update_targets_sortfn(const void *ap, const void *bp, void *dp)
2871 {
2872   const Id *a = ap;
2873   const Id *b = bp;
2874   if (a[0] - b[0])
2875     return a[0] - b[0];
2876   return a[1] - b[1];
2877 }
2878
2879 static void
2880 transform_update_targets(Solver *solv)
2881 {
2882   Repo *installed = solv->installed;
2883   Queue *update_targets = solv->update_targets;
2884   int i, j;
2885   Id p, q, lastp, lastq;
2886
2887   if (!update_targets->count)
2888     {
2889       queue_free(update_targets);
2890       solv->update_targets = solv_free(update_targets);
2891       return;
2892     }
2893   if (update_targets->count > 2)
2894     solv_sort(update_targets->elements, update_targets->count >> 1, 2 * sizeof(Id), transform_update_targets_sortfn, solv);
2895   queue_insertn(update_targets, 0, installed->end - installed->start, 0);
2896   lastp = lastq = 0;
2897   for (i = j = installed->end - installed->start; i < update_targets->count; i += 2)
2898     {
2899       if ((p = update_targets->elements[i]) != lastp)
2900         {
2901           if (!solv->updatemap.size)
2902             map_grow(&solv->updatemap, installed->end - installed->start);
2903           MAPSET(&solv->updatemap, p - installed->start);
2904           update_targets->elements[j++] = 0;                    /* finish old set */
2905           update_targets->elements[p - installed->start] = j;   /* start new set */
2906           lastp = p;
2907           lastq = 0;
2908         }
2909       if ((q = update_targets->elements[i + 1]) != lastq)
2910         {
2911           update_targets->elements[j++] = q;
2912           lastq = q;
2913         }
2914     }
2915   queue_truncate(update_targets, j);
2916   queue_push(update_targets, 0);        /* finish last set */
2917 }
2918
2919
2920 static void
2921 addedmap2deduceq(Solver *solv, Map *addedmap)
2922 {
2923   Pool *pool = solv->pool;
2924   int i, j;
2925   Id p;
2926   Rule *r;
2927
2928   queue_empty(&solv->addedmap_deduceq);
2929   for (i = 2, j = solv->rpmrules_end - 1; i < pool->nsolvables && j > 0; j--)
2930     {
2931       r = solv->rules + j;
2932       if (r->p >= 0)
2933         continue;
2934       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2935         continue;
2936       p = -r->p;
2937       if (!MAPTST(addedmap, p))
2938         {
2939           /* should never happen, but... */
2940           if (!solv->addedmap_deduceq.count || solv->addedmap_deduceq.elements[solv->addedmap_deduceq.count - 1] != -p)
2941             queue_push(&solv->addedmap_deduceq, -p);
2942           continue;
2943         }
2944       for (; i < p; i++)
2945         if (MAPTST(addedmap, i))
2946           queue_push(&solv->addedmap_deduceq, i);
2947       if (i == p)
2948         i++;
2949     }
2950   for (; i < pool->nsolvables; i++)
2951     if (MAPTST(addedmap, i))
2952       queue_push(&solv->addedmap_deduceq, i);
2953   j = 0;
2954   for (i = 2; i < pool->nsolvables; i++)
2955     if (MAPTST(addedmap, i))
2956       j++;
2957 }
2958
2959 static void 
2960 deduceq2addedmap(Solver *solv, Map *addedmap)
2961 {
2962   int j;
2963   Id p;
2964   Rule *r;
2965   for (j = solv->rpmrules_end - 1; j > 0; j--)
2966     {
2967       r = solv->rules + j;
2968       if (r->d < 0 && r->p)
2969         solver_enablerule(solv, r);
2970       if (r->p >= 0)
2971         continue;
2972       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2973         continue;
2974       p = -r->p;
2975       MAPSET(addedmap, p);
2976     }
2977   for (j = 0; j < solv->addedmap_deduceq.count; j++)
2978     {
2979       p = solv->addedmap_deduceq.elements[j];
2980       if (p > 0)
2981         MAPSET(addedmap, p);
2982       else
2983         MAPCLR(addedmap, p);
2984     }
2985 }
2986
2987
2988 /*
2989  *
2990  * solve job queue
2991  *
2992  */
2993
2994 int
2995 solver_solve(Solver *solv, Queue *job)
2996 {
2997   Pool *pool = solv->pool;
2998   Repo *installed = solv->installed;
2999   int i;
3000   int oldnrules, initialnrules;
3001   Map addedmap;                /* '1' == have rpm-rules for solvable */
3002   Map installcandidatemap;
3003   Id how, what, select, name, weak, p, pp, d;
3004   Queue q;
3005   Solvable *s;
3006   Rule *r;
3007   int now, solve_start;
3008   int hasdupjob = 0;
3009   int hasbestinstalljob = 0;
3010
3011   solve_start = solv_timems(0);
3012
3013   /* log solver options */
3014   POOL_DEBUG(SOLV_DEBUG_STATS, "solver started\n");
3015   POOL_DEBUG(SOLV_DEBUG_STATS, "dosplitprovides=%d, noupdateprovide=%d, noinfarchcheck=%d\n", solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
3016   POOL_DEBUG(SOLV_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allownamechange=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allownamechange, solv->allowarchchange, solv->allowvendorchange);
3017   POOL_DEBUG(SOLV_DEBUG_STATS, "promoteepoch=%d, forbidselfconflicts=%d\n", pool->promoteepoch, pool->forbidselfconflicts);
3018   POOL_DEBUG(SOLV_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d, implicitobsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors, pool->implicitobsoleteusescolors);
3019   POOL_DEBUG(SOLV_DEBUG_STATS, "dontinstallrecommended=%d, addalreadyrecommended=%d\n", solv->dontinstallrecommended, solv->addalreadyrecommended);
3020
3021   /* create whatprovides if not already there */
3022   if (!pool->whatprovides)
3023     pool_createwhatprovides(pool);
3024
3025   /* create obsolete index */
3026   policy_create_obsolete_index(solv);
3027
3028   /* remember job */
3029   queue_free(&solv->job);
3030   queue_init_clone(&solv->job, job);
3031   solv->pooljobcnt = pool->pooljobs.count;
3032   if (pool->pooljobs.count)
3033     queue_insertn(&solv->job, 0, pool->pooljobs.count, pool->pooljobs.elements);
3034   job = &solv->job;
3035
3036   /* free old stuff */
3037   if (solv->update_targets)
3038     {
3039       queue_free(solv->update_targets);
3040       solv->update_targets = solv_free(solv->update_targets);
3041     }
3042   if (solv->cleandeps_updatepkgs)
3043     {
3044       queue_free(solv->cleandeps_updatepkgs);
3045       solv->cleandeps_updatepkgs = solv_free(solv->cleandeps_updatepkgs);
3046     }
3047   queue_empty(&solv->ruleassertions);
3048   solv->bestrules_pkg = solv_free(solv->bestrules_pkg);
3049   solv->choicerules_ref = solv_free(solv->choicerules_ref);
3050   if (solv->noupdate.size)
3051     map_empty(&solv->noupdate);
3052   if (solv->multiversion.size)
3053     {
3054       map_free(&solv->multiversion);
3055       map_init(&solv->multiversion, 0);
3056     }
3057   solv->updatemap_all = 0;
3058   if (solv->updatemap.size)
3059     {
3060       map_free(&solv->updatemap);
3061       map_init(&solv->updatemap, 0);
3062     }
3063   solv->bestupdatemap_all = 0;
3064   if (solv->bestupdatemap.size)
3065     {
3066       map_free(&solv->bestupdatemap);
3067       map_init(&solv->bestupdatemap, 0);
3068     }
3069   solv->fixmap_all = 0;
3070   if (solv->fixmap.size)
3071     {
3072       map_free(&solv->fixmap);
3073       map_init(&solv->fixmap, 0);
3074     }
3075   solv->dupmap_all = 0;
3076   if (solv->dupmap.size)
3077     {
3078       map_free(&solv->dupmap);
3079       map_init(&solv->dupmap, 0);
3080     }
3081   if (solv->dupinvolvedmap.size)
3082     {
3083       map_free(&solv->dupinvolvedmap);
3084       map_init(&solv->dupinvolvedmap, 0);
3085     }
3086   solv->droporphanedmap_all = 0;
3087   if (solv->droporphanedmap.size)
3088     {
3089       map_free(&solv->droporphanedmap);
3090       map_init(&solv->droporphanedmap, 0);
3091     }
3092   if (solv->cleandepsmap.size)
3093     {
3094       map_free(&solv->cleandepsmap);
3095       map_init(&solv->cleandepsmap, 0);
3096     }
3097   
3098   queue_empty(&solv->weakruleq);
3099   solv->watches = solv_free(solv->watches);
3100   queue_empty(&solv->ruletojob);
3101   if (solv->decisionq.count)
3102     memset(solv->decisionmap, 0, pool->nsolvables * sizeof(Id));
3103   queue_empty(&solv->decisionq);
3104   queue_empty(&solv->decisionq_why);
3105   solv->decisioncnt_update = solv->decisioncnt_keep = solv->decisioncnt_resolve = solv->decisioncnt_weak = solv->decisioncnt_orphan = 0;
3106   queue_empty(&solv->learnt_why);
3107   queue_empty(&solv->learnt_pool);
3108   queue_empty(&solv->branches);
3109   solv->propagate_index = 0;
3110   queue_empty(&solv->problems);
3111   queue_empty(&solv->solutions);
3112   queue_empty(&solv->orphaned);
3113   solv->stats_learned = solv->stats_unsolvable = 0;
3114   if (solv->recommends_index)
3115     {
3116       map_empty(&solv->recommendsmap);
3117       map_empty(&solv->suggestsmap);
3118       solv->recommends_index = 0;
3119     }
3120   solv->multiversionupdaters = solv_free(solv->multiversionupdaters);
3121   
3122
3123   /*
3124    * create basic rule set of all involved packages
3125    * use addedmap bitmap to make sure we don't create rules twice
3126    */
3127
3128   /* create multiversion map if needed */
3129   solver_calculate_multiversionmap(pool, job, &solv->multiversion);
3130
3131   map_init(&addedmap, pool->nsolvables);
3132   MAPSET(&addedmap, SYSTEMSOLVABLE);
3133
3134   map_init(&installcandidatemap, pool->nsolvables);
3135   queue_init(&q);
3136
3137   now = solv_timems(0);
3138   /*
3139    * create rules for all package that could be involved with the solving
3140    * so called: rpm rules
3141    *
3142    */
3143   initialnrules = solv->rpmrules_end ? solv->rpmrules_end : 1;
3144   if (initialnrules > 1)
3145     deduceq2addedmap(solv, &addedmap);
3146   if (solv->nrules != initialnrules)
3147     solver_shrinkrules(solv, initialnrules);
3148   solv->nrules = initialnrules;
3149   solv->rpmrules_end = 0;
3150   
3151   if (installed)
3152     {
3153       /* check for update/verify jobs as they need to be known early */
3154       for (i = 0; i < job->count; i += 2)
3155         {
3156           how = job->elements[i];
3157           what = job->elements[i + 1];
3158           select = how & SOLVER_SELECTMASK;
3159           switch (how & SOLVER_JOBMASK)
3160             {
3161             case SOLVER_VERIFY:
3162               if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3163                 solv->fixmap_all = 1;
3164               FOR_JOB_SELECT(p, pp, select, what)
3165                 {
3166                   s = pool->solvables + p;
3167                   if (s->repo != installed)
3168                     continue;
3169                   if (!solv->fixmap.size)
3170                     map_grow(&solv->fixmap, installed->end - installed->start);
3171                   MAPSET(&solv->fixmap, p - installed->start);
3172                 }
3173               break;
3174             case SOLVER_UPDATE:
3175               if (select == SOLVER_SOLVABLE_ALL)
3176                 {
3177                   solv->updatemap_all = 1;
3178                   if (how & SOLVER_FORCEBEST)
3179                     solv->bestupdatemap_all = 1;
3180                   if (how & SOLVER_CLEANDEPS)
3181                     {
3182                       FOR_REPO_SOLVABLES(installed, p, s)
3183                         add_cleandeps_package(solv, p);
3184                     }
3185                 }
3186               else if (select == SOLVER_SOLVABLE_REPO)
3187                 {
3188                   Repo *repo = pool_id2repo(pool, what);
3189                   if (!repo)
3190                     break;
3191                   if (repo == installed && !(how & SOLVER_TARGETED))
3192                     {
3193                       solv->updatemap_all = 1;
3194                       if (how & SOLVER_FORCEBEST)
3195                         solv->bestupdatemap_all = 1;
3196                       if (how & SOLVER_CLEANDEPS)
3197                         {
3198                           FOR_REPO_SOLVABLES(installed, p, s)
3199                             add_cleandeps_package(solv, p);
3200                         }
3201                       break;
3202                     }
3203                   if (solv->noautotarget && !(how & SOLVER_TARGETED))
3204                     break;
3205                   /* targeted update */
3206                   FOR_REPO_SOLVABLES(repo, p, s)
3207                     add_update_target(solv, p, how);
3208                 }
3209               else
3210                 {
3211                   if (!(how & SOLVER_TARGETED))
3212                     {
3213                       int targeted = 1;
3214                       FOR_JOB_SELECT(p, pp, select, what)
3215                         {
3216                           s = pool->solvables + p;
3217                           if (s->repo != installed)
3218                             continue;
3219                           if (!solv->updatemap.size)
3220                             map_grow(&solv->updatemap, installed->end - installed->start);
3221                           MAPSET(&solv->updatemap, p - installed->start);
3222                           if (how & SOLVER_FORCEBEST)
3223                             {
3224                               if (!solv->bestupdatemap.size)
3225                                 map_grow(&solv->bestupdatemap, installed->end - installed->start);
3226                               MAPSET(&solv->bestupdatemap, p - installed->start);
3227                             }
3228                           if (how & SOLVER_CLEANDEPS)
3229                             add_cleandeps_package(solv, p);
3230                           targeted = 0;
3231                         }
3232                       if (!targeted || solv->noautotarget)
3233                         break;
3234                     }
3235                   FOR_JOB_SELECT(p, pp, select, what)
3236                     add_update_target(solv, p, how);
3237                 }
3238               break;
3239             default:
3240               break;
3241             }
3242         }
3243
3244       if (solv->update_targets)
3245         transform_update_targets(solv);
3246
3247       oldnrules = solv->nrules;
3248       FOR_REPO_SOLVABLES(installed, p, s)
3249         solver_addrpmrulesforsolvable(solv, s, &addedmap);
3250       POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3251       oldnrules = solv->nrules;
3252       FOR_REPO_SOLVABLES(installed, p, s)
3253         solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
3254       POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3255     }
3256
3257   /*
3258    * create rules for all packages involved in the job
3259    * (to be installed or removed)
3260    */
3261     
3262   oldnrules = solv->nrules;
3263   for (i = 0; i < job->count; i += 2)
3264     {
3265       how = job->elements[i];
3266       what = job->elements[i + 1];
3267       select = how & SOLVER_SELECTMASK;
3268
3269       switch (how & SOLVER_JOBMASK)
3270         {
3271         case SOLVER_INSTALL:
3272           FOR_JOB_SELECT(p, pp, select, what)
3273             {
3274               MAPSET(&installcandidatemap, p);
3275               solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3276             }
3277           break;
3278         case SOLVER_DISTUPGRADE:
3279           if (select == SOLVER_SOLVABLE_ALL)
3280             {
3281               solv->dupmap_all = 1;
3282               solv->updatemap_all = 1;
3283               if (how & SOLVER_FORCEBEST)
3284                 solv->bestupdatemap_all = 1;
3285             }
3286           if (!solv->dupmap_all)
3287             hasdupjob = 1;
3288           break;
3289         default:
3290           break;
3291         }
3292     }
3293   POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3294
3295     
3296   /*
3297    * add rules for suggests, enhances
3298    */
3299   oldnrules = solv->nrules;
3300   solver_addrpmrulesforweak(solv, &addedmap);
3301   POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3302
3303   /*
3304    * first pass done, we now have all the rpm rules we need.
3305    * unify existing rules before going over all job rules and
3306    * policy rules.
3307    * at this point the system is always solvable,
3308    * as an empty system (remove all packages) is a valid solution
3309    */
3310
3311   IF_POOLDEBUG (SOLV_DEBUG_STATS)
3312     {
3313       int possible = 0, installable = 0;
3314       for (i = 1; i < pool->nsolvables; i++)
3315         {
3316           if (pool_installable(pool, pool->solvables + i))
3317             installable++;
3318           if (MAPTST(&addedmap, i))
3319             possible++;
3320         }
3321       POOL_DEBUG(SOLV_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
3322     }
3323
3324   if (solv->nrules > initialnrules)
3325     solver_unifyrules(solv);                    /* remove duplicate rpm rules */
3326   solv->rpmrules_end = solv->nrules;            /* mark end of rpm rules */
3327
3328   if (solv->nrules > initialnrules)
3329     addedmap2deduceq(solv, &addedmap);          /* so that we can recreate the addedmap */
3330
3331   POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3332   POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule creation took %d ms\n", solv_timems(now));
3333
3334   /* create dup maps if needed. We need the maps early to create our
3335    * update rules */
3336   if (hasdupjob)
3337     solver_createdupmaps(solv);
3338
3339   /*
3340    * create feature rules
3341    * 
3342    * foreach installed:
3343    *   create assertion (keep installed, if no update available)
3344    *   or
3345    *   create update rule (A|update1(A)|update2(A)|...)
3346    * 
3347    * those are used later on to keep a version of the installed packages in
3348    * best effort mode
3349    */
3350     
3351   solv->featurerules = solv->nrules;              /* mark start of feature rules */
3352   if (installed)
3353     {
3354       /* foreach possibly installed solvable */
3355       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3356         {
3357           if (s->repo != installed)
3358             {
3359               solver_addrule(solv, 0, 0);       /* create dummy rule */
3360               continue;
3361             }
3362           solver_addupdaterule(solv, s, 1);    /* allow s to be updated */
3363         }
3364       /* make sure we accounted for all rules */
3365       assert(solv->nrules - solv->featurerules == installed->end - installed->start);
3366     }
3367   solv->featurerules_end = solv->nrules;
3368
3369     /*
3370      * Add update rules for installed solvables
3371      * 
3372      * almost identical to feature rules
3373      * except that downgrades/archchanges/vendorchanges are not allowed
3374      */
3375     
3376   solv->updaterules = solv->nrules;
3377
3378   if (installed)
3379     { /* foreach installed solvables */
3380       /* we create all update rules, but disable some later on depending on the job */
3381       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3382         {
3383           Rule *sr;
3384
3385           if (s->repo != installed)
3386             {
3387               solver_addrule(solv, 0, 0);       /* create dummy rule */
3388               continue;
3389             }
3390           solver_addupdaterule(solv, s, 0);     /* allowall = 0: downgrades not allowed */
3391           /*
3392            * check for and remove duplicate
3393            */
3394           r = solv->rules + solv->nrules - 1;           /* r: update rule */
3395           sr = r - (installed->end - installed->start); /* sr: feature rule */
3396           /* it's orphaned if there is no feature rule or the feature rule
3397            * consists just of the installed package */
3398           if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
3399             queue_push(&solv->orphaned, i);
3400           if (!r->p)
3401             {
3402               assert(solv->dupmap_all && !sr->p);
3403               continue;
3404             }
3405           if (!solver_rulecmp(solv, r, sr))
3406             memset(sr, 0, sizeof(*sr));         /* delete unneeded feature rule */
3407           else
3408             solver_disablerule(solv, sr);       /* disable feature rule */
3409         }
3410       /* consistency check: we added a rule for _every_ installed solvable */
3411       assert(solv->nrules - solv->updaterules == installed->end - installed->start);
3412     }
3413   solv->updaterules_end = solv->nrules;
3414
3415
3416   /*
3417    * now add all job rules
3418    */
3419
3420   solv->jobrules = solv->nrules;
3421   for (i = 0; i < job->count; i += 2)
3422     {
3423       oldnrules = solv->nrules;
3424
3425       if (i && i == solv->pooljobcnt)
3426         POOL_DEBUG(SOLV_DEBUG_JOB, "end of pool jobs\n");
3427       how = job->elements[i];
3428       what = job->elements[i + 1];
3429       weak = how & SOLVER_WEAK;
3430       select = how & SOLVER_SELECTMASK;
3431       switch (how & SOLVER_JOBMASK)
3432         {
3433         case SOLVER_INSTALL:
3434           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3435           if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3436             map_grow(&solv->cleandepsmap, installed->end - installed->start);
3437           if (select == SOLVER_SOLVABLE)
3438             {
3439               p = what;
3440               d = 0;
3441             }
3442           else
3443             {
3444               queue_empty(&q);
3445               FOR_JOB_SELECT(p, pp, select, what)
3446                 queue_push(&q, p);
3447               if (!q.count)
3448                 {
3449                   if (select == SOLVER_SOLVABLE_ONE_OF)
3450                     break;      /* ignore empty installs */
3451                   /* no candidate found or unsupported, make this an impossible rule */
3452                   queue_push(&q, -SYSTEMSOLVABLE);
3453                 }
3454               p = queue_shift(&q);      /* get first candidate */
3455               d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q);    /* internalize */
3456             }
3457           /* force install of namespace supplements hack */
3458           if (select == SOLVER_SOLVABLE_PROVIDES && !d && (p == SYSTEMSOLVABLE || p == -SYSTEMSOLVABLE) && ISRELDEP(what))
3459             {
3460               Reldep *rd = GETRELDEP(pool, what);
3461               if (rd->flags == REL_NAMESPACE)
3462                 {
3463                   p = SYSTEMSOLVABLE;
3464                   if (!solv->installsuppdepq)
3465                     {
3466                       solv->installsuppdepq = solv_calloc(1, sizeof(Queue));
3467                       queue_init(solv->installsuppdepq);
3468                     }
3469                   queue_pushunique(solv->installsuppdepq, rd->evr == 0 ? rd->name : what);
3470                 }
3471             }
3472           solver_addjobrule(solv, p, d, i, weak);
3473           if (how & SOLVER_FORCEBEST)
3474             hasbestinstalljob = 1;
3475           break;
3476         case SOLVER_ERASE:
3477           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
3478           if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3479             map_grow(&solv->cleandepsmap, installed->end - installed->start);
3480           /* specific solvable: by id or by nevra */
3481           name = (select == SOLVER_SOLVABLE || (select == SOLVER_SOLVABLE_NAME && ISRELDEP(what))) ? 0 : -1;
3482           if (select == SOLVER_SOLVABLE_ALL)    /* hmmm ;) */
3483             {
3484               FOR_POOL_SOLVABLES(p)
3485                 solver_addjobrule(solv, -p, 0, i, weak);
3486             }
3487           else if (select == SOLVER_SOLVABLE_REPO)
3488             {
3489               Repo *repo = pool_id2repo(pool, what);
3490               if (repo)
3491                 FOR_REPO_SOLVABLES(repo, p, s)
3492                   solver_addjobrule(solv, -p, 0, i, weak);
3493             }
3494           FOR_JOB_SELECT(p, pp, select, what)
3495             {
3496               s = pool->solvables + p;
3497               if (installed && s->repo == installed)
3498                 name = !name ? s->name : -1;
3499               solver_addjobrule(solv, -p, 0, i, weak);
3500             }
3501           /* special case for "erase a specific solvable": we also
3502            * erase all other solvables with that name, so that they
3503            * don't get picked up as replacement.
3504            * name is > 0 if exactly one installed solvable matched.
3505            */
3506           /* XXX: look also at packages that obsolete this package? */
3507           if (name > 0)
3508             {
3509               int j, k;
3510               k = solv->nrules;
3511               FOR_PROVIDES(p, pp, name)
3512                 {
3513                   s = pool->solvables + p;
3514                   if (s->name != name)
3515                     continue;
3516                   /* keep other versions installed */
3517                   if (s->repo == installed)
3518                     continue;
3519                   /* keep installcandidates of other jobs */
3520                   if (MAPTST(&installcandidatemap, p))
3521                     continue;
3522                   /* don't add the same rule twice */
3523                   for (j = oldnrules; j < k; j++)
3524                     if (solv->rules[j].p == -p)
3525                       break;
3526                   if (j == k)
3527                     solver_addjobrule(solv, -p, 0, i, weak);    /* remove by id */
3528                 }
3529             }
3530           break;
3531
3532         case SOLVER_UPDATE:
3533           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3534           break;
3535         case SOLVER_VERIFY:
3536           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3537           break;
3538         case SOLVER_WEAKENDEPS:
3539           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3540           if (select != SOLVER_SOLVABLE)
3541             break;
3542           s = pool->solvables + what;
3543           weaken_solvable_deps(solv, what);
3544           break;
3545         case SOLVER_MULTIVERSION:
3546           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %smultiversion %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3547           break;
3548         case SOLVER_LOCK:
3549           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3550           if (select == SOLVER_SOLVABLE_ALL)
3551             {
3552               FOR_POOL_SOLVABLES(p)
3553                 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3554             }
3555           else if (select == SOLVER_SOLVABLE_REPO)
3556             {
3557               Repo *repo = pool_id2repo(pool, what);
3558               if (repo)
3559                 FOR_REPO_SOLVABLES(repo, p, s)
3560                   solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3561             }
3562           FOR_JOB_SELECT(p, pp, select, what)
3563             solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3564           break;
3565         case SOLVER_DISTUPGRADE:
3566           POOL_DEBUG(SOLV_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
3567           break;
3568         case SOLVER_DROP_ORPHANED:
3569           POOL_DEBUG(SOLV_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
3570           if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3571             solv->droporphanedmap_all = 1;
3572           FOR_JOB_SELECT(p, pp, select, what)
3573             {
3574               s = pool->solvables + p;
3575               if (!installed || s->repo != installed)
3576                 continue;
3577               if (!solv->droporphanedmap.size)
3578                 map_grow(&solv->droporphanedmap, installed->end - installed->start);
3579               MAPSET(&solv->droporphanedmap, p - installed->start);
3580             }
3581           break;
3582         case SOLVER_USERINSTALLED:
3583           POOL_DEBUG(SOLV_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
3584           break;
3585         default:
3586           POOL_DEBUG(SOLV_DEBUG_JOB, "job: unknown job\n");
3587           break;
3588         }
3589         
3590         /*
3591          * debug
3592          */
3593         
3594       IF_POOLDEBUG (SOLV_DEBUG_JOB)
3595         {
3596           int j;
3597           if (solv->nrules == oldnrules)
3598             POOL_DEBUG(SOLV_DEBUG_JOB, "  - no rule created\n");
3599           for (j = oldnrules; j < solv->nrules; j++)
3600             {
3601               POOL_DEBUG(SOLV_DEBUG_JOB, "  - job ");
3602               solver_printrule(solv, SOLV_DEBUG_JOB, solv->rules + j);
3603             }
3604         }
3605     }
3606   assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
3607   solv->jobrules_end = solv->nrules;
3608
3609   /* now create infarch and dup rules */
3610   if (!solv->noinfarchcheck)
3611     {
3612       solver_addinfarchrules(solv, &addedmap);
3613       if (pool->implicitobsoleteusescolors)
3614         {
3615           /* currently doesn't work well with infarch rules, so make
3616            * them weak */
3617           for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
3618             queue_push(&solv->weakruleq, i);
3619         }
3620     }
3621   else
3622     solv->infarchrules = solv->infarchrules_end = solv->nrules;
3623
3624   if (hasdupjob)
3625     solver_addduprules(solv, &addedmap);
3626   else
3627     solv->duprules = solv->duprules_end = solv->nrules;
3628
3629   if (solv->bestupdatemap_all || solv->bestupdatemap.size || hasbestinstalljob)
3630     solver_addbestrules(solv, hasbestinstalljob);
3631   else
3632     solv->bestrules = solv->bestrules_end = solv->nrules;
3633
3634   if (hasdupjob)
3635     solver_freedupmaps(solv);   /* no longer needed */
3636
3637   if (1)
3638     solver_addchoicerules(solv);
3639   else
3640     solv->choicerules = solv->choicerules_end = solv->nrules;
3641
3642   if (0)
3643     {
3644       for (i = solv->featurerules; i < solv->nrules; i++)
3645         solver_printruleclass(solv, SOLV_DEBUG_RESULT, solv->rules + i);
3646     }
3647   /* all rules created
3648    * --------------------------------------------------------------
3649    * prepare for solving
3650    */
3651     
3652   /* free unneeded memory */
3653   map_free(&addedmap);
3654   map_free(&installcandidatemap);
3655   queue_free(&q);
3656
3657   POOL_DEBUG(SOLV_DEBUG_STATS, "%d rpm rules, 2 * %d update rules, %d job rules, %d infarch rules, %d dup rules, %d choice rules, %d best rules\n", solv->rpmrules_end - 1, solv->updaterules_end - solv->updaterules, solv->jobrules_end - solv->jobrules, solv->infarchrules_end - solv->infarchrules, solv->duprules_end - solv->duprules, solv->choicerules_end - solv->choicerules, solv->bestrules_end - solv->bestrules);
3658   POOL_DEBUG(SOLV_DEBUG_STATS, "overall rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3659
3660   /* create weak map */
3661   map_init(&solv->weakrulemap, solv->nrules);
3662   for (i = 0; i < solv->weakruleq.count; i++)
3663     {
3664       p = solv->weakruleq.elements[i];
3665       MAPSET(&solv->weakrulemap, p);
3666     }
3667
3668   /* enable cleandepsmap creation if we have updatepkgs */
3669   if (solv->cleandeps_updatepkgs && !solv->cleandepsmap.size)
3670     map_grow(&solv->cleandepsmap, installed->end - installed->start);
3671   /* no mistakes */
3672   if (solv->cleandeps_mistakes)
3673     {    
3674       queue_free(solv->cleandeps_mistakes);
3675       solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
3676     }    
3677
3678   /* all new rules are learnt after this point */
3679   solv->learntrules = solv->nrules;
3680
3681   /* create watches chains */
3682   makewatches(solv);
3683
3684   /* create assertion index. it is only used to speed up
3685    * makeruledecsions() a bit */
3686   for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
3687     if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
3688       queue_push(&solv->ruleassertions, i);
3689
3690   /* disable update rules that conflict with our job */
3691   solver_disablepolicyrules(solv);
3692
3693   /* make initial decisions based on assertion rules */
3694   makeruledecisions(solv);
3695   POOL_DEBUG(SOLV_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
3696
3697   /*
3698    * ********************************************
3699    * solve!
3700    * ********************************************
3701    */
3702     
3703   now = solv_timems(0);
3704   solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
3705   POOL_DEBUG(SOLV_DEBUG_STATS, "solver took %d ms\n", solv_timems(now));
3706
3707   /*
3708    * prepare solution queue if there were problems
3709    */
3710   solver_prepare_solutions(solv);
3711
3712   POOL_DEBUG(SOLV_DEBUG_STATS, "final solver statistics: %d problems, %d learned rules, %d unsolvable\n", solv->problems.count / 2, solv->stats_learned, solv->stats_unsolvable);
3713   POOL_DEBUG(SOLV_DEBUG_STATS, "solver_solve took %d ms\n", solv_timems(solve_start));
3714
3715   /* return number of problems */
3716   return solv->problems.count ? solv->problems.count / 2 : 0;
3717 }
3718
3719 Transaction *
3720 solver_create_transaction(Solver *solv)
3721 {
3722   return transaction_create_decisionq(solv->pool, &solv->decisionq, &solv->multiversion);
3723 }
3724
3725 void solver_get_orphaned(Solver *solv, Queue *orphanedq)
3726 {
3727   queue_free(orphanedq);
3728   queue_init_clone(orphanedq, &solv->orphaned);
3729 }
3730
3731 void solver_get_recommendations(Solver *solv, Queue *recommendationsq, Queue *suggestionsq, int noselected)
3732 {
3733   Pool *pool = solv->pool;
3734   Queue redoq, disabledq;
3735   int goterase, i;
3736   Solvable *s;
3737   Rule *r;
3738   Map obsmap;
3739
3740   if (!recommendationsq && !suggestionsq)
3741     return;
3742
3743   map_init(&obsmap, pool->nsolvables);
3744   if (solv->installed)
3745     {
3746       Id obs, *obsp, p, po, ppo;
3747       for (p = solv->installed->start; p < solv->installed->end; p++)
3748         {
3749           s = pool->solvables + p;
3750           if (s->repo != solv->installed || !s->obsoletes)
3751             continue;
3752           if (solv->decisionmap[p] <= 0)
3753             continue;
3754           if (solv->multiversion.size && MAPTST(&solv->multiversion, p))
3755             continue;
3756           obsp = s->repo->idarraydata + s->obsoletes;
3757           /* foreach obsoletes */
3758           while ((obs = *obsp++) != 0)
3759             FOR_PROVIDES(po, ppo, obs)
3760               MAPSET(&obsmap, po);
3761         }
3762     }
3763
3764   queue_init(&redoq);
3765   queue_init(&disabledq);
3766   goterase = 0;
3767   /* disable all erase jobs (including weak "keep uninstalled" rules) */
3768   for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
3769     {
3770       if (r->d < 0)     /* disabled ? */
3771         continue;
3772       if (r->p >= 0)    /* install job? */
3773         continue;
3774       queue_push(&disabledq, i);
3775       solver_disablerule(solv, r);
3776       goterase++;
3777     }
3778   
3779   if (goterase)
3780     {
3781       enabledisablelearntrules(solv);
3782       removedisabledconflicts(solv, &redoq);
3783     }
3784
3785   /*
3786    * find recommended packages
3787    */
3788   if (recommendationsq)
3789     {
3790       Id rec, *recp, p, pp;
3791
3792       queue_empty(recommendationsq);
3793       /* create map of all recommened packages */
3794       solv->recommends_index = -1;
3795       MAPZERO(&solv->recommendsmap);
3796
3797       /* put all packages the solver already chose in the map */
3798       if (solv->decisioncnt_weak)
3799         {
3800           for (i = solv->decisioncnt_weak; i < solv->decisioncnt_orphan; i++)
3801             {
3802               Id why;
3803               why = solv->decisionq_why.elements[i];
3804               if (why)
3805                 continue;       /* forced by unit rule or dep resolving */
3806               p = solv->decisionq.elements[i];
3807               if (p < 0)
3808                 continue;
3809               MAPSET(&solv->recommendsmap, p);
3810             }
3811         }
3812
3813       for (i = 0; i < solv->decisionq.count; i++)
3814         {
3815           p = solv->decisionq.elements[i];
3816           if (p < 0)
3817             continue;
3818           s = pool->solvables + p;
3819           if (s->recommends)
3820             {
3821               recp = s->repo->idarraydata + s->recommends;
3822               while ((rec = *recp++) != 0)
3823                 {
3824                   FOR_PROVIDES(p, pp, rec)
3825                     if (solv->decisionmap[p] > 0)
3826                       break;
3827                   if (p)
3828                     {
3829                       if (!noselected)
3830                         {
3831                           FOR_PROVIDES(p, pp, rec)
3832                             if (solv->decisionmap[p] > 0)
3833                               MAPSET(&solv->recommendsmap, p);
3834                         }
3835                       continue; /* p != 0: already fulfilled */
3836                     }
3837                   FOR_PROVIDES(p, pp, rec)
3838                     MAPSET(&solv->recommendsmap, p);
3839                 }
3840             }
3841         }
3842       for (i = 1; i < pool->nsolvables; i++)
3843         {
3844           if (solv->decisionmap[i] < 0)
3845             continue;
3846           if (solv->decisionmap[i] > 0 && noselected)
3847             continue;
3848           if (MAPTST(&obsmap, i))
3849             continue;
3850           s = pool->solvables + i;
3851           if (!MAPTST(&solv->recommendsmap, i))
3852             {
3853               if (!s->supplements)
3854                 continue;
3855               if (!pool_installable(pool, s))
3856                 continue;
3857               if (!solver_is_supplementing(solv, s))
3858                 continue;
3859             }
3860           queue_push(recommendationsq, i);
3861         }
3862       /* we use MODE_SUGGEST here so that repo prio is ignored */
3863       policy_filter_unwanted(solv, recommendationsq, POLICY_MODE_SUGGEST);
3864     }
3865
3866   /*
3867    * find suggested packages
3868    */
3869     
3870   if (suggestionsq)
3871     {
3872       Id sug, *sugp, p, pp;
3873
3874       queue_empty(suggestionsq);
3875       /* create map of all suggests that are still open */
3876       solv->recommends_index = -1;
3877       MAPZERO(&solv->suggestsmap);
3878       for (i = 0; i < solv->decisionq.count; i++)
3879         {
3880           p = solv->decisionq.elements[i];
3881           if (p < 0)
3882             continue;
3883           s = pool->solvables + p;
3884           if (s->suggests)
3885             {
3886               sugp = s->repo->idarraydata + s->suggests;
3887               while ((sug = *sugp++) != 0)
3888                 {
3889                   FOR_PROVIDES(p, pp, sug)
3890                     if (solv->decisionmap[p] > 0)
3891                       break;
3892                   if (p)
3893                     {
3894                       if (!noselected)
3895                         {
3896                           FOR_PROVIDES(p, pp, sug)
3897                             if (solv->decisionmap[p] > 0)
3898                               MAPSET(&solv->suggestsmap, p);
3899                         }
3900                       continue; /* already fulfilled */
3901                     }
3902                   FOR_PROVIDES(p, pp, sug)
3903                     MAPSET(&solv->suggestsmap, p);
3904                 }
3905             }
3906         }
3907       for (i = 1; i < pool->nsolvables; i++)
3908         {
3909           if (solv->decisionmap[i] < 0)
3910             continue;
3911           if (solv->decisionmap[i] > 0 && noselected)
3912             continue;
3913           if (MAPTST(&obsmap, i))
3914             continue;
3915           s = pool->solvables + i;
3916           if (!MAPTST(&solv->suggestsmap, i))
3917             {
3918               if (!s->enhances)
3919                 continue;
3920               if (!pool_installable(pool, s))
3921                 continue;
3922               if (!solver_is_enhancing(solv, s))
3923                 continue;
3924             }
3925           queue_push(suggestionsq, i);
3926         }
3927       policy_filter_unwanted(solv, suggestionsq, POLICY_MODE_SUGGEST);
3928     }
3929
3930   /* undo removedisabledconflicts */
3931   if (redoq.count)
3932     undo_removedisabledconflicts(solv, &redoq);
3933   queue_free(&redoq);
3934   
3935   /* undo job rule disabling */
3936   for (i = 0; i < disabledq.count; i++)
3937     solver_enablerule(solv, solv->rules + disabledq.elements[i]);
3938   queue_free(&disabledq);
3939   map_free(&obsmap);
3940 }
3941
3942
3943 /***********************************************************************/
3944 /* disk usage computations */
3945
3946 /*-------------------------------------------------------------------
3947  * 
3948  * calculate DU changes
3949  */
3950
3951 void
3952 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
3953 {
3954   Map installedmap;
3955
3956   solver_create_state_maps(solv, &installedmap, 0);
3957   pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
3958   map_free(&installedmap);
3959 }
3960
3961
3962 /*-------------------------------------------------------------------
3963  * 
3964  * calculate changes in install size
3965  */
3966
3967 int
3968 solver_calc_installsizechange(Solver *solv)
3969 {
3970   Map installedmap;
3971   int change;
3972
3973   solver_create_state_maps(solv, &installedmap, 0);
3974   change = pool_calc_installsizechange(solv->pool, &installedmap);
3975   map_free(&installedmap);
3976   return change;
3977 }
3978
3979 void
3980 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
3981 {
3982   pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
3983 }
3984
3985 void
3986 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
3987 {
3988   Pool *pool = solv->pool;
3989   Map installedmap;
3990   int i;
3991   pool_create_state_maps(pool,  &solv->decisionq, &installedmap, 0);
3992   pool_trivial_installable_multiversionmap(pool, &installedmap, pkgs, res, solv->multiversion.size ? &solv->multiversion : 0);
3993   for (i = 0; i < res->count; i++)
3994     if (res->elements[i] != -1)
3995       {
3996         Solvable *s = pool->solvables + pkgs->elements[i];
3997         if (!strncmp("patch:", pool_id2str(pool, s->name), 6) && solvable_is_irrelevant_patch(s, &installedmap))
3998           res->elements[i] = -1;
3999       }
4000   map_free(&installedmap);
4001 }
4002
4003 /*-------------------------------------------------------------------
4004  * 
4005  * decision introspection
4006  */
4007
4008 int
4009 solver_get_decisionlevel(Solver *solv, Id p)
4010 {
4011   return solv->decisionmap[p];
4012 }
4013
4014 void
4015 solver_get_decisionqueue(Solver *solv, Queue *decisionq)
4016 {
4017   queue_free(decisionq);
4018   queue_init_clone(decisionq, &solv->decisionq);
4019 }
4020
4021 int
4022 solver_get_lastdecisionblocklevel(Solver *solv)
4023 {
4024   Id p;
4025   if (solv->decisionq.count == 0)
4026     return 0;
4027   p = solv->decisionq.elements[solv->decisionq.count - 1];
4028   if (p < 0)
4029     p = -p;
4030   return solv->decisionmap[p] < 0 ? -solv->decisionmap[p] : solv->decisionmap[p];
4031 }
4032
4033 void
4034 solver_get_decisionblock(Solver *solv, int level, Queue *decisionq)
4035 {
4036   Id p;
4037   int i;
4038
4039   queue_empty(decisionq);
4040   for (i = 0; i < solv->decisionq.count; i++)
4041     {
4042       p = solv->decisionq.elements[i];
4043       if (p < 0)
4044         p = -p;
4045       if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
4046         break;
4047     }
4048   if (i == solv->decisionq.count)
4049     return;
4050   for (i = 0; i < solv->decisionq.count; i++)
4051     {
4052       p = solv->decisionq.elements[i];
4053       if (p < 0)
4054         p = -p;
4055       if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
4056         queue_push(decisionq, p);
4057       else
4058         break;
4059     }
4060 }
4061
4062 int
4063 solver_describe_decision(Solver *solv, Id p, Id *infop)
4064 {
4065   int i;
4066   Id pp, why;
4067   
4068   if (infop)
4069     *infop = 0;
4070   if (!solv->decisionmap[p])
4071     return SOLVER_REASON_UNRELATED;
4072   pp = solv->decisionmap[p] < 0 ? -p : p;
4073   for (i = 0; i < solv->decisionq.count; i++)
4074     if (solv->decisionq.elements[i] == pp)
4075       break;
4076   if (i == solv->decisionq.count)       /* just in case... */
4077     return SOLVER_REASON_UNRELATED;
4078   why = solv->decisionq_why.elements[i];
4079   if (infop)
4080     *infop = why > 0 ? why : -why;
4081   if (why > 0)
4082     return SOLVER_REASON_UNIT_RULE;
4083   why = -why;
4084   if (i < solv->decisioncnt_update)
4085     {
4086       if (i == 0)
4087         return SOLVER_REASON_KEEP_INSTALLED;
4088       return SOLVER_REASON_RESOLVE_JOB;
4089     }
4090   if (i < solv->decisioncnt_keep)
4091     {
4092       if (why == 0 && pp < 0)
4093         return SOLVER_REASON_CLEANDEPS_ERASE;
4094       return SOLVER_REASON_UPDATE_INSTALLED;
4095     }
4096   if (i < solv->decisioncnt_resolve)
4097     {
4098       if (why == 0 && pp < 0)
4099         return SOLVER_REASON_CLEANDEPS_ERASE;
4100       return SOLVER_REASON_KEEP_INSTALLED;
4101     }
4102   if (why > 0)
4103     return SOLVER_REASON_RESOLVE;
4104   /* weak or orphaned */
4105   if (solv->decisionq.count < solv->decisioncnt_orphan)
4106     return SOLVER_REASON_WEAKDEP;
4107   return SOLVER_REASON_RESOLVE_ORPHAN;
4108 }
4109
4110
4111 void
4112 solver_describe_weakdep_decision(Solver *solv, Id p, Queue *whyq)
4113 {
4114   Pool *pool = solv->pool;
4115   int i;
4116   int level = solv->decisionmap[p];
4117   int decisionno;
4118   Solvable *s;
4119
4120   queue_empty(whyq);
4121   if (level < 0)
4122     return;     /* huh? */
4123   for (decisionno = 0; decisionno < solv->decisionq.count; decisionno++)
4124     if (solv->decisionq.elements[decisionno] == p)
4125       break;
4126   if (decisionno == solv->decisionq.count)
4127     return;     /* huh? */
4128   if (decisionno < solv->decisioncnt_weak || decisionno >= solv->decisioncnt_orphan)
4129     return;     /* huh? */
4130
4131   /* 1) list all packages that recommend us */
4132   for (i = 1; i < pool->nsolvables; i++)
4133     {
4134       Id *recp, rec, pp2, p2;
4135       if (solv->decisionmap[i] < 0 || solv->decisionmap[i] >= level)
4136         continue;
4137       s = pool->solvables + i;
4138       if (!s->recommends)
4139         continue;
4140       if (!solv->addalreadyrecommended && s->repo == solv->installed)
4141         continue;
4142       recp = s->repo->idarraydata + s->recommends;
4143       while ((rec = *recp++) != 0)
4144         {
4145           int found = 0;
4146           FOR_PROVIDES(p2, pp2, rec)
4147             {
4148               if (p2 == p)
4149                 found = 1;
4150               else
4151                 {
4152                   /* if p2 is already installed, this recommends is ignored */
4153                   if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
4154                     break;
4155                 }
4156             }
4157           if (!p2 && found)
4158             {
4159               queue_push(whyq, SOLVER_REASON_RECOMMENDED);
4160               queue_push2(whyq, p2, rec);
4161             }
4162         }
4163     }
4164   /* 2) list all supplements */
4165   s = pool->solvables + p;
4166   if (s->supplements && level > 0)
4167     {
4168       Id *supp, sup, pp2, p2;
4169       /* this is a hack. to use solver_dep_fulfilled we temporarily clear
4170        * everything above our level in the decisionmap */
4171       for (i = decisionno; i < solv->decisionq.count; i++ )
4172         {
4173           p2 = solv->decisionq.elements[i];
4174           if (p2 > 0)
4175             solv->decisionmap[p2] = -solv->decisionmap[p2];
4176         }
4177       supp = s->repo->idarraydata + s->supplements;
4178       while ((sup = *supp++) != 0)
4179         if (solver_dep_fulfilled(solv, sup))
4180           {
4181             int found = 0;
4182             /* let's see if this is an easy supp */
4183             FOR_PROVIDES(p2, pp2, sup)
4184               {
4185                 if (!solv->addalreadyrecommended && solv->installed)
4186                   {
4187                     if (pool->solvables[p2].repo == solv->installed)
4188                       continue;
4189                   }
4190                 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
4191                   {
4192                     queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
4193                     queue_push2(whyq, p2, sup);
4194                     found = 1;
4195                   }
4196               }
4197             if (!found)
4198               {
4199                 /* hard case, just note dependency with no package */
4200                 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
4201                 queue_push2(whyq, 0, sup);
4202               }
4203           }
4204       for (i = decisionno; i < solv->decisionq.count; i++)
4205         {
4206           p2 = solv->decisionq.elements[i];
4207           if (p2 > 0)
4208             solv->decisionmap[p2] = -solv->decisionmap[p2];
4209         }
4210     }
4211 }
4212
4213 void
4214 pool_job2solvables(Pool *pool, Queue *pkgs, Id how, Id what)
4215 {
4216   Id p, pp;
4217   how &= SOLVER_SELECTMASK;
4218   queue_empty(pkgs);
4219   if (how == SOLVER_SOLVABLE_ALL)
4220     {
4221       FOR_POOL_SOLVABLES(p)
4222         queue_push(pkgs, p);
4223     }
4224   else if (how == SOLVER_SOLVABLE_REPO)
4225     {
4226       Repo *repo = pool_id2repo(pool, what);
4227       Solvable *s;
4228       if (repo)
4229         FOR_REPO_SOLVABLES(repo, p, s)
4230           queue_push(pkgs, p);
4231     }
4232   else
4233     {
4234       FOR_JOB_SELECT(p, pp, how, what)
4235         queue_push(pkgs, p);
4236     }
4237 }
4238
4239 int
4240 pool_isemptyupdatejob(Pool *pool, Id how, Id what)
4241 {
4242   Id p, pp, pi, pip;
4243   Id select = how & SOLVER_SELECTMASK;
4244   if ((how & SOLVER_JOBMASK) != SOLVER_UPDATE)
4245     return 0;
4246   if (select == SOLVER_SOLVABLE_ALL || select == SOLVER_SOLVABLE_REPO)
4247     return 0;
4248   if (!pool->installed)
4249     return 1;
4250   FOR_JOB_SELECT(p, pp, select, what)
4251     if (pool->solvables[p].repo == pool->installed)
4252       return 0;
4253   /* hard work */
4254   FOR_JOB_SELECT(p, pp, select, what)
4255     {
4256       Solvable *s = pool->solvables + p;
4257       FOR_PROVIDES(pi, pip, s->name)
4258         {
4259           Solvable *si = pool->solvables + pi;
4260           if (si->repo != pool->installed || si->name != s->name)
4261             continue;
4262           return 0;
4263         }
4264       if (s->obsoletes)
4265         {
4266           Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
4267           while ((obs = *obsp++) != 0)
4268             {
4269               FOR_PROVIDES(pi, pip, obs) 
4270                 {
4271                   Solvable *si = pool->solvables + pi;
4272                   if (si->repo != pool->installed)
4273                     continue;
4274                   if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
4275                     continue;
4276                   if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
4277                     continue;
4278                   return 0;
4279                 }
4280             }
4281         }
4282     }
4283   return 1;
4284 }
4285
4286 const char *
4287 solver_select2str(Pool *pool, Id select, Id what)
4288 {
4289   const char *s;
4290   char *b;
4291   select &= SOLVER_SELECTMASK;
4292   if (select == SOLVER_SOLVABLE)
4293     return pool_solvid2str(pool, what);
4294   if (select == SOLVER_SOLVABLE_NAME)
4295     return pool_dep2str(pool, what);
4296   if (select == SOLVER_SOLVABLE_PROVIDES)
4297     {
4298       s = pool_dep2str(pool, what);
4299       b = pool_alloctmpspace(pool, 11 + strlen(s));
4300       sprintf(b, "providing %s", s);
4301       return b;
4302     }
4303   if (select == SOLVER_SOLVABLE_ONE_OF)
4304     {
4305       Id p;
4306       b = 0;
4307       while ((p = pool->whatprovidesdata[what++]) != 0)
4308         {
4309           s = pool_solvid2str(pool, p);
4310           if (b)
4311             b = pool_tmpappend(pool, b, ", ", s);
4312           else
4313             b = pool_tmpjoin(pool, s, 0, 0);
4314           pool_freetmpspace(pool, s);
4315         }
4316       return b ? b : "nothing";
4317     }
4318   if (select == SOLVER_SOLVABLE_REPO)
4319     {
4320       b = pool_alloctmpspace(pool, 20);
4321       sprintf(b, "repo #%d", what);
4322       return b;
4323     }
4324   if (select == SOLVER_SOLVABLE_ALL)
4325     return "all packages";
4326   return "unknown job select";
4327 }
4328
4329 const char *
4330 pool_job2str(Pool *pool, Id how, Id what, Id flagmask)
4331 {
4332   Id select = how & SOLVER_SELECTMASK;
4333   const char *strstart = 0, *strend = 0;
4334   char *s;
4335   int o;
4336
4337   switch (how & SOLVER_JOBMASK)
4338     {
4339     case SOLVER_NOOP:
4340       return "do nothing";
4341     case SOLVER_INSTALL:
4342       if (select == SOLVER_SOLVABLE && pool->installed && pool->solvables[what].repo == pool->installed)
4343         strstart = "keep ", strend = "installed";
4344       else if (select == SOLVER_SOLVABLE || select == SOLVER_SOLVABLE_NAME)
4345         strstart = "install ";
4346       else if (select == SOLVER_SOLVABLE_PROVIDES)
4347         strstart = "install a package ";
4348       else
4349         strstart = "install one of ";
4350       break;
4351     case SOLVER_ERASE:
4352       if (select == SOLVER_SOLVABLE && !(pool->installed && pool->solvables[what].repo == pool->installed))
4353         strstart = "keep ", strend = "unstalled";
4354       else if (select == SOLVER_SOLVABLE_PROVIDES)
4355         strstart = "deinstall all packages ";
4356       else
4357         strstart = "deinstall ";
4358       break;
4359     case SOLVER_UPDATE:
4360       strstart = "update ";
4361       break;
4362     case SOLVER_WEAKENDEPS:
4363       strstart = "weaken deps of ";
4364       break;
4365     case SOLVER_MULTIVERSION:
4366       strstart = "multi version ";
4367       break;
4368     case SOLVER_LOCK:
4369       strstart = "update ";
4370       break;
4371     case SOLVER_DISTUPGRADE:
4372       strstart = "dist upgrade ";
4373       break;
4374     case SOLVER_VERIFY:
4375       strstart = "verify ";
4376       break;
4377     case SOLVER_DROP_ORPHANED:
4378       strstart = "deinstall ", strend = "if orphaned";
4379       break;
4380     case SOLVER_USERINSTALLED:
4381       strstart = "regard ", strend = "as userinstalled";
4382       break;
4383     default:
4384       strstart = "unknown job ";
4385       break;
4386     }
4387   s = pool_tmpjoin(pool, strstart, solver_select2str(pool, select, what), strend);
4388   how &= flagmask;
4389   if ((how & ~(SOLVER_SELECTMASK|SOLVER_JOBMASK)) == 0)
4390     return s;
4391   o = strlen(s);
4392   s = pool_tmpappend(pool, s, " ", 0);
4393   if (how & SOLVER_WEAK)
4394     s = pool_tmpappend(pool, s, ",weak", 0);
4395   if (how & SOLVER_ESSENTIAL)
4396     s = pool_tmpappend(pool, s, ",essential", 0);
4397   if (how & SOLVER_CLEANDEPS)
4398     s = pool_tmpappend(pool, s, ",cleandeps", 0);
4399   if (how & SOLVER_ORUPDATE)
4400     s = pool_tmpappend(pool, s, ",orupdate", 0);
4401   if (how & SOLVER_FORCEBEST)
4402     s = pool_tmpappend(pool, s, ",forcebest", 0);
4403   if (how & SOLVER_TARGETED)
4404     s = pool_tmpappend(pool, s, ",targeted", 0);
4405   if (how & SOLVER_SETEV)
4406     s = pool_tmpappend(pool, s, ",setev", 0);
4407   if (how & SOLVER_SETEVR)
4408     s = pool_tmpappend(pool, s, ",setevr", 0);
4409   if (how & SOLVER_SETARCH)
4410     s = pool_tmpappend(pool, s, ",setarch", 0);
4411   if (how & SOLVER_SETVENDOR)
4412     s = pool_tmpappend(pool, s, ",setvendor", 0);
4413   if (how & SOLVER_SETREPO)
4414     s = pool_tmpappend(pool, s, ",setrepo", 0);
4415   if (how & SOLVER_SETNAME)
4416     s = pool_tmpappend(pool, s, ",setname", 0);
4417   if (how & SOLVER_NOAUTOSET)
4418     s = pool_tmpappend(pool, s, ",noautoset", 0);
4419   if (s[o + 1] != ',')
4420     s = pool_tmpappend(pool, s, ",?", 0);
4421   s[o + 1] = '[';
4422   return pool_tmpappend(pool, s, "]", 0);
4423 }
4424