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