- add SOLVER_DROP_ORPHANED for coolo
[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 "bitmap.h"
22 #include "pool.h"
23 #include "util.h"
24 #include "policy.h"
25 #include "solverdebug.h"
26
27 #define RULES_BLOCK 63
28
29 /********************************************************************
30  *
31  * dependency check helpers
32  *
33  */
34
35 /*-------------------------------------------------------------------
36  * handle split provides
37  */
38
39 int
40 solver_splitprovides(Solver *solv, Id dep)
41 {
42   Pool *pool = solv->pool;
43   Id p, pp;
44   Reldep *rd;
45   Solvable *s;
46
47   if (!solv->dosplitprovides || !solv->installed)
48     return 0;
49   if (!ISRELDEP(dep))
50     return 0;
51   rd = GETRELDEP(pool, dep);
52   if (rd->flags != REL_WITH)
53     return 0;
54   FOR_PROVIDES(p, pp, dep)
55     {
56       s = pool->solvables + p;
57       if (s->repo == solv->installed && s->name == rd->name)
58         return 1;
59     }
60   return 0;
61 }
62
63
64 /*-------------------------------------------------------------------
65  * solver_dep_installed
66  */
67
68 int
69 solver_dep_installed(Solver *solv, Id dep)
70 {
71 #if 0
72   Pool *pool = solv->pool;
73   Id p, pp;
74
75   if (ISRELDEP(dep))
76     {
77       Reldep *rd = GETRELDEP(pool, dep);
78       if (rd->flags == REL_AND)
79         {
80           if (!solver_dep_installed(solv, rd->name))
81             return 0;
82           return solver_dep_installed(solv, rd->evr);
83         }
84       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
85         return solver_dep_installed(solv, rd->evr);
86     }
87   FOR_PROVIDES(p, pp, dep)
88     {
89       if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
90         return 1;
91     }
92 #endif
93   return 0;
94 }
95
96
97
98 /************************************************************************/
99
100 /*
101  * make assertion rules into decisions
102  * 
103  * Go through rules and add direct assertions to the decisionqueue.
104  * If we find a conflict, disable rules and add them to problem queue.
105  */
106
107 static void
108 makeruledecisions(Solver *solv)
109 {
110   Pool *pool = solv->pool;
111   int i, ri, ii;
112   Rule *r, *rr;
113   Id v, vv;
114   int decisionstart;
115
116   POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions ; size decisionq: %d -----\n",solv->decisionq.count);
117
118   /* The system solvable is always installed first */
119   assert(solv->decisionq.count == 0);
120   queue_push(&solv->decisionq, SYSTEMSOLVABLE);
121   queue_push(&solv->decisionq_why, 0);
122   solv->decisionmap[SYSTEMSOLVABLE] = 1;        /* installed at level '1' */
123
124   decisionstart = solv->decisionq.count;
125   for (ii = 0; ii < solv->ruleassertions.count; ii++)
126     {
127       ri = solv->ruleassertions.elements[ii];
128       r = solv->rules + ri;
129         
130       if (r->d < 0 || !r->p || r->w2)   /* disabled, dummy or no assertion */
131         continue;
132       /* do weak rules in phase 2 */
133       if (ri < solv->learntrules && MAPTST(&solv->weakrulemap, ri))
134         continue;
135         
136       v = r->p;
137       vv = v > 0 ? v : -v;
138         
139       if (!solv->decisionmap[vv])          /* if not yet decided */
140         {
141             /*
142              * decide !
143              */
144           queue_push(&solv->decisionq, v);
145           queue_push(&solv->decisionq_why, r - solv->rules);
146           solv->decisionmap[vv] = v > 0 ? 1 : -1;
147           IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
148             {
149               Solvable *s = solv->pool->solvables + vv;
150               if (v < 0)
151                 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", solvable2str(solv->pool, s));
152               else
153                 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing  %s (assertion)\n", solvable2str(solv->pool, s));
154             }
155           continue;
156         }
157         /*
158          * check previous decision: is it sane ?
159          */
160         
161       if (v > 0 && solv->decisionmap[vv] > 0)    /* ok to install */
162         continue;
163       if (v < 0 && solv->decisionmap[vv] < 0)    /* ok to remove */
164         continue;
165         
166         /*
167          * found a conflict!
168          * 
169          * The rule (r) we're currently processing says something
170          * different (v = r->p) than a previous decision (decisionmap[abs(v)])
171          * on this literal
172          */
173         
174       if (ri >= solv->learntrules)
175         {
176           /* conflict with a learnt rule */
177           /* can happen when packages cannot be installed for
178            * multiple reasons. */
179           /* we disable the learnt rule in this case */
180           solver_disablerule(solv, r);
181           continue;
182         }
183         
184         /*
185          * find the decision which is the "opposite" of the rule
186          */
187         
188       for (i = 0; i < solv->decisionq.count; i++)
189         if (solv->decisionq.elements[i] == -v)
190           break;
191       assert(i < solv->decisionq.count);         /* assert that we found it */
192         
193       /*
194        * conflict with system solvable ?
195        */
196         
197       if (v == -SYSTEMSOLVABLE) {
198         /* conflict with system solvable */
199         queue_push(&solv->problems, solv->learnt_pool.count);
200         queue_push(&solv->learnt_pool, ri);
201         queue_push(&solv->learnt_pool, 0);
202         POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
203         if  (ri >= solv->jobrules && ri < solv->jobrules_end)
204           v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
205         else
206           v = ri;
207         queue_push(&solv->problems, v);
208         queue_push(&solv->problems, 0);
209         solver_disableproblem(solv, v);
210         continue;
211       }
212
213       assert(solv->decisionq_why.elements[i] > 0);
214         
215       /*
216        * conflict with an rpm rule ?
217        */
218         
219       if (solv->decisionq_why.elements[i] < solv->rpmrules_end)
220         {
221           /* conflict with rpm rule assertion */
222           queue_push(&solv->problems, solv->learnt_pool.count);
223           queue_push(&solv->learnt_pool, ri);
224           queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
225           queue_push(&solv->learnt_pool, 0);
226           assert(v > 0 || v == -SYSTEMSOLVABLE);
227           POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
228           if (ri >= solv->jobrules && ri < solv->jobrules_end)
229             v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
230           else
231             v = ri;
232           queue_push(&solv->problems, v);
233           queue_push(&solv->problems, 0);
234           solver_disableproblem(solv, v);
235           continue;
236         }
237
238       /*
239        * conflict with another job or update/feature rule
240        */
241         
242       /* record proof */
243       queue_push(&solv->problems, solv->learnt_pool.count);
244       queue_push(&solv->learnt_pool, ri);
245       queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
246       queue_push(&solv->learnt_pool, 0);
247
248       POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflicting update/job assertions over literal %d\n", vv);
249
250         /*
251          * push all of our rules (can only be feature or job rules)
252          * asserting this literal on the problem stack
253          */
254         
255       for (i = solv->featurerules, rr = solv->rules + i; i < solv->learntrules; i++, rr++)
256         {
257           if (rr->d < 0                          /* disabled */
258               || rr->w2)                         /*  or no assertion */
259             continue;
260           if (rr->p != vv                        /* not affecting the literal */
261               && rr->p != -vv)
262             continue;
263           if (MAPTST(&solv->weakrulemap, i))     /* weak: silently ignore */
264             continue;
265             
266           POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
267             
268           solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, solv->rules + i);
269             
270           v = i;
271             /* is is a job rule ? */
272           if (i >= solv->jobrules && i < solv->jobrules_end)
273             v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
274             
275           queue_push(&solv->problems, v);
276           solver_disableproblem(solv, v);
277         }
278       queue_push(&solv->problems, 0);
279
280       /*
281        * start over
282        * (back up from decisions)
283        */
284       while (solv->decisionq.count > decisionstart)
285         {
286           v = solv->decisionq.elements[--solv->decisionq.count];
287           --solv->decisionq_why.count;
288           vv = v > 0 ? v : -v;
289           solv->decisionmap[vv] = 0;
290         }
291       ii = -1; /* restarts loop at 0 */
292     }
293
294   /*
295    * phase 2: now do the weak assertions
296    */
297   for (ii = 0; ii < solv->ruleassertions.count; ii++)
298     {
299       ri = solv->ruleassertions.elements[ii];
300       r = solv->rules + ri;
301       if (r->d < 0 || r->w2)                     /* disabled or no assertion */
302         continue;
303       if (ri >= solv->learntrules || !MAPTST(&solv->weakrulemap, ri))       /* skip non-weak */
304         continue;
305       v = r->p;
306       vv = v > 0 ? v : -v;
307       /*
308        * decide !
309        * (if not yet decided)
310        */
311       if (!solv->decisionmap[vv])
312         {
313           queue_push(&solv->decisionq, v);
314           queue_push(&solv->decisionq_why, r - solv->rules);
315           solv->decisionmap[vv] = v > 0 ? 1 : -1;
316           IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
317             {
318               Solvable *s = solv->pool->solvables + vv;
319               if (v < 0)
320                 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", solvable2str(solv->pool, s));
321               else
322                 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing  %s (weak assertion)\n", solvable2str(solv->pool, s));
323             }
324           continue;
325         }
326       /*
327        * previously decided, sane ?
328        */
329       if (v > 0 && solv->decisionmap[vv] > 0)
330         continue;
331       if (v < 0 && solv->decisionmap[vv] < 0)
332         continue;
333         
334       POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
335       solver_printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
336
337       if (ri >= solv->jobrules && ri < solv->jobrules_end)
338         v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
339       else
340         v = ri;
341       solver_disableproblem(solv, v);
342       if (v < 0)
343         solver_reenablepolicyrules(solv, -(v + 1));
344     }
345   
346   POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions end; size decisionq: %d -----\n",solv->decisionq.count);
347 }
348
349
350 /*-------------------------------------------------------------------
351  * enable/disable learnt rules 
352  *
353  * we have enabled or disabled some of our rules. We now reenable all
354  * of our learnt rules except the ones that were learnt from rules that
355  * are now disabled.
356  */
357 static void
358 enabledisablelearntrules(Solver *solv)
359 {
360   Pool *pool = solv->pool;
361   Rule *r;
362   Id why, *whyp;
363   int i;
364
365   POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
366   for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
367     {
368       whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
369       while ((why = *whyp++) != 0)
370         {
371           assert(why > 0 && why < i);
372           if (solv->rules[why].d < 0)
373             break;
374         }
375       /* why != 0: we found a disabled rule, disable the learnt rule */
376       if (why && r->d >= 0)
377         {
378           IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
379             {
380               POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "disabling ");
381               solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
382             }
383           solver_disablerule(solv, r);
384         }
385       else if (!why && r->d < 0)
386         {
387           IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
388             {
389               POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "re-enabling ");
390               solver_printruleclass(solv, SAT_DEBUG_SOLUTIONS, r);
391             }
392           solver_enablerule(solv, r);
393         }
394     }
395 }
396
397
398 /********************************************************************/
399 /* watches */
400
401
402 /*-------------------------------------------------------------------
403  * makewatches
404  *
405  * initial setup for all watches
406  */
407
408 static void
409 makewatches(Solver *solv)
410 {
411   Rule *r;
412   int i;
413   int nsolvables = solv->pool->nsolvables;
414
415   sat_free(solv->watches);
416                                        /* lower half for removals, upper half for installs */
417   solv->watches = sat_calloc(2 * nsolvables, sizeof(Id));
418 #if 1
419   /* do it reverse so rpm rules get triggered first (XXX: obsolete?) */
420   for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
421 #else
422   for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
423 #endif
424     {
425       if (!r->w2)               /* assertions do not need watches */
426         continue;
427
428       /* see addwatches_rule(solv, r) */
429       r->n1 = solv->watches[nsolvables + r->w1];
430       solv->watches[nsolvables + r->w1] = r - solv->rules;
431
432       r->n2 = solv->watches[nsolvables + r->w2];
433       solv->watches[nsolvables + r->w2] = r - solv->rules;
434     }
435 }
436
437
438 /*-------------------------------------------------------------------
439  *
440  * add watches (for a new learned rule)
441  * sets up watches for a single rule
442  * 
443  * see also makewatches() above.
444  */
445
446 static inline void
447 addwatches_rule(Solver *solv, Rule *r)
448 {
449   int nsolvables = solv->pool->nsolvables;
450
451   r->n1 = solv->watches[nsolvables + r->w1];
452   solv->watches[nsolvables + r->w1] = r - solv->rules;
453
454   r->n2 = solv->watches[nsolvables + r->w2];
455   solv->watches[nsolvables + r->w2] = r - solv->rules;
456 }
457
458
459 /********************************************************************/
460 /*
461  * rule propagation
462  */
463
464
465 /* shortcuts to check if a literal (positive or negative) assignment
466  * evaluates to 'true' or 'false'
467  */
468 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
469 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
470 #define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
471
472 /*-------------------------------------------------------------------
473  * 
474  * propagate
475  *
476  * make decision and propagate to all rules
477  * 
478  * Evaluate each term affected by the decision (linked through watches)
479  * If we find unit rules we make new decisions based on them
480  * 
481  * Everything's fixed there, it's just finding rules that are
482  * unit.
483  * 
484  * return : 0 = everything is OK
485  *          rule = conflict found in this rule
486  */
487
488 static Rule *
489 propagate(Solver *solv, int level)
490 {
491   Pool *pool = solv->pool;
492   Id *rp, *next_rp;           /* rule pointer, next rule pointer in linked list */
493   Rule *r;                    /* rule */
494   Id p, pkg, other_watch;
495   Id *dp;
496   Id *decisionmap = solv->decisionmap;
497     
498   Id *watches = solv->watches + pool->nsolvables;   /* place ptr in middle */
499
500   POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
501
502   /* foreach non-propagated decision */
503   while (solv->propagate_index < solv->decisionq.count)
504     {
505         /*
506          * 'pkg' was just decided
507          * negate because our watches trigger if literal goes FALSE
508          */
509       pkg = -solv->decisionq.elements[solv->propagate_index++];
510         
511       IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
512         {
513           POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
514           solver_printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
515         }
516
517       /* foreach rule where 'pkg' is now FALSE */
518       for (rp = watches + pkg; *rp; rp = next_rp)
519         {
520           r = solv->rules + *rp;
521           if (r->d < 0)
522             {
523               /* rule is disabled, goto next */
524               if (pkg == r->w1)
525                 next_rp = &r->n1;
526               else
527                 next_rp = &r->n2;
528               continue;
529             }
530
531           IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
532             {
533               POOL_DEBUG(SAT_DEBUG_PROPAGATE,"  watch triggered ");
534               solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
535             }
536
537             /* 'pkg' was just decided (was set to FALSE)
538              * 
539              *  now find other literal watch, check clause
540              *   and advance on linked list
541              */
542           if (pkg == r->w1)
543             {
544               other_watch = r->w2;
545               next_rp = &r->n1;
546             }
547           else
548             {
549               other_watch = r->w1;
550               next_rp = &r->n2;
551             }
552             
553             /* 
554              * This term is already true (through the other literal)
555              * so we have nothing to do
556              */
557           if (DECISIONMAP_TRUE(other_watch))
558             continue;
559
560             /*
561              * The other literal is FALSE or UNDEF
562              * 
563              */
564             
565           if (r->d)
566             {
567               /* Not a binary clause, try to move our watch.
568                * 
569                * Go over all literals and find one that is
570                *   not other_watch
571                *   and not FALSE
572                * 
573                * (TRUE is also ok, in that case the rule is fulfilled)
574                */
575               if (r->p                                /* we have a 'p' */
576                   && r->p != other_watch              /* which is not watched */
577                   && !DECISIONMAP_FALSE(r->p))        /* and not FALSE */
578                 {
579                   p = r->p;
580                 }
581               else                                    /* go find a 'd' to make 'true' */
582                 {
583                   /* foreach p in 'd'
584                      we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
585                    */
586                   for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
587                     {
588                       if (p != other_watch              /* which is not watched */
589                           && !DECISIONMAP_FALSE(p))     /* and not FALSE */
590                         break;
591                     }
592                 }
593
594               if (p)
595                 {
596                   /*
597                    * if we found some p that is UNDEF or TRUE, move
598                    * watch to it
599                    */
600                   IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
601                     {
602                       if (p > 0)
603                         POOL_DEBUG(SAT_DEBUG_PROPAGATE, "    -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvid2str(pool, p));
604                       else
605                         POOL_DEBUG(SAT_DEBUG_PROPAGATE,"    -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvid2str(pool, -p));
606                     }
607                     
608                   *rp = *next_rp;
609                   next_rp = rp;
610                     
611                   if (pkg == r->w1)
612                     {
613                       r->w1 = p;
614                       r->n1 = watches[p];
615                     }
616                   else
617                     {
618                       r->w2 = p;
619                       r->n2 = watches[p];
620                     }
621                   watches[p] = r - solv->rules;
622                   continue;
623                 }
624               /* search failed, thus all unwatched literals are FALSE */
625                 
626             } /* not binary */
627             
628             /*
629              * unit clause found, set literal other_watch to TRUE
630              */
631
632           if (DECISIONMAP_FALSE(other_watch))      /* check if literal is FALSE */
633             return r;                              /* eek, a conflict! */
634             
635           IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
636             {
637               POOL_DEBUG(SAT_DEBUG_PROPAGATE, "   unit ");
638               solver_printrule(solv, SAT_DEBUG_PROPAGATE, r);
639             }
640
641           if (other_watch > 0)
642             decisionmap[other_watch] = level;    /* install! */
643           else
644             decisionmap[-other_watch] = -level;  /* remove! */
645             
646           queue_push(&solv->decisionq, other_watch);
647           queue_push(&solv->decisionq_why, r - solv->rules);
648
649           IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
650             {
651               if (other_watch > 0)
652                 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "    -> decided to install %s\n", solvid2str(pool, other_watch));
653               else
654                 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "    -> decided to conflict %s\n", solvid2str(pool, -other_watch));
655             }
656             
657         } /* foreach rule involving 'pkg' */
658         
659     } /* while we have non-decided decisions */
660     
661   POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
662
663   return 0;     /* all is well */
664 }
665
666
667 /********************************************************************/
668 /* Analysis */
669
670 /*-------------------------------------------------------------------
671  * 
672  * analyze
673  *   and learn
674  */
675
676 static int
677 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
678 {
679   Pool *pool = solv->pool;
680   Queue r;
681   int rlevel = 1;
682   Map seen;             /* global? */
683   Id d, v, vv, *dp, why;
684   int l, i, idx;
685   int num = 0, l1num = 0;
686   int learnt_why = solv->learnt_pool.count;
687   Id *decisionmap = solv->decisionmap;
688
689   queue_init(&r);
690
691   POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
692   map_init(&seen, pool->nsolvables);
693   idx = solv->decisionq.count;
694   for (;;)
695     {
696       IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
697         solver_printruleclass(solv, SAT_DEBUG_ANALYZE, c);
698       queue_push(&solv->learnt_pool, c - solv->rules);
699       d = c->d < 0 ? -c->d - 1 : c->d;
700       dp = d ? pool->whatprovidesdata + d : 0;
701       /* go through all literals of the rule */
702       for (i = -1; ; i++)
703         {
704           if (i == -1)
705             v = c->p;
706           else if (d == 0)
707             v = i ? 0 : c->w2;
708           else
709             v = *dp++;
710           if (v == 0)
711             break;
712
713           if (DECISIONMAP_TRUE(v))      /* the one true literal */
714             continue;
715           vv = v > 0 ? v : -v;
716           if (MAPTST(&seen, vv))
717             continue;
718           l = solv->decisionmap[vv];
719           if (l < 0)
720             l = -l;
721           MAPSET(&seen, vv);
722           if (l == 1)
723             l1num++;                    /* need to do this one in level1 pass */
724           else if (l == level)
725             num++;                      /* need to do this one as well */
726           else
727             {
728               queue_push(&r, v);        /* not level1 or conflict level, add to new rule */
729               if (l > rlevel)
730                 rlevel = l;
731             }
732         }
733 l1retry:
734       if (!num && !--l1num)
735         break;  /* all level 1 literals done */
736       for (;;)
737         {
738           assert(idx > 0);
739           v = solv->decisionq.elements[--idx];
740           vv = v > 0 ? v : -v;
741           if (MAPTST(&seen, vv))
742             break;
743         }
744       MAPCLR(&seen, vv);
745       if (num && --num == 0)
746         {
747           *pr = -v;     /* so that v doesn't get lost */
748           if (!l1num)
749             break;
750           POOL_DEBUG(SAT_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
751           for (i = 0; i < r.count; i++)
752             {
753               v = r.elements[i];
754               MAPCLR(&seen, v > 0 ? v : -v);
755             }
756           /* only level 1 marks left */
757           l1num++;
758           goto l1retry;
759         }
760       why = solv->decisionq_why.elements[idx];
761       if (why <= 0)     /* just in case, maybe for SYSTEMSOLVABLE */
762         goto l1retry;
763       c = solv->rules + why;
764     }
765   map_free(&seen);
766
767   if (r.count == 0)
768     *dr = 0;
769   else if (r.count == 1 && r.elements[0] < 0)
770     *dr = r.elements[0];
771   else
772     *dr = pool_queuetowhatprovides(pool, &r);
773   IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
774     {
775       POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
776       solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, *pr);
777       for (i = 0; i < r.count; i++)
778         solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
779     }
780   /* push end marker on learnt reasons stack */
781   queue_push(&solv->learnt_pool, 0);
782   if (whyp)
783     *whyp = learnt_why;
784   solv->stats_learned++;
785   return rlevel;
786 }
787
788
789 /*-------------------------------------------------------------------
790  * 
791  * solver_reset
792  * 
793  * reset the solver decisions to right after the rpm rules.
794  * called after rules have been enabled/disabled
795  */
796
797 void
798 solver_reset(Solver *solv)
799 {
800   Pool *pool = solv->pool;
801   int i;
802   Id v;
803
804   /* rewind all decisions */
805   for (i = solv->decisionq.count - 1; i >= 0; i--)
806     {
807       v = solv->decisionq.elements[i];
808       solv->decisionmap[v > 0 ? v : -v] = 0;
809     }
810   solv->decisionq_why.count = 0;
811   solv->decisionq.count = 0;
812   solv->recommends_index = -1;
813   solv->propagate_index = 0;
814   solv->recommendations.count = 0;
815   solv->branches.count = 0;
816
817   /* adapt learnt rule status to new set of enabled/disabled rules */
818   enabledisablelearntrules(solv);
819
820   /* redo all job/update decisions */
821   makeruledecisions(solv);
822   POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
823 }
824
825
826 /*-------------------------------------------------------------------
827  * 
828  * analyze_unsolvable_rule
829  */
830
831 static void
832 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp)
833 {
834   Pool *pool = solv->pool;
835   int i;
836   Id why = r - solv->rules;
837
838   IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
839     solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
840   if (solv->learntrules && why >= solv->learntrules)
841     {
842       for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
843         if (solv->learnt_pool.elements[i] > 0)
844           analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp);
845       return;
846     }
847   if (MAPTST(&solv->weakrulemap, why))
848     if (!*lastweakp || why > *lastweakp)
849       *lastweakp = why;
850   /* do not add rpm rules to problem */
851   if (why < solv->rpmrules_end)
852     return;
853   /* turn rule into problem */
854   if (why >= solv->jobrules && why < solv->jobrules_end)
855     why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
856   /* normalize dup/infarch rules */
857   if (why > solv->infarchrules && why < solv->infarchrules_end)
858     {
859       Id name = pool->solvables[-solv->rules[why].p].name;
860       while (why > solv->infarchrules && pool->solvables[-solv->rules[why - 1].p].name == name)
861         why--;
862     }
863   if (why > solv->duprules && why < solv->duprules_end)
864     {
865       Id name = pool->solvables[-solv->rules[why].p].name;
866       while (why > solv->duprules && pool->solvables[-solv->rules[why - 1].p].name == name)
867         why--;
868     }
869
870   /* return if problem already countains our rule */
871   if (solv->problems.count)
872     {
873       for (i = solv->problems.count - 1; i >= 0; i--)
874         if (solv->problems.elements[i] == 0)    /* end of last problem reached? */
875           break;
876         else if (solv->problems.elements[i] == why)
877           return;
878     }
879   queue_push(&solv->problems, why);
880 }
881
882
883 /*-------------------------------------------------------------------
884  * 
885  * analyze_unsolvable
886  *
887  * return: 1 - disabled some rules, try again
888  *         0 - hopeless
889  */
890
891 static int
892 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
893 {
894   Pool *pool = solv->pool;
895   Rule *r;
896   Map seen;             /* global to speed things up? */
897   Id d, v, vv, *dp, why;
898   int l, i, idx;
899   Id *decisionmap = solv->decisionmap;
900   int oldproblemcount;
901   int oldlearntpoolcount;
902   Id lastweak;
903
904   POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
905   solv->stats_unsolvable++;
906   oldproblemcount = solv->problems.count;
907   oldlearntpoolcount = solv->learnt_pool.count;
908
909   /* make room for proof index */
910   /* must update it later, as analyze_unsolvable_rule would confuse
911    * it with a rule index if we put the real value in already */
912   queue_push(&solv->problems, 0);
913
914   r = cr;
915   map_init(&seen, pool->nsolvables);
916   queue_push(&solv->learnt_pool, r - solv->rules);
917   lastweak = 0;
918   analyze_unsolvable_rule(solv, r, &lastweak);
919   d = r->d < 0 ? -r->d - 1 : r->d;
920   dp = d ? pool->whatprovidesdata + d : 0;
921   for (i = -1; ; i++)
922     {
923       if (i == -1)
924         v = r->p;
925       else if (d == 0)
926         v = i ? 0 : r->w2;
927       else
928         v = *dp++;
929       if (v == 0)
930         break;
931       if (DECISIONMAP_TRUE(v))  /* the one true literal */
932           continue;
933       vv = v > 0 ? v : -v;
934       l = solv->decisionmap[vv];
935       if (l < 0)
936         l = -l;
937       MAPSET(&seen, vv);
938     }
939   idx = solv->decisionq.count;
940   while (idx > 0)
941     {
942       v = solv->decisionq.elements[--idx];
943       vv = v > 0 ? v : -v;
944       if (!MAPTST(&seen, vv))
945         continue;
946       why = solv->decisionq_why.elements[idx];
947       assert(why > 0);
948       queue_push(&solv->learnt_pool, why);
949       r = solv->rules + why;
950       analyze_unsolvable_rule(solv, r, &lastweak);
951       d = r->d < 0 ? -r->d - 1 : r->d;
952       dp = d ? pool->whatprovidesdata + d : 0;
953       for (i = -1; ; i++)
954         {
955           if (i == -1)
956             v = r->p;
957           else if (d == 0)
958             v = i ? 0 : r->w2;
959           else
960             v = *dp++;
961           if (v == 0)
962             break;
963           if (DECISIONMAP_TRUE(v))      /* the one true literal */
964               continue;
965           vv = v > 0 ? v : -v;
966           l = solv->decisionmap[vv];
967           if (l < 0)
968             l = -l;
969           MAPSET(&seen, vv);
970         }
971     }
972   map_free(&seen);
973   queue_push(&solv->problems, 0);       /* mark end of this problem */
974
975   if (lastweak)
976     {
977       Id v;
978       extern void disablechoicerules(Solver *solv, Rule *r);
979       /* disable last weak rule */
980       solv->problems.count = oldproblemcount;
981       solv->learnt_pool.count = oldlearntpoolcount;
982       if (lastweak >= solv->jobrules && lastweak < solv->jobrules_end)
983         v = -(solv->ruletojob.elements[lastweak - solv->jobrules] + 1);
984       else
985         v = lastweak;
986       POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling ");
987       solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, solv->rules + lastweak);
988       if (lastweak >= solv->choicerules && lastweak < solv->choicerules_end)
989         disablechoicerules(solv, solv->rules + lastweak);
990       solver_disableproblem(solv, v);
991       if (v < 0)
992         solver_reenablepolicyrules(solv, -(v + 1));
993       solver_reset(solv);
994       return 1;
995     }
996
997   /* finish proof */
998   queue_push(&solv->learnt_pool, 0);
999   solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1000
1001   if (disablerules)
1002     {
1003       for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1004         solver_disableproblem(solv, solv->problems.elements[i]);
1005       /* XXX: might want to enable all weak rules again */
1006       solver_reset(solv);
1007       return 1;
1008     }
1009   POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1010   return 0;
1011 }
1012
1013
1014 /********************************************************************/
1015 /* Decision revert */
1016
1017 /*-------------------------------------------------------------------
1018  * 
1019  * revert
1020  * revert decision at level
1021  */
1022
1023 static void
1024 revert(Solver *solv, int level)
1025 {
1026   Pool *pool = solv->pool;
1027   Id v, vv;
1028   while (solv->decisionq.count)
1029     {
1030       v = solv->decisionq.elements[solv->decisionq.count - 1];
1031       vv = v > 0 ? v : -v;
1032       if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1033         break;
1034       POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1035       if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
1036         solv->recommendations.count--;
1037       solv->decisionmap[vv] = 0;
1038       solv->decisionq.count--;
1039       solv->decisionq_why.count--;
1040       solv->propagate_index = solv->decisionq.count;
1041     }
1042   while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1043     {
1044       solv->branches.count--;
1045       while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1046         solv->branches.count--;
1047     }
1048   solv->recommends_index = -1;
1049 }
1050
1051
1052 /*-------------------------------------------------------------------
1053  * 
1054  * watch2onhighest - put watch2 on literal with highest level
1055  */
1056
1057 static inline void
1058 watch2onhighest(Solver *solv, Rule *r)
1059 {
1060   int l, wl = 0;
1061   Id d, v, *dp;
1062
1063   d = r->d < 0 ? -r->d - 1 : r->d;
1064   if (!d)
1065     return;     /* binary rule, both watches are set */
1066   dp = solv->pool->whatprovidesdata + d;
1067   while ((v = *dp++) != 0)
1068     {
1069       l = solv->decisionmap[v < 0 ? -v : v];
1070       if (l < 0)
1071         l = -l;
1072       if (l > wl)
1073         {
1074           r->w2 = dp[-1];
1075           wl = l;
1076         }
1077     }
1078 }
1079
1080
1081 /*-------------------------------------------------------------------
1082  * 
1083  * setpropagatelearn
1084  *
1085  * add free decision (solvable to install) to decisionq
1086  * increase level and propagate decision
1087  * return if no conflict.
1088  *
1089  * in conflict case, analyze conflict rule, add resulting
1090  * rule to learnt rule set, make decision from learnt
1091  * rule (always unit) and re-propagate.
1092  *
1093  * returns the new solver level or 0 if unsolvable
1094  *
1095  */
1096
1097 static int
1098 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules, Id ruleid)
1099 {
1100   Pool *pool = solv->pool;
1101   Rule *r;
1102   Id p = 0, d = 0;
1103   int l, why;
1104
1105   assert(ruleid >= 0);
1106   if (decision)
1107     {
1108       level++;
1109       if (decision > 0)
1110         solv->decisionmap[decision] = level;
1111       else
1112         solv->decisionmap[-decision] = -level;
1113       queue_push(&solv->decisionq, decision);
1114       queue_push(&solv->decisionq_why, -ruleid);        /* <= 0 -> free decision */
1115     }
1116   for (;;)
1117     {
1118       r = propagate(solv, level);
1119       if (!r)
1120         break;
1121       if (level == 1)
1122         return analyze_unsolvable(solv, r, disablerules);
1123       POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1124       l = analyze(solv, level, r, &p, &d, &why);        /* learnt rule in p and d */
1125       assert(l > 0 && l < level);
1126       POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1127       level = l;
1128       revert(solv, level);
1129       r = solver_addrule(solv, p, d);
1130       assert(r);
1131       assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
1132       queue_push(&solv->learnt_why, why);
1133       if (d)
1134         {
1135           /* at least 2 literals, needs watches */
1136           watch2onhighest(solv, r);
1137           addwatches_rule(solv, r);
1138         }
1139       else
1140         {
1141           /* learnt rule is an assertion */
1142           queue_push(&solv->ruleassertions, r - solv->rules);
1143         }
1144       /* the new rule is unit by design */
1145       solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1146       queue_push(&solv->decisionq, p);
1147       queue_push(&solv->decisionq_why, r - solv->rules);
1148       IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1149         {
1150           POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
1151           solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
1152           POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
1153           solver_printrule(solv, SAT_DEBUG_ANALYZE, r);
1154         }
1155     }
1156   return level;
1157 }
1158
1159
1160 /*-------------------------------------------------------------------
1161  * 
1162  * select and install
1163  * 
1164  * install best package from the queue. We add an extra package, inst, if
1165  * provided. See comment in weak install section.
1166  *
1167  * returns the new solver level or 0 if unsolvable
1168  *
1169  */
1170
1171 static int
1172 selectandinstall(Solver *solv, int level, Queue *dq, int disablerules, Id ruleid)
1173 {
1174   Pool *pool = solv->pool;
1175   Id p;
1176   int i;
1177
1178   if (dq->count > 1)
1179     policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1180   if (dq->count > 1)
1181     {
1182       /* XXX: didn't we already do that? */
1183       /* XXX: shouldn't we prefer installed packages? */
1184       /* XXX: move to policy.c? */
1185       /* choose the supplemented one */
1186       for (i = 0; i < dq->count; i++)
1187         if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1188           {
1189             dq->elements[0] = dq->elements[i];
1190             dq->count = 1;
1191             break;
1192           }
1193     }
1194   if (dq->count > 1)
1195     {
1196       /* multiple candidates, open a branch */
1197       for (i = 1; i < dq->count; i++)
1198         queue_push(&solv->branches, dq->elements[i]);
1199       queue_push(&solv->branches, -level);
1200     }
1201   p = dq->elements[0];
1202
1203   POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvid2str(pool, p));
1204
1205   return setpropagatelearn(solv, level, p, disablerules, ruleid);
1206 }
1207
1208
1209 /********************************************************************/
1210 /* Main solver interface */
1211
1212
1213 /*-------------------------------------------------------------------
1214  * 
1215  * solver_create
1216  * create solver structure
1217  *
1218  * pool: all available solvables
1219  * installed: installed Solvables
1220  *
1221  *
1222  * Upon solving, rules are created to flag the Solvables
1223  * of the 'installed' Repo as installed.
1224  */
1225
1226 Solver *
1227 solver_create(Pool *pool)
1228 {
1229   Solver *solv;
1230   solv = (Solver *)sat_calloc(1, sizeof(Solver));
1231   solv->pool = pool;
1232   solv->installed = pool->installed;
1233
1234   transaction_init(&solv->trans, pool);
1235   queue_init(&solv->ruletojob);
1236   queue_init(&solv->decisionq);
1237   queue_init(&solv->decisionq_why);
1238   queue_init(&solv->problems);
1239   queue_init(&solv->suggestions);
1240   queue_init(&solv->recommendations);
1241   queue_init(&solv->orphaned);
1242   queue_init(&solv->learnt_why);
1243   queue_init(&solv->learnt_pool);
1244   queue_init(&solv->branches);
1245   queue_init(&solv->covenantq);
1246   queue_init(&solv->weakruleq);
1247   queue_init(&solv->ruleassertions);
1248
1249   map_init(&solv->recommendsmap, pool->nsolvables);
1250   map_init(&solv->suggestsmap, pool->nsolvables);
1251   map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1252   solv->recommends_index = 0;
1253
1254   solv->decisionmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
1255   solv->nrules = 1;
1256   solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1257   memset(solv->rules, 0, sizeof(Rule));
1258
1259   return solv;
1260 }
1261
1262
1263 /*-------------------------------------------------------------------
1264  * 
1265  * solver_free
1266  */
1267
1268 void
1269 solver_free(Solver *solv)
1270 {
1271   transaction_free(&solv->trans);
1272   queue_free(&solv->job);
1273   queue_free(&solv->ruletojob);
1274   queue_free(&solv->decisionq);
1275   queue_free(&solv->decisionq_why);
1276   queue_free(&solv->learnt_why);
1277   queue_free(&solv->learnt_pool);
1278   queue_free(&solv->problems);
1279   queue_free(&solv->solutions);
1280   queue_free(&solv->suggestions);
1281   queue_free(&solv->recommendations);
1282   queue_free(&solv->orphaned);
1283   queue_free(&solv->branches);
1284   queue_free(&solv->covenantq);
1285   queue_free(&solv->weakruleq);
1286   queue_free(&solv->ruleassertions);
1287
1288   map_free(&solv->recommendsmap);
1289   map_free(&solv->suggestsmap);
1290   map_free(&solv->noupdate);
1291   map_free(&solv->weakrulemap);
1292   map_free(&solv->noobsoletes);
1293
1294   map_free(&solv->updatemap);
1295   map_free(&solv->fixmap);
1296   map_free(&solv->dupmap);
1297   map_free(&solv->dupinvolvedmap);
1298   map_free(&solv->droporphanedmap);
1299
1300   sat_free(solv->decisionmap);
1301   sat_free(solv->rules);
1302   sat_free(solv->watches);
1303   sat_free(solv->obsoletes);
1304   sat_free(solv->obsoletes_data);
1305   sat_free(solv->multiversionupdaters);
1306   sat_free(solv->choicerules_ref);
1307   sat_free(solv);
1308 }
1309
1310
1311 /*-------------------------------------------------------------------
1312  * 
1313  * solver_run_sat
1314  *
1315  * all rules have been set up, now actually run the solver
1316  *
1317  */
1318
1319 void
1320 solver_run_sat(Solver *solv, int disablerules, int doweak)
1321 {
1322   Queue dq;             /* local decisionqueue */
1323   Queue dqs;            /* local decisionqueue for supplements */
1324   int systemlevel;
1325   int level, olevel;
1326   Rule *r;
1327   int i, j, n;
1328   Solvable *s;
1329   Pool *pool = solv->pool;
1330   Id p, *dp;
1331   int minimizationsteps;
1332   int installedpos = solv->installed ? solv->installed->start : 0;
1333
1334   IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1335     {
1336       POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1337       for (i = 1; i < solv->nrules; i++)
1338         solver_printruleclass(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
1339     }
1340
1341   POOL_DEBUG(SAT_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1342
1343   IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
1344     solver_printdecisions(solv);
1345
1346   /* start SAT algorithm */
1347   level = 1;
1348   systemlevel = level + 1;
1349   POOL_DEBUG(SAT_DEBUG_SOLVER, "solving...\n");
1350
1351   queue_init(&dq);
1352   queue_init(&dqs);
1353
1354   /*
1355    * here's the main loop:
1356    * 1) propagate new decisions (only needed once)
1357    * 2) fulfill jobs
1358    * 3) try to keep installed packages
1359    * 4) fulfill all unresolved rules
1360    * 5) install recommended packages
1361    * 6) minimalize solution if we had choices
1362    * if we encounter a problem, we rewind to a safe level and restart
1363    * with step 1
1364    */
1365    
1366   minimizationsteps = 0;
1367   for (;;)
1368     {
1369       /*
1370        * initial propagation of the assertions
1371        */
1372       if (level == 1)
1373         {
1374           POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d;  size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1375           if ((r = propagate(solv, level)) != 0)
1376             {
1377               if (analyze_unsolvable(solv, r, disablerules))
1378                 continue;
1379               queue_free(&dq);
1380               queue_free(&dqs);
1381               return;
1382             }
1383         }
1384
1385       /*
1386        * resolve jobs first
1387        */
1388      if (level < systemlevel)
1389         {
1390           POOL_DEBUG(SAT_DEBUG_SOLVER, "resolving job rules\n");
1391           for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1392             {
1393               Id l;
1394               if (r->d < 0)             /* ignore disabled rules */
1395                 continue;
1396               queue_empty(&dq);
1397               FOR_RULELITERALS(l, dp, r)
1398                 {
1399                   if (l < 0)
1400                     {
1401                       if (solv->decisionmap[-l] <= 0)
1402                         break;
1403                     }
1404                   else
1405                     {
1406                       if (solv->decisionmap[l] > 0)
1407                         break;
1408                       if (solv->decisionmap[l] == 0)
1409                         queue_push(&dq, l);
1410                     }
1411                 }
1412               if (l || !dq.count)
1413                 continue;
1414               /* prune to installed if not updating */
1415               if (!solv->updatesystem && solv->installed && dq.count > 1)
1416                 {
1417                   int j, k;
1418                   for (j = k = 0; j < dq.count; j++)
1419                     {
1420                       Solvable *s = pool->solvables + dq.elements[j];
1421                       if (s->repo == solv->installed)
1422                         dq.elements[k++] = dq.elements[j];
1423                     }
1424                   if (k)
1425                     dq.count = k;
1426                 }
1427               olevel = level;
1428               level = selectandinstall(solv, level, &dq, disablerules, i);
1429               if (level == 0)
1430                 {
1431                   queue_free(&dq);
1432                   queue_free(&dqs);
1433                   return;
1434                 }
1435               if (level <= olevel)
1436                 break;
1437             }
1438           systemlevel = level + 1;
1439           if (i < solv->jobrules_end)
1440             continue;
1441         }
1442
1443
1444       /*
1445        * installed packages
1446        */
1447       if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1448         {
1449           Repo *installed = solv->installed;
1450           int pass;
1451
1452           POOL_DEBUG(SAT_DEBUG_SOLVER, "resolving installed packages\n");
1453           /* we use two passes if we need to update packages 
1454            * to create a better user experience */
1455           for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1456             {
1457               int passlevel = level;
1458               /* start with installedpos, the position that gave us problems last time */
1459               for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1460                 {
1461                   Rule *rr;
1462                   Id d;
1463
1464                   if (i == installed->end)
1465                     i = installed->start;
1466                   s = pool->solvables + i;
1467                   if (s->repo != installed)
1468                     continue;
1469
1470                   if (solv->decisionmap[i] > 0)
1471                     continue;
1472                   /* XXX: noupdate check is probably no longer needed, as all jobs should
1473                    * already be satisfied */
1474                   if (MAPTST(&solv->noupdate, i - installed->start))
1475                     continue;
1476                   if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1477                     continue;           /* updates first */
1478                   r = solv->rules + solv->updaterules + (i - installed->start);
1479                   rr = r;
1480                   if (!rr->p || rr->d < 0)      /* disabled -> look at feature rule */
1481                     rr -= solv->installed->end - solv->installed->start;
1482                   if (!rr->p)           /* identical to update rule? */
1483                     rr = r;
1484                   if (!rr->p)
1485                     continue;           /* orpaned package */
1486
1487                   queue_empty(&dq);
1488                   if (solv->decisionmap[i] < 0 || solv->updatesystem || (solv->updatemap.size && MAPTST(&solv->updatemap, i - installed->start)) || rr->p != i)
1489                     {
1490                       if (solv->noobsoletes.size && solv->multiversionupdaters
1491                              && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1492                         {
1493                           /* special multiversion handling, make sure best version is chosen */
1494                           queue_push(&dq, i);
1495                           while ((p = pool->whatprovidesdata[d++]) != 0)
1496                             if (solv->decisionmap[p] >= 0)
1497                               queue_push(&dq, p);
1498                           policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1499                           p = dq.elements[0];
1500                           if (p != i && solv->decisionmap[p] == 0)
1501                             {
1502                               rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1503                               if (!rr->p)               /* update rule == feature rule? */
1504                                 rr = rr - solv->featurerules + solv->updaterules;
1505                               dq.count = 1;
1506                             }
1507                           else
1508                             dq.count = 0;
1509                         }
1510                       else
1511                         {
1512                           /* update to best package */
1513                           FOR_RULELITERALS(p, dp, rr)
1514                             {
1515                               if (solv->decisionmap[p] > 0)
1516                                 {
1517                                   dq.count = 0;         /* already fulfilled */
1518                                   break;
1519                                 }
1520                               if (!solv->decisionmap[p])
1521                                 queue_push(&dq, p);
1522                             }
1523                         }
1524                     }
1525                   /* install best version */
1526                   if (dq.count)
1527                     {
1528                       olevel = level;
1529                       level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1530                       if (level == 0)
1531                         {
1532                           queue_free(&dq);
1533                           queue_free(&dqs);
1534                           return;
1535                         }
1536                       if (level <= olevel)
1537                         {
1538                           if (level == 1 || level < passlevel)
1539                             break;      /* trouble */
1540                           if (level < olevel)
1541                             n = installed->start;       /* redo all */
1542                           i--;
1543                           n--;
1544                           continue;
1545                         }
1546                     }
1547                   /* if still undecided keep package */
1548                   if (solv->decisionmap[i] == 0)
1549                     {
1550                       olevel = level;
1551                       POOL_DEBUG(SAT_DEBUG_POLICY, "keeping %s\n", solvid2str(pool, i));
1552                       level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1553                       if (level == 0)
1554                         {
1555                           queue_free(&dq);
1556                           queue_free(&dqs);
1557                           return;
1558                         }
1559                       if (level <= olevel)
1560                         {
1561                           if (level == 1 || level < passlevel)
1562                             break;      /* trouble */
1563                           if (level < olevel)
1564                             n = installed->start;       /* redo all */
1565                           i--;
1566                           n--;
1567                           continue; /* retry with learnt rule */
1568                         }
1569                     }
1570                 }
1571               if (n < installed->end)
1572                 {
1573                   installedpos = i;     /* retry problem solvable next time */
1574                   break;                /* ran into trouble */
1575                 }
1576               installedpos = installed->start;  /* reset installedpos */
1577             }
1578           systemlevel = level + 1;
1579           if (pass < 2)
1580             continue;           /* had trouble, retry */
1581         }
1582
1583       if (level < systemlevel)
1584         systemlevel = level;
1585
1586       /*
1587        * decide
1588        */
1589       POOL_DEBUG(SAT_DEBUG_POLICY, "deciding unresolved rules\n");
1590       for (i = 1, n = 1; n < solv->nrules; i++, n++)
1591         {
1592           if (i == solv->nrules)
1593             i = 1;
1594           r = solv->rules + i;
1595           if (r->d < 0)         /* ignore disabled rules */
1596             continue;
1597           queue_empty(&dq);
1598           if (r->d == 0)
1599             {
1600               /* binary or unary rule */
1601               /* need two positive undecided literals */
1602               if (r->p < 0 || r->w2 <= 0)
1603                 continue;
1604               if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1605                 continue;
1606               queue_push(&dq, r->p);
1607               queue_push(&dq, r->w2);
1608             }
1609           else
1610             {
1611               /* make sure that
1612                * all negative literals are installed
1613                * no positive literal is installed
1614                * i.e. the rule is not fulfilled and we
1615                * just need to decide on the positive literals
1616                */
1617               if (r->p < 0)
1618                 {
1619                   if (solv->decisionmap[-r->p] <= 0)
1620                     continue;
1621                 }
1622               else
1623                 {
1624                   if (solv->decisionmap[r->p] > 0)
1625                     continue;
1626                   if (solv->decisionmap[r->p] == 0)
1627                     queue_push(&dq, r->p);
1628                 }
1629               dp = pool->whatprovidesdata + r->d;
1630               while ((p = *dp++) != 0)
1631                 {
1632                   if (p < 0)
1633                     {
1634                       if (solv->decisionmap[-p] <= 0)
1635                         break;
1636                     }
1637                   else
1638                     {
1639                       if (solv->decisionmap[p] > 0)
1640                         break;
1641                       if (solv->decisionmap[p] == 0)
1642                         queue_push(&dq, p);
1643                     }
1644                 }
1645               if (p)
1646                 continue;
1647             }
1648           IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1649             {
1650               POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
1651               solver_printruleclass(solv, SAT_DEBUG_PROPAGATE, r);
1652             }
1653           /* dq.count < 2 cannot happen as this means that
1654            * the rule is unit */
1655           assert(dq.count > 1);
1656
1657           olevel = level;
1658           level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
1659           if (level == 0)
1660             {
1661               queue_free(&dq);
1662               queue_free(&dqs);
1663               return;
1664             }
1665           if (level < systemlevel || level == 1)
1666             break;              /* trouble */
1667           /* something changed, so look at all rules again */
1668           n = 0;
1669         }
1670
1671       if (n != solv->nrules)    /* ran into trouble, restart */
1672         continue;
1673
1674       /* at this point we have a consistent system. now do the extras... */
1675
1676       if (doweak)
1677         {
1678           int qcount;
1679
1680           POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended packages\n");
1681           queue_empty(&dq);     /* recommended packages */
1682           queue_empty(&dqs);    /* supplemented packages */
1683           for (i = 1; i < pool->nsolvables; i++)
1684             {
1685               if (solv->decisionmap[i] < 0)
1686                 continue;
1687               if (solv->decisionmap[i] > 0)
1688                 {
1689                   /* installed, check for recommends */
1690                   Id *recp, rec, pp, p;
1691                   s = pool->solvables + i;
1692                   if (solv->ignorealreadyrecommended && s->repo == solv->installed)
1693                     continue;
1694                   /* XXX need to special case AND ? */
1695                   if (s->recommends)
1696                     {
1697                       recp = s->repo->idarraydata + s->recommends;
1698                       while ((rec = *recp++) != 0)
1699                         {
1700                           qcount = dq.count;
1701                           FOR_PROVIDES(p, pp, rec)
1702                             {
1703                               if (solv->decisionmap[p] > 0)
1704                                 {
1705                                   dq.count = qcount;
1706                                   break;
1707                                 }
1708                               else if (solv->decisionmap[p] == 0)
1709                                 {
1710                                   queue_pushunique(&dq, p);
1711                                 }
1712                             }
1713                         }
1714                     }
1715                 }
1716               else
1717                 {
1718                   s = pool->solvables + i;
1719                   if (!s->supplements)
1720                     continue;
1721                   if (!pool_installable(pool, s))
1722                     continue;
1723                   if (!solver_is_supplementing(solv, s))
1724                     continue;
1725                   queue_push(&dqs, i);
1726                 }
1727             }
1728
1729           /* filter out all packages obsoleted by installed packages */
1730           /* this is no longer needed if we have reverse obsoletes */
1731           if ((dqs.count || dq.count) && solv->installed)
1732             {
1733               Map obsmap;
1734               Id obs, *obsp, po, ppo;
1735
1736               map_init(&obsmap, pool->nsolvables);
1737               for (p = solv->installed->start; p < solv->installed->end; p++)
1738                 {
1739                   s = pool->solvables + p;
1740                   if (s->repo != solv->installed || !s->obsoletes)
1741                     continue;
1742                   if (solv->decisionmap[p] <= 0)
1743                     continue;
1744                   if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
1745                     continue;
1746                   obsp = s->repo->idarraydata + s->obsoletes;
1747                   /* foreach obsoletes */
1748                   while ((obs = *obsp++) != 0)
1749                     FOR_PROVIDES(po, ppo, obs)
1750                       MAPSET(&obsmap, po);
1751                 }
1752               for (i = j = 0; i < dqs.count; i++)
1753                 if (!MAPTST(&obsmap, dqs.elements[i]))
1754                   dqs.elements[j++] = dqs.elements[i];
1755               dqs.count = j;
1756               for (i = j = 0; i < dq.count; i++)
1757                 if (!MAPTST(&obsmap, dq.elements[i]))
1758                   dq.elements[j++] = dq.elements[i];
1759               dq.count = j;
1760               map_free(&obsmap);
1761             }
1762
1763           /* filter out all already supplemented packages if requested */
1764           if (solv->ignorealreadyrecommended && dqs.count)
1765             {
1766               /* turn off all new packages */
1767               for (i = 0; i < solv->decisionq.count; i++)
1768                 {
1769                   p = solv->decisionq.elements[i];
1770                   if (p < 0)
1771                     continue;
1772                   s = pool->solvables + p;
1773                   if (s->repo && s->repo != solv->installed)
1774                     solv->decisionmap[p] = -solv->decisionmap[p];
1775                 }
1776               /* filter out old supplements */
1777               for (i = j = 0; i < dqs.count; i++)
1778                 {
1779                   p = dqs.elements[i];
1780                   s = pool->solvables + p;
1781                   if (!s->supplements)
1782                     continue;
1783                   if (!solver_is_supplementing(solv, s))
1784                     dqs.elements[j++] = p;
1785                 }
1786               dqs.count = j;
1787               /* undo turning off */
1788               for (i = 0; i < solv->decisionq.count; i++)
1789                 {
1790                   p = solv->decisionq.elements[i];
1791                   if (p < 0)
1792                     continue;
1793                   s = pool->solvables + p;
1794                   if (s->repo && s->repo != solv->installed)
1795                     solv->decisionmap[p] = -solv->decisionmap[p];
1796                 }
1797             }
1798
1799           /* multiversion doesn't mix well with supplements.
1800            * filter supplemented packages where we already decided
1801            * to install a different version (see bnc#501088) */
1802           if (dqs.count && solv->noobsoletes.size)
1803             {
1804               for (i = j = 0; i < dqs.count; i++)
1805                 {
1806                   p = dqs.elements[i];
1807                   if (MAPTST(&solv->noobsoletes, p))
1808                     {
1809                       Id p2, pp2;
1810                       s = pool->solvables + p;
1811                       FOR_PROVIDES(p2, pp2, s->name)
1812                         if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
1813                           break;
1814                       if (p2)
1815                         continue;       /* ignore this package */
1816                     }
1817                   dqs.elements[j++] = p;
1818                 }
1819               dqs.count = j;
1820             }
1821
1822           /* make dq contain both recommended and supplemented pkgs */
1823           if (dqs.count)
1824             {
1825               for (i = 0; i < dqs.count; i++)
1826                 queue_pushunique(&dq, dqs.elements[i]);
1827             }
1828
1829           if (dq.count)
1830             {
1831               Map dqmap;
1832               int decisioncount = solv->decisionq.count;
1833
1834               if (dq.count == 1)
1835                 {
1836                   /* simple case, just one package. no need to choose  */
1837                   p = dq.elements[0];
1838                   if (dqs.count)
1839                     POOL_DEBUG(SAT_DEBUG_POLICY, "installing supplemented %s\n", solvid2str(pool, p));
1840                   else
1841                     POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended %s\n", solvid2str(pool, p));
1842                   queue_push(&solv->recommendations, p);
1843                   level = setpropagatelearn(solv, level, p, 0, 0);
1844                   continue;     /* back to main loop */
1845                 }
1846
1847               /* filter packages, this gives us the best versions */
1848               policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
1849
1850               /* create map of result */
1851               map_init(&dqmap, pool->nsolvables);
1852               for (i = 0; i < dq.count; i++)
1853                 MAPSET(&dqmap, dq.elements[i]);
1854
1855               /* install all supplemented packages */
1856               for (i = 0; i < dqs.count; i++)
1857                 {
1858                   p = dqs.elements[i];
1859                   if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
1860                     continue;
1861                   POOL_DEBUG(SAT_DEBUG_POLICY, "installing supplemented %s\n", solvid2str(pool, p));
1862                   queue_push(&solv->recommendations, p);
1863                   olevel = level;
1864                   level = setpropagatelearn(solv, level, p, 0, 0);
1865                   if (level <= olevel)
1866                     break;
1867                 }
1868               if (i < dqs.count || solv->decisionq.count < decisioncount)
1869                 {
1870                   map_free(&dqmap);
1871                   continue;
1872                 }
1873
1874               /* install all recommended packages */
1875               /* more work as we want to created branches if multiple
1876                * choices are valid */
1877               for (i = 0; i < decisioncount; i++)
1878                 {
1879                   Id rec, *recp, pp;
1880                   p = solv->decisionq.elements[i];
1881                   if (p < 0)
1882                     continue;
1883                   s = pool->solvables + p;
1884                   if (!s->repo || (solv->ignorealreadyrecommended && s->repo == solv->installed))
1885                     continue;
1886                   if (!s->recommends)
1887                     continue;
1888                   recp = s->repo->idarraydata + s->recommends;
1889                   while ((rec = *recp++) != 0)
1890                     {
1891                       queue_empty(&dq);
1892                       FOR_PROVIDES(p, pp, rec)
1893                         {
1894                           if (solv->decisionmap[p] > 0)
1895                             {
1896                               dq.count = 0;
1897                               break;
1898                             }
1899                           else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
1900                             queue_pushunique(&dq, p);
1901                         }
1902                       if (!dq.count)
1903                         continue;
1904                       if (dq.count > 1)
1905                         {
1906                           /* multiple candidates, open a branch */
1907                           for (i = 1; i < dq.count; i++)
1908                             queue_push(&solv->branches, dq.elements[i]);
1909                           queue_push(&solv->branches, -level);
1910                         }
1911                       p = dq.elements[0];
1912                       POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended %s\n", solvid2str(pool, p));
1913                       queue_push(&solv->recommendations, p);
1914                       olevel = level;
1915                       level = setpropagatelearn(solv, level, p, 0, 0);
1916                       if (level <= olevel || solv->decisionq.count < decisioncount)
1917                         break;  /* we had to revert some decisions */
1918                     }
1919                   if (rec)
1920                     break;      /* had a problem above, quit loop */
1921                 }
1922               map_free(&dqmap);
1923
1924               continue;         /* back to main loop so that all deps are checked */
1925             }
1926         }
1927
1928      if (solv->distupgrade && solv->installed)
1929         {
1930           int installedone = 0;
1931
1932           /* let's see if we can install some unsupported package */
1933           POOL_DEBUG(SAT_DEBUG_SOLVER, "deciding orphaned packages\n");
1934           for (i = 0; i < solv->orphaned.count; i++)
1935             {
1936               p = solv->orphaned.elements[i];
1937               if (solv->decisionmap[p])
1938                 continue;       /* already decided */
1939               olevel = level;
1940               if (solv->distupgrade_removeunsupported)
1941                 continue;
1942               if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
1943                 continue;
1944               POOL_DEBUG(SAT_DEBUG_SOLVER, "keeping orphaned %s\n", solvid2str(pool, p));
1945               level = setpropagatelearn(solv, level, p, 0, 0);
1946               installedone = 1;
1947               if (level < olevel)
1948                 break;
1949             }
1950           if (installedone || i < solv->orphaned.count)
1951             continue;           /* back to main loop */
1952           for (i = 0; i < solv->orphaned.count; i++)
1953             {
1954               p = solv->orphaned.elements[i];
1955               if (solv->decisionmap[p])
1956                 continue;       /* already decided */
1957               POOL_DEBUG(SAT_DEBUG_SOLVER, "removing orphaned %s\n", solvid2str(pool, p));
1958               olevel = level;
1959               level = setpropagatelearn(solv, level, -p, 0, 0);
1960               if (level < olevel)
1961                 break;
1962             }
1963           if (i < solv->orphaned.count)
1964             continue;           /* back to main loop */
1965         }
1966
1967      if (solv->solution_callback)
1968         {
1969           solv->solution_callback(solv, solv->solution_callback_data);
1970           if (solv->branches.count)
1971             {
1972               int i = solv->branches.count - 1;
1973               int l = -solv->branches.elements[i];
1974               Id why;
1975
1976               for (; i > 0; i--)
1977                 if (solv->branches.elements[i - 1] < 0)
1978                   break;
1979               p = solv->branches.elements[i];
1980               POOL_DEBUG(SAT_DEBUG_SOLVER, "branching with %s\n", solvid2str(pool, p));
1981               queue_empty(&dq);
1982               for (j = i + 1; j < solv->branches.count; j++)
1983                 queue_push(&dq, solv->branches.elements[j]);
1984               solv->branches.count = i;
1985               level = l;
1986               revert(solv, level);
1987               if (dq.count > 1)
1988                 for (j = 0; j < dq.count; j++)
1989                   queue_push(&solv->branches, dq.elements[j]);
1990               olevel = level;
1991               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
1992               assert(why >= 0);
1993               level = setpropagatelearn(solv, level, p, disablerules, why);
1994               if (level == 0)
1995                 {
1996                   queue_free(&dq);
1997                   queue_free(&dqs);
1998                   return;
1999                 }
2000               continue;
2001             }
2002           /* all branches done, we're finally finished */
2003           break;
2004         }
2005
2006       /* minimization step */
2007      if (solv->branches.count)
2008         {
2009           int l = 0, lasti = -1, lastl = -1;
2010           Id why;
2011
2012           p = 0;
2013           for (i = solv->branches.count - 1; i >= 0; i--)
2014             {
2015               p = solv->branches.elements[i];
2016               if (p < 0)
2017                 l = -p;
2018               else if (p > 0 && solv->decisionmap[p] > l + 1)
2019                 {
2020                   lasti = i;
2021                   lastl = l;
2022                 }
2023             }
2024           if (lasti >= 0)
2025             {
2026               /* kill old solvable so that we do not loop */
2027               p = solv->branches.elements[lasti];
2028               solv->branches.elements[lasti] = 0;
2029               POOL_DEBUG(SAT_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, solvid2str(pool, p));
2030               minimizationsteps++;
2031
2032               level = lastl;
2033               revert(solv, level);
2034               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2035               assert(why >= 0);
2036               olevel = level;
2037               level = setpropagatelearn(solv, level, p, disablerules, why);
2038               if (level == 0)
2039                 {
2040                   queue_free(&dq);
2041                   queue_free(&dqs);
2042                   return;
2043                 }
2044               continue;         /* back to main loop */
2045             }
2046         }
2047       /* no minimization found, we're finally finished! */
2048       break;
2049     }
2050
2051   POOL_DEBUG(SAT_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2052
2053   POOL_DEBUG(SAT_DEBUG_STATS, "done solving.\n\n");
2054   queue_free(&dq);
2055   queue_free(&dqs);
2056 #if 0
2057   solver_printdecisionq(solv, SAT_DEBUG_RESULT);
2058 #endif
2059 }
2060
2061
2062 /*-------------------------------------------------------------------
2063  * 
2064  * remove disabled conflicts
2065  *
2066  * purpose: update the decisionmap after some rules were disabled.
2067  * this is used to calculate the suggested/recommended package list.
2068  * Also returns a "removed" list to undo the discisionmap changes.
2069  */
2070
2071 static void
2072 removedisabledconflicts(Solver *solv, Queue *removed)
2073 {
2074   Pool *pool = solv->pool;
2075   int i, n;
2076   Id p, why, *dp;
2077   Id new;
2078   Rule *r;
2079   Id *decisionmap = solv->decisionmap;
2080
2081   POOL_DEBUG(SAT_DEBUG_SCHUBI, "removedisabledconflicts\n");
2082   queue_empty(removed);
2083   for (i = 0; i < solv->decisionq.count; i++)
2084     {
2085       p = solv->decisionq.elements[i];
2086       if (p > 0)
2087         continue;
2088       /* a conflict. we never do conflicts on free decisions, so there
2089        * must have been an unit rule */
2090       why = solv->decisionq_why.elements[i];
2091       assert(why > 0);
2092       r = solv->rules + why;
2093       if (r->d < 0 && decisionmap[-p])
2094         {
2095           /* rule is now disabled, remove from decisionmap */
2096           POOL_DEBUG(SAT_DEBUG_SCHUBI, "removing conflict for package %s[%d]\n", solvid2str(pool, -p), -p);
2097           queue_push(removed, -p);
2098           queue_push(removed, decisionmap[-p]);
2099           decisionmap[-p] = 0;
2100         }
2101     }
2102   if (!removed->count)
2103     return;
2104   /* we removed some confliced packages. some of them might still
2105    * be in conflict, so search for unit rules and re-conflict */
2106   new = 0;
2107   for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2108     {
2109       if (i == solv->nrules)
2110         {
2111           i = 1;
2112           r = solv->rules + i;
2113         }
2114       if (r->d < 0)
2115         continue;
2116       if (!r->w2)
2117         {
2118           if (r->p < 0 && !decisionmap[-r->p])
2119             new = r->p;
2120         }
2121       else if (!r->d)
2122         {
2123           /* binary rule */
2124           if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2125             new = r->p;
2126           else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2127             new = r->w2;
2128         }
2129       else
2130         {
2131           if (r->p < 0 && decisionmap[-r->p] == 0)
2132             new = r->p;
2133           if (new || DECISIONMAP_FALSE(r->p))
2134             {
2135               dp = pool->whatprovidesdata + r->d;
2136               while ((p = *dp++) != 0)
2137                 {
2138                   if (new && p == new)
2139                     continue;
2140                   if (p < 0 && decisionmap[-p] == 0)
2141                     {
2142                       if (new)
2143                         {
2144                           new = 0;
2145                           break;
2146                         }
2147                       new = p;
2148                     }
2149                   else if (!DECISIONMAP_FALSE(p))
2150                     {
2151                       new = 0;
2152                       break;
2153                     }
2154                 }
2155             }
2156         }
2157       if (new)
2158         {
2159           POOL_DEBUG(SAT_DEBUG_SCHUBI, "re-conflicting package %s[%d]\n", solvid2str(pool, -new), -new);
2160           decisionmap[-new] = -1;
2161           new = 0;
2162           n = 0;        /* redo all rules */
2163         }
2164     }
2165 }
2166
2167 static inline void
2168 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2169 {
2170   int i;
2171   for (i = 0; i < removed->count; i += 2)
2172     solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2173 }
2174
2175
2176 /*-------------------------------------------------------------------
2177  *
2178  * weaken solvable dependencies
2179  */
2180
2181 static void
2182 weaken_solvable_deps(Solver *solv, Id p)
2183 {
2184   int i;
2185   Rule *r;
2186
2187   for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2188     {
2189       if (r->p != -p)
2190         continue;
2191       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2192         continue;       /* conflict */
2193       queue_push(&solv->weakruleq, i);
2194     }
2195 }
2196
2197
2198 /********************************************************************/
2199 /* main() */
2200
2201
2202 static void
2203 findrecommendedsuggested(Solver *solv)
2204 {
2205   Pool *pool = solv->pool;
2206   Queue redoq, disabledq;
2207   int goterase, i;
2208   Solvable *s;
2209   Rule *r;
2210   Map obsmap;
2211
2212   map_init(&obsmap, pool->nsolvables);
2213   if (solv->installed)
2214     {
2215       Id obs, *obsp, p, po, ppo;
2216       for (p = solv->installed->start; p < solv->installed->end; p++)
2217         {
2218           s = pool->solvables + p;
2219           if (s->repo != solv->installed || !s->obsoletes)
2220             continue;
2221           if (solv->decisionmap[p] <= 0)
2222             continue;
2223           if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
2224             continue;
2225           obsp = s->repo->idarraydata + s->obsoletes;
2226           /* foreach obsoletes */
2227           while ((obs = *obsp++) != 0)
2228             FOR_PROVIDES(po, ppo, obs)
2229               MAPSET(&obsmap, po);
2230         }
2231     }
2232
2233   queue_init(&redoq);
2234   queue_init(&disabledq);
2235   goterase = 0;
2236   /* disable all erase jobs (including weak "keep uninstalled" rules) */
2237   for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2238     {
2239       if (r->d < 0)     /* disabled ? */
2240         continue;
2241       if (r->p >= 0)    /* install job? */
2242         continue;
2243       queue_push(&disabledq, i);
2244       solver_disablerule(solv, r);
2245       goterase++;
2246     }
2247   
2248   if (goterase)
2249     {
2250       enabledisablelearntrules(solv);
2251       removedisabledconflicts(solv, &redoq);
2252     }
2253
2254   /*
2255    * find recommended packages
2256    */
2257     
2258   /* if redoq.count == 0 we already found all recommended in the
2259    * solver run */
2260   if (redoq.count || solv->dontinstallrecommended || !solv->dontshowinstalledrecommended || solv->ignorealreadyrecommended)
2261     {
2262       Id rec, *recp, p, pp;
2263
2264       /* create map of all recommened packages */
2265       solv->recommends_index = -1;
2266       MAPZERO(&solv->recommendsmap);
2267       for (i = 0; i < solv->decisionq.count; i++)
2268         {
2269           p = solv->decisionq.elements[i];
2270           if (p < 0)
2271             continue;
2272           s = pool->solvables + p;
2273           if (s->recommends)
2274             {
2275               recp = s->repo->idarraydata + s->recommends;
2276               while ((rec = *recp++) != 0)
2277                 {
2278                   FOR_PROVIDES(p, pp, rec)
2279                     if (solv->decisionmap[p] > 0)
2280                       break;
2281                   if (p)
2282                     {
2283                       if (!solv->dontshowinstalledrecommended)
2284                         {
2285                           FOR_PROVIDES(p, pp, rec)
2286                             if (solv->decisionmap[p] > 0)
2287                               MAPSET(&solv->recommendsmap, p);
2288                         }
2289                       continue; /* p != 0: already fulfilled */
2290                     }
2291                   FOR_PROVIDES(p, pp, rec)
2292                     MAPSET(&solv->recommendsmap, p);
2293                 }
2294             }
2295         }
2296       for (i = 1; i < pool->nsolvables; i++)
2297         {
2298           if (solv->decisionmap[i] < 0)
2299             continue;
2300           if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2301             continue;
2302           if (MAPTST(&obsmap, i))
2303             continue;
2304           s = pool->solvables + i;
2305           if (!MAPTST(&solv->recommendsmap, i))
2306             {
2307               if (!s->supplements)
2308                 continue;
2309               if (!pool_installable(pool, s))
2310                 continue;
2311               if (!solver_is_supplementing(solv, s))
2312                 continue;
2313             }
2314           if (solv->dontinstallrecommended)
2315             queue_push(&solv->recommendations, i);
2316           else
2317             queue_pushunique(&solv->recommendations, i);
2318         }
2319       /* we use MODE_SUGGEST here so that repo prio is ignored */
2320       policy_filter_unwanted(solv, &solv->recommendations, POLICY_MODE_SUGGEST);
2321     }
2322
2323   /*
2324    * find suggested packages
2325    */
2326     
2327   if (1)
2328     {
2329       Id sug, *sugp, p, pp;
2330
2331       /* create map of all suggests that are still open */
2332       solv->recommends_index = -1;
2333       MAPZERO(&solv->suggestsmap);
2334       for (i = 0; i < solv->decisionq.count; i++)
2335         {
2336           p = solv->decisionq.elements[i];
2337           if (p < 0)
2338             continue;
2339           s = pool->solvables + p;
2340           if (s->suggests)
2341             {
2342               sugp = s->repo->idarraydata + s->suggests;
2343               while ((sug = *sugp++) != 0)
2344                 {
2345                   FOR_PROVIDES(p, pp, sug)
2346                     if (solv->decisionmap[p] > 0)
2347                       break;
2348                   if (p)
2349                     {
2350                       if (!solv->dontshowinstalledrecommended)
2351                         {
2352                           FOR_PROVIDES(p, pp, sug)
2353                             if (solv->decisionmap[p] > 0)
2354                               MAPSET(&solv->suggestsmap, p);
2355                         }
2356                       continue; /* already fulfilled */
2357                     }
2358                   FOR_PROVIDES(p, pp, sug)
2359                     MAPSET(&solv->suggestsmap, p);
2360                 }
2361             }
2362         }
2363       for (i = 1; i < pool->nsolvables; i++)
2364         {
2365           if (solv->decisionmap[i] < 0)
2366             continue;
2367           if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2368             continue;
2369           if (MAPTST(&obsmap, i))
2370             continue;
2371           s = pool->solvables + i;
2372           if (!MAPTST(&solv->suggestsmap, i))
2373             {
2374               if (!s->enhances)
2375                 continue;
2376               if (!pool_installable(pool, s))
2377                 continue;
2378               if (!solver_is_enhancing(solv, s))
2379                 continue;
2380             }
2381           queue_push(&solv->suggestions, i);
2382         }
2383       policy_filter_unwanted(solv, &solv->suggestions, POLICY_MODE_SUGGEST);
2384     }
2385
2386   /* undo removedisabledconflicts */
2387   if (redoq.count)
2388     undo_removedisabledconflicts(solv, &redoq);
2389   queue_free(&redoq);
2390   
2391   /* undo job rule disabling */
2392   for (i = 0; i < disabledq.count; i++)
2393     solver_enablerule(solv, solv->rules + disabledq.elements[i]);
2394   queue_free(&disabledq);
2395   map_free(&obsmap);
2396 }
2397
2398 void
2399 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap)
2400 {
2401   int i;
2402   Id how, what, select;
2403   Id p, pp;
2404   for (i = 0; i < job->count; i += 2)
2405     {
2406       how = job->elements[i];
2407       if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
2408         continue;
2409       what = job->elements[i + 1];
2410       select = how & SOLVER_SELECTMASK;
2411       if (!noobsmap->size)
2412         map_grow(noobsmap, pool->nsolvables);
2413       FOR_JOB_SELECT(p, pp, select, what)
2414         MAPSET(noobsmap, p);
2415     }
2416 }
2417
2418 /*
2419  *
2420  * solve job queue
2421  *
2422  */
2423
2424 void
2425 solver_solve(Solver *solv, Queue *job)
2426 {
2427   Pool *pool = solv->pool;
2428   Repo *installed = solv->installed;
2429   int i;
2430   int oldnrules;
2431   Map addedmap;                /* '1' == have rpm-rules for solvable */
2432   Map installcandidatemap;
2433   Id how, what, select, name, weak, p, pp, d;
2434   Queue q;
2435   Solvable *s;
2436   Rule *r;
2437   int now, solve_start;
2438   int hasdupjob = 0;
2439
2440   solve_start = sat_timems(0);
2441
2442   /* log solver options */
2443   POOL_DEBUG(SAT_DEBUG_STATS, "solver started\n");
2444   POOL_DEBUG(SAT_DEBUG_STATS, "fixsystem=%d updatesystem=%d dosplitprovides=%d, noupdateprovide=%d noinfarchcheck=%d\n", solv->fixsystem, solv->updatesystem, solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
2445   POOL_DEBUG(SAT_DEBUG_STATS, "distupgrade=%d distupgrade_removeunsupported=%d\n", solv->distupgrade, solv->distupgrade_removeunsupported);
2446   POOL_DEBUG(SAT_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allowarchchange, solv->allowvendorchange);
2447   POOL_DEBUG(SAT_DEBUG_STATS, "promoteepoch=%d, novirtualconflicts=%d, allowselfconflicts=%d\n", pool->promoteepoch, pool->novirtualconflicts, pool->allowselfconflicts);
2448   POOL_DEBUG(SAT_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2449   POOL_DEBUG(SAT_DEBUG_STATS, "dontinstallrecommended=%d, ignorealreadyrecommended=%d, dontshowinstalledrecommended=%d\n", solv->dontinstallrecommended, solv->ignorealreadyrecommended, solv->dontshowinstalledrecommended);
2450
2451   /* create whatprovides if not already there */
2452   if (!pool->whatprovides)
2453     pool_createwhatprovides(pool);
2454
2455   /* create obsolete index */
2456   policy_create_obsolete_index(solv);
2457
2458   /* remember job */
2459   queue_free(&solv->job);
2460   queue_init_clone(&solv->job, job);
2461
2462   /*
2463    * create basic rule set of all involved packages
2464    * use addedmap bitmap to make sure we don't create rules twice
2465    */
2466
2467   /* create noobsolete map if needed */
2468   solver_calculate_noobsmap(pool, job, &solv->noobsoletes);
2469
2470   map_init(&addedmap, pool->nsolvables);
2471   MAPSET(&addedmap, SYSTEMSOLVABLE);
2472
2473   map_init(&installcandidatemap, pool->nsolvables);
2474   queue_init(&q);
2475
2476   now = sat_timems(0);
2477   /*
2478    * create rules for all package that could be involved with the solving
2479    * so called: rpm rules
2480    *
2481    */
2482   if (installed)
2483     {
2484       /* check for verify jobs */
2485       for (i = 0; i < job->count; i += 2)
2486         {
2487           how = job->elements[i];
2488           what = job->elements[i + 1];
2489           select = how & SOLVER_SELECTMASK;
2490           switch (how & SOLVER_JOBMASK)
2491             {
2492             case SOLVER_VERIFY:
2493               FOR_JOB_SELECT(p, pp, select, what)
2494                 {
2495                   s = pool->solvables + p;
2496                   if (!solv->installed || s->repo != solv->installed)
2497                     continue;
2498                   if (!solv->fixmap.size)
2499                     map_grow(&solv->fixmap, solv->installed->end - solv->installed->start);
2500                   MAPSET(&solv->fixmap, p - solv->installed->start);
2501                 }
2502               break;
2503             default:
2504               break;
2505             }
2506         }
2507
2508       oldnrules = solv->nrules;
2509       POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
2510       FOR_REPO_SOLVABLES(installed, p, s)
2511         solver_addrpmrulesforsolvable(solv, s, &addedmap);
2512       POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
2513       POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
2514       oldnrules = solv->nrules;
2515       FOR_REPO_SOLVABLES(installed, p, s)
2516         solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
2517       POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
2518     }
2519
2520   /*
2521    * create rules for all packages involved in the job
2522    * (to be installed or removed)
2523    */
2524     
2525   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
2526   oldnrules = solv->nrules;
2527   for (i = 0; i < job->count; i += 2)
2528     {
2529       how = job->elements[i];
2530       what = job->elements[i + 1];
2531       select = how & SOLVER_SELECTMASK;
2532
2533       switch (how & SOLVER_JOBMASK)
2534         {
2535         case SOLVER_INSTALL:
2536           FOR_JOB_SELECT(p, pp, select, what)
2537             {
2538               MAPSET(&installcandidatemap, p);
2539               solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
2540             }
2541           break;
2542         case SOLVER_DISTUPGRADE:
2543           if (!solv->distupgrade)
2544             hasdupjob = 1;
2545           break;
2546         default:
2547           break;
2548         }
2549     }
2550   POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
2551
2552     
2553   /*
2554    * add rules for suggests, enhances
2555    */
2556   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for suggested/enhanced packages ***\n");
2557   oldnrules = solv->nrules;
2558   solver_addrpmrulesforweak(solv, &addedmap);
2559   POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
2560
2561   /*
2562    * first pass done, we now have all the rpm rules we need.
2563    * unify existing rules before going over all job rules and
2564    * policy rules.
2565    * at this point the system is always solvable,
2566    * as an empty system (remove all packages) is a valid solution
2567    */
2568
2569   IF_POOLDEBUG (SAT_DEBUG_STATS)
2570     {
2571       int possible = 0, installable = 0;
2572       for (i = 1; i < pool->nsolvables; i++)
2573         {
2574           if (pool_installable(pool, pool->solvables + i))
2575             installable++;
2576           if (MAPTST(&addedmap, i))
2577             possible++;
2578         }
2579       POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
2580     }
2581
2582   solver_unifyrules(solv);                          /* remove duplicate rpm rules */
2583   solv->rpmrules_end = solv->nrules;              /* mark end of rpm rules */
2584
2585   POOL_DEBUG(SAT_DEBUG_STATS, "rpm rule memory usage: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
2586   POOL_DEBUG(SAT_DEBUG_STATS, "rpm rule creation took %d ms\n", sat_timems(now));
2587
2588   /* create dup maps if needed. We need the maps early to create our
2589    * update rules */
2590   if (hasdupjob)
2591     solver_createdupmaps(solv);
2592
2593   /*
2594    * create feature rules
2595    * 
2596    * foreach installed:
2597    *   create assertion (keep installed, if no update available)
2598    *   or
2599    *   create update rule (A|update1(A)|update2(A)|...)
2600    * 
2601    * those are used later on to keep a version of the installed packages in
2602    * best effort mode
2603    */
2604     
2605   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add feature rules ***\n");
2606   solv->featurerules = solv->nrules;              /* mark start of feature rules */
2607   if (installed)
2608     {
2609       /* foreach possibly installed solvable */
2610       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2611         {
2612           if (s->repo != installed)
2613             {
2614               solver_addrule(solv, 0, 0);       /* create dummy rule */
2615               continue;
2616             }
2617           solver_addupdaterule(solv, s, 1);    /* allow s to be updated */
2618         }
2619       /* make sure we accounted for all rules */
2620       assert(solv->nrules - solv->featurerules == installed->end - installed->start);
2621     }
2622   solv->featurerules_end = solv->nrules;
2623
2624     /*
2625      * Add update rules for installed solvables
2626      * 
2627      * almost identical to feature rules
2628      * except that downgrades/archchanges/vendorchanges are not allowed
2629      */
2630     
2631   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add update rules ***\n");
2632   solv->updaterules = solv->nrules;
2633
2634   if (installed)
2635     { /* foreach installed solvables */
2636       /* we create all update rules, but disable some later on depending on the job */
2637       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2638         {
2639           Rule *sr;
2640
2641           if (s->repo != installed)
2642             {
2643               solver_addrule(solv, 0, 0);       /* create dummy rule */
2644               continue;
2645             }
2646           solver_addupdaterule(solv, s, 0);     /* allowall = 0: downgrades not allowed */
2647           /*
2648            * check for and remove duplicate
2649            */
2650           r = solv->rules + solv->nrules - 1;           /* r: update rule */
2651           sr = r - (installed->end - installed->start); /* sr: feature rule */
2652           /* it's orphaned if there is no feature rule or the feature rule
2653            * consists just of the installed package */
2654           if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
2655             queue_push(&solv->orphaned, i);
2656           if (!r->p)
2657             {
2658               assert(solv->distupgrade && !sr->p);
2659               continue;
2660             }
2661           if (!solver_samerule(solv, r, sr))
2662             {
2663               /* identical rule, kill unneeded one */
2664               if (solv->allowuninstall)
2665                 {
2666                   /* keep feature rule, make it weak */
2667                   memset(r, 0, sizeof(*r));
2668                   queue_push(&solv->weakruleq, sr - solv->rules);
2669                 }
2670               else
2671                 {
2672                   /* keep update rule */
2673                   memset(sr, 0, sizeof(*sr));
2674                 }
2675             }
2676           else if (solv->allowuninstall)
2677             {
2678               /* make both feature and update rule weak */
2679               queue_push(&solv->weakruleq, r - solv->rules);
2680               queue_push(&solv->weakruleq, sr - solv->rules);
2681             }
2682           else
2683             solver_disablerule(solv, sr);
2684         }
2685       /* consistency check: we added a rule for _every_ installed solvable */
2686       assert(solv->nrules - solv->updaterules == installed->end - installed->start);
2687     }
2688   solv->updaterules_end = solv->nrules;
2689
2690
2691   /*
2692    * now add all job rules
2693    */
2694
2695   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
2696
2697   solv->jobrules = solv->nrules;
2698   for (i = 0; i < job->count; i += 2)
2699     {
2700       oldnrules = solv->nrules;
2701
2702       how = job->elements[i];
2703       what = job->elements[i + 1];
2704       weak = how & SOLVER_WEAK;
2705       select = how & SOLVER_SELECTMASK;
2706       switch (how & SOLVER_JOBMASK)
2707         {
2708         case SOLVER_INSTALL:
2709           POOL_DEBUG(SAT_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2710           if (select == SOLVER_SOLVABLE)
2711             {
2712               p = what;
2713               d = 0;
2714             }
2715           else
2716             {
2717               queue_empty(&q);
2718               FOR_JOB_SELECT(p, pp, select, what)
2719                 queue_push(&q, p);
2720               if (!q.count)
2721                 {
2722                   /* no candidate found, make this an impossible rule */
2723                   queue_push(&q, -SYSTEMSOLVABLE);
2724                 }
2725               p = queue_shift(&q);      /* get first candidate */
2726               d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q);    /* internalize */
2727             }
2728           solver_addrule(solv, p, d);           /* add install rule */
2729           queue_push(&solv->ruletojob, i);
2730           if (weak)
2731             queue_push(&solv->weakruleq, solv->nrules - 1);
2732           break;
2733         case SOLVER_ERASE:
2734           POOL_DEBUG(SAT_DEBUG_JOB, "job: %serase %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2735           if (select == SOLVER_SOLVABLE && solv->installed && pool->solvables[what].repo == solv->installed)
2736             {
2737               /* special case for "erase a specific solvable": we also
2738                * erase all other solvables with that name, so that they
2739                * don't get picked up as replacement */
2740               name = pool->solvables[what].name;
2741               FOR_PROVIDES(p, pp, name)
2742                 {
2743                   if (p == what)
2744                     continue;
2745                   s = pool->solvables + p;
2746                   if (s->name != name)
2747                     continue;
2748                   /* keep other versions installed */
2749                   if (s->repo == solv->installed)
2750                     continue;
2751                   /* keep installcandidates of other jobs */
2752                   if (MAPTST(&installcandidatemap, p))
2753                     continue;
2754                   solver_addrule(solv, -p, 0);                  /* remove by Id */
2755                   queue_push(&solv->ruletojob, i);
2756                   if (weak)
2757                     queue_push(&solv->weakruleq, solv->nrules - 1);
2758                 }
2759             }
2760           FOR_JOB_SELECT(p, pp, select, what)
2761             {
2762               solver_addrule(solv, -p, 0);
2763               queue_push(&solv->ruletojob, i);
2764               if (weak)
2765                 queue_push(&solv->weakruleq, solv->nrules - 1);
2766             }
2767           break;
2768
2769         case SOLVER_UPDATE:
2770           POOL_DEBUG(SAT_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2771           FOR_JOB_SELECT(p, pp, select, what)
2772             {
2773               s = pool->solvables + p;
2774               if (!solv->installed || s->repo != solv->installed)
2775                 continue;
2776               if (!solv->updatemap.size)
2777                 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
2778               MAPSET(&solv->updatemap, p - solv->installed->start);
2779             }
2780           break;
2781         case SOLVER_VERIFY:
2782           POOL_DEBUG(SAT_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2783           break;
2784         case SOLVER_WEAKENDEPS:
2785           POOL_DEBUG(SAT_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2786           if (select != SOLVER_SOLVABLE)
2787             break;
2788           s = pool->solvables + what;
2789           weaken_solvable_deps(solv, what);
2790           break;
2791         case SOLVER_NOOBSOLETES:
2792           POOL_DEBUG(SAT_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2793           break;
2794         case SOLVER_LOCK:
2795           POOL_DEBUG(SAT_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2796           FOR_JOB_SELECT(p, pp, select, what)
2797             {
2798               s = pool->solvables + p;
2799               if (installed && s->repo == installed)
2800                 solver_addrule(solv, p, 0);
2801               else
2802                 solver_addrule(solv, -p, 0);
2803               queue_push(&solv->ruletojob, i);
2804               if (weak)
2805                 queue_push(&solv->weakruleq, solv->nrules - 1);
2806             }
2807           break;
2808         case SOLVER_DISTUPGRADE:
2809           POOL_DEBUG(SAT_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(solv, select, what));
2810           break;
2811         case SOLVER_DROP_ORPHANED:
2812           POOL_DEBUG(SAT_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(solv, select, what));
2813           FOR_JOB_SELECT(p, pp, select, what)
2814             {
2815               s = pool->solvables + p;
2816               if (!installed || s->repo != installed)
2817                 continue;
2818               if (!solv->droporphanedmap.size)
2819                 map_grow(&solv->droporphanedmap, installed->end - installed->start);
2820               MAPSET(&solv->droporphanedmap, p - installed->start);
2821             }
2822           break;
2823         default:
2824           POOL_DEBUG(SAT_DEBUG_JOB, "job: unknown job\n");
2825           break;
2826         }
2827         
2828         /*
2829          * debug
2830          */
2831         
2832       IF_POOLDEBUG (SAT_DEBUG_JOB)
2833         {
2834           int j;
2835           if (solv->nrules == oldnrules)
2836             POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created\n");
2837           for (j = oldnrules; j < solv->nrules; j++)
2838             {
2839               POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
2840               solver_printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
2841             }
2842         }
2843     }
2844   assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
2845   solv->jobrules_end = solv->nrules;
2846
2847   /* now create infarch and dup rules */
2848   if (!solv->noinfarchcheck)
2849     {
2850       solver_addinfarchrules(solv, &addedmap);
2851       if (pool->obsoleteusescolors)
2852         {
2853           /* currently doesn't work well with infarch rules, so make
2854            * them weak */
2855           for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
2856             queue_push(&solv->weakruleq, i);
2857         }
2858     }
2859   else
2860     solv->infarchrules = solv->infarchrules_end = solv->nrules;
2861
2862   if (hasdupjob)
2863     {
2864       solver_addduprules(solv, &addedmap);
2865       solver_freedupmaps(solv); /* no longer needed */
2866     }
2867   else
2868     solv->duprules = solv->duprules_end = solv->nrules;
2869
2870   if (1)
2871     {
2872       extern void addchoicerules(Solver *solv);
2873       addchoicerules(solv);
2874     }
2875   else
2876     solv->choicerules = solv->choicerules_end = solv->nrules;
2877
2878   /* all rules created
2879    * --------------------------------------------------------------
2880    * prepare for solving
2881    */
2882     
2883   /* free unneeded memory */
2884   map_free(&addedmap);
2885   map_free(&installcandidatemap);
2886   queue_free(&q);
2887
2888   POOL_DEBUG(SAT_DEBUG_STATS, "%d rpm rules, %d job rules, %d infarch rules, %d dup rules, %d choice rules\n", solv->rpmrules_end - 1, solv->jobrules_end - solv->jobrules, solv->infarchrules_end - solv->infarchrules, solv->duprules_end - solv->duprules, solv->choicerules_end - solv->choicerules);
2889
2890   /* create weak map */
2891   map_init(&solv->weakrulemap, solv->nrules);
2892   for (i = 0; i < solv->weakruleq.count; i++)
2893     {
2894       p = solv->weakruleq.elements[i];
2895       MAPSET(&solv->weakrulemap, p);
2896     }
2897
2898   /* all new rules are learnt after this point */
2899   solv->learntrules = solv->nrules;
2900
2901   /* create watches chains */
2902   makewatches(solv);
2903
2904   /* create assertion index. it is only used to speed up
2905    * makeruledecsions() a bit */
2906   for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
2907     if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
2908       queue_push(&solv->ruleassertions, i);
2909
2910   /* disable update rules that conflict with our job */
2911   solver_disablepolicyrules(solv);
2912
2913   /* make decisions based on job/update assertions */
2914   makeruledecisions(solv);
2915   POOL_DEBUG(SAT_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
2916
2917   /*
2918    * ********************************************
2919    * solve!
2920    * ********************************************
2921    */
2922     
2923   now = sat_timems(0);
2924   solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
2925   POOL_DEBUG(SAT_DEBUG_STATS, "solver took %d ms\n", sat_timems(now));
2926
2927   /*
2928    * calculate recommended/suggested packages
2929    */
2930   findrecommendedsuggested(solv);
2931
2932   /*
2933    * prepare solution queue if there were problems
2934    */
2935   solver_prepare_solutions(solv);
2936
2937   /*
2938    * finally prepare transaction info
2939    */
2940   transaction_calculate(&solv->trans, &solv->decisionq, &solv->noobsoletes);
2941
2942   POOL_DEBUG(SAT_DEBUG_STATS, "final solver statistics: %d problems, %d learned rules, %d unsolvable\n", solv->problems.count / 2, solv->stats_learned, solv->stats_unsolvable);
2943   POOL_DEBUG(SAT_DEBUG_STATS, "solver_solve took %d ms\n", sat_timems(solve_start));
2944 }
2945
2946 /***********************************************************************/
2947 /* disk usage computations */
2948
2949 /*-------------------------------------------------------------------
2950  * 
2951  * calculate DU changes
2952  */
2953
2954 void
2955 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
2956 {
2957   Map installedmap;
2958
2959   solver_create_state_maps(solv, &installedmap, 0);
2960   pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
2961   map_free(&installedmap);
2962 }
2963
2964
2965 /*-------------------------------------------------------------------
2966  * 
2967  * calculate changes in install size
2968  */
2969
2970 int
2971 solver_calc_installsizechange(Solver *solv)
2972 {
2973   Map installedmap;
2974   int change;
2975
2976   solver_create_state_maps(solv, &installedmap, 0);
2977   change = pool_calc_installsizechange(solv->pool, &installedmap);
2978   map_free(&installedmap);
2979   return change;
2980 }
2981
2982 void
2983 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
2984 {
2985   Map installedmap;
2986   solver_create_state_maps(solv, &installedmap, 0);
2987   pool_trivial_installable_noobsoletesmap(solv->pool, &installedmap, pkgs, res, solv->noobsoletes.size ? &solv->noobsoletes : 0);
2988   map_free(&installedmap);
2989 }
2990
2991
2992 #if 0
2993 #define FIND_INVOLVED_DEBUG 0
2994 void
2995 solver_find_involved(Solver *solv, Queue *installedq, Solvable *ts, Queue *q)
2996 {
2997   Pool *pool = solv->pool;
2998   Map im;
2999   Map installedm;
3000   Solvable *s;
3001   Queue iq;
3002   Queue installedq_internal;
3003   Id tp, ip, p, pp, req, *reqp, sup, *supp;
3004   int i, count;
3005
3006   tp = ts - pool->solvables;
3007   queue_init(&iq);
3008   queue_init(&installedq_internal);
3009   map_init(&im, pool->nsolvables);
3010   map_init(&installedm, pool->nsolvables);
3011
3012   if (!installedq)
3013     {
3014       installedq = &installedq_internal;
3015       if (solv->installed)
3016         {
3017           for (ip = solv->installed->start; ip < solv->installed->end; ip++)
3018             {
3019               s = pool->solvables + ip;
3020               if (s->repo != solv->installed)
3021                 continue;
3022               queue_push(installedq, ip);
3023             }
3024         }
3025     }
3026   for (i = 0; i < installedq->count; i++)
3027     {
3028       ip = installedq->elements[i];
3029       MAPSET(&installedm, ip);
3030       MAPSET(&im, ip);
3031     }
3032
3033   queue_push(&iq, ts - pool->solvables);
3034   while (iq.count)
3035     {
3036       ip = queue_shift(&iq);
3037       if (!MAPTST(&im, ip))
3038         continue;
3039       if (!MAPTST(&installedm, ip))
3040         continue;
3041       MAPCLR(&im, ip);
3042       s = pool->solvables + ip;
3043 #if FIND_INVOLVED_DEBUG
3044       printf("hello %s\n", solvable2str(pool, s));
3045 #endif
3046       if (s->requires)
3047         {
3048           reqp = s->repo->idarraydata + s->requires;
3049           while ((req = *reqp++) != 0)
3050             {
3051               if (req == SOLVABLE_PREREQMARKER)
3052                 continue;
3053               /* count number of installed packages that match */
3054               count = 0;
3055               FOR_PROVIDES(p, pp, req)
3056                 if (MAPTST(&installedm, p))
3057                   count++;
3058               if (count > 1)
3059                 continue;
3060               FOR_PROVIDES(p, pp, req)
3061                 {
3062                   if (MAPTST(&im, p))
3063                     {
3064 #if FIND_INVOLVED_DEBUG
3065                       printf("%s requires %s\n", solvid2str(pool, ip), solvid2str(pool, p));
3066 #endif
3067                       queue_push(&iq, p);
3068                     }
3069                 }
3070             }
3071         }
3072       if (s->recommends)
3073         {
3074           reqp = s->repo->idarraydata + s->recommends;
3075           while ((req = *reqp++) != 0)
3076             {
3077               count = 0;
3078               FOR_PROVIDES(p, pp, req)
3079                 if (MAPTST(&installedm, p))
3080                   count++;
3081               if (count > 1)
3082                 continue;
3083               FOR_PROVIDES(p, pp, req)
3084                 {
3085                   if (MAPTST(&im, p))
3086                     {
3087 #if FIND_INVOLVED_DEBUG
3088                       printf("%s recommends %s\n", solvid2str(pool, ip), solvid2str(pool, p));
3089 #endif
3090                       queue_push(&iq, p);
3091                     }
3092                 }
3093             }
3094         }
3095       if (!iq.count)
3096         {
3097           /* supplements pass */
3098           for (i = 0; i < installedq->count; i++)
3099             {
3100               ip = installedq->elements[i];
3101               s = pool->solvables + ip;
3102               if (!s->supplements)
3103                 continue;
3104               if (!MAPTST(&im, ip))
3105                 continue;
3106               supp = s->repo->idarraydata + s->supplements;
3107               while ((sup = *supp++) != 0)
3108                 if (!dep_possible(solv, sup, &im) && dep_possible(solv, sup, &installedm))
3109                   break;
3110               /* no longer supplemented, also erase */
3111               if (sup)
3112                 {
3113 #if FIND_INVOLVED_DEBUG
3114                   printf("%s supplemented\n", solvid2str(pool, ip));
3115 #endif
3116                   queue_push(&iq, ip);
3117                 }
3118             }
3119         }
3120     }
3121
3122   for (i = 0; i < installedq->count; i++)
3123     {
3124       ip = installedq->elements[i];
3125       if (MAPTST(&im, ip))
3126         queue_push(&iq, ip);
3127     }
3128
3129   while (iq.count)
3130     {
3131       ip = queue_shift(&iq);
3132       if (!MAPTST(&installedm, ip))
3133         continue;
3134       s = pool->solvables + ip;
3135 #if FIND_INVOLVED_DEBUG
3136       printf("bye %s\n", solvable2str(pool, s));
3137 #endif
3138       if (s->requires)
3139         {
3140           reqp = s->repo->idarraydata + s->requires;
3141           while ((req = *reqp++) != 0)
3142             {
3143               FOR_PROVIDES(p, pp, req)
3144                 {
3145                   if (!MAPTST(&im, p))
3146                     {
3147                       if (p == tp)
3148                         continue;
3149 #if FIND_INVOLVED_DEBUG
3150                       printf("%s requires %s\n", solvid2str(pool, ip), solvid2str(pool, p));
3151 #endif
3152                       MAPSET(&im, p);
3153                       queue_push(&iq, p);
3154                     }
3155                 }
3156             }
3157         }
3158       if (s->recommends)
3159         {
3160           reqp = s->repo->idarraydata + s->recommends;
3161           while ((req = *reqp++) != 0)
3162             {
3163               FOR_PROVIDES(p, pp, req)
3164                 {
3165                   if (!MAPTST(&im, p))
3166                     {
3167                       if (p == tp)
3168                         continue;
3169 #if FIND_INVOLVED_DEBUG
3170                       printf("%s recommends %s\n", solvid2str(pool, ip), solvid2str(pool, p));
3171 #endif
3172                       MAPSET(&im, p);
3173                       queue_push(&iq, p);
3174                     }
3175                 }
3176             }
3177         }
3178       if (!iq.count)
3179         {
3180           /* supplements pass */
3181           for (i = 0; i < installedq->count; i++)
3182             {
3183               ip = installedq->elements[i];
3184               if (ip == tp)
3185                 continue;
3186               s = pool->solvables + ip;
3187               if (!s->supplements)
3188                 continue;
3189               if (MAPTST(&im, ip))
3190                 continue;
3191               supp = s->repo->idarraydata + s->supplements;
3192               while ((sup = *supp++) != 0)
3193                 if (dep_possible(solv, sup, &im))
3194                   break;
3195               if (sup)
3196                 {
3197 #if FIND_INVOLVED_DEBUG
3198                   printf("%s supplemented\n", solvid2str(pool, ip));
3199 #endif
3200                   MAPSET(&im, ip);
3201                   queue_push(&iq, ip);
3202                 }
3203             }
3204         }
3205     }
3206     
3207   queue_free(&iq);
3208
3209   /* convert map into result */
3210   for (i = 0; i < installedq->count; i++)
3211     {
3212       ip = installedq->elements[i];
3213       if (MAPTST(&im, ip))
3214         continue;
3215       if (ip == ts - pool->solvables)
3216         continue;
3217       queue_push(q, ip);
3218     }
3219   map_free(&im);
3220   map_free(&installedm);
3221   queue_free(&installedq_internal);
3222 }
3223 #endif
3224