bde00a3ca93b22e34d1f0f169d25b63a9782f349
[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   case SOLVER_FLAG_NO_AUTOTARGET:
1489     return solv->noautotarget;
1490   default:
1491     break;
1492   }
1493   return -1;
1494 }
1495
1496 int
1497 solver_set_flag(Solver *solv, int flag, int value)
1498 {
1499   int old = solver_get_flag(solv, flag);
1500   switch (flag)
1501   {
1502   case SOLVER_FLAG_ALLOW_DOWNGRADE:
1503     solv->allowdowngrade = value;
1504     break;
1505   case SOLVER_FLAG_ALLOW_NAMECHANGE:
1506     solv->allownamechange = value;
1507     break;
1508   case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1509     solv->allowarchchange = value;
1510     break;
1511   case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1512     solv->allowvendorchange = value;
1513     break;
1514   case SOLVER_FLAG_ALLOW_UNINSTALL:
1515     solv->allowuninstall = value;
1516     break;
1517   case SOLVER_FLAG_NO_UPDATEPROVIDE:
1518     solv->noupdateprovide = value;
1519     break;
1520   case SOLVER_FLAG_SPLITPROVIDES:
1521     solv->dosplitprovides = value;
1522     break;
1523   case SOLVER_FLAG_IGNORE_RECOMMENDED:
1524     solv->dontinstallrecommended = value;
1525     break;
1526   case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1527     solv->addalreadyrecommended = value;
1528     break;
1529   case SOLVER_FLAG_NO_INFARCHCHECK:
1530     solv->noinfarchcheck = value;
1531     break;
1532   case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1533     solv->keepexplicitobsoletes = value;
1534     break;
1535   case SOLVER_FLAG_BEST_OBEY_POLICY:
1536     solv->bestobeypolicy = value;
1537     break;
1538   case SOLVER_FLAG_NO_AUTOTARGET:
1539     solv->noautotarget = value;
1540     break;
1541   default:
1542     break;
1543   }
1544   return old;
1545 }
1546
1547 int
1548 cleandeps_check_mistakes(Solver *solv, int level)
1549 {
1550   Pool *pool = solv->pool;
1551   Rule *r;
1552   Id p, pp;
1553   int i;
1554   int mademistake = 0;
1555
1556   if (!solv->cleandepsmap.size)
1557     return 0;
1558   /* check for mistakes */
1559   for (i = solv->installed->start; i < solv->installed->end; i++)
1560     {
1561       if (!MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1562         continue;
1563       r = solv->rules + solv->featurerules + (i - solv->installed->start);
1564       /* a mistake is when the featurerule is true but the updaterule is false */
1565       if (!r->p)
1566         continue;
1567       FOR_RULELITERALS(p, pp, r)
1568         if (p > 0 && solv->decisionmap[p] > 0)
1569           break;
1570       if (!p)
1571         continue;       /* feature rule is not true */
1572       r = solv->rules + solv->updaterules + (i - solv->installed->start);
1573       if (!r->p)
1574         continue;
1575       FOR_RULELITERALS(p, pp, r)
1576         if (p > 0 && solv->decisionmap[p] > 0)
1577           break;
1578       if (p)
1579         continue;       /* update rule is true */
1580       POOL_DEBUG(SOLV_DEBUG_SOLVER, "cleandeps mistake: ");
1581       solver_printruleclass(solv, SOLV_DEBUG_SOLVER, r);
1582       POOL_DEBUG(SOLV_DEBUG_SOLVER, "feature rule: ");
1583       solver_printruleclass(solv, SOLV_DEBUG_SOLVER, solv->rules + solv->featurerules + (i - solv->installed->start));
1584       if (!solv->cleandeps_mistakes)
1585         {
1586           solv->cleandeps_mistakes = solv_calloc(1, sizeof(Queue));
1587           queue_init(solv->cleandeps_mistakes);
1588         }
1589       queue_push(solv->cleandeps_mistakes, i);
1590       MAPCLR(&solv->cleandepsmap, i - solv->installed->start);
1591       solver_reenablepolicyrules_cleandeps(solv, i);
1592       mademistake = 1;
1593     }
1594   if (mademistake)
1595     solver_reset(solv);
1596   return mademistake;
1597 }
1598
1599 static void
1600 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
1601 {
1602   int i, j;
1603   Id p, *cp2;
1604   for (i = j = 0; i < q->count; i++)
1605     {
1606       p = q->elements[i];
1607       for (cp2 = cp; *cp2; cp2++)
1608         if (*cp2 == p)
1609           {
1610             q->elements[j++] = p;
1611             break;
1612           }
1613     }
1614   queue_truncate(q, j);
1615 }
1616
1617 /*-------------------------------------------------------------------
1618  * 
1619  * solver_run_sat
1620  *
1621  * all rules have been set up, now actually run the solver
1622  *
1623  */
1624
1625 void
1626 solver_run_sat(Solver *solv, int disablerules, int doweak)
1627 {
1628   Queue dq;             /* local decisionqueue */
1629   Queue dqs;            /* local decisionqueue for supplements */
1630   int systemlevel;
1631   int level, olevel;
1632   Rule *r;
1633   int i, j, n;
1634   Solvable *s;
1635   Pool *pool = solv->pool;
1636   Id p, pp, *dp;
1637   int minimizationsteps;
1638   int installedpos = solv->installed ? solv->installed->start : 0;
1639
1640   IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
1641     {
1642       POOL_DEBUG (SOLV_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1643       for (i = 1; i < solv->nrules; i++)
1644         solver_printruleclass(solv, SOLV_DEBUG_RULE_CREATION, solv->rules + i);
1645     }
1646
1647   POOL_DEBUG(SOLV_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1648
1649   /* start SAT algorithm */
1650   level = 1;
1651   systemlevel = level + 1;
1652   POOL_DEBUG(SOLV_DEBUG_SOLVER, "solving...\n");
1653
1654   queue_init(&dq);
1655   queue_init(&dqs);
1656
1657   /*
1658    * here's the main loop:
1659    * 1) propagate new decisions (only needed once)
1660    * 2) fulfill jobs
1661    * 3) try to keep installed packages
1662    * 4) fulfill all unresolved rules
1663    * 5) install recommended packages
1664    * 6) minimalize solution if we had choices
1665    * if we encounter a problem, we rewind to a safe level and restart
1666    * with step 1
1667    */
1668    
1669   minimizationsteps = 0;
1670   for (;;)
1671     {
1672       /*
1673        * initial propagation of the assertions
1674        */
1675       if (level == 1)
1676         {
1677           POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagating (propagate_index: %d;  size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1678           if ((r = propagate(solv, level)) != 0)
1679             {
1680               if (analyze_unsolvable(solv, r, disablerules))
1681                 continue;
1682               level = 0;
1683               break;    /* unsolvable */
1684             }
1685         }
1686
1687       /*
1688        * resolve jobs first
1689        */
1690      if (level < systemlevel)
1691         {
1692           POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving job rules\n");
1693           for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1694             {
1695               Id l;
1696               if (r->d < 0)             /* ignore disabled rules */
1697                 continue;
1698               queue_empty(&dq);
1699               FOR_RULELITERALS(l, pp, r)
1700                 {
1701                   if (l < 0)
1702                     {
1703                       if (solv->decisionmap[-l] <= 0)
1704                         break;
1705                     }
1706                   else
1707                     {
1708                       if (solv->decisionmap[l] > 0)
1709                         break;
1710                       if (solv->decisionmap[l] == 0)
1711                         queue_push(&dq, l);
1712                     }
1713                 }
1714               if (l || !dq.count)
1715                 continue;
1716               /* prune to installed if not updating */
1717               if (dq.count > 1 && solv->installed && !solv->updatemap_all &&
1718                   !(solv->job.elements[solv->ruletojob.elements[i - solv->jobrules]] & SOLVER_ORUPDATE))
1719                 {
1720                   int j, k;
1721                   for (j = k = 0; j < dq.count; j++)
1722                     {
1723                       Solvable *s = pool->solvables + dq.elements[j];
1724                       if (s->repo == solv->installed)
1725                         {
1726                           dq.elements[k++] = dq.elements[j];
1727                           if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
1728                             {
1729                               k = 0;    /* package wants to be updated, do not prune */
1730                               break;
1731                             }
1732                         }
1733                     }
1734                   if (k)
1735                     dq.count = k;
1736                 }
1737               olevel = level;
1738               level = selectandinstall(solv, level, &dq, disablerules, i);
1739               if (level == 0)
1740                 break;
1741               if (level <= olevel)
1742                 break;
1743             }
1744           if (level == 0)
1745             break;      /* unsolvable */
1746           systemlevel = level + 1;
1747           if (i < solv->jobrules_end)
1748             continue;
1749           solv->decisioncnt_update = solv->decisionq.count;
1750           solv->decisioncnt_keep = solv->decisionq.count;
1751         }
1752
1753       /*
1754        * installed packages
1755        */
1756       if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1757         {
1758           Repo *installed = solv->installed;
1759           int pass;
1760
1761           POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving installed packages\n");
1762           /* we use two passes if we need to update packages 
1763            * to create a better user experience */
1764           for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1765             {
1766               int passlevel = level;
1767               if (pass == 1)
1768                 solv->decisioncnt_keep = solv->decisionq.count;
1769               /* start with installedpos, the position that gave us problems last time */
1770               for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1771                 {
1772                   Rule *rr;
1773                   Id d;
1774
1775                   if (i == installed->end)
1776                     i = installed->start;
1777                   s = pool->solvables + i;
1778                   if (s->repo != installed)
1779                     continue;
1780
1781                   if (solv->decisionmap[i] > 0)
1782                     continue;
1783                   if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1784                     continue;           /* updates first */
1785                   r = solv->rules + solv->updaterules + (i - installed->start);
1786                   rr = r;
1787                   if (!rr->p || rr->d < 0)      /* disabled -> look at feature rule */
1788                     rr -= solv->installed->end - solv->installed->start;
1789                   if (!rr->p)           /* identical to update rule? */
1790                     rr = r;
1791                   if (!rr->p)
1792                     continue;           /* orpaned package */
1793
1794                   /* XXX: noupdate check is probably no longer needed, as all jobs should
1795                    * already be satisfied */
1796                   /* Actually we currently still need it because of erase jobs */
1797                   /* if noupdate is set we do not look at update candidates */
1798                   queue_empty(&dq);
1799                   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))
1800                     {
1801                       if (solv->noobsoletes.size && solv->multiversionupdaters
1802                              && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1803                         {
1804                           /* special multiversion handling, make sure best version is chosen */
1805                           queue_push(&dq, i);
1806                           while ((p = pool->whatprovidesdata[d++]) != 0)
1807                             if (solv->decisionmap[p] >= 0)
1808                               queue_push(&dq, p);
1809                           if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1810                             prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
1811                           if (dq.count)
1812                             {
1813                               policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1814                               p = dq.elements[0];
1815                               if (p != i && solv->decisionmap[p] == 0)
1816                                 {
1817                                   rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1818                                   if (!rr->p)           /* update rule == feature rule? */
1819                                     rr = rr - solv->featurerules + solv->updaterules;
1820                                   dq.count = 1;
1821                                 }
1822                               else
1823                                 dq.count = 0;
1824                             }
1825                         }
1826                       else
1827                         {
1828                           /* update to best package */
1829                           FOR_RULELITERALS(p, pp, rr)
1830                             {
1831                               if (solv->decisionmap[p] > 0)
1832                                 {
1833                                   dq.count = 0;         /* already fulfilled */
1834                                   break;
1835                                 }
1836                               if (!solv->decisionmap[p])
1837                                 queue_push(&dq, p);
1838                             }
1839                         }
1840                     }
1841                   if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1842                     prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
1843                   /* install best version */
1844                   if (dq.count)
1845                     {
1846                       olevel = level;
1847                       level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1848                       if (level == 0)
1849                         {
1850                           queue_free(&dq);
1851                           queue_free(&dqs);
1852                           return;
1853                         }
1854                       if (level <= olevel)
1855                         {
1856                           if (level == 1 || level < passlevel)
1857                             break;      /* trouble */
1858                           if (level < olevel)
1859                             n = installed->start;       /* redo all */
1860                           i--;
1861                           n--;
1862                           continue;
1863                         }
1864                     }
1865                   /* if still undecided keep package */
1866                   if (solv->decisionmap[i] == 0)
1867                     {
1868                       olevel = level;
1869                       if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
1870                         {
1871                           POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
1872                           level = setpropagatelearn(solv, level, -i, disablerules, 0);
1873                         }
1874                       else
1875                         {
1876                           POOL_DEBUG(SOLV_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
1877                           level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1878                         }
1879                       if (level == 0)
1880                         break;
1881                       if (level <= olevel)
1882                         {
1883                           if (level == 1 || level < passlevel)
1884                             break;      /* trouble */
1885                           if (level < olevel)
1886                             n = installed->start;       /* redo all */
1887                           i--;
1888                           n--;
1889                           continue;     /* retry with learnt rule */
1890                         }
1891                     }
1892                 }
1893               if (n < installed->end)
1894                 {
1895                   installedpos = i;     /* retry problem solvable next time */
1896                   break;                /* ran into trouble */
1897                 }
1898               installedpos = installed->start;  /* reset installedpos */
1899             }
1900           if (level == 0)
1901             break;              /* unsolvable */
1902           systemlevel = level + 1;
1903           if (pass < 2)
1904             continue;           /* had trouble, retry */
1905         }
1906
1907       if (level < systemlevel)
1908         systemlevel = level;
1909
1910       /*
1911        * decide
1912        */
1913       solv->decisioncnt_resolve = solv->decisionq.count;
1914       POOL_DEBUG(SOLV_DEBUG_POLICY, "deciding unresolved rules\n");
1915       for (i = 1, n = 1; n < solv->nrules; i++, n++)
1916         {
1917           if (i == solv->nrules)
1918             i = 1;
1919           r = solv->rules + i;
1920           if (r->d < 0)         /* ignore disabled rules */
1921             continue;
1922           queue_empty(&dq);
1923           if (r->d == 0)
1924             {
1925               /* binary or unary rule */
1926               /* need two positive undecided literals */
1927               if (r->p < 0 || r->w2 <= 0)
1928                 continue;
1929               if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1930                 continue;
1931               queue_push(&dq, r->p);
1932               queue_push(&dq, r->w2);
1933             }
1934           else
1935             {
1936               /* make sure that
1937                * all negative literals are installed
1938                * no positive literal is installed
1939                * i.e. the rule is not fulfilled and we
1940                * just need to decide on the positive literals
1941                */
1942               if (r->p < 0)
1943                 {
1944                   if (solv->decisionmap[-r->p] <= 0)
1945                     continue;
1946                 }
1947               else
1948                 {
1949                   if (solv->decisionmap[r->p] > 0)
1950                     continue;
1951                   if (solv->decisionmap[r->p] == 0)
1952                     queue_push(&dq, r->p);
1953                 }
1954               dp = pool->whatprovidesdata + r->d;
1955               while ((p = *dp++) != 0)
1956                 {
1957                   if (p < 0)
1958                     {
1959                       if (solv->decisionmap[-p] <= 0)
1960                         break;
1961                     }
1962                   else
1963                     {
1964                       if (solv->decisionmap[p] > 0)
1965                         break;
1966                       if (solv->decisionmap[p] == 0)
1967                         queue_push(&dq, p);
1968                     }
1969                 }
1970               if (p)
1971                 continue;
1972             }
1973           IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
1974             {
1975               POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "unfulfilled ");
1976               solver_printruleclass(solv, SOLV_DEBUG_PROPAGATE, r);
1977             }
1978           /* dq.count < 2 cannot happen as this means that
1979            * the rule is unit */
1980           assert(dq.count > 1);
1981
1982           olevel = level;
1983           level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
1984           if (level == 0)
1985             break;              /* unsolvable */
1986           if (level < systemlevel || level == 1)
1987             break;              /* trouble */
1988           /* something changed, so look at all rules again */
1989           n = 0;
1990         }
1991
1992       if (n != solv->nrules)    /* ran into trouble? */
1993         {
1994           if (level == 0)
1995             break;              /* unsolvable */
1996           continue;             /* start over */
1997         }
1998
1999       /* at this point we have a consistent system. now do the extras... */
2000
2001       solv->decisioncnt_weak = solv->decisionq.count;
2002       if (doweak)
2003         {
2004           int qcount;
2005
2006           POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended packages\n");
2007           queue_empty(&dq);     /* recommended packages */
2008           queue_empty(&dqs);    /* supplemented packages */
2009           for (i = 1; i < pool->nsolvables; i++)
2010             {
2011               if (solv->decisionmap[i] < 0)
2012                 continue;
2013               if (solv->decisionmap[i] > 0)
2014                 {
2015                   /* installed, check for recommends */
2016                   Id *recp, rec, pp, p;
2017                   s = pool->solvables + i;
2018                   if (!solv->addalreadyrecommended && s->repo == solv->installed)
2019                     continue;
2020                   /* XXX need to special case AND ? */
2021                   if (s->recommends)
2022                     {
2023                       recp = s->repo->idarraydata + s->recommends;
2024                       while ((rec = *recp++) != 0)
2025                         {
2026                           qcount = dq.count;
2027                           FOR_PROVIDES(p, pp, rec)
2028                             {
2029                               if (solv->decisionmap[p] > 0)
2030                                 {
2031                                   dq.count = qcount;
2032                                   break;
2033                                 }
2034                               else if (solv->decisionmap[p] == 0)
2035                                 {
2036                                   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))))
2037                                     continue;
2038                                   queue_pushunique(&dq, p);
2039                                 }
2040                             }
2041                         }
2042                     }
2043                 }
2044               else
2045                 {
2046                   s = pool->solvables + i;
2047                   if (!s->supplements)
2048                     continue;
2049                   if (!pool_installable(pool, s))
2050                     continue;
2051                   if (!solver_is_supplementing(solv, s))
2052                     continue;
2053                   if (solv->dupmap_all && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
2054                     continue;
2055                   queue_push(&dqs, i);
2056                 }
2057             }
2058
2059           /* filter out all packages obsoleted by installed packages */
2060           /* this is no longer needed if we have reverse obsoletes */
2061           if ((dqs.count || dq.count) && solv->installed)
2062             {
2063               Map obsmap;
2064               Id obs, *obsp, po, ppo;
2065
2066               map_init(&obsmap, pool->nsolvables);
2067               for (p = solv->installed->start; p < solv->installed->end; p++)
2068                 {
2069                   s = pool->solvables + p;
2070                   if (s->repo != solv->installed || !s->obsoletes)
2071                     continue;
2072                   if (solv->decisionmap[p] <= 0)
2073                     continue;
2074                   if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
2075                     continue;
2076                   obsp = s->repo->idarraydata + s->obsoletes;
2077                   /* foreach obsoletes */
2078                   while ((obs = *obsp++) != 0)
2079                     FOR_PROVIDES(po, ppo, obs)
2080                       MAPSET(&obsmap, po);
2081                 }
2082               for (i = j = 0; i < dqs.count; i++)
2083                 if (!MAPTST(&obsmap, dqs.elements[i]))
2084                   dqs.elements[j++] = dqs.elements[i];
2085               dqs.count = j;
2086               for (i = j = 0; i < dq.count; i++)
2087                 if (!MAPTST(&obsmap, dq.elements[i]))
2088                   dq.elements[j++] = dq.elements[i];
2089               dq.count = j;
2090               map_free(&obsmap);
2091             }
2092
2093           /* filter out all already supplemented packages if requested */
2094           if (!solv->addalreadyrecommended && dqs.count)
2095             {
2096               /* turn off all new packages */
2097               for (i = 0; i < solv->decisionq.count; i++)
2098                 {
2099                   p = solv->decisionq.elements[i];
2100                   if (p < 0)
2101                     continue;
2102                   s = pool->solvables + p;
2103                   if (s->repo && s->repo != solv->installed)
2104                     solv->decisionmap[p] = -solv->decisionmap[p];
2105                 }
2106               /* filter out old supplements */
2107               for (i = j = 0; i < dqs.count; i++)
2108                 {
2109                   p = dqs.elements[i];
2110                   s = pool->solvables + p;
2111                   if (!s->supplements)
2112                     continue;
2113                   if (!solver_is_supplementing(solv, s))
2114                     dqs.elements[j++] = p;
2115                 }
2116               dqs.count = j;
2117               /* undo turning off */
2118               for (i = 0; i < solv->decisionq.count; i++)
2119                 {
2120                   p = solv->decisionq.elements[i];
2121                   if (p < 0)
2122                     continue;
2123                   s = pool->solvables + p;
2124                   if (s->repo && s->repo != solv->installed)
2125                     solv->decisionmap[p] = -solv->decisionmap[p];
2126                 }
2127             }
2128
2129           /* multiversion doesn't mix well with supplements.
2130            * filter supplemented packages where we already decided
2131            * to install a different version (see bnc#501088) */
2132           if (dqs.count && solv->noobsoletes.size)
2133             {
2134               for (i = j = 0; i < dqs.count; i++)
2135                 {
2136                   p = dqs.elements[i];
2137                   if (MAPTST(&solv->noobsoletes, p))
2138                     {
2139                       Id p2, pp2;
2140                       s = pool->solvables + p;
2141                       FOR_PROVIDES(p2, pp2, s->name)
2142                         if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
2143                           break;
2144                       if (p2)
2145                         continue;       /* ignore this package */
2146                     }
2147                   dqs.elements[j++] = p;
2148                 }
2149               dqs.count = j;
2150             }
2151
2152           /* make dq contain both recommended and supplemented pkgs */
2153           if (dqs.count)
2154             {
2155               for (i = 0; i < dqs.count; i++)
2156                 queue_pushunique(&dq, dqs.elements[i]);
2157             }
2158
2159           if (dq.count)
2160             {
2161               Map dqmap;
2162               int decisioncount = solv->decisionq.count;
2163
2164               if (dq.count == 1)
2165                 {
2166                   /* simple case, just one package. no need to choose to best version */
2167                   p = dq.elements[0];
2168                   if (dqs.count)
2169                     POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2170                   else
2171                     POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2172                   level = setpropagatelearn(solv, level, p, 0, 0);
2173                   if (level == 0)
2174                     break;
2175                   continue;     /* back to main loop */
2176                 }
2177
2178               /* filter packages, this gives us the best versions */
2179               policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
2180
2181               /* create map of result */
2182               map_init(&dqmap, pool->nsolvables);
2183               for (i = 0; i < dq.count; i++)
2184                 MAPSET(&dqmap, dq.elements[i]);
2185
2186               /* install all supplemented packages */
2187               for (i = 0; i < dqs.count; i++)
2188                 {
2189                   p = dqs.elements[i];
2190                   if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
2191                     continue;
2192                   POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2193                   olevel = level;
2194                   level = setpropagatelearn(solv, level, p, 0, 0);
2195                   if (level <= olevel)
2196                     break;
2197                 }
2198               if (i < dqs.count || solv->decisionq.count < decisioncount)
2199                 {
2200                   map_free(&dqmap);
2201                   if (level == 0)
2202                     break;
2203                   continue;
2204                 }
2205
2206               /* install all recommended packages */
2207               /* more work as we want to created branches if multiple
2208                * choices are valid */
2209               for (i = 0; i < decisioncount; i++)
2210                 {
2211                   Id rec, *recp, pp;
2212                   p = solv->decisionq.elements[i];
2213                   if (p < 0)
2214                     continue;
2215                   s = pool->solvables + p;
2216                   if (!s->repo || (!solv->addalreadyrecommended && s->repo == solv->installed))
2217                     continue;
2218                   if (!s->recommends)
2219                     continue;
2220                   recp = s->repo->idarraydata + s->recommends;
2221                   while ((rec = *recp++) != 0)
2222                     {
2223                       queue_empty(&dq);
2224                       FOR_PROVIDES(p, pp, rec)
2225                         {
2226                           if (solv->decisionmap[p] > 0)
2227                             {
2228                               dq.count = 0;
2229                               break;
2230                             }
2231                           else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
2232                             queue_pushunique(&dq, p);
2233                         }
2234                       if (!dq.count)
2235                         continue;
2236                       if (dq.count > 1)
2237                         {
2238                           /* multiple candidates, open a branch */
2239                           for (i = 1; i < dq.count; i++)
2240                             queue_push(&solv->branches, dq.elements[i]);
2241                           queue_push(&solv->branches, -level);
2242                         }
2243                       p = dq.elements[0];
2244                       POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2245                       olevel = level;
2246                       level = setpropagatelearn(solv, level, p, 0, 0);
2247                       if (level <= olevel || solv->decisionq.count < decisioncount)
2248                         break;  /* we had to revert some decisions */
2249                     }
2250                   if (rec)
2251                     break;      /* had a problem above, quit loop */
2252                 }
2253               map_free(&dqmap);
2254               if (level == 0)
2255                 break;
2256               continue;         /* back to main loop so that all deps are checked */
2257             }
2258         }
2259
2260       solv->decisioncnt_orphan = solv->decisionq.count;
2261       if (solv->dupmap_all && solv->installed)
2262         {
2263           int installedone = 0;
2264
2265           /* let's see if we can install some unsupported package */
2266           POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding orphaned packages\n");
2267           for (i = 0; i < solv->orphaned.count; i++)
2268             {
2269               p = solv->orphaned.elements[i];
2270               if (solv->decisionmap[p])
2271                 continue;       /* already decided */
2272               if (solv->droporphanedmap_all)
2273                 continue;
2274               if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
2275                 continue;
2276               POOL_DEBUG(SOLV_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
2277               olevel = level;
2278               level = setpropagatelearn(solv, level, p, 0, 0);
2279               installedone = 1;
2280               if (level < olevel)
2281                 break;
2282             }
2283           if (installedone || i < solv->orphaned.count)
2284             {
2285               if (level == 0)
2286                 break;
2287               continue;         /* back to main loop */
2288             }
2289           for (i = 0; i < solv->orphaned.count; i++)
2290             {
2291               p = solv->orphaned.elements[i];
2292               if (solv->decisionmap[p])
2293                 continue;       /* already decided */
2294               POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2295               olevel = level;
2296               level = setpropagatelearn(solv, level, -p, 0, 0);
2297               if (level < olevel)
2298                 break;
2299             }
2300           if (i < solv->orphaned.count)
2301             {
2302               if (level == 0)
2303                 break;
2304               continue;         /* back to main loop */
2305             }
2306         }
2307
2308      if (solv->installed && solv->cleandepsmap.size)
2309         {
2310           if (cleandeps_check_mistakes(solv, level))
2311             {
2312               level = 1;        /* restart from scratch */
2313               continue;
2314             }
2315         }
2316
2317      if (solv->solution_callback)
2318         {
2319           solv->solution_callback(solv, solv->solution_callback_data);
2320           if (solv->branches.count)
2321             {
2322               int i = solv->branches.count - 1;
2323               int l = -solv->branches.elements[i];
2324               Id why;
2325
2326               for (; i > 0; i--)
2327                 if (solv->branches.elements[i - 1] < 0)
2328                   break;
2329               p = solv->branches.elements[i];
2330               POOL_DEBUG(SOLV_DEBUG_SOLVER, "branching with %s\n", pool_solvid2str(pool, p));
2331               queue_empty(&dq);
2332               for (j = i + 1; j < solv->branches.count; j++)
2333                 queue_push(&dq, solv->branches.elements[j]);
2334               solv->branches.count = i;
2335               level = l;
2336               revert(solv, level);
2337               if (dq.count > 1)
2338                 for (j = 0; j < dq.count; j++)
2339                   queue_push(&solv->branches, dq.elements[j]);
2340               olevel = level;
2341               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2342               assert(why >= 0);
2343               level = setpropagatelearn(solv, level, p, disablerules, why);
2344               if (level == 0)
2345                 break;
2346               continue;
2347             }
2348           /* all branches done, we're finally finished */
2349           break;
2350         }
2351
2352       /* auto-minimization step */
2353      if (solv->branches.count)
2354         {
2355           int l = 0, lasti = -1, lastl = -1;
2356           Id why;
2357
2358           p = 0;
2359           for (i = solv->branches.count - 1; i >= 0; i--)
2360             {
2361               p = solv->branches.elements[i];
2362               if (p < 0)
2363                 l = -p;
2364               else if (p > 0 && solv->decisionmap[p] > l + 1)
2365                 {
2366                   lasti = i;
2367                   lastl = l;
2368                 }
2369             }
2370           if (lasti >= 0)
2371             {
2372               /* kill old solvable so that we do not loop */
2373               p = solv->branches.elements[lasti];
2374               solv->branches.elements[lasti] = 0;
2375               POOL_DEBUG(SOLV_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, pool_solvid2str(pool, p));
2376               minimizationsteps++;
2377
2378               level = lastl;
2379               revert(solv, level);
2380               why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2381               assert(why >= 0);
2382               olevel = level;
2383               level = setpropagatelearn(solv, level, p, disablerules, why);
2384               if (level == 0)
2385                 break;
2386               continue;         /* back to main loop */
2387             }
2388         }
2389       /* no minimization found, we're finally finished! */
2390       break;
2391     }
2392
2393   POOL_DEBUG(SOLV_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2394
2395   POOL_DEBUG(SOLV_DEBUG_STATS, "done solving.\n\n");
2396   queue_free(&dq);
2397   queue_free(&dqs);
2398   if (level == 0)
2399     {
2400       /* unsolvable */
2401       solv->decisioncnt_update = solv->decisionq.count;
2402       solv->decisioncnt_keep = solv->decisionq.count;
2403       solv->decisioncnt_resolve = solv->decisionq.count;
2404       solv->decisioncnt_weak = solv->decisionq.count;
2405       solv->decisioncnt_orphan = solv->decisionq.count;
2406     }
2407 #if 0
2408   solver_printdecisionq(solv, SOLV_DEBUG_RESULT);
2409 #endif
2410 }
2411
2412
2413 /*-------------------------------------------------------------------
2414  * 
2415  * remove disabled conflicts
2416  *
2417  * purpose: update the decisionmap after some rules were disabled.
2418  * this is used to calculate the suggested/recommended package list.
2419  * Also returns a "removed" list to undo the discisionmap changes.
2420  */
2421
2422 static void
2423 removedisabledconflicts(Solver *solv, Queue *removed)
2424 {
2425   Pool *pool = solv->pool;
2426   int i, n;
2427   Id p, why, *dp;
2428   Id new;
2429   Rule *r;
2430   Id *decisionmap = solv->decisionmap;
2431
2432   queue_empty(removed);
2433   for (i = 0; i < solv->decisionq.count; i++)
2434     {
2435       p = solv->decisionq.elements[i];
2436       if (p > 0)
2437         continue;       /* conflicts only, please */
2438       why = solv->decisionq_why.elements[i];
2439       if (why == 0)
2440         {
2441           /* no rule involved, must be a orphan package drop */
2442           continue;
2443         }
2444       /* we never do conflicts on free decisions, so there
2445        * must have been an unit rule */
2446       assert(why > 0);
2447       r = solv->rules + why;
2448       if (r->d < 0 && decisionmap[-p])
2449         {
2450           /* rule is now disabled, remove from decisionmap */
2451           POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2452           queue_push(removed, -p);
2453           queue_push(removed, decisionmap[-p]);
2454           decisionmap[-p] = 0;
2455         }
2456     }
2457   if (!removed->count)
2458     return;
2459   /* we removed some confliced packages. some of them might still
2460    * be in conflict, so search for unit rules and re-conflict */
2461   new = 0;
2462   for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2463     {
2464       if (i == solv->nrules)
2465         {
2466           i = 1;
2467           r = solv->rules + i;
2468         }
2469       if (r->d < 0)
2470         continue;
2471       if (!r->w2)
2472         {
2473           if (r->p < 0 && !decisionmap[-r->p])
2474             new = r->p;
2475         }
2476       else if (!r->d)
2477         {
2478           /* binary rule */
2479           if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2480             new = r->p;
2481           else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2482             new = r->w2;
2483         }
2484       else
2485         {
2486           if (r->p < 0 && decisionmap[-r->p] == 0)
2487             new = r->p;
2488           if (new || DECISIONMAP_FALSE(r->p))
2489             {
2490               dp = pool->whatprovidesdata + r->d;
2491               while ((p = *dp++) != 0)
2492                 {
2493                   if (new && p == new)
2494                     continue;
2495                   if (p < 0 && decisionmap[-p] == 0)
2496                     {
2497                       if (new)
2498                         {
2499                           new = 0;
2500                           break;
2501                         }
2502                       new = p;
2503                     }
2504                   else if (!DECISIONMAP_FALSE(p))
2505                     {
2506                       new = 0;
2507                       break;
2508                     }
2509                 }
2510             }
2511         }
2512       if (new)
2513         {
2514           POOL_DEBUG(SOLV_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2515           decisionmap[-new] = -1;
2516           new = 0;
2517           n = 0;        /* redo all rules */
2518         }
2519     }
2520 }
2521
2522 static inline void
2523 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2524 {
2525   int i;
2526   for (i = 0; i < removed->count; i += 2)
2527     solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2528 }
2529
2530
2531 /*-------------------------------------------------------------------
2532  *
2533  * weaken solvable dependencies
2534  */
2535
2536 static void
2537 weaken_solvable_deps(Solver *solv, Id p)
2538 {
2539   int i;
2540   Rule *r;
2541
2542   for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2543     {
2544       if (r->p != -p)
2545         continue;
2546       if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2547         continue;       /* conflict */
2548       queue_push(&solv->weakruleq, i);
2549     }
2550 }
2551
2552
2553 /********************************************************************/
2554 /* main() */
2555
2556
2557 void
2558 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *noobsmap)
2559 {
2560   int i;
2561   Id how, what, select;
2562   Id p, pp;
2563   for (i = 0; i < job->count; i += 2)
2564     {
2565       how = job->elements[i];
2566       if ((how & SOLVER_JOBMASK) != SOLVER_NOOBSOLETES)
2567         continue;
2568       what = job->elements[i + 1];
2569       select = how & SOLVER_SELECTMASK;
2570       if (!noobsmap->size)
2571         map_grow(noobsmap, pool->nsolvables);
2572       if (select == SOLVER_SOLVABLE_ALL)
2573         {
2574           FOR_POOL_SOLVABLES(p)
2575             MAPSET(noobsmap, p);
2576         }
2577       else if (select == SOLVER_SOLVABLE_REPO)
2578         {
2579           Solvable *s;
2580           Repo *repo = pool_id2repo(pool, what);
2581           if (repo)
2582             FOR_REPO_SOLVABLES(repo, p, s)
2583               MAPSET(noobsmap, p);
2584         }
2585       FOR_JOB_SELECT(p, pp, select, what)
2586         MAPSET(noobsmap, p);
2587     }
2588 }
2589
2590 /*
2591  * add a rule created by a job, record job number and weak flag
2592  */
2593 static inline void
2594 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
2595 {
2596   solver_addrule(solv, p, d);
2597   queue_push(&solv->ruletojob, job);
2598   if (weak)
2599     queue_push(&solv->weakruleq, solv->nrules - 1);
2600 }
2601
2602 static void
2603 add_update_target(Solver *solv, Id p, Id how)
2604 {
2605   Pool *pool = solv->pool;
2606   Solvable *s = pool->solvables + p;
2607   Repo *installed = solv->installed;
2608   Id pi, pip;
2609   if (!solv->update_targets)
2610     {
2611       solv->update_targets = solv_calloc(1, sizeof(Queue));
2612       queue_init(solv->update_targets);
2613     }
2614   if (s->repo == installed)
2615     {
2616       queue_push2(solv->update_targets, p, p);
2617       return;
2618     }
2619   FOR_PROVIDES(pi, pip, s->name)
2620     {
2621       Solvable *si = pool->solvables + pi;
2622       if (si->repo != installed || si->name != s->name)
2623         continue;
2624       if (how & SOLVER_FORCEBEST)
2625         {
2626           if (!solv->bestupdatemap.size)
2627             map_grow(&solv->bestupdatemap, installed->end - installed->start);
2628           MAPSET(&solv->bestupdatemap, pi - installed->start);
2629         }
2630       queue_push2(solv->update_targets, pi, p);
2631       /* check if it's ok to keep the installed package */
2632       if (s->evr == si->evr && solvable_identical(s, si))
2633         queue_push2(solv->update_targets, pi, pi);
2634     }
2635   if (s->obsoletes)
2636     {
2637       Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2638       while ((obs = *obsp++) != 0)
2639         {
2640           FOR_PROVIDES(pi, pip, obs) 
2641             {
2642               Solvable *si = pool->solvables + pi;
2643               if (si->repo != installed)
2644                 continue;
2645               if (si->name == s->name)
2646                 continue;       /* already handled above */
2647               if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
2648                 continue;
2649               if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
2650                 continue;
2651               if (how & SOLVER_FORCEBEST)
2652                 {
2653                   if (!solv->bestupdatemap.size)
2654                     map_grow(&solv->bestupdatemap, installed->end - installed->start);
2655                   MAPSET(&solv->bestupdatemap, pi - installed->start);
2656                 }
2657               queue_push2(solv->update_targets, pi, p);
2658             }
2659         }
2660     }
2661 }
2662
2663 static int
2664 transform_update_targets_sortfn(const void *ap, const void *bp, void *dp)
2665 {
2666   const Id *a = ap;
2667   const Id *b = bp;
2668   if (a[0] - b[0])
2669     return a[0] - b[0];
2670   return a[1] - b[1];
2671 }
2672
2673 static void
2674 transform_update_targets(Solver *solv)
2675 {
2676   Repo *installed = solv->installed;
2677   Queue *update_targets = solv->update_targets;
2678   int i, j;
2679   Id p, q, lastp, lastq;
2680
2681   if (!update_targets->count)
2682     {
2683       queue_free(update_targets);
2684       solv->update_targets = solv_free(update_targets);
2685       return;
2686     }
2687   if (update_targets->count > 2)
2688     solv_sort(update_targets->elements, update_targets->count >> 1, 2 * sizeof(Id), transform_update_targets_sortfn, solv);
2689   queue_insertn(update_targets, 0, installed->end - installed->start);
2690   lastp = lastq = 0;
2691   for (i = j = installed->end - installed->start; i < update_targets->count; i += 2)
2692     {
2693       if ((p = update_targets->elements[i]) != lastp)
2694         {
2695           if (!solv->updatemap.size)
2696             map_grow(&solv->updatemap, installed->end - installed->start);
2697           MAPSET(&solv->updatemap, p - installed->start);
2698           update_targets->elements[j++] = 0;                    /* finish old set */
2699           update_targets->elements[p - installed->start] = j;   /* start new set */
2700           lastp = p;
2701           lastq = 0;
2702         }
2703       if ((q = update_targets->elements[i + 1]) != lastq)
2704         {
2705           update_targets->elements[j++] = q;
2706           lastq = q;
2707         }
2708     }
2709   queue_truncate(update_targets, j);
2710   queue_push(update_targets, 0);        /* finish last set */
2711 }
2712
2713
2714 /*
2715  *
2716  * solve job queue
2717  *
2718  */
2719
2720 int
2721 solver_solve(Solver *solv, Queue *job)
2722 {
2723   Pool *pool = solv->pool;
2724   Repo *installed = solv->installed;
2725   int i;
2726   int oldnrules;
2727   Map addedmap;                /* '1' == have rpm-rules for solvable */
2728   Map installcandidatemap;
2729   Id how, what, select, name, weak, p, pp, d;
2730   Queue q;
2731   Solvable *s;
2732   Rule *r;
2733   int now, solve_start;
2734   int hasdupjob = 0;
2735   int hasbestinstalljob = 0;
2736
2737   solve_start = solv_timems(0);
2738
2739   /* log solver options */
2740   POOL_DEBUG(SOLV_DEBUG_STATS, "solver started\n");
2741   POOL_DEBUG(SOLV_DEBUG_STATS, "dosplitprovides=%d, noupdateprovide=%d, noinfarchcheck=%d\n", solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
2742   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);
2743   POOL_DEBUG(SOLV_DEBUG_STATS, "promoteepoch=%d, forbidselfconflicts=%d\n", pool->promoteepoch, pool->forbidselfconflicts);
2744   POOL_DEBUG(SOLV_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2745   POOL_DEBUG(SOLV_DEBUG_STATS, "dontinstallrecommended=%d, addalreadyrecommended=%d\n", solv->dontinstallrecommended, solv->addalreadyrecommended);
2746
2747   /* create whatprovides if not already there */
2748   if (!pool->whatprovides)
2749     pool_createwhatprovides(pool);
2750
2751   /* create obsolete index */
2752   policy_create_obsolete_index(solv);
2753
2754   /* remember job */
2755   queue_free(&solv->job);
2756   queue_init_clone(&solv->job, job);
2757
2758   if (solv->update_targets)
2759     {
2760       queue_free(solv->update_targets);
2761       solv->update_targets = solv_free(solv->update_targets);
2762     }
2763   /*
2764    * create basic rule set of all involved packages
2765    * use addedmap bitmap to make sure we don't create rules twice
2766    */
2767
2768   /* create noobsolete map if needed */
2769   solver_calculate_noobsmap(pool, job, &solv->noobsoletes);
2770
2771   map_init(&addedmap, pool->nsolvables);
2772   MAPSET(&addedmap, SYSTEMSOLVABLE);
2773
2774   map_init(&installcandidatemap, pool->nsolvables);
2775   queue_init(&q);
2776
2777   now = solv_timems(0);
2778   /*
2779    * create rules for all package that could be involved with the solving
2780    * so called: rpm rules
2781    *
2782    */
2783   if (installed)
2784     {
2785       /* check for update/verify jobs as they need to be known early */
2786       for (i = 0; i < job->count; i += 2)
2787         {
2788           how = job->elements[i];
2789           what = job->elements[i + 1];
2790           select = how & SOLVER_SELECTMASK;
2791           switch (how & SOLVER_JOBMASK)
2792             {
2793             case SOLVER_VERIFY:
2794               if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
2795                 solv->fixmap_all = 1;
2796               FOR_JOB_SELECT(p, pp, select, what)
2797                 {
2798                   s = pool->solvables + p;
2799                   if (s->repo != installed)
2800                     continue;
2801                   if (!solv->fixmap.size)
2802                     map_grow(&solv->fixmap, installed->end - installed->start);
2803                   MAPSET(&solv->fixmap, p - installed->start);
2804                 }
2805               break;
2806             case SOLVER_UPDATE:
2807               if (select == SOLVER_SOLVABLE_ALL)
2808                 {
2809                   solv->updatemap_all = 1;
2810                   if (how & SOLVER_FORCEBEST)
2811                     solv->bestupdatemap_all = 1;
2812                 }
2813               else if (select == SOLVER_SOLVABLE_REPO)
2814                 {
2815                   Repo *repo = pool_id2repo(pool, what);
2816                   if (!repo)
2817                     break;
2818                   if (repo == installed && !(how & SOLVER_TARGETED))
2819                     {
2820                       solv->updatemap_all = 1;
2821                       if (how & SOLVER_FORCEBEST)
2822                         solv->bestupdatemap_all = 1;
2823                       break;
2824                     }
2825                   if (solv->noautotarget && !(how & SOLVER_TARGETED))
2826                     break;
2827                   /* targeted update */
2828                   FOR_REPO_SOLVABLES(repo, p, s)
2829                     add_update_target(solv, p, how);
2830                 }
2831               else
2832                 {
2833                   if (!(how & SOLVER_TARGETED))
2834                     {
2835                       int targeted = 1;
2836                       FOR_JOB_SELECT(p, pp, select, what)
2837                         {
2838                           s = pool->solvables + p;
2839                           if (s->repo != installed)
2840                             continue;
2841                           if (!solv->updatemap.size)
2842                             map_grow(&solv->updatemap, installed->end - installed->start);
2843                           MAPSET(&solv->updatemap, p - installed->start);
2844                           if (how & SOLVER_FORCEBEST)
2845                             {
2846                               if (!solv->bestupdatemap.size)
2847                                 map_grow(&solv->bestupdatemap, installed->end - installed->start);
2848                               MAPSET(&solv->bestupdatemap, p - installed->start);
2849                             }
2850                           targeted = 0;
2851                         }
2852                       if (!targeted || solv->noautotarget)
2853                         break;
2854                     }
2855                   FOR_JOB_SELECT(p, pp, select, what)
2856                     add_update_target(solv, p, how);
2857                 }
2858               break;
2859             default:
2860               break;
2861             }
2862         }
2863
2864       if (solv->update_targets)
2865         transform_update_targets(solv);
2866
2867       oldnrules = solv->nrules;
2868       FOR_REPO_SOLVABLES(installed, p, s)
2869         solver_addrpmrulesforsolvable(solv, s, &addedmap);
2870       POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
2871       oldnrules = solv->nrules;
2872       FOR_REPO_SOLVABLES(installed, p, s)
2873         solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
2874       POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
2875     }
2876
2877   /*
2878    * create rules for all packages involved in the job
2879    * (to be installed or removed)
2880    */
2881     
2882   oldnrules = solv->nrules;
2883   for (i = 0; i < job->count; i += 2)
2884     {
2885       how = job->elements[i];
2886       what = job->elements[i + 1];
2887       select = how & SOLVER_SELECTMASK;
2888
2889       switch (how & SOLVER_JOBMASK)
2890         {
2891         case SOLVER_INSTALL:
2892           FOR_JOB_SELECT(p, pp, select, what)
2893             {
2894               MAPSET(&installcandidatemap, p);
2895               solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
2896             }
2897           break;
2898         case SOLVER_DISTUPGRADE:
2899           if (select == SOLVER_SOLVABLE_ALL)
2900             {
2901               solv->dupmap_all = 1;
2902               solv->updatemap_all = 1;
2903               if (how & SOLVER_FORCEBEST)
2904                 solv->bestupdatemap_all = 1;
2905             }
2906           if (!solv->dupmap_all)
2907             hasdupjob = 1;
2908           break;
2909         default:
2910           break;
2911         }
2912     }
2913   POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
2914
2915     
2916   /*
2917    * add rules for suggests, enhances
2918    */
2919   oldnrules = solv->nrules;
2920   solver_addrpmrulesforweak(solv, &addedmap);
2921   POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
2922
2923   /*
2924    * first pass done, we now have all the rpm rules we need.
2925    * unify existing rules before going over all job rules and
2926    * policy rules.
2927    * at this point the system is always solvable,
2928    * as an empty system (remove all packages) is a valid solution
2929    */
2930
2931   IF_POOLDEBUG (SOLV_DEBUG_STATS)
2932     {
2933       int possible = 0, installable = 0;
2934       for (i = 1; i < pool->nsolvables; i++)
2935         {
2936           if (pool_installable(pool, pool->solvables + i))
2937             installable++;
2938           if (MAPTST(&addedmap, i))
2939             possible++;
2940         }
2941       POOL_DEBUG(SOLV_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
2942     }
2943
2944   solver_unifyrules(solv);                          /* remove duplicate rpm rules */
2945   solv->rpmrules_end = solv->nrules;              /* mark end of rpm rules */
2946
2947   POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
2948   POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule creation took %d ms\n", solv_timems(now));
2949
2950   /* create dup maps if needed. We need the maps early to create our
2951    * update rules */
2952   if (hasdupjob)
2953     solver_createdupmaps(solv);
2954
2955   /*
2956    * create feature rules
2957    * 
2958    * foreach installed:
2959    *   create assertion (keep installed, if no update available)
2960    *   or
2961    *   create update rule (A|update1(A)|update2(A)|...)
2962    * 
2963    * those are used later on to keep a version of the installed packages in
2964    * best effort mode
2965    */
2966     
2967   solv->featurerules = solv->nrules;              /* mark start of feature rules */
2968   if (installed)
2969     {
2970       /* foreach possibly installed solvable */
2971       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2972         {
2973           if (s->repo != installed)
2974             {
2975               solver_addrule(solv, 0, 0);       /* create dummy rule */
2976               continue;
2977             }
2978           solver_addupdaterule(solv, s, 1);    /* allow s to be updated */
2979         }
2980       /* make sure we accounted for all rules */
2981       assert(solv->nrules - solv->featurerules == installed->end - installed->start);
2982     }
2983   solv->featurerules_end = solv->nrules;
2984
2985     /*
2986      * Add update rules for installed solvables
2987      * 
2988      * almost identical to feature rules
2989      * except that downgrades/archchanges/vendorchanges are not allowed
2990      */
2991     
2992   solv->updaterules = solv->nrules;
2993
2994   if (installed)
2995     { /* foreach installed solvables */
2996       /* we create all update rules, but disable some later on depending on the job */
2997       for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
2998         {
2999           Rule *sr;
3000
3001           if (s->repo != installed)
3002             {
3003               solver_addrule(solv, 0, 0);       /* create dummy rule */
3004               continue;
3005             }
3006           solver_addupdaterule(solv, s, 0);     /* allowall = 0: downgrades not allowed */
3007           /*
3008            * check for and remove duplicate
3009            */
3010           r = solv->rules + solv->nrules - 1;           /* r: update rule */
3011           sr = r - (installed->end - installed->start); /* sr: feature rule */
3012           /* it's orphaned if there is no feature rule or the feature rule
3013            * consists just of the installed package */
3014           if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
3015             queue_push(&solv->orphaned, i);
3016           if (!r->p)
3017             {
3018               assert(solv->dupmap_all && !sr->p);
3019               continue;
3020             }
3021           if (!solver_rulecmp(solv, r, sr))
3022             memset(sr, 0, sizeof(*sr));         /* delete unneeded feature rule */
3023           else
3024             solver_disablerule(solv, sr);       /* disable feature rule */
3025         }
3026       /* consistency check: we added a rule for _every_ installed solvable */
3027       assert(solv->nrules - solv->updaterules == installed->end - installed->start);
3028     }
3029   solv->updaterules_end = solv->nrules;
3030
3031
3032   /*
3033    * now add all job rules
3034    */
3035
3036   solv->jobrules = solv->nrules;
3037   if (solv->cleandeps_updatepkgs)
3038     {
3039       queue_free(solv->cleandeps_updatepkgs);
3040       solv->cleandeps_updatepkgs = solv_free(solv->cleandeps_updatepkgs);
3041     }
3042   for (i = 0; i < job->count; i += 2)
3043     {
3044       oldnrules = solv->nrules;
3045
3046       how = job->elements[i];
3047       what = job->elements[i + 1];
3048       weak = how & SOLVER_WEAK;
3049       select = how & SOLVER_SELECTMASK;
3050       switch (how & SOLVER_JOBMASK)
3051         {
3052         case SOLVER_INSTALL:
3053           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3054           if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3055             map_grow(&solv->cleandepsmap, installed->end - installed->start);
3056           if (select == SOLVER_SOLVABLE)
3057             {
3058               p = what;
3059               d = 0;
3060             }
3061           else
3062             {
3063               queue_empty(&q);
3064               FOR_JOB_SELECT(p, pp, select, what)
3065                 queue_push(&q, p);
3066               if (!q.count)
3067                 {
3068                   /* no candidate found, make this an impossible rule */
3069                   queue_push(&q, -SYSTEMSOLVABLE);
3070                 }
3071               p = queue_shift(&q);      /* get first candidate */
3072               d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q);    /* internalize */
3073             }
3074           solver_addjobrule(solv, p, d, i, weak);
3075           if (how & SOLVER_FORCEBEST)
3076             hasbestinstalljob = 1;
3077           break;
3078         case SOLVER_ERASE:
3079           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
3080           if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3081             map_grow(&solv->cleandepsmap, installed->end - installed->start);
3082           /* specific solvable: by id or by nevra */
3083           name = (select == SOLVER_SOLVABLE || (select == SOLVER_SOLVABLE_NAME && ISRELDEP(what))) ? 0 : -1;
3084           if (select == SOLVER_SOLVABLE_ALL)    /* hmmm ;) */
3085             {
3086               FOR_POOL_SOLVABLES(p)
3087                 solver_addjobrule(solv, -p, 0, i, weak);
3088             }
3089           else if (select == SOLVER_SOLVABLE_REPO)
3090             {
3091               Repo *repo = pool_id2repo(pool, what);
3092               if (repo)
3093                 FOR_REPO_SOLVABLES(repo, p, s)
3094                   solver_addjobrule(solv, -p, 0, i, weak);
3095             }
3096           FOR_JOB_SELECT(p, pp, select, what)
3097             {
3098               s = pool->solvables + p;
3099               if (installed && s->repo == installed)
3100                 name = !name ? s->name : -1;
3101               solver_addjobrule(solv, -p, 0, i, weak);
3102             }
3103           /* special case for "erase a specific solvable": we also
3104            * erase all other solvables with that name, so that they
3105            * don't get picked up as replacement.
3106            * name is > 0 if exactly one installed solvable matched.
3107            */
3108           /* XXX: look also at packages that obsolete this package? */
3109           if (name > 0)
3110             {
3111               int j, k;
3112               k = solv->nrules;
3113               FOR_PROVIDES(p, pp, name)
3114                 {
3115                   s = pool->solvables + p;
3116                   if (s->name != name)
3117                     continue;
3118                   /* keep other versions installed */
3119                   if (s->repo == installed)
3120                     continue;
3121                   /* keep installcandidates of other jobs */
3122                   if (MAPTST(&installcandidatemap, p))
3123                     continue;
3124                   /* don't add the same rule twice */
3125                   for (j = oldnrules; j < k; j++)
3126                     if (solv->rules[j].p == -p)
3127                       break;
3128                   if (j == k)
3129                     solver_addjobrule(solv, -p, 0, i, weak);    /* remove by id */
3130                 }
3131             }
3132           break;
3133
3134         case SOLVER_UPDATE:
3135           if ((how & SOLVER_CLEANDEPS) != 0 && installed)
3136             {
3137               if (how == SOLVER_SOLVABLE_ALL || (how == SOLVER_SOLVABLE_REPO && what == installed->repoid))
3138                 {
3139                   FOR_REPO_SOLVABLES(installed, p, s)
3140                     {
3141                       if (!solv->cleandeps_updatepkgs)
3142                         {
3143                           solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
3144                           queue_init(solv->cleandeps_updatepkgs);
3145                         }
3146                       queue_pushunique(solv->cleandeps_updatepkgs, p);
3147                       if (!solv->cleandepsmap.size)
3148                         map_grow(&solv->cleandepsmap, installed->end - installed->start);
3149                     }
3150                 }
3151               FOR_JOB_SELECT(p, pp, select, what)
3152                 {
3153                   s = pool->solvables + p;
3154                   if (s->repo != installed)
3155                     continue;
3156                   if (!solv->cleandeps_updatepkgs)
3157                     {
3158                       solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
3159                       queue_init(solv->cleandeps_updatepkgs);
3160                     }
3161                   queue_pushunique(solv->cleandeps_updatepkgs, p);
3162                   if (!solv->cleandepsmap.size)
3163                     map_grow(&solv->cleandepsmap, installed->end - installed->start);
3164                 }
3165             }
3166           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3167           break;
3168         case SOLVER_VERIFY:
3169           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3170           break;
3171         case SOLVER_WEAKENDEPS:
3172           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3173           if (select != SOLVER_SOLVABLE)
3174             break;
3175           s = pool->solvables + what;
3176           weaken_solvable_deps(solv, what);
3177           break;
3178         case SOLVER_NOOBSOLETES:
3179           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sno obsolete %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3180           break;
3181         case SOLVER_LOCK:
3182           POOL_DEBUG(SOLV_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3183           if (select == SOLVER_SOLVABLE_ALL)
3184             {
3185               FOR_POOL_SOLVABLES(p)
3186                 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3187             }
3188           else if (select == SOLVER_SOLVABLE_REPO)
3189             {
3190               Repo *repo = pool_id2repo(pool, what);
3191               if (repo)
3192                 FOR_REPO_SOLVABLES(repo, p, s)
3193                   solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3194             }
3195           FOR_JOB_SELECT(p, pp, select, what)
3196             solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3197           break;
3198         case SOLVER_DISTUPGRADE:
3199           POOL_DEBUG(SOLV_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
3200           break;
3201         case SOLVER_DROP_ORPHANED:
3202           POOL_DEBUG(SOLV_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
3203           if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3204             solv->droporphanedmap_all = 1;
3205           FOR_JOB_SELECT(p, pp, select, what)
3206             {
3207               s = pool->solvables + p;
3208               if (!installed || s->repo != installed)
3209                 continue;
3210               if (!solv->droporphanedmap.size)
3211                 map_grow(&solv->droporphanedmap, installed->end - installed->start);
3212               MAPSET(&solv->droporphanedmap, p - installed->start);
3213             }
3214           break;
3215         case SOLVER_USERINSTALLED:
3216           POOL_DEBUG(SOLV_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
3217           break;
3218         default:
3219           POOL_DEBUG(SOLV_DEBUG_JOB, "job: unknown job\n");
3220           break;
3221         }
3222         
3223         /*
3224          * debug
3225          */
3226         
3227       IF_POOLDEBUG (SOLV_DEBUG_JOB)
3228         {
3229           int j;
3230           if (solv->nrules == oldnrules)
3231             POOL_DEBUG(SOLV_DEBUG_JOB, " - no rule created\n");
3232           for (j = oldnrules; j < solv->nrules; j++)
3233             {
3234               POOL_DEBUG(SOLV_DEBUG_JOB, " - job ");
3235               solver_printrule(solv, SOLV_DEBUG_JOB, solv->rules + j);
3236             }
3237         }
3238     }
3239   assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
3240   solv->jobrules_end = solv->nrules;
3241
3242   /* now create infarch and dup rules */
3243   if (!solv->noinfarchcheck)
3244     {
3245       solver_addinfarchrules(solv, &addedmap);
3246       if (pool->obsoleteusescolors)
3247         {
3248           /* currently doesn't work well with infarch rules, so make
3249            * them weak */
3250           for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
3251             queue_push(&solv->weakruleq, i);
3252         }
3253     }
3254   else
3255     solv->infarchrules = solv->infarchrules_end = solv->nrules;
3256
3257   if (hasdupjob)
3258     solver_addduprules(solv, &addedmap);
3259   else
3260     solv->duprules = solv->duprules_end = solv->nrules;
3261
3262   if (solv->bestupdatemap_all || solv->bestupdatemap.size || hasbestinstalljob)
3263     solver_addbestrules(solv, hasbestinstalljob);
3264   else
3265     solv->bestrules = solv->bestrules_end = solv->nrules;
3266
3267   if (hasdupjob)
3268     solver_freedupmaps(solv);   /* no longer needed */
3269
3270   if (1)
3271     solver_addchoicerules(solv);
3272   else
3273     solv->choicerules = solv->choicerules_end = solv->nrules;
3274
3275   if (0)
3276     {
3277       for (i = solv->featurerules; i < solv->nrules; i++)
3278         solver_printruleclass(solv, SOLV_DEBUG_RESULT, solv->rules + i);
3279     }
3280   /* all rules created
3281    * --------------------------------------------------------------
3282    * prepare for solving
3283    */
3284     
3285   /* free unneeded memory */
3286   map_free(&addedmap);
3287   map_free(&installcandidatemap);
3288   queue_free(&q);
3289
3290   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);
3291   POOL_DEBUG(SOLV_DEBUG_STATS, "overall rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3292
3293   /* create weak map */
3294   map_init(&solv->weakrulemap, solv->nrules);
3295   for (i = 0; i < solv->weakruleq.count; i++)
3296     {
3297       p = solv->weakruleq.elements[i];
3298       MAPSET(&solv->weakrulemap, p);
3299     }
3300
3301   /* all new rules are learnt after this point */
3302   solv->learntrules = solv->nrules;
3303
3304   /* create watches chains */
3305   makewatches(solv);
3306
3307   /* create assertion index. it is only used to speed up
3308    * makeruledecsions() a bit */
3309   for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
3310     if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
3311       queue_push(&solv->ruleassertions, i);
3312
3313   /* disable update rules that conflict with our job */
3314   solver_disablepolicyrules(solv);
3315
3316   /* make initial decisions based on assertion rules */
3317   makeruledecisions(solv);
3318   POOL_DEBUG(SOLV_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
3319
3320   /* no mistakes */
3321   if (solv->cleandeps_mistakes)
3322     {    
3323       queue_free(solv->cleandeps_mistakes);
3324       solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
3325     }    
3326
3327   /*
3328    * ********************************************
3329    * solve!
3330    * ********************************************
3331    */
3332     
3333   now = solv_timems(0);
3334   solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
3335   POOL_DEBUG(SOLV_DEBUG_STATS, "solver took %d ms\n", solv_timems(now));
3336
3337   /*
3338    * prepare solution queue if there were problems
3339    */
3340   solver_prepare_solutions(solv);
3341
3342   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);
3343   POOL_DEBUG(SOLV_DEBUG_STATS, "solver_solve took %d ms\n", solv_timems(solve_start));
3344
3345   /* return number of problems */
3346   return solv->problems.count ? solv->problems.count / 2 : 0;
3347 }
3348
3349 Transaction *
3350 solver_create_transaction(Solver *solv)
3351 {
3352   return transaction_create_decisionq(solv->pool, &solv->decisionq, &solv->noobsoletes);
3353 }
3354
3355 void solver_get_orphaned(Solver *solv, Queue *orphanedq)
3356 {
3357   queue_free(orphanedq);
3358   queue_init_clone(orphanedq, &solv->orphaned);
3359 }
3360
3361 void solver_get_recommendations(Solver *solv, Queue *recommendationsq, Queue *suggestionsq, int noselected)
3362 {
3363   Pool *pool = solv->pool;
3364   Queue redoq, disabledq;
3365   int goterase, i;
3366   Solvable *s;
3367   Rule *r;
3368   Map obsmap;
3369
3370   if (!recommendationsq && !suggestionsq)
3371     return;
3372
3373   map_init(&obsmap, pool->nsolvables);
3374   if (solv->installed)
3375     {
3376       Id obs, *obsp, p, po, ppo;
3377       for (p = solv->installed->start; p < solv->installed->end; p++)
3378         {
3379           s = pool->solvables + p;
3380           if (s->repo != solv->installed || !s->obsoletes)
3381             continue;
3382           if (solv->decisionmap[p] <= 0)
3383             continue;
3384           if (solv->noobsoletes.size && MAPTST(&solv->noobsoletes, p))
3385             continue;
3386           obsp = s->repo->idarraydata + s->obsoletes;
3387           /* foreach obsoletes */
3388           while ((obs = *obsp++) != 0)
3389             FOR_PROVIDES(po, ppo, obs)
3390               MAPSET(&obsmap, po);
3391         }
3392     }
3393
3394   queue_init(&redoq);
3395   queue_init(&disabledq);
3396   goterase = 0;
3397   /* disable all erase jobs (including weak "keep uninstalled" rules) */
3398   for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
3399     {
3400       if (r->d < 0)     /* disabled ? */
3401         continue;
3402       if (r->p >= 0)    /* install job? */
3403         continue;
3404       queue_push(&disabledq, i);
3405       solver_disablerule(solv, r);
3406       goterase++;
3407     }
3408   
3409   if (goterase)
3410     {
3411       enabledisablelearntrules(solv);
3412       removedisabledconflicts(solv, &redoq);
3413     }
3414
3415   /*
3416    * find recommended packages
3417    */
3418   if (recommendationsq)
3419     {
3420       Id rec, *recp, p, pp;
3421
3422       queue_empty(recommendationsq);
3423       /* create map of all recommened packages */
3424       solv->recommends_index = -1;
3425       MAPZERO(&solv->recommendsmap);
3426
3427       /* put all packages the solver already chose in the map */
3428       if (solv->decisioncnt_weak)
3429         {
3430           for (i = solv->decisioncnt_weak; i < solv->decisioncnt_orphan; i++)
3431             {
3432               Id why;
3433               why = solv->decisionq_why.elements[i];
3434               if (why)
3435                 continue;       /* forced by unit rule */
3436               p = solv->decisionq.elements[i];
3437               if (p < 0)
3438                 continue;
3439               MAPSET(&solv->recommendsmap, p);
3440             }
3441         }
3442
3443       for (i = 0; i < solv->decisionq.count; i++)
3444         {
3445           p = solv->decisionq.elements[i];
3446           if (p < 0)
3447             continue;
3448           s = pool->solvables + p;
3449           if (s->recommends)
3450             {
3451               recp = s->repo->idarraydata + s->recommends;
3452               while ((rec = *recp++) != 0)
3453                 {
3454                   FOR_PROVIDES(p, pp, rec)
3455                     if (solv->decisionmap[p] > 0)
3456                       break;
3457                   if (p)
3458                     {
3459                       if (!noselected)
3460                         {
3461                           FOR_PROVIDES(p, pp, rec)
3462                             if (solv->decisionmap[p] > 0)
3463                               MAPSET(&solv->recommendsmap, p);
3464                         }
3465                       continue; /* p != 0: already fulfilled */
3466                     }
3467                   FOR_PROVIDES(p, pp, rec)
3468                     MAPSET(&solv->recommendsmap, p);
3469                 }
3470             }
3471         }
3472       for (i = 1; i < pool->nsolvables; i++)
3473         {
3474           if (solv->decisionmap[i] < 0)
3475             continue;
3476           if (solv->decisionmap[i] > 0 && noselected)
3477             continue;
3478           if (MAPTST(&obsmap, i))
3479             continue;
3480           s = pool->solvables + i;
3481           if (!MAPTST(&solv->recommendsmap, i))
3482             {
3483               if (!s->supplements)
3484                 continue;
3485               if (!pool_installable(pool, s))
3486                 continue;
3487               if (!solver_is_supplementing(solv, s))
3488                 continue;
3489             }
3490           queue_push(recommendationsq, i);
3491         }
3492       /* we use MODE_SUGGEST here so that repo prio is ignored */
3493       policy_filter_unwanted(solv, recommendationsq, POLICY_MODE_SUGGEST);
3494     }
3495
3496   /*
3497    * find suggested packages
3498    */
3499     
3500   if (suggestionsq)
3501     {
3502       Id sug, *sugp, p, pp;
3503
3504       queue_empty(suggestionsq);
3505       /* create map of all suggests that are still open */
3506       solv->recommends_index = -1;
3507       MAPZERO(&solv->suggestsmap);
3508       for (i = 0; i < solv->decisionq.count; i++)
3509         {
3510           p = solv->decisionq.elements[i];
3511           if (p < 0)
3512             continue;
3513           s = pool->solvables + p;
3514           if (s->suggests)
3515             {
3516               sugp = s->repo->idarraydata + s->suggests;
3517               while ((sug = *sugp++) != 0)
3518                 {
3519                   FOR_PROVIDES(p, pp, sug)
3520                     if (solv->decisionmap[p] > 0)
3521                       break;
3522                   if (p)
3523                     {
3524                       if (!noselected)
3525                         {
3526                           FOR_PROVIDES(p, pp, sug)
3527                             if (solv->decisionmap[p] > 0)
3528                               MAPSET(&solv->suggestsmap, p);
3529                         }
3530                       continue; /* already fulfilled */
3531                     }
3532                   FOR_PROVIDES(p, pp, sug)
3533                     MAPSET(&solv->suggestsmap, p);
3534                 }
3535             }
3536         }
3537       for (i = 1; i < pool->nsolvables; i++)
3538         {
3539           if (solv->decisionmap[i] < 0)
3540             continue;
3541           if (solv->decisionmap[i] > 0 && noselected)
3542             continue;
3543           if (MAPTST(&obsmap, i))
3544             continue;
3545           s = pool->solvables + i;
3546           if (!MAPTST(&solv->suggestsmap, i))
3547             {
3548               if (!s->enhances)
3549                 continue;
3550               if (!pool_installable(pool, s))
3551                 continue;
3552               if (!solver_is_enhancing(solv, s))
3553                 continue;
3554             }
3555           queue_push(suggestionsq, i);
3556         }
3557       policy_filter_unwanted(solv, suggestionsq, POLICY_MODE_SUGGEST);
3558     }
3559
3560   /* undo removedisabledconflicts */
3561   if (redoq.count)
3562     undo_removedisabledconflicts(solv, &redoq);
3563   queue_free(&redoq);
3564   
3565   /* undo job rule disabling */
3566   for (i = 0; i < disabledq.count; i++)
3567     solver_enablerule(solv, solv->rules + disabledq.elements[i]);
3568   queue_free(&disabledq);
3569   map_free(&obsmap);
3570 }
3571
3572
3573 /***********************************************************************/
3574 /* disk usage computations */
3575
3576 /*-------------------------------------------------------------------
3577  * 
3578  * calculate DU changes
3579  */
3580
3581 void
3582 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
3583 {
3584   Map installedmap;
3585
3586   solver_create_state_maps(solv, &installedmap, 0);
3587   pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
3588   map_free(&installedmap);
3589 }
3590
3591
3592 /*-------------------------------------------------------------------
3593  * 
3594  * calculate changes in install size
3595  */
3596
3597 int
3598 solver_calc_installsizechange(Solver *solv)
3599 {
3600   Map installedmap;
3601   int change;
3602
3603   solver_create_state_maps(solv, &installedmap, 0);
3604   change = pool_calc_installsizechange(solv->pool, &installedmap);
3605   map_free(&installedmap);
3606   return change;
3607 }
3608
3609 void
3610 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
3611 {
3612   pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
3613 }
3614
3615 void
3616 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
3617 {
3618   Map installedmap;
3619   pool_create_state_maps(solv->pool,  &solv->decisionq, &installedmap, 0);
3620   pool_trivial_installable_noobsoletesmap(solv->pool, &installedmap, pkgs, res, solv->noobsoletes.size ? &solv->noobsoletes : 0);
3621   map_free(&installedmap);
3622 }
3623
3624 /*-------------------------------------------------------------------
3625  * 
3626  * decision introspection
3627  */
3628
3629 int
3630 solver_get_decisionlevel(Solver *solv, Id p)
3631 {
3632   return solv->decisionmap[p];
3633 }
3634
3635 void
3636 solver_get_decisionqueue(Solver *solv, Queue *decisionq)
3637 {
3638   queue_free(decisionq);
3639   queue_init_clone(decisionq, &solv->decisionq);
3640 }
3641
3642 int
3643 solver_get_lastdecisionblocklevel(Solver *solv)
3644 {
3645   Id p;
3646   if (solv->decisionq.count == 0)
3647     return 0;
3648   p = solv->decisionq.elements[solv->decisionq.count - 1];
3649   if (p < 0)
3650     p = -p;
3651   return solv->decisionmap[p] < 0 ? -solv->decisionmap[p] : solv->decisionmap[p];
3652 }
3653
3654 void
3655 solver_get_decisionblock(Solver *solv, int level, Queue *decisionq)
3656 {
3657   Id p;
3658   int i;
3659
3660   queue_empty(decisionq);
3661   for (i = 0; i < solv->decisionq.count; i++)
3662     {
3663       p = solv->decisionq.elements[i];
3664       if (p < 0)
3665         p = -p;
3666       if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3667         break;
3668     }
3669   if (i == solv->decisionq.count)
3670     return;
3671   for (i = 0; i < solv->decisionq.count; i++)
3672     {
3673       p = solv->decisionq.elements[i];
3674       if (p < 0)
3675         p = -p;
3676       if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3677         queue_push(decisionq, p);
3678       else
3679         break;
3680     }
3681 }
3682
3683 int
3684 solver_describe_decision(Solver *solv, Id p, Id *infop)
3685 {
3686   int i;
3687   Id pp, why;
3688   
3689   if (infop)
3690     *infop = 0;
3691   if (!solv->decisionmap[p])
3692     return SOLVER_REASON_UNRELATED;
3693   pp = solv->decisionmap[p] < 0 ? -p : p;
3694   for (i = 0; i < solv->decisionq.count; i++)
3695     if (solv->decisionq.elements[i] == pp)
3696       break;
3697   if (i == solv->decisionq.count)       /* just in case... */
3698     return SOLVER_REASON_UNRELATED;
3699   why = solv->decisionq_why.elements[i];
3700   if (why > 0)
3701     {
3702       if (infop)
3703         *infop = why;
3704       return SOLVER_REASON_UNIT_RULE;
3705     }
3706   why = -why;
3707   if (i < solv->decisioncnt_update)
3708     {
3709       if (i == 0)
3710         {
3711           if (infop)
3712             *infop = SYSTEMSOLVABLE;
3713           return SOLVER_REASON_KEEP_INSTALLED;
3714         }
3715       if (infop)
3716         *infop = why;
3717       return SOLVER_REASON_RESOLVE_JOB;
3718     }
3719   if (i < solv->decisioncnt_keep)
3720     {
3721       if (why == 0 && pp < 0)
3722         return SOLVER_REASON_CLEANDEPS_ERASE;
3723       if (infop)
3724         {
3725           if (why >= solv->updaterules && why < solv->updaterules_end)
3726             *infop = why - solv->updaterules;
3727           else if (why >= solv->featurerules && why < solv->featurerules_end)
3728             *infop = why - solv->featurerules;
3729         }
3730       return SOLVER_REASON_UPDATE_INSTALLED;
3731     }
3732   if (i < solv->decisioncnt_resolve)
3733     {
3734       if (why == 0 && pp < 0)
3735         return SOLVER_REASON_CLEANDEPS_ERASE;
3736       if (infop)
3737         {
3738           if (why >= solv->updaterules && why < solv->updaterules_end)
3739             *infop = why - solv->updaterules;
3740           else if (why >= solv->featurerules && why < solv->featurerules_end)
3741             *infop = why - solv->featurerules;
3742         }
3743       return SOLVER_REASON_KEEP_INSTALLED;
3744     }
3745   if (i < solv->decisioncnt_weak)
3746     {
3747       if (infop)
3748         *infop = why;
3749       return SOLVER_REASON_RESOLVE;
3750     }
3751   if (solv->decisionq.count < solv->decisioncnt_orphan)
3752     return SOLVER_REASON_WEAKDEP;
3753   return SOLVER_REASON_RESOLVE_ORPHAN;
3754 }
3755
3756
3757 void
3758 solver_describe_weakdep_decision(Solver *solv, Id p, Queue *whyq)
3759 {
3760   Pool *pool = solv->pool;
3761   int i;
3762   int level = solv->decisionmap[p];
3763   int decisionno;
3764   Solvable *s;
3765
3766   queue_empty(whyq);
3767   if (level < 0)
3768     return;     /* huh? */
3769   for (decisionno = 0; decisionno < solv->decisionq.count; decisionno++)
3770     if (solv->decisionq.elements[decisionno] == p)
3771       break;
3772   if (decisionno == solv->decisionq.count)
3773     return;     /* huh? */
3774   if (decisionno < solv->decisioncnt_weak || decisionno >= solv->decisioncnt_orphan)
3775     return;     /* huh? */
3776
3777   /* 1) list all packages that recommend us */
3778   for (i = 1; i < pool->nsolvables; i++)
3779     {
3780       Id *recp, rec, pp2, p2;
3781       if (solv->decisionmap[i] < 0 || solv->decisionmap[i] >= level)
3782         continue;
3783       s = pool->solvables + i;
3784       if (!s->recommends)
3785         continue;
3786       if (!solv->addalreadyrecommended && s->repo == solv->installed)
3787         continue;
3788       recp = s->repo->idarraydata + s->recommends;
3789       while ((rec = *recp++) != 0)
3790         {
3791           int found = 0;
3792           FOR_PROVIDES(p2, pp2, rec)
3793             {
3794               if (p2 == p)
3795                 found = 1;
3796               else
3797                 {
3798                   /* if p2 is already installed, this recommends is ignored */
3799                   if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3800                     break;
3801                 }
3802             }
3803           if (!p2 && found)
3804             {
3805               queue_push(whyq, SOLVER_REASON_RECOMMENDED);
3806               queue_push2(whyq, p2, rec);
3807             }
3808         }
3809     }
3810   /* 2) list all supplements */
3811   s = pool->solvables + p;
3812   if (s->supplements && level > 0)
3813     {
3814       Id *supp, sup, pp2, p2;
3815       /* this is a hack. to use solver_dep_fulfilled we temporarily clear
3816        * everything above our level in the decisionmap */
3817       for (i = decisionno; i < solv->decisionq.count; i++ )
3818         {
3819           p2 = solv->decisionq.elements[i];
3820           if (p2 > 0)
3821             solv->decisionmap[p2] = -solv->decisionmap[p2];
3822         }
3823       supp = s->repo->idarraydata + s->supplements;
3824       while ((sup = *supp++) != 0)
3825         if (solver_dep_fulfilled(solv, sup))
3826           {
3827             int found = 0;
3828             /* let's see if this is an easy supp */
3829             FOR_PROVIDES(p2, pp2, sup)
3830               {
3831                 if (!solv->addalreadyrecommended && solv->installed)
3832                   {
3833                     if (pool->solvables[p2].repo == solv->installed)
3834                       continue;
3835                   }
3836                 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
3837                   {
3838                     queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3839                     queue_push2(whyq, p2, sup);
3840                     found = 1;
3841                   }
3842               }
3843             if (!found)
3844               {
3845                 /* hard case, just note dependency with no package */
3846                 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
3847                 queue_push2(whyq, 0, sup);
3848               }
3849           }
3850       for (i = decisionno; i < solv->decisionq.count; i++)
3851         {
3852           p2 = solv->decisionq.elements[i];
3853           if (p2 > 0)
3854             solv->decisionmap[p2] = -solv->decisionmap[p2];
3855         }
3856     }
3857 }
3858
3859 void
3860 pool_job2solvables(Pool *pool, Queue *pkgs, Id how, Id what)
3861 {
3862   Id p, pp;
3863   how &= SOLVER_SELECTMASK;
3864   queue_empty(pkgs);
3865   if (how == SOLVER_SOLVABLE_ALL)
3866     {
3867       FOR_POOL_SOLVABLES(p)
3868         queue_push(pkgs, p);
3869     }
3870   else if (how == SOLVER_SOLVABLE_REPO)
3871     {
3872       Repo *repo = pool_id2repo(pool, what);
3873       Solvable *s;
3874       if (repo)
3875         FOR_REPO_SOLVABLES(repo, p, s)
3876           queue_push(pkgs, p);
3877     }
3878   else
3879     {
3880       FOR_JOB_SELECT(p, pp, how, what)
3881         queue_push(pkgs, p);
3882     }
3883 }
3884
3885 int
3886 pool_isemptyupdatejob(Pool *pool, Id how, Id what)
3887 {
3888   Id p, pp, pi, pip;
3889   Id select = how & SOLVER_SELECTMASK;
3890   if ((how & SOLVER_JOBMASK) != SOLVER_UPDATE)
3891     return 0;
3892   if (select == SOLVER_SOLVABLE_ALL || select == SOLVER_SOLVABLE_REPO)
3893     return 0;
3894   if (!pool->installed)
3895     return 1;
3896   FOR_JOB_SELECT(p, pp, select, what)
3897     if (pool->solvables[p].repo == pool->installed)
3898       return 0;
3899   /* hard work */
3900   FOR_JOB_SELECT(p, pp, select, what)
3901     {
3902       Solvable *s = pool->solvables + p;
3903       FOR_PROVIDES(pi, pip, s->name)
3904         {
3905           Solvable *si = pool->solvables + pi;
3906           if (si->repo != pool->installed || si->name != s->name)
3907             continue;
3908           return 0;
3909         }
3910       if (s->obsoletes)
3911         {
3912           Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
3913           while ((obs = *obsp++) != 0)
3914             {
3915               FOR_PROVIDES(pi, pip, obs) 
3916                 {
3917                   Solvable *si = pool->solvables + pi;
3918                   if (si->repo != pool->installed)
3919                     continue;
3920                   if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
3921                     continue;
3922                   if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
3923                     continue;
3924                   return 0;
3925                 }
3926             }
3927         }
3928     }
3929   return 1;
3930 }