- make installed packages pass much faster
[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
1299   sat_free(solv->decisionmap);
1300   sat_free(solv->rules);
1301   sat_free(solv->watches);
1302   sat_free(solv->obsoletes);
1303   sat_free(solv->obsoletes_data);
1304   sat_free(solv->multiversionupdaters);
1305   sat_free(solv->choicerules_ref);
1306   sat_free(solv);
1307 }
1308
1309
1310 /*-------------------------------------------------------------------
1311  * 
1312  * solver_run_sat
1313  *
1314  * all rules have been set up, now actually run the solver
1315  *
1316  */
1317
1318 void
1319 solver_run_sat(Solver *solv, int disablerules, int doweak)
1320 {
1321   Queue dq;             /* local decisionqueue */
1322   Queue dqs;            /* local decisionqueue for supplements */
1323   int systemlevel;
1324   int level, olevel;
1325   Rule *r;
1326   int i, j, n;
1327   Solvable *s;
1328   Pool *pool = solv->pool;
1329   Id p, *dp;
1330   int minimizationsteps;
1331   int installedpos = solv->installed ? solv->installed->start : 0;
1332
1333   IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1334     {
1335       POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1336       for (i = 1; i < solv->nrules; i++)
1337         solver_printruleclass(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
1338     }
1339
1340   POOL_DEBUG(SAT_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1341
1342   IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
1343     solver_printdecisions(solv);
1344
1345   /* start SAT algorithm */
1346   level = 1;
1347   systemlevel = level + 1;
1348   POOL_DEBUG(SAT_DEBUG_SOLVER, "solving...\n");
1349
1350   queue_init(&dq);
1351   queue_init(&dqs);
1352
1353   /*
1354    * here's the main loop:
1355    * 1) propagate new decisions (only needed for level 1)
1356    * 2) try to keep installed packages
1357    * 3) fulfill all unresolved rules
1358    * 4) install recommended packages
1359    * 5) minimalize solution if we had choices
1360    * if we encounter a problem, we rewind to a safe level and restart
1361    * with step 1
1362    */
1363    
1364   minimizationsteps = 0;
1365   for (;;)
1366     {
1367       /*
1368        * propagate
1369        */
1370       if (level == 1)
1371         {
1372           POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d;  size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1373           if ((r = propagate(solv, level)) != 0)
1374             {
1375               if (analyze_unsolvable(solv, r, disablerules))
1376                 continue;
1377               queue_free(&dq);
1378               queue_free(&dqs);
1379               return;
1380             }
1381         }
1382
1383      if (level < systemlevel)
1384         {
1385           POOL_DEBUG(SAT_DEBUG_SOLVER, "resolving job rules\n");
1386           for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1387             {
1388               Id l;
1389               if (r->d < 0)             /* ignore disabled rules */
1390                 continue;
1391               queue_empty(&dq);
1392               FOR_RULELITERALS(l, dp, r)
1393                 {
1394                   if (l < 0)
1395                     {
1396                       if (solv->decisionmap[-l] <= 0)
1397                         break;
1398                     }
1399                   else
1400                     {
1401                       if (solv->decisionmap[l] > 0)
1402                         break;
1403                       if (solv->decisionmap[l] == 0)
1404                         queue_push(&dq, l);
1405                     }
1406                 }
1407               if (l || !dq.count)
1408                 continue;
1409               /* prune to installed if not updating */
1410               if (!solv->updatesystem && solv->installed && dq.count > 1)
1411                 {
1412                   int j, k;
1413                   for (j = k = 0; j < dq.count; j++)
1414                     {
1415                       Solvable *s = pool->solvables + dq.elements[j];
1416                       if (s->repo == solv->installed)
1417                         dq.elements[k++] = dq.elements[j];
1418                     }
1419                   if (k)
1420                     dq.count = k;
1421                 }
1422               olevel = level;
1423               level = selectandinstall(solv, level, &dq, disablerules, i);
1424               if (level == 0)
1425                 {
1426                   queue_free(&dq);
1427                   queue_free(&dqs);
1428                   return;
1429                 }
1430               if (level <= olevel)
1431                 break;
1432             }
1433           systemlevel = level + 1;
1434           if (i < solv->jobrules_end)
1435             continue;
1436         }
1437
1438
1439       /*
1440        * installed packages
1441        */
1442
1443       if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1444         {
1445           Repo *installed = solv->installed;
1446           int pass;
1447
1448           POOL_DEBUG(SAT_DEBUG_SOLVER, "resolving installed packages\n");
1449           /* we use two passes if we need to update packages 
1450            * to create a better user experience */
1451           for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1452             {
1453               int passlevel = level;
1454               /* start with installedpos, the position that gave us problems last time */
1455               for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1456                 {
1457                   Rule *rr;
1458                   Id d;
1459
1460                   if (i == installed->end)
1461                     i = installed->start;
1462                   s = pool->solvables + i;
1463                   if (s->repo != installed)
1464                     continue;
1465
1466                   if (solv->decisionmap[i] > 0)
1467                     continue;
1468                   /* XXX: noupdate check is probably no longer needed, as all jobs should
1469                    * already be satisfied */
1470                   if (MAPTST(&solv->noupdate, i - installed->start))
1471                     continue;
1472                   if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1473                     continue;           /* updates first */
1474                   r = solv->rules + solv->updaterules + (i - installed->start);
1475                   rr = r;
1476                   if (!rr->p || rr->d < 0)      /* disabled -> look at feature rule */
1477                     rr -= solv->installed->end - solv->installed->start;
1478                   if (!rr->p)           /* identical to update rule? */
1479                     rr = r;
1480                   if (!rr->p)
1481                     continue;           /* orpaned package */
1482
1483                   queue_empty(&dq);
1484                   if (solv->decisionmap[i] < 0 || solv->updatesystem || (solv->updatemap.size && MAPTST(&solv->updatemap, i - installed->start)) || rr->p != i)
1485                     {
1486                       if (solv->noobsoletes.size && solv->multiversionupdaters
1487                              && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1488                         {
1489                           /* special multiversion handling, make sure best version is chosen */
1490                           queue_push(&dq, i);
1491                           while ((p = pool->whatprovidesdata[d++]) != 0)
1492                             if (solv->decisionmap[p] >= 0)
1493                               queue_push(&dq, p);
1494                           policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1495                           p = dq.elements[0];
1496                           if (p != i && solv->decisionmap[p] == 0)
1497                             {
1498                               rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1499                               if (!rr->p)               /* update rule == feature rule? */
1500                                 rr = rr - solv->featurerules + solv->updaterules;
1501                               dq.count = 1;
1502                             }
1503                           else
1504                             dq.count = 0;
1505                         }
1506                       else
1507                         {
1508                           /* update to best package */
1509                           FOR_RULELITERALS(p, dp, rr)
1510                             {
1511                               if (solv->decisionmap[p] > 0)
1512                                 {
1513                                   dq.count = 0;         /* already fulfilled */
1514                                   break;
1515                                 }
1516                               if (!solv->decisionmap[p])
1517                                 queue_push(&dq, p);
1518                             }
1519                         }
1520                     }
1521                   /* install best version */
1522                   if (dq.count)
1523                     {
1524                       olevel = level;
1525                       level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1526                       if (level == 0)
1527                         {
1528                           queue_free(&dq);
1529                           queue_free(&dqs);
1530                           return;
1531                         }
1532                       if (level <= olevel)
1533                         {
1534                           if (level < passlevel)
1535                             break;      /* trouble */
1536                           if (level < olevel)
1537                             n = installed->start;       /* redo all */
1538                           i--;
1539                           n--;
1540                           continue;
1541                         }
1542                     }
1543                   /* if still undecided keep package */
1544                   if (solv->decisionmap[i] == 0)
1545                     {
1546                       olevel = level;
1547                       POOL_DEBUG(SAT_DEBUG_POLICY, "keeping %s\n", solvid2str(pool, i));
1548                       level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1549                       if (level == 0)
1550                         {
1551                           queue_free(&dq);
1552                           queue_free(&dqs);
1553                           return;
1554                         }
1555                       if (level <= olevel)
1556                         {
1557                           if (level < passlevel)
1558                             break;      /* trouble */
1559                           if (level < olevel)
1560                             n = installed->start;       /* redo all */
1561                           i--;
1562                           n--;
1563                           continue; /* retry with learnt rule */
1564                         }
1565                     }
1566                 }
1567               if (n < installed->end)
1568                 {
1569                   installedpos = i;     /* retry problem solvable next time */
1570                   break;                /* ran into trouble */
1571                 }
1572               installedpos = installed->start;  /* reset installedpos */
1573             }
1574           systemlevel = level + 1;
1575           if (pass < 2)
1576             continue;           /* had trouble, retry */
1577         }
1578
1579       if (level < systemlevel)
1580         systemlevel = level;
1581
1582       /*
1583        * decide
1584        */
1585
1586       POOL_DEBUG(SAT_DEBUG_POLICY, "deciding unresolved rules\n");
1587       for (i = 1, n = 1; n < solv->nrules; i++, n++)
1588         {
1589           if (i == solv->nrules)
1590             i = 1;
1591           r = solv->rules + i;
1592           if (r->d < 0)         /* ignore disabled rules */
1593             continue;
1594           queue_empty(&dq);
1595           if (r->d == 0)
1596             {
1597               /* binary or unary rule */
1598               /* need two positive undecided literals */
1599               if (r->p < 0 || r->w2 <= 0)
1600                 continue;
1601               if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1602                 continue;
1603               queue_push(&dq, r->p);
1604               queue_push(&dq, r->w2);
1605             }
1606           else
1607             {
1608               /* make sure that
1609                * all negative literals are installed
1610                * no positive literal is installed
1611                * i.e. the rule is not fulfilled and we
1612                * just need to decide on the positive literals
1613                */
1614               if (r->p < 0)
1615                 {
1616                   if (solv->decisionmap[-r->p] <= 0)
1617                     continue;
1618                 }
1619               else
1620                 {
1621                   if (solv->decisionmap[r->p] > 0)
1622                     continue;
1623                   if (solv->decisionmap[r->p] == 0)
1624                     queue_push(&dq, r->p);
1625                 }
1626               dp = pool->whatprovidesdata + r->d;
1627               while ((p = *dp++) != 0)
1628                 {
1629                   if (p < 0)
1630                     {
1631                       if (solv->decisionmap[-p] <= 0)
1632                         break;
1633                     }
1634                   else
1635                     {
1636                       if (solv->decisionmap[p] > 0)
1637                         break;
1638                       if (solv->decisionmap[p] == 0)
1639                         queue_push(&dq, p);
1640                     }
1641                 }
1642               if (p)
1643                 continue;
1644             }
1645           IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1646             {
1647               POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
1648               solver_printruleclass(solv, SAT_DEBUG_PROPAGATE, r);
1649             }
1650           /* dq.count < 2 cannot happen as this means that
1651            * the rule is unit */
1652           assert(dq.count > 1);
1653
1654           olevel = level;
1655           level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
1656           if (level == 0)
1657             {
1658               queue_free(&dq);
1659               queue_free(&dqs);
1660               return;
1661             }
1662           if (level < systemlevel || level == 1)
1663             break;
1664           n = 0;
1665         } /* for(), decide */
1666
1667       if (n != solv->nrules)    /* continue if level < systemlevel */
1668         continue;
1669
1670       if (doweak)
1671         {
1672           int qcount;
1673
1674           POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended packages\n");
1675           queue_empty(&dq);     /* recommended packages */
1676           queue_empty(&dqs);    /* supplemented packages */
1677           for (i = 1; i < pool->nsolvables; i++)
1678             {
1679               if (solv->decisionmap[i] < 0)
1680                 continue;
1681               if (solv->decisionmap[i] > 0)
1682                 {
1683                   /* installed, check for recommends */
1684                   Id *recp, rec, pp, p;
1685                   s = pool->solvables + i;
1686                   if (solv->ignorealreadyrecommended && s->repo == solv->installed)
1687                     continue;
1688                   /* XXX need to special case AND ? */
1689                   if (s->recommends)
1690                     {
1691                       recp = s->repo->idarraydata + s->recommends;
1692                       while ((rec = *recp++) != 0)
1693                         {
1694                           qcount = dq.count;
1695                           FOR_PROVIDES(p, pp, rec)
1696                             {
1697                               if (solv->decisionmap[p] > 0)
1698                                 {
1699                                   dq.count = qcount;
1700                                   break;
1701                                 }
1702                               else if (solv->decisionmap[p] == 0)
1703                                 {
1704                                   queue_pushunique(&dq, p);
1705                                 }
1706                             }
1707                         }
1708                     }
1709                 }
1710               else
1711                 {
1712                   s = pool->solvables + i;
1713                   if (!s->supplements)
1714                     continue;
1715                   if (!pool_installable(pool, s))
1716                     continue;
1717                   if (!solver_is_supplementing(solv, s))
1718                     continue;
1719                   queue_push(&dqs, i);
1720                 }
1721             }
1722
1723           /* filter out all packages obsoleted by installed packages */
1724           /* this is no longer needed if we have reverse obsoletes */
1725           if ((dqs.count || dq.count) && solv->installed)
1726             {
1727               Map obsmap;
1728               Id obs, *obsp, po, ppo;
1729
1730               map_init(&obsmap, pool->nsolvables);
1731               for (p = solv->installed->start; p < solv->installed->end; p++)
1732                 {
1733                   s = pool->solvables + p;
1734                   if (s->repo != solv->installed || !s->obsoletes)
1735                     continue;
1736                   if (solv->decisionmap[p] <= 0)
1737                     continue;
1738                   if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
1739                     continue;
1740                   obsp = s->repo->idarraydata + s->obsoletes;
1741                   /* foreach obsoletes */
1742                   while ((obs = *obsp++) != 0)
1743                     FOR_PROVIDES(po, ppo, obs)
1744                       MAPSET(&obsmap, po);
1745                 }
1746               for (i = j = 0; i < dqs.count; i++)
1747                 if (!MAPTST(&obsmap, dqs.elements[i]))
1748                   dqs.elements[j++] = dqs.elements[i];
1749               dqs.count = j;
1750               for (i = j = 0; i < dq.count; i++)
1751                 if (!MAPTST(&obsmap, dq.elements[i]))
1752                   dq.elements[j++] = dq.elements[i];
1753               dq.count = j;
1754               map_free(&obsmap);
1755             }
1756
1757           /* filter out all already supplemented packages if requested */
1758           if (solv->ignorealreadyrecommended && dqs.count)
1759             {
1760               /* turn off all new packages */
1761               for (i = 0; i < solv->decisionq.count; i++)
1762                 {
1763                   p = solv->decisionq.elements[i];
1764                   if (p < 0)
1765                     continue;
1766                   s = pool->solvables + p;
1767                   if (s->repo && s->repo != solv->installed)
1768                     solv->decisionmap[p] = -solv->decisionmap[p];
1769                 }
1770               /* filter out old supplements */
1771               for (i = j = 0; i < dqs.count; i++)
1772                 {
1773                   p = dqs.elements[i];
1774                   s = pool->solvables + p;
1775                   if (!s->supplements)
1776                     continue;
1777                   if (!solver_is_supplementing(solv, s))
1778                     dqs.elements[j++] = p;
1779                 }
1780               dqs.count = j;
1781               /* undo turning off */
1782               for (i = 0; i < solv->decisionq.count; i++)
1783                 {
1784                   p = solv->decisionq.elements[i];
1785                   if (p < 0)
1786                     continue;
1787                   s = pool->solvables + p;
1788                   if (s->repo && s->repo != solv->installed)
1789                     solv->decisionmap[p] = -solv->decisionmap[p];
1790                 }
1791             }
1792
1793           /* multiversion doesn't mix well with supplements.
1794            * filter supplemented packages where we already decided
1795            * to install a different version (see bnc#501088) */
1796           if (dqs.count && solv->noobsoletes.size)
1797             {
1798               for (i = j = 0; i < dqs.count; i++)
1799                 {
1800                   p = dqs.elements[i];
1801                   if (MAPTST(&solv->noobsoletes, p))
1802                     {
1803                       Id p2, pp2;
1804                       s = pool->solvables + p;
1805                       FOR_PROVIDES(p2, pp2, s->name)
1806                         if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
1807                           break;
1808                       if (p2)
1809                         continue;       /* ignore this package */
1810                     }
1811                   dqs.elements[j++] = p;
1812                 }
1813               dqs.count = j;
1814             }
1815
1816           /* make dq contain both recommended and supplemented pkgs */
1817           if (dqs.count)
1818             {
1819               for (i = 0; i < dqs.count; i++)
1820                 queue_pushunique(&dq, dqs.elements[i]);
1821             }
1822
1823           if (dq.count)
1824             {
1825               Map dqmap;
1826               int decisioncount = solv->decisionq.count;
1827
1828               if (dq.count == 1)
1829                 {
1830                   /* simple case, just one package. no need to choose  */
1831                   p = dq.elements[0];
1832                   if (dqs.count)
1833                     POOL_DEBUG(SAT_DEBUG_POLICY, "installing supplemented %s\n", solvid2str(pool, p));
1834                   else
1835                     POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended %s\n", solvid2str(pool, p));
1836                   queue_push(&solv->recommendations, p);
1837                   level = setpropagatelearn(solv, level, p, 0, 0);
1838                   continue;     /* back to main loop */
1839                 }
1840
1841               /* filter packages, this gives us the best versions */
1842               policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
1843
1844               /* create map of result */
1845               map_init(&dqmap, pool->nsolvables);
1846               for (i = 0; i < dq.count; i++)
1847                 MAPSET(&dqmap, dq.elements[i]);
1848
1849               /* install all supplemented packages */
1850               for (i = 0; i < dqs.count; i++)
1851                 {
1852                   p = dqs.elements[i];
1853                   if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
1854                     continue;
1855                   POOL_DEBUG(SAT_DEBUG_POLICY, "installing supplemented %s\n", solvid2str(pool, p));
1856                   queue_push(&solv->recommendations, p);
1857                   olevel = level;
1858                   level = setpropagatelearn(solv, level, p, 0, 0);
1859                   if (level <= olevel)
1860                     break;
1861                 }
1862               if (i < dqs.count || solv->decisionq.count < decisioncount)
1863                 {
1864                   map_free(&dqmap);
1865                   continue;
1866                 }
1867
1868               /* install all recommended packages */
1869               /* more work as we want to created branches if multiple
1870                * choices are valid */
1871               for (i = 0; i < decisioncount; i++)
1872                 {
1873                   Id rec, *recp, pp;
1874                   p = solv->decisionq.elements[i];
1875                   if (p < 0)
1876                     continue;
1877                   s = pool->solvables + p;
1878                   if (!s->repo || (solv->ignorealreadyrecommended && s->repo == solv->installed))
1879                     continue;
1880                   if (!s->recommends)
1881                     continue;
1882                   recp = s->repo->idarraydata + s->recommends;
1883                   while ((rec = *recp++) != 0)
1884                     {
1885                       queue_empty(&dq);
1886                       FOR_PROVIDES(p, pp, rec)
1887                         {
1888                           if (solv->decisionmap[p] > 0)
1889                             {
1890                               dq.count = 0;
1891                               break;
1892                             }
1893                           else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
1894                             queue_pushunique(&dq, p);
1895                         }
1896                       if (!dq.count)
1897                         continue;
1898                       if (dq.count > 1)
1899                         {
1900                           /* multiple candidates, open a branch */
1901                           for (i = 1; i < dq.count; i++)
1902                             queue_push(&solv->branches, dq.elements[i]);
1903                           queue_push(&solv->branches, -level);
1904                         }
1905                       p = dq.elements[0];
1906                       POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended %s\n", solvid2str(pool, p));
1907                       queue_push(&solv->recommendations, p);
1908                       olevel = level;
1909                       level = setpropagatelearn(solv, level, p, 0, 0);
1910                       if (level <= olevel || solv->decisionq.count < decisioncount)
1911                         break;  /* we had to revert some decisions */
1912                     }
1913                   if (rec)
1914                     break;      /* had a problem above, quit loop */
1915                 }
1916               map_free(&dqmap);
1917
1918               continue;         /* back to main loop */
1919             }
1920         }
1921
1922      if (solv->distupgrade && solv->installed)
1923         {
1924           int installedone = 0;
1925
1926           /* let's see if we can install some unsupported package */
1927           POOL_DEBUG(SAT_DEBUG_SOLVER, "deciding unsupported packages\n");
1928           for (i = 0; i < solv->orphaned.count; i++)
1929             {
1930               p = solv->orphaned.elements[i];
1931               if (solv->decisionmap[p])
1932                 continue;       /* already decided */
1933               olevel = level;
1934               if (solv->distupgrade_removeunsupported)
1935                 {
1936                   POOL_DEBUG(SAT_DEBUG_SOLVER, "removing unsupported %s\n", solvid2str(pool, p));
1937                   level = setpropagatelearn(solv, level, -p, 0, 0);
1938                 }
1939               else
1940                 {
1941                   POOL_DEBUG(SAT_DEBUG_SOLVER, "keeping unsupported %s\n", solvid2str(pool, p));
1942                   level = setpropagatelearn(solv, level, p, 0, 0);
1943                   installedone = 1;
1944                 }
1945               if (level < olevel)
1946                 break;
1947             }
1948           if (installedone || i < solv->orphaned.count)
1949             continue;
1950         }
1951
1952      if (solv->solution_callback)
1953         {
1954           solv->solution_callback(solv, solv->solution_callback_data);
1955           if (solv->branches.count)
1956             {
1957               int i = solv->branches.count - 1;
1958               int l = -solv->branches.elements[i];
1959               Id why;
1960
1961               for (; i > 0; i--)
1962                 if (solv->branches.elements[i - 1] < 0)
1963                   break;
1964               p = solv->branches.elements[i];
1965               POOL_DEBUG(SAT_DEBUG_SOLVER, "branching with %s\n", solvid2str(pool, p));
1966               queue_empty(&dq);
1967               for (j = i + 1; j < solv->branches.count; j++)
1968                 queue_push(&dq, solv->branches.elements[j]);
1969               solv->branches.count = i;
1970               level = l;
1971               revert(solv, level);
1972               if (dq.count > 1)
1973                 for (j = 0; j < dq.count; j++)
1974                   queue_push(&solv->branches, dq.elements[j]);
1975               olevel = level;
1976               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
1977               assert(why >= 0);
1978               level = setpropagatelearn(solv, level, p, disablerules, why);
1979               if (level == 0)
1980                 {
1981                   queue_free(&dq);
1982                   queue_free(&dqs);
1983                   return;
1984                 }
1985               continue;
1986             }
1987           /* all branches done, we're finally finished */
1988           break;
1989         }
1990
1991       /* minimization step */
1992      if (solv->branches.count)
1993         {
1994           int l = 0, lasti = -1, lastl = -1;
1995           Id why;
1996
1997           p = 0;
1998           for (i = solv->branches.count - 1; i >= 0; i--)
1999             {
2000               p = solv->branches.elements[i];
2001               if (p < 0)
2002                 l = -p;
2003               else if (p > 0 && solv->decisionmap[p] > l + 1)
2004                 {
2005                   lasti = i;
2006                   lastl = l;
2007                 }
2008             }
2009           if (lasti >= 0)
2010             {
2011               /* kill old solvable so that we do not loop */
2012               p = solv->branches.elements[lasti];
2013               solv->branches.elements[lasti] = 0;
2014               POOL_DEBUG(SAT_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, solvid2str(pool, p));
2015               minimizationsteps++;
2016
2017               level = lastl;
2018               revert(solv, level);
2019               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2020               assert(why >= 0);
2021               olevel = level;
2022               level = setpropagatelearn(solv, level, p, disablerules, why);
2023               if (level == 0)
2024                 {
2025                   queue_free(&dq);
2026                   queue_free(&dqs);
2027                   return;
2028                 }
2029               continue;
2030             }
2031         }
2032       break;
2033     }
2034   POOL_DEBUG(SAT_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2035
2036   POOL_DEBUG(SAT_DEBUG_STATS, "done solving.\n\n");
2037   queue_free(&dq);
2038   queue_free(&dqs);
2039 #if 0
2040   solver_printdecisionq(solv, SAT_DEBUG_RESULT);
2041 #endif
2042 }
2043
2044
2045 /*-------------------------------------------------------------------
2046  * 
2047  * remove disabled conflicts
2048  *
2049  * purpose: update the decisionmap after some rules were disabled.
2050  * this is used to calculate the suggested/recommended package list.
2051  * Also returns a "removed" list to undo the discisionmap changes.
2052  */
2053
2054 static void
2055 removedisabledconflicts(Solver *solv, Queue *removed)
2056 {
2057   Pool *pool = solv->pool;
2058   int i, n;
2059   Id p, why, *dp;
2060   Id new;
2061   Rule *r;
2062   Id *decisionmap = solv->decisionmap;
2063
2064   POOL_DEBUG(SAT_DEBUG_SCHUBI, "removedisabledconflicts\n");
2065   queue_empty(removed);
2066   for (i = 0; i < solv->decisionq.count; i++)
2067     {
2068       p = solv->decisionq.elements[i];
2069       if (p > 0)
2070         continue;
2071       /* a conflict. we never do conflicts on free decisions, so there
2072        * must have been an unit rule */
2073       why = solv->decisionq_why.elements[i];
2074       assert(why > 0);
2075       r = solv->rules + why;
2076       if (r->d < 0 && decisionmap[-p])
2077         {
2078           /* rule is now disabled, remove from decisionmap */
2079           POOL_DEBUG(SAT_DEBUG_SCHUBI, "removing conflict for package %s[%d]\n", solvid2str(pool, -p), -p);
2080           queue_push(removed, -p);
2081           queue_push(removed, decisionmap[-p]);
2082           decisionmap[-p] = 0;
2083         }
2084     }
2085   if (!removed->count)
2086     return;
2087   /* we removed some confliced packages. some of them might still
2088    * be in conflict, so search for unit rules and re-conflict */
2089   new = 0;
2090   for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2091     {
2092       if (i == solv->nrules)
2093         {
2094           i = 1;
2095           r = solv->rules + i;
2096         }
2097       if (r->d < 0)
2098         continue;
2099       if (!r->w2)
2100         {
2101           if (r->p < 0 && !decisionmap[-r->p])
2102             new = r->p;
2103         }
2104       else if (!r->d)
2105         {
2106           /* binary rule */
2107           if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2108             new = r->p;
2109           else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2110             new = r->w2;
2111         }
2112       else
2113         {
2114           if (r->p < 0 && decisionmap[-r->p] == 0)
2115             new = r->p;
2116           if (new || DECISIONMAP_FALSE(r->p))
2117             {
2118               dp = pool->whatprovidesdata + r->d;
2119               while ((p = *dp++) != 0)
2120                 {
2121                   if (new && p == new)
2122                     continue;
2123                   if (p < 0 && decisionmap[-p] == 0)
2124                     {
2125                       if (new)
2126                         {
2127                           new = 0;
2128                           break;
2129                         }
2130                       new = p;
2131                     }
2132                   else if (!DECISIONMAP_FALSE(p))
2133                     {
2134                       new = 0;
2135                       break;
2136                     }
2137                 }
2138             }
2139         }
2140       if (new)
2141         {
2142           POOL_DEBUG(SAT_DEBUG_SCHUBI, "re-conflicting package %s[%d]\n", solvid2str(pool, -new), -new);
2143           decisionmap[-new] = -1;
2144           new = 0;
2145           n = 0;        /* redo all rules */
2146         }
2147     }
2148 }
2149
2150 static inline void
2151 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2152 {
2153   int i;
2154   for (i = 0; i < removed->count; i += 2)
2155     solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2156 }
2157
2158
2159 /*-------------------------------------------------------------------
2160  *
2161  * weaken solvable dependencies
2162  */
2163
2164 static void
2165 weaken_solvable_deps(Solver *solv, Id p)
2166 {
2167   int i;
2168   Rule *r;
2169
2170   for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2171     {
2172       if (r->p != -p)
2173         continue;
2174       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2175         continue;       /* conflict */
2176       queue_push(&solv->weakruleq, i);
2177     }
2178 }
2179
2180
2181 /********************************************************************/
2182 /* main() */
2183
2184
2185 static void
2186 findrecommendedsuggested(Solver *solv)
2187 {
2188   Pool *pool = solv->pool;
2189   Queue redoq, disabledq;
2190   int goterase, i;
2191   Solvable *s;
2192   Rule *r;
2193   Map obsmap;
2194
2195   map_init(&obsmap, pool->nsolvables);
2196   if (solv->installed)
2197     {
2198       Id obs, *obsp, p, po, ppo;
2199       for (p = solv->installed->start; p < solv->installed->end; p++)
2200         {
2201           s = pool->solvables + p;
2202           if (s->repo != solv->installed || !s->obsoletes)
2203             continue;
2204           if (solv->decisionmap[p] <= 0)
2205             continue;
2206           if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
2207             continue;
2208           obsp = s->repo->idarraydata + s->obsoletes;
2209           /* foreach obsoletes */
2210           while ((obs = *obsp++) != 0)
2211             FOR_PROVIDES(po, ppo, obs)
2212               MAPSET(&obsmap, po);
2213         }
2214     }
2215
2216   queue_init(&redoq);
2217   queue_init(&disabledq);
2218   goterase = 0;
2219   /* disable all erase jobs (including weak "keep uninstalled" rules) */
2220   for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2221     {
2222       if (r->d < 0)     /* disabled ? */
2223         continue;
2224       if (r->p >= 0)    /* install job? */
2225         continue;
2226       queue_push(&disabledq, i);
2227       solver_disablerule(solv, r);
2228       goterase++;
2229     }
2230   
2231   if (goterase)
2232     {
2233       enabledisablelearntrules(solv);
2234       removedisabledconflicts(solv, &redoq);
2235     }
2236
2237   /*
2238    * find recommended packages
2239    */
2240     
2241   /* if redoq.count == 0 we already found all recommended in the
2242    * solver run */
2243   if (redoq.count || solv->dontinstallrecommended || !solv->dontshowinstalledrecommended || solv->ignorealreadyrecommended)
2244     {
2245       Id rec, *recp, p, pp;
2246
2247       /* create map of all recommened packages */
2248       solv->recommends_index = -1;
2249       MAPZERO(&solv->recommendsmap);
2250       for (i = 0; i < solv->decisionq.count; i++)
2251         {
2252           p = solv->decisionq.elements[i];
2253           if (p < 0)
2254             continue;
2255           s = pool->solvables + p;
2256           if (s->recommends)
2257             {
2258               recp = s->repo->idarraydata + s->recommends;
2259               while ((rec = *recp++) != 0)
2260                 {
2261                   FOR_PROVIDES(p, pp, rec)
2262                     if (solv->decisionmap[p] > 0)
2263                       break;
2264                   if (p)
2265                     {
2266                       if (!solv->dontshowinstalledrecommended)
2267                         {
2268                           FOR_PROVIDES(p, pp, rec)
2269                             if (solv->decisionmap[p] > 0)
2270                               MAPSET(&solv->recommendsmap, p);
2271                         }
2272                       continue; /* p != 0: already fulfilled */
2273                     }
2274                   FOR_PROVIDES(p, pp, rec)
2275                     MAPSET(&solv->recommendsmap, p);
2276                 }
2277             }
2278         }
2279       for (i = 1; i < pool->nsolvables; i++)
2280         {
2281           if (solv->decisionmap[i] < 0)
2282             continue;
2283           if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2284             continue;
2285           if (MAPTST(&obsmap, i))
2286             continue;
2287           s = pool->solvables + i;
2288           if (!MAPTST(&solv->recommendsmap, i))
2289             {
2290               if (!s->supplements)
2291                 continue;
2292               if (!pool_installable(pool, s))
2293                 continue;
2294               if (!solver_is_supplementing(solv, s))
2295                 continue;
2296             }
2297           if (solv->dontinstallrecommended)
2298             queue_push(&solv->recommendations, i);
2299           else
2300             queue_pushunique(&solv->recommendations, i);
2301         }
2302       /* we use MODE_SUGGEST here so that repo prio is ignored */
2303       policy_filter_unwanted(solv, &solv->recommendations, POLICY_MODE_SUGGEST);
2304     }
2305
2306   /*
2307    * find suggested packages
2308    */
2309     
2310   if (1)
2311     {
2312       Id sug, *sugp, p, pp;
2313
2314       /* create map of all suggests that are still open */
2315       solv->recommends_index = -1;
2316       MAPZERO(&solv->suggestsmap);
2317       for (i = 0; i < solv->decisionq.count; i++)
2318         {
2319           p = solv->decisionq.elements[i];
2320           if (p < 0)
2321             continue;
2322           s = pool->solvables + p;
2323           if (s->suggests)
2324             {
2325               sugp = s->repo->idarraydata + s->suggests;
2326               while ((sug = *sugp++) != 0)
2327                 {
2328                   FOR_PROVIDES(p, pp, sug)
2329                     if (solv->decisionmap[p] > 0)
2330                       break;
2331                   if (p)
2332                     {
2333                       if (!solv->dontshowinstalledrecommended)
2334                         {
2335                           FOR_PROVIDES(p, pp, sug)
2336                             if (solv->decisionmap[p] > 0)
2337                               MAPSET(&solv->suggestsmap, p);
2338                         }
2339                       continue; /* already fulfilled */
2340                     }
2341                   FOR_PROVIDES(p, pp, sug)
2342                     MAPSET(&solv->suggestsmap, p);
2343                 }
2344             }
2345         }
2346       for (i = 1; i < pool->nsolvables; i++)
2347         {
2348           if (solv->decisionmap[i] < 0)
2349             continue;
2350           if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2351             continue;
2352           if (MAPTST(&obsmap, i))
2353             continue;
2354           s = pool->solvables + i;
2355           if (!MAPTST(&solv->suggestsmap, i))
2356             {
2357               if (!s->enhances)
2358                 continue;
2359               if (!pool_installable(pool, s))
2360                 continue;
2361               if (!solver_is_enhancing(solv, s))
2362                 continue;
2363             }
2364           queue_push(&solv->suggestions, i);
2365         }
2366       policy_filter_unwanted(solv, &solv->suggestions, POLICY_MODE_SUGGEST);
2367     }
2368
2369   /* undo removedisabledconflicts */
2370   if (redoq.count)
2371     undo_removedisabledconflicts(solv, &redoq);
2372   queue_free(&redoq);
2373   
2374   /* undo job rule disabling */
2375   for (i = 0; i < disabledq.count; i++)
2376     solver_enablerule(solv, solv->rules + disabledq.elements[i]);
2377   queue_free(&disabledq);
2378   map_free(&obsmap);
2379 }
2380
2381 void
2382 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap)
2383 {
2384   int i;
2385   Id how, what, select;
2386   Id p, pp;
2387   for (i = 0; i < job->count; i += 2)
2388     {
2389       how = job->elements[i];
2390       if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
2391         continue;
2392       what = job->elements[i + 1];
2393       select = how & SOLVER_SELECTMASK;
2394       if (!noobsmap->size)
2395         map_grow(noobsmap, pool->nsolvables);
2396       FOR_JOB_SELECT(p, pp, select, what)
2397         MAPSET(noobsmap, p);
2398     }
2399 }
2400
2401 /*
2402  *
2403  * solve job queue
2404  *
2405  */
2406
2407 void
2408 solver_solve(Solver *solv, Queue *job)
2409 {
2410   Pool *pool = solv->pool;
2411   Repo *installed = solv->installed;
2412   int i;
2413   int oldnrules;
2414   Map addedmap;                /* '1' == have rpm-rules for solvable */
2415   Map installcandidatemap;
2416   Id how, what, select, name, weak, p, pp, d;
2417   Queue q;
2418   Solvable *s;
2419   Rule *r;
2420   int now, solve_start;
2421   int hasdupjob = 0;
2422
2423   solve_start = sat_timems(0);
2424
2425   /* log solver options */
2426   POOL_DEBUG(SAT_DEBUG_STATS, "solver started\n");
2427   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);
2428   POOL_DEBUG(SAT_DEBUG_STATS, "distupgrade=%d distupgrade_removeunsupported=%d\n", solv->distupgrade, solv->distupgrade_removeunsupported);
2429   POOL_DEBUG(SAT_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allowarchchange, solv->allowvendorchange);
2430   POOL_DEBUG(SAT_DEBUG_STATS, "promoteepoch=%d, novirtualconflicts=%d, allowselfconflicts=%d\n", pool->promoteepoch, pool->novirtualconflicts, pool->allowselfconflicts);
2431   POOL_DEBUG(SAT_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2432   POOL_DEBUG(SAT_DEBUG_STATS, "dontinstallrecommended=%d, ignorealreadyrecommended=%d, dontshowinstalledrecommended=%d\n", solv->dontinstallrecommended, solv->ignorealreadyrecommended, solv->dontshowinstalledrecommended);
2433
2434   /* create whatprovides if not already there */
2435   if (!pool->whatprovides)
2436     pool_createwhatprovides(pool);
2437
2438   /* create obsolete index */
2439   policy_create_obsolete_index(solv);
2440
2441   /* remember job */
2442   queue_free(&solv->job);
2443   queue_init_clone(&solv->job, job);
2444
2445   /*
2446    * create basic rule set of all involved packages
2447    * use addedmap bitmap to make sure we don't create rules twice
2448    */
2449
2450   /* create noobsolete map if needed */
2451   solver_calculate_noobsmap(pool, job, &solv->noobsoletes);
2452
2453   map_init(&addedmap, pool->nsolvables);
2454   MAPSET(&addedmap, SYSTEMSOLVABLE);
2455
2456   map_init(&installcandidatemap, pool->nsolvables);
2457   queue_init(&q);
2458
2459   now = sat_timems(0);
2460   /*
2461    * create rules for all package that could be involved with the solving
2462    * so called: rpm rules
2463    *
2464    */
2465   if (installed)
2466     {
2467       /* check for verify jobs */
2468       for (i = 0; i < job->count; i += 2)
2469         {
2470           how = job->elements[i];
2471           what = job->elements[i + 1];
2472           select = how & SOLVER_SELECTMASK;
2473           switch (how & SOLVER_JOBMASK)
2474             {
2475             case SOLVER_VERIFY:
2476               FOR_JOB_SELECT(p, pp, select, what)
2477                 {
2478                   s = pool->solvables + p;
2479                   if (!solv->installed || s->repo != solv->installed)
2480                     continue;
2481                   if (!solv->fixmap.size)
2482                     map_grow(&solv->fixmap, solv->installed->end - solv->installed->start);
2483                   MAPSET(&solv->fixmap, p - solv->installed->start);
2484                 }
2485               break;
2486             default:
2487               break;
2488             }
2489         }
2490
2491       oldnrules = solv->nrules;
2492       POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
2493       FOR_REPO_SOLVABLES(installed, p, s)
2494         solver_addrpmrulesforsolvable(solv, s, &addedmap);
2495       POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
2496       POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
2497       oldnrules = solv->nrules;
2498       FOR_REPO_SOLVABLES(installed, p, s)
2499         solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
2500       POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
2501     }
2502
2503   /*
2504    * create rules for all packages involved in the job
2505    * (to be installed or removed)
2506    */
2507     
2508   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
2509   oldnrules = solv->nrules;
2510   for (i = 0; i < job->count; i += 2)
2511     {
2512       how = job->elements[i];
2513       what = job->elements[i + 1];
2514       select = how & SOLVER_SELECTMASK;
2515
2516       switch (how & SOLVER_JOBMASK)
2517         {
2518         case SOLVER_INSTALL:
2519           FOR_JOB_SELECT(p, pp, select, what)
2520             {
2521               MAPSET(&installcandidatemap, p);
2522               solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
2523             }
2524           break;
2525         case SOLVER_DISTUPGRADE:
2526           if (!solv->distupgrade)
2527             hasdupjob = 1;
2528           break;
2529         default:
2530           break;
2531         }
2532     }
2533   POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
2534
2535     
2536   /*
2537    * add rules for suggests, enhances
2538    */
2539   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for suggested/enhanced packages ***\n");
2540   oldnrules = solv->nrules;
2541   solver_addrpmrulesforweak(solv, &addedmap);
2542   POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
2543
2544   /*
2545    * first pass done, we now have all the rpm rules we need.
2546    * unify existing rules before going over all job rules and
2547    * policy rules.
2548    * at this point the system is always solvable,
2549    * as an empty system (remove all packages) is a valid solution
2550    */
2551
2552   IF_POOLDEBUG (SAT_DEBUG_STATS)
2553     {
2554       int possible = 0, installable = 0;
2555       for (i = 1; i < pool->nsolvables; i++)
2556         {
2557           if (pool_installable(pool, pool->solvables + i))
2558             installable++;
2559           if (MAPTST(&addedmap, i))
2560             possible++;
2561         }
2562       POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
2563     }
2564
2565   solver_unifyrules(solv);                          /* remove duplicate rpm rules */
2566   solv->rpmrules_end = solv->nrules;              /* mark end of rpm rules */
2567
2568   POOL_DEBUG(SAT_DEBUG_STATS, "rpm rule memory usage: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
2569   POOL_DEBUG(SAT_DEBUG_STATS, "rpm rule creation took %d ms\n", sat_timems(now));
2570
2571   /* create dup maps if needed. We need the maps early to create our
2572    * update rules */
2573   if (hasdupjob)
2574     solver_createdupmaps(solv);
2575
2576   /*
2577    * create feature rules
2578    * 
2579    * foreach installed:
2580    *   create assertion (keep installed, if no update available)
2581    *   or
2582    *   create update rule (A|update1(A)|update2(A)|...)
2583    * 
2584    * those are used later on to keep a version of the installed packages in
2585    * best effort mode
2586    */
2587     
2588   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add feature rules ***\n");
2589   solv->featurerules = solv->nrules;              /* mark start of feature rules */
2590   if (installed)
2591     {
2592       /* foreach possibly installed solvable */
2593       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2594         {
2595           if (s->repo != installed)
2596             {
2597               solver_addrule(solv, 0, 0);       /* create dummy rule */
2598               continue;
2599             }
2600           solver_addupdaterule(solv, s, 1);    /* allow s to be updated */
2601         }
2602       /* make sure we accounted for all rules */
2603       assert(solv->nrules - solv->featurerules == installed->end - installed->start);
2604     }
2605   solv->featurerules_end = solv->nrules;
2606
2607     /*
2608      * Add update rules for installed solvables
2609      * 
2610      * almost identical to feature rules
2611      * except that downgrades/archchanges/vendorchanges are not allowed
2612      */
2613     
2614   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add update rules ***\n");
2615   solv->updaterules = solv->nrules;
2616
2617   if (installed)
2618     { /* foreach installed solvables */
2619       /* we create all update rules, but disable some later on depending on the job */
2620       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2621         {
2622           Rule *sr;
2623
2624           if (s->repo != installed)
2625             {
2626               solver_addrule(solv, 0, 0);       /* create dummy rule */
2627               continue;
2628             }
2629           solver_addupdaterule(solv, s, 0);     /* allowall = 0: downgrades not allowed */
2630           /*
2631            * check for and remove duplicate
2632            */
2633           r = solv->rules + solv->nrules - 1;           /* r: update rule */
2634           sr = r - (installed->end - installed->start); /* sr: feature rule */
2635           /* it's orphaned if there is no feature rule or the feature rule
2636            * consists just of the installed package */
2637           if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
2638             queue_push(&solv->orphaned, i);
2639           if (!r->p)
2640             {
2641               assert(solv->distupgrade && !sr->p);
2642               continue;
2643             }
2644           if (!solver_samerule(solv, r, sr))
2645             {
2646               /* identical rule, kill unneeded one */
2647               if (solv->allowuninstall)
2648                 {
2649                   /* keep feature rule, make it weak */
2650                   memset(r, 0, sizeof(*r));
2651                   queue_push(&solv->weakruleq, sr - solv->rules);
2652                 }
2653               else
2654                 {
2655                   /* keep update rule */
2656                   memset(sr, 0, sizeof(*sr));
2657                 }
2658             }
2659           else if (solv->allowuninstall)
2660             {
2661               /* make both feature and update rule weak */
2662               queue_push(&solv->weakruleq, r - solv->rules);
2663               queue_push(&solv->weakruleq, sr - solv->rules);
2664             }
2665           else
2666             solver_disablerule(solv, sr);
2667         }
2668       /* consistency check: we added a rule for _every_ installed solvable */
2669       assert(solv->nrules - solv->updaterules == installed->end - installed->start);
2670     }
2671   solv->updaterules_end = solv->nrules;
2672
2673
2674   /*
2675    * now add all job rules
2676    */
2677
2678   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
2679
2680   solv->jobrules = solv->nrules;
2681   for (i = 0; i < job->count; i += 2)
2682     {
2683       oldnrules = solv->nrules;
2684
2685       how = job->elements[i];
2686       what = job->elements[i + 1];
2687       weak = how & SOLVER_WEAK;
2688       select = how & SOLVER_SELECTMASK;
2689       switch (how & SOLVER_JOBMASK)
2690         {
2691         case SOLVER_INSTALL:
2692           POOL_DEBUG(SAT_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2693           if (select == SOLVER_SOLVABLE)
2694             {
2695               p = what;
2696               d = 0;
2697             }
2698           else
2699             {
2700               queue_empty(&q);
2701               FOR_JOB_SELECT(p, pp, select, what)
2702                 queue_push(&q, p);
2703               if (!q.count)
2704                 {
2705                   /* no candidate found, make this an impossible rule */
2706                   queue_push(&q, -SYSTEMSOLVABLE);
2707                 }
2708               p = queue_shift(&q);      /* get first candidate */
2709               d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q);    /* internalize */
2710             }
2711           solver_addrule(solv, p, d);           /* add install rule */
2712           queue_push(&solv->ruletojob, i);
2713           if (weak)
2714             queue_push(&solv->weakruleq, solv->nrules - 1);
2715           break;
2716         case SOLVER_ERASE:
2717           POOL_DEBUG(SAT_DEBUG_JOB, "job: %serase %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2718           if (select == SOLVER_SOLVABLE && solv->installed && pool->solvables[what].repo == solv->installed)
2719             {
2720               /* special case for "erase a specific solvable": we also
2721                * erase all other solvables with that name, so that they
2722                * don't get picked up as replacement */
2723               name = pool->solvables[what].name;
2724               FOR_PROVIDES(p, pp, name)
2725                 {
2726                   if (p == what)
2727                     continue;
2728                   s = pool->solvables + p;
2729                   if (s->name != name)
2730                     continue;
2731                   /* keep other versions installed */
2732                   if (s->repo == solv->installed)
2733                     continue;
2734                   /* keep installcandidates of other jobs */
2735                   if (MAPTST(&installcandidatemap, p))
2736                     continue;
2737                   solver_addrule(solv, -p, 0);                  /* remove by Id */
2738                   queue_push(&solv->ruletojob, i);
2739                   if (weak)
2740                     queue_push(&solv->weakruleq, solv->nrules - 1);
2741                 }
2742             }
2743           FOR_JOB_SELECT(p, pp, select, what)
2744             {
2745               solver_addrule(solv, -p, 0);
2746               queue_push(&solv->ruletojob, i);
2747               if (weak)
2748                 queue_push(&solv->weakruleq, solv->nrules - 1);
2749             }
2750           break;
2751
2752         case SOLVER_UPDATE:
2753           POOL_DEBUG(SAT_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2754           FOR_JOB_SELECT(p, pp, select, what)
2755             {
2756               s = pool->solvables + p;
2757               if (!solv->installed || s->repo != solv->installed)
2758                 continue;
2759               if (!solv->updatemap.size)
2760                 map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
2761               MAPSET(&solv->updatemap, p - solv->installed->start);
2762             }
2763           break;
2764         case SOLVER_VERIFY:
2765           POOL_DEBUG(SAT_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2766           break;
2767         case SOLVER_WEAKENDEPS:
2768           POOL_DEBUG(SAT_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2769           if (select != SOLVER_SOLVABLE)
2770             break;
2771           s = pool->solvables + what;
2772           weaken_solvable_deps(solv, what);
2773           break;
2774         case SOLVER_NOOBSOLETES:
2775           POOL_DEBUG(SAT_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2776           break;
2777         case SOLVER_LOCK:
2778           POOL_DEBUG(SAT_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(solv, select, what));
2779           FOR_JOB_SELECT(p, pp, select, what)
2780             {
2781               s = pool->solvables + p;
2782               if (installed && s->repo == installed)
2783                 solver_addrule(solv, p, 0);
2784               else
2785                 solver_addrule(solv, -p, 0);
2786               queue_push(&solv->ruletojob, i);
2787               if (weak)
2788                 queue_push(&solv->weakruleq, solv->nrules - 1);
2789             }
2790           break;
2791         case SOLVER_DISTUPGRADE:
2792           POOL_DEBUG(SAT_DEBUG_JOB, "job: distupgrade repo #%d\n", what);
2793           break;
2794         default:
2795           POOL_DEBUG(SAT_DEBUG_JOB, "job: unknown job\n");
2796           break;
2797         }
2798         
2799         /*
2800          * debug
2801          */
2802         
2803       IF_POOLDEBUG (SAT_DEBUG_JOB)
2804         {
2805           int j;
2806           if (solv->nrules == oldnrules)
2807             POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created\n");
2808           for (j = oldnrules; j < solv->nrules; j++)
2809             {
2810               POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
2811               solver_printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
2812             }
2813         }
2814     }
2815   assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
2816   solv->jobrules_end = solv->nrules;
2817
2818   /* now create infarch and dup rules */
2819   if (!solv->noinfarchcheck)
2820     {
2821       solver_addinfarchrules(solv, &addedmap);
2822       if (pool->obsoleteusescolors)
2823         {
2824           /* currently doesn't work well with infarch rules, so make
2825            * them weak */
2826           for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
2827             queue_push(&solv->weakruleq, i);
2828         }
2829     }
2830   else
2831     solv->infarchrules = solv->infarchrules_end = solv->nrules;
2832
2833   if (hasdupjob)
2834     {
2835       solver_addduprules(solv, &addedmap);
2836       solver_freedupmaps(solv); /* no longer needed */
2837     }
2838   else
2839     solv->duprules = solv->duprules_end = solv->nrules;
2840
2841   if (1)
2842     {
2843       extern void addchoicerules(Solver *solv);
2844       addchoicerules(solv);
2845     }
2846
2847   /* all rules created
2848    * --------------------------------------------------------------
2849    * prepare for solving
2850    */
2851     
2852   /* free unneeded memory */
2853   map_free(&addedmap);
2854   map_free(&installcandidatemap);
2855   queue_free(&q);
2856
2857   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);
2858
2859   /* create weak map */
2860   map_init(&solv->weakrulemap, solv->nrules);
2861   for (i = 0; i < solv->weakruleq.count; i++)
2862     {
2863       p = solv->weakruleq.elements[i];
2864       MAPSET(&solv->weakrulemap, p);
2865     }
2866
2867   /* all new rules are learnt after this point */
2868   solv->learntrules = solv->nrules;
2869
2870   /* create watches chains */
2871   makewatches(solv);
2872
2873   /* create assertion index. it is only used to speed up
2874    * makeruledecsions() a bit */
2875   for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
2876     if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
2877       queue_push(&solv->ruleassertions, i);
2878
2879   /* disable update rules that conflict with our job */
2880   solver_disablepolicyrules(solv);
2881
2882   /* make decisions based on job/update assertions */
2883   makeruledecisions(solv);
2884   POOL_DEBUG(SAT_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
2885
2886   /*
2887    * ********************************************
2888    * solve!
2889    * ********************************************
2890    */
2891     
2892   now = sat_timems(0);
2893   solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
2894   POOL_DEBUG(SAT_DEBUG_STATS, "solver took %d ms\n", sat_timems(now));
2895
2896   /*
2897    * calculate recommended/suggested packages
2898    */
2899   findrecommendedsuggested(solv);
2900
2901   /*
2902    * prepare solution queue if there were problems
2903    */
2904   solver_prepare_solutions(solv);
2905
2906   /*
2907    * finally prepare transaction info
2908    */
2909   transaction_calculate(&solv->trans, &solv->decisionq, &solv->noobsoletes);
2910
2911   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);
2912   POOL_DEBUG(SAT_DEBUG_STATS, "solver_solve took %d ms\n", sat_timems(solve_start));
2913 }
2914
2915 /***********************************************************************/
2916 /* disk usage computations */
2917
2918 /*-------------------------------------------------------------------
2919  * 
2920  * calculate DU changes
2921  */
2922
2923 void
2924 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
2925 {
2926   Map installedmap;
2927
2928   solver_create_state_maps(solv, &installedmap, 0);
2929   pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
2930   map_free(&installedmap);
2931 }
2932
2933
2934 /*-------------------------------------------------------------------
2935  * 
2936  * calculate changes in install size
2937  */
2938
2939 int
2940 solver_calc_installsizechange(Solver *solv)
2941 {
2942   Map installedmap;
2943   int change;
2944
2945   solver_create_state_maps(solv, &installedmap, 0);
2946   change = pool_calc_installsizechange(solv->pool, &installedmap);
2947   map_free(&installedmap);
2948   return change;
2949 }
2950
2951 void
2952 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
2953 {
2954   Map installedmap;
2955   solver_create_state_maps(solv, &installedmap, 0);
2956   pool_trivial_installable_noobsoletesmap(solv->pool, &installedmap, pkgs, res, solv->noobsoletes.size ? &solv->noobsoletes : 0);
2957   map_free(&installedmap);
2958 }
2959
2960
2961 #if 0
2962 #define FIND_INVOLVED_DEBUG 0
2963 void
2964 solver_find_involved(Solver *solv, Queue *installedq, Solvable *ts, Queue *q)
2965 {
2966   Pool *pool = solv->pool;
2967   Map im;
2968   Map installedm;
2969   Solvable *s;
2970   Queue iq;
2971   Queue installedq_internal;
2972   Id tp, ip, p, pp, req, *reqp, sup, *supp;
2973   int i, count;
2974
2975   tp = ts - pool->solvables;
2976   queue_init(&iq);
2977   queue_init(&installedq_internal);
2978   map_init(&im, pool->nsolvables);
2979   map_init(&installedm, pool->nsolvables);
2980
2981   if (!installedq)
2982     {
2983       installedq = &installedq_internal;
2984       if (solv->installed)
2985         {
2986           for (ip = solv->installed->start; ip < solv->installed->end; ip++)
2987             {
2988               s = pool->solvables + ip;
2989               if (s->repo != solv->installed)
2990                 continue;
2991               queue_push(installedq, ip);
2992             }
2993         }
2994     }
2995   for (i = 0; i < installedq->count; i++)
2996     {
2997       ip = installedq->elements[i];
2998       MAPSET(&installedm, ip);
2999       MAPSET(&im, ip);
3000     }
3001
3002   queue_push(&iq, ts - pool->solvables);
3003   while (iq.count)
3004     {
3005       ip = queue_shift(&iq);
3006       if (!MAPTST(&im, ip))
3007         continue;
3008       if (!MAPTST(&installedm, ip))
3009         continue;
3010       MAPCLR(&im, ip);
3011       s = pool->solvables + ip;
3012 #if FIND_INVOLVED_DEBUG
3013       printf("hello %s\n", solvable2str(pool, s));
3014 #endif
3015       if (s->requires)
3016         {
3017           reqp = s->repo->idarraydata + s->requires;
3018           while ((req = *reqp++) != 0)
3019             {
3020               if (req == SOLVABLE_PREREQMARKER)
3021                 continue;
3022               /* count number of installed packages that match */
3023               count = 0;
3024               FOR_PROVIDES(p, pp, req)
3025                 if (MAPTST(&installedm, p))
3026                   count++;
3027               if (count > 1)
3028                 continue;
3029               FOR_PROVIDES(p, pp, req)
3030                 {
3031                   if (MAPTST(&im, p))
3032                     {
3033 #if FIND_INVOLVED_DEBUG
3034                       printf("%s requires %s\n", solvid2str(pool, ip), solvid2str(pool, p));
3035 #endif
3036                       queue_push(&iq, p);
3037                     }
3038                 }
3039             }
3040         }
3041       if (s->recommends)
3042         {
3043           reqp = s->repo->idarraydata + s->recommends;
3044           while ((req = *reqp++) != 0)
3045             {
3046               count = 0;
3047               FOR_PROVIDES(p, pp, req)
3048                 if (MAPTST(&installedm, p))
3049                   count++;
3050               if (count > 1)
3051                 continue;
3052               FOR_PROVIDES(p, pp, req)
3053                 {
3054                   if (MAPTST(&im, p))
3055                     {
3056 #if FIND_INVOLVED_DEBUG
3057                       printf("%s recommends %s\n", solvid2str(pool, ip), solvid2str(pool, p));
3058 #endif
3059                       queue_push(&iq, p);
3060                     }
3061                 }
3062             }
3063         }
3064       if (!iq.count)
3065         {
3066           /* supplements pass */
3067           for (i = 0; i < installedq->count; i++)
3068             {
3069               ip = installedq->elements[i];
3070               s = pool->solvables + ip;
3071               if (!s->supplements)
3072                 continue;
3073               if (!MAPTST(&im, ip))
3074                 continue;
3075               supp = s->repo->idarraydata + s->supplements;
3076               while ((sup = *supp++) != 0)
3077                 if (!dep_possible(solv, sup, &im) && dep_possible(solv, sup, &installedm))
3078                   break;
3079               /* no longer supplemented, also erase */
3080               if (sup)
3081                 {
3082 #if FIND_INVOLVED_DEBUG
3083                   printf("%s supplemented\n", solvid2str(pool, ip));
3084 #endif
3085                   queue_push(&iq, ip);
3086                 }
3087             }
3088         }
3089     }
3090
3091   for (i = 0; i < installedq->count; i++)
3092     {
3093       ip = installedq->elements[i];
3094       if (MAPTST(&im, ip))
3095         queue_push(&iq, ip);
3096     }
3097
3098   while (iq.count)
3099     {
3100       ip = queue_shift(&iq);
3101       if (!MAPTST(&installedm, ip))
3102         continue;
3103       s = pool->solvables + ip;
3104 #if FIND_INVOLVED_DEBUG
3105       printf("bye %s\n", solvable2str(pool, s));
3106 #endif
3107       if (s->requires)
3108         {
3109           reqp = s->repo->idarraydata + s->requires;
3110           while ((req = *reqp++) != 0)
3111             {
3112               FOR_PROVIDES(p, pp, req)
3113                 {
3114                   if (!MAPTST(&im, p))
3115                     {
3116                       if (p == tp)
3117                         continue;
3118 #if FIND_INVOLVED_DEBUG
3119                       printf("%s requires %s\n", solvid2str(pool, ip), solvid2str(pool, p));
3120 #endif
3121                       MAPSET(&im, p);
3122                       queue_push(&iq, p);
3123                     }
3124                 }
3125             }
3126         }
3127       if (s->recommends)
3128         {
3129           reqp = s->repo->idarraydata + s->recommends;
3130           while ((req = *reqp++) != 0)
3131             {
3132               FOR_PROVIDES(p, pp, req)
3133                 {
3134                   if (!MAPTST(&im, p))
3135                     {
3136                       if (p == tp)
3137                         continue;
3138 #if FIND_INVOLVED_DEBUG
3139                       printf("%s recommends %s\n", solvid2str(pool, ip), solvid2str(pool, p));
3140 #endif
3141                       MAPSET(&im, p);
3142                       queue_push(&iq, p);
3143                     }
3144                 }
3145             }
3146         }
3147       if (!iq.count)
3148         {
3149           /* supplements pass */
3150           for (i = 0; i < installedq->count; i++)
3151             {
3152               ip = installedq->elements[i];
3153               if (ip == tp)
3154                 continue;
3155               s = pool->solvables + ip;
3156               if (!s->supplements)
3157                 continue;
3158               if (MAPTST(&im, ip))
3159                 continue;
3160               supp = s->repo->idarraydata + s->supplements;
3161               while ((sup = *supp++) != 0)
3162                 if (dep_possible(solv, sup, &im))
3163                   break;
3164               if (sup)
3165                 {
3166 #if FIND_INVOLVED_DEBUG
3167                   printf("%s supplemented\n", solvid2str(pool, ip));
3168 #endif
3169                   MAPSET(&im, ip);
3170                   queue_push(&iq, ip);
3171                 }
3172             }
3173         }
3174     }
3175     
3176   queue_free(&iq);
3177
3178   /* convert map into result */
3179   for (i = 0; i < installedq->count; i++)
3180     {
3181       ip = installedq->elements[i];
3182       if (MAPTST(&im, ip))
3183         continue;
3184       if (ip == ts - pool->solvables)
3185         continue;
3186       queue_push(q, ip);
3187     }
3188   map_free(&im);
3189   map_free(&installedm);
3190   queue_free(&installedq_internal);
3191 }
3192 #endif
3193