fix unbalanced ===
[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
1371 /*-------------------------------------------------------------------
1372  * 
1373  * select and install
1374  * 
1375  * install best package from the queue. We add an extra package, inst, if
1376  * provided. See comment in weak install section.
1377  *
1378  * returns the new solver level or 0 if unsolvable
1379  *
1380  */
1381
1382 static int
1383 selectandinstall(Solver *solv, int level, Queue *dq, int disablerules, Id ruleid)
1384 {
1385   Pool *pool = solv->pool;
1386   Id p;
1387   int i;
1388
1389   if (dq->count > 1)
1390     policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1391   if (dq->count > 1)
1392     {
1393       /* XXX: didn't we already do that? */
1394       /* XXX: shouldn't we prefer installed packages? */
1395       /* XXX: move to policy.c? */
1396       /* choose the supplemented one */
1397       for (i = 0; i < dq->count; i++)
1398         if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1399           {
1400             dq->elements[0] = dq->elements[i];
1401             dq->count = 1;
1402             break;
1403           }
1404     }
1405   if (dq->count > 1)
1406     {
1407       /* multiple candidates, open a branch */
1408       for (i = 1; i < dq->count; i++)
1409         queue_push(&solv->branches, dq->elements[i]);
1410       queue_push(&solv->branches, -level);
1411     }
1412   p = dq->elements[0];
1413
1414   POOL_DEBUG(SOLV_DEBUG_POLICY, "installing %s\n", pool_solvid2str(pool, p));
1415
1416   return setpropagatelearn(solv, level, p, disablerules, ruleid);
1417 }
1418
1419
1420 /********************************************************************/
1421 /* Main solver interface */
1422
1423
1424 /*-------------------------------------------------------------------
1425  * 
1426  * solver_create
1427  * create solver structure
1428  *
1429  * pool: all available solvables
1430  * installed: installed Solvables
1431  *
1432  *
1433  * Upon solving, rules are created to flag the Solvables
1434  * of the 'installed' Repo as installed.
1435  */
1436
1437 Solver *
1438 solver_create(Pool *pool)
1439 {
1440   Solver *solv;
1441   solv = (Solver *)solv_calloc(1, sizeof(Solver));
1442   solv->pool = pool;
1443   solv->installed = pool->installed;
1444
1445   solv->allownamechange = 1;
1446
1447   solv->dup_allowdowngrade = 1;
1448   solv->dup_allownamechange = 1;
1449   solv->dup_allowarchchange = 1;
1450   solv->dup_allowvendorchange = 1;
1451
1452   solv->keepexplicitobsoletes = pool->noobsoletesmultiversion ? 0 : 1;
1453
1454   queue_init(&solv->ruletojob);
1455   queue_init(&solv->decisionq);
1456   queue_init(&solv->decisionq_why);
1457   queue_init(&solv->problems);
1458   queue_init(&solv->orphaned);
1459   queue_init(&solv->learnt_why);
1460   queue_init(&solv->learnt_pool);
1461   queue_init(&solv->branches);
1462   queue_init(&solv->weakruleq);
1463   queue_init(&solv->ruleassertions);
1464   queue_init(&solv->addedmap_deduceq);
1465
1466   queue_push(&solv->learnt_pool, 0);    /* so that 0 does not describe a proof */
1467
1468   map_init(&solv->recommendsmap, pool->nsolvables);
1469   map_init(&solv->suggestsmap, pool->nsolvables);
1470   map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1471   solv->recommends_index = 0;
1472
1473   solv->decisionmap = (Id *)solv_calloc(pool->nsolvables, sizeof(Id));
1474   solv->nrules = 1;
1475   solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1476   memset(solv->rules, 0, sizeof(Rule));
1477
1478   return solv;
1479 }
1480
1481
1482 /*-------------------------------------------------------------------
1483  * 
1484  * solver_free
1485  */
1486
1487 void
1488 solver_free(Solver *solv)
1489 {
1490   queue_free(&solv->job);
1491   queue_free(&solv->ruletojob);
1492   queue_free(&solv->decisionq);
1493   queue_free(&solv->decisionq_why);
1494   queue_free(&solv->learnt_why);
1495   queue_free(&solv->learnt_pool);
1496   queue_free(&solv->problems);
1497   queue_free(&solv->solutions);
1498   queue_free(&solv->orphaned);
1499   queue_free(&solv->branches);
1500   queue_free(&solv->weakruleq);
1501   queue_free(&solv->ruleassertions);
1502   queue_free(&solv->addedmap_deduceq);
1503   if (solv->cleandeps_updatepkgs)
1504     {
1505       queue_free(solv->cleandeps_updatepkgs);
1506       solv->cleandeps_updatepkgs = solv_free(solv->cleandeps_updatepkgs);
1507     }
1508   if (solv->cleandeps_mistakes)
1509     {
1510       queue_free(solv->cleandeps_mistakes);
1511       solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
1512     }
1513   if (solv->update_targets)
1514     {
1515       queue_free(solv->update_targets);
1516       solv->update_targets = solv_free(solv->update_targets);
1517     }
1518   if (solv->installsuppdepq)
1519     {
1520       queue_free(solv->installsuppdepq);
1521       solv->installsuppdepq = solv_free(solv->installsuppdepq);
1522     }
1523
1524   map_free(&solv->recommendsmap);
1525   map_free(&solv->suggestsmap);
1526   map_free(&solv->noupdate);
1527   map_free(&solv->weakrulemap);
1528   map_free(&solv->multiversion);
1529
1530   map_free(&solv->updatemap);
1531   map_free(&solv->bestupdatemap);
1532   map_free(&solv->fixmap);
1533   map_free(&solv->dupmap);
1534   map_free(&solv->dupinvolvedmap);
1535   map_free(&solv->droporphanedmap);
1536   map_free(&solv->cleandepsmap);
1537
1538   solv_free(solv->decisionmap);
1539   solv_free(solv->rules);
1540   solv_free(solv->watches);
1541   solv_free(solv->obsoletes);
1542   solv_free(solv->obsoletes_data);
1543   solv_free(solv->multiversionupdaters);
1544   solv_free(solv->choicerules_ref);
1545   solv_free(solv->bestrules_pkg);
1546   solv_free(solv);
1547 }
1548
1549 int
1550 solver_get_flag(Solver *solv, int flag)
1551 {
1552   switch (flag)
1553   {
1554   case SOLVER_FLAG_ALLOW_DOWNGRADE:
1555     return solv->allowdowngrade;
1556   case SOLVER_FLAG_ALLOW_NAMECHANGE:
1557     return solv->allownamechange;
1558   case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1559     return solv->allowarchchange;
1560   case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1561     return solv->allowvendorchange;
1562   case SOLVER_FLAG_ALLOW_UNINSTALL:
1563     return solv->allowuninstall;
1564   case SOLVER_FLAG_NO_UPDATEPROVIDE:
1565     return solv->noupdateprovide;
1566   case SOLVER_FLAG_SPLITPROVIDES:
1567     return solv->dosplitprovides;
1568   case SOLVER_FLAG_IGNORE_RECOMMENDED:
1569     return solv->dontinstallrecommended;
1570   case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1571     return solv->addalreadyrecommended;
1572   case SOLVER_FLAG_NO_INFARCHCHECK:
1573     return solv->noinfarchcheck;
1574   case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1575     return solv->keepexplicitobsoletes;
1576   case SOLVER_FLAG_BEST_OBEY_POLICY:
1577     return solv->bestobeypolicy;
1578   case SOLVER_FLAG_NO_AUTOTARGET:
1579     return solv->noautotarget;
1580   default:
1581     break;
1582   }
1583   return -1;
1584 }
1585
1586 int
1587 solver_set_flag(Solver *solv, int flag, int value)
1588 {
1589   int old = solver_get_flag(solv, flag);
1590   switch (flag)
1591   {
1592   case SOLVER_FLAG_ALLOW_DOWNGRADE:
1593     solv->allowdowngrade = value;
1594     break;
1595   case SOLVER_FLAG_ALLOW_NAMECHANGE:
1596     solv->allownamechange = value;
1597     break;
1598   case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1599     solv->allowarchchange = value;
1600     break;
1601   case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1602     solv->allowvendorchange = value;
1603     break;
1604   case SOLVER_FLAG_ALLOW_UNINSTALL:
1605     solv->allowuninstall = value;
1606     break;
1607   case SOLVER_FLAG_NO_UPDATEPROVIDE:
1608     solv->noupdateprovide = value;
1609     break;
1610   case SOLVER_FLAG_SPLITPROVIDES:
1611     solv->dosplitprovides = value;
1612     break;
1613   case SOLVER_FLAG_IGNORE_RECOMMENDED:
1614     solv->dontinstallrecommended = value;
1615     break;
1616   case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1617     solv->addalreadyrecommended = value;
1618     break;
1619   case SOLVER_FLAG_NO_INFARCHCHECK:
1620     solv->noinfarchcheck = value;
1621     break;
1622   case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1623     solv->keepexplicitobsoletes = value;
1624     break;
1625   case SOLVER_FLAG_BEST_OBEY_POLICY:
1626     solv->bestobeypolicy = value;
1627     break;
1628   case SOLVER_FLAG_NO_AUTOTARGET:
1629     solv->noautotarget = value;
1630     break;
1631   default:
1632     break;
1633   }
1634   return old;
1635 }
1636
1637 int
1638 cleandeps_check_mistakes(Solver *solv, int level)
1639 {
1640   Pool *pool = solv->pool;
1641   Rule *r;
1642   Id p, pp;
1643   int i;
1644   int mademistake = 0;
1645
1646   if (!solv->cleandepsmap.size)
1647     return 0;
1648   /* check for mistakes */
1649   for (i = solv->installed->start; i < solv->installed->end; i++)
1650     {
1651       if (!MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1652         continue;
1653       r = solv->rules + solv->featurerules + (i - solv->installed->start);
1654       /* a mistake is when the featurerule is true but the updaterule is false */
1655       if (!r->p)
1656         continue;
1657       FOR_RULELITERALS(p, pp, r)
1658         if (p > 0 && solv->decisionmap[p] > 0)
1659           break;
1660       if (!p)
1661         continue;       /* feature rule is not true */
1662       r = solv->rules + solv->updaterules + (i - solv->installed->start);
1663       if (!r->p)
1664         continue;
1665       FOR_RULELITERALS(p, pp, r)
1666         if (p > 0 && solv->decisionmap[p] > 0)
1667           break;
1668       if (p)
1669         continue;       /* update rule is true */
1670       POOL_DEBUG(SOLV_DEBUG_SOLVER, "cleandeps mistake: ");
1671       solver_printruleclass(solv, SOLV_DEBUG_SOLVER, r);
1672       POOL_DEBUG(SOLV_DEBUG_SOLVER, "feature rule: ");
1673       solver_printruleclass(solv, SOLV_DEBUG_SOLVER, solv->rules + solv->featurerules + (i - solv->installed->start));
1674       if (!solv->cleandeps_mistakes)
1675         {
1676           solv->cleandeps_mistakes = solv_calloc(1, sizeof(Queue));
1677           queue_init(solv->cleandeps_mistakes);
1678         }
1679       queue_push(solv->cleandeps_mistakes, i);
1680       MAPCLR(&solv->cleandepsmap, i - solv->installed->start);
1681       solver_reenablepolicyrules_cleandeps(solv, i);
1682       mademistake = 1;
1683     }
1684   if (mademistake)
1685     solver_reset(solv);
1686   return mademistake;
1687 }
1688
1689 static void
1690 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
1691 {
1692   int i, j;
1693   Id p, *cp2;
1694   for (i = j = 0; i < q->count; i++)
1695     {
1696       p = q->elements[i];
1697       for (cp2 = cp; *cp2; cp2++)
1698         if (*cp2 == p)
1699           {
1700             q->elements[j++] = p;
1701             break;
1702           }
1703     }
1704   queue_truncate(q, j);
1705 }
1706
1707 /*-------------------------------------------------------------------
1708  * 
1709  * solver_run_sat
1710  *
1711  * all rules have been set up, now actually run the solver
1712  *
1713  */
1714
1715 void
1716 solver_run_sat(Solver *solv, int disablerules, int doweak)
1717 {
1718   Queue dq;             /* local decisionqueue */
1719   Queue dqs;            /* local decisionqueue for supplements */
1720   int systemlevel;
1721   int level, olevel;
1722   Rule *r;
1723   int i, j, n;
1724   Solvable *s;
1725   Pool *pool = solv->pool;
1726   Id p, pp, *dp;
1727   int minimizationsteps;
1728   int installedpos = solv->installed ? solv->installed->start : 0;
1729
1730   IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
1731     {
1732       POOL_DEBUG (SOLV_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1733       for (i = 1; i < solv->nrules; i++)
1734         solver_printruleclass(solv, SOLV_DEBUG_RULE_CREATION, solv->rules + i);
1735     }
1736
1737   POOL_DEBUG(SOLV_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1738
1739   /* start SAT algorithm */
1740   level = 1;
1741   systemlevel = level + 1;
1742   POOL_DEBUG(SOLV_DEBUG_SOLVER, "solving...\n");
1743
1744   queue_init(&dq);
1745   queue_init(&dqs);
1746
1747   /*
1748    * here's the main loop:
1749    * 1) propagate new decisions (only needed once)
1750    * 2) fulfill jobs
1751    * 3) try to keep installed packages
1752    * 4) fulfill all unresolved rules
1753    * 5) install recommended packages
1754    * 6) minimalize solution if we had choices
1755    * if we encounter a problem, we rewind to a safe level and restart
1756    * with step 1
1757    */
1758    
1759   minimizationsteps = 0;
1760   for (;;)
1761     {
1762       /*
1763        * initial propagation of the assertions
1764        */
1765       if (level == 1)
1766         {
1767           POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagating (propagate_index: %d;  size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1768           if ((r = propagate(solv, level)) != 0)
1769             {
1770               if (analyze_unsolvable(solv, r, disablerules))
1771                 continue;
1772               level = 0;
1773               break;    /* unsolvable */
1774             }
1775         }
1776
1777       /*
1778        * resolve jobs first
1779        */
1780      if (level < systemlevel)
1781         {
1782           POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving job rules\n");
1783           for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1784             {
1785               Id l;
1786               if (r->d < 0)             /* ignore disabled rules */
1787                 continue;
1788               queue_empty(&dq);
1789               FOR_RULELITERALS(l, pp, r)
1790                 {
1791                   if (l < 0)
1792                     {
1793                       if (solv->decisionmap[-l] <= 0)
1794                         break;
1795                     }
1796                   else
1797                     {
1798                       if (solv->decisionmap[l] > 0)
1799                         break;
1800                       if (solv->decisionmap[l] == 0)
1801                         queue_push(&dq, l);
1802                     }
1803                 }
1804               if (l || !dq.count)
1805                 continue;
1806               /* prune to installed if not updating */
1807               if (dq.count > 1 && solv->installed && !solv->updatemap_all &&
1808                   !(solv->job.elements[solv->ruletojob.elements[i - solv->jobrules]] & SOLVER_ORUPDATE))
1809                 {
1810                   int j, k;
1811                   for (j = k = 0; j < dq.count; j++)
1812                     {
1813                       Solvable *s = pool->solvables + dq.elements[j];
1814                       if (s->repo == solv->installed)
1815                         {
1816                           dq.elements[k++] = dq.elements[j];
1817                           if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
1818                             {
1819                               k = 0;    /* package wants to be updated, do not prune */
1820                               break;
1821                             }
1822                         }
1823                     }
1824                   if (k)
1825                     dq.count = k;
1826                 }
1827               olevel = level;
1828               level = selectandinstall(solv, level, &dq, disablerules, i);
1829               if (level == 0)
1830                 break;
1831               if (level <= olevel)
1832                 break;
1833             }
1834           if (level == 0)
1835             break;      /* unsolvable */
1836           systemlevel = level + 1;
1837           if (i < solv->jobrules_end)
1838             continue;
1839           if (!solv->decisioncnt_update)
1840             solv->decisioncnt_update = solv->decisionq.count;
1841         }
1842
1843       /*
1844        * installed packages
1845        */
1846       if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1847         {
1848           Repo *installed = solv->installed;
1849           int pass;
1850
1851           POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving installed packages\n");
1852           /* we use two passes if we need to update packages 
1853            * to create a better user experience */
1854           for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1855             {
1856               int passlevel = level;
1857               Id *multiversionupdaters = solv->multiversion.size ? solv->multiversionupdaters : 0;
1858               if (pass == 1 && !solv->decisioncnt_keep)
1859                 solv->decisioncnt_keep = solv->decisionq.count;
1860               /* start with installedpos, the position that gave us problems the last time */
1861               for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1862                 {
1863                   Rule *rr;
1864                   Id d;
1865
1866                   if (i == installed->end)
1867                     i = installed->start;
1868                   s = pool->solvables + i;
1869                   if (s->repo != installed)
1870                     continue;
1871
1872                   if (solv->decisionmap[i] > 0 && (!multiversionupdaters || !multiversionupdaters[i - installed->start]))
1873                     continue;           /* already decided */
1874                   if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1875                     continue;           /* updates first */
1876                   r = solv->rules + solv->updaterules + (i - installed->start);
1877                   rr = r;
1878                   if (!rr->p || rr->d < 0)      /* disabled -> look at feature rule */
1879                     rr -= solv->installed->end - solv->installed->start;
1880                   if (!rr->p)           /* identical to update rule? */
1881                     rr = r;
1882                   if (!rr->p)
1883                     continue;           /* orpaned package */
1884
1885                   /* check if we should update this package to the latest version
1886                    * noupdate is set for erase jobs, in that case we want to deinstall
1887                    * the installed package and not replace it with a newer version */
1888                   queue_empty(&dq);
1889                   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))
1890                     {
1891                       if (multiversionupdaters && (d = multiversionupdaters[i - installed->start]) != 0)
1892                         {
1893                           /* special multiversion handling, make sure best version is chosen */
1894                           if (rr->p == i && solv->decisionmap[i] >= 0)
1895                             queue_push(&dq, i);
1896                           while ((p = pool->whatprovidesdata[d++]) != 0)
1897                             if (solv->decisionmap[p] >= 0)
1898                               queue_push(&dq, p);
1899                           if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1900                             prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
1901                           if (dq.count)
1902                             {
1903                               policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1904                               p = dq.elements[0];
1905                               if (p != i && solv->decisionmap[p] == 0)
1906                                 {
1907                                   rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1908                                   if (!rr->p)           /* update rule == feature rule? */
1909                                     rr = rr - solv->featurerules + solv->updaterules;
1910                                   dq.count = 1;
1911                                 }
1912                               else
1913                                 dq.count = 0;
1914                             }
1915                         }
1916                       else
1917                         {
1918                           /* update to best package of the update rule */
1919                           FOR_RULELITERALS(p, pp, rr)
1920                             {
1921                               if (solv->decisionmap[p] > 0)
1922                                 {
1923                                   dq.count = 0;         /* already fulfilled */
1924                                   break;
1925                                 }
1926                               if (!solv->decisionmap[p])
1927                                 queue_push(&dq, p);
1928                             }
1929                         }
1930                     }
1931                   if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1932                     prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
1933                   /* install best version */
1934                   if (dq.count)
1935                     {
1936                       olevel = level;
1937                       level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1938                       if (level == 0)
1939                         {
1940                           queue_free(&dq);
1941                           queue_free(&dqs);
1942                           return;
1943                         }
1944                       if (level <= olevel)
1945                         {
1946                           if (level == 1 || level < passlevel)
1947                             break;      /* trouble */
1948                           if (level < olevel)
1949                             n = installed->start;       /* redo all */
1950                           i--;
1951                           n--;
1952                           continue;
1953                         }
1954                     }
1955                   /* if still undecided keep package */
1956                   if (solv->decisionmap[i] == 0)
1957                     {
1958                       olevel = level;
1959                       if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
1960                         {
1961 #if 0
1962                           POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
1963                           level = setpropagatelearn(solv, level, -i, disablerules, 0);
1964 #else
1965                           continue;
1966 #endif
1967                         }
1968                       else
1969                         {
1970                           POOL_DEBUG(SOLV_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
1971                           level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1972                         }
1973                       if (level == 0)
1974                         break;
1975                       if (level <= olevel)
1976                         {
1977                           if (level == 1 || level < passlevel)
1978                             break;      /* trouble */
1979                           if (level < olevel)
1980                             n = installed->start;       /* redo all */
1981                           i--;
1982                           n--;
1983                           continue;     /* retry with learnt rule */
1984                         }
1985                     }
1986                 }
1987               if (n < installed->end)
1988                 {
1989                   installedpos = i;     /* retry problem solvable next time */
1990                   break;                /* ran into trouble */
1991                 }
1992               installedpos = installed->start;  /* reset installedpos */
1993             }
1994           if (level == 0)
1995             break;              /* unsolvable */
1996           systemlevel = level + 1;
1997           if (pass < 2)
1998             continue;           /* had trouble, retry */
1999         }
2000       if (!solv->decisioncnt_keep)
2001         solv->decisioncnt_keep = solv->decisionq.count;
2002
2003       if (level < systemlevel)
2004         systemlevel = level;
2005
2006       /*
2007        * decide
2008        */
2009       if (!solv->decisioncnt_resolve)
2010         solv->decisioncnt_resolve = solv->decisionq.count;
2011       POOL_DEBUG(SOLV_DEBUG_POLICY, "deciding unresolved rules\n");
2012       for (i = 1, n = 1; n < solv->nrules; i++, n++)
2013         {
2014           if (i == solv->nrules)
2015             i = 1;
2016           r = solv->rules + i;
2017           if (r->d < 0)         /* ignore disabled rules */
2018             continue;
2019           queue_empty(&dq);
2020           if (r->d == 0)
2021             {
2022               /* binary or unary rule */
2023               /* need two positive undecided literals */
2024               if (r->p < 0 || r->w2 <= 0)
2025                 continue;
2026               if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2027                 continue;
2028               queue_push(&dq, r->p);
2029               queue_push(&dq, r->w2);
2030             }
2031           else
2032             {
2033               /* make sure that
2034                * all negative literals are installed
2035                * no positive literal is installed
2036                * i.e. the rule is not fulfilled and we
2037                * just need to decide on the positive literals
2038                */
2039               if (r->p < 0)
2040                 {
2041                   if (solv->decisionmap[-r->p] <= 0)
2042                     continue;
2043                 }
2044               else
2045                 {
2046                   if (solv->decisionmap[r->p] > 0)
2047                     continue;
2048                   if (solv->decisionmap[r->p] == 0)
2049                     queue_push(&dq, r->p);
2050                 }
2051               dp = pool->whatprovidesdata + r->d;
2052               while ((p = *dp++) != 0)
2053                 {
2054                   if (p < 0)
2055                     {
2056                       if (solv->decisionmap[-p] <= 0)
2057                         break;
2058                     }
2059                   else
2060                     {
2061                       if (solv->decisionmap[p] > 0)
2062                         break;
2063                       if (solv->decisionmap[p] == 0)
2064                         queue_push(&dq, p);
2065                     }
2066                 }
2067               if (p)
2068                 continue;
2069             }
2070           IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
2071             {
2072               POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "unfulfilled ");
2073               solver_printruleclass(solv, SOLV_DEBUG_PROPAGATE, r);
2074             }
2075           /* dq.count < 2 cannot happen as this means that
2076            * the rule is unit */
2077           assert(dq.count > 1);
2078
2079           /* prune to cleandeps packages */
2080           if (solv->cleandepsmap.size && solv->installed)
2081             {
2082               Repo *installed = solv->installed;
2083               for (j = 0; j < dq.count; j++)
2084                 if (pool->solvables[dq.elements[j]].repo == installed && MAPTST(&solv->cleandepsmap, dq.elements[j] - installed->start))
2085                   break;
2086               if (j < dq.count)
2087                 {
2088                   dq.elements[0] = dq.elements[j];
2089                   queue_truncate(&dq, 1);
2090                 }
2091             }
2092
2093           olevel = level;
2094           level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
2095           if (level == 0)
2096             break;              /* unsolvable */
2097           if (level < systemlevel || level == 1)
2098             break;              /* trouble */
2099           /* something changed, so look at all rules again */
2100           n = 0;
2101         }
2102
2103       if (n != solv->nrules)    /* ran into trouble? */
2104         {
2105           if (level == 0)
2106             break;              /* unsolvable */
2107           continue;             /* start over */
2108         }
2109
2110       /* at this point we have a consistent system. now do the extras... */
2111
2112       /* first decide leftover cleandeps packages */
2113       if (solv->cleandepsmap.size && solv->installed)
2114         {
2115           for (p = solv->installed->start; p < solv->installed->end; p++)
2116             {
2117               s = pool->solvables + p;
2118               if (s->repo != solv->installed)
2119                 continue;
2120               if (solv->decisionmap[p] == 0 && MAPTST(&solv->cleandepsmap, p - solv->installed->start))
2121                 {
2122                   POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, p));
2123                   olevel = level;
2124                   level = setpropagatelearn(solv, level, -p, 0, 0);
2125                   if (level < olevel)
2126                     break;
2127                 }
2128             }
2129           if (p < solv->installed->end)
2130             continue;
2131         }
2132
2133       if (!solv->decisioncnt_weak)
2134         solv->decisioncnt_weak = solv->decisionq.count;
2135       if (doweak)
2136         {
2137           int qcount;
2138
2139           POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended packages\n");
2140           queue_empty(&dq);     /* recommended packages */
2141           queue_empty(&dqs);    /* supplemented packages */
2142           for (i = 1; i < pool->nsolvables; i++)
2143             {
2144               if (solv->decisionmap[i] < 0)
2145                 continue;
2146               if (solv->decisionmap[i] > 0)
2147                 {
2148                   /* installed, check for recommends */
2149                   Id *recp, rec, pp, p;
2150                   s = pool->solvables + i;
2151                   if (!solv->addalreadyrecommended && s->repo == solv->installed)
2152                     continue;
2153                   /* XXX need to special case AND ? */
2154                   if (s->recommends)
2155                     {
2156                       recp = s->repo->idarraydata + s->recommends;
2157                       while ((rec = *recp++) != 0)
2158                         {
2159                           qcount = dq.count;
2160                           FOR_PROVIDES(p, pp, rec)
2161                             {
2162                               if (solv->decisionmap[p] > 0)
2163                                 {
2164                                   dq.count = qcount;
2165                                   break;
2166                                 }
2167                               else if (solv->decisionmap[p] == 0)
2168                                 {
2169                                   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))))
2170                                     continue;
2171                                   queue_pushunique(&dq, p);
2172                                 }
2173                             }
2174                         }
2175                     }
2176                 }
2177               else
2178                 {
2179                   s = pool->solvables + i;
2180                   if (!s->supplements)
2181                     continue;
2182                   if (!pool_installable(pool, s))
2183                     continue;
2184                   if (!solver_is_supplementing(solv, s))
2185                     continue;
2186                   if (solv->dupmap_all && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
2187                     continue;
2188                   queue_push(&dqs, i);
2189                 }
2190             }
2191
2192           /* filter out all packages obsoleted by installed packages */
2193           /* this is no longer needed if we have reverse obsoletes */
2194           if ((dqs.count || dq.count) && solv->installed)
2195             {
2196               Map obsmap;
2197               Id obs, *obsp, po, ppo;
2198
2199               map_init(&obsmap, pool->nsolvables);
2200               for (p = solv->installed->start; p < solv->installed->end; p++)
2201                 {
2202                   s = pool->solvables + p;
2203                   if (s->repo != solv->installed || !s->obsoletes)
2204                     continue;
2205                   if (solv->decisionmap[p] <= 0)
2206                     continue;
2207                   if (solv->multiversion.size && MAPTST(&solv->multiversion, p))
2208                     continue;
2209                   obsp = s->repo->idarraydata + s->obsoletes;
2210                   /* foreach obsoletes */
2211                   while ((obs = *obsp++) != 0)
2212                     FOR_PROVIDES(po, ppo, obs)
2213                       MAPSET(&obsmap, po);
2214                 }
2215               for (i = j = 0; i < dqs.count; i++)
2216                 if (!MAPTST(&obsmap, dqs.elements[i]))
2217                   dqs.elements[j++] = dqs.elements[i];
2218               dqs.count = j;
2219               for (i = j = 0; i < dq.count; i++)
2220                 if (!MAPTST(&obsmap, dq.elements[i]))
2221                   dq.elements[j++] = dq.elements[i];
2222               dq.count = j;
2223               map_free(&obsmap);
2224             }
2225
2226           /* filter out all already supplemented packages if requested */
2227           if (!solv->addalreadyrecommended && dqs.count)
2228             {
2229               int dosplitprovides_old = solv->dosplitprovides;
2230               /* turn off all new packages */
2231               for (i = 0; i < solv->decisionq.count; i++)
2232                 {
2233                   p = solv->decisionq.elements[i];
2234                   if (p < 0)
2235                     continue;
2236                   s = pool->solvables + p;
2237                   if (s->repo && s->repo != solv->installed)
2238                     solv->decisionmap[p] = -solv->decisionmap[p];
2239                 }
2240               solv->dosplitprovides = 0;
2241               /* filter out old supplements */
2242               for (i = j = 0; i < dqs.count; i++)
2243                 {
2244                   p = dqs.elements[i];
2245                   s = pool->solvables + p;
2246                   if (!s->supplements)
2247                     continue;
2248                   if (!solver_is_supplementing(solv, s))
2249                     dqs.elements[j++] = p;
2250                   else if (solv->installsuppdepq && solver_check_installsuppdepq(solv, s))
2251                     dqs.elements[j++] = p;
2252                 }
2253               dqs.count = j;
2254               /* undo turning off */
2255               for (i = 0; i < solv->decisionq.count; i++)
2256                 {
2257                   p = solv->decisionq.elements[i];
2258                   if (p < 0)
2259                     continue;
2260                   s = pool->solvables + p;
2261                   if (s->repo && s->repo != solv->installed)
2262                     solv->decisionmap[p] = -solv->decisionmap[p];
2263                 }
2264               solv->dosplitprovides = dosplitprovides_old;
2265             }
2266
2267           /* multiversion doesn't mix well with supplements.
2268            * filter supplemented packages where we already decided
2269            * to install a different version (see bnc#501088) */
2270           if (dqs.count && solv->multiversion.size)
2271             {
2272               for (i = j = 0; i < dqs.count; i++)
2273                 {
2274                   p = dqs.elements[i];
2275                   if (MAPTST(&solv->multiversion, p))
2276                     {
2277                       Id p2, pp2;
2278                       s = pool->solvables + p;
2279                       FOR_PROVIDES(p2, pp2, s->name)
2280                         if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
2281                           break;
2282                       if (p2)
2283                         continue;       /* ignore this package */
2284                     }
2285                   dqs.elements[j++] = p;
2286                 }
2287               dqs.count = j;
2288             }
2289
2290           /* make dq contain both recommended and supplemented pkgs */
2291           if (dqs.count)
2292             {
2293               for (i = 0; i < dqs.count; i++)
2294                 queue_pushunique(&dq, dqs.elements[i]);
2295             }
2296
2297           if (dq.count)
2298             {
2299               Map dqmap;
2300               int decisioncount = solv->decisionq.count;
2301
2302               if (dq.count == 1)
2303                 {
2304                   /* simple case, just one package. no need to choose to best version */
2305                   p = dq.elements[0];
2306                   if (dqs.count)
2307                     POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2308                   else
2309                     POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2310                   level = setpropagatelearn(solv, level, p, 0, 0);
2311                   if (level == 0)
2312                     break;
2313                   continue;     /* back to main loop */
2314                 }
2315
2316               /* filter packages, this gives us the best versions */
2317               policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
2318
2319               /* create map of result */
2320               map_init(&dqmap, pool->nsolvables);
2321               for (i = 0; i < dq.count; i++)
2322                 MAPSET(&dqmap, dq.elements[i]);
2323
2324               /* install all supplemented packages */
2325               for (i = 0; i < dqs.count; i++)
2326                 {
2327                   p = dqs.elements[i];
2328                   if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
2329                     continue;
2330                   POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2331                   olevel = level;
2332                   level = setpropagatelearn(solv, level, p, 0, 0);
2333                   if (level <= olevel)
2334                     break;
2335                 }
2336               if (i < dqs.count || solv->decisionq.count < decisioncount)
2337                 {
2338                   map_free(&dqmap);
2339                   if (level == 0)
2340                     break;
2341                   continue;
2342                 }
2343
2344               /* install all recommended packages */
2345               /* more work as we want to created branches if multiple
2346                * choices are valid */
2347               for (i = 0; i < decisioncount; i++)
2348                 {
2349                   Id rec, *recp, pp;
2350                   p = solv->decisionq.elements[i];
2351                   if (p < 0)
2352                     continue;
2353                   s = pool->solvables + p;
2354                   if (!s->repo || (!solv->addalreadyrecommended && s->repo == solv->installed))
2355                     continue;
2356                   if (!s->recommends)
2357                     continue;
2358                   recp = s->repo->idarraydata + s->recommends;
2359                   while ((rec = *recp++) != 0)
2360                     {
2361                       queue_empty(&dq);
2362                       FOR_PROVIDES(p, pp, rec)
2363                         {
2364                           if (solv->decisionmap[p] > 0)
2365                             {
2366                               dq.count = 0;
2367                               break;
2368                             }
2369                           else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
2370                             queue_pushunique(&dq, p);
2371                         }
2372                       if (!dq.count)
2373                         continue;
2374                       if (dq.count > 1)
2375                         {
2376                           /* multiple candidates, open a branch */
2377                           for (i = 1; i < dq.count; i++)
2378                             queue_push(&solv->branches, dq.elements[i]);
2379                           queue_push(&solv->branches, -level);
2380                         }
2381                       p = dq.elements[0];
2382                       POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2383                       olevel = level;
2384                       level = setpropagatelearn(solv, level, p, 0, 0);
2385                       if (level <= olevel || solv->decisionq.count < decisioncount)
2386                         break;  /* we had to revert some decisions */
2387                     }
2388                   if (rec)
2389                     break;      /* had a problem above, quit loop */
2390                 }
2391               map_free(&dqmap);
2392               if (level == 0)
2393                 break;
2394               continue;         /* back to main loop so that all deps are checked */
2395             }
2396         }
2397
2398       if (!solv->decisioncnt_orphan)
2399         solv->decisioncnt_orphan = solv->decisionq.count;
2400       if (solv->dupmap_all && solv->installed)
2401         {
2402           int installedone = 0;
2403
2404           /* let's see if we can install some unsupported package */
2405           POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding orphaned packages\n");
2406           for (i = 0; i < solv->orphaned.count; i++)
2407             {
2408               p = solv->orphaned.elements[i];
2409               if (solv->decisionmap[p])
2410                 continue;       /* already decided */
2411               if (solv->droporphanedmap_all)
2412                 continue;
2413               if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
2414                 continue;
2415               POOL_DEBUG(SOLV_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
2416               olevel = level;
2417               level = setpropagatelearn(solv, level, p, 0, 0);
2418               installedone = 1;
2419               if (level < olevel)
2420                 break;
2421             }
2422           if (installedone || i < solv->orphaned.count)
2423             {
2424               if (level == 0)
2425                 break;
2426               continue;         /* back to main loop */
2427             }
2428           for (i = 0; i < solv->orphaned.count; i++)
2429             {
2430               p = solv->orphaned.elements[i];
2431               if (solv->decisionmap[p])
2432                 continue;       /* already decided */
2433               POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2434               olevel = level;
2435               level = setpropagatelearn(solv, level, -p, 0, 0);
2436               if (level < olevel)
2437                 break;
2438             }
2439           if (i < solv->orphaned.count)
2440             {
2441               if (level == 0)
2442                 break;
2443               continue;         /* back to main loop */
2444             }
2445         }
2446
2447      if (solv->installed && solv->cleandepsmap.size)
2448         {
2449           if (cleandeps_check_mistakes(solv, level))
2450             {
2451               level = 1;        /* restart from scratch */
2452               systemlevel = level + 1;
2453               continue;
2454             }
2455         }
2456
2457      if (solv->solution_callback)
2458         {
2459           solv->solution_callback(solv, solv->solution_callback_data);
2460           if (solv->branches.count)
2461             {
2462               int i = solv->branches.count - 1;
2463               int l = -solv->branches.elements[i];
2464               Id why;
2465
2466               for (; i > 0; i--)
2467                 if (solv->branches.elements[i - 1] < 0)
2468                   break;
2469               p = solv->branches.elements[i];
2470               POOL_DEBUG(SOLV_DEBUG_SOLVER, "branching with %s\n", pool_solvid2str(pool, p));
2471               queue_empty(&dq);
2472               for (j = i + 1; j < solv->branches.count; j++)
2473                 queue_push(&dq, solv->branches.elements[j]);
2474               solv->branches.count = i;
2475               level = l;
2476               revert(solv, level);
2477               if (dq.count > 1)
2478                 for (j = 0; j < dq.count; j++)
2479                   queue_push(&solv->branches, dq.elements[j]);
2480               olevel = level;
2481               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2482               assert(why >= 0);
2483               level = setpropagatelearn(solv, level, p, disablerules, why);
2484               if (level == 0)
2485                 break;
2486               continue;
2487             }
2488           /* all branches done, we're finally finished */
2489           break;
2490         }
2491
2492       /* auto-minimization step */
2493      if (solv->branches.count)
2494         {
2495           int l = 0, lasti = -1, lastl = -1;
2496           Id why;
2497
2498           p = 0;
2499           for (i = solv->branches.count - 1; i >= 0; i--)
2500             {
2501               p = solv->branches.elements[i];
2502               if (p < 0)
2503                 l = -p;
2504               else if (p > 0 && solv->decisionmap[p] > l + 1)
2505                 {
2506                   lasti = i;
2507                   lastl = l;
2508                 }
2509             }
2510           if (lasti >= 0)
2511             {
2512               /* kill old solvable so that we do not loop */
2513               p = solv->branches.elements[lasti];
2514               solv->branches.elements[lasti] = 0;
2515               POOL_DEBUG(SOLV_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, pool_solvid2str(pool, p));
2516               minimizationsteps++;
2517
2518               level = lastl;
2519               revert(solv, level);
2520               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2521               assert(why >= 0);
2522               olevel = level;
2523               level = setpropagatelearn(solv, level, p, disablerules, why);
2524               if (level == 0)
2525                 break;
2526               continue;         /* back to main loop */
2527             }
2528         }
2529       /* no minimization found, we're finally finished! */
2530       break;
2531     }
2532
2533   POOL_DEBUG(SOLV_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2534
2535   POOL_DEBUG(SOLV_DEBUG_STATS, "done solving.\n\n");
2536   queue_free(&dq);
2537   queue_free(&dqs);
2538   if (level == 0)
2539     {
2540       /* unsolvable */
2541       solv->decisioncnt_update = solv->decisionq.count;
2542       solv->decisioncnt_keep = solv->decisionq.count;
2543       solv->decisioncnt_resolve = solv->decisionq.count;
2544       solv->decisioncnt_weak = solv->decisionq.count;
2545       solv->decisioncnt_orphan = solv->decisionq.count;
2546     }
2547 #if 0
2548   solver_printdecisionq(solv, SOLV_DEBUG_RESULT);
2549 #endif
2550 }
2551
2552
2553 /*-------------------------------------------------------------------
2554  * 
2555  * remove disabled conflicts
2556  *
2557  * purpose: update the decisionmap after some rules were disabled.
2558  * this is used to calculate the suggested/recommended package list.
2559  * Also returns a "removed" list to undo the discisionmap changes.
2560  */
2561
2562 static void
2563 removedisabledconflicts(Solver *solv, Queue *removed)
2564 {
2565   Pool *pool = solv->pool;
2566   int i, n;
2567   Id p, why, *dp;
2568   Id new;
2569   Rule *r;
2570   Id *decisionmap = solv->decisionmap;
2571
2572   queue_empty(removed);
2573   for (i = 0; i < solv->decisionq.count; i++)
2574     {
2575       p = solv->decisionq.elements[i];
2576       if (p > 0)
2577         continue;       /* conflicts only, please */
2578       why = solv->decisionq_why.elements[i];
2579       if (why == 0)
2580         {
2581           /* no rule involved, must be a orphan package drop */
2582           continue;
2583         }
2584       /* we never do conflicts on free decisions, so there
2585        * must have been an unit rule */
2586       assert(why > 0);
2587       r = solv->rules + why;
2588       if (r->d < 0 && decisionmap[-p])
2589         {
2590           /* rule is now disabled, remove from decisionmap */
2591           POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2592           queue_push(removed, -p);
2593           queue_push(removed, decisionmap[-p]);
2594           decisionmap[-p] = 0;
2595         }
2596     }
2597   if (!removed->count)
2598     return;
2599   /* we removed some confliced packages. some of them might still
2600    * be in conflict, so search for unit rules and re-conflict */
2601   new = 0;
2602   for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2603     {
2604       if (i == solv->nrules)
2605         {
2606           i = 1;
2607           r = solv->rules + i;
2608         }
2609       if (r->d < 0)
2610         continue;
2611       if (!r->w2)
2612         {
2613           if (r->p < 0 && !decisionmap[-r->p])
2614             new = r->p;
2615         }
2616       else if (!r->d)
2617         {
2618           /* binary rule */
2619           if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2620             new = r->p;
2621           else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2622             new = r->w2;
2623         }
2624       else
2625         {
2626           if (r->p < 0 && decisionmap[-r->p] == 0)
2627             new = r->p;
2628           if (new || DECISIONMAP_FALSE(r->p))
2629             {
2630               dp = pool->whatprovidesdata + r->d;
2631               while ((p = *dp++) != 0)
2632                 {
2633                   if (new && p == new)
2634                     continue;
2635                   if (p < 0 && decisionmap[-p] == 0)
2636                     {
2637                       if (new)
2638                         {
2639                           new = 0;
2640                           break;
2641                         }
2642                       new = p;
2643                     }
2644                   else if (!DECISIONMAP_FALSE(p))
2645                     {
2646                       new = 0;
2647                       break;
2648                     }
2649                 }
2650             }
2651         }
2652       if (new)
2653         {
2654           POOL_DEBUG(SOLV_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2655           decisionmap[-new] = -1;
2656           new = 0;
2657           n = 0;        /* redo all rules */
2658         }
2659     }
2660 }
2661
2662 static inline void
2663 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2664 {
2665   int i;
2666   for (i = 0; i < removed->count; i += 2)
2667     solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2668 }
2669
2670
2671 /*-------------------------------------------------------------------
2672  *
2673  * weaken solvable dependencies
2674  */
2675
2676 static void
2677 weaken_solvable_deps(Solver *solv, Id p)
2678 {
2679   int i;
2680   Rule *r;
2681
2682   for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2683     {
2684       if (r->p != -p)
2685         continue;
2686       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2687         continue;       /* conflict */
2688       queue_push(&solv->weakruleq, i);
2689     }
2690 }
2691
2692
2693 /********************************************************************/
2694 /* main() */
2695
2696
2697 void
2698 solver_calculate_multiversionmap(Pool *pool, Queue *job, Map *multiversionmap)
2699 {
2700   int i;
2701   Id how, what, select;
2702   Id p, pp;
2703   for (i = 0; i < job->count; i += 2)
2704     {
2705       how = job->elements[i];
2706       if ((how & SOLVER_JOBMASK) != SOLVER_MULTIVERSION)
2707         continue;
2708       what = job->elements[i + 1];
2709       select = how & SOLVER_SELECTMASK;
2710       if (!multiversionmap->size)
2711         map_grow(multiversionmap, pool->nsolvables);
2712       if (select == SOLVER_SOLVABLE_ALL)
2713         {
2714           FOR_POOL_SOLVABLES(p)
2715             MAPSET(multiversionmap, p);
2716         }
2717       else if (select == SOLVER_SOLVABLE_REPO)
2718         {
2719           Solvable *s;
2720           Repo *repo = pool_id2repo(pool, what);
2721           if (repo)
2722             FOR_REPO_SOLVABLES(repo, p, s)
2723               MAPSET(multiversionmap, p);
2724         }
2725       FOR_JOB_SELECT(p, pp, select, what)
2726         MAPSET(multiversionmap, p);
2727     }
2728 }
2729
2730 void
2731 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *multiversionmap)
2732 {
2733   solver_calculate_multiversionmap(pool, job, multiversionmap);
2734 }
2735
2736 /*
2737  * add a rule created by a job, record job number and weak flag
2738  */
2739 static inline void
2740 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
2741 {
2742   solver_addrule(solv, p, d);
2743   queue_push(&solv->ruletojob, job);
2744   if (weak)
2745     queue_push(&solv->weakruleq, solv->nrules - 1);
2746 }
2747
2748 static inline void
2749 add_cleandeps_package(Solver *solv, Id p)
2750 {
2751   if (!solv->cleandeps_updatepkgs)
2752     {
2753       solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
2754       queue_init(solv->cleandeps_updatepkgs);
2755     }
2756   queue_pushunique(solv->cleandeps_updatepkgs, p);
2757 }
2758
2759 static void
2760 add_update_target(Solver *solv, Id p, Id how)
2761 {
2762   Pool *pool = solv->pool;
2763   Solvable *s = pool->solvables + p;
2764   Repo *installed = solv->installed;
2765   Id pi, pip;
2766   if (!solv->update_targets)
2767     {
2768       solv->update_targets = solv_calloc(1, sizeof(Queue));
2769       queue_init(solv->update_targets);
2770     }
2771   if (s->repo == installed)
2772     {
2773       queue_push2(solv->update_targets, p, p);
2774       return;
2775     }
2776   FOR_PROVIDES(pi, pip, s->name)
2777     {
2778       Solvable *si = pool->solvables + pi;
2779       if (si->repo != installed || si->name != s->name)
2780         continue;
2781       if (how & SOLVER_FORCEBEST)
2782         {
2783           if (!solv->bestupdatemap.size)
2784             map_grow(&solv->bestupdatemap, installed->end - installed->start);
2785           MAPSET(&solv->bestupdatemap, pi - installed->start);
2786         }
2787       if (how & SOLVER_CLEANDEPS)
2788         add_cleandeps_package(solv, pi);
2789       queue_push2(solv->update_targets, pi, p);
2790       /* check if it's ok to keep the installed package */
2791       if (s->evr == si->evr && solvable_identical(s, si))
2792         queue_push2(solv->update_targets, pi, pi);
2793     }
2794   if (s->obsoletes)
2795     {
2796       Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2797       while ((obs = *obsp++) != 0)
2798         {
2799           FOR_PROVIDES(pi, pip, obs) 
2800             {
2801               Solvable *si = pool->solvables + pi;
2802               if (si->repo != installed)
2803                 continue;
2804               if (si->name == s->name)
2805                 continue;       /* already handled above */
2806               if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
2807                 continue;
2808               if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
2809                 continue;
2810               if (how & SOLVER_FORCEBEST)
2811                 {
2812                   if (!solv->bestupdatemap.size)
2813                     map_grow(&solv->bestupdatemap, installed->end - installed->start);
2814                   MAPSET(&solv->bestupdatemap, pi - installed->start);
2815                 }
2816               if (how & SOLVER_CLEANDEPS)
2817                 add_cleandeps_package(solv, pi);
2818               queue_push2(solv->update_targets, pi, p);
2819             }
2820         }
2821     }
2822 }
2823
2824 static int
2825 transform_update_targets_sortfn(const void *ap, const void *bp, void *dp)
2826 {
2827   const Id *a = ap;
2828   const Id *b = bp;
2829   if (a[0] - b[0])
2830     return a[0] - b[0];
2831   return a[1] - b[1];
2832 }
2833
2834 static void
2835 transform_update_targets(Solver *solv)
2836 {
2837   Repo *installed = solv->installed;
2838   Queue *update_targets = solv->update_targets;
2839   int i, j;
2840   Id p, q, lastp, lastq;
2841
2842   if (!update_targets->count)
2843     {
2844       queue_free(update_targets);
2845       solv->update_targets = solv_free(update_targets);
2846       return;
2847     }
2848   if (update_targets->count > 2)
2849     solv_sort(update_targets->elements, update_targets->count >> 1, 2 * sizeof(Id), transform_update_targets_sortfn, solv);
2850   queue_insertn(update_targets, 0, installed->end - installed->start, 0);
2851   lastp = lastq = 0;
2852   for (i = j = installed->end - installed->start; i < update_targets->count; i += 2)
2853     {
2854       if ((p = update_targets->elements[i]) != lastp)
2855         {
2856           if (!solv->updatemap.size)
2857             map_grow(&solv->updatemap, installed->end - installed->start);
2858           MAPSET(&solv->updatemap, p - installed->start);
2859           update_targets->elements[j++] = 0;                    /* finish old set */
2860           update_targets->elements[p - installed->start] = j;   /* start new set */
2861           lastp = p;
2862           lastq = 0;
2863         }
2864       if ((q = update_targets->elements[i + 1]) != lastq)
2865         {
2866           update_targets->elements[j++] = q;
2867           lastq = q;
2868         }
2869     }
2870   queue_truncate(update_targets, j);
2871   queue_push(update_targets, 0);        /* finish last set */
2872 }
2873
2874
2875 static void
2876 addedmap2deduceq(Solver *solv, Map *addedmap)
2877 {
2878   Pool *pool = solv->pool;
2879   int i, j;
2880   Id p;
2881   Rule *r;
2882
2883   queue_empty(&solv->addedmap_deduceq);
2884   for (i = 2, j = solv->rpmrules_end - 1; i < pool->nsolvables && j > 0; j--)
2885     {
2886       r = solv->rules + j;
2887       if (r->p >= 0)
2888         continue;
2889       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2890         continue;
2891       p = -r->p;
2892       if (!MAPTST(addedmap, p))
2893         {
2894           /* should never happen, but... */
2895           if (!solv->addedmap_deduceq.count || solv->addedmap_deduceq.elements[solv->addedmap_deduceq.count - 1] != -p)
2896             queue_push(&solv->addedmap_deduceq, -p);
2897           continue;
2898         }
2899       for (; i < p; i++)
2900         if (MAPTST(addedmap, i))
2901           queue_push(&solv->addedmap_deduceq, i);
2902       if (i == p)
2903         i++;
2904     }
2905   for (; i < pool->nsolvables; i++)
2906     if (MAPTST(addedmap, i))
2907       queue_push(&solv->addedmap_deduceq, i);
2908   j = 0;
2909   for (i = 2; i < pool->nsolvables; i++)
2910     if (MAPTST(addedmap, i))
2911       j++;
2912 }
2913
2914 static void 
2915 deduceq2addedmap(Solver *solv, Map *addedmap)
2916 {
2917   int j;
2918   Id p;
2919   Rule *r;
2920   for (j = solv->rpmrules_end - 1; j > 0; j--)
2921     {
2922       r = solv->rules + j;
2923       if (r->d < 0 && r->p)
2924         solver_enablerule(solv, r);
2925       if (r->p >= 0)
2926         continue;
2927       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2928         continue;
2929       p = -r->p;
2930       MAPSET(addedmap, p);
2931     }
2932   for (j = 0; j < solv->addedmap_deduceq.count; j++)
2933     {
2934       p = solv->addedmap_deduceq.elements[j];
2935       if (p > 0)
2936         MAPSET(addedmap, p);
2937       else
2938         MAPCLR(addedmap, p);
2939     }
2940 }
2941
2942
2943 /*
2944  *
2945  * solve job queue
2946  *
2947  */
2948
2949 int
2950 solver_solve(Solver *solv, Queue *job)
2951 {
2952   Pool *pool = solv->pool;
2953   Repo *installed = solv->installed;
2954   int i;
2955   int oldnrules, initialnrules;
2956   Map addedmap;                /* '1' == have rpm-rules for solvable */
2957   Map installcandidatemap;
2958   Id how, what, select, name, weak, p, pp, d;
2959   Queue q;
2960   Solvable *s;
2961   Rule *r;
2962   int now, solve_start;
2963   int hasdupjob = 0;
2964   int hasbestinstalljob = 0;
2965
2966   solve_start = solv_timems(0);
2967
2968   /* log solver options */
2969   POOL_DEBUG(SOLV_DEBUG_STATS, "solver started\n");
2970   POOL_DEBUG(SOLV_DEBUG_STATS, "dosplitprovides=%d, noupdateprovide=%d, noinfarchcheck=%d\n", solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
2971   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);
2972   POOL_DEBUG(SOLV_DEBUG_STATS, "promoteepoch=%d, forbidselfconflicts=%d\n", pool->promoteepoch, pool->forbidselfconflicts);
2973   POOL_DEBUG(SOLV_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d, implicitobsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors, pool->implicitobsoleteusescolors);
2974   POOL_DEBUG(SOLV_DEBUG_STATS, "dontinstallrecommended=%d, addalreadyrecommended=%d\n", solv->dontinstallrecommended, solv->addalreadyrecommended);
2975
2976   /* create whatprovides if not already there */
2977   if (!pool->whatprovides)
2978     pool_createwhatprovides(pool);
2979
2980   /* create obsolete index */
2981   policy_create_obsolete_index(solv);
2982
2983   /* remember job */
2984   queue_free(&solv->job);
2985   queue_init_clone(&solv->job, job);
2986   solv->pooljobcnt = pool->pooljobs.count;
2987   if (pool->pooljobs.count)
2988     queue_insertn(&solv->job, 0, pool->pooljobs.count, pool->pooljobs.elements);
2989   job = &solv->job;
2990
2991   /* free old stuff */
2992   if (solv->update_targets)
2993     {
2994       queue_free(solv->update_targets);
2995       solv->update_targets = solv_free(solv->update_targets);
2996     }
2997   if (solv->cleandeps_updatepkgs)
2998     {
2999       queue_free(solv->cleandeps_updatepkgs);
3000       solv->cleandeps_updatepkgs = solv_free(solv->cleandeps_updatepkgs);
3001     }
3002   queue_empty(&solv->ruleassertions);
3003   solv->bestrules_pkg = solv_free(solv->bestrules_pkg);
3004   solv->choicerules_ref = solv_free(solv->choicerules_ref);
3005   if (solv->noupdate.size)
3006     map_empty(&solv->noupdate);
3007   if (solv->multiversion.size)
3008     {
3009       map_free(&solv->multiversion);
3010       map_init(&solv->multiversion, 0);
3011     }
3012   solv->updatemap_all = 0;
3013   if (solv->updatemap.size)
3014     {
3015       map_free(&solv->updatemap);
3016       map_init(&solv->updatemap, 0);
3017     }
3018   solv->bestupdatemap_all = 0;
3019   if (solv->bestupdatemap.size)
3020     {
3021       map_free(&solv->bestupdatemap);
3022       map_init(&solv->bestupdatemap, 0);
3023     }
3024   solv->fixmap_all = 0;
3025   if (solv->fixmap.size)
3026     {
3027       map_free(&solv->fixmap);
3028       map_init(&solv->fixmap, 0);
3029     }
3030   solv->dupmap_all = 0;
3031   if (solv->dupmap.size)
3032     {
3033       map_free(&solv->dupmap);
3034       map_init(&solv->dupmap, 0);
3035     }
3036   if (solv->dupinvolvedmap.size)
3037     {
3038       map_free(&solv->dupinvolvedmap);
3039       map_init(&solv->dupinvolvedmap, 0);
3040     }
3041   solv->droporphanedmap_all = 0;
3042   if (solv->droporphanedmap.size)
3043     {
3044       map_free(&solv->droporphanedmap);
3045       map_init(&solv->droporphanedmap, 0);
3046     }
3047   if (solv->cleandepsmap.size)
3048     {
3049       map_free(&solv->cleandepsmap);
3050       map_init(&solv->cleandepsmap, 0);
3051     }
3052   
3053   queue_empty(&solv->weakruleq);
3054   solv->watches = solv_free(solv->watches);
3055   queue_empty(&solv->ruletojob);
3056   if (solv->decisionq.count)
3057     memset(solv->decisionmap, 0, pool->nsolvables * sizeof(Id));
3058   queue_empty(&solv->decisionq);
3059   queue_empty(&solv->decisionq_why);
3060   solv->decisioncnt_update = solv->decisioncnt_keep = solv->decisioncnt_resolve = solv->decisioncnt_weak = solv->decisioncnt_orphan = 0;
3061   queue_empty(&solv->learnt_why);
3062   queue_empty(&solv->learnt_pool);
3063   queue_empty(&solv->branches);
3064   solv->propagate_index = 0;
3065   queue_empty(&solv->problems);
3066   queue_empty(&solv->solutions);
3067   queue_empty(&solv->orphaned);
3068   solv->stats_learned = solv->stats_unsolvable = 0;
3069   if (solv->recommends_index)
3070     {
3071       map_empty(&solv->recommendsmap);
3072       map_empty(&solv->suggestsmap);
3073       solv->recommends_index = 0;
3074     }
3075   solv->multiversionupdaters = solv_free(solv->multiversionupdaters);
3076   
3077
3078   /*
3079    * create basic rule set of all involved packages
3080    * use addedmap bitmap to make sure we don't create rules twice
3081    */
3082
3083   /* create multiversion map if needed */
3084   solver_calculate_multiversionmap(pool, job, &solv->multiversion);
3085
3086   map_init(&addedmap, pool->nsolvables);
3087   MAPSET(&addedmap, SYSTEMSOLVABLE);
3088
3089   map_init(&installcandidatemap, pool->nsolvables);
3090   queue_init(&q);
3091
3092   now = solv_timems(0);
3093   /*
3094    * create rules for all package that could be involved with the solving
3095    * so called: rpm rules
3096    *
3097    */
3098   initialnrules = solv->rpmrules_end ? solv->rpmrules_end : 1;
3099   if (initialnrules > 1)
3100     deduceq2addedmap(solv, &addedmap);
3101   if (solv->nrules != initialnrules)
3102     solver_shrinkrules(solv, initialnrules);
3103   solv->nrules = initialnrules;
3104   solv->rpmrules_end = 0;
3105   
3106   if (installed)
3107     {
3108       /* check for update/verify jobs as they need to be known early */
3109       for (i = 0; i < job->count; i += 2)
3110         {
3111           how = job->elements[i];
3112           what = job->elements[i + 1];
3113           select = how & SOLVER_SELECTMASK;
3114           switch (how & SOLVER_JOBMASK)
3115             {
3116             case SOLVER_VERIFY:
3117               if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3118                 solv->fixmap_all = 1;
3119               FOR_JOB_SELECT(p, pp, select, what)
3120                 {
3121                   s = pool->solvables + p;
3122                   if (s->repo != installed)
3123                     continue;
3124                   if (!solv->fixmap.size)
3125                     map_grow(&solv->fixmap, installed->end - installed->start);
3126                   MAPSET(&solv->fixmap, p - installed->start);
3127                 }
3128               break;
3129             case SOLVER_UPDATE:
3130               if (select == SOLVER_SOLVABLE_ALL)
3131                 {
3132                   solv->updatemap_all = 1;
3133                   if (how & SOLVER_FORCEBEST)
3134                     solv->bestupdatemap_all = 1;
3135                   if (how & SOLVER_CLEANDEPS)
3136                     {
3137                       FOR_REPO_SOLVABLES(installed, p, s)
3138                         add_cleandeps_package(solv, p);
3139                     }
3140                 }
3141               else if (select == SOLVER_SOLVABLE_REPO)
3142                 {
3143                   Repo *repo = pool_id2repo(pool, what);
3144                   if (!repo)
3145                     break;
3146                   if (repo == installed && !(how & SOLVER_TARGETED))
3147                     {
3148                       solv->updatemap_all = 1;
3149                       if (how & SOLVER_FORCEBEST)
3150                         solv->bestupdatemap_all = 1;
3151                       if (how & SOLVER_CLEANDEPS)
3152                         {
3153                           FOR_REPO_SOLVABLES(installed, p, s)
3154                             add_cleandeps_package(solv, p);
3155                         }
3156                       break;
3157                     }
3158                   if (solv->noautotarget && !(how & SOLVER_TARGETED))
3159                     break;
3160                   /* targeted update */
3161                   FOR_REPO_SOLVABLES(repo, p, s)
3162                     add_update_target(solv, p, how);
3163                 }
3164               else
3165                 {
3166                   if (!(how & SOLVER_TARGETED))
3167                     {
3168                       int targeted = 1;
3169                       FOR_JOB_SELECT(p, pp, select, what)
3170                         {
3171                           s = pool->solvables + p;
3172                           if (s->repo != installed)
3173                             continue;
3174                           if (!solv->updatemap.size)
3175                             map_grow(&solv->updatemap, installed->end - installed->start);
3176                           MAPSET(&solv->updatemap, p - installed->start);
3177                           if (how & SOLVER_FORCEBEST)
3178                             {
3179                               if (!solv->bestupdatemap.size)
3180                                 map_grow(&solv->bestupdatemap, installed->end - installed->start);
3181                               MAPSET(&solv->bestupdatemap, p - installed->start);
3182                             }
3183                           if (how & SOLVER_CLEANDEPS)
3184                             add_cleandeps_package(solv, p);
3185                           targeted = 0;
3186                         }
3187                       if (!targeted || solv->noautotarget)
3188                         break;
3189                     }
3190                   FOR_JOB_SELECT(p, pp, select, what)
3191                     add_update_target(solv, p, how);
3192                 }
3193               break;
3194             default:
3195               break;
3196             }
3197         }
3198
3199       if (solv->update_targets)
3200         transform_update_targets(solv);
3201
3202       oldnrules = solv->nrules;
3203       FOR_REPO_SOLVABLES(installed, p, s)
3204         solver_addrpmrulesforsolvable(solv, s, &addedmap);
3205       POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3206       oldnrules = solv->nrules;
3207       FOR_REPO_SOLVABLES(installed, p, s)
3208         solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
3209       POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3210     }
3211
3212   /*
3213    * create rules for all packages involved in the job
3214    * (to be installed or removed)
3215    */
3216     
3217   oldnrules = solv->nrules;
3218   for (i = 0; i < job->count; i += 2)
3219     {
3220       how = job->elements[i];
3221       what = job->elements[i + 1];
3222       select = how & SOLVER_SELECTMASK;
3223
3224       switch (how & SOLVER_JOBMASK)
3225         {
3226         case SOLVER_INSTALL:
3227           FOR_JOB_SELECT(p, pp, select, what)
3228             {
3229               MAPSET(&installcandidatemap, p);
3230               solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3231             }
3232           break;
3233         case SOLVER_DISTUPGRADE:
3234           if (select == SOLVER_SOLVABLE_ALL)
3235             {
3236               solv->dupmap_all = 1;
3237               solv->updatemap_all = 1;
3238               if (how & SOLVER_FORCEBEST)
3239                 solv->bestupdatemap_all = 1;
3240             }
3241           if (!solv->dupmap_all)
3242             hasdupjob = 1;
3243           break;
3244         default:
3245           break;
3246         }
3247     }
3248   POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3249
3250     
3251   /*
3252    * add rules for suggests, enhances
3253    */
3254   oldnrules = solv->nrules;
3255   solver_addrpmrulesforweak(solv, &addedmap);
3256   POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3257
3258   /*
3259    * first pass done, we now have all the rpm rules we need.
3260    * unify existing rules before going over all job rules and
3261    * policy rules.
3262    * at this point the system is always solvable,
3263    * as an empty system (remove all packages) is a valid solution
3264    */
3265
3266   IF_POOLDEBUG (SOLV_DEBUG_STATS)
3267     {
3268       int possible = 0, installable = 0;
3269       for (i = 1; i < pool->nsolvables; i++)
3270         {
3271           if (pool_installable(pool, pool->solvables + i))
3272             installable++;
3273           if (MAPTST(&addedmap, i))
3274             possible++;
3275         }
3276       POOL_DEBUG(SOLV_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
3277     }
3278
3279   if (solv->nrules > initialnrules)
3280     solver_unifyrules(solv);                    /* remove duplicate rpm rules */
3281   solv->rpmrules_end = solv->nrules;            /* mark end of rpm rules */
3282
3283   if (solv->nrules > initialnrules)
3284     addedmap2deduceq(solv, &addedmap);          /* so that we can recreate the addedmap */
3285
3286   POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3287   POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule creation took %d ms\n", solv_timems(now));
3288
3289   /* create dup maps if needed. We need the maps early to create our
3290    * update rules */
3291   if (hasdupjob)
3292     solver_createdupmaps(solv);
3293
3294   /*
3295    * create feature rules
3296    * 
3297    * foreach installed:
3298    *   create assertion (keep installed, if no update available)
3299    *   or
3300    *   create update rule (A|update1(A)|update2(A)|...)
3301    * 
3302    * those are used later on to keep a version of the installed packages in
3303    * best effort mode
3304    */
3305     
3306   solv->featurerules = solv->nrules;              /* mark start of feature rules */
3307   if (installed)
3308     {
3309       /* foreach possibly installed solvable */
3310       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3311         {
3312           if (s->repo != installed)
3313             {
3314               solver_addrule(solv, 0, 0);       /* create dummy rule */
3315               continue;
3316             }
3317           solver_addupdaterule(solv, s, 1);    /* allow s to be updated */
3318         }
3319       /* make sure we accounted for all rules */
3320       assert(solv->nrules - solv->featurerules == installed->end - installed->start);
3321     }
3322   solv->featurerules_end = solv->nrules;
3323
3324     /*
3325      * Add update rules for installed solvables
3326      * 
3327      * almost identical to feature rules
3328      * except that downgrades/archchanges/vendorchanges are not allowed
3329      */
3330     
3331   solv->updaterules = solv->nrules;
3332
3333   if (installed)
3334     { /* foreach installed solvables */
3335       /* we create all update rules, but disable some later on depending on the job */
3336       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3337         {
3338           Rule *sr;
3339
3340           if (s->repo != installed)
3341             {
3342               solver_addrule(solv, 0, 0);       /* create dummy rule */
3343               continue;
3344             }
3345           solver_addupdaterule(solv, s, 0);     /* allowall = 0: downgrades not allowed */
3346           /*
3347            * check for and remove duplicate
3348            */
3349           r = solv->rules + solv->nrules - 1;           /* r: update rule */
3350           sr = r - (installed->end - installed->start); /* sr: feature rule */
3351           /* it's orphaned if there is no feature rule or the feature rule
3352            * consists just of the installed package */
3353           if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
3354             queue_push(&solv->orphaned, i);
3355           if (!r->p)
3356             {
3357               assert(solv->dupmap_all && !sr->p);
3358               continue;
3359             }
3360           if (!solver_rulecmp(solv, r, sr))
3361             memset(sr, 0, sizeof(*sr));         /* delete unneeded feature rule */
3362           else
3363             solver_disablerule(solv, sr);       /* disable feature rule */
3364         }
3365       /* consistency check: we added a rule for _every_ installed solvable */
3366       assert(solv->nrules - solv->updaterules == installed->end - installed->start);
3367     }
3368   solv->updaterules_end = solv->nrules;
3369
3370
3371   /*
3372    * now add all job rules
3373    */
3374
3375   solv->jobrules = solv->nrules;
3376   for (i = 0; i < job->count; i += 2)
3377     {
3378       oldnrules = solv->nrules;
3379
3380       if (i && i == solv->pooljobcnt)
3381         POOL_DEBUG(SOLV_DEBUG_JOB, "end of pool jobs\n");
3382       how = job->elements[i];
3383       what = job->elements[i + 1];
3384       weak = how & SOLVER_WEAK;
3385       select = how & SOLVER_SELECTMASK;
3386       switch (how & SOLVER_JOBMASK)
3387         {
3388         case SOLVER_INSTALL:
3389           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3390           if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3391             map_grow(&solv->cleandepsmap, installed->end - installed->start);
3392           if (select == SOLVER_SOLVABLE)
3393             {
3394               p = what;
3395               d = 0;
3396             }
3397           else
3398             {
3399               queue_empty(&q);
3400               FOR_JOB_SELECT(p, pp, select, what)
3401                 queue_push(&q, p);
3402               if (!q.count)
3403                 {
3404                   if (select == SOLVER_SOLVABLE_ONE_OF)
3405                     break;      /* ignore empty installs */
3406                   /* no candidate found or unsupported, make this an impossible rule */
3407                   queue_push(&q, -SYSTEMSOLVABLE);
3408                 }
3409               p = queue_shift(&q);      /* get first candidate */
3410               d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q);    /* internalize */
3411             }
3412           /* force install of namespace supplements hack */
3413           if (select == SOLVER_SOLVABLE_PROVIDES && !d && (p == SYSTEMSOLVABLE || p == -SYSTEMSOLVABLE) && ISRELDEP(what))
3414             {
3415               Reldep *rd = GETRELDEP(pool, what);
3416               if (rd->flags == REL_NAMESPACE)
3417                 {
3418                   p = SYSTEMSOLVABLE;
3419                   if (!solv->installsuppdepq)
3420                     {
3421                       solv->installsuppdepq = solv_calloc(1, sizeof(Queue));
3422                       queue_init(solv->installsuppdepq);
3423                     }
3424                   queue_pushunique(solv->installsuppdepq, rd->evr == 0 ? rd->name : what);
3425                 }
3426             }
3427           solver_addjobrule(solv, p, d, i, weak);
3428           if (how & SOLVER_FORCEBEST)
3429             hasbestinstalljob = 1;
3430           break;
3431         case SOLVER_ERASE:
3432           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
3433           if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3434             map_grow(&solv->cleandepsmap, installed->end - installed->start);
3435           /* specific solvable: by id or by nevra */
3436           name = (select == SOLVER_SOLVABLE || (select == SOLVER_SOLVABLE_NAME && ISRELDEP(what))) ? 0 : -1;
3437           if (select == SOLVER_SOLVABLE_ALL)    /* hmmm ;) */
3438             {
3439               FOR_POOL_SOLVABLES(p)
3440                 solver_addjobrule(solv, -p, 0, i, weak);
3441             }
3442           else if (select == SOLVER_SOLVABLE_REPO)
3443             {
3444               Repo *repo = pool_id2repo(pool, what);
3445               if (repo)
3446                 FOR_REPO_SOLVABLES(repo, p, s)
3447                   solver_addjobrule(solv, -p, 0, i, weak);
3448             }
3449           FOR_JOB_SELECT(p, pp, select, what)
3450             {
3451               s = pool->solvables + p;
3452               if (installed && s->repo == installed)
3453                 name = !name ? s->name : -1;
3454               solver_addjobrule(solv, -p, 0, i, weak);
3455             }
3456           /* special case for "erase a specific solvable": we also
3457            * erase all other solvables with that name, so that they
3458            * don't get picked up as replacement.
3459            * name is > 0 if exactly one installed solvable matched.
3460            */
3461           /* XXX: look also at packages that obsolete this package? */
3462           if (name > 0)
3463             {
3464               int j, k;
3465               k = solv->nrules;
3466               FOR_PROVIDES(p, pp, name)
3467                 {
3468                   s = pool->solvables + p;
3469                   if (s->name != name)
3470                     continue;
3471                   /* keep other versions installed */
3472                   if (s->repo == installed)
3473                     continue;
3474                   /* keep installcandidates of other jobs */
3475                   if (MAPTST(&installcandidatemap, p))
3476                     continue;
3477                   /* don't add the same rule twice */
3478                   for (j = oldnrules; j < k; j++)
3479                     if (solv->rules[j].p == -p)
3480                       break;
3481                   if (j == k)
3482                     solver_addjobrule(solv, -p, 0, i, weak);    /* remove by id */
3483                 }
3484             }
3485           break;
3486
3487         case SOLVER_UPDATE:
3488           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3489           break;
3490         case SOLVER_VERIFY:
3491           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3492           break;
3493         case SOLVER_WEAKENDEPS:
3494           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3495           if (select != SOLVER_SOLVABLE)
3496             break;
3497           s = pool->solvables + what;
3498           weaken_solvable_deps(solv, what);
3499           break;
3500         case SOLVER_MULTIVERSION:
3501           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %smultiversion %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3502           break;
3503         case SOLVER_LOCK:
3504           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3505           if (select == SOLVER_SOLVABLE_ALL)
3506             {
3507               FOR_POOL_SOLVABLES(p)
3508                 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3509             }
3510           else if (select == SOLVER_SOLVABLE_REPO)
3511             {
3512               Repo *repo = pool_id2repo(pool, what);
3513               if (repo)
3514                 FOR_REPO_SOLVABLES(repo, p, s)
3515                   solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3516             }
3517           FOR_JOB_SELECT(p, pp, select, what)
3518             solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3519           break;
3520         case SOLVER_DISTUPGRADE:
3521           POOL_DEBUG(SOLV_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
3522           break;
3523         case SOLVER_DROP_ORPHANED:
3524           POOL_DEBUG(SOLV_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
3525           if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3526             solv->droporphanedmap_all = 1;
3527           FOR_JOB_SELECT(p, pp, select, what)
3528             {
3529               s = pool->solvables + p;
3530               if (!installed || s->repo != installed)
3531                 continue;
3532               if (!solv->droporphanedmap.size)
3533                 map_grow(&solv->droporphanedmap, installed->end - installed->start);
3534               MAPSET(&solv->droporphanedmap, p - installed->start);
3535             }
3536           break;
3537         case SOLVER_USERINSTALLED:
3538           POOL_DEBUG(SOLV_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
3539           break;
3540         default:
3541           POOL_DEBUG(SOLV_DEBUG_JOB, "job: unknown job\n");
3542           break;
3543         }
3544         
3545         /*
3546          * debug
3547          */
3548         
3549       IF_POOLDEBUG (SOLV_DEBUG_JOB)
3550         {
3551           int j;
3552           if (solv->nrules == oldnrules)
3553             POOL_DEBUG(SOLV_DEBUG_JOB, "  - no rule created\n");
3554           for (j = oldnrules; j < solv->nrules; j++)
3555             {
3556               POOL_DEBUG(SOLV_DEBUG_JOB, "  - job ");
3557               solver_printrule(solv, SOLV_DEBUG_JOB, solv->rules + j);
3558             }
3559         }
3560     }
3561   assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
3562   solv->jobrules_end = solv->nrules;
3563
3564   /* now create infarch and dup rules */
3565   if (!solv->noinfarchcheck)
3566     {
3567       solver_addinfarchrules(solv, &addedmap);
3568       if (pool->implicitobsoleteusescolors)
3569         {
3570           /* currently doesn't work well with infarch rules, so make
3571            * them weak */
3572           for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
3573             queue_push(&solv->weakruleq, i);
3574         }
3575     }
3576   else
3577     solv->infarchrules = solv->infarchrules_end = solv->nrules;
3578
3579   if (hasdupjob)
3580     solver_addduprules(solv, &addedmap);
3581   else
3582     solv->duprules = solv->duprules_end = solv->nrules;
3583
3584   if (solv->bestupdatemap_all || solv->bestupdatemap.size || hasbestinstalljob)
3585     solver_addbestrules(solv, hasbestinstalljob);
3586   else
3587     solv->bestrules = solv->bestrules_end = solv->nrules;
3588
3589   if (hasdupjob)
3590     solver_freedupmaps(solv);   /* no longer needed */
3591
3592   if (1)
3593     solver_addchoicerules(solv);
3594   else
3595     solv->choicerules = solv->choicerules_end = solv->nrules;
3596
3597   if (0)
3598     {
3599       for (i = solv->featurerules; i < solv->nrules; i++)
3600         solver_printruleclass(solv, SOLV_DEBUG_RESULT, solv->rules + i);
3601     }
3602   /* all rules created
3603    * --------------------------------------------------------------
3604    * prepare for solving
3605    */
3606     
3607   /* free unneeded memory */
3608   map_free(&addedmap);
3609   map_free(&installcandidatemap);
3610   queue_free(&q);
3611
3612   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);
3613   POOL_DEBUG(SOLV_DEBUG_STATS, "overall rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3614
3615   /* create weak map */
3616   map_init(&solv->weakrulemap, solv->nrules);
3617   for (i = 0; i < solv->weakruleq.count; i++)
3618     {
3619       p = solv->weakruleq.elements[i];
3620       MAPSET(&solv->weakrulemap, p);
3621     }
3622
3623   /* enable cleandepsmap creation if we have updatepkgs */
3624   if (solv->cleandeps_updatepkgs && !solv->cleandepsmap.size)
3625     map_grow(&solv->cleandepsmap, installed->end - installed->start);
3626   /* no mistakes */
3627   if (solv->cleandeps_mistakes)
3628     {    
3629       queue_free(solv->cleandeps_mistakes);
3630       solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
3631     }    
3632
3633   /* all new rules are learnt after this point */
3634   solv->learntrules = solv->nrules;
3635
3636   /* create watches chains */
3637   makewatches(solv);
3638
3639   /* create assertion index. it is only used to speed up
3640    * makeruledecsions() a bit */
3641   for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
3642     if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
3643       queue_push(&solv->ruleassertions, i);
3644
3645   /* disable update rules that conflict with our job */
3646   solver_disablepolicyrules(solv);
3647
3648   /* make initial decisions based on assertion rules */
3649   makeruledecisions(solv);
3650   POOL_DEBUG(SOLV_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
3651
3652   /*
3653    * ********************************************
3654    * solve!
3655    * ********************************************
3656    */
3657     
3658   now = solv_timems(0);
3659   solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
3660   POOL_DEBUG(SOLV_DEBUG_STATS, "solver took %d ms\n", solv_timems(now));
3661
3662   /*
3663    * prepare solution queue if there were problems
3664    */
3665   solver_prepare_solutions(solv);
3666
3667   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);
3668   POOL_DEBUG(SOLV_DEBUG_STATS, "solver_solve took %d ms\n", solv_timems(solve_start));
3669
3670   /* return number of problems */
3671   return solv->problems.count ? solv->problems.count / 2 : 0;
3672 }
3673
3674 Transaction *
3675 solver_create_transaction(Solver *solv)
3676 {
3677   return transaction_create_decisionq(solv->pool, &solv->decisionq, &solv->multiversion);
3678 }
3679
3680 void solver_get_orphaned(Solver *solv, Queue *orphanedq)
3681 {
3682   queue_free(orphanedq);
3683   queue_init_clone(orphanedq, &solv->orphaned);
3684 }
3685
3686 void solver_get_recommendations(Solver *solv, Queue *recommendationsq, Queue *suggestionsq, int noselected)
3687 {
3688   Pool *pool = solv->pool;
3689   Queue redoq, disabledq;
3690   int goterase, i;
3691   Solvable *s;
3692   Rule *r;
3693   Map obsmap;
3694
3695   if (!recommendationsq && !suggestionsq)
3696     return;
3697
3698   map_init(&obsmap, pool->nsolvables);
3699   if (solv->installed)
3700     {
3701       Id obs, *obsp, p, po, ppo;
3702       for (p = solv->installed->start; p < solv->installed->end; p++)
3703         {
3704           s = pool->solvables + p;
3705           if (s->repo != solv->installed || !s->obsoletes)
3706             continue;
3707           if (solv->decisionmap[p] <= 0)
3708             continue;
3709           if (solv->multiversion.size && MAPTST(&solv->multiversion, p))
3710             continue;
3711           obsp = s->repo->idarraydata + s->obsoletes;
3712           /* foreach obsoletes */
3713           while ((obs = *obsp++) != 0)
3714             FOR_PROVIDES(po, ppo, obs)
3715               MAPSET(&obsmap, po);
3716         }
3717     }
3718
3719   queue_init(&redoq);
3720   queue_init(&disabledq);
3721   goterase = 0;
3722   /* disable all erase jobs (including weak "keep uninstalled" rules) */
3723   for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
3724     {
3725       if (r->d < 0)     /* disabled ? */
3726         continue;
3727       if (r->p >= 0)    /* install job? */
3728         continue;
3729       queue_push(&disabledq, i);
3730       solver_disablerule(solv, r);
3731       goterase++;
3732     }
3733   
3734   if (goterase)
3735     {
3736       enabledisablelearntrules(solv);
3737       removedisabledconflicts(solv, &redoq);
3738     }
3739
3740   /*
3741    * find recommended packages
3742    */
3743   if (recommendationsq)
3744     {
3745       Id rec, *recp, p, pp;
3746
3747       queue_empty(recommendationsq);
3748       /* create map of all recommened packages */
3749       solv->recommends_index = -1;
3750       MAPZERO(&solv->recommendsmap);
3751
3752       /* put all packages the solver already chose in the map */
3753       if (solv->decisioncnt_weak)
3754         {
3755           for (i = solv->decisioncnt_weak; i < solv->decisioncnt_orphan; i++)
3756             {
3757               Id why;
3758               why = solv->decisionq_why.elements[i];
3759               if (why)
3760                 continue;       /* forced by unit rule or dep resolving */
3761               p = solv->decisionq.elements[i];
3762               if (p < 0)
3763                 continue;
3764               MAPSET(&solv->recommendsmap, p);
3765             }
3766         }
3767
3768       for (i = 0; i < solv->decisionq.count; i++)
3769         {
3770           p = solv->decisionq.elements[i];
3771           if (p < 0)
3772             continue;
3773           s = pool->solvables + p;
3774           if (s->recommends)
3775             {
3776               recp = s->repo->idarraydata + s->recommends;
3777               while ((rec = *recp++) != 0)
3778                 {
3779                   FOR_PROVIDES(p, pp, rec)
3780                     if (solv->decisionmap[p] > 0)
3781                       break;
3782                   if (p)
3783                     {
3784                       if (!noselected)
3785                         {
3786                           FOR_PROVIDES(p, pp, rec)
3787                             if (solv->decisionmap[p] > 0)
3788                               MAPSET(&solv->recommendsmap, p);
3789                         }
3790                       continue; /* p != 0: already fulfilled */
3791                     }
3792                   FOR_PROVIDES(p, pp, rec)
3793                     MAPSET(&solv->recommendsmap, p);
3794                 }
3795             }
3796         }
3797       for (i = 1; i < pool->nsolvables; i++)
3798         {
3799           if (solv->decisionmap[i] < 0)
3800             continue;
3801           if (solv->decisionmap[i] > 0 && noselected)
3802             continue;
3803           if (MAPTST(&obsmap, i))
3804             continue;
3805           s = pool->solvables + i;
3806           if (!MAPTST(&solv->recommendsmap, i))
3807             {
3808               if (!s->supplements)
3809                 continue;
3810               if (!pool_installable(pool, s))
3811                 continue;
3812               if (!solver_is_supplementing(solv, s))
3813                 continue;
3814             }
3815           queue_push(recommendationsq, i);
3816         }
3817       /* we use MODE_SUGGEST here so that repo prio is ignored */
3818       policy_filter_unwanted(solv, recommendationsq, POLICY_MODE_SUGGEST);
3819     }
3820
3821   /*
3822    * find suggested packages
3823    */
3824     
3825   if (suggestionsq)
3826     {
3827       Id sug, *sugp, p, pp;
3828
3829       queue_empty(suggestionsq);
3830       /* create map of all suggests that are still open */
3831       solv->recommends_index = -1;
3832       MAPZERO(&solv->suggestsmap);
3833       for (i = 0; i < solv->decisionq.count; i++)
3834         {
3835           p = solv->decisionq.elements[i];
3836           if (p < 0)
3837             continue;
3838           s = pool->solvables + p;
3839           if (s->suggests)
3840             {
3841               sugp = s->repo->idarraydata + s->suggests;
3842               while ((sug = *sugp++) != 0)
3843                 {
3844                   FOR_PROVIDES(p, pp, sug)
3845                     if (solv->decisionmap[p] > 0)
3846                       break;
3847                   if (p)
3848                     {
3849                       if (!noselected)
3850                         {
3851                           FOR_PROVIDES(p, pp, sug)
3852                             if (solv->decisionmap[p] > 0)
3853                               MAPSET(&solv->suggestsmap, p);
3854                         }
3855                       continue; /* already fulfilled */
3856                     }
3857                   FOR_PROVIDES(p, pp, sug)
3858                     MAPSET(&solv->suggestsmap, p);
3859                 }
3860             }
3861         }
3862       for (i = 1; i < pool->nsolvables; i++)
3863         {
3864           if (solv->decisionmap[i] < 0)
3865             continue;
3866           if (solv->decisionmap[i] > 0 && noselected)
3867             continue;
3868           if (MAPTST(&obsmap, i))
3869             continue;
3870           s = pool->solvables + i;
3871           if (!MAPTST(&solv->suggestsmap, i))
3872             {
3873               if (!s->enhances)
3874                 continue;
3875               if (!pool_installable(pool, s))
3876                 continue;
3877               if (!solver_is_enhancing(solv, s))
3878                 continue;
3879             }
3880           queue_push(suggestionsq, i);
3881         }
3882       policy_filter_unwanted(solv, suggestionsq, POLICY_MODE_SUGGEST);
3883     }
3884
3885   /* undo removedisabledconflicts */
3886   if (redoq.count)
3887     undo_removedisabledconflicts(solv, &redoq);
3888   queue_free(&redoq);
3889   
3890   /* undo job rule disabling */
3891   for (i = 0; i < disabledq.count; i++)
3892     solver_enablerule(solv, solv->rules + disabledq.elements[i]);
3893   queue_free(&disabledq);
3894   map_free(&obsmap);
3895 }
3896
3897
3898 /***********************************************************************/
3899 /* disk usage computations */
3900
3901 /*-------------------------------------------------------------------
3902  * 
3903  * calculate DU changes
3904  */
3905
3906 void
3907 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
3908 {
3909   Map installedmap;
3910
3911   solver_create_state_maps(solv, &installedmap, 0);
3912   pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
3913   map_free(&installedmap);
3914 }
3915
3916
3917 /*-------------------------------------------------------------------
3918  * 
3919  * calculate changes in install size
3920  */
3921
3922 int
3923 solver_calc_installsizechange(Solver *solv)
3924 {
3925   Map installedmap;
3926   int change;
3927
3928   solver_create_state_maps(solv, &installedmap, 0);
3929   change = pool_calc_installsizechange(solv->pool, &installedmap);
3930   map_free(&installedmap);
3931   return change;
3932 }
3933
3934 void
3935 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
3936 {
3937   pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
3938 }
3939
3940 void
3941 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
3942 {
3943   Pool *pool = solv->pool;
3944   Map installedmap;
3945   int i;
3946   pool_create_state_maps(pool,  &solv->decisionq, &installedmap, 0);
3947   pool_trivial_installable_multiversionmap(pool, &installedmap, pkgs, res, solv->multiversion.size ? &solv->multiversion : 0);
3948   for (i = 0; i < res->count; i++)
3949     if (res->elements[i] != -1)
3950       {
3951         Solvable *s = pool->solvables + pkgs->elements[i];
3952         if (!strncmp("patch:", pool_id2str(pool, s->name), 6) && solvable_is_irrelevant_patch(s, &installedmap))
3953           res->elements[i] = -1;
3954       }
3955   map_free(&installedmap);
3956 }
3957
3958 /*-------------------------------------------------------------------
3959  * 
3960  * decision introspection
3961  */
3962
3963 int
3964 solver_get_decisionlevel(Solver *solv, Id p)
3965 {
3966   return solv->decisionmap[p];
3967 }
3968
3969 void
3970 solver_get_decisionqueue(Solver *solv, Queue *decisionq)
3971 {
3972   queue_free(decisionq);
3973   queue_init_clone(decisionq, &solv->decisionq);
3974 }
3975
3976 int
3977 solver_get_lastdecisionblocklevel(Solver *solv)
3978 {
3979   Id p;
3980   if (solv->decisionq.count == 0)
3981     return 0;
3982   p = solv->decisionq.elements[solv->decisionq.count - 1];
3983   if (p < 0)
3984     p = -p;
3985   return solv->decisionmap[p] < 0 ? -solv->decisionmap[p] : solv->decisionmap[p];
3986 }
3987
3988 void
3989 solver_get_decisionblock(Solver *solv, int level, Queue *decisionq)
3990 {
3991   Id p;
3992   int i;
3993
3994   queue_empty(decisionq);
3995   for (i = 0; i < solv->decisionq.count; i++)
3996     {
3997       p = solv->decisionq.elements[i];
3998       if (p < 0)
3999         p = -p;
4000       if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
4001         break;
4002     }
4003   if (i == solv->decisionq.count)
4004     return;
4005   for (i = 0; i < solv->decisionq.count; i++)
4006     {
4007       p = solv->decisionq.elements[i];
4008       if (p < 0)
4009         p = -p;
4010       if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
4011         queue_push(decisionq, p);
4012       else
4013         break;
4014     }
4015 }
4016
4017 int
4018 solver_describe_decision(Solver *solv, Id p, Id *infop)
4019 {
4020   int i;
4021   Id pp, why;
4022   
4023   if (infop)
4024     *infop = 0;
4025   if (!solv->decisionmap[p])
4026     return SOLVER_REASON_UNRELATED;
4027   pp = solv->decisionmap[p] < 0 ? -p : p;
4028   for (i = 0; i < solv->decisionq.count; i++)
4029     if (solv->decisionq.elements[i] == pp)
4030       break;
4031   if (i == solv->decisionq.count)       /* just in case... */
4032     return SOLVER_REASON_UNRELATED;
4033   why = solv->decisionq_why.elements[i];
4034   if (infop)
4035     *infop = why > 0 ? why : -why;
4036   if (why > 0)
4037     return SOLVER_REASON_UNIT_RULE;
4038   why = -why;
4039   if (i < solv->decisioncnt_update)
4040     {
4041       if (i == 0)
4042         return SOLVER_REASON_KEEP_INSTALLED;
4043       return SOLVER_REASON_RESOLVE_JOB;
4044     }
4045   if (i < solv->decisioncnt_keep)
4046     {
4047       if (why == 0 && pp < 0)
4048         return SOLVER_REASON_CLEANDEPS_ERASE;
4049       return SOLVER_REASON_UPDATE_INSTALLED;
4050     }
4051   if (i < solv->decisioncnt_resolve)
4052     {
4053       if (why == 0 && pp < 0)
4054         return SOLVER_REASON_CLEANDEPS_ERASE;
4055       return SOLVER_REASON_KEEP_INSTALLED;
4056     }
4057   if (why > 0)
4058     return SOLVER_REASON_RESOLVE;
4059   /* weak or orphaned */
4060   if (solv->decisionq.count < solv->decisioncnt_orphan)
4061     return SOLVER_REASON_WEAKDEP;
4062   return SOLVER_REASON_RESOLVE_ORPHAN;
4063 }
4064
4065
4066 void
4067 solver_describe_weakdep_decision(Solver *solv, Id p, Queue *whyq)
4068 {
4069   Pool *pool = solv->pool;
4070   int i;
4071   int level = solv->decisionmap[p];
4072   int decisionno;
4073   Solvable *s;
4074
4075   queue_empty(whyq);
4076   if (level < 0)
4077     return;     /* huh? */
4078   for (decisionno = 0; decisionno < solv->decisionq.count; decisionno++)
4079     if (solv->decisionq.elements[decisionno] == p)
4080       break;
4081   if (decisionno == solv->decisionq.count)
4082     return;     /* huh? */
4083   if (decisionno < solv->decisioncnt_weak || decisionno >= solv->decisioncnt_orphan)
4084     return;     /* huh? */
4085
4086   /* 1) list all packages that recommend us */
4087   for (i = 1; i < pool->nsolvables; i++)
4088     {
4089       Id *recp, rec, pp2, p2;
4090       if (solv->decisionmap[i] < 0 || solv->decisionmap[i] >= level)
4091         continue;
4092       s = pool->solvables + i;
4093       if (!s->recommends)
4094         continue;
4095       if (!solv->addalreadyrecommended && s->repo == solv->installed)
4096         continue;
4097       recp = s->repo->idarraydata + s->recommends;
4098       while ((rec = *recp++) != 0)
4099         {
4100           int found = 0;
4101           FOR_PROVIDES(p2, pp2, rec)
4102             {
4103               if (p2 == p)
4104                 found = 1;
4105               else
4106                 {
4107                   /* if p2 is already installed, this recommends is ignored */
4108                   if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
4109                     break;
4110                 }
4111             }
4112           if (!p2 && found)
4113             {
4114               queue_push(whyq, SOLVER_REASON_RECOMMENDED);
4115               queue_push2(whyq, p2, rec);
4116             }
4117         }
4118     }
4119   /* 2) list all supplements */
4120   s = pool->solvables + p;
4121   if (s->supplements && level > 0)
4122     {
4123       Id *supp, sup, pp2, p2;
4124       /* this is a hack. to use solver_dep_fulfilled we temporarily clear
4125        * everything above our level in the decisionmap */
4126       for (i = decisionno; i < solv->decisionq.count; i++ )
4127         {
4128           p2 = solv->decisionq.elements[i];
4129           if (p2 > 0)
4130             solv->decisionmap[p2] = -solv->decisionmap[p2];
4131         }
4132       supp = s->repo->idarraydata + s->supplements;
4133       while ((sup = *supp++) != 0)
4134         if (solver_dep_fulfilled(solv, sup))
4135           {
4136             int found = 0;
4137             /* let's see if this is an easy supp */
4138             FOR_PROVIDES(p2, pp2, sup)
4139               {
4140                 if (!solv->addalreadyrecommended && solv->installed)
4141                   {
4142                     if (pool->solvables[p2].repo == solv->installed)
4143                       continue;
4144                   }
4145                 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
4146                   {
4147                     queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
4148                     queue_push2(whyq, p2, sup);
4149                     found = 1;
4150                   }
4151               }
4152             if (!found)
4153               {
4154                 /* hard case, just note dependency with no package */
4155                 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
4156                 queue_push2(whyq, 0, sup);
4157               }
4158           }
4159       for (i = decisionno; i < solv->decisionq.count; i++)
4160         {
4161           p2 = solv->decisionq.elements[i];
4162           if (p2 > 0)
4163             solv->decisionmap[p2] = -solv->decisionmap[p2];
4164         }
4165     }
4166 }
4167
4168 void
4169 pool_job2solvables(Pool *pool, Queue *pkgs, Id how, Id what)
4170 {
4171   Id p, pp;
4172   how &= SOLVER_SELECTMASK;
4173   queue_empty(pkgs);
4174   if (how == SOLVER_SOLVABLE_ALL)
4175     {
4176       FOR_POOL_SOLVABLES(p)
4177         queue_push(pkgs, p);
4178     }
4179   else if (how == SOLVER_SOLVABLE_REPO)
4180     {
4181       Repo *repo = pool_id2repo(pool, what);
4182       Solvable *s;
4183       if (repo)
4184         FOR_REPO_SOLVABLES(repo, p, s)
4185           queue_push(pkgs, p);
4186     }
4187   else
4188     {
4189       FOR_JOB_SELECT(p, pp, how, what)
4190         queue_push(pkgs, p);
4191     }
4192 }
4193
4194 int
4195 pool_isemptyupdatejob(Pool *pool, Id how, Id what)
4196 {
4197   Id p, pp, pi, pip;
4198   Id select = how & SOLVER_SELECTMASK;
4199   if ((how & SOLVER_JOBMASK) != SOLVER_UPDATE)
4200     return 0;
4201   if (select == SOLVER_SOLVABLE_ALL || select == SOLVER_SOLVABLE_REPO)
4202     return 0;
4203   if (!pool->installed)
4204     return 1;
4205   FOR_JOB_SELECT(p, pp, select, what)
4206     if (pool->solvables[p].repo == pool->installed)
4207       return 0;
4208   /* hard work */
4209   FOR_JOB_SELECT(p, pp, select, what)
4210     {
4211       Solvable *s = pool->solvables + p;
4212       FOR_PROVIDES(pi, pip, s->name)
4213         {
4214           Solvable *si = pool->solvables + pi;
4215           if (si->repo != pool->installed || si->name != s->name)
4216             continue;
4217           return 0;
4218         }
4219       if (s->obsoletes)
4220         {
4221           Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
4222           while ((obs = *obsp++) != 0)
4223             {
4224               FOR_PROVIDES(pi, pip, obs) 
4225                 {
4226                   Solvable *si = pool->solvables + pi;
4227                   if (si->repo != pool->installed)
4228                     continue;
4229                   if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
4230                     continue;
4231                   if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
4232                     continue;
4233                   return 0;
4234                 }
4235             }
4236         }
4237     }
4238   return 1;
4239 }
4240
4241 const char *
4242 solver_select2str(Pool *pool, Id select, Id what)
4243 {
4244   const char *s;
4245   char *b;
4246   select &= SOLVER_SELECTMASK;
4247   if (select == SOLVER_SOLVABLE)
4248     return pool_solvid2str(pool, what);
4249   if (select == SOLVER_SOLVABLE_NAME)
4250     return pool_dep2str(pool, what);
4251   if (select == SOLVER_SOLVABLE_PROVIDES)
4252     {
4253       s = pool_dep2str(pool, what);
4254       b = pool_alloctmpspace(pool, 11 + strlen(s));
4255       sprintf(b, "providing %s", s);
4256       return b;
4257     }
4258   if (select == SOLVER_SOLVABLE_ONE_OF)
4259     {
4260       Id p;
4261       b = 0;
4262       while ((p = pool->whatprovidesdata[what++]) != 0)
4263         {
4264           s = pool_solvid2str(pool, p);
4265           if (b)
4266             b = pool_tmpappend(pool, b, ", ", s);
4267           else
4268             b = pool_tmpjoin(pool, s, 0, 0);
4269           pool_freetmpspace(pool, s);
4270         }
4271       return b ? b : "nothing";
4272     }
4273   if (select == SOLVER_SOLVABLE_REPO)
4274     {
4275       b = pool_alloctmpspace(pool, 20);
4276       sprintf(b, "repo #%d", what);
4277       return b;
4278     }
4279   if (select == SOLVER_SOLVABLE_ALL)
4280     return "all packages";
4281   return "unknown job select";
4282 }
4283
4284 const char *
4285 pool_job2str(Pool *pool, Id how, Id what, Id flagmask)
4286 {
4287   Id select = how & SOLVER_SELECTMASK;
4288   const char *strstart = 0, *strend = 0;
4289   char *s;
4290   int o;
4291
4292   switch (how & SOLVER_JOBMASK)
4293     {
4294     case SOLVER_NOOP:
4295       return "do nothing";
4296     case SOLVER_INSTALL:
4297       if (select == SOLVER_SOLVABLE && pool->installed && pool->solvables[what].repo == pool->installed)
4298         strstart = "keep ", strend = "installed";
4299       else if (select == SOLVER_SOLVABLE || select == SOLVER_SOLVABLE_NAME)
4300         strstart = "install ";
4301       else if (select == SOLVER_SOLVABLE_PROVIDES)
4302         strstart = "install a package ";
4303       else
4304         strstart = "install one of ";
4305       break;
4306     case SOLVER_ERASE:
4307       if (select == SOLVER_SOLVABLE && !(pool->installed && pool->solvables[what].repo == pool->installed))
4308         strstart = "keep ", strend = "unstalled";
4309       else if (select == SOLVER_SOLVABLE_PROVIDES)
4310         strstart = "deinstall all packages ";
4311       else
4312         strstart = "deinstall ";
4313       break;
4314     case SOLVER_UPDATE:
4315       strstart = "update ";
4316       break;
4317     case SOLVER_WEAKENDEPS:
4318       strstart = "weaken deps of ";
4319       break;
4320     case SOLVER_MULTIVERSION:
4321       strstart = "multi version ";
4322       break;
4323     case SOLVER_LOCK:
4324       strstart = "update ";
4325       break;
4326     case SOLVER_DISTUPGRADE:
4327       strstart = "dist upgrade ";
4328       break;
4329     case SOLVER_VERIFY:
4330       strstart = "verify ";
4331       break;
4332     case SOLVER_DROP_ORPHANED:
4333       strstart = "deinstall ", strend = "if orphaned";
4334       break;
4335     case SOLVER_USERINSTALLED:
4336       strstart = "regard ", strend = "as userinstalled";
4337       break;
4338     default:
4339       strstart = "unknown job ";
4340       break;
4341     }
4342   s = pool_tmpjoin(pool, strstart, solver_select2str(pool, select, what), strend);
4343   how &= flagmask;
4344   if ((how & ~(SOLVER_SELECTMASK|SOLVER_JOBMASK)) == 0)
4345     return s;
4346   o = strlen(s);
4347   s = pool_tmpappend(pool, s, " ", 0);
4348   if (how & SOLVER_WEAK)
4349     s = pool_tmpappend(pool, s, ",weak", 0);
4350   if (how & SOLVER_ESSENTIAL)
4351     s = pool_tmpappend(pool, s, ",essential", 0);
4352   if (how & SOLVER_CLEANDEPS)
4353     s = pool_tmpappend(pool, s, ",cleandeps", 0);
4354   if (how & SOLVER_ORUPDATE)
4355     s = pool_tmpappend(pool, s, ",orupdate", 0);
4356   if (how & SOLVER_FORCEBEST)
4357     s = pool_tmpappend(pool, s, ",forcebest", 0);
4358   if (how & SOLVER_TARGETED)
4359     s = pool_tmpappend(pool, s, ",targeted", 0);
4360   if (how & SOLVER_SETEV)
4361     s = pool_tmpappend(pool, s, ",setev", 0);
4362   if (how & SOLVER_SETEVR)
4363     s = pool_tmpappend(pool, s, ",setevr", 0);
4364   if (how & SOLVER_SETARCH)
4365     s = pool_tmpappend(pool, s, ",setarch", 0);
4366   if (how & SOLVER_SETVENDOR)
4367     s = pool_tmpappend(pool, s, ",setvendor", 0);
4368   if (how & SOLVER_SETREPO)
4369     s = pool_tmpappend(pool, s, ",setrepo", 0);
4370   if (how & SOLVER_SETNAME)
4371     s = pool_tmpappend(pool, s, ",setname", 0);
4372   if (how & SOLVER_NOAUTOSET)
4373     s = pool_tmpappend(pool, s, ",noautoset", 0);
4374   if (s[o + 1] != ',')
4375     s = pool_tmpappend(pool, s, ",?", 0);
4376   s[o + 1] = '[';
4377   return pool_tmpappend(pool, s, "]", 0);
4378 }
4379