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