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