- add SOLVER_SET bits to tell solver which properties of a job are intended by the...
[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       /* disable last weak rule */
979       solv->problems.count = oldproblemcount;
980       solv->learnt_pool.count = oldlearntpoolcount;
981       if (lastweak >= solv->jobrules && lastweak < solv->jobrules_end)
982         v = -(solv->ruletojob.elements[lastweak - solv->jobrules] + 1);
983       else
984         v = lastweak;
985       POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling ");
986       solver_printruleclass(solv, SAT_DEBUG_UNSOLVABLE, solv->rules + lastweak);
987       if (lastweak >= solv->choicerules && lastweak < solv->choicerules_end)
988         solver_disablechoicerules(solv, solv->rules + lastweak);
989       solver_disableproblem(solv, v);
990       if (v < 0)
991         solver_reenablepolicyrules(solv, -(v + 1));
992       solver_reset(solv);
993       return 1;
994     }
995
996   /* finish proof */
997   queue_push(&solv->learnt_pool, 0);
998   solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
999
1000   if (disablerules)
1001     {
1002       for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1003         solver_disableproblem(solv, solv->problems.elements[i]);
1004       /* XXX: might want to enable all weak rules again */
1005       solver_reset(solv);
1006       return 1;
1007     }
1008   POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1009   return 0;
1010 }
1011
1012
1013 /********************************************************************/
1014 /* Decision revert */
1015
1016 /*-------------------------------------------------------------------
1017  * 
1018  * revert
1019  * revert decision at level
1020  */
1021
1022 static void
1023 revert(Solver *solv, int level)
1024 {
1025   Pool *pool = solv->pool;
1026   Id v, vv;
1027   while (solv->decisionq.count)
1028     {
1029       v = solv->decisionq.elements[solv->decisionq.count - 1];
1030       vv = v > 0 ? v : -v;
1031       if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1032         break;
1033       POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1034       if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
1035         solv->recommendations.count--;
1036       solv->decisionmap[vv] = 0;
1037       solv->decisionq.count--;
1038       solv->decisionq_why.count--;
1039       solv->propagate_index = solv->decisionq.count;
1040     }
1041   while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1042     {
1043       solv->branches.count--;
1044       while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1045         solv->branches.count--;
1046     }
1047   solv->recommends_index = -1;
1048 }
1049
1050
1051 /*-------------------------------------------------------------------
1052  * 
1053  * watch2onhighest - put watch2 on literal with highest level
1054  */
1055
1056 static inline void
1057 watch2onhighest(Solver *solv, Rule *r)
1058 {
1059   int l, wl = 0;
1060   Id d, v, *dp;
1061
1062   d = r->d < 0 ? -r->d - 1 : r->d;
1063   if (!d)
1064     return;     /* binary rule, both watches are set */
1065   dp = solv->pool->whatprovidesdata + d;
1066   while ((v = *dp++) != 0)
1067     {
1068       l = solv->decisionmap[v < 0 ? -v : v];
1069       if (l < 0)
1070         l = -l;
1071       if (l > wl)
1072         {
1073           r->w2 = dp[-1];
1074           wl = l;
1075         }
1076     }
1077 }
1078
1079
1080 /*-------------------------------------------------------------------
1081  * 
1082  * setpropagatelearn
1083  *
1084  * add free decision (solvable to install) to decisionq
1085  * increase level and propagate decision
1086  * return if no conflict.
1087  *
1088  * in conflict case, analyze conflict rule, add resulting
1089  * rule to learnt rule set, make decision from learnt
1090  * rule (always unit) and re-propagate.
1091  *
1092  * returns the new solver level or 0 if unsolvable
1093  *
1094  */
1095
1096 static int
1097 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules, Id ruleid)
1098 {
1099   Pool *pool = solv->pool;
1100   Rule *r;
1101   Id p = 0, d = 0;
1102   int l, why;
1103
1104   assert(ruleid >= 0);
1105   if (decision)
1106     {
1107       level++;
1108       if (decision > 0)
1109         solv->decisionmap[decision] = level;
1110       else
1111         solv->decisionmap[-decision] = -level;
1112       queue_push(&solv->decisionq, decision);
1113       queue_push(&solv->decisionq_why, -ruleid);        /* <= 0 -> free decision */
1114     }
1115   for (;;)
1116     {
1117       r = propagate(solv, level);
1118       if (!r)
1119         break;
1120       if (level == 1)
1121         return analyze_unsolvable(solv, r, disablerules);
1122       POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1123       l = analyze(solv, level, r, &p, &d, &why);        /* learnt rule in p and d */
1124       assert(l > 0 && l < level);
1125       POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1126       level = l;
1127       revert(solv, level);
1128       r = solver_addrule(solv, p, d);
1129       assert(r);
1130       assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
1131       queue_push(&solv->learnt_why, why);
1132       if (d)
1133         {
1134           /* at least 2 literals, needs watches */
1135           watch2onhighest(solv, r);
1136           addwatches_rule(solv, r);
1137         }
1138       else
1139         {
1140           /* learnt rule is an assertion */
1141           queue_push(&solv->ruleassertions, r - solv->rules);
1142         }
1143       /* the new rule is unit by design */
1144       solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1145       queue_push(&solv->decisionq, p);
1146       queue_push(&solv->decisionq_why, r - solv->rules);
1147       IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1148         {
1149           POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
1150           solver_printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
1151           POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
1152           solver_printrule(solv, SAT_DEBUG_ANALYZE, r);
1153         }
1154     }
1155   return level;
1156 }
1157
1158
1159 /*-------------------------------------------------------------------
1160  * 
1161  * select and install
1162  * 
1163  * install best package from the queue. We add an extra package, inst, if
1164  * provided. See comment in weak install section.
1165  *
1166  * returns the new solver level or 0 if unsolvable
1167  *
1168  */
1169
1170 static int
1171 selectandinstall(Solver *solv, int level, Queue *dq, int disablerules, Id ruleid)
1172 {
1173   Pool *pool = solv->pool;
1174   Id p;
1175   int i;
1176
1177   if (dq->count > 1)
1178     policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1179   if (dq->count > 1)
1180     {
1181       /* XXX: didn't we already do that? */
1182       /* XXX: shouldn't we prefer installed packages? */
1183       /* XXX: move to policy.c? */
1184       /* choose the supplemented one */
1185       for (i = 0; i < dq->count; i++)
1186         if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1187           {
1188             dq->elements[0] = dq->elements[i];
1189             dq->count = 1;
1190             break;
1191           }
1192     }
1193   if (dq->count > 1)
1194     {
1195       /* multiple candidates, open a branch */
1196       for (i = 1; i < dq->count; i++)
1197         queue_push(&solv->branches, dq->elements[i]);
1198       queue_push(&solv->branches, -level);
1199     }
1200   p = dq->elements[0];
1201
1202   POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvid2str(pool, p));
1203
1204   return setpropagatelearn(solv, level, p, disablerules, ruleid);
1205 }
1206
1207
1208 /********************************************************************/
1209 /* Main solver interface */
1210
1211
1212 /*-------------------------------------------------------------------
1213  * 
1214  * solver_create
1215  * create solver structure
1216  *
1217  * pool: all available solvables
1218  * installed: installed Solvables
1219  *
1220  *
1221  * Upon solving, rules are created to flag the Solvables
1222  * of the 'installed' Repo as installed.
1223  */
1224
1225 Solver *
1226 solver_create(Pool *pool)
1227 {
1228   Solver *solv;
1229   solv = (Solver *)sat_calloc(1, sizeof(Solver));
1230   solv->pool = pool;
1231   solv->installed = pool->installed;
1232
1233   transaction_init(&solv->trans, pool);
1234   queue_init(&solv->ruletojob);
1235   queue_init(&solv->decisionq);
1236   queue_init(&solv->decisionq_why);
1237   queue_init(&solv->problems);
1238   queue_init(&solv->suggestions);
1239   queue_init(&solv->recommendations);
1240   queue_init(&solv->orphaned);
1241   queue_init(&solv->learnt_why);
1242   queue_init(&solv->learnt_pool);
1243   queue_init(&solv->branches);
1244   queue_init(&solv->covenantq);
1245   queue_init(&solv->weakruleq);
1246   queue_init(&solv->ruleassertions);
1247
1248   map_init(&solv->recommendsmap, pool->nsolvables);
1249   map_init(&solv->suggestsmap, pool->nsolvables);
1250   map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1251   solv->recommends_index = 0;
1252
1253   solv->decisionmap = (Id *)sat_calloc(pool->nsolvables, sizeof(Id));
1254   solv->nrules = 1;
1255   solv->rules = sat_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1256   memset(solv->rules, 0, sizeof(Rule));
1257
1258   return solv;
1259 }
1260
1261
1262 /*-------------------------------------------------------------------
1263  * 
1264  * solver_free
1265  */
1266
1267 void
1268 solver_free(Solver *solv)
1269 {
1270   transaction_free(&solv->trans);
1271   queue_free(&solv->job);
1272   queue_free(&solv->ruletojob);
1273   queue_free(&solv->decisionq);
1274   queue_free(&solv->decisionq_why);
1275   queue_free(&solv->learnt_why);
1276   queue_free(&solv->learnt_pool);
1277   queue_free(&solv->problems);
1278   queue_free(&solv->solutions);
1279   queue_free(&solv->suggestions);
1280   queue_free(&solv->recommendations);
1281   queue_free(&solv->orphaned);
1282   queue_free(&solv->branches);
1283   queue_free(&solv->covenantq);
1284   queue_free(&solv->weakruleq);
1285   queue_free(&solv->ruleassertions);
1286
1287   map_free(&solv->recommendsmap);
1288   map_free(&solv->suggestsmap);
1289   map_free(&solv->noupdate);
1290   map_free(&solv->weakrulemap);
1291   map_free(&solv->noobsoletes);
1292
1293   map_free(&solv->updatemap);
1294   map_free(&solv->fixmap);
1295   map_free(&solv->dupmap);
1296   map_free(&solv->dupinvolvedmap);
1297   map_free(&solv->droporphanedmap);
1298   map_free(&solv->cleandepsmap);
1299
1300   sat_free(solv->decisionmap);
1301   sat_free(solv->rules);
1302   sat_free(solv->watches);
1303   sat_free(solv->obsoletes);
1304   sat_free(solv->obsoletes_data);
1305   sat_free(solv->multiversionupdaters);
1306   sat_free(solv->choicerules_ref);
1307   sat_free(solv);
1308 }
1309
1310
1311 /*-------------------------------------------------------------------
1312  * 
1313  * solver_run_sat
1314  *
1315  * all rules have been set up, now actually run the solver
1316  *
1317  */
1318
1319 void
1320 solver_run_sat(Solver *solv, int disablerules, int doweak)
1321 {
1322   Queue dq;             /* local decisionqueue */
1323   Queue dqs;            /* local decisionqueue for supplements */
1324   int systemlevel;
1325   int level, olevel;
1326   Rule *r;
1327   int i, j, n;
1328   Solvable *s;
1329   Pool *pool = solv->pool;
1330   Id p, *dp;
1331   int minimizationsteps;
1332   int installedpos = solv->installed ? solv->installed->start : 0;
1333
1334   IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1335     {
1336       POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1337       for (i = 1; i < solv->nrules; i++)
1338         solver_printruleclass(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
1339     }
1340
1341   POOL_DEBUG(SAT_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1342
1343   IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
1344     solver_printdecisions(solv);
1345
1346   /* start SAT algorithm */
1347   level = 1;
1348   systemlevel = level + 1;
1349   POOL_DEBUG(SAT_DEBUG_SOLVER, "solving...\n");
1350
1351   queue_init(&dq);
1352   queue_init(&dqs);
1353
1354   /*
1355    * here's the main loop:
1356    * 1) propagate new decisions (only needed once)
1357    * 2) fulfill jobs
1358    * 3) try to keep installed packages
1359    * 4) fulfill all unresolved rules
1360    * 5) install recommended packages
1361    * 6) minimalize solution if we had choices
1362    * if we encounter a problem, we rewind to a safe level and restart
1363    * with step 1
1364    */
1365    
1366   minimizationsteps = 0;
1367   for (;;)
1368     {
1369       /*
1370        * initial propagation of the assertions
1371        */
1372       if (level == 1)
1373         {
1374           POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d;  size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1375           if ((r = propagate(solv, level)) != 0)
1376             {
1377               if (analyze_unsolvable(solv, r, disablerules))
1378                 continue;
1379               queue_free(&dq);
1380               queue_free(&dqs);
1381               return;
1382             }
1383         }
1384
1385       /*
1386        * resolve jobs first
1387        */
1388      if (level < systemlevel)
1389         {
1390           POOL_DEBUG(SAT_DEBUG_SOLVER, "resolving job rules\n");
1391           for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1392             {
1393               Id l;
1394               if (r->d < 0)             /* ignore disabled rules */
1395                 continue;
1396               queue_empty(&dq);
1397               FOR_RULELITERALS(l, dp, r)
1398                 {
1399                   if (l < 0)
1400                     {
1401                       if (solv->decisionmap[-l] <= 0)
1402                         break;
1403                     }
1404                   else
1405                     {
1406                       if (solv->decisionmap[l] > 0)
1407                         break;
1408                       if (solv->decisionmap[l] == 0)
1409                         queue_push(&dq, l);
1410                     }
1411                 }
1412               if (l || !dq.count)
1413                 continue;
1414               /* prune to installed if not updating */
1415               if (dq.count > 1 && solv->installed && !solv->updatemap_all)
1416                 {
1417                   int j, k;
1418                   for (j = k = 0; j < dq.count; j++)
1419                     {
1420                       Solvable *s = pool->solvables + dq.elements[j];
1421                       if (s->repo == solv->installed)
1422                         {
1423                           dq.elements[k++] = dq.elements[j];
1424                           if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
1425                             {
1426                               k = 0;    /* package wants to be updated, do not prune */
1427                               break;
1428                             }
1429                         }
1430                     }
1431                   if (k)
1432                     dq.count = k;
1433                 }
1434               olevel = level;
1435               level = selectandinstall(solv, level, &dq, disablerules, i);
1436               if (level == 0)
1437                 {
1438                   queue_free(&dq);
1439                   queue_free(&dqs);
1440                   return;
1441                 }
1442               if (level <= olevel)
1443                 break;
1444             }
1445           systemlevel = level + 1;
1446           if (i < solv->jobrules_end)
1447             continue;
1448         }
1449
1450
1451       /*
1452        * installed packages
1453        */
1454       if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1455         {
1456           Repo *installed = solv->installed;
1457           int pass;
1458
1459           POOL_DEBUG(SAT_DEBUG_SOLVER, "resolving installed packages\n");
1460           /* we use two passes if we need to update packages 
1461            * to create a better user experience */
1462           for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1463             {
1464               int passlevel = level;
1465               /* start with installedpos, the position that gave us problems last time */
1466               for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1467                 {
1468                   Rule *rr;
1469                   Id d;
1470
1471                   if (i == installed->end)
1472                     i = installed->start;
1473                   s = pool->solvables + i;
1474                   if (s->repo != installed)
1475                     continue;
1476
1477                   if (solv->decisionmap[i] > 0)
1478                     continue;
1479                   if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1480                     continue;           /* updates first */
1481                   r = solv->rules + solv->updaterules + (i - installed->start);
1482                   rr = r;
1483                   if (!rr->p || rr->d < 0)      /* disabled -> look at feature rule */
1484                     rr -= solv->installed->end - solv->installed->start;
1485                   if (!rr->p)           /* identical to update rule? */
1486                     rr = r;
1487                   if (!rr->p)
1488                     continue;           /* orpaned package */
1489
1490                   /* XXX: noupdate check is probably no longer needed, as all jobs should
1491                    * already be satisfied */
1492                   /* Actually we currently still need it because of erase jobs */
1493                   /* if noupdate is set we do not look at update candidates */
1494                   queue_empty(&dq);
1495                   if (!MAPTST(&solv->noupdate, i - installed->start) && (solv->decisionmap[i] < 0 || solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, i - installed->start)) || rr->p != i))
1496                     {
1497                       if (solv->noobsoletes.size && solv->multiversionupdaters
1498                              && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1499                         {
1500                           /* special multiversion handling, make sure best version is chosen */
1501                           queue_push(&dq, i);
1502                           while ((p = pool->whatprovidesdata[d++]) != 0)
1503                             if (solv->decisionmap[p] >= 0)
1504                               queue_push(&dq, p);
1505                           policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1506                           p = dq.elements[0];
1507                           if (p != i && solv->decisionmap[p] == 0)
1508                             {
1509                               rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1510                               if (!rr->p)               /* update rule == feature rule? */
1511                                 rr = rr - solv->featurerules + solv->updaterules;
1512                               dq.count = 1;
1513                             }
1514                           else
1515                             dq.count = 0;
1516                         }
1517                       else
1518                         {
1519                           /* update to best package */
1520                           FOR_RULELITERALS(p, dp, rr)
1521                             {
1522                               if (solv->decisionmap[p] > 0)
1523                                 {
1524                                   dq.count = 0;         /* already fulfilled */
1525                                   break;
1526                                 }
1527                               if (!solv->decisionmap[p])
1528                                 queue_push(&dq, p);
1529                             }
1530                         }
1531                     }
1532                   /* install best version */
1533                   if (dq.count)
1534                     {
1535                       olevel = level;
1536                       level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1537                       if (level == 0)
1538                         {
1539                           queue_free(&dq);
1540                           queue_free(&dqs);
1541                           return;
1542                         }
1543                       if (level <= olevel)
1544                         {
1545                           if (level == 1 || level < passlevel)
1546                             break;      /* trouble */
1547                           if (level < olevel)
1548                             n = installed->start;       /* redo all */
1549                           i--;
1550                           n--;
1551                           continue;
1552                         }
1553                     }
1554                   /* if still undecided keep package */
1555                   if (solv->decisionmap[i] == 0)
1556                     {
1557                       olevel = level;
1558                       if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
1559                         {
1560                           POOL_DEBUG(SAT_DEBUG_POLICY, "cleandeps erasing %s\n", solvid2str(pool, i));
1561                           level = setpropagatelearn(solv, level, -i, disablerules, 0);
1562                         }
1563                       else
1564                         {
1565                           POOL_DEBUG(SAT_DEBUG_POLICY, "keeping %s\n", solvid2str(pool, i));
1566                           level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1567                         }
1568                       if (level == 0)
1569                         {
1570                           queue_free(&dq);
1571                           queue_free(&dqs);
1572                           return;
1573                         }
1574                       if (level <= olevel)
1575                         {
1576                           if (level == 1 || level < passlevel)
1577                             break;      /* trouble */
1578                           if (level < olevel)
1579                             n = installed->start;       /* redo all */
1580                           i--;
1581                           n--;
1582                           continue; /* retry with learnt rule */
1583                         }
1584                     }
1585                 }
1586               if (n < installed->end)
1587                 {
1588                   installedpos = i;     /* retry problem solvable next time */
1589                   break;                /* ran into trouble */
1590                 }
1591               installedpos = installed->start;  /* reset installedpos */
1592             }
1593           systemlevel = level + 1;
1594           if (pass < 2)
1595             continue;           /* had trouble, retry */
1596         }
1597
1598       if (level < systemlevel)
1599         systemlevel = level;
1600
1601       /*
1602        * decide
1603        */
1604       POOL_DEBUG(SAT_DEBUG_POLICY, "deciding unresolved rules\n");
1605       for (i = 1, n = 1; n < solv->nrules; i++, n++)
1606         {
1607           if (i == solv->nrules)
1608             i = 1;
1609           r = solv->rules + i;
1610           if (r->d < 0)         /* ignore disabled rules */
1611             continue;
1612           queue_empty(&dq);
1613           if (r->d == 0)
1614             {
1615               /* binary or unary rule */
1616               /* need two positive undecided literals */
1617               if (r->p < 0 || r->w2 <= 0)
1618                 continue;
1619               if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1620                 continue;
1621               queue_push(&dq, r->p);
1622               queue_push(&dq, r->w2);
1623             }
1624           else
1625             {
1626               /* make sure that
1627                * all negative literals are installed
1628                * no positive literal is installed
1629                * i.e. the rule is not fulfilled and we
1630                * just need to decide on the positive literals
1631                */
1632               if (r->p < 0)
1633                 {
1634                   if (solv->decisionmap[-r->p] <= 0)
1635                     continue;
1636                 }
1637               else
1638                 {
1639                   if (solv->decisionmap[r->p] > 0)
1640                     continue;
1641                   if (solv->decisionmap[r->p] == 0)
1642                     queue_push(&dq, r->p);
1643                 }
1644               dp = pool->whatprovidesdata + r->d;
1645               while ((p = *dp++) != 0)
1646                 {
1647                   if (p < 0)
1648                     {
1649                       if (solv->decisionmap[-p] <= 0)
1650                         break;
1651                     }
1652                   else
1653                     {
1654                       if (solv->decisionmap[p] > 0)
1655                         break;
1656                       if (solv->decisionmap[p] == 0)
1657                         queue_push(&dq, p);
1658                     }
1659                 }
1660               if (p)
1661                 continue;
1662             }
1663           IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1664             {
1665               POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
1666               solver_printruleclass(solv, SAT_DEBUG_PROPAGATE, r);
1667             }
1668           /* dq.count < 2 cannot happen as this means that
1669            * the rule is unit */
1670           assert(dq.count > 1);
1671
1672           olevel = level;
1673           level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
1674           if (level == 0)
1675             {
1676               queue_free(&dq);
1677               queue_free(&dqs);
1678               return;
1679             }
1680           if (level < systemlevel || level == 1)
1681             break;              /* trouble */
1682           /* something changed, so look at all rules again */
1683           n = 0;
1684         }
1685
1686       if (n != solv->nrules)    /* ran into trouble, restart */
1687         continue;
1688
1689       /* at this point we have a consistent system. now do the extras... */
1690
1691       if (doweak)
1692         {
1693           int qcount;
1694
1695           POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended packages\n");
1696           queue_empty(&dq);     /* recommended packages */
1697           queue_empty(&dqs);    /* supplemented packages */
1698           for (i = 1; i < pool->nsolvables; i++)
1699             {
1700               if (solv->decisionmap[i] < 0)
1701                 continue;
1702               if (solv->decisionmap[i] > 0)
1703                 {
1704                   /* installed, check for recommends */
1705                   Id *recp, rec, pp, p;
1706                   s = pool->solvables + i;
1707                   if (solv->ignorealreadyrecommended && s->repo == solv->installed)
1708                     continue;
1709                   /* XXX need to special case AND ? */
1710                   if (s->recommends)
1711                     {
1712                       recp = s->repo->idarraydata + s->recommends;
1713                       while ((rec = *recp++) != 0)
1714                         {
1715                           qcount = dq.count;
1716                           FOR_PROVIDES(p, pp, rec)
1717                             {
1718                               if (solv->decisionmap[p] > 0)
1719                                 {
1720                                   dq.count = qcount;
1721                                   break;
1722                                 }
1723                               else if (solv->decisionmap[p] == 0)
1724                                 {
1725                                   queue_pushunique(&dq, p);
1726                                 }
1727                             }
1728                         }
1729                     }
1730                 }
1731               else
1732                 {
1733                   s = pool->solvables + i;
1734                   if (!s->supplements)
1735                     continue;
1736                   if (!pool_installable(pool, s))
1737                     continue;
1738                   if (!solver_is_supplementing(solv, s))
1739                     continue;
1740                   queue_push(&dqs, i);
1741                 }
1742             }
1743
1744           /* filter out all packages obsoleted by installed packages */
1745           /* this is no longer needed if we have reverse obsoletes */
1746           if ((dqs.count || dq.count) && solv->installed)
1747             {
1748               Map obsmap;
1749               Id obs, *obsp, po, ppo;
1750
1751               map_init(&obsmap, pool->nsolvables);
1752               for (p = solv->installed->start; p < solv->installed->end; p++)
1753                 {
1754                   s = pool->solvables + p;
1755                   if (s->repo != solv->installed || !s->obsoletes)
1756                     continue;
1757                   if (solv->decisionmap[p] <= 0)
1758                     continue;
1759                   if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
1760                     continue;
1761                   obsp = s->repo->idarraydata + s->obsoletes;
1762                   /* foreach obsoletes */
1763                   while ((obs = *obsp++) != 0)
1764                     FOR_PROVIDES(po, ppo, obs)
1765                       MAPSET(&obsmap, po);
1766                 }
1767               for (i = j = 0; i < dqs.count; i++)
1768                 if (!MAPTST(&obsmap, dqs.elements[i]))
1769                   dqs.elements[j++] = dqs.elements[i];
1770               dqs.count = j;
1771               for (i = j = 0; i < dq.count; i++)
1772                 if (!MAPTST(&obsmap, dq.elements[i]))
1773                   dq.elements[j++] = dq.elements[i];
1774               dq.count = j;
1775               map_free(&obsmap);
1776             }
1777
1778           /* filter out all already supplemented packages if requested */
1779           if (solv->ignorealreadyrecommended && dqs.count)
1780             {
1781               /* turn off all new packages */
1782               for (i = 0; i < solv->decisionq.count; i++)
1783                 {
1784                   p = solv->decisionq.elements[i];
1785                   if (p < 0)
1786                     continue;
1787                   s = pool->solvables + p;
1788                   if (s->repo && s->repo != solv->installed)
1789                     solv->decisionmap[p] = -solv->decisionmap[p];
1790                 }
1791               /* filter out old supplements */
1792               for (i = j = 0; i < dqs.count; i++)
1793                 {
1794                   p = dqs.elements[i];
1795                   s = pool->solvables + p;
1796                   if (!s->supplements)
1797                     continue;
1798                   if (!solver_is_supplementing(solv, s))
1799                     dqs.elements[j++] = p;
1800                 }
1801               dqs.count = j;
1802               /* undo turning off */
1803               for (i = 0; i < solv->decisionq.count; i++)
1804                 {
1805                   p = solv->decisionq.elements[i];
1806                   if (p < 0)
1807                     continue;
1808                   s = pool->solvables + p;
1809                   if (s->repo && s->repo != solv->installed)
1810                     solv->decisionmap[p] = -solv->decisionmap[p];
1811                 }
1812             }
1813
1814           /* multiversion doesn't mix well with supplements.
1815            * filter supplemented packages where we already decided
1816            * to install a different version (see bnc#501088) */
1817           if (dqs.count && solv->noobsoletes.size)
1818             {
1819               for (i = j = 0; i < dqs.count; i++)
1820                 {
1821                   p = dqs.elements[i];
1822                   if (MAPTST(&solv->noobsoletes, p))
1823                     {
1824                       Id p2, pp2;
1825                       s = pool->solvables + p;
1826                       FOR_PROVIDES(p2, pp2, s->name)
1827                         if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
1828                           break;
1829                       if (p2)
1830                         continue;       /* ignore this package */
1831                     }
1832                   dqs.elements[j++] = p;
1833                 }
1834               dqs.count = j;
1835             }
1836
1837           /* make dq contain both recommended and supplemented pkgs */
1838           if (dqs.count)
1839             {
1840               for (i = 0; i < dqs.count; i++)
1841                 queue_pushunique(&dq, dqs.elements[i]);
1842             }
1843
1844           if (dq.count)
1845             {
1846               Map dqmap;
1847               int decisioncount = solv->decisionq.count;
1848
1849               if (dq.count == 1)
1850                 {
1851                   /* simple case, just one package. no need to choose  */
1852                   p = dq.elements[0];
1853                   if (dqs.count)
1854                     POOL_DEBUG(SAT_DEBUG_POLICY, "installing supplemented %s\n", solvid2str(pool, p));
1855                   else
1856                     POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended %s\n", solvid2str(pool, p));
1857                   queue_push(&solv->recommendations, p);
1858                   level = setpropagatelearn(solv, level, p, 0, 0);
1859                   continue;     /* back to main loop */
1860                 }
1861
1862               /* filter packages, this gives us the best versions */
1863               policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
1864
1865               /* create map of result */
1866               map_init(&dqmap, pool->nsolvables);
1867               for (i = 0; i < dq.count; i++)
1868                 MAPSET(&dqmap, dq.elements[i]);
1869
1870               /* install all supplemented packages */
1871               for (i = 0; i < dqs.count; i++)
1872                 {
1873                   p = dqs.elements[i];
1874                   if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
1875                     continue;
1876                   POOL_DEBUG(SAT_DEBUG_POLICY, "installing supplemented %s\n", solvid2str(pool, p));
1877                   queue_push(&solv->recommendations, p);
1878                   olevel = level;
1879                   level = setpropagatelearn(solv, level, p, 0, 0);
1880                   if (level <= olevel)
1881                     break;
1882                 }
1883               if (i < dqs.count || solv->decisionq.count < decisioncount)
1884                 {
1885                   map_free(&dqmap);
1886                   continue;
1887                 }
1888
1889               /* install all recommended packages */
1890               /* more work as we want to created branches if multiple
1891                * choices are valid */
1892               for (i = 0; i < decisioncount; i++)
1893                 {
1894                   Id rec, *recp, pp;
1895                   p = solv->decisionq.elements[i];
1896                   if (p < 0)
1897                     continue;
1898                   s = pool->solvables + p;
1899                   if (!s->repo || (solv->ignorealreadyrecommended && s->repo == solv->installed))
1900                     continue;
1901                   if (!s->recommends)
1902                     continue;
1903                   recp = s->repo->idarraydata + s->recommends;
1904                   while ((rec = *recp++) != 0)
1905                     {
1906                       queue_empty(&dq);
1907                       FOR_PROVIDES(p, pp, rec)
1908                         {
1909                           if (solv->decisionmap[p] > 0)
1910                             {
1911                               dq.count = 0;
1912                               break;
1913                             }
1914                           else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
1915                             queue_pushunique(&dq, p);
1916                         }
1917                       if (!dq.count)
1918                         continue;
1919                       if (dq.count > 1)
1920                         {
1921                           /* multiple candidates, open a branch */
1922                           for (i = 1; i < dq.count; i++)
1923                             queue_push(&solv->branches, dq.elements[i]);
1924                           queue_push(&solv->branches, -level);
1925                         }
1926                       p = dq.elements[0];
1927                       POOL_DEBUG(SAT_DEBUG_POLICY, "installing recommended %s\n", solvid2str(pool, p));
1928                       queue_push(&solv->recommendations, p);
1929                       olevel = level;
1930                       level = setpropagatelearn(solv, level, p, 0, 0);
1931                       if (level <= olevel || solv->decisionq.count < decisioncount)
1932                         break;  /* we had to revert some decisions */
1933                     }
1934                   if (rec)
1935                     break;      /* had a problem above, quit loop */
1936                 }
1937               map_free(&dqmap);
1938
1939               continue;         /* back to main loop so that all deps are checked */
1940             }
1941         }
1942
1943      if (solv->dupmap_all && solv->installed)
1944         {
1945           int installedone = 0;
1946
1947           /* let's see if we can install some unsupported package */
1948           POOL_DEBUG(SAT_DEBUG_SOLVER, "deciding orphaned packages\n");
1949           for (i = 0; i < solv->orphaned.count; i++)
1950             {
1951               p = solv->orphaned.elements[i];
1952               if (solv->decisionmap[p])
1953                 continue;       /* already decided */
1954               olevel = level;
1955               if (solv->droporphanedmap_all)
1956                 continue;
1957               if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
1958                 continue;
1959               POOL_DEBUG(SAT_DEBUG_SOLVER, "keeping orphaned %s\n", solvid2str(pool, p));
1960               level = setpropagatelearn(solv, level, p, 0, 0);
1961               installedone = 1;
1962               if (level < olevel)
1963                 break;
1964             }
1965           if (installedone || i < solv->orphaned.count)
1966             continue;           /* back to main loop */
1967           for (i = 0; i < solv->orphaned.count; i++)
1968             {
1969               p = solv->orphaned.elements[i];
1970               if (solv->decisionmap[p])
1971                 continue;       /* already decided */
1972               POOL_DEBUG(SAT_DEBUG_SOLVER, "removing orphaned %s\n", solvid2str(pool, p));
1973               olevel = level;
1974               level = setpropagatelearn(solv, level, -p, 0, 0);
1975               if (level < olevel)
1976                 break;
1977             }
1978           if (i < solv->orphaned.count)
1979             continue;           /* back to main loop */
1980         }
1981
1982      if (solv->solution_callback)
1983         {
1984           solv->solution_callback(solv, solv->solution_callback_data);
1985           if (solv->branches.count)
1986             {
1987               int i = solv->branches.count - 1;
1988               int l = -solv->branches.elements[i];
1989               Id why;
1990
1991               for (; i > 0; i--)
1992                 if (solv->branches.elements[i - 1] < 0)
1993                   break;
1994               p = solv->branches.elements[i];
1995               POOL_DEBUG(SAT_DEBUG_SOLVER, "branching with %s\n", solvid2str(pool, p));
1996               queue_empty(&dq);
1997               for (j = i + 1; j < solv->branches.count; j++)
1998                 queue_push(&dq, solv->branches.elements[j]);
1999               solv->branches.count = i;
2000               level = l;
2001               revert(solv, level);
2002               if (dq.count > 1)
2003                 for (j = 0; j < dq.count; j++)
2004                   queue_push(&solv->branches, dq.elements[j]);
2005               olevel = level;
2006               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2007               assert(why >= 0);
2008               level = setpropagatelearn(solv, level, p, disablerules, why);
2009               if (level == 0)
2010                 {
2011                   queue_free(&dq);
2012                   queue_free(&dqs);
2013                   return;
2014                 }
2015               continue;
2016             }
2017           /* all branches done, we're finally finished */
2018           break;
2019         }
2020
2021       /* minimization step */
2022      if (solv->branches.count)
2023         {
2024           int l = 0, lasti = -1, lastl = -1;
2025           Id why;
2026
2027           p = 0;
2028           for (i = solv->branches.count - 1; i >= 0; i--)
2029             {
2030               p = solv->branches.elements[i];
2031               if (p < 0)
2032                 l = -p;
2033               else if (p > 0 && solv->decisionmap[p] > l + 1)
2034                 {
2035                   lasti = i;
2036                   lastl = l;
2037                 }
2038             }
2039           if (lasti >= 0)
2040             {
2041               /* kill old solvable so that we do not loop */
2042               p = solv->branches.elements[lasti];
2043               solv->branches.elements[lasti] = 0;
2044               POOL_DEBUG(SAT_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, solvid2str(pool, p));
2045               minimizationsteps++;
2046
2047               level = lastl;
2048               revert(solv, level);
2049               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2050               assert(why >= 0);
2051               olevel = level;
2052               level = setpropagatelearn(solv, level, p, disablerules, why);
2053               if (level == 0)
2054                 {
2055                   queue_free(&dq);
2056                   queue_free(&dqs);
2057                   return;
2058                 }
2059               continue;         /* back to main loop */
2060             }
2061         }
2062       /* no minimization found, we're finally finished! */
2063       break;
2064     }
2065
2066   POOL_DEBUG(SAT_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2067
2068   POOL_DEBUG(SAT_DEBUG_STATS, "done solving.\n\n");
2069   queue_free(&dq);
2070   queue_free(&dqs);
2071 #if 0
2072   solver_printdecisionq(solv, SAT_DEBUG_RESULT);
2073 #endif
2074 }
2075
2076
2077 /*-------------------------------------------------------------------
2078  * 
2079  * remove disabled conflicts
2080  *
2081  * purpose: update the decisionmap after some rules were disabled.
2082  * this is used to calculate the suggested/recommended package list.
2083  * Also returns a "removed" list to undo the discisionmap changes.
2084  */
2085
2086 static void
2087 removedisabledconflicts(Solver *solv, Queue *removed)
2088 {
2089   Pool *pool = solv->pool;
2090   int i, n;
2091   Id p, why, *dp;
2092   Id new;
2093   Rule *r;
2094   Id *decisionmap = solv->decisionmap;
2095
2096   POOL_DEBUG(SAT_DEBUG_SCHUBI, "removedisabledconflicts\n");
2097   queue_empty(removed);
2098   for (i = 0; i < solv->decisionq.count; i++)
2099     {
2100       p = solv->decisionq.elements[i];
2101       if (p > 0)
2102         continue;       /* conflicts only, please */
2103       why = solv->decisionq_why.elements[i];
2104       if (why == 0)
2105         {
2106           /* no rule involved, must be a orphan package drop */
2107           continue;
2108         }
2109       /* we never do conflicts on free decisions, so there
2110        * must have been an unit rule */
2111       assert(why > 0);
2112       r = solv->rules + why;
2113       if (r->d < 0 && decisionmap[-p])
2114         {
2115           /* rule is now disabled, remove from decisionmap */
2116           POOL_DEBUG(SAT_DEBUG_SCHUBI, "removing conflict for package %s[%d]\n", solvid2str(pool, -p), -p);
2117           queue_push(removed, -p);
2118           queue_push(removed, decisionmap[-p]);
2119           decisionmap[-p] = 0;
2120         }
2121     }
2122   if (!removed->count)
2123     return;
2124   /* we removed some confliced packages. some of them might still
2125    * be in conflict, so search for unit rules and re-conflict */
2126   new = 0;
2127   for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2128     {
2129       if (i == solv->nrules)
2130         {
2131           i = 1;
2132           r = solv->rules + i;
2133         }
2134       if (r->d < 0)
2135         continue;
2136       if (!r->w2)
2137         {
2138           if (r->p < 0 && !decisionmap[-r->p])
2139             new = r->p;
2140         }
2141       else if (!r->d)
2142         {
2143           /* binary rule */
2144           if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2145             new = r->p;
2146           else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2147             new = r->w2;
2148         }
2149       else
2150         {
2151           if (r->p < 0 && decisionmap[-r->p] == 0)
2152             new = r->p;
2153           if (new || DECISIONMAP_FALSE(r->p))
2154             {
2155               dp = pool->whatprovidesdata + r->d;
2156               while ((p = *dp++) != 0)
2157                 {
2158                   if (new && p == new)
2159                     continue;
2160                   if (p < 0 && decisionmap[-p] == 0)
2161                     {
2162                       if (new)
2163                         {
2164                           new = 0;
2165                           break;
2166                         }
2167                       new = p;
2168                     }
2169                   else if (!DECISIONMAP_FALSE(p))
2170                     {
2171                       new = 0;
2172                       break;
2173                     }
2174                 }
2175             }
2176         }
2177       if (new)
2178         {
2179           POOL_DEBUG(SAT_DEBUG_SCHUBI, "re-conflicting package %s[%d]\n", solvid2str(pool, -new), -new);
2180           decisionmap[-new] = -1;
2181           new = 0;
2182           n = 0;        /* redo all rules */
2183         }
2184     }
2185 }
2186
2187 static inline void
2188 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2189 {
2190   int i;
2191   for (i = 0; i < removed->count; i += 2)
2192     solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2193 }
2194
2195
2196 /*-------------------------------------------------------------------
2197  *
2198  * weaken solvable dependencies
2199  */
2200
2201 static void
2202 weaken_solvable_deps(Solver *solv, Id p)
2203 {
2204   int i;
2205   Rule *r;
2206
2207   for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2208     {
2209       if (r->p != -p)
2210         continue;
2211       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2212         continue;       /* conflict */
2213       queue_push(&solv->weakruleq, i);
2214     }
2215 }
2216
2217
2218 /********************************************************************/
2219 /* main() */
2220
2221
2222 static void
2223 findrecommendedsuggested(Solver *solv)
2224 {
2225   Pool *pool = solv->pool;
2226   Queue redoq, disabledq;
2227   int goterase, i;
2228   Solvable *s;
2229   Rule *r;
2230   Map obsmap;
2231
2232   map_init(&obsmap, pool->nsolvables);
2233   if (solv->installed)
2234     {
2235       Id obs, *obsp, p, po, ppo;
2236       for (p = solv->installed->start; p < solv->installed->end; p++)
2237         {
2238           s = pool->solvables + p;
2239           if (s->repo != solv->installed || !s->obsoletes)
2240             continue;
2241           if (solv->decisionmap[p] <= 0)
2242             continue;
2243           if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
2244             continue;
2245           obsp = s->repo->idarraydata + s->obsoletes;
2246           /* foreach obsoletes */
2247           while ((obs = *obsp++) != 0)
2248             FOR_PROVIDES(po, ppo, obs)
2249               MAPSET(&obsmap, po);
2250         }
2251     }
2252
2253   queue_init(&redoq);
2254   queue_init(&disabledq);
2255   goterase = 0;
2256   /* disable all erase jobs (including weak "keep uninstalled" rules) */
2257   for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
2258     {
2259       if (r->d < 0)     /* disabled ? */
2260         continue;
2261       if (r->p >= 0)    /* install job? */
2262         continue;
2263       queue_push(&disabledq, i);
2264       solver_disablerule(solv, r);
2265       goterase++;
2266     }
2267   
2268   if (goterase)
2269     {
2270       enabledisablelearntrules(solv);
2271       removedisabledconflicts(solv, &redoq);
2272     }
2273
2274   /*
2275    * find recommended packages
2276    */
2277     
2278   /* if redoq.count == 0 we already found all recommended in the
2279    * solver run */
2280   if (redoq.count || solv->dontinstallrecommended || !solv->dontshowinstalledrecommended || solv->ignorealreadyrecommended)
2281     {
2282       Id rec, *recp, p, pp;
2283
2284       /* create map of all recommened packages */
2285       solv->recommends_index = -1;
2286       MAPZERO(&solv->recommendsmap);
2287       for (i = 0; i < solv->decisionq.count; i++)
2288         {
2289           p = solv->decisionq.elements[i];
2290           if (p < 0)
2291             continue;
2292           s = pool->solvables + p;
2293           if (s->recommends)
2294             {
2295               recp = s->repo->idarraydata + s->recommends;
2296               while ((rec = *recp++) != 0)
2297                 {
2298                   FOR_PROVIDES(p, pp, rec)
2299                     if (solv->decisionmap[p] > 0)
2300                       break;
2301                   if (p)
2302                     {
2303                       if (!solv->dontshowinstalledrecommended)
2304                         {
2305                           FOR_PROVIDES(p, pp, rec)
2306                             if (solv->decisionmap[p] > 0)
2307                               MAPSET(&solv->recommendsmap, p);
2308                         }
2309                       continue; /* p != 0: already fulfilled */
2310                     }
2311                   FOR_PROVIDES(p, pp, rec)
2312                     MAPSET(&solv->recommendsmap, p);
2313                 }
2314             }
2315         }
2316       for (i = 1; i < pool->nsolvables; i++)
2317         {
2318           if (solv->decisionmap[i] < 0)
2319             continue;
2320           if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2321             continue;
2322           if (MAPTST(&obsmap, i))
2323             continue;
2324           s = pool->solvables + i;
2325           if (!MAPTST(&solv->recommendsmap, i))
2326             {
2327               if (!s->supplements)
2328                 continue;
2329               if (!pool_installable(pool, s))
2330                 continue;
2331               if (!solver_is_supplementing(solv, s))
2332                 continue;
2333             }
2334           if (solv->dontinstallrecommended)
2335             queue_push(&solv->recommendations, i);
2336           else
2337             queue_pushunique(&solv->recommendations, i);
2338         }
2339       /* we use MODE_SUGGEST here so that repo prio is ignored */
2340       policy_filter_unwanted(solv, &solv->recommendations, POLICY_MODE_SUGGEST);
2341     }
2342
2343   /*
2344    * find suggested packages
2345    */
2346     
2347   if (1)
2348     {
2349       Id sug, *sugp, p, pp;
2350
2351       /* create map of all suggests that are still open */
2352       solv->recommends_index = -1;
2353       MAPZERO(&solv->suggestsmap);
2354       for (i = 0; i < solv->decisionq.count; i++)
2355         {
2356           p = solv->decisionq.elements[i];
2357           if (p < 0)
2358             continue;
2359           s = pool->solvables + p;
2360           if (s->suggests)
2361             {
2362               sugp = s->repo->idarraydata + s->suggests;
2363               while ((sug = *sugp++) != 0)
2364                 {
2365                   FOR_PROVIDES(p, pp, sug)
2366                     if (solv->decisionmap[p] > 0)
2367                       break;
2368                   if (p)
2369                     {
2370                       if (!solv->dontshowinstalledrecommended)
2371                         {
2372                           FOR_PROVIDES(p, pp, sug)
2373                             if (solv->decisionmap[p] > 0)
2374                               MAPSET(&solv->suggestsmap, p);
2375                         }
2376                       continue; /* already fulfilled */
2377                     }
2378                   FOR_PROVIDES(p, pp, sug)
2379                     MAPSET(&solv->suggestsmap, p);
2380                 }
2381             }
2382         }
2383       for (i = 1; i < pool->nsolvables; i++)
2384         {
2385           if (solv->decisionmap[i] < 0)
2386             continue;
2387           if (solv->decisionmap[i] > 0 && solv->dontshowinstalledrecommended)
2388             continue;
2389           if (MAPTST(&obsmap, i))
2390             continue;
2391           s = pool->solvables + i;
2392           if (!MAPTST(&solv->suggestsmap, i))
2393             {
2394               if (!s->enhances)
2395                 continue;
2396               if (!pool_installable(pool, s))
2397                 continue;
2398               if (!solver_is_enhancing(solv, s))
2399                 continue;
2400             }
2401           queue_push(&solv->suggestions, i);
2402         }
2403       policy_filter_unwanted(solv, &solv->suggestions, POLICY_MODE_SUGGEST);
2404     }
2405
2406   /* undo removedisabledconflicts */
2407   if (redoq.count)
2408     undo_removedisabledconflicts(solv, &redoq);
2409   queue_free(&redoq);
2410   
2411   /* undo job rule disabling */
2412   for (i = 0; i < disabledq.count; i++)
2413     solver_enablerule(solv, solv->rules + disabledq.elements[i]);
2414   queue_free(&disabledq);
2415   map_free(&obsmap);
2416 }
2417
2418 void
2419 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap)
2420 {
2421   int i;
2422   Id how, what, select;
2423   Id p, pp;
2424   for (i = 0; i < job->count; i += 2)
2425     {
2426       how = job->elements[i];
2427       if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
2428         continue;
2429       what = job->elements[i + 1];
2430       select = how & SOLVER_SELECTMASK;
2431       if (!noobsmap->size)
2432         map_grow(noobsmap, pool->nsolvables);
2433       FOR_JOB_SELECT(p, pp, select, what)
2434         MAPSET(noobsmap, p);
2435     }
2436 }
2437
2438 /*
2439  * add a rule created by a job, record job number and weak flag
2440  */
2441 static inline void
2442 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
2443 {
2444   solver_addrule(solv, p, d);
2445   queue_push(&solv->ruletojob, job);
2446   if (weak)
2447     queue_push(&solv->weakruleq, solv->nrules - 1);
2448 }
2449
2450 /*
2451  *
2452  * solve job queue
2453  *
2454  */
2455
2456 void
2457 solver_solve(Solver *solv, Queue *job)
2458 {
2459   Pool *pool = solv->pool;
2460   Repo *installed = solv->installed;
2461   int i;
2462   int oldnrules;
2463   Map addedmap;                /* '1' == have rpm-rules for solvable */
2464   Map installcandidatemap;
2465   Id how, what, select, name, weak, p, pp, d;
2466   Queue q;
2467   Solvable *s;
2468   Rule *r;
2469   int now, solve_start;
2470   int hasdupjob = 0;
2471
2472   solve_start = sat_timems(0);
2473
2474   /* log solver options */
2475   POOL_DEBUG(SAT_DEBUG_STATS, "solver started\n");
2476   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);
2477   POOL_DEBUG(SAT_DEBUG_STATS, "distupgrade=%d distupgrade_removeunsupported=%d\n", solv->distupgrade, solv->distupgrade_removeunsupported);
2478   POOL_DEBUG(SAT_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allowarchchange, solv->allowvendorchange);
2479   POOL_DEBUG(SAT_DEBUG_STATS, "promoteepoch=%d, novirtualconflicts=%d, allowselfconflicts=%d\n", pool->promoteepoch, pool->novirtualconflicts, pool->allowselfconflicts);
2480   POOL_DEBUG(SAT_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2481   POOL_DEBUG(SAT_DEBUG_STATS, "dontinstallrecommended=%d, ignorealreadyrecommended=%d, dontshowinstalledrecommended=%d\n", solv->dontinstallrecommended, solv->ignorealreadyrecommended, solv->dontshowinstalledrecommended);
2482
2483   /* create whatprovides if not already there */
2484   if (!pool->whatprovides)
2485     pool_createwhatprovides(pool);
2486
2487   /* create obsolete index */
2488   policy_create_obsolete_index(solv);
2489
2490   /* remember job */
2491   queue_free(&solv->job);
2492   queue_init_clone(&solv->job, job);
2493
2494   /* initialize with legacy values */
2495   solv->fixmap_all = solv->fixsystem;
2496   solv->updatemap_all = solv->updatesystem;
2497   solv->droporphanedmap_all = solv->distupgrade_removeunsupported;
2498   solv->dupmap_all = solv->distupgrade;
2499
2500   /*
2501    * create basic rule set of all involved packages
2502    * use addedmap bitmap to make sure we don't create rules twice
2503    */
2504
2505   /* create noobsolete map if needed */
2506   solver_calculate_noobsmap(pool, job, &solv->noobsoletes);
2507
2508   map_init(&addedmap, pool->nsolvables);
2509   MAPSET(&addedmap, SYSTEMSOLVABLE);
2510
2511   map_init(&installcandidatemap, pool->nsolvables);
2512   queue_init(&q);
2513
2514   now = sat_timems(0);
2515   /*
2516    * create rules for all package that could be involved with the solving
2517    * so called: rpm rules
2518    *
2519    */
2520   if (installed)
2521     {
2522       /* check for update/verify jobs as they need to be known early */
2523       for (i = 0; i < job->count; i += 2)
2524         {
2525           how = job->elements[i];
2526           what = job->elements[i + 1];
2527           select = how & SOLVER_SELECTMASK;
2528           switch (how & SOLVER_JOBMASK)
2529             {
2530             case SOLVER_VERIFY:
2531               if (select == SOLVER_SOLVABLE_ALL)
2532                 solv->fixmap_all = 1;
2533               FOR_JOB_SELECT(p, pp, select, what)
2534                 {
2535                   s = pool->solvables + p;
2536                   if (!solv->installed || s->repo != solv->installed)
2537                     continue;
2538                   if (!solv->fixmap.size)
2539                     map_grow(&solv->fixmap, solv->installed->end - solv->installed->start);
2540                   MAPSET(&solv->fixmap, p - solv->installed->start);
2541                 }
2542               break;
2543             case SOLVER_UPDATE:
2544               if (select == SOLVER_SOLVABLE_ALL)
2545                 solv->updatemap_all = 1;
2546               FOR_JOB_SELECT(p, pp, select, what)
2547                 {
2548                   s = pool->solvables + p;
2549                   if (!solv->installed || s->repo != solv->installed)
2550                     continue;
2551                   if (!solv->updatemap.size)
2552                     map_grow(&solv->updatemap, solv->installed->end - solv->installed->start);
2553                   MAPSET(&solv->updatemap, p - solv->installed->start);
2554                 }
2555               break;
2556             default:
2557               break;
2558             }
2559         }
2560
2561       oldnrules = solv->nrules;
2562       POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
2563       FOR_REPO_SOLVABLES(installed, p, s)
2564         solver_addrpmrulesforsolvable(solv, s, &addedmap);
2565       POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
2566       POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
2567       oldnrules = solv->nrules;
2568       FOR_REPO_SOLVABLES(installed, p, s)
2569         solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
2570       POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
2571     }
2572
2573   /*
2574    * create rules for all packages involved in the job
2575    * (to be installed or removed)
2576    */
2577     
2578   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
2579   oldnrules = solv->nrules;
2580   for (i = 0; i < job->count; i += 2)
2581     {
2582       how = job->elements[i];
2583       what = job->elements[i + 1];
2584       select = how & SOLVER_SELECTMASK;
2585
2586       switch (how & SOLVER_JOBMASK)
2587         {
2588         case SOLVER_INSTALL:
2589           FOR_JOB_SELECT(p, pp, select, what)
2590             {
2591               MAPSET(&installcandidatemap, p);
2592               solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
2593             }
2594           break;
2595         case SOLVER_DISTUPGRADE:
2596           if (select == SOLVER_SOLVABLE_ALL)
2597             {
2598               solv->dupmap_all = 1;
2599               solv->updatemap_all = 1;
2600             }
2601           if (!solv->dupmap_all)
2602             hasdupjob = 1;
2603           break;
2604         default:
2605           break;
2606         }
2607     }
2608   POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
2609
2610     
2611   /*
2612    * add rules for suggests, enhances
2613    */
2614   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for suggested/enhanced packages ***\n");
2615   oldnrules = solv->nrules;
2616   solver_addrpmrulesforweak(solv, &addedmap);
2617   POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
2618
2619   /*
2620    * first pass done, we now have all the rpm rules we need.
2621    * unify existing rules before going over all job rules and
2622    * policy rules.
2623    * at this point the system is always solvable,
2624    * as an empty system (remove all packages) is a valid solution
2625    */
2626
2627   IF_POOLDEBUG (SAT_DEBUG_STATS)
2628     {
2629       int possible = 0, installable = 0;
2630       for (i = 1; i < pool->nsolvables; i++)
2631         {
2632           if (pool_installable(pool, pool->solvables + i))
2633             installable++;
2634           if (MAPTST(&addedmap, i))
2635             possible++;
2636         }
2637       POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
2638     }
2639
2640   solver_unifyrules(solv);                          /* remove duplicate rpm rules */
2641   solv->rpmrules_end = solv->nrules;              /* mark end of rpm rules */
2642
2643   POOL_DEBUG(SAT_DEBUG_STATS, "rpm rule memory usage: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
2644   POOL_DEBUG(SAT_DEBUG_STATS, "rpm rule creation took %d ms\n", sat_timems(now));
2645
2646   /* create dup maps if needed. We need the maps early to create our
2647    * update rules */
2648   if (hasdupjob)
2649     solver_createdupmaps(solv);
2650
2651   /*
2652    * create feature rules
2653    * 
2654    * foreach installed:
2655    *   create assertion (keep installed, if no update available)
2656    *   or
2657    *   create update rule (A|update1(A)|update2(A)|...)
2658    * 
2659    * those are used later on to keep a version of the installed packages in
2660    * best effort mode
2661    */
2662     
2663   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add feature rules ***\n");
2664   solv->featurerules = solv->nrules;              /* mark start of feature rules */
2665   if (installed)
2666     {
2667       /* foreach possibly installed solvable */
2668       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2669         {
2670           if (s->repo != installed)
2671             {
2672               solver_addrule(solv, 0, 0);       /* create dummy rule */
2673               continue;
2674             }
2675           solver_addupdaterule(solv, s, 1);    /* allow s to be updated */
2676         }
2677       /* make sure we accounted for all rules */
2678       assert(solv->nrules - solv->featurerules == installed->end - installed->start);
2679     }
2680   solv->featurerules_end = solv->nrules;
2681
2682     /*
2683      * Add update rules for installed solvables
2684      * 
2685      * almost identical to feature rules
2686      * except that downgrades/archchanges/vendorchanges are not allowed
2687      */
2688     
2689   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add update rules ***\n");
2690   solv->updaterules = solv->nrules;
2691
2692   if (installed)
2693     { /* foreach installed solvables */
2694       /* we create all update rules, but disable some later on depending on the job */
2695       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2696         {
2697           Rule *sr;
2698
2699           if (s->repo != installed)
2700             {
2701               solver_addrule(solv, 0, 0);       /* create dummy rule */
2702               continue;
2703             }
2704           solver_addupdaterule(solv, s, 0);     /* allowall = 0: downgrades not allowed */
2705           /*
2706            * check for and remove duplicate
2707            */
2708           r = solv->rules + solv->nrules - 1;           /* r: update rule */
2709           sr = r - (installed->end - installed->start); /* sr: feature rule */
2710           /* it's orphaned if there is no feature rule or the feature rule
2711            * consists just of the installed package */
2712           if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
2713             queue_push(&solv->orphaned, i);
2714           if (!r->p)
2715             {
2716               assert(solv->dupmap_all && !sr->p);
2717               continue;
2718             }
2719           if (!solver_samerule(solv, r, sr))
2720             {
2721               /* identical rule, kill unneeded one */
2722               if (solv->allowuninstall)
2723                 {
2724                   /* keep feature rule, make it weak */
2725                   memset(r, 0, sizeof(*r));
2726                   queue_push(&solv->weakruleq, sr - solv->rules);
2727                 }
2728               else
2729                 {
2730                   /* keep update rule */
2731                   memset(sr, 0, sizeof(*sr));
2732                 }
2733             }
2734           else if (solv->allowuninstall)
2735             {
2736               /* make both feature and update rule weak */
2737               queue_push(&solv->weakruleq, r - solv->rules);
2738               queue_push(&solv->weakruleq, sr - solv->rules);
2739             }
2740           else
2741             solver_disablerule(solv, sr);
2742         }
2743       /* consistency check: we added a rule for _every_ installed solvable */
2744       assert(solv->nrules - solv->updaterules == installed->end - installed->start);
2745     }
2746   solv->updaterules_end = solv->nrules;
2747
2748
2749   /*
2750    * now add all job rules
2751    */
2752
2753   POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
2754
2755   solv->jobrules = solv->nrules;
2756   for (i = 0; i < job->count; i += 2)
2757     {
2758       oldnrules = solv->nrules;
2759
2760       how = job->elements[i];
2761       what = job->elements[i + 1];
2762       weak = how & SOLVER_WEAK;
2763       select = how & SOLVER_SELECTMASK;
2764       switch (how & SOLVER_JOBMASK)
2765         {
2766         case SOLVER_INSTALL:
2767           POOL_DEBUG(SAT_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2768           if (select == SOLVER_SOLVABLE)
2769             {
2770               p = what;
2771               d = 0;
2772             }
2773           else
2774             {
2775               queue_empty(&q);
2776               FOR_JOB_SELECT(p, pp, select, what)
2777                 queue_push(&q, p);
2778               if (!q.count)
2779                 {
2780                   /* no candidate found, make this an impossible rule */
2781                   queue_push(&q, -SYSTEMSOLVABLE);
2782                 }
2783               p = queue_shift(&q);      /* get first candidate */
2784               d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q);    /* internalize */
2785             }
2786           solver_addjobrule(solv, p, d, i, weak);
2787           break;
2788         case SOLVER_ERASE:
2789           POOL_DEBUG(SAT_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
2790           if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && solv->installed)
2791             map_grow(&solv->cleandepsmap, solv->installed->end - solv->installed->start);
2792           if (select == SOLVER_SOLVABLE && solv->installed && pool->solvables[what].repo == solv->installed)
2793             {
2794               /* special case for "erase a specific solvable": we also
2795                * erase all other solvables with that name, so that they
2796                * don't get picked up as replacement */
2797               /* XXX: look also at packages that obsolete this package? */
2798               name = pool->solvables[what].name;
2799               FOR_PROVIDES(p, pp, name)
2800                 {
2801                   if (p == what)
2802                     continue;
2803                   s = pool->solvables + p;
2804                   if (s->name != name)
2805                     continue;
2806                   /* keep other versions installed */
2807                   if (s->repo == solv->installed)
2808                     continue;
2809                   /* keep installcandidates of other jobs */
2810                   if (MAPTST(&installcandidatemap, p))
2811                     continue;
2812                   solver_addjobrule(solv, -p, 0, i, weak);      /* remove by id */
2813                 }
2814             }
2815           FOR_JOB_SELECT(p, pp, select, what)
2816             solver_addjobrule(solv, -p, 0, i, weak);
2817           break;
2818
2819         case SOLVER_UPDATE:
2820           POOL_DEBUG(SAT_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2821           break;
2822         case SOLVER_VERIFY:
2823           POOL_DEBUG(SAT_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2824           break;
2825         case SOLVER_WEAKENDEPS:
2826           POOL_DEBUG(SAT_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2827           if (select != SOLVER_SOLVABLE)
2828             break;
2829           s = pool->solvables + what;
2830           weaken_solvable_deps(solv, what);
2831           break;
2832         case SOLVER_NOOBSOLETES:
2833           POOL_DEBUG(SAT_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2834           break;
2835         case SOLVER_LOCK:
2836           POOL_DEBUG(SAT_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
2837           FOR_JOB_SELECT(p, pp, select, what)
2838             {
2839               s = pool->solvables + p;
2840               solver_addjobrule(solv, installed && s->repo == installed ? p : -p, 0, i, weak);
2841             }
2842           break;
2843         case SOLVER_DISTUPGRADE:
2844           POOL_DEBUG(SAT_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
2845           break;
2846         case SOLVER_DROP_ORPHANED:
2847           POOL_DEBUG(SAT_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
2848           if (select == SOLVER_SOLVABLE_ALL)
2849             solv->droporphanedmap_all = 1;
2850           FOR_JOB_SELECT(p, pp, select, what)
2851             {
2852               s = pool->solvables + p;
2853               if (!installed || s->repo != installed)
2854                 continue;
2855               if (!solv->droporphanedmap.size)
2856                 map_grow(&solv->droporphanedmap, installed->end - installed->start);
2857               MAPSET(&solv->droporphanedmap, p - installed->start);
2858             }
2859           break;
2860         case SOLVER_USERINSTALLED:
2861           POOL_DEBUG(SAT_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
2862           break;
2863         default:
2864           POOL_DEBUG(SAT_DEBUG_JOB, "job: unknown job\n");
2865           break;
2866         }
2867         
2868         /*
2869          * debug
2870          */
2871         
2872       IF_POOLDEBUG (SAT_DEBUG_JOB)
2873         {
2874           int j;
2875           if (solv->nrules == oldnrules)
2876             POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created\n");
2877           for (j = oldnrules; j < solv->nrules; j++)
2878             {
2879               POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
2880               solver_printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
2881             }
2882         }
2883     }
2884   assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
2885   solv->jobrules_end = solv->nrules;
2886
2887   /* now create infarch and dup rules */
2888   if (!solv->noinfarchcheck)
2889     {
2890       solver_addinfarchrules(solv, &addedmap);
2891       if (pool->obsoleteusescolors)
2892         {
2893           /* currently doesn't work well with infarch rules, so make
2894            * them weak */
2895           for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
2896             queue_push(&solv->weakruleq, i);
2897         }
2898     }
2899   else
2900     solv->infarchrules = solv->infarchrules_end = solv->nrules;
2901
2902   if (hasdupjob)
2903     {
2904       solver_addduprules(solv, &addedmap);
2905       solver_freedupmaps(solv); /* no longer needed */
2906     }
2907   else
2908     solv->duprules = solv->duprules_end = solv->nrules;
2909
2910   if (1)
2911     solver_addchoicerules(solv);
2912   else
2913     solv->choicerules = solv->choicerules_end = solv->nrules;
2914
2915   /* all rules created
2916    * --------------------------------------------------------------
2917    * prepare for solving
2918    */
2919     
2920   /* free unneeded memory */
2921   map_free(&addedmap);
2922   map_free(&installcandidatemap);
2923   queue_free(&q);
2924
2925   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);
2926
2927   /* create weak map */
2928   map_init(&solv->weakrulemap, solv->nrules);
2929   for (i = 0; i < solv->weakruleq.count; i++)
2930     {
2931       p = solv->weakruleq.elements[i];
2932       MAPSET(&solv->weakrulemap, p);
2933     }
2934
2935   /* all new rules are learnt after this point */
2936   solv->learntrules = solv->nrules;
2937
2938   /* create watches chains */
2939   makewatches(solv);
2940
2941   /* create assertion index. it is only used to speed up
2942    * makeruledecsions() a bit */
2943   for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
2944     if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
2945       queue_push(&solv->ruleassertions, i);
2946
2947   /* disable update rules that conflict with our job */
2948   solver_disablepolicyrules(solv);
2949
2950   /* make decisions based on job/update assertions */
2951   makeruledecisions(solv);
2952   POOL_DEBUG(SAT_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
2953
2954   /*
2955    * ********************************************
2956    * solve!
2957    * ********************************************
2958    */
2959     
2960   now = sat_timems(0);
2961   solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
2962   POOL_DEBUG(SAT_DEBUG_STATS, "solver took %d ms\n", sat_timems(now));
2963
2964   /*
2965    * calculate recommended/suggested packages
2966    */
2967   findrecommendedsuggested(solv);
2968
2969   /*
2970    * prepare solution queue if there were problems
2971    */
2972   solver_prepare_solutions(solv);
2973
2974   /*
2975    * finally prepare transaction info
2976    */
2977   transaction_calculate(&solv->trans, &solv->decisionq, &solv->noobsoletes);
2978
2979   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);
2980   POOL_DEBUG(SAT_DEBUG_STATS, "solver_solve took %d ms\n", sat_timems(solve_start));
2981 }
2982
2983 /***********************************************************************/
2984 /* disk usage computations */
2985
2986 /*-------------------------------------------------------------------
2987  * 
2988  * calculate DU changes
2989  */
2990
2991 void
2992 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
2993 {
2994   Map installedmap;
2995
2996   solver_create_state_maps(solv, &installedmap, 0);
2997   pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
2998   map_free(&installedmap);
2999 }
3000
3001
3002 /*-------------------------------------------------------------------
3003  * 
3004  * calculate changes in install size
3005  */
3006
3007 int
3008 solver_calc_installsizechange(Solver *solv)
3009 {
3010   Map installedmap;
3011   int change;
3012
3013   solver_create_state_maps(solv, &installedmap, 0);
3014   change = pool_calc_installsizechange(solv->pool, &installedmap);
3015   map_free(&installedmap);
3016   return change;
3017 }
3018
3019 void
3020 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
3021 {
3022   Map installedmap;
3023   solver_create_state_maps(solv, &installedmap, 0);
3024   pool_trivial_installable_noobsoletesmap(solv->pool, &installedmap, pkgs, res, solv->noobsoletes.size ? &solv->noobsoletes : 0);
3025   map_free(&installedmap);
3026 }
3027