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